OSDN Git Service

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