PDA

View Full Version : examples/tree/get-nodes.php


dj
07-22-2007, 01:59 PM
Applies to Ext 1.1 (SVN) and Ext 2.0 (SVN)

examples/tree/get-nodes.php generates some warnings if it is executed in a PHP interpreter with a higher warning level.

Here is a diff that fixes the warnings:

Index: D:/workspace/ext/examples/tree/get-nodes.php
================================================== =================
--- D:/workspace/ext/examples/tree/get-nodes.php (revision 771)
+++ D:/workspace/ext/examples/tree/get-nodes.php (working copy)
@@ -19,8 +19,8 @@
elseif($p !== false) $val = round($val, $digits-$p);
return round($val, $digits) . " " . $symbols[$i] . $bB;
}
-$dir = $_REQUEST['lib'] == 'yui' ? '../../../' : '../../';
-$node = $_REQUEST['node'];
+$dir = isset($_REQUEST['lib'])&&$_REQUEST['lib'] == 'yui' ? '../../../' : '../../';
+$node = isset($_REQUEST['node'])?$_REQUEST['node']:"";
if(strpos($node, '..') !== false){
die('Nice try buddy.');
}
@@ -31,11 +31,11 @@
$lastmod = date('M j, Y, g:i a',filemtime($dir.$node.'/'.$f));
if(is_dir($dir.$node.'/'.$f)){
$qtip = 'Type: Folder<br />Last Modified: '.$lastmod;
- $nodes[] = array('text'=>$f, id=>$node.'/'.$f/*, qtip=>$qtip*/, cls=>'folder');
+ $nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f/*, 'qtip'=>$qtip*/, 'cls'=>'folder');
}else{
$size = formatBytes(filesize($dir.$node.'/'.$f), 2);
$qtip = 'Type: JavaScript File<br />Last Modified: '.$lastmod.'<br />Size: '.$size;
- $nodes[] = array('text'=>$f, id=>$node.'/'.$f, leaf=>true/*, qtip=>$qtip, qtipTitle=>$f */, cls=>'file');
+ $nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f, 'leaf'=>true/*, 'qtip'=>$qtip, 'qtipTitle'=>$f */, 'cls'=>'file');
}
}
$d->close();


... and for convenience the patched file (based on revision 771 of trunk):

<?
// from php manual page
function formatBytes($val, $digits = 3, $mode = "SI", $bB = "B"){ //$mode == "SI"|"IEC", $bB == "b"|"B"
$si = array("", "K", "M", "G", "T", "P", "E", "Z", "Y");
$iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi");
switch(strtoupper($mode)) {
case "SI" : $factor = 1000; $symbols = $si; break;
case "IEC" : $factor = 1024; $symbols = $iec; break;
default : $factor = 1000; $symbols = $si; break;
}
switch($bB) {
case "b" : $val *= 8; break;
default : $bB = "B"; break;
}
for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
$val /= $factor;
$p = strpos($val, ".");
if($p !== false && $p > $digits) $val = round($val);
elseif($p !== false) $val = round($val, $digits-$p);
return round($val, $digits) . " " . $symbols[$i] . $bB;
}
$dir = isset($_REQUEST['lib'])&&$_REQUEST['lib'] == 'yui' ? '../../../' : '../../';
$node = isset($_REQUEST['node'])?$_REQUEST['node']:"";
if(strpos($node, '..') !== false){
die('Nice try buddy.');
}
$nodes = array();
$d = dir($dir.$node);
while($f = $d->read()){
if($f == '.' || $f == '..' || substr($f, 0, 1) == '.')continue;
$lastmod = date('M j, Y, g:i a',filemtime($dir.$node.'/'.$f));
if(is_dir($dir.$node.'/'.$f)){
$qtip = 'Type: Folder<br />Last Modified: '.$lastmod;
$nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f/*, 'qtip'=>$qtip*/, 'cls'=>'folder');
}else{
$size = formatBytes(filesize($dir.$node.'/'.$f), 2);
$qtip = 'Type: JavaScript File<br />Last Modified: '.$lastmod.'<br />Size: '.$size;
$nodes[] = array('text'=>$f, 'id'=>$node.'/'.$f, 'leaf'=>true/*, 'qtip'=>$qtip, 'qtipTitle'=>$f */, 'cls'=>'file');
}
}
$d->close();
echo json_encode($nodes);
?>

jack.slocum
07-23-2007, 05:18 AM
I have mixed feelings about this. Personally, I like to keep my PHP warning settings to a normal usable level (which I believe happen to be the default :D). I have seen code like this in phpBB and other packages with all the isset() checks, etc and I can't imagine why anyone would want to code like that. PHP code is already hideous enough. :)

Thanks for providng the updated file.

dj
07-23-2007, 09:58 AM
I don't know exactly why PHP requires you to check all variables and hash keys but I guess the following:

misspelled variable names are a very common error in PHP programs and the warnings can help you track those errors down.
Accessing variables or hash keys that do not exist might actually create them initialized with null.
PHP does not have a "undefined" value. !isset() is the only correct way to check if a value is not defined.


The missing quotes for the hash keys could make some trouble if someone would actually define these constants.
define("id", "harharhar");
Ok, that's not really going to happen but it could... anyway if you add the quotes you save the PHP interpreter a lookup whether the constant is valid or not.