266 lines
5.6 KiB
PHP
Executable File
266 lines
5.6 KiB
PHP
Executable File
<?php
|
|
/*
|
|
interface dataItem {
|
|
|
|
function __construct($item);
|
|
function lister($deepscan);
|
|
|
|
|
|
}
|
|
*/
|
|
|
|
class DSDirectory {
|
|
|
|
//var $root = $_SERVER['DOCUMENT_ROOT'];
|
|
var $dir = '.';
|
|
var $targetDir = '';
|
|
|
|
var $listing = array();
|
|
|
|
|
|
function __construct($item, $target = '.') {
|
|
|
|
$this->dir = $item . '/';
|
|
$tg = $this->makeAR(realpath($item), realpath($target));
|
|
if (empty($tg)) $tg = './';
|
|
$this->targetDir = $tg;
|
|
}
|
|
|
|
function chroot ($root) {
|
|
$this->root = realpath($root);
|
|
return $this->root;
|
|
}
|
|
|
|
function lister($deepscan, $deep = 1) {
|
|
|
|
$dir = $this->targetDir;
|
|
$this->listing = $this->recursiveList($dir, $deepscan, $deep++);
|
|
return $this->listing;
|
|
}
|
|
|
|
function read($item) {
|
|
|
|
/*if (!is_array($item)) return $this->readOne($item);
|
|
$items = array();
|
|
foreach($item as $oneitem) {
|
|
|
|
$items[] = $this->readOne($oneitem);
|
|
|
|
}
|
|
return $items;
|
|
*/
|
|
return $this->vector('readOne', array($item));
|
|
}
|
|
|
|
function write($item, $data) {
|
|
|
|
if (!is_array($item) && !is_array($data)) return $this->writeOne($item, $data);
|
|
if (sizeof($item) != sizeof($data)) {
|
|
echo "DSDirectory::write error $item and $data have a different length.";
|
|
return false;
|
|
}
|
|
$items = array();
|
|
foreach($item as $key => $oneitem) {
|
|
|
|
$items[] = $this->writeOne($oneitem, $data[$key]);
|
|
|
|
}
|
|
return array_combine($item, $data);
|
|
|
|
//return $this->vector('writeOne', array($item, $data));
|
|
}
|
|
|
|
function writeH($items) {
|
|
$itemi = array();
|
|
foreach($items as $item) {
|
|
$itemi[] = $this->writeOne($item['path'], $item['data']);
|
|
}
|
|
return $itemi;
|
|
}
|
|
|
|
|
|
private function makeAR($base, $target) { // both absolute
|
|
|
|
$rel = '';
|
|
$sep = "![\\|/]!";
|
|
$bases = preg_split($sep, $base);
|
|
$tg = preg_split($sep, $target);
|
|
$b = min(sizeof($bases), sizeof($tg));
|
|
$i = 0;
|
|
|
|
while ($i < $b && $bases[$i] == $tg[$i]) {
|
|
$i++;
|
|
}
|
|
$z = $i;
|
|
|
|
while (!empty($bases[$i])) {
|
|
$rel .= "../";
|
|
$i++;
|
|
}
|
|
|
|
while (!empty($tg[$z])) {
|
|
$rel .= $tg[$z] . "/";
|
|
$z++;
|
|
}
|
|
|
|
return $rel; // relative to base
|
|
}
|
|
|
|
|
|
private function vector($func, $arglist) {
|
|
|
|
$cplx = false;
|
|
$str = '';
|
|
$i = 0;
|
|
foreach($arglist as $key => $item) {
|
|
$cplx = $cplx || is_array($item);
|
|
$str .= "\$arglist[$key], ";
|
|
}
|
|
$str = ereg_replace(", $", "", $str);
|
|
if (!$cplx) return $this->$func($item);
|
|
|
|
$items = array();
|
|
|
|
foreach (current($arglist) as $unitem) {
|
|
|
|
$items[] = eval('$this->$func(' . $str . ');');
|
|
}
|
|
|
|
|
|
return eval('array_combine(' . $str . ');');
|
|
}
|
|
|
|
private function deleteOne($item) {
|
|
|
|
if ( !is_dir($item) ) unlink($item);
|
|
return true;
|
|
}
|
|
|
|
private function readOne($item) {
|
|
|
|
$content = '';
|
|
$file = $this->dir . $item;
|
|
$fd = fopen($file, "r");
|
|
while (!feof($fd)) {
|
|
$content .= fgets($fd, 256);
|
|
}
|
|
fclose($fd);
|
|
return $content;
|
|
}
|
|
|
|
private function writeOne($item, $data) {
|
|
|
|
|
|
$path = pathinfo($item);
|
|
echo $path['dirname'] . "\n";
|
|
if (!file_exists($path['dirname'])) {
|
|
mkdir($path['dirname'], 077, 1);
|
|
}
|
|
$file = $item;
|
|
$fd = fopen($file, "w");
|
|
fwrite($fd, $data);
|
|
fclose($fd);
|
|
return $data;
|
|
}
|
|
|
|
private function appendOne($item, $data) {
|
|
|
|
$file = $this->dir . $item;
|
|
$fd = fopen($file, "a");
|
|
fwrite($fd, $data);
|
|
fclose($fd);
|
|
return $data;
|
|
}
|
|
|
|
private function readOneHex($item) {
|
|
|
|
$c = '';
|
|
$file = $this->dir . $item;
|
|
$fid = fopen($file, 'r');
|
|
while (!feof($fid)) {
|
|
$c .= bin2hex(fgets($fid, 2));
|
|
}
|
|
fclose($fid);
|
|
return $c;
|
|
}
|
|
|
|
function sizeFile($size) {
|
|
|
|
if ($size < 1024) $size = $size . ' o';
|
|
else if ($size < 1024 * 1024) $size = (int)($size / 1024) . ' Ko';
|
|
else if ($size < 1024 * 1024 * 1024) $size = (int)($size / 1024 / 1024) . ' Ko';
|
|
else $size = (int)($size / 1024 / 1024 / 1024) . ' Mo';
|
|
return $size;
|
|
}
|
|
|
|
function synchronise() {
|
|
|
|
}
|
|
|
|
private function recursiveList($curr, $deepscan, $deep) {
|
|
$deep--;
|
|
$result = array();
|
|
$list = scandir($curr);
|
|
if (!$list) return null;
|
|
$isempty = true;
|
|
$z = sizeof($list);
|
|
foreach($list as $key => $elem) {
|
|
if (!in_array($elem, array('..', '.'))) {if (is_dir($curr . $elem) and $elem != '.' and $elem != '..') {
|
|
|
|
if ($deepscan) {
|
|
if ( $deep < 1 ) $res = null;
|
|
else $res = $this->recursiveList($curr . $elem . '/', 1, $deep);
|
|
$tot = 0;
|
|
if ($res){
|
|
foreach($res as $k => $it) {
|
|
$tot += $it['size'];
|
|
$it['size'] = $this->sizeFile($it['size']);
|
|
}
|
|
}
|
|
$result[] = array( 'type' => 'dir', 'curr' => $curr, 'name' => $elem, 'size' => $tot, 'subC' => $res);
|
|
} else {
|
|
$result[] = array( 'type' => 'dir', 'curr' => $curr, 'name' => $elem, 'size' => 0 );
|
|
}
|
|
$isempty = false;
|
|
} elseif (!$deepscan || ($elem != '.' and $elem != '..')) {
|
|
$a = preg_split('!\.!', $elem);
|
|
$type = $a[sizeof($a) - 1];
|
|
if ($elem == '.' || $elem == '..') $type = 'dir';
|
|
$size = filesize($curr . $elem );
|
|
//$size = $this->sizeFile($size);
|
|
$result[] = array( 'type' => $type, 'curr' => $curr, 'name' => $elem, 'size' => $size );
|
|
$isempty = false;
|
|
} elseif ($elem == '.' || $elem == '..') {
|
|
|
|
$result[] = array( 'type' => 'dir', 'curr' => $curr, 'name' => $elem, 'size' => 0 );
|
|
}
|
|
}
|
|
}
|
|
if ($isempty) return null;
|
|
return $result;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function recursiveTpl($list) {
|
|
$template = new Template('.');
|
|
$template->set_filenames(array('explorer' => 'dev2.html'));
|
|
if (!$list) return false;
|
|
foreach ($list as $row) {
|
|
if ($row['type'] == 'dir') {
|
|
if (isset($row['subC'])) $row['sub'] = recursiveTpl($row['subC']);
|
|
$template->assign_block_vars("dir", $row);
|
|
} else {
|
|
$template->assign_block_vars("file", $row);
|
|
}
|
|
}
|
|
$template->pparse('explorer');
|
|
return $template->xml;
|
|
}
|
|
|
|
|
|
|
|
?>
|