PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/inc/Configuration.php

https://github.com/cyrke/phpMyFAQ
PHP | 330 lines | 161 code | 27 blank | 142 comment | 13 complexity | 4fb62f3ddd18072c9f082ab38ea0e6aa MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * The main class for fetching the configuration, update and delete items. This
  4. * class is also a small Dependency Injection Container for phpMyFAQ.
  5. *
  6. * PHP Version 5.3
  7. *
  8. * This Source Code Form is subject to the terms of the Mozilla Public License,
  9. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  10. * obtain one at http://mozilla.org/MPL/2.0/.
  11. *
  12. * @category phpMyFAQ
  13. * @package Configuration
  14. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  15. * @copyright 2006-2012 phpMyFAQ Team
  16. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  17. * @link http://www.phpmyfaq.de
  18. * @since 2006-01-04
  19. */
  20. if (!defined('IS_VALID_PHPMYFAQ')) {
  21. exit();
  22. }
  23. /**
  24. * Configuration
  25. *
  26. * @category phpMyFAQ
  27. * @package Configuration
  28. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  29. * @copyright 2006-2012 phpMyFAQ Team
  30. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  31. * @link http://www.phpmyfaq.de
  32. * @since 2006-01-04
  33. */
  34. class PMF_Configuration
  35. {
  36. /**
  37. * Tablename
  38. *
  39. * @var string
  40. */
  41. protected $_tableName = 'faqconfig';
  42. /**
  43. * Configuration array
  44. *
  45. * @var array
  46. */
  47. public $config = array();
  48. /**
  49. * Constructor
  50. *
  51. * @param PMF_DB_Driver $database
  52. *
  53. * @return PMF_Configuration
  54. */
  55. public function __construct(PMF_DB_Driver $database)
  56. {
  57. $this->setDb($database);
  58. }
  59. /**
  60. * Fetches all configuration items into an array
  61. *
  62. * @return void
  63. */
  64. public function getAll()
  65. {
  66. $query = sprintf("
  67. SELECT
  68. config_name, config_value
  69. FROM
  70. %s%s",
  71. PMF_Db::getTablePrefix(),
  72. $this->_tableName
  73. );
  74. $result = $this->getDb()->query($query);
  75. $config = $this->getDb()->fetchAll($result);
  76. foreach ($config as $items) {
  77. $this->config[$items->config_name] = $items->config_value;
  78. }
  79. }
  80. /**
  81. * Returns a configuration item
  82. *
  83. * @param string $item Configuration item
  84. *
  85. * @return mixed
  86. */
  87. public function get($item)
  88. {
  89. if (!isset($this->config[$item])) {
  90. $this->getAll();
  91. }
  92. switch ($this->config[$item]) {
  93. case 'true':
  94. return true;
  95. break;
  96. case 'false':
  97. return false;
  98. break;
  99. default:
  100. return $this->config[$item];
  101. break;
  102. }
  103. }
  104. /**
  105. * Sets the PMF_DB_Driver object
  106. *
  107. * @param PMF_DB_Driver $database
  108. *
  109. * @return void
  110. */
  111. public function setDb(PMF_DB_Driver $database)
  112. {
  113. $this->config['core.database'] = $database;
  114. }
  115. /**
  116. * Returns the PMF_DB_Driver object
  117. *
  118. * @return PMF_DB_Driver
  119. */
  120. public function getDb()
  121. {
  122. return $this->config['core.database'];
  123. }
  124. /**
  125. * Sets the PMF_Instance object
  126. *
  127. * @param PMF_Instance $instance
  128. *
  129. * @return void
  130. */
  131. public function setInstance(PMF_Instance $instance)
  132. {
  133. $this->config['core.instance'] = $instance;
  134. }
  135. /**
  136. * Returns the PMF_Instance object
  137. *
  138. * @return PMF_Instance
  139. */
  140. public function getInstance()
  141. {
  142. return $this->config['core.instance'];
  143. }
  144. /**
  145. * Sets the Language object
  146. *
  147. * @param PMF_Language $language
  148. *
  149. * @return void
  150. */
  151. public function setLanguage(PMF_Language $language)
  152. {
  153. $this->config['core.language'] = $language;
  154. }
  155. /**
  156. * Returns the Language object
  157. *
  158. * @return PMF_Language
  159. */
  160. public function getLanguage()
  161. {
  162. return $this->config['core.language'];
  163. }
  164. /**
  165. * Sets the PMF_Ldap object
  166. *
  167. * @param PMF_Ldap $ldap
  168. *
  169. * @return void
  170. */
  171. public function setLdap(PMF_Ldap $ldap)
  172. {
  173. $this->config['core.ldap'] = $ldap;
  174. }
  175. /**
  176. * Returns the PMF_Ldap object
  177. *
  178. * @return PMF_Ldap
  179. */
  180. public function getLdap()
  181. {
  182. return $this->config['core.ldap'];
  183. }
  184. /**
  185. * Sets the LDAP configuration
  186. *
  187. * @param Array $ldapConfig
  188. *
  189. * @return void
  190. */
  191. public function setLdapConfig(Array $ldapConfig)
  192. {
  193. if (true === $ldapConfig['ldap_use_multiple_servers']) {
  194. // Multiple LDAP servers
  195. $key = 0;
  196. while ($key >= 0) {
  197. if (isset($ldapConfig[$key])) {
  198. $this->config['core.ldapConfig'][$key] = $ldapConfig[$key];
  199. $key++;
  200. } else {
  201. break;
  202. }
  203. }
  204. } else {
  205. // one LDAP server
  206. $this->config['core.ldapConfig'] = $ldapConfig;
  207. }
  208. }
  209. /**
  210. * Returns the LDAP configuration
  211. *
  212. * @return array
  213. */
  214. public function getLdapConfig()
  215. {
  216. return isset($this->config['core.ldapConfig']) ? $this->config['core.ldapConfig'] : array();
  217. }
  218. /**
  219. * Adds a configuration item for the database
  220. *
  221. * @param string $name
  222. * @param mixed $value
  223. *
  224. * @return boolean
  225. */
  226. public function add($name, $value)
  227. {
  228. $insert = sprintf(
  229. "INSERT INTO
  230. %s%s
  231. VALUES
  232. ('%s', '%s')",
  233. PMF_Db::getTablePrefix(),
  234. $this->_tableName,
  235. $this->getDb()->escape(trim($name)),
  236. $this->getDb()->escape(trim($value))
  237. );
  238. return $this->getDb()->query($insert);
  239. }
  240. /**
  241. * Updates all configuration items
  242. *
  243. * @param array $newConfigs Array with new configuration values
  244. *
  245. * @return bool
  246. */
  247. public function update(Array $newConfigs)
  248. {
  249. $runtimeConfigs = array(
  250. 'core.database', // PMF_DB_Driver
  251. 'core.instance', // PMF_Instance
  252. 'core.language', // Language
  253. 'core.ldap', // PMF_Ldap
  254. 'core.ldapConfig' // $PMF_LDAP
  255. );
  256. if (is_array($newConfigs)) {
  257. foreach ($newConfigs as $name => $value) {
  258. if ($name != 'main.phpMyFAQToken' &&
  259. !in_array($name, $runtimeConfigs)
  260. ) {
  261. $update = sprintf("
  262. UPDATE
  263. %s%s
  264. SET
  265. config_value = '%s'
  266. WHERE
  267. config_name = '%s'",
  268. PMF_Db::getTablePrefix(),
  269. $this->_tableName,
  270. $this->getDb()->escape(trim($value)),
  271. $name
  272. );
  273. $this->getDb()->query($update);
  274. if (isset($this->config[$name])) {
  275. unset($this->config[$name]);
  276. }
  277. }
  278. }
  279. return true;
  280. }
  281. return false;
  282. }
  283. /**
  284. * Returns all sorting possibilities for FAQ records
  285. *
  286. * @param string $current
  287. *
  288. * @return string
  289. */
  290. public static function sortingOptions($current)
  291. {
  292. global $PMF_LANG;
  293. $options = array('id', 'thema', 'visits', 'datum', 'author');
  294. $output = '';
  295. foreach ($options as $value) {
  296. printf('<option value="%s"%s>%s</option>',
  297. $value,
  298. ($value == $current) ? ' selected="selected"' : '',
  299. $PMF_LANG['ad_conf_order_'.$value]);
  300. }
  301. return $output;
  302. }
  303. }