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 1878 2012-06-17 07:42:07Z sakamocchi $
28 * Cached ITEM, BLOG, PLUGIN, KARMA and MEMBER objects. When these objects are requested
29 * through the global $manager object (getItem, getBlog, ...), only the first call
30 * will create an object. Subsequent calls will return the same object.
32 * 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
58 * NOTE: this is referred by Comments::addComment() for spamcheck API
59 * TODO: we should add new methods to get this
61 public $subscriptions;
64 * Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
65 * requests. tickets are user specific
67 private $currentRequestTicket = '';
70 * Returns the only instance of this class. Creates the instance if it
71 * does not yet exists. Users should use this function as
72 * $manager =& Manager::instance(); to get a reference to the object
75 public static function &instance()
77 static $instance = array();
78 if ( empty($instance) )
80 $instance[0] = new Manager();
86 * The constructor of this class initializes the object caches
88 public function __construct()
90 $this->items = array();
91 $this->blogs = array();
92 $this->plugins = array();
93 $this->karma = array();
94 $this->templates = array();
95 $this->skins = array();
96 $this->parserPrefs = array();
97 $this->cachedInfo = array();
98 $this->members = array();
103 * Returns the requested item object. If it is not in the cache, it will
104 * first be loaded and then placed in the cache.
105 * Intended use: $item =& $manager->getItem(1234, 0, 0)
107 public function &getItem($itemid, $allowdraft, $allowfuture)
109 /* confirm to cached */
110 if ( !array_key_exists($itemid, $this->items) )
112 $this->loadClass('ITEM');
113 $item = Item::getitem($itemid, $allowdraft, $allowfuture);
114 $this->items[$itemid] = $item;
117 $item =& $this->items[$itemid];
118 if ( !$allowdraft && ($item['draft']) )
123 $blog =& $this->getBlog($item['blogid']);
124 if ( !$allowfuture && ($item['timestamp'] > $blog->getCorrectTime()) )
133 * Loads a class if it has not yet been loaded
135 public function loadClass($name)
137 $this->_loadClass($name, $name . '.php');
142 * Checks if an item exists
144 public function existsItem($id,$future,$draft)
146 $this->_loadClass('ITEM','ITEM.php');
147 return Item::exists($id,$future,$draft);
151 * Checks if a category exists
153 public function existsCategory($id)
155 return (DB::getValue('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
159 * Returns the blog object for a given blogid
161 public function &getBlog($blogid)
163 if ( !array_key_exists($blogid, $this->blogs) )
165 $this->_loadClass('BLOG','BLOG.php');
166 $this->blogs[$blogid] = new Blog($blogid);
168 return $this->blogs[$blogid];
172 * Checks if a blog exists
174 public function existsBlog($name)
176 $this->_loadClass('BLOG','BLOG.php');
177 return Blog::exists($name);
181 * Checks if a blog id exists
183 public function existsBlogID($id)
185 $this->_loadClass('BLOG','BLOG.php');
186 return Blog::existsID($id);
190 * Returns a previously read template
192 public function &getTemplate($templateName)
194 if ( !array_key_exists($templateName, $this->templates) )
196 $this->_loadClass('Template','TEMPLATE.php');
197 $tmplate_tmp = Template::read($templateName);
198 $this->templates[$templateName] =& $tmplate_tmp;
200 return $this->templates[$templateName];
204 * Returns a KARMA object (karma votes)
206 public function &getKarma($itemid)
208 if ( !array_key_exists($itemid, $this->karma) )
210 $this->_loadClass('Karma','KARMA.php');
211 $this->karma[$itemid] = new Karma($itemid);
213 return $this->karma[$itemid];
217 * Returns a MEMBER object
219 public function &getMember($memberid)
221 if ( !array_key_exists($memberid, $this->members) )
223 $this->_loadClass('Member','MEMBER.php');
224 $this->members[$memberid] =& Member::createFromID($memberid);;
226 return $this->members[$memberid];
232 * @param integer $skinid ID for skin
233 * @param string $action_class action class for handling skin variables
234 * @param string $event_identifier identifier for event name
235 * @return object instance of Skin class
237 public function &getSkin($skinid, $action_class='Actions', $event_identifier='Skin')
239 if ( !array_key_exists($skinid, $this->skins) )
241 $this->_loadClass('Skin', 'SKIN.php');
242 $this->skins[$skinid] = new Skin($skinid, $action_class, $event_identifier);
245 return $this->skins[$skinid];
249 * Set the global parser preferences
251 public function setParserProperty($name, $value)
253 $this->parserPrefs[$name] = $value;
258 * Get the global parser preferences
260 public function getParserProperty($name)
262 return $this->parserPrefs[$name];
266 * A helper function to load a class
270 private function _loadClass($name, $filename)
274 if ( !class_exists($name) )
276 include($DIR_LIBS . $filename);
282 * Manager::_loadPlugin()
283 * loading a certain plugin
285 * @param string $name plugin name
288 private function _loadPlugin($name)
290 global $DIR_PLUGINS, $MYSQL_HANDLER, $MYSQL_PREFIX;
292 if ( class_exists($name) )
297 $fileName = "{$DIR_PLUGINS}{$name}.php";
299 if ( !file_exists($fileName) )
301 if ( !defined('_MANAGER_PLUGINFILE_NOTFOUND') )
303 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
305 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
312 // check if class exists (avoid errors in eval'd code)
313 if ( !class_exists($name) )
315 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
319 // add to plugin array
320 $this->plugins[$name] = new $name();
323 $this->plugins[$name]->setID($this->getPidFromName($name));
325 // unload plugin if a prefix is used and the plugin cannot handle this
326 if ( ($MYSQL_PREFIX != '')
327 && !$this->plugins[$name]->supportsFeature('SqlTablePrefix') )
329 unset($this->plugins[$name]);
330 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
334 // unload plugin if using non-mysql handler and plugin does not support it
335 if ( (!in_array('mysql',$MYSQL_HANDLER))
336 && !$this->plugins[$name]->supportsFeature('SqlApi') )
338 unset($this->plugins[$name]);
339 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
344 $this->plugins[$name]->init();
350 * Manager:getPlugin()
351 * Returns a PLUGIN object
353 * @param string $name name of plugin
354 * @return object plugin object
356 public function &getPlugin($name)
358 // retrieve the name of the plugin in the right capitalisation
359 $name = $this->getUpperCaseName ($name);
362 $plugin =& $this->plugins[$name];
366 // load class if needed
367 $this->_loadPlugin($name);
368 $plugin =& $this->plugins[$name];
374 * Manager::pluginLoaded()
375 * Checks if the given plugin IS loaded or not
377 * @param string $name name of plugin
378 * @return object plugin object
380 public function &pluginLoaded($name)
382 $plugin =& $this->plugins[$name];
387 * Manager::pidLoaded()
389 * @param integer $pid id for plugin
390 * @return object plugin object
392 public function &pidLoaded($pid)
395 reset($this->plugins);
396 while ( list($name) = each($this->plugins) )
398 if ( $pid!=$this->plugins[$name]->getId() )
402 $plugin= & $this->plugins[$name];
409 * Manager::pluginInstalled()
410 * checks if the given plugin IS installed or not
412 * @param string $name name of plugin
413 * @return boolean exists or not
415 public function pluginInstalled($name)
417 $this->_initCacheInfo('installedPlugins');
418 return ($this->getPidFromName($name) != -1);
422 * Manager::pidInstalled()
423 * checks if the given plugin IS installed or not
425 * @param integer $pid id of plugin
426 * @return boolean exists or not
428 public function pidInstalled($pid)
430 $this->_initCacheInfo('installedPlugins');
431 return ($this->cachedInfo['installedPlugins'][$pid] != '');
435 * Manager::getPidFromName()
437 * @param string $name name of plugin
438 * @return mixed id for plugin or -1 if not exists
440 public function getPidFromName($name)
442 $this->_initCacheInfo('installedPlugins');
443 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
445 if (strtolower($pfile) == strtolower($name))
454 * Manager::getPluginNameFromPid()
456 * @param string $pid ID for plugin
457 * @return string name of plugin
459 public function getPluginNameFromPid($pid)
461 if ( !array_key_exists($pid, $this->cachedInfo['installedPlugins']) )
463 $query = 'SELECT pfile FROM %s WHERE pid=%d;';
464 $query = sprintf($query, sql_table('plugin'), (integer) $pid);
465 return DB::getValue($query);
467 return $this->cachedInfo['installedPlugins'][$pid];
471 * Manager::getUpperCaseName()
472 * Retrieve the name of a plugin in the right capitalisation
474 * @param string $name name of plugin
475 * @return string name according to UpperCamelCase
477 public function getUpperCaseName ($name)
479 $this->_initCacheInfo('installedPlugins');
480 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
482 if ( strtolower($pfile) == strtolower($name) )
491 * Manager::clearCachedInfo()
493 * @param string $what
496 public function clearCachedInfo($what)
498 unset($this->cachedInfo[$what]);
503 * Manager::_initCacheInfo()
504 * Loads some info on the first call only
506 * @param string $what 'installedPlugins'
509 private function _initCacheInfo($what)
511 if ( array_key_exists($what, $this->cachedInfo)
512 && is_array($this->cachedInfo[$what]) )
519 // 'installedPlugins' = array ($pid => $name)
520 case 'installedPlugins':
521 $this->cachedInfo['installedPlugins'] = array();
522 $res = DB::getResult('SELECT pid, pfile FROM ' . sql_table('plugin'));
523 foreach ( $res as $row )
525 $this->cachedInfo['installedPlugins'][$row['pid']] = $row['pfile'];
534 * A function to notify plugins that something has happened. Only the plugins
535 * that are subscribed to the event will get notified.
536 * Upon the first call, the list of subscriptions will be fetched from the
537 * database. The plugins itsself will only get loaded when they are first needed
539 * @param string $eventName Name of the event (method to be called on plugins)
540 * @param string $data Can contain any type of data,
541 * depending on the event type. Usually this is an itemid, blogid, ...
542 * but it can also be an array containing multiple values
545 public function notify($eventName, $data)
547 // load subscription list if needed
548 if ( !is_array($this->subscriptions) )
550 $this->_loadSubscriptions();
553 // get listening objects
555 if ( array_key_exists($eventName, $this->subscriptions)
556 && !empty($this->subscriptions[$eventName]) )
558 $listeners = $this->subscriptions[$eventName];
561 // notify all of them
562 if ( is_array($listeners) )
564 foreach( $listeners as $listener )
566 // load class if needed
567 $this->_loadPlugin($listener);
569 // do notify (if method exists)
570 if ( array_key_exists($listener, $this->plugins)
571 && !empty($this->plugins[$listener])
572 && method_exists($this->plugins[$listener], 'event_' . $eventName) )
574 call_user_func(array(&$this->plugins[$listener], 'event_' . $eventName), array(&$data));
582 * Manager::_loadSubscriptions()
583 * Loads plugin subscriptions
588 private function _loadSubscriptions()
590 // initialize as array
591 $this->subscriptions = array();
593 $query = "SELECT p.pfile as pfile, e.event as event"
594 . " FROM %s as e, %s as p"
595 . " WHERE e.pid=p.pid ORDER BY p.porder ASC";
596 $query = sprintf($query, sql_table('plugin_event'), sql_table('plugin'));
597 $res = DB::getResult($query);
599 foreach ( $res as $row )
601 $pluginName = $row['pfile'];
602 $eventName = $row['event'];
603 $this->subscriptions[$eventName][] = $pluginName;
609 * Manager::getNumberOfSubscribers()
611 * @param string $event name of events
612 * @return integer number of event subscriber
614 public function getNumberOfSubscribers($event)
616 $query = 'SELECT COUNT(*) as count FROM %s WHERE event=%s;';
617 $query = sprintf($query, sql_table('plugin_event'), DB::quoteValue($event));
618 return (integer) DB::getValue($query);
622 * Manager::addTicketToUrl()
623 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
625 * @param string url string for URI
628 public function addTicketToUrl($url)
630 $ticketCode = 'ticket=' . $this->_generateTicket();
631 if ( i18n::strpos($url, '?') === FALSE )
633 $ticketCode = "{$url}?{$ticketCode}";
637 $ticketCode = "{$url}&{$ticketCode}";
643 * Manager::addTicketHidden()
644 * POST requests: Adds ticket as hidden formvar
649 public function addTicketHidden()
651 $ticket = $this->_generateTicket();
652 echo '<input type="hidden" name="ticket" value="', Entity::hsc($ticket), '" />';
657 * Manager::getNewTicket()
659 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
662 * @return string string of ticket
664 public function getNewTicket()
666 $this->currentRequestTicket = '';
667 return $this->_generateTicket();
671 * Manager::checkTicket()
672 * Checks the ticket that was passed along with the current request
675 * @return boolean correct or not
677 public function checkTicket()
681 // get ticket from request
682 $ticket = requestVar('ticket');
684 // no ticket -> don't allow
690 // remove expired tickets first
691 $this->_cleanUpExpiredTickets();
694 if (!$member->isLoggedIn())
700 $memberId = $member->getID();
703 // check if ticket is a valid one
704 $query = sprintf('SELECT COUNT(*) as result FROM %s WHERE member=%d and ticket=%s',
705 sql_table('tickets'),
707 DB::quoteValue($ticket)
712 * [in the original implementation, the checked ticket was deleted. This would lead to invalid
713 * tickets when using the browsers back button and clicking another link/form
714 * leaving the keys in the database is not a real problem, since they're member-specific and
715 * only valid for a period of one hour]
717 if ( DB::getValue($query) != 1 )
726 * Manager::_cleanUpExpiredTickets()
727 * Removes the expired tickets
732 private function _cleanUpExpiredTickets()
734 // remove tickets older than 1 hour
735 $oldTime = time() - 60 * 60;
736 $query = 'DELETE FROM %s WHERE ctime < %s';
737 $query = sprintf($query, sql_table('tickets'), DB::formatDateTime($oldTime));
743 * Manager::_generateTicket()
744 * Generates/returns a ticket (one ticket per page request)
749 private function _generateTicket()
751 if ( $this->currentRequestTicket == '' )
753 // generate new ticket (only one ticket will be generated per page request)
754 // and store in database
757 if ( !$member->isLoggedIn() )
763 $memberId = $member->getID();
769 // generate a random token
770 srand((double)microtime()*1000000);
771 $ticket = md5(uniqid(rand(), true));
773 // add in database as non-active
774 $query = 'INSERT INTO %s (ticket, member, ctime) VALUES (%s, %d, %s)';
775 $query = sprintf($query, sql_table('tickets'), DB::quoteValue($ticket), (integer) $memberId, DB::formatDateTime());
777 if ( DB::execute($query) !== FALSE )
782 $this->currentRequestTicket = $ticket;
784 return $this->currentRequestTicket;