opts |= METHOD_GET; else if (!strcasecmp($w, 'POST')) $this->opts |= METHOD_POST; else if (!strcasecmp($w, 'HEAD')) $this->opts |= METHOD_HEAD; return true; } public final function setproxy($ip, $port, $w = USE_PROXY_HTTP) { if ((!is_numeric($port)) || (!in_array($w, Array(USE_PROXY_HTTP, USE_PROXY_SOCKS4, USE_PROXY_SOCKS5)))) { throw new Exception ('Parametres proxy incorrects'); return false; } $this->opts |= ~(USE_PROXY_HTTP|USE_PROXY_SOCKS4|USE_PROXY_SOCKS5); $this->opts |= $w; $this->proxy = Array($ip, $port); return true; } public final function cleardata() { $this->contents = $this->gheaders = Array(); $this->repcode = 0; $this->last = false; } public final function setheader($header, $value) { $header = trim($header); if (substr($header, -1, 1) == ':') $header = substr($header, 0, -1); $this->headers[strtolower($header)] = $value; return true; } private final function getheader($header) { $h = strtolower($header); return isset($this->headers[$h]) ? $this->headers[$h] : false; } public final function getheaders() { return $this->gheaders; } public final function getcookies() { $c = Array(); foreach ($this->gheaders as $a => $b) { if (!strcasecmp($b[0], 'set-cookie')) { if (preg_match('/^([^;]+)/', $b[1], $r)) $c[] = $r[1]; } } return $c; } public final function close() { if ($this->socket) @fclose($this->socket); } public final function addcontent($name, $content) { $this->contents[$name] = $content; return true; } private final function proxy_http($host, $port) { if (!$this->writeline('CONNECT '.$host.':'.$port.' HTTP/1.0'."\r\n".'Host: '.$this->proxy[0].':'.$this->proxy[1]."\r\n".'User-agent: Mozilla/5.0'."\r\n")) { throw new Exception ('Erreur d\'ecriture'); return false; } if (!preg_match('/^HTTP\/1\.\d 200/', $this->readline())) { throw new Exception ('Erreur de connexion à l\'hote via le proxy (code invalide)'); return false; } $b = false; while (($a = $this->readline()) !== false) { if ($a == "\r\n") { $b = true; break; } } return $b; } private final function proxy_socks5($host, $port) { if (!$this->writeline("\x05\x01\x00", false)) { throw new Exception ('Erreur d\'ecriture'); return false; } $b = $this->readline(10, true); if ($b === false) { throw new Exception ('Erreur de lecture'); return false; } if ((isset($b[1])) && ($b{1} == "\x00")) { if (!$this->writeline("\x05\x01\x00\x03".chr(strlen($host) % 256).$host.chr($port / 256).chr($port % 256), false)) { throw new Exception ('Erreur d\'ecriture'); return false; } $b = $this->readline(10, true); if ($b === false) { throw new Exception ('Erreur de lecture'); return false; } echo ord($b{0}); if ((isset($b{1})) && ($b{1} == "\x00")) return true; } return false; } private final function proxy_socks4($host, $port) { if (!$this->writeline("\x04\x01".chr(($port / 256)).chr($port % 256)."\x00\x00\x00".chr(rand(1, 255))."\x00".$host."\x00")) { throw new Exception ('Erreur d\'ecriture'); return false; } if (($b = @$this->readline(10, true)) === false) { throw new Exception ('Erreur d\'ecriture'); return false; } if ((isset($b{1})) && ($b{1} == "\x5A")) return true; return false; } public final function go($url) { $u = @parse_url($url); if (($u === false) || (!isset($u['scheme'])) || (!isset($u['host']))) { throw new Exception ('Url de destination incorrecte'); return false; } if ($this->opts & (USE_PROXY_HTTP|USE_PROXY_SOCKS4|USE_PROXY_SOCKS5)) { if (!$this->socket = @fsockopen($this->proxy[0], $this->proxy[1], $errno = NULL, $errstr = NULL, 5)) { throw new Exception ('Impossible de se connecter au serveur proxy.'); return false; } if ($this->opts & USE_PROXY_HTTP) { if (!$this->proxy_http($u['host'], (isset($u['port']) ? $u['port'] : 80))) { throw new Exception ('Impossible d\'utiliser le proxy'); return false; } } else if ($this->opts & USE_PROXY_SOCKS4) { if (!$this->proxy_socks4($u['host'], (isset($u['port']) ? $u['port'] : 80))) { throw new Exception ('Impossible d\'utiliser le proxy'); return false; } } else if ($this->opts & USE_PROXY_SOCKS5) { if (!$this->proxy_socks5($u['host'], (isset($u['port']) ? $u['port'] : 80))) { throw new Exception ('Impossible d\'utiliser le proxy'); return false; } } } else if (!$this->socket = @fsockopen($u['host'], (isset($u['port']) ? $u['port'] : 80), $errno = NULL, $errstr, 5)) { throw new Exception ('Impossible de se connecter à l\'hote distant '.$errstr); return false; } $request = isset($u['path']) ? $u['path'] : '/'; if (isset($u['query'])) $request .= '?'.$u['query']; if ($this->opts & METHOD_GET) { if (!$this->writeline('GET '.$request.' HTTP/1.0')) { throw new Exception ('Erreur d\'ecriture'); return false; } } else if ($this->opts & METHOD_POST) { if (!$this->writeline('POST '.$request.' HTTP/1.0')) { throw new Exception ('Erreur d\'ecriture'); return false; } } else if ($this->opts & METHOD_HEAD) { if (!$this->writeline('HEAD '.$request.' HTTP/1.0')) { throw new Exception ('Erreur d\'ecriture'); return false; } } if (!$this->writeline('host: '.$u['host'].':'.(isset($u['port']) ? $u['port'] : 80))) { throw new Exception ('Erreur d\'ecriture'); return false; } $q = ''; if (($this->opts & METHOD_POST) && ($this->contents)) { if ($this->getheader('content-type') === false) $this->setheader('content-type', 'application/x-www-form-urlencoded'); $q = http_build_query($this->contents); if ($this->getheader('content-length') === false) $this->setheader('content-length', strlen($q)); } foreach ($this->headers as $v => $w) { if (!$this->writeline($v.': '.$w)) { throw new Exception ('Erreur d\'ecriture'); return false; } } if (!$this->writeline('')) { throw new Exception ('Erreur d\'ecriture'); return false; } if (($this->opts & METHOD_POST) && ($q != '')) { $r = explode("\0", wordwrap($q, $this->buffer, "\0", true)); foreach ($r as $s) { if (!$this->writeline($s, false)) { throw new Exception ('Erreur d\'ecriture'); return false; } } } while (($g = $this->readline()) !== false) { if ($g == "\r\n") break; if (preg_match('/^HTTP\/1\.\d (\d+) /', $g, $r)) $this->repcode = $r[1]; else if (preg_match('/^([^:]+):([^\r\n]+)/', $g, $r)) $this->gheaders[] = Array($r[1], trim($r[2])); } if ($this->repcode == 0) { throw new Exception ('Erreur de lecture :: Pas de header http correct ?'); return false; } return $this->repcode; } public final function readline($buffer = 1024, $force = false) { if (!$this->socket) return false; if ($force) { if ($this->last) { $this->last = false; return substr(@fread($this->socket, $buffer+1), 1); } return @fread($this->socket, $buffer); } else { $this->last = true; return @fgets($this->socket, $buffer); } } public final function readall() { return @stream_get_contents($this->socket); } private final function writeline($line, $lf = true) { if ($lf) $line .= "\r\n"; if ((@fwrite($this->socket, $line)) !== false) return true; return false; } public final function getasxml() { return @simplexml_load_string($this->readall()); } } ?>