raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 if ( !in_array('Snoopy', get_declared_classes() ) ) :
mp-wp_genesis 3 /*************************************************
mp-wp_genesis 4
mp-wp_genesis 5 Snoopy - the PHP net client
mp-wp_genesis 6 Author: Monte Ohrt <monte@ispi.net>
mp-wp_genesis 7 Copyright (c): 1999-2008 New Digital Group, all rights reserved
mp-wp_genesis 8 Version: 1.2.4
mp-wp_genesis 9
mp-wp_genesis 10 * This library is free software; you can redistribute it and/or
mp-wp_genesis 11 * modify it under the terms of the GNU Lesser General Public
mp-wp_genesis 12 * License as published by the Free Software Foundation; either
mp-wp_genesis 13 * version 2.1 of the License, or (at your option) any later version.
mp-wp_genesis 14 *
mp-wp_genesis 15 * This library is distributed in the hope that it will be useful,
mp-wp_genesis 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mp-wp_genesis 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
mp-wp_genesis 18 * Lesser General Public License for more details.
mp-wp_genesis 19 *
mp-wp_genesis 20 * You should have received a copy of the GNU Lesser General Public
mp-wp_genesis 21 * License along with this library; if not, write to the Free Software
mp-wp_genesis 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
mp-wp_genesis 23
mp-wp_genesis 24 You may contact the author of Snoopy by e-mail at:
mp-wp_genesis 25 monte@ohrt.com
mp-wp_genesis 26
mp-wp_genesis 27 The latest version of Snoopy can be obtained from:
mp-wp_genesis 28 http://snoopy.sourceforge.net/
mp-wp_genesis 29
mp-wp_genesis 30 *************************************************/
mp-wp_genesis 31
mp-wp_genesis 32 class Snoopy
mp-wp_genesis 33 {
mp-wp_genesis 34 /**** Public variables ****/
mp-wp_genesis 35
mp-wp_genesis 36 /* user definable vars */
mp-wp_genesis 37
mp-wp_genesis 38 var $host = "www.php.net"; // host name we are connecting to
mp-wp_genesis 39 var $port = 80; // port we are connecting to
mp-wp_genesis 40 var $proxy_host = ""; // proxy host to use
mp-wp_genesis 41 var $proxy_port = ""; // proxy port to use
mp-wp_genesis 42 var $proxy_user = ""; // proxy user to use
mp-wp_genesis 43 var $proxy_pass = ""; // proxy password to use
mp-wp_genesis 44
mp-wp_genesis 45 var $agent = "Snoopy v1.2.4"; // agent we masquerade as
mp-wp_genesis 46 var $referer = ""; // referer info to pass
mp-wp_genesis 47 var $cookies = array(); // array of cookies to pass
mp-wp_genesis 48 // $cookies["username"]="joe";
mp-wp_genesis 49 var $rawheaders = array(); // array of raw headers to send
mp-wp_genesis 50 // $rawheaders["Content-type"]="text/html";
mp-wp_genesis 51
mp-wp_genesis 52 var $maxredirs = 5; // http redirection depth maximum. 0 = disallow
mp-wp_genesis 53 var $lastredirectaddr = ""; // contains address of last redirected address
mp-wp_genesis 54 var $offsiteok = true; // allows redirection off-site
mp-wp_genesis 55 var $maxframes = 0; // frame content depth maximum. 0 = disallow
mp-wp_genesis 56 var $expandlinks = true; // expand links to fully qualified URLs.
mp-wp_genesis 57 // this only applies to fetchlinks()
mp-wp_genesis 58 // submitlinks(), and submittext()
mp-wp_genesis 59 var $passcookies = true; // pass set cookies back through redirects
mp-wp_genesis 60 // NOTE: this currently does not respect
mp-wp_genesis 61 // dates, domains or paths.
mp-wp_genesis 62
mp-wp_genesis 63 var $user = ""; // user for http authentication
mp-wp_genesis 64 var $pass = ""; // password for http authentication
mp-wp_genesis 65
mp-wp_genesis 66 // http accept types
mp-wp_genesis 67 var $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
mp-wp_genesis 68
mp-wp_genesis 69 var $results = ""; // where the content is put
mp-wp_genesis 70
mp-wp_genesis 71 var $error = ""; // error messages sent here
mp-wp_genesis 72 var $response_code = ""; // response code returned from server
mp-wp_genesis 73 var $headers = array(); // headers returned from server sent here
mp-wp_genesis 74 var $maxlength = 500000; // max return data length (body)
mp-wp_genesis 75 var $read_timeout = 0; // timeout on read operations, in seconds
mp-wp_genesis 76 // supported only since PHP 4 Beta 4
mp-wp_genesis 77 // set to 0 to disallow timeouts
mp-wp_genesis 78 var $timed_out = false; // if a read operation timed out
mp-wp_genesis 79 var $status = 0; // http request status
mp-wp_genesis 80
mp-wp_genesis 81 var $temp_dir = "/tmp"; // temporary directory that the webserver
mp-wp_genesis 82 // has permission to write to.
mp-wp_genesis 83 // under Windows, this should be C:\temp
mp-wp_genesis 84
mp-wp_genesis 85 var $curl_path = "/usr/local/bin/curl";
mp-wp_genesis 86 // Snoopy will use cURL for fetching
mp-wp_genesis 87 // SSL content if a full system path to
mp-wp_genesis 88 // the cURL binary is supplied here.
mp-wp_genesis 89 // set to false if you do not have
mp-wp_genesis 90 // cURL installed. See http://curl.haxx.se
mp-wp_genesis 91 // for details on installing cURL.
mp-wp_genesis 92 // Snoopy does *not* use the cURL
mp-wp_genesis 93 // library functions built into php,
mp-wp_genesis 94 // as these functions are not stable
mp-wp_genesis 95 // as of this Snoopy release.
mp-wp_genesis 96
mp-wp_genesis 97 /**** Private variables ****/
mp-wp_genesis 98
mp-wp_genesis 99 var $_maxlinelen = 4096; // max line length (headers)
mp-wp_genesis 100
mp-wp_genesis 101 var $_httpmethod = "GET"; // default http request method
mp-wp_genesis 102 var $_httpversion = "HTTP/1.0"; // default http request version
mp-wp_genesis 103 var $_submit_method = "POST"; // default submit method
mp-wp_genesis 104 var $_submit_type = "application/x-www-form-urlencoded"; // default submit type
mp-wp_genesis 105 var $_mime_boundary = ""; // MIME boundary for multipart/form-data submit type
mp-wp_genesis 106 var $_redirectaddr = false; // will be set if page fetched is a redirect
mp-wp_genesis 107 var $_redirectdepth = 0; // increments on an http redirect
mp-wp_genesis 108 var $_frameurls = array(); // frame src urls
mp-wp_genesis 109 var $_framedepth = 0; // increments on frame depth
mp-wp_genesis 110
mp-wp_genesis 111 var $_isproxy = false; // set if using a proxy server
mp-wp_genesis 112 var $_fp_timeout = 30; // timeout for socket connection
mp-wp_genesis 113
mp-wp_genesis 114 /*======================================================================*\
mp-wp_genesis 115 Function: fetch
mp-wp_genesis 116 Purpose: fetch the contents of a web page
mp-wp_genesis 117 (and possibly other protocols in the
mp-wp_genesis 118 future like ftp, nntp, gopher, etc.)
mp-wp_genesis 119 Input: $URI the location of the page to fetch
mp-wp_genesis 120 Output: $this->results the output text from the fetch
mp-wp_genesis 121 \*======================================================================*/
mp-wp_genesis 122
mp-wp_genesis 123 function fetch($URI)
mp-wp_genesis 124 {
mp-wp_genesis 125
mp-wp_genesis 126 //preg_match("|^([^:]+)://([^:/]+)(:[\d]+)*(.*)|",$URI,$URI_PARTS);
mp-wp_genesis 127 $URI_PARTS = parse_url($URI);
mp-wp_genesis 128 if (!empty($URI_PARTS["user"]))
mp-wp_genesis 129 $this->user = $URI_PARTS["user"];
mp-wp_genesis 130 if (!empty($URI_PARTS["pass"]))
mp-wp_genesis 131 $this->pass = $URI_PARTS["pass"];
mp-wp_genesis 132 if (empty($URI_PARTS["query"]))
mp-wp_genesis 133 $URI_PARTS["query"] = '';
mp-wp_genesis 134 if (empty($URI_PARTS["path"]))
mp-wp_genesis 135 $URI_PARTS["path"] = '';
mp-wp_genesis 136
mp-wp_genesis 137 switch(strtolower($URI_PARTS["scheme"]))
mp-wp_genesis 138 {
mp-wp_genesis 139 case "http":
mp-wp_genesis 140 $this->host = $URI_PARTS["host"];
mp-wp_genesis 141 if(!empty($URI_PARTS["port"]))
mp-wp_genesis 142 $this->port = $URI_PARTS["port"];
mp-wp_genesis 143 if($this->_connect($fp))
mp-wp_genesis 144 {
mp-wp_genesis 145 if($this->_isproxy)
mp-wp_genesis 146 {
mp-wp_genesis 147 // using proxy, send entire URI
mp-wp_genesis 148 $this->_httprequest($URI,$fp,$URI,$this->_httpmethod);
mp-wp_genesis 149 }
mp-wp_genesis 150 else
mp-wp_genesis 151 {
mp-wp_genesis 152 $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
mp-wp_genesis 153 // no proxy, send only the path
mp-wp_genesis 154 $this->_httprequest($path, $fp, $URI, $this->_httpmethod);
mp-wp_genesis 155 }
mp-wp_genesis 156
mp-wp_genesis 157 $this->_disconnect($fp);
mp-wp_genesis 158
mp-wp_genesis 159 if($this->_redirectaddr)
mp-wp_genesis 160 {
mp-wp_genesis 161 /* url was redirected, check if we've hit the max depth */
mp-wp_genesis 162 if($this->maxredirs > $this->_redirectdepth)
mp-wp_genesis 163 {
mp-wp_genesis 164 // only follow redirect if it's on this site, or offsiteok is true
mp-wp_genesis 165 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
mp-wp_genesis 166 {
mp-wp_genesis 167 /* follow the redirect */
mp-wp_genesis 168 $this->_redirectdepth++;
mp-wp_genesis 169 $this->lastredirectaddr=$this->_redirectaddr;
mp-wp_genesis 170 $this->fetch($this->_redirectaddr);
mp-wp_genesis 171 }
mp-wp_genesis 172 }
mp-wp_genesis 173 }
mp-wp_genesis 174
mp-wp_genesis 175 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
mp-wp_genesis 176 {
mp-wp_genesis 177 $frameurls = $this->_frameurls;
mp-wp_genesis 178 $this->_frameurls = array();
mp-wp_genesis 179
mp-wp_genesis 180 while(list(,$frameurl) = each($frameurls))
mp-wp_genesis 181 {
mp-wp_genesis 182 if($this->_framedepth < $this->maxframes)
mp-wp_genesis 183 {
mp-wp_genesis 184 $this->fetch($frameurl);
mp-wp_genesis 185 $this->_framedepth++;
mp-wp_genesis 186 }
mp-wp_genesis 187 else
mp-wp_genesis 188 break;
mp-wp_genesis 189 }
mp-wp_genesis 190 }
mp-wp_genesis 191 }
mp-wp_genesis 192 else
mp-wp_genesis 193 {
mp-wp_genesis 194 return false;
mp-wp_genesis 195 }
mp-wp_genesis 196 return true;
mp-wp_genesis 197 break;
mp-wp_genesis 198 case "https":
mp-wp_genesis 199 if(!$this->curl_path)
mp-wp_genesis 200 return false;
mp-wp_genesis 201 if(function_exists("is_executable"))
mp-wp_genesis 202 if (!is_executable($this->curl_path))
mp-wp_genesis 203 return false;
mp-wp_genesis 204 $this->host = $URI_PARTS["host"];
mp-wp_genesis 205 if(!empty($URI_PARTS["port"]))
mp-wp_genesis 206 $this->port = $URI_PARTS["port"];
mp-wp_genesis 207 if($this->_isproxy)
mp-wp_genesis 208 {
mp-wp_genesis 209 // using proxy, send entire URI
mp-wp_genesis 210 $this->_httpsrequest($URI,$URI,$this->_httpmethod);
mp-wp_genesis 211 }
mp-wp_genesis 212 else
mp-wp_genesis 213 {
mp-wp_genesis 214 $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
mp-wp_genesis 215 // no proxy, send only the path
mp-wp_genesis 216 $this->_httpsrequest($path, $URI, $this->_httpmethod);
mp-wp_genesis 217 }
mp-wp_genesis 218
mp-wp_genesis 219 if($this->_redirectaddr)
mp-wp_genesis 220 {
mp-wp_genesis 221 /* url was redirected, check if we've hit the max depth */
mp-wp_genesis 222 if($this->maxredirs > $this->_redirectdepth)
mp-wp_genesis 223 {
mp-wp_genesis 224 // only follow redirect if it's on this site, or offsiteok is true
mp-wp_genesis 225 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
mp-wp_genesis 226 {
mp-wp_genesis 227 /* follow the redirect */
mp-wp_genesis 228 $this->_redirectdepth++;
mp-wp_genesis 229 $this->lastredirectaddr=$this->_redirectaddr;
mp-wp_genesis 230 $this->fetch($this->_redirectaddr);
mp-wp_genesis 231 }
mp-wp_genesis 232 }
mp-wp_genesis 233 }
mp-wp_genesis 234
mp-wp_genesis 235 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
mp-wp_genesis 236 {
mp-wp_genesis 237 $frameurls = $this->_frameurls;
mp-wp_genesis 238 $this->_frameurls = array();
mp-wp_genesis 239
mp-wp_genesis 240 while(list(,$frameurl) = each($frameurls))
mp-wp_genesis 241 {
mp-wp_genesis 242 if($this->_framedepth < $this->maxframes)
mp-wp_genesis 243 {
mp-wp_genesis 244 $this->fetch($frameurl);
mp-wp_genesis 245 $this->_framedepth++;
mp-wp_genesis 246 }
mp-wp_genesis 247 else
mp-wp_genesis 248 break;
mp-wp_genesis 249 }
mp-wp_genesis 250 }
mp-wp_genesis 251 return true;
mp-wp_genesis 252 break;
mp-wp_genesis 253 default:
mp-wp_genesis 254 // not a valid protocol
mp-wp_genesis 255 $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
mp-wp_genesis 256 return false;
mp-wp_genesis 257 break;
mp-wp_genesis 258 }
mp-wp_genesis 259 return true;
mp-wp_genesis 260 }
mp-wp_genesis 261
mp-wp_genesis 262 /*======================================================================*\
mp-wp_genesis 263 Function: submit
mp-wp_genesis 264 Purpose: submit an http form
mp-wp_genesis 265 Input: $URI the location to post the data
mp-wp_genesis 266 $formvars the formvars to use.
mp-wp_genesis 267 format: $formvars["var"] = "val";
mp-wp_genesis 268 $formfiles an array of files to submit
mp-wp_genesis 269 format: $formfiles["var"] = "/dir/filename.ext";
mp-wp_genesis 270 Output: $this->results the text output from the post
mp-wp_genesis 271 \*======================================================================*/
mp-wp_genesis 272
mp-wp_genesis 273 function submit($URI, $formvars="", $formfiles="")
mp-wp_genesis 274 {
mp-wp_genesis 275 unset($postdata);
mp-wp_genesis 276
mp-wp_genesis 277 $postdata = $this->_prepare_post_body($formvars, $formfiles);
mp-wp_genesis 278
mp-wp_genesis 279 $URI_PARTS = parse_url($URI);
mp-wp_genesis 280 if (!empty($URI_PARTS["user"]))
mp-wp_genesis 281 $this->user = $URI_PARTS["user"];
mp-wp_genesis 282 if (!empty($URI_PARTS["pass"]))
mp-wp_genesis 283 $this->pass = $URI_PARTS["pass"];
mp-wp_genesis 284 if (empty($URI_PARTS["query"]))
mp-wp_genesis 285 $URI_PARTS["query"] = '';
mp-wp_genesis 286 if (empty($URI_PARTS["path"]))
mp-wp_genesis 287 $URI_PARTS["path"] = '';
mp-wp_genesis 288
mp-wp_genesis 289 switch(strtolower($URI_PARTS["scheme"]))
mp-wp_genesis 290 {
mp-wp_genesis 291 case "http":
mp-wp_genesis 292 $this->host = $URI_PARTS["host"];
mp-wp_genesis 293 if(!empty($URI_PARTS["port"]))
mp-wp_genesis 294 $this->port = $URI_PARTS["port"];
mp-wp_genesis 295 if($this->_connect($fp))
mp-wp_genesis 296 {
mp-wp_genesis 297 if($this->_isproxy)
mp-wp_genesis 298 {
mp-wp_genesis 299 // using proxy, send entire URI
mp-wp_genesis 300 $this->_httprequest($URI,$fp,$URI,$this->_submit_method,$this->_submit_type,$postdata);
mp-wp_genesis 301 }
mp-wp_genesis 302 else
mp-wp_genesis 303 {
mp-wp_genesis 304 $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
mp-wp_genesis 305 // no proxy, send only the path
mp-wp_genesis 306 $this->_httprequest($path, $fp, $URI, $this->_submit_method, $this->_submit_type, $postdata);
mp-wp_genesis 307 }
mp-wp_genesis 308
mp-wp_genesis 309 $this->_disconnect($fp);
mp-wp_genesis 310
mp-wp_genesis 311 if($this->_redirectaddr)
mp-wp_genesis 312 {
mp-wp_genesis 313 /* url was redirected, check if we've hit the max depth */
mp-wp_genesis 314 if($this->maxredirs > $this->_redirectdepth)
mp-wp_genesis 315 {
mp-wp_genesis 316 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
mp-wp_genesis 317 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
mp-wp_genesis 318
mp-wp_genesis 319 // only follow redirect if it's on this site, or offsiteok is true
mp-wp_genesis 320 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
mp-wp_genesis 321 {
mp-wp_genesis 322 /* follow the redirect */
mp-wp_genesis 323 $this->_redirectdepth++;
mp-wp_genesis 324 $this->lastredirectaddr=$this->_redirectaddr;
mp-wp_genesis 325 if( strpos( $this->_redirectaddr, "?" ) > 0 )
mp-wp_genesis 326 $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
mp-wp_genesis 327 else
mp-wp_genesis 328 $this->submit($this->_redirectaddr,$formvars, $formfiles);
mp-wp_genesis 329 }
mp-wp_genesis 330 }
mp-wp_genesis 331 }
mp-wp_genesis 332
mp-wp_genesis 333 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
mp-wp_genesis 334 {
mp-wp_genesis 335 $frameurls = $this->_frameurls;
mp-wp_genesis 336 $this->_frameurls = array();
mp-wp_genesis 337
mp-wp_genesis 338 while(list(,$frameurl) = each($frameurls))
mp-wp_genesis 339 {
mp-wp_genesis 340 if($this->_framedepth < $this->maxframes)
mp-wp_genesis 341 {
mp-wp_genesis 342 $this->fetch($frameurl);
mp-wp_genesis 343 $this->_framedepth++;
mp-wp_genesis 344 }
mp-wp_genesis 345 else
mp-wp_genesis 346 break;
mp-wp_genesis 347 }
mp-wp_genesis 348 }
mp-wp_genesis 349
mp-wp_genesis 350 }
mp-wp_genesis 351 else
mp-wp_genesis 352 {
mp-wp_genesis 353 return false;
mp-wp_genesis 354 }
mp-wp_genesis 355 return true;
mp-wp_genesis 356 break;
mp-wp_genesis 357 case "https":
mp-wp_genesis 358 if(!$this->curl_path)
mp-wp_genesis 359 return false;
mp-wp_genesis 360 if(function_exists("is_executable"))
mp-wp_genesis 361 if (!is_executable($this->curl_path))
mp-wp_genesis 362 return false;
mp-wp_genesis 363 $this->host = $URI_PARTS["host"];
mp-wp_genesis 364 if(!empty($URI_PARTS["port"]))
mp-wp_genesis 365 $this->port = $URI_PARTS["port"];
mp-wp_genesis 366 if($this->_isproxy)
mp-wp_genesis 367 {
mp-wp_genesis 368 // using proxy, send entire URI
mp-wp_genesis 369 $this->_httpsrequest($URI, $URI, $this->_submit_method, $this->_submit_type, $postdata);
mp-wp_genesis 370 }
mp-wp_genesis 371 else
mp-wp_genesis 372 {
mp-wp_genesis 373 $path = $URI_PARTS["path"].($URI_PARTS["query"] ? "?".$URI_PARTS["query"] : "");
mp-wp_genesis 374 // no proxy, send only the path
mp-wp_genesis 375 $this->_httpsrequest($path, $URI, $this->_submit_method, $this->_submit_type, $postdata);
mp-wp_genesis 376 }
mp-wp_genesis 377
mp-wp_genesis 378 if($this->_redirectaddr)
mp-wp_genesis 379 {
mp-wp_genesis 380 /* url was redirected, check if we've hit the max depth */
mp-wp_genesis 381 if($this->maxredirs > $this->_redirectdepth)
mp-wp_genesis 382 {
mp-wp_genesis 383 if(!preg_match("|^".$URI_PARTS["scheme"]."://|", $this->_redirectaddr))
mp-wp_genesis 384 $this->_redirectaddr = $this->_expandlinks($this->_redirectaddr,$URI_PARTS["scheme"]."://".$URI_PARTS["host"]);
mp-wp_genesis 385
mp-wp_genesis 386 // only follow redirect if it's on this site, or offsiteok is true
mp-wp_genesis 387 if(preg_match("|^http://".preg_quote($this->host)."|i",$this->_redirectaddr) || $this->offsiteok)
mp-wp_genesis 388 {
mp-wp_genesis 389 /* follow the redirect */
mp-wp_genesis 390 $this->_redirectdepth++;
mp-wp_genesis 391 $this->lastredirectaddr=$this->_redirectaddr;
mp-wp_genesis 392 if( strpos( $this->_redirectaddr, "?" ) > 0 )
mp-wp_genesis 393 $this->fetch($this->_redirectaddr); // the redirect has changed the request method from post to get
mp-wp_genesis 394 else
mp-wp_genesis 395 $this->submit($this->_redirectaddr,$formvars, $formfiles);
mp-wp_genesis 396 }
mp-wp_genesis 397 }
mp-wp_genesis 398 }
mp-wp_genesis 399
mp-wp_genesis 400 if($this->_framedepth < $this->maxframes && count($this->_frameurls) > 0)
mp-wp_genesis 401 {
mp-wp_genesis 402 $frameurls = $this->_frameurls;
mp-wp_genesis 403 $this->_frameurls = array();
mp-wp_genesis 404
mp-wp_genesis 405 while(list(,$frameurl) = each($frameurls))
mp-wp_genesis 406 {
mp-wp_genesis 407 if($this->_framedepth < $this->maxframes)
mp-wp_genesis 408 {
mp-wp_genesis 409 $this->fetch($frameurl);
mp-wp_genesis 410 $this->_framedepth++;
mp-wp_genesis 411 }
mp-wp_genesis 412 else
mp-wp_genesis 413 break;
mp-wp_genesis 414 }
mp-wp_genesis 415 }
mp-wp_genesis 416 return true;
mp-wp_genesis 417 break;
mp-wp_genesis 418
mp-wp_genesis 419 default:
mp-wp_genesis 420 // not a valid protocol
mp-wp_genesis 421 $this->error = 'Invalid protocol "'.$URI_PARTS["scheme"].'"\n';
mp-wp_genesis 422 return false;
mp-wp_genesis 423 break;
mp-wp_genesis 424 }
mp-wp_genesis 425 return true;
mp-wp_genesis 426 }
mp-wp_genesis 427
mp-wp_genesis 428 /*======================================================================*\
mp-wp_genesis 429 Function: fetchlinks
mp-wp_genesis 430 Purpose: fetch the links from a web page
mp-wp_genesis 431 Input: $URI where you are fetching from
mp-wp_genesis 432 Output: $this->results an array of the URLs
mp-wp_genesis 433 \*======================================================================*/
mp-wp_genesis 434
mp-wp_genesis 435 function fetchlinks($URI)
mp-wp_genesis 436 {
mp-wp_genesis 437 if ($this->fetch($URI))
mp-wp_genesis 438 {
mp-wp_genesis 439 if($this->lastredirectaddr)
mp-wp_genesis 440 $URI = $this->lastredirectaddr;
mp-wp_genesis 441 if(is_array($this->results))
mp-wp_genesis 442 {
mp-wp_genesis 443 for($x=0;$x<count($this->results);$x++)
mp-wp_genesis 444 $this->results[$x] = $this->_striplinks($this->results[$x]);
mp-wp_genesis 445 }
mp-wp_genesis 446 else
mp-wp_genesis 447 $this->results = $this->_striplinks($this->results);
mp-wp_genesis 448
mp-wp_genesis 449 if($this->expandlinks)
mp-wp_genesis 450 $this->results = $this->_expandlinks($this->results, $URI);
mp-wp_genesis 451 return true;
mp-wp_genesis 452 }
mp-wp_genesis 453 else
mp-wp_genesis 454 return false;
mp-wp_genesis 455 }
mp-wp_genesis 456
mp-wp_genesis 457 /*======================================================================*\
mp-wp_genesis 458 Function: fetchform
mp-wp_genesis 459 Purpose: fetch the form elements from a web page
mp-wp_genesis 460 Input: $URI where you are fetching from
mp-wp_genesis 461 Output: $this->results the resulting html form
mp-wp_genesis 462 \*======================================================================*/
mp-wp_genesis 463
mp-wp_genesis 464 function fetchform($URI)
mp-wp_genesis 465 {
mp-wp_genesis 466
mp-wp_genesis 467 if ($this->fetch($URI))
mp-wp_genesis 468 {
mp-wp_genesis 469
mp-wp_genesis 470 if(is_array($this->results))
mp-wp_genesis 471 {
mp-wp_genesis 472 for($x=0;$x<count($this->results);$x++)
mp-wp_genesis 473 $this->results[$x] = $this->_stripform($this->results[$x]);
mp-wp_genesis 474 }
mp-wp_genesis 475 else
mp-wp_genesis 476 $this->results = $this->_stripform($this->results);
mp-wp_genesis 477
mp-wp_genesis 478 return true;
mp-wp_genesis 479 }
mp-wp_genesis 480 else
mp-wp_genesis 481 return false;
mp-wp_genesis 482 }
mp-wp_genesis 483
mp-wp_genesis 484
mp-wp_genesis 485 /*======================================================================*\
mp-wp_genesis 486 Function: fetchtext
mp-wp_genesis 487 Purpose: fetch the text from a web page, stripping the links
mp-wp_genesis 488 Input: $URI where you are fetching from
mp-wp_genesis 489 Output: $this->results the text from the web page
mp-wp_genesis 490 \*======================================================================*/
mp-wp_genesis 491
mp-wp_genesis 492 function fetchtext($URI)
mp-wp_genesis 493 {
mp-wp_genesis 494 if($this->fetch($URI))
mp-wp_genesis 495 {
mp-wp_genesis 496 if(is_array($this->results))
mp-wp_genesis 497 {
mp-wp_genesis 498 for($x=0;$x<count($this->results);$x++)
mp-wp_genesis 499 $this->results[$x] = $this->_striptext($this->results[$x]);
mp-wp_genesis 500 }
mp-wp_genesis 501 else
mp-wp_genesis 502 $this->results = $this->_striptext($this->results);
mp-wp_genesis 503 return true;
mp-wp_genesis 504 }
mp-wp_genesis 505 else
mp-wp_genesis 506 return false;
mp-wp_genesis 507 }
mp-wp_genesis 508
mp-wp_genesis 509 /*======================================================================*\
mp-wp_genesis 510 Function: submitlinks
mp-wp_genesis 511 Purpose: grab links from a form submission
mp-wp_genesis 512 Input: $URI where you are submitting from
mp-wp_genesis 513 Output: $this->results an array of the links from the post
mp-wp_genesis 514 \*======================================================================*/
mp-wp_genesis 515
mp-wp_genesis 516 function submitlinks($URI, $formvars="", $formfiles="")
mp-wp_genesis 517 {
mp-wp_genesis 518 if($this->submit($URI,$formvars, $formfiles))
mp-wp_genesis 519 {
mp-wp_genesis 520 if($this->lastredirectaddr)
mp-wp_genesis 521 $URI = $this->lastredirectaddr;
mp-wp_genesis 522 if(is_array($this->results))
mp-wp_genesis 523 {
mp-wp_genesis 524 for($x=0;$x<count($this->results);$x++)
mp-wp_genesis 525 {
mp-wp_genesis 526 $this->results[$x] = $this->_striplinks($this->results[$x]);
mp-wp_genesis 527 if($this->expandlinks)
mp-wp_genesis 528 $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
mp-wp_genesis 529 }
mp-wp_genesis 530 }
mp-wp_genesis 531 else
mp-wp_genesis 532 {
mp-wp_genesis 533 $this->results = $this->_striplinks($this->results);
mp-wp_genesis 534 if($this->expandlinks)
mp-wp_genesis 535 $this->results = $this->_expandlinks($this->results,$URI);
mp-wp_genesis 536 }
mp-wp_genesis 537 return true;
mp-wp_genesis 538 }
mp-wp_genesis 539 else
mp-wp_genesis 540 return false;
mp-wp_genesis 541 }
mp-wp_genesis 542
mp-wp_genesis 543 /*======================================================================*\
mp-wp_genesis 544 Function: submittext
mp-wp_genesis 545 Purpose: grab text from a form submission
mp-wp_genesis 546 Input: $URI where you are submitting from
mp-wp_genesis 547 Output: $this->results the text from the web page
mp-wp_genesis 548 \*======================================================================*/
mp-wp_genesis 549
mp-wp_genesis 550 function submittext($URI, $formvars = "", $formfiles = "")
mp-wp_genesis 551 {
mp-wp_genesis 552 if($this->submit($URI,$formvars, $formfiles))
mp-wp_genesis 553 {
mp-wp_genesis 554 if($this->lastredirectaddr)
mp-wp_genesis 555 $URI = $this->lastredirectaddr;
mp-wp_genesis 556 if(is_array($this->results))
mp-wp_genesis 557 {
mp-wp_genesis 558 for($x=0;$x<count($this->results);$x++)
mp-wp_genesis 559 {
mp-wp_genesis 560 $this->results[$x] = $this->_striptext($this->results[$x]);
mp-wp_genesis 561 if($this->expandlinks)
mp-wp_genesis 562 $this->results[$x] = $this->_expandlinks($this->results[$x],$URI);
mp-wp_genesis 563 }
mp-wp_genesis 564 }
mp-wp_genesis 565 else
mp-wp_genesis 566 {
mp-wp_genesis 567 $this->results = $this->_striptext($this->results);
mp-wp_genesis 568 if($this->expandlinks)
mp-wp_genesis 569 $this->results = $this->_expandlinks($this->results,$URI);
mp-wp_genesis 570 }
mp-wp_genesis 571 return true;
mp-wp_genesis 572 }
mp-wp_genesis 573 else
mp-wp_genesis 574 return false;
mp-wp_genesis 575 }
mp-wp_genesis 576
mp-wp_genesis 577
mp-wp_genesis 578
mp-wp_genesis 579 /*======================================================================*\
mp-wp_genesis 580 Function: set_submit_multipart
mp-wp_genesis 581 Purpose: Set the form submission content type to
mp-wp_genesis 582 multipart/form-data
mp-wp_genesis 583 \*======================================================================*/
mp-wp_genesis 584 function set_submit_multipart()
mp-wp_genesis 585 {
mp-wp_genesis 586 $this->_submit_type = "multipart/form-data";
mp-wp_genesis 587 }
mp-wp_genesis 588
mp-wp_genesis 589
mp-wp_genesis 590 /*======================================================================*\
mp-wp_genesis 591 Function: set_submit_normal
mp-wp_genesis 592 Purpose: Set the form submission content type to
mp-wp_genesis 593 application/x-www-form-urlencoded
mp-wp_genesis 594 \*======================================================================*/
mp-wp_genesis 595 function set_submit_normal()
mp-wp_genesis 596 {
mp-wp_genesis 597 $this->_submit_type = "application/x-www-form-urlencoded";
mp-wp_genesis 598 }
mp-wp_genesis 599
mp-wp_genesis 600
mp-wp_genesis 601
mp-wp_genesis 602
mp-wp_genesis 603 /*======================================================================*\
mp-wp_genesis 604 Private functions
mp-wp_genesis 605 \*======================================================================*/
mp-wp_genesis 606
mp-wp_genesis 607
mp-wp_genesis 608 /*======================================================================*\
mp-wp_genesis 609 Function: _striplinks
mp-wp_genesis 610 Purpose: strip the hyperlinks from an html document
mp-wp_genesis 611 Input: $document document to strip.
mp-wp_genesis 612 Output: $match an array of the links
mp-wp_genesis 613 \*======================================================================*/
mp-wp_genesis 614
mp-wp_genesis 615 function _striplinks($document)
mp-wp_genesis 616 {
mp-wp_genesis 617 preg_match_all("'<\s*a\s.*?href\s*=\s* # find <a href=
mp-wp_genesis 618 ([\"\'])? # find single or double quote
mp-wp_genesis 619 (?(1) (.*?)\\1 | ([^\s\>]+)) # if quote found, match up to next matching
mp-wp_genesis 620 # quote, otherwise match up to next space
mp-wp_genesis 621 'isx",$document,$links);
mp-wp_genesis 622
mp-wp_genesis 623
mp-wp_genesis 624 // catenate the non-empty matches from the conditional subpattern
mp-wp_genesis 625
mp-wp_genesis 626 while(list($key,$val) = each($links[2]))
mp-wp_genesis 627 {
mp-wp_genesis 628 if(!empty($val))
mp-wp_genesis 629 $match[] = $val;
mp-wp_genesis 630 }
mp-wp_genesis 631
mp-wp_genesis 632 while(list($key,$val) = each($links[3]))
mp-wp_genesis 633 {
mp-wp_genesis 634 if(!empty($val))
mp-wp_genesis 635 $match[] = $val;
mp-wp_genesis 636 }
mp-wp_genesis 637
mp-wp_genesis 638 // return the links
mp-wp_genesis 639 return $match;
mp-wp_genesis 640 }
mp-wp_genesis 641
mp-wp_genesis 642 /*======================================================================*\
mp-wp_genesis 643 Function: _stripform
mp-wp_genesis 644 Purpose: strip the form elements from an html document
mp-wp_genesis 645 Input: $document document to strip.
mp-wp_genesis 646 Output: $match an array of the links
mp-wp_genesis 647 \*======================================================================*/
mp-wp_genesis 648
mp-wp_genesis 649 function _stripform($document)
mp-wp_genesis 650 {
mp-wp_genesis 651 preg_match_all("'<\/?(FORM|INPUT|SELECT|TEXTAREA|(OPTION))[^<>]*>(?(2)(.*(?=<\/?(option|select)[^<>]*>[\r\n]*)|(?=[\r\n]*))|(?=[\r\n]*))'Usi",$document,$elements);
mp-wp_genesis 652
mp-wp_genesis 653 // catenate the matches
mp-wp_genesis 654 $match = implode("\r\n",$elements[0]);
mp-wp_genesis 655
mp-wp_genesis 656 // return the links
mp-wp_genesis 657 return $match;
mp-wp_genesis 658 }
mp-wp_genesis 659
mp-wp_genesis 660
mp-wp_genesis 661
mp-wp_genesis 662 /*======================================================================*\
mp-wp_genesis 663 Function: _striptext
mp-wp_genesis 664 Purpose: strip the text from an html document
mp-wp_genesis 665 Input: $document document to strip.
mp-wp_genesis 666 Output: $text the resulting text
mp-wp_genesis 667 \*======================================================================*/
mp-wp_genesis 668
mp-wp_genesis 669 function _striptext($document)
mp-wp_genesis 670 {
mp-wp_genesis 671
mp-wp_genesis 672 // I didn't use preg eval (//e) since that is only available in PHP 4.0.
mp-wp_genesis 673 // so, list your entities one by one here. I included some of the
mp-wp_genesis 674 // more common ones.
mp-wp_genesis 675
mp-wp_genesis 676 $search = array("'<script[^>]*?>.*?</script>'si", // strip out javascript
mp-wp_genesis 677 "'<[\/\!]*?[^<>]*?>'si", // strip out html tags
mp-wp_genesis 678 "'([\r\n])[\s]+'", // strip out white space
mp-wp_genesis 679 "'&(quot|#34|#034|#x22);'i", // replace html entities
mp-wp_genesis 680 "'&(amp|#38|#038|#x26);'i", // added hexadecimal values
mp-wp_genesis 681 "'&(lt|#60|#060|#x3c);'i",
mp-wp_genesis 682 "'&(gt|#62|#062|#x3e);'i",
mp-wp_genesis 683 "'&(nbsp|#160|#xa0);'i",
mp-wp_genesis 684 "'&(iexcl|#161);'i",
mp-wp_genesis 685 "'&(cent|#162);'i",
mp-wp_genesis 686 "'&(pound|#163);'i",
mp-wp_genesis 687 "'&(copy|#169);'i",
mp-wp_genesis 688 "'&(reg|#174);'i",
mp-wp_genesis 689 "'&(deg|#176);'i",
mp-wp_genesis 690 "'&(#39|#039|#x27);'",
mp-wp_genesis 691 "'&(euro|#8364);'i", // europe
mp-wp_genesis 692 "'&a(uml|UML);'", // german
mp-wp_genesis 693 "'&o(uml|UML);'",
mp-wp_genesis 694 "'&u(uml|UML);'",
mp-wp_genesis 695 "'&A(uml|UML);'",
mp-wp_genesis 696 "'&O(uml|UML);'",
mp-wp_genesis 697 "'&U(uml|UML);'",
mp-wp_genesis 698 "'&szlig;'i",
mp-wp_genesis 699 );
mp-wp_genesis 700 $replace = array( "",
mp-wp_genesis 701 "",
mp-wp_genesis 702 "\\1",
mp-wp_genesis 703 "\"",
mp-wp_genesis 704 "&",
mp-wp_genesis 705 "<",
mp-wp_genesis 706 ">",
mp-wp_genesis 707 " ",
mp-wp_genesis 708 chr(161),
mp-wp_genesis 709 chr(162),
mp-wp_genesis 710 chr(163),
mp-wp_genesis 711 chr(169),
mp-wp_genesis 712 chr(174),
mp-wp_genesis 713 chr(176),
mp-wp_genesis 714 chr(39),
mp-wp_genesis 715 chr(128),
mp-wp_genesis 716 "",
mp-wp_genesis 717 "",
mp-wp_genesis 718 "",
mp-wp_genesis 719 "",
mp-wp_genesis 720 "",
mp-wp_genesis 721 "",
mp-wp_genesis 722 "",
mp-wp_genesis 723 );
mp-wp_genesis 724
mp-wp_genesis 725 $text = preg_replace($search,$replace,$document);
mp-wp_genesis 726
mp-wp_genesis 727 return $text;
mp-wp_genesis 728 }
mp-wp_genesis 729
mp-wp_genesis 730 /*======================================================================*\
mp-wp_genesis 731 Function: _expandlinks
mp-wp_genesis 732 Purpose: expand each link into a fully qualified URL
mp-wp_genesis 733 Input: $links the links to qualify
mp-wp_genesis 734 $URI the full URI to get the base from
mp-wp_genesis 735 Output: $expandedLinks the expanded links
mp-wp_genesis 736 \*======================================================================*/
mp-wp_genesis 737
mp-wp_genesis 738 function _expandlinks($links,$URI)
mp-wp_genesis 739 {
mp-wp_genesis 740
mp-wp_genesis 741 preg_match("/^[^\?]+/",$URI,$match);
mp-wp_genesis 742
mp-wp_genesis 743 $match = preg_replace("|/[^\/\.]+\.[^\/\.]+$|","",$match[0]);
mp-wp_genesis 744 $match = preg_replace("|/$|","",$match);
mp-wp_genesis 745 $match_part = parse_url($match);
mp-wp_genesis 746 $match_root =
mp-wp_genesis 747 $match_part["scheme"]."://".$match_part["host"];
mp-wp_genesis 748
mp-wp_genesis 749 $search = array( "|^http://".preg_quote($this->host)."|i",
mp-wp_genesis 750 "|^(\/)|i",
mp-wp_genesis 751 "|^(?!http://)(?!mailto:)|i",
mp-wp_genesis 752 "|/\./|",
mp-wp_genesis 753 "|/[^\/]+/\.\./|"
mp-wp_genesis 754 );
mp-wp_genesis 755
mp-wp_genesis 756 $replace = array( "",
mp-wp_genesis 757 $match_root."/",
mp-wp_genesis 758 $match."/",
mp-wp_genesis 759 "/",
mp-wp_genesis 760 "/"
mp-wp_genesis 761 );
mp-wp_genesis 762
mp-wp_genesis 763 $expandedLinks = preg_replace($search,$replace,$links);
mp-wp_genesis 764
mp-wp_genesis 765 return $expandedLinks;
mp-wp_genesis 766 }
mp-wp_genesis 767
mp-wp_genesis 768 /*======================================================================*\
mp-wp_genesis 769 Function: _httprequest
mp-wp_genesis 770 Purpose: go get the http data from the server
mp-wp_genesis 771 Input: $url the url to fetch
mp-wp_genesis 772 $fp the current open file pointer
mp-wp_genesis 773 $URI the full URI
mp-wp_genesis 774 $body body contents to send if any (POST)
mp-wp_genesis 775 Output:
mp-wp_genesis 776 \*======================================================================*/
mp-wp_genesis 777
mp-wp_genesis 778 function _httprequest($url,$fp,$URI,$http_method,$content_type="",$body="")
mp-wp_genesis 779 {
mp-wp_genesis 780 $cookie_headers = '';
mp-wp_genesis 781 if($this->passcookies && $this->_redirectaddr)
mp-wp_genesis 782 $this->setcookies();
mp-wp_genesis 783
mp-wp_genesis 784 $URI_PARTS = parse_url($URI);
mp-wp_genesis 785 if(empty($url))
mp-wp_genesis 786 $url = "/";
mp-wp_genesis 787 $headers = $http_method." ".$url." ".$this->_httpversion."\r\n";
mp-wp_genesis 788 if(!empty($this->agent))
mp-wp_genesis 789 $headers .= "User-Agent: ".$this->agent."\r\n";
mp-wp_genesis 790 if(!empty($this->host) && !isset($this->rawheaders['Host'])) {
mp-wp_genesis 791 $headers .= "Host: ".$this->host;
mp-wp_genesis 792 if(!empty($this->port) && $this->port != 80)
mp-wp_genesis 793 $headers .= ":".$this->port;
mp-wp_genesis 794 $headers .= "\r\n";
mp-wp_genesis 795 }
mp-wp_genesis 796 if(!empty($this->accept))
mp-wp_genesis 797 $headers .= "Accept: ".$this->accept."\r\n";
mp-wp_genesis 798 if(!empty($this->referer))
mp-wp_genesis 799 $headers .= "Referer: ".$this->referer."\r\n";
mp-wp_genesis 800 if(!empty($this->cookies))
mp-wp_genesis 801 {
mp-wp_genesis 802 if(!is_array($this->cookies))
mp-wp_genesis 803 $this->cookies = (array)$this->cookies;
mp-wp_genesis 804
mp-wp_genesis 805 reset($this->cookies);
mp-wp_genesis 806 if ( count($this->cookies) > 0 ) {
mp-wp_genesis 807 $cookie_headers .= 'Cookie: ';
mp-wp_genesis 808 foreach ( $this->cookies as $cookieKey => $cookieVal ) {
mp-wp_genesis 809 $cookie_headers .= $cookieKey."=".urlencode($cookieVal)."; ";
mp-wp_genesis 810 }
mp-wp_genesis 811 $headers .= substr($cookie_headers,0,-2) . "\r\n";
mp-wp_genesis 812 }
mp-wp_genesis 813 }
mp-wp_genesis 814 if(!empty($this->rawheaders))
mp-wp_genesis 815 {
mp-wp_genesis 816 if(!is_array($this->rawheaders))
mp-wp_genesis 817 $this->rawheaders = (array)$this->rawheaders;
mp-wp_genesis 818 while(list($headerKey,$headerVal) = each($this->rawheaders))
mp-wp_genesis 819 $headers .= $headerKey.": ".$headerVal."\r\n";
mp-wp_genesis 820 }
mp-wp_genesis 821 if(!empty($content_type)) {
mp-wp_genesis 822 $headers .= "Content-type: $content_type";
mp-wp_genesis 823 if ($content_type == "multipart/form-data")
mp-wp_genesis 824 $headers .= "; boundary=".$this->_mime_boundary;
mp-wp_genesis 825 $headers .= "\r\n";
mp-wp_genesis 826 }
mp-wp_genesis 827 if(!empty($body))
mp-wp_genesis 828 $headers .= "Content-length: ".strlen($body)."\r\n";
mp-wp_genesis 829 if(!empty($this->user) || !empty($this->pass))
mp-wp_genesis 830 $headers .= "Authorization: Basic ".base64_encode($this->user.":".$this->pass)."\r\n";
mp-wp_genesis 831
mp-wp_genesis 832 //add proxy auth headers
mp-wp_genesis 833 if(!empty($this->proxy_user))
mp-wp_genesis 834 $headers .= 'Proxy-Authorization: ' . 'Basic ' . base64_encode($this->proxy_user . ':' . $this->proxy_pass)."\r\n";
mp-wp_genesis 835
mp-wp_genesis 836
mp-wp_genesis 837 $headers .= "\r\n";
mp-wp_genesis 838
mp-wp_genesis 839 // set the read timeout if needed
mp-wp_genesis 840 if ($this->read_timeout > 0)
mp-wp_genesis 841 socket_set_timeout($fp, $this->read_timeout);
mp-wp_genesis 842 $this->timed_out = false;
mp-wp_genesis 843
mp-wp_genesis 844 fwrite($fp,$headers.$body,strlen($headers.$body));
mp-wp_genesis 845
mp-wp_genesis 846 $this->_redirectaddr = false;
mp-wp_genesis 847 unset($this->headers);
mp-wp_genesis 848
mp-wp_genesis 849 while($currentHeader = fgets($fp,$this->_maxlinelen))
mp-wp_genesis 850 {
mp-wp_genesis 851 if ($this->read_timeout > 0 && $this->_check_timeout($fp))
mp-wp_genesis 852 {
mp-wp_genesis 853 $this->status=-100;
mp-wp_genesis 854 return false;
mp-wp_genesis 855 }
mp-wp_genesis 856
mp-wp_genesis 857 if($currentHeader == "\r\n")
mp-wp_genesis 858 break;
mp-wp_genesis 859
mp-wp_genesis 860 // if a header begins with Location: or URI:, set the redirect
mp-wp_genesis 861 if(preg_match("/^(Location:|URI:)/i",$currentHeader))
mp-wp_genesis 862 {
mp-wp_genesis 863 // get URL portion of the redirect
mp-wp_genesis 864 preg_match("/^(Location:|URI:)[ ]+(.*)/i",chop($currentHeader),$matches);
mp-wp_genesis 865 // look for :// in the Location header to see if hostname is included
mp-wp_genesis 866 if(!preg_match("|\:\/\/|",$matches[2]))
mp-wp_genesis 867 {
mp-wp_genesis 868 // no host in the path, so prepend
mp-wp_genesis 869 $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
mp-wp_genesis 870 // eliminate double slash
mp-wp_genesis 871 if(!preg_match("|^/|",$matches[2]))
mp-wp_genesis 872 $this->_redirectaddr .= "/".$matches[2];
mp-wp_genesis 873 else
mp-wp_genesis 874 $this->_redirectaddr .= $matches[2];
mp-wp_genesis 875 }
mp-wp_genesis 876 else
mp-wp_genesis 877 $this->_redirectaddr = $matches[2];
mp-wp_genesis 878 }
mp-wp_genesis 879
mp-wp_genesis 880 if(preg_match("|^HTTP/|",$currentHeader))
mp-wp_genesis 881 {
mp-wp_genesis 882 if(preg_match("|^HTTP/[^\s]*\s(.*?)\s|",$currentHeader, $status))
mp-wp_genesis 883 {
mp-wp_genesis 884 $this->status= $status[1];
mp-wp_genesis 885 }
mp-wp_genesis 886 $this->response_code = $currentHeader;
mp-wp_genesis 887 }
mp-wp_genesis 888
mp-wp_genesis 889 $this->headers[] = $currentHeader;
mp-wp_genesis 890 }
mp-wp_genesis 891
mp-wp_genesis 892 $results = '';
mp-wp_genesis 893 do {
mp-wp_genesis 894 $_data = fread($fp, $this->maxlength);
mp-wp_genesis 895 if (strlen($_data) == 0) {
mp-wp_genesis 896 break;
mp-wp_genesis 897 }
mp-wp_genesis 898 $results .= $_data;
mp-wp_genesis 899 } while(true);
mp-wp_genesis 900
mp-wp_genesis 901 if ($this->read_timeout > 0 && $this->_check_timeout($fp))
mp-wp_genesis 902 {
mp-wp_genesis 903 $this->status=-100;
mp-wp_genesis 904 return false;
mp-wp_genesis 905 }
mp-wp_genesis 906
mp-wp_genesis 907 // check if there is a a redirect meta tag
mp-wp_genesis 908
mp-wp_genesis 909 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
mp-wp_genesis 910
mp-wp_genesis 911 {
mp-wp_genesis 912 $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
mp-wp_genesis 913 }
mp-wp_genesis 914
mp-wp_genesis 915 // have we hit our frame depth and is there frame src to fetch?
mp-wp_genesis 916 if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
mp-wp_genesis 917 {
mp-wp_genesis 918 $this->results[] = $results;
mp-wp_genesis 919 for($x=0; $x<count($match[1]); $x++)
mp-wp_genesis 920 $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
mp-wp_genesis 921 }
mp-wp_genesis 922 // have we already fetched framed content?
mp-wp_genesis 923 elseif(is_array($this->results))
mp-wp_genesis 924 $this->results[] = $results;
mp-wp_genesis 925 // no framed content
mp-wp_genesis 926 else
mp-wp_genesis 927 $this->results = $results;
mp-wp_genesis 928
mp-wp_genesis 929 return true;
mp-wp_genesis 930 }
mp-wp_genesis 931
mp-wp_genesis 932 /*======================================================================*\
mp-wp_genesis 933 Function: _httpsrequest
mp-wp_genesis 934 Purpose: go get the https data from the server using curl
mp-wp_genesis 935 Input: $url the url to fetch
mp-wp_genesis 936 $URI the full URI
mp-wp_genesis 937 $body body contents to send if any (POST)
mp-wp_genesis 938 Output:
mp-wp_genesis 939 \*======================================================================*/
mp-wp_genesis 940
mp-wp_genesis 941 function _httpsrequest($url,$URI,$http_method,$content_type="",$body="")
mp-wp_genesis 942 {
mp-wp_genesis 943 if($this->passcookies && $this->_redirectaddr)
mp-wp_genesis 944 $this->setcookies();
mp-wp_genesis 945
mp-wp_genesis 946 $headers = array();
mp-wp_genesis 947
mp-wp_genesis 948 $URI_PARTS = parse_url($URI);
mp-wp_genesis 949 if(empty($url))
mp-wp_genesis 950 $url = "/";
mp-wp_genesis 951 // GET ... header not needed for curl
mp-wp_genesis 952 //$headers[] = $http_method." ".$url." ".$this->_httpversion;
mp-wp_genesis 953 if(!empty($this->agent))
mp-wp_genesis 954 $headers[] = "User-Agent: ".$this->agent;
mp-wp_genesis 955 if(!empty($this->host))
mp-wp_genesis 956 if(!empty($this->port))
mp-wp_genesis 957 $headers[] = "Host: ".$this->host.":".$this->port;
mp-wp_genesis 958 else
mp-wp_genesis 959 $headers[] = "Host: ".$this->host;
mp-wp_genesis 960 if(!empty($this->accept))
mp-wp_genesis 961 $headers[] = "Accept: ".$this->accept;
mp-wp_genesis 962 if(!empty($this->referer))
mp-wp_genesis 963 $headers[] = "Referer: ".$this->referer;
mp-wp_genesis 964 if(!empty($this->cookies))
mp-wp_genesis 965 {
mp-wp_genesis 966 if(!is_array($this->cookies))
mp-wp_genesis 967 $this->cookies = (array)$this->cookies;
mp-wp_genesis 968
mp-wp_genesis 969 reset($this->cookies);
mp-wp_genesis 970 if ( count($this->cookies) > 0 ) {
mp-wp_genesis 971 $cookie_str = 'Cookie: ';
mp-wp_genesis 972 foreach ( $this->cookies as $cookieKey => $cookieVal ) {
mp-wp_genesis 973 $cookie_str .= $cookieKey."=".urlencode($cookieVal)."; ";
mp-wp_genesis 974 }
mp-wp_genesis 975 $headers[] = substr($cookie_str,0,-2);
mp-wp_genesis 976 }
mp-wp_genesis 977 }
mp-wp_genesis 978 if(!empty($this->rawheaders))
mp-wp_genesis 979 {
mp-wp_genesis 980 if(!is_array($this->rawheaders))
mp-wp_genesis 981 $this->rawheaders = (array)$this->rawheaders;
mp-wp_genesis 982 while(list($headerKey,$headerVal) = each($this->rawheaders))
mp-wp_genesis 983 $headers[] = $headerKey.": ".$headerVal;
mp-wp_genesis 984 }
mp-wp_genesis 985 if(!empty($content_type)) {
mp-wp_genesis 986 if ($content_type == "multipart/form-data")
mp-wp_genesis 987 $headers[] = "Content-type: $content_type; boundary=".$this->_mime_boundary;
mp-wp_genesis 988 else
mp-wp_genesis 989 $headers[] = "Content-type: $content_type";
mp-wp_genesis 990 }
mp-wp_genesis 991 if(!empty($body))
mp-wp_genesis 992 $headers[] = "Content-length: ".strlen($body);
mp-wp_genesis 993 if(!empty($this->user) || !empty($this->pass))
mp-wp_genesis 994 $headers[] = "Authorization: BASIC ".base64_encode($this->user.":".$this->pass);
mp-wp_genesis 995
mp-wp_genesis 996 for($curr_header = 0; $curr_header < count($headers); $curr_header++) {
mp-wp_genesis 997 $safer_header = strtr( $headers[$curr_header], "\"", " " );
mp-wp_genesis 998 $cmdline_params .= " -H \"".$safer_header."\"";
mp-wp_genesis 999 }
mp-wp_genesis 1000
mp-wp_genesis 1001 if(!empty($body))
mp-wp_genesis 1002 $cmdline_params .= " -d \"$body\"";
mp-wp_genesis 1003
mp-wp_genesis 1004 if($this->read_timeout > 0)
mp-wp_genesis 1005 $cmdline_params .= " -m ".$this->read_timeout;
mp-wp_genesis 1006
mp-wp_genesis 1007 $headerfile = tempnam($temp_dir, "sno");
mp-wp_genesis 1008
mp-wp_genesis 1009 exec($this->curl_path." -k -D \"$headerfile\"".$cmdline_params." \"".escapeshellcmd($URI)."\"",$results,$return);
mp-wp_genesis 1010
mp-wp_genesis 1011 if($return)
mp-wp_genesis 1012 {
mp-wp_genesis 1013 $this->error = "Error: cURL could not retrieve the document, error $return.";
mp-wp_genesis 1014 return false;
mp-wp_genesis 1015 }
mp-wp_genesis 1016
mp-wp_genesis 1017
mp-wp_genesis 1018 $results = implode("\r\n",$results);
mp-wp_genesis 1019
mp-wp_genesis 1020 $result_headers = file("$headerfile");
mp-wp_genesis 1021
mp-wp_genesis 1022 $this->_redirectaddr = false;
mp-wp_genesis 1023 unset($this->headers);
mp-wp_genesis 1024
mp-wp_genesis 1025 for($currentHeader = 0; $currentHeader < count($result_headers); $currentHeader++)
mp-wp_genesis 1026 {
mp-wp_genesis 1027
mp-wp_genesis 1028 // if a header begins with Location: or URI:, set the redirect
mp-wp_genesis 1029 if(preg_match("/^(Location: |URI: )/i",$result_headers[$currentHeader]))
mp-wp_genesis 1030 {
mp-wp_genesis 1031 // get URL portion of the redirect
mp-wp_genesis 1032 preg_match("/^(Location: |URI:)\s+(.*)/",chop($result_headers[$currentHeader]),$matches);
mp-wp_genesis 1033 // look for :// in the Location header to see if hostname is included
mp-wp_genesis 1034 if(!preg_match("|\:\/\/|",$matches[2]))
mp-wp_genesis 1035 {
mp-wp_genesis 1036 // no host in the path, so prepend
mp-wp_genesis 1037 $this->_redirectaddr = $URI_PARTS["scheme"]."://".$this->host.":".$this->port;
mp-wp_genesis 1038 // eliminate double slash
mp-wp_genesis 1039 if(!preg_match("|^/|",$matches[2]))
mp-wp_genesis 1040 $this->_redirectaddr .= "/".$matches[2];
mp-wp_genesis 1041 else
mp-wp_genesis 1042 $this->_redirectaddr .= $matches[2];
mp-wp_genesis 1043 }
mp-wp_genesis 1044 else
mp-wp_genesis 1045 $this->_redirectaddr = $matches[2];
mp-wp_genesis 1046 }
mp-wp_genesis 1047
mp-wp_genesis 1048 if(preg_match("|^HTTP/|",$result_headers[$currentHeader]))
mp-wp_genesis 1049 $this->response_code = $result_headers[$currentHeader];
mp-wp_genesis 1050
mp-wp_genesis 1051 $this->headers[] = $result_headers[$currentHeader];
mp-wp_genesis 1052 }
mp-wp_genesis 1053
mp-wp_genesis 1054 // check if there is a a redirect meta tag
mp-wp_genesis 1055
mp-wp_genesis 1056 if(preg_match("'<meta[\s]*http-equiv[^>]*?content[\s]*=[\s]*[\"\']?\d+;[\s]*URL[\s]*=[\s]*([^\"\']*?)[\"\']?>'i",$results,$match))
mp-wp_genesis 1057 {
mp-wp_genesis 1058 $this->_redirectaddr = $this->_expandlinks($match[1],$URI);
mp-wp_genesis 1059 }
mp-wp_genesis 1060
mp-wp_genesis 1061 // have we hit our frame depth and is there frame src to fetch?
mp-wp_genesis 1062 if(($this->_framedepth < $this->maxframes) && preg_match_all("'<frame\s+.*src[\s]*=[\'\"]?([^\'\"\>]+)'i",$results,$match))
mp-wp_genesis 1063 {
mp-wp_genesis 1064 $this->results[] = $results;
mp-wp_genesis 1065 for($x=0; $x<count($match[1]); $x++)
mp-wp_genesis 1066 $this->_frameurls[] = $this->_expandlinks($match[1][$x],$URI_PARTS["scheme"]."://".$this->host);
mp-wp_genesis 1067 }
mp-wp_genesis 1068 // have we already fetched framed content?
mp-wp_genesis 1069 elseif(is_array($this->results))
mp-wp_genesis 1070 $this->results[] = $results;
mp-wp_genesis 1071 // no framed content
mp-wp_genesis 1072 else
mp-wp_genesis 1073 $this->results = $results;
mp-wp_genesis 1074
mp-wp_genesis 1075 unlink("$headerfile");
mp-wp_genesis 1076
mp-wp_genesis 1077 return true;
mp-wp_genesis 1078 }
mp-wp_genesis 1079
mp-wp_genesis 1080 /*======================================================================*\
mp-wp_genesis 1081 Function: setcookies()
mp-wp_genesis 1082 Purpose: set cookies for a redirection
mp-wp_genesis 1083 \*======================================================================*/
mp-wp_genesis 1084
mp-wp_genesis 1085 function setcookies()
mp-wp_genesis 1086 {
mp-wp_genesis 1087 for($x=0; $x<count($this->headers); $x++)
mp-wp_genesis 1088 {
mp-wp_genesis 1089 if(preg_match('/^set-cookie:[\s]+([^=]+)=([^;]+)/i', $this->headers[$x],$match))
mp-wp_genesis 1090 $this->cookies[$match[1]] = urldecode($match[2]);
mp-wp_genesis 1091 }
mp-wp_genesis 1092 }
mp-wp_genesis 1093
mp-wp_genesis 1094
mp-wp_genesis 1095 /*======================================================================*\
mp-wp_genesis 1096 Function: _check_timeout
mp-wp_genesis 1097 Purpose: checks whether timeout has occurred
mp-wp_genesis 1098 Input: $fp file pointer
mp-wp_genesis 1099 \*======================================================================*/
mp-wp_genesis 1100
mp-wp_genesis 1101 function _check_timeout($fp)
mp-wp_genesis 1102 {
mp-wp_genesis 1103 if ($this->read_timeout > 0) {
mp-wp_genesis 1104 $fp_status = socket_get_status($fp);
mp-wp_genesis 1105 if ($fp_status["timed_out"]) {
mp-wp_genesis 1106 $this->timed_out = true;
mp-wp_genesis 1107 return true;
mp-wp_genesis 1108 }
mp-wp_genesis 1109 }
mp-wp_genesis 1110 return false;
mp-wp_genesis 1111 }
mp-wp_genesis 1112
mp-wp_genesis 1113 /*======================================================================*\
mp-wp_genesis 1114 Function: _connect
mp-wp_genesis 1115 Purpose: make a socket connection
mp-wp_genesis 1116 Input: $fp file pointer
mp-wp_genesis 1117 \*======================================================================*/
mp-wp_genesis 1118
mp-wp_genesis 1119 function _connect(&$fp)
mp-wp_genesis 1120 {
mp-wp_genesis 1121 if(!empty($this->proxy_host) && !empty($this->proxy_port))
mp-wp_genesis 1122 {
mp-wp_genesis 1123 $this->_isproxy = true;
mp-wp_genesis 1124
mp-wp_genesis 1125 $host = $this->proxy_host;
mp-wp_genesis 1126 $port = $this->proxy_port;
mp-wp_genesis 1127 }
mp-wp_genesis 1128 else
mp-wp_genesis 1129 {
mp-wp_genesis 1130 $host = $this->host;
mp-wp_genesis 1131 $port = $this->port;
mp-wp_genesis 1132 }
mp-wp_genesis 1133
mp-wp_genesis 1134 $this->status = 0;
mp-wp_genesis 1135
mp-wp_genesis 1136 if($fp = fsockopen(
mp-wp_genesis 1137 $host,
mp-wp_genesis 1138 $port,
mp-wp_genesis 1139 $errno,
mp-wp_genesis 1140 $errstr,
mp-wp_genesis 1141 $this->_fp_timeout
mp-wp_genesis 1142 ))
mp-wp_genesis 1143 {
mp-wp_genesis 1144 // socket connection succeeded
mp-wp_genesis 1145
mp-wp_genesis 1146 return true;
mp-wp_genesis 1147 }
mp-wp_genesis 1148 else
mp-wp_genesis 1149 {
mp-wp_genesis 1150 // socket connection failed
mp-wp_genesis 1151 $this->status = $errno;
mp-wp_genesis 1152 switch($errno)
mp-wp_genesis 1153 {
mp-wp_genesis 1154 case -3:
mp-wp_genesis 1155 $this->error="socket creation failed (-3)";
mp-wp_genesis 1156 case -4:
mp-wp_genesis 1157 $this->error="dns lookup failure (-4)";
mp-wp_genesis 1158 case -5:
mp-wp_genesis 1159 $this->error="connection refused or timed out (-5)";
mp-wp_genesis 1160 default:
mp-wp_genesis 1161 $this->error="connection failed (".$errno.")";
mp-wp_genesis 1162 }
mp-wp_genesis 1163 return false;
mp-wp_genesis 1164 }
mp-wp_genesis 1165 }
mp-wp_genesis 1166 /*======================================================================*\
mp-wp_genesis 1167 Function: _disconnect
mp-wp_genesis 1168 Purpose: disconnect a socket connection
mp-wp_genesis 1169 Input: $fp file pointer
mp-wp_genesis 1170 \*======================================================================*/
mp-wp_genesis 1171
mp-wp_genesis 1172 function _disconnect($fp)
mp-wp_genesis 1173 {
mp-wp_genesis 1174 return(fclose($fp));
mp-wp_genesis 1175 }
mp-wp_genesis 1176
mp-wp_genesis 1177
mp-wp_genesis 1178 /*======================================================================*\
mp-wp_genesis 1179 Function: _prepare_post_body
mp-wp_genesis 1180 Purpose: Prepare post body according to encoding type
mp-wp_genesis 1181 Input: $formvars - form variables
mp-wp_genesis 1182 $formfiles - form upload files
mp-wp_genesis 1183 Output: post body
mp-wp_genesis 1184 \*======================================================================*/
mp-wp_genesis 1185
mp-wp_genesis 1186 function _prepare_post_body($formvars, $formfiles)
mp-wp_genesis 1187 {
mp-wp_genesis 1188 settype($formvars, "array");
mp-wp_genesis 1189 settype($formfiles, "array");
mp-wp_genesis 1190 $postdata = '';
mp-wp_genesis 1191
mp-wp_genesis 1192 if (count($formvars) == 0 && count($formfiles) == 0)
mp-wp_genesis 1193 return;
mp-wp_genesis 1194
mp-wp_genesis 1195 switch ($this->_submit_type) {
mp-wp_genesis 1196 case "application/x-www-form-urlencoded":
mp-wp_genesis 1197 reset($formvars);
mp-wp_genesis 1198 while(list($key,$val) = each($formvars)) {
mp-wp_genesis 1199 if (is_array($val) || is_object($val)) {
mp-wp_genesis 1200 while (list($cur_key, $cur_val) = each($val)) {
mp-wp_genesis 1201 $postdata .= urlencode($key)."[]=".urlencode($cur_val)."&";
mp-wp_genesis 1202 }
mp-wp_genesis 1203 } else
mp-wp_genesis 1204 $postdata .= urlencode($key)."=".urlencode($val)."&";
mp-wp_genesis 1205 }
mp-wp_genesis 1206 break;
mp-wp_genesis 1207
mp-wp_genesis 1208 case "multipart/form-data":
mp-wp_genesis 1209 $this->_mime_boundary = "Snoopy".md5(uniqid(microtime()));
mp-wp_genesis 1210
mp-wp_genesis 1211 reset($formvars);
mp-wp_genesis 1212 while(list($key,$val) = each($formvars)) {
mp-wp_genesis 1213 if (is_array($val) || is_object($val)) {
mp-wp_genesis 1214 while (list($cur_key, $cur_val) = each($val)) {
mp-wp_genesis 1215 $postdata .= "--".$this->_mime_boundary."\r\n";
mp-wp_genesis 1216 $postdata .= "Content-Disposition: form-data; name=\"$key\[\]\"\r\n\r\n";
mp-wp_genesis 1217 $postdata .= "$cur_val\r\n";
mp-wp_genesis 1218 }
mp-wp_genesis 1219 } else {
mp-wp_genesis 1220 $postdata .= "--".$this->_mime_boundary."\r\n";
mp-wp_genesis 1221 $postdata .= "Content-Disposition: form-data; name=\"$key\"\r\n\r\n";
mp-wp_genesis 1222 $postdata .= "$val\r\n";
mp-wp_genesis 1223 }
mp-wp_genesis 1224 }
mp-wp_genesis 1225
mp-wp_genesis 1226 reset($formfiles);
mp-wp_genesis 1227 while (list($field_name, $file_names) = each($formfiles)) {
mp-wp_genesis 1228 settype($file_names, "array");
mp-wp_genesis 1229 while (list(, $file_name) = each($file_names)) {
mp-wp_genesis 1230 if (!is_readable($file_name)) continue;
mp-wp_genesis 1231
mp-wp_genesis 1232 $fp = fopen($file_name, "r");
mp-wp_genesis 1233 $file_content = fread($fp, filesize($file_name));
mp-wp_genesis 1234 fclose($fp);
mp-wp_genesis 1235 $base_name = basename($file_name);
mp-wp_genesis 1236
mp-wp_genesis 1237 $postdata .= "--".$this->_mime_boundary."\r\n";
mp-wp_genesis 1238 $postdata .= "Content-Disposition: form-data; name=\"$field_name\"; filename=\"$base_name\"\r\n\r\n";
mp-wp_genesis 1239 $postdata .= "$file_content\r\n";
mp-wp_genesis 1240 }
mp-wp_genesis 1241 }
mp-wp_genesis 1242 $postdata .= "--".$this->_mime_boundary."--\r\n";
mp-wp_genesis 1243 break;
mp-wp_genesis 1244 }
mp-wp_genesis 1245
mp-wp_genesis 1246 return $postdata;
mp-wp_genesis 1247 }
mp-wp_genesis 1248 }
mp-wp_genesis 1249 endif;
mp-wp_genesis 1250 ?>