PageRenderTime 34ms CodeModel.GetById 44ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/autoload.function.php

https://gitlab.com/OnBlox/OnBlox-Template
PHP | 385 lines | 349 code | 2 blank | 34 comment | 1 complexity | 8a3d250475a42a320823b6dc02353402 MD5 | raw file
  1. <?php
  2. /*
  3. * @version $Id$
  4. -------------------------------------------------------------------------
  5. GLPI - Gestionnaire Libre de Parc Informatique
  6. Copyright (C) 2015 Teclib'.
  7. http://glpi-project.org
  8. based on GLPI - Gestionnaire Libre de Parc Informatique
  9. Copyright (C) 2003-2014 by the INDEPNET Development Team.
  10. -------------------------------------------------------------------------
  11. LICENSE
  12. This file is part of GLPI.
  13. GLPI is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17. GLPI is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with GLPI. If not, see <http://www.gnu.org/licenses/>.
  23. --------------------------------------------------------------------------
  24. */
  25. /** @file
  26. * @brief
  27. */
  28. if (!defined('GLPI_ROOT')) {
  29. die("Sorry. You can't access directly to this file");
  30. }
  31. include_once (GLPI_ROOT."/config/based_config.php");
  32. include_once (GLPI_ROOT."/config/define.php");
  33. /**
  34. * Is the script launch in Command line ?
  35. *
  36. * @return boolean
  37. **/
  38. function isCommandLine() {
  39. return (!isset($_SERVER["SERVER_NAME"]));
  40. }
  41. /**
  42. * Determine if an object name is a plugin one
  43. *
  44. * @param $classname class name to analyze
  45. *
  46. * @return false or an object containing plugin name and class name
  47. **/
  48. function isPluginItemType($classname) {
  49. if (preg_match("/Plugin([A-Z][a-z0-9]+)([A-Z]\w+)/",$classname,$matches)) {
  50. $plug = array();
  51. $plug['plugin'] = $matches[1];
  52. $plug['class'] = $matches[2];
  53. return $plug;
  54. }
  55. // Standard case
  56. return false;
  57. }
  58. /// Translation functions
  59. /// since version 0.84
  60. /**
  61. * For translation
  62. *
  63. * @param $str string
  64. * @param $domain string domain used (default is glpi, may be plugin name)
  65. *
  66. * @return translated string
  67. **/
  68. function __($str, $domain='glpi') {
  69. global $TRANSLATE;
  70. if (is_null($TRANSLATE)) { // before login
  71. return $str;
  72. }
  73. $trans = $TRANSLATE->translate($str, $domain);
  74. // Wrong call when plural defined
  75. if (is_array($trans)) {
  76. return $trans[0];
  77. }
  78. return $trans;
  79. }
  80. /**
  81. * For translation
  82. *
  83. * @param $str string
  84. * @param $domain string domain used (default is glpi, may be plugin name)
  85. *
  86. * @return protected string (with htmlentities)
  87. **/
  88. function __s($str, $domain='glpi') {
  89. return htmlentities(__($str, $domain), ENT_QUOTES, 'UTF-8');
  90. }
  91. /**
  92. * For translation
  93. *
  94. * @since version 0.84
  95. *
  96. * @param $ctx string context
  97. * @param $str string to translate
  98. * @param $domain string domain used (default is glpi, may be plugin name)
  99. *
  100. * @return protected string (with htmlentities)
  101. **/
  102. function _sx($ctx, $str, $domain='glpi') {
  103. return htmlentities(_x($ctx, $str, $domain), ENT_QUOTES, 'UTF-8');
  104. }
  105. /**
  106. * to delete echo in translation
  107. *
  108. * @param $str string
  109. * @param $domain string domain used (default is glpi, may be plugin name)
  110. *
  111. * @return echo string
  112. **/
  113. function _e($str, $domain='glpi') {
  114. echo __($str, $domain);
  115. }
  116. /**
  117. * For translation
  118. *
  119. * @param $sing string in singular
  120. * @param $plural string in plural
  121. * @param $nb to select singular or plurial
  122. * @param $domain string domain used (default is glpi, may be plugin name)
  123. *
  124. * @return translated string
  125. **/
  126. function _n($sing, $plural, $nb, $domain='glpi') {
  127. global $TRANSLATE;
  128. return $TRANSLATE->translatePlural($sing, $plural, $nb, $domain);
  129. }
  130. /**
  131. * For translation
  132. *
  133. * @since version 0.84
  134. *
  135. * @param $sing string in singular
  136. * @param $plural string in plural
  137. * @param $nb to select singular or plurial
  138. * @param $domain string domain used (default is glpi, may be plugin name)
  139. *
  140. * @return protected string (with htmlentities)
  141. **/
  142. function _sn($sing, $plural, $nb, $domain='glpi') {
  143. global $TRANSLATE;
  144. return htmlentities(_n($sing, $plural, $nb, $domain), ENT_QUOTES, 'UTF-8');
  145. }
  146. /**
  147. * For context in translation
  148. *
  149. * @param $ctx string context
  150. * @param $str string to translate
  151. * @param $domain string domain used (default is glpi, may be plugin name)
  152. *
  153. * @return string
  154. **/
  155. function _x($ctx, $str, $domain='glpi') {
  156. // simulate pgettext
  157. $msg = $ctx."\004".$str;
  158. $trans = __($msg, $domain);
  159. if ($trans == $msg) {
  160. // No translation
  161. return $str;
  162. }
  163. return $trans;
  164. }
  165. /**
  166. * Echo for context in translation
  167. *
  168. * @param $ctx string context
  169. * @param $str string to translated
  170. * @param $domain string domain used (default is glpi, may be plugin name)
  171. *
  172. * @return string
  173. **/
  174. function _ex($ctx, $str, $domain='glpi') {
  175. // simulate pgettext
  176. $msg = $ctx."\004".$str;
  177. $trans = __($msg, $domain);
  178. if ($trans == $msg) {
  179. // No translation
  180. echo $str;
  181. }
  182. echo $trans;
  183. }
  184. /**
  185. * For context in plural translation
  186. *
  187. * @param $ctx string context
  188. * @param $sing string in singular
  189. * @param $plural string in plural
  190. * @param $nb to select singular or plurial
  191. * @param $domain string domain used (default is glpi, may be plugin name)
  192. *
  193. * @return string
  194. **/
  195. function _nx($ctx, $sing, $plural, $nb, $domain='glpi') {
  196. // simulate pgettext
  197. $singmsg = $ctx."\004".$sing;
  198. $pluralmsg = $ctx."\004".$plural;
  199. $trans = _n($singmsg, $pluralmsg, $nb, $domain);
  200. if ($trans == $singmsg) {
  201. // No translation
  202. return $sing;
  203. }
  204. if ($trans == $pluralmsg) {
  205. // No translation
  206. return $plural;
  207. }
  208. return $trans;
  209. }
  210. /**
  211. * To load classes
  212. *
  213. * @param $classname : class to load
  214. **/
  215. function glpi_autoload($classname) {
  216. global $DEBUG_AUTOLOAD, $CFG_GLPI;
  217. static $notfound = array('xStates' => true,
  218. 'xAllAssets' => true, );
  219. // empty classname or non concerted plugin or classname containing dot (leaving GLPI main treee)
  220. if (empty($classname) || is_numeric($classname) || (strpos($classname, '.') !== false)) {
  221. die("Security die. trying to load an forbidden class name");
  222. }
  223. $dir = GLPI_ROOT . "/inc/";
  224. if ($plug = isPluginItemType($classname)) {
  225. $plugname = strtolower($plug['plugin']);
  226. $dir = GLPI_ROOT . "/plugins/$plugname/inc/";
  227. $item = strtolower($plug['class']);
  228. // Is the plugin activate ?
  229. // Command line usage of GLPI : need to do a real check plugin activation
  230. if (isCommandLine()) {
  231. $plugin = new Plugin();
  232. if (count($plugin->find("directory='$plugname' AND state=".Plugin::ACTIVATED)) == 0) {
  233. // Plugin does not exists or not activated
  234. return false;
  235. }
  236. } else {
  237. // Standard use of GLPI
  238. if (!in_array($plugname,$_SESSION['glpi_plugins'])) {
  239. // Plugin not activated
  240. return false;
  241. }
  242. }
  243. } else {
  244. // Is ezComponent class ?
  245. if (preg_match('/^ezc([A-Z][a-z]+)/',$classname,$matches)) {
  246. include_once(GLPI_EZC_BASE);
  247. ezcBase::autoload($classname);
  248. return true;
  249. }
  250. // Do not try to load phpcas using GLPI autoload
  251. if (preg_match('/^CAS_.*/', $classname)) {
  252. return false;
  253. }
  254. // Do not try to load Zend using GLPI autoload
  255. if (preg_match('/^Zend.*/', $classname)) {
  256. return false;
  257. }
  258. // Do not try to load Simplepie using GLPI autoload
  259. if (preg_match('/^SimplePie.*/', $classname)) {
  260. return false;
  261. }
  262. $item = strtolower($classname);
  263. }
  264. if (file_exists("$dir$item.class.php")) {
  265. include_once("$dir$item.class.php");
  266. if (isset($_SESSION['glpi_use_mode'])
  267. && ($_SESSION['glpi_use_mode'] == Session::DEBUG_MODE)) {
  268. $DEBUG_AUTOLOAD[] = $classname;
  269. }
  270. } else if (!isset($notfound["x$classname"])) {
  271. // trigger an error to get a backtrace, but only once (use prefix 'x' to handle empty case)
  272. // trigger_error("GLPI autoload : file $dir$item.class.php not founded trying to load class '$classname'");
  273. $notfound["x$classname"] = true;
  274. }
  275. }
  276. // Use spl autoload to allow stackable autoload.
  277. spl_autoload_register('glpi_autoload');
  278. require_once (GLPI_ZEND_PATH . '/Loader/StandardAutoloader.php');
  279. $option = array(Zend\Loader\StandardAutoloader::LOAD_NS => array('Zend' => GLPI_ZEND_PATH));
  280. $loader = new Zend\Loader\StandardAutoloader($option);
  281. $loader->register();
  282. // SimplePie autoloader
  283. spl_autoload_register(array(new SimplePie_Autoloader(), 'autoload'));
  284. /**
  285. * Autoloader class
  286. *
  287. * @since version 0.84
  288. *
  289. * From package SimplePie
  290. **/
  291. class SimplePie_Autoloader {
  292. /**
  293. * Constructor
  294. **/
  295. public function __construct() {
  296. $this->path = GLPI_SIMPLEPIE_PATH;
  297. }
  298. /**
  299. * Autoloader
  300. *
  301. * @param string $class The name of the class to attempt to load.
  302. **/
  303. public function autoload($class) {
  304. // empty classname or non concerted plugin or classname containing dot (leaving GLPI main treee)
  305. if (empty($class) || is_numeric($class) || (strpos($class, '.') !== false)) {
  306. return false;
  307. }
  308. // Only load the class if it starts with "SimplePie"
  309. if (strpos($class, 'SimplePie') !== 0) {
  310. return;
  311. }
  312. $filename = $this->path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $class) .
  313. '.php';
  314. require_once($filename);
  315. }
  316. }
  317. ?>