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));
185 * Creates a new skin, with the given characteristics.
188 * @param String $name value for nucleus_skin.sdname
189 * @param String $desc value for nucleus_skin.sddesc
190 * @param String $type value for nucleus_skin.sdtype
191 * @param String $includeMode value for nucleus_skin.sdinclude
192 * @param String $includePrefix value for nucleus_skin.sdincpref
193 * @return Integer ID for just inserted record
196 function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
204 'description' => &$desc,
206 'includeMode' => &$includeMode,
207 'includePrefix' => &$includePrefix
211 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s')";
212 $query = sprintf($query, sql_table('skin_desc'), 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));
214 $newid = sql_insert_id();
221 'description' => $desc,
223 'includeMode' => $includeMode,
224 'includePrefix' => $includePrefix
233 * @param string $type
235 function parse($type)
237 global $manager, $CONF;
239 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
242 sendContentType($this->getContentType(), 'skin');
244 // set skin name as global var (so plugins can access it)
245 global $currentSkinName;
246 $currentSkinName = $this->getName();
248 $contents = $this->getContent($type);
252 // use base skin if this skin does not have contents
253 $defskin = new SKIN($CONF['BaseSkin']);
254 $contents = $defskin->getContent($type);
262 $actions = $this->getAllowedActionsForType($type);
264 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
266 // set IncludeMode properties of parser
267 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
268 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
270 $handler = new ACTIONS($type, $this);
271 $parser = new PARSER($actions, $handler);
272 $handler->setParser($parser);
273 $handler->setSkin($this);
274 $parser->parse($contents);
276 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
280 * Get content of the skin part from the database
282 * @param $type type of the skin (e.g. index, item, search ...)
284 function getContent($type)
286 $query = 'SELECT scontent FROM '. sql_table('skin') . " WHERE sdesc=$this->id and stype='" . sql_real_escape_string($type) . "'";
287 $res = sql_query($query);
289 if ( sql_num_rows($res) == 0 )
295 return sql_result($res, 0, 0);
301 * Updates the contents for one part of the skin in the database
303 * @param $type type of the skin part (e.g. index, item, search ...)
304 * @param $content new content for this skin part
308 function update($type, $content)
314 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d";
315 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $skinid);
316 $res = sql_query($query);
318 $skintypeexists = sql_fetch_object($res);
319 $skintypevalue = ($content == true);
321 if( $skintypevalue && $skintypeexists )
323 // PreUpdateSkinPart event
329 'content' => &$content
333 else if( $skintypevalue && (!$skintypeexists) )
335 // PreAddSkinPart event
341 'content' => &$content
345 else if( (!$skintypevalue) && $skintypeexists )
347 // PreDeleteSkinPart event
357 // delete old thingie
358 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
359 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $skinid);
365 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
366 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $skinid);
370 if( $skintypevalue && $skintypeexists )
372 // PostUpdateSkinPart event
374 'PostUpdateSkinPart',
378 'content' => &$content
382 else if( $skintypevalue && (!$skintypeexists) )
384 // PostAddSkinPart event
390 'content' => &$content
394 else if( (!$skintypevalue) && $skintypeexists )
396 // PostDeleteSkinPart event
398 'PostDeleteSkinPart',
409 * Deletes all skin parts from the database
411 function deleteAllParts()
413 sql_query('DELETE FROM ' . sql_table('skin') . ' WHERE sdesc=' . $this->getID());
417 * Updates the general information about the skin
419 function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
421 $query = 'UPDATE '.sql_table('skin_desc').' SET'
422 . " sdname='" . sql_real_escape_string($name) . "',"
423 . " sddesc='" . sql_real_escape_string($desc) . "',"
424 . " sdtype='" . sql_real_escape_string($type) . "',"
425 . " sdincmode='" . sql_real_escape_string($includeMode) . "',"
426 . " sdincpref='" . sql_real_escape_string($includePrefix) . "'"
427 . " WHERE sdnumber=" . $this->getID();
432 * Get an array with the names of possible skin parts
433 * Used to show all possible parts of a skin in the administration backend
435 * static: returns an array of friendly names
437 function getFriendlyNames()
440 'index' => _SKIN_PART_MAIN,
441 'item' => _SKIN_PART_ITEM,
442 'archivelist' => _SKIN_PART_ALIST,
443 'archive' => _SKIN_PART_ARCHIVE,
444 'search' => _SKIN_PART_SEARCH,
445 'error' => _SKIN_PART_ERROR,
446 'member' => _SKIN_PART_MEMBER,
447 'imagepopup' => _SKIN_PART_POPUP
450 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
451 $res = sql_query($query);
452 while ( $row = sql_fetch_array($res) )
454 $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
460 * Get the allowed actions for a skin type
461 * returns an array with the allowed actions
463 * @param $type type of the skin (e.g. index, item, search ...)
465 function getAllowedActionsForType($type)
469 // some actions that can be performed at any time, from anywhere
470 $defaultActions = array('otherblog',
480 'otherarchivedaylist',
481 'otherarchiveyearlist',
504 // deprecated (Nucleus v2.0)
508 // extra actions specific for a certain skin type
509 $extraActions = array();
514 $extraActions = array('blog',
527 $extraActions = array('blog',
544 $extraActions = array('blog',
553 $extraActions = array('blog',
559 'othersearchresults',
567 $extraActions = array('image',
568 // deprecated (Nucleus v2.0)
573 $extraActions = array(
581 $extraActions = array('blog',
603 $extraActions = array(
609 if ( $blogid && $blogid > 0 )
611 $extraActions = array(
629 return array_merge($defaultActions, $extraActions);