3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
13 * Class representing a skin
\r
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
17 * @version $Id: SKIN.php 1816 2012-05-03 01:40:10Z sakamocchi $
\r
20 if ( !function_exists('requestVar') )
\r
27 // after creating a SKIN object, evaluates to true when the skin exists
\r
30 // skin characteristics. Use the getXXX methods rather than accessing directly
\r
32 private $description;
\r
33 private $contentType;
\r
34 private $includeMode; // either 'normal' or 'skindir'
\r
35 private $includePrefix;
\r
39 private $action_class;
\r
40 private $event_identifier;
\r
43 * Skin::__construct()
\r
44 * Constructor for a new SKIN object
\r
46 * @param integer $id id of the skin
\r
47 * @param string $action_class name of class extended from BaseActions
\r
48 * @param string $event_identifier event identifier. for example, InitAdminSkinParse if AdminSkin is used
\r
51 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
\r
55 $this->id = (integer) $id;
\r
58 * NOTE: include needed action class
\r
60 if ( $action_class != 'Actions' )
\r
62 if ( !class_exists($action_class, FALSE)
\r
63 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
\r
64 || !include("{$DIR_LIBS}{$action_class}.php")) )
\r
71 if ( !class_exists('Actions', FALSE)
\r
72 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
\r
73 || !include("{$DIR_LIBS}ACTIONS.php")) )
\r
79 $this->action_class = $action_class;
\r
80 $this->event_identifier = $event_identifier;
\r
82 // read skin name/description/content type
\r
83 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
\r
84 $query = sprintf($query, sql_table('skin_desc'), $this->id);
\r
85 $res = DB::getRow($query);
\r
87 $this->valid = !empty($res);
\r
89 { $this->name = $res['sdname'];
\r
90 $this->description = $res['sddesc'];
\r
91 $this->contentType = $res['sdtype'];
\r
92 $this->includeMode = $res['sdincmode'];
\r
93 $this->includePrefix = $res['sdincpref'];
\r
105 * @return integer id for this skin instance
\r
107 public function getID()
\r
109 return (integer) $this->id;
\r
118 public function isValid()
\r
120 return (boolean) $this->valid;
\r
128 * @return string name of this skin instance
\r
130 public function getName()
\r
132 return (string) $this->name;
\r
136 * Skin::getDescription()
\r
137 * Get SKIN description
\r
140 * @return string description of this skin instance
\r
142 public function getDescription()
\r
144 return (string) $this->description;
\r
148 * Skin::getContentType()
\r
149 * Get SKIN content type
\r
150 * e.g. text/xml, text/html, application/atom+xml
\r
153 * @return string name of this skin instance
\r
155 public function getContentType()
\r
157 return (string) $this->contentType;
\r
161 * Skin::getIncludeMode()
\r
162 * Get include mode of the SKIN
\r
164 * Returns either 'normal' or 'skindir':
\r
165 * 'normal': if a all data of the skin can be found in the databse
\r
166 * 'skindir': if the skin has data in the it's skin driectory
\r
169 * @return string normal/skindir
\r
171 public function getIncludeMode()
\r
173 return (string) $this->includeMode;
\r
177 * Skin::getIncludePrefix()
\r
178 * Get include prefix of the SKIN
\r
180 * Get name of the subdirectory (with trailing slash) where
\r
181 * the files of the current skin can be found (e.g. 'default/')
\r
184 * @return string include prefix of this skin instance
\r
186 public function getIncludePrefix()
\r
188 return (string) $this->includePrefix;
\r
193 * Checks if a skin with a given shortname exists
\r
196 * @param string $name Skin short name
\r
197 * @return integer number of skins with the given ID
\r
199 static public function exists($name)
\r
201 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
\r
202 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
\r
203 return (DB::getValue($query) > 0);
\r
208 * Checks if a skin with a given ID exists
\r
211 * @param string $id Skin ID
\r
212 * @return integer number of skins with the given ID
\r
214 static public function existsID($id)
\r
216 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
\r
217 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
218 return (DB::getValue($query) > 0);
\r
222 * Skin::createFromName()
\r
223 * Returns a skin given its shortname
\r
226 * @param string $name Skin shortname
\r
227 * @return object instance of Skin class
\r
229 static public function createFromName($name)
\r
231 return new SKIN(SKIN::getIdFromName($name));
\r
235 * Skin::getIdFromName()
\r
236 * Returns a skin ID given its shortname
\r
239 * @param string $name Skin shortname
\r
240 * @return integer Skin ID
\r
242 static public function getIdFromName($name)
\r
244 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
\r
245 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
\r
246 return DB::getValue($query);
\r
250 * Skin::getNameFromId()
\r
251 * Returns a skin shortname given its ID
\r
254 * @param string $name
\r
255 * @return string Skin short name
\r
257 static public function getNameFromId($id)
\r
259 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
\r
260 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
261 return DB::getValue($query);
\r
265 * SKIN::createNew()
\r
266 * Creates a new skin, with the given characteristics.
\r
269 * @param String $name value for nucleus_skin.sdname
\r
270 * @param String $desc value for nucleus_skin.sddesc
\r
271 * @param String $type value for nucleus_skin.sdtype
\r
272 * @param String $includeMode value for nucleus_skin.sdinclude
\r
273 * @param String $includePrefix value for nucleus_skin.sdincpref
\r
274 * @return Integer ID for just inserted record
\r
276 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
282 'description' => &$desc,
\r
284 'includeMode' => &$includeMode,
\r
285 'includePrefix' => &$includePrefix
\r
287 $manager->notify('PreAddSkin', $data);
289 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
\r
290 $sdname = DB::quoteValue($name);
\r
291 $sddesc = DB::quoteValue($desc);
\r
292 $sdtype = DB::quoteValue($type);
\r
293 $sdincmode = DB::quoteValue($includeMode);
\r
294 $sdincpref = DB::quoteValue($includePrefix);
\r
295 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
\r
296 DB::execute($query);
\r
297 $newid = DB::getInsertId();
\r
300 'skinid' => $newid,
\r
302 'description' => $desc,
\r
304 'includeMode' => $includeMode,
\r
305 'includePrefix' => $includePrefix
\r
307 $manager->notify('PostAddSkin', $data);
316 * @param string $type
\r
317 * @param string $path path to file if using fileparser
\r
320 public function parse($type, $path='')
\r
322 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
\r
328 $manager->notify("Init{$this->event_identifier}Parse", $data);
330 // include skin locale file for <%text%> tag if useable
\r
331 $this->includeTranslation();
\r
334 sendContentType($this->getContentType(), 'skin');
\r
336 /* FIX: should be obsoleted */
\r
337 $currentSkinName = $this->getName();
\r
339 // retrieve contents
\r
341 if ( $type == 'pluginadmin' )
\r
347 if ( $type != 'fileparse' && $type != 'importAdmin')
\r
349 $contents = $this->getContentFromDB($type);
\r
351 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
\r
353 $contents = $this->getContentFromFile($path);
\r
356 // use base skin if this skin does not have contents
\r
357 if ( $contents === FALSE )
\r
359 $defskin = new SKIN($CONF['BaseSkin']);
\r
360 $contents = $defskin->getContentFromDB($type);
\r
371 'contents' => &$contents
373 $manager->notify("Pre{$this->event_identifier}Parse", $data);
375 // set IncludeMode properties of parser
\r
376 Parser::setProperty('IncludeMode', $this->getIncludeMode());
\r
377 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
\r
379 // call action handler
\r
380 $action_class = $this->action_class;
\r
381 $handler = new $action_class($type);
\r
383 // register action handler to parser
\r
384 $parser = new Parser($handler);
\r
385 $parser->setSkin($this);
\r
386 $parser->parse($contents);
\r
392 $manager->notify("Post{$this->event_identifier}Parse", $data);
398 * Skin::getContentFromDB()
\r
400 * @param string $skintype skin type
\r
401 * @return string content for the skin type
\r
403 public function getContentFromDB($skintype)
\r
405 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
\r
406 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
\r
407 $res = DB::getValue($query);
\r
409 return $res ? $res : '';
\r
413 * Skin::getContentFromFile()
\r
415 * @param string $fullpath fullpath to the file to parse
\r
416 * @return mixed file contents or FALSE
\r
418 public function getContentFromFile($fullpath)
\r
420 $fsize = filesize($fullpath);
\r
426 $fd = fopen ($fullpath, 'r');
\r
427 if ( $fd === FALSE )
\r
432 $contents = fread ($fd, $fsize);
\r
433 if ( $contents === FALSE )
\r
444 * Updates the contents for one part of the skin in the database
\r
446 * @param string $type type of the skin part (e.g. index, item, search ...)
\r
447 * @param string $content new content for this skin part
\r
451 public function update($type, $content)
\r
455 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
\r
456 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
\r
457 $res = DB::getValue($query);
\r
459 $skintypeexists = !empty($res);
\r
460 $skintypevalue = ($content == true);
\r
462 if( $skintypevalue && $skintypeexists )
\r
465 'skinid' => $this->id,
\r
467 'content' => &$content
\r
470 // PreUpdateSkinPart event
\r
471 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
\r
473 else if( $skintypevalue && !$skintypeexists )
\r
476 'skinid' => $this->id,
\r
478 'content' => &$content
\r
481 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
\r
483 else if( !$skintypevalue && $skintypeexists )
\r
486 'skinid' => $this->id,
\r
490 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
\r
493 // delete old thingie
\r
494 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d";
\r
495 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
\r
496 DB::execute($query);
\r
498 // write new thingie
\r
501 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
\r
502 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
\r
503 DB::execute($query);
\r
506 if( $skintypevalue && $skintypeexists )
\r
509 'skinid' => $this->id,
\r
511 'content' => &$content
\r
514 // PostUpdateSkinPart event
\r
515 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
\r
517 else if( $skintypevalue && (!$skintypeexists) )
\r
520 'skinid' => $this->id,
\r
522 'content' => &$content
\r
525 // PostAddSkinPart event
\r
526 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
\r
528 else if( (!$skintypevalue) && $skintypeexists )
\r
531 'skinid' => $this->id,
\r
535 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
\r
541 * Skin::deleteAllParts()
\r
542 * Deletes all skin parts from the database
\r
547 public function deleteAllParts()
\r
549 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
550 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
551 DB::execute($query);
\r
555 * Skin::updateGeneralInfo()
\r
556 * Updates the general information about the skin
\r
558 * @param string $name name of the skin
\r
559 * @param string $desc description of the skin
\r
560 * @param string $type type of the skin
\r
561 * @param string $includeMode include mode of the skin
\r
562 * @param string $includePrefix include prefix of the skin
\r
565 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
567 $name = DB::quoteValue($name);
\r
568 $desc = DB::quoteValue($desc);
\r
569 $type = DB::quoteValue($type);
\r
570 $includeMode = DB::quoteValue($includeMode);
\r
571 $includePrefix = DB::quoteValue($includePrefix);
\r
573 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
\r
574 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
576 DB::execute($query);
\r
581 * Skin::includeTranslation()
\r
586 private function includeTranslation()
\r
590 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
\r
592 if( $this->includeMode == "normal" )
\r
594 $filename = "./locales/{$locale}.php";
\r
596 else if( $this->includeMode == "skindir" )
\r
598 if ( $this->includePrefix == '' )
\r
600 $filename = "{$DIR_SKINS}locales/{$locale}.php";
\r
604 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
\r
612 if ( !file_exists($filename) )
\r
617 include_once($filename);
\r
623 * Skin::getDefaultTypes()
\r
625 * @param string void
\r
626 * @return array default skin types
\r
628 public function getDefaultTypes()
\r
630 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));
\r
634 * Skin::getAvailableTypes()
\r
636 * @param string void
\r
637 * @return array registered skin types
\r
639 public function getAvailableTypes()
\r
641 $default_skintypes = $this->getDefaultTypes();
\r
642 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
\r
643 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
645 /* NOTE: force to put default types in the beginning */
\r
646 $in_default = array();
\r
647 $no_default = array();
\r
649 $res = DB::getResult($query);
\r
650 foreach ( $res as $row )
\r
652 if ( !array_key_exists($row['stype'], $default_skintypes) )
\r
654 $no_default[$row['stype']] = FALSE;
\r
658 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
\r
662 return array_merge($in_default, $no_default);
\r
666 * Skin::getAllowedActionsForType()
\r
667 * Get the allowed actions for a skin type
\r
668 * returns an array with the allowed actions
\r
669 * @return array allowed action types
\r
670 * @param string $skintype type of the skin
\r
671 * @return array allowed action types
\r
673 public function getAllowedActionsForType($skintype)
\r
675 $handler = new $this->action_class($skintype);
\r
676 return $handler->getAvailableActions();
\r