| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* This is a short example on how to use the schema cache facility.
|
|
|
4 |
* Schema caching allows you to store the fetched schema on disk
|
|
|
5 |
* (or wherever you want, depending on the cache class used) so
|
|
|
6 |
* initialisation of Net_LDAP2 becomes a little faster.
|
|
|
7 |
*
|
|
|
8 |
* Two examples will be showed here:
|
|
|
9 |
* 1. how to use the packaged file based cache
|
|
|
10 |
* 2. how to write a custom cache class
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
// Class includes; this assumes Net_LDAP2 installed in PHPs include path
|
|
|
14 |
// or under subfolder "Net" in the local directory.
|
|
|
15 |
require_once 'Net/LDAP2.php';
|
|
|
16 |
|
|
|
17 |
// Configuration
|
|
|
18 |
// host can be a single server (string) or multiple ones - if we define more
|
|
|
19 |
// servers here (array), we can implement a basic fail over scenario.
|
|
|
20 |
// If no credentials (binddn and bindpw) are given, Net_LDAP2 establishes
|
|
|
21 |
// an anonymous bind.
|
|
|
22 |
// See the documentation for more information on the configuration items!
|
|
|
23 |
$ldap_config = array(
|
|
|
24 |
// 'host' => 'ldap.example.org',
|
|
|
25 |
'host' => array('ldap1.example.org', 'ldap2.example.org'),
|
|
|
26 |
// 'binddn' => 'cn=admin,o=example,dc=org',
|
|
|
27 |
// 'bindpw' => 'your-secret-password',
|
|
|
28 |
'tls' => false,
|
|
|
29 |
'base' => 'o=example,dc=org',
|
|
|
30 |
'port' => 389,
|
|
|
31 |
'version' => 3,
|
|
|
32 |
'filter' => '(cn=*)',
|
|
|
33 |
'scope' => 'sub'
|
|
|
34 |
);
|
|
|
35 |
|
|
|
36 |
|
|
|
37 |
/*
|
|
|
38 |
* EXAMPLE 1: How to use the packaged file based cach
|
|
|
39 |
* This cache class stores the schema object on disk once Net_LDAP2
|
|
|
40 |
* initially fetched it from the LDAP server. This will make Net_LDAP2
|
|
|
41 |
* use the disk version instead of loading the schema from LDAP
|
|
|
42 |
* unless the schema object becomes too old.
|
|
|
43 |
*/
|
|
|
44 |
|
|
|
45 |
// Configuring the schema cacher
|
|
|
46 |
// see the source code of SimpleFileSchemaCache.php for config options
|
|
|
47 |
// An interesting idea is, to store the file in some tmpfs mount, which will
|
|
|
48 |
// result in storing the schema cache in memory instead of disk.
|
|
|
49 |
$mySchemaCache_cfg = array(
|
|
|
50 |
'path' => '/tmp/Net_LDAP2_Schema.cache', // place to put cache file
|
|
|
51 |
'max_age' => 86400 // max age is 24 hours (in seconds)
|
|
|
52 |
);
|
|
|
53 |
|
|
|
54 |
// Initialize cache with the config
|
|
|
55 |
$mySchemaCache = new Net_LDAP2_SimpleFileSchemaCache($mySchemaCache_cfg);
|
|
|
56 |
|
|
|
57 |
// As usual, connect to configured ldap server
|
|
|
58 |
$ldap = Net_LDAP2::connect($ldap_config);
|
|
|
59 |
if (Net_LDAP2::isError($ldap)) {
|
|
|
60 |
die('BIND FAILED: '.$ldap->getMessage());
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// and finally register our initialized cache object
|
|
|
64 |
$res = $ldap->registerSchemaCache($mySchemaCache);
|
|
|
65 |
if (Net_LDAP2::isError($res)) {
|
|
|
66 |
die('REGISTER CACHE FAILED: '.$res->getMessage());
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
// Here we go, Net_LDAP2 will fetch the schema once and then use the disk version.
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
/*
|
|
|
76 |
* EXAMPLE 2: How to write a custom cache class
|
|
|
77 |
* Writing a custom cache class is easy. You just have to wipe out a
|
|
|
78 |
* class that implements the SchemaCache interface.
|
|
|
79 |
* How a cache class must look like is documented in the interface
|
|
|
80 |
* definition file: SchemaCache.interface.php
|
|
|
81 |
* Here we will write a small hacky cache that stores the schema
|
|
|
82 |
* in the php session. This gives us a nice per-user cache that
|
|
|
83 |
* survives for the php session. This cache will obviously not
|
|
|
84 |
* be so performant as the SimpleFileSchemaCache but may be
|
|
|
85 |
* useful for other purposes.
|
|
|
86 |
*/
|
|
|
87 |
|
|
|
88 |
// Firstly, we need our custom schema class...
|
|
|
89 |
class MySessionSchemaCache implements Net_LDAP2_SchemaCache {
|
|
|
90 |
/**
|
|
|
91 |
* Initilize the cache
|
|
|
92 |
*
|
|
|
93 |
* Here we do nothing. You can use the class constructor for everything you
|
|
|
94 |
* want, but typically it is used to configure the caches config.
|
|
|
95 |
*/
|
|
|
96 |
public function MySessionSchemaCache () {
|
|
|
97 |
// nothing to see here, move along...
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Load schema from session
|
|
|
102 |
*
|
|
|
103 |
* For the sake of simplicity we dont implement a cache aging here.
|
|
|
104 |
* This is not a big problem, since php sessions shouldnt last endlessly.
|
|
|
105 |
*
|
|
|
106 |
* @return Net_LDAP2_Schema|Net_LDAP2_Error|false
|
|
|
107 |
*/
|
|
|
108 |
public function loadSchema() {
|
|
|
109 |
// Lets see if we have a session, otherwise we cant use this cache
|
|
|
110 |
// and drop some error that will be returned by Net_LDAP2->schema().
|
|
|
111 |
// Minor errors should be indicated by returning false, so Net_LDAP2
|
|
|
112 |
// can continue its work. This will result in the same behavior as
|
|
|
113 |
// no schema cache would have been registered.
|
|
|
114 |
if (!isset($_SESSION)) {
|
|
|
115 |
return new Net_LDAP2_Error(__CLASS__.": No PHP Session initialized.".
|
|
|
116 |
" This cache needs an open PHP session.");
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
// Here the session is valid, so we return the stores schema.
|
|
|
120 |
// If we cant find the schema (because cahce is empty),w e return
|
|
|
121 |
// false to inidicate a minor error to Net_LDAP2.
|
|
|
122 |
// This causes it to fetch a fresh object from LDAP.
|
|
|
123 |
if (array_key_exists(__CLASS__, $_SESSION)
|
|
|
124 |
&& $_SESSION[__CLASS__] instanceof Net_LDAP2_SchemaCache) {
|
|
|
125 |
return $_SESSION[__CLASS__];
|
|
|
126 |
} else {
|
|
|
127 |
return false;
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Store the schema object in session
|
|
|
133 |
*
|
|
|
134 |
* @return true|Net_LDAP2_Error
|
|
|
135 |
*/
|
|
|
136 |
public function storeSchema($schema) {
|
|
|
137 |
// Just dump the given object into the session
|
|
|
138 |
// unless in loadSchema(), it is important to only return
|
|
|
139 |
// Net_LDAP2_Error objects if something crucial went wrong.
|
|
|
140 |
// If you feel that you want to return an error object, be sure
|
|
|
141 |
// that you have read the comments in Net_LDAP2_SchemaCache.interface.php
|
|
|
142 |
// or you will seriously hurt the performance of your application!!!!
|
|
|
143 |
$_SESSION[__CLASS__] = $schema;
|
|
|
144 |
return true;
|
|
|
145 |
}
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
// Ok, now we have our finished cache object. Now initialize and register it
|
|
|
150 |
// the usual way:
|
|
|
151 |
$mySchemaCache = new MySessionSchemaCache();
|
|
|
152 |
|
|
|
153 |
$ldap = Net_LDAP2::connect($ldap_config);
|
|
|
154 |
if (Net_LDAP2::isError($ldap)) {
|
|
|
155 |
die('BIND FAILED: '.$ldap->getMessage());
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
$res = $ldap->registerSchemaCache($mySchemaCache);
|
|
|
159 |
if (Net_LDAP2::isError($res)) {
|
|
|
160 |
die('REGISTER CACHE FAILED: '.$res->getMessage());
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
// Now, the Schema is cached in the PHP session :)
|