
23/02/2007, 21:08
|
| | Fecha de Ingreso: noviembre-2006 Ubicación: Salinas
Mensajes: 216
Antigüedad: 18 años, 3 meses Puntos: 0 | |
Re: Problema con robot.
class.url.php Código PHP: <?php
// -----------------------------------------------------
//
// Author: WiZ2
// E-Mail: [email protected]
// Date (YYYY/MM/DD): 2002-08-26
// Revision (YYYY/MM/DD): 2002-09-22
// Version: 1.5
//
// -----------------------------------------------------
//
// -----------------------------------------------------
// Class:
//
// void = url (string $url)
// void = parseUrl (string $url)
//
// string = getProtocol ();
// string = getServer ();
// string = getFileA ();
// string = getFile ();
// string = getPath ();
// string = getArgs ();
// string = getUriA ();
// string = getUrl ();
// string = getExtension ();
//
// object url = getAbsolute (object url);
// bool = inDomain (object url);
// string = getParent (string path);
//
// void = setProtocol (string protocol);
// void = setServer (string server);
// void = setPath (string path);
// void = setFile (string file);
// void = setArgs (string args);
//
// bool = download ();
// bool = save (string path);
// bool = exists ();
//
// -----------------------------------------------------
// Other:
//
// Functions to write & read files:
//
// bool = write_file (string path, string txt, [mode = w|w+|a|a+]);
// string = read_file (string path, [mode = r]);
//
// -----------------------------------------------------
//
//
// ----------------
// 1.5 - 2002-09-22
//
// - Add function inDomain to class
// - Add function exists to class
// ----------------
// 1.3 - 2002-09-22
//
// - Add function download to class
// - Add function save to class
// - Add function getExtension to class
// - Add write_file to script
// - Add read_file to script
//
// ----------------
// 1.0 - 2002-08-26
//
// - First release
//
class url {
// Internal variables
var $protocol;
var $server;
var $path;
var $file;
var $fileargs;
// Get functions
function getProtocol () { return $this->protocol; }
function getServer () { return $this->server; }
function getFileA () { return $this->file; }
function getFile () { return $this->file . $this->fileargs; }
function getPath () { return $this->path; }
function getArgs () { return $this->fileargs; }
function getUriA () { return $this->path . $this->file; }
function getUri () { return $this->path . $this->file . $this->fileargs; }
function getUrl () { return $this->protocol . $this->server . $this->path . $this->file . $this->fileargs; }
function getExtension() { return substr($this->file, strpos($this->file, ".")); }
function getAbsolute ($objUri) {
if (strlen ($objUri->getServer()) == 0) {
$act = $objUri->getPath ();
$frm = $this->getPath ();
if (substr ($act, 0, 1) == "/") $frm = "";
$act = explode ("/", $act);
for ($n = 0; $n < sizeof ($act); $n++) {
if (strlen ($act[$n]) > 0) {
if ($act[$n] == "..") {
$frm = $this->getParent ($frm);
} elseif ($act[$n] != ".") {
if (strpos ($act[$n], ".") != "" && $n == sizeof ($act) - 1) {
$frm .= $act[$n];
} else {
$frm .= $act[$n] . "/";
}
}
}
}
return (new url ($this->protocol . $this->server . $frm . $objUri->getFile()));
} else {
return ($objUri);
}
}
function inDomain ($objUri) { return ($objUri->server == $this->server); }
// Set functions
function setProtocol ($protocol) { $this->protocol = $protocol; }
function setServer ($server) { $this->server = $server; }
function setPath ($path) { $this->path = $path; }
function setFile ($file) { $this->file = $file; }
function setArgs ($args) { $this->fileargs = $args; }
// Parse functions
function parseURL ($url) {
$this->protocol = "http://"; $this->server = "";
$this->path = ""; $this->file = ""; $this->fileargs = "";
$act = 0;
for ($n = 0; $n < strlen ($url); $n++) {
$c = substr ($url, $n, 1);
if (substr ($url, $n, 3) == "://") {
if ($act == 0) {
$this->protocol = $buffer . "://";
$buffer = ""; $act = 1; $n+=2;
} else {
$buffer .= $c;
}
} elseif ($c == "/") {
$buffer .= $c;
if (strpos ($buffer, ".") != "") $act = 1;
if ($act == 0) {
$this->path .= $buffer; $buffer = "";
} elseif ($act == 1) {
$this->server = $buffer;
$act = 0; $buffer = "";
}
} elseif ($c == "?") {
if ($act == 0) {
$this->file = $buffer;
$act = 2; $buffer = "";
} else {
$buffer .= $c;
}
} else {
$buffer .= $c;
}
}
if (strlen ($buffer) > 0) {
if ($act == 2) {
$this->fileargs = "?" . $buffer;
} elseif ($act == 0) {
if (strpos ($buffer, ".") != "") {
$this->file = $buffer;
} else {
$this->path .= $buffer . "/";
}
}
}
}
// Internal functions
function getParent ($url) {
if (substr ($url, strlen ($url) - 1, 1) == "/") $url = substr ($url, 0, strlen ($url) - 1);
return substr ($url, 0, strrpos ($url, "/") + 1);
}
// Hacks
function download() {
$url = $this->getUrl();
if (trim(strtolower($this->getProtocol())) == "file://") $url = substr($url, 7);
$code = "";
if ($fd = @fopen($url, "r")) {
while (!feof($fd)) $code .= fread($fd, 40960);
fclose($fd);
}
return $code;
}
function save($file) {
$url = $this->getUrl();
if (trim(strtolower($this->getProtocol())) == "file://") $url = substr($url, 7);
if ($fd = @fopen($url, "r")) {
if ($fd2 = @fopen($file, "w+")) {
$code = "";
while (!feof($fd)) fwrite($fd2, fread($fd, 40960));
fclose($fd2);
return true;
}
fclose($fd);
}
return false;
}
function exists() {
//ob_start();
$url = $this->getUrl();
if (trim(strtolower($this->getProtocol())) == "file://") $url = substr($url, 7);
if ($fd = @fopen($url, "r")) {
fclose($fd);
return true;
} else {
return false;
}
//ob_end_clean();
}
// Constructor
function url ($url) { parseURL ($url); }
}
function write_file($path, $txt, $mode = "w+") {
if ($fd = @fopen($path, $mode)) {
fwrite($fd, $txt);
fclose($fd);
return true;
} else {
return false;
}
}
function read_file($path, $mode = "r") {
$code = "";
if ($fd = @fopen($path, $mode)) {
while(!feof($fd)) $code .= fread($fd, 409600);
fclose($fd);
}
return $code;
}
?> |