OSDN Git Service

FIX: Skin::getFriendryName()と関係する箇所のコードを修正。Skin::$adminの廃止。
[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-2012 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 1727 2012-04-07 02:54:22Z 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                 $actions = $this->getAllowedActionsForType($type);\r
352                 \r
353                 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));\r
354                 \r
355                 // set IncludeMode properties of parser\r
356                 Parser::setProperty('IncludeMode', $this->getIncludeMode());\r
357                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());\r
358                 \r
359                 $action_class = $this->action_class;\r
360                 $handler = new $action_class($type);\r
361                 \r
362                 $parser = new Parser($actions, $handler);\r
363                 $handler->setParser($parser);\r
364                 $handler->setSkin($this);\r
365                 $parser->parse($contents);\r
366                 \r
367                 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));\r
368                 return;\r
369         }\r
370         \r
371         /**\r
372          * Skin::getContent()\r
373          * Get content of the skin part from the database\r
374          * \r
375          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
376          * @return      string  content of scontent\r
377          */\r
378         public function getContent($type)\r
379         {\r
380                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";\r
381                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));\r
382                 $res = sql_query($query);\r
383                 \r
384                 if ( sql_num_rows($res) == 0 )\r
385                 {\r
386                         return '';\r
387                 }\r
388                 return sql_result($res, 0, 0);\r
389         }\r
390 \r
391         /**\r
392          * SKIN::update()\r
393          * Updates the contents for one part of the skin in the database\r
394          * \r
395          * @param       string  $type type of the skin part (e.g. index, item, search ...) \r
396          * @param       string  $content new content for this skin part\r
397          * @return      void\r
398          * \r
399          */\r
400         public function update($type, $content)\r
401         {\r
402                 global $manager;\r
403                 \r
404                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";\r
405                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
406                 $res = sql_query($query);\r
407                 \r
408                 $skintypeexists = sql_fetch_object($res);\r
409                 $skintypevalue = ($content == true);\r
410                 \r
411                 if( $skintypevalue && $skintypeexists )\r
412                 {\r
413                         $data = array(\r
414                                 'skinid'        =>  $this->id,\r
415                                 'type'          =>  $type,\r
416                                 'content'       => &$content\r
417                         );\r
418                         \r
419                         // PreUpdateSkinPart event\r
420                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);\r
421                 }\r
422                 else if( $skintypevalue && !$skintypeexists )\r
423                 {\r
424                         $data = array(\r
425                                 'skinid' => $this->id,\r
426                                 'type' => $type,\r
427                                 'content' => &$content\r
428                         );\r
429                         \r
430                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);\r
431                 }\r
432                 else if( !$skintypevalue && $skintypeexists )\r
433                 {\r
434                         $data = array(\r
435                                 'skinid' => $this->id,\r
436                                 'type' => $type\r
437                         );\r
438                         \r
439                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);\r
440                 }\r
441                 \r
442                 // delete old thingie\r
443                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";\r
444                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
445                 sql_query($query);\r
446                 \r
447                 // write new thingie\r
448                 if ( $content )\r
449                 {\r
450                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";\r
451                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);\r
452                         sql_query($query);\r
453                 }\r
454                 \r
455                 if( $skintypevalue && $skintypeexists )\r
456                 {\r
457                         $data = array(\r
458                                 'skinid'        => $this->id,\r
459                                 'type'          => $type,\r
460                                 'content'       => &$content\r
461                         );\r
462                         \r
463                         // PostUpdateSkinPart event\r
464                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);\r
465                 }\r
466                 else if( $skintypevalue && (!$skintypeexists) )\r
467                 {\r
468                         $data = array(\r
469                                 'skinid'        => $this->id,\r
470                                 'type'          => $type,\r
471                                 'content'       => &$content\r
472                         );\r
473                         \r
474                         // PostAddSkinPart event\r
475                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);\r
476                 }\r
477                 else if( (!$skintypevalue) && $skintypeexists )\r
478                 {\r
479                         $data = array(\r
480                                 'skinid'        => $this->id,\r
481                                 'type'          => $type\r
482                         );\r
483                         \r
484                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);\r
485                 }\r
486                 return;\r
487         }\r
488         \r
489         /**\r
490          * Skin::deleteAllParts()\r
491          * Deletes all skin parts from the database\r
492          * \r
493          * @param       void\r
494          * @return      void\r
495          */\r
496         public function deleteAllParts()\r
497         {\r
498                 $query = "DELETE FROM %s WHERE sdesc=%d;";\r
499                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
500                 sql_query($query);\r
501         }\r
502         \r
503         /**\r
504          * Skin::updateGeneralInfo()\r
505          * Updates the general information about the skin\r
506          * \r
507          * @param       string  $name                   name of the skin\r
508          * @param       string  $desc                   description of the skin\r
509          * @param       string  $type                   type of the skin\r
510          * @param       string  $includeMode    include mode of the skin\r
511          * @param       string  $includePrefix  include prefix of the skin\r
512          * @return      void\r
513          */\r
514         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
515         {\r
516                 $name                   = sql_real_escape_string($name);\r
517                 $desc                   = sql_real_escape_string($desc);\r
518                 $type                   = sql_real_escape_string($type);\r
519                 $includeMode    = sql_real_escape_string($includeMode);\r
520                 $includePrefix  = sql_real_escape_string($includePrefix);\r
521                 \r
522                 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";\r
523                 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);\r
524                 \r
525                 sql_query($query);\r
526                 return;\r
527         }\r
528         \r
529         /**\r
530          * Skin::getAllowedActionsForType()\r
531          * Get the allowed actions for a skin type\r
532          * returns an array with the allowed actions\r
533          * \r
534          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
535          * @return      array   allowed action types\r
536          */\r
537         public function getAllowedActionsForType($type)\r
538         {\r
539                 /**\r
540                  * NOTE: static method with variable class name is supported since 5.3\r
541                  *  So now we utilize eval function.\r
542                  */\r
543                 $page_action_names = array();\r
544                 eval("\$page_action_names = {$this->action_class}::get_allowed_actions_for_type('{$type}');");\r
545                 return $page_action_names;\r
546         }\r
547         \r
548         /**\r
549          * Skin::getFriendlyNames()\r
550          * Get an array with the names of possible skin parts\r
551          * Used to show all possible parts of a skin in the administration backend\r
552          * \r
553          * @static\r
554          * @param       string  $action_class   name of action class (optional)\r
555          * @param       array   type of the skin\r
556          */\r
557         static public function getFriendlyNames($action_class='Actions')\r
558         {\r
559                 global $DIR_LIBS;\r
560                 \r
561                 /*\r
562                  * NOTE: include needed action class\r
563                  */\r
564                 if ( $action_class != 'Actions' )\r
565                 {\r
566                         if ( !class_exists($action_class, FALSE)\r
567                           && !file_exists("{$DIR_LIBS}{$action_class}.php")\r
568                           || !include("{$DIR_LIBS}{$action_class}.php") )\r
569                         {\r
570                                 return;\r
571                         }\r
572                 }\r
573                 else\r
574                 {\r
575                         if ( !class_exists('Actions', FALSE)\r
576                           && !file_exists("{$DIR_LIBS}ACTIONS.php")\r
577                           || !include("{$DIR_LIBS}ACTIONS.php") )\r
578                         {\r
579                                 return;\r
580                         }\r
581                 }\r
582                 \r
583                 /**\r
584                  * NOTE: static method with variable class name is supported since 5.3\r
585                  *  So now we utilize eval function.\r
586                  */\r
587                 eval("\$friendly_names = {$action_class}::get_page_type_friendly_names();");\r
588                 \r
589                 $action_names = array();\r
590                 foreach ( $friendly_names as $action_name => $friendly_name )\r
591                 {\r
592                         $action_names[] = $action_name;\r
593                 }\r
594                 \r
595                 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";\r
596                 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));\r
597                 $res = sql_query($query);\r
598                 \r
599                 while ( $row = sql_fetch_array($res) )\r
600                 {\r
601                         $friendly_names[strtolower($row['stype'])] = $row['stype'];\r
602                 }\r
603                 return $friendly_names;\r
604         }\r
605 }\r