20 require_once
'Auth/OpenID.php';
21 require_once
'Auth/OpenID/Interface.php';
22 require_once
'Auth/OpenID/HMAC.php';
23 require_once
'Auth/OpenID/Nonce.php';
52 trigger_error(
'Not a directory and failed to create: '
53 . $directory, E_USER_ERROR);
55 $directory = realpath($directory);
57 $this->directory = $directory;
60 $this->nonce_dir = $directory . DIRECTORY_SEPARATOR .
'nonces';
62 $this->association_dir = $directory . DIRECTORY_SEPARATOR .
67 $this->temp_dir = $directory . DIRECTORY_SEPARATOR .
'temp';
69 $this->max_nonce_age = 6 * 60 * 60;
72 trigger_error(
'Failed to initialize OpenID file store in ' .
73 $directory, E_USER_ERROR);
80 $this->active =
false;
111 $file_obj = @fopen($name,
'wb');
112 if ($file_obj !==
false) {
113 return array($file_obj, $name);
128 foreach ($nonces as $nonce_fname) {
129 $base = basename($nonce_fname);
130 $parts = explode(
'-', $base, 2);
133 if (abs(
$timestamp - $now) > $Auth_OpenID_SKEW) {
152 if (!$this->active) {
153 trigger_error(
"FileStore no longer active", E_USER_ERROR);
157 if (strpos($server_url,
'://') ===
false) {
158 trigger_error(sprintf(
"Bad server URL: %s", $server_url),
163 list($proto,
$rest) = explode(
'://', $server_url, 2);
164 $parts = explode(
'/',
$rest);
173 $filename = sprintf(
'%s-%s-%s-%s', $proto, $domain, $url_hash,
176 return $this->association_dir. DIRECTORY_SEPARATOR .
$filename;
184 if (!$this->active) {
185 trigger_error(
"FileStore no longer active", E_USER_ERROR);
189 $association_s = $association->serialize();
191 $association->handle);
192 list($tmp_file, $tmp) = $this->
_mktemp();
195 trigger_error(
"_mktemp didn't return a valid file descriptor",
200 fwrite($tmp_file, $association_s);
234 if (!$this->active) {
235 trigger_error(
"FileStore no longer active", E_USER_ERROR);
239 if ($handle === null) {
252 $matching_files = array();
256 foreach ($association_files as $association_file) {
257 $base = basename($association_file);
258 if (strpos($base, $name) === 0) {
259 $matching_files[] = $association_file;
263 $matching_associations = array();
265 foreach ($matching_files as $full_name) {
267 if ($association !== null) {
268 $matching_associations[] = array($association->issued,
275 foreach ($matching_associations as $key => $assoc) {
276 $issued[$key] = $assoc[0];
277 $assocs[$key] = $assoc[1];
280 array_multisort($issued, SORT_DESC, $assocs, SORT_DESC,
281 $matching_associations);
284 if ($matching_associations) {
285 list($issued, $assoc) = $matching_associations[0];
298 if (!$this->active) {
299 trigger_error(
"FileStore no longer active", E_USER_ERROR);
305 if ($assoc_file ===
false) {
309 $assoc_s = fread($assoc_file, filesize(
$filename));
325 if ($association->getExpiresIn() == 0) {
340 if (!$this->active) {
341 trigger_error(
"FileStore no longer active", E_USER_ERROR);
346 if ($assoc === null) {
364 if (!$this->active) {
365 trigger_error(
"FileStore no longer active", E_USER_ERROR);
369 if ( abs(
$timestamp - time()) > $Auth_OpenID_SKEW ) {
374 list($proto,
$rest) = explode(
'://', $server_url, 2);
380 $parts = explode(
'/',
$rest, 2);
382 $url_hash = $this->
_safe64($server_url);
383 $salt_hash = $this->
_safe64($salt);
386 $domain, $url_hash, $salt_hash);
407 $all_associations = array();
409 $association_filenames =
412 foreach ($association_filenames as $association_filename) {
413 $association_file = fopen($association_filename,
'rb');
415 if ($association_file !==
false) {
416 $assoc_s = fread($association_file,
417 filesize($association_filename));
418 fclose($association_file);
423 'Auth_OpenID_Association', $assoc_s);
425 if ($association === null) {
427 $association_filename);
429 if ($association->getExpiresIn() == 0) {
430 $all_associations[] = array($association_filename,
437 return $all_associations;
442 if (!$this->active) {
443 trigger_error(
"FileStore no longer active", E_USER_ERROR);
451 foreach ($nonces as $nonce) {
453 $filename = $this->nonce_dir . DIRECTORY_SEPARATOR . $nonce;
459 list($assoc_filename, $assoc) = $pair;
460 if ($assoc->getExpiresIn() == 0) {
471 if ($dir[strlen($dir) - 1] != DIRECTORY_SEPARATOR) {
472 $dir .= DIRECTORY_SEPARATOR;
475 if ($handle = opendir($dir)) {
476 while ($item = readdir($handle)) {
477 if (!in_array($item, array(
'.',
'..'))) {
478 if (is_dir($dir . $item)) {
483 }
else if (is_file($dir . $item)) {
484 if (!unlink($dir . $item)) {
509 foreach (range(0, 4) as $i) {
510 $name = tempnam($dir,
"php_openid_filestore_");
512 if ($name !==
false) {
524 foreach (range(0, 4) as $i) {
525 $name = $dir . strval(DIRECTORY_SEPARATOR) . strval(getmypid()) .
526 "-" . strval(rand(1, time()));
527 if (!mkdir($name, 0700)) {
541 $handle = opendir($dir);
543 while (
false !== (
$filename = readdir($handle))) {
544 if (!in_array(
$filename, array(
'.',
'..'))) {
545 $files[] = $dir . DIRECTORY_SEPARATOR .
$filename;
558 return (strpos($_Auth_OpenID_filename_allowed, $char) !==
false);
567 $h64 = str_replace(
'+',
'_', $h64);
568 $h64 = str_replace(
'/',
'.', $h64);
569 $h64 = str_replace(
'=',
'', $h64);
581 for ($i = 0; $i < count($b); $i++) {
608 list($assoc_filename, $assoc) = $pair;
609 if ($assoc->getExpiresIn() == 0) {