summaryrefslogtreecommitdiff
path: root/framework/Web/Services/TPageConfiguration.php
blob: 681af1338ac9073ae42a3343b525209b60a15367 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
/**
 * TPageService class file.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package Prado\Web\Services
 */

namespace Prado\Web\Services;
use Prado\Exceptions\TConfigurationException;
use Prado\Prado;
use Prado\TApplicationConfiguration;
use Prado\Security\TAuthorizationRuleCollection;
use Prado\TApplication;
use Prado\Xml\TXmlElement;
use Prado\Xml\TXmlDocument;

/**
 * TPageConfiguration class
 *
 * TPageConfiguration represents the configuration for a page.
 * The page is specified by a dot-connected path.
 * Configurations along this path are merged together to be provided for the page.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package Prado\Web\Services
 * @since 3.0
 */
class TPageConfiguration extends \Prado\TComponent
{
	/**
	 * @var array list of application configurations
	 */
	private $_appConfigs=array();
	/**
	 * @var array list of page initial property values
	 */
	private $_properties=array();
	/**
	 * @var TAuthorizationRuleCollection list of authorization rules
	 */
	private $_rules=array();
	/**
	 * @var array list of included configurations
	 */
	private $_includes=array();
	/**
	 * @var string the currently request page in the format of Path.To.PageName
	 */
	private $_pagePath='';

	/**
	 * Constructor.
	 * @param string the currently request page in the format of Path.To.PageName
	 */
	public function __construct($pagePath)
	{
		$this->_pagePath=$pagePath;
	}

	/**
	 * @return array list of external configuration files. Each element is like $filePath=>$condition
	 */
	public function getExternalConfigurations()
	{
		return $this->_includes;
	}

	/**
	 * Returns list of page initial property values.
	 * Each array element represents a single property with the key
	 * being the property name and the value the initial property value.
	 * @return array list of page initial property values
	 */
	public function getProperties()
	{
		return $this->_properties;
	}

	/**
	 * Returns list of authorization rules.
	 * The authorization rules are aggregated (bottom-up) from configuration files
	 * along the path to the specified page.
	 * @return TAuthorizationRuleCollection collection of authorization rules
	 */
	public function getRules()
	{
		return $this->_rules;
	}

	/**
	 * @return array list of application configurations specified along page path
	 */
	public function getApplicationConfigurations()
	{
		return $this->_appConfigs;
	}

	/**
	 * Loads configuration for a page specified in a path format.
	 * @param string root path for pages
	 */
	public function loadFromFiles($basePath)
	{
		$paths=explode('.',$this->_pagePath);
		$page=array_pop($paths);
		$path=$basePath;
		$configPagePath='';
		$fileName = Prado::getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP
			? TPageService::CONFIG_FILE_PHP
			: TPageService::CONFIG_FILE_XML;
		foreach($paths as $p)
		{
			$this->loadFromFile($path.DIRECTORY_SEPARATOR.$fileName,$configPagePath);
			$path.=DIRECTORY_SEPARATOR.$p;
			if($configPagePath==='')
				$configPagePath=$p;
			else
				$configPagePath.='.'.$p;
		}
		$this->loadFromFile($path.DIRECTORY_SEPARATOR.$fileName,$configPagePath);
		$this->_rules=new TAuthorizationRuleCollection($this->_rules);
	}

	/**
	 * Loads a specific config file.
	 * @param string config file name
	 * @param string the page path that the config file is associated with. The page path doesn't include the page name.
	 */
	public function loadFromFile($fname,$configPagePath)
	{
		Prado::trace("Loading page configuration file $fname",'System.Web.Services.TPageService');
		if(empty($fname) || !is_file($fname))
			return;

		if(Prado::getApplication()->getConfigurationType()==TApplication::CONFIG_TYPE_PHP)
		{
			$fcontent = include $fname;
			$this->loadFromPhp($fcontent,dirname($fname),$configPagePath);
		}
		else
		{
			$dom=new TXmlDocument;
			if($dom->loadFromFile($fname))
				$this->loadFromXml($dom,dirname($fname),$configPagePath);
			else
				throw new TConfigurationException('pageserviceconf_file_invalid',$fname);
		}
	}

