File: schema_cache.php

package info (click to toggle)
php-net-ldap2 2.0.12-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 676 kB
  • ctags: 1,474
  • sloc: php: 3,583; xml: 800; makefile: 5
file content (163 lines) | stat: -rw-r--r-- 6,074 bytes parent folder | download | duplicates (7)
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
<?php
/**
* This is a short example on how to use the schema cache facility.
* Schema caching allows you to store the fetched schema on disk
* (or wherever you want, depending on the cache class used) so
* initialisation of Net_LDAP2 becomes a little faster.
*
* Two examples will be showed here:
* 1. how to use the packaged file based cache
* 2. how to write a custom cache class
*/

// Class includes; this assumes Net_LDAP2 installed in PHPs include path
// or under subfolder "Net" in the local directory.
require_once 'Net/LDAP2.php';

// Configuration
// host can be a single server (string) or multiple ones - if we define more
// servers here (array), we can implement a basic fail over scenario.
// If no credentials (binddn and bindpw) are given, Net_LDAP2 establishes
// an anonymous bind.
// See the documentation for more information on the configuration items!
$ldap_config = array(
// 'host'    => 'ldap.example.org',
    'host'    => array('ldap1.example.org', 'ldap2.example.org'),
// 'binddn'  => 'cn=admin,o=example,dc=org',
// 'bindpw'  => 'your-secret-password',
    'tls'     => false,
    'base'    => 'o=example,dc=org',
    'port'    => 389,
    'version' => 3,
    'filter'  => '(cn=*)',
    'scope'   => 'sub'
);


/*
* EXAMPLE 1: How to use the packaged file based cach
*            This cache class stores the schema object on disk once Net_LDAP2
*            initially fetched it from the LDAP server. This will make Net_LDAP2
*            use the disk version instead of loading the schema from LDAP
*            unless the schema object becomes too old.
*/

// Configuring the schema cacher
// see the source code of SimpleFileSchemaCache.php for config options
// An interesting idea is, to store the file in some tmpfs mount, which will
// result in storing the schema cache in memory instead of disk.
$mySchemaCache_cfg = array(
    'path'    =>  '/tmp/Net_LDAP2_Schema.cache', // place to put cache file
    'max_age' =>  86400 // max age is 24 hours (in seconds)
);

// Initialize cache with the config
$mySchemaCache = new Net_LDAP2_SimpleFileSchemaCache($mySchemaCache_cfg);

// As usual, connect to configured ldap server
$ldap = Net_LDAP2::connect($ldap_config);
if (Net_LDAP2::isError($ldap)) {
    die('BIND FAILED: '.$ldap->getMessage());
}

// and finally register our initialized cache object
$res = $ldap->registerSchemaCache($mySchemaCache);
if (Net_LDAP2::isError($res)) {
    die('REGISTER CACHE FAILED: '.$res->getMessage());
}

// Here we go, Net_LDAP2 will fetch the schema once and then use the disk version.





/*
* EXAMPLE 2: How to write a custom cache class
*            Writing a custom cache class is easy. You just have to wipe out a
*            class that implements the SchemaCache interface.
*            How a cache class must look like is documented in the interface
*            definition file: SchemaCache.interface.php
*            Here we will write a small hacky cache that stores the schema
*            in the php session. This gives us a nice per-user cache that
*            survives for the php session. This cache will obviously not
*            be so performant as the SimpleFileSchemaCache but may be
*            useful for other purposes.
*/

// Firstly, we need our custom schema class...
class MySessionSchemaCache implements Net_LDAP2_SchemaCache {
    /**
    * Initilize the cache
    *
    * Here we do nothing. You can use the class constructor for everything you
    * want, but typically it is used to configure the caches config.
    */
    public function MySessionSchemaCache () {
        // nothing to see here, move along...
    }

    /**
    * Load schema from session
    *
    * For the sake of simplicity we dont implement a cache aging here.
    * This is not a big problem, since php sessions shouldnt last endlessly.
    *
    * @return Net_LDAP2_Schema|Net_LDAP2_Error|false
    */
    public function loadSchema() {
        // Lets see if we have a session, otherwise we cant use this cache
        // and drop some error that will be returned by Net_LDAP2->schema().
        // Minor errors should be indicated by returning false, so Net_LDAP2
        // can continue its work. This will result in the same behavior as
        // no schema cache would have been registered.
        if (!isset($_SESSION)) {
            return new Net_LDAP2_Error(__CLASS__.": No PHP Session initialized.".
                                       " This cache needs an open PHP session.");
        }

        // Here the session is valid, so we return the stores schema.
        // If we cant find the schema (because cahce is empty),w e return
        // false to inidicate a minor error to Net_LDAP2.
        // This causes it to fetch a fresh object from LDAP.
        if (array_key_exists(__CLASS__, $_SESSION)
        && $_SESSION[__CLASS__] instanceof Net_LDAP2_SchemaCache) {
            return $_SESSION[__CLASS__];
        } else {
            return false;
        }
    }

    /**
    * Store the schema object in session
    *
    * @return true|Net_LDAP2_Error
    */
    public function storeSchema($schema) {
        // Just dump the given object into the session
        // unless in loadSchema(), it is important to only return
        // Net_LDAP2_Error objects if something crucial went wrong.
        // If you feel that you want to return an error object, be sure
        // that you have read the comments in Net_LDAP2_SchemaCache.interface.php
        // or you will seriously hurt the performance of your application!!!!
        $_SESSION[__CLASS__] = $schema;
        return true;
    }
}


// Ok, now we have our finished cache object. Now initialize and register it
// the usual way:
$mySchemaCache = new MySessionSchemaCache();

$ldap          = Net_LDAP2::connect($ldap_config);
if (Net_LDAP2::isError($ldap)) {
    die('BIND FAILED: '.$ldap->getMessage());
}

$res = $ldap->registerSchemaCache($mySchemaCache);
if (Net_LDAP2::isError($res)) {
    die('REGISTER CACHE FAILED: '.$res->getMessage());
}

// Now, the Schema is cached in the PHP session :)