Php73.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\User_LDAP\PagedResults;
  26. /**
  27. * Class Php73
  28. *
  29. * implements paged results support with PHP APIs available from PHP 7.3
  30. *
  31. * @package OCA\User_LDAP\PagedResults
  32. */
  33. class Php73 implements IAdapter {
  34. use TLinkId;
  35. /** @var array */
  36. protected $linkData = [];
  37. public function getResponseCallFunc(): string {
  38. return 'ldap_parse_result';
  39. }
  40. public function responseCall($link): bool {
  41. $linkId = $this->getLinkId($link);
  42. return ldap_parse_result(...$this->linkData[$linkId]['responseArgs']);
  43. }
  44. public function getResponseCallArgs(array $originalArgs): array {
  45. $link = array_shift($originalArgs);
  46. $linkId = $this->getLinkId($link);
  47. if (!isset($this->linkData[$linkId])) {
  48. $this->linkData[$linkId] = [];
  49. }
  50. $this->linkData[$linkId]['responseErrorCode'] = 0;
  51. $this->linkData[$linkId]['responseErrorMessage'] = '';
  52. $this->linkData[$linkId]['serverControls'] = [];
  53. $matchedDn = null;
  54. $referrals = [];
  55. $this->linkData[$linkId]['responseArgs'] = [
  56. $link,
  57. array_shift($originalArgs),
  58. &$this->linkData[$linkId]['responseErrorCode'],
  59. $matchedDn,
  60. &$this->linkData[$linkId]['responseErrorMessage'],
  61. $referrals,
  62. &$this->linkData[$linkId]['serverControls']
  63. ];
  64. return $this->linkData[$linkId]['responseArgs'];
  65. }
  66. public function getCookie($link): string {
  67. $linkId = $this->getLinkId($link);
  68. return $this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
  69. }
  70. private function resetCookie(int $linkId): void {
  71. if (isset($this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
  72. $this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] = '';
  73. }
  74. }
  75. public function getRequestCallFunc(): ?string {
  76. return null;
  77. }
  78. public function setRequestParameters($link, int $pageSize, bool $isCritical): void {
  79. $linkId = $this->getLinkId($link);
  80. if (!isset($this->linkData[$linkId])) {
  81. $this->linkData[$linkId] = [];
  82. }
  83. $this->linkData[$linkId]['requestArgs'] = [];
  84. $this->linkData[$linkId]['requestArgs']['pageSize'] = $pageSize;
  85. $this->linkData[$linkId]['requestArgs']['isCritical'] = $isCritical;
  86. if ($pageSize === 0) {
  87. $this->resetCookie($linkId);
  88. }
  89. }
  90. public function getRequestCallArgs($link): array {
  91. // no separate call
  92. return [];
  93. }
  94. public function requestCall($link): bool {
  95. // no separate call
  96. return false;
  97. }
  98. public function setSearchArgs(
  99. $link,
  100. string $baseDN,
  101. string $filter,
  102. array $attr,
  103. int $attrsOnly,
  104. int $limit
  105. ): void {
  106. $linkId = $this->getLinkId($link);
  107. if (!isset($this->linkData[$linkId])) {
  108. $this->linkData[$linkId] = [];
  109. }
  110. $this->linkData[$linkId]['searchArgs'] = func_get_args();
  111. $this->preparePagesResultsArgs($linkId, 'searchArgs');
  112. }
  113. public function getSearchArgs($link): array {
  114. $linkId = $this->getLinkId($link);
  115. return $this->linkData[$linkId]['searchArgs'];
  116. }
  117. public function setReadArgs($link, string $baseDN, string $filter, array $attr): void {
  118. $linkId = $this->getLinkId($link);
  119. if (!isset($this->linkData[$linkId])) {
  120. $this->linkData[$linkId] = [];
  121. }
  122. $this->linkData[$linkId]['readArgs'] = func_get_args();
  123. $this->linkData[$linkId]['readArgs'][] = 0; // $attrsonly default
  124. $this->linkData[$linkId]['readArgs'][] = -1; // $sizelimit default
  125. }
  126. public function getReadArgs($link): array {
  127. $linkId = $this->getLinkId($link);
  128. return $this->linkData[$linkId]['readArgs'];
  129. }
  130. protected function preparePagesResultsArgs(int $linkId, string $methodKey): void {
  131. if (!isset($this->linkData[$linkId]['requestArgs'])) {
  132. return;
  133. }
  134. $serverControls = [[
  135. 'oid' => LDAP_CONTROL_PAGEDRESULTS,
  136. 'value' => [
  137. 'size' => $this->linkData[$linkId]['requestArgs']['pageSize'],
  138. 'cookie' => $this->linkData[$linkId]['serverControls'][LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '',
  139. ]
  140. ]];
  141. $this->linkData[$linkId][$methodKey][] = -1; // timelimit
  142. $this->linkData[$linkId][$methodKey][] = LDAP_DEREF_NEVER;
  143. $this->linkData[$linkId][$methodKey][] = $serverControls;
  144. }
  145. }