OSDN Git Service

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