Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file pnMod.php

Documentation is available at pnMod.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2001, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: pnMod.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @package Zikula_Core
  10.  * @subpackage pnAPI
  11. */
  12.  
  13. /**
  14.  * pnModInitCoreVars
  15.  *
  16.  * preloads module vars for a number of key modules to reduce sql statements
  17.  * @access private
  18.  * @return void 
  19.  */
  20. function pnModInitCoreVars()
  21. {
  22.     global $pnmodvar;
  23.  
  24.     // don't init vars during the installer
  25.     if (defined('_PNINSTALLVER')) {
  26.         return;
  27.     }
  28.  
  29.     // if we haven't got vars for this module yet then lets get them
  30.     if (!isset($pnmodvar)) {
  31.         $pnmodvar array();
  32.         $pntables pnDBGetTables();
  33.         $col      $pntables['module_vars_column'];
  34.         $where "$col[modname]='"PN_CONFIG_MODULE ."' OR $col[modname]='pnRender' OR $col[modname]='Theme' OR $col[modname]='Blocks' OR $col[modname]='Users' OR $col[modname]='Settings' OR $col[modname]='Profile'";
  35.         $pnmodvars DBUtil::selectObjectArray ('module_vars'$where);
  36.         foreach ($pnmodvars as $var{
  37.             $pnmodvar[$var['modname']][$var['name']] @unserialize($var['value']);
  38.         }
  39.     }
  40.  
  41. }
  42.  
  43. /**
  44.  * pnModVarExists - check to see if a module variable is set
  45.  * @author Chris Miller
  46.  * @param 'modname' the name of the module
  47.  * @param 'name' the name of the variable
  48.  * @return  true if the variable exists in the database, false if not
  49.  */
  50. function pnModVarExists($modname$name)
  51. {
  52.     // define input, all numbers and booleans to strings
  53.     $modname = isset($modname((string)$modname'';
  54.     $name = isset($name((string)$name'';
  55.  
  56.     // make sure we have the necessary parameters
  57.     if (!pnVarValidate($modname'mod'|| !pnVarValidate($name'modvar')){
  58.         return false;
  59.     }
  60.  
  61.     // get all module vars for this module
  62.     $modvars pnModGetVar($modname);
  63.  
  64.     // return boolean indicator
  65.     // bp 2008-03-01 checking with isset() will prevent variables with Null values to be found!
  66.     // bp 2008-03-03 we revert to the old logic because some other code can't handle Null variables
  67.     return isset($modvars[$name]);
  68.  
  69. //    if (is_null($modvars[$name]) || isset($modvars[$name])) {
  70. //        return true;
  71. //    }
  72.  
  73. }
  74.  
  75. /**
  76.  * pnModGetVar - get a module variable
  77.  *
  78.  * if the name parameter is included then function returns the
  79.  * module variable value.
  80.  * if the name parameter is ommitted then function returns a multi
  81.  * dimentional array of the keys and values for the module vars.
  82.  *
  83.  * @author Jim McDonald <jim@mcdee.net>
  84.  * @param 'modname' the name of the module
  85.  * @param 'name' the name of the variable
  86.  * @param 'default' the value to return if the requested modvar is not set
  87.  * @return  if the name parameter is included then function returns
  88.  *           string - module variable value
  89.  *           if the name parameter is ommitted then function returns
  90.  *           array - multi dimentional array of the keys
  91.  *                   and values for the module vars.
  92.  */
  93. function pnModGetVar($modname$name=''$default=false)
  94. {
  95.     // if we don't know the modname then lets assume it is the current
  96.     // active module
  97.     if (!isset($modname)) {
  98.         $modname pnModGetName();
  99.     }
  100.  
  101.     global $pnmodvar;
  102.  
  103.     // if we haven't got vars for this module yet then lets get them
  104.     if (!isset($pnmodvar[$modname])) {
  105.         $pntables pnDBGetTables();
  106.         $col      $pntables['module_vars_column'];
  107.         $where    "WHERE $col[modname]='DataUtil::formatForStore($modname"'";
  108.         $sort     ' '// this is not a mistake, it disables the default sort for DBUtil::selectFieldArray()
  109.  
  110.         $results DBUtil::selectFieldArray ('module_vars''value'$where$sortfalse'name');
  111.         foreach($results as $k => $v{      
  112.             // Be carefull to allow check for unserialize of empty values and boolean false
  113.             $pnmodvar[$modname][$k@unserialize($v);
  114.             if ($pnmodvar[$modname][$k=== false  &&  $v != 'b:0;'{
  115.                   // backwards compatibility for non-serialized vars
  116.                   $pnmodvar[$modname][$k$v;
  117.             }
  118.         }
  119.     }
  120.  
  121.     // if they didn't pass a variable name then return every variable
  122.     // for the specified module as an associative array.
  123.     // array('var1'=>value1, 'var2'=>value2)
  124.     if (empty($name&& isset($pnmodvar[$modname])) {
  125.         return $pnmodvar[$modname];
  126.     }
  127.  
  128.     // since they passed a variable name then only return the value for
  129.     // that variable
  130.     // bp 2008-03-01 just checking with isset() will prevent Null values to be returned! (bug #15857)
  131.     // bp 2008-03-03 we revert to the old logic because some other code can't handle Null variables
  132.     if (isset($pnmodvar[$modname][$name])) {
  133. //        if (is_null($pnmodvar[$modname][$name]) || isset($pnmodvar[$modname][$name])) {  
  134.         return $pnmodvar[$modname][$name];
  135.     }
  136.  
  137.     // we don't know the required module var but we established all known
  138.     // module vars for this module so the requested one can't exist.
  139.     // we return the default (which itself defaults to false)
  140.     return $default;
  141. }
  142.  
  143. /**
  144.  * pnModSetVar - set a module variable
  145.  * @author Jim McDonald <jim@mcdee.net>
  146.  * @link http://www.mcdee.net
  147.  * @param 'modname' the name of the module
  148.  * @param 'name' the name of the variable
  149.  * @param 'value' the value of the variable
  150.  * @return bool true if successful, false otherwise
  151.  */
  152. function pnModSetVar($modname$name$value='')
  153. {
  154.     // define input, all numbers and booleans to strings
  155.     $modname = isset($modname((string)$modname'';
  156.  
  157.     // validate
  158.     if (!pnVarValidate($modname'mod'|| !isset($name)) {
  159.         return false;
  160.     }
  161.  
  162.     // bp 2008-03-03 - workaround for bug bug #15857
  163.     // code removed to fix bugs #15939 and #15968
  164.     /*if (isset($name) && is_null($value)) {
  165.         return LogUtil::registerError (pnML('_ERROR_NONULLVALUEALLOWED', array('modname' => $modname, 'varname' => $name)));
  166.     }*/
  167.     
  168.  
  169.     global $pnmodvar;
  170.  
  171.     $obj array();
  172.  
  173.     // This test can be removed after a future 0.8 release - drak
  174.     if (defined('_PNINSTALLVER')) {
  175.         $obj['value'DataUtil::is_serialized($value$value serialize($value);
  176.     else {
  177.         $obj['value'serialize($value);
  178.     }
  179.  
  180.     if (pnModVarExists($modname,$name)) {
  181.         $pntable pnDBGetTables();
  182.         $cols    $pntable['module_vars_column'];
  183.         $where   "WHERE $cols[modname] = 'DataUtil::formatForStore($modname"'
  184.                     AND $cols[name] = 'DataUtil::formatForStore($name."'";
  185.         $res DBUtil::updateObject ($obj'module_vars'$where);
  186.     else {
  187.         $obj['name']    $name;
  188.         $obj['modname'$modname;
  189.         $res DBUtil::insertObject ($obj'module_vars');
  190.     }
  191.  
  192.     if ($res)
  193.         $pnmodvar[$modname][$name$value;
  194.  
  195.     return (bool)$res;
  196. }
  197.  
  198.  
  199. /**
  200.  * pnModSetVars - set multiple module variables
  201.  * @param 'modname' the name of the module
  202.  * @param 'vars' an associative array of varnames/varvalues.
  203.  * @return bool true if successful, false otherwise
  204.  */
  205. function pnModSetVars($modname$vars)
  206. {
  207.   $ok true;
  208.   foreach ($vars as $var => $value)
  209.     $ok $ok && pnModSetVar($modname$var$value);
  210.   return $ok;
  211. }
  212.  
  213.  
  214. /**
  215.  * pnModDelVar
  216.  *
  217.  * Delete a module variables. If the optional name parameter is not supplied all variables
  218.  * for the module 'modname' are deleted
  219.  * @author Jim McDonald <jim@mcdee.net>
  220.  * @link http://www.mcdee.net
  221.  * @param 'modname' the name of the module
  222.  * @param 'name' the name of the variable (optional)
  223.  * @return bool true if successful, false otherwise
  224.  */
  225. function pnModDelVar($modname$name='')
  226. {
  227.     // define input, all numbers and booleans to strings
  228.     $modname = isset($modname((string)$modname'';
  229.  
  230.     // validate
  231.     if (!pnVarValidate($modname'modvar')) {
  232.         return false;
  233.     }
  234.  
  235.     global $pnmodvar;
  236.     $val null;
  237.     if (empty($name)) {
  238.         if (isset($pnmodvar[$modname])) {
  239.             unset($pnmodvar[$modname]);
  240.         }
  241.     else {
  242.         if (isset($pnmodvar[$modname][$name])) {
  243.             $val $pnmodvar[$modname][$name];
  244.             unset($pnmodvar[$modname][$name]);
  245.         }
  246.     }
  247.  
  248.     $pntable pnDBGetTables();
  249.     $cols    $pntable['module_vars_column'];
  250.  
  251.     // check if we're deleting one module var or all module vars
  252.     $specificvar '';
  253.     $name DataUtil::formatForStore($name);
  254.     $modname DataUtil::formatForStore($modname);
  255.     if (!empty($name)) {
  256.         $specificvar " AND $cols[name] = '$name'";
  257.     }
  258.  
  259.     $where "WHERE $cols[modname] = '$modname$specificvar";
  260.     $res   = (bool)DBUtil::deleteWhere ('module_vars'$where);
  261.     return ($val $val $res);
  262. }
  263.  
  264. /**
  265.  * pnModGetIDFromName - get module ID given its name
  266.  * @author Jim McDonald <jim@mcdee.net>
  267.  * @link http://www.mcdee.net
  268.  * @param 'module' the name of the module
  269.  * @return int module ID
  270.  */
  271. function pnModGetIDFromName($module)
  272. {
  273.     // define input, all numbers and booleans to strings
  274.     $module (isset($modulestrtolower((string)$module'');
  275.  
  276.     // validate
  277.     if (!pnVarValidate($module'mod')) {
  278.         return false;
  279.     }
  280.  
  281.     if (substr($module,0,3== 'ns-'{
  282.         $module substr($module,3);
  283.     }
  284.  
  285.     static $modid;
  286.  
  287.     if (!is_array($modid|| defined('_PNINSTALLVER')) {
  288.         $modules pnModGetModsTable();
  289.  
  290.         if ($modules === false{
  291.             return;
  292.         }
  293.  
  294.         foreach ($modules as $mod{
  295.             $mName  strtolower($mod['name']);
  296.             $modid[$mName$mod['id'];
  297.             if (isset($mod['displayname']&& $mod['displayname'])
  298.             {
  299.                 $mdName strtolower($mod['displayname']);
  300.                 $modid[$mdName$mod['id'];
  301.             }
  302.         }
  303.  
  304.         if (!isset($modid[$module])) {
  305.             $modid[$modulefalse;
  306.             return false;
  307.         }
  308.     }
  309.  
  310.     if (isset($modid[$module])) {
  311.         return $modid[$module];
  312.     }
  313.  
  314.     return false;
  315. }
  316.  
  317. /**
  318.  * get information on module
  319.  * return array of module information or false if core ( id = 0 )
  320.  * @author Jim McDonald <jim@mcdee.net>
  321.  * @link http://www.mcdee.net
  322.  * @param 'id' module ID
  323.  * @return mixed module information array or false
  324.  */
  325. function pnModGetInfo($modid=0)
  326. {
  327.     // a $modid of 0 is associated with core ( pn_blocks.mid, ... ).
  328.     if ($modid == || !is_numeric($modid)) {
  329.         return false;
  330.     }
  331.  
  332.     static $modinfo;
  333.  
  334.     if (!is_array($modinfo|| defined('_PNINSTALLVER')) {
  335.         $modinfo pnModGetModsTable();
  336.  
  337.         if (!$modinfo{
  338.             return null;
  339.         }
  340.  
  341.         if (!isset($modinfo[$modid])) {
  342.             $modinfo[$modidfalse;
  343.             return $modinfo[$modid];
  344.         }
  345.     }
  346.  
  347.     if (isset($modinfo[$modid])) {
  348.         return $modinfo[$modid];
  349.     }
  350.  
  351.     return false;
  352. }
  353.  
  354. /**
  355.  * get list of user modules
  356.  * @author Jim McDonald <jim@mcdee.net>
  357.  * @author Robert Gasch
  358.  * @link http://www.mcdee.net
  359.  * @return array array of module information arrays
  360.  */
  361. function pnModGetUserMods()
  362. {
  363.     $mods pnModGetTypeMods('user');
  364.     return $mods;
  365. }
  366.  
  367. /**
  368.  * get list of administration modules
  369.  * @author Jim McDonald <jim@mcdee.net>
  370.  * @author Robert Gasch
  371.  * @link http://www.mcdee.net
  372.  * @return array array of module information arrays
  373.  */
  374. function pnModGetAdminMods()
  375. {
  376.     $mods pnModGetTypeMods('admin');
  377.     return $mods;
  378. }
  379.  
  380. /**
  381.  * get list of modules by module type
  382.  * @author Jim McDonald <jim@mcdee.net>
  383.  * @author Robert Gasch
  384.  * @param 'type' the module type to get (either 'user' or 'admin') (optional) (default='user')
  385.  * @return array array of module information arrays
  386.  */
  387. function pnModGetTypeMods($type='user')
  388. {
  389.     if ($type != 'user' && $type != 'admin')
  390.         $type 'user';
  391.  
  392.     static $modcache array();
  393.  
  394.     if (!isset($modcache[$type]|| !$modcache[$type])
  395.     {
  396.         $modcache[$typearray();
  397.  
  398.         $cap  $type '_capable';
  399.         $mods pnModGetAllMods();
  400.         $ak   array_keys ($mods);
  401.         foreach ($ak as $k{
  402.             if ($mods[$k][$cap]{
  403.                 $modcache[$type][$mods[$k];
  404.             }
  405.         }
  406.     }
  407.  
  408.     return $modcache[$type];
  409. }
  410.  
  411. /**
  412.  * get list of all modules
  413.  * @author Mark West <mark@markwest.me.uk>
  414.  * @link http://www.markwest.me.uk
  415.  * @return array array of module information arrays
  416.  */
  417. function pnModGetAllMods()
  418. {
  419.     static $modsarray array();
  420.  
  421.     if (empty($modsarray)) {
  422.         $pntable pnDBGetTables();
  423.         $modulescolumn $pntable['modules_column'];
  424.         $where "WHERE $modulescolumn[state] = PNMODULE_STATE_ACTIVE "
  425.                   OR $modulescolumn[name] = 'Modules'";
  426.         $orderBy "ORDER BY $modulescolumn[displayname]";
  427.         $modsarray DBUtil::selectObjectArray('modules'$where$orderBy);
  428.         if ($modsarray === false{
  429.             return false;
  430.         }
  431.     }
  432.  
  433.     return $modsarray;
  434. }
  435.  
  436. /**
  437.  * load datbase definition for a module
  438.  * @author Jim McDonald <jim@mcdee.net>
  439.  * @link http://www.mcdee.net
  440.  * @param 'name' the name of the module to load database definition for
  441.  * @param 'directory' directory that module is in (if known)
  442.  * @param 'force' force table information to be reloaded
  443.  * @return bool true if successful, false otherwise
  444.  */
  445. function pnModDBInfoLoad($modname$directory=''$force=false)
  446. {
  447.     // define input, all numbers and booleans to strings
  448.     $modname (isset($modnamestrtolower((string)$modname'');
  449.  
  450.     // default return value
  451.     $data false;
  452.  
  453.     // validate
  454.     if (!pnVarValidate($modname'mod')) {
  455.         return $data;
  456.     }
  457.  
  458.     static $loaded array();
  459.     // Check to ensure we aren't doing this twice
  460.     if (isset($loaded[$modname]&& !$force{
  461.         $result true;
  462.         return $result;
  463.     }
  464.  
  465.     // Get the directory if we don't already have it
  466.     if (empty($directory)) {
  467.         // get the module info
  468.         $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  469.         $directory $modinfo['directory'];
  470.     }
  471.  
  472.     // not required since it's done elsewhere
  473.     //$osDirectory = DataUtil::formatForOS($directory);
  474.  
  475.     // Load the database definition if required
  476.     $files array();
  477.     $files["config/functions/$directory/pntables.php";
  478.     $files["system/$directory/pntables.php";
  479.     $files["modules/$directory/pntables.php";
  480.     Loader::loadOneFile ($files);
  481.  
  482.     $tablefunc $modname '_pntables';
  483.     if (function_exists($tablefunc)) {
  484.         $data $tablefunc();
  485.         // V4B RNG: added casts to ensure proper behaviour under PHP5
  486.         $GLOBALS['pntables'array_merge((array)$GLOBALS['pntables'](array)$data);
  487.     }
  488.     $loaded[$modnametrue;
  489.  
  490.     // V4B RNG: return data so we know which tables were loaded by this module
  491.     return $data;
  492. }
  493.  
  494. /**
  495.  * load a module
  496.  * @author Robert Gasch
  497.  * @param 'name' the name of the module
  498.  * @param 'type' the type of functions to load
  499.  * @param 'force' determines to load Module even if module isn't active
  500.  * @return string name of module loaded, or false on failure
  501.  */
  502. function pnModLoad($modname$type='user'$force=false)
  503. {
  504.     if (strtolower(substr($type-3)) == 'api'{
  505.        return false;
  506.     }
  507.     return pnModLoadGeneric ($modname$type$force);
  508. }
  509.  
  510. /**
  511.  * load an API module
  512.  * @author Robert Gasch
  513.  * @param 'name' the name of the module
  514.  * @param 'type' the type of functions to load
  515.  * @param 'force' determines to load Module even if module isn't active
  516.  * @return string name of module loaded, or false on failure
  517.  */
  518. function pnModAPILoad($modname$type='user'$force=false)
  519. {
  520.     return pnModLoadGeneric ($modname$type$forcetrue);
  521. }
  522.  
  523.  
  524. /**
  525.  * load a module
  526.  * @access private
  527.  * @author Robert Gasch
  528.  * @author Jim McDonald <jim@mcdee.net>
  529.  * @param 'name' the name of the module
  530.  * @param 'type' the type of functions to load
  531.  * @param 'force' determines to load Module even if module isn't active
  532.  * @param 'api' whether or not to load an API (or regular) module
  533.  * @return string name of module loaded, or false on failure
  534.  */
  535. function pnModLoadGeneric($modname$type='user'$force=false$api=false)
  536. {
  537.     // define input, all numbers and booleans to strings
  538.     $osapi   ($api 'api' '');
  539.     $modname = isset($modname((string)$modname'';
  540.     $modtype strtolower("$modname{$type}{$osapi}");
  541.  
  542.     static $loaded array();
  543.     if (!empty($loaded[$modtype])) {
  544.         // Already loaded from somewhere else
  545.         return true;
  546.     }
  547.  
  548.     // check the modules state
  549.     if (!$force && !pnModAvailable($modname&& $modname != 'Modules'{
  550.         return false;
  551.     }
  552.  
  553.     // get the module info
  554.     $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  555.     if (!$modinfo// check for bad pnVarValidate($modname)
  556.         return false;
  557.     }
  558.  
  559.     // create variables for the OS preped version of the directory
  560.     $osdirectory DataUtil::formatForOS($modinfo['directory']);
  561.     $ostype DataUtil::formatForOS($type);
  562.     $mosfile "modules/$osdirectory/pn{$ostype}{$osapi}.php";
  563.     $sosfile "system/$osdirectory/pn{$ostype}{$osapi}.php";
  564.     $mosdir "modules/$osdirectory/pn{$ostype}{$osapi}";
  565.     $sosdir "system/$osdirectory/pn{$ostype}{$osapi}";
  566.  
  567.     if (file_exists($mosfile)) {
  568.         // Load the file from modules
  569.         Loader::includeOnce($mosfile);
  570.     elseif (file_exists($sosfile)) {
  571.         // Load the file from system
  572.         Loader::includeOnce($sosfile);
  573.     elseif (is_dir($mosdir|| is_dir($sosdir)) {
  574.     else {
  575.         // File does not exist
  576.         return false;
  577.     }
  578.     $loaded[$modtype1;
  579.  
  580.     // Load the module language files
  581.     if (!isset($loaded[strtolower($modname)])) {
  582.         pnModLangLoad($modname'common'false);
  583.         $loaded[strtolower($modname)1;
  584.     }
  585.     pnModLangLoad($modname'common');
  586.     pnModLangLoad($modname$type$api);
  587.  
  588.     // Load datbase info
  589.     pnModDBInfoLoad($modname$modinfo['directory']);
  590.  
  591.     return $modname;
  592. }
  593.  
  594. /**
  595.  * run a module function
  596.  * @author Jim McDonald <jim@mcdee.net>
  597.  * @author Robert Gasch
  598.  * @link http://www.mcdee.net
  599.  * @param 'modname' the name of the module
  600.  * @param 'type' the type of function to run
  601.  * @param 'func' the specific function to run
  602.  * @param 'args' the arguments to pass to the function
  603.  * @returns mixed
  604.  */
  605. function pnModFunc($modname$type='user'$func='main'$args=array())
  606. {
  607.     return pnModFuncExec($modname$type$func$args);
  608. }
  609.  
  610. /**
  611.  * run a module API function
  612.  * @author Jim McDonald <jim@mcdee.net>
  613.  * @author Robert Gasch
  614.  * @link http://www.mcdee.net
  615.  * @param 'modname' the name of the module
  616.  * @param 'type' the type of function to run
  617.  * @param 'func' the specific function to run
  618.  * @param 'args' the arguments to pass to the function
  619.  * @returns mixed
  620.  */
  621. function pnModAPIFunc($modname$type='user'$func='main'$args=array())
  622. {
  623.     if (empty($type)) {
  624.         $type 'user';
  625.     elseif (!pnVarValidate($type'api')) {
  626.         return null;
  627.     }
  628.  
  629.     if (empty($func)) {
  630.         $func 'main';
  631.     }
  632.  
  633.     return pnModFuncExec($modname$type$func$argstrue);
  634. }
  635.  
  636. /**
  637.  * run a module function
  638.  * @author Robert Gasch
  639.  * @access private
  640.  * @param 'modname' the name of the module
  641.  * @param 'type' the type of function to run
  642.  * @param 'func' the specific function to run
  643.  * @param 'args' the arguments to pass to the function
  644.  * @param 'api' whether or not to execute an API (or regular) function
  645.  * @returns mixed
  646.  */
  647. function pnModFuncExec ($modname$type='user'$func='main'$args=array()$api=false)
  648. {
  649.     // define input, all numbers and booleans to strings
  650.     $modname  = isset($modname((string)$modname'';
  651.     $ftype    ($api 'api' '');
  652.     $loadfunc ($api 'pnModAPILoad' 'pnModLoad');
  653.  
  654.     // validate
  655.     if (!pnVarValidate($modname'mod')) {
  656.         return null;
  657.     }
  658.  
  659.     $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  660.     $path ($modinfo['type'== '3' 'system' 'modules');
  661.  
  662.     // get the theme
  663.     if ($GLOBALS['loadstages'PN_CORE_THEME{
  664.         $theme ThemeUtil::getInfo(ThemeUtil::getIDFromName(pnUserGetTheme()));
  665.     }
  666.     // Build function name and call function
  667.     $modfunc "{$modname}_{$type}{$ftype}_{$func}";
  668.     if ($loadfunc($modname$type)) {
  669.         if (function_exists($modfunc)) {
  670.             return $modfunc($args);
  671.         else {
  672.             if (($GLOBALS['loadstages'PN_CORE_THEME&& file_exists($file "themes/$theme/functions/$modname/pn{$type}{$ftype}/$func.php")) {
  673.                 Loader::loadFile($file);
  674.                 if (function_exists($modfunc)) {
  675.                     return $modfunc($args);
  676.                 }
  677.             elseif (file_exists($file "config/functions/$modname/pn{$type}{$ftype}/$func.php")) {
  678.                 Loader::loadFile($file);
  679.                 if (function_exists($modfunc)) {
  680.                     return $modfunc($args);
  681.                 }
  682.             elseif (file_exists($file "$path/$modname/pn{$type}{$ftype}/$func.php")) {
  683.                 Loader::loadFile($file);
  684.                 if (function_exists($modfunc)) {
  685.                     return $modfunc($args);
  686.                 }
  687.             }
  688.         }
  689.     }
  690.  
  691.     // if we get here, the function does not exist - show an error and die()
  692.     // to-do: get execptions working for better handling of such errors
  693.     // commented out for the minute since the new url scheme calls a module
  694.     // api that might not exist (to decode module specific urls) - markwest
  695. /*  include_once 'header.php';
  696.     echo DataUtil::formatForDisplayHTML(_UNKNOWNFUNC) . " " . DataUtil::formatForDisplay($modfunc) . "()<br />\n";
  697.     if (SecurityUtil::checkPermission($modname . '.*', '.*', ACCESS_ADMIN)) {
  698.         foreach($args as $key => $value) {
  699.             echo DataUtil::formatForDisplay($key) . " => " . DataUtil::formatForDisplay($value) . "<br />\n";
  700.         }
  701.     }
  702.     include_once 'footer.php';
  703.     pnShutDown();*/
  704. }
  705.  
  706. /**
  707.  * generate a module function URL
  708.  *
  709.  * if the module is non-API compliant (type 1) then
  710.  * a) $func is ignored.
  711.  * b) $type=admin will generate admin.php?module=... and $type=user will generate index.php?name=...
  712.  *
  713.  * @author Jim McDonald <jim@mcdee.net>
  714.  * @link http://www.mcdee.net
  715.  * @param 'modname' the name of the module
  716.  * @param 'type' the type of function to run
  717.  * @param 'func' the specific function to run
  718.  * @param 'args' the array of arguments to put on the URL
  719.  * @param 'ssl'  set to constant null,true,false $ssl = true not $ssl = 'true'  null - leave the current status untouched, true - create a ssl url, false - create a non-ssl url
  720.  * @param 'fragment' the framgment to target within the URL
  721.  * @param 'fqurl' Fully Qualified URL. True to get full URL, eg for Redirect, else gets root-relative path unless SSL
  722.  * @return sting absolute URL for call
  723.  */
  724. function pnModURL($modname$type 'user'$func 'main'$args array()$ssl null$fragment null$fqurl null)
  725. {
  726.     // define input, all numbers and booleans to strings
  727.     $modname = isset($modname((string)$modname'';
  728.  
  729.     // validate
  730.     if (!pnVarValidate($modname'mod')) {
  731.         return null;
  732.     }
  733.  
  734.     //get the module info
  735.     $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  736.  
  737.     // set the module name to the display name if this is present
  738.     if (isset($modinfo['displayname']&& !empty($modinfo['displayname'])) {
  739.         $modname rawurlencode($modinfo['displayname']);
  740.     }
  741.  
  742.     // define some statics as this API is likely to be called many times
  743.     static $entrypoint$host$baseuri$https$shorturls$shorturlstype$shorturlsstripentrypoint;
  744.  
  745.     // entry point
  746.     if (!isset($entrypoint)) {
  747.         $entrypoint pnConfigGetVar('entrypoint');
  748.     }
  749.     // Hostname
  750.     if (!isset($host)) {
  751.         $host pnServerGetVar('HTTP_HOST');
  752.     }
  753.     if (empty($host)) return false;
  754.     // Base URI
  755.     if (!isset($baseuri)) {
  756.         $baseuri pnGetBaseURI();
  757.     }
  758.     // HTTPS Support
  759.     if (!isset($https)) {
  760.         $https pnServerGetVar('HTTPS');
  761.     }
  762.     // use friendly url setup
  763.     if (!isset($shorturls)) {
  764.         $shorturls pnConfigGetVar('shorturls');
  765.     }
  766.     if (!isset($shorturlstype)) {
  767.         $shorturlstype pnConfigGetVar('shorturlstype');
  768.     }
  769.     if (!isset($shorturlsstripentrypoint)) {
  770.         $shorturlsstripentrypoint pnConfigGetVar('shorturlsstripentrypoint');
  771.     }
  772.  
  773.     // Only produce full URL when HTTPS is on or $ssl is set
  774.     $siteRoot '';
  775.     if ((isset($https&& $https == 'on'|| $ssl != null || $fqurl == true{
  776.         $siteRoot 'http'.(($https == 'on' && $ssl !== false|| $ssl === true 's':'').'://'.$host.$baseuri.'/';
  777.     }
  778.  
  779.     // Only convert User URLs. Exclude links that append a theme parameter
  780.     if ($shorturls && $shorturlstype == && $type=='user'{
  781.         if (isset($args['theme'])) {
  782.             $theme $args['theme'];
  783.             unset($args['theme']);
  784.         }
  785.         // Module-specific Short URLs
  786.         $url pnModAPIFunc($modinfo['name']'user''encodeurl'array('modname' => $modname'type' => $type'func' => $func'args' => $args));
  787.         if (empty($url)) {
  788.             // Generic short URLs: [module]/[function]/[param1]/[value1]/[param2]/[value2]
  789.             $vars '';
  790.             foreach ($args as $k => $v{
  791.                 if (is_array($v)) {
  792.                     foreach ($v as $k2 => $w{
  793.                         if ($w != ''{
  794.                             $vars .= "/$k[$k2]/$w"// &$k[$k2]=$w
  795.                         }
  796.                     }
  797.                 elseif ($v != ''{
  798.                     $vars .= "/$k/$v"// &$k=$v
  799.                 }
  800.             }
  801.             $vars substr($vars1);
  802.             if ((!empty($func&& $func != 'main'|| $vars != ''{
  803.                 $func "/$func/";
  804.             else {
  805.                 $func '/';
  806.             }
  807.             $url $modname.$func.$vars;
  808.         }
  809.         if (isset($theme)) {
  810.             $url rawurlencode($theme).'/'.$url;
  811.         }
  812.         if (!$shorturlsstripentrypoint{
  813.             $url "$entrypoint/$url".(!empty($query'?'.$query '');
  814.         else {
  815.             $url "$url".(!empty($query'?'.$query '');
  816.         }
  817.  
  818.     else {
  819.         // Regular URLs
  820.  
  821.         // The arguments
  822.         $urlargs "module=$modname";
  823.         if ((!empty($type)) && ($type != 'user'))
  824.             $urlargs .= "&type=$type";
  825.  
  826.         if ((!empty($func)) && ($func != 'main'))
  827.             $urlargs .= "&func=$func";
  828.  
  829.         $url "$entrypoint?$urlargs";
  830.  
  831.         // <rabbitt> added array check on args
  832.         // April 11, 2003
  833.         if (!is_array($args)) {
  834.             return false;
  835.         else {
  836.             foreach ($args as $k => $v{
  837.                 if (is_array($v)) {
  838.                     foreach($v as $l => $w{
  839.                         $url .= "&$k"[$l]=$w";
  840.                     }
  841.                 else {
  842.                     $url .= "&$k=$v";
  843.                 }
  844.             }
  845.         }
  846.     }
  847.  
  848.     if (isset($fragment)) {
  849.         $url .= '#' $fragment;
  850.     }
  851.  
  852.     return $siteRoot $url;
  853. }
  854.  
  855. /**
  856.  * see if a module is available
  857.  * @author Jim McDonald <jim@mcdee.net>
  858.  * @link http://www.mcdee.net
  859.  * @param 'modname' the name of the module
  860.  * @return bool true if the module is available, false if not
  861.  */
  862. function pnModAvailable($modname null)
  863. {
  864.     // define input, all numbers and booleans to strings
  865.     $modname (isset($modnamestrtolower((string)$modname'');
  866.  
  867.     // validate
  868.     if (!pnVarValidate($modname'mod')) {
  869.         return false;
  870.     }
  871.  
  872.     static $modstate array();
  873.  
  874.     if (!isset($modstate[$modname]))  {
  875.         $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  876.         $modstate[$modname$modinfo['state'];
  877.     }
  878.  
  879.     if ((isset($modstate[$modname]&& $modstate[$modname== PNMODULE_STATE_ACTIVE||
  880.          $modname == 'Modules'{
  881.         return true;
  882.     }
  883.  
  884.     return false;
  885. }
  886.  
  887. /**
  888.  * get name of current top-level module
  889.  * @author Jim McDonald <jim@mcdee.net>
  890.  * @link http://www.mcdee.net
  891.  * @return string the name of the current top-level module, false if not in a module
  892.  */
  893. function pnModGetName()
  894. {
  895.     static $module;
  896.  
  897.     if (!isset($module)) {
  898.         $type FormUtil::getPassedValue('type'null'GETPOST');
  899.         $modname FormUtil::getPassedValue('module'null'GETPOST');
  900.         if (empty($modname|| (!pnModAvailable($modname&& $type <> 'init')) {
  901.             $name FormUtil::getPassedValue('name'null'GETPOST');
  902.             if (empty($name|| (!pnModAvailable($modname&& $type <> 'init')) {
  903.                 // anything from user.php is the user module
  904.                 // not really - of course but it'll do..... [markwest]
  905.                 if (stristr($_SERVER['PHP_SELF']'user.php')) {
  906.                     $ourmod 'Users';
  907.                 else {
  908.                     $modname pnConfigGetVar('startpage');
  909.                     $ourmod $modname;
  910.                 }
  911.             else {
  912.                 $ourmod $name;
  913.             }
  914.         else {
  915.             $ourmod $modname;
  916.         }
  917.  
  918.         // strip any NS- prefixes
  919.         if (substr($ourmod,0,3== 'NS-'{
  920.             $ourmod substr($ourmod,3);
  921.         }
  922.  
  923.         // the parameters may provide the module alias so lets get
  924.         // the real name from the db
  925.         $modinfo pnModGetInfo(pnModGetIDFromName($ourmod));
  926.         $module = isset($modinfo['name']$modinfo['name'$ourmod;
  927.     }
  928.  
  929.     return $module;
  930. }
  931.  
  932. /**
  933.  * register a hook function
  934.  * @author Jim McDonald <jim@mcdee.net>
  935.  * @link http://www.mcdee.net
  936.  * @param 'hookobject' the hook object
  937.  * @param 'hookaction' the hook action
  938.  * @param 'hookarea' the area of the hook (either 'GUI' or 'API')
  939.  * @param 'hookmodule' name of the hook module
  940.  * @param 'hooktype' name of the hook type
  941.  * @param 'hookfunc' name of the hook function
  942.  * @return bool true if successful, false otherwise
  943.  */
  944. function pnModRegisterHook($hookobject$hookaction$hookarea,
  945.                            $hookmodule$hooktype$hookfunc)
  946. {
  947.     // define input, all numbers and booleans to strings
  948.     $hookmodule = isset($hookmodule((string)$hookmodule'';
  949.  
  950.     // validate
  951.     if (!pnVarValidate($hookmodule'mod')) {
  952.         return false;
  953.     }
  954.  
  955.     // Insert hook
  956.     $obj array('object'  => $hookobject,
  957.                  'action'  => $hookaction,
  958.                  'tarea'   => $hookarea,
  959.                  'tmodule' => $hookmodule,
  960.                  'ttype'   => $hooktype,
  961.                  'tfunc'   => $hookfunc);
  962.     return (bool)DBUtil::insertObject($obj'hooks''id');
  963. }
  964.  
  965. /**
  966.  * unregister a hook function
  967.  * @author Jim McDonald <jim@mcdee.net>
  968.  * @link http://www.mcdee.net
  969.  * @param 'hookobject' the hook object
  970.  * @param 'hookaction' the hook action
  971.  * @param 'hookarea' the area of the hook (either 'GUI' or 'API')
  972.  * @param 'hookmodule' name of the hook module
  973.  * @param 'hooktype' name of the hook type
  974.  * @param 'hookfunc' name of the hook function
  975.  * @return bool true if successful, false otherwise
  976.  */
  977. function pnModUnregisterHook($hookobject$hookaction$hookarea,
  978.                              $hookmodule$hooktype$hookfunc)
  979. {
  980.     // define input, all numbers and booleans to strings
  981.     $hookmodule = isset($hookmodule((string)$hookmodule'';
  982.  
  983.     // validate
  984.     if (!pnVarValidate($hookmodule'mod')) {
  985.         return false;
  986.     }
  987.  
  988.     // Get database info
  989.     $pntable pnDBGetTables();
  990.     $hookscolumn $pntable['hooks_column'];
  991.     // Remove hook
  992.     $where "WHERE $hookscolumn[object] = '"DataUtil::formatForStore($hookobject"'
  993.               AND $hookscolumn[action] = '"DataUtil::formatForStore($hookaction"'
  994.               AND $hookscolumn[tarea] = '"DataUtil::formatForStore($hookarea"'
  995.               AND $hookscolumn[tmodule] = '"DataUtil::formatForStore($hookmodule"'
  996.               AND $hookscolumn[ttype] = '"DataUtil::formatForStore($hooktype"'
  997.               AND $hookscolumn[tfunc] = '"DataUtil::formatForStore($hookfunc"'";
  998.  
  999.      return (bool)DBUtil::deleteWhere('hooks'$where);
  1000. }
  1001.  
  1002. /**
  1003.  * carry out hook operations for module
  1004.  * @author Jim McDonald <jim@mcdee.net>
  1005.  * @link http://www.mcdee.net
  1006.  * @param 'hookobject' the object the hook is called for - one of 'item', 'category' or 'module'
  1007.  * @param 'hookaction' the action the hook is called for - one of 'new', 'create', 'modify', 'update', 'delete', 'transform', 'display', 'modifyconfig', 'updateconfig'
  1008.  * @param 'hookid' the id of the object the hook is called for (module-specific)
  1009.  * @param 'extrainfo' extra information for the hook, dependent on hookaction
  1010.  * @param 'implode' implode collapses all display hooks into a single string - default to true for compatability with .7x
  1011.  * @return mixed string output from GUI hooks, extrainfo array for API hooks
  1012.  */
  1013. function pnModCallHooks($hookobject$hookaction$hookid$extrainfo=array()$implode true)
  1014. {
  1015.     static $modulehooks;
  1016.  
  1017.     if (!isset($hookaction)) {
  1018.         return null;
  1019.     }
  1020.  
  1021.     if (isset($extrainfo['module']&&
  1022.         (pnModAvailable($extrainfo['module']|| strtolower($hookobject== 'module')) {
  1023.         $modname $extrainfo['module'];
  1024.     else {
  1025.         $modname pnModGetName();
  1026.     }
  1027.  
  1028.     $lModname strtolower($modname);
  1029.     if (!isset($modulehooks[$lModname])) {
  1030.         // Get database info
  1031.         $pntable pnDBGetTables();
  1032.         $hookscolumn $pntable['hooks_column'];
  1033.         $where "WHERE $hookscolumn[smodule] = 'DataUtil::formatForStore($modname"'";
  1034.         $orderby "$hookscolumn[sequence] ASC";
  1035.         $hooks DBUtil::selectObjectArray('hooks'$where$orderby);
  1036.         $modulehooks[$lModname$hooks;
  1037.     }
  1038.  
  1039.     $gui false;
  1040.     $output array();
  1041.     // Call each hook
  1042.     foreach($modulehooks[$lModnameas $modulehook{
  1043.         if (!isset($extrainfo['tmodule']|| (isset($extrainfo['tmodule']&& $extrainfo['tmodule'== $modulehook['tmodule'])) {
  1044.             if (($modulehook['action'== $hookaction&& ($modulehook['object'== $hookobject)) {
  1045.                 if (isset($modulehook['tarea']&& $modulehook['tarea'== 'GUI'{
  1046.                     $gui true;
  1047.                     if (pnModAvailable($modulehook['tmodule']$modulehook['ttype']&& pnModLoad($modulehook['tmodule']$modulehook['ttype'])) {
  1048.                         $output[$modulehook['tmodule']] pnModFunc($modulehook['tmodule']$modulehook['ttype']$modulehook['tfunc'],
  1049.                                                                     array('objectid' => $hookid,
  1050.                                                                           'extrainfo' => $extrainfo));
  1051.                     }
  1052.                 else {
  1053.                     if (isset($modulehook['tmodule']&& pnModAvailable($modulehook['tmodule']$modulehook['ttype']&& pnModAPILoad($modulehook['tmodule']$modulehook['ttype'])) {
  1054.                         $extrainfo pnModAPIFunc($modulehook['tmodule']$modulehook['ttype']$modulehook['tfunc'],
  1055.                                                   array('objectid' => $hookid,
  1056.                                                         'extrainfo' => $extrainfo));
  1057.                     }
  1058.                 }
  1059.             }
  1060.         }
  1061.     }
  1062.  
  1063.     // check what type of information we need to return
  1064.     $hookaction strtolower($hookaction);
  1065.     if ($gui ||
  1066.         $hookaction == 'display' ||
  1067.         $hookaction == 'new' ||
  1068.         $hookaction == 'modify' ||
  1069.         $hookaction == 'modifyconfig'{
  1070.         if ($implode || empty($output)) {
  1071.             $output implode("\n"$output);
  1072.         }
  1073.         return $output;
  1074.     }
  1075.  
  1076.     return $extrainfo;
  1077. }
  1078.  
  1079. /**
  1080.  * Determine if a module is hooked by another module
  1081.  * @author Mark West (mark@markwest.me.uk)
  1082.  * @link http://www.markwest.me.uk
  1083.  * @param 'tmodule' the target module
  1084.  * @param 'smodule' the source module - default the current top most module
  1085.  * @return bool true if the current module is hooked by the target module, false otherwise
  1086.  */
  1087. function pnModIsHooked($tmodule$smodule)
  1088. {
  1089.     static $hooked array();
  1090.  
  1091.     if (isset($hooked[$tmodule][$smodule])) {
  1092.         return $hooked[$tmodule][$smodule];
  1093.     }
  1094.  
  1095.     // define input, all numbers and booleans to strings
  1096.     $tmodule = isset($tmodule((string)$tmodule'';
  1097.     $smodule = isset($smodule((string)$smodule'';
  1098.  
  1099.     // validate
  1100.     if (!pnVarValidate($tmodule'mod'|| !pnVarValidate($smodule'mod') ) {
  1101.         return false;
  1102.     }
  1103.  
  1104.     // Get database info
  1105.     $pntable pnDBGetTables();
  1106.     $hookscolumn $pntable['hooks_column'];
  1107.  
  1108.     // Get applicable hooks
  1109.     $where "WHERE $hookscolumn[smodule] = 'DataUtil::formatForStore($smodule"'
  1110.               AND $hookscolumn[tmodule] = 'DataUtil::formatForStore($tmodule"'";
  1111.  
  1112.     $hooked[$tmodule][$smodule$numitems DBUtil::selectObjectCount('hooks'$where);
  1113.     $hooked[$tmodule][$smodule($numitems 0);
  1114.  
  1115.     return $hooked[$tmodule][$smodule];
  1116. }
  1117.  
  1118. /**
  1119.  * pnModLangLoad
  1120.  * loads the language files for a module
  1121.  *
  1122.  * @author Mark West
  1123.  * @link http://www.markwest.me.uk
  1124.  * @param modname - name of the module
  1125.  * @param type - type of the language file to load e.g. user, admin
  1126.  * @param api - load api lang file or gui lang file
  1127.  */
  1128. function pnModLangLoad($modname$type 'user'$api false)
  1129. {
  1130.     // define input, all numbers and booleans to strings
  1131.     $modname = isset($modname((string)$modname'';
  1132.  
  1133.     // validate
  1134.     if (!pnVarValidate($modname'mod')) {
  1135.         return false;
  1136.     }
  1137.  
  1138.     // get the module info
  1139.     $modinfo pnModGetInfo(pnModGetIDFromName($modname));
  1140.     if (!$modinfo){
  1141.         return false;
  1142.     }
  1143.  
  1144.     // create variables for the OS preped version of the directory
  1145.     $directory $modinfo['directory'];
  1146.  
  1147.     $osapi '';
  1148.     if ($api{
  1149.         $osapi 'api';
  1150.     }
  1151.  
  1152.     $path ($modinfo['type'== '3' 'system' 'modules');
  1153.  
  1154.     $currentlang pnUserGetLang();
  1155.     $defaultlang pnConfigGetVar('language''eng');
  1156.     $moddir "$path/$directory/pnlang";
  1157.     $osfile  $type $osapi '.php';
  1158.  
  1159.     $files array();
  1160.  
  1161.     // This directory is for easy of use when developing language packs.
  1162.     // See http://community.zikula.org/index.php?module=Wiki&tag=LanguagePack
  1163.     if (isset($GLOBALS['PNConfig']['System']['development']&& $GLOBALS['PNConfig']['System']['development'])
  1164.         $files["config/languages/$currentlang/$moddir/$currentlang/$osfile";
  1165.  
  1166.     $files["config/languages/$currentlang/$directory/$osfile";
  1167.     $files["$moddir/$currentlang/$osfile";
  1168.     $files["config/languages/$defaultlang/$directory/$osfile";
  1169.     $files["$moddir/$defaultlang/$osfile";
  1170.     return Loader::loadOneFile ($files);
  1171. }
  1172.  
  1173. /**
  1174.  * Get the base directory for a module
  1175.  *
  1176.  * Example: If the webroot is located at
  1177.  * /var/www/html
  1178.  * and the module name is Template and is found
  1179.  * in the modules directory then this function
  1180.  * would return /var/www/html/modules/Template
  1181.  *
  1182.  * If the Template module was located in the system
  1183.  * directory then this function would return
  1184.  * /var/www/html/system/Template
  1185.  *
  1186.  * This allows you to say:
  1187.  * include(pnModGetBaseDir() . '/includes/private_functions.php');
  1188.  *
  1189.  * @author Chris Miller
  1190.  * @param   $modname - name of module to that you want the
  1191.  *                      base directory of.
  1192.  * @return  string - the path from the root directory to the
  1193.  *                    specified module.
  1194.  */
  1195. function pnModGetBaseDir($modname='')
  1196. {
  1197.     if (empty($modname)){
  1198.         $modname pnModGetName();
  1199.     }
  1200.  
  1201.     $path pnGetBaseURI();
  1202.     $directory 'system/'.$modname;
  1203.     if ($path!=''){
  1204.         $path.='/';
  1205.     }
  1206.  
  1207.     $url=$path.$directory;
  1208.     if (!is_dir($url)){
  1209.         $directory 'modules/'.$modname;
  1210.         $url=$path.$directory;
  1211.     }
  1212.  
  1213.     return $url;
  1214. }
  1215.  
  1216. /**
  1217.  * gets the modules table
  1218.  *
  1219.  * small wrapper function to avoid duplicate sql
  1220.  * @access private
  1221.  * @return array modules table
  1222. */
  1223. function pnModGetModsTable()
  1224. {
  1225.     static $modstable;
  1226.     if (!isset($modstable|| defined('_PNINSTALLVER')) {
  1227.         $modstable DBUtil::selectObjectArray('modules'''''-1-1'id');
  1228.     }
  1229.     return $modstable;
  1230. }

Documentation generated on Fri, 18 Jul 2008 21:53:59 +0200 by phpDocumentor 1.4.1