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 1621 2012-01-09 02:49:37Z sakamocchi $
20 if ( !function_exists('requestVar') )
24 require_once dirname(__FILE__) . '/ACTIONS.php';
28 // after creating a SKIN object, evaluates to true when the skin exists
31 // skin characteristics. Use the getXXX methods rather than accessing directly
35 var $includeMode; // either 'normal' or 'skindir'
40 * Constructor for a new SKIN object
47 $this->id = intval($id);
49 // read skin name/description/content type
50 $res = sql_query('SELECT * FROM '.sql_table('skin_desc').' WHERE sdnumber=' . $this->id);
51 $obj = sql_fetch_object($res);
52 $this->isValid = (sql_num_rows($res) > 0);
53 if ( !$this->isValid )
58 $this->name = $obj->sdname;
59 $this->description = $obj->sddesc;
60 $this->contentType = $obj->sdtype;
61 $this->includeMode = $obj->sdincmode;
62 $this->includePrefix = $obj->sdincpref;
83 * Get SKIN description
85 function getDescription()
87 return $this->description;
91 * Get SKIN content type
93 * e.g. text/xml, text/html, application/atom+xml
95 function getContentType()
97 return $this->contentType;
101 * Get include mode of the SKIN
103 * Returns either 'normal' or 'skindir':
104 * 'normal': if a all data of the skin can be found in the databse
105 * 'skindir': if the skin has data in the it's skin driectory
107 function getIncludeMode()
109 return $this->includeMode;
113 * Get include prefix of the SKIN
115 * Get name of the subdirectory (with trailing slash) where
116 * the files of the current skin can be found (e.g. 'default/')
118 function getIncludePrefix()
120 return $this->includePrefix;
124 * Checks if a skin with a given shortname exists
125 * @param string $name Skin short name
126 * @return int number of skins with the given ID
129 function exists($name)
131 return quickQuery('select count(*) as result FROM ' . sql_table('skin_desc') . ' WHERE sdname="' . sql_real_escape_string($name) . '"') > 0;
135 * Checks if a skin with a given ID exists
136 * @param string $id Skin ID
137 * @return int number of skins with the given ID
140 function existsID($id)
142 return quickQuery('select COUNT(*) as result FROM ' . sql_table('skin_desc') . ' WHERE sdnumber=' . intval($id)) > 0;
146 * Returns a skin given its shortname
147 * @param string $name Skin shortname
148 * @return object SKIN
151 function createFromName($name)
153 return new SKIN(SKIN::getIdFromName($name));
157 * Returns a skin ID given its shortname
158 * @param string $name Skin shortname
159 * @return int Skin ID
162 function getIdFromName($name)
164 $query = 'SELECT sdnumber'
165 . ' FROM ' . sql_table('skin_desc')
166 . ' WHERE sdname="' . sql_real_escape_string($name) . '"';
167 $res = sql_query($query);
168 $obj = sql_fetch_object($res);
169 return $obj->sdnumber;
173 * Returns a skin shortname given its ID
174 * @param string $name
175 * @return string Skin short name
178 function getNameFromId($id)
180 return quickQuery('SELECT sdname as result FROM ' . sql_table('skin_desc') . ' WHERE sdnumber=' . intval($id));
184 * Creates a new skin, with the given characteristics.
188 function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
196 'description' => &$desc,
198 'includeMode' => &$includeMode,
199 'includePrefix' => &$includePrefix
203 sql_query('INSERT INTO ' . sql_table('skin_desc') . " (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('" . sql_real_escape_string($name) . "','" . sql_real_escape_string($desc) . "','" . sql_real_escape_string($type) . "','" . sql_real_escape_string($includeMode) . "','" . sql_real_escape_string($includePrefix) . "')");
204 $newid = sql_insert_id();
211 'description' => $desc,
213 'includeMode' => $includeMode,
214 'includePrefix' => $includePrefix
223 * @param string $type
225 function parse($type)
227 global $manager, $CONF;
229 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
232 sendContentType($this->getContentType(), 'skin');
234 // set skin name as global var (so plugins can access it)
235 global $currentSkinName;
236 $currentSkinName = $this->getName();
238 $contents = $this->getContent($type);
242 // use base skin if this skin does not have contents
243 $defskin = new SKIN($CONF['BaseSkin']);
244 $contents = $defskin->getContent($type);
252 $actions = $this->getAllowedActionsForType($type);
254 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
256 // set IncludeMode properties of parser
257 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
258 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
260 $handler = new ACTIONS($type, $this);
261 $parser = new PARSER($actions, $handler);
262 $handler->setParser($parser);
263 $handler->setSkin($this);
264 $parser->parse($contents);
266 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
270 * Get content of the skin part from the database
272 * @param $type type of the skin (e.g. index, item, search ...)
274 function getContent($type)
276 $query = 'SELECT scontent FROM '. sql_table('skin') . " WHERE sdesc=$this->id and stype='" . sql_real_escape_string($type) . "'";
277 $res = sql_query($query);
279 if ( sql_num_rows($res) == 0 )
285 return sql_result($res, 0, 0);
290 * Updates the contents for one part of the skin in the database
292 * @param $type type of the skin part (e.g. index, item, search ...)
293 * @param $content new content for this skin part
296 function update($type, $content)
302 $query = 'SELECT sdesc FROM ' . sql_table('skin') . " WHERE stype='" . sql_real_escape_string($type) . "' and sdesc=" . intval($skinid);
303 $res = sql_query($query);
304 $skintypeexists = sql_fetch_object($res);
305 $skintypevalue = ($content == true);
307 if( $skintypevalue && $skintypeexists )
309 // PreUpdateSkinPart event
315 'content' => &$content
319 else if( $skintypevalue && (!$skintypeexists) )
321 // PreAddSkinPart event
327 'content' => &$content
331 else if( (!$skintypevalue) && $skintypeexists )
333 // PreDeleteSkinPart event
343 // delete old thingie
344 sql_query('DELETE FROM ' . sql_table('skin') . " WHERE stype='" . sql_real_escape_string($type) . "' and sdesc=" . intval($skinid));
349 sql_query('INSERT INTO ' . sql_table('skin') . " SET scontent='" . sql_real_escape_string($content) . "', stype='" . sql_real_escape_string($type) . "', sdesc=" . intval($skinid));
352 if( $skintypevalue && $skintypeexists )
354 // PostUpdateSkinPart event
356 'PostUpdateSkinPart',
360 'content' => &$content
364 else if( $skintypevalue && (!$skintypeexists) )
366 // PostAddSkinPart event
372 'content' => &$content
376 else if( (!$skintypevalue) && $skintypeexists )
378 // PostDeleteSkinPart event
380 'PostDeleteSkinPart',
390 * Deletes all skin parts from the database
392 function deleteAllParts()
394 sql_query('DELETE FROM ' . sql_table('skin') . ' WHERE sdesc=' . $this->getID());
398 * Updates the general information about the skin
400 function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
402 $query = 'UPDATE '.sql_table('skin_desc').' SET'
403 . " sdname='" . sql_real_escape_string($name) . "',"
404 . " sddesc='" . sql_real_escape_string($desc) . "',"
405 . " sdtype='" . sql_real_escape_string($type) . "',"
406 . " sdincmode='" . sql_real_escape_string($includeMode) . "',"
407 . " sdincpref='" . sql_real_escape_string($includePrefix) . "'"
408 . " WHERE sdnumber=" . $this->getID();
413 * Get an array with the names of possible skin parts
414 * Used to show all possible parts of a skin in the administration backend
416 * static: returns an array of friendly names
418 function getFriendlyNames()
421 'index' => _SKIN_PART_MAIN,
422 'item' => _SKIN_PART_ITEM,
423 'archivelist' => _SKIN_PART_ALIST,
424 'archive' => _SKIN_PART_ARCHIVE,
425 'search' => _SKIN_PART_SEARCH,
426 'error' => _SKIN_PART_ERROR,
427 'member' => _SKIN_PART_MEMBER,
428 'imagepopup' => _SKIN_PART_POPUP
431 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
432 $res = sql_query($query);
433 while ( $row = sql_fetch_array($res) )
435 $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
441 * Get the allowed actions for a skin type
442 * returns an array with the allowed actions
444 * @param $type type of the skin (e.g. index, item, search ...)
446 function getAllowedActionsForType($type)
450 // some actions that can be performed at any time, from anywhere
451 $defaultActions = array('otherblog',
461 'otherarchivedaylist',
462 'otherarchiveyearlist',
485 // deprecated (Nucleus v2.0)
489 // extra actions specific for a certain skin type
490 $extraActions = array();
495 $extraActions = array('blog',
508 $extraActions = array('blog',
525 $extraActions = array('blog',
534 $extraActions = array('blog',
540 'othersearchresults',
548 $extraActions = array('image',
549 // deprecated (Nucleus v2.0)
554 $extraActions = array(
562 $extraActions = array('blog',
584 $extraActions = array(
590 if ( $blogid && $blogid > 0 )
592 $extraActions = array(
610 return array_merge($defaultActions, $extraActions);