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
57 /* NOTE: include needed action class */
\r
58 if ( $action_class != 'Actions' )
\r
60 if ( !class_exists($action_class, FALSE)
\r
61 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
\r
62 || !include("{$DIR_LIBS}{$action_class}.php")) )
\r
69 if ( !class_exists('Actions', FALSE)
\r
70 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
\r
71 || !include("{$DIR_LIBS}ACTIONS.php")) )
\r
77 $this->action_class = $action_class;
\r
78 $this->event_identifier = $event_identifier;
\r
80 // read skin name/description/content type
\r
81 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
\r
82 $query = sprintf($query, sql_table('skin_desc'), $this->id);
\r
83 $res = DB::getRow($query);
\r
85 $this->valid = !empty($res);
\r
87 { $this->name = $res['sdname'];
\r
88 $this->description = $res['sddesc'];
\r
89 $this->contentType = $res['sdtype'];
\r
90 $this->includeMode = $res['sdincmode'];
\r
91 $this->includePrefix = $res['sdincpref'];
\r
103 * @return integer id for this skin instance
\r
105 public function getID()
\r
107 return (integer) $this->id;
\r
116 public function isValid()
\r
118 return (boolean) $this->valid;
\r
126 * @return string name of this skin instance
\r
128 public function getName()
\r
130 return (string) $this->name;
\r
134 * Skin::getDescription()
\r
135 * Get SKIN description
\r
138 * @return string description of this skin instance
\r
140 public function getDescription()
\r
142 return (string) $this->description;
\r
146 * Skin::getContentType()
\r
147 * Get SKIN content type
\r
148 * e.g. text/xml, text/html, application/atom+xml
\r
151 * @return string name of this skin instance
\r
153 public function getContentType()
\r
155 return (string) $this->contentType;
\r
159 * Skin::getIncludeMode()
\r
160 * Get include mode of the SKIN
\r
162 * Returns either 'normal' or 'skindir':
\r
163 * 'normal': if a all data of the skin can be found in the databse
\r
164 * 'skindir': if the skin has data in the it's skin driectory
\r
167 * @return string normal/skindir
\r
169 public function getIncludeMode()
\r
171 return (string) $this->includeMode;
\r
175 * Skin::getIncludePrefix()
\r
176 * Get include prefix of the SKIN
\r
178 * Get name of the subdirectory (with trailing slash) where
\r
179 * the files of the current skin can be found (e.g. 'default/')
\r
182 * @return string include prefix of this skin instance
\r
184 public function getIncludePrefix()
\r
186 return (string) $this->includePrefix;
\r
191 * Checks if a skin with a given shortname exists
\r
194 * @param string $name Skin short name
\r
195 * @return integer number of skins with the given ID
\r
197 static public function exists($name)
\r
199 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
\r
200 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
\r
201 return (DB::getValue($query) > 0);
\r
206 * Checks if a skin with a given ID exists
\r
209 * @param string $id Skin ID
\r
210 * @return integer number of skins with the given ID
\r
212 static public function existsID($id)
\r
214 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
\r
215 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
216 return (DB::getValue($query) > 0);
\r
220 * Skin::createFromName()
\r
221 * Returns a skin given its shortname
\r
224 * @param string $name Skin shortname
\r
225 * @return object instance of Skin class
\r
227 static public function createFromName($name)
\r
229 return new SKIN(SKIN::getIdFromName($name));
\r
233 * Skin::getIdFromName()
\r
234 * Returns a skin ID given its shortname
\r
237 * @param string $name Skin shortname
\r
238 * @return integer Skin ID
\r
240 static public function getIdFromName($name)
\r
242 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
\r
243 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
\r
244 return DB::getValue($query);
\r
248 * Skin::getNameFromId()
\r
249 * Returns a skin shortname given its ID
\r
252 * @param string $name
\r
253 * @return string Skin short name
\r
255 static public function getNameFromId($id)
\r
257 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
\r
258 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
259 return DB::getValue($query);
\r
263 * SKIN::createNew()
\r
264 * Creates a new skin, with the given characteristics.
\r
267 * @param String $name value for nucleus_skin.sdname
\r
268 * @param String $desc value for nucleus_skin.sddesc
\r
269 * @param String $type value for nucleus_skin.sdtype
\r
270 * @param String $includeMode value for nucleus_skin.sdinclude
\r
271 * @param String $includePrefix value for nucleus_skin.sdincpref
\r
272 * @return Integer ID for just inserted record
\r
274 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
280 'description' => &$desc,
\r
282 'includeMode' => &$includeMode,
\r
283 'includePrefix' => &$includePrefix
\r
285 $manager->notify('PreAddSkin', $data);
287 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
\r
288 $sdname = DB::quoteValue($name);
\r
289 $sddesc = DB::quoteValue($desc);
\r
290 $sdtype = DB::quoteValue($type);
\r
291 $sdincmode = DB::quoteValue($includeMode);
\r
292 $sdincpref = DB::quoteValue($includePrefix);
\r
293 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
\r
294 DB::execute($query);
\r
295 $newid = DB::getInsertId();
\r
298 'skinid' => $newid,
\r
300 'description' => $desc,
\r
302 'includeMode' => $includeMode,
\r
303 'includePrefix' => $includePrefix
\r
305 $manager->notify('PostAddSkin', $data);
314 * @param string $type
\r
315 * @param string $path path to file if using fileparser
\r
318 public function parse($type, $path='')
\r
320 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
\r
326 $manager->notify("Init{$this->event_identifier}Parse", $data);
328 // include skin locale file for <%text%> tag if useable
\r
329 $this->includeTranslation();
\r
332 sendContentType($this->getContentType(), 'skin');
\r
334 /* FIX: should be obsoleted */
\r
335 $currentSkinName = $this->getName();
\r
337 // retrieve contents
\r
339 if ( $type == 'pluginadmin' )
\r
345 if ( $type != 'fileparse' && $type != 'importAdmin')
\r
347 $contents = $this->getContentFromDB($type);
\r
349 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
\r
351 $contents = $this->getContentFromFile($path);
\r
354 // use base skin if this skin does not have contents
\r
355 if ( $contents === FALSE )
\r
357 $defskin = new SKIN($CONF['BaseSkin']);
\r
358 $contents = $defskin->getContentFromDB($type);
\r
369 'contents' => &$contents
371 $manager->notify("Pre{$this->event_identifier}Parse", $data);
373 // set IncludeMode properties of parser
\r
374 Parser::setProperty('IncludeMode', $this->getIncludeMode());
\r
375 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
\r
377 // call action handler
\r
378 $action_class = $this->action_class;
\r
379 $handler = new $action_class($type);
\r
381 // register action handler to parser
\r
382 $parser = new Parser($handler);
\r
383 $parser->setSkin($this);
\r
384 $parser->parse($contents);
\r
390 $manager->notify("Post{$this->event_identifier}Parse", $data);
396 * Skin::getContentFromDB()
\r
398 * @param string $skintype skin type
\r
399 * @return string content for the skin type
\r
401 public function getContentFromDB($skintype)
\r
403 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
\r
404 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
\r
405 $res = DB::getValue($query);
\r
407 return $res ? $res : '';
\r
411 * Skin::getContentFromFile()
\r
413 * @param string $fullpath fullpath to the file to parse
\r
414 * @return mixed file contents or FALSE
\r
416 public function getContentFromFile($fullpath)
\r
418 $fsize = filesize($fullpath);
\r
424 $fd = fopen ($fullpath, 'r');
\r
425 if ( $fd === FALSE )
\r
430 $contents = fread ($fd, $fsize);
\r
431 if ( $contents === FALSE )
\r
442 * Updates the contents for one part of the skin in the database
\r
444 * @param string $type type of the skin part (e.g. index, item, search ...)
\r
445 * @param string $content new content for this skin part
\r
449 public function update($type, $content)
\r
453 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
\r
454 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
\r
455 $res = DB::getValue($query);
\r
457 $skintypeexists = !empty($res);
\r
458 $skintypevalue = ($content == true);
\r
460 if( $skintypevalue && $skintypeexists )
\r
463 'skinid' => $this->id,
\r
465 'content' => &$content
\r
468 // PreUpdateSkinPart event
\r
469 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
\r
471 else if( $skintypevalue && !$skintypeexists )
\r
474 'skinid' => $this->id,
\r
476 'content' => &$content
\r
479 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
\r
481 else if( !$skintypevalue && $skintypeexists )
\r
484 'skinid' => $this->id,
\r
488 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
\r
491 // delete old thingie
\r
492 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d";
\r
493 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
\r
494 DB::execute($query);
\r
496 // write new thingie
\r
499 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
\r
500 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
\r
501 DB::execute($query);
\r
504 if( $skintypevalue && $skintypeexists )
\r
507 'skinid' => $this->id,
\r
509 'content' => &$content
\r
512 // PostUpdateSkinPart event
\r
513 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
\r
515 else if( $skintypevalue && (!$skintypeexists) )
\r
518 'skinid' => $this->id,
\r
520 'content' => &$content
\r
523 // PostAddSkinPart event
\r
524 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
\r
526 else if( (!$skintypevalue) && $skintypeexists )
\r
529 'skinid' => $this->id,
\r
533 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
\r
539 * Skin::deleteAllParts()
\r
540 * Deletes all skin parts from the database
\r
545 public function deleteAllParts()
\r
547 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
548 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
549 DB::execute($query);
\r
553 * Skin::updateGeneralInfo()
\r
554 * Updates the general information about the skin
\r
556 * @param string $name name of the skin
\r
557 * @param string $desc description of the skin
\r
558 * @param string $type type of the skin
\r
559 * @param string $includeMode include mode of the skin
\r
560 * @param string $includePrefix include prefix of the skin
\r
563 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
565 $name = DB::quoteValue($name);
\r
566 $desc = DB::quoteValue($desc);
\r
567 $type = DB::quoteValue($type);
\r
568 $includeMode = DB::quoteValue($includeMode);
\r
569 $includePrefix = DB::quoteValue($includePrefix);
\r
571 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
\r
572 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
574 DB::execute($query);
\r
579 * Skin::includeTranslation()
\r
584 private function includeTranslation()
\r
588 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
\r
590 if( $this->includeMode == "normal" )
\r
592 $filename = "./locales/{$locale}.php";
\r
594 else if( $this->includeMode == "skindir" )
\r
596 if ( $this->includePrefix == '' )
\r
598 $filename = "{$DIR_SKINS}locales/{$locale}.php";
\r
602 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
\r
610 if ( !file_exists($filename) )
\r
615 include_once($filename);
\r
621 * Skin::getDefaultTypes()
\r
623 * @param string void
\r
624 * @return array default skin types
\r
626 public function getDefaultTypes()
\r
628 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));
\r
632 * Skin::getAvailableTypes()
\r
634 * @param string void
\r
635 * @return array registered skin types
\r
637 public function getAvailableTypes()
\r
639 $default_skintypes = $this->getDefaultTypes();
\r
640 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
\r
641 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
643 /* NOTE: force to put default types in the beginning */
\r
644 $in_default = array();
\r
645 $no_default = array();
\r
647 $res = DB::getResult($query);
\r
648 foreach ( $res as $row )
\r
650 if ( !array_key_exists($row['stype'], $default_skintypes) )
\r
652 $no_default[$row['stype']] = FALSE;
\r
656 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
\r
660 return array_merge($in_default, $no_default);
\r
664 * Skin::getAllowedActionsForType()
\r
665 * Get the allowed actions for a skin type
\r
666 * returns an array with the allowed actions
\r
667 * @return array allowed action types
\r
668 * @param string $skintype type of the skin
\r
669 * @return array allowed action types
\r
671 public function getAllowedActionsForType($skintype)
\r
673 $handler = new $this->action_class($skintype);
\r
674 return $handler->getAvailableActions();
\r