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 = !empty($content);
\r
460 if( $skintypevalue && $skintypeexists )
\r
463 'skinid' => $this->id,
\r
465 'content' => &$content
\r
467 $manager->notify("PreUpdate{$this->event_identifier}Part", $data);
\r
469 else if( $skintypevalue )
\r
472 'skinid' => $this->id,
\r
474 'content' => &$content
\r
476 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
\r
478 else if( $skintypeexists )
\r
481 'skinid' => $this->id,
\r
484 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
\r
487 // delete old thingie
\r
488 $query = "DELETE FROM %s WHERE stype=%s and sdesc=%d;";
\r
489 $query = sprintf($query, sql_table('skin'), DB::quoteValue($type), (integer) $this->id);
\r
490 DB::execute($query);
\r
492 // write new thingie
\r
495 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUES (%s, %s, %d);";
\r
496 $query = sprintf($query, sql_table('skin'), DB::quoteValue($content), DB::quoteValue($type), (integer) $this->id);
\r
497 DB::execute($query);
\r
500 if( $skintypevalue && $skintypeexists )
\r
503 'skinid' => $this->id,
\r
505 'content' => &$content
\r
507 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
\r
509 else if( $skintypevalue )
\r
512 'skinid' => $this->id,
\r
514 'content' => &$content
\r
516 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
\r
518 else if( $skintypeexists )
\r
521 'skinid' => $this->id,
\r
524 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
\r
530 * Skin::deleteAllParts()
\r
531 * Deletes all skin parts from the database
\r
536 public function deleteAllParts()
\r
538 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
539 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
540 DB::execute($query);
\r
544 * Skin::updateGeneralInfo()
\r
545 * Updates the general information about the skin
\r
547 * @param string $name name of the skin
\r
548 * @param string $desc description of the skin
\r
549 * @param string $type type of the skin
\r
550 * @param string $includeMode include mode of the skin
\r
551 * @param string $includePrefix include prefix of the skin
\r
554 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
556 $name = DB::quoteValue($name);
\r
557 $desc = DB::quoteValue($desc);
\r
558 $type = DB::quoteValue($type);
\r
559 $includeMode = DB::quoteValue($includeMode);
\r
560 $includePrefix = DB::quoteValue($includePrefix);
\r
562 $query ="UPDATE %s SET sdname=%s, sddesc=%s, sdtype=%s, sdincmode=%s, sdincpref=%s WHERE sdnumber=%d";
\r
563 $query = sprintf($query, sql_table('skin_desc'), $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
565 DB::execute($query);
\r
570 * Skin::includeTranslation()
\r
575 private function includeTranslation()
\r
579 $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset();
\r
581 if( $this->includeMode == "normal" )
\r
583 $filename = "./locales/{$locale}.php";
\r
585 else if( $this->includeMode == "skindir" )
\r
587 if ( $this->includePrefix == '' )
\r
589 $filename = "{$DIR_SKINS}locales/{$locale}.php";
\r
593 $filename = "{$DIR_SKINS}{$this->includePrefix}locales/{$locale}.php";
\r
601 if ( !file_exists($filename) )
\r
606 include_once($filename);
\r
612 * Skin::getDefaultTypes()
\r
614 * @param string void
\r
615 * @return array default skin types
\r
617 public function getDefaultTypes()
\r
619 return call_user_func(array($this->action_class, 'getAvailableSkinTypes'));
\r
623 * Skin::getAvailableTypes()
\r
625 * @param string void
\r
626 * @return array registered skin types
\r
628 public function getAvailableTypes()
\r
630 $default_skintypes = $this->getDefaultTypes();
\r
631 $query = "SELECT stype FROM %s WHERE sdesc=%d;";
\r
632 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
634 /* NOTE: force to put default types in the beginning */
\r
635 $in_default = array();
\r
636 $no_default = array();
\r
638 $res = DB::getResult($query);
\r
639 foreach ( $res as $row )
\r
641 if ( !array_key_exists($row['stype'], $default_skintypes) )
\r
643 $no_default[$row['stype']] = FALSE;
\r
647 $in_default[$row['stype']] = $default_skintypes[$row['stype']];
\r
651 return array_merge($in_default, $no_default);
\r
655 * Skin::getAllowedActionsForType()
\r
656 * Get the allowed actions for a skin type
\r
657 * returns an array with the allowed actions
\r
658 * @return array allowed action types
\r
659 * @param string $skintype type of the skin
\r
660 * @return array allowed action types
\r
662 public function getAllowedActionsForType($skintype)
\r
664 $handler = new $this->action_class($skintype);
\r
665 return $handler->getAvailableActions();
\r