OSDN Git Service

4.0のtrunkを展開するためにmasterディレクトリを作成しファイル群を移動した。
[nucleus-jp/nucleus-jp-ancient.git] / master / nucleus / libs / SKIN.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2011 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-2011 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                 $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 ('" . 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)."')");
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, $skinid;
156                 
157                 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
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                 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
182                 $skinid = $this->id;
183                 
184                 // set IncludeMode properties of parser
185                 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
186                 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
187                 
188                 $handler =& new ACTIONS($type, $this);
189                 $parser =& new PARSER($actions, $handler);
190                 $handler->setParser($parser);
191                 $handler->setSkin($this);
192                 $parser->parse($contents);
193                 
194                 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
195                 $skinid = $this->id;
196
197
198         }
199
200         function getContent($type) {
201                 $query = 'SELECT scontent FROM '.sql_table('skin')." WHERE sdesc=$this->id and stype='". sql_real_escape_string($type) ."'";
202                 $res = sql_query($query);
203
204                 if (sql_num_rows($res) == 0)
205                         return '';
206                 else
207                         return sql_result($res, 0, 0);
208         }
209
210         /**
211          * Updates the contents of one part of the skin
212          */
213         function update($type, $content) {
214                 $skinid = $this->id;
215
216                 // delete old thingie
217                 sql_query('DELETE FROM '.sql_table('skin')." WHERE stype='".sql_real_escape_string($type)."' and sdesc=" . intval($skinid));
218
219                 // write new thingie
220                 if ($content) {
221                         sql_query('INSERT INTO '.sql_table('skin')." SET scontent='" . sql_real_escape_string($content) . "', stype='" . sql_real_escape_string($type) . "', sdesc=" . intval($skinid));
222                 }
223         }
224
225         /**
226          * Deletes all skin parts from the database
227          */
228         function deleteAllParts() {
229                 sql_query('DELETE FROM '.sql_table('skin').' WHERE sdesc='.$this->getID());
230         }
231
232         /**
233          * Updates the general information about the skin
234          */
235         function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '') {
236                 $query =  'UPDATE '.sql_table('skin_desc').' SET'
237                            . " sdname='" . sql_real_escape_string($name) . "',"
238                            . " sddesc='" . sql_real_escape_string($desc) . "',"
239                            . " sdtype='" . sql_real_escape_string($type) . "',"
240                            . " sdincmode='" . sql_real_escape_string($includeMode) . "',"
241                            . " sdincpref='" . sql_real_escape_string($includePrefix) . "'"
242                            . " WHERE sdnumber=" . $this->getID();
243                 sql_query($query);
244         }
245
246         /**
247          * static: returns an array of friendly names
248          */
249         function getFriendlyNames() {
250                 $skintypes = array(
251                         'index' => _SKIN_PART_MAIN,
252                         'item' => _SKIN_PART_ITEM,
253                         'archivelist' => _SKIN_PART_ALIST,
254                         'archive' => _SKIN_PART_ARCHIVE,
255                         'search' => _SKIN_PART_SEARCH,
256                         'error' => _SKIN_PART_ERROR,
257                         'member' => _SKIN_PART_MEMBER,
258                         'imagepopup' => _SKIN_PART_POPUP
259                 );
260
261                 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
262                 $res = sql_query($query);
263                 while ($row = sql_fetch_array($res)) {
264                         $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
265                 }
266
267                 return $skintypes;
268         }
269
270         function getAllowedActionsForType($type) {
271                 global $blogid;
272
273                 // some actions that can be performed at any time, from anywhere
274                 $defaultActions = array('otherblog',
275                                                                 'plugin',
276                                                                 'version',
277                                                                 'nucleusbutton',
278                                                                 'include',
279                                                                 'phpinclude',
280                                                                 'parsedinclude',
281                                                                 'loginform',
282                                                                 'sitevar',
283                                                                 'otherarchivelist',
284                                                                 'otherarchivedaylist',
285                                                                 'otherarchiveyearlist',
286                                                                 'self',
287                                                                 'adminurl',
288                                                                 'todaylink',
289                                                                 'archivelink',
290                                                                 'member',
291                                                                 'ifcat',                                        // deprecated (Nucleus v2.0)
292                                                                 'category',
293                                                                 'searchform',
294                                                                 'referer',
295                                                                 'skinname',
296                                                                 'skinfile',
297                                                                 'set',
298                                                                 'if',
299                                                                 'else',
300                                                                 'endif',
301                                                                 'elseif',
302                                                                 'ifnot',
303                                                                 'elseifnot',
304                                                                 'charset',
305                                                                 'bloglist',
306                                                                 'addlink',
307                                                                 'addpopupcode',
308                                                                 'sticky'
309                                                                 );
310
311                 // extra actions specific for a certain skin type
312                 $extraActions = array();
313
314                 switch ($type) {
315                         case 'index':
316                                 $extraActions = array('blog',
317                                                                 'blogsetting',
318                                                                 'preview',
319                                                                 'additemform',
320                                                                 'categorylist',
321                                                                 'archivelist',
322                                                                 'archivedaylist',
323                                                                 'archiveyearlist',
324                                                                 'nextlink',
325                                                                 'prevlink'
326                                                                 );
327                                 break;
328                         case 'archive':
329                                 $extraActions = array('blog',
330                                                                 'archive',
331                                                                 'otherarchive',
332                                                                 'categorylist',
333                                                                 'archivelist',
334                                                                 'archivedaylist',
335                                                                 'archiveyearlist',
336                                                                 'blogsetting',
337                                                                 'archivedate',
338                                                                 'nextarchive',
339                                                                 'prevarchive',
340                                                                 'nextlink',
341                                                                 'prevlink',
342                                                                 'archivetype'
343                                 );
344                                 break;
345                         case 'archivelist':
346                                 $extraActions = array('blog',
347                                                                 'archivelist',
348                                                                 'archivedaylist',
349                                                                 'archiveyearlist',
350                                                                 'categorylist',
351                                                                 'blogsetting',
352                                                            );
353                                 break;
354                         case 'search':
355                                 $extraActions = array('blog',
356                                                                 'archivelist',
357                                                                 'archivedaylist',
358                                                                 'archiveyearlist',
359                                                                 'categorylist',
360                                                                 'searchresults',
361                                                                 'othersearchresults',
362                                                                 'blogsetting',
363                                                                 'query',
364                                                                 'nextlink',
365                                                                 'prevlink'
366                                                                 );
367                                 break;
368                         case 'imagepopup':
369                                 $extraActions = array('image',
370                                                                 'imagetext',                            // deprecated (Nucleus v2.0)
371                                                                 );
372                                 break;
373                         case 'member':
374                                 $extraActions = array(
375                                                                 'membermailform',
376                                                                 'blogsetting',
377 //                                                              'nucleusbutton'
378                                                                 'categorylist'
379                                 );
380                                 break;
381                         case 'item':
382                                 $extraActions = array('blog',
383                                                                 'item',
384                                                                 'comments',
385                                                                 'commentform',
386                                                                 'vars',
387                                                                 'blogsetting',
388                                                                 'nextitem',
389                                                                 'previtem',
390                                                                 'nextlink',
391                                                                 'prevlink',
392                                                                 'nextitemtitle',
393                                                                 'previtemtitle',
394                                                                 'categorylist',
395                                                                 'archivelist',
396                                                                 'archivedaylist',
397                                                                 'archiveyearlist',
398                                                                 'itemtitle',
399                                                                 'itemid',
400                                                                 'itemlink',
401                                                                 );
402                                 break;
403                         case 'error':
404                                 $extraActions = array(
405                                                                 'errormessage',
406                                                                 'categorylist'
407                                 );
408                                 break;
409                         default:
410                                 if ($blogid && $blogid > 0) {
411                                         $extraActions = array(
412                                                 'blog',
413                                                 'blogsetting',
414                                                 'preview',
415                                                 'additemform',
416                                                 'categorylist',
417                                                 'archivelist',
418                                                 'archivedaylist',
419                                                 'archiveyearlist',
420                                                 'nextlink',
421                                                 'prevlink',
422                                                 'membermailform',
423 //                                              'nucleusbutton'
424                                                 'categorylist'
425                                         );
426                                 }
427                                 break;
428                 }
429
430                 return array_merge($defaultActions, $extraActions);
431         }
432
433 }
434
435 ?>