3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
13 * Class representing a skin
\r
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
17 * @version $Id: SKIN.php 1813 2012-05-02 14:41:57Z sakamocchi $
\r
20 if ( !function_exists('requestVar') )
\r
27 // after creating a SKIN object, evaluates to true when the skin exists
\r
30 // skin characteristics. Use the getXXX methods rather than accessing directly
\r
32 private $description;
\r
33 private $contentType;
\r
34 private $includeMode; // either 'normal' or 'skindir'
\r
35 private $includePrefix;
\r
39 private $action_class;
\r
40 private $event_identifier;
\r
43 * Skin::__construct()
\r
44 * Constructor for a new SKIN object
\r
46 * @param integer $id id of the skin
\r
47 * @param string $action_class name of class extended from BaseActions
\r
48 * @param string $event_identifier event identifier. for example, InitAdminSkinParse if AdminSkin is used
\r
51 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
\r
55 $this->id = (integer) $id;
\r
58 * NOTE: include needed action class
\r
60 if ( $action_class != 'Actions' )
\r
62 if ( !class_exists($action_class, FALSE)
\r
63 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
\r
64 || !include("{$DIR_LIBS}{$action_class}.php")) )
\r
71 if ( !class_exists('Actions', FALSE)
\r
72 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
\r
73 || !include("{$DIR_LIBS}ACTIONS.php")) )
\r
79 $this->action_class = $action_class;
\r
80 $this->event_identifier = $event_identifier;
\r
82 // read skin name/description/content type
\r
83 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
\r
84 $query = sprintf($query, sql_table('skin_desc'), $this->id);
\r
85 $res = sql_query($query);
\r
86 $obj = sql_fetch_object($res);
\r
88 $this->valid = (sql_num_rows($res) > 0);
\r
89 if ( !$this->valid )
\r
94 $this->name = $obj->sdname;
\r
95 $this->description = $obj->sddesc;
\r
96 $this->contentType = $obj->sdtype;
\r
97 $this->includeMode = $obj->sdincmode;
\r
98 $this->includePrefix = $obj->sdincpref;
\r
108 * @return integer id for this skin instance
\r
110 public function getID()
\r
112 return (integer) $this->id;
\r
121 public function isValid()
\r
123 return (boolean) $this->valid;
\r
131 * @return string name of this skin instance
\r
133 public function getName()
\r
135 return (string) $this->name;
\r
139 * Skin::getDescription()
\r
140 * Get SKIN description
\r
143 * @return string description of this skin instance
\r
145 public function getDescription()
\r
147 return (string) $this->description;
\r
151 * Skin::getContentType()
\r
152 * Get SKIN content type
\r
153 * e.g. text/xml, text/html, application/atom+xml
\r
156 * @return string name of this skin instance
\r
158 public function getContentType()
\r
160 return (string) $this->contentType;
\r
164 * Skin::getIncludeMode()
\r
165 * Get include mode of the SKIN
\r
167 * Returns either 'normal' or 'skindir':
\r
168 * 'normal': if a all data of the skin can be found in the databse
\r
169 * 'skindir': if the skin has data in the it's skin driectory
\r
172 * @return string normal/skindir
\r
174 public function getIncludeMode()
\r
176 return (string) $this->includeMode;
\r
180 * Skin::getIncludePrefix()
\r
181 * Get include prefix of the SKIN
\r
183 * Get name of the subdirectory (with trailing slash) where
\r
184 * the files of the current skin can be found (e.g. 'default/')
\r
187 * @return string include prefix of this skin instance
\r
189 public function getIncludePrefix()
\r
191 return (string) $this->includePrefix;
\r
196 * Checks if a skin with a given shortname exists
\r
199 * @param string $name Skin short name
\r
200 * @return integer number of skins with the given ID
\r
202 static public function exists($name)
\r
204 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";
\r
205 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
206 return (quickQuery($query) > 0);
\r
211 * Checks if a skin with a given ID exists
\r
214 * @param string $id Skin ID
\r
215 * @return integer number of skins with the given ID
\r
217 static public function existsID($id)
\r
219 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
\r
220 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
221 return (quickQuery($query) > 0);
\r
225 * Skin::createFromName()
\r
226 * Returns a skin given its shortname
\r
229 * @param string $name Skin shortname
\r
230 * @return object instance of Skin class
\r
232 static public function createFromName($name)
\r
234 return new SKIN(SKIN::getIdFromName($name));
\r
238 * Skin::getIdFromName()
\r
239 * Returns a skin ID given its shortname
\r
242 * @param string $name Skin shortname
\r
243 * @return integer Skin ID
\r
245 static public function getIdFromName($name)
\r
247 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";
\r
248 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
249 $res = sql_query($query);
\r
250 $obj = sql_fetch_object($res);
\r
251 return $obj->sdnumber;
\r
255 * Skin::getNameFromId()
\r
256 * Returns a skin shortname given its ID
\r
259 * @param string $name
\r
260 * @return string Skin short name
\r
262 static public function getNameFromId($id)
\r
264 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
\r
265 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
266 return quickQuery($query);
\r
270 * SKIN::createNew()
\r
271 * Creates a new skin, with the given characteristics.
\r
274 * @param String $name value for nucleus_skin.sdname
\r
275 * @param String $desc value for nucleus_skin.sddesc
\r
276 * @param String $type value for nucleus_skin.sdtype
\r
277 * @param String $includeMode value for nucleus_skin.sdinclude
\r
278 * @param String $includePrefix value for nucleus_skin.sdincpref
\r
279 * @return Integer ID for just inserted record
\r
281 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
289 'description' => &$desc,
\r
291 'includeMode' => &$includeMode,
\r
292 'includePrefix' => &$includePrefix
\r
296 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";
\r
297 $sdname = sql_real_escape_string($name);
\r
298 $sddesc = sql_real_escape_string($desc);
\r
299 $sdtype = sql_real_escape_string($type);
\r
300 $sdincmode = sql_real_escape_string($includeMode);
\r
301 $sdincpref = sql_real_escape_string($includePrefix);
\r
302 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
\r
304 $newid = sql_insert_id();
\r
309 'skinid' => $newid,
\r
311 'description' => $desc,
\r
313 'includeMode' => $includeMode,
\r
314 'includePrefix' => $includePrefix
\r
324 * @param string $type
\r
325 * @param string $path path to file if using fileparser
\r
328 public function parse($type, $path='')
\r
330 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
\r
332 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
\r
334 // include skin locale file for <%text%> tag if useable
\r
335 $this->includeTranslation();
\r
338 sendContentType($this->getContentType(), 'skin');
\r
340 /* FIX: should be obsoleted */
\r
341 $currentSkinName = $this->getName();
\r
343 // retrieve contents
\r
345 if ( $type != 'fileparse' )
\r
347 $contents = $this->getContentFromDB($type);
\r
349 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
\r
351 $contents = $this->getContentFromFile($path);
\r
353 // use base skin if this skin does not have contents
\r
354 if ( $contents === FALSE )
\r
356 $defskin = new SKIN($CONF['BaseSkin']);
\r
357 $contents = $defskin->getContentFromDB($type);
\r
365 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
\r
367 // set IncludeMode properties of parser
\r
368 Parser::setProperty('IncludeMode', $this->getIncludeMode());
\r
369 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
\r
371 // call action handler
\r
372 $action_class = $this->action_class;
\r
373 $handler = new $action_class($type);
\r
375 // register action handler to parser
\r
376 $actions = $handler->getDefinedActions($type);
\r
377 $parser = new Parser($actions, $handler);
\r
379 $handler->setParser($parser);
\r
380 $handler->setSkin($this);
\r
381 $parser->parse($contents);
\r
383 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
\r
388 * Skin::getContentFromDB()
\r
390 * @param string $skintype skin type
\r
391 * @return string content for the skin type
\r
393 public function getContentFromDB($skintype)
\r
395 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
\r
396 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($skintype));
\r
397 $res = sql_query($query);
\r
399 if ( sql_num_rows($res) == 0 )
\r
404 return sql_result($res, 0, 0);
\r
408 * Skin::getContentFromFile()
\r
410 * @param string $fullpath fullpath to the file to parse
\r
411 * @return mixed file contents or FALSE
\r
413 public function getContentFromFile($fullpath)
\r
415 $fsize = filesize($fullpath);
\r
421 $fd = fopen ($fullpath, 'r');
\r
422 if ( $fd === FALSE )
\r
427 $contents = fread ($fd, $fsize);
\r
428 if ( $contents === FALSE )
\r
439 * Updates the contents for one part of the skin in the database
\r
441 * @param string $type type of the skin part (e.g. index, item, search ...)
\r
442 * @param string $content new content for this skin part
\r
446 public function update($type, $content)
\r
450 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
\r
451 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
452 $res = sql_query($query);
\r
454 $skintypeexists = sql_fetch_object($res);
\r
455 $skintypevalue = ($content == true);
\r
457 if( $skintypevalue && $skintypeexists )
\r
460 'skinid' => $this->id,
\r
462 'content' => &$content
\r
465 // PreUpdateSkinPart event
\r
466 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
\r
468 else if( $skintypevalue && !$skintypeexists )
\r
471 'skinid' => $this->id,
\r
473 'content' => &$content
\r
476 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
\r
478 else if( !$skintypevalue && $skintypeexists )
\r
481 'skinid' => $this->id,
\r
485 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
\r
488 // delete old thingie
\r
489 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
\r
490 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
493 // write new thingie
\r
496 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
\r
497 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
\r
501 if( $skintypevalue && $skintypeexists )
\r
504 'skinid' => $this->id,
\r
506 'content' => &$content
\r
509 // PostUpdateSkinPart event
\r
510 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
\r
512 else if( $skintypevalue && (!$skintypeexists) )
\r
515 'skinid' => $this->id,
\r
517 'content' => &$content
\r
520 // PostAddSkinPart event
\r
521 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
\r
523 else if( (!$skintypevalue) && $skintypeexists )
\r
526 'skinid' => $this->id,
\r
530 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
\r
536 * Skin::deleteAllParts()
\r
537 * Deletes all skin parts from the database
\r
542 public function deleteAllParts()
\r
544 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
545 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
550 * Skin::updateGeneralInfo()
\r
551 * Updates the general information about the skin
\r
553 * @param string $name name of the skin
\r
554 * @param string $desc description of the skin
\r
555 * @param string $type type of the skin
\r
556 * @param string $includeMode include mode of the skin
\r
557 * @param string $includePrefix include prefix of the skin
\r
560 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
562 $name = sql_real_escape_string($name);
\r
563 $desc = sql_real_escape_string($desc);
\r
564 $type = sql_real_escape_string($type);
\r
565 $includeMode = sql_real_escape_string($includeMode);
\r
566 $includePrefix = sql_real_escape_string($includePrefix);
\r
568 $query ="UPDATE %s SET sdname='%s', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d";
\r
569 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
576 * Skin::includeTranslation()
\r
581 private function includeTranslation()
\r
585 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
\r
587 if( $this->includeMode == "normal" )
\r
589 $filename = "./locales/{$locale}.php";
\r
591 else if( $this->includeMode == "skindir" )
\r
593 if ( $this->includePrefix == '' )
\r
595 $filename = "{$DIR_SKINS}locales/{$locale}.php";
\r
599 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
\r
607 if ( !file_exists($filename) )
\r
612 include_once($filename);
\r
618 * Skin::getDefaultTypes()
\r
620 * @param string void
\r
621 * @return array default skin types
\r
623 public function getDefaultTypes()
\r
625 return call_user_func(array($this->action_class, 'getDefaultSkinTypes'));
\r
629 * Skin::getAvailableTypes()
\r
631 * @param string void
\r
632 * @return array registered skin types
\r
634 public function getAvailableTypes()
\r
636 $default_skintypes = $this->getDefaultTypes();
\r
637 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
\r
638 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
640 /* NOTE: force to put default types in the beginning */
\r
641 $in_default = array();
\r
642 $no_default = array();
\r
644 $res = sql_query($query);
\r
645 while ( $row = sql_fetch_array($res) )
\r
647 if ( !array_key_exists($row['stype'], $default_skintypes) )
\r
649 $no_default[$row['stype']] = FALSE;
\r
653 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
\r
657 return array_merge($in_default, $no_default);
\r
661 * Skin::getAllowedActionsForType()
\r
662 * Get the allowed actions for a skin type
\r
663 * returns an array with the allowed actions
\r
665 * @param string $type type of the skin
\r
666 * @return array allowed action types
\r
668 public function getAllowedActionsForType($type)
\r
670 return call_user_func(array($this->action_class, 'getDefinedActions'), $type);
\r