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

/phpmyfaq/src/phpMyFAQ/Configuration.php

http://github.com/thorsten/phpMyFAQ
PHP | 486 lines | 264 code | 49 blank | 173 comment | 15 complexity | 51c97614ec7c7901e115b6d499b9195d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  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. * This Source Code Form is subject to the terms of the Mozilla Public License,
  7. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  8. * obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * @package phpMyFAQ
  11. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  12. * @copyright 2006-2021 phpMyFAQ Team
  13. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  14. * @link https://www.phpmyfaq.de
  15. * @since 2006-01-04
  16. */
  17. namespace phpMyFAQ;
  18. use Elasticsearch\Client;
  19. use phpMyFAQ\Database\DatabaseDriver;
  20. /**
  21. * Class Configuration
  22. *
  23. * @package phpMyFAQ
  24. */
  25. class Configuration
  26. {
  27. /**
  28. * @var array
  29. */
  30. public $config = [];
  31. /**
  32. * @var string
  33. */
  34. protected $tableName = 'faqconfig';
  35. /**
  36. * Constructor.
  37. *
  38. * @param DatabaseDriver $database
  39. */
  40. public function __construct(DatabaseDriver $database)
  41. {
  42. $this->setDb($database);
  43. }
  44. /**
  45. * Sets the phpMyFAQ\Db_Driver object.
  46. *
  47. * @param DatabaseDriver $database
  48. */
  49. public function setDb(DatabaseDriver $database)
  50. {
  51. $this->config['core.database'] = $database;
  52. }
  53. /**
  54. * Returns all sorting possibilities for FAQ records.
  55. *
  56. * @param string $current
  57. * @return string
  58. */
  59. public static function sortingOptions(string $current): string
  60. {
  61. global $PMF_LANG;
  62. $options = ['id', 'thema', 'visits', 'updated', 'author'];
  63. $output = '';
  64. foreach ($options as $value) {
  65. printf(
  66. '<option value="%s" %s>%s</option>',
  67. $value,
  68. ($value == $current) ? 'selected' : '',
  69. $PMF_LANG['ad_conf_order_' . $value]
  70. );
  71. }
  72. return $output;
  73. }
  74. /**
  75. * Sets one single configuration item.
  76. *
  77. * @param string $key
  78. * @param mixed $value
  79. * @return bool
  80. */
  81. public function set(string $key, $value): bool
  82. {
  83. $query = sprintf(
  84. "UPDATE %s%s SET config_value = '%s' WHERE config_name = '%s'",
  85. Database::getTablePrefix(),
  86. $this->tableName,
  87. $this->getDb()->escape(trim($value)),
  88. $this->getDb()->escape(trim($key))
  89. );
  90. return (bool) $this->getDb()->query($query);
  91. }
  92. /**
  93. * Returns the DatabaseDriver object.
  94. *
  95. * @return DatabaseDriver
  96. */
  97. public function getDb(): DatabaseDriver
  98. {
  99. return $this->config['core.database'];
  100. }
  101. /**
  102. * Sets the Instance object.
  103. *
  104. * @param Instance $instance
  105. */
  106. public function setInstance(Instance $instance)
  107. {
  108. $this->config['core.instance'] = $instance;
  109. }
  110. /**
  111. * Returns the Instance object.
  112. *
  113. * @return Instance
  114. */
  115. public function getInstance(): Instance
  116. {
  117. return $this->config['core.instance'];
  118. }
  119. /**
  120. * Sets the Language object.
  121. *
  122. * @param Language $language
  123. */
  124. public function setLanguage(Language $language)
  125. {
  126. $this->config['core.language'] = $language;
  127. }
  128. /**
  129. * Returns the Language object.
  130. *
  131. * @return Language
  132. */
  133. public function getLanguage(): Language
  134. {
  135. return $this->config['core.language'];
  136. }
  137. /**
  138. * Returns the default language.
  139. * @return string
  140. */
  141. public function getDefaultLanguage(): string
  142. {
  143. return str_replace(['language_', '.php'], '', $this->config['main.language']);
  144. }
  145. /**
  146. * Returns the current version
  147. * @return string
  148. */
  149. public function getVersion(): string
  150. {
  151. return $this->config['main.currentVersion'];
  152. }
  153. /**
  154. * Returns the title of the FAQ installation
  155. * @return string
  156. */
  157. public function getTitle(): string
  158. {
  159. return $this->config['main.titleFAQ'];
  160. }
  161. /**
  162. * Returns the email address of the main admin
  163. * @return string
  164. */
  165. public function getAdminEmail(): string
  166. {
  167. return $this->config['main.administrationMail'];
  168. }
  169. /**
  170. * Returns the default URL of the phpMyFAQ installation.
  171. *
  172. * @return string
  173. */
  174. public function getDefaultUrl(): string
  175. {
  176. $defaultUrl = $this->get('main.referenceURL');
  177. if (substr($defaultUrl, -1) !== '/') {
  178. return $defaultUrl . '/';
  179. } else {
  180. return $defaultUrl;
  181. }
  182. }
  183. /**
  184. * Returns a configuration item.
  185. *
  186. * @param string $item Configuration item
  187. * @return mixed
  188. */
  189. public function get(string $item)
  190. {
  191. if (!isset($this->config[$item])) {
  192. $this->getAll();
  193. }
  194. if (isset($this->config[$item])) {
  195. switch ($this->config[$item]) {
  196. case 'true':
  197. return true;
  198. case 'false':
  199. return false;
  200. default:
  201. return $this->config[$item];
  202. }
  203. }
  204. return null;
  205. }
  206. /**
  207. * Fetches all configuration items into an array.
  208. */
  209. public function getAll()
  210. {
  211. $config = [];
  212. $query = sprintf(
  213. '
  214. SELECT
  215. config_name, config_value
  216. FROM
  217. %s%s',
  218. Database::getTablePrefix(),
  219. $this->tableName
  220. );
  221. $result = $this->getDb()->query($query);
  222. try {
  223. $config = $this->getDb()->fetchAll($result);
  224. } catch (Exception $e) {
  225. // @todo Added proper handling of exception
  226. echo $e->getMessage();
  227. }
  228. foreach ($config as $items) {
  229. $this->config[$items->config_name] = $items->config_value;
  230. }
  231. }
  232. /**
  233. * Sets the LDAP configuration.
  234. *
  235. * @param array $ldapConfig
  236. */
  237. public function setLdapConfig(array $ldapConfig)
  238. {
  239. // Always add main LDAP server
  240. $this->config['core.ldapServer'][0] = [
  241. 'ldap_server' => $ldapConfig['ldap_server'],
  242. 'ldap_port' => $ldapConfig['ldap_port'],
  243. 'ldap_user' => $ldapConfig['ldap_user'],
  244. 'ldap_password' => $ldapConfig['ldap_password'],
  245. 'ldap_base' => $ldapConfig['ldap_base'],
  246. ];
  247. // Add multiple LDAP servers if enabled
  248. if (true === $this->get('ldap.ldap_use_multiple_servers')) {
  249. $key = 1;
  250. while ($key >= 1) {
  251. if (isset($ldapConfig[$key])) {
  252. $this->config['core.ldapServer'][$key] = $ldapConfig[$key];
  253. ++$key;
  254. } else {
  255. break;
  256. }
  257. }
  258. }
  259. // Set LDAP configuration
  260. $this->config['core.ldapConfig'] = [
  261. 'ldap_use_multiple_servers' => $this->get('ldap.ldap_use_multiple_servers'),
  262. 'ldap_mapping' => $this->getLdapMapping(),
  263. 'ldap_use_domain_prefix' => $this->get('ldap.ldap_use_domain_prefix'),
  264. 'ldap_options' => $this->getLdapOptions(),
  265. 'ldap_use_memberOf' => $this->get('ldap.ldap_use_memberOf'),
  266. 'ldap_use_sasl' => $this->get('ldap.ldap_use_sasl'),
  267. 'ldap_use_anonymous_login' => $this->get('ldap.ldap_use_anonymous_login'),
  268. ];
  269. }
  270. /**
  271. * Returns the LDAP mapping configuration.
  272. *
  273. * @return array
  274. */
  275. public function getLdapMapping(): array
  276. {
  277. return [
  278. 'name' => $this->get('ldap.ldap_mapping.name'),
  279. 'username' => $this->get('ldap.ldap_mapping.username'),
  280. 'mail' => $this->get('ldap.ldap_mapping.mail'),
  281. 'memberOf' => $this->get('ldap.ldap_mapping.memberOf')
  282. ];
  283. }
  284. /**
  285. * Returns the LDAP options configuration.
  286. *
  287. * @return array
  288. */
  289. public function getLdapOptions(): array
  290. {
  291. return [
  292. 'LDAP_OPT_PROTOCOL_VERSION' => $this->get('ldap.ldap_options.LDAP_OPT_PROTOCOL_VERSION'),
  293. 'LDAP_OPT_REFERRALS' => $this->get('ldap.ldap_options.LDAP_OPT_REFERRALS')
  294. ];
  295. }
  296. /**
  297. * Returns the LDAP configuration.
  298. *
  299. * @return array
  300. */
  301. public function getLdapConfig(): array
  302. {
  303. return isset($this->config['core.ldapConfig']) ? $this->config['core.ldapConfig'] : [];
  304. }
  305. /**
  306. * Returns the LDAP server(s).
  307. *
  308. * @return array
  309. */
  310. public function getLdapServer(): array
  311. {
  312. return isset($this->config['core.ldapServer']) ? $this->config['core.ldapServer'] : [];
  313. }
  314. /**
  315. * Sets the Elasticsearch client instance.
  316. *
  317. * @param Client $esClient
  318. */
  319. public function setElasticsearch(Client $esClient)
  320. {
  321. $this->config['core.elasticsearch'] = $esClient;
  322. }
  323. /**
  324. * Returns the Elasticsearch client instance.
  325. *
  326. * @return Client
  327. */
  328. public function getElasticsearch(): Client
  329. {
  330. return $this->config['core.elasticsearch'];
  331. }
  332. /**
  333. * Sets the Elasticsearch configuration.
  334. *
  335. * @param array $data
  336. */
  337. public function setElasticsearchConfig(array $data)
  338. {
  339. $this->config['core.elasticsearchConfig'] = $data;
  340. }
  341. /**
  342. * Returns the Elasticsearch configuration.
  343. *
  344. * @return array
  345. */
  346. public function getElasticsearchConfig(): array
  347. {
  348. return isset($this->config['core.elasticsearchConfig']) ? $this->config['core.elasticsearchConfig'] : [];
  349. }
  350. /**
  351. * Adds a configuration item for the database.
  352. *
  353. * @param string $name
  354. * @param mixed $value
  355. * @return bool|object
  356. */
  357. public function add(string $name, $value)
  358. {
  359. $insert = sprintf(
  360. "INSERT INTO
  361. %s%s
  362. VALUES
  363. ('%s', '%s')",
  364. Database::getTablePrefix(),
  365. $this->tableName,
  366. $this->getDb()->escape(trim($name)),
  367. $this->getDb()->escape(trim($value))
  368. );
  369. return $this->getDb()->query($insert);
  370. }
  371. /**
  372. * Deletes a configuration item for the database.
  373. *
  374. * @param string $name
  375. * @return bool
  376. */
  377. public function delete(string $name): bool
  378. {
  379. $delete = sprintf(
  380. "DELETE FROM
  381. %s%s
  382. WHERE
  383. config_name = '%s'",
  384. Database::getTablePrefix(),
  385. $this->tableName,
  386. $this->getDb()->escape(trim($name))
  387. );
  388. return (bool)$this->getDb()->query($delete);
  389. }
  390. /**
  391. * Updates all configuration items.
  392. *
  393. * @param array $newConfigs Array with new configuration values
  394. *
  395. * @return bool
  396. */
  397. public function update(array $newConfigs): bool
  398. {
  399. $runtimeConfigs = [
  400. 'core.database', // phpMyFAQ\Database\DatabaseDriver
  401. 'core.instance', // Instance
  402. 'core.language', // Language
  403. 'core.ldapServer', // Ldap
  404. 'core.ldapConfig', // $LDAP
  405. 'core.elasticsearch', // Elasticsearch\Client
  406. 'core.elasticsearchConfig' // $ES
  407. ];
  408. if (is_array($newConfigs)) {
  409. foreach ($newConfigs as $name => $value) {
  410. if (
  411. $name != 'main.phpMyFAQToken'
  412. && !in_array($name, $runtimeConfigs)
  413. ) {
  414. $update = sprintf(
  415. "
  416. UPDATE
  417. %s%s
  418. SET
  419. config_value = '%s'
  420. WHERE
  421. config_name = '%s'",
  422. Database::getTablePrefix(),
  423. $this->tableName,
  424. $this->getDb()->escape(trim($value)),
  425. $name
  426. );
  427. $this->getDb()->query($update);
  428. if (isset($this->config[$name])) {
  429. unset($this->config[$name]);
  430. }
  431. }
  432. }
  433. return true;
  434. }
  435. return false;
  436. }
  437. }