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
43 * cachedInfo to avoid repeated SQL queries (see pidInstalled/pluginInstalled/getPidFromName)
\r
44 * e.g. which plugins exists?
\r
46 * $cachedInfo['installedPlugins'] = array($pid -> $name)
\r
48 private $cachedInfo;
\r
51 * The plugin subscriptionlist
\r
53 * The subcription array has the following structure
\r
54 * $subscriptions[$EventName] = array containing names of plugin classes to be
\r
55 * notified when that event happens
\r
57 * NOTE: this is referred by Comments::addComment() for spamcheck API
\r
58 * TODO: we should add new methods to get this
\r
60 public $subscriptions;
\r
63 * Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
\r
64 * requests. tickets are user specific
\r
66 private $currentRequestTicket = '';
\r
69 * Returns the only instance of this class. Creates the instance if it
\r
70 * does not yet exists. Users should use this function as
\r
71 * $manager =& Manager::instance(); to get a reference to the object
\r
74 public function &instance()
\r
76 static $instance = array();
\r
77 if ( empty($instance) )
\r
79 $instance[0] = new Manager();
\r
81 return $instance[0];
\r
85 * The constructor of this class initializes the object caches
\r
87 public function __construct()
\r
89 $this->items = array();
\r
90 $this->blogs = array();
\r
91 $this->plugins = array();
\r
92 $this->karma = array();
\r
93 $this->parserPrefs = array();
\r
94 $this->cachedInfo = array();
\r
99 * Returns the requested item object. If it is not in the cache, it will
\r
100 * first be loaded and then placed in the cache.
\r
101 * Intended use: $item =& $manager->getItem(1234)
\r
103 public function &getItem($itemid, $allowdraft, $allowfuture)
\r
105 $item =& $this->items[$itemid];
\r
107 // check the draft and future rules if the item was already cached
\r
110 if ( (!$allowdraft) && ($item['draft']) )
\r
115 $blog =& $this->getBlog(getBlogIDFromItemID($itemid));
\r
117 if ( (!$allowfuture) && ($item['timestamp'] > $blog->getCorrectTime()) )
\r
125 // load class if needed
\r
126 $this->loadClass('ITEM');
\r
127 // load item object
\r
128 $item = Item::getitem($itemid, $allowdraft, $allowfuture);
\r
129 $this->items[$itemid] = $item;
\r
135 * Loads a class if it has not yet been loaded
\r
137 public function loadClass($name)
\r
139 $this->_loadClass($name, $name . '.php');
\r
144 * Checks if an item exists
\r
146 public function existsItem($id,$future,$draft)
\r
148 $this->_loadClass('ITEM','ITEM.php');
\r
149 return Item::exists($id,$future,$draft);
\r
153 * Checks if a category exists
\r
155 public function existsCategory($id)
\r
157 return (DB::getValue('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
\r
161 * Returns the blog object for a given blogid
\r
163 public function &getBlog($blogid)
\r
165 $blog =& $this->blogs[$blogid];
\r
169 // load class if needed
\r
170 $this->_loadClass('BLOG','BLOG.php');
\r
171 // load blog object
\r
172 $blog = new Blog($blogid);
\r
173 $this->blogs[$blogid] =& $blog;
\r
179 * Checks if a blog exists
\r
181 public function existsBlog($name)
\r
183 $this->_loadClass('BLOG','BLOG.php');
\r
184 return Blog::exists($name);
\r
188 * Checks if a blog id exists
\r
190 public function existsBlogID($id)
\r
192 $this->_loadClass('BLOG','BLOG.php');
\r
193 return Blog::existsID($id);
\r
197 * Returns a previously read template
\r
199 public function &getTemplate($templateName)
\r
201 $template =& $this->templates[$templateName];
\r
205 $template = Template::read($templateName);
\r
206 $this->templates[$templateName] =& $template;
\r
212 * Returns a KARMA object (karma votes)
\r
214 public function &getKarma($itemid)
\r
216 $karma =& $this->karma[$itemid];
\r
220 // load class if needed
\r
221 $this->_loadClass('KARMA','KARMA.php');
\r
222 // create KARMA object
\r
223 $karma = new Karma($itemid);
\r
224 $this->karma[$itemid] =& $karma;
\r
230 * Returns a MEMBER object
\r
232 public function &getMember($memberid)
\r
234 $mem =& $this->members[$memberid];
\r
238 // load class if needed
\r
239 $this->_loadClass('MEMBER','MEMBER.php');
\r
240 // create MEMBER object
\r
241 $mem =& Member::createFromID($memberid);
\r
242 $this->members[$memberid] =& $mem;
\r
248 * Set the global parser preferences
\r
250 public function setParserProperty($name, $value)
\r
252 $this->parserPrefs[$name] = $value;
\r
257 * Get the global parser preferences
\r
259 public function getParserProperty($name)
\r
261 return $this->parserPrefs[$name];
\r
265 * A helper function to load a class
\r
269 private function _loadClass($name, $filename)
\r
271 if ( !class_exists($name) )
\r
274 include($DIR_LIBS . $filename);
\r
280 * Manager::_loadPlugin()
\r
281 * loading a certain plugin
\r
283 * @param string $name plugin name
\r
286 private function _loadPlugin($name)
\r
288 global $DIR_PLUGINS, $MYSQL_HANDLER, $MYSQL_PREFIX;
\r
290 if ( class_exists($name) )
\r
295 $fileName = "{$DIR_PLUGINS}{$name}.php";
\r
297 if ( !file_exists($fileName) )
\r
299 if ( !defined('_MANAGER_PLUGINFILE_NOTFOUND') )
\r
301 define('_MANAGER_PLUGINFILE_NOTFOUND', 'Plugin %s was not loaded (File not found)');
\r
303 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOTFOUND, $name));
\r
308 include($fileName);
\r
310 // check if class exists (avoid errors in eval'd code)
\r
311 if ( !class_exists($name) )
\r
313 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINFILE_NOCLASS, $name));
\r
317 // add to plugin array
\r
318 $this->plugins[$name] = new $name();
\r
321 $this->plugins[$name]->setID($this->getPidFromName($name));
\r
323 // unload plugin if a prefix is used and the plugin cannot handle this
\r
324 if ( ($MYSQL_PREFIX != '')
\r
325 && !$this->plugins[$name]->supportsFeature('SqlTablePrefix') )
\r
327 unset($this->plugins[$name]);
\r
328 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINTABLEPREFIX_NOTSUPPORT, $name));
\r
332 // unload plugin if using non-mysql handler and plugin does not support it
\r
333 if ( (!in_array('mysql',$MYSQL_HANDLER))
\r
334 && !$this->plugins[$name]->supportsFeature('SqlApi') )
\r
336 unset($this->plugins[$name]);
\r
337 ActionLog::add(WARNING, sprintf(_MANAGER_PLUGINSQLAPI_NOTSUPPORT, $name));
\r
341 // call init method
\r
342 $this->plugins[$name]->init();
\r
348 * Manager:getPlugin()
\r
349 * Returns a PLUGIN object
\r
351 * @param string $name name of plugin
\r
352 * @return object plugin object
\r
354 public function &getPlugin($name)
\r
356 // retrieve the name of the plugin in the right capitalisation
\r
357 $name = $this->getUpperCaseName ($name);
\r
360 $plugin =& $this->plugins[$name];
\r
364 // load class if needed
\r
365 $this->_loadPlugin($name);
\r
366 $plugin =& $this->plugins[$name];
\r
372 * Manager::pluginLoaded()
\r
373 * Checks if the given plugin IS loaded or not
\r
375 * @param string $name name of plugin
\r
376 * @return object plugin object
\r
378 public function &pluginLoaded($name)
\r
380 $plugin =& $this->plugins[$name];
\r
385 * Manager::pidLoaded()
\r
387 * @param integer $pid id for plugin
\r
388 * @return object plugin object
\r
390 public function &pidLoaded($pid)
\r
393 reset($this->plugins);
\r
394 while ( list($name) = each($this->plugins) )
\r
396 if ( $pid!=$this->plugins[$name]->getId() )
\r
400 $plugin= & $this->plugins[$name];
\r
407 * Manager::pluginInstalled()
\r
408 * checks if the given plugin IS installed or not
\r
410 * @param string $name name of plugin
\r
411 * @return boolean exists or not
\r
413 public function pluginInstalled($name)
\r
415 $this->_initCacheInfo('installedPlugins');
\r
416 return ($this->getPidFromName($name) != -1);
\r
420 * Manager::pidInstalled()
\r
421 * checks if the given plugin IS installed or not
\r
423 * @param integer $pid id of plugin
\r
424 * @return boolean exists or not
\r
426 public function pidInstalled($pid)
\r
428 $this->_initCacheInfo('installedPlugins');
\r
429 return ($this->cachedInfo['installedPlugins'][$pid] != '');
\r
433 * Manager::getPidFromName()
\r
435 * @param string $name name of plugin
\r
436 * @return mixed id for plugin or -1 if not exists
\r
438 public function getPidFromName($name)
\r
440 $this->_initCacheInfo('installedPlugins');
\r
441 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
\r
443 if (strtolower($pfile) == strtolower($name))
\r
452 * Manager::getPluginNameFromPid()
\r
454 * @param string $pid ID for plugin
\r
455 * @return string name of plugin
\r
457 public function getPluginNameFromPid($pid)
\r
459 if ( !array_key_exists($pid, $this->cachedInfo['installedPlugins']) )
\r
461 $query = 'SELECT pfile FROM %s WHERE pid=%d;';
\r
462 $query = sprintf($query, sql_table('plugin'), (integer) $pid);
\r
463 return DB::getValue($query);
\r
465 return $this->cachedInfo['installedPlugins'][$pid];
\r
469 * Manager::getUpperCaseName()
\r
470 * Retrieve the name of a plugin in the right capitalisation
\r
472 * @param string $name name of plugin
\r
473 * @return string name according to UpperCamelCase
\r
475 public function getUpperCaseName ($name)
\r
477 $this->_initCacheInfo('installedPlugins');
\r
478 foreach ( $this->cachedInfo['installedPlugins'] as $pid => $pfile )
\r
480 if ( strtolower($pfile) == strtolower($name) )
\r
489 * Manager::clearCachedInfo()
\r
491 * @param string $what
\r
494 public function clearCachedInfo($what)
\r
496 unset($this->cachedInfo[$what]);
\r
501 * Manager::_initCacheInfo()
\r
502 * Loads some info on the first call only
\r
504 * @param string $what 'installedPlugins'
\r
507 private function _initCacheInfo($what)
\r
509 if ( array_key_exists($what, $this->cachedInfo)
\r
510 && is_array($this->cachedInfo[$what]) )
\r
517 // 'installedPlugins' = array ($pid => $name)
\r
518 case 'installedPlugins':
\r
519 $this->cachedInfo['installedPlugins'] = array();
\r
520 $res = DB::getResult('SELECT pid, pfile FROM ' . sql_table('plugin'));
\r
521 foreach ( $res as $row )
\r
523 $this->cachedInfo['installedPlugins'][$row['pid']] = $row['pfile'];
\r
531 * Manager::notify()
\r
532 * A function to notify plugins that something has happened. Only the plugins
\r
533 * that are subscribed to the event will get notified.
\r
534 * Upon the first call, the list of subscriptions will be fetched from the
\r
535 * database. The plugins itsself will only get loaded when they are first needed
\r
537 * @param string $eventName Name of the event (method to be called on plugins)
\r
538 * @param string $data Can contain any type of data,
\r
539 * depending on the event type. Usually this is an itemid, blogid, ...
\r
540 * but it can also be an array containing multiple values
\r
543 public function notify($eventName, $data)
\r
545 // load subscription list if needed
\r
546 if ( !is_array($this->subscriptions) )
\r
548 $this->_loadSubscriptions();
\r
551 // get listening objects
\r
552 $listeners = false;
\r
553 if ( array_key_exists($eventName, $this->subscriptions)
\r
554 && !empty($this->subscriptions[$eventName]) )
\r
556 $listeners = $this->subscriptions[$eventName];
\r
559 // notify all of them
\r
560 if ( is_array($listeners) )
\r
562 foreach( $listeners as $listener )
\r
564 // load class if needed
\r
565 $this->_loadPlugin($listener);
\r
567 // do notify (if method exists)
\r
568 if ( array_key_exists($listener, $this->plugins)
\r
569 && !empty($this->plugins[$listener])
\r
570 && method_exists($this->plugins[$listener], 'event_' . $eventName) )
\r
572 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);
\r
580 * Manager::_loadSubscriptions()
\r
581 * Loads plugin subscriptions
\r
586 private function _loadSubscriptions()
\r
588 // initialize as array
\r
589 $this->subscriptions = array();
\r
591 $query = "SELECT p.pfile as pfile, e.event as event"
\r
592 . " FROM %s as e, %s as p"
\r
593 . " WHERE e.pid=p.pid ORDER BY p.porder ASC";
\r
594 $query = sprintf($query, sql_table('plugin_event'), sql_table('plugin'));
\r
595 $res = DB::getResult($query);
\r
597 foreach ( $res as $row )
\r
599 $pluginName = $row['pfile'];
\r
600 $eventName = $row['event'];
\r
601 $this->subscriptions[$eventName][] = $pluginName;
\r
607 * Manager::getNumberOfSubscribers()
\r
609 * @param string $event name of events
\r
610 * @return integer number of event subscriber
\r
612 public function getNumberOfSubscribers($event)
\r
614 $query = 'SELECT COUNT(*) as count FROM %s WHERE event=%s;';
\r
615 $query = sprintf($query, sql_table('plugin_event'), DB::quoteValue($event));
\r
616 return (integer) DB::getValue($query);
\r
620 * Manager::addTicketToUrl()
\r
621 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
\r
623 * @param string url string for URI
\r
626 public function addTicketToUrl($url)
\r
628 $ticketCode = 'ticket=' . $this->_generateTicket();
\r
629 if ( i18n::strpos($url, '?') === FALSE )
\r
631 $ticketCode = "{$url}?{$ticketCode}";
\r
635 $ticketCode = "{$url}&{$ticketCode}";
\r
637 return $ticketCode;
\r
641 * Manager::addTicketHidden()
\r
642 * POST requests: Adds ticket as hidden formvar
\r
647 public function addTicketHidden()
\r
649 $ticket = $this->_generateTicket();
\r
650 echo '<input type="hidden" name="ticket" value="', Entity::hsc($ticket), '" />';
\r
655 * Manager::getNewTicket()
\r
657 * (xmlHTTPRequest AutoSaveDraft uses this to refresh the ticket)
\r
660 * @return string string of ticket
\r
662 public function getNewTicket()
\r
664 $this->currentRequestTicket = '';
\r
665 return $this->_generateTicket();
\r
669 * Manager::checkTicket()
\r
670 * Checks the ticket that was passed along with the current request
\r
673 * @return boolean correct or not
\r
675 public function checkTicket()
\r
679 // get ticket from request
\r
680 $ticket = requestVar('ticket');
\r
682 // no ticket -> don't allow
\r
683 if ( $ticket == '' )
\r
688 // remove expired tickets first
\r
689 $this->_cleanUpExpiredTickets();
\r
692 if (!$member->isLoggedIn())
\r
698 $memberId = $member->getID();
\r
701 // check if ticket is a valid one
\r
702 $query = sprintf('SELECT COUNT(*) as result FROM %s WHERE member=%d and ticket=%s',
\r
703 sql_table('tickets'),
\r
705 DB::quoteValue($ticket)
\r
710 * [in the original implementation, the checked ticket was deleted. This would lead to invalid
\r
711 * tickets when using the browsers back button and clicking another link/form
\r
712 * leaving the keys in the database is not a real problem, since they're member-specific and
\r
713 * only valid for a period of one hour]
\r
715 if ( DB::getValue($query) != 1 )
\r
724 * Manager::_cleanUpExpiredTickets()
\r
725 * Removes the expired tickets
\r
730 private function _cleanUpExpiredTickets()
\r
732 // remove tickets older than 1 hour
\r
733 $oldTime = time() - 60 * 60;
\r
734 $query = "DELETE FROM %s WHERE ctime < '%s';";
\r
735 $query = sprintf($query, sql_table('tickets'), date('Y-m-d H:i:s',$oldTime));
\r
736 DB::execute($query);
\r
741 * Manager::_generateTicket()
\r
742 * Generates/returns a ticket (one ticket per page request)
\r
747 private function _generateTicket()
\r
749 if ( $this->currentRequestTicket == '' )
\r
751 // generate new ticket (only one ticket will be generated per page request)
\r
752 // and store in database
\r
755 if ( !$member->isLoggedIn() )
\r
761 $memberId = $member->getID();
\r
767 // generate a random token
\r
768 srand((double)microtime()*1000000);
\r
769 $ticket = md5(uniqid(rand(), true));
\r
771 // add in database as non-active
\r
772 $query = "INSERT INTO %s (ticket, member, ctime)"
\r
773 . " VALUES (%s, %d, '%s');";
\r
774 $query = sprintf($query, sql_table('tickets'), DB::quoteValue($ticket), (integer) $memberId, date('Y-m-d H:i:s',time()));
\r
776 if ( DB::execute($query) !== FALSE )
\r
781 $this->currentRequestTicket = $ticket;
\r
783 return $this->currentRequestTicket;
\r