OSDN Git Service

CHANGE: SQL92標準に合わせてINSERT文を変更。
[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          * SKIN::createNew()
185          * Creates a new skin, with the given characteristics.
186          *
187          * @static
188          * @param       String  $name   value for nucleus_skin.sdname
189          * @param       String  $desc   value for nucleus_skin.sddesc
190          * @param       String  $type   value for nucleus_skin.sdtype
191          * @param       String  $includeMode    value for nucleus_skin.sdinclude
192          * @param       String  $includePrefix  value for nucleus_skin.sdincpref
193          * @return      Integer ID for just inserted record
194          * 
195          */
196         function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
197         {
198                 global $manager;
199                 
200                 $manager->notify(
201                         'PreAddSkin',
202                         array(
203                                 'name' => &$name,
204                                 'description' => &$desc,
205                                 'type' => &$type,
206                                 'includeMode' => &$includeMode,
207                                 'includePrefix' => &$includePrefix
208                         )
209                 );
210                 
211                 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s')";
212                 $query = sprintf($query, sql_table('skin_desc'), 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));
213                 sql_query($query);
214                 $newid = sql_insert_id();
215                 
216                 $manager->notify(
217                         'PostAddSkin',
218                         array(
219                                 'skinid' => $newid,
220                                 'name' => $name,
221                                 'description' => $desc,
222                                 'type' => $type,
223                                 'includeMode' => $includeMode,
224                                 'includePrefix' => $includePrefix
225                         )
226                 );
227                 return $newid;
228         }
229         
230         /**
231          * Parse a SKIN
232          * 
233          * @param string $type
234          */
235         function parse($type)
236         {
237                 global $manager, $CONF;
238                 
239                 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
240                 
241                 // set output type
242                 sendContentType($this->getContentType(), 'skin');
243                 
244                 // set skin name as global var (so plugins can access it)
245                 global $currentSkinName;
246                 $currentSkinName = $this->getName();
247                 
248                 $contents = $this->getContent($type);
249                 
250                 if ( !$contents )
251                 {
252                         // use base skin if this skin does not have contents
253                         $defskin = new SKIN($CONF['BaseSkin']);
254                         $contents = $defskin->getContent($type);
255                         if ( !$contents )
256                         {
257                                 echo _ERROR_SKIN;
258                                 return;
259                         }
260                 }
261                 
262                 $actions = $this->getAllowedActionsForType($type);
263                 
264                 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
265                 
266                 // set IncludeMode properties of parser
267                 PARSER::setProperty('IncludeMode',$this->getIncludeMode());
268                 PARSER::setProperty('IncludePrefix',$this->getIncludePrefix());
269                 
270                 $handler = new ACTIONS($type, $this);
271                 $parser = new PARSER($actions, $handler);
272                 $handler->setParser($parser);
273                 $handler->setSkin($this);
274                 $parser->parse($contents);
275                 
276                 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
277         }
278         
279         /**
280          * Get content of the skin part from the database
281          * 
282          * @param $type type of the skin (e.g. index, item, search ...)
283          */
284         function getContent($type)
285         {
286                 $query = 'SELECT scontent FROM '. sql_table('skin') . " WHERE sdesc=$this->id and stype='" . sql_real_escape_string($type) . "'";
287                 $res = sql_query($query);
288                 
289                 if ( sql_num_rows($res) == 0 )
290                 {
291                         return '';
292                 }
293                 else
294                 {
295                         return sql_result($res, 0, 0);
296                 }
297         }
298
299         /**
300          * SKIN::update()
301          * Updates the contents for one part of the skin in the database
302          * 
303          * @param $type type of the skin part (e.g. index, item, search ...) 
304          * @param $content new content for this skin part
305          * @return      Void
306          * 
307          */
308         function update($type, $content)
309         {
310                 global $manager;
311                 
312                 $skinid = $this->id;
313                 
314                 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d";
315                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $skinid);
316                 $res = sql_query($query);
317                 
318                 $skintypeexists = sql_fetch_object($res);
319                 $skintypevalue = ($content == true);
320                 
321                 if( $skintypevalue && $skintypeexists )
322                 {
323                         // PreUpdateSkinPart event
324                         $manager->notify(
325                                 'PreUpdateSkinPart',
326                                 array(
327                                         'skinid' => $skinid,
328                                         'type' => $type,
329                                         'content' => &$content
330                                 )
331                         );
332                 }
333                 else if( $skintypevalue && (!$skintypeexists) )
334                 {
335                         // PreAddSkinPart event
336                         $manager->notify(
337                                 'PreAddSkinPart',
338                                 array(
339                                         'skinid' => $skinid,
340                                         'type' => $type,
341                                         'content' => &$content
342                                 )
343                         );
344                 }
345                 else if( (!$skintypevalue) && $skintypeexists )
346                 {
347                         // PreDeleteSkinPart event
348                         $manager->notify(
349                                 'PreDeleteSkinPart',
350                                 array(
351                                         'skinid' => $skinid,
352                                         'type' => $type
353                                 )
354                         );
355                 }
356                 
357                 // delete old thingie
358                 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
359                 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $skinid);
360                 sql_query($query);
361                 
362                 // write new thingie
363                 if ( $content )
364                 {
365                         $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
366                         $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $skinid);
367                         sql_query($query);
368                 }
369                 
370                 if( $skintypevalue && $skintypeexists )
371                 {
372                         // PostUpdateSkinPart event
373                         $manager->notify(
374                         'PostUpdateSkinPart',
375                                 array(
376                                         'skinid' => $skinid,
377                                         'type' => $type,
378                                         'content' => &$content
379                                 )
380                         );
381                 }
382                 else if( $skintypevalue && (!$skintypeexists) )
383                 {
384                         // PostAddSkinPart event
385                         $manager->notify(
386                                 'PostAddSkinPart',
387                                 array(
388                                         'skinid' => $skinid,
389                                         'type' => $type,
390                                         'content' => &$content
391                                 )
392                         );
393                 }
394                 else if( (!$skintypevalue) && $skintypeexists )
395                 {
396                         // PostDeleteSkinPart event
397                         $manager->notify(
398                                 'PostDeleteSkinPart',
399                                 array(
400                                         'skinid' => $skinid,
401                                         'type' => $type
402                                 )
403                         );
404                 }
405                 return;
406         }
407         
408         /**
409          * Deletes all skin parts from the database
410          */
411         function deleteAllParts()
412         {
413                 sql_query('DELETE FROM ' . sql_table('skin') . ' WHERE sdesc=' . $this->getID());
414         }
415
416         /**
417          * Updates the general information about the skin
418          */
419         function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
420         {
421                 $query =  'UPDATE '.sql_table('skin_desc').' SET'
422                            . " sdname='" . sql_real_escape_string($name) . "',"
423                            . " sddesc='" . sql_real_escape_string($desc) . "',"
424                            . " sdtype='" . sql_real_escape_string($type) . "',"
425                            . " sdincmode='" . sql_real_escape_string($includeMode) . "',"
426                            . " sdincpref='" . sql_real_escape_string($includePrefix) . "'"
427                            . " WHERE sdnumber=" . $this->getID();
428                 sql_query($query);
429         }
430         
431         /**
432          * Get an array with the names of possible skin parts
433          * Used to show all possible parts of a skin in the administration backend
434          * 
435          * static: returns an array of friendly names
436          */
437         function getFriendlyNames()
438         {
439                 $skintypes = array(
440                         'index' => _SKIN_PART_MAIN,
441                         'item' => _SKIN_PART_ITEM,
442                         'archivelist' => _SKIN_PART_ALIST,
443                         'archive' => _SKIN_PART_ARCHIVE,
444                         'search' => _SKIN_PART_SEARCH,
445                         'error' => _SKIN_PART_ERROR,
446                         'member' => _SKIN_PART_MEMBER,
447                         'imagepopup' => _SKIN_PART_POPUP
448                 );
449                 
450                 $query = "SELECT stype FROM " . sql_table('skin') . " WHERE stype NOT IN ('index', 'item', 'error', 'search', 'archive', 'archivelist', 'imagepopup', 'member')";
451                 $res = sql_query($query);
452                 while ( $row = sql_fetch_array($res) )
453                 {
454                         $skintypes[strtolower($row['stype'])] = ucfirst($row['stype']);
455                 }
456                 return $skintypes;
457         }
458
459         /**
460          * Get the allowed actions for a skin type
461          * returns an array with the allowed actions
462          * 
463          * @param $type type of the skin (e.g. index, item, search ...)
464          */
465         function getAllowedActionsForType($type)
466         {
467                 global $blogid;
468                 
469                 // some actions that can be performed at any time, from anywhere
470                 $defaultActions = array('otherblog',
471                                                                 'plugin',
472                                                                 'version',
473                                                                 'nucleusbutton',
474                                                                 'include',
475                                                                 'phpinclude',
476                                                                 'parsedinclude',
477                                                                 'loginform',
478                                                                 'sitevar',
479                                                                 'otherarchivelist',
480                                                                 'otherarchivedaylist',
481                                                                 'otherarchiveyearlist',
482                                                                 'self',
483                                                                 'adminurl',
484                                                                 'todaylink',
485                                                                 'archivelink',
486                                                                 'member',
487                                                                 'category',
488                                                                 'searchform',
489                                                                 'referer',
490                                                                 'skinname',
491                                                                 'skinfile',
492                                                                 'set',
493                                                                 'if',
494                                                                 'else',
495                                                                 'endif',
496                                                                 'elseif',
497                                                                 'ifnot',
498                                                                 'elseifnot',
499                                                                 'charset',
500                                                                 'bloglist',
501                                                                 'addlink',
502                                                                 'addpopupcode',
503                                                                 'sticky',
504                                                                 // deprecated (Nucleus v2.0)
505                                                                 'ifcat'
506                                                                 );
507                 
508                 // extra actions specific for a certain skin type
509                 $extraActions = array();
510                 
511                 switch ( $type )
512                 {
513                         case 'index':
514                                 $extraActions = array('blog',
515                                                                 'blogsetting',
516                                                                 'preview',
517                                                                 'additemform',
518                                                                 'categorylist',
519                                                                 'archivelist',
520                                                                 'archivedaylist',
521                                                                 'archiveyearlist',
522                                                                 'nextlink',
523                                                                 'prevlink'
524                                                                 );
525                                 break;
526                         case 'archive':
527                                 $extraActions = array('blog',
528                                                                 'archive',
529                                                                 'otherarchive',
530                                                                 'categorylist',
531                                                                 'archivelist',
532                                                                 'archivedaylist',
533                                                                 'archiveyearlist',
534                                                                 'blogsetting',
535                                                                 'archivedate',
536                                                                 'nextarchive',
537                                                                 'prevarchive',
538                                                                 'nextlink',
539                                                                 'prevlink',
540                                                                 'archivetype'
541                                 );
542                                 break;
543                         case 'archivelist':
544                                 $extraActions = array('blog',
545                                                                 'archivelist',
546                                                                 'archivedaylist',
547                                                                 'archiveyearlist',
548                                                                 'categorylist',
549                                                                 'blogsetting',
550                                                            );
551                                 break;
552                         case 'search':
553                                 $extraActions = array('blog',
554                                                                 'archivelist',
555                                                                 'archivedaylist',
556                                                                 'archiveyearlist',
557                                                                 'categorylist',
558                                                                 'searchresults',
559                                                                 'othersearchresults',
560                                                                 'blogsetting',
561                                                                 'query',
562                                                                 'nextlink',
563                                                                 'prevlink'
564                                                                 );
565                                 break;
566                         case 'imagepopup':
567                                 $extraActions = array('image',
568                                                                 // deprecated (Nucleus v2.0)
569                                                                 'imagetext',
570                                                                 );
571                                 break;
572                         case 'member':
573                                 $extraActions = array(
574                                                                 'membermailform',
575                                                                 'blogsetting',
576                                                                 'nucleusbutton',
577                                                                 'categorylist'
578                                 );
579                                 break;
580                         case 'item':
581                                 $extraActions = array('blog',
582                                                                 'item',
583                                                                 'comments',
584                                                                 'commentform',
585                                                                 'vars',
586                                                                 'blogsetting',
587                                                                 'nextitem',
588                                                                 'previtem',
589                                                                 'nextlink',
590                                                                 'prevlink',
591                                                                 'nextitemtitle',
592                                                                 'previtemtitle',
593                                                                 'categorylist',
594                                                                 'archivelist',
595                                                                 'archivedaylist',
596                                                                 'archiveyearlist',
597                                                                 'itemtitle',
598                                                                 'itemid',
599                                                                 'itemlink',
600                                                                 );
601                                 break;
602                         case 'error':
603                                 $extraActions = array(
604                                                                 'errormessage',
605                                                                 'categorylist'
606                                 );
607                                 break;
608                         default:
609                                 if ( $blogid && $blogid > 0 )
610                                 {
611                                         $extraActions = array(
612                                                 'blog',
613                                                 'blogsetting',
614                                                 'preview',
615                                                 'additemform',
616                                                 'categorylist',
617                                                 'archivelist',
618                                                 'archivedaylist',
619                                                 'archiveyearlist',
620                                                 'nextlink',
621                                                 'prevlink',
622                                                 'membermailform',
623                                                 'nucleusbutton',
624                                                 'categorylist'
625                                         );
626                                 }
627                                 break;
628                 }
629                 return array_merge($defaultActions, $extraActions);
630         }
631 }