OSDN Git Service

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