svn: /web/php/trunk/ quickref.php ws.php
| From: | Rasmus Lerdorf | Date: | Sun, 17 Jul 2011 00:02:14 +0000 |
| Subject: | svn: /web/php/trunk/ quickref.php ws.php | ||
| Groups: | php.webmaster | ||
| Request: | Send a blank email to php-webmaster+get-11670@lists.php.net to get a copy of this message | ||
rasmus Sun, 17 Jul 2011 00:02:14 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=313301
Log:
Add a limited intermediate result display cache
Changed paths:
U web/php/trunk/quickref.php
U web/php/trunk/ws.php
Modified: web/php/trunk/quickref.php
===================================================================
--- web/php/trunk/quickref.php 2011-07-16 22:22:06 UTC (rev 313300)
+++ web/php/trunk/quickref.php 2011-07-17 00:02:14 UTC (rev 313301)
@@ -146,8 +146,10 @@
<?php quickref_table($maybe, false); ?>
<?php
-// Don't do a web search if the search term contains tp:// since we are seeing a lot of proxy
attempts through the 404 handler
-if(strlen($notfound) > 2 && !strstr($notfound,'tp://')):
+// Don't do a web search if the search term contains:
+// tp:// since we are seeing a lot of proxy attempts through the 404 handler
+// admin/ since these tend to be script-kiddie hack attempts
+if(strlen($notfound) > 2 && !strstr($notfound,'tp://') &&
!strstr($notfound,'admin/')):
$srch_rqst =
"/ws.php?profile=$scope&q=".urlencode($notfound)."&lang=$LANG&results=10&start=0&mirror=".trim(substr($MYSITE,7),'/');
$url = "http://www.php.net".$srch_rqst;
$data = fetch_contents($url);
Modified: web/php/trunk/ws.php
===================================================================
--- web/php/trunk/ws.php 2011-07-16 22:22:06 UTC (rev 313300)
+++ web/php/trunk/ws.php 2011-07-17 00:02:14 UTC (rev 313301)
@@ -36,11 +36,47 @@
$scope = 'all';
}
+// mtime is stored on the first line of the result file to
+// avoid needing to call clearstatcache()
+function limited_intermediate_result_cache($url,$ttl=1800, &$error=NULL) {
+ $cfile = '/var/tmp/bing_'.md5($url).'.json';
+ $mtime = 0;
+ $data = '';
+
+ $fp = @fopen($cfile, 'r');
+ if($fp) {
+ $mtime = (int)fgets($fp);
+ $data = trim(stream_get_contents($fp));
+ fclose($fp);
+ }
+ if(!$mtime || ($time_out && ($mtime < ($_SERVER['REQUEST_TIME']-$time_out))))
{
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 5);
+ $tmp = curl_exec($ch);
+ $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ if($status_code!=200) {
+ $error = array('status_code'=>$status_code,'body'=>$data);
+ fclose($fp);
+ if(strlen($data)) return $data;
+ else return false;
+ } else $data = $tmp;
+ $fp = fopen($cfile, 'w');
+ fputs($fp, $_SERVER['REQUEST_TIME']."\n");
+ fputs($fp, $data);
+ fflush($fp);
+ fclose($fp);
+ }
+ return $data;
+}
+
$request =
"{$conf['svc']}?appid={$conf['appid']}&query=$q%20site:{$sites[$scope]}&version=2.2&Sources=Web&web.offset=$s&web.count=$r&market=$market";
-$data = @file_get_contents($request);
-list($version,$status_code,$msg) = explode(' ',$http_response_header[0], 3);
-if($status_code==200) echo ws_bing_massage($data);
-else echo serialize($http_response_header[0]);
+$error = '';
+$data = limited_intermediate_result_cache($request, $error);
+if($data) echo ws_bing_massage($data);
+else echo serialize($error);
function ws_bing_massage($data) {
$results = json_decode($data, true);