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 1816 2012-05-03 01:40:10Z 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 = DB::getRow($query);
87 $this->valid = !empty($res);
90 $this->name = $res['sdname'];
91 $this->description = $res['sddesc'];
92 $this->contentType = $res['sdtype'];
93 $this->includeMode = $res['sdincmode'];
94 $this->includePrefix = $res['sdincpref'];
105 * @return integer id for this skin instance
107 public function getID()
109 return (integer) $this->id;
118 public function isValid()
120 return (boolean) $this->valid;
128 * @return string name of this skin instance
130 public function getName()
132 return (string) $this->name;
136 * Skin::getDescription()
137 * Get SKIN description
140 * @return string description of this skin instance
142 public function getDescription()
144 return (string) $this->description;
148 * Skin::getContentType()
149 * Get SKIN content type
150 * e.g. text/xml, text/html, application/atom+xml
153 * @return string name of this skin instance
155 public function getContentType()
157 return (string) $this->contentType;
161 * Skin::getIncludeMode()
162 * Get include mode of the SKIN
164 * Returns either 'normal' or 'skindir':
165 * 'normal': if a all data of the skin can be found in the databse
166 * 'skindir': if the skin has data in the it's skin driectory
169 * @return string normal/skindir
171 public function getIncludeMode()
173 return (string) $this->includeMode;
177 * Skin::getIncludePrefix()
178 * Get include prefix of the SKIN
180 * Get name of the subdirectory (with trailing slash) where
181 * the files of the current skin can be found (e.g. 'default/')
184 * @return string include prefix of this skin instance
186 public function getIncludePrefix()
188 return (string) $this->includePrefix;
193 * Checks if a skin with a given shortname exists
196 * @param string $name Skin short name
197 * @return integer number of skins with the given ID
199 static public function exists($name)
201 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
202 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
203 return (DB::getValue($query) > 0);
208 * Checks if a skin with a given ID exists
211 * @param string $id Skin ID
212 * @return integer number of skins with the given ID
214 static public function existsID($id)
216 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
217 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
218 return (DB::getValue($query) > 0);
222 * Skin::createFromName()
223 * Returns a skin given its shortname
226 * @param string $name Skin shortname
227 * @return object instance of Skin class
229 static public function createFromName($name)
231 return new SKIN(SKIN::getIdFromName($name));
235 * Skin::getIdFromName()
236 * Returns a skin ID given its shortname
239 * @param string $name Skin shortname
240 * @return integer Skin ID
242 static public function getIdFromName($name)
244 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
245 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
246 return DB::getValue($query);
250 * Skin::getNameFromId()
251 * Returns a skin shortname given its ID
254 * @param string $name
255 * @return string Skin short name
257 static public function getNameFromId($id)
259 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
260 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
261 return DB::getValue($query);
266 * Creates a new skin, with the given characteristics.
269 * @param String $name value for nucleus_skin.sdname
270 * @param String $desc value for nucleus_skin.sddesc
271 * @param String $type value for nucleus_skin.sdtype
272 * @param String $includeMode value for nucleus_skin.sdinclude
273 * @param String $includePrefix value for nucleus_skin.sdincpref
274 * @return Integer ID for just inserted record
276 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
282 'description' => &$desc,
284 'includeMode' => &$includeMode,
285 'includePrefix' => &$includePrefix
287 $manager->notify('PreAddSkin', $data);
289 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
290 $sdname = DB::quoteValue($name);
291 $sddesc = DB::quoteValue($desc);
292 $sdtype = DB::quoteValue($type);
293 $sdincmode = DB::quoteValue($includeMode);
294 $sdincpref = DB::quoteValue($includePrefix);
295 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
297 $newid = DB::getInsertId();
302 'description' => $desc,
304 'includeMode' => $includeMode,
305 'includePrefix' => $includePrefix
307 $manager->notify('PostAddSkin', $data);
316 * @param string $type
317 * @param string $path path to file if using fileparser
320 public function parse($type, $path='')
322 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
328 $manager->notify("Init{$this->event_identifier}Parse", $data);
330 // include skin locale file for <%text%> tag if useable
331 $this->includeTranslation();
334 sendContentType($this->getContentType(), 'skin');
336 /* FIX: should be obsoleted */
337 $currentSkinName = $this->getName();
341 if ( $type != 'fileparse' )
343 $contents = $this->getContentFromDB($type);
345 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
347 $contents = $this->getContentFromFile($path);
349 // use base skin if this skin does not have contents
350 if ( $contents === FALSE )
352 $defskin = new SKIN($CONF['BaseSkin']);
353 $contents = $defskin->getContentFromDB($type);
364 'contents' => &$contents
366 $manager->notify("Pre{$this->event_identifier}Parse", $data);
368 // set IncludeMode properties of parser
369 Parser::setProperty('IncludeMode', $this->getIncludeMode());
370 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
372 // call action handler
373 $action_class = $this->action_class;
374 $handler = new $action_class($type);
375 $handler->setSkin($this);
377 // register action handler to parser
378 $parser = new Parser($handler);
379 $parser->parse($contents);
385 $manager->notify("Post{$this->event_identifier}Parse", $data);
391 * Skin::getContentFromDB()
393 * @param string $skintype skin type
394 * @return string content for the skin type
396 public function getContentFromDB($skintype)
398 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
399 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
400 $res = DB::getValue($query);
402 return $res ? $res : '';
406 * Skin::getContentFromFile()
408 * @param string $fullpath fullpath to the file to parse
409 * @return mixed file contents or FALSE
411 public function getContentFromFile($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'), DB::quoteValue($type), (integer) $this->id);
450 $res = DB::getValue($query);
452 $skintypeexists = !empty($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'), DB::quoteValue($type), (integer) $this->id);
494 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
495 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($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 = DB::quoteValue($name);
561 $desc = DB::quoteValue($desc);
562 $type = DB::quoteValue($type);
563 $includeMode = DB::quoteValue($includeMode);
564 $includePrefix = DB::quoteValue($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::includeTranslation()
579 private function includeTranslation()
583 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
585 if( $this->includeMode == "normal" )
587 $filename = "./locales/{$locale}.php";
589 else if( $this->includeMode == "skindir" )
591 if ( $this->includePrefix == '' )
593 $filename = "{$DIR_SKINS}locales/{$locale}.php";
597 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
605 if ( !file_exists($filename) )
610 include_once($filename);
616 * Skin::getDefaultTypes()
619 * @return array default skin types
621 public function getDefaultTypes()
623 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));
627 * Skin::getAvailableTypes()
630 * @return array registered skin types
632 public function getAvailableTypes()
634 $default_skintypes = $this->getDefaultTypes();
635 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
636 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
638 /* NOTE: force to put default types in the beginning */
639 $in_default = array();
640 $no_default = array();
642 $res = DB::getResult($query);
643 foreach ( $res as $row )
645 if ( !array_key_exists($row['stype'], $default_skintypes) )
647 $no_default[$row['stype']] = FALSE;
651 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
655 return array_merge($in_default, $no_default);
659 * Skin::getAllowedActionsForType()
660 * Get the allowed actions for a skin type
661 * returns an array with the allowed actions
663 * @param string $skintype type of the skin
664 * @return array allowed action types
666 public function getAllowedActionsForType($skintype)
668 $handler = new $this->action_class($skintype);
669 return $handler->getAvailableActions();