	public function loadFromPhp($config,$configPath,$configPagePath)
	{
		$this->loadApplicationConfigurationFromPhp($config,$configPath);
		$this->loadPageConfigurationFromPhp($config,$configPath,$configPagePath);
	}

	/**
	 * Loads a page configuration.
	 * The configuration includes information for both application
	 * and page service.
	 * @param TXmlElement config xml element
	 * @param string the directory containing this configuration
	 * @param string the page path that the config XML is associated with. The page path doesn't include the page name.
	 */
	public function loadFromXml($dom,$configPath,$configPagePath)
	{
		$this->loadApplicationConfigurationFromXml($dom,$configPath);
		$this->loadPageConfigurationFromXml($dom,$configPath,$configPagePath);
	}

	public function loadApplicationConfigurationFromPhp($config,$configPath)
	{
		$appConfig=new TApplicationConfiguration;
		$appConfig->loadFromPhp($config,$configPath);
		$this->_appConfigs[]=$appConfig;
	}

	/**
	 * Loads the configuration specific for application part
	 * @param TXmlElement config xml element
	 * @param string base path corresponding to this xml element
	 */
	public function loadApplicationConfigurationFromXml($dom,$configPath)
	{
		$appConfig=new TApplicationConfiguration;
		$appConfig->loadFromXml($dom,$configPath);
		$this->_appConfigs[]=$appConfig;
	}

	public function loadPageConfigurationFromPhp($config, $configPath, $configPagePath)
	{
		// authorization
		if(isset($config['authorization']) && is_array($config['authorization']))
		{
			$rules = array();
			foreach($config['authorization'] as $authorization)
			{
				$patterns=isset($authorization['pages'])?$authorization['pages']:'';
				$ruleApplies=false;
				if(empty($patterns) || trim($patterns)==='*') // null or empty string
					$ruleApplies=true;
				else
				{
					foreach(explode(',',$patterns) as $pattern)
					{
						if(($pattern=trim($pattern))!=='')
						{
							// we know $configPagePath and $this->_pagePath
							if($configPagePath!=='')  // prepend the pattern with ConfigPagePath
								$pattern=$configPagePath.'.'.$pattern;
							if(strcasecmp($pattern,$this->_pagePath)===0)
							{
								$ruleApplies=true;
								break;
							}
							if($pattern[strlen($pattern)-1]==='*') // try wildcard matching
							{
								if(strncasecmp($this->_pagePath,$pattern,strlen($pattern)-1)===0)
								{
									$ruleApplies=true;
									break;
								}
							}
						}
					}
				}
				if($ruleApplies)
				{
					$action = isset($authorization['action'])?$authorization['action']:'';
					$users = isset($authorization['users'])?$authorization['users']:'';
					$roles = isset($authorization['roles'])?$authorization['roles']:'';
					$verb = isset($authorization['verb'])?$authorization['verb']:'';
					$ips = isset($authorization['ips'])?$authorization['ips']:'';
					$rules[]=new TAuthorizationRule($action,$users,$roles,$verb,$ips);
				}
			}
			$this->_rules=array_merge($rules,$this->_rules);
		}
		// pages
		if(isset($config['pages']) && is_array($config['pages']))
		{
			if(isset($config['pages']['properties']))
			{
				$this->_properties = array_merge($this->_properties, $config['pages']['properties']);
				unset($config['pages']['properties']);
			}
			foreach($config['pages'] as $id => $page)
			{
				$properties = array();
				if(isset($page['properties']))
				{
					$properties=$page['properties'];
					unset($page['properties']);
				}
				$matching=false;
				$id=($configPagePath==='')?$id:$configPagePath.'.'.$id;
				if(strcasecmp($id,$this->_pagePath)===0)
					$matching=true;
				else if($id[strlen($id)-1]==='*') // try wildcard matching
					$matching=strncasecmp($this->_pagePath,$id,strlen($id)-1)===0;
				if($matching)
					$this->_properties=array_merge($this->_properties,$properties);
			}
		}

		// external configurations
		if(isset($config['includes']) && is_array($config['includes']))
		{
			foreach($config['includes'] as $include)
			{
				$when = isset($include['when'])?true:false;
				if(!isset($include['file']))
					throw new TConfigurationException('pageserviceconf_includefile_required');
				$filePath = $include['file'];
				if(isset($this->_includes[$filePath]))
					$this->_includes[$filePath]=array($configPagePath,'('.$this->_includes[$filePath][1].') || ('.$when.')');
				else
					$this->_includes[$filePath]=array($configPagePath,$when);
			}
		}
	}

