OSDN Git Service

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