<?php
/*
Copyright (C) 2007 thomas_e (at) imgcms.de

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/

  define ('ADMIN_USER', 'admin'); 
  define ('ADMIN_PW', 'admin');  // use a safe ADMIN_PW here, because this script can change the content of your site !

  define ('VERSION_NR', '0.95');

  define ('CUR_DIR', '.');
  define ('UPPER_DIR', '..');
  define ('DS', '/');
  define ('CR', chr(13));

  define ('THIS_SCRIPT', basename($_SERVER['PHP_SELF']));
  define ('IMG_FILE', 'img.php');
  define ('ADMIN_FILE', 'admin.php');
  define ('EA_FOLDER', 'edit_area');
  define ('EA_FILE', 'edit_area_full.js');
  define ('DOWNLOAD_FOLDER', 'download');
  define ('ZIP_EXTENSION', 'zip');

  define ('STR_SUBDIR', 'subdir');

  $excludedFiles      = array(THIS_SCRIPT, IMG_FILE, ADMIN_FILE, EA_FOLDER, 'index.html', 'robots.txt', 'sitemap.xml', 'favicon.ico', 'missing.html');
  $imagesExtensions   = array('png', 'gif', 'jpg', 'jpeg');
  $editableExtensions = array('html', 'php', 'css', 'txt', 'xml', 'js', 'python');
  $useEAExtensions    = array('html', 'php', 'css', 'xml', 'js', 'python');
  $alsoShowExtensions = array('html', ZIP_EXTENSION); // use an empty array to show all

  if (isset($_REQUEST[STR_SUBDIR])
   && (strlen($_REQUEST[STR_SUBDIR]) > 0)
   && is_dir($_REQUEST[STR_SUBDIR])
   && (strpos($_REQUEST[STR_SUBDIR], UPPER_DIR) === FALSE)
   && (substr($_REQUEST[STR_SUBDIR], 0, 1) != DS)) {
    define ('SUB_DIR', $_REQUEST[STR_SUBDIR]);
  }
  else { define ('SUB_DIR', CUR_DIR); }
  
  if (SUB_DIR != CUR_DIR) { define ('HREF_BEG', '<a href="'.THIS_SCRIPT.'?'.STR_SUBDIR.'='.SUB_DIR.'&amp;'); }
  else                    { define ('HREF_BEG', '<a href="'.THIS_SCRIPT.'?'); }
  
  if (isset($_REQUEST['msg'])) { $msg = rawurldecode($_REQUEST['msg']); }
  elseif($_REQUEST['act'] || $_GET['delete'] || $_GET['rename']) { $msg = '&nbsp;'; }
  else { $msg = ''; }

  if (ADMIN_PW == 'admin') { $msg = 'please change the default admin password first !'; }
  elseif (ADMIN_PW == '') { $msg = 'you should not use an empty admin password !'; }

  $isAdmin = (isset($_SERVER['PHP_AUTH_USER']) && (strtolower($_SERVER['PHP_AUTH_USER']) == ADMIN_USER) && ($_SERVER['PHP_AUTH_PW'] == ADMIN_PW));
  // $isAdmin = true;
  if ($isAdmin) {
    if ((strlen(ADMIN_FILE) > 0) && (THIS_SCRIPT != ADMIN_FILE) && (ADMIN_PW != '') && (ADMIN_PW != 'admin') && !@file_exists(ADMIN_FILE)) {
      @copy(THIS_SCRIPT, ADMIN_FILE);
    }
  }
  elseif (isset($_GET['act']) && ($_GET['act'] == 'login')) {
    Header('WWW-Authenticate: Basic realm="cool people"');
    Header('HTTP/1.0 401 Unauthorized');
    $msg = 'login failed :-(';
  }

  if (isset($_FILES['newValue']) && $isAdmin) {
    $pathAndFile = SUB_DIR.DS.$_FILES['newValue']['name'];
    if (strlen($_FILES['newValue']['name']) > 0) {
      if (@move_uploaded_file($_FILES['newValue']['tmp_name'], $pathAndFile)) {
        $msg = '<span class="entry">'.$_FILES['newValue']['name'].'</span> uploaded :-)';
      } else { $msg = 'upload of <span class="entry">'.$_FILES['newValue']['name'].'</span> failed :-('; }
    } else { $msg = 'first choose a file for upload'; }
    reLocate('upload');
  }
  elseif (isset($_GET['download']) && ($isAdmin || (SUB_DIR == DOWNLOAD_FOLDER))) {
    $pathAndFile = SUB_DIR.DS.$_GET['download'];
    if (@file_exists($pathAndFile)) {
      clearstatcache();
      header('Content-Type: application/force-download');
      header('Content-Transfer-Encoding: binary');
      header('Content-Length: '.filesize($pathAndFile));
      header('Content-Disposition: attachment; filename="'.$_GET['download'].'"');
      @readfile($pathAndFile);
      @flush();
      exit;
    }
  }
  elseif (isset($_GET['zip']) && ($isAdmin)) {
    $pathAndFile = SUB_DIR.DS.$_GET['zip'];
    if (@file_exists($pathAndFile)) {
      if(@is_dir($pathAndFile)) {
        $zipFile = $_GET['zip'].'.'.ZIP_EXTENSION;
        if(SUB_DIR != CUR_DIR) { $currentWorkDir = getcwd(); @chdir(SUB_DIR); }
        $result = (@exec('zip -r '.$zipFile.' '.$_GET['zip'], $zipResult) !== FALSE);
        if(SUB_DIR != CUR_DIR) { @chdir($currentWorkDir); }
        if ($result) {
          if (@file_exists(SUB_DIR.DS.$zipFile)) {
            $msg = 'folder <span class="entry">'.$_GET['zip'].'</span> zipped to <span class="entry">'.$zipFile.'</span>';
          } else { $msg = 'zip of folder <span class="entry">'.$_GET['zip'].'</span> failed :-('; }
        } else { $msg = 'execution of the zip program failed :-('; }
      }
    }
    reLocate('');
  }
  elseif (isset($_GET['unzip']) && ($isAdmin)) {
    $pathAndFile = SUB_DIR.DS.$_GET['unzip'];
    if (@file_exists($pathAndFile)) {
      if(@is_file($pathAndFile) || @is_link($pathAndFile)) {
        if (@exec('unzip -d '.SUB_DIR.' '.$pathAndFile, $unzipResult) !== FALSE) {
          $msg = 'unzip of <span class="entry">'.$_GET['unzip'].'</span> succeeded :-)';
        } else { $msg = 'unzip of <span class="entry">'.$_GET['unzip'].'</span> failed :-('; }
      }
    }
    reLocate('');
  }
  elseif (isset($_POST['newFolder']) && $isAdmin) {
    $pathAndFile = SUB_DIR.DS.$_POST['newValue'];
    if(strlen ($_POST['newValue']) > 0) {
      if (!@file_exists($pathAndFile)) {
        if(@mkdir($pathAndFile)) {
          $msg = 'new folder <span class="entry">'.$_POST['newValue'].'</span> created :-)';
        } else { $msg = 'creation of <span class="entry">'.$_POST['newValue'].'</span> failed :-('; }
      } else { $msg = 'folder <span class="entry">'.$_POST['newValue'].'</span> already exists :-('; }
    } else { $msg = 'ok, nothing changed ;-)'; }
    reLocate('mkdir');
  }
  elseif (isset($_POST['saveFile']) && $isAdmin) {
    if(strlen ($_POST['saveFile']) > 0) {
      $content = $_POST['newValue'];
      if (get_magic_quotes_gpc() ==1) { $content = stripslashes($content); }
      $pathAndFile = SUB_DIR.DS.$_POST['saveFile'];
      if (@file_put_contents ($pathAndFile, $content) !== FALSE) {
        $msg = '<span class="entry">'.$_POST['saveFile'].'</span> saved :-)';
      } else { $msg = '<span class="entry">'.$_POST['saveFile'].'</span> not saved :-('; }
    } else { $msg = 'ok, nothing changed ;-)'; }
    reLocate('');
  }
  elseif (isset($_POST['renameFrom']) && $isAdmin) {
    $pathAndFileFrom = SUB_DIR.DS.$_POST['renameFrom'];
    $pathAndFileTo = SUB_DIR.DS.$_POST['newValue'];
    if(strcmp ($_POST['renameFrom'], $_POST['newValue']) != 0) {
      if (!@file_exists($pathAndFileTo)) {
        if(@rename($pathAndFileFrom, $pathAndFileTo)) {
          $msg = 'ok, <span class="entry">'.$_POST['renameFrom'].'</span> renamed to <span class="entry">'.$_POST['newValue'].'</span> :-)';
        } else { $msg = 'rename of <span class="entry">'.$_POST['renameFrom'].'</span> to <span class="entry">'.$_POST['newValue'].'</span> failed :-('; }
      } else { $msg = '<span class="entry">'.$_POST['newValue'].'</span> already exists :-('; }
    } else { $msg = 'ok, nothing changed ;-)'; }
    reLocate('');
  }
  elseif (isset($_POST['deleteFile']) && $isAdmin) {
    $pathAndFile = SUB_DIR.DS.$_POST['deleteFile'];
    if (@file_exists ($pathAndFile)) {
      if (@is_file($pathAndFile) || is_link($pathAndFile)) {
        if (@unlink($pathAndFile)) {
          $msg = 'file <span class="entry">'.$_POST['deleteFile'].'</span> deleted :-)';
        } else { $msg = 'delete of file <span class="entry">'.$_POST['deleteFile'].'</span> failed :-('; }
      }
      elseif (@is_dir($pathAndFile)) {
        if (rec_rmdir($pathAndFile)) {
          $msg = 'folder <span class="entry">'.$_POST['deleteFile'].'</span> removed :-)';
        } else { $msg = 'remove of folder <span class="entry">'.$_POST['deleteFile'].'</span> failed :-('; }
      }
    } else { $msg = '<span class="entry">'.$_POST['deleteFile'].'</span> not found :-('; }
    reLocate('');
  }

  $imageCount = 0;
  $files = array();
  if ($dh = @opendir(SUB_DIR)) {
    while (($file = @readdir($dh)) !== FALSE) {
      if (($file != CUR_DIR) && ($file != UPPER_DIR) && ($isAdmin || (SUB_DIR == DOWNLOAD_FOLDER) || !in_Array($file, $excludedFiles))) {
        if (@is_file(SUB_DIR.DS.$file) || @is_link(SUB_DIR.DS.$file)) {
          if (in_array(extension($file), $imagesExtensions)) {
            array_push($files, $file);
            $imageCount++;
          }
          elseif (($isAdmin) || (count($alsoShowExtensions) == 0) || in_array(extension($file), $alsoShowExtensions) || (SUB_DIR == DOWNLOAD_FOLDER)) {
            array_push($files, $file);
          }
        }
        elseif (is_dir(SUB_DIR.DS.$file)) {
          array_push($files, $file);
        }
      }
    }
    @closedir($dh);
    sort($files);
  }

  page_header();

  if (isset($_GET['rename']) && $isAdmin) { page_renameForm(); }
  elseif (isset($_GET['delete']) && $isAdmin) { page_deleteForm(); }
  elseif (isset($_GET['act']) && ($_GET['act'] == 'mkdir') && $isAdmin) { page_mkdirForm(); }
  elseif (isset($_REQUEST['act']) && ($_REQUEST['act'] == 'upload') && $isAdmin) { page_uploadForm(); }

  if (isset($_GET['edit']) && $isAdmin) { page_editForm(); }
  elseif (isset($_GET['showImage'])) { page_showImage(); }
  else {
    page_msg();
    echo '<div><table><tr><td>';
      echo '<table width="100%"><tr><td>';
        page_breadCrumb();
      echo '</td><td class="td_right">';
        page_actionHead();
      echo '</td></tr></table>';
      echo '</td></tr><tr><td>';
      page_fileList();
      echo '</td></tr></table></div>';
  }

  page_footer();

  function page_header() {
    $h  = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'.CR;
    $h .= '<html><head>'.CR;
    $h .= '<title> cms.php presented by thomas_e(at)imgcms.de :-) </title>'.CR;
    $h .= '<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">'.CR;
    // $h .= '<meta name="language" content="de">'.CR;
    $h .= '<meta name="description" content="img+cms=imgcms - das zwei-dateien-(bilder!)-content-management-system von thomas_e(at)imgcms.de :-)">'.CR;
    $h .= '<meta name="author" content="thomas_e =:-)">'.CR;
    $h .= '<meta name="robots" content="follow">'.CR;
    $h .= '<meta name="keywords" content="imgcms.de, imgcms, image, img, cms, content managment system, thomas_e, php, slideShow, free, software';
    $h .= ', download, sourcecode, opensource">'.CR;
    $h .= '<style type="text/css">'.CR;
    $h .= 'body { color:navy; margin:20px; }'.CR;
    $h .= 'a { color:maroon; text-decoration:none; font-weight:normal; }'.CR;
    $h .= 'a:hover { color:red; }'.CR;
    $h .= '#msg_div { color:teal; }'.CR;
    $h .= 'div { text-align:center; }'.CR;
    $h .= 'div.div_left { text-align:left; }'.CR;
    $h .= 'div.div_full { text-align:left;margin:0px;width:100%;height:100%; }'.CR;
    $h .= 'div.footer { color:gray; font-size:x-small; }'.CR;
    $h .= 'table { margin-left:auto; margin-right:auto; }'.CR;
    $h .= 'td { padding-left:10px; padding-right:10px; padding-top:5px; white-space:nowrap; }'.CR;
    $h .= 'td.head { text-align:center; border-bottom-color:navy; border-bottom-style:solid; border-bottom-width:1px; }'.CR;
    $h .= 'td.td_right { text-align:right; }'.CR;
    $h .= 'td.td_center { text-align:center; }'.CR;
    $h .= 'td.td_placeHolder { text-align:center; color:silver; }'.CR;
    $h .= 'span.entry { color:blue; }'.CR;
    $h .= '</style>'.CR;
    $h .= '<script type="text/javascript">'.CR;
    $h .= 'function mkdirCheck() {'.CR;
      $h .= 'if (document.myForm.newValue.value.length > 0) {'.CR;
        $h .= 'document.getElementById("msg_div").innerHTML = "creating new folder...";'.CR;
        $h .= 'return true; }'.CR;
      $h .= 'else {'.CR;
        $h .= 'document.getElementById("msg_div").innerHTML = "first type in a name for the new folder";'.CR;
        $h .= 'return false;}'.CR;
    $h .= '}'.CR;
    $h .= 'function cancelClick() { window.location.href="'.THIS_SCRIPT;
    if (SUB_DIR != CUR_DIR) { $h .= '?'.STR_SUBDIR.'='.SUB_DIR; }
    $h .= '" }'.CR;
    $h .= 'function myLoad() {';
      $h .= 'if(document.myForm && document.myForm.newValue) { document.myForm.newValue.focus(); } ';
    $h .= '}';
    $h .= '</script>'.CR;
    $h .= '</head>'.CR;
    $h .= '<body onLoad="javascript:myLoad();">'.CR;

    echo $h;
  }

  function page_footer() {
      $f = '<br><div class="footer">';
      $f .= ''.CR; // add your footer here :-)
      $f .= '</div>'.CR;
      $f .= '<noscript><div>'.CR;
        $f .= 'sorry, aber dieses script ben&ouml;tigt javascript. <br><br>'.CR;
        $f .= 'weitere infos gibts unter <br><br>'.CR;
        $f .= '<a href="http://imgcms.de">http://imgcms.de</a><br>'.CR;
      $f .= '</div></noscript>';
    $f .= '</body></html>'.CR;
    echo $f;
  }

  function page_msg() {
    global $msg;
    if (strlen($msg) == 0) { $msg = 'this is '.THIS_SCRIPT.' version '.VERSION_NR.' running on '.$_SERVER['HTTP_HOST'].CR; }
    echo '<div id="msg_div">&nbsp;'.$msg.'&nbsp;</div>'.CR;
  }

  function page_deleteForm() {
    $pathAndFile = SUB_DIR.DS.$_GET['delete'];
    $d = '<div><form action="'.THIS_SCRIPT.'" method="post" enctype="multipart/form-data" name="myForm">'.CR;
      $d .= '<input name="'.STR_SUBDIR.'" type="hidden" value="'.SUB_DIR.'">'.CR;
      $d .= '<input name="deleteFile" type="hidden" value="'.$_GET['delete'].'">'.CR;
      if (is_file($pathAndFile) || is_link($pathAndFile)) {
        $d .= 'realy delete file <span class="entry">'.$_GET['delete'].'</span>  ?';
      }
      else {
        $d .= 'realy remove folder <span class="entry">'.$_GET['delete'].'</span> and all of its content ?';
      }
      $d .= '&nbsp;'.CR;
      $d .= '<input type="submit" value="delete">&nbsp;'.CR;
      $d .= '<input type="reset" name="cancelBtn" value="cancel" onClick="cancelClick();">'.CR;
    $d .= '</form></div>'.CR;
    $d .= '<script type="text/javascript">'.CR;
      $d .= 'function triggerCancel() {if(event.keyCode==27){document.myForm.cancelBtn.click();}}'.CR;
      $d .= 'document.onkeypress = triggerCancel;'.CR;
    $d .= '</script>'.CR;
    echo $d;
  }

  function page_renameForm() {
    $r = '<div><form action="'.THIS_SCRIPT.'" method="post" enctype="multipart/form-data" name="myForm">'.CR;
      $r .= '<input name="'.STR_SUBDIR.'" type="hidden" value="'.SUB_DIR.'">'.CR;
      $r .= '<input name="renameFrom" type="hidden" value="'.$_GET['rename'].'">'.CR;
      $r .= 'rename&nbsp;&gt;&gt;&nbsp;<input name="newValue" type="text" size="20" value="'.$_GET['rename'].'"> '.CR;
      $r .= '<input type="submit" value="rename">&nbsp;'.CR;
      $r .= '<input type="reset" value="cancel" onClick="cancelClick();">'.CR;
    $r .= '</form></div>'.CR;
    echo $r;
  }

  function page_mkdirForm() {
    $m = '<div><form action="'.THIS_SCRIPT.'" method="post" enctype="multipart/form-data" name="myForm">'.CR;
      $m .= '<input name="'.STR_SUBDIR.'" type="hidden" value="'.SUB_DIR.'">'.CR;
      $m .= '<input name="newFolder" type="hidden">'.CR;
      $m .= 'new folder&nbsp;&gt;&gt;&nbsp;'.CR;
      $m .= '<input name="newValue" type="text" size="20" value="">'.CR;
      $m .= '<input type="submit" value="create" onClick="return mkdirCheck();">&nbsp;'.CR;
      $m .= '<input type="reset" value="cancel" onClick="cancelClick();">'.CR;
    $m .= '</form></div>'.CR;
    echo $m;
  }

  function page_uploadForm() {
    $u = '<div><form name="myForm" action="'.THIS_SCRIPT.'" method="post" enctype="multipart/form-data">'.CR;
      $u .= '<input name="'.STR_SUBDIR.'" type="hidden" value="'.SUB_DIR.'">'.CR;
      $u .= '<input type="hidden" name="MAX_FILE_SIZE" value="10000000">'.CR;
      $u .= 'select file for upload&nbsp;&gt;&gt;&nbsp;&nbsp;'.CR;
      $u .= '<input name="newValue" type="file">'.CR;
      $u .= '&nbsp;<input type="reset" name="cancelBtn" value="cancel" onClick="cancelClick();">'.CR;
      $u .= '<input type="submit" name="submitBtn" value="upload" style="display:none;" onClick="return uploadCheck();">';
    $u .= '</form></div>'.CR;

    $u .= '<script type="text/javascript">'.CR;

      $u .= 'function file_name(str) { str=str.replace(/\\\\/g, "/"); return str.substring(str.lastIndexOf("/") + 1, str.length) }'.CR;

      $u .= 'function uploadCheck() {'.CR;
        $u .= 'if (document.myForm.newValue.value.length > 0) {'.CR;
          $u .= 'document.getElementById("msg_div").innerHTML = "uploading <span class='."'entry'".'>" + file_name(document.myForm.newValue.value) + "</span> ...";'.CR;
          $u .= 'return true;'.CR;
        $u .= '}'.CR;
        $u .= 'else {return false;}'.CR;
      $u .= '}'.CR;

      $u .= 'function doUpload() {'.CR;
        $u .= 'document.myForm.newValue.blur();'.CR;
        $u .= 'if (document.myForm.newValue.value.length > 0) {'.CR;
          $u .= 'window.clearInterval (myInterval);'.CR;
          $u .= 'document.myForm.submitBtn.click();'.CR;
        $u .= '}'.CR;
      $u .= '}'.CR;

      $u .= 'function triggerCancel() {if(event.keyCode==27){document.myForm.cancelBtn.click();}}'.CR;

      $u .= 'document.onkeypress = triggerCancel;'.CR;
      $u .= 'myInterval = window.setInterval("doUpload()", 888);'.CR;

    $u .= '</script>'.CR;
    echo $u;
  }

  function page_editForm() {
    global $useEAExtensions;
    define ('TAB', chr(9));
    define ('LF', chr(10));
    $transContent = array (TAB   => '  ', CR.LF => LF);
    $pathAndFile = SUB_DIR.DS.$_GET['edit'];
    $content = strtr (@file_get_contents($pathAndFile), $transContent);
    $rows = explode(LF, $content);
    $cols = 80; foreach ($rows as $row) { $i = strlen($row); if ($i > $cols) { $cols = $i; } }
    $useEA = (@file_exists(EA_FOLDER.DS.EA_FILE) && in_array(extension($_GET['edit']), $useEAExtensions));
    $m = '<div class="div_left"><form action="'.THIS_SCRIPT.'" method="post" enctype="multipart/form-data" name="myForm">'.CR;
      $m .= '<input name="'.STR_SUBDIR.'" type="hidden" value="'.SUB_DIR.'">'.CR;
      $m .= '<input name="saveFile" type="hidden" value="'.$_GET['edit'].'">'.CR;
      $m .= '&nbsp;<input type="submit" value="save">'.CR;
      $m .= '&nbsp;<input type="reset" value="cancel" onClick="javascript:cancelClick();">'.CR;
      if ($useEA) { $m .= '&nbsp;<input type="button" value="editarea on/off" onClick="javascript:editAreaLoader.toggle('."'ta'".');blur();">'.CR; }
      $m .= ' &nbsp; file: <span class="entry">'.$_GET['edit'].'</span>';
      // $m .= ' rows: <span class="entry">'.count($rows).'</span>';
      // $m .= ' cols: <span class="entry">'.$cols.'</span>'.CR;
      $m .= '<br><br>';
        $m .= '<textarea id="ta" name="newValue" rows="'.(count($rows)+3).'" cols="'.($cols+5).'" wrap="off">'.CR;
          $m .= htmlspecialchars($content);
        $m .= '</textarea>'.CR;
    $m .= '</form></div>'.CR;
    if ($useEA) {
      $m .= '<script type="text/javascript" src="'.EA_FOLDER.DS.EA_FILE.'"></script>'.CR;
      $m .= '<script type="text/javascript">'.CR;
        $m .= 'editAreaLoader.init({'.CR;
          $m .= 'id : "ta"'.CR;
          $m .= ',syntax: "'.extension($_GET['edit']).'"'.CR;
          $m .= ',start_highlight: true'.CR;
          $m .= ',display: "onload"'.CR; // or "later"
          $m .= ',allow_toggle: false'.CR;
          $m .= ',toolbar: ""'.CR;
          $m .= ',replace_tab_by_spaces: 2'.CR;
        $m .= '});'.CR;

      $m .= '</script>'.CR;
    }
    echo $m;
  }

  function page_showImage() {
    $pathAndFile = SUB_DIR.DS.$_GET['showImage'];
    $s = '<div><br>';
      $s .= '<a href="'.$THIS_SCRIPT;
      if (SUB_DIR != CUR_DIR) { $s .= '?subdir='.SUB_DIR; }
      $s .='"><img src="'.$pathAndFile.'"></a>';
    $s .= '</div>';
    echo $s;
  }

  function page_breadCrumb() {
    $n = '';
    $n .= '<a href="'.THIS_SCRIPT.'" title="back to the root directory" onClick="blur();">root</a>'.CR;
    if (SUB_DIR != CUR_DIR) {
      $subdirArray = explode (DS, SUB_DIR);
      $path = '';
      $i = 0;
      while ($i < count($subdirArray)-1) {
        if (strlen($path) > 0) { $path .= DS; }
        $path .= $subdirArray[$i];
        $n .= ' / <a href="'.THIS_SCRIPT.'?'.STR_SUBDIR.'='.$path.'" title="go to directory '.$subdirArray[$i].'" onClick="blur();">'.$subdirArray[$i].'</a>'.CR;
        $i++;
      }
      $n .= ' / '.$subdirArray[$i]; // das aktuelle verzeichnis ohne link
    }
    $n .= '&nbsp;&nbsp;'.CR;
    echo $n;
  }

  function page_actionHead() {
    global $imageCount, $isAdmin;
    $a = '&nbsp;&nbsp;';
    if (($imageCount > 0) && @file_exists(IMG_FILE)) {
      $a .= '<a href="'.IMG_FILE;
      if (SUB_DIR != CUR_DIR) { $a.= '?subdir='.SUB_DIR; }
      $a .= '" title="start the slideShow with the images in the current folder" onClick="blur();">slide</a>'.CR;
      $a .= '&nbsp;|&nbsp;';
    }
    if (!$isAdmin) {
      $a .= HREF_BEG.'act=login" title="for the cool people only ;-)" onClick="blur();">login</a>'.CR;
    }
    else {
      $a .= HREF_BEG.'act=mkdir" title="create a new folder" onClick="blur();">mkdir</a>'.CR;
      $a .= '&nbsp;|&nbsp;';
      $a .= HREF_BEG.'act=upload" title="upload files" onClick="blur();">upload</a>'.CR;
    }
    echo $a;
  }

  function page_fileList() {
    global $isAdmin, $files, $imagesExtensions, $editableExtensions;
    if (count ($files) > 0) {
      $r = '<table width="100%"><tr>'.CR;
      $r .= '<td class="head">name</td>';
      $r .= '<td class="head">size</td>';
      $r .= '<td class="head">date</td>';
      if ($isAdmin) { $r .= '<td class="head">action</td>'; }
      $r .= '</tr>'.CR;
      foreach ($files as $file) {
        $fileExtension = extension($file);
        $r .= '<tr>';
          if(SUB_DIR == CUR_DIR) { $pathAndFile = $file; }
          else { $pathAndFile = SUB_DIR.DS.$file; }
          if (@is_file($pathAndFile) || @is_link($pathAndFile)) {
            if ((SUB_DIR == DOWNLOAD_FOLDER) || ($fileExtension == ZIP_EXTENSION)) {
              $r .= '<td>'.HREF_BEG.'download='.$file.'" title="download '.$file.'" onClick="blur();">'.$file.'</a></td>'.CR;
            }
            elseif (in_array($fileExtension, $imagesExtensions)) {
              if (@file_exists(IMG_FILE)) {
                $r .= '<td><a href="'.IMG_FILE;
                if (SUB_DIR != CUR_DIR) { $r.= '?subdir='.SUB_DIR; $sep = '&amp;'; } else { $sep = '?'; }
                $r .= $sep.'image='.$file.'" title="show image '.$file.'" onClick="blur();">'.$file.'</a></td>'.CR;
              }
              else {
                $r .= '<td>'.HREF_BEG.'showImage='.$file.'" title="show Image '.$file.'" onClick="blur();">'.$file.'</a></td>'.CR;
              }
            }
            else {
              $r .= '<td><a href="'.$pathAndFile.'" title="open '.$file.' in the browser" onClick="blur();">'.$file.'</a></td>'.CR;
            }
            $r .= '<td class="td_right">'.size2str(@filesize($pathAndFile)).'</td>'.CR;
          }
          elseif (is_dir($pathAndFile)) {
            $r .= '<td><a href="'.THIS_SCRIPT.'?'.STR_SUBDIR.'='.$pathAndFile.'" title="change directory to '.$file.'" onClick="blur();">'.$file.'</a></td>'.CR;
            $r .= '<td class="td_placeHolder">[dir]</td>'.CR;
          }
          $r .= '<td>'.date ('Y-m-d', @filemtime($pathAndFile)).'</td>'.CR;
          if ($isAdmin) {
            $r .= '<td>';
            $r .= ''.HREF_BEG.'delete='.$file.'" title="delete '.$file.'" onClick="blur();">del</a>'.CR;
            $r .= '&nbsp;|&nbsp;';
            $r .= ''.HREF_BEG.'rename='.$file.'" title="rename '.$file.'" onClick="blur();">ren</a>'.CR;
            if (@is_file($pathAndFile) || @is_link($pathAndFile)) {
              if ($fileExtension != ZIP_EXTENSION) {
                $r .= '&nbsp;|&nbsp;';
                $r .= ''.HREF_BEG.'download='.$file.'" title="download '.$file.'" onClick="blur();">dnl</a>'.CR;
              }
              if (in_array($fileExtension, $editableExtensions)) {
                $r .= '&nbsp;|&nbsp;';
                $r .= HREF_BEG.'edit='.$file.'" title="edit '.$file.'" onClick="blur();">edt</a>'.CR;
              }
              elseif ($fileExtension == ZIP_EXTENSION) {
                $r .= '&nbsp;|&nbsp;';
                $r .= HREF_BEG.'unzip='.$file.'" title="unzip '.$file.'" onClick="blur();">unz</a>'.CR;
              }
            }
            elseif (@is_dir($pathAndFile)) {
              $r .= '&nbsp;|&nbsp;';
              $r .= HREF_BEG.'zip='.$file.'" title="zip '.$file.'" onClick="blur();">zip</a>'.CR;
            }
            $r .= '</td>';
          }
        $r .= '</tr>'.CR;
      }
      $r .= '</table>'.CR;
      echo $r;
    }
  }

  function size2str($size){
    $i=0; $iec = array("", "k", "m", "g", "t", "p", "e", "z", "y");
    while (($size/1024)>1) { $size=$size/1024; $i++; }
    return rtrim(substr(number_format(round($size, 2), 2, '.', ''), 0, 4), '.').'&nbsp;'.$iec[$i].'b';
  }

  function extension($fileName) { return strtolower(substr(($t=strrchr($fileName,'.'))!==FALSE?$t:'',1)); }

  function reLocate($action) {
    global $msg;
    $l = 'Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    if (SUB_DIR != CUR_DIR) { $l .= '?'.STR_SUBDIR.'='.SUB_DIR; $sep = '&'; } else { $sep = '?'; }
    if (strlen($action) > 0) { $l .= $sep.'act='.$action; $sep = '&'; }
    if (strlen($msg) > 0) { $l .= $sep.'msg='.rawurlencode($msg); }
    header($l);
    exit;
  }

  function rec_rmdir ($path) {
    if ($ok = ($dh = @opendir ($path))) {
      while ($ok && (($file = @readdir($dh)) !== false)) {
        if ($file != CUR_DIR && $file != UPPER_DIR) {
          $pathAndFile = $path.DS.$file;
          if (@is_file ($pathAndFile) || @is_link ($pathAndFile)) { $ok = $ok && @unlink ($pathAndFile); }
          elseif (@is_dir ($pathAndFile)) { $ok = $ok && rec_rmdir ($pathAndFile); }
        }
      }
      @closedir ($path);
    }
    return $ok && @rmdir ($path);
  }
?>
