File: Query.php

package info (click to toggle)
phpldapadmin 1.2.6.7-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 5,528 kB
  • sloc: php: 17,684; javascript: 5,299; xml: 1,498; sh: 379; python: 148; makefile: 23
file content (284 lines) | stat: -rw-r--r-- 8,507 bytes parent folder | download | duplicates (2)
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
<?php
/**
 * Classes and functions for the query engine.
 *
 * @author The phpLDAPadmin development team
 * @package phpLDAPadmin
 */

/**
 * Query Class
 *
 * @package phpLDAPadmin
 * @subpackage Queries
 */
#[\AllowDynamicProperties]
class Query extends xmlTemplate {
	protected $description = '';
	public $results = array();

	/**
	 * Main processing to store the template.
	 *
	 * @param xmldata Parsed xmldata from xml2array object
	 */
	protected function storeTemplate($xmldata) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();

		foreach ($xmldata['query'] as $xml_key => $xml_value) {
			if (DEBUG_ENABLED)
				debug_log('Foreach loop Key [%s] Value [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key,is_array($xml_value));

			switch ($xml_key) {

				# Build our attribute list from the DN and Template.
				case ('attributes'):
					if (DEBUG_ENABLED)
						debug_log('Case [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key);

					if (is_array($xmldata['query'][$xml_key])) {
						foreach ($xmldata['query'][$xml_key] as $tattrs) {
							foreach ($tattrs as $index => $details) {

								if (DEBUG_ENABLED)
									debug_log('Foreach tattrs Key [%s] Value [%s]',4,0,__FILE__,__LINE__,__METHOD__,
										$index,$details);

								# If there is no schema definition for the attribute, it will be ignored.
								if ($sattr = $server->getSchemaAttribute($index)) {
									if (is_null($attribute = $this->getAttribute($sattr->getName())))
										$attribute = $this->addAttribute($sattr->getName(false),array('values'=>array()));

									$attribute->show();
									$attribute->setXML($details);
								}
							}
						}
					}

					break;

				# Build our bases list from the DN and Template.
				case ('bases'):
					if (isset($xmldata['query'][$xml_key]['base']))
						if (is_array($xmldata['query'][$xml_key]['base']))
							$this->base = $xmldata['query'][$xml_key]['base'];
						else
							$this->base = array($xmldata['query'][$xml_key]['base']);
					else
						error(sprintf(_('In the XML file (%s), [%s] contains an unknown key.'),
							$this->filename,$xml_key),'error','index.php');

					$this->base = array_unique($this->base);
					break;

				default:
					if (DEBUG_ENABLED)
						debug_log('Case [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key);

					# Some key definitions need to be an array, some must not be:
					$allowed_arrays = array('');
					$storelower = array('');
					$storearray = array('');

					# Items that must be stored lowercase
					if (in_array($xml_key,$storelower))
						if (is_array($xml_value))
							foreach ($xml_value as $index => $value)
								$xml_value[$index] = strtolower($value);
						else
							$xml_value = strtolower($xml_value);

					# Items that must be stored as arrays
					if (in_array($xml_key,$storearray) && ! is_array($xml_value))
						$xml_value = array($xml_value);

					# Items that should not be an array
					if (! in_array($xml_key,$allowed_arrays) && is_array($xml_value)) {
						debug_dump(array(__METHOD__,'key'=>$xml_key,'value'=>$xml_value));
						error(sprintf(_('In the XML file (%s), [%s] is an array, it must be a string.'),
							$this->filename,$xml_key),'error');
					}

					$this->$xml_key = $xml_value;
			}
		}

		# Check we have some manditory items.
		foreach (array() as $key) {
			if (! isset($this->$key)
				|| (! is_array($this->$key) && ! trim($this->$key))) {

				$this->setInvalid(sprintf(_('Missing %s in the XML file.'),$key));
				break;
			}
		}
	}

	/**
	 * Accept will run the query and store the results in results()
	 */
	public function accept() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$server = $this->getServer();

		$query = array();
		$query['size_limit'] = get_request('size_limit','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','size_limit'));
		$query['format'] = get_request('format','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','display'));
		$query['orderby'] = get_request('orderby','REQUEST',false,'dn');

