PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/core/src/main/php/peer/ldap/LDAPQuery.class.php

https://github.com/treuter/xp-framework
PHP | 321 lines | 279 code | 12 blank | 30 comment | 12 complexity | e764dcab14bca63df94d85e92f06a7e6 MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses('peer.ldap.LDAPClient');
  7. /**
  8. * Class encapsulating LDAP queries.
  9. *
  10. * @see xp://peer.ldap.LDAPClient
  11. * @see rfc://2254
  12. * @test xp://net.xp_framework.unittest.peer.LDAPQueryTest
  13. * @purpose Wrap LDAP queries
  14. */
  15. class LDAPQuery extends Object {
  16. const RECEIVE_TYPES= 1;
  17. const RECEIVE_VALUES= 0;
  18. public
  19. $filter= '',
  20. $scope= 0,
  21. $base= '',
  22. $attrs= array(),
  23. $attrsOnly= self::RECEIVE_VALUES,
  24. $sizelimit= 0,
  25. $timelimit= 0,
  26. $sort= FALSE,
  27. $deref= FALSE;
  28. /**
  29. * Constructor.
  30. *
  31. * @param string base
  32. * @param var[] args
  33. */
  34. public function __construct() {
  35. $args= func_get_args();
  36. $this->base= array_shift($args);
  37. if (sizeof ($args)) $this->filter= $this->_prepare($args);
  38. }
  39. /**
  40. * Format the query as requested by the format identifiers. Values are escaped
  41. * approriately, so they're safe to use in the query.
  42. *
  43. * @param var[] args
  44. * @return string filter
  45. */
  46. protected function _prepare($args) {
  47. $query= $args[0];
  48. if (sizeof($args) <= 1) return $query;
  49. $i= 0;
  50. // This fixes strtok for cases where '%' is the first character
  51. $query= $tok= strtok(' '.$query, '%');
  52. while (++$i && $tok= strtok('%')) {
  53. // Support %1$s syntax
  54. if (is_numeric($tok{0})) {
  55. sscanf($tok, '%d$', $ofs);
  56. $mod= strlen($ofs) + 1;
  57. } else {
  58. $ofs= $i;
  59. $mod= 0;
  60. }
  61. if (is_array($args[$ofs])) {
  62. throw new IllegalArgumentException(
  63. 'Non-scalar or -object given in for LDAP query.'
  64. );
  65. }
  66. // Type-based conversion
  67. if ($args[$ofs] instanceof Date) {
  68. $tok{$mod}= 's';
  69. $arg= $args[$ofs]->toString('YmdHi\\ZO');
  70. } else if ($args[$ofs] instanceof Generic) {
  71. $arg= $args[$ofs]->toString();
  72. } else {
  73. $arg= $args[$ofs];
  74. }
  75. // NULL actually doesn't exist in LDAP, but is being used here to
  76. // clarify things (ie. show that no argument has been passed)
  77. switch ($tok{$mod}) {
  78. case 'd': $r= is_null($arg) ? 'NULL' : sprintf('%.0f', $arg); break;
  79. case 'f': $r= is_null($arg) ? 'NULL' : floatval($arg); break;
  80. case 'c': $r= is_null($arg) ? 'NULL' : $arg; break;
  81. case 's': $r= is_null($arg) ? 'NULL' : strtr($arg, array('(' => '\\28', ')' => '\\29', '\\' => '\\5c', '*' => '\\2a', chr(0) => '\\00')); break;
  82. default: $r= '%'; $mod= -1; $i--; continue;
  83. }
  84. $query.= $r.substr($tok, 1 + $mod);
  85. }
  86. return substr($query, 1);
  87. }
  88. /**
  89. * Prepare a query statement.
  90. *
  91. * @param var[] args
  92. * @return string
  93. */
  94. public function prepare() {
  95. $args= func_get_args();
  96. return $this->_prepare($args);
  97. }
  98. /**
  99. * Set Filter
  100. *
  101. * @param string filter
  102. */
  103. public function setFilter() {
  104. $args= func_get_args();
  105. $this->filter= $this->_prepare($args);
  106. }
  107. /**
  108. * Get Filter
  109. *
  110. * @return string
  111. */
  112. public function getFilter() {
  113. return $this->filter;
  114. }
  115. /**
  116. * Set Scope
  117. *
  118. * @param int scope
  119. */
  120. public function setScope($scope) {
  121. $this->scope= $scope;
  122. }
  123. /**
  124. * Get Scope
  125. *
  126. * @return string
  127. */
  128. public function getScope() {
  129. return $this->scope;
  130. }
  131. /**
  132. * Set Base
  133. *
  134. * @param var[] args
  135. */
  136. public function setBase() {
  137. $args= func_get_args();
  138. $this->base= $this->_prepare($args);
  139. }
  140. /**
  141. * Get Base
  142. *
  143. * @return string
  144. */
  145. public function getBase() {
  146. return $this->base;
  147. }
  148. /**
  149. * Checks whether query has a base specified.
  150. *
  151. * @return bool
  152. */
  153. public function hasBase() {
  154. return (bool)strlen($this->base);
  155. }
  156. /**
  157. * Set Attrs
  158. *
  159. * @param var[] attrs
  160. */
  161. public function setAttrs($attrs) {
  162. $this->attrs= $attrs;
  163. }
  164. /**
  165. * Get Attrs
  166. *
  167. * @return var[]
  168. */
  169. public function getAttrs() {
  170. return $this->attrs;
  171. }
  172. /**
  173. * Set whether to return only attribute types.
  174. *
  175. * @param bool mode
  176. */
  177. public function setAttrsOnly($mode) {
  178. $this->attrsOnly= $mode;
  179. }
  180. /**
  181. * Check whether to return only requested attributes.
  182. *
  183. * @return bool attrsonly
  184. */
  185. public function getAttrsOnly() {
  186. return $this->attrsOnly;
  187. }
  188. /**
  189. * Set Sizelimit
  190. *
  191. * @param int sizelimit
  192. */
  193. public function setSizelimit($sizelimit) {
  194. $this->sizelimit= $sizelimit;
  195. }
  196. /**
  197. * Get Sizelimit
  198. *
  199. * @return int
  200. */
  201. public function getSizelimit() {
  202. return $this->sizelimit;
  203. }
  204. /**
  205. * Set Timelimit
  206. *
  207. * @param int timelimit
  208. */
  209. public function setTimelimit($timelimit) {
  210. $this->timelimit= $timelimit;
  211. }
  212. /**
  213. * Get Timelimit
  214. *
  215. * @return int
  216. */
  217. public function getTimelimit() {
  218. return $this->timelimit;
  219. }
  220. /**
  221. * Set sort fields; the field(s) to sort on must be
  222. * used in the filter, as well, for the sort to take
  223. * place at all.
  224. *
  225. * @see php://ldap_sort
  226. * @param string[] sort array of fields to sort with
  227. */
  228. public function setSort($sort) {
  229. $this->sort= $sort;
  230. }
  231. /**
  232. * Get sort
  233. *
  234. * @return array sort
  235. */
  236. public function getSort() {
  237. return (array)$this->sort;
  238. }
  239. /**
  240. * Set Deref
  241. *
  242. * @param bool deref
  243. */
  244. public function setDeref($deref) {
  245. $this->deref= $deref;
  246. }
  247. /**
  248. * Get Deref
  249. *
  250. * @return bool
  251. */
  252. public function getDeref() {
  253. return $this->deref;
  254. }
  255. /**
  256. * Return a nice string representation of this object.
  257. *
  258. * @return string
  259. */
  260. public function toString() {
  261. $namelen= 0;
  262. $str= $this->getClassName()."@{\n";
  263. foreach (array_keys(get_object_vars($this)) as $index) { $namelen= max($namelen, strlen($index)); }
  264. foreach (get_object_vars($this) as $name => $value) {
  265. if ('_' == $name{0}) continue;
  266. // Nicely convert certain types
  267. if (is_bool($value)) $value= $value ? 'TRUE' : 'FALSE';
  268. if (is_array($value)) $value= implode(', ', $value);
  269. if ('scope' == $name) switch ($value) {
  270. case LDAP_SCOPE_BASE: $value= 'LDAP_SCOPE_BASE'; break;
  271. case LDAP_SCOPE_ONELEVEL: $value= 'LDAP_SCOPE_ONELEVEL'; break;
  272. case LDAP_SCOPE_SUB: $value= 'LDAP_SCOPE_SUB'; break;
  273. }
  274. $str.= sprintf(" [%-".($namelen+5)."s] %s\n",
  275. $name,
  276. $value
  277. );
  278. }
  279. return $str."}\n";
  280. }
  281. }
  282. ?>