1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | <?php // // Open Web Analytics - An Open Source Web Analytics Framework // // Copyright 2006 Peter Adams. All rights reserved. // // Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // $Id$ // if(!class_exists('Snoopy')) { require_once(OWA_INCLUDE_DIR.'/Snoopy.class.php'); } require_once(OWA_HTTPCLIENT_DIR.'http.php'); /** * Wrapper for Snoopy http request class * * @author Peter Adams <peter@openwebanalytics.com> * @copyright Copyright © 2006 Peter Adams <peter@openwebanalytics.com> * @license http://www.gnu.org/copyleft/gpl.html GPL v2.0 * @category owa * @package owa * @version $Revision$ * @since owa 1.0.0 */ class owa_http { /** * Configuration * * @var array */ var $config; /** * Error handler * * @var object */ var $e; /** * The length of text contained in the snippet * * @var string */ var $snip_len = 100; /** * The string that is added to the beginning and * end of snippet text. * * @var string */ var $snip_str = '...'; /** * Anchor information for a particular link * * @var array */ var $anchor_info; var $crawler; var $testcrawler; var $http; var $response; var $response_headers; var $response_code; var $request_headers; function __construct() { $c = &owa_coreAPI::configSingleton(); $this->config = $c->fetch('base'); $this->e = &owa_coreAPI::errorSingleton(); $this->crawler = new Snoopy; // do not allow snoopy to follow links $this->crawler->maxredirs = 5; $this->crawler->agent = owa_coreAPI::getSetting('base', 'owa_user_agent'); //$this->crawler->agent = "Firefox"; //owa_coreAPI::debug('hello from owa_http constructor'); return; } function fetch($uri) { //owa_coreAPI::debug('hello from owa_http fetch'); return $this->crawler->fetch($uri); } function testFetch($url) { $http= new http_class; owa_coreAPI::debug('hello owa_http testfetch method'); /* Connection timeout */ $http->timeout=0; /* Data transfer timeout */ $http->data_timeout=0; /* Output debugging information about the progress of the connection */ $http->debug=1; $http->user_agent = owa_coreAPI::getSetting('base', 'owa_user_agent'); $http->follow_redirect=1; $http->redirection_limit=5; $http->exclude_address=""; $http->prefer_curl=0; $arguments = array(); $error=$http->GetRequestArguments($url,$arguments); $error=$http->Open($arguments); //for(;;) // { $error=$http->ReadReplyBody($body,50000); if($error!="" || strlen($body)==0) owa_coreAPI::debug(HtmlSpecialChars($body)); // } } /** * Searches a fetched html document for the anchor of a specific url * * @param string $link */ function extract_anchor($link) { $matches = ''; $regex = '/<a[^>]*href=\"%s\"[^>]*>(.*?)<\/a>/i'; //$escaped_link = str_replace(array("/", "?"), array("\/", "\?"), $link); $pattern = trim(sprintf($regex, preg_quote($link, '/'))); $search = preg_match($pattern, $this->response, $matches); //$this->e->debug('pattern: '.$pattern); //$this->e->debug('link: '.$link); if (empty($matches)) { if (substr($link, -1) === '/') { $link = substr($link, 0, -1); $pattern = trim(sprintf($regex, preg_quote($link, '/'))); $search = preg_match($pattern, $this->response, $matches); //$this->e->debug('pattern: '.$pattern); //$this->e->debug('link: '.$link); } } $this->e->debug('ref search: '.$search); //$this->e->debug('ref matches: '.print_r($this->results, true)); //$this->e->debug('ref matches: '.print_r($matches, true)); if (isset($matches[0])) { $this->anchor_info = array('anchor_tag' => $matches[0], 'anchor_text' => owa_lib::inputFilter($matches[0])); $this->e->debug('Anchor info: '.print_r($this->anchor_info, true)); } } /** * Creates a text snippet of the portion of page where the * specific link is found. * * Takes fully qualified URL for the link to search for. * * @param string $link * @return string */ function extract_anchor_snippet($link){ // Search the page for a specific anchor $this->extract_anchor($link); if(!empty($this->anchor_info['anchor_tag'])) { // drop certain HTML entitities and their content $nohtml = $this->strip_selected_tags( $this->response, array('title', 'head', 'script', 'object', 'style', 'meta', 'link', 'rdf:'), true); //$this->e->debug('Refering page content after certain html entities were dropped: '.$this->results); // calc len of the anchor text $atext_len = strlen($this->anchor_info['anchor_tag']); // find position within document of the anchor text $start = strpos($nohtml, $this->anchor_info['anchor_tag']); if ($start < $this->snip_len) { $part1_start_pos = 0; $part1_snip_len = $start; } else { $part1_start_pos = $start; $part1_snip_len = $this->snip_len; } $replace_items = array("\r\n", "\n\n", "\t", "\r", "\n"); // Create first segment of snippet $first_part = substr($nohtml, 0, $part1_start_pos); $first_part = str_replace($replace_items, '', $first_part); $first_part = strip_tags(owa_lib::inputFilter($first_part)); //$part1 = trim(substr($nohtml, $part1_start_pos, $part1_snip_len)); $part1 = substr($first_part,-$part1_snip_len, $part1_snip_len); //$part1 = str_replace(array('\r\n', '\n\n', '\t', '\r', '\n'), '', $part1); //$part1 = owa_lib::inputFilter($part1); // Create second segment of snippet $part2 = trim(substr($nohtml, $start + $atext_len, $this->snip_len+300)); $part2 = str_replace($replace_items, '', $part2); $part2 = substr(strip_tags(owa_lib::inputFilter($part2)),0, $this->snip_len); // Put humpty dumpy back together again and create actual snippet $snippet = $this->snip_str.$part1.' <span class="snippet_anchor">'.owa_lib::inputFilter($this->anchor_info['anchor_tag']).'</span> '.$part2.$this->snip_str; } else { $snippet = ''; } return $snippet; } function extract_title() { preg_match('~(</head>|<body>|(<title>\s*(.*?)\s*</title>))~i', $this->response, $m); $this->e->debug("referer title extract: ". print_r($m, true)); return $m[3]; } function strip_selected_tags($str, $tags = array(), $stripContent = false) { foreach ($tags as $k => $tag){ if ($stripContent == true) { $pattern = sprintf('#(<%s.*?>)(.*?)(<\/%s.*?>)#is', preg_quote($tag), preg_quote($tag)); $str = preg_replace($pattern,"",$str); } $str = preg_replace($pattern, ${2},$str); } return $str; } function SetupHTTP() { if(!IsSet($this->http)) { $this->http = new http_class; $this->http->follow_redirect = 1; $this->http->debug = 0; $this->http->debug_response_body = 0; $this->http->html_debug = 1; $this->http->user_agent = owa_coreAPI::getSetting('base', 'owa_user_agent'); $this->http->timeout = 3; $this->http->data_timeout = 3; } } function OpenRequest($arguments, &$headers) { if(strlen($this->error=$this->http->Open($arguments))) return(0); if(strlen($this->error=$this->http->SendRequest($arguments)) || strlen($this->error=$this->http->ReadReplyHeaders($headers))) { $this->http->Close(); return(0); } if($this->http->response_status!=200) { $this->error = 'the HTTP request returned the status '.$this->http->response_status; $this->http->Close(); return(0); } return(1); } function GetRequestResponse(&$response) { for($response = ''; ; ) { if(strlen($this->error=$this->http->ReadReplyBody($body, 500000))) & |