summaryrefslogtreecommitdiff
path: root/lib/util.php
diff options
context:
space:
mode:
authorEvan Prodromou <evan@prodromou.name>2008-05-27 16:07:21 -0400
committerEvan Prodromou <evan@prodromou.name>2008-05-27 16:07:21 -0400
commit9977591b78210bcd200376e1476809db12384f2e (patch)
treef38089e726d1799951de298f59c962a42a21f48c /lib/util.php
parent90b4873a00b0d8b4249a323fc84a7460024f491b (diff)
server-side storage model
First pass at a server-side storage model. New tables for consumers, tokens, and nonces, with associated classes. An OAuthDataStore class interfaces with the OAuth.php library to enable server logic. Some additional work to get pretty-OK random number generation into the utilities library. Use /dev/urandom if available; else use mt_rand(). darcs-hash:20080527200721-84dde-308c047af2ebc2c4d753c1e1e24af20fef862a7e.gz
Diffstat (limited to 'lib/util.php')
-rw-r--r--lib/util.php32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/util.php b/lib/util.php
index 31a2cbd4f..52f25c9d3 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -447,6 +447,38 @@ function common_root_url() {
return "http://".$config['site']['server'].'/'.$pathpart;
}
+# returns $bytes bytes of random data as a hexadecimal string
+# "good" here is a goal and not a guarantee
+
+function common_good_rand($bytes) {
+ # XXX: use random.org...?
+ if (file_exists('/dev/urandom')) {
+ return common_urandom($bytes);
+ } else { # FIXME: this is probably not good enough
+ return common_mtrand($bytes);
+ }
+}
+
+function common_urandom($bytes) {
+ $h = fopen('/dev/urandom', 'rb');
+ # should not block
+ $src = fread($h, $bytes);
+ fclose($h);
+ $enc = '';
+ for ($i = 0; $i < $bytes; $i++) {
+ $enc .= sprintf("%02x", (ord($src[$i])));
+ }
+ return $enc;
+}
+
+function common_mtrand($bytes) {
+ $enc = '';
+ for ($i = 0; $i < $bytes; $i++) {
+ $enc .= sprintf("%02x", mt_rand(0, 255));
+ }
+ return $enc;
+}
+
// XXX: set up gettext
function _t($str) {