OSDN Git Service

MERGE: リビジョン1778/1779のマージ。Skin::getFriendlyNames()のアクセス方法の変更
[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 1779 2012-04-21 10:04:33Z 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          * @param       string  $path   path to file if using fileparser
325          * @return      void
326          */
327         public function parse($type, $path='')
328         {
329                 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
330                 
331                 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
332                 
333                 // set output type
334                 sendContentType($this->getContentType(), 'skin');
335                 
336                 // set skin name as global var (so plugins can access it)
337                 $currentSkinName = $this->getName();
338                 
339                 $contents = FALSE;
340                 if ( $type != 'fileparse' )
341                 {
342                         $contents = $this->getContent($type);
343                 }
344                 else if ( $path !== ''  && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
345                 {
346                         $contents = $this->getFileContent($path);
347                 }
348                 
349                 if ( !$contents )
350                 {
351                         // use base skin if this skin does not have contents
352                         $defskin = new SKIN($CONF['BaseSkin']);
353                         $contents = $defskin->getContent($type);
354                         if ( !$contents )
355                         {
356                                 echo _ERROR_SKIN;
357                                 return;
358                         }
359                 }
360                 
361                 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
362                 
363                 // set IncludeMode properties of parser
364                 Parser::setProperty('IncludeMode', $this->getIncludeMode());
365                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
366                 
367                 $action_class = $this->action_class;
368                 $handler = new $action_class($type);
369                 
370                 $actions = $handler->getDefinedActions($type);
371                 $parser = new Parser($actions, $handler);
372                 
373                 $handler->setParser($parser);
374                 $handler->setSkin($this);
375                 $parser->parse($contents);
376                 
377                 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
378                 return;
379         }
380         
381         /**
382          * Skin::getContent()
383          * Get content of the skin part from the database
384          * 
385          * @param       string  $type   type of the skin (e.g. index, item, search ...)
386          * @return      string  content of scontent
387          */
388         public function getContent($type)
389         {
390                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
391                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));
392                 $res = sql_query($query);
393                 
394                 if ( sql_num_rows($res) == 0 )
395                 {
396                         return FALSE;
397                 }
398                 return sql_result($res, 0, 0);
399         }
400         
401         /**
402          * Skin::getFileContent()
403          * 
404          * @param       string  $fullpath       fullpath to the file to parse
405          * @return      mixed   file contents or FALSE
406          */
407         public function getFileContent($fullpath)
408         {
409                 $fsize = filesize($fullpath);
410                 if ( $fsize <= 0 )
411                 {
412                         return;
413                 }
414                 
415                 $fd = fopen ($fullpath, 'r');
416                 if ( $fd === FALSE )
417                 {
418                         return FALSE;
419                 }
420                 
421                 $contents = fread ($fd, $fsize);
422                 if ( $contents === FALSE )
423                 {
424                         return FALSE;
425                 }
426                 
427                 fclose ($fd);
428                 return $contents;
429         }
430         
431         /**
432          * SKIN::update()
433          * Updates the contents for one part of the skin in the database
434          * 
435          * @param       string  $type type of the skin part (e.g. index, item, search ...) 
436          * @param       string  $content new content for this skin part
437          * @return      void
438          * 
439          */
440         public function update($type, $content)
441         {
442                 global $manager;
443                 
444                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
445                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
446                 $res = sql_query($query);
447                 
448                 $skintypeexists = sql_fetch_object($res);
449                 $skintypevalue = ($content == true);
450                 
451                 if( $skintypevalue && $skintypeexists )
452                 {
453                         $data = array(
454                                 'skinid'        =>  $this->id,
455                                 'type'          =>  $type,
456                                 'content'       => &$content
457                         );
458                         
459                         // PreUpdateSkinPart event
460                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
461                 }
462                 else if( $skintypevalue && !$skintypeexists )
463                 {
464                         $data = array(
465                                 'skinid' => $this->id,
466                                 'type' => $type,
467                                 'content' => &$content
468                         );
469                         
470                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);
471                 }
472                 else if( !$skintypevalue && $skintypeexists )
473                 {
474                         $data = array(
475                                 'skinid' => $this->id,
476                                 'type' => $type
477                         );
478                         
479                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);
480                 }
481                 
482                 // delete old thingie
483                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
484                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
485                 sql_query($query);
486                 
487                 // write new thingie
488                 if ( $content )
489                 {
490                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
491                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
492                         sql_query($query);
493                 }
494                 
495                 if( $skintypevalue && $skintypeexists )
496                 {
497                         $data = array(
498                                 'skinid'        => $this->id,
499                                 'type'          => $type,
500                                 'content'       => &$content
501                         );
502                         
503                         // PostUpdateSkinPart event
504                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
505                 }
506                 else if( $skintypevalue && (!$skintypeexists) )
507                 {
508                         $data = array(
509                                 'skinid'        => $this->id,
510                                 'type'          => $type,
511                                 'content'       => &$content
512                         );
513                         
514                         // PostAddSkinPart event
515                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);
516                 }
517                 else if( (!$skintypevalue) && $skintypeexists )
518                 {
519                         $data = array(
520                                 'skinid'        => $this->id,
521                                 'type'          => $type
522                         );
523                         
524                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);
525                 }
526                 return;
527         }
528         
529         /**
530          * Skin::deleteAllParts()
531          * Deletes all skin parts from the database
532          * 
533          * @param       void
534          * @return      void
535          */
536         public function deleteAllParts()
537         {
538                 $query = "DELETE FROM %s WHERE sdesc=%d;";
539                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
540                 sql_query($query);
541         }
542         
543         /**
544          * Skin::updateGeneralInfo()
545          * Updates the general information about the skin
546          * 
547          * @param       string  $name                   name of the skin
548          * @param       string  $desc                   description of the skin
549          * @param       string  $type                   type of the skin
550          * @param       string  $includeMode    include mode of the skin
551          * @param       string  $includePrefix  include prefix of the skin
552          * @return      void
553          */
554         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
555         {
556                 $name                   = sql_real_escape_string($name);
557                 $desc                   = sql_real_escape_string($desc);
558                 $type                   = sql_real_escape_string($type);
559                 $includeMode    = sql_real_escape_string($includeMode);
560                 $includePrefix  = sql_real_escape_string($includePrefix);
561                 
562                 $query ="UPDATE %s SET sdname='%s', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d";
563                 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
564                 
565                 sql_query($query);
566                 return;
567         }
568         
569         /**
570          * Skin::getAllowedActionsForType()
571          * Get the allowed actions for a skin type
572          * returns an array with the allowed actions
573          * 
574          * @param       string  $type   type of the skin (e.g. index, item, search ...)
575          * @return      array   allowed action types
576          */
577         public function getAllowedActionsForType($type)
578         {
579                 /**
580                  * NOTE: static method with variable class name is supported since PHP 5.3
581                  *  So now we utilize eval function.
582                  */
583                 $page_action_names = array();
584                 eval("\$defined_actions = {$this->action_class}::getDefinedActions('{$type}');");
585                 return $defined_actions;
586         }
587         
588         /**
589          * Skin::getFriendlyNames()
590          * Get an array with the names of possible skin parts
591          * Used to show all possible parts of a skin in the administration backend
592          * 
593          * @static
594          * @param       string  $action_class   name of action class (optional)
595          * @param       array   type of the skin
596          */
597         public function getFriendlyNames($action_class='Actions')
598         {
599                 /**
600                  * NOTE: static method with variable class name is supported since PHP 5.3
601                  *  So now we utilize eval function.
602                  */
603                 eval("\$friendly_names = {$action_class}::getSkinTypeFriendlyNames();");
604                 return $friendly_names;
605         }
606 }