OSDN Git Service

Skin/Actions/AdminActionsクラスの整備
[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         /* \r
39          * FIXME:\r
40          * whether to be used by Admin class or not\r
41          * the other way is preferrable instead of this\r
42          * for example, adding admindir to normal/skindir in nucleus_skin_desc.sdincmode \r
43          * \r
44          */\r
45         private $admin = FALSE;\r
46         \r
47         /**\r
48          * Skin::__construct()\r
49          * Constructor for a new SKIN object\r
50          * \r
51          * @param       integer $id             id of the skin\r
52          * @param       boolean $admin  for admin pages or not\r
53          * @return      void\r
54          */\r
55         public function __construct($id, $admin=FALSE)\r
56         {\r
57                 global $DIR_LIBS;\r
58                 \r
59                 $this->id = (integer) $id;\r
60                 \r
61                 // read skin name/description/content type\r
62                 $query = "SELECT * FROM %s WHERE sdnumber=%d";\r
63                 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);\r
64                 $res = sql_query($query);\r
65                 $obj = sql_fetch_object($res);\r
66                 $this->valid = (sql_num_rows($res) > 0);\r
67                 if ( !$this->valid )\r
68                 {\r
69                         return;\r
70                 }\r
71                 \r
72                 $this->name = $obj->sdname;\r
73                 $this->description = $obj->sddesc;\r
74                 $this->contentType = $obj->sdtype;\r
75                 $this->includeMode = $obj->sdincmode;\r
76                 $this->includePrefix = $obj->sdincpref;\r
77                 /*\r
78                  * FIXME: this key should be replaced by other way\r
79                  * such that adding admin key into skin table and so on\r
80                  */\r
81                 if ( !$admin )\r
82                 {\r
83                         if ( !class_exists('Actions', FALSE) )\r
84                         {\r
85                                 include("{$DIR_LIBS}ACTIONS.php");\r
86                         }\r
87                         $this->admin = FALSE;\r
88                 }\r
89                 else\r
90                 {\r
91                         if ( !class_exists('AdminActions', FALSE) )\r
92                         {\r
93                                 include("{$DIR_LIBS}AdminActions.php");\r
94                         }\r
95                         $this->admin = TRUE;\r
96                 }\r
97                 \r
98                 return;\r
99         }\r
100         \r
101         /**\r
102          * Skin::getID()\r
103          * Get SKIN id\r
104          * \r
105          * @param       void\r
106          * @return      integer id for this skin instance\r
107          */\r
108         public function getID()\r
109         {\r
110                 return (integer) $this->id;\r
111         }\r
112         \r
113         /**\r
114          * Skin::isValid()\r
115          * \r
116          * @param       void\r
117          * @return      boolean\r
118          */\r
119         public function isValid()\r
120         {\r
121                 return (boolean) $this->valid;\r
122         }\r
123         \r
124         /**\r
125          * Skin::getName()\r
126          * Get SKIN name\r
127          * \r
128          * @param       void\r
129          * @return      string  name of this skin instance\r
130          */\r
131         public function getName()\r
132         {\r
133                 return (string) $this->name;\r
134         }\r
135         \r
136         /**\r
137          * Skin::getDescription()\r
138          * Get SKIN description\r
139          * \r
140          * @param       void\r
141          * @return      string  description of this skin instance\r
142          */\r
143         public function getDescription()\r
144         {\r
145                 return (string) $this->description;\r
146         }\r
147         \r
148         /**\r
149          * Skin::getContentType()\r
150          * Get SKIN content type\r
151          * e.g. text/xml, text/html, application/atom+xml\r
152          * \r
153          * @param       void\r
154          * @return      string  name of this skin instance\r
155          */\r
156         public function getContentType()\r
157         {\r
158                 return (string) $this->contentType;\r
159         }\r
160         \r
161         /**\r
162          * Skin::getIncludeMode()\r
163          * Get include mode of the SKIN\r
164          * \r
165          * Returns either 'normal' or 'skindir':\r
166          * 'normal': if a all data of the skin can be found in the databse\r
167          * 'skindir': if the skin has data in the it's skin driectory\r
168          * \r
169          * @param       void\r
170          * @return      string  normal/skindir\r
171          */\r
172         public function getIncludeMode()\r
173         {\r
174                 return (string) $this->includeMode;\r
175         }\r
176         \r
177         /**\r
178          * Skin::getIncludePrefix()\r
179          * Get include prefix of the SKIN\r
180          * \r
181          * Get name of the subdirectory (with trailing slash) where\r
182          * the files of the current skin can be found (e.g. 'default/')\r
183          * \r
184          * @param       void\r
185          * @return      string  include prefix of this skin instance\r
186          */\r
187         public function getIncludePrefix()\r
188         {\r
189                 return (string) $this->includePrefix;\r
190         }\r
191         \r
192         /**\r
193          * Skin::exists()\r
194          * Checks if a skin with a given shortname exists\r
195          * \r
196          * @static\r
197          * @param       string  $name   Skin short name\r
198          * @return      integer number of skins with the given ID\r
199          */\r
200         static public function exists($name)\r
201         {\r
202                 $query = "SELECT COUNT (*) AS result FROM %s WHERE sdname='%s';";\r
203                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
204                 return (quickQuery($query) > 0);\r
205         }\r
206         \r
207         /**\r
208          * Skin::existsID()\r
209          * Checks if a skin with a given ID exists\r
210          * \r
211          * @static\r
212          * @param       string  $id     Skin ID\r
213          * @return      integer number of skins with the given ID\r
214          */\r
215         static public function existsID($id)\r
216         {\r
217                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";\r
218                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
219                 return (quickQuery($query) > 0);\r
220         }\r
221         \r
222         /**\r
223          * Skin::createFromName()\r
224          * Returns a skin given its shortname\r
225          * \r
226          * @static\r
227          * @param       string  $name   Skin shortname\r
228          * @return      object instance of Skin class\r
229          */\r
230         static public function createFromName($name)\r
231         {\r
232                 return new SKIN(SKIN::getIdFromName($name));\r
233         }\r
234         \r
235         /**\r
236          * Skin::getIdFromName()\r
237          * Returns a skin ID given its shortname\r
238          * \r
239          * @static\r
240          * @param       string  $name   Skin shortname\r
241          * @return      integer Skin ID\r
242          */\r
243         static public function getIdFromName($name)\r
244         {\r
245                 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";\r
246                 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));\r
247                 $res = sql_query($query);\r
248                 $obj = sql_fetch_object($res);\r
249                 return $obj->sdnumber;\r
250         }\r
251         \r
252         /**\r
253          * Skin::getNameFromId()\r
254          * Returns a skin shortname given its ID\r
255          * \r
256          * @static\r
257          * @param       string  $name\r
258          * @return      string  Skin short name\r
259          */\r
260         static public function getNameFromId($id)\r
261         {\r
262                 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";\r
263                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);\r
264                 return quickQuery($query);\r
265         }\r
266         \r
267         /**\r
268          * SKIN::createNew()\r
269          * Creates a new skin, with the given characteristics.\r
270          *\r
271          * @static\r
272          * @param       String  $name   value for nucleus_skin.sdname\r
273          * @param       String  $desc   value for nucleus_skin.sddesc\r
274          * @param       String  $type   value for nucleus_skin.sdtype\r
275          * @param       String  $includeMode    value for nucleus_skin.sdinclude\r
276          * @param       String  $includePrefix  value for nucleus_skin.sdincpref\r
277          * @return      Integer ID for just inserted record\r
278          */\r
279         public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
280         {\r
281                 global $manager;\r
282                 \r
283                 $manager->notify(\r
284                         'PreAddSkin',\r
285                         array(\r
286                                 'name' => &$name,\r
287                                 'description' => &$desc,\r
288                                 'type' => &$type,\r
289                                 'includeMode' => &$includeMode,\r
290                                 'includePrefix' => &$includePrefix\r
291                         )\r
292                 );\r
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                 $manager->notify(\r
305                         'PostAddSkin',\r
306                         array(\r
307                                 'skinid'                => $newid,\r
308                                 'name'                  => $name,\r
309                                 'description'   => $desc,\r
310                                 'type'                  => $type,\r
311                                 'includeMode'   => $includeMode,\r
312                                 'includePrefix' => $includePrefix\r
313                         )\r
314                 );\r
315                 return $newid;\r
316         }\r
317         \r
318         /**\r
319          * Skin::parse()\r
320          * Parse a SKIN\r
321          * \r
322          * @param       string  $type\r
323          * @return      void\r
324          */\r
325         public function parse($type)\r
326         {\r
327                 global $currentSkinName, $manager, $CONF;\r
328                 \r
329                 if ( !$this->admin )\r
330                 {\r
331                         $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));\r
332                 }\r
333                 else\r
334                 {\r
335                         $manager->notify('InitAdminSkinParse',array('skin' => &$this, 'type' => $type));\r
336                 }\r
337                 \r
338                 // set output type\r
339                 sendContentType($this->getContentType(), 'skin');\r
340                 \r
341                 // set skin name as global var (so plugins can access it)\r
342                 $currentSkinName = $this->getName();\r
343                 $contents = $this->getContent($type);\r
344                 \r
345                 if ( !$contents )\r
346                 {\r
347                         // use base skin if this skin does not have contents\r
348                         $defskin = new SKIN($CONF['BaseSkin']);\r
349                         $contents = $defskin->getContent($type);\r
350                         if ( !$contents )\r
351                         {\r
352                                 echo _ERROR_SKIN;\r
353                                 return;\r
354                         }\r
355                 }\r
356                 \r
357                 $actions = $this->getAllowedActionsForType($type);\r
358                 \r
359                 if ( !$this->admin )\r
360                 {\r
361                         $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));\r
362                 }\r
363                 else\r
364                 {\r
365                         $manager->notify('PreAdminSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));\r
366                 }\r
367                 \r
368                 // set IncludeMode properties of parser\r
369                 Parser::setProperty('IncludeMode', $this->getIncludeMode());\r
370                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());\r
371                 \r
372                 if ( !$this->admin )\r
373                 {\r
374                         $handler = new Actions($type);\r
375                 }\r
376                 else\r
377                 {\r
378                         $handler = new AdminActions($type);\r
379                 }\r
380                 \r
381                 $parser = new Parser($actions, $handler);\r
382                 $handler->setParser($parser);\r
383                 $handler->setSkin($this);\r
384                 $parser->parse($contents);\r
385                 \r
386                 if ( !$this->admin )\r
387                 {\r
388                         $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));\r
389                 }\r
390                 else\r
391                 {\r
392                         $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));\r
393                 }\r
394                 return;\r
395         }\r
396         \r
397         /**\r
398          * Skin::getContent()\r
399          * Get content of the skin part from the database\r
400          * \r
401          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
402          * @return      string  content of scontent\r
403          */\r
404         public function getContent($type)\r
405         {\r
406                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";\r
407                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));\r
408                 $res = sql_query($query);\r
409                 \r
410                 if ( sql_num_rows($res) == 0 )\r
411                 {\r
412                         return '';\r
413                 }\r
414                 return sql_result($res, 0, 0);\r
415         }\r
416 \r
417         /**\r
418          * SKIN::update()\r
419          * Updates the contents for one part of the skin in the database\r
420          * \r
421          * @param       string  $type type of the skin part (e.g. index, item, search ...) \r
422          * @param       string  $content new content for this skin part\r
423          * @return      void\r
424          * \r
425          */\r
426         public function update($type, $content)\r
427         {\r
428                 global $manager;\r
429                 \r
430                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";\r
431                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
432                 $res = sql_query($query);\r
433                 \r
434                 $skintypeexists = sql_fetch_object($res);\r
435                 $skintypevalue = ($content == true);\r
436                 \r
437                 if( $skintypevalue && $skintypeexists )\r
438                 {\r
439                         $data = array(\r
440                                 'skinid'        =>  $this->id,\r
441                                 'type'          =>  $type,\r
442                                 'content'       => &$content\r
443                         );\r
444                         \r
445                         // PreUpdateSkinPart event\r
446                         if ( !$this->admin )\r
447                         {\r
448                                 $manager->notify('PreUpdateSkinPart', $data);\r
449                         }\r
450                         else\r
451                         {\r
452                                 $manager->notify('PreUpdateAdminSkinPart', $data);\r
453                         }\r
454                 }\r
455                 else 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                         // PreAddSkinPart event\r
464                         if ( !$this->admin )\r
465                         {\r
466                                 $manager->notify('PreAddSkinPart', $data);\r
467                         }\r
468                         else\r
469                         {\r
470                                 $manager->notify('PreAddAdminSkinPart', $data);\r
471                         }\r
472                 }\r
473                 else if( !$skintypevalue && $skintypeexists )\r
474                 {\r
475                         $data = array(\r
476                                 'skinid' => $this->id,\r
477                                 'type' => $type\r
478                         );\r
479                         \r
480                         // PreDeleteSkinPart event\r
481                         if ( !$this->admin )\r
482                         {\r
483                                 $manager->notify('PreDeleteSkinPart', $data);\r
484                         }\r
485                         else\r
486                         {\r
487                                 $manager->notify('PreDeleteAdminSkinPart', $data);\r
488                         }\r
489                 }\r
490                 \r
491                 // delete old thingie\r
492                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";\r
493                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);\r
494                 sql_query($query);\r
495                 \r
496                 // write new thingie\r
497                 if ( $content )\r
498                 {\r
499                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";\r
500                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);\r
501                         sql_query($query);\r
502                 }\r
503                 \r
504                 if( $skintypevalue && $skintypeexists )\r
505                 {\r
506                         $data = array(\r
507                                 'skinid'        => $this->id,\r
508                                 'type'          => $type,\r
509                                 'content'       => &$content\r
510                         );\r
511                         \r
512                         // PostUpdateSkinPart event\r
513                         if ( !$this->admin )\r
514                         {\r
515                                 $manager->notify('PostUpdateSkinPart', $data);\r
516                         }\r
517                         else\r
518                         {\r
519                                 $manager->notify('PostUpdateAdminSkinPart', $data);\r
520                         }\r
521                 }\r
522                 else if( $skintypevalue && (!$skintypeexists) )\r
523                 {\r
524                         $data = array(\r
525                                 'skinid'        => $this->id,\r
526                                 'type'          => $type,\r
527                                 'content'       => &$content\r
528                         );\r
529                         \r
530                         // PostAddSkinPart event\r
531                         if ( !$this->admin )\r
532                         {\r
533                                 $manager->notify('PostAddSkinPart', $data);\r
534                         }\r
535                         else\r
536                         {\r
537                                 $manager->notify('PostAddAdminSkinPart', $data);\r
538                         }\r
539                 }\r
540                 else if( (!$skintypevalue) && $skintypeexists )\r
541                 {\r
542                         $data = array(\r
543                                 'skinid'        => $this->id,\r
544                                 'type'          => $type\r
545                         );\r
546                         \r
547                         // PostDeleteSkinPart event\r
548                         if ( !$this->admin )\r
549                         {\r
550                                 $manager->notify('PostDeleteSkinPart', $data);\r
551                         }\r
552                         else\r
553                         {\r
554                                 $manager->notify('PostDeleteAdminSkinPart', $data);\r
555                         }\r
556                 }\r
557                 return;\r
558         }\r
559         \r
560         /**\r
561          * Skin::deleteAllParts()\r
562          * Deletes all skin parts from the database\r
563          * \r
564          * @param       void\r
565          * @return      void\r
566          */\r
567         public function deleteAllParts()\r
568         {\r
569                 $query = "DELETE FROM %s WHERE sdesc=%d;";\r
570                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);\r
571                 sql_query($query);\r
572         }\r
573         \r
574         /**\r
575          * Skin::updateGeneralInfo()\r
576          * Updates the general information about the skin\r
577          * \r
578          * @param       string  $name                   name of the skin\r
579          * @param       string  $desc                   description of the skin\r
580          * @param       string  $type                   type of the skin\r
581          * @param       string  $includeMode    include mode of the skin\r
582          * @param       string  $includePrefix  include prefix of the skin\r
583          * @return      void\r
584          */\r
585         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')\r
586         {\r
587                 $name                   = sql_real_escape_string($name);\r
588                 $desc                   = sql_real_escape_string($desc);\r
589                 $type                   = sql_real_escape_string($type);\r
590                 $includeMode    = sql_real_escape_string($includeMode);\r
591                 $includePrefix  = sql_real_escape_string($includePrefix);\r
592                 \r
593                 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";\r
594                 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);\r
595                 \r
596                 sql_query($query);\r
597                 return;\r
598         }\r
599         \r
600         /**\r
601          * Skin::getFriendlyNames()\r
602          * Get an array with the names of possible skin parts\r
603          * Used to show all possible parts of a skin in the administration backend\r
604          * \r
605          * @param       void\r
606          * @param       array   type of the skin\r
607          */\r
608         static public function getFriendlyNames()\r
609         {\r
610                 if ( !$this->$admin )\r
611                 {\r
612                         $friendly_names = Actions::get_page_type_friendly_names();\r
613                         $action_names = Actions::get_page_action_names();\r
614                 }\r
615                 else\r
616                 {\r
617                         $friendly_names = AdminActions::get_page_type_friendly_names();\r
618                         $action_names = AdminActions::get_page_action_names();\r
619                 }\r
620                 \r
621                 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";\r
622                 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));\r
623                 $res = sql_query($query);\r
624                 \r
625                 while ( $row = sql_fetch_array($res) )\r
626                 {\r
627                         $friendly_names[strtolower($row['stype'])] = $row['stype'];\r
628                 }\r
629                 return $friendly_names;\r
630         }\r
631         \r
632         /**\r
633          * Skin::getAllowedActionsForType()\r
634          * Get the allowed actions for a skin type\r
635          * returns an array with the allowed actions\r
636          * \r
637          * @param       string  $type   type of the skin (e.g. index, item, search ...)\r
638          * @return      array   allowed action types\r
639          */\r
640         public function getAllowedActionsForType($type)\r
641         {\r
642                 if ( !$this->admin )\r
643                 {\r
644                         return Actions::get_allowed_actions_for_type($type);\r
645                 }\r
646                 else\r
647                 {\r
648                         return AdminActions::get_allowed_actions_for_type($type);\r
649                 }\r
650         }\r
651 }\r