OSDN Git Service

MERGE: リビジョン1816。Skinクラスのコード整理。
[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 1816 2012-05-03 01:40:10Z 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                 /*
58                  * NOTE: include needed action class
59                  */
60                 if ( $action_class != 'Actions' )
61                 {
62                         if ( !class_exists($action_class, FALSE)
63                           && (!file_exists("{$DIR_LIBS}{$action_class}.php")
64                            || !include("{$DIR_LIBS}{$action_class}.php")) )
65                         {
66                                 return;
67                         }
68                 }
69                 else
70                 {
71                         if ( !class_exists('Actions', FALSE)
72                           && (!file_exists("{$DIR_LIBS}ACTIONS.php")
73                            || !include("{$DIR_LIBS}ACTIONS.php")) )
74                         {
75                                 return;
76                         }
77                 }
78                 
79                 $this->action_class = $action_class;
80                 $this->event_identifier = $event_identifier;
81                 
82                 // read skin name/description/content type
83                 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
84                 $query = sprintf($query, sql_table('skin_desc'), $this->id);
85                 $res = DB::getRow($query);
86                 
87                 $this->valid = !empty($res);
88                 if ( $this->valid )
89                 {
90                         $this->name = $res['sdname'];
91                         $this->description = $res['sddesc'];
92                         $this->contentType = $res['sdtype'];
93                         $this->includeMode = $res['sdincmode'];
94                         $this->includePrefix = $res['sdincpref'];
95                 }
96                 
97                 return;
98         }
99         
100         /**
101          * Skin::getID()
102          * Get SKIN id
103          * 
104          * @param       void
105          * @return      integer id for this skin instance
106          */
107         public function getID()
108         {
109                 return (integer) $this->id;
110         }
111         
112         /**
113          * Skin::isValid()
114          * 
115          * @param       void
116          * @return      boolean
117          */
118         public function isValid()
119         {
120                 return (boolean) $this->valid;
121         }
122         
123         /**
124          * Skin::getName()
125          * Get SKIN name
126          * 
127          * @param       void
128          * @return      string  name of this skin instance
129          */
130         public function getName()
131         {
132                 return (string) $this->name;
133         }
134         
135         /**
136          * Skin::getDescription()
137          * Get SKIN description
138          * 
139          * @param       void
140          * @return      string  description of this skin instance
141          */
142         public function getDescription()
143         {
144                 return (string) $this->description;
145         }
146         
147         /**
148          * Skin::getContentType()
149          * Get SKIN content type
150          * e.g. text/xml, text/html, application/atom+xml
151          * 
152          * @param       void
153          * @return      string  name of this skin instance
154          */
155         public function getContentType()
156         {
157                 return (string) $this->contentType;
158         }
159         
160         /**
161          * Skin::getIncludeMode()
162          * Get include mode of the SKIN
163          * 
164          * Returns either 'normal' or 'skindir':
165          * 'normal': if a all data of the skin can be found in the databse
166          * 'skindir': if the skin has data in the it's skin driectory
167          * 
168          * @param       void
169          * @return      string  normal/skindir
170          */
171         public function getIncludeMode()
172         {
173                 return (string) $this->includeMode;
174         }
175         
176         /**
177          * Skin::getIncludePrefix()
178          * Get include prefix of the SKIN
179          * 
180          * Get name of the subdirectory (with trailing slash) where
181          * the files of the current skin can be found (e.g. 'default/')
182          * 
183          * @param       void
184          * @return      string  include prefix of this skin instance
185          */
186         public function getIncludePrefix()
187         {
188                 return (string) $this->includePrefix;
189         }
190         
191         /**
192          * Skin::exists()
193          * Checks if a skin with a given shortname exists
194          * 
195          * @static
196          * @param       string  $name   Skin short name
197          * @return      integer number of skins with the given ID
198          */
199         static public function exists($name)
200         {
201                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
202                 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
203                 return (DB::getValue($query) > 0);
204         }
205         
206         /**
207          * Skin::existsID()
208          * Checks if a skin with a given ID exists
209          * 
210          * @static
211          * @param       string  $id     Skin ID
212          * @return      integer number of skins with the given ID
213          */
214         static public function existsID($id)
215         {
216                 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
217                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
218                 return (DB::getValue($query) > 0);
219         }
220         
221         /**
222          * Skin::createFromName()
223          * Returns a skin given its shortname
224          * 
225          * @static
226          * @param       string  $name   Skin shortname
227          * @return      object instance of Skin class
228          */
229         static public function createFromName($name)
230         {
231                 return new SKIN(SKIN::getIdFromName($name));
232         }
233         
234         /**
235          * Skin::getIdFromName()
236          * Returns a skin ID given its shortname
237          * 
238          * @static
239          * @param       string  $name   Skin shortname
240          * @return      integer Skin ID
241          */
242         static public function getIdFromName($name)
243         {
244                 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
245                 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
246                 return DB::getValue($query);
247         }
248         
249         /**
250          * Skin::getNameFromId()
251          * Returns a skin shortname given its ID
252          * 
253          * @static
254          * @param       string  $name
255          * @return      string  Skin short name
256          */
257         static public function getNameFromId($id)
258         {
259                 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
260                 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
261                 return DB::getValue($query);
262         }
263         
264         /**
265          * SKIN::createNew()
266          * Creates a new skin, with the given characteristics.
267          *
268          * @static
269          * @param       String  $name   value for nucleus_skin.sdname
270          * @param       String  $desc   value for nucleus_skin.sddesc
271          * @param       String  $type   value for nucleus_skin.sdtype
272          * @param       String  $includeMode    value for nucleus_skin.sdinclude
273          * @param       String  $includePrefix  value for nucleus_skin.sdincpref
274          * @return      Integer ID for just inserted record
275          */
276         public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
277         {
278                 global $manager;
279                 
280                 $data = array(
281                         'name'                  => &$name,
282                         'description'   => &$desc,
283                         'type'                  => &$type,
284                         'includeMode'   => &$includeMode,
285                         'includePrefix' => &$includePrefix
286                 );
287                 $manager->notify('PreAddSkin', $data);
288                 
289                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
290                 $sdname         = DB::quoteValue($name);
291                 $sddesc         = DB::quoteValue($desc);
292                 $sdtype         = DB::quoteValue($type);
293                 $sdincmode      = DB::quoteValue($includeMode);
294                 $sdincpref      = DB::quoteValue($includePrefix);
295                 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
296                 DB::execute($query);
297                 $newid = DB::getInsertId();
298                 
299                 $data = array(
300                         'skinid'                => $newid,
301                         'name'                  => $name,
302                         'description'   => $desc,
303                         'type'                  => $type,
304                         'includeMode'   => $includeMode,
305                         'includePrefix' => $includePrefix
306                 );
307                 $manager->notify('PostAddSkin', $data);
308                 
309                 return $newid;
310         }
311         
312         /**
313          * Skin::parse()
314          * Parse a SKIN
315          * 
316          * @param       string  $type
317          * @param       string  $path   path to file if using fileparser
318          * @return      void
319          */
320         public function parse($type, $path='')
321         {
322                 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
323                 
324                 $data = array(
325                         'skin' => &$this,
326                         'type' => $type
327                 );
328                 $manager->notify("Init{$this->event_identifier}Parse", $data);
329                 
330                 // include skin locale file for <%text%> tag if useable
331                 $this->includeTranslation();
332                 
333                 // set output type
334                 sendContentType($this->getContentType(), 'skin');
335                 
336                 /* FIX: should be obsoleted */
337                 $currentSkinName = $this->getName();
338                 
339                 // retrieve contents
340                 $contents = FALSE;
341                 if ( $type != 'fileparse' )
342                 {
343                         $contents = $this->getContentFromDB($type);
344                 }
345                 else if ( $path !== ''  && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
346                 {
347                         $contents = $this->getContentFromFile($path);
348                 }
349                 // use base skin if this skin does not have contents
350                 if ( $contents === FALSE )
351                 {
352                         $defskin = new SKIN($CONF['BaseSkin']);
353                         $contents = $defskin->getContentFromDB($type);
354                         if ( !$contents )
355                         {
356                                 echo _ERROR_SKIN;
357                                 return;
358                         }
359                 }
360                 
361                 $data = array(
362                         'skin'          => &$this,
363                         'type'          => $type,
364                         'contents'      => &$contents
365                 );
366                 $manager->notify("Pre{$this->event_identifier}Parse", $data);
367                 
368                 // set IncludeMode properties of parser
369                 Parser::setProperty('IncludeMode', $this->getIncludeMode());
370                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
371                 
372                 // call action handler
373                 $action_class = $this->action_class;
374                 $handler = new $action_class($type);
375                 
376                 // register action handler to parser
377                 $actions = $handler->getDefinedActions($type);
378                 $parser = new Parser($actions, $handler);
379                 
380                 $handler->setParser($parser);
381                 $handler->setSkin($this);
382                 $parser->parse($contents);
383                 
384                 $data = array(
385                         'skin' => &$this,
386                         'type' => $type
387                 );
388                 $manager->notify("Post{$this->event_identifier}Parse", $data);
389                 
390                 return;
391         }
392         
393         /**
394          * Skin::getContentFromDB()
395          * 
396          * @param       string  $skintype       skin type
397          * @return      string  content for the skin type
398          */
399         public function getContentFromDB($skintype)
400         {
401                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
402                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
403                 $res = DB::getValue($query);
404                 
405                 return $res ? $res : '';
406         }
407         
408         /**
409          * Skin::getContentFromFile()
410          * 
411          * @param       string  $fullpath       fullpath to the file to parse
412          * @return      mixed   file contents or FALSE
413          */
414         public function getContentFromFile($fullpath)
415         {
416                 $fsize = filesize($fullpath);
417                 if ( $fsize <= 0 )
418                 {
419                         return;
420                 }
421                 
422                 $fd = fopen ($fullpath, 'r');
423                 if ( $fd === FALSE )
424                 {
425                         return FALSE;
426                 }
427                 
428                 $contents = fread ($fd, $fsize);
429                 if ( $contents === FALSE )
430                 {
431                         return FALSE;
432                 }
433                 
434                 fclose ($fd);
435                 return $contents;
436         }
437         
438         /**
439          * SKIN::update()
440          * Updates the contents for one part of the skin in the database
441          * 
442          * @param       string  $type type of the skin part (e.g. index, item, search ...) 
443          * @param       string  $content new content for this skin part
444          * @return      void
445          * 
446          */
447         public function update($type, $content)
448         {
449                 global $manager;
450                 
451                 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
452                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
453                 $res = DB::getValue($query);
454                 
455                 $skintypeexists = !empty($res);
456                 $skintypevalue = ($content == true);
457                 
458                 if( $skintypevalue && $skintypeexists )
459                 {
460                         $data = array(
461                                 'skinid'        =>  $this->id,
462                                 'type'          =>  $type,
463                                 'content'       => &$content
464                         );
465                         
466                         // PreUpdateSkinPart event
467                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
468                 }
469                 else if( $skintypevalue && !$skintypeexists )
470                 {
471                         $data = array(
472                                 'skinid'        => $this->id,
473                                 'type'          => $type,
474                                 'content'       => &$content
475                         );
476                         
477                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);
478                 }
479                 else if( !$skintypevalue && $skintypeexists )
480                 {
481                         $data = array(
482                                 'skinid'        => $this->id,
483                                 'type'          => $type
484                         );
485                         
486                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);
487                 }
488                 
489                 // delete old thingie
490                 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d";
491                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
492                 DB::execute($query);
493                 
494                 // write new thingie
495                 if ( $content )
496                 {
497                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
498                         $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
499                         DB::execute($query);
500                 }
501                 
502                 if( $skintypevalue && $skintypeexists )
503                 {
504                         $data = array(
505                                 'skinid'        => $this->id,
506                                 'type'          => $type,
507                                 'content'       => &$content
508                         );
509                         
510                         // PostUpdateSkinPart event
511                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
512                 }
513                 else if( $skintypevalue && (!$skintypeexists) )
514                 {
515                         $data = array(
516                                 'skinid'        => $this->id,
517                                 'type'          => $type,
518                                 'content'       => &$content
519                         );
520                         
521                         // PostAddSkinPart event
522                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);
523                 }
524                 else if( (!$skintypevalue) && $skintypeexists )
525                 {
526                         $data = array(
527                                 'skinid'        => $this->id,
528                                 'type'          => $type
529                         );
530                         
531                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);
532                 }
533                 return;
534         }
535         
536         /**
537          * Skin::deleteAllParts()
538          * Deletes all skin parts from the database
539          * 
540          * @param       void
541          * @return      void
542          */
543         public function deleteAllParts()
544         {
545                 $query = "DELETE FROM %s WHERE sdesc=%d;";
546                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
547                 DB::execute($query);
548         }
549         
550         /**
551          * Skin::updateGeneralInfo()
552          * Updates the general information about the skin
553          * 
554          * @param       string  $name                   name of the skin
555          * @param       string  $desc                   description of the skin
556          * @param       string  $type                   type of the skin
557          * @param       string  $includeMode    include mode of the skin
558          * @param       string  $includePrefix  include prefix of the skin
559          * @return      void
560          */
561         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
562         {
563                 $name                   = DB::quoteValue($name);
564                 $desc                   = DB::quoteValue($desc);
565                 $type                   = DB::quoteValue($type);
566                 $includeMode    = DB::quoteValue($includeMode);
567                 $includePrefix  = DB::quoteValue($includePrefix);
568                 
569                 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
570                 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
571                 
572                 DB::execute($query);
573                 return;
574         }
575         
576         /**
577          * Skin::includeTranslation()
578          * 
579          * @param       void
580          * @return      void
581          */
582         private function includeTranslation()
583         {
584                 global $DIR_SKINS;
585                 
586                 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
587                 
588                 if( $this->includeMode == "normal" )
589                 {
590                         $filename = "./locales/{$locale}.php";
591                 }
592                 else if( $this->includeMode == "skindir" )
593                 {
594                         if ( $this->includePrefix == '' )
595                         {
596                                 $filename = "{$DIR_SKINS}locales/{$locale}.php";
597                         }
598                         else
599                         {
600                                 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
601                         }
602                 }
603                 else
604                 {
605                         return;
606                 }
607                 
608                 if ( !file_exists($filename) )
609                 {
610                         return;
611                 }
612                 
613                 include_once($filename);
614                 
615                 return;
616         }
617         
618         /**
619          * Skin::getDefaultTypes()
620          * 
621          * @param       string  void
622          * @return      array   default skin types
623          */
624         public function getDefaultTypes()
625         {
626                 return call_user_func(array($this->action_class, 'getDefaultSkinTypes'));
627         }
628         
629         /**
630          * Skin::getAvailableTypes()
631          * 
632          * @param       string  void
633          * @return      array   registered skin types
634          */
635         public function getAvailableTypes()
636         {
637                 $default_skintypes = $this->getDefaultTypes();
638                 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
639                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
640                 
641                 /* NOTE: force to put default types in the beginning */
642                 $in_default = array();
643                 $no_default = array();
644                 
645                 $res = DB::getResult($query);
646                 foreach ( $res as $row )
647                 {
648                         if ( !array_key_exists($row['stype'], $default_skintypes) )
649                         {
650                                 $no_default[$row['stype']] = FALSE;
651                         }
652                         else
653                         {
654                                 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
655                         }
656                 }
657                 
658                 return array_merge($in_default, $no_default);
659         }
660         
661         /**
662          * Skin::getAllowedActionsForType()
663          * Get the allowed actions for a skin type
664          * returns an array with the allowed actions
665          * 
666          * @param       string  $type   type of the skin
667          * @return      array   allowed action types
668          */
669         public function getAllowedActionsForType($type)
670         {
671                 return call_user_func(array($this->action_class, 'getDefinedActions'), $type);
672         }
673         
674 }