Example #1
0
 /**
  *
  * create directory tree html
  */
 public function getDirectoryTree($path, $skip_files = false, $link_prefix = '')
 {
     $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
     $dom = new DomDocument("1.0");
     $li = $dom;
     $ul = $dom->createElement('ul');
     $li->appendChild($ul);
     $el = $dom->createElement('li');
     $a = $dom->createElement('a', 'Home');
     $at = $dom->createAttribute('clink');
     $at->value = $link_prefix;
     $el->appendChild($a);
     $el->appendChild($at);
     $ul->appendChild($el);
     $node = $ul;
     $depth = -1;
     $skip_dir = '';
     foreach ($objects as $object) {
         $name = $object->getFilename();
         $path = $object->getPathname();
         $isDir = is_dir($path);
         $link = str_replace($this->_repository, '', $path);
         $level = substr_count($link, '\\');
         $link = File::encodeurl(ltrim($link, '/\\'));
         $accessLevel = $_SESSION['role'] == 'Lecturer' ? 2 : 0;
         //skip unwanted folders to 2 levels
         if (!in_array($link, $this->_allowedPaths) && $level <= $accessLevel && $isDir) {
             $skip_dir = str_replace('..', '', $link);
             continue;
         }
         if ($skip_dir != '' && !in_array($link, $this->_allowedPaths) && strstr($link, $skip_dir)) {
             continue;
         }
         // skip unwanted files
         if ($name == '.' || $name == '..' || in_array($name, Config::get('restricted_files')) && !$isDir) {
             continue;
         }
         $skip_dir = '';
         $skip = false;
         if ($isDir == false && $skip_files == true) {
             // skip unwanted files
             $skip = true;
         } elseif ($isDir == false) {
             // this is regural file, no links here
             $link = '';
         } else {
             // this is dir
             $link = $link;
         }
         if ($objects->getDepth() == $depth) {
             //the depth hasnt changed so just add another li
             if (!$skip) {
                 $el = $dom->createElement('li');
                 $a = $dom->createElement('a', $name);
                 $at = $dom->createAttribute('clink');
                 $at->value = $link_prefix . $this->encrypt($link);
                 $el->appendChild($a);
                 $el->appendChild($at);
                 if (!$isDir) {
                     $el->appendChild($dom->createAttribute('isfile'));
                 }
                 $node->appendChild($el);
             }
         } elseif ($objects->getDepth() > $depth) {
             //the depth increased, the last li is a non-empty folder
             $li = $node->lastChild;
             $ul = $dom->createElement('ul');
             $li->appendChild($ul);
             if (!$skip) {
                 $el = $dom->createElement('li');
                 $a = $dom->createElement('a', $name);
                 $at = $dom->createAttribute('clink');
                 $at->value = $link_prefix . $this->encrypt($link);
                 $el->appendChild($a);
                 $el->appendChild($at);
                 if (!$isDir) {
                     $el->appendChild($dom->createAttribute('isfile'));
                 }
                 $ul->appendChild($el);
             }
             $node = $ul;
         } else {
             //the depth decreased, going up $difference directories
             $difference = $depth - $objects->getDepth();
             for ($i = 0; $i < $difference; $difference--) {
                 $node = $node->parentNode->parentNode;
             }
             if (!$skip) {
                 $el = $dom->createElement('li');
                 $a = $dom->createElement('a', $name);
                 $at = $dom->createAttribute('clink');
                 $at->value = $link_prefix . $this->encrypt($link);
                 $el->appendChild($a);
                 $el->appendChild($at);
                 if (!$isDir) {
                     $el->appendChild($dom->createAttribute('isfile'));
                 }
                 $node->appendChild($el);
             }
         }
         $depth = $objects->getDepth();
     }
     return $dom->saveHtml();
 }