PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/phpldapadmin-1.2.2/lib/Query.php

#
PHP | 283 lines | 182 code | 58 blank | 43 comment | 50 complexity | 444c271b6ed67a35395e3557c20b380b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Classes and functions for the query engine.
  4. *
  5. * @author The phpLDAPadmin development team
  6. * @package phpLDAPadmin
  7. */
  8. /**
  9. * Query Class
  10. *
  11. * @package phpLDAPadmin
  12. * @subpackage Queries
  13. */
  14. class Query extends xmlTemplate {
  15. protected $description = '';
  16. public $results = array();
  17. /**
  18. * Main processing to store the template.
  19. *
  20. * @param xmldata Parsed xmldata from xml2array object
  21. */
  22. protected function storeTemplate($xmldata) {
  23. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  24. debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
  25. $server = $this->getServer();
  26. foreach ($xmldata['query'] as $xml_key => $xml_value) {
  27. if (DEBUG_ENABLED)
  28. debug_log('Foreach loop Key [%s] Value [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key,is_array($xml_value));
  29. switch ($xml_key) {
  30. # Build our attribute list from the DN and Template.
  31. case ('attributes'):
  32. if (DEBUG_ENABLED)
  33. debug_log('Case [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key);
  34. if (is_array($xmldata['query'][$xml_key])) {
  35. foreach ($xmldata['query'][$xml_key] as $tattrs) {
  36. foreach ($tattrs as $index => $details) {
  37. if (DEBUG_ENABLED)
  38. debug_log('Foreach tattrs Key [%s] Value [%s]',4,0,__FILE__,__LINE__,__METHOD__,
  39. $index,$details);
  40. # If there is no schema definition for the attribute, it will be ignored.
  41. if ($sattr = $server->getSchemaAttribute($index)) {
  42. if (is_null($attribute = $this->getAttribute($sattr->getName())))
  43. $attribute = $this->addAttribute($sattr->getName(false),array('values'=>array()));
  44. $attribute->show();
  45. $attribute->setXML($details);
  46. }
  47. }
  48. }
  49. }
  50. break;
  51. # Build our bases list from the DN and Template.
  52. case ('bases'):
  53. if (isset($xmldata['query'][$xml_key]['base']))
  54. if (is_array($xmldata['query'][$xml_key]['base']))
  55. $this->base = $xmldata['query'][$xml_key]['base'];
  56. else
  57. $this->base = array($xmldata['query'][$xml_key]['base']);
  58. else
  59. error(sprintf(_('In the XML file (%s), [%s] contains an unknown key.'),
  60. $this->filename,$xml_key),'error','index.php');
  61. $this->base = array_unique($this->base);
  62. break;
  63. default:
  64. if (DEBUG_ENABLED)
  65. debug_log('Case [%s]',4,0,__FILE__,__LINE__,__METHOD__,$xml_key);
  66. # Some key definitions need to be an array, some must not be:
  67. $allowed_arrays = array('');
  68. $storelower = array('');
  69. $storearray = array('');
  70. # Items that must be stored lowercase
  71. if (in_array($xml_key,$storelower))
  72. if (is_array($xml_value))
  73. foreach ($xml_value as $index => $value)
  74. $xml_value[$index] = strtolower($value);
  75. else
  76. $xml_value = strtolower($xml_value);
  77. # Items that must be stored as arrays
  78. if (in_array($xml_key,$storearray) && ! is_array($xml_value))
  79. $xml_value = array($xml_value);
  80. # Items that should not be an array
  81. if (! in_array($xml_key,$allowed_arrays) && is_array($xml_value)) {
  82. debug_dump(array(__METHOD__,'key'=>$xml_key,'value'=>$xml_value));
  83. error(sprintf(_('In the XML file (%s), [%s] is an array, it must be a string.'),
  84. $this->filename,$xml_key),'error');
  85. }
  86. $this->$xml_key = $xml_value;
  87. }
  88. }
  89. # Check we have some manditory items.
  90. foreach (array() as $key) {
  91. if (! isset($this->$key)
  92. || (! is_array($this->$key) && ! trim($this->$key))) {
  93. $this->setInvalid(sprintf(_('Missing %s in the XML file.'),$key));
  94. break;
  95. }
  96. }
  97. }
  98. /**
  99. * Accept will run the query and store the results in results()
  100. */
  101. public function accept() {
  102. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  103. debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
  104. $server = $this->getServer();
  105. $query = array();
  106. $query['size_limit'] = get_request('size_limit','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','size_limit'));
  107. $query['format'] = get_request('format','REQUEST',false,$_SESSION[APPCONFIG]->getValue('search','display'));
  108. $query['orderby'] = get_request('orderby','REQUEST',false,'dn');
  109. # If this is a custom search, we need to populate are paramters
  110. if ($this->getID() == 'none') {
  111. $bases = get_request('base','REQUEST',false,null);
  112. $query['filter'] = get_request('filter','REQUEST',false,'objectClass=*');
  113. $query['scope'] = get_request('scope','REQUEST',false,'sub');
  114. $attrs = get_request('display_attrs','REQUEST');
  115. $attrs = preg_replace('/\s+/','',$attrs);
  116. if ($attrs)
  117. $query['attrs'] = explode(',',$attrs);
  118. else
  119. $query['attrs'] = array('*');
  120. } else {
  121. $bases = $this->base;
  122. $query['filter'] = $this->filter;
  123. $query['scope'] = $this->scope;
  124. $query['attrs'] = $this->getAttributeNames();
  125. }
  126. if (! $bases)
  127. $bases = $server->getBaseDN();
  128. elseif (! is_array($bases))
  129. $bases = explode('|',$bases);
  130. foreach ($bases as $base) {
  131. $query['base'] = $base;
  132. $time_start = utime();
  133. $this->results[$base] = $server->query($query,null);
  134. $time_end = utime();
  135. $this->resultsdata[$base]['time'] = round($time_end-$time_start,2);
  136. $this->resultsdata[$base]['scope'] = $query['scope'];
  137. $this->resultsdata[$base]['filter'] = $query['filter'];
  138. $this->resultsdata[$base]['attrs'] = $query['attrs'];
  139. if ($this->getAttrSortOrder() == 'dn')
  140. usort($this->results[$base],'pla_compare_dns');
  141. elseif ($this->getAttrSortOrder())
  142. masort($this->results[$base],$this->getAttrSortOrder());
  143. }
  144. }
  145. /**
  146. * This is temporary to get around objects that use a DN for rendering, for example jpegPhoto
  147. */
  148. public function setDN($dn) {
  149. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  150. debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs);
  151. $this->dn = $dn;
  152. }
  153. /**
  154. * This is temporary to get around objects that use a DN for rendering, for example jpegPhoto
  155. */
  156. public function getDN() {
  157. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  158. debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->dn);
  159. return $this->dn;
  160. }
  161. public function getDNEncode($url=true) {
  162. // @todo Be nice to do all this in 1 location
  163. if ($url)
  164. return urlencode(preg_replace('/%([0-9a-fA-F]+)/',"%25\\1",$this->dn));
  165. else
  166. return preg_replace('/%([0-9a-fA-F]+)/',"%25\\1",$this->dn);
  167. }
  168. public function getAttrSortOrder() {
  169. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  170. debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
  171. $result = array();
  172. if (count($this->attributes)) {
  173. masort($this->attributes,'ordersort');
  174. foreach ($this->attributes as $attribute)
  175. array_push($result,$attribute->getName());
  176. } else {
  177. $display = preg_replace('/,\s+/',',',get_request('orderby','REQUEST',false,'dn'));
  178. if (trim($display))
  179. $result = explode(',',$display);
  180. }
  181. return implode(',',$result);
  182. }
  183. public function getAttrDisplayOrder() {
  184. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  185. debug_log('Entered (%%)',5,0,__FILE__,__LINE__,__METHOD__,$fargs);
  186. $result = array();
  187. if (count($this->attributes)) {
  188. masort($this->attributes,'order');
  189. foreach ($this->attributes as $attribute)
  190. array_push($result,$attribute->getName());
  191. } else {
  192. $display = preg_replace('/,\s+/',',',get_request('display_attrs','REQUEST',false,''));
  193. if (trim($display))
  194. $result = explode(',',$display);
  195. }
  196. # If our display order is empty, then dynamically build it
  197. if (! count($result)) {
  198. foreach ($this->results as $details)
  199. foreach ($details as $attrs)
  200. $result = array_merge($result,array_keys(array_change_key_case($attrs)));
  201. $result = array_unique($result);
  202. sort($result);
  203. }
  204. # Put the DN first
  205. array_unshift($result,'dn');
  206. $result = array_unique($result);
  207. return implode(',',$result);
  208. }
  209. /**
  210. * Test if the template is visible
  211. *
  212. * @return boolean
  213. */
  214. public function isVisible() {
  215. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  216. debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->visible);
  217. return $this->visible;
  218. }
  219. public function getDescription() {
  220. if (DEBUG_ENABLED && (($fargs=func_get_args())||$fargs='NOARGS'))
  221. debug_log('Entered (%%)',5,1,__FILE__,__LINE__,__METHOD__,$fargs,$this->description);
  222. return $this->description;
  223. }
  224. }
  225. ?>