Česky
Kamil Dudka

Web components

File detail

Name:DownloadFbScanDir.class.php [Download]
Location: src > lib
Size:6.8 KB
Last modification:2007-12-11 16:08

Source code

<?php
/**
 * @file FbScanDir.class.php
 * Definition of FbScanDir class.
 * @author Kamil Dudka <xdudka00@gmail.com>
 * @ingroup FileBrowser
 */
 
/**
 * Scan directory for files and directories and sort list by desired column.
 * @ingroup FileBrowser
 */
class FbScanDir
{
  private $dirList_ = Array();
  private $fileList_ = Array();
  private $l10n_;
  /**
   * Scan directory for files and directories and sort list by desired column.
   * @param dir Directory to scan.
   * @throw ExceptionNotFound Exception is thrown when directory not exists.
   */
  public function __construct($dir) {
    // Check directory validity
    if (!is_dir($dir))
      throw new ExceptionNotFound($dir);
 
    // No localization by default (empty L10n object)
    $this->l10n_ = new L10n;
 
    // Scan directory for subdirectories and files (and sort)
    $scanList = scandir($dir);
    array_shift($scanList);     // Remove '.'
    array_shift($scanList);     // Remove '..'
 
    // Fill dirList and fileList arrays
    foreach ($scanList as $current) {
      $file = $dir.'/'.$current;
      $size = filesize($file);
      $mtime = filemtime($file);
      if (is_dir($file)) {
        $this->dirList_ []= Array(
          'name' => $current,
          'mtime' => $mtime);
      } else {
        $this->fileList_ []= Array(
          'name' => $current,
          'size' => $size,
          'mtime' => $mtime);
      }
    }
 
    // Sort lists
    $this->handleSortRequest();
    $this->sort();
  }
  /**
   * Set localization for visible output.
   * @param l10n Reference to initialized L10n object.
   */
  public function setLocale($l10n) {
    $this->l10n_ = $l10n;
  }
  /**
   * Current sort string.
   * @note Do not call this method static!
   */
  public function currentSort() {
    return $_SESSION['fbSort'];
  }
  /**
   * Build array of visible file list table headers.
   * @return Return prepared array for FileBrowser.tpl Smarty template.
   */
  public function sortLinkArray() {
    $l10n = $this->l10n_;
    $sort = Array(
      'name' => 'N',
      'size' => 'S',
      'mtime' => 'M');
    foreach ($sort as $field => $cap) {
      $bSortAsc = (bool)($this->currentSort() == $cap);
      $bSort = str_replace('-', '', $this->currentSort()) == $cap;
 
      $href = '?sort='.($bSortAsc?'-':'').$cap;
      $text = $l10n->tr($bSortAsc?'Descending':'Ascending');
      $img = $alt = null;
      if ($bSort) {
        $img = ($bSortAsc)?'/s_asc.png':'/s_desc.png';
        $alt = $l10n->tr($bSortAsc?'Ascending':'Descending');
      }
      $sort[$field] = Array(
        'href' => $href,
        'text' => $text,
        'img' => $img,
        'alt' => $alt);
    }
    return $sort;
  }
  /**
   * Handle change-sort request from user trough GET
   * and set session variable.
   */
  private function handleSortRequest() {
    session_start();
    session_register('fbSort');
    if (isset($_GET['sort'])) {
      $sortRq = strtoupper($_GET['sort']);
      switch ($sortRq) {
        case 'N':
        case 'S':
        case 'M':
        case '-N':
        case '-S':
        case '-M':
          $_SESSION['fbSort'] = $sortRq;
      }
    }
    if (!isset($_SESSION['fbSort']))
      $_SESSION['fbSort'] = 'N';
  }
  /**
   * Sort fileList and dirList arrays using currentSort()
   */
  private function sort() {
    $sort = strtoupper($this->currentSort());
    $dirSort = $fileSort = '';
    switch ($sort) {
      case '+N': case 'N':
        $dirSort = $fileSort = 'FbScanDir_cmpName';
        break;
      case '-N':
        $dirSort = $fileSort = 'FbScanDir_cmpNameD';
        break;
      case '+S': case 'S':
        $dirSort = 'FbScanDir_cmpName';
        $fileSort = 'FbScanDir_cmpSize';
        break;
      case '-S':
        $dirSort = 'FbScanDir_cmpName';
        $fileSort = 'FbScanDir_cmpSizeD';
        break;
      case '+M': case 'M':
        $dirSort = $fileSort = 'FbScanDir_cmpMtime';
        break;
      case '-M':
        $dirSort = $fileSort = 'FbScanDir_cmpMtimeD';
        break;
    }
    usort($this->dirList_, $dirSort);
    usort($this->fileList_, $fileSort);
  }
 
  /**
   * Sorted directory list.
   * Each array item is associative array of:
   * - 'name' => directory name (relative)
   * - 'mtime' => file last modification as unix timestamp
   * @return Return directory list as array.
   */
  public function dirList() {
    return $this->dirList_;
  }
 
  /**
   * Sorted file-only list.
   * Each array item is associative array of:
   * - 'name' => file name (relative)
   * - 'size' => file size in bytes
   * - 'mtime' => file last modification as unix timestamp
   * @return Return file list as array.
   */
  public function fileList() {
    return $this->fileList_;
  }
 
  /**
   * Convert file size to human readable format.
   * @param countOfBytes File size in bytes.
   * @return Return file size converted to string.
   */
  public static function sizeToString($countOfBytes) {
    if ($countOfBytes<1024)
      return sprintf('%d B', $countOfBytes);
    $size = (float)$countOfBytes;
    $size /= 1024;
    if ($size<1024)
      return sprintf('%.1f KB', $size);
    $size /= 1024;
    if ($size<1024)
      return sprintf('%.1f MB', $size);
    else
      return sprintf('%.1f GB', $size/1024);
  }
 
  /**
   * Convert file's last modification timestamp to human readable format.
   * @param timeStamp File's last modification as unix timestamp.
   * @return Return File's last modification as string.
   */
  public static function mtimeToString($timeStamp) {
    return date('Y-m-d H:i', $timeStamp);
  }
 
  /**
   * Convert file's last modification timestamp @b date to human readable format.
   * @param timeStamp File's last modification as unix timestamp.
   * @return Return File's last modification @b date as string.
   */
  public static function mtimeDateToString($timeStamp) {
    return date('Y-m-d', $timeStamp);
  }
 
  /**
   * Convert file's last modification timestamp @b time to human readable format.
   * @param timeStamp File's last modification as unix timestamp.
   * @return Return File's last modification @b time as string.
   */
  public static function mtimeTimeToString($timeStamp) {
    return date('H:i', $timeStamp);
  }
}
 
// end of FbScanDir class
function FbScanDir_intCmp($a, $b) {
  if ($a>$b)
    return 1;
  else if ($b>$a)
    return -1;
  else
    return 0;
}
function FbScanDir_cmpName($a, $b) {
  return strcmp($a['name'], $b['name']);
}
function FbScanDir_cmpNameD($a, $b) {
  return strcmp($b['name'], $a['name']);
}
function FbScanDir_cmpSize($a, $b) {
  return FbScanDir_intCmp($a['size'], $b['size']);
}
function FbScanDir_cmpSizeD($a, $b) {
  return FbScanDir_intCmp($b['size'], $a['size']);
}
function FbScanDir_cmpMtime($a, $b) {
  return FbScanDir_intCmp($a['mtime'], $b['mtime']);
}
function FbScanDir_cmpMtimeD($a, $b) {
  return FbScanDir_intCmp($b['mtime'], $a['mtime']);
}
?>