OSDN Git Service

MERGE: リビジョン1758〜1766までのマージと。form用テンプレートに使えるタグ種の変更
[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 1755 2012-04-14 10:05:49Z 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                 // read skin name/description/content type\r
58                 $query = "SELECT * FROM %s WHERE sdnumber=%d";\r
59                 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);\r
60                 $res = sql_query($query);\r
61                 $obj = sql_fetch_object($res);\r
62                 $this->valid = (sql_num_rows($res) > 0);\r
63                 if ( !$this->valid )\r
64                 {\r
65                         return;\r
66                 }\r
67                 \r
68                 /*\r
69                  * NOTE: include needed action class\r
70                  */\r
71                 if ( $action_class != 'Actions' )\r
72                 {\r
73                         if ( !class_exists($action_class, FALSE)\r
74                           && (!file_exists("{$DIR_LIBS}{$action_class}.php")\r
75                            || !include("{$DIR_LIBS}{$action_class}.php")) )\r
76                         {\r
77                                 return;\r
78                         }\r
79                 }\r
80                 else\r
81                 {\r
82                         if ( !class_exists('Actions', FALSE)\r
83                           && (!file_exists("{$DIR_LIBS}ACTIONS.php")\r
84                            || !include("{$DIR_LIBS}ACTIONS.php")) )\r
85                         {\r
86                                 return;\r
87                         }\r
88                 }\r
89                 \r
90                 $this->action_class = $action_class;\r
91                 $this->event_identifier = $event_identifier;\r
92                 \r
93                 $this->name = $obj->sdname;\r
94                 $this->description = $obj->sddesc;\r
95                 $this->contentType = $obj->sdtype;\r
96                 $this->includeMode = $obj->sdincmode;\r
97                 $this->includePrefix = $obj->sdincpref;\r
98                 \r
99                 return;\r
100         }\r
101         \r
102         /**\r
103          * Skin::getID()\r
104          * Get SKIN id\r
105          * \r
106          * @param       void\r
107          * @return      integer id for this skin instance\r
108          */\r
109         public function getID()\r
110         {\r
111                 return (integer) $this->id;\r
112         }\r
113         \r
114         /**\r
115          * Skin::isValid()\r
116          * \r
117          * @param       void\r
118          * @return      boolean\r
119          */\r
120         public function isValid()\r
121         {\r
122                 return (boolean) $this->valid;\r
123         }\r
124         \r
125         /**\r
126          * Skin::getName()\r
127          * Get SKIN name\r
128          * \r
129          * @param       void\r
130          * @return      string  name of this skin instance\r
131          */\r
132         public function getName()\r
133         {\r
134                 return (string) $this->name;\r
135         }\r
136         \r
137         /**\r
138          * Skin::getDescription()\r
139          * Get SKIN description\r
140          * \r
141          * @param       void\r
142          * @return      string  description of this skin instance\r
143          */\r
144         public function getDescription()\r
145         {\r
146                 return (string) $this->description;\r
147         }\r
148         \r
149         /**\r
150          * Skin::getContentType()\r
151          * Get SKIN content type\r
152          * e.g. text/xml, text/html, application/atom+xml\r
153          * \r
154          * @param       void\r
155          * @return      string  name of this skin instance\r
156          */\r
157         public function getContentType()\r
158         {\r
159                 return (string) $this->contentType;\r
160         }\r
161         \r
162         /**\r
163          * Skin::getIncludeMode()\r
164          * Get include mode of the SKIN\r
165          * \r
166          * Returns either 'normal' or 'skindir':\r
167          * 'normal': if a all data of the skin can be found in the databse\r
168          * 'skindir': if the skin has data in the it's skin driectory\r
169          * \r
170          * @param       void\r
171          * @return      string  normal/skindir\r
172          */\r
173         public function getIncludeMode()\r
174         {\r
175                 return (string) $this->includeMode;\r
176         }\r
177         \r
178         /**\r
179          * Skin::getIncludePrefix()\r
180          * Get include prefix of the SKIN\r
181          * \r
182          * Get name of the subdirectory (with trailing slash) where\r
183          * the files of the current skin can be found (e.g. 'default/')\r
184          * \r
185          * @param       void\r
186          * @return      string  include prefix of this skin instance\r
187          */\r
188         public function getIncludePrefix()\r
189         {\r
190                 return (string) $this->includePrefix;\r
191         }\r
192         \r
193         /**\r
194          * Skin::exists()\r
195          * Checks if a skin with a given shortname exists\r
196          * \r
197          * @static\r
198          * @param       string  $name   Skin short name\r
199          * @return      integer number of skins with the given ID\r
200          */\r
201         static public function exists($name)\r
202         {\r
203                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";\r
204                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
205                 return (quickQuery($query) > 0);\r
206         }\r
207         \r
208         /**\r
209          * Skin::existsID()\r
210          * Checks if a skin with a given ID exists\r
211          * \r
212          * @static\r
213          * @param       string  $id     Skin ID\r
214          * @return      integer number of skins with the given ID\r
215          */\r
216         static public function existsID($id)\r
217         {\r
218                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";\r
219                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
220                 return (quickQuery($query) > 0);\r
221         }\r
222         \r
223         /**\r
224          * Skin::createFromName()\r
225          * Returns a skin given its shortname\r
226          * \r
227          * @static\r
228          * @param       string  $name   Skin shortname\r
229          * @return      object instance of Skin class\r
230          */\r
231         static public function createFromName($name)\r
232         {\r
233                 return new SKIN(SKIN::getIdFromName($name));\r
234         }\r
235         \r
236         /**\r
237          * Skin::getIdFromName()\r
238          * Returns a skin ID given its shortname\r
239          * \r
240          * @static\r
241          * @param       string  $name   Skin shortname\r
242          * @return      integer Skin ID\r
243          */\r
244         static public function getIdFromName($name)\r
245         {\r
246                 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";\r
247                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
248                 $res = sql_query($query);\r
249                 $obj = sql_fetch_object($res);\r
250                 return $obj->sdnumber;\r
251         }\r
252         \r
253         /**\r
254          * Skin::getNameFromId()\r
255          * Returns a skin shortname given its ID\r
256          * \r
257          * @static\r
258          * @param       string  $name\r
259          * @return      string  Skin short name\r
260          */\r
261         static public function getNameFromId($id)\r
262         {\r
263                 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";\r
264                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
265                 return quickQuery($query);\r
266         }\r
267         \r
268         /**\r
269          * SKIN::createNew()\r
270          * Creates a new skin, with the given characteristics.\r
271          *\r
272          * @static\r
273          * @param       String  $name   value for nucleus_skin.sdname\r
274          * @param       String  $desc   value for nucleus_skin.sddesc\r
275          * @param       String  $type   value for nucleus_skin.sdtype\r
276          * @param       String  $includeMode    value for nucleus_skin.sdinclude\r
277          * @param       String  $includePrefix  value for nucleus_skin.sdincpref\r
278          * @return      Integer ID for just inserted record\r
279          */\r
280         public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
281         {\r
282                 global $manager;\r
283                 \r
284                 $manager->notify(\r
285                         'PreAddSkin',\r
286                         array(\r
287                                 'name' => &$name,\r
288                                 'description' => &$desc,\r
289                                 'type' => &$type,\r
290                                 'includeMode' => &$includeMode,\r
291                                 'includePrefix' => &$includePrefix\r
292                         )\r
293                 );\r
294                 \r
295                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";\r
296                 $sdname         = sql_real_escape_string($name);\r
297                 $sddesc         = sql_real_escape_string($desc);\r
298                 $sdtype         = sql_real_escape_string($type);\r
299                 $sdincmode      = sql_real_escape_string($includeMode);\r
300                 $sdincpref      = sql_real_escape_string($includePrefix);\r
301                 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);\r
302                 sql_query($query);\r
303                 $newid = sql_insert_id();\r
304                 \r
305                 $manager->notify(\r
306                         'PostAddSkin',\r
307                         array(\r
308                                 'skinid'                => $newid,\r
309                                 'name'                  => $name,\r
310                                 'description'   => $desc,\r
311                                 'type'                  => $type,\r
312                                 'includeMode'   => $includeMode,\r
313                                 'includePrefix' => $includePrefix\r
314                         )\r
315                 );\r
316                 return $newid;\r
317         }\r
318         \r
319         /**\r
320          * Skin::parse()\r
321          * Parse a SKIN\r
322          * \r
323          * @param       string  $type\r
324          * @return      void\r
325          */\r
326         public function parse($type)\r
327         {\r
328                 global $currentSkinName, $manager, $CONF;\r
329                 \r
330                 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));\r
331                 \r
332                 // set output type\r
333                 sendContentType($this->getContentType(), 'skin');\r
334                 \r
335                 // set skin name as global var (so plugins can access it)\r
336                 $currentSkinName = $this->getName();\r
337                 $contents = $this->getContent($type);\r
338                 \r
339                 if ( !$contents )\r
340                 {\r
341                         // use base skin if this skin does not have contents\r
342                         $defskin = new SKIN($CONF['BaseSkin']);\r
343                         $contents = $defskin->getContent($type);\r
344                         if ( !$contents )\r
345                         {\r
346                                 echo _ERROR_SKIN;\r
347                                 return;\r
348                         }\r
349                 }\r
350                 \r
351                 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));\r
352                 \r
353                 // set IncludeMode properties of parser\r
354                 Parser::setProperty('IncludeMode', $this->getIncludeMode());\r
355                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());\r
356                 \r
357                 $action_class = $this->action_class;\r
358                 $handler = new $action_class($type);\r
359                 \r
360                 $actions = $handler->getDefinedActions($type);
361                 $parser = new Parser($actions, $handler);\r
362                 $handler->setParser($parser);\r
363                 $handler->setSkin($this);\r
364                 $parser->parse($contents);\r
365                 \r
366                 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));\r
367                 return;\r
368         }\r
369         \r
370         /**\r
371          * Skin::getContent()\r
372          * Get content of the skin part from the database\r
373          * \r
374          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
375          * @return      string  content of scontent\r
376          */\r
377         public function getContent($type)\r
378         {\r
379                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";\r
380                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));\r
381                 $res = sql_query($query);\r
382                 \r
383                 if ( sql_num_rows($res) == 0 )\r
384                 {\r
385                         return '';\r
386                 }\r
387                 return sql_result($res, 0, 0);\r
388         }\r
389 \r
390         /**\r
391          * SKIN::update()\r
392          * Updates the contents for one part of the skin in the database\r
393          * \r
394          * @param       string  $type type of the skin part (e.g. index, item, search ...) \r
395          * @param       string  $content new content for this skin part\r
396          * @return      void\r
397          * \r
398          */\r
399         public function update($type, $content)\r
400         {\r
401                 global $manager;\r
402                 \r
403                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";\r
404                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
405                 $res = sql_query($query);\r
406                 \r
407                 $skintypeexists = sql_fetch_object($res);\r
408                 $skintypevalue = ($content == true);\r
409                 \r
410                 if( $skintypevalue && $skintypeexists )\r
411                 {\r
412                         $data = array(\r
413                                 'skinid'        =>  $this->id,\r
414                                 'type'          =>  $type,\r
415                                 'content'       => &$content\r
416                         );\r
417                         \r
418                         // PreUpdateSkinPart event\r
419                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);\r
420                 }\r
421                 else if( $skintypevalue && !$skintypeexists )\r
422                 {\r
423                         $data = array(\r
424                                 'skinid' => $this->id,\r
425                                 'type' => $type,\r
426                                 'content' => &$content\r
427                         );\r
428                         \r
429                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);\r
430                 }\r
431                 else if( !$skintypevalue && $skintypeexists )\r
432                 {\r
433                         $data = array(\r
434                                 'skinid' => $this->id,\r
435                                 'type' => $type\r
436                         );\r
437                         \r
438                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);\r
439                 }\r
440                 \r
441                 // delete old thingie\r
442                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";\r
443                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
444                 sql_query($query);\r
445                 \r
446                 // write new thingie\r
447                 if ( $content )\r
448                 {\r
449                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";\r
450                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);\r
451                         sql_query($query);\r
452                 }\r
453                 \r
454                 if( $skintypevalue && $skintypeexists )\r
455                 {\r
456                         $data = array(\r
457                                 'skinid'        => $this->id,\r
458                                 'type'          => $type,\r
459                                 'content'       => &$content\r
460                         );\r
461                         \r
462                         // PostUpdateSkinPart event\r
463                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);\r
464                 }\r
465                 else 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                         // PostAddSkinPart event\r
474                         $manager->notify("PostAdd{$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                         );\r
482                         \r
483                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);\r
484                 }\r
485                 return;\r
486         }\r
487         \r
488         /**\r
489          * Skin::deleteAllParts()\r
490          * Deletes all skin parts from the database\r
491          * \r
492          * @param       void\r
493          * @return      void\r
494          */\r
495         public function deleteAllParts()\r
496         {\r
497                 $query = "DELETE FROM %s WHERE sdesc=%d;";\r
498                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
499                 sql_query($query);\r
500         }\r
501         \r
502         /**\r
503          * Skin::updateGeneralInfo()\r
504          * Updates the general information about the skin\r
505          * \r
506          * @param       string  $name                   name of the skin\r
507          * @param       string  $desc                   description of the skin\r
508          * @param       string  $type                   type of the skin\r
509          * @param       string  $includeMode    include mode of the skin\r
510          * @param       string  $includePrefix  include prefix of the skin\r
511          * @return      void\r
512          */\r
513         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
514         {\r
515                 $name                   = sql_real_escape_string($name);\r
516                 $desc                   = sql_real_escape_string($desc);\r
517                 $type                   = sql_real_escape_string($type);\r
518                 $includeMode    = sql_real_escape_string($includeMode);\r
519                 $includePrefix  = sql_real_escape_string($includePrefix);\r
520                 \r
521                 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";\r
522                 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);\r
523                 \r
524                 sql_query($query);\r
525                 return;\r
526         }\r
527         \r
528         /**\r
529          * Skin::getAllowedActionsForType()\r
530          * Get the allowed actions for a skin type\r
531          * returns an array with the allowed actions\r
532          * \r
533          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
534          * @return      array   allowed action types\r
535          */\r
536         public function getAllowedActionsForType($type)\r
537         {\r
538                 /**\r
539                  * NOTE: static method with variable class name is supported since PHP 5.3\r
540                  *  So now we utilize eval function.\r
541                  */\r
542                 $page_action_names = array();\r
543                 eval("\$defined_actions = {$this->action_class}::getDefinedActions('{$type}');");\r
544                 return $defined_actions;\r
545         }\r
546         \r
547         /**\r
548          * Skin::getFriendlyNames()\r
549          * Get an array with the names of possible skin parts\r
550          * Used to show all possible parts of a skin in the administration backend\r
551          * \r
552          * @static\r
553          * @param       string  $action_class   name of action class (optional)\r
554          * @param       array   type of the skin\r
555          */\r
556         static public function getFriendlyNames($action_class='Actions')\r
557         {\r
558                 global $DIR_LIBS;\r
559                 \r
560                 /*\r
561                  * NOTE: include needed action class\r
562                  */\r
563                 if ( $action_class != 'Actions' )\r
564                 {\r
565                         if ( !class_exists($action_class, FALSE)\r
566                           && (!file_exists("{$DIR_LIBS}{$action_class}.php")\r
567                            || !include("{$DIR_LIBS}{$action_class}.php")) )\r
568                         {\r
569                                 return;\r
570                         }\r
571                 }\r
572                 else\r
573                 {\r
574                         if ( !class_exists('Actions', FALSE)\r
575                           && (!file_exists("{$DIR_LIBS}ACTIONS.php")\r
576                            || !include("{$DIR_LIBS}ACTIONS.php")) )\r
577                         {\r
578                                 return;\r
579                         }\r
580                 }\r
581                 \r
582                 /**\r
583                  * NOTE: static method with variable class name is supported since PHP 5.3\r
584                  *  So now we utilize eval function.\r
585                  */\r
586                 eval("\$friendly_names = {$action_class}::getSkinTypeFriendlyNames();");\r
587                 \r
588                 $action_names = array();\r
589                 foreach ( $friendly_names as $action_name => $friendly_name )\r
590                 {\r
591                         $action_names[] = $action_name;\r
592                 }\r
593                 \r
594                 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";\r
595                 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));\r
596                 $res = sql_query($query);\r
597                 \r
598                 while ( $row = sql_fetch_array($res) )\r
599                 {\r
600                         $friendly_names[strtolower($row['stype'])] = $row['stype'];\r
601                 }\r
602                 return $friendly_names;\r
603         }\r
604 }\r