2 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
3 * Copyright (C) 2002-2005 The Nucleus Group
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
9 * (see nucleus/documentation/index.html#license for more info)
\r
11 * This is an (abstract) class of which all Nucleus Plugins must inherit
\r
13 * for more information on plugins and how to write your own, see the
\r
14 * plugins.html file that is included with the Nucleus documenation
\r
16 * $Id: PLUGIN.php,v 1.3 2005-03-16 08:10:35 kimitake Exp $
\r
17 * $NucleusJP: PLUGIN.php,v 1.3 2005/03/12 06:19:05 kimitake Exp $
\r
19 class NucleusPlugin {
\r
21 // these functions _have_ to be redefined in your plugin
\r
23 function getName() { return 'Undefined'; }
\r
24 function getAuthor() { return 'Undefined'; }
\r
25 function getURL() { return 'Undefined'; }
\r
26 function getVersion() { return '0.0'; }
\r
27 function getDescription() { return 'Undefined';}
\r
29 // these function _may_ be redefined in your plugin
\r
31 function getMinNucleusVersion() { return 150; }
\r
32 function getMinNucleusPatchLevel() { return 0; }
\r
33 function getEventList() { return array(); }
\r
34 function getTableList() { return array(); }
\r
35 function hasAdminArea() { return 0; }
\r
37 function install() {}
\r
38 function unInstall() {}
\r
42 function doSkinVar($skinType) {}
\r
43 function doTemplateVar(&$item) {
\r
44 $args = func_get_args();
\r
46 array_unshift($args, 'template');
\r
47 call_user_func_array(array(&$this,'doSkinVar'),$args);
\r
49 function doTemplateCommentsVar(&$item, &$comment) {
\r
50 $args = func_get_args();
\r
53 array_unshift($args, 'template');
\r
54 call_user_func_array(array(&$this,'doSkinVar'),$args);
\r
56 function doAction($type) { return 'No Such Action'; }
\r
59 * Checks if a plugin supports a certain feature.
\r
61 * @returns 1 if the feature is reported, 0 if not
\r
63 * Name of the feature. See plugin documentation for more info
\r
64 * 'SqlTablePrefix' -> if the plugin uses the sql_table() method to get table names
\r
65 * 'HelpPage' -> if the plugin provides a helppage
\r
67 function supportsFeature($feature) {
\r
72 * Report a list of plugin that is required to function
\r
74 * @returns an array of names of plugin, an empty array indicates no dependency
\r
76 function getPluginDep() { return array(); }
\r
78 // these helper functions should not be redefined in your plugin
\r
81 * Creates a new option for this plugin
\r
84 * A string uniquely identifying your option. (max. length is 20 characters)
\r
85 * @param description
\r
86 * A description that will show up in the nucleus admin area (max. length: 255 characters)
\r
88 * Either 'text', 'yesno' or 'password'
\r
89 * This info is used when showing 'edit plugin options' screens
\r
91 * Initial value for the option (max. value length is 128 characters)
\r
93 function createOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
\r
94 return $this->_createOption('global', $name, $desc, $type, $defValue, $typeExtras);
\r
96 function createBlogOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
\r
97 return $this->_createOption('blog', $name, $desc, $type, $defValue, $typeExtras);
\r
99 function createMemberOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
\r
100 return $this->_createOption('member', $name, $desc, $type, $defValue, $typeExtras);
\r
102 function createCategoryOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
\r
103 return $this->_createOption('category', $name, $desc, $type, $defValue, $typeExtras);
\r
105 function createItemOption($name, $desc, $type, $defValue = '', $typeExtras = '') {
\r
106 return $this->_createOption('item', $name, $desc, $type, $defValue, $typeExtras);
\r
110 * Removes the option from the database
\r
112 * Note: Options get erased automatically on plugin uninstall
\r
114 function deleteOption($name) {
\r
115 return $this->_deleteOption('global', $name);
\r
117 function deleteBlogOption($name) {
\r
118 return $this->_deleteOption('blog', $name);
\r
120 function deleteMemberOption($name) {
\r
121 return $this->_deleteOption('member', $name);
\r
123 function deleteCategoryOption($name) {
\r
124 return $this->_deleteOption('category', $name);
\r
126 function deleteItemOption($name) {
\r
127 return $this->_deleteOption('item', $name);
\r
131 * Sets the value of an option to something new
\r
133 function setOption($name, $value) {
\r
134 return $this->_setOption('global', 0, $name, $value);
\r
136 function setBlogOption($blogid, $name, $value) {
\r
137 return $this->_setOption('blog', $blogid, $name, $value);
\r
139 function setMemberOption($memberid, $name, $value) {
\r
140 return $this->_setOption('member', $memberid, $name, $value);
\r
142 function setCategoryOption($catid, $name, $value) {
\r
143 return $this->_setOption('category', $catid, $name, $value);
\r
145 function setItemOption($itemid, $name, $value) {
\r
146 return $this->_setOption('item', $itemid, $name, $value);
\r
150 * Retrieves the current value for an option
\r
152 function getOption($name)
\r
154 // only request the options the very first time. On subsequent requests
\r
155 // the static collection is used to save SQL queries.
\r
156 if ($this->plugin_options == 0)
\r
158 $this->plugin_options = array();
\r
159 $query = mysql_query(
\r
160 'SELECT d.oname as name, o.ovalue as value '.
\r
162 sql_table('plugin_option').' o, '.
\r
163 sql_table('plugin_option_desc').' d '.
\r
164 'WHERE d.opid='. intval($this->getID()).' AND d.oid=o.oid'
\r
166 while ($row = mysql_fetch_object($query))
\r
167 $this->plugin_options[strtolower($row->name)] = $row->value;
\r
169 if (isset($this->plugin_options[strtolower($name)]))
\r
170 return $this->plugin_options[strtolower($name)];
\r
172 return $this->_getOption('global', 0, $name);
\r
175 function getBlogOption($blogid, $name) {
\r
176 return $this->_getOption('blog', $blogid, $name);
\r
178 function getMemberOption($memberid, $name) {
\r
179 return $this->_getOption('member', $memberid, $name);
\r
181 function getCategoryOption($catid, $name) {
\r
182 return $this->_getOption('category', $catid, $name);
\r
184 function getItemOption($itemid, $name) {
\r
185 return $this->_getOption('item', $itemid, $name);
\r
189 * Retrieves an associative array with the option value for each
\r
192 function getAllBlogOptions($name) {
\r
193 return $this->_getAllOptions('blog', $name);
\r
195 function getAllMemberOptions($name) {
\r
196 return $this->_getAllOptions('member', $name);
\r
198 function getAllCategoryOptions($name) {
\r
199 return $this->_getAllOptions('category', $name);
\r
201 function getAllItemOptions($name) {
\r
202 return $this->_getAllOptions('item', $name);
\r
206 * Retrieves an indexed array with the top (or bottom) of an option
\r
207 * (delegates to _getOptionTop())
\r
209 function getBlogOptionTop($name, $amount = 10, $sort = 'desc') {
\r
210 return $this->_getOptionTop('blog', $name, $amount, $sort);
\r
212 function getMemberOptionTop($name, $amount = 10, $sort = 'desc') {
\r
213 return $this->_getOptionTop('member', $name, $amount, $sort);
\r
215 function getCategoryOptionTop($name, $amount = 10, $sort = 'desc') {
\r
216 return $this->_getOptionTop('category', $name, $amount, $sort);
\r
218 function getItemOptionTop($name, $amount = 10, $sort = 'desc') {
\r
219 return $this->_getOptionTop('item', $name, $amount, $sort);
\r
223 * Retrieves an array of the top (or bottom) of an option from a plugin.
\r
225 * @param string $context the context for the option: item, blog, member,...
\r
226 * @param string $name the name of the option
\r
227 * @param int $amount how many rows must be returned
\r
228 * @param string $sort desc or asc
\r
229 * @return array array with both values and contextid's
\r
232 function _getOptionTop($context, $name, $amount = 10, $sort = 'desc') {
\r
233 if (($sort != 'desc') && ($sort != 'asc')) {
\r
237 $oid = $this->_getOID($context, $name);
\r
239 // retrieve the data and return
\r
240 $q = 'SELECT otype, oextra FROM '.sql_table('plugin_option_desc').' WHERE oid = '.$oid;
\r
241 $query = mysql_query($q);
\r
243 $o = mysql_fetch_array($query);
\r
245 if (($this->optionCanBeNumeric($o['otype'])) && ($o['oextra'] == 'number' )) {
\r
246 $orderby = 'CAST(ovalue AS SIGNED)';
\r
248 $orderby = 'ovalue';
\r
250 $q = 'SELECT ovalue value, ocontextid id FROM '.sql_table('plugin_option').' WHERE oid = '.$oid.' ORDER BY '.$orderby.' '.$sort.' LIMIT 0,'.$amount;
\r
251 $query = mysql_query($q);
\r
253 // create the array
\r
256 while($row = mysql_fetch_array($query)) {
\r
260 // return the array (duh!)
\r
265 * Returns the plugin ID
\r
268 return $this->plugid;
\r
272 * returns the URL of the admin area for this plugin (in case there's
\r
273 * no such area, the returned information is invalid)
\r
275 function getAdminURL() {
\r
277 return $CONF['PluginURL'] . $this->getShortName() . '/';
\r
281 * Returns the directory where the admin directory is located and
\r
282 * where the plugin can maintain his extra files
\r
284 function getDirectory() {
\r
285 global $DIR_PLUGINS;
\r
286 return $DIR_PLUGINS . $this->getShortName() . '/';
\r
290 * Derives the short name for the plugin from the classname (all lowercase)
\r
292 function getShortName() {
\r
293 return str_replace('np_','',strtolower(get_class($this)));
\r
296 var $_aOptionValues; // oid_contextid => value
\r
297 var $_aOptionToInfo; // context_name => array('oid' => ..., 'default' => ...)
\r
298 var $plugin_options; // see getOption()
\r
299 var $plugid; // plugin id
\r
302 // constructor. Initializes some internal data
\r
303 function NucleusPlugin() {
\r
304 $this->_aOptionValues = array(); // oid_contextid => value
\r
305 $this->_aOptionToInfo = array(); // context_name => array('oid' => ..., 'default' => ...)
\r
306 $this->plugin_options = 0;
\r
310 function _createOption($context, $name, $desc, $type, $defValue, $typeExtras = '') {
\r
311 // create in plugin_option_desc
\r
312 $query = 'INSERT INTO ' . sql_table('plugin_option_desc')
\r
313 .' (opid, oname, ocontext, odesc, otype, odef, oextra)'
\r
314 .' VALUES ('.intval($this->plugid)
\r
315 .', \''.addslashes($name).'\''
\r
316 .', \''.addslashes($context).'\''
\r
317 .', \''.addslashes($desc).'\''
\r
318 .', \''.addslashes($type).'\''
\r
319 .', \''.addslashes($defValue).'\''
\r
320 .', \''.addslashes($typeExtras).'\')';
\r
322 $oid = mysql_insert_id();
\r
324 $key = $context . '_' . $name;
\r
325 $this->_aOptionToInfo[$key] = array('oid' => $oid, 'default' => $defValue);
\r
331 function _deleteOption($context, $name) {
\r
332 $oid = $this->_getOID($context, $name);
\r
333 if (!$oid) return 0; // no such option
\r
335 // delete all things from plugin_option
\r
336 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
\r
338 // delete entry from plugin_option_desc
\r
339 sql_query('DELETE FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . $oid);
\r
341 // clear from cache
\r
342 unset($this->_aOptionToInfo[$context . '_' . $name]);
\r
343 $this->_aOptionValues = array();
\r
349 * returns: 1 on success, 0 on failure
\r
351 function _setOption($context, $contextid, $name, $value) {
\r
354 $oid = $this->_getOID($context, $name);
\r
355 if (!$oid) return 0;
\r
357 // check if context id exists
\r
358 switch ($context) {
\r
360 if (!MEMBER::existsID($contextid)) return 0;
\r
363 if (!$manager->existsBlogID($contextid)) return 0;
\r
366 if (!$manager->existsCategory($contextid)) return 0;
\r
369 if (!$manager->existsItem($contextid, true, true)) return 0;
\r
372 if ($contextid != 0) return 0;
\r
377 // update plugin_option
\r
378 sql_query('DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid) . ' and ocontextid='. intval($contextid));
\r
379 sql_query('INSERT INTO ' . sql_table('plugin_option') . ' (ovalue, oid, ocontextid) VALUES (\''.addslashes($value).'\', '. intval($oid) . ', ' . intval($contextid) . ')');
\r
382 $this->_aOptionValues[$oid . '_' . $contextid] = $value;
\r
388 function _getOption($context, $contextid, $name) {
\r
389 $oid = $this->_getOID($context, $name);
\r
390 if (!$oid) return '';
\r
393 $key = $oid . '_' . $contextid;
\r
395 if (isset($this->_aOptionValues[$key]))
\r
396 return $this->_aOptionValues[$key];
\r
399 $res = sql_query('SELECT ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid='.intval($oid).' and ocontextid=' . intval($contextid));
\r
401 if (!$res || (mysql_num_rows($res) == 0)) {
\r
402 $defVal = $this->_getDefVal($context, $name);
\r
403 $this->_aOptionValues[$key] = $defVal;
\r
405 // fill DB with default value
\r
406 $query = 'INSERT INTO ' . sql_table('plugin_option') . ' (oid,ocontextid,ovalue)'
\r
407 .' VALUES ('.intval($oid).', '.intval($contextid).', \''.addslashes($defVal).'\')';
\r
411 $o = mysql_fetch_object($res);
\r
412 $this->_aOptionValues[$key] = $o->ovalue;
\r
415 return $this->_aOptionValues[$key];
\r
419 * Returns assoc array with all values for a given option (one option per
\r
420 * possible context id)
\r
422 function _getAllOptions($context, $name) {
\r
423 $oid = $this->_getOID($context, $name);
\r
424 if (!$oid) return array();
\r
425 $defVal = $this->_getDefVal($context, $name);
\r
427 $aOptions = array();
\r
428 switch ($context) {
\r
430 $r = sql_query('SELECT bnumber as contextid FROM ' . sql_table('blog'));
\r
433 $r = sql_query('SELECT catid as contextid FROM ' . sql_table('category'));
\r
436 $r = sql_query('SELECT mnumber as contextid FROM ' . sql_table('member'));
\r
440 while ($o = mysql_fetch_object($r))
\r
441 $aOptions[$o->contextid] = $defVal;
\r
444 $res = sql_query('SELECT ocontextid, ovalue FROM ' . sql_table('plugin_option') . ' WHERE oid=' . $oid);
\r
445 while ($o = mysql_fetch_object($res))
\r
446 $aOptions[$o->ocontextid] = $o->ovalue;
\r
452 * Gets the 'option identifier' that corresponds to a given option name.
\r
453 * When this method is called for the first time, all the OIDs for the plugin
\r
454 * are loaded into memory, to avoid re-doing the same query all over.
\r
456 function _getOID($context, $name) {
\r
457 $key = $context . '_' . $name;
\r
458 $info = $this->_aOptionToInfo[$key];
\r
459 if (is_array($info)) return $info['oid'];
\r
461 // load all OIDs for this plugin from the database
\r
462 $this->_aOptionToInfo = array();
\r
463 $query = 'SELECT oid, oname, ocontext, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE opid=' . intval($this->plugid);
\r
464 $res = sql_query($query);
\r
465 while ($o = mysql_fetch_object($res)) {
\r
466 $k = $o->ocontext . '_' . $o->oname;
\r
467 $this->_aOptionToInfo[$k] = array('oid' => $o->oid, 'default' => $o->odef);
\r
469 mysql_free_result($res);
\r
471 return $this->_aOptionToInfo[$key]['oid'];
\r
473 function _getDefVal($context, $name) {
\r
474 $key = $context . '_' . $name;
\r
475 $info = $this->_aOptionToInfo[$key];
\r
476 if (is_array($info)) return $info['default'];
\r
481 * Deletes all option values for a given context and contextid
\r
482 * (used when e.g. a blog, member or category is deleted)
\r
486 function _deleteOptionValues($context, $contextid) {
\r
487 // delete all associated plugin options
\r
490 $query = 'SELECT oid FROM '.sql_table('plugin_option_desc') . ' WHERE ocontext=\''.addslashes($context).'\'';
\r
491 $res = sql_query($query);
\r
492 while ($o = mysql_fetch_object($res))
\r
493 array_push($aOIDs, $o->oid);
\r
494 mysql_free_result($res);
\r
495 // delete those options. go go go
\r
496 if (count($aOIDs) > 0) {
\r
497 $query = 'DELETE FROM ' . sql_table('plugin_option') . ' WHERE oid in ('.implode(',',$aOIDs).') and ocontextid=' . intval($contextid);
\r
503 * splits the option's typeextra field (at ;'s) to split the meta collection
\r
504 * @param string $typeExtra the value of the typeExtra field of an option
\r
505 * @return array array of the meta-key/value-pairs
\r
509 function getOptionMeta($typeExtra) {
\r
510 $tmpMeta = explode(';', $typeExtra);
\r
512 for ($i = 0; $i < count($tmpMeta); $i++) {
\r
513 if (($i == 0) && (!strstr($tmpMeta[0], '='))) {
\r
514 // we have the select-list
\r
515 $meta['select'] = $tmpMeta[0];
\r
517 $tmp = explode('=', $tmpMeta[$i]);
\r
518 $meta[$tmp[0]] = $tmp[1];
\r
525 * filters the selectlists out of the meta collection
\r
526 * @param string $typeExtra the value of the typeExtra field of an option
\r
527 * @return string the selectlist
\r
530 function getOptionSelectValues($typeExtra) {
\r
531 $meta = NucleusPlugin::getOptionMeta($typeExtra);
\r
532 //the select list must always be the first part
\r
533 return $meta['select'];
\r
537 * checks if the eventlist in the database is up-to-date
\r
538 * @return bool if it is up-to-date it return true, else false
\r
541 function subscribtionListIsUptodate() {
\r
542 $res = sql_query('SELECT event FROM '.sql_table('plugin_event').' WHERE pid = '.$this->getID());
\r
544 while($a = mysql_fetch_array($res)) {
\r
545 array_push($ev, $a['event']);
\r
547 if (count($ev) != count($this->getEventList())) {
\r
550 $d = array_diff($ev, $this->getEventList());
\r
551 if (count($d) > 0) {
\r
552 // there are differences so the db is not up-to-date
\r
559 * @param $aOptions: array ( 'oid' => array( 'contextid' => 'value'))
\r
560 * (taken from request using requestVar())
\r
561 * @param $newContextid: integer (accepts a contextid when it is for a new
\r
562 * contextid there was no id available at the moment of writing the
\r
563 * formcontrols into the page (by ex: itemOptions for new item)
\r
566 function _applyPluginOptions(&$aOptions, $newContextid = 0) {
\r
568 if (!is_array($aOptions)) return;
\r
570 foreach ($aOptions as $oid => $values) {
\r
572 // get option type info
\r
573 $query = 'SELECT opid, oname, ocontext, otype, oextra, odef FROM ' . sql_table('plugin_option_desc') . ' WHERE oid=' . intval($oid);
\r
574 $res = sql_query($query);
\r
575 if ($o = mysql_fetch_object($res))
\r
577 foreach ($values as $contextid => $value) {
\r
578 // retreive any metadata
\r
579 $meta = NucleusPlugin::getOptionMeta($o->oextra);
\r
581 // if the option is readonly or hidden it may not be saved
\r
582 if (($meta['access'] != 'readonly') && ($meta['access'] != 'hidden')) {
\r
584 $value = undoMagic($value); // value comes from request
\r
586 switch($o->otype) {
\r
588 if (($value != 'yes') && ($value != 'no')) $value = 'no';
\r
594 // check the validity of numerical options
\r
595 if (($meta['datatype'] == 'numerical') && (!is_numeric($value))) {
\r
596 //the option must be numeric, but the it isn't
\r
597 //use the default for this option
\r
601 // decide wether we are using the contextid of newContextid
\r
602 if ($newContextid != 0) {
\r
603 $contextid = $newContextid;
\r
606 //trigger event PrePluginOptionsUpdate to give the plugin the
\r
607 //possibility to change/validate the new value for the option
\r
608 $manager->notify('PrePluginOptionsUpdate',array('context' => $o->ocontext, 'plugid' => $o->opid, 'optionname' => $o->oname, 'contextid' => $contextid, 'value' => &$value));
\r
610 // delete the old value for the option
\r
611 sql_query('DELETE FROM '.sql_table('plugin_option').' WHERE oid='.intval($oid).' AND ocontextid='.intval($contextid));
\r
612 sql_query('INSERT INTO '.sql_table('plugin_option')." (oid, ocontextid, ovalue) VALUES (".intval($oid).",".intval($contextid).",'" . addslashes($value) . "')");
\r