OSDN Git Service

126233a9ffbadd622cfcfd137d3d333c35a04357
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / SKIN.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2007 The Nucleus Group
5  *
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)
11  */
12
13 /**
14  * Class representing a skin
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2007 The Nucleus Group
18  * @version $Id: SKIN.php,v 1.8 2007-03-20 19:31:29 kmorimatsu Exp $
19  * @version $NucleusJP: SKIN.php,v 1.7 2007/02/04 06:28:46 kimitake Exp $
20  */
21
22 if ( !function_exists('requestVar') ) exit;
23 require_once dirname(__FILE__) . '/ACTIONS.php';
24
25 class SKIN {
26
27         // after creating a SKIN object, evaluates to true when the skin exists
28         var $isValid;
29
30         // skin characteristics. Use the getXXX methods rather than accessing directly
31         var $id;
32         var $description;
33         var $contentType;
34         var $includeMode;               // either 'normal' or 'skindir'
35         var $includePrefix;
36         var $name;
37
38         function SKIN($id) {
39                 $this->id = intval($id);
40
41                 // read skin name/description/content type
42                 $res = sql_query('SELECT * FROM '.sql_table('skin_desc').' WHERE sdnumber=' . $this->id);
43                 $obj = mysql_fetch_object($res);
44                 $this->isValid = (mysql_num_rows($res) > 0);
45                 if (!$this->isValid)
46                         return;
47
48                 $this->name = $obj->sdname;
49                 $this->description = $obj->sddesc;
50                 $this->contentType = $obj->sdtype;
51                 $this->includeMode = $obj->sdincmode;
52                 $this->includePrefix = $obj->sdincpref;
53
54         }
55
56         function getID() {                              return $this->id; }
57         function getName() {                    return $this->name; }
58         function getDescription() {     return $this->description; }
59         function getContentType() {     return $this->contentType; }
60         function getIncludeMode() {     return $this->includeMode; }
61         function getIncludePrefix() {   return $this->includePrefix; }
62
63         /**
64          * Checks if a skin with a given shortname exists
65          * @param string $name Skin short name
66          * @return int number of skins with the given ID
67          * @static
68          */
69         function exists($name) {
70                 return quickQuery('select count(*) as result FROM '.sql_table('skin_desc').' WHERE sdname="'.addslashes($name).'"') > 0;
71         }
72
73         /**
74          * Checks if a skin with a given ID exists
75          * @param string $id Skin ID
76          * @return int number of skins with the given ID
77          * @static
78          */
79         function existsID($id) {
80                 return quickQuery('select COUNT(*) as result FROM '.sql_table('skin_desc').' WHERE sdnumber='.intval($id)) > 0;
81         }
82
83         /**
84          * Returns a skin given its shortname
85          * @param string $name Skin shortname
86          * @return object SKIN
87          * @static
88          */
89         function createFromName($name) {
90                 return new SKIN(SKIN::getIdFromName($name));
91         }
92
93         /**
94          * Returns a skin ID given its shortname
95          * @param string $name Skin shortname
96          * @return int Skin ID
97          * @static
98          */
99         function getIdFromName($name) {
100                 $query =  'SELECT sdnumber'
101                            . ' FROM '.sql_table('skin_desc')
102                            . ' WHERE sdname="'.addslashes($name).'"';
103                 $res = sql_query($query);
104                 $obj = mysql_fetch_object($res);
105                 return $obj->sdnumber;
106         }
107
108         /**
109          * Returns a skin shortname given its ID
110          * @param string $name
111          * @return string Skin short name
112          * @static
113          */
114         function getNameFromId($id) {
115                 return quickQuery('SELECT sdname as result FROM '.sql_table('skin_desc').' WHERE sdnumber=' . intval($id));
116         }
117
118         /**
119          * Creates a new skin, with the given characteristics.
120          *
121          * @static
122          */
123         function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '') {
124                 global $manager;
125
126                 $manager->notify(
127                         'PreAddSkin',
128                         array(
129                                 'name' => &$name,
130                                 'description' => &$desc,
131                                 'type' => &$type,
132                                 'includeMode' => &$includeMode,
133                                 'includePrefix' => &$includePrefix
134                         )
135                 );
136
137                 sql_query('INSERT INTO '.sql_table('skin_desc')." (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "','".addslashes($type)."','".addslashes($includeMode)."','".addslashes($includePrefix)."')");
138                 $newid = mysql_insert_id();
139
140                 $manager->notify(
141                         'PostAddSkin',
142                         array(
143                                 'skinid' => $newid,
144                                 'name' => $name,
145                                 'description' => $desc,
146                                 'type' => $type,
147                                 'includeMode' => $includeMode,
148                                 'includePrefix' => $includePrefix
149                         )
150                 );
151
152                 return $newid;
153         }
154
155         function parse($type) {
156                 global $manager, $CONF;
157
158                 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
159
160                 // set output type
161                 sendContentType($this->getContentType(), 'skin', _CHARSET);
162
163                 // set skin name as global var (so plugins can access it)
164                 global $currentSkinName;
165                 $currentSkinName = $this->getName();
166
167                 $contents = $this->getContent($type);
168
169                 if (!$contents) {
170                         // use base skin if this skin does not have contents
171                         $defskin =& new SKIN($CONF['BaseSkin']);
172                         $contents = $defskin->getContent($type);
173                         if (!$contents) {
174                                 echo _ERROR_SKIN;
175                                 return;
176                         }
177                 }
178
179                 $actions = $this->getAllowedActionsForType($type);
180
181                 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
182
183                 // set IncludeMode properties of parser
184                 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
185                 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
186
187                 $handler =& new ACTIONS($type, $this);
188                 $parser =& new PARSER($actions, $handler);
189                 $handler->setParser($parser);
190                 $handler->setSkin($this);
191                 $parser->parse($contents);
192
193                 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
194
195
196         }
197
198         function getContent($type) {
199                 $query = 'SELECT scontent FROM '.sql_table('skin')." WHERE sdesc=$this->id and stype='". addslashes($type) ."'";
200                 $res = sql_query($query);
201
202                 if (mysql_num_rows($res) == 0)
203                         return '';
204                 else
205                         return mysql_result($res, 0, 0);
206         }
207
208         /**
209          * Updates the contents of one part of the skin
210          */
211         function update($type, $content) {
212                 $skinid = $this->id;
213
214                 // delete old thingie
215                 sql_query('DELETE FROM '.sql_table('skin')." WHERE stype='".addslashes($type)."' and sdesc=" . intval($skinid));
216
217                 // write new thingie
218                 if ($content) {
219                         sql_query('INSERT INTO '.sql_table('skin')." SET scontent='" . addslashes($content) . "', stype='" . addslashes($type) . "', sdesc=" . intval($skinid));
220                 }
221         }
222
223         /**
224          * Deletes all skin parts from the database
225          */
226         function deleteAllParts() {
227                 sql_query('DELETE FROM '.sql_table('skin').' WHERE sdesc='.$this->getID());
228         }
229
230         /**
231          * Updates the general information about the skin
232          */
233         function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '') {
234                 $query =  'UPDATE '.sql_table('skin_desc').' SET'
235                            . " sdname='" . addslashes($name) . "',"
236                            . " sddesc='" . addslashes($desc) . "',"
237                            . " sdtype='" . addslashes($type) . "',"
238                            . " sdincmode='" . addslashes($includeMode) . "',"
239                            . " sdincpref='" . addslashes($includePrefix) . "'"
240                            . " WHERE sdnumber=" . $this->getID();
241                 sql_query($query);
242         }
243
244         /**
245          * static: returns an array of friendly names
246          */
247         function getFriendlyNames() {
248                 $skintypes = array(
249                         'index' => _SKIN_PART_MAIN,
250                         'item' => _SKIN_PART_ITEM,
251                         'archivelist' => _SKIN_PART_ALIST,
252                         'archive' => _SKIN_PART_ARCHIVE,
253                         'search' => _SKIN_PART_SEARCH,
254                         'error' => _SKIN_PART_ERROR,
255                         'member' => _SKIN_PART_MEMBER,
256                         'imagepopup' => _SKIN_PART_POPUP
257                 );
258
259                 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
260                 $res = sql_query($query);
261                 while ($row = mysql_fetch_array($res)) {
262                         $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
263                 }
264
265                 return $skintypes;
266         }
267
268         function getAllowedActionsForType($type) {
269                 global $blogid;
270
271                 // some actions that can be performed at any time, from anywhere
272                 $defaultActions = array('otherblog',
273                                                                 'plugin',
274                                                                 'version',
275                                                                 'nucleusbutton',
276                                                                 'include',
277                                                                 'phpinclude',
278                                                                 'parsedinclude',
279                                                                 'loginform',
280                                                                 'sitevar',
281                                                                 'otherarchivelist',
282                                                                 'otherarchivedaylist',
283                                                                 'self',
284                                                                 'adminurl',
285                                                                 'todaylink',
286                                                                 'archivelink',
287                                                                 'member',
288                                                                 'ifcat',                                        // deprecated (Nucleus v2.0)
289                                                                 'category',
290                                                                 'searchform',
291                                                                 'referer',
292                                                                 'skinname',
293                                                                 'skinfile',
294                                                                 'set',
295                                                                 'if',
296                                                                 'else',
297                                                                 'endif',
298                                                                 'elseif',
299                                                                 'ifnot',
300                                                                 'elseifnot',
301                                                                 'charset',
302                                                                 'bloglist'
303                                                                 );
304
305                 // extra actions specific for a certain skin type
306                 $extraActions = array();
307
308                 switch ($type) {
309                         case 'index':
310                                 $extraActions = array('blog',
311                                                                 'blogsetting',
312                                                                 'preview',
313                                                                 'additemform',
314                                                                 'categorylist',
315                                                                 'archivelist',
316                                                                 'archivedaylist',
317                                                                 'nextlink',
318                                                                 'prevlink'
319                                                                 );
320                                 break;
321                         case 'archive':
322                                 $extraActions = array('blog',
323                                                                 'archive',
324                                                                 'otherarchive',
325                                                                 'categorylist',
326                                                                 'archivelist',
327                                                                 'archivedaylist',
328                                                                 'blogsetting',
329                                                                 'archivedate',
330                                                                 'nextarchive',
331                                                                 'prevarchive',
332                                                                 'nextlink',
333                                                                 'prevlink',
334                                                                 'archivetype'
335                                 );
336                                 break;
337                         case 'archivelist':
338                                 $extraActions = array('blog',
339                                                                 'archivelist',
340                                                                 'archivedaylist',
341                                                                 'categorylist',
342                                                                 'blogsetting',
343                                                            );
344                                 break;
345                         case 'search':
346                                 $extraActions = array('blog',
347                                                                 'archivelist',
348                                                                 'archivedaylist',
349                                                                 'categorylist',
350                                                                 'searchresults',
351                                                                 'othersearchresults',
352                                                                 'blogsetting',
353                                                                 'query',
354                                                                 'nextlink',
355                                                                 'prevlink'
356                                                                 );
357                                 break;
358                         case 'imagepopup':
359                                 $extraActions = array('image',
360                                                                 'imagetext',                            // deprecated (Nucleus v2.0)
361                                                                 );
362                                 break;
363                         case 'member':
364                                 $extraActions = array(
365                                                                 'membermailform',
366                                                                 'blogsetting',
367                                                                 'nucleusbutton'
368                                 );
369                                 break;
370                         case 'item':
371                                 $extraActions = array('blog',
372                                                                 'item',
373                                                                 'comments',
374                                                                 'commentform',
375                                                                 'vars',
376                                                                 'blogsetting',
377                                                                 'nextitem',
378                                                                 'previtem',
379                                                                 'nextlink',
380                                                                 'prevlink',
381                                                                 'nextitemtitle',
382                                                                 'previtemtitle',
383                                                                 'categorylist',
384                                                                 'archivelist',
385                                                                 'archivedaylist',
386                                                                 'itemtitle',
387                                                                 'itemid',
388                                                                 'itemlink',
389                                                                 );
390                                 break;
391                         case 'error':
392                                 $extraActions = array(
393                                                                 'errormessage'
394                                 );
395                                 break;
396                         default:
397                                 if ($blogid && $blogid > 0) {
398                                         $extraActions = array(
399                                                 'blog',
400                                                 'blogsetting',
401                                                 'preview',
402                                                 'additemform',
403                                                 'categorylist',
404                                                 'archivelist',
405                                                 'archivedaylist',
406                                                 'nextlink',
407                                                 'archivelist',
408                                                 'archivedaylist',
409                                                 'prevlink',
410                                                 'membermailform',
411                                                 'nucleusbutton'
412                                         );
413                                 }
414                                 break;
415                 }
416
417                 return array_merge($defaultActions, $extraActions);
418         }
419
420 }
421
422 ?>