OSDN Git Service

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