PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

389 lines
12 KiB

  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at :
  10. * http://www.postfixadmin.com or http://postfixadmin.sf.net
  11. *
  12. * @version $Id: common.php 733 2009-10-20 19:25:20Z christian_boltz $
  13. * @license GNU GPL v2 or later.
  14. *
  15. * File: common.php
  16. * All pages should include this file - which itself sets up the necessary
  17. * environment and ensures other functions are loaded.
  18. */
  19. $incpath = PATH;
  20. (ini_get('magic_quotes_gpc') ? ini_set('magic_quotes_runtime', '0') : '1');
  21. (ini_get('magic_quotes_gpc') ? ini_set('magic_quotes_sybase', '0') : '1');
  22. if(ini_get('register_globals') == 'on') {
  23. die("Please turn off register_globals; edit your php.ini");
  24. }
  25. require_once("$incpath/variables.inc.php");
  26. /*
  27. if(!is_file("$incpath/config.inc.php")) {
  28. die("config.inc.php is missing!");
  29. }
  30. require_once("$incpath/config.inc.php");
  31. */
  32. if(isset($CONF['configured'])) {
  33. if($CONF['configured'] == FALSE) {
  34. die("Please edit config.inc.php - change \$CONF['configured'] to true after setting your database settings");
  35. }
  36. }
  37. /*
  38. require_once("$incpath/languages/language.php");
  39. require_once("$incpath/functions.inc.php");
  40. require_once("$incpath/languages/en.lang");
  41. */
  42. /**
  43. * @param string $class
  44. * __autoload implementation, for use with spl_autoload_register().
  45. */
  46. function postfixadmin_autoload2($class) {
  47. $PATH = CORE_INCLUDE_PATH.'/../model/' . $class . '.php';
  48. if(is_file($PATH)) {
  49. require_once($PATH);
  50. return true;
  51. }
  52. return false;
  53. }
  54. spl_autoload_register('postfixadmin_autoload2');
  55. /**
  56. * Convenience method for strtolower().
  57. *
  58. * @param string $str String to lowercase
  59. * @return string Lowercased string
  60. */
  61. function low($str) {
  62. return strtolower($str);
  63. }
  64. /**
  65. * Convenience method for strtoupper().
  66. *
  67. * @param string $str String to uppercase
  68. * @return string Uppercased string
  69. */
  70. function up($str) {
  71. return strtoupper($str);
  72. }
  73. /**
  74. * Convenience method for str_replace().
  75. *
  76. * @param string $search String to be replaced
  77. * @param string $replace String to insert
  78. * @param string $subject String to search
  79. * @return string Replaced string
  80. */
  81. function r($search, $replace, $subject) {
  82. return str_replace($search, $replace, $subject);
  83. }
  84. /**
  85. * Print_r convenience function, which prints out <PRE> tags around
  86. * the output of given array. Similar to debug().
  87. *
  88. * @see debug()
  89. * @param array $var Variable to print out
  90. * @param boolean $showFrom If set to true, the method prints from where the function was called
  91. */
  92. function pr($var) {
  93. if (Configure::read() > 0) {
  94. echo "<pre>";
  95. print_r($var);
  96. echo "</pre>";
  97. }
  98. }
  99. class Config {
  100. /**
  101. * Determine if $__objects cache should be wrote
  102. *
  103. * @var boolean
  104. * @access private
  105. */
  106. var $__cache = false;
  107. /**
  108. * Holds and key => value array of objects type
  109. *
  110. * @var array
  111. * @access private
  112. */
  113. var $__objects = array();
  114. /**
  115. * Return a singleton instance of Configure.
  116. *
  117. * @return Configure instance
  118. * @access public
  119. */
  120. function &getInstance() {
  121. static $instance = array();
  122. if (!$instance) {
  123. $instance[0] = new Config();
  124. //$instance[0]->__loadBootstrap($boot);
  125. }
  126. return $instance[0];
  127. }
  128. /**
  129. * Used to write a dynamic var in the Configure instance.
  130. *
  131. * Usage
  132. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  133. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  134. * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
  135. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
  136. *
  137. * @param array $config Name of var to write
  138. * @param mixed $value Value to set for var
  139. * @return void
  140. * @access public
  141. */
  142. function write($config, $value = null) {
  143. $_this =& Config::getInstance();
  144. if (!is_array($config)) {
  145. $config = array($config => $value);
  146. }
  147. foreach ($config as $names => $value) {
  148. $name = $_this->__configVarNames($names);
  149. switch (count($name)) {
  150. case 3:
  151. $_this->{$name[0]}[$name[1]][$name[2]] = $value;
  152. break;
  153. case 2:
  154. $_this->{$name[0]}[$name[1]] = $value;
  155. break;
  156. case 1:
  157. $_this->{$name[0]} = $value;
  158. break;
  159. }
  160. }
  161. }
  162. /**
  163. * Used to read Configure::$var
  164. *
  165. * Usage
  166. * Configure::read('Name'); will return all values for Name
  167. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  168. *
  169. * @param string $var Variable to obtain
  170. * @return string value of Configure::$var
  171. * @access public
  172. */
  173. function read($var) {
  174. $_this =& Config::getInstance();
  175. if ($var === 'all') {
  176. $return = array();
  177. foreach ($_this AS $key =>$var) {
  178. $return[$key] = $var;
  179. }
  180. return $return;
  181. }
  182. $name = $_this->__configVarNames($var);
  183. switch (count($name)) {
  184. case 3:
  185. if (isset($_this->{$name[0]}[$name[1]][$name[2]])) {
  186. return $_this->{$name[0]}[$name[1]][$name[2]];
  187. }
  188. break;
  189. case 2:
  190. if (isset($_this->{$name[0]}[$name[1]])) {
  191. return $_this->{$name[0]}[$name[1]];
  192. }
  193. break;
  194. case 1:
  195. if (isset($_this->{$name[0]})) {
  196. return $_this->{$name[0]};
  197. }
  198. break;
  199. }
  200. return null;
  201. }
  202. function getAll() {
  203. $output = $this->config;
  204. return $output;
  205. }
  206. /**
  207. * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
  208. *
  209. * @param mixed $name Name to split
  210. * @return array Name separated in items through dot notation
  211. * @access private
  212. */
  213. function __configVarNames($name) {
  214. if (is_string($name)) {
  215. if (strpos($name, ".")) {
  216. return explode(".", $name);
  217. }
  218. return array($name);
  219. }
  220. return $name;
  221. }
  222. }
  223. class Lang {
  224. /**
  225. * Determine if $__objects cache should be wrote
  226. *
  227. * @var boolean
  228. * @access private
  229. */
  230. var $__cache = false;
  231. /**
  232. * Holds and key => value array of objects type
  233. *
  234. * @var array
  235. * @access private
  236. */
  237. var $__objects = array();
  238. /**
  239. * Return a singleton instance of Configure.
  240. *
  241. * @return Configure instance
  242. * @access public
  243. */
  244. function &getInstance() {
  245. static $instance = array();
  246. if (!$instance) {
  247. $instance[0] = new Config();
  248. //$instance[0]->__loadBootstrap($boot);
  249. }
  250. return $instance[0];
  251. }
  252. /**
  253. * Used to write a dynamic var in the Configure instance.
  254. *
  255. * Usage
  256. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  257. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  258. * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
  259. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
  260. *
  261. * @param array $config Name of var to write
  262. * @param mixed $value Value to set for var
  263. * @return void
  264. * @access public
  265. */
  266. function write($config, $value = null) {
  267. $_this =& Config::getInstance();
  268. if (!is_array($config)) {
  269. $config = array($config => $value);
  270. }
  271. foreach ($config as $names => $value) {
  272. $name = $_this->__configVarNames($names);
  273. switch (count($name)) {
  274. case 3:
  275. $_this->{$name[0]}[$name[1]][$name[2]] = $value;
  276. break;
  277. case 2:
  278. $_this->{$name[0]}[$name[1]] = $value;
  279. break;
  280. case 1:
  281. $_this->{$name[0]} = $value;
  282. break;
  283. }
  284. }
  285. }
  286. /**
  287. * Used to read Configure::$var
  288. *
  289. * Usage
  290. * Configure::read('Name'); will return all values for Name
  291. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  292. *
  293. * @param string $var Variable to obtain
  294. * @return string value of Configure::$var
  295. * @access public
  296. */
  297. function read($var) {
  298. $_this =& Config::getInstance();
  299. if ($var === 'all') {
  300. $return = array();
  301. foreach ($_this AS $key =>$var) {
  302. $return[$key] = $var;
  303. }
  304. return $return;
  305. }
  306. $name = $_this->__configVarNames($var);
  307. switch (count($name)) {
  308. case 3:
  309. if (isset($_this->{$name[0]}[$name[1]][$name[2]])) {
  310. return $_this->{$name[0]}[$name[1]][$name[2]];
  311. }
  312. break;
  313. case 2:
  314. if (isset($_this->{$name[0]}[$name[1]])) {
  315. return $_this->{$name[0]}[$name[1]];
  316. }
  317. break;
  318. case 1:
  319. if (isset($_this->{$name[0]})) {
  320. return $_this->{$name[0]};
  321. }
  322. break;
  323. }
  324. return null;
  325. }
  326. function getAll() {
  327. $output = $this->config;
  328. return $output;
  329. }
  330. /**
  331. * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
  332. *
  333. * @param mixed $name Name to split
  334. * @return array Name separated in items through dot notation
  335. * @access private
  336. */
  337. function __configVarNames($name) {
  338. if (is_string($name)) {
  339. if (strpos($name, ".")) {
  340. return explode(".", $name);
  341. }
  342. return array($name);
  343. }
  344. return $name;
  345. }
  346. }
  347. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */