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