WG: AW: HTML_DocumentDependencies / Net_URL problem
| From: | Christoph Lütjen - webEdition Software GmbH | Date: | Thu, 26 Jan 2006 20:26:09 +0000 |
| Subject: | WG: AW: HTML_DocumentDependencies / Net_URL problem | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-41101@lists.php.net to get a copy of this message | ||
Hi Laurent,
thanks again for your answer - yes I understand what you mean ;-)
... and sorry for the next "but" ...
If I follow your suggestion, I would have to parse the url myself. Ok, no
problem but it would be exactly what Net_URL::__construct() does - except of
the one thing that Net_URL always uses the url of the current script to get
values for missing parts in incomplete URLs.
My current solution is quite easy and works fine, but it's "not nice": I
just set $_SERVER["HTTP_HOST"], $_SERVER['PHP_SELF'],.. to the values of the
URL I've opened and everything works fine..
So currently I see three possibilities and I really don't know ...
1) Extending Net_URL to accept a second parameter
$url = new Net_URL('./test.html', 'http://www.remotehost.tld/folder/');
So I can tell the class what to use as defaults instead of
the current script.
(Would be about 5 lines in Net_URL::__construct() ;-)
2) Parse the URL myself. (Copy and Paste Net_URL and add the five lines for
the default values... or write something around Net_URL that would be very
similar to Net_URLs constructor function..)
3) Use my current solution.
Imho the only clean way is candidate No. 1. If I'm right: May I reopen the
feature request? No. 2 stands in conflict to oop guidelines and No. 3 is -
in german I would say "totaler Pfusch", my English dictionary says "botch".
If I'm on the wrong path, please push me someone back to the right one :-)
Chris
-----Ursprüngliche Nachricht-----
Von: Laurent Laville [mailto:pear@laurent-laville.org]
Gesendet: Mittwoch, 25. Januar 2006 18:48
An: Christoph Lütjen - webEdition Software GmbH
Cc: pear-dev@lists.php.net
Betreff: Re: AW: HTML_DocumentDependencies / Net_URL problem
Christoph Lütjen - webEdition Software GmbH a écrit :
>
> Exactly that's my problem. "º!äøÃ°së
> P@³¶!ëhttp://host2/example/text.html" contains e.g.
>
> <a href="./anotherfile.html">text</a>
>
> This Link refers to http://host2/example/anotherfile.html and
> not to
> something relative to the script that's currently running. It's relative
to
> the opened file (using HTTP_Request($uri) and not to the PHP Script that
has
> opened it.
>
>
>
About relative path, i think we could have (security) problem , if we
could navigate into remote (host2) directories.
So solve a relative
"./anotherfile.html"
which should give
http://host2/example/anotherfile.html
will be impossible. (hope i'm not wrong)
But you can set and replace values you expect to have.
//host1/parser.php
<?php
require_once 'Net/URL.php';
require_once 'HTTP/Request.php';
$uri = 'http://host2/example/text.html';
$href_doc1 = new Net_URL($uri);
var_dump($href_doc1);
$host1_path = dirname($href_doc1->path);
var_dump($host1_path);
$req = &new HTTP_Request($uri);
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->sendRequest();
$response1 = $req->getResponseBody();
$dom = new DOMDocument();
@$dom->loadHTML($response1);
$tags = $dom->getElementsByTagName('a');
foreach ($tags as $tag) {
$dest = $tag->getAttribute('href');
$href_doc = new Net_URL($dest);
var_dump($href_doc);
$host2_dest = $href_doc1->protocol .'://'. $href_doc1->host .
$host1_path;
if (substr($href_doc->url, 0, 1) !== '/') {
$host2_dest .= '/';
}
$host2_dest .= $href_doc->url;
$tag = array(
'host' => $href_doc1->host,
'url' => $host2_dest
);
var_dump($tag); // [1]
}
?>
Sorry for the draft and lack of explains, but i'm pretty sure you can
understand
what i means.
with a //host2/example/text.html doc that have 2 links
<a href="/link.html">text 1</a>
<a href="./anotherfile.html">text 2</a>
In [1] you will have such output :
host => host2
url => http://host2/example/link.html
then
host => host2
url => http://host2/example/./anotherfile.html
> Hoping I'm not simply too stupid... ;-)
>
>
There are no stupid question, but answers may be stupid (hope it will be
not my case ;-) )
Laurent