From 754b0d53c899ee0190172b464aa1724b1d71cefb Mon Sep 17 00:00:00 2001 From: Simon Josefsson Date: Mon, 22 Feb 2010 12:55:29 +0000 Subject: [PATCH] Re-add, some duplication but needed by KSMdecryptOTP. --- ykval-common.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/ykval-common.php b/ykval-common.php index 3e363e3..79e7041 100644 --- a/ykval-common.php +++ b/ykval-common.php @@ -117,6 +117,84 @@ function modhex2b64 ($modhex_str) { return hex2b64($hex_str); } +// This function takes a list of URLs. It will return the content of +// the first successfully retrieved URL, whose content matches ^OK. +// The request are sent asynchronously. Some of the URLs can fail +// with unknown host, connection errors, or network timeout, but as +// long as one of the URLs given work, data will be returned. If all +// URLs fail, data from some URL that did not match parameter $match +// (defaults to ^OK) is returned, or if all URLs failed, false. +function retrieveURLasync ($urls, $ans_req=1, $match="^OK", $returl=False) { + $mh = curl_multi_init(); + + $ch = array(); + foreach ($urls as $id => $url) { + $handle = curl_init(); + debug("url is: " . $url); + curl_setopt($handle, CURLOPT_URL, $url); + curl_setopt($handle, CURLOPT_USERAGENT, "YK-VAL"); + curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($handle, CURLOPT_FAILONERROR, true); + curl_setopt($handle, CURLOPT_TIMEOUT, 10); + + curl_multi_add_handle($mh, $handle); + + $ch[$handle] = $handle; + } + + $str = false; + $ans_count = 0; + $ans_arr = array(); + + do { + while (($mrc = curl_multi_exec($mh, $active)) == CURLM_CALL_MULTI_PERFORM) + ; + + while ($info = curl_multi_info_read($mh)) { + debug ("YK-KSM multi", $info); + if ($info['result'] == CURL_OK) { + $str = curl_multi_getcontent($info['handle']); + debug($str); + if (preg_match("/".$match."/", $str)) { + $error = curl_error ($info['handle']); + $errno = curl_errno ($info['handle']); + $cinfo = curl_getinfo ($info['handle']); + debug("YK-KSM errno/error: " . $errno . "/" . $error, $cinfo); + $ans_count++; + debug("found entry"); + if ($returl) $ans_arr[]="url=" . $cinfo['url'] . "\n" . $str; + else $ans_arr[]=$str; + } + + if ($ans_count >= $ans_req) { + foreach ($ch as $h) { + curl_multi_remove_handle ($mh, $h); + curl_close ($h); + } + curl_multi_close ($mh); + + if ($ans_count==1) return $ans_arr[0]; + else return $ans_arr; + } + + curl_multi_remove_handle ($mh, $info['handle']); + curl_close ($info['handle']); + unset ($ch[$info['handle']]); + } + + curl_multi_select ($mh); + } + } while($active); + + foreach ($ch as $h) { + curl_multi_remove_handle ($mh, $h); + curl_close ($h); + } + curl_multi_close ($mh); + + return $str; +} + // $otp: A yubikey OTP function KSMdecryptOTP($urls) { $ret = array();