-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathexamples.xml
230 lines (201 loc) · 6.4 KB
/
examples.xml
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
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 39899ee59d6d6ffa387d1d3e34b49b56fb08e151 Maintainer: takagi Status: ready -->
<!-- CREDITS: hirokawa,mumumu -->
<chapter xml:id="ldap.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<section xml:id="ldap.examples-basic">
<title>基本的な使用法</title>
<para>
あるディレクトリサーバーから姓が "S" から始まる全てのエントリに
関する情報を検索し、名前と電子メールアドレスで検索結果を表示します。
</para>
<example>
<title>LDAP 検索の例</title>
<programlisting role="php">
<![CDATA[
<?php
// LDAP の基本シーケンスは、接続、バインド、検索、検索結果の解釈、
// 接続のクローズです。
echo "<h3>LDAP query test</h3>";
echo "Connecting ...";
$ds=ldap_connect("localhost"); // 有効な LDAP サーバーに違いない!
echo "connect result is " . $ds . "<br />";
if ($ds) {
echo "Binding ...";
$r=ldap_bind($ds); // これは "匿名" バインドで、通常は
// 読みこみのみのアクセスとなります。
echo "Bind result is " . $r . "<br />";
echo "Searching for (sn=S*) ...";
// 名前(surname)エントリを検索
$sr=ldap_search($ds, "o=My Company, c=US", "sn=S*");
echo "Search result is " . $sr . "<br />";
echo "Number of entries returned is " . ldap_count_entries($ds, $sr) . "<br />";
echo "Getting entries ...<p>";
$info = ldap_get_entries($ds, $sr);
echo "Data for " . $info["count"] . " items returned:<p>";
for ($i=0; $i<$info["count"]; $i++) {
echo "dn is: " . $info[$i]["dn"] . "<br />";
echo "first cn entry is: " . $info[$i]["cn"][0] . "<br />";
echo "first email entry is: " . $info[$i]["mail"][0] . "<br /><hr />";
}
echo "Closing connection";
ldap_close($ds);
} else {
echo "<h4>Unable to connect to LDAP server</h4>";
}
?>
]]>
</programlisting>
</example>
</section>
<section xml:id="ldap.examples-controls">
<title>LDAP コントロール</title>
<para>
PHP >= 7.3.0 で LDAPコントロールを使う例をいくつか示します。
</para>
<example>
<title>policy(パスワードポリシー) 情報とバインドする</title>
<programlisting role="php">
<![CDATA[
<?php
$user = 'cn=admin,dc=example,dc=com';
$passwd = 'adminpassword';
$ds = ldap_connect('ldap://localhost');
if ($ds) {
$r = ldap_bind_ext($ds, $user, $passwd, [['oid' => LDAP_CONTROL_PASSWORDPOLICYREQUEST]]);
if (ldap_parse_result($ds, $r, $errcode, $matcheddn, $errmsg, $referrals, $ctrls)) {
if ($errcode != 0) {
die("Error: $errmsg ($errcode)");
}
if (isset($ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE])) {
$value = $ctrls[LDAP_CONTROL_PASSWORDPOLICYRESPONSE]['value'];
echo "Expires in: ".$value['expire']." seconds\n";
echo "Number of auth left: ".$value['grace']."\n";
if (isset($value['error'])) {
echo "Policy error code: ".$value['error'];
}
}
}
} else {
die("Unable to connect to LDAP server");
}
?>
]]>
</programlisting>
</example>
<example>
<title>空でない場合に限って、説明を変更する</title>
<programlisting role="php">
<![CDATA[
<?php
// $link は、 LDAP 接続です
$result = ldap_mod_replace_ext(
$link,
'o=test,dc=example,dc=com',
['description' => 'New description'],
[
[
'oid' => LDAP_CONTROL_ASSERT,
'iscritical' => TRUE,
'value' => ['filter' => '(!(description=*))']
]
]
);
// ここからは、ldap_parse_result を使う
?>
]]>
</programlisting>
</example>
<example>
<title>削除する前に、いくつか値を読み取る</title>
<programlisting role="php">
<![CDATA[
<?php
// $link は、 LDAP 接続です
$result = ldap_delete_ext(
$link,
'o=test,dc=example,dc=com',
[
[
'oid' => LDAP_CONTROL_PRE_READ,
'iscritical' => TRUE,
'value' => ['attrs' => ['o', 'description']]
]
]
);
// ここからは、ldap_parse_result を使う
?>
]]>
</programlisting>
</example>
<example>
<title>参照を削除する</title>
<programlisting role="php">
<![CDATA[
<?php
// $link は、 LDAP 接続です
// コントロールが存在しない場合、参照されているノードを削除します。
// それを避けるためにも、control を critical として必ず設定してください。
$result = ldap_delete_ext(
$link,
'cn=reference,dc=example,dc=com',
[['oid' => LDAP_CONTROL_MANAGEDSAIT, 'iscritical' => TRUE]]
);
// ここからは、ldap_parse_result を使う
?>
]]>
</programlisting>
</example>
<example>
<title>検索結果を複数ページに分割する</title>
<programlisting role="php">
<![CDATA[
<?php
// $link は、 LDAP 接続です
$cookie = '';
do {
$result = ldap_search(
$link, 'dc=example,dc=base', '(cn=*)', ['cn'], 0, 0, 0, LDAP_DEREF_NEVER,
[['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 2, 'cookie' => $cookie]]]
);
ldap_parse_result($link, $result, $errcode , $matcheddn , $errmsg , $referrals, $controls);
// 例を短くするために、エラー処理はテストしていません
$entries = ldap_get_entries($link, $result);
foreach ($entries as $entry) {
echo "cn: ".$entry['cn'][0]."\n";
}
if (isset($controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'])) {
// 最後の呼び出しから得られる cookie を次に引き渡す必要があります
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'];
} else {
$cookie = '';
}
// cookie が空の場合、最後のページ
} while (strlen($cookie) > 0);
?>
]]>
</programlisting>
</example>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->