OSDN Git Service

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