3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2011 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-2011 The Nucleus Group
24 * $NucleusJP: MANAGER.php,v 1.8.2.1 2007/09/05 07:00:18 kimitake Exp $
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
66 function &instance() {
67 static $instance = array();
68 if (empty($instance)) {
69 $instance[0] =& new MANAGER();
75 * The constructor of this class initializes the object caches
78 $this->items = array();
79 $this->blogs = array();
80 $this->plugins = array();
81 $this->karma = array();
82 $this->parserPrefs = array();
83 $this->cachedInfo = array();
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)
91 function &getItem($itemid, $allowdraft, $allowfuture) {
92 $item =& $this->items[$itemid];
94 // check the draft and future rules if the item was already cached
96 if ((!$allowdraft) && ($item['draft']))
99 $blog =& $this->getBlog(getBlogIDFromItemID($itemid));
100 if ((!$allowfuture) && ($item['timestamp'] > $blog->getCorrectTime()))
104 // load class if needed
105 $this->loadClass('ITEM');
107 $item = ITEM::getitem($itemid, $allowdraft, $allowfuture);
108 $this->items[$itemid] = $item;
114 * Loads a class if it has not yet been loaded
116 function loadClass($name) {
117 $this->_loadClass($name, $name . '.php');
121 * Checks if an item exists
123 function existsItem($id,$future,$draft) {
124 $this->_loadClass('ITEM','ITEM.php');
125 return ITEM::exists($id,$future,$draft);
129 * Checks if a category exists
131 function existsCategory($id) {
132 return (quickQuery('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
136 * Returns the blog object for a given blogid
138 function &getBlog($blogid) {
139 $blog =& $this->blogs[$blogid];
142 // load class if needed
143 $this->_loadClass('BLOG','BLOG.php');
145 $blog =& new BLOG($blogid);
146 $this->blogs[$blogid] =& $blog;
152 * Checks if a blog exists
154 function existsBlog($name) {
155 $this->_loadClass('BLOG','BLOG.php');
156 return BLOG::exists($name);
160 * Checks if a blog id exists
162 function existsBlogID($id) {
163 $this->_loadClass('BLOG','BLOG.php');
164 return BLOG::existsID($id);
168 * Returns a previously read template
170 function &getTemplate($templateName) {
171 $template =& $this->templates[$templateName];
174 $template = TEMPLATE::read($templateName);
175 $this->templates[$templateName] =& $template;
181 * Returns a KARMA object (karma votes)
183 function &getKarma($itemid) {
184 $karma =& $this->karma[$itemid];
187 // load class if needed
188 $this->_loadClass('KARMA','KARMA.php');
189 // create KARMA object
190 $karma =& new KARMA($itemid);
191 $this->karma[$itemid] =& $karma;
197 * Returns a MEMBER object
199 function &getMember($memberid) {
200 $mem =& $this->members[$memberid];
203 // load class if needed
204 $this->_loadClass('MEMBER','MEMBER.php');
205 // create MEMBER object
206 $mem =& MEMBER::createFromID($memberid);
207 $this->members[$memberid] =& $mem;
213 * Set the global parser preferences
215 function setParserProperty($name, $value) {
216 $this->parserPrefs[$name] = $value;
220 * Get the global parser preferences
222 function getParserProperty($name) {
223 return $this->parserPrefs[$name];
227 * A helper function to load a class
231 function _loadClass($name, $filename) {
232 if (!class_exists($name)) {
234 include($DIR_LIBS . $filename);
239 * A helper function to load a plugin
243 function _loadPlugin($name) {
244 if (!class_exists($name)) {
247 $fileName = $DIR_PLUGINS . $name . '.php';
249 if (!file_exists($fileName))
251 if (!defined('_MANAGER_PLUGINFILE_NOTFOUND')) {
252 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
254 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
261 // check if class exists (avoid errors in eval'd code)
262 if (!class_exists($name))
264 if (!defined('_MANAGER_PLUGINFILE_NOCLASS')) {
265 define('_MANAGER_PLUGINFILE_NOCLASS', "Plugin %s was not loaded (Class not found in file, possible parse error)");
267 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
271 // add to plugin array
272 eval('$this->plugins[$name] =& new ' . $name . '();');
275 $this->plugins[$name]->plugid = $this->getPidFromName($name);
277 // unload plugin if a prefix is used and the plugin cannot handle this^
278 global $MYSQL_PREFIX;
279 if (($MYSQL_PREFIX != '') && !$this->plugins[$name]->supportsFeature('SqlTablePrefix'))
281 unset($this->plugins[$name]);
282 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
286 // unload plugin if using non-mysql handler and plugin does not support it
287 global $MYSQL_HANDLER;
288 if ((!in_array('mysql',$MYSQL_HANDLER)) && !$this->plugins[$name]->supportsFeature('SqlApi'))
290 unset($this->plugins[$name]);
291 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
296 $this->plugins[$name]->init();
302 * Returns a PLUGIN object
304 function &getPlugin($name) {
305 // retrieve the name of the plugin in the right capitalisation
306 $name = $this->getUpperCaseName ($name);
308 $plugin =& $this->plugins[$name];
311 // load class if needed
312 $this->_loadPlugin($name);
313 $plugin =& $this->plugins[$name];
319 * Checks if the given plugin IS loaded or not
321 function &pluginLoaded($name) {
322 $plugin =& $this->plugins[$name];
326 function &pidLoaded($pid) {
328 reset($this->plugins);
329 while (list($name) = each($this->plugins)) {
330 if ($pid!=$this->plugins[$name]->getId()) continue;
331 $plugin= & $this->plugins[$name];
338 * checks if the given plugin IS installed or not
340 function pluginInstalled($name) {
341 $this->_initCacheInfo('installedPlugins');
342 return ($this->getPidFromName($name) != -1);
345 function pidInstalled($pid) {
346 $this->_initCacheInfo('installedPlugins');
347 return ($this->cachedInfo['installedPlugins'][$pid] != '');
350 function getPidFromName($name) {
351 $this->_initCacheInfo('installedPlugins');
352 foreach ($this->cachedInfo['installedPlugins'] as $pid => $pfile)
354 if (strtolower($pfile) == strtolower($name))
361 * Retrieve the name of a plugin in the right capitalisation
363 function getUpperCaseName ($name) {
364 $this->_initCacheInfo('installedPlugins');
365 foreach ($this->cachedInfo['installedPlugins'] as $pid => $pfile)
367 if (strtolower($pfile) == strtolower($name))
373 function clearCachedInfo($what) {
374 unset($this->cachedInfo[$what]);
378 * Loads some info on the first call only
380 function _initCacheInfo($what)
382 if (isset($this->cachedInfo[$what]) && is_array($this->cachedInfo[$what]))
386 // 'installedPlugins' = array ($pid => $name)
387 case 'installedPlugins':
388 $this->cachedInfo['installedPlugins'] = array();
389 $res = sql_query('SELECT pid, pfile FROM ' . sql_table('plugin'));
390 while ($o = sql_fetch_object($res))
392 $this->cachedInfo['installedPlugins'][$o->pid] = $o->pfile;
399 * A function to notify plugins that something has happened. Only the plugins
400 * that are subscribed to the event will get notified.
401 * Upon the first call, the list of subscriptions will be fetched from the
402 * database. The plugins itsself will only get loaded when they are first needed
405 * Name of the event (method to be called on plugins)
407 * Can contain any type of data, depending on the event type. Usually this is
408 * an itemid, blogid, ... but it can also be an array containing multiple values
410 function notify($eventName, $data) {
411 // load subscription list if needed
412 if (!is_array($this->subscriptions))
413 $this->_loadSubscriptions();
416 // get listening objects
418 if (isset($this->subscriptions[$eventName])) {
419 $listeners = $this->subscriptions[$eventName];
422 // notify all of them
423 if (is_array($listeners)) {
424 foreach($listeners as $listener) {
425 // load class if needed
426 $this->_loadPlugin($listener);
427 // do notify (if method exists)
428 if (isset($this->plugins[$listener]) && method_exists($this->plugins[$listener], 'event_' . $eventName))
429 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), &$data);
436 * Loads plugin subscriptions
438 function _loadSubscriptions() {
439 // initialize as array
440 $this->subscriptions = array();
442 $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');
443 while ($o = sql_fetch_object($res)) {
444 $pluginName = $o->pfile;
445 $eventName = $o->event;
446 $this->subscriptions[$eventName][] = $pluginName;
452 Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
453 requests. tickets are user specific
456 var $currentRequestTicket = '';
459 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
461 function addTicketToUrl($url)
463 $ticketCode = 'ticket=' . $this->_generateTicket();
464 if (strstr($url, '?'))
465 return $url . '&' . $ticketCode;
467 return $url . '?' . $ticketCode;
471 * POST requests: Adds ticket as hidden formvar
473 function addTicketHidden()
475 $ticket = $this->_generateTicket();
477 echo '<input type="hidden" name="ticket" value="', htmlspecialchars($ticket), '" />';
482 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
484 function getNewTicket()
486 $this->currentRequestTicket = '';
487 return $this->_generateTicket();
491 * Checks the ticket that was passed along with the current request
493 function checkTicket()
497 // get ticket from request
498 $ticket = requestVar('ticket');
500 // no ticket -> don't allow
504 // remove expired tickets first
505 $this->_cleanUpExpiredTickets();
508 if (!$member->isLoggedIn())
511 $memberId = $member->getID();
513 // check if ticket is a valid one
514 $query = 'SELECT COUNT(*) as result FROM ' . sql_table('tickets') . ' WHERE member=' . intval($memberId). ' and ticket=\''.sql_real_escape_string($ticket).'\'';
515 if (quickQuery($query) == 1)
517 // [in the original implementation, the checked ticket was deleted. This would lead to invalid
518 // tickets when using the browsers back button and clicking another link/form
519 // leaving the keys in the database is not a real problem, since they're member-specific and
520 // only valid for a period of one hour
522 // sql_query('DELETE FROM '.sql_table('tickets').' WHERE member=' . intval($memberId). ' and ticket=\''.addslashes($ticket).'\'');
525 // not a valid ticket
532 * (internal method) Removes the expired tickets
534 function _cleanUpExpiredTickets()
536 // remove tickets older than 1 hour
537 $oldTime = time() - 60 * 60;
538 $query = 'DELETE FROM ' . sql_table('tickets'). ' WHERE ctime < \'' . date('Y-m-d H:i:s',$oldTime) .'\'';
543 * (internal method) Generates/returns a ticket (one ticket per page request)
545 function _generateTicket()
547 if ($this->currentRequestTicket == '')
549 // generate new ticket (only one ticket will be generated per page request)
550 // and store in database
553 if (!$member->isLoggedIn())
556 $memberId = $member->getID();
561 // generate a random token
562 srand((double)microtime()*1000000);
563 $ticket = md5(uniqid(rand(), true));
565 // add in database as non-active
566 $query = 'INSERT INTO ' . sql_table('tickets') . ' (ticket, member, ctime) ';
567 $query .= 'VALUES (\'' . sql_real_escape_string($ticket). '\', \'' . intval($memberId). '\', \'' . date('Y-m-d H:i:s',time()) . '\')';
568 if (sql_query($query))
572 $this->currentRequestTicket = $ticket;
574 return $this->currentRequestTicket;