PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Text » Text_Wiki » Bug #3004

Wrong path for rule "image"

Details

Submitted2004-12-21 15:20 UTC
Fromreg at dav-muz dot net
Assignedpmjones
StatusClosed
PackageText_Wiki
PHP Version4.3.8
OSLinux
Roadmaps(Not assigned)

Comments

[2004-12-21 15:20 UTC] reg at dav-muz dot net

Description:
------------
If the host ($_SERVER['DOCUMENT_ROOT']) is, for example, "/home/my/public_html" and my index.php is in "/home/my/public_html/sub/site" the link generated by rule "image" omitt "/sub/site".

I have solved the problem in "Reproduce code" section.

Reproduce code:
---------------
<?php
// Actual situation.
$_SERVER['DOCUMENT_ROOT'] == "/home/my/public_html";
$_SERVER['HTTP_REFERER'] == "http://localhost/sub/site";

// Modify the base path.
$Wiki->setRenderConf('xhtml', 'image', 'base', "./share/images/");
// Without the initial dot the image will not be found.

// String to parse.
$textToParse = '[[image event/PHOTOsmall.jpg link="./share/images/event/PHOTOmedium.jpg"]]'
?>

To solve the problem I have changed the line 96 in /PEAR/Text/Wiki/Render/Xhtml/Image.php with:
$imageFile = $_SERVER['HTTP_REFERER'] . $src;

Expected result:
----------------
<a href="./share/images/event/PHOTOmedium.jpg"><img src="./share/images/event/PHOTOsmall.jpg" /></a>

Actual result:
--------------
Warning: getimagesize(/home/my/public_html./share/images/event/PHOTOsmall.jpg): failed to open stream: No such file or directory in /home/my/PEAR/Text/Wiki/Render/Xhtml/Image.php on line 96

<--! The image is linked and displayed correctly. -->
<a href="./share/images/event/PHOTOmedium.jpg"><img src="./share/images/event/PHOTOsmall.jpg" /></a>

[2005-02-24 15:41 UTC] reg at dav-muz dot net

The problem is getsizeimage(), it don't find the file. It don't work with absolute path if I am in a subsite, and it don't work again if I use relative path.
I don't know where is the website because it can be installed everywhere, so I really need to use relative paths in "base" variable.

Situation:
- $_SERVER[DOCUMENT_ROOT] => "/home/dati/www/htdocs"
- $_SERVER[PATH_TRANSLATED] => "/home/dati/www/htdocs/DEV/dav-muz.net/index.php"
- correct root sould be (don't exist a system variable for that...): "/home/dati/www/htdocs/DEV/dav-muz.net" or others for test and production copy.

This is the code:
<code>
[[image event/dscn1122s.jpg link="./share/downloads/event/dscn1122m.jpg"]] # Relative...
[[image event/dscn1122s.jpg link="/share/downloads/event/dscn1122m.jpg"]] # ...or absolute
</code>

This is the base variable:
<php>
$Wiki->setRenderConf('xhtml', 'image', 'base', './share/downloads/'); # I have to use a relative path; the absolute path is not definite because it change frequently.
</php>

Output generated by relative path, bad:
<output>
<p>Warning: getimagesize(/home/dati/www/htdocs./share/downloads/event/dscn1122s.jpg): failed to open stream: No such file or directory in /home/dati/www/php/Text/Wiki/Render/Xhtml/Image.php on line 97</p>
<a href="./share/downloads/event/dscn1122m.jpg"><img src="./share/downloads/event/dscn1122s.jpg" /></a>
</output>

Output generated by absolute path, bad:
<output>
<p>Warning: getimagesize(/home/dati/www/htdocs/share/downloads/event/dscn1122s.jpg): failed to open stream: No such file or directory in /home/dati/www/php/Text/Wiki/Render/Xhtml/Image.php on line 97</p>
<a href="/share/downloads/event/dscn1122m.jpg"><img src="/share/downloads/event/dscn1122s.jpg" /></a>
</output>

---

This is the patch, it start at line 84 of /Text/Wiki/Render/Xhtml/Image.php file:
<patch>
// try to guess width and height
if (! isset($options['attr']['width']) &&
! isset($options['attr']['height'])) {

// does the source refer to a local file or a URL?
if (strpos($src,'://')) {
// is a URL link
$imageFile = $src;
} elseif ($src[0] == '.') { # ADDED. If the path is a relative path...
// is a local file on relative path
$imageFile = $src; # ...don't do anything because it's perfect!
} else {
// is a local file on absolute path
$imageFile = $_SERVER['DOCUMENT_ROOT'] . $src;
}

// attempt to get the image size
$imageSize = @getimagesize($imageFile);

if (is_array($imageSize)) {
$options['attr']['width'] = $imageSize[0];
$options['attr']['height'] = $imageSize[1];
}

}
</patch>

Output generated, correct without warnings:
<output>
<a href="./share/downloads/event/dscn1122m.jpg"><img src="./share/downloads/event/dscn1122s.jpg" /></a>
</output>

The patch is compatible with the past and supply who uses subsites and frameworks. :-)