OSDN Git Service

MERGE: リビジョン1818。Parserクラスのコンストラクタを変更。BaseActions派生クラスのメソッド名を変更
[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 1813 2012-05-02 14:41:57Z 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                 /*\r
58                  * NOTE: include needed action class\r
59                  */\r
60                 if ( $action_class != 'Actions' )\r
61                 {\r
62                         if ( !class_exists($action_class, FALSE)\r
63                           && (!file_exists("{$DIR_LIBS}{$action_class}.php")\r
64                            || !include("{$DIR_LIBS}{$action_class}.php")) )\r
65                         {\r
66                                 return;\r
67                         }\r
68                 }\r
69                 else\r
70                 {\r
71                         if ( !class_exists('Actions', FALSE)\r
72                           && (!file_exists("{$DIR_LIBS}ACTIONS.php")\r
73                            || !include("{$DIR_LIBS}ACTIONS.php")) )\r
74                         {\r
75                                 return;\r
76                         }\r
77                 }\r
78                 \r
79                 $this->action_class = $action_class;\r
80                 $this->event_identifier = $event_identifier;\r
81                 \r
82                 // read skin name/description/content type\r
83                 $query = "SELECT * FROM %s WHERE sdnumber=%d;";\r
84                 $query = sprintf($query, sql_table('skin_desc'), $this->id);\r
85                 $res = sql_query($query);\r
86                 $obj = sql_fetch_object($res);\r
87                 \r
88                 $this->valid = (sql_num_rows($res) > 0);\r
89                 if ( !$this->valid )\r
90                 {\r
91                         return;\r
92                 }\r
93                 \r
94                 $this->name = $obj->sdname;\r
95                 $this->description = $obj->sddesc;\r
96                 $this->contentType = $obj->sdtype;\r
97                 $this->includeMode = $obj->sdincmode;\r
98                 $this->includePrefix = $obj->sdincpref;\r
99                 \r
100                 return;\r
101         }\r
102         \r
103         /**\r
104          * Skin::getID()\r
105          * Get SKIN id\r
106          * \r
107          * @param       void\r
108          * @return      integer id for this skin instance\r
109          */\r
110         public function getID()\r
111         {\r
112                 return (integer) $this->id;\r
113         }\r
114         \r
115         /**\r
116          * Skin::isValid()\r
117          * \r
118          * @param       void\r
119          * @return      boolean\r
120          */\r
121         public function isValid()\r
122         {\r
123                 return (boolean) $this->valid;\r
124         }\r
125         \r
126         /**\r
127          * Skin::getName()\r
128          * Get SKIN name\r
129          * \r
130          * @param       void\r
131          * @return      string  name of this skin instance\r
132          */\r
133         public function getName()\r
134         {\r
135                 return (string) $this->name;\r
136         }\r
137         \r
138         /**\r
139          * Skin::getDescription()\r
140          * Get SKIN description\r
141          * \r
142          * @param       void\r
143          * @return      string  description of this skin instance\r
144          */\r
145         public function getDescription()\r
146         {\r
147                 return (string) $this->description;\r
148         }\r
149         \r
150         /**\r
151          * Skin::getContentType()\r
152          * Get SKIN content type\r
153          * e.g. text/xml, text/html, application/atom+xml\r
154          * \r
155          * @param       void\r
156          * @return      string  name of this skin instance\r
157          */\r
158         public function getContentType()\r
159         {\r
160                 return (string) $this->contentType;\r
161         }\r
162         \r
163         /**\r
164          * Skin::getIncludeMode()\r
165          * Get include mode of the SKIN\r
166          * \r
167          * Returns either 'normal' or 'skindir':\r
168          * 'normal': if a all data of the skin can be found in the databse\r
169          * 'skindir': if the skin has data in the it's skin driectory\r
170          * \r
171          * @param       void\r
172          * @return      string  normal/skindir\r
173          */\r
174         public function getIncludeMode()\r
175         {\r
176                 return (string) $this->includeMode;\r
177         }\r
178         \r
179         /**\r
180          * Skin::getIncludePrefix()\r
181          * Get include prefix of the SKIN\r
182          * \r
183          * Get name of the subdirectory (with trailing slash) where\r
184          * the files of the current skin can be found (e.g. 'default/')\r
185          * \r
186          * @param       void\r
187          * @return      string  include prefix of this skin instance\r
188          */\r
189         public function getIncludePrefix()\r
190         {\r
191                 return (string) $this->includePrefix;\r
192         }\r
193         \r
194         /**\r
195          * Skin::exists()\r
196          * Checks if a skin with a given shortname exists\r
197          * \r
198          * @static\r
199          * @param       string  $name   Skin short name\r
200          * @return      integer number of skins with the given ID\r
201          */\r
202         static public function exists($name)\r
203         {\r
204                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";\r
205                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
206                 return (quickQuery($query) > 0);\r
207         }\r
208         \r
209         /**\r
210          * Skin::existsID()\r
211          * Checks if a skin with a given ID exists\r
212          * \r
213          * @static\r
214          * @param       string  $id     Skin ID\r
215          * @return      integer number of skins with the given ID\r
216          */\r
217         static public function existsID($id)\r
218         {\r
219                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";\r
220                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
221                 return (quickQuery($query) > 0);\r
222         }\r
223         \r
224         /**\r
225          * Skin::createFromName()\r
226          * Returns a skin given its shortname\r
227          * \r
228          * @static\r
229          * @param       string  $name   Skin shortname\r
230          * @return      object instance of Skin class\r
231          */\r
232         static public function createFromName($name)\r
233         {\r
234                 return new SKIN(SKIN::getIdFromName($name));\r
235         }\r
236         \r
237         /**\r
238          * Skin::getIdFromName()\r
239          * Returns a skin ID given its shortname\r
240          * \r
241          * @static\r
242          * @param       string  $name   Skin shortname\r
243          * @return      integer Skin ID\r
244          */\r
245         static public function getIdFromName($name)\r
246         {\r
247                 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";\r
248                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
249                 $res = sql_query($query);\r
250                 $obj = sql_fetch_object($res);\r
251                 return $obj->sdnumber;\r
252         }\r
253         \r
254         /**\r
255          * Skin::getNameFromId()\r
256          * Returns a skin shortname given its ID\r
257          * \r
258          * @static\r
259          * @param       string  $name\r
260          * @return      string  Skin short name\r
261          */\r
262         static public function getNameFromId($id)\r
263         {\r
264                 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";\r
265                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
266                 return quickQuery($query);\r
267         }\r
268         \r
269         /**\r
270          * SKIN::createNew()\r
271          * Creates a new skin, with the given characteristics.\r
272          *\r
273          * @static\r
274          * @param       String  $name   value for nucleus_skin.sdname\r
275          * @param       String  $desc   value for nucleus_skin.sddesc\r
276          * @param       String  $type   value for nucleus_skin.sdtype\r
277          * @param       String  $includeMode    value for nucleus_skin.sdinclude\r
278          * @param       String  $includePrefix  value for nucleus_skin.sdincpref\r
279          * @return      Integer ID for just inserted record\r
280          */\r
281         public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
282         {\r
283                 global $manager;\r
284                 \r
285                 $data = array(
286                                 'name' => &$name,\r
287                                 'description' => &$desc,\r
288                                 'type' => &$type,\r
289                                 'includeMode' => &$includeMode,\r
290                                 'includePrefix' => &$includePrefix\r
291                 );\r
292                 $manager->notify('PreAddSkin', $data);
293                 \r
294                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";\r
295                 $sdname         = sql_real_escape_string($name);\r
296                 $sddesc         = sql_real_escape_string($desc);\r
297                 $sdtype         = sql_real_escape_string($type);\r
298                 $sdincmode      = sql_real_escape_string($includeMode);\r
299                 $sdincpref      = sql_real_escape_string($includePrefix);\r
300                 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);\r
301                 sql_query($query);\r
302                 $newid = sql_insert_id();\r
303                 \r
304                 $data = array(
305                                 'skinid'                => $newid,\r
306                                 'name'                  => $name,\r
307                                 'description'   => $desc,\r
308                                 'type'                  => $type,\r
309                                 'includeMode'   => $includeMode,\r
310                                 'includePrefix' => $includePrefix\r
311                 );\r
312                 $manager->notify('PostAddSkin', $data);
313                 
314                 return $newid;\r
315         }\r
316         \r
317         /**\r
318          * Skin::parse()\r
319          * Parse a SKIN\r
320          * \r
321          * @param       string  $type\r
322          * @param       string  $path   path to file if using fileparser\r
323          * @return      void\r
324          */\r
325         public function parse($type, $path='')\r
326         {\r
327                 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;\r
328                 \r
329                 $data = array(
330                         'skin' => &$this,
331                         'type' => $type
332                 );
333                 $manager->notify("Init{$this->event_identifier}Parse", $data);
334                 \r
335                 // include skin locale file for <%text%> tag if useable\r
336                 $this->includeTranslation();\r
337                 \r
338                 // set output type\r
339                 sendContentType($this->getContentType(), 'skin');\r
340                 \r
341                 /* FIX: should be obsoleted */\r
342                 $currentSkinName = $this->getName();\r
343                 \r
344                 // retrieve contents\r
345                 $contents = FALSE;\r
346                 if ( $type != 'fileparse' )\r
347                 {\r
348                         $contents = $this->getContentFromDB($type);\r
349                 }\r
350                 else if ( $path !== ''  && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )\r
351                 {\r
352                         $contents = $this->getContentFromFile($path);\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                 $handler->setSkin($this);\r
381                 \r
382                 // register action handler to parser\r
383                 $parser = new Parser($handler);\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, sql_real_escape_string($skintype));\r
405                 $res = sql_query($query);\r
406                 \r
407                 if ( sql_num_rows($res) == 0 )\r
408                 {\r
409                         return FALSE;\r
410                 }\r
411                 \r
412                 return sql_result($res, 0, 0);\r
413         }\r
414         \r
415         /**\r
416          * Skin::getContentFromFile()\r
417          * \r
418          * @param       string  $fullpath       fullpath to the file to parse\r
419          * @return      mixed   file contents or FALSE\r
420          */\r
421         public function getContentFromFile($fullpath)\r
422         {\r
423                 $fsize = filesize($fullpath);\r
424                 if ( $fsize <= 0 )\r
425                 {\r
426                         return;\r
427                 }\r
428                 \r
429                 $fd = fopen ($fullpath, 'r');\r
430                 if ( $fd === FALSE )\r
431                 {\r
432                         return FALSE;\r
433                 }\r
434                 \r
435                 $contents = fread ($fd, $fsize);\r
436                 if ( $contents === FALSE )\r
437                 {\r
438                         return FALSE;\r
439                 }\r
440                 \r
441                 fclose ($fd);\r
442                 return $contents;\r
443         }\r
444         \r
445         /**\r
446          * SKIN::update()\r
447          * Updates the contents for one part of the skin in the database\r
448          * \r
449          * @param       string  $type type of the skin part (e.g. index, item, search ...) \r
450          * @param       string  $content new content for this skin part\r
451          * @return      void\r
452          * \r
453          */\r
454         public function update($type, $content)\r
455         {\r
456                 global $manager;\r
457                 \r
458                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";\r
459                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
460                 $res = sql_query($query);\r
461                 \r
462                 $skintypeexists = sql_fetch_object($res);\r
463                 $skintypevalue = ($content == true);\r
464                 \r
465                 if( $skintypevalue && $skintypeexists )\r
466                 {\r
467                         $data = array(\r
468                                 'skinid'        =>  $this->id,\r
469                                 'type'          =>  $type,\r
470                                 'content'       => &$content\r
471                         );\r
472                         \r
473                         // PreUpdateSkinPart event\r
474                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);\r
475                 }\r
476                 else if( $skintypevalue && !$skintypeexists )\r
477                 {\r
478                         $data = array(\r
479                                 'skinid' => $this->id,\r
480                                 'type' => $type,\r
481                                 'content' => &$content\r
482                         );\r
483                         \r
484                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);\r
485                 }\r
486                 else if( !$skintypevalue && $skintypeexists )\r
487                 {\r
488                         $data = array(\r
489                                 'skinid' => $this->id,\r
490                                 'type' => $type\r
491                         );\r
492                         \r
493                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);\r
494                 }\r
495                 \r
496                 // delete old thingie\r
497                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";\r
498                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
499                 sql_query($query);\r
500                 \r
501                 // write new thingie\r
502                 if ( $content )\r
503                 {\r
504                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";\r
505                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);\r
506                         sql_query($query);\r
507                 }\r
508                 \r
509                 if( $skintypevalue && $skintypeexists )\r
510                 {\r
511                         $data = array(\r
512                                 'skinid'        => $this->id,\r
513                                 'type'          => $type,\r
514                                 'content'       => &$content\r
515                         );\r
516                         \r
517                         // PostUpdateSkinPart event\r
518                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);\r
519                 }\r
520                 else if( $skintypevalue && (!$skintypeexists) )\r
521                 {\r
522                         $data = array(\r
523                                 'skinid'        => $this->id,\r
524                                 'type'          => $type,\r
525                                 'content'       => &$content\r
526                         );\r
527                         \r
528                         // PostAddSkinPart event\r
529                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);\r
530                 }\r
531                 else if( (!$skintypevalue) && $skintypeexists )\r
532                 {\r
533                         $data = array(\r
534                                 'skinid'        => $this->id,\r
535                                 'type'          => $type\r
536                         );\r
537                         \r
538                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);\r
539                 }\r
540                 return;\r
541         }\r
542         \r
543         /**\r
544          * Skin::deleteAllParts()\r
545          * Deletes all skin parts from the database\r
546          * \r
547          * @param       void\r
548          * @return      void\r
549          */\r
550         public function deleteAllParts()\r
551         {\r
552                 $query = "DELETE FROM %s WHERE sdesc=%d;";\r
553                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
554                 sql_query($query);\r
555         }\r
556         \r
557         /**\r
558          * Skin::updateGeneralInfo()\r
559          * Updates the general information about the skin\r
560          * \r
561          * @param       string  $name                   name of the skin\r
562          * @param       string  $desc                   description of the skin\r
563          * @param       string  $type                   type of the skin\r
564          * @param       string  $includeMode    include mode of the skin\r
565          * @param       string  $includePrefix  include prefix of the skin\r
566          * @return      void\r
567          */\r
568         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
569         {\r
570                 $name                   = sql_real_escape_string($name);\r
571                 $desc                   = sql_real_escape_string($desc);\r
572                 $type                   = sql_real_escape_string($type);\r
573                 $includeMode    = sql_real_escape_string($includeMode);\r
574                 $includePrefix  = sql_real_escape_string($includePrefix);\r
575                 \r
576                 $query ="UPDATE %s SET sdname='%s', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d";\r
577                 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);\r
578                 \r
579                 sql_query($query);\r
580                 return;\r
581         }\r
582         \r
583         /**\r
584          * Skin::includeTranslation()\r
585          * \r
586          * @param       void\r
587          * @return      void\r
588          */\r
589         private function includeTranslation()\r
590         {\r
591                 global $DIR_SKINS;\r
592                 \r
593                 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();\r
594                 \r
595                 if( $this->includeMode == "normal" )\r
596                 {\r
597                         $filename = "./locales/{$locale}.php";\r
598                 }\r
599                 else if( $this->includeMode == "skindir" )\r
600                 {\r
601                         if ( $this->includePrefix == '' )\r
602                         {\r
603                                 $filename = "{$DIR_SKINS}locales/{$locale}.php";\r
604                         }\r
605                         else\r
606                         {\r
607                                 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";\r
608                         }\r
609                 }\r
610                 else\r
611                 {\r
612                         return;\r
613                 }\r
614                 \r
615                 if ( !file_exists($filename) )\r
616                 {\r
617                         return;\r
618                 }\r
619                 \r
620                 include_once($filename);\r
621                 \r
622                 return;\r
623         }\r
624         \r
625         /**\r
626          * Skin::getDefaultTypes()\r
627          * \r
628          * @param       string  void\r
629          * @return      array   default skin types\r
630          */\r
631         public function getDefaultTypes()\r
632         {\r
633                 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));\r
634         }\r
635         \r
636         /**\r
637          * Skin::getAvailableTypes()\r
638          * \r
639          * @param       string  void\r
640          * @return      array   registered skin types\r
641          */\r
642         public function getAvailableTypes()\r
643         {\r
644                 $default_skintypes = $this->getDefaultTypes();\r
645                 $query = "SELECT stype FROM %s WHERE sdesc=%d;";\r
646                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
647                 \r
648                 /* NOTE: force to put default types in the beginning */\r
649                 $in_default = array();\r
650                 $no_default = array();\r
651                 \r
652                 $res = sql_query($query);\r
653                 while ( $row = sql_fetch_array($res) )\r
654                 {\r
655                         if ( !array_key_exists($row['stype'], $default_skintypes) )\r
656                         {\r
657                                 $no_default[$row['stype']] = FALSE;\r
658                         }\r
659                         else\r
660                         {\r
661                                 $in_default[$row['stype']] = $default_skintypes[$row['stype']];\r
662                         }\r
663                 }\r
664                 \r
665                 return array_merge($in_default, $no_default);\r
666         }\r
667         \r
668         /**\r
669          * Skin::getAllowedActionsForType()\r
670          * Get the allowed actions for a skin type\r
671          * returns an array with the allowed actions\r
672          * \r
673          * @param       string  $skintype       type of the skin\r
674          * @return      array   allowed action types\r
675          */\r
676         public function getAllowedActionsForType($skintype)\r
677         {\r
678                 $handler = new $this->action_class($skintype);\r
679                 return $handler->getAvailableActions();\r
680         }\r
681         \r
682 }\r