configuration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Elgg configuration procedural code.
  4. *
  5. * Includes functions for manipulating the configuration values stored in the database
  6. * Plugin authors should use the {@link elgg_get_config()}, {@link elgg_set_config()},
  7. * {@link elgg_save_config()}, and {@unset_config()} functions to access or update
  8. * config values.
  9. *
  10. * Elgg's configuration is split among 2 tables and 1 file:
  11. * - dbprefix_config
  12. * - dbprefix_datalists
  13. * - engine/settings.php (See {@link settings.example.php})
  14. *
  15. * Upon system boot, all values in dbprefix_config are read into $CONFIG.
  16. *
  17. * @package Elgg.Core
  18. * @subpackage Configuration
  19. */
  20. /**
  21. * Get the URL for the current (or specified) site
  22. *
  23. * @param int $site_guid The GUID of the site whose URL we want to grab
  24. * @return string
  25. * @since 1.8.0
  26. */
  27. function elgg_get_site_url($site_guid = 0) {
  28. return _elgg_services()->config->getSiteUrl($site_guid);
  29. }
  30. /**
  31. * Get the plugin path for this installation
  32. *
  33. * @return string
  34. * @since 1.8.0
  35. */
  36. function elgg_get_plugins_path() {
  37. return _elgg_services()->config->getPluginsPath();
  38. }
  39. /**
  40. * Get the data directory path for this installation
  41. *
  42. * @return string
  43. * @since 1.8.0
  44. */
  45. function elgg_get_data_path() {
  46. return _elgg_services()->config->getDataPath();
  47. }
  48. /**
  49. * Get the root directory path for this installation
  50. *
  51. * @return string
  52. * @since 1.8.0
  53. */
  54. function elgg_get_root_path() {
  55. return _elgg_services()->config->getRootPath();
  56. }
  57. /**
  58. * Get an Elgg configuration value
  59. *
  60. * @param string $name Name of the configuration value
  61. * @param int $site_guid null for installation setting, 0 for default site
  62. *
  63. * @return mixed Configuration value or null if it does not exist
  64. * @since 1.8.0
  65. */
  66. function elgg_get_config($name, $site_guid = 0) {
  67. return _elgg_services()->config->get($name, $site_guid);
  68. }
  69. /**
  70. * Set an Elgg configuration value
  71. *
  72. * @warning This does not persist the configuration setting. Use elgg_save_config()
  73. *
  74. * @param string $name Name of the configuration value
  75. * @param mixed $value Value
  76. *
  77. * @return void
  78. * @since 1.8.0
  79. */
  80. function elgg_set_config($name, $value) {
  81. return _elgg_services()->config->set($name, $value);
  82. }
  83. /**
  84. * Save a configuration setting
  85. *
  86. * @param string $name Configuration name (cannot be greater than 255 characters)
  87. * @param mixed $value Configuration value. Should be string for installation setting
  88. * @param int $site_guid null for installation setting, 0 for default site
  89. *
  90. * @return bool
  91. * @since 1.8.0
  92. */
  93. function elgg_save_config($name, $value, $site_guid = 0) {
  94. return _elgg_services()->config->save($name, $value, $site_guid);
  95. }
  96. /**
  97. * Get the value of a datalist element.
  98. *
  99. * Plugin authors should use elgg_get_config() and pass null for the site GUID.
  100. *
  101. * @internal Datalists are stored in the datalist table.
  102. *
  103. * @tip Use datalists to store information common to a full installation.
  104. *
  105. * @param string $name The name of the datalist
  106. * @return string|null|false String if value exists, null if doesn't, false on error
  107. * @access private
  108. */
  109. function datalist_get($name) {
  110. return _elgg_services()->datalist->get($name);
  111. }
  112. /**
  113. * Set the value for a datalist element.
  114. *
  115. * Plugin authors should use elgg_save_config() and pass null for the site GUID.
  116. *
  117. * @warning Names should be selected so as not to collide with the names for the
  118. * site config.
  119. *
  120. * @warning Values set through datalist_set() are not available in $CONFIG until
  121. * next page load.
  122. *
  123. * @param string $name The name of the datalist
  124. * @param string $value The new value
  125. *
  126. * @return bool
  127. * @access private
  128. */
  129. function datalist_set($name, $value) {
  130. return _elgg_services()->datalist->set($name, $value);
  131. }
  132. /**
  133. * Run a function one time per installation.
  134. *
  135. * If you pass a timestamp as the second argument, it will run the function
  136. * only if (i) it has never been run before or (ii) the timestamp is >=
  137. * the last time it was run.
  138. *
  139. * @warning Functions are determined by their name. If you change the name of a function
  140. * it will be run again.
  141. *
  142. * @tip Use $timelastupdatedcheck in your plugins init function to perform automated
  143. * upgrades. Schedule a function to run once and pass the timestamp of the new release.
  144. * This will cause the run once function to be run on all installations. To perform
  145. * additional upgrades, create new functions for each release.
  146. *
  147. * @warning The function name cannot be longer than 255 characters long due to
  148. * the current schema for the datalist table.
  149. *
  150. * @internal A datalist entry $functioname is created with the value of time().
  151. *
  152. * @param string $functionname The name of the function you want to run.
  153. * @param int $timelastupdatedcheck A UNIX timestamp. If time() is > than this,
  154. * this function will be run again.
  155. *
  156. * @return bool
  157. * @todo deprecate
  158. */
  159. function run_function_once($functionname, $timelastupdatedcheck = 0) {
  160. return _elgg_services()->datalist->runFunctionOnce($functionname, $timelastupdatedcheck);
  161. }
  162. /**
  163. * Removes a config setting.
  164. *
  165. * @note Internal: These settings are stored in the dbprefix_config table and read
  166. * during system boot into $CONFIG.
  167. *
  168. * @param string $name The name of the field.
  169. * @param int $site_guid Optionally, the GUID of the site (default: current site).
  170. *
  171. * @return bool Success or failure
  172. *
  173. * @see get_config()
  174. * @see set_config()
  175. */
  176. function unset_config($name, $site_guid = 0) {
  177. return _elgg_services()->configTable->remove($name, $site_guid);
  178. }
  179. /**
  180. * Add or update a config setting.
  181. *
  182. * Plugin authors should use elgg_set_config().
  183. *
  184. * If the config name already exists, it will be updated to the new value.
  185. *
  186. * @warning Names should be selected so as not to collide with the names for the
  187. * datalist (application configuration)
  188. *
  189. * @internal These settings are stored in the dbprefix_config table and read
  190. * during system boot into $CONFIG.
  191. *
  192. * @internal The value is serialized so we maintain type information.
  193. *
  194. * @param string $name The name of the configuration value
  195. * @param mixed $value Its value
  196. * @param int $site_guid Optionally, the GUID of the site (current site is assumed by default)
  197. *
  198. * @return bool
  199. * @see unset_config()
  200. * @see get_config()
  201. * @access private
  202. */
  203. function set_config($name, $value, $site_guid = 0) {
  204. return _elgg_services()->configTable->set($name, $value, $site_guid);
  205. }
  206. /**
  207. * Gets a configuration value
  208. *
  209. * Plugin authors should use elgg_get_config().
  210. *
  211. * @internal These settings are stored in the dbprefix_config table and read
  212. * during system boot into $CONFIG.
  213. *
  214. * @param string $name The name of the config value
  215. * @param int $site_guid Optionally, the GUID of the site (default: current site)
  216. *
  217. * @return mixed|null
  218. * @see set_config()
  219. * @see unset_config()
  220. * @access private
  221. */
  222. function get_config($name, $site_guid = 0) {
  223. return _elgg_services()->configTable->get($name, $site_guid);
  224. }
  225. /**
  226. * Loads configuration related to this site
  227. *
  228. * This runs on engine boot and loads from the config database table and the
  229. * site entity. It runs after the application configuration is loaded by
  230. * _elgg_load_application_config().
  231. *
  232. * @see _elgg_engine_boot()
  233. *
  234. * @access private
  235. */
  236. function _elgg_load_site_config() {
  237. global $CONFIG;
  238. $CONFIG->site_guid = (int) datalist_get('default_site');
  239. $CONFIG->site_id = $CONFIG->site_guid;
  240. $CONFIG->site = _elgg_services()->entityTable->get($CONFIG->site_guid, 'site');
  241. if (!$CONFIG->site) {
  242. throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down.");
  243. }
  244. $CONFIG->wwwroot = $CONFIG->site->url;
  245. $CONFIG->sitename = $CONFIG->site->name;
  246. $CONFIG->sitedescription = $CONFIG->site->description;
  247. $CONFIG->siteemail = $CONFIG->site->email;
  248. $CONFIG->url = $CONFIG->wwwroot;
  249. _elgg_services()->configTable->loadAll();
  250. // gives hint to elgg_get_config function how to approach missing values
  251. $CONFIG->site_config_loaded = true;
  252. if (!empty($CONFIG->debug)) {
  253. _elgg_services()->logger->setLevel($CONFIG->debug);
  254. _elgg_services()->logger->setDisplay(true);
  255. }
  256. }
  257. /**
  258. * Loads configuration related to Elgg as an application
  259. *
  260. * This runs on the engine boot and loads from the datalists database table.
  261. *
  262. * @see _elgg_engine_boot()
  263. *
  264. * @access private
  265. */
  266. function _elgg_load_application_config() {
  267. global $CONFIG;
  268. $install_root = str_replace("\\", "/", dirname(dirname(dirname(__FILE__))));
  269. $defaults = array(
  270. 'path' => "$install_root/",
  271. 'view_path' => "$install_root/views/",
  272. 'plugins_path' => "$install_root/mod/",
  273. 'language' => 'en',
  274. // compatibility with old names for plugins not using elgg_get_config()
  275. 'viewpath' => "$install_root/views/",
  276. 'pluginspath' => "$install_root/mod/",
  277. );
  278. foreach ($defaults as $name => $value) {
  279. if (empty($CONFIG->$name)) {
  280. $CONFIG->$name = $value;
  281. }
  282. }
  283. // set cookie values for session and remember me
  284. if (!isset($CONFIG->cookies)) {
  285. $CONFIG->cookies = array();
  286. }
  287. if (!isset($CONFIG->cookies['session'])) {
  288. $CONFIG->cookies['session'] = array();
  289. }
  290. $session_defaults = session_get_cookie_params();
  291. $session_defaults['name'] = 'Elgg';
  292. $CONFIG->cookies['session'] = array_merge($session_defaults, $CONFIG->cookies['session']);
  293. if (!isset($CONFIG->cookies['remember_me'])) {
  294. $CONFIG->cookies['remember_me'] = array();
  295. }
  296. $session_defaults['name'] = 'elggperm';
  297. $session_defaults['expire'] = strtotime("+30 days");
  298. $CONFIG->cookies['remember_me'] = array_merge($session_defaults, $CONFIG->cookies['remember_me']);
  299. if (!is_memcache_available()) {
  300. _elgg_services()->datalist->loadAll();
  301. }
  302. // allow sites to set dataroot and simplecache_enabled in settings.php
  303. if (isset($CONFIG->dataroot)) {
  304. $CONFIG->dataroot = sanitise_filepath($CONFIG->dataroot);
  305. $CONFIG->dataroot_in_settings = true;
  306. } else {
  307. $dataroot = datalist_get('dataroot');
  308. if (!empty($dataroot)) {
  309. $CONFIG->dataroot = $dataroot;
  310. }
  311. $CONFIG->dataroot_in_settings = false;
  312. }
  313. if (isset($CONFIG->simplecache_enabled)) {
  314. $CONFIG->simplecache_enabled_in_settings = true;
  315. } else {
  316. $simplecache_enabled = datalist_get('simplecache_enabled');
  317. if ($simplecache_enabled !== false) {
  318. $CONFIG->simplecache_enabled = $simplecache_enabled;
  319. } else {
  320. $CONFIG->simplecache_enabled = 1;
  321. }
  322. $CONFIG->simplecache_enabled_in_settings = false;
  323. }
  324. $system_cache_enabled = datalist_get('system_cache_enabled');
  325. if ($system_cache_enabled !== false) {
  326. $CONFIG->system_cache_enabled = $system_cache_enabled;
  327. } else {
  328. $CONFIG->system_cache_enabled = 1;
  329. }
  330. // needs to be set before system, init for links in html head
  331. $CONFIG->lastcache = (int)datalist_get("simplecache_lastupdate");
  332. $CONFIG->i18n_loaded_from_cache = false;
  333. // this must be synced with the enum for the entities table
  334. $CONFIG->entity_types = array('group', 'object', 'site', 'user');
  335. }
  336. /**
  337. * @access private
  338. */
  339. function _elgg_config_test($hook, $type, $tests) {
  340. global $CONFIG;
  341. $tests[] = "{$CONFIG->path}engine/tests/ElggCoreConfigTest.php";
  342. return $tests;
  343. }
  344. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  345. $hooks->registerHandler('unit_test', 'system', '_elgg_config_test');
  346. };