OSDN Git Service

e2ef5e2fbe90392c6b9716f6c1ac4856f4b84893
[nucleus-jp/nucleus-next.git] / 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: SKIN.php 1621 2012-01-09 02:49:37Z sakamocchi $
18  */
19
20 if ( !function_exists('requestVar') ) 
21 {
22         exit;
23 }
24 require_once dirname(__FILE__) . '/ACTIONS.php';
25
26 class SKIN {
27         
28         // after creating a SKIN object, evaluates to true when the skin exists
29         var $isValid;
30         
31         // skin characteristics. Use the getXXX methods rather than accessing directly
32         var $id;
33         var $description;
34         var $contentType;
35         var $includeMode;               // either 'normal' or 'skindir'
36         var $includePrefix;
37         var $name;
38         
39         /**
40          * Constructor for a new SKIN object
41          * 
42          * @param $id 
43          *                      id of the skin
44          */
45         function SKIN($id)
46         {
47                 $this->id = intval($id);
48                 
49                 // read skin name/description/content type
50                 $res = sql_query('SELECT * FROM '.sql_table('skin_desc').' WHERE sdnumber=' . $this->id);
51                 $obj = sql_fetch_object($res);
52                 $this->isValid = (sql_num_rows($res) > 0);
53                 if ( !$this->isValid )
54                 {
55                         return;
56                 }
57                 
58                 $this->name = $obj->sdname;
59                 $this->description = $obj->sddesc;
60                 $this->contentType = $obj->sdtype;
61                 $this->includeMode = $obj->sdincmode;
62                 $this->includePrefix = $obj->sdincpref;
63
64         }
65         
66         /**
67          * Get SKIN id
68          */
69         function getID()
70         {
71                 return $this->id;
72         }
73         
74         /**
75          * Get SKIN name
76          */
77         function getName()
78         {
79                 return $this->name;
80         }
81         
82         /**
83          * Get SKIN description
84          */
85         function getDescription()
86         {
87                 return $this->description;
88         }
89         
90         /**
91          * Get SKIN content type
92          * 
93          * e.g. text/xml, text/html, application/atom+xml
94          */
95         function getContentType()
96         {
97                 return $this->contentType;
98         }
99         
100         /**
101          * Get include mode of the SKIN
102          * 
103          * Returns either 'normal' or 'skindir':
104          * 'normal': if a all data of the skin can be found in the databse
105          * 'skindir': if the skin has data in the it's skin driectory
106          */
107         function getIncludeMode()
108         {
109                 return $this->includeMode;
110         }
111         
112         /**
113          * Get include prefix of the SKIN
114          * 
115          * Get name of the subdirectory (with trailing slash) where
116          * the files of the current skin can be found (e.g. 'default/')
117          */
118         function getIncludePrefix()
119         {
120                 return $this->includePrefix;
121         }
122         
123         /**
124          * Checks if a skin with a given shortname exists
125          * @param string $name Skin short name
126          * @return int number of skins with the given ID
127          * @static
128          */
129         function exists($name)
130         {
131                 return quickQuery('select count(*) as result FROM ' . sql_table('skin_desc') . ' WHERE sdname="' . sql_real_escape_string($name) . '"') > 0;
132         }
133         
134         /**
135          * Checks if a skin with a given ID exists
136          * @param string $id Skin ID
137          * @return int number of skins with the given ID
138          * @static
139          */
140         function existsID($id)
141         {
142                 return quickQuery('select COUNT(*) as result FROM ' . sql_table('skin_desc') . ' WHERE sdnumber=' . intval($id)) > 0;
143         }
144         
145         /**
146          * Returns a skin given its shortname
147          * @param string $name Skin shortname
148          * @return object SKIN
149          * @static
150          */
151         function createFromName($name)
152         {
153                 return new SKIN(SKIN::getIdFromName($name));
154         }
155         
156         /**
157          * Returns a skin ID given its shortname
158          * @param string $name Skin shortname
159          * @return int Skin ID
160          * @static
161          */
162         function getIdFromName($name)
163         {
164                 $query =  'SELECT sdnumber'
165                                 . ' FROM ' . sql_table('skin_desc')
166                                 . ' WHERE sdname="' . sql_real_escape_string($name) . '"';
167                 $res = sql_query($query);
168                 $obj = sql_fetch_object($res);
169                 return $obj->sdnumber;
170         }
171         
172         /**
173          * Returns a skin shortname given its ID
174          * @param string $name
175          * @return string Skin short name
176          * @static
177          */
178         function getNameFromId($id)
179         {
180                 return quickQuery('SELECT sdname as result FROM ' . sql_table('skin_desc') . ' WHERE sdnumber=' . intval($id));
181         }
182         
183         /**
184          * Creates a new skin, with the given characteristics.
185          *
186          * @static
187          */
188         function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
189         {
190                 global $manager;
191                 
192                 $manager->notify(
193                         'PreAddSkin',
194                         array(
195                                 'name' => &$name,
196                                 'description' => &$desc,
197                                 'type' => &$type,
198                                 'includeMode' => &$includeMode,
199                                 'includePrefix' => &$includePrefix
200                         )
201                 );
202                 
203                 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) . "')");
204                 $newid = sql_insert_id();
205                 
206                 $manager->notify(
207                         'PostAddSkin',
208                         array(
209                                 'skinid' => $newid,
210                                 'name' => $name,
211                                 'description' => $desc,
212                                 'type' => $type,
213                                 'includeMode' => $includeMode,
214                                 'includePrefix' => $includePrefix
215                         )
216                 );
217                 return $newid;
218         }
219         
220         /**
221          * Parse a SKIN
222          * 
223          * @param string $type
224          */
225         function parse($type)
226         {
227                 global $manager, $CONF;
228                 
229                 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
230                 
231                 // set output type
232                 sendContentType($this->getContentType(), 'skin');
233                 
234                 // set skin name as global var (so plugins can access it)
235                 global $currentSkinName;
236                 $currentSkinName = $this->getName();
237                 
238                 $contents = $this->getContent($type);
239                 
240                 if ( !$contents )
241                 {
242                         // use base skin if this skin does not have contents
243                         $defskin = new SKIN($CONF['BaseSkin']);
244                         $contents = $defskin->getContent($type);
245                         if ( !$contents )
246                         {
247                                 echo _ERROR_SKIN;
248                                 return;
249                         }
250                 }
251                 
252                 $actions = $this->getAllowedActionsForType($type);
253                 
254                 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
255                 
256                 // set IncludeMode properties of parser
257                 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
258                 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
259                 
260                 $handler = new ACTIONS($type, $this);
261                 $parser = new PARSER($actions, $handler);
262                 $handler->setParser($parser);
263                 $handler->setSkin($this);
264                 $parser->parse($contents);
265                 
266                 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
267         }
268         
269         /**
270          * Get content of the skin part from the database
271          * 
272          * @param $type type of the skin (e.g. index, item, search ...)
273          */
274         function getContent($type)
275         {
276                 $query = 'SELECT scontent FROM '. sql_table('skin') . " WHERE sdesc=$this->id and stype='" . sql_real_escape_string($type) . "'";
277                 $res = sql_query($query);
278                 
279                 if ( sql_num_rows($res) == 0 )
280                 {
281                         return '';
282                 }
283                 else
284                 {
285                         return sql_result($res, 0, 0);
286                 }
287         }
288
289         /**
290          * Updates the contents for one part of the skin in the database
291          * 
292          * @param $type type of the skin part (e.g. index, item, search ...) 
293          * @param $content new content for this skin part
294          */
295
296         function update($type, $content)
297         {
298                 global $manager;
299                 
300                 $skinid = $this->id;
301                 
302                 $query = 'SELECT sdesc FROM ' . sql_table('skin') . " WHERE stype='" . sql_real_escape_string($type) . "' and sdesc=" . intval($skinid);
303                 $res = sql_query($query);
304                 $skintypeexists = sql_fetch_object($res);
305                 $skintypevalue = ($content == true);
306                 
307                 if( $skintypevalue && $skintypeexists )
308                 {
309                         // PreUpdateSkinPart event
310                         $manager->notify(
311                                 'PreUpdateSkinPart',
312                                 array(
313                                         'skinid' => $skinid,
314                                         'type' => $type,
315                                         'content' => &$content
316                                 )
317                         );
318                 }
319                 else if( $skintypevalue && (!$skintypeexists) )
320                 {
321                         // PreAddSkinPart event
322                         $manager->notify(
323                                 'PreAddSkinPart',
324                                 array(
325                                         'skinid' => $skinid,
326                                         'type' => $type,
327                                         'content' => &$content
328                                 )
329                         );
330                 }
331                 else if( (!$skintypevalue) && $skintypeexists )
332                 {
333                         // PreDeleteSkinPart event
334                         $manager->notify(
335                                 'PreDeleteSkinPart',
336                                 array(
337                                         'skinid' => $skinid,
338                                         'type' => $type
339                                 )
340                         );
341                 }
342                 
343                 // delete old thingie
344                 sql_query('DELETE FROM ' . sql_table('skin') . " WHERE stype='" . sql_real_escape_string($type) . "' and sdesc=" . intval($skinid));
345                 
346                 // write new thingie
347                 if ( $content )
348                 {
349                         sql_query('INSERT INTO ' . sql_table('skin') . " SET scontent='" . sql_real_escape_string($content) . "', stype='" . sql_real_escape_string($type) . "', sdesc=" . intval($skinid));
350                 }
351                 
352                 if( $skintypevalue && $skintypeexists )
353                 {
354                         // PostUpdateSkinPart event
355                         $manager->notify(
356                         'PostUpdateSkinPart',
357                                 array(
358                                         'skinid' => $skinid,
359                                         'type' => $type,
360                                         'content' => &$content
361                                 )
362                         );
363                 }
364                 else if( $skintypevalue && (!$skintypeexists) )
365                 {
366                         // PostAddSkinPart event
367                         $manager->notify(
368                                 'PostAddSkinPart',
369                                 array(
370                                         'skinid' => $skinid,
371                                         'type' => $type,
372                                         'content' => &$content
373                                 )
374                         );
375                 }
376                 else if( (!$skintypevalue) && $skintypeexists )
377                 {
378                         // PostDeleteSkinPart event
379                         $manager->notify(
380                                 'PostDeleteSkinPart',
381                                 array(
382                                         'skinid' => $skinid,
383                                         'type' => $type
384                                 )
385                         );
386                 }
387         }
388         
389         /**
390          * Deletes all skin parts from the database
391          */
392         function deleteAllParts()
393         {
394                 sql_query('DELETE FROM ' . sql_table('skin') . ' WHERE sdesc=' . $this->getID());
395         }
396
397         /**
398          * Updates the general information about the skin
399          */
400         function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
401         {
402                 $query =  'UPDATE '.sql_table('skin_desc').' SET'
403                            . " sdname='" . sql_real_escape_string($name) . "',"
404                            . " sddesc='" . sql_real_escape_string($desc) . "',"
405                            . " sdtype='" . sql_real_escape_string($type) . "',"
406                            . " sdincmode='" . sql_real_escape_string($includeMode) . "',"
407                            . " sdincpref='" . sql_real_escape_string($includePrefix) . "'"
408                            . " WHERE sdnumber=" . $this->getID();
409                 sql_query($query);
410         }
411         
412         /**
413          * Get an array with the names of possible skin parts
414          * Used to show all possible parts of a skin in the administration backend
415          * 
416          * static: returns an array of friendly names
417          */
418         function getFriendlyNames()
419         {
420                 $skintypes = array(
421                         'index' => _SKIN_PART_MAIN,
422                         'item' => _SKIN_PART_ITEM,
423                         'archivelist' => _SKIN_PART_ALIST,
424                         'archive' => _SKIN_PART_ARCHIVE,
425                         'search' => _SKIN_PART_SEARCH,
426                         'error' => _SKIN_PART_ERROR,
427                         'member' => _SKIN_PART_MEMBER,
428                         'imagepopup' => _SKIN_PART_POPUP
429                 );
430                 
431                 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
432                 $res = sql_query($query);
433                 while ( $row = sql_fetch_array($res) )
434                 {
435                         $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
436                 }
437                 return $skintypes;
438         }
439
440         /**
441          * Get the allowed actions for a skin type
442          * returns an array with the allowed actions
443          * 
444          * @param $type type of the skin (e.g. index, item, search ...)
445          */
446         function getAllowedActionsForType($type)
447         {
448                 global $blogid;
449                 
450                 // some actions that can be performed at any time, from anywhere
451                 $defaultActions = array('otherblog',
452                                                                 'plugin',
453                                                                 'version',
454                                                                 'nucleusbutton',
455                                                                 'include',
456                                                                 'phpinclude',
457                                                                 'parsedinclude',
458                                                                 'loginform',
459                                                                 'sitevar',
460                                                                 'otherarchivelist',
461                                                                 'otherarchivedaylist',
462                                                                 'otherarchiveyearlist',
463                                                                 'self',
464                                                                 'adminurl',
465                                                                 'todaylink',
466                                                                 'archivelink',
467                                                                 'member',
468                                                                 'category',
469                                                                 'searchform',
470                                                                 'referer',
471                                                                 'skinname',
472                                                                 'skinfile',
473                                                                 'set',
474                                                                 'if',
475                                                                 'else',
476                                                                 'endif',
477                                                                 'elseif',
478                                                                 'ifnot',
479                                                                 'elseifnot',
480                                                                 'charset',
481                                                                 'bloglist',
482                                                                 'addlink',
483                                                                 'addpopupcode',
484                                                                 'sticky',
485                                                                 // deprecated (Nucleus v2.0)
486                                                                 'ifcat'
487                                                                 );
488                 
489                 // extra actions specific for a certain skin type
490                 $extraActions = array();
491                 
492                 switch ( $type )
493                 {
494                         case 'index':
495                                 $extraActions = array('blog',
496                                                                 'blogsetting',
497                                                                 'preview',
498                                                                 'additemform',
499                                                                 'categorylist',
500                                                                 'archivelist',
501                                                                 'archivedaylist',
502                                                                 'archiveyearlist',
503                                                                 'nextlink',
504                                                                 'prevlink'
505                                                                 );
506                                 break;
507                         case 'archive':
508                                 $extraActions = array('blog',
509                                                                 'archive',
510                                                                 'otherarchive',
511                                                                 'categorylist',
512                                                                 'archivelist',
513                                                                 'archivedaylist',
514                                                                 'archiveyearlist',
515                                                                 'blogsetting',
516                                                                 'archivedate',
517                                                                 'nextarchive',
518                                                                 'prevarchive',
519                                                                 'nextlink',
520                                                                 'prevlink',
521                                                                 'archivetype'
522                                 );
523                                 break;
524                         case 'archivelist':
525                                 $extraActions = array('blog',
526                                                                 'archivelist',
527                                                                 'archivedaylist',
528                                                                 'archiveyearlist',
529                                                                 'categorylist',
530                                                                 'blogsetting',
531                                                            );
532                                 break;
533                         case 'search':
534                                 $extraActions = array('blog',
535                                                                 'archivelist',
536                                                                 'archivedaylist',
537                                                                 'archiveyearlist',
538                                                                 'categorylist',
539                                                                 'searchresults',
540                                                                 'othersearchresults',
541                                                                 'blogsetting',
542                                                                 'query',
543                                                                 'nextlink',
544                                                                 'prevlink'
545                                                                 );
546                                 break;
547                         case 'imagepopup':
548                                 $extraActions = array('image',
549                                                                 // deprecated (Nucleus v2.0)
550                                                                 'imagetext',
551                                                                 );
552                                 break;
553                         case 'member':
554                                 $extraActions = array(
555                                                                 'membermailform',
556                                                                 'blogsetting',
557                                                                 'nucleusbutton',
558                                                                 'categorylist'
559                                 );
560                                 break;
561                         case 'item':
562                                 $extraActions = array('blog',
563                                                                 'item',
564                                                                 'comments',
565                                                                 'commentform',
566                                                                 'vars',
567                                                                 'blogsetting',
568                                                                 'nextitem',
569                                                                 'previtem',
570                                                                 'nextlink',
571                                                                 'prevlink',
572                                                                 'nextitemtitle',
573                                                                 'previtemtitle',
574                                                                 'categorylist',
575                                                                 'archivelist',
576                                                                 'archivedaylist',
577                                                                 'archiveyearlist',
578                                                                 'itemtitle',
579                                                                 'itemid',
580                                                                 'itemlink',
581                                                                 );
582                                 break;
583                         case 'error':
584                                 $extraActions = array(
585                                                                 'errormessage',
586                                                                 'categorylist'
587                                 );
588                                 break;
589                         default:
590                                 if ( $blogid && $blogid > 0 )
591                                 {
592                                         $extraActions = array(
593                                                 'blog',
594                                                 'blogsetting',
595                                                 'preview',
596                                                 'additemform',
597                                                 'categorylist',
598                                                 'archivelist',
599                                                 'archivedaylist',
600                                                 'archiveyearlist',
601                                                 'nextlink',
602                                                 'prevlink',
603                                                 'membermailform',
604                                                 'nucleusbutton',
605                                                 'categorylist'
606                                         );
607                                 }
608                                 break;
609                 }
610                 return array_merge($defaultActions, $extraActions);
611         }
612 }