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 * A helper function to load a plugin
271 function _loadPlugin($name)
273 if ( !class_exists($name) )
277 $fileName = $DIR_PLUGINS . $name . '.php';
279 if ( !file_exists($fileName) )
281 if ( !defined('_MANAGER_PLUGINFILE_NOTFOUND') )
283 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
285 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
292 // check if class exists (avoid errors in eval'd code)
293 if ( !class_exists($name) )
295 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
299 // add to plugin array
300 eval('$this->plugins[$name] = new ' . $name . '();');
303 $this->plugins[$name]->plugid = $this->getPidFromName($name);
305 // unload plugin if a prefix is used and the plugin cannot handle this^
306 global $MYSQL_PREFIX;
307 if ( ($MYSQL_PREFIX != '')
308 && !$this->plugins[$name]->supportsFeature('SqlTablePrefix') )
310 unset($this->plugins[$name]);
311 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
315 // unload plugin if using non-mysql handler and plugin does not support it
316 global $MYSQL_HANDLER;
317 if ( (!in_array('mysql',$MYSQL_HANDLER))
318 && !$this->plugins[$name]->supportsFeature('SqlApi') )
320 unset($this->plugins[$name]);
321 ACTIONLOG::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
326 $this->plugins[$name]->init();
332 * Returns a PLUGIN object
334 function &getPlugin($name)
336 // retrieve the name of the plugin in the right capitalisation
337 $name = $this->getUpperCaseName ($name);
339 $plugin =& $this->plugins[$name];
343 // load class if needed
344 $this->_loadPlugin($name);
345 $plugin =& $this->plugins[$name];
351 * Checks if the given plugin IS loaded or not
353 function &pluginLoaded($name)
355 $plugin =& $this->plugins[$name];
359 function &pidLoaded($pid)
362 reset($this->plugins);
363 while ( list($name) = each($this->plugins) )
365 if ( $pid!=$this->plugins[$name]->getId() )
369 $plugin= & $this->plugins[$name];
376 * checks if the given plugin IS installed or not
378 function pluginInstalled($name)
380 $this->_initCacheInfo('installedPlugins');
381 return ($this->getPidFromName($name) != -1);
384 function pidInstalled($pid)
386 $this->_initCacheInfo('installedPlugins');
387 return ($this->cachedInfo['installedPlugins'][$pid] != '');
390 function getPidFromName($name)
392 $this->_initCacheInfo('installedPlugins');
393 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
395 if (strtolower($pfile) == strtolower($name))
404 * Retrieve the name of a plugin in the right capitalisation
406 function getUpperCaseName ($name)
408 $this->_initCacheInfo('installedPlugins');
409 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
411 if ( strtolower($pfile) == strtolower($name) )
419 function clearCachedInfo($what)
421 unset($this->cachedInfo[$what]);
426 * Loads some info on the first call only
428 function _initCacheInfo($what)
430 if ( isset($this->cachedInfo[$what]) && is_array($this->cachedInfo[$what]) )
436 // 'installedPlugins' = array ($pid => $name)
437 case 'installedPlugins':
438 $this->cachedInfo['installedPlugins'] = array();
439 $res = sql_query('SELECT pid, pfile FROM ' . sql_table('plugin'));
440 while ( $o = sql_fetch_object($res) )
442 $this->cachedInfo['installedPlugins'][$o->pid] = $o->pfile;
450 * A function to notify plugins that something has happened. Only the plugins
451 * that are subscribed to the event will get notified.
452 * Upon the first call, the list of subscriptions will be fetched from the
453 * database. The plugins itsself will only get loaded when they are first needed
456 * Name of the event (method to be called on plugins)
458 * Can contain any type of data, depending on the event type. Usually this is
459 * an itemid, blogid, ... but it can also be an array containing multiple values
461 function notify($eventName, $data)
463 // load subscription list if needed
464 if ( !is_array($this->subscriptions) )
466 $this->_loadSubscriptions();
469 // get listening objects
471 if ( isset($this->subscriptions[$eventName]) )
473 $listeners = $this->subscriptions[$eventName];
476 // notify all of them
477 if ( is_array($listeners) )
479 foreach( $listeners as $listener )
481 // load class if needed
482 $this->_loadPlugin($listener);
483 // do notify (if method exists)
484 if ( isset($this->plugins[$listener])
485 && method_exists($this->plugins[$listener], 'event_' . $eventName))
487 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);
495 * Loads plugin subscriptions
497 function _loadSubscriptions()
499 // initialize as array
500 $this->subscriptions = array();
502 $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');
503 while ( $o = sql_fetch_object($res) )
505 $pluginName = $o->pfile;
506 $eventName = $o->event;
507 $this->subscriptions[$eventName][] = $pluginName;
513 Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
514 requests. tickets are user specific
517 var $currentRequestTicket = '';
520 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
522 function addTicketToUrl($url)
524 $ticketCode = 'ticket=' . $this->_generateTicket();
525 if ( strstr($url, '?') )
527 $ticketCode = "{$url}&{$ticketCode}";
531 $ticketCode = "{$url}?{$ticketCode}";
537 * POST requests: Adds ticket as hidden formvar
539 function addTicketHidden()
541 $ticket = $this->_generateTicket();
542 echo '<input type="hidden" name="ticket" value="', i18n::hsc($ticket), '" />';
548 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
550 function getNewTicket()
552 $this->currentRequestTicket = '';
553 return $this->_generateTicket();
557 * Checks the ticket that was passed along with the current request
559 function checkTicket()
563 // get ticket from request
564 $ticket = requestVar('ticket');
566 // no ticket -> don't allow
572 // remove expired tickets first
573 $this->_cleanUpExpiredTickets();
576 if (!$member->isLoggedIn())
582 $memberId = $member->getID();
585 // check if ticket is a valid one
586 $query = 'SELECT COUNT(*) as result FROM ' . sql_table('tickets') . ' WHERE member=' . intval($memberId). ' and ticket=\''.sql_real_escape_string($ticket).'\'';
590 * [in the original implementation, the checked ticket was deleted. This would lead to invalid
591 * tickets when using the browsers back button and clicking another link/form
592 * leaving the keys in the database is not a real problem, since they're member-specific and
593 * only valid for a period of one hour]
595 if ( quickQuery($query) == 1 )
599 // not a valid ticket
607 * (internal method) Removes the expired tickets
609 function _cleanUpExpiredTickets()
611 // remove tickets older than 1 hour
612 $oldTime = time() - 60 * 60;
613 $query = 'DELETE FROM ' . sql_table('tickets'). ' WHERE ctime < \'' . date('Y-m-d H:i:s',$oldTime) .'\'';
619 * (internal method) Generates/returns a ticket (one ticket per page request)
621 function _generateTicket()
623 if ( $this->currentRequestTicket == '' )
625 // generate new ticket (only one ticket will be generated per page request)
626 // and store in database
629 if ( !$member->isLoggedIn() )
635 $memberId = $member->getID();
641 // generate a random token
642 srand((double)microtime()*1000000);
643 $ticket = md5(uniqid(rand(), true));
645 // add in database as non-active
646 $query = 'INSERT INTO ' . sql_table('tickets') . ' (ticket, member, ctime) ';
647 $query .= 'VALUES (\'' . sql_real_escape_string($ticket). '\', \'' . intval($memberId). '\', \'' . date('Y-m-d H:i:s',time()) . '\')';
648 if ( sql_query($query) )
653 $this->currentRequestTicket = $ticket;
655 return $this->currentRequestTicket;