コード例 #1
0
ファイル: http.php プロジェクト: kosir/thatcamp-org
 function check($url)
 {
     $url = $this->clean_url($url);
     //Note : Snoopy doesn't work too well with HTTPS URLs.
     $result = array('broken' => false, 'timeout' => false);
     $log = '';
     //Get the timeout setting from the BLC configuration.
     $conf = blc_get_configuration();
     $timeout = $conf->options['timeout'];
     $start_time = microtime_float();
     //Fetch the URL with Snoopy
     $snoopy = new Snoopy();
     $snoopy->read_timeout = $timeout;
     //read timeout in seconds
     $snoopy->agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
     //masquerade as IE 7
     $snoopy->referer = home_url();
     //valid referer helps circumvent some hotlink protection schemes
     $snoopy->maxlength = 1024 * 5;
     //load up to 5 kilobytes
     $snoopy->fetch($this->urlencodefix($url));
     $result['request_duration'] = microtime_float() - $start_time;
     $result['http_code'] = $snoopy->status;
     //HTTP status code
     //Snoopy returns -100 on timeout
     if ($result['http_code'] == -100) {
         $result['http_code'] = 0;
         $result['timeout'] = true;
     }
     //Build the log
     $log .= '=== ';
     if ($result['http_code']) {
         $log .= sprintf(__('HTTP code : %d', 'broken-link-checker'), $result['http_code']);
     } else {
         $log .= __('(No response)', 'broken-link-checker');
     }
     $log .= " ===\n\n";
     if ($snoopy->error) {
         $log .= $snoopy->error . "\n";
     }
     if ($snoopy->timed_out) {
         $log .= __("Request timed out.", 'broken-link-checker') . "\n";
         $result['timeout'] = true;
     }
     if (is_array($snoopy->headers)) {
         $log .= implode("", $snoopy->headers) . "\n";
     }
     //those headers already contain newlines
     //Redirected?
     if ($snoopy->lastredirectaddr) {
         $result['final_url'] = $snoopy->lastredirectaddr;
         $result['redirect_count'] = $snoopy->_redirectdepth;
     } else {
         $result['final_url'] = $url;
     }
     //Determine if the link counts as "broken"
     $result['broken'] = $this->is_error_code($result['http_code']) || $result['timeout'];
     $log .= "<em>(" . __('Using Snoopy', 'broken-link-checker') . ")</em>";
     $result['log'] = $log;
     //The hash should contain info about all pieces of data that pertain to determining if the
     //link is working.
     $result['result_hash'] = implode('|', array($result['http_code'], $result['broken'] ? 'broken' : '0', $result['timeout'] ? 'timeout' : '0', blcLink::remove_query_string($result['final_url'])));
     return $result;
 }