OSDN Git Service

FIX: Skin::update()においてイベント発生条件が不適切だったバグの修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / SKIN.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2009 The Nucleus Group\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  * (see nucleus/documentation/index.html#license for more info)\r
11  */\r
12 /**\r
13  * Class representing a skin\r
14  *\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
17  * @version $Id: SKIN.php 1816 2012-05-03 01:40:10Z sakamocchi $\r
18  */\r
19 \r
20 if ( !function_exists('requestVar') )\r
21 {\r
22         exit;\r
23 }\r
24 \r
25 class Skin\r
26 {\r
27         // after creating a SKIN object, evaluates to true when the skin exists\r
28         private $valid;\r
29         \r
30         // skin characteristics. Use the getXXX methods rather than accessing directly\r
31         private $id;\r
32         private $description;\r
33         private $contentType;\r
34         private $includeMode;           // either 'normal' or 'skindir'\r
35         private $includePrefix;\r
36         private $name;\r
37         \r
38         /* action class */\r
39         private $action_class;\r
40         private $event_identifier;\r
41         \r
42         /**\r
43          * Skin::__construct()\r
44          * Constructor for a new SKIN object\r
45          * \r
46          * @param       integer $id                                     id of the skin\r
47          * @param       string  $action_class           name of class extended from BaseActions\r
48          * @param       string  $event_identifier       event identifier. for example, InitAdminSkinParse if AdminSkin is used\r
49          * @return      void\r
50          */\r
51         public function __construct($id, $action_class='Actions', $event_identifier='Skin')\r
52         {\r
53                 global $DIR_LIBS;\r
54                 \r
55                 $this->id = (integer) $id;\r
56                 \r
57                 /* NOTE: include needed action class */\r
58                 if ( $action_class != 'Actions' )\r
59                 {\r
60                         if ( !class_exists($action_class, FALSE)\r
61                           && (!file_exists("{$DIR_LIBS}{$action_class}.php")\r
62                            || !include("{$DIR_LIBS}{$action_class}.php")) )\r
63                         {\r
64                                 return;\r
65                         }\r
66                 }\r
67                 else\r
68                 {\r
69                         if ( !class_exists('Actions', FALSE)\r
70                           && (!file_exists("{$DIR_LIBS}ACTIONS.php")\r
71                            || !include("{$DIR_LIBS}ACTIONS.php")) )\r
72                         {\r
73                                 return;\r
74                         }\r
75                 }\r
76                 \r
77                 $this->action_class = $action_class;\r
78                 $this->event_identifier = $event_identifier;\r
79                 \r
80                 // read skin name/description/content type\r
81                 $query = "SELECT * FROM %s WHERE sdnumber=%d;";\r
82                 $query = sprintf($query, sql_table('skin_desc'), $this->id);\r
83                 $res = DB::getRow($query);\r
84                 \r
85                 $this->valid = !empty($res);\r
86                 if ( $this->valid )\r
87                 {                       $this->name = $res['sdname'];\r
88                         $this->description = $res['sddesc'];\r
89                         $this->contentType = $res['sdtype'];\r
90                         $this->includeMode = $res['sdincmode'];\r
91                         $this->includePrefix = $res['sdincpref'];\r
92                 }\r
93                 \r
94                 \r
95                 return;\r
96         }\r
97         \r
98         /**\r
99          * Skin::getID()\r
100          * Get SKIN id\r
101          * \r
102          * @param       void\r
103          * @return      integer id for this skin instance\r
104          */\r
105         public function getID()\r
106         {\r
107                 return (integer) $this->id;\r
108         }\r
109         \r
110         /**\r
111          * Skin::isValid()\r
112          * \r
113          * @param       void\r
114          * @return      boolean\r
115          */\r
116         public function isValid()\r
117         {\r
118                 return (boolean) $this->valid;\r
119         }\r
120         \r
121         /**\r
122          * Skin::getName()\r
123          * Get SKIN name\r
124          * \r
125          * @param       void\r
126          * @return      string  name of this skin instance\r
127          */\r
128         public function getName()\r
129         {\r
130                 return (string) $this->name;\r
131         }\r
132         \r
133         /**\r
134          * Skin::getDescription()\r
135          * Get SKIN description\r
136          * \r
137          * @param       void\r
138          * @return      string  description of this skin instance\r
139          */\r
140         public function getDescription()\r
141         {\r
142                 return (string) $this->description;\r
143         }\r
144         \r
145         /**\r
146          * Skin::getContentType()\r
147          * Get SKIN content type\r
148          * e.g. text/xml, text/html, application/atom+xml\r
149          * \r
150          * @param       void\r
151          * @return      string  name of this skin instance\r
152          */\r
153         public function getContentType()\r
154         {\r
155                 return (string) $this->contentType;\r
156         }\r
157         \r
158         /**\r
159          * Skin::getIncludeMode()\r
160          * Get include mode of the SKIN\r
161          * \r
162          * Returns either 'normal' or 'skindir':\r
163          * 'normal': if a all data of the skin can be found in the databse\r
164          * 'skindir': if the skin has data in the it's skin driectory\r
165          * \r
166          * @param       void\r
167          * @return      string  normal/skindir\r
168          */\r
169         public function getIncludeMode()\r
170         {\r
171                 return (string) $this->includeMode;\r
172         }\r
173         \r
174         /**\r
175          * Skin::getIncludePrefix()\r
176          * Get include prefix of the SKIN\r
177          * \r
178          * Get name of the subdirectory (with trailing slash) where\r
179          * the files of the current skin can be found (e.g. 'default/')\r
180          * \r
181          * @param       void\r
182          * @return      string  include prefix of this skin instance\r
183          */\r
184         public function getIncludePrefix()\r
185         {\r
186                 return (string) $this->includePrefix;\r
187         }\r
188         \r
189         /**\r
190          * Skin::exists()\r
191          * Checks if a skin with a given shortname exists\r
192          * \r
193          * @static\r
194          * @param       string  $name   Skin short name\r
195          * @return      integer number of skins with the given ID\r
196          */\r
197         static public function exists($name)\r
198         {\r
199                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";\r
200                 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));\r
201                 return (DB::getValue($query) > 0);\r
202         }\r
203         \r
204         /**\r
205          * Skin::existsID()\r
206          * Checks if a skin with a given ID exists\r
207          * \r
208          * @static\r
209          * @param       string  $id     Skin ID\r
210          * @return      integer number of skins with the given ID\r
211          */\r
212         static public function existsID($id)\r
213         {\r
214                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";\r
215                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
216                 return (DB::getValue($query) > 0);\r
217         }\r
218         \r
219         /**\r
220          * Skin::createFromName()\r
221          * Returns a skin given its shortname\r
222          * \r
223          * @static\r
224          * @param       string  $name   Skin shortname\r
225          * @return      object instance of Skin class\r
226          */\r
227         static public function createFromName($name)\r
228         {\r
229                 return new SKIN(SKIN::getIdFromName($name));\r
230         }\r
231         \r
232         /**\r
233          * Skin::getIdFromName()\r
234          * Returns a skin ID given its shortname\r
235          * \r
236          * @static\r
237          * @param       string  $name   Skin shortname\r
238          * @return      integer Skin ID\r
239          */\r
240         static public function getIdFromName($name)\r
241         {\r
242                 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";\r
243                 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));\r
244                 return DB::getValue($query);\r
245         }\r
246         \r
247         /**\r
248          * Skin::getNameFromId()\r
249          * Returns a skin shortname given its ID\r
250          * \r
251          * @static\r
252          * @param       string  $name\r
253          * @return      string  Skin short name\r
254          */\r
255         static public function getNameFromId($id)\r
256         {\r
257                 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";\r
258                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
259                 return DB::getValue($query);\r
260         }\r
261         \r
262         /**\r
263          * SKIN::createNew()\r
264          * Creates a new skin, with the given characteristics.\r
265          *\r
266          * @static\r
267          * @param       String  $name   value for nucleus_skin.sdname\r
268          * @param       String  $desc   value for nucleus_skin.sddesc\r
269          * @param       String  $type   value for nucleus_skin.sdtype\r
270          * @param       String  $includeMode    value for nucleus_skin.sdinclude\r
271          * @param       String  $includePrefix  value for nucleus_skin.sdincpref\r
272          * @return      Integer ID for just inserted record\r
273          */\r
274         public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
275         {\r
276                 global $manager;\r
277                 \r
278                 $data = array(\r
279                         'name'                  => &$name,\r
280                         'description'   => &$desc,\r
281                         'type'                  => &$type,\r
282                         'includeMode'   => &$includeMode,\r
283                         'includePrefix' => &$includePrefix\r
284                 );\r
285                 $manager->notify('PreAddSkin', $data);
286                 \r
287                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";\r
288                 $sdname         = DB::quoteValue($name);\r
289                 $sddesc         = DB::quoteValue($desc);\r
290                 $sdtype         = DB::quoteValue($type);\r
291                 $sdincmode      = DB::quoteValue($includeMode);\r
292                 $sdincpref      = DB::quoteValue($includePrefix);\r
293                 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);\r
294                 DB::execute($query);\r
295                 $newid = DB::getInsertId();\r
296                 \r
297                 $data = array(\r
298                         'skinid'                => $newid,\r
299                         'name'                  => $name,\r
300                         'description'   => $desc,\r
301                         'type'                  => $type,\r
302                         'includeMode'   => $includeMode,\r
303                         'includePrefix' => $includePrefix\r
304                 );\r
305                 $manager->notify('PostAddSkin', $data);
306                 
307                 return $newid;\r
308         }\r
309         \r
310         /**\r
311          * Skin::parse()\r
312          * Parse a SKIN\r
313          * \r
314          * @param       string  $type\r
315          * @param       string  $path   path to file if using fileparser\r
316          * @return      void\r
317          */\r
318         public function parse($type, $path='')\r
319         {\r
320                 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;\r
321                 \r
322                 $data = array(
323                         'skin' => &$this,
324                         'type' => $type
325                 );
326                 $manager->notify("Init{$this->event_identifier}Parse", $data);
327                 \r
328                 // include skin locale file for <%text%> tag if useable\r
329                 $this->includeTranslation();\r
330                 \r
331                 // set output type\r
332                 sendContentType($this->getContentType(), 'skin');\r
333                 \r
334                 /* FIX: should be obsoleted */\r
335                 $currentSkinName = $this->getName();\r
336                 \r
337                 // retrieve contents\r
338                 $contents = FALSE;\r
339                 if ( $type == 'pluginadmin' )\r
340                 {\r
341                         $contents = $path;\r
342                 }\r
343                 else\r
344                 {\r
345                         if ( $type != 'fileparse' && $type != 'importAdmin')\r
346                         {\r
347                                 $contents = $this->getContentFromDB($type);\r
348                         }\r
349                         else if ( $path !== ''  && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )\r
350                         {\r
351                                 $contents = $this->getContentFromFile($path);\r
352                         }\r
353                 }\r
354                 // use base skin if this skin does not have contents\r
355                 if ( $contents === FALSE )\r
356                 {\r
357                         $defskin = new SKIN($CONF['BaseSkin']);\r
358                         $contents = $defskin->getContentFromDB($type);\r
359                         if ( !$contents )\r
360                         {\r
361                                 echo _ERROR_SKIN;\r
362                                 return;\r
363                         }\r
364                 }\r
365                 \r
366                 $data = array(
367                         'skin'          => &$this,
368                         'type'          => $type,
369                         'contents'      => &$contents
370                 );
371                 $manager->notify("Pre{$this->event_identifier}Parse", $data);
372                 \r
373                 // set IncludeMode properties of parser\r
374                 Parser::setProperty('IncludeMode', $this->getIncludeMode());\r
375                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());\r
376                 \r
377                 // call action handler\r
378                 $action_class = $this->action_class;\r
379                 $handler = new $action_class($type);\r
380                 \r
381                 // register action handler to parser\r
382                 $parser = new Parser($handler);\r
383                 $parser->setSkin($this);\r
384                 $parser->parse($contents);\r
385                 \r
386                 $data = array(
387                         'skin' => &$this,
388                         'type' => $type
389                 );
390                 $manager->notify("Post{$this->event_identifier}Parse", $data);
391                 
392                 return;\r
393         }\r
394         \r
395         /**\r
396          * Skin::getContentFromDB()\r
397          * \r
398          * @param       string  $skintype       skin type\r
399          * @return      string  content for the skin type\r
400          */\r
401         public function getContentFromDB($skintype)\r
402         {\r
403                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";\r
404                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));\r
405                 $res = DB::getValue($query);\r
406                 \r
407                 return $res ? $res : '';\r
408         }\r
409         \r
410         /**\r
411          * Skin::getContentFromFile()\r
412          * \r
413          * @param       string  $fullpath       fullpath to the file to parse\r
414          * @return      mixed   file contents or FALSE\r
415          */\r
416         public function getContentFromFile($fullpath)\r
417         {\r
418                 $fsize = filesize($fullpath);\r
419                 if ( $fsize <= 0 )\r
420                 {\r
421                         return;\r
422                 }\r
423                 \r
424                 $fd = fopen ($fullpath, 'r');\r
425                 if ( $fd === FALSE )\r
426                 {\r
427                         return FALSE;\r
428                 }\r
429                 \r
430                 $contents = fread ($fd, $fsize);\r
431                 if ( $contents === FALSE )\r
432                 {\r
433                         return FALSE;\r
434                 }\r
435                 \r
436                 fclose ($fd);\r
437                 return $contents;\r
438         }\r
439         \r
440         /**\r
441          * SKIN::update()\r
442          * Updates the contents for one part of the skin in the database\r
443          * \r
444          * @param       string  $type type of the skin part (e.g. index, item, search ...) \r
445          * @param       string  $content new content for this skin part\r
446          * @return      void\r
447          * \r
448          */\r
449         public function update($type, $content)\r
450         {\r
451                 global $manager;\r
452                 \r
453                 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";\r
454                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);\r
455                 $res = DB::getValue($query);\r
456                 \r
457                 $skintypeexists = !empty($res);\r
458                 $skintypevalue = !empty($content);\r
459                 \r
460                 if( $skintypevalue && $skintypeexists )\r
461                 {\r
462                         $data = array(\r
463                                 'skinid'        =>  $this->id,\r
464                                 'type'          =>  $type,\r
465                                 'content'       => &$content\r
466                         );\r
467                         $manager->notify("PreUpdate{$this->event_identifier}Part", $data);\r
468                 }\r
469                 else if( $skintypevalue )\r
470                 {\r
471                         $data = array(\r
472                                 'skinid'        =>  $this->id,\r
473                                 'type'          =>  $type,\r
474                                 'content'       => &$content\r
475                         );\r
476                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);\r
477                 }\r
478                 else if( $skintypeexists )\r
479                 {\r
480                         $data = array(\r
481                                 'skinid'        => $this->id,\r
482                                 'type'          => $type\r
483                         );\r
484                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);\r
485                 }\r
486                 \r
487                 // delete old thingie\r
488                 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d;";\r
489                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);\r
490                 DB::execute($query);\r
491                 \r
492                 // write new thingie\r
493                 if ( $content )\r
494                 {\r
495                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d);";\r
496                         $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);\r
497                         DB::execute($query);\r
498                 }\r
499                 \r
500                 if( $skintypevalue && $skintypeexists )\r
501                 {\r
502                         $data = array(\r
503                                 'skinid'        =>  $this->id,\r
504                                 'type'          =>  $type,\r
505                                 'content'       => &$content\r
506                         );\r
507                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);\r
508                 }\r
509                 else if( $skintypevalue )\r
510                 {\r
511                         $data = array(\r
512                                 'skinid'        =>  $this->id,\r
513                                 'type'          =>  $type,\r
514                                 'content'       => &$content\r
515                         );\r
516                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);\r
517                 }\r
518                 else if( $skintypeexists )\r
519                 {\r
520                         $data = array(\r
521                                 'skinid'        => $this->id,\r
522                                 'type'          => $type\r
523                         );\r
524                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);\r
525                 }\r
526                 return;\r
527         }\r
528         \r
529         /**\r
530          * Skin::deleteAllParts()\r
531          * Deletes all skin parts from the database\r
532          * \r
533          * @param       void\r
534          * @return      void\r
535          */\r
536         public function deleteAllParts()\r
537         {\r
538                 $query = "DELETE FROM %s WHERE sdesc=%d;";\r
539                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
540                 DB::execute($query);\r
541         }\r
542         \r
543         /**\r
544          * Skin::updateGeneralInfo()\r
545          * Updates the general information about the skin\r
546          * \r
547          * @param       string  $name                   name of the skin\r
548          * @param       string  $desc                   description of the skin\r
549          * @param       string  $type                   type of the skin\r
550          * @param       string  $includeMode    include mode of the skin\r
551          * @param       string  $includePrefix  include prefix of the skin\r
552          * @return      void\r
553          */\r
554         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
555         {\r
556                 $name                   = DB::quoteValue($name);\r
557                 $desc                   = DB::quoteValue($desc);\r
558                 $type                   = DB::quoteValue($type);\r
559                 $includeMode    = DB::quoteValue($includeMode);\r
560                 $includePrefix  = DB::quoteValue($includePrefix);\r
561                 \r
562                 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";\r
563                 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);\r
564                 \r
565                 DB::execute($query);\r
566                 return;\r
567         }\r
568         \r
569         /**\r
570          * Skin::includeTranslation()\r
571          * \r
572          * @param       void\r
573          * @return      void\r
574          */\r
575         private function includeTranslation()\r
576         {\r
577                 global $DIR_SKINS;\r
578                 \r
579                 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();\r
580                 \r
581                 if( $this->includeMode == "normal" )\r
582                 {\r
583                         $filename = "./locales/{$locale}.php";\r
584                 }\r
585                 else if( $this->includeMode == "skindir" )\r
586                 {\r
587                         if ( $this->includePrefix == '' )\r
588                         {\r
589                                 $filename = "{$DIR_SKINS}locales/{$locale}.php";\r
590                         }\r
591                         else\r
592                         {\r
593                                 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";\r
594                         }\r
595                 }\r
596                 else\r
597                 {\r
598                         return;\r
599                 }\r
600                 \r
601                 if ( !file_exists($filename) )\r
602                 {\r
603                         return;\r
604                 }\r
605                 \r
606                 include_once($filename);\r
607                 \r
608                 return;\r
609         }\r
610         \r
611         /**\r
612          * Skin::getDefaultTypes()\r
613          * \r
614          * @param       string  void\r
615          * @return      array   default skin types\r
616          */\r
617         public function getDefaultTypes()\r
618         {\r
619                 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));\r
620         }\r
621         \r
622         /**\r
623          * Skin::getAvailableTypes()\r
624          * \r
625          * @param       string  void\r
626          * @return      array   registered skin types\r
627          */\r
628         public function getAvailableTypes()\r
629         {\r
630                 $default_skintypes = $this->getDefaultTypes();\r
631                 $query = "SELECT stype FROM %s WHERE sdesc=%d;";\r
632                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
633                 \r
634                 /* NOTE: force to put default types in the beginning */\r
635                 $in_default = array();\r
636                 $no_default = array();\r
637                 \r
638                 $res = DB::getResult($query);\r
639                 foreach ( $res as $row )\r
640                 {\r
641                         if ( !array_key_exists($row['stype'], $default_skintypes) )\r
642                         {\r
643                                 $no_default[$row['stype']] = FALSE;\r
644                         }\r
645                         else\r
646                         {\r
647                                 $in_default[$row['stype']] = $default_skintypes[$row['stype']];\r
648                         }\r
649                 }\r
650                 \r
651                 return array_merge($in_default, $no_default);\r
652         }\r
653         \r
654         /**\r
655          * Skin::getAllowedActionsForType()\r
656          * Get the allowed actions for a skin type\r
657          * returns an array with the allowed actions\r
658          * @return      array   allowed action types\r
659          * @param       string  $skintype       type of the skin\r
660          * @return      array   allowed action types\r
661          */\r
662         public function getAllowedActionsForType($skintype)\r
663         {\r
664                 $handler = new $this->action_class($skintype);\r
665                 return $handler->getAvailableActions();\r
666         }\r
667         \r
668 }\r