OSDN Git Service

PDO対応
[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-2009 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-2009 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="'.addslashes($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="'.addslashes($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                 $manager->notify(
126                         'PreAddSkin',
127                         array(
128                                 'name' => &$name,
129                                 'description' => &$desc,
130                                 'type' => &$type,
131                                 'includeMode' => &$includeMode,
132                                 'includePrefix' => &$includePrefix
133                         )
134                 );
135
136                 sql_query('INSERT INTO '.sql_table('skin_desc')." (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('" . addslashes($name) . "','" . addslashes($desc) . "','".addslashes($type)."','".addslashes($includeMode)."','".addslashes($includePrefix)."')");
137                 $newid = sql_insert_id();
138
139                 $manager->notify(
140                         'PostAddSkin',
141                         array(
142                                 'skinid' => $newid,
143                                 'name' => $name,
144                                 'description' => $desc,
145                                 'type' => $type,
146                                 'includeMode' => $includeMode,
147                                 'includePrefix' => $includePrefix
148                         )
149                 );
150
151                 return $newid;
152         }
153
154         function parse($type) {
155                 global $manager, $CONF;
156
157                 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
158
159                 // set output type
160                 sendContentType($this->getContentType(), 'skin', _CHARSET);
161
162                 // set skin name as global var (so plugins can access it)
163                 global $currentSkinName;
164                 $currentSkinName = $this->getName();
165
166                 $contents = $this->getContent($type);
167
168                 if (!$contents) {
169                         // use base skin if this skin does not have contents
170                         $defskin =& new SKIN($CONF['BaseSkin']);
171                         $contents = $defskin->getContent($type);
172                         if (!$contents) {
173                                 echo _ERROR_SKIN;
174                                 return;
175                         }
176                 }
177
178                 $actions = $this->getAllowedActionsForType($type);
179
180                 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
181
182                 // set IncludeMode properties of parser
183                 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
184                 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
185
186                 $handler =& new ACTIONS($type, $this);
187                 $parser =& new PARSER($actions, $handler);
188                 $handler->setParser($parser);
189                 $handler->setSkin($this);
190                 $parser->parse($contents);
191
192                 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
193
194
195         }
196
197         function getContent($type) {
198                 $query = 'SELECT scontent FROM '.sql_table('skin')." WHERE sdesc=$this->id and stype='". addslashes($type) ."'";
199                 $res = sql_query($query);
200
201                 if (sql_num_rows($res) == 0)
202                         return '';
203                 else
204                         return sql_result($res, 0, 0);
205         }
206
207         /**
208          * Updates the contents of one part of the skin
209          */
210         function update($type, $content) {
211                 $skinid = $this->id;
212
213                 // delete old thingie
214                 sql_query('DELETE FROM '.sql_table('skin')." WHERE stype='".addslashes($type)."' and sdesc=" . intval($skinid));
215
216                 // write new thingie
217                 if ($content) {
218                         sql_query('INSERT INTO '.sql_table('skin')." SET scontent='" . addslashes($content) . "', stype='" . addslashes($type) . "', sdesc=" . intval($skinid));
219                 }
220         }
221
222         /**
223          * Deletes all skin parts from the database
224          */
225         function deleteAllParts() {
226                 sql_query('DELETE FROM '.sql_table('skin').' WHERE sdesc='.$this->getID());
227         }
228
229         /**
230          * Updates the general information about the skin
231          */
232         function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '') {
233                 $query =  'UPDATE '.sql_table('skin_desc').' SET'
234                            . " sdname='" . addslashes($name) . "',"
235                            . " sddesc='" . addslashes($desc) . "',"
236                            . " sdtype='" . addslashes($type) . "',"
237                            . " sdincmode='" . addslashes($includeMode) . "',"
238                            . " sdincpref='" . addslashes($includePrefix) . "'"
239                            . " WHERE sdnumber=" . $this->getID();
240                 sql_query($query);
241         }
242
243         /**
244          * static: returns an array of friendly names
245          */
246         function getFriendlyNames() {
247                 $skintypes = array(
248                         'index' => _SKIN_PART_MAIN,
249                         'item' => _SKIN_PART_ITEM,
250                         'archivelist' => _SKIN_PART_ALIST,
251                         'archive' => _SKIN_PART_ARCHIVE,
252                         'search' => _SKIN_PART_SEARCH,
253                         'error' => _SKIN_PART_ERROR,
254                         'member' => _SKIN_PART_MEMBER,
255                         'imagepopup' => _SKIN_PART_POPUP
256                 );
257
258                 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
259                 $res = sql_query($query);
260                 while ($row = sql_fetch_array($res)) {
261                         $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
262                 }
263
264                 return $skintypes;
265         }
266
267         function getAllowedActionsForType($type) {
268                 global $blogid;
269
270                 // some actions that can be performed at any time, from anywhere
271                 $defaultActions = array('otherblog',
272                                                                 'plugin',
273                                                                 'version',
274                                                                 'nucleusbutton',
275                                                                 'include',
276                                                                 'phpinclude',
277                                                                 'parsedinclude',
278                                                                 'loginform',
279                                                                 'sitevar',
280                                                                 'otherarchivelist',
281                                                                 'otherarchivedaylist',
282                                                                 'otherarchiveyearlist',
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                                                                 'addlink',
304                                                                 'addpopupcode',
305                                                                 'sticky'
306                                                                 );
307
308                 // extra actions specific for a certain skin type
309                 $extraActions = array();
310
311                 switch ($type) {
312                         case 'index':
313                                 $extraActions = array('blog',
314                                                                 'blogsetting',
315                                                                 'preview',
316                                                                 'additemform',
317                                                                 'categorylist',
318                                                                 'archivelist',
319                                                                 'archivedaylist',
320                                                                 'archiveyearlist',
321                                                                 'nextlink',
322                                                                 'prevlink'
323                                                                 );
324                                 break;
325                         case 'archive':
326                                 $extraActions = array('blog',
327                                                                 'archive',
328                                                                 'otherarchive',
329                                                                 'categorylist',
330                                                                 'archivelist',
331                                                                 'archivedaylist',
332                                                                 'archiveyearlist',
333                                                                 'blogsetting',
334                                                                 'archivedate',
335                                                                 'nextarchive',
336                                                                 'prevarchive',
337                                                                 'nextlink',
338                                                                 'prevlink',
339                                                                 'archivetype'
340                                 );
341                                 break;
342                         case 'archivelist':
343                                 $extraActions = array('blog',
344                                                                 'archivelist',
345                                                                 'archivedaylist',
346                                                                 'archiveyearlist',
347                                                                 'categorylist',
348                                                                 'blogsetting',
349                                                            );
350                                 break;
351                         case 'search':
352                                 $extraActions = array('blog',
353                                                                 'archivelist',
354                                                                 'archivedaylist',
355                                                                 'archiveyearlist',
356                                                                 'categorylist',
357                                                                 'searchresults',
358                                                                 'othersearchresults',
359                                                                 'blogsetting',
360                                                                 'query',
361                                                                 'nextlink',
362                                                                 'prevlink'
363                                                                 );
364                                 break;
365                         case 'imagepopup':
366                                 $extraActions = array('image',
367                                                                 'imagetext',                            // deprecated (Nucleus v2.0)
368                                                                 );
369                                 break;
370                         case 'member':
371                                 $extraActions = array(
372                                                                 'membermailform',
373                                                                 'blogsetting',
374                                                                 'nucleusbutton'
375                                 );
376                                 break;
377                         case 'item':
378                                 $extraActions = array('blog',
379                                                                 'item',
380                                                                 'comments',
381                                                                 'commentform',
382                                                                 'vars',
383                                                                 'blogsetting',
384                                                                 'nextitem',
385                                                                 'previtem',
386                                                                 'nextlink',
387                                                                 'prevlink',
388                                                                 'nextitemtitle',
389                                                                 'previtemtitle',
390                                                                 'categorylist',
391                                                                 'archivelist',
392                                                                 'archivedaylist',
393                                                                 'archiveyearlist',
394                                                                 'itemtitle',
395                                                                 'itemid',
396                                                                 'itemlink',
397                                                                 );
398                                 break;
399                         case 'error':
400                                 $extraActions = array(
401                                                                 'errormessage'
402                                 );
403                                 break;
404                         default:
405                                 if ($blogid && $blogid > 0) {
406                                         $extraActions = array(
407                                                 'blog',
408                                                 'blogsetting',
409                                                 'preview',
410                                                 'additemform',
411                                                 'categorylist',
412                                                 'archivelist',
413                                                 'archivedaylist',
414                                                 'archiveyearlist',
415                                                 'nextlink',
416                                                 'prevlink',
417                                                 'membermailform',
418                                                 'nucleusbutton'
419                                         );
420                                 }
421                                 break;
422                 }
423
424                 return array_merge($defaultActions, $extraActions);
425         }
426
427 }
428
429 ?>