3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2005 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
12 * This class makes sure each item/weblog/comment object gets requested from
\r
13 * the database only once, by keeping them in a cache. The class also acts as
\r
14 * a dynamic classloader, loading classes _only_ when they are first needed,
\r
15 * hoping to diminish execution time
\r
17 * The class is a singleton, meaning that there will be only one object of it
\r
18 * active at all times. The object can be requested using MANAGER::instance()
\r
20 * $Id: MANAGER.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $
\r
26 * Cached ITEM, BLOG, PLUGIN and KARMA objects. When these objects are requested
\r
27 * through the global $manager object (getItem, getBlog, ...), only the first call
\r
28 * will create an object. Subsequent calls will return the same object.
\r
30 * The $items, $blogs, ... arrays map an id to an object (for plugins, the name is used
\r
31 * rather than an ID)
\r
40 * cachedInfo to avoid repeated SQL queries (see pidInstalled/pluginInstalled/getPidFromName)
\r
41 * e.g. which plugins exists?
\r
43 * $cachedInfo['installedPlugins'] = array($pid -> $name)
\r
48 * The plugin subscriptionlist
\r
50 * The subcription array has the following structure
\r
51 * $subscriptions[$EventName] = array containing names of plugin classes to be
\r
52 * notified when that event happens
\r
54 var $subscriptions;
\r
57 * Returns the only instance of this class. Creates the instance if it
\r
58 * does not yet exists. Users should use this function as
\r
59 * $manager =& MANAGER::instance(); to get a reference to the object
\r
62 function &instance() {
\r
63 static $instance = '';
\r
64 if ($instance == '')
\r
65 $instance =& new MANAGER();
\r
70 * The constructor of this class initializes the object caches
\r
72 function MANAGER() {
\r
73 $this->items = array();
\r
74 $this->blogs = array();
\r
75 $this->plugins = array();
\r
76 $this->karma = array();
\r
77 $this->parserPrefs = array();
\r
78 $this->cachedInfo = array();
\r
82 * Returns the requested item object. If it is not in the cache, it will
\r
83 * first be loaded and then placed in the cache.
\r
84 * Intended use: $item =& $manager->getItem(1234)
\r
86 function &getItem($itemid, $allowdraft, $allowfuture) {
\r
87 $item =& $this->items[$itemid];
\r
89 // check the draft and future rules if the item was already cached
\r
91 if ((!$allowdraft) && ($item['draft']))
\r
94 $blog =& $this->getBlog(getBlogIDFromItemID($itemid));
\r
95 if ((!$allowfuture) && ($item['timestamp'] > $blog->getCorrectTime()))
\r
99 // load class if needed
\r
100 $this->loadClass('ITEM');
\r
101 // load item object
\r
102 $item = ITEM::getitem($itemid, $allowdraft, $allowfuture);
\r
103 $this->items[$itemid] = $item;
\r
109 * Loads a class if it has not yet been loaded
\r
111 function loadClass($name) {
\r
112 $this->_loadClass($name, $name . '.php');
\r
116 * Checks if an item exists
\r
118 function existsItem($id,$future,$draft) {
\r
119 $this->_loadClass('ITEM','ITEM.php');
\r
120 return ITEM::exists($id,$future,$draft);
\r
124 * Checks if a category exists
\r
126 function existsCategory($id) {
\r
127 return (quickQuery('SELECT COUNT(*) as result FROM '.sql_table('category').' WHERE catid='.intval($id)) > 0);
\r
130 function &getBlog($blogid) {
\r
131 $blog =& $this->blogs[$blogid];
\r
134 // load class if needed
\r
135 $this->_loadClass('BLOG','BLOG.php');
\r
136 // load blog object
\r
137 $blog =& new BLOG($blogid);
\r
138 $this->blogs[$blogid] =& $blog;
\r
143 function existsBlog($name) {
\r
144 $this->_loadClass('BLOG','BLOG.php');
\r
145 return BLOG::exists($name);
\r
148 function existsBlogID($id) {
\r
149 $this->_loadClass('BLOG','BLOG.php');
\r
150 return BLOG::existsID($id);
\r
154 * Returns a previously read template
\r
156 function &getTemplate($templateName) {
\r
157 $template =& $this->templates[$templateName];
\r
160 $template = TEMPLATE::read($templateName);
\r
161 $this->templates[$templateName] =& $template;
\r
167 * Returns a KARMA object (karma votes)
\r
169 function &getKarma($itemid) {
\r
170 $karma =& $this->karma[$itemid];
\r
173 // load class if needed
\r
174 $this->_loadClass('KARMA','KARMA.php');
\r
175 // create KARMA object
\r
176 $karma =& new KARMA($itemid);
\r
177 $this->karma[$itemid] =& $karma;
\r
183 * Global parser preferences
\r
185 function setParserProperty($name, $value) {
\r
186 $this->parserPrefs[$name] = $value;
\r
188 function getParserProperty($name) {
\r
189 return $this->parserPrefs[$name];
\r
193 * A private helper class to load classes
\r
195 function _loadClass($name, $filename) {
\r
196 if (!class_exists($name)) {
\r
198 include($DIR_LIBS . $filename);
\r
202 function _loadPlugin($name) {
\r
203 if (!class_exists($name)) {
\r
204 global $DIR_PLUGINS;
\r
206 $fileName = $DIR_PLUGINS . $name . '.php';
\r
208 if (!file_exists($fileName))
\r
210 ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (File not found)');
\r
215 include($fileName);
\r
217 // check if class exists (avoid errors in eval'd code)
\r
218 if (!class_exists($name))
\r
220 ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (Class not found in file, possible parse error)');
\r
224 // add to plugin array
\r
225 eval('$this->plugins[$name] =& new ' . $name . '();');
\r
228 $this->plugins[$name]->plugid = $this->getPidFromName($name);
\r
230 // unload plugin if a prefix is used and the plugin cannot handle this^
\r
231 global $MYSQL_PREFIX;
\r
232 if (($MYSQL_PREFIX != '') && !$this->plugins[$name]->supportsFeature('SqlTablePrefix'))
\r
234 unset($this->plugins[$name]);
\r
235 ACTIONLOG::add(WARNING, 'Plugin ' . $name . ' was not loaded (does not support SqlTablePrefix)');
\r
239 // call init method
\r
240 $this->plugins[$name]->init();
\r
245 function &getPlugin($name) {
\r
246 $plugin =& $this->plugins[$name];
\r
249 // load class if needed
\r
250 $this->_loadPlugin($name);
\r
251 $plugin =& $this->plugins[$name];
\r
257 * checks if the given plugin IS installed or not
\r
259 function pluginInstalled($name) {
\r
260 $this->_initCacheInfo('installedPlugins');
\r
261 return ($this->getPidFromName($name) != -1);
\r
263 function pidInstalled($pid) {
\r
264 $this->_initCacheInfo('installedPlugins');
\r
265 return ($this->cachedInfo['installedPlugins'][$pid] != '');
\r
267 function getPidFromName($name) {
\r
268 $this->_initCacheInfo('installedPlugins');
\r
269 foreach ($this->cachedInfo['installedPlugins'] as $pid => $pfile)
\r
271 if ($pfile == $name)
\r
276 function clearCachedInfo($what) {
\r
277 unset($this->cachedInfo[$what]);
\r
281 * Loads some info on the first call only
\r
283 function _initCacheInfo($what)
\r
285 if (is_array($this->cachedInfo[$what]))
\r
289 // 'installedPlugins' = array ($pid => $name)
\r
290 case 'installedPlugins':
\r
291 $this->cachedInfo['installedPlugins'] = array();
\r
292 $res = sql_query('SELECT pid, pfile FROM ' . sql_table('plugin'));
\r
293 while ($o = mysql_fetch_object($res))
\r
295 $this->cachedInfo['installedPlugins'][$o->pid] = $o->pfile;
\r
302 * A function to notify plugins that something has happened. Only the plugins
\r
303 * that are subscribed to the event will get notified.
\r
304 * Upon the first call, the list of subscriptions will be fetched from the
\r
305 * database. The plugins itsself will only get loaded when they are first needed
\r
307 * @param $eventName
\r
308 * Name of the event (method to be called on plugins)
\r
310 * Can contain any type of data, depending on the event type. Usually this is
\r
311 * an itemid, blogid, ... but it can also be an array containing multiple values
\r
313 function notify($eventName, $data) {
\r
314 // load subscription list if needed
\r
315 if (!is_array($this->subscriptions))
\r
316 $this->_loadSubscriptions();
\r
319 // get listening objects
\r
320 $listeners = $this->subscriptions[$eventName];
\r
322 // notify all of them
\r
323 if (is_array($listeners)) {
\r
324 foreach($listeners as $listener) {
\r
325 // load class if needed
\r
326 $this->_loadPlugin($listener);
\r
327 // do notify (if method exists)
\r
328 if (method_exists($this->plugins[$listener], 'event_' . $eventName))
\r
329 call_user_func(array(&$this->plugins[$listener],'event_' . $eventName), $data);
\r
336 * Loads plugin subscriptions
\r
338 function _loadSubscriptions() {
\r
339 // initialize as array
\r
340 $this->subscriptions = array();
\r
342 $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');
\r
343 while ($o = mysql_fetch_object($res)) {
\r
344 $pluginName = $o->pfile;
\r
345 $eventName = $o->event;
\r
346 $this->subscriptions[$eventName][] = $pluginName;
\r
352 Ticket functions. These are uses by the admin area to make it impossible to simulate certain GET/POST
\r
353 requests. tickets are user specific
\r
356 var $currentRequestTicket = '';
\r
359 * GET requests: Adds ticket to URL (URL should NOT be html-encoded!, ticket is added at the end)
\r
361 function addTicketToUrl($url)
\r
363 $ticketCode = 'ticket=' . $this->_generateTicket();
\r
364 if (strstr($url, '?'))
\r
365 return $url . '&' . $ticketCode;
\r
367 return $url . '?' . $ticketCode;
\r
371 * POST requests: Adds ticket as hidden formvar
\r
373 function addTicketHidden()
\r
375 $ticket = $this->_generateTicket();
\r
377 echo '<input type="hidden" name="ticket" value="', htmlspecialchars($ticket), '" />';
\r
381 * Checks the ticket that was passed along with the current request
\r
383 function checkTicket()
\r
387 // get ticket from request
\r
388 $ticket = requestVar('ticket');
\r
390 // no ticket -> don't allow
\r
394 // remove expired tickets first
\r
395 $this->_cleanUpExpiredTickets();
\r
398 if (!$member->isLoggedIn())
\r
401 $memberId = $member->getID();
\r
403 // check if ticket is a valid one
\r
404 $query = 'SELECT COUNT(*) as result FROM ' . sql_table('tickets') . ' WHERE member=' . intval($memberId). ' and ticket=\''.addslashes($ticket).'\'';
\r
405 if (quickQuery($query) == 1)
\r
407 // [in the original implementation, the checked ticket was deleted. This would lead to invalid
\r
408 // tickets when using the browsers back button and clicking another link/form
\r
409 // leaving the keys in the database is not a real problem, since they're member-specific and
\r
410 // only valid for a period of one hour
\r
412 // sql_query('DELETE FROM '.sql_table('tickets').' WHERE member=' . intval($memberId). ' and ticket=\''.addslashes($ticket).'\'');
\r
415 // not a valid ticket
\r
422 * (internal method) Removes the expired tickets
\r
424 function _cleanUpExpiredTickets()
\r
426 // remove tickets older than 1 hour
\r
427 $oldTime = time() - 60 * 60;
\r
428 $query = 'DELETE FROM ' . sql_table('tickets'). ' WHERE ctime < \'' . date('Y-m-d H:i:s',$oldTime) .'\'';
\r
433 * (internal method) Generates/returns a ticket (one ticket per page request)
\r
435 function _generateTicket()
\r
437 if ($this->currentRequestTicket == '')
\r
439 // generate new ticket (only one ticket will be generated per page request)
\r
440 // and store in database
\r
443 if (!$member->isLoggedIn())
\r
446 $memberId = $member->getID();
\r
451 // generate a random token
\r
452 srand((double)microtime()*1000000);
\r
453 $ticket = md5(uniqid(rand(), true));
\r
455 // add in database as non-active
\r
456 $query = 'INSERT INTO ' . sql_table('tickets') . ' (ticket, member, ctime) ';
\r
457 $query .= 'VALUES (\'' . addslashes($ticket). '\', \'' . intval($memberId). '\', \'' . date('Y-m-d H:i:s',time()) . '\')';
\r
458 if (sql_query($query))
\r
462 $this->currentRequestTicket = $ticket;
\r
464 return $this->currentRequestTicket;
\r