3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
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)
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
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()
21 * @license http://nucleuscms.org/license.txt GNU General Public License
22 * @copyright Copyright (C) 2002-2009 The Nucleus Group
23 * @version $Id: MANAGER.php 1624 2012-01-09 11:36:20Z sakamocchi $
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.
33 * The $items, $blogs, ... arrays map an id to an object (for plugins, the name is used
44 * cachedInfo to avoid repeated SQL queries (see pidInstalled/pluginInstalled/getPidFromName)
45 * e.g. which plugins exists?
47 * $cachedInfo['installedPlugins'] = array($pid -> $name)
52 * The plugin subscriptionlist
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
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
68 static $instance = array();
69 if ( empty($instance) )
71 $instance[0] = new MANAGER();
77 * The constructor of this class initializes the object caches
81 $this->items = array();
82 $this->blogs = array();
83 $this->plugins = array();
84 $this->karma = array();
85 $this->parserPrefs = array();
86 $this->cachedInfo = array();
91 * Returns the requested item object. If it is not in the cache, it will
92 * first be loaded and then placed in the cache.
93 * Intended use: $item =& $manager->getItem(1234)
95 function &getItem($itemid, $allowdraft, $allowfuture)
97 $item =& $this->items[$itemid];
99 // check the draft and future rules if the item was already cached
102 if ((!$allowdraft) && ($item['draft']))
105 $blog =& $this->getBlog(getBlogIDFromItemID($itemid));
106 if ( (!$allowfuture) && ($item['timestamp'] > $blog->getCorrectTime()) )
113 // load class if needed
114 $this->loadClass('ITEM');
116 $item = ITEM::getitem($itemid, $allowdraft, $allowfuture);
117 $this->items[$itemid] = $item;
123 * Loads a class if it has not yet been loaded
125 function loadClass($name)
127 $this->_loadClass($name, $name . '.php');
132 * Checks if an item exists
134 function existsItem($id,$future,$draft)
136 $this->_loadClass('ITEM','ITEM.php');
137 return ITEM::exists($id,$future,$draft);
141 * Checks if a category exists
143 function existsCategory($id)
145 return (quickQuery('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
149 * Returns the blog object for a given blogid
151 function &getBlog($blogid)
153 $blog =& $this->blogs[$blogid];
157 // load class if needed
158 $this->_loadClass('BLOG','BLOG.php');
160 $blog = new BLOG($blogid);
161 $this->blogs[$blogid] =& $blog;
167 * Checks if a blog exists
169 function existsBlog($name)
171 $this->_loadClass('BLOG','BLOG.php');
172 return BLOG::exists($name);
176 * Checks if a blog id exists
178 function existsBlogID($id)
180 $this->_loadClass('BLOG','BLOG.php');
181 return BLOG::existsID($id);
185 * Returns a previously read template
187 function &getTemplate($templateName)
189 $template =& $this->templates[$templateName];
193 $template = TEMPLATE::read($templateName);
194 $this->templates[$templateName] =& $template;
200 * Returns a KARMA object (karma votes)
202 function &getKarma($itemid)
204 $karma =& $this->karma[$itemid];
207 // load class if needed
208 $this->_loadClass('KARMA','KARMA.php');
209 // create KARMA object
210 $karma = new KARMA($itemid);
211 $this->karma[$itemid] =& $karma;
217 * Returns a MEMBER object
219 function &getMember($memberid)
221 $mem =& $this->members[$memberid];
225 // load class if needed
226 $this->_loadClass('MEMBER','MEMBER.php');
227 // create MEMBER object
228 $mem =& MEMBER::createFromID($memberid);
229 $this->members[$memberid] =& $mem;
235 * Set the global parser preferences
237 function setParserProperty($name, $value)
239 $this->parserPrefs[$name] = $value;
244 * Get the global parser preferences
246 function getParserProperty($name)
248 return $this->parserPrefs[$name];
252 * A helper function to load a class
256 function _loadClass($name, $filename)
258 if ( !class_exists($name) )
261 include($DIR_LIBS . $filename);
267 * MANAGER::_loadPlugin()
268 * loading a certain plugin
270 * @param string $name plugin name
273 function _loadPlugin($name)
275 if ( !class_exists($name) )
279 $fileName = $DIR_PLUGINS . $name . '.php';
281 if ( !file_exists($fileName) )
283 if ( !defined('_MANAGER_PLUGINFILE_NOTFOUND') )
285 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
287 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
294 // check if class exists (avoid errors in eval'd code)
295 if ( !class_exists($name) )
297 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
301 // add to plugin array
302 eval('$this->plugins[$name] = new ' . $name . '();');
305 $this->plugins[$name]->setID($this->getPidFromName($name));
307 // unload plugin if a prefix is used and the plugin cannot handle this^
308 global $MYSQL_PREFIX;
309 if ( ($MYSQL_PREFIX != '')
310 && !$this->plugins[$name]->supportsFeature('SqlTablePrefix') )
312 unset($this->plugins[$name]);
313 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
317 // unload plugin if using non-mysql handler and plugin does not support it
318 global $MYSQL_HANDLER;
319 if ( (!in_array('mysql',$MYSQL_HANDLER))
320 && !$this->plugins[$name]->supportsFeature('SqlApi') )
322 unset($this->plugins[$name]);
323 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
328 $this->plugins[$name]->init();
334 * Returns a PLUGIN object
336 function &getPlugin($name)
338 // retrieve the name of the plugin in the right capitalisation
339 $name = $this->getUpperCaseName ($name);
341 $plugin =& $this->plugins[$name];
345 // load class if needed
346 $this->_loadPlugin($name);
347 $plugin =& $this->plugins[$name];
353 * Checks if the given plugin IS loaded or not
355 function &pluginLoaded($name)
357 $plugin =& $this->plugins[$name];
361 function &pidLoaded($pid)
364 reset($this->plugins);
365 while ( list($name) = each($this->plugins) )
367 if ( $pid!=$this->plugins[$name]->getId() )
371 $plugin= & $this->plugins[$name];
378 * checks if the given plugin IS installed or not
380 function pluginInstalled($name)
382 $this->_initCacheInfo('installedPlugins');
383 return ($this->getPidFromName($name) != -1);
386 function pidInstalled($pid)
388 $this->_initCacheInfo('installedPlugins');
389 return ($this->cachedInfo['installedPlugins'][$pid] != '');
392 function getPidFromName($name)
394 $this->_initCacheInfo('installedPlugins');
395 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
397 if (strtolower($pfile) == strtolower($name))
406 * Retrieve the name of a plugin in the right capitalisation
408 function getUpperCaseName ($name)
410 $this->_initCacheInfo('installedPlugins');
411 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
413 if ( strtolower($pfile) == strtolower($name) )
421 function clearCachedInfo($what)
423 unset($this->cachedInfo[$what]);
428 * Loads some info on the first call only
430 function _initCacheInfo($what)
432 if ( isset($this->cachedInfo[$what]) && is_array($this->cachedInfo[$what]) )
438 // 'installedPlugins' = array ($pid => $name)
439 case 'installedPlugins':
440 $this->cachedInfo['installedPlugins'] = array();
441 $res = sql_query('SELECT pid, pfile FROM ' . sql_table('plugin'));
442 while ( $o = sql_fetch_object($res) )
444 $this->cachedInfo['installedPlugins'][$o->pid] = $o->pfile;
452 * A function to notify plugins that something has happened. Only the plugins
453 * that are subscribed to the event will get notified.
454 * Upon the first call, the list of subscriptions will be fetched from the
455 * database. The plugins itsself will only get loaded when they are first needed
458 * Name of the event (method to be called on plugins)
460 * Can contain any type of data, depending on the event type. Usually this is
461 * an itemid, blogid, ... but it can also be an array containing multiple values
463 function notify($eventName, $data)
465 // load subscription list if needed
466 if ( !is_array($this->subscriptions) )
468 $this->_loadSubscriptions();
471 // get listening objects
473 if ( isset($this->subscriptions[$eventName]) )
475 $listeners = $this->subscriptions[$eventName];
478 // notify all of them
479 if ( is_array($listeners) )
481 foreach( $listeners as $listener )
483 // load class if needed
484 $this->_loadPlugin($listener);
485 // do notify (if method exists)
486 if ( isset($this->plugins[$listener])
487 && method_exists($this->plugins[$listener], 'event_' . $eventName))
489 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);
497 * Loads plugin subscriptions
499 function _loadSubscriptions()
501 // initialize as array
502 $this->subscriptions = array();
504 $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');
505 while ( $o = sql_fetch_object($res) )
507 $pluginName = $o->pfile;
508 $eventName = $o->event;
509 $this->subscriptions[$eventName][] = $pluginName;
515 Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
516 requests. tickets are user specific
519 var $currentRequestTicket = '';
522 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
524 function addTicketToUrl($url)
526 $ticketCode = 'ticket=' . $this->_generateTicket();
527 if ( strstr($url, '?') )
529 $ticketCode = "{$url}&{$ticketCode}";
533 $ticketCode = "{$url}?{$ticketCode}";
539 * POST requests: Adds ticket as hidden formvar
541 function addTicketHidden()
543 $ticket = $this->_generateTicket();
544 echo '<input type="hidden" name="ticket" value="', i18n::hsc($ticket), '" />';
550 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
552 function getNewTicket()
554 $this->currentRequestTicket = '';
555 return $this->_generateTicket();
559 * Checks the ticket that was passed along with the current request
561 function checkTicket()
565 // get ticket from request
566 $ticket = requestVar('ticket');
568 // no ticket -> don't allow
574 // remove expired tickets first
575 $this->_cleanUpExpiredTickets();
578 if (!$member->isLoggedIn())
584 $memberId = $member->getID();
587 // check if ticket is a valid one
588 $query = 'SELECT COUNT(*) as result FROM ' . sql_table('tickets') . ' WHERE member=' . intval($memberId). ' and ticket=\''.sql_real_escape_string($ticket).'\'';
592 * [in the original implementation, the checked ticket was deleted. This would lead to invalid
593 * tickets when using the browsers back button and clicking another link/form
594 * leaving the keys in the database is not a real problem, since they're member-specific and
595 * only valid for a period of one hour]
597 if ( quickQuery($query) == 1 )
601 // not a valid ticket
609 * (internal method) Removes the expired tickets
611 function _cleanUpExpiredTickets()
613 // remove tickets older than 1 hour
614 $oldTime = time() - 60 * 60;
615 $query = 'DELETE FROM ' . sql_table('tickets'). ' WHERE ctime < \'' . date('Y-m-d H:i:s',$oldTime) .'\'';
621 * (internal method) Generates/returns a ticket (one ticket per page request)
623 function _generateTicket()
625 if ( $this->currentRequestTicket == '' )
627 // generate new ticket (only one ticket will be generated per page request)
628 // and store in database
631 if ( !$member->isLoggedIn() )
637 $memberId = $member->getID();
643 // generate a random token
644 srand((double)microtime()*1000000);
645 $ticket = md5(uniqid(rand(), true));
647 // add in database as non-active
648 $query = 'INSERT INTO ' . sql_table('tickets') . ' (ticket, member, ctime) ';
649 $query .= 'VALUES (\'' . sql_real_escape_string($ticket). '\', \'' . intval($memberId). '\', \'' . date('Y-m-d H:i:s',time()) . '\')';
650 if ( sql_query($query) )
655 $this->currentRequestTicket = $ticket;
657 return $this->currentRequestTicket;