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
18 * @version $Id: SKIN.php 1816 2012-05-03 01:40:10Z sakamocchi $
20 * @version $Id: SKIN.php 1886 2012-06-17 08:27:27Z sakamocchi $
21 >>>>>>> skinnable-master
24 if ( !function_exists('requestVar') )
31 // after creating a SKIN object, evaluates to true when the skin exists
34 // skin characteristics. Use the getXXX methods rather than accessing directly
38 private $includeMode; // either 'normal' or 'skindir'
39 private $includePrefix;
43 private $action_class;
44 private $event_identifier;
48 * Constructor for a new SKIN object
50 * @param integer $id id of the skin
51 * @param string $action_class name of class extended from BaseActions
52 * @param string $event_identifier event identifier. for example, InitAdminSkinParse if AdminSkin is used
55 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
59 $this->id = (integer) $id;
63 * NOTE: include needed action class
66 /* NOTE: include needed action class */
67 >>>>>>> skinnable-master
68 if ( $action_class != 'Actions' )
70 if ( !class_exists($action_class, FALSE)
71 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
72 || !include("{$DIR_LIBS}{$action_class}.php")) )
79 if ( !class_exists('Actions', FALSE)
80 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
81 || !include("{$DIR_LIBS}ACTIONS.php")) )
87 $this->action_class = $action_class;
88 $this->event_identifier = $event_identifier;
90 // read skin name/description/content type
91 $query = "SELECT * FROM %s WHERE sdnumber=%d;";
92 $query = sprintf($query, sql_table('skin_desc'), $this->id);
93 $res = DB::getRow($query);
95 $this->valid = !empty($res);
98 $this->name = $res['sdname'];
99 $this->description = $res['sddesc'];
100 $this->contentType = $res['sdtype'];
101 $this->includeMode = $res['sdincmode'];
102 $this->includePrefix = $res['sdincpref'];
113 * @return integer id for this skin instance
115 public function getID()
117 return (integer) $this->id;
126 public function isValid()
128 return (boolean) $this->valid;
136 * @return string name of this skin instance
138 public function getName()
140 return (string) $this->name;
144 * Skin::getDescription()
145 * Get SKIN description
148 * @return string description of this skin instance
150 public function getDescription()
152 return (string) $this->description;
156 * Skin::getContentType()
157 * Get SKIN content type
158 * e.g. text/xml, text/html, application/atom+xml
161 * @return string name of this skin instance
163 public function getContentType()
165 return (string) $this->contentType;
169 * Skin::getIncludeMode()
170 * Get include mode of the SKIN
172 * Returns either 'normal' or 'skindir':
173 * 'normal': if a all data of the skin can be found in the databse
174 * 'skindir': if the skin has data in the it's skin driectory
177 * @return string normal/skindir
179 public function getIncludeMode()
181 return (string) $this->includeMode;
185 * Skin::getIncludePrefix()
186 * Get include prefix of the SKIN
188 * Get name of the subdirectory (with trailing slash) where
189 * the files of the current skin can be found (e.g. 'default/')
192 * @return string include prefix of this skin instance
194 public function getIncludePrefix()
196 return (string) $this->includePrefix;
201 * Checks if a skin with a given shortname exists
204 * @param string $name Skin short name
205 * @return integer number of skins with the given ID
207 static public function exists($name)
209 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname=%s;";
210 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
211 return (DB::getValue($query) > 0);
216 * Checks if a skin with a given ID exists
219 * @param string $id Skin ID
220 * @return integer number of skins with the given ID
222 static public function existsID($id)
224 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
225 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
226 return (DB::getValue($query) > 0);
230 * Skin::createFromName()
231 * Returns a skin given its shortname
234 * @param string $name Skin shortname
235 * @return object instance of Skin class
237 static public function createFromName($name)
239 return new SKIN(SKIN::getIdFromName($name));
243 * Skin::getIdFromName()
244 * Returns a skin ID given its shortname
247 * @param string $name Skin shortname
248 * @return integer Skin ID
250 static public function getIdFromName($name)
252 $query = "SELECT sdnumber FROM %s WHERE sdname=%s;";
253 $query = sprintf($query, sql_table('skin_desc'), DB::quoteValue($name));
254 return DB::getValue($query);
258 * Skin::getNameFromId()
259 * Returns a skin shortname given its ID
262 * @param string $name
263 * @return string Skin short name
265 static public function getNameFromId($id)
267 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
268 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
269 return DB::getValue($query);
274 * Creates a new skin, with the given characteristics.
277 * @param String $name value for nucleus_skin.sdname
278 * @param String $desc value for nucleus_skin.sddesc
279 * @param String $type value for nucleus_skin.sdtype
280 * @param String $includeMode value for nucleus_skin.sdinclude
281 * @param String $includePrefix value for nucleus_skin.sdincpref
282 * @return Integer ID for just inserted record
284 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
290 'description' => &$desc,
292 'includeMode' => &$includeMode,
293 'includePrefix' => &$includePrefix
295 $manager->notify('PreAddSkin', $data);
297 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES (%s, %s, %s, %s, %s);";
298 $sdname = DB::quoteValue($name);
299 $sddesc = DB::quoteValue($desc);
300 $sdtype = DB::quoteValue($type);
301 $sdincmode = DB::quoteValue($includeMode);
302 $sdincpref = DB::quoteValue($includePrefix);
303 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
305 $newid = DB::getInsertId();
310 'description' => $desc,
312 'includeMode' => $includeMode,
313 'includePrefix' => $includePrefix
315 $manager->notify('PostAddSkin', $data);
324 * @param string $type
325 * @param string $path path to file if using fileparser
328 public function parse($type, $path='')
330 global $currentSkinName, $manager, $CONF, $DIR_NUCLEUS;
336 $manager->notify("Init{$this->event_identifier}Parse", $data);
338 // include skin locale file for <%text%> tag if useable
339 $this->includeTranslation();
342 sendContentType($this->getContentType(), 'skin');
344 /* FIX: should be obsoleted */
345 $currentSkinName = $this->getName();
350 if ( $type != 'fileparse' )
352 $contents = $this->getContentFromDB($type);
354 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
356 $contents = $this->getContentFromFile($path);
358 if ( $type == 'pluginadmin' )
364 if ( $type != 'fileparse' && $type != 'importAdmin')
366 $contents = $this->getContentFromDB($type);
368 else if ( $path !== '' && i18n::strpos(realpath($path), realpath("$DIR_NUCLEUS/../")) == 0 )
370 $contents = $this->getContentFromFile($path);
372 >>>>>>> skinnable-master
374 // use base skin if this skin does not have contents
375 if ( $contents === FALSE )
377 $defskin = new SKIN($CONF['BaseSkin']);
378 $contents = $defskin->getContentFromDB($type);
389 'contents' => &$contents
391 $manager->notify("Pre{$this->event_identifier}Parse", $data);
393 // set IncludeMode properties of parser
394 Parser::setProperty('IncludeMode', $this->getIncludeMode());
395 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
397 // call action handler
398 $action_class = $this->action_class;
399 $handler = new $action_class($type);
401 // register action handler to parser
402 $parser = new Parser($handler);
403 $parser->setSkin($this);
404 $parser->parse($contents);
410 $manager->notify("Post{$this->event_identifier}Parse", $data);
416 * Skin::getContentFromDB()
418 * @param string $skintype skin type
419 * @return string content for the skin type
421 public function getContentFromDB($skintype)
423 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype=%s;";
424 $query = sprintf($query, sql_table('skin'), (integer) $this->id, DB::quoteValue($skintype));
425 $res = DB::getValue($query);
427 return $res ? $res : '';
431 * Skin::getContentFromFile()
433 * @param string $fullpath fullpath to the file to parse
434 * @return mixed file contents or FALSE
436 public function getContentFromFile($fullpath)
438 $fsize = filesize($fullpath);
444 $fd = fopen ($fullpath, 'r');
450 $contents = fread ($fd, $fsize);
451 if ( $contents === FALSE )
462 * Updates the contents for one part of the skin in the database
464 * @param string $type type of the skin part (e.g. index, item, search ...)
465 * @param string $content new content for this skin part
469 public function update($type, $content)
473 $query = "SELECT sdesc FROM %s WHERE stype=%s and sdesc=%d;";
474 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
475 $res = DB::getValue($query);
477 $skintypeexists = !empty($res);
479 $skintypevalue = ($content == true);
481 $skintypevalue = !empty($content);
482 >>>>>>> skinnable-master
484 if( $skintypevalue && $skintypeexists )
487 'skinid' => $this->id,
489 'content' => &$content
493 // PreUpdateSkinPart event
494 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
496 else if( $skintypevalue && !$skintypeexists )
499 'skinid' => $this->id,
501 'content' => &$content
504 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
506 else if( !$skintypevalue && $skintypeexists )
508 $manager->notify("PreUpdate{$this->event_identifier}Part", $data);
510 else if( $skintypevalue )
513 'skinid' => $this->id,
515 'content' => &$content
517 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
519 else if( $skintypeexists )
520 >>>>>>> skinnable-master
523 'skinid' => $this->id,
529 >>>>>>> skinnable-master
530 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
533 // delete old thingie
535 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d";
537 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d;";
538 >>>>>>> skinnable-master
539 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
546 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE (%s, %s, %d)";
548 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUES (%s, %s, %d);";
549 >>>>>>> skinnable-master
550 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
554 if( $skintypevalue && $skintypeexists )
558 'skinid' => $this->id,
560 'content' => &$content
563 // PostUpdateSkinPart event
564 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
566 else if( $skintypevalue && (!$skintypeexists) )
569 'skinid' => $this->id,
571 'content' => &$content
574 // PostAddSkinPart event
575 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
577 else if( (!$skintypevalue) && $skintypeexists )
579 'skinid' => $this->id,
581 'content' => &$content
583 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
585 else if( $skintypevalue )
588 'skinid' => $this->id,
590 'content' => &$content
592 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
594 else if( $skintypeexists )
595 >>>>>>> skinnable-master
598 'skinid' => $this->id,
604 >>>>>>> skinnable-master
605 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
611 * Skin::deleteAllParts()
612 * Deletes all skin parts from the database
617 public function deleteAllParts()
619 $query = "DELETE FROM %s WHERE sdesc=%d;";
620 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
625 * Skin::updateGeneralInfo()
626 * Updates the general information about the skin
628 * @param string $name name of the skin
629 * @param string $desc description of the skin
630 * @param string $type type of the skin
631 * @param string $includeMode include mode of the skin
632 * @param string $includePrefix include prefix of the skin
635 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
637 $name = DB::quoteValue($name);
638 $desc = DB::quoteValue($desc);
639 $type = DB::quoteValue($type);
640 $includeMode = DB::quoteValue($includeMode);
641 $includePrefix = DB::quoteValue($includePrefix);
643 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
644 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
651 * Skin::includeTranslation()
656 private function includeTranslation()
660 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
662 if( $this->includeMode == "normal" )
664 $filename = "./locales/{$locale}.php";
666 else if( $this->includeMode == "skindir" )
668 if ( $this->includePrefix == '' )
670 $filename = "{$DIR_SKINS}locales/{$locale}.php";
674 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
682 if ( !file_exists($filename) )
687 include_once($filename);
694 * Skin::getDefaultTypes()
696 * Skin::getNormalTypes()
697 >>>>>>> skinnable-master
700 * @return array default skin types
703 public function getDefaultTypes()
705 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));
707 public function getNormalTypes()
709 return call_user_func(array($this->action_class, 'getNormalSkinTypes'));
710 >>>>>>> skinnable-master
714 * Skin::getAvailableTypes()
717 * @return array registered skin types
719 public function getAvailableTypes()
722 $default_skintypes = $this->getDefaultTypes();
724 $default_skintypes = $this->getNormalTypes();
725 >>>>>>> skinnable-master
726 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
727 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
729 /* NOTE: force to put default types in the beginning */
730 $in_default = array();
731 $no_default = array();
733 $res = DB::getResult($query);
734 foreach ( $res as $row )
736 if ( !array_key_exists($row['stype'], $default_skintypes) )
738 $no_default[$row['stype']] = FALSE;
742 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
746 return array_merge($in_default, $no_default);
750 * Skin::getAllowedActionsForType()
751 * Get the allowed actions for a skin type
752 * returns an array with the allowed actions
756 * @return array allowed action types
757 >>>>>>> skinnable-master
758 * @param string $skintype type of the skin
759 * @return array allowed action types
761 public function getAllowedActionsForType($skintype)
763 $handler = new $this->action_class($skintype);
764 return $handler->getAvailableActions();