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 1779 2012-04-21 10:04:33Z 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;
57 // read skin name/description/content type
58 $query = "SELECT * FROM %s WHERE sdnumber=%d";
59 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);
60 $res = sql_query($query);
61 $obj = sql_fetch_object($res);
62 $this->valid = (sql_num_rows($res) > 0);
69 * NOTE: include needed action class
71 if ( $action_class != 'Actions' )
73 if ( !class_exists($action_class, FALSE)
74 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
75 || !include("{$DIR_LIBS}{$action_class}.php")) )
82 if ( !class_exists('Actions', FALSE)
83 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
84 || !include("{$DIR_LIBS}ACTIONS.php")) )
90 $this->action_class = $action_class;
91 $this->event_identifier = $event_identifier;
93 $this->name = $obj->sdname;
94 $this->description = $obj->sddesc;
95 $this->contentType = $obj->sdtype;
96 $this->includeMode = $obj->sdincmode;
97 $this->includePrefix = $obj->sdincpref;
107 * @return integer id for this skin instance
109 public function getID()
111 return (integer) $this->id;
120 public function isValid()
122 return (boolean) $this->valid;
130 * @return string name of this skin instance
132 public function getName()
134 return (string) $this->name;
138 * Skin::getDescription()
139 * Get SKIN description
142 * @return string description of this skin instance
144 public function getDescription()
146 return (string) $this->description;
150 * Skin::getContentType()
151 * Get SKIN content type
152 * e.g. text/xml, text/html, application/atom+xml
155 * @return string name of this skin instance
157 public function getContentType()
159 return (string) $this->contentType;
163 * Skin::getIncludeMode()
164 * Get include mode of the SKIN
166 * Returns either 'normal' or 'skindir':
167 * 'normal': if a all data of the skin can be found in the databse
168 * 'skindir': if the skin has data in the it's skin driectory
171 * @return string normal/skindir
173 public function getIncludeMode()
175 return (string) $this->includeMode;
179 * Skin::getIncludePrefix()
180 * Get include prefix of the SKIN
182 * Get name of the subdirectory (with trailing slash) where
183 * the files of the current skin can be found (e.g. 'default/')
186 * @return string include prefix of this skin instance
188 public function getIncludePrefix()
190 return (string) $this->includePrefix;
195 * Checks if a skin with a given shortname exists
198 * @param string $name Skin short name
199 * @return integer number of skins with the given ID
201 static public function exists($name)
203 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";
204 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
205 return (quickQuery($query) > 0);
210 * Checks if a skin with a given ID exists
213 * @param string $id Skin ID
214 * @return integer number of skins with the given ID
216 static public function existsID($id)
218 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
219 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
220 return (quickQuery($query) > 0);
224 * Skin::createFromName()
225 * Returns a skin given its shortname
228 * @param string $name Skin shortname
229 * @return object instance of Skin class
231 static public function createFromName($name)
233 return new SKIN(SKIN::getIdFromName($name));
237 * Skin::getIdFromName()
238 * Returns a skin ID given its shortname
241 * @param string $name Skin shortname
242 * @return integer Skin ID
244 static public function getIdFromName($name)
246 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";
247 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
248 $res = sql_query($query);
249 $obj = sql_fetch_object($res);
250 return $obj->sdnumber;
254 * Skin::getNameFromId()
255 * Returns a skin shortname given its ID
258 * @param string $name
259 * @return string Skin short name
261 static public function getNameFromId($id)
263 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
264 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
265 return quickQuery($query);
270 * Creates a new skin, with the given characteristics.
273 * @param String $name value for nucleus_skin.sdname
274 * @param String $desc value for nucleus_skin.sddesc
275 * @param String $type value for nucleus_skin.sdtype
276 * @param String $includeMode value for nucleus_skin.sdinclude
277 * @param String $includePrefix value for nucleus_skin.sdincpref
278 * @return Integer ID for just inserted record
280 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
288 'description' => &$desc,
290 'includeMode' => &$includeMode,
291 'includePrefix' => &$includePrefix
295 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";
296 $sdname = sql_real_escape_string($name);
297 $sddesc = sql_real_escape_string($desc);
298 $sdtype = sql_real_escape_string($type);
299 $sdincmode = sql_real_escape_string($includeMode);
300 $sdincpref = sql_real_escape_string($includePrefix);
301 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
303 $newid = sql_insert_id();
310 'description' => $desc,
312 'includeMode' => $includeMode,
313 'includePrefix' => $includePrefix
323 * @param string $type
324 * @param string $path path to file if using fileparser
327 public function parse($type, $path='')
329 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
331 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
334 sendContentType($this->getContentType(), 'skin');
336 // set skin name as global var (so plugins can access it)
337 $currentSkinName = $this->getName();
340 if ( $type != 'fileparse' )
342 $contents = $this->getContent($type);
344 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
346 $contents = $this->getFileContent($path);
351 // use base skin if this skin does not have contents
352 $defskin = new SKIN($CONF['BaseSkin']);
353 $contents = $defskin->getContent($type);
361 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
363 // set IncludeMode properties of parser
364 Parser::setProperty('IncludeMode', $this->getIncludeMode());
365 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
367 $action_class = $this->action_class;
368 $handler = new $action_class($type);
370 $actions = $handler->getDefinedActions($type);
371 $parser = new Parser($actions, $handler);
373 $handler->setParser($parser);
374 $handler->setSkin($this);
375 $parser->parse($contents);
377 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
383 * Get content of the skin part from the database
385 * @param string $type type of the skin (e.g. index, item, search ...)
386 * @return string content of scontent
388 public function getContent($type)
390 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
391 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));
392 $res = sql_query($query);
394 if ( sql_num_rows($res) == 0 )
398 return sql_result($res, 0, 0);
402 * Skin::getFileContent()
404 * @param string $fullpath fullpath to the file to parse
405 * @return mixed file contents or FALSE
407 public function getFileContent($fullpath)
409 $fsize = filesize($fullpath);
415 $fd = fopen ($fullpath, 'r');
421 $contents = fread ($fd, $fsize);
422 if ( $contents === FALSE )
433 * Updates the contents for one part of the skin in the database
435 * @param string $type type of the skin part (e.g. index, item, search ...)
436 * @param string $content new content for this skin part
440 public function update($type, $content)
444 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
445 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
446 $res = sql_query($query);
448 $skintypeexists = sql_fetch_object($res);
449 $skintypevalue = ($content == true);
451 if( $skintypevalue && $skintypeexists )
454 'skinid' => $this->id,
456 'content' => &$content
459 // PreUpdateSkinPart event
460 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
462 else if( $skintypevalue && !$skintypeexists )
465 'skinid' => $this->id,
467 'content' => &$content
470 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
472 else if( !$skintypevalue && $skintypeexists )
475 'skinid' => $this->id,
479 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
482 // delete old thingie
483 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
484 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
490 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
491 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
495 if( $skintypevalue && $skintypeexists )
498 'skinid' => $this->id,
500 'content' => &$content
503 // PostUpdateSkinPart event
504 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
506 else if( $skintypevalue && (!$skintypeexists) )
509 'skinid' => $this->id,
511 'content' => &$content
514 // PostAddSkinPart event
515 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
517 else if( (!$skintypevalue) && $skintypeexists )
520 'skinid' => $this->id,
524 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
530 * Skin::deleteAllParts()
531 * Deletes all skin parts from the database
536 public function deleteAllParts()
538 $query = "DELETE FROM %s WHERE sdesc=%d;";
539 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
544 * Skin::updateGeneralInfo()
545 * Updates the general information about the skin
547 * @param string $name name of the skin
548 * @param string $desc description of the skin
549 * @param string $type type of the skin
550 * @param string $includeMode include mode of the skin
551 * @param string $includePrefix include prefix of the skin
554 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
556 $name = sql_real_escape_string($name);
557 $desc = sql_real_escape_string($desc);
558 $type = sql_real_escape_string($type);
559 $includeMode = sql_real_escape_string($includeMode);
560 $includePrefix = sql_real_escape_string($includePrefix);
562 $query ="UPDATE %s SET sdname='%s', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d";
563 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
570 * Skin::getAllowedActionsForType()
571 * Get the allowed actions for a skin type
572 * returns an array with the allowed actions
574 * @param string $type type of the skin (e.g. index, item, search ...)
575 * @return array allowed action types
577 public function getAllowedActionsForType($type)
580 * NOTE: static method with variable class name is supported since PHP 5.3
581 * So now we utilize eval function.
583 $page_action_names = array();
584 eval("\$defined_actions = {$this->action_class}::getDefinedActions('{$type}');");
585 return $defined_actions;
589 * Skin::getFriendlyNames()
590 * Get an array with the names of possible skin parts
591 * Used to show all possible parts of a skin in the administration backend
594 * @param string $action_class name of action class (optional)
595 * @param array type of the skin
597 public function getFriendlyNames($action_class='Actions')
600 * NOTE: static method with variable class name is supported since PHP 5.3
601 * So now we utilize eval function.
603 eval("\$friendly_names = {$action_class}::getSkinTypeFriendlyNames();");
604 return $friendly_names;