		# If this is a custom search, we need to populate are paramters
		if ($this->getID() == 'none') {
			$bases = get_request('base','REQUEST',false,null);
			$query['filter'] = get_request('filter','REQUEST',false,'objectClass=*',false);
			$query['scope'] = get_request('scope','REQUEST',false,'sub');
			$attrs = get_request('display_attrs','REQUEST',false,'');

			$attrs = preg_replace('/\s+/','',$attrs);
			if ($attrs)
				$query['attrs'] = explode(',',$attrs);
			else
				$query['attrs'] = array('*');

		} else {
			$bases = $this->base;
			$query['filter'] = $this->filter;
			$query['scope'] = $this->scope;
			$query['attrs'] = $this->getAttributeNames();
		}

		if (! $bases)
			$bases = $server->getBaseDN();
		elseif (! is_array($bases))
			$bases = explode('|',$bases);

		foreach ($bases as $base) {
			$query['base'] = $base;

			$time_start = utime();
			$this->results[$base] = $server->query($query,null);
			$time_end = utime();

			$this->resultsdata[$base]['time'] = round($time_end-$time_start,2);
			$this->resultsdata[$base]['scope'] = $query['scope'];
			$this->resultsdata[$base]['filter'] = $query['filter'];
			$this->resultsdata[$base]['attrs'] = $query['attrs'];

			if ($this->getAttrSortOrder() == 'dn')
				usort($this->results[$base],'pla_compare_dns');
			elseif ($this->getAttrSortOrder())
				masort($this->results[$base],$this->getAttrSortOrder());
		}
	}

	/**
	 * This is temporary to get around objects that use a DN for rendering, for example jpegPhoto
	 */
	public function setDN($dn) {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs);

		$this->dn = $dn;
	}

	/**
	 * This is temporary to get around objects that use a DN for rendering, for example jpegPhoto
	 */
	public function getDN() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->dn);

		return $this->dn;
	}

	public function getDNEncode($url=true) {
		// @todo Be nice to do all this in 1 location
		if ($url)
			return urlencode(preg_replace('/%([0-9a-fA-F]+)/',"%25\\1",$this->dn));
		else
			return preg_replace('/%([0-9a-fA-F]+)/',"%25\\1",$this->dn);
	}

	public function getAttrSortOrder() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$result = array();

		if (count($this->attributes)) {
			masort($this->attributes,'ordersort');

			foreach ($this->attributes as $attribute)
				array_push($result,$attribute->getName());

		} else {
			$display = preg_replace('/,\s+/',',',get_request('orderby','REQUEST',false,'dn'));

			if (trim($display))
				$result = explode(',',$display);
		}

		return implode(',',$result);
	}

	public function getAttrDisplayOrder() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);

		$result = array();

		if (count($this->attributes)) {
			masort($this->attributes,'order');

			foreach ($this->attributes as $attribute)
				array_push($result,$attribute->getName());

		} else {
			$display = preg_replace('/,\s+/',',',get_request('display_attrs','REQUEST',false,''));

			if (trim($display))
				$result = explode(',',$display);
		}

		# If our display order is empty, then dynamically build it
		if (! count($result)) {
			foreach ($this->results as $details)
				foreach ($details as $attrs)
					$result = array_merge($result,array_keys(array_change_key_case($attrs)));

			$result = array_unique($result);
			sort($result);
		}

		# Put the DN first
		array_unshift($result,'dn');
		$result = array_unique($result);

		return implode(',',$result);
	}

	/**
	 * Test if the template is visible
	 *
	 * @return boolean
	 */
	public function isVisible() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->visible);

		return $this->visible;
	}

	public function getDescription() {
		if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
			debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->description);

		return $this->description;
	}
}
?>