	/**
	 * Loads the configuration specific for page service.
	 * @param TXmlElement config xml element
	 * @param string base path corresponding to this xml element
	 * @param string the page path that the config XML is associated with. The page path doesn't include the page name.
	 */
	public function loadPageConfigurationFromXml($dom,$configPath,$configPagePath)
	{
		// authorization
		if(($authorizationNode=$dom->getElementByTagName('authorization'))!==null)
		{
			$rules=array();
			foreach($authorizationNode->getElements() as $node)
			{
				$patterns=$node->getAttribute('pages');
				$ruleApplies=false;
				if(empty($patterns) || trim($patterns)==='*') // null or empty string
					$ruleApplies=true;
				else
				{
					foreach(explode(',',$patterns) as $pattern)
					{
						if(($pattern=trim($pattern))!=='')
						{
							// we know $configPagePath and $this->_pagePath
							if($configPagePath!=='')  // prepend the pattern with ConfigPagePath
								$pattern=$configPagePath.'.'.$pattern;
							if(strcasecmp($pattern,$this->_pagePath)===0)
							{
								$ruleApplies=true;
								break;
							}
							if($pattern[strlen($pattern)-1]==='*') // try wildcard matching
							{
								if(strncasecmp($this->_pagePath,$pattern,strlen($pattern)-1)===0)
								{
									$ruleApplies=true;
									break;
								}
							}
						}
					}
				}
				if($ruleApplies)
					$rules[]=new TAuthorizationRule($node->getTagName(),$node->getAttribute('users'),$node->getAttribute('roles'),$node->getAttribute('verb'),$node->getAttribute('ips'));
			}
			$this->_rules=array_merge($rules,$this->_rules);
		}

		// pages
		if(($pagesNode=$dom->getElementByTagName('pages'))!==null)
		{
			$this->_properties=array_merge($this->_properties,$pagesNode->getAttributes()->toArray());
			// at the page folder
			foreach($pagesNode->getElementsByTagName('page') as $node)
			{
				$properties=$node->getAttributes();
				$id=$properties->remove('id');
				if(empty($id))
					throw new TConfigurationException('pageserviceconf_page_invalid',$configPath);
				$matching=false;
				$id=($configPagePath==='')?$id:$configPagePath.'.'.$id;
				if(strcasecmp($id,$this->_pagePath)===0)
					$matching=true;
				else if($id[strlen($id)-1]==='*') // try wildcard matching
					$matching=strncasecmp($this->_pagePath,$id,strlen($id)-1)===0;
				if($matching)
					$this->_properties=array_merge($this->_properties,$properties->toArray());
			}
		}

		// external configurations
		foreach($dom->getElementsByTagName('include') as $node)
		{
			if(($when=$node->getAttribute('when'))===null)
				$when=true;
			if(($filePath=$node->getAttribute('file'))===null)
				throw new TConfigurationException('pageserviceconf_includefile_required');
			if(isset($this->_includes[$filePath]))
				$this->_includes[$filePath]=array($configPagePath,'('.$this->_includes[$filePath][1].') || ('.$when.')');
			else
				$this->_includes[$filePath]=array($configPagePath,$when);
		}
	}
}