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));
335 sendContentType($this->getContentType(), 'skin');
337 // set skin name as global var (so plugins can access it)
338 $currentSkinName = $this->getName();
341 if ( $type != 'fileparse' )
343 $contents = $this->getContent($type);
345 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
347 $contents = $this->getFileContent($path);
352 // use base skin if this skin does not have contents
353 $defskin = new SKIN($CONF['BaseSkin']);
354 $contents = $defskin->getContent($type);
362 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
364 // set IncludeMode properties of parser
365 Parser::setProperty('IncludeMode', $this->getIncludeMode());
366 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
368 $action_class = $this->action_class;
369 $handler = new $action_class($type);
371 $actions = $handler->getDefinedActions($type);
372 $parser = new Parser($actions, $handler);
374 $handler->setParser($parser);
375 $handler->setSkin($this);
376 $parser->parse($contents);
378 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
384 * Get content of the skin part from the database
386 * @param string $type type of the skin (e.g. index, item, search ...)
387 * @return string content of scontent
389 public function getContent($type)
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);
395 if ( sql_num_rows($res) == 0 )
399 return sql_result($res, 0, 0);
403 * Skin::getFileContent()
405 * @param string $fullpath fullpath to the file to parse
406 * @return mixed file contents or FALSE
408 public function getFileContent($fullpath)
410 $fsize = filesize($fullpath);
416 $fd = fopen ($fullpath, 'r');
422 $contents = fread ($fd, $fsize);
423 if ( $contents === FALSE )
434 * Updates the contents for one part of the skin in the database
436 * @param string $type type of the skin part (e.g. index, item, search ...)
437 * @param string $content new content for this skin part
441 public function update($type, $content)
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);
449 $skintypeexists = sql_fetch_object($res);
450 $skintypevalue = ($content == true);
452 if( $skintypevalue && $skintypeexists )
455 'skinid' => $this->id,
457 'content' => &$content
460 // PreUpdateSkinPart event
461 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
463 else if( $skintypevalue && !$skintypeexists )
466 'skinid' => $this->id,
468 'content' => &$content
471 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
473 else if( !$skintypevalue && $skintypeexists )
476 'skinid' => $this->id,
480 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
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);
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);
496 if( $skintypevalue && $skintypeexists )
499 'skinid' => $this->id,
501 'content' => &$content
504 // PostUpdateSkinPart event
505 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
507 else if( $skintypevalue && (!$skintypeexists) )
510 'skinid' => $this->id,
512 'content' => &$content
515 // PostAddSkinPart event
516 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
518 else if( (!$skintypevalue) && $skintypeexists )
521 'skinid' => $this->id,
525 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
531 * Skin::deleteAllParts()
532 * Deletes all skin parts from the database
537 public function deleteAllParts()
539 $query = "DELETE FROM %s WHERE sdesc=%d;";
540 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
545 * Skin::updateGeneralInfo()
546 * Updates the general information about the skin
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
555 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
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);
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);
571 * Skin::getDefaultTypes()
574 * @return array default skin types
576 public function getDefaultTypes()
578 return call_user_func(array($this->action_class, 'getDefaultSkinTypes'));
582 * Skin::getAvailableTypes()
585 * @return array registered skin types
587 public function getAvailableTypes()
589 $default_skintypes = $this->getDefaultTypes();
590 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
591 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
593 /* NOTE: force to put default types in the beginning */
594 $in_default = array();
595 $no_default = array();
597 $res = sql_query($query);
598 while ( $row = sql_fetch_array($res) )
600 if ( !array_key_exists($row['stype'], $default_skintypes) )
602 $no_default[$row['stype']] = FALSE;
606 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
610 return array_merge($in_default, $no_default);
614 * Skin::getAllowedActionsForType()
615 * Get the allowed actions for a skin type
616 * returns an array with the allowed actions
618 * @param string $type type of the skin
619 * @return array allowed action types
621 public function getAllowedActionsForType($type)
623 return call_user_func(array($this->action_class, 'getDefinedActions'), $type);