Browse Source

Added new logging class and logging in discoverer (issue #1)

tags/0.6
Chris Smith 15 years ago
parent
commit
21a2f0fee2
2 changed files with 73 additions and 1 deletions
  1. 21
    1
      discoverer.inc.php
  2. 52
    0
      logging.inc.php

+ 21
- 1
discoverer.inc.php View File

@@ -22,6 +22,8 @@
22 22
  * SOFTWARE.
23 23
  */
24 24
 
25
+require_once(dirname(__FILE__) . '/logging.inc.php');
26
+
25 27
 class Server {
26 28
 
27 29
  private $url = null;
@@ -150,6 +152,8 @@ class Discoverer {
150 152
  }
151 153
 
152 154
  private function discover($uri) {
155
+  Logger::log('Performing discovery for %s', $uri);
156
+
153 157
   $this->delegate = $uri;
154 158
   $this->server = null;
155 159
 
@@ -159,6 +163,8 @@ class Discoverer {
159 163
  }
160 164
 
161 165
  private function yadisDiscover($uri, $allowLocation = true) {
166
+  Logger::log('Attempting Yadis discovery on %s', $uri);
167
+
162 168
   $ctx = stream_context_create(array(
163 169
     'http' => array(
164 170
       'header' => "Accept: application/xrds+xml\r\n",
@@ -168,6 +174,7 @@ class Discoverer {
168 174
   $fh = @fopen($uri, 'r', false, $ctx);
169 175
 
170 176
   if (!$fh) {
177
+   Logger::log('Unable to open stream');
171 178
    return false;
172 179
   }
173 180
 
@@ -196,6 +203,7 @@ class Discoverer {
196 203
   $sxml = @new SimpleXMLElement($data); 
197 204
 
198 205
   if (!$sxml) {
206
+   Logger::log('Failed to parse XRDS data as XML');
199 207
    // TODO: Die somehow?
200 208
    return false;
201 209
   }
@@ -207,11 +215,14 @@ class Discoverer {
207 215
    $server = null;
208 216
 
209 217
    foreach ($service->Type as $type) {
218
+    Logger::log('Found service of type %s', $type);
219
+
210 220
     if ((String) $type == 'http://specs.openid.net/auth/2.0/server') {
211 221
      $this->version = 2;
212 222
      $this->server = (String) $service->URI;
213 223
      $this->identity = $this->delegate = 'http://specs.openid.net/auth/2.0/identifier_select';
214 224
      $this->servers[] = $server = new Server($this->server, 2);
225
+     Logger::log('OpenID EP found (server). Server: %s, identity: %s, delegate: %s', $this->server, $this->identity, $this->delegate);
215 226
      $found = true;
216 227
     } else if ((String) $type == 'http://specs.openid.net/auth/2.0/signon') {
217 228
      $this->version = 2;
@@ -224,7 +235,8 @@ class Discoverer {
224 235
       $this->identity = 'http://specs.openid.net/auth/2.0/identifier_select';
225 236
      }
226 237
      $this->delegate = 'http://specs.openid.net/auth/2.0/identifier_select';
227
-  
238
+
239
+     Logger::log('OpenID EP found (signon). Server: %s, identity: %s, delegate: %s', $this->server, $this->identity, $this->delegate);  
228 240
      $found = true;
229 241
     } else {
230 242
      $services[] = (String) $type;
@@ -243,6 +255,7 @@ class Discoverer {
243 255
   $meta = self::getMetaTags($data); 
244 256
 
245 257
   if (isset($meta['x-xrds-location'])) {
258
+   Logger::log('Found XRDS meta tag: %s', $meta['x-xrds-location']);
246 259
    // TODO: Allow relative URLs?
247 260
    return $this->yadisDiscover($meta['x-xrds-location'], false);
248 261
   }
@@ -251,9 +264,12 @@ class Discoverer {
251 264
  }
252 265
 
253 266
  private function htmlDiscover($uri) {
267
+  Logger::log('Performing HTML discovery on %s', $uri);
268
+
254 269
   $fh = @fopen($uri, 'r');
255 270
 
256 271
   if (!$fh) {
272
+   Logger::log('Unable to open stream');
257 273
    return;
258 274
   }
259 275
 
@@ -275,6 +291,8 @@ class Discoverer {
275 291
    $this->identity = self::normalise($this->identity);
276 292
   }
277 293
 
294
+  Logger::log('Identity: %s', $this->identity);
295
+
278 296
   $data = '';
279 297
   while (!feof($fh) && strpos($data, '</head>') === false) {
280 298
    $data .= fgets($fh);
@@ -328,6 +346,7 @@ class Discoverer {
328 346
    if (isset($links['openid2.local_id'])) {
329 347
     $this->delegate = $links['openid2.local_id'];
330 348
    }
349
+   Logger::log('OpenID EP found. Server: %s, identity: %s, delegate: %s', $this->server, $this->identity, $this->delegate);
331 350
   } else if (isset($links['openid.server'])) {
332 351
    $this->version = 1;
333 352
    $this->server = $links['openid.server'];
@@ -336,6 +355,7 @@ class Discoverer {
336 355
    if (isset($links['openid.delegate'])) {
337 356
     $this->delegate = $links['openid.delegate'];
338 357
    }
358
+   Logger::log('OpenID EP found. Server: %s, identity: %s, delegate: %s', $this->server, $this->identity, $this->delegate);
339 359
   }
340 360
  }
341 361
 

+ 52
- 0
logging.inc.php View File

@@ -0,0 +1,52 @@
1
+<?PHP
2
+
3
+/* Poidsy 0.5 - http://chris.smith.name/projects/poidsy
4
+ * Copyright (c) 2008-2009 Chris Smith
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in
14
+ * all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+class Logger {
26
+
27
+ const ENABLE_LOGGING = true;
28
+ const LOGGING_FILENAME = '/tmp/poidsy-debug.log';
29
+
30
+ private static $fh;
31
+
32
+ public static function log($message) {
33
+  if (self::ENABLE_LOGGING) {
34
+   if (self::$fh == null) {
35
+    self::$fh = fopen(self::LOGGING_FILENAME, 'a');
36
+   }
37
+
38
+   $args = func_get_args();
39
+   $arg = call_user_func_array('sprintf', $args);
40
+   fputs(self::$fh, sprintf("[%s] %s: %s\n", date('r'), self::getCaller(), $arg));
41
+  }
42
+ }
43
+
44
+ protected static function getCaller() {
45
+  $trace = debug_backtrace(); // First two will be log and getCaller
46
+  $trace = $trace[2];
47
+  return sprintf('%s:%s %s%s%s(%s)', basename($trace['file']), $trace['line'], $trace['class'], $trace['type'], $trace['function'], implode(', ', $trace['args']));
48
+ }
49
+
50
+}
51
+
52
+?>

Loading…
Cancel
Save