OSDN Git Service

applied 3.2 modification
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / MANAGER.php
1 <?php\r
2 /**\r
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
4   * Copyright (C) 2002-2005 The Nucleus Group\r
5   *\r
6   * This program is free software; you can redistribute it and/or\r
7   * modify it under the terms of the GNU General Public License\r
8   * as published by the Free Software Foundation; either version 2\r
9   * of the License, or (at your option) any later version.\r
10   * (see nucleus/documentation/index.html#license for more info)\r
11   *\r
12   *     This class makes sure each item/weblog/comment object gets requested from\r
13   * the database only once, by keeping them in a cache. The class also acts as \r
14   * a dynamic classloader, loading classes _only_ when they are first needed,\r
15   * hoping to diminish execution time\r
16   *\r
17   * The class is a singleton, meaning that there will be only one object of it\r
18   * active at all times. The object can be requested using MANAGER::instance()\r
19   *\r
20   * $Id: MANAGER.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $\r
21   * $NucleusJP$\r
22   */\r
23 class MANAGER {\r
24 \r
25         /**\r
26          * Cached ITEM, BLOG, PLUGIN and KARMA objects. When these objects are requested \r
27          * through the global $manager object (getItem, getBlog, ...), only the first call \r
28          * will create an object. Subsequent calls will return the same object.\r
29          *\r
30          * The $items, $blogs, ... arrays map an id to an object (for plugins, the name is used\r
31          * rather than an ID)\r
32          */\r
33         var $items;\r
34         var $blogs;\r
35         var $plugins;\r
36         var $karma;\r
37         var $templates;\r
38         \r
39         /**\r
40          * cachedInfo to avoid repeated SQL queries (see pidInstalled/pluginInstalled/getPidFromName)\r
41          * e.g. which plugins exists?\r
42          *\r
43          * $cachedInfo['installedPlugins'] = array($pid -> $name)\r
44          */\r
45         var $cachedInfo;\r
46         \r
47         /**\r
48           * The plugin subscriptionlist\r
49           *\r
50           * The subcription array has the following structure\r
51           *             $subscriptions[$EventName] = array containing names of plugin classes to be\r
52           *                                                                      notified when that event happens\r
53           */\r
54         var $subscriptions;     \r
55         \r
56         /**\r
57           * Returns the only instance of this class. Creates the instance if it \r
58           * does not yet exists. Users should use this function as \r
59           * $manager =& MANAGER::instance(); to get a reference to the object\r
60           * instead of a copy\r
61           */\r
62         function &instance() {\r
63                 static $instance = '';\r
64                 if ($instance == '')\r
65                         $instance =& new MANAGER();\r
66                 return $instance;\r
67         }\r
68         \r
69         /**\r
70           * The constructor of this class initializes the object caches \r
71           */\r
72         function MANAGER() {\r
73                 $this->items = array();\r
74                 $this->blogs = array();\r
75                 $this->plugins = array();\r
76                 $this->karma = array();\r
77                 $this->parserPrefs = array();\r
78                 $this->cachedInfo = array();\r
79         }\r
80         \r
81         /**\r
82           * Returns the requested item object. If it is not in the cache, it will\r
83           * first be loaded and then placed in the cache.\r
84           * Intended use: $item =& $manager->getItem(1234)\r
85           */\r
86         function &getItem($itemid, $allowdraft, $allowfuture) {\r
87                 $item =& $this->items[$itemid];\r
88                 \r
89                 // check the draft and future rules if the item was already cached \r
90                 if ($item) {\r
91                         if ((!$allowdraft) && ($item['draft']))\r
92                                 return 0;\r
93 \r
94                         $blog =& $this->getBlog(getBlogIDFromItemID($itemid));\r
95                         if ((!$allowfuture) && ($item['timestamp'] > $blog->getCorrectTime()))\r
96                                 return 0;                               \r
97                 }\r
98                 if (!$item) {\r
99                         // load class if needed\r
100                         $this->loadClass('ITEM');\r
101                         // load item object\r
102                         $item = ITEM::getitem($itemid, $allowdraft, $allowfuture);\r
103                         $this->items[$itemid] = $item;\r
104                 }\r
105                 return $item;\r
106         }\r
107         \r
108         /**\r
109           * Loads a class if it has not yet been loaded\r
110           */\r
111         function loadClass($name) {\r
112                 $this->_loadClass($name, $name . '.php');\r
113         }\r
114         \r
115         /**\r
116           * Checks if an item exists\r
117           */\r
118         function existsItem($id,$future,$draft) {\r
119                 $this->_loadClass('ITEM','ITEM.php');   \r
120                 return ITEM::exists($id,$future,$draft);\r
121         }\r
122         \r
123         /**\r
124           * Checks if a category exists\r
125           */\r
126         function existsCategory($id) {\r
127                 return (quickQuery('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);\r
128         }\r
129         \r
130         function &getBlog($blogid) {\r
131                 $blog =& $this->blogs[$blogid];\r
132 \r
133                 if (!$blog) {\r
134                         // load class if needed\r
135                         $this->_loadClass('BLOG','BLOG.php');\r
136                         // load blog object\r
137                         $blog =& new BLOG($blogid);\r
138                         $this->blogs[$blogid] =& $blog;\r
139                 }\r
140                 return $blog;\r
141         }\r
142         \r
143         function existsBlog($name) {\r
144                 $this->_loadClass('BLOG','BLOG.php');\r
145                 return BLOG::exists($name);\r
146         }\r
147 \r
148         function existsBlogID($id) {\r
149                 $this->_loadClass('BLOG','BLOG.php');\r
150                 return BLOG::existsID($id);\r
151         }       \r
152         \r
153         /**\r
154          * Returns a previously read template\r
155          */\r
156         function &getTemplate($templateName) {\r
157                 $template =& $this->templates[$templateName];\r
158 \r
159                 if (!$template) {\r
160                         $template = TEMPLATE::read($templateName);\r
161                         $this->templates[$templateName] =& $template;\r
162                 }\r
163                 return $template;\r
164         }       \r
165 \r
166         /**\r
167          * Returns a KARMA object (karma votes)\r
168          */\r
169         function &getKarma($itemid) {\r
170                 $karma =& $this->karma[$itemid];\r
171 \r
172                 if (!$karma) {\r
173                         // load class if needed\r
174                         $this->_loadClass('KARMA','KARMA.php');\r
175                         // create KARMA object\r
176                         $karma =& new KARMA($itemid);\r
177                         $this->karma[$itemid] =& $karma;\r
178                 }\r
179                 return $karma;\r
180         }       \r
181         \r
182         /**\r
183          * Global parser preferences\r
184          */\r
185         function setParserProperty($name, $value) {\r
186                 $this->parserPrefs[$name] = $value;\r
187         }\r
188         function getParserProperty($name) {\r
189                 return $this->parserPrefs[$name];\r
190         }\r
191 \r
192         /**\r
193           * A private helper class to load classes\r
194           */\r
195         function _loadClass($name, $filename) {\r
196                 if (!class_exists($name)) {\r
197                                 global $DIR_LIBS;\r
198                                 include($DIR_LIBS . $filename);\r
199                 }       \r
200         }\r
201         \r
202         function _loadPlugin($name) {\r
203                 if (!class_exists($name)) {\r
204                                 global $DIR_PLUGINS;\r
205                                 \r
206                                 $fileName = $DIR_PLUGINS . $name . '.php';\r
207                                 \r
208                                 if (!file_exists($fileName))\r
209                                 {\r
210                                         ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (File not found)');\r
211                                         return 0;\r
212                                 }\r
213                                 \r
214                                 // load plugin\r
215                                 include($fileName);\r
216                                 \r
217                                 // check if class exists (avoid errors in eval'd code)\r
218                                 if (!class_exists($name))\r
219                                 {\r
220                                         ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (Class not found in file, possible parse error)');                         \r
221                                         return 0;\r
222                                 }\r
223                                 \r
224                                 // add to plugin array\r
225                                 eval('$this->plugins[$name] =& new ' . $name . '();');\r
226                                 \r
227                                 // get plugid\r
228                                 $this->plugins[$name]->plugid = $this->getPidFromName($name);\r
229                                 \r
230                                 // unload plugin if a prefix is used and the plugin cannot handle this^\r
231                                 global $MYSQL_PREFIX;\r
232                                 if (($MYSQL_PREFIX != '') && !$this->plugins[$name]->supportsFeature('SqlTablePrefix')) \r
233                                 {\r
234                                         unset($this->plugins[$name]);\r
235                                         ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (does not support SqlTablePrefix)');\r
236                                         return 0;\r
237                                 }\r
238                                 \r
239                                 // call init method\r
240                                 $this->plugins[$name]->init();\r
241                                 \r
242                 }       \r
243         }\r
244         \r
245         function &getPlugin($name) {\r
246                 $plugin =& $this->plugins[$name];\r
247 \r
248                 if (!$plugin) {\r
249                         // load class if needed\r
250                         $this->_loadPlugin($name);\r
251                         $plugin =& $this->plugins[$name];                       \r
252                 }\r
253                 return $plugin;\r
254         }\r
255 \r
256         /**\r
257           * checks if the given plugin IS installed or not\r
258           */\r
259         function pluginInstalled($name) {\r
260                 $this->_initCacheInfo('installedPlugins');\r
261                 return ($this->getPidFromName($name) != -1);\r
262         }\r
263         function pidInstalled($pid) {\r
264                 $this->_initCacheInfo('installedPlugins');\r
265                 return ($this->cachedInfo['installedPlugins'][$pid] != '');\r
266         }\r
267         function getPidFromName($name) {\r
268                 $this->_initCacheInfo('installedPlugins');\r
269                 foreach ($this->cachedInfo['installedPlugins'] as $pid => $pfile)\r
270                 {\r
271                         if ($pfile == $name)\r
272                                 return $pid;\r
273                 }\r
274                 return -1;\r
275         }\r
276         function clearCachedInfo($what) {\r
277                 unset($this->cachedInfo[$what]);\r
278         }\r
279         \r
280         /**\r
281          * Loads some info on the first call only\r
282          */\r
283         function _initCacheInfo($what)\r
284         {\r
285                 if (is_array($this->cachedInfo[$what]))\r
286                         return;\r
287                 switch ($what)\r
288                 {\r
289                         // 'installedPlugins' = array ($pid => $name)\r
290                         case 'installedPlugins':\r
291                                 $this->cachedInfo['installedPlugins'] = array();\r
292                                 $res = sql_query('SELECT pid, pfile FROM ' . sql_table('plugin'));\r
293                                 while ($o = mysql_fetch_object($res))\r
294                                 {\r
295                                         $this->cachedInfo['installedPlugins'][$o->pid] = $o->pfile;\r
296                                 }\r
297                                 break;\r
298                 }\r
299         }\r
300         \r
301         /**\r
302           * A function to notify plugins that something has happened. Only the plugins\r
303           * that are subscribed to the event will get notified.\r
304           * Upon the first call, the list of subscriptions will be fetched from the \r
305           * database. The plugins itsself will only get loaded when they are first needed\r
306           *\r
307           * @param $eventName\r
308           *             Name of the event (method to be called on plugins)\r
309           * @param $data\r
310           *             Can contain any type of data, depending on the event type. Usually this is\r
311           *             an itemid, blogid, ... but it can also be an array containing multiple values\r
312           */\r
313         function notify($eventName, $data) {\r
314                 // load subscription list if needed\r
315                 if (!is_array($this->subscriptions)) \r
316                         $this->_loadSubscriptions();\r
317                         \r
318 \r
319                 // get listening objects\r
320                 $listeners = $this->subscriptions[$eventName];\r
321                 \r
322                 // notify all of them\r
323                 if (is_array($listeners)) {\r
324                         foreach($listeners as $listener) {\r
325                                 // load class if needed\r
326                                 $this->_loadPlugin($listener);\r
327                                 // do notify (if method exists)\r
328                                 if (method_exists($this->plugins[$listener], 'event_' . $eventName))\r
329                                         call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);\r
330                         }\r
331                 }\r
332                 \r
333         }\r
334         \r
335         /**\r
336           * Loads plugin subscriptions\r
337           */\r
338         function _loadSubscriptions() {\r
339                 // initialize as array\r
340                 $this->subscriptions = array();\r
341 \r
342                 $res = sql_query('SELECT p.pfile as pfile, e.event as event FROM '.sql_table('plugin_event').' as e, '.sql_table('plugin').' as p WHERE e.pid=p.pid ORDER BY p.porder ASC');\r
343                 while ($o = mysql_fetch_object($res)) {\r
344                         $pluginName = $o->pfile;\r
345                         $eventName = $o->event;\r
346                         $this->subscriptions[$eventName][] = $pluginName;\r
347                 }\r
348                 \r
349         }\r
350 \r
351         /*\r
352                 Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST\r
353                 requests. tickets are user specific\r
354         */\r
355 \r
356         var $currentRequestTicket = '';\r
357         \r
358         /**\r
359          * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)\r
360          */\r
361         function addTicketToUrl($url)\r
362         {\r
363                 $ticketCode = 'ticket=' . $this->_generateTicket();\r
364                 if (strstr($url, '?'))\r
365                         return $url . '&' . $ticketCode;\r
366                 else \r
367                         return $url . '?' . $ticketCode;\r
368         }\r
369         \r
370         /**\r
371          * POST requests: Adds ticket as hidden formvar\r
372          */\r
373         function addTicketHidden()\r
374         {\r
375                 $ticket = $this->_generateTicket();\r
376                 \r
377                 echo '<input type="hidden" name="ticket" value="', htmlspecialchars($ticket), '" />';\r
378         }\r
379         \r
380         /**\r
381          * Checks the ticket that was passed along with the current request\r
382          */\r
383         function checkTicket() \r
384         {\r
385                 global $member;\r
386                 \r
387                 // get ticket from request\r
388                 $ticket = requestVar('ticket');\r
389                 \r
390                 // no ticket -> don't allow\r
391                 if ($ticket == '')\r
392                         return false;\r
393                         \r
394                 // remove expired tickets first\r
395                 $this->_cleanUpExpiredTickets();\r
396                 \r
397                 // get member id\r
398                 if (!$member->isLoggedIn())\r
399                         $memberId = -1;\r
400                 else\r
401                         $memberId = $member->getID();\r
402                 \r
403                 // check if ticket is a valid one\r
404                 $query = 'SELECT COUNT(*) as result FROM ' . sql_table('tickets') . ' WHERE member=' . intval($memberId). ' and ticket=\''.addslashes($ticket).'\'';\r
405                 if (quickQuery($query) == 1)\r
406                 {\r
407                         // [in the original implementation, the checked ticket was deleted. This would lead to invalid\r
408                         //  tickets when using the browsers back button and clicking another link/form\r
409                         //  leaving the keys in the database is not a real problem, since they're member-specific and \r
410                         //  only valid for a period of one hour\r
411                         // ]\r
412                         // sql_query('DELETE FROM '.sql_table('tickets').' WHERE member=' . intval($memberId). ' and ticket=\''.addslashes($ticket).'\'');\r
413                         return true;                    \r
414                 } else {\r
415                         // not a valid ticket\r
416                         return false;\r
417                 }\r
418 \r
419         }\r
420         \r
421         /**\r
422          * (internal method) Removes the expired tickets \r
423          */\r
424         function _cleanUpExpiredTickets()\r
425         {\r
426                 // remove tickets older than 1 hour\r
427                 $oldTime = time() - 60 * 60;\r
428                 $query = 'DELETE FROM ' . sql_table('tickets'). ' WHERE ctime < \'' . date('Y-m-d H:i:s',$oldTime) .'\'';\r
429                 sql_query($query);\r
430         }\r
431 \r
432         /**\r
433          * (internal method) Generates/returns a ticket (one ticket per page request)\r
434          */\r
435         function _generateTicket()\r
436         {\r
437                 if ($this->currentRequestTicket == '')\r
438                 {\r
439                         // generate new ticket (only one ticket will be generated per page request)\r
440                         // and store in database \r
441                         global $member;\r
442                         // get member id\r
443                         if (!$member->isLoggedIn())\r
444                                 $memberId = -1;\r
445                         else\r
446                                 $memberId = $member->getID();\r
447                         \r
448                         $ok = false;\r
449                         while (!$ok)\r
450                         {\r
451                                 // generate a random token\r
452                                 srand((double)microtime()*1000000);\r
453                                 $ticket = md5(uniqid(rand(), true));\r
454 \r
455                                 // add in database as non-active\r
456                                 $query = 'INSERT INTO ' . sql_table('tickets') . ' (ticket, member, ctime) ';\r
457                                 $query .= 'VALUES (\'' . addslashes($ticket). '\', \'' . intval($memberId). '\', \'' . date('Y-m-d H:i:s',time()) . '\')';\r
458                                 if (sql_query($query))\r
459                                         $ok = true;\r
460                         }\r
461                         \r
462                         $this->currentRequestTicket = $ticket;\r
463                 }\r
464                 return $this->currentRequestTicket;\r
465         }\r
466         \r
467 }\r
468 \r
469 ?>\r