3 // ----------------------------------------------------------------------
4 // POST-NUKE Content Management System
5 // Copyright (C) 2001 by the Post-Nuke Development Team.
6 // http://www.postnuke.com/
7 // ----------------------------------------------------------------------
9 // PHP-NUKE Web Portal System - http://phpnuke.org/
10 // Thatware - http://thatware.org/
11 // ----------------------------------------------------------------------
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License (GPL)
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
24 // To read the license please visit http://www.gnu.org/copyleft/gpl.html
25 // ----------------------------------------------------------------------
26 // Original Author of file: Jim McDonald
27 // Purpose of file: Module variable handling
28 // ----------------------------------------------------------------------
31 * pnModGetVar - get a module variable
32 * Takes two parameters:
33 * - the name of the module
34 * - the name of the variable
36 function pnModGetVar($modname, $name)
38 if ((empty($modname)) ||
(empty($name))) {
43 if (isset($pnmodvar[$modname][$name])) {
44 return $pnmodvar[$modname][$name];
47 list($dbconn) = pnDBGetConn();
48 $pntable = pnDBGetTables();
50 $modulevarstable = $pntable['module_vars'];
51 $modulevarscolumn = &$pntable['module_vars_column'];
52 $query = "SELECT $modulevarscolumn[value]
54 WHERE $modulevarscolumn[modname] = '" . pnVarPrepForStore($modname) . "'
55 AND $modulevarscolumn[name] = '" . pnVarPrepForStore($name) . "'";
56 $result = $dbconn->Execute($query);
58 if ($dbconn->ErrorNo() != 0) {
63 $pnmodvar[$modname][$name] = false;
67 list($value) = $result->fields
;
70 $pnmodvar[$modname][$name] = $value;
75 * pnModSetVar - set a module variable
76 * Takes three parameters:
77 * - the name of the module
78 * - the name of the variable
79 * - the value of the variable
81 function pnModSetVar($modname, $name, $value)
83 if ((empty($modname)) ||
(empty($name))) {
87 list($dbconn) = pnDBGetConn();
88 $pntable = pnDBGetTables();
90 $curvar = pnModGetVar($modname, $name);
92 $modulevarstable = $pntable['module_vars'];
93 $modulevarscolumn = &$pntable['module_vars_column'];
94 if (!isset($curvar)) {
95 $query = "INSERT INTO $modulevarstable
96 ($modulevarscolumn[modname],
97 $modulevarscolumn[name],
98 $modulevarscolumn[value])
100 ('" . pnVarPrepForStore($modname) . "',
101 '" . pnVarPrepForStore($name) . "',
102 '" . pnVarPrepForStore($value) . "');";
104 $query = "UPDATE $modulevarstable
105 SET $modulevarscolumn[value] = '" . pnVarPrepForStore($value) . "'
106 WHERE $modulevarscolumn[modname] = '" . pnVarPrepForStore($modname) . "'
107 AND $modulevarscolumn[name] = '" . pnVarPrepForStore($name) . "'";
110 $dbconn->Execute($query);
112 if ($dbconn->ErrorNo() != 0) {
117 $pnmodvar[$modname][$name] = $value;
123 * pnModDelVar - delete a module variable
124 * Takes two parameters:
125 * - the name of the module
126 * - the name of the variable
128 function pnModDelVar($modname, $name)
130 if ((empty($modname)) ||
(empty($name))) {
134 list($dbconn) = pnDBGetConn();
135 $pntable = pnDBGetTables();
137 $modulevarstable = $pntable['module_vars'];
138 $modulevarscolumn = &$pntable['module_vars_column'];
139 $query = "DELETE FROM $modulevarstable
140 WHERE $modulevarscolumn[modname] = '" . pnVarPrepForStore($modname) . "'
141 AND $modulevarscolumn[name] = '" . pnVarPrepForStore($name) . "'";
142 $dbconn->Execute($query);
144 if ($dbconn->ErrorNo() != 0) {
149 if (isset($pnmodvar[$modname][$name])) {
150 unset($pnmodvar[$modname][$name]);
157 * pnModGetIDFromName - get module ID given its name
158 * Takes one parameter:
159 * - the name of the module
161 function pnModGetIDFromName($module)
163 if (empty($module)) {
167 static $modid = array();
168 if (isset($modid[$module])) {
169 return $modid[$module];
172 list($dbconn) = pnDBGetConn();
173 $pntable = pnDBGetTables();
175 $modulestable = $pntable['modules'];
176 $modulescolumn = &$pntable['modules_column'];
177 $query = "SELECT $modulescolumn[id]
179 WHERE $modulescolumn[name] = '" . pnVarPrepForStore($module) . "'";
180 $result = $dbconn->Execute($query);
182 if ($dbconn->ErrorNo() != 0) {
187 $modid[$module] = false;
191 list($id) = $result->fields
;
194 $modid[$module] = $id;
199 * get information on module
202 * @ return array of module information or false if core ( id = 0 )
204 function pnModGetInfo($modid)
206 // a $modid of 0 is associated with core ( pn_blocks.mid, ... ).
211 static $modinfo = array();
212 if (isset($modinfo[$modid])) {
213 return $modinfo[$modid];
216 list($dbconn) = pnDBGetConn();
217 $pntable = pnDBGetTables();
219 $modulestable = $pntable['modules'];
220 $modulescolumn = &$pntable['modules_column'];
221 $query = "SELECT $modulescolumn[name],
222 $modulescolumn[type],
223 $modulescolumn[directory],
224 $modulescolumn[regid],
225 $modulescolumn[displayname],
226 $modulescolumn[description],
227 $modulescolumn[version]
229 WHERE $modulescolumn[id] = " . pnVarPrepForStore($modid);
230 $result = $dbconn->Execute($query);
232 if ($dbconn->ErrorNo() != 0) {
237 $modinfo[$modid] = false;
241 list($resarray['name'],
243 $resarray['directory'],
245 $resarray['displayname'],
246 $resarray['description'],
247 $resarray['version']) = $result->fields
;
250 $modinfo[$modid] = $resarray;
255 * get list of user modules
257 * @return array of module information arrays
259 function pnModGetUserMods()
261 list($dbconn) = pnDBGetConn();
262 $pntable = pnDBGetTables();
264 $modulestable = $pntable['modules'];
265 $modulescolumn = &$pntable['modules_column'];
266 $query = "SELECT $modulescolumn[name],
267 $modulescolumn[type],
268 $modulescolumn[directory],
269 $modulescolumn[regid],
270 $modulescolumn[displayname],
271 $modulescolumn[description],
272 $modulescolumn[version]
274 WHERE $modulescolumn[state] = " . _PNMODULE_STATE_ACTIVE
. "
275 AND $modulescolumn[user_capable] = 1
276 ORDER BY $modulescolumn[name]";
277 $result = $dbconn->Execute($query);
279 if ($dbconn->ErrorNo() != 0) {
294 $version) = $result->fields
) {
297 $tmparray = array('name' => $name,
299 'directory' => $directory,
301 'displayname' => $displayname,
302 'description' => $description,
303 'version' => $version);
305 array_push($resarray, $tmparray);
314 * get list of administration modules
316 * @return array of module information arrays
318 function pnModGetAdminMods()
320 list($dbconn) = pnDBGetConn();
321 $pntable = pnDBGetTables();
323 $modulestable = $pntable['modules'];
324 $modulescolumn = &$pntable['modules_column'];
326 $query = "SELECT $modulescolumn[name],
327 $modulescolumn[type],
328 $modulescolumn[directory],
329 $modulescolumn[regid],
330 $modulescolumn[displayname],
331 $modulescolumn[description],
332 $modulescolumn[version]
334 WHERE $modulescolumn[state] = " . _PNMODULE_STATE_ACTIVE
. "
335 AND $modulescolumn[admin_capable] = 1
336 AND $modulescolumn[directory] != 'NS-Admin'
337 ORDER BY $modulescolumn[name]";
339 $result = $dbconn->Execute($query);
341 if ($dbconn->ErrorNo() != 0) {
356 $version) = $result->fields
) {
359 $tmparray = array('name' => $name,
361 'directory' => $directory,
363 'displayname' => $displayname,
364 'description' => $description,
365 'version' => $version);
367 array_push($resarray, $tmparray);
376 * load an API for a module
377 * @param modname - registered name of the module
378 * @param type - type of functions to load
380 * @return true on success, false on failure
382 function pnModAPILoad($modname, $type = 'user')
384 static $loaded = array();
386 if (empty($modname)) {
390 list($dbconn) = pnDBGetConn();
391 $pntable = pnDBGetTables();
393 if (!empty($loaded["$modname$type"])) {
394 // Already loaded from somewhere else
398 $modulestable = $pntable['modules'];
399 $modulescolumn = &$pntable['modules_column'];
400 $query = "SELECT $modulescolumn[name],
401 $modulescolumn[directory],
402 $modulescolumn[state]
404 WHERE $modulescolumn[name] = '" . pnVarPrepForStore($modname) . "'";
405 $result = $dbconn->Execute($query);
407 if ($dbconn->ErrorNo() != 0) {
412 pnSessionSetVar('errmsg', "Unknown module $modname");
416 list($name, $directory, $state) = $result->fields
;
419 list($osdirectory, $ostype) = pnVarPrepForOS($directory, $type);
421 $osfile = "modules/$osdirectory/pn{$ostype}api.php";
422 if (!file_exists($osfile)) {
423 // File does not exist
429 $loaded["$modname$type"] = 1;
431 // Load the module language files
432 $currentlang = pnUserGetLang();
433 $defaultlang = pnConfigGetVar('language');
434 if (empty($defaultlang)) {
435 $defaultlang = 'eng';
438 list($oscurrentlang, $osdefaultlang) = pnVarPrepForOS($currentlang, $defaultlang);
439 if (file_exists("modules/$osdirectory/pnlang/$oscurrentlang/{$ostype}api.php")) {
440 include "modules/$osdirectory/pnlang/$oscurrentlang/{$ostype}api.php";
441 } elseif (file_exists("modules/$osdirectory/pnlang/$osdefaultlang/{$ostype}api.php")) {
442 include "modules/$osdirectory/pnlang/$osdefaultlang/{$ostype}api.php";
446 pnModDBInfoLoad($modname, $directory);
452 * load datbase definition for a module
453 * @param name - name of module to load database definition for
454 * @param directory - directory that module is in (if known)
457 function pnModDBInfoLoad($modname, $directory = '')
459 static $loaded = array();
461 // Check to ensure we aren't doing this twice
462 if (isset($loaded[$modname])) {
466 // Get the directory if we don't already have it
467 if (empty($directory)) {
468 list($dbconn) = pnDBGetConn();
469 $pntable = pnDBGetTables();
470 $modulestable = $pntable['modules'];
471 $modulescolumn = &$pntable['modules_column'];
472 $sql = "SELECT $modulescolumn[directory]
474 WHERE $modulescolumn[name] = '" . pnVarPrepForStore($modname) . "'";
475 $result = $dbconn->Execute($sql);
476 if ($dbconn->ErrorNo() != 0) {
484 $directory = $result->fields
[0];
488 // Load the database definition if required
489 $ospntablefile = 'modules/' . pnVarPrepForOS($directory) . '/pntables.php';
490 // Ignore errors for this, if it fails we'll find out and handle
491 // it when we look for the function itself
492 @include_once
$ospntablefile;
493 $tablefunc = $modname . '_' . 'pntables';
494 if (function_exists($tablefunc)) {
496 $pntable = array_merge($pntable, $tablefunc());
499 $loaded[$modname] = true;
506 * @param name - name of module to load
507 * @param type - type of functions to load
509 * @return name of module loaded, or false on failure
511 function pnModLoad($modname, $type = 'user')
513 static $loaded = array();
515 if (empty($modname)) {
519 list($dbconn) = pnDBGetConn();
520 $pntable = pnDBGetTables();
522 $modulestable = $pntable['modules'];
523 $modulescolumn = &$pntable['modules_column'];
525 if (!empty($loaded["$modname$type"])) {
526 // Already loaded from somewhere else
530 $query = "SELECT $modulescolumn[directory],
531 $modulescolumn[state]
533 WHERE $modulescolumn[name] = '" . pnVarPrepForStore($modname) . "'";
534 $result = $dbconn->Execute($query);
536 if ($dbconn->ErrorNo() != 0) {
544 list($directory, $state) = $result->fields
;
547 // Load the module and module language files
548 list($osdirectory, $ostype) = pnVarPrepForOS($directory, $type);
549 $osfile = "modules/$osdirectory/pn$ostype.php";
551 if (!file_exists($osfile)) {
552 // File does not exist
558 $loaded["$modname$type"] = 1;
560 $defaultlang = pnConfigGetVar('language');
561 if (empty($defaultlang)) {
562 $defaultlang = 'eng';
565 $currentlang = pnUserGetLang();
566 if (file_exists("modules/$osdirectory/pnlang/$currentlang/$ostype.php")) {
567 include "modules/$osdirectory/pnlang/" . pnVarPrepForOS($currentlang) . "/$ostype.php";
568 } elseif (file_exists("modules/$directory/pnlang/$defaultlang/$ostype.php")) {
569 include "modules/$osdirectory/pnlang/" . pnVarPrepForOS($defaultlang) . "/$ostype.php";
573 pnModDBInfoLoad($modname, $directory);
575 // Return the module name
580 * run a module API function
581 * @param modname - registered name of module
582 * @param type - type of function to run
583 * @param func - specific function to run
584 * @param args - arguments to pass to the function
587 function pnModAPIFunc($modname, $type = 'user', $func = 'main', $args = array())
590 if (empty($modname)) {
602 // Build function name and call function
603 $modapifunc = "{$modname}_{$type}api_{$func}";
604 if (function_exists($modapifunc)) {
605 return $modapifunc($args);
612 * run a module function
613 * @param modname - registered name of module
614 * @param type - type of function to run
615 * @param func - specific function to run
616 * @param args - argument array
619 function pnModFunc($modname, $type = 'user', $func = 'main', $args = array())
622 if (empty($modname)) {
634 // Build function name and call function
635 $modfunc = "{$modname}_{$type}_{$func}";
636 if (function_exists($modfunc)) {
637 return $modfunc($args);
644 * generate a module function URL
645 * @param modname - registered name of module
646 * @param type - type of function
647 * @param func - module function
648 * @param args - array of arguments to put on the URL
650 * @return absolute URL for call
652 function pnModURL($modname, $type = 'user', $func = 'main', $args = array(), $path = '')
654 if (empty($modname)) {
658 global $HTTP_SERVER_VARS;
661 $host = $HTTP_SERVER_VARS['HTTP_HOST'];
663 $host = getenv('HTTP_HOST');
670 $urlargs[] = "module=$modname";
671 if ((!empty($type)) && ($type != 'user')) {
672 $urlargs[] = "type=$type";
675 if ((!empty($func)) && ($func != 'main')) {
676 $urlargs[] = "func=$func";
679 $urlargs = join('&', $urlargs);
680 $url = "index.php?$urlargs";
683 // <rabbitt> added array check on args
685 if (!is_array($args)) {
688 foreach ($args as $k => $v) {
690 foreach ($v as $l => $w) {
691 $url .= "&$k" . "[$l]=$w";
699 //remove characters not belonging in a path, prevent possible injection
700 //this may break windows path accesses?
701 $path = preg_replace("/[^\.\/a-zA-Z0-9]/", "", $path)
704 $final_url = pnGetBaseURL() . $path . $url;
709 * see if a module is available
711 * @return true if the module is available, false if not
713 function pnModAvailable($modname)
715 if (empty($modname)) {
719 static $modstate = array();
720 if (isset($modstate[$modname])) {
721 if ($modstate[$modname] == _PNMODULE_STATE_ACTIVE
) {
728 list($dbconn) = pnDBGetConn();
729 $pntable = pnDBGetTables();
731 $modulestable = $pntable['modules'];
732 $modulescolumn = &$pntable['modules_column'];
733 $query = "SELECT $modulescolumn[state]
735 WHERE $modulescolumn[name] = '" . pnVarPrepForStore($modname) . "'";
736 $result = $dbconn->Execute($query);
738 if ($dbconn->ErrorNo() != 0) {
743 $modstate[$modname] = _PNMODULE_STATE_MISSING
;
747 list($state) = $result->fields
;
750 $modstate[$modname] = $state;
751 if ($state == _PNMODULE_STATE_ACTIVE
) {
759 * get name of current top-level module
761 * @return the name of the current top-level module, false if not in a module
763 function pnModGetName()
765 $modname = pnVarCleanFromInput('module');
766 if (empty($modname)) {
767 $name = pnVarCleanFromInput('name');
770 if (empty($ModName)) {
774 $modname = preg_replace('/^NS-/', '', $ModName);
780 $modname = preg_replace('/^NS-/', '', $modname);
786 * register a hook function
787 * @param hookobject the hook object
788 * @param hookaction the hook action
789 * @param hookarea the area of the hook (either 'GUI' or 'API')
790 * @param hookmodule name of the hook module
791 * @param hooktype name of the hook type
792 * @param hookfunc name of the hook function
794 function pnModRegisterHook(
805 list($dbconn) = pnDBGetConn();
806 $pntable = pnDBGetTables();
807 $hookstable = $pntable['hooks'];
808 $hookscolumn = &$pntable['hooks_column'];
811 $sql = "INSERT INTO $hookstable (
813 $hookscolumn[object],
814 $hookscolumn[action],
816 $hookscolumn[tmodule],
820 " . pnVarPrepForStore($dbconn->GenId($hookstable)) . ",
821 '" . pnVarPrepForStore($hookobject) . "',
822 '" . pnVarPrepForStore($hookaction) . "',
823 '" . pnVarPrepForStore($hookarea) . "',
824 '" . pnVarPrepForStore($hookmodule) . "',
825 '" . pnVarPrepForStore($hooktype) . "',
826 '" . pnVarPrepForStore($hookfunc) . "')";
827 $dbconn->Execute($sql);
829 if ($dbconn->ErrorNo() != 0) {
837 * unregister a hook function
838 * @param hookobject the hook object
839 * @param hookaction the hook action
840 * @param hookarea the area of the hook (either 'GUI' or 'API')
841 * @param hookmodule name of the hook module
842 * @param hooktype name of the hook type
843 * @param hookfunc name of the hook function
845 function pnModUnregisterHook(
856 list($dbconn) = pnDBGetConn();
857 $pntable = pnDBGetTables();
858 $hookstable = $pntable['hooks'];
859 $hookscolumn = &$pntable['hooks_column'];
862 $sql = "DELETE FROM $hookstable
863 WHERE $hookscolumn[object] = '" . pnVarPrepForStore($hookobject) . "'
864 AND $hookscolumn[action] = '" . pnVarPrepForStore($hookaction) . "'
865 AND $hookscolumn[tarea] = '" . pnVarPrepForStore($hookarea) . "'
866 AND $hookscolumn[tmodule] = '" . pnVarPrepForStore($hookmodule) . "'
867 AND $hookscolumn[ttype] = '" . pnVarPrepForStore($hooktype) . "'
868 AND $hookscolumn[tfunc] = '" . pnVarPrepForStore($hookfunc) . "'";
869 $dbconn->Execute($sql);
871 if ($dbconn->ErrorNo() != 0) {
879 * carry out hook operations for module
880 * @param hookobject the object the hook is called for - either 'item' or 'category'
881 * @param hookaction the action the hook is called for - one of 'create', 'delete', 'transform', or 'display'
882 * @param hookid the id of the object the hook is called for (module-specific)
883 * @param extrainfo extra information for the hook, dependent on hookaction
885 * @return output from hooks
887 function pnModCallHooks($hookobject, $hookaction, $hookid, $extrainfo)
891 list($dbconn) = pnDBGetConn();
892 $pntable = pnDBGetTables();
893 $hookstable = $pntable['hooks'];
894 $hookscolumn = &$pntable['hooks_column'];
896 // Get applicable hooks
897 $sql = "SELECT $hookscolumn[tarea],
898 $hookscolumn[tmodule],
902 WHERE $hookscolumn[smodule] = '" . pnVarPrepForStore(pnModGetName()) . "'
903 AND $hookscolumn[object] = '" . pnVarPrepForStore($hookobject) . "'
904 AND $hookscolumn[action] = '" . pnVarPrepForStore($hookaction) . "'";
905 $result = $dbconn->Execute($sql);
907 if ($dbconn->ErrorNo() != 0) {
914 for (; !$result->EOF
; $result->MoveNext()) {
915 list($hookarea, $hookmodule, $hooktype, $hookfunc) = $result->fields
;
916 if ($hookarea == 'GUI') {
917 if (pnModAvailable($hookmodule, $hooktype) &&
918 pnModLoad($hookmodule, $hooktype)) {
919 $output .= pnModFunc(
923 array('objectid' => $hookid,
924 'extrainfo' => $extrainfo)
928 if (pnModAvailable($hookmodule, $hooktype) &&
929 pnModAPILoad($hookmodule, $hooktype)) {
930 $extrainfo = pnModAPIFunc(
934 array('objectid' => $hookid,
935 'extrainfo' => $extrainfo)
941 if ($hookaction == 'display') {