OSDN Git Service

CHANGE/REMOVE: <%text%>タグをスキンのどのコンテクストでも利用可能に。NP_Textの廃止。
[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 1784 2012-04-22 04:28:30Z 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                 $manager->notify(
281                         'PreAddSkin',
282                         array(
283                                 'name' => &$name,
284                                 'description' => &$desc,
285                                 'type' => &$type,
286                                 'includeMode' => &$includeMode,
287                                 'includePrefix' => &$includePrefix
288                         )
289                 );
290                 
291                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
292                 $sdname         = DB::quoteValue($name);
293                 $sddesc         = DB::quoteValue($desc);
294                 $sdtype         = DB::quoteValue($type);
295                 $sdincmode      = DB::quoteValue($includeMode);
296                 $sdincpref      = DB::quoteValue($includePrefix);
297                 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
298                 DB::execute($query);
299                 $newid = DB::getInsertId();
300                 
301                 $manager->notify(
302                         'PostAddSkin',
303                         array(
304                                 'skinid'                => $newid,
305                                 'name'                  => $name,
306                                 'description'   => $desc,
307                                 'type'                  => $type,
308                                 'includeMode'   => $includeMode,
309                                 'includePrefix' => $includePrefix
310                         )
311                 );
312                 return $newid;
313         }
314         
315         /**
316          * Skin::parse()
317          * Parse a SKIN
318          * 
319          * @param       string  $type
320          * @param       string  $path   path to file if using fileparser
321          * @return      void
322          */
323         public function parse($type, $path='')
324         {
325                 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
326                 
327                 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
328                 
329                 // include skin locale file for <%text%> tag if useable
330                 $this->includeLocaleFile(i18n::get_current_locale());
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                 
338                 $contents = FALSE;
339                 if ( $type != 'fileparse' )
340                 {
341                         $contents = $this->getContent($type);
342                 }
343                 else if ( $path !== ''  && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
344                 {
345                         $contents = $this->getFileContent($path);
346                 }
347                 
348                 if ( !$contents )
349                 {
350                         // use base skin if this skin does not have contents
351                         $defskin = new SKIN($CONF['BaseSkin']);
352                         $contents = $defskin->getContent($type);
353                         if ( !$contents )
354                         {
355                                 echo _ERROR_SKIN;
356                                 return;
357                         }
358                 }
359                 
360                 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
361                 
362                 // set IncludeMode properties of parser
363                 Parser::setProperty('IncludeMode', $this->getIncludeMode());
364                 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
365                 
366                 $action_class = $this->action_class;
367                 $handler = new $action_class($type);
368                 
369                 $actions = $handler->getDefinedActions($type);
370                 $parser = new Parser($actions, $handler);
371                 
372                 $handler->setParser($parser);
373                 $handler->setSkin($this);
374                 $parser->parse($contents);
375                 
376                 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
377                 return;
378         }
379         
380         /**
381          * Skin::getContent()
382          * Get content of the skin part from the database
383          * 
384          * @param       string  $type   type of the skin (e.g. index, item, search ...)
385          * @return      string  content of scontent
386          */
387         public function getContent($type)
388         {
389                 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
390                 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($type));
391                 $res = DB::getValue($query);
392                 
393                 return $res ? $res : '';
394         }
395         
396         /**
397          * Skin::getFileContent()
398          * 
399          * @param       string  $fullpath       fullpath to the file to parse
400          * @return      mixed   file contents or FALSE
401          */
402         public function getFileContent($fullpath)
403         {
404                 $fsize = filesize($fullpath);
405                 if ( $fsize <= 0 )
406                 {
407                         return;
408                 }
409                 
410                 $fd = fopen ($fullpath, 'r');
411                 if ( $fd === FALSE )
412                 {
413                         return FALSE;
414                 }
415                 
416                 $contents = fread ($fd, $fsize);
417                 if ( $contents === FALSE )
418                 {
419                         return FALSE;
420                 }
421                 
422                 fclose ($fd);
423                 return $contents;
424         }
425         
426         /**
427          * SKIN::update()
428          * Updates the contents for one part of the skin in the database
429          * 
430          * @param       string  $type type of the skin part (e.g. index, item, search ...) 
431          * @param       string  $content new content for this skin part
432          * @return      void
433          * 
434          */
435         public function update($type, $content)
436         {
437                 global $manager;
438                 
439                 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
440                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
441                 $res = DB::getValue($query);
442                 
443                 $skintypeexists = !empty($res);
444                 $skintypevalue = ($content == true);
445                 
446                 if( $skintypevalue && $skintypeexists )
447                 {
448                         $data = array(
449                                 'skinid'        =>  $this->id,
450                                 'type'          =>  $type,
451                                 'content'       => &$content
452                         );
453                         
454                         // PreUpdateSkinPart event
455                         $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
456                 }
457                 else if( $skintypevalue && !$skintypeexists )
458                 {
459                         $data = array(
460                                 'skinid' => $this->id,
461                                 'type' => $type,
462                                 'content' => &$content
463                         );
464                         
465                         $manager->notify("PreAdd{$this->event_identifier}Part", $data);
466                 }
467                 else if( !$skintypevalue && $skintypeexists )
468                 {
469                         $data = array(
470                                 'skinid' => $this->id,
471                                 'type' => $type
472                         );
473                         
474                         $manager->notify("PreDelete{$this->event_identifier}Part", $data);
475                 }
476                 
477                 // delete old thingie
478                 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d";
479                 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
480                 DB::execute($query);
481                 
482                 // write new thingie
483                 if ( $content )
484                 {
485                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
486                         $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
487                         DB::execute($query);
488                 }
489                 
490                 if( $skintypevalue && $skintypeexists )
491                 {
492                         $data = array(
493                                 'skinid'        => $this->id,
494                                 'type'          => $type,
495                                 'content'       => &$content
496                         );
497                         
498                         // PostUpdateSkinPart event
499                         $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
500                 }
501                 else if( $skintypevalue && (!$skintypeexists) )
502                 {
503                         $data = array(
504                                 'skinid'        => $this->id,
505                                 'type'          => $type,
506                                 'content'       => &$content
507                         );
508                         
509                         // PostAddSkinPart event
510                         $manager->notify("PostAdd{$this->event_identifier}Part", $data);
511                 }
512                 else if( (!$skintypevalue) && $skintypeexists )
513                 {
514                         $data = array(
515                                 'skinid'        => $this->id,
516                                 'type'          => $type
517                         );
518                         
519                         $manager->notify("PostDelete{$this->event_identifier}Part", $data);
520                 }
521                 return;
522         }
523         
524         /**
525          * Skin::deleteAllParts()
526          * Deletes all skin parts from the database
527          * 
528          * @param       void
529          * @return      void
530          */
531         public function deleteAllParts()
532         {
533                 $query = "DELETE FROM %s WHERE sdesc=%d;";
534                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
535                 DB::execute($query);
536         }
537         
538         /**
539          * Skin::updateGeneralInfo()
540          * Updates the general information about the skin
541          * 
542          * @param       string  $name                   name of the skin
543          * @param       string  $desc                   description of the skin
544          * @param       string  $type                   type of the skin
545          * @param       string  $includeMode    include mode of the skin
546          * @param       string  $includePrefix  include prefix of the skin
547          * @return      void
548          */
549         public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
550         {
551                 $name                   = DB::quoteValue($name);
552                 $desc                   = DB::quoteValue($desc);
553                 $type                   = DB::quoteValue($type);
554                 $includeMode    = DB::quoteValue($includeMode);
555                 $includePrefix  = DB::quoteValue($includePrefix);
556                 
557                 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
558                 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
559                 
560                 DB::execute($query);
561                 return;
562         }
563         
564         /**
565          * Skin::includeLocaleFile()
566          * 
567          * @param       string  $locale locale name
568          * @return      void
569          */
570         private function includeLocaleFile($locale)
571         {
572                 global $DIR_SKINS;
573                 
574                 if( $this->includeMode == "normal" )
575                 {
576                         $filename = "./locale/{$locale}.php";
577                 }
578                 elseif( $this->includeMode == "skindir" )
579                 {
580                         if ( $this->includePrefix == '' )
581                         {
582                                 $filename = "{$DIR_SKINS}locale/{$locale}.php";
583                         }
584                         else
585                         {
586                                 $filename = "{$DIR_SKINS}{$this->includePrefix}locale/{$locale}.php";
587                         }
588                 }
589                 else
590                 {
591                         return;
592                 }
593                 
594                 if ( !file_exists($filename) )
595                 {
596                         return;
597                 }
598                 
599                 include_once($filename);
600                 
601                 return;
602         }
603         
604         /**
605          * Skin::getDefaultTypes()
606          * 
607          * @param       string  void
608          * @return      array   default skin types
609          */
610         public function getDefaultTypes()
611         {
612                 return call_user_func(array($this->action_class, 'getDefaultSkinTypes'));
613         }
614         
615         /**
616          * Skin::getAvailableTypes()
617          * 
618          * @param       string  void
619          * @return      array   registered skin types
620          */
621         public function getAvailableTypes()
622         {
623                 $default_skintypes = $this->getDefaultTypes();
624                 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
625                 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
626                 
627                 /* NOTE: force to put default types in the beginning */
628                 $in_default = array();
629                 $no_default = array();
630                 
631                 $res = DB::getResult($query);
632                 foreach ( $res as $row )
633                 {
634                         if ( !array_key_exists($row['stype'], $default_skintypes) )
635                         {
636                                 $no_default[$row['stype']] = FALSE;
637                         }
638                         else
639                         {
640                                 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
641                         }
642                 }
643                 
644                 return array_merge($in_default, $no_default);
645         }
646         
647         /**
648          * Skin::getAllowedActionsForType()
649          * Get the allowed actions for a skin type
650          * returns an array with the allowed actions
651          * 
652          * @param       string  $type   type of the skin
653          * @return      array   allowed action types
654          */
655         public function getAllowedActionsForType($type)
656         {
657                 return call_user_func(array($this->action_class, 'getDefinedActions'), $type);
658         }
659         
660 }