PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Console/Config.php

https://github.com/bhar1red/anahita
PHP | 351 lines | 312 code | 8 blank | 31 comment | 2 complexity | 2f16ac4f9bcfad36da09a680aaeaf7f1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Console;
  3. /**
  4. * Config class.
  5. *
  6. * Provides a way to manipulate the site configuration file
  7. */
  8. class Config
  9. {
  10. /**
  11. * Site path
  12. *
  13. * @var string
  14. */
  15. protected $_site_path;
  16. /**
  17. * Configuration key map
  18. *
  19. * @var array
  20. */
  21. protected $_key_map = array(
  22. 'database_type' => 'dbtype',
  23. 'database_host' => 'host',
  24. 'database_user' => 'user',
  25. 'database_password' => 'password',
  26. 'database_name' => 'db',
  27. 'database_prefix' => 'dbprefix',
  28. 'enable_debug' => 'debug',
  29. 'enable_caching' => 'caching',
  30. 'url_rewrite' => 'sef_rewrite',
  31. 'cache_lifetime' => 'cachetime',
  32. 'session_lifetime' => 'lifetime',
  33. 'offline',
  34. 'secret',
  35. 'error_reporting',
  36. 'session_handler',
  37. 'cache_handler'
  38. );
  39. /**
  40. * Configuration data
  41. *
  42. * @var array
  43. */
  44. protected $_data = array();
  45. /**
  46. * Configuration file
  47. *
  48. * @var string
  49. */
  50. protected $_configuration_file;
  51. /**
  52. * Creates a configuration from a configuration.php file
  53. *
  54. * @param string $site_path
  55. */
  56. public function __construct($site_path)
  57. {
  58. $this->_site_path = $site_path;
  59. $map = array();
  60. foreach($this->_key_map as $key => $value)
  61. {
  62. if ( is_numeric($key) ) {
  63. $key = $value;
  64. }
  65. $map[$key] = $value;
  66. }
  67. $this->_key_map = $map;
  68. $this->_data = array(
  69. 'debug_db' => 0,
  70. 'debug_lang' => 0,
  71. 'mailer' => 'mail',
  72. 'mailfrom' => '',
  73. 'fromname' => '',
  74. 'sendmail' => '/usr/sbin/sendmail',
  75. 'smtpauth' => '0',
  76. 'smtpuser' => '',
  77. 'smtppass' => '',
  78. 'smtphost' => 'localhost',
  79. 'force_ssl' => 0,
  80. 'log_path' => $site_path.'/log',
  81. 'tmp_path' => $site_path.'/tmp',
  82. 'offline_message' => 'This site is down for maintenance.<br /> Please check back again soon.',
  83. 'sitename' => 'Anahita',
  84. 'editor' => 'tinymce',
  85. //'memcache_settings' => array(),
  86. 'list_limit' => 20,
  87. 'gzip' => 0,
  88. 'xmlrpc_server' => '',
  89. 'ftp_enable' => 0,
  90. 'offset' => 0,
  91. 'MetaAuthor' => '',
  92. 'MetaTitle' => '',
  93. 'sef' => '',
  94. 'sef_suffix' => '',
  95. 'feed_limit' => 10
  96. );
  97. $this->set(array(
  98. 'secret' => '',
  99. 'offline' => 0,
  100. 'enable_debug' => 0,
  101. 'cache_lifetime' => 60,
  102. 'session_lifetime' => 1440,
  103. 'error_reporting' => 0,
  104. 'enable_caching' => 1,
  105. 'url_rewrite' => 0,
  106. 'session_handler' => function_exists('apc_fetch') ? 'apc' : 'database',
  107. 'cache_handler' => function_exists('apc_fetch') ? 'apc' : 'file'
  108. ));
  109. $this->_configuration_file = $site_path.'/configuration.php';
  110. if ( file_exists($this->_configuration_file) )
  111. {
  112. $classname = 'JConfig'.md5(uniqid());
  113. $content = file_get_contents($this->_configuration_file);
  114. $content = str_replace('JConfig', $classname, $content);
  115. $content = str_replace(array('<?php',''), '', $content);
  116. $classname = '\\'.$classname;
  117. $return = @eval($content);
  118. if ( class_exists($classname) )
  119. {
  120. $config = new $classname;
  121. $this->_data = array_merge($this->_data, get_object_vars($config));
  122. }
  123. }
  124. $this->database_type = 'mysqli';
  125. }
  126. /**
  127. * Check if the configuation file exist
  128. *
  129. * @return boolean
  130. */
  131. public function isConfigured()
  132. {
  133. return file_exists($this->_configuration_file );
  134. }
  135. /**
  136. * Sets the keys that make the debug on for a site
  137. */
  138. public function enableDebug()
  139. {
  140. $this->set(array(
  141. 'error_reporting' => E_ALL,
  142. 'enable_debug' => 1,
  143. ));
  144. }
  145. /**
  146. * Disable debug
  147. */
  148. public function disableDebug()
  149. {
  150. $this->set(array(
  151. 'error_reporting' => 0,
  152. 'enable_debug' => 0,
  153. ));
  154. }
  155. /**
  156. * Set a configuration key/value
  157. *
  158. * @param string|array $key
  159. * @param string $value
  160. *
  161. * @return void.
  162. */
  163. public function set($key ,$value = null)
  164. {
  165. if ( is_array($key) )
  166. {
  167. foreach($key as $k => $v) {
  168. $this->$k = $v;
  169. }
  170. } else {
  171. $this->$key = $value;
  172. }
  173. }
  174. /**
  175. * Return a configuraiton value
  176. *
  177. * @param string $key
  178. *
  179. * @return Ambigous <NULL, multitype:>
  180. */
  181. public function __get($key)
  182. {
  183. if ( isset($this->_key_map[$key]) )
  184. {
  185. $key = $this->_key_map[$key];
  186. }
  187. return isset($this->_data[$key]) ? $this->_data[$key] : null;
  188. }
  189. /**
  190. * Set a configuration value. For setting array. We can use [val1,val2]
  191. *
  192. * @param string $key
  193. * @param string $value
  194. */
  195. public function __set($key , $value)
  196. {
  197. $matches = array();
  198. if ( preg_match('/^\[(.*?)\]$/', $value, $matches) ) {
  199. $value = explode(',', $matches[1]);
  200. }
  201. if ( isset($this->_key_map[$key]) ){
  202. $key = $this->_key_map[$key];
  203. }
  204. if ( $key == 'dbprefix' ) {
  205. $value = str_replace('_', '', $value).'_';
  206. }
  207. $this->_data[$key] = $value;
  208. }
  209. /**
  210. * Set configuration database info
  211. *
  212. * @param array $data
  213. *
  214. * @return void
  215. */
  216. public function setDatabaseInfo($data)
  217. {
  218. $data['host'] = $data['host'].':'.$data['port'];
  219. unset($data['port']);
  220. $keys = array_map(function($key) {
  221. return 'database_'.$key;
  222. }, array_keys($data));
  223. $data = array_combine($keys, array_values($data));
  224. $this->set($data);
  225. }
  226. /**
  227. * Get the database info
  228. *
  229. * @return array
  230. */
  231. public function getDatabaseInfo()
  232. {
  233. $parts = explode(':', $this->database_host);
  234. return array(
  235. 'host' => $parts[0],
  236. 'port' => isset($parts[1]) ? $parts[1] : '3306',
  237. 'user' => $this->database_user,
  238. 'password' => $this->database_password,
  239. 'name' => $this->database_name,
  240. 'prefix' => $this->database_prefix
  241. );
  242. }
  243. /**
  244. * Retunr the data
  245. *
  246. * @return arary
  247. */
  248. public function toData()
  249. {
  250. return $this->_data;
  251. }
  252. /**
  253. * Save the configuration into the file
  254. *
  255. * @return string
  256. */
  257. public function save()
  258. {
  259. $data = $this->toData();
  260. if ( file_exists($this->_configuration_file) &&
  261. !is_writable($this->_configuration_file) ) {
  262. chmod($this->_configuration_file,0755);
  263. }
  264. $file = new \SplFileObject($this->_configuration_file, 'w');
  265. $file->fwrite("<?php\n");
  266. $file->fwrite("class JConfig {\n\n");
  267. $print_array = function($array) use (&$print_array) {
  268. if ( is_array($array) )
  269. {
  270. $values = array();
  271. $hash = !is_numeric(key($array));
  272. foreach($array as $key => $value)
  273. {
  274. if ( !is_numeric($key) ) {
  275. $key = "'".addslashes($key)."'";
  276. }
  277. if ( !is_numeric($value) ) {
  278. $value = "'".addslashes($value)."'";
  279. }
  280. $values[] = $hash ? "$key=>$value" : $value;
  281. }
  282. return 'array('.implode(',', $values).')';
  283. }
  284. };
  285. $write = function($data) use($file, $print_array)
  286. {
  287. foreach($data as $key => $value)
  288. {
  289. if ( is_array($value) ) {
  290. $value = $print_array($value);
  291. }
  292. elseif ( !is_numeric($value) ) {
  293. $value = "'".addslashes($value)."'";
  294. }
  295. $file->fwrite(" var \$$key = $value;\n");
  296. }
  297. };
  298. $write_group = function($keys, $comment = null)
  299. use (&$data, $file, $write)
  300. {
  301. $values = array();
  302. foreach($keys as $key)
  303. {
  304. if (isset($data[$key])) {
  305. $values[$key] = $data[$key];
  306. unset($data[$key]);
  307. }
  308. }
  309. if ( !empty($values) )
  310. {
  311. if ( !empty($comment) ) {
  312. $file->fwrite(" /*$comment*/\n");
  313. }
  314. $write($values);
  315. $file->fwrite("\n");
  316. }
  317. };
  318. $write_group(array('offline','offline_message','sitename','editor'), 'Site Settings');
  319. $write_group(array('dbtype','host','user','password','db','dbprefix'), 'Database Settings');
  320. $write_group(array('secret','error_reporting','tmp_path','log_path','force_ssl'), 'Server Settings');
  321. $write_group(array('lifetime','session_handler'), 'Session Settings');
  322. $write_group(array('mailer','mailfrom','fromname','sendmail','smtpauth','smtpuser','smtppass','smtphost'), 'Mail Settings');
  323. $write_group(array('caching','cachetime','cache_handler'), 'Cache Settings');
  324. $write_group(array('debug','debug_db','debug_lang'), 'Debug Settings');
  325. $write_group(array('sef_rewrite'), 'Route Settings');
  326. $write_group(array('list_limit','gzip','xmlrpc_server','ftp_enable','offset','MetaAuthor','MetaTitle','sef','sef_suffix','feed_limit'),'Legacy. Will be removed');
  327. $write_group(array_keys($data),'Other configurations');
  328. $file->fwrite("}");
  329. }
  330. }
  331. ?>