3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
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)
13 * Class representing a skin
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 $
20 if ( !function_exists('requestVar') )
27 // after creating a SKIN object, evaluates to true when the skin exists
30 // skin characteristics. Use the getXXX methods rather than accessing directly
34 private $includeMode; // either 'normal' or 'skindir'
35 private $includePrefix;
39 private $action_class;
40 private $event_identifier;
44 * Constructor for a new SKIN object
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
51 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
55 $this->id = (integer) $id;
58 * NOTE: include needed action class
60 if ( $action_class != 'Actions' )
62 if ( !class_exists($action_class, FALSE)
63 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
64 || !include("{$DIR_LIBS}{$action_class}.php")) )
71 if ( !class_exists('Actions', FALSE)
72 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
73 || !include("{$DIR_LIBS}ACTIONS.php")) )
79 $this->action_class = $action_class;
80 $this->event_identifier = $event_identifier;
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);
88 $this->valid = (sql_num_rows($res) > 0);
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;
108 * @return integer id for this skin instance
110 public function getID()
112 return (integer) $this->id;
121 public function isValid()
123 return (boolean) $this->valid;
131 * @return string name of this skin instance
133 public function getName()
135 return (string) $this->name;
139 * Skin::getDescription()
140 * Get SKIN description
143 * @return string description of this skin instance
145 public function getDescription()
147 return (string) $this->description;
151 * Skin::getContentType()
152 * Get SKIN content type
153 * e.g. text/xml, text/html, application/atom+xml
156 * @return string name of this skin instance
158 public function getContentType()
160 return (string) $this->contentType;
164 * Skin::getIncludeMode()
165 * Get include mode of the SKIN
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
172 * @return string normal/skindir
174 public function getIncludeMode()
176 return (string) $this->includeMode;
180 * Skin::getIncludePrefix()
181 * Get include prefix of the SKIN
183 * Get name of the subdirectory (with trailing slash) where
184 * the files of the current skin can be found (e.g. 'default/')
187 * @return string include prefix of this skin instance
189 public function getIncludePrefix()
191 return (string) $this->includePrefix;
196 * Checks if a skin with a given shortname exists
199 * @param string $name Skin short name
200 * @return integer number of skins with the given ID
202 static public function exists($name)
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);
211 * Checks if a skin with a given ID exists
214 * @param string $id Skin ID
215 * @return integer number of skins with the given ID
217 static public function existsID($id)
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);
225 * Skin::createFromName()
226 * Returns a skin given its shortname
229 * @param string $name Skin shortname
230 * @return object instance of Skin class
232 static public function createFromName($name)
234 return new SKIN(SKIN::getIdFromName($name));
238 * Skin::getIdFromName()
239 * Returns a skin ID given its shortname
242 * @param string $name Skin shortname
243 * @return integer Skin ID
245 static public function getIdFromName($name)
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;
255 * Skin::getNameFromId()
256 * Returns a skin shortname given its ID
259 * @param string $name
260 * @return string Skin short name
262 static public function getNameFromId($id)
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);
271 * Creates a new skin, with the given characteristics.
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
281 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
289 'description' => &$desc,
291 'includeMode' => &$includeMode,
292 'includePrefix' => &$includePrefix
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);
304 $newid = sql_insert_id();
311 'description' => $desc,
313 'includeMode' => $includeMode,
314 'includePrefix' => $includePrefix
324 * @param string $type
325 * @param string $path path to file if using fileparser
328 public function parse($type, $path='')
330 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
332 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
334 // include skin locale file for <%text%> tag if useable
335 $this->includeLocaleFile(i18n::get_current_locale());
338 sendContentType($this->getContentType(), 'skin');
340 // set skin name as global var (so plugins can access it)
341 $currentSkinName = $this->getName();
344 if ( $type != 'fileparse' )
346 $contents = $this->getContent($type);
348 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
350 $contents = $this->getFileContent($path);
355 // use base skin if this skin does not have contents
356 $defskin = new SKIN($CONF['BaseSkin']);
357 $contents = $defskin->getContent($type);
365 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
367 // set IncludeMode properties of parser
368 Parser::setProperty('IncludeMode', $this->getIncludeMode());
369 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
371 $action_class = $this->action_class;
372 $handler = new $action_class($type);
374 $actions = $handler->getDefinedActions($type);
375 $parser = new Parser($actions, $handler);
377 $handler->setParser($parser);
378 $handler->setSkin($this);
379 $parser->parse($contents);
381 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
387 * Get content of the skin part from the database
389 * @param string $type type of the skin (e.g. index, item, search ...)
390 * @return string content of scontent
392 public function getContent($type)
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);
398 if ( sql_num_rows($res) == 0 )
402 return sql_result($res, 0, 0);
406 * Skin::getFileContent()
408 * @param string $fullpath fullpath to the file to parse
409 * @return mixed file contents or FALSE
411 public function getFileContent($fullpath)
413 $fsize = filesize($fullpath);
419 $fd = fopen ($fullpath, 'r');
425 $contents = fread ($fd, $fsize);
426 if ( $contents === FALSE )
437 * Updates the contents for one part of the skin in the database
439 * @param string $type type of the skin part (e.g. index, item, search ...)
440 * @param string $content new content for this skin part
444 public function update($type, $content)
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);
452 $skintypeexists = sql_fetch_object($res);
453 $skintypevalue = ($content == true);
455 if( $skintypevalue && $skintypeexists )
458 'skinid' => $this->id,
460 'content' => &$content
463 // PreUpdateSkinPart event
464 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
466 else if( $skintypevalue && !$skintypeexists )
469 'skinid' => $this->id,
471 'content' => &$content
474 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
476 else if( !$skintypevalue && $skintypeexists )
479 'skinid' => $this->id,
483 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
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);
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);
499 if( $skintypevalue && $skintypeexists )
502 'skinid' => $this->id,
504 'content' => &$content
507 // PostUpdateSkinPart event
508 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
510 else if( $skintypevalue && (!$skintypeexists) )
513 'skinid' => $this->id,
515 'content' => &$content
518 // PostAddSkinPart event
519 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
521 else if( (!$skintypevalue) && $skintypeexists )
524 'skinid' => $this->id,
528 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
534 * Skin::deleteAllParts()
535 * Deletes all skin parts from the database
540 public function deleteAllParts()
542 $query = "DELETE FROM %s WHERE sdesc=%d;";
543 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
548 * Skin::updateGeneralInfo()
549 * Updates the general information about the skin
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
558 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
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);
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);
574 * Skin::includeLocaleFile()
576 * @param string $locale locale name
579 private function includeLocaleFile($locale)
583 if( $this->includeMode == "normal" )
585 $filename = "./locale/{$locale}.php";
587 elseif( $this->includeMode == "skindir" )
589 if ( $this->includePrefix == '' )
591 $filename = "{$DIR_SKINS}locale/{$locale}.php";
595 $filename = "{$DIR_SKINS}{$this->includePrefix}locale/{$locale}.php";
603 if ( !file_exists($filename) )
608 include_once($filename);
614 * Skin::getDefaultTypes()
617 * @return array default skin types
619 public function getDefaultTypes()
621 return call_user_func(array($this->action_class, 'getDefaultSkinTypes'));
625 * Skin::getAvailableTypes()
628 * @return array registered skin types
630 public function getAvailableTypes()
632 $default_skintypes = $this->getDefaultTypes();
633 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
634 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
636 /* NOTE: force to put default types in the beginning */
637 $in_default = array();
638 $no_default = array();
640 $res = sql_query($query);
641 while ( $row = sql_fetch_array($res) )
643 if ( !array_key_exists($row['stype'], $default_skintypes) )
645 $no_default[$row['stype']] = FALSE;
649 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
653 return array_merge($in_default, $no_default);
657 * Skin::getAllowedActionsForType()
658 * Get the allowed actions for a skin type
659 * returns an array with the allowed actions
661 * @param string $type type of the skin
662 * @return array allowed action types
664 public function getAllowedActionsForType($type)
666 return call_user_func(array($this->action_class, 'getDefinedActions'), $type);