PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/core/web/includes/modules.inc.php

https://github.com/vmasilva/mmc
PHP | 367 lines | 224 code | 47 blank | 96 comment | 45 complexity | 4c87ad7edc361a589fca2d5e52391b87 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * (c) 2004-2007 Linbox / Free&ALter Soft, http://linbox.com
  4. * (c) 2007-2008 Mandriva, http://www.mandriva.com
  5. *
  6. * $Id$
  7. *
  8. * This file is part of Mandriva Management Console (MMC).
  9. *
  10. * MMC is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * MMC is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with MMC; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. ?>
  25. <?php
  26. define('__ROOT__', dirname(dirname(__FILE__)));
  27. require_once(__ROOT__."/site.php");
  28. require_once("ModulesGenerator.php");
  29. /**
  30. * return an array of all subdirs in a dir
  31. *
  32. * @param $dir directory usually "modules/"
  33. * @return an array of String with a list of dirs
  34. */
  35. function fetchModulesList($dir) {
  36. $ret = array();
  37. $registerList = array();
  38. if (isset($_SESSION["supportModList"])) {
  39. foreach($_SESSION["supportModList"] as $module) {
  40. if (file_exists($dir.$module.'/infoPackage.inc.php') &&
  41. hasCorrectModuleAcl($module)) {
  42. $ret[] = $dir.$module;
  43. $registerList[] = $module;
  44. }
  45. }
  46. sort($ret);
  47. sort($registerList);
  48. }
  49. /* register modulesList in $_SESSION */
  50. $_SESSION["modulesList"] = $registerList;
  51. return $ret;
  52. }
  53. /**
  54. *
  55. * Read main ini file
  56. *
  57. */
  58. function fetchIniFile() {
  59. global $conf;
  60. $INI = __sysconfdir__."/mmc/mmc.ini";
  61. $conf = array();
  62. if (is_readable($INI)) {
  63. $conf = array_merge_recursive(parse_ini_file($INI, TRUE),$conf);
  64. } else {
  65. print "MMC: Can't read $INI configuration file. Please check your installation.";
  66. exit();
  67. }
  68. /* If password is obfusctated, decode it */
  69. if (isset($conf["global"]["password"])) {
  70. $value = $conf["global"]["password"];
  71. /* We only support base64 encoded password */
  72. if (preg_match('/^{base64}(.+)$/', $value, $matches)) {
  73. $value = base64_decode($matches[1]);
  74. if ($value === False) {
  75. print "MMC: Can't decode base64 encoded password. Please check your installation.";
  76. exit();
  77. }
  78. $conf["global"]["password"] = $value;
  79. }
  80. }
  81. /* Put the quantities proposed in the selector for the maxperpage value in
  82. an array */
  83. if (isset($conf["global"]["pagination"])) {
  84. $conf["global"]["pagination"] = explode(' ', $conf["global"]["pagination"]);
  85. } else {
  86. /* Default values */
  87. $conf["global"]["pagination"] = array(10, 20, 50, 100);
  88. }
  89. if (!isset($conf["global"]["community"])) {
  90. $conf["global"]["community"] = True;
  91. }
  92. /* Set default option for MMC agent access */
  93. foreach ($conf as $key => $value) {
  94. if (strstr($key, "server_")) {
  95. if (!isset($conf[$key]["verifypeer"])) {
  96. $conf[$key]["verifypeer"] = 0;
  97. $conf[$key]["cacert"] = "";
  98. $conf[$key]["localcert"] = "";
  99. }
  100. if (!isset($conf[$key]["cacert"])) {
  101. $conf[$key]["cacert"] = "";
  102. }
  103. if (!isset($conf[$key]["localcert"])) {
  104. $conf[$key]["localcert"] = "";
  105. }
  106. /* Set default socket I/O timeout to 5 minutes */
  107. if (!isset($conf[$key]["timeout"])) {
  108. $conf[$key]["timeout"] = 300;
  109. }
  110. if (($conf[$key]["verifypeer"] == 1)
  111. && ($conf[$key]["cacert"] == "")
  112. && ($conf[$key]["localcert"] == "")) {
  113. printf("The entry %s is missing the cacert and localcert options.", $key);
  114. exit;
  115. }
  116. if (isset($conf[$key]["forgotPassword"]) && $conf[$key]["forgotPassword"])
  117. $conf[$key]["forgotPassword"] = true;
  118. else
  119. $conf[$key]["forgotPassword"] = false;
  120. }
  121. }
  122. }
  123. /**
  124. * include infoPackage.inc.php from directory
  125. * @param $dirA directory array returned by fetchModulesList
  126. */
  127. function includeInfoPackage($dirA) {
  128. foreach ($dirA as $path) {
  129. if (file_exists("$path/infoPackage.inc.php")) {
  130. require_once("$path/infoPackage.inc.php");
  131. }
  132. }
  133. $MMCApp =& MMCApp::getInstance();
  134. $MMCApp->process();
  135. }
  136. function insert_without_delete($arr,$ind,$value) {
  137. if (isset($arr[$ind])) {
  138. return insert_without_delete($arr,$ind+1,$value);
  139. } else {
  140. $arr[$ind] = $value;
  141. return $arr;
  142. }
  143. }
  144. function getSorted($objlist) {
  145. $prio = array();
  146. foreach ($objlist as $obj) {
  147. $prio = insert_without_delete($prio,$obj->getPriority(),$obj);
  148. }
  149. ksort($prio);
  150. return $prio;
  151. }
  152. /**
  153. * Build and Display the top navigation bar
  154. * The top navigation bar is made of sub-modules icons.
  155. */
  156. function autoGenerateNavbar() {
  157. $MMCApp =& MMCApp::getInstance();
  158. $prio = array();
  159. foreach ($MMCApp->getModules() as $mod) {
  160. foreach ($mod->getSubmodules() as $submod) {
  161. $add = False;
  162. foreach($submod->getPages() as $page) {
  163. $add = $page->hasAccessAndVisible($mod, $submod);
  164. if ($add) break;
  165. }
  166. if ($add) $prio = insert_without_delete($prio,$submod->getPriority(),$submod);
  167. }
  168. }
  169. ksort($prio);
  170. foreach ($prio as $submod) {
  171. $submod->generateNavbar();
  172. }
  173. }
  174. /**
  175. * include publicFunc.php
  176. * @param $dirA directory array returned by fetchModulesList
  177. */
  178. function includePublicFunc($dirA) {
  179. /* We use a global variable to check if the file has not been already included */
  180. if (!isset($GLOBALS["included"])) $GLOBALS["included"] = array();
  181. foreach ($dirA as $path) {
  182. $path = str_replace("//", "/", $path);
  183. if (file_exists("$path/includes/publicFunc.php") && ! in_array("$path/includes/publicFunc.php", $GLOBALS["included"])) {
  184. array_push($GLOBALS["included"], "$path/includes/publicFunc.php");
  185. include("$path/includes/publicFunc.php");
  186. }
  187. }
  188. }
  189. /**
  190. * include localCss from directory /!\ deprecated
  191. * @param $dirA directory array returned by fetchModulesList
  192. */
  193. function includeLocalCss($dirA) {
  194. foreach ($dirA as $path) {
  195. if (file_exists("$path/localCss.php")) {
  196. include("$path/localCss.php");
  197. print"\n";
  198. }
  199. }
  200. }
  201. /**
  202. * @param $modules modules like base or samba
  203. * @param $submod sub modules like "users"
  204. * @param $action action like index or add
  205. * @return if action require noheader send
  206. */
  207. function isNoHeader($pModules,$pSubmod,$pAction) {
  208. $MMCApp =&MMCApp::getInstance();
  209. $mod = $MMCApp->getModule($pModules);
  210. $submodo = $mod->_submod[$pSubmod];
  211. $actiono = $submodo->_pages[$pAction];
  212. return ($actiono->_options["noHeader"]||$actiono->_options["AJAX"]);
  213. }
  214. /**
  215. * Call the given function in all available plugins
  216. * @param $function The function name to execute
  217. * @param $paramArr The array of parameters for the function
  218. * @param $reverse If true, the plugins are considered in reverse priority
  219. *
  220. * This function will try to call all _$plugins_$functionName function
  221. * The plugins are looked up according to their priority, and in reverse order if $reverse = True
  222. */
  223. function callPluginFunction($function, $paramArr = null, $reverse = False) {
  224. /* Fetch and order available plugins for the current logged user */
  225. $list = $_SESSION["modulesList"];
  226. if ($reverse) $list = array_reverse($list);
  227. /*
  228. If the user try to change his/her password, we do it for each available
  229. module, and we bypass all ACL check
  230. */
  231. if (($function == "changeUserPasswd") || ($function == "baseEdit")) {
  232. /* Change password for all modules, even those where the user has no right. */
  233. $list = $_SESSION["supportModList"];
  234. global $conf;
  235. foreach($list as $module) {
  236. if (!function_exists("_" . $module . "_" . "changeUserPasswd"))
  237. includePublicFunc(array($conf["global"]["rootfsmodules"] . "/$module"));
  238. }
  239. }
  240. $result = array();
  241. foreach($list as $item) {
  242. $functionName = "_" . $item . "_" . $function;
  243. if (function_exists($functionName)) {
  244. $result[$item] = call_user_func_array($functionName, $paramArr);
  245. if (isXMLRPCError()) {
  246. /* Break the loop if there is an error */
  247. global $errorDesc;
  248. $result[$item] = $errorDesc;
  249. break;
  250. }
  251. }
  252. }
  253. return $result;
  254. }
  255. /**
  256. * render template view in template corresponding module
  257. * ex: module samba $view = add
  258. * render /modules/samba/views/add.tpl.php
  259. */
  260. function renderTPL($view,$module = null) {
  261. if (!$module) {
  262. $module = $_GET["module"];
  263. }
  264. $template = "modules/$module/views/$view.tpl.php";
  265. if (file_exists($template)) {
  266. global $__TPLref;
  267. extract($__TPLref);
  268. //print_r($__TPLref);
  269. //print_r($users);
  270. require($template);
  271. } else {
  272. echo "<b>template render error Cannot find file \"$template\"</b>";
  273. }
  274. }
  275. /**
  276. * some functions for template, render etc...
  277. */
  278. /**
  279. * array of value for template
  280. */
  281. $__TPLref = array();
  282. function setVar($name, $value) {
  283. global $__TPLref;
  284. $__TPLref[$name]=$value;
  285. }
  286. function getVar($name) {
  287. global $__TPLref;
  288. return $__TPLref[$name];
  289. }
  290. function eT($name) {
  291. global $__TPLref;
  292. echo $__TPLref[$name];
  293. }
  294. function redirectTo($url) {
  295. header('Location: '.$url);
  296. }
  297. /**
  298. * list possible locale
  299. */
  300. function list_system_locales($dir){
  301. /* Save current locale */
  302. $current = setlocale(LC_ALL, 0);
  303. $ret = array();
  304. $ret[] = "C";
  305. if (is_dir($dir)) {
  306. if ($dh = opendir($dir)) {
  307. while (($file = readdir($dh)) !== false) {
  308. if ((file_exists("$dir/$file/LC_MESSAGES/base.mo")) && (setlocale(LC_ALL, $file . ".UTF-8"))) {
  309. $ret[]=$file;
  310. }
  311. }
  312. }
  313. closedir($dh);
  314. }
  315. $_SESSION['__locale'] = $ret;
  316. /* Restore current locale */
  317. setlocale(LC_ALL, $current);
  318. return $_SESSION['__locale'];
  319. }
  320. ?>