aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/configuration.php
blob: b2bc8350519647839eac2688acdcabf9e9152632 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

	/**
	 * Elgg configuration library
	 * Contains functions for managing system configuration
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 */

	
	/**
	 * Sets a configuration value
	 *
	 * @param string $name The name of the configuration value
	 * @param string $value Its value
	 * @param int $site_guid Optionally, the GUID of the site (current site is assumed by default)
	 * @return false|int 1 or false depending on success or failure 
	 */
		function set_config($name, $value, $site_guid = 0) {
			
			global $CONFIG;
			$name = mysql_real_escape_string($name);
			$value = mysql_real_escape_string($value);
			$site_guid = (int) $site_guid;
			if ($site_guid == 0)
				$site_guid = (int) $CONFIG->site_id;
			$CONFIG->$name = $value;
			$value = serialize($value);
			return insert_data("insert into {$CONFIG->dbprefix}config set name = '{$name}', value = '{$value}', site_guid = {$site_guid}");
			
		}

	/**
	 * Gets a configuration value
	 *
	 * @param string $name The name of the config value
	 * @param int $site_guid Optionally, the GUID of the site (current site is assumed by default)
	 * @return mixed|false Depending on success
	 */
		function get_config($name, $site_guid = 0) {
			
			global $CONFIG;
			if (isset($CONFIG->$name))
				return $CONFIG->$name;
			$name = mysql_real_escape_string($name);
			$site_guid = (int) $site_guid;
			if ($site_guid == 0)
				$site_guid = (int) $CONFIG->site_id;
			if ($result = get_data_row("select value from {$CONFIG->dbprefix}config where name = '{$name}' and site_guid = {$site_guid}")) {
				$result = $result->value;
				$result = unserialize($result->value);
				$CONFIG->$name = $result;
				return $result;
			}
			return false;
			
		}

	/**
	 * If certain configuration elements don't exist, autodetect sensible defaults 
	 * 
	 * @uses $CONFIG The main configuration global
	 *
	 */
		function set_default_config() {
			
			global $CONFIG;
			if (empty($CONFIG->path))
				$CONFIG->path = str_replace("\\","/",dirname(dirname(dirname(__FILE__)))) . "/";
				
			if (empty($CONFIG->viewpath))
				$CONFIG->viewpath = $CONFIG->path . "views/";	

			if (empty($CONFIG->pluginspath))
				$CONFIG->pluginspath = $CONFIG->path . "mod/";
				
			if (empty($CONFIG->wwwroot)) {
				/*
				$CONFIG->wwwroot = "http://" . $_SERVER['SERVER_NAME'];
				
				$request = $_SERVER['REQUEST_URI'];
				
				if (strripos($request,"/") < (strlen($request) - 1)) {
					// addressing a file directly, not a dir
					$request = substr($request, 0, strripos($request,"/")+1);
				}
				
				$CONFIG->wwwroot .= $request;
				*/
				$CONFIG->wwwroot = "http://" . $_SERVER['HTTP_HOST'] . str_replace("//","/",str_replace($_SERVER['DOCUMENT_ROOT'],"",$CONFIG->path));
		
			}
		
			if (empty($CONFIG->url))
				$CONFIG->url = $CONFIG->wwwroot;
			
			if (empty($CONFIG->sitename))
				$CONFIG->sitename = "New Elgg site";
				
			if (empty($CONFIG->debug))
				$CONFIG->debug = false;

		}
		
	/**
	 * Function that provides some config initialisation on system init
	 *
	 */
		
		function configuration_init() {
			
			global $CONFIG;
			
			$CONFIG->path = datalist_get('path');
			$CONFIG->dataroot = datalist_get('dataroot');
			if (isset($CONFIG->site)) {
				$CONFIG->wwwroot = $CONFIG->site->url;
				$CONFIG->sitename = $CONFIG->site->name;
			}
			$CONFIG->url = $CONFIG->wwwroot;
			
			return true;
			
		}
		
	/**
	 * Register config_init
	 */

		register_event_handler('init','system','configuration_init',10);
		
?>