19 require_once
'Auth/OpenID/Interface.php';
20 require_once
'Auth/OpenID/Nonce.php';
25 require_once
'Auth/OpenID.php';
30 require_once
'Auth/OpenID/Nonce.php';
79 $associations_table = null,
82 $this->associations_table_name =
"oid_associations";
83 $this->nonces_table_name =
"oid_nonces";
87 if (!(is_object($connection) &&
88 (is_subclass_of($connection,
'db_common') ||
89 is_subclass_of($connection,
90 'auth_openid_databaseconnection')))) {
91 trigger_error(
"Auth_OpenID_SQLStore expected PEAR connection " .
92 "object (got ".get_class($connection).
")",
97 $this->connection = $connection;
104 if (is_subclass_of($this->connection,
'db_common')) {
108 if ($associations_table) {
109 $this->associations_table_name = $associations_table;
113 $this->nonces_table_name = $nonces_table;
116 $this->max_nonce_age = 6 * 60 * 60;
123 $this->connection->autoCommit(
false);
126 $this->sql = array();
138 trigger_error(
"Expected keys in SQL query list: " .
139 implode(
", ", $missing),
145 trigger_error(
"SQL list keys have no SQL strings: " .
146 implode(
", ", $empty),
158 $this->connection->query(
159 sprintf(
"SELECT * FROM %s LIMIT 0",
201 $this->connection->query(sprintf(
"DELETE FROM %s",
202 $this->associations_table_name));
204 $this->connection->query(sprintf(
"DELETE FROM %s",
205 $this->nonces_table_name));
216 $required_sql_keys = array(
225 foreach ($required_sql_keys as $key) {
226 if (!array_key_exists($key, $this->sql)) {
228 }
else if (!$this->sql[$key]) {
233 return array($missing, $empty);
241 $replacements = array(
243 'value' => $this->nonces_table_name,
244 'keys' => array(
'nonce_table',
249 'value' => $this->associations_table_name,
250 'keys' => array(
'assoc_table',
259 foreach ($replacements as $item) {
260 $value = $item[
'value'];
261 $keys = $item[
'keys'];
263 foreach ($keys as $k) {
264 if (is_array($this->sql[$k])) {
265 foreach ($this->sql[$k] as $part_key => $part_value) {
266 $this->sql[$k][$part_key] = sprintf($part_value,
270 $this->sql[$k] = sprintf($this->sql[$k], $value);
288 $this->connection->autoCommit(
true);
291 $this->connection->autoCommit(
false);
302 if (!$this->
tableExists($this->nonces_table_name)) {
303 $r = $this->connection->query($this->sql[
'nonce_table']);
311 if (!$this->
tableExists($this->associations_table_name)) {
312 $r = $this->connection->query($this->sql[
'assoc_table']);
322 $lifetime, $assoc_type)
324 return $this->connection->query($this->sql[
'set_assoc'],
338 $association->handle,
340 $association->secret),
341 $association->issued,
342 $association->lifetime,
343 $association->assoc_type
345 $this->connection->commit();
347 $this->connection->rollback();
356 $result = $this->connection->getRow($this->sql[
'get_assoc'],
370 $result = $this->connection->getAll($this->sql[
'get_assocs'],
387 $this->sql[
'remove_assoc'],
388 array($server_url,
$handle)))) {
389 $this->connection->commit();
391 $this->connection->rollback();
410 if (!$assocs || (count($assocs) == 0)) {
413 $associations = array();
415 foreach ($assocs as $assoc_row) {
417 $assoc_row[
'secret'],
418 $assoc_row[
'issued'],
419 $assoc_row[
'lifetime'],
420 $assoc_row[
'assoc_type']);
422 $assoc->secret = $this->
blobDecode($assoc->secret);
424 if ($assoc->getExpiresIn() == 0) {
427 $associations[] = array($assoc->issued, $assoc);
434 foreach ($associations as $key => $assoc) {
435 $issued[$key] = $assoc[0];
436 $assocs[$key] = $assoc[1];
439 array_multisort($issued, SORT_DESC, $assocs, SORT_DESC,
443 list($issued, $assoc) = $associations[0];
456 $sql = $this->sql[
'add_nonce'];
457 $result = $this->connection->query($sql, array($server_url,
461 $this->connection->rollback();
463 $this->connection->commit();
472 if ( abs(
$timestamp - time()) > $Auth_OpenID_SKEW ) {
490 $ch = substr($str, $i, 1);
492 $result .=
"\\\\\\\\";
493 }
else if (ord($ch) == 0) {
494 $result .=
"\\\\000";
496 $result .=
"\\" . strval(decoct(ord($ch)));
512 while ($i < strlen($str)) {
517 if ($str[$i + 1] !=
"\\") {
518 $octal_digits = substr($str, $i + 1, 3);
519 $dec = octdec($octal_digits);
541 $this->connection->query($this->sql[
'clean_nonce'], array($v));
542 $num = $this->connection->affectedRows();
543 $this->connection->commit();
549 $this->connection->query($this->sql[
'clean_assoc'],
551 $num = $this->connection->affectedRows();
552 $this->connection->commit();