3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
13 * This class makes sure each item/weblog/comment object gets requested from
\r
14 * the database only once, by keeping them in a cache. The class also acts as
\r
15 * a dynamic classloader, loading classes _only_ when they are first needed,
\r
16 * hoping to diminish execution time
\r
18 * The class is a singleton, meaning that there will be only one object of it
\r
19 * active at all times. The object can be requested using Manager::instance()
\r
21 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
22 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
23 * @version $Id: MANAGER.php 1731 2012-04-08 15:10:35Z sakamocchi $
\r
28 * Cached ITEM, BLOG, PLUGIN, KARMA and MEMBER objects. When these objects are requested
\r
29 * through the global $manager object (getItem, getBlog, ...), only the first call
\r
30 * will create an object. Subsequent calls will return the same object.
\r
32 * The $items, $blogs, ... arrays map an id to an object (for plugins, the name is used
\r
33 * rather than an ID)
\r
44 * cachedInfo to avoid repeated SQL queries (see pidInstalled/pluginInstalled/getPidFromName)
\r
45 * e.g. which plugins exists?
\r
47 * $cachedInfo['installedPlugins'] = array($pid -> $name)
\r
49 private $cachedInfo;
\r
52 * The plugin subscriptionlist
\r
54 * The subcription array has the following structure
\r
55 * $subscriptions[$EventName] = array containing names of plugin classes to be
\r
56 * notified when that event happens
\r
58 * NOTE: this is referred by Comments::addComment() for spamcheck API
\r
59 * TODO: we should add new methods to get this
\r
61 public $subscriptions;
\r
64 * Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
\r
65 * requests. tickets are user specific
\r
67 private $currentRequestTicket = '';
\r
70 * Returns the only instance of this class. Creates the instance if it
\r
71 * does not yet exists. Users should use this function as
\r
72 * $manager =& Manager::instance(); to get a reference to the object
\r
75 public function &instance()
\r
77 static $instance = array();
\r
78 if ( empty($instance) )
\r
80 $instance[0] = new Manager();
\r
82 return $instance[0];
\r
86 * The constructor of this class initializes the object caches
\r
88 public function __construct()
\r
90 $this->items = array();
\r
91 $this->blogs = array();
\r
92 $this->plugins = array();
\r
93 $this->karma = array();
\r
94 $this->templates = array();
\r
95 $this->skins = array();
\r
96 $this->parserPrefs = array();
\r
97 $this->cachedInfo = array();
\r
102 * Returns the requested item object. If it is not in the cache, it will
\r
103 * first be loaded and then placed in the cache.
\r
104 * Intended use: $item =& $manager->getItem(1234, 0, 0)
\r
106 public function &getItem($itemid, $allowdraft, $allowfuture)
\r
108 /* confirm to cached */
\r
109 if ( !array_key_exists($itemid, $this->items) )
\r
111 $this->loadClass('ITEM');
\r
112 $item = Item::getitem($itemid, $allowdraft, $allowfuture);
\r
113 $this->items[$itemid] = $item;
\r
116 $item =& $this->items[$itemid];
\r
117 if ( !$allowdraft && ($item['draft']) )
\r
122 $blog =& $this->getBlog($item['blogid']);
\r
123 if ( !$allowfuture && ($item['timestamp'] > $blog->getCorrectTime()) )
\r
132 * Loads a class if it has not yet been loaded
\r
134 public function loadClass($name)
\r
136 $this->_loadClass($name, $name . '.php');
\r
141 * Checks if an item exists
\r
143 public function existsItem($id,$future,$draft)
\r
145 $this->_loadClass('ITEM','ITEM.php');
\r
146 return Item::exists($id,$future,$draft);
\r
150 * Checks if a category exists
\r
152 public function existsCategory($id)
\r
154 return (DB::getValue('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
\r
158 * Returns the blog object for a given blogid
\r
160 public function &getBlog($blogid)
\r
162 if ( !array_key_exists($blogid, $this->blogs) )
\r
164 $this->_loadClass('BLOG','BLOG.php');
\r
165 $this->blogs[$blogid] = new Blog($blogid);
\r
167 return $this->blogs[$blogid];
\r
171 * Checks if a blog exists
\r
173 public function existsBlog($name)
\r
175 $this->_loadClass('BLOG','BLOG.php');
\r
176 return Blog::exists($name);
\r
180 * Checks if a blog id exists
\r
182 public function existsBlogID($id)
\r
184 $this->_loadClass('BLOG','BLOG.php');
\r
185 return Blog::existsID($id);
\r
189 * Returns a previously read template
\r
191 public function &getTemplate($templateName)
\r
193 if ( !array_key_exists($templateName, $this->templates) )
\r
195 $this->_loadClass('Template','TEMPLATE.php');
\r
196 $this->templates[$templateName] =& Template::read($templateName);
\r
198 return $this->templates[$templateName];
\r
202 * Returns a KARMA object (karma votes)
\r
204 public function &getKarma($itemid)
\r
206 if ( !array_key_exists($itemid, $this->karma) )
\r
208 $this->_loadClass('Karma','KARMA.php');
\r
209 $this->karma[$itemid] = new Karma($itemid);
\r
211 return $this->karma[$itemid];
\r
215 * Returns a MEMBER object
\r
217 public function &getMember($memberid)
\r
219 if ( !$this->members )
\r
221 $this->members = array();
\r
223 if ( !array_key_exists($memberid, $this->members) )
\r
225 $this->_loadClass('Member','MEMBER.php');
\r
226 $this->members[$memberid] =& Member::createFromID($memberid);;
\r
228 return $this->members[$memberid];
\r
232 * Manager::getSkin()
\r
234 * @param integer $skinid ID for skin
\r
235 * @param string $action_class action class for handling skin variables
\r
236 * @param string $event_identifier identifier for event name
\r
237 * @return object instance of Skin class
\r
239 public function &getSkin($skinid, $action_class='Actions', $event_identifier='Skin')
\r
241 if ( !array_key_exists($skinid, $this->skins) )
\r
243 $this->_loadClass('Skin', 'SKIN.php');
\r
244 $this->skins[$skinid] = new Skin($skinid, $action_class, $event_identifier);
\r
247 return $this->skins[$skinid];
\r
251 * Set the global parser preferences
\r
253 public function setParserProperty($name, $value)
\r
255 $this->parserPrefs[$name] = $value;
\r
260 * Get the global parser preferences
\r
262 public function getParserProperty($name)
\r
264 return $this->parserPrefs[$name];
\r
268 * A helper function to load a class
\r
272 private function _loadClass($name, $filename)
\r
276 if ( !class_exists($name) )
\r
278 include($DIR_LIBS . $filename);
\r
284 * Manager::_loadPlugin()
\r
285 * loading a certain plugin
\r
287 * @param string $name plugin name
\r
290 private function _loadPlugin($name)
\r
292 global $DIR_PLUGINS, $MYSQL_HANDLER, $MYSQL_PREFIX;
\r
294 if ( class_exists($name) )
\r
299 $fileName = "{$DIR_PLUGINS}{$name}.php";
\r
301 if ( !file_exists($fileName) )
\r
303 if ( !defined('_MANAGER_PLUGINFILE_NOTFOUND') )
\r
305 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
\r
307 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
\r
312 include($fileName);
\r
314 // check if class exists (avoid errors in eval'd code)
\r
315 if ( !class_exists($name) )
\r
317 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
\r
321 // add to plugin array
\r
322 $this->plugins[$name] = new $name();
\r
325 $this->plugins[$name]->setID($this->getPidFromName($name));
\r
327 // unload plugin if a prefix is used and the plugin cannot handle this
\r
328 if ( ($MYSQL_PREFIX != '')
\r
329 && !$this->plugins[$name]->supportsFeature('SqlTablePrefix') )
\r
331 unset($this->plugins[$name]);
\r
332 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
\r
336 // unload plugin if using non-mysql handler and plugin does not support it
\r
337 if ( (!in_array('mysql',$MYSQL_HANDLER))
\r
338 && !$this->plugins[$name]->supportsFeature('SqlApi') )
\r
340 unset($this->plugins[$name]);
\r
341 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
\r
345 // call init method
\r
346 $this->plugins[$name]->init();
\r
352 * Manager:getPlugin()
\r
353 * Returns a PLUGIN object
\r
355 * @param string $name name of plugin
\r
356 * @return object plugin object
\r
358 public function &getPlugin($name)
\r
360 // retrieve the name of the plugin in the right capitalisation
\r
361 $name = $this->getUpperCaseName ($name);
\r
364 $plugin =& $this->plugins[$name];
\r
368 // load class if needed
\r
369 $this->_loadPlugin($name);
\r
370 $plugin =& $this->plugins[$name];
\r
376 * Manager::pluginLoaded()
\r
377 * Checks if the given plugin IS loaded or not
\r
379 * @param string $name name of plugin
\r
380 * @return object plugin object
\r
382 public function &pluginLoaded($name)
\r
384 $plugin =& $this->plugins[$name];
\r
389 * Manager::pidLoaded()
\r
391 * @param integer $pid id for plugin
\r
392 * @return object plugin object
\r
394 public function &pidLoaded($pid)
\r
397 reset($this->plugins);
\r
398 while ( list($name) = each($this->plugins) )
\r
400 if ( $pid!=$this->plugins[$name]->getId() )
\r
404 $plugin= & $this->plugins[$name];
\r
411 * Manager::pluginInstalled()
\r
412 * checks if the given plugin IS installed or not
\r
414 * @param string $name name of plugin
\r
415 * @return boolean exists or not
\r
417 public function pluginInstalled($name)
\r
419 $this->_initCacheInfo('installedPlugins');
\r
420 return ($this->getPidFromName($name) != -1);
\r
424 * Manager::pidInstalled()
\r
425 * checks if the given plugin IS installed or not
\r
427 * @param integer $pid id of plugin
\r
428 * @return boolean exists or not
\r
430 public function pidInstalled($pid)
\r
432 $this->_initCacheInfo('installedPlugins');
\r
433 return ($this->cachedInfo['installedPlugins'][$pid] != '');
\r
437 * Manager::getPidFromName()
\r
439 * @param string $name name of plugin
\r
440 * @return mixed id for plugin or -1 if not exists
\r
442 public function getPidFromName($name)
\r
444 $this->_initCacheInfo('installedPlugins');
\r
445 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
\r
447 if (strtolower($pfile) == strtolower($name))
\r
456 * Manager::getPluginNameFromPid()
\r
458 * @param string $pid ID for plugin
\r
459 * @return string name of plugin
\r
461 public function getPluginNameFromPid($pid)
\r
463 if ( !array_key_exists($pid, $this->cachedInfo['installedPlugins']) )
\r
465 $query = 'SELECT pfile FROM %s WHERE pid=%d;';
\r
466 $query = sprintf($query, sql_table('plugin'), (integer) $pid);
\r
467 return DB::getValue($query);
\r
469 return $this->cachedInfo['installedPlugins'][$pid];
\r
473 * Manager::getUpperCaseName()
\r
474 * Retrieve the name of a plugin in the right capitalisation
\r
476 * @param string $name name of plugin
\r
477 * @return string name according to UpperCamelCase
\r
479 public function getUpperCaseName ($name)
\r
481 $this->_initCacheInfo('installedPlugins');
\r
482 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
\r
484 if ( strtolower($pfile) == strtolower($name) )
\r
493 * Manager::clearCachedInfo()
\r
495 * @param string $what
\r
498 public function clearCachedInfo($what)
\r
500 unset($this->cachedInfo[$what]);
\r
505 * Manager::_initCacheInfo()
\r
506 * Loads some info on the first call only
\r
508 * @param string $what 'installedPlugins'
\r
511 private function _initCacheInfo($what)
\r
513 if ( array_key_exists($what, $this->cachedInfo)
\r
514 && is_array($this->cachedInfo[$what]) )
\r
521 // 'installedPlugins' = array ($pid => $name)
\r
522 case 'installedPlugins':
\r
523 $this->cachedInfo['installedPlugins'] = array();
\r
524 $res = DB::getResult('SELECT pid, pfile FROM ' . sql_table('plugin'));
\r
525 foreach ( $res as $row )
\r
527 $this->cachedInfo['installedPlugins'][$row['pid']] = $row['pfile'];
\r
535 * Manager::notify()
\r
536 * A function to notify plugins that something has happened. Only the plugins
\r
537 * that are subscribed to the event will get notified.
\r
538 * Upon the first call, the list of subscriptions will be fetched from the
\r
539 * database. The plugins itsself will only get loaded when they are first needed
\r
541 * @param string $eventName Name of the event (method to be called on plugins)
\r
542 * @param string $data Can contain any type of data,
\r
543 * depending on the event type. Usually this is an itemid, blogid, ...
\r
544 * but it can also be an array containing multiple values
\r
547 public function notify($eventName, $data)
\r
549 // load subscription list if needed
\r
550 if ( !is_array($this->subscriptions) )
\r
552 $this->_loadSubscriptions();
\r
555 // get listening objects
\r
556 $listeners = false;
\r
557 if ( array_key_exists($eventName, $this->subscriptions)
\r
558 && !empty($this->subscriptions[$eventName]) )
\r
560 $listeners = $this->subscriptions[$eventName];
\r
563 // notify all of them
\r
564 if ( is_array($listeners) )
\r
566 foreach( $listeners as $listener )
\r
568 // load class if needed
\r
569 $this->_loadPlugin($listener);
\r
571 // do notify (if method exists)
\r
572 if ( array_key_exists($listener, $this->plugins)
\r
573 && !empty($this->plugins[$listener])
\r
574 && method_exists($this->plugins[$listener], 'event_' . $eventName) )
\r
576 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);
\r
584 * Manager::_loadSubscriptions()
\r
585 * Loads plugin subscriptions
\r
590 private function _loadSubscriptions()
\r
592 // initialize as array
\r
593 $this->subscriptions = array();
\r
595 $query = "SELECT p.pfile as pfile, e.event as event"
\r
596 . " FROM %s as e, %s as p"
\r
597 . " WHERE e.pid=p.pid ORDER BY p.porder ASC";
\r
598 $query = sprintf($query, sql_table('plugin_event'), sql_table('plugin'));
\r
599 $res = DB::getResult($query);
\r
601 foreach ( $res as $row )
\r
603 $pluginName = $row['pfile'];
\r
604 $eventName = $row['event'];
\r
605 $this->subscriptions[$eventName][] = $pluginName;
\r
611 * Manager::getNumberOfSubscribers()
\r
613 * @param string $event name of events
\r
614 * @return integer number of event subscriber
\r
616 public function getNumberOfSubscribers($event)
\r
618 $query = 'SELECT COUNT(*) as count FROM %s WHERE event=%s;';
\r
619 $query = sprintf($query, sql_table('plugin_event'), DB::quoteValue($event));
\r
620 return (integer) DB::getValue($query);
\r
624 * Manager::addTicketToUrl()
\r
625 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
\r
627 * @param string url string for URI
\r
630 public function addTicketToUrl($url)
\r
632 $ticketCode = 'ticket=' . $this->_generateTicket();
\r
633 if ( i18n::strpos($url, '?') === FALSE )
\r
635 $ticketCode = "{$url}?{$ticketCode}";
\r
639 $ticketCode = "{$url}&{$ticketCode}";
\r
641 return $ticketCode;
\r
645 * Manager::addTicketHidden()
\r
646 * POST requests: Adds ticket as hidden formvar
\r
651 public function addTicketHidden()
\r
653 $ticket = $this->_generateTicket();
\r
654 echo '<input type="hidden" name="ticket" value="', Entity::hsc($ticket), '" />';
\r
659 * Manager::getNewTicket()
\r
661 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
\r
664 * @return string string of ticket
\r
666 public function getNewTicket()
\r
668 $this->currentRequestTicket = '';
\r
669 return $this->_generateTicket();
\r
673 * Manager::checkTicket()
\r
674 * Checks the ticket that was passed along with the current request
\r
677 * @return boolean correct or not
\r
679 public function checkTicket()
\r
683 // get ticket from request
\r
684 $ticket = requestVar('ticket');
\r
686 // no ticket -> don't allow
\r
687 if ( $ticket == '' )
\r
692 // remove expired tickets first
\r
693 $this->_cleanUpExpiredTickets();
\r
696 if (!$member->isLoggedIn())
\r
702 $memberId = $member->getID();
\r
705 // check if ticket is a valid one
\r
706 $query = sprintf('SELECT COUNT(*) as result FROM %s WHERE member=%d and ticket=%s',
\r
707 sql_table('tickets'),
\r
709 DB::quoteValue($ticket)
\r
714 * [in the original implementation, the checked ticket was deleted. This would lead to invalid
\r
715 * tickets when using the browsers back button and clicking another link/form
\r
716 * leaving the keys in the database is not a real problem, since they're member-specific and
\r
717 * only valid for a period of one hour]
\r
719 if ( DB::getValue($query) != 1 )
\r
728 * Manager::_cleanUpExpiredTickets()
\r
729 * Removes the expired tickets
\r
734 private function _cleanUpExpiredTickets()
\r
736 // remove tickets older than 1 hour
\r
737 $oldTime = time() - 60 * 60;
\r
738 $query = 'DELETE FROM %s WHERE ctime < %s';
\r
739 $query = sprintf($query, sql_table('tickets'), DB::formatDateTime($oldTime));
\r
740 DB::execute($query);
\r
745 * Manager::_generateTicket()
\r
746 * Generates/returns a ticket (one ticket per page request)
\r
751 private function _generateTicket()
\r
753 if ( $this->currentRequestTicket == '' )
\r
755 // generate new ticket (only one ticket will be generated per page request)
\r
756 // and store in database
\r
759 if ( !$member->isLoggedIn() )
\r
765 $memberId = $member->getID();
\r
771 // generate a random token
\r
772 srand((double)microtime()*1000000);
\r
773 $ticket = md5(uniqid(rand(), true));
\r
775 // add in database as non-active
\r
776 $query = 'INSERT INTO %s (ticket, member, ctime) VALUES (%s, %d, %s)';
\r
777 $query = sprintf($query, sql_table('tickets'), DB::quoteValue($ticket), (integer) $memberId, DB::formatDateTime());
\r
779 if ( DB::execute($query) !== FALSE )
\r
784 $this->currentRequestTicket = $ticket;
\r
786 return $this->currentRequestTicket;
\r