Path: blob/master/web-gui/buildyourownbotnet/assets/js/aci-tree/php/FsTree.php
1293 views
<?php12// a sample class to show file system based aciTree3// note: a simple file system helper class is at the end of this file45class FsTree extends Tree {67private $fs = null;89public function __construct(Fs $fs) {10$this->fs = $fs;11}1213/*14* $parentId will be the path to the folder.15*/1617public function branch($parentId = null) {18$branch = array();19$list = $this->fs->folders($parentId);20foreach ($list as $entry) {21$branch["$parentId/$entry"] = $entry;22}23$list = $this->fs->files($parentId);24foreach ($list as $entry) {25$branch["$parentId/$entry"] = $entry;26}27return $branch;28}2930/**31* Return TRUE if it's a TREE folder (have children).32* @param string $path33* @param string $icon - item icon34*/35private function hasChildren($path, &$icon) {36if (is_dir($path)) {37$icon = 'folder';38// here we can return NULL instead of checking for children39// return null;40return !$this->fs->isEmpty($path);41} else {42$icon = 'file';43return false;44}45}4647/*48* $itemId will be the path to the file/folder.49*/5051public function itemProps($itemId) {52$itemId = trim($itemId, '/\\');53if ($this->fs->allow($itemId, $real)) {5455return array_merge(parent::itemProps($itemId), array(56'inode' => $this->hasChildren($real, $icon),57'icon' => $icon,58'size' => (is_file($real) ? formatSizeUnits(filesize($real)) : ''),59'type' => recongnizeType($real),60'random' => mt_rand(0, 99) // just a random property61));62}63return parent::itemProps($itemId);64}6566}6768// a file system helper for getting file system folders/files69// and for limiting the listings to the base folder7071class Fs {7273// keep the base folder74private $base = null;7576public function __construct($base) {77$this->base($base);78}7980/**81* Set/Get the base folder.82* @param string $base83* @return string84*/85public function base($base = null) {86if ($base === null) {87return $this->base;88} else {89$this->base = realpath($base);90}91}9293/**94* Check if $path is under $this->base.95* @param string $path - relative path96* @param string $real - absolute path97* @return bool98*/99public function allow($path, &$real = null) {100$real = realpath("$this->base/$path");101return strpos($real, $this->base) === 0;102}103104/**105* Get a list of folders.106* @param string $path - relative path107* @return array108*/109public function folders($path) {110$list = array();111if ($this->allow($path, $path) && is_dir($path)) {112$handle = opendir($path);113if ($handle) {114while ($entry = readdir($handle)) {115if (($entry != '.') && ($entry != '..') && is_dir("$path/$entry")) {116$list[] = $entry;117}118}119closedir($handle);120}121}122asort($list);123return $list;124}125126/**127* Check if a folder is empty.128* @param string $path - absolute path129* @return boolean130*/131public function isEmpty($path) {132$handle = opendir($path);133if ($handle) {134while ($entry = readdir($handle)) {135if (($entry != '.') && ($entry != '..')) {136closedir($handle);137return false;138}139}140closedir($handle);141}142return true;143}144145/**146* Get a list of files.147* @param string $path - relative path148* @return array149*/150public function files($path) {151$list = array();152if ($this->allow($path, $path) && is_dir($path)) {153$handle = opendir($path);154if ($handle) {155while ($entry = readdir($handle)) {156if (($entry != '.') && ($entry != '..') && is_file("$path/$entry")) {157$list[] = $entry;158}159}160closedir($handle);161}162}163asort($list);164return $list;165}166167}168169170function formatSizeUnits($bytes)171{172if ($bytes >= 1073741824)173{174$bytes = number_format($bytes / 1073741824, 2) . ' GB';175}176elseif ($bytes >= 1048576)177{178$bytes = number_format($bytes / 1048576, 2) . ' MB';179}180elseif ($bytes >= 1024)181{182$bytes = number_format($bytes / 1024, 2) . ' KB';183}184elseif ($bytes > 1)185{186$bytes = $bytes . ' bytes';187}188elseif ($bytes == 1)189{190$bytes = $bytes . ' byte';191}192else193{194$bytes = '0 bytes';195}196197return $bytes;198}199200201function recongnizeType($real)202{203$ext = strtoupper( end(explode(".", $real)) );204205switch($ext)206{207case "GIF":208case "PNG":209case "JPG":210case "JPEG":211case "TIF":212return "Image";213214215case "TXT":216case "PDF":217case "RTF":218return "Text";219}220221if( ! is_file($real))222return "Folder";223224return "Unknown File Type";225}226227