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 1886 2012-06-17 08:27:27Z 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 /* NOTE: include needed action class */
58 if ( $action_class != 'Actions' )
60 if ( !class_exists($action_class, FALSE)
61 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
62 || !include("{$DIR_LIBS}{$action_class}.php")) )
69 if ( !class_exists('Actions', FALSE)
70 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
71 || !include("{$DIR_LIBS}ACTIONS.php")) )
77 $this->action_class = $action_class;
78 $this->event_identifier = $event_identifier;
80 // read skin name/description/content type
81 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
82 $query = sprintf($query, sql_table('skin_desc'), $this->id);
83 $res = DB::getRow($query);
85 $this->valid = !empty($res);
88 $this->name = $res['sdname'];
89 $this->description = $res['sddesc'];
90 $this->contentType = $res['sdtype'];
91 $this->includeMode = $res['sdincmode'];
92 $this->includePrefix = $res['sdincpref'];
103 * @return integer id for this skin instance
105 public function getID()
107 return (integer) $this->id;
116 public function isValid()
118 return (boolean) $this->valid;
126 * @return string name of this skin instance
128 public function getName()
130 return (string) $this->name;
134 * Skin::getDescription()
135 * Get SKIN description
138 * @return string description of this skin instance
140 public function getDescription()
142 return (string) $this->description;
146 * Skin::getContentType()
147 * Get SKIN content type
148 * e.g. text/xml, text/html, application/atom+xml
151 * @return string name of this skin instance
153 public function getContentType()
155 return (string) $this->contentType;
159 * Skin::getIncludeMode()
160 * Get include mode of the SKIN
162 * Returns either 'normal' or 'skindir':
163 * 'normal': if a all data of the skin can be found in the databse
164 * 'skindir': if the skin has data in the it's skin driectory
167 * @return string normal/skindir
169 public function getIncludeMode()
171 return (string) $this->includeMode;
175 * Skin::getIncludePrefix()
176 * Get include prefix of the SKIN
178 * Get name of the subdirectory (with trailing slash) where
179 * the files of the current skin can be found (e.g. 'default/')
182 * @return string include prefix of this skin instance
184 public function getIncludePrefix()
186 return (string) $this->includePrefix;
191 * Checks if a skin with a given shortname exists
194 * @param string $name Skin short name
195 * @return integer number of skins with the given ID
197 static public function exists($name)
199 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
200 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
201 return (DB::getValue($query) > 0);
206 * Checks if a skin with a given ID exists
209 * @param string $id Skin ID
210 * @return integer number of skins with the given ID
212 static public function existsID($id)
214 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
215 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
216 return (DB::getValue($query) > 0);
220 * Skin::createFromName()
221 * Returns a skin given its shortname
224 * @param string $name Skin shortname
225 * @return object instance of Skin class
227 static public function createFromName($name)
229 return new SKIN(SKIN::getIdFromName($name));
233 * Skin::getIdFromName()
234 * Returns a skin ID given its shortname
237 * @param string $name Skin shortname
238 * @return integer Skin ID
240 static public function getIdFromName($name)
242 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
243 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
244 return DB::getValue($query);
248 * Skin::getNameFromId()
249 * Returns a skin shortname given its ID
252 * @param string $name
253 * @return string Skin short name
255 static public function getNameFromId($id)
257 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
258 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
259 return DB::getValue($query);
264 * Creates a new skin, with the given characteristics.
267 * @param String $name value for nucleus_skin.sdname
268 * @param String $desc value for nucleus_skin.sddesc
269 * @param String $type value for nucleus_skin.sdtype
270 * @param String $includeMode value for nucleus_skin.sdinclude
271 * @param String $includePrefix value for nucleus_skin.sdincpref
272 * @return Integer ID for just inserted record
274 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
280 'description' => &$desc,
282 'includeMode' => &$includeMode,
283 'includePrefix' => &$includePrefix
285 $manager->notify('PreAddSkin', $data);
287 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
288 $sdname = DB::quoteValue($name);
289 $sddesc = DB::quoteValue($desc);
290 $sdtype = DB::quoteValue($type);
291 $sdincmode = DB::quoteValue($includeMode);
292 $sdincpref = DB::quoteValue($includePrefix);
293 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
295 $newid = DB::getInsertId();
300 'description' => $desc,
302 'includeMode' => $includeMode,
303 'includePrefix' => $includePrefix
305 $manager->notify('PostAddSkin', $data);
314 * @param string $type
315 * @param string $path path to file if using fileparser
318 public function parse($type, $path='')
320 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
326 $manager->notify("Init{$this->event_identifier}Parse", $data);
328 // include skin locale file for <%text%> tag if useable
329 $this->includeTranslation();
332 sendContentType($this->getContentType(), 'skin');
334 /* FIX: should be obsoleted */
335 $currentSkinName = $this->getName();
339 if ( $type == 'pluginadmin' )
345 if ( $type != 'fileparse' && $type != 'importAdmin')
347 $contents = $this->getContentFromDB($type);
349 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
351 $contents = $this->getContentFromFile($path);
354 // use base skin if this skin does not have contents
355 if ( $contents === FALSE )
357 $defskin = new SKIN($CONF['BaseSkin']);
358 $contents = $defskin->getContentFromDB($type);
369 'contents' => &$contents
371 $manager->notify("Pre{$this->event_identifier}Parse", $data);
373 // set IncludeMode properties of parser
374 Parser::setProperty('IncludeMode', $this->getIncludeMode());
375 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
377 // call action handler
378 $action_class = $this->action_class;
379 $handler = new $action_class($type);
381 // register action handler to parser
382 $parser = new Parser($handler);
383 $parser->setSkin($this);
384 $parser->parse($contents);
390 $manager->notify("Post{$this->event_identifier}Parse", $data);
396 * Skin::getContentFromDB()
398 * @param string $skintype skin type
399 * @return string content for the skin type
401 public function getContentFromDB($skintype)
403 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
404 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
405 $res = DB::getValue($query);
407 return $res ? $res : '';
411 * Skin::getContentFromFile()
413 * @param string $fullpath fullpath to the file to parse
414 * @return mixed file contents or FALSE
416 public function getContentFromFile($fullpath)
418 $fsize = filesize($fullpath);
424 $fd = fopen ($fullpath, 'r');
430 $contents = fread ($fd, $fsize);
431 if ( $contents === FALSE )
442 * Updates the contents for one part of the skin in the database
444 * @param string $type type of the skin part (e.g. index, item, search ...)
445 * @param string $content new content for this skin part
449 public function update($type, $content)
453 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
454 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
455 $res = DB::getValue($query);
457 $skintypeexists = !empty($res);
458 $skintypevalue = !empty($content);
460 if( $skintypevalue && $skintypeexists )
463 'skinid' => $this->id,
465 'content' => &$content
467 $manager->notify("PreUpdate{$this->event_identifier}Part", $data);
469 else if( $skintypevalue )
472 'skinid' => $this->id,
474 'content' => &$content
476 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
478 else if( $skintypeexists )
481 'skinid' => $this->id,
484 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
487 // delete old thingie
488 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d;";
489 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
495 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUES (%s, %s, %d);";
496 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
500 if( $skintypevalue && $skintypeexists )
503 'skinid' => $this->id,
505 'content' => &$content
507 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
509 else if( $skintypevalue )
512 'skinid' => $this->id,
514 'content' => &$content
516 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
518 else if( $skintypeexists )
521 '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 = DB::quoteValue($name);
557 $desc = DB::quoteValue($desc);
558 $type = DB::quoteValue($type);
559 $includeMode = DB::quoteValue($includeMode);
560 $includePrefix = DB::quoteValue($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::includeTranslation()
575 private function includeTranslation()
579 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
581 if( $this->includeMode == "normal" )
583 $filename = "./locales/{$locale}.php";
585 else if( $this->includeMode == "skindir" )
587 if ( $this->includePrefix == '' )
589 $filename = "{$DIR_SKINS}locales/{$locale}.php";
593 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
601 if ( !file_exists($filename) )
606 include_once($filename);
612 * Skin::getNormalTypes()
615 * @return array default skin types
617 public function getNormalTypes()
619 return call_user_func(array($this->action_class, 'getNormalSkinTypes'));
623 * Skin::getAvailableTypes()
626 * @return array registered skin types
628 public function getAvailableTypes()
630 $default_skintypes = $this->getNormalTypes();
631 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
632 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
634 /* NOTE: force to put default types in the beginning */
635 $in_default = array();
636 $no_default = array();
638 $res = DB::getResult($query);
639 foreach ( $res as $row )
641 if ( !array_key_exists($row['stype'], $default_skintypes) )
643 $no_default[$row['stype']] = FALSE;
647 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
651 return array_merge($in_default, $no_default);
655 * Skin::getAllowedActionsForType()
656 * Get the allowed actions for a skin type
657 * returns an array with the allowed actions
658 * @return array allowed action types
659 * @param string $skintype type of the skin
660 * @return array allowed action types
662 public function getAllowedActionsForType($skintype)
664 $handler = new $this->action_class($skintype);
665 return $handler->getAvailableActions();