kernel
[ class tree: kernel ] [ index: kernel ] [ all elements ]

Source for file ajax_file_browser_inc.php

Documentation is available at ajax_file_browser_inc.php

  1. <?php
  2. /**
  3.  * @version $Header$
  4.  *
  5.  *  Copyright (c) 2008 bitweaver.org
  6.  *  All Rights Reserved. See below for details and a complete list of authors.
  7.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  8.  *
  9.  * @package kernel
  10.  * @subpackage functions
  11.  */
  12.  
  13. /**
  14.  * Quick guide to this file:
  15.  * $_REQUEST['ajax_path_conf'] is the kernel configuration name that contains the absolute path to the directory where the files are.
  16.  *
  17.  * Safety first:
  18.  * This method was chosen to provide a measure of security since we never pass in an absolute path via the URL this way.
  19.  * Another safety measure is provided that the configuration value set in $gBitSystem->mConfig['$_REQUEST['ajax_path_conf']] is used as 'jail'.
  20.  * Paths outside this 'jail' will be ignored including ../../ or symbolic links.
  21.  * Evil extensions as defined in EVIL_EXTENSION_PATTERN will be ignored as are [dot] files e.g.: .private.txt
  22.  *
  23.  * e.g.:
  24.  * /home/ftp/public/ is the 'jail'
  25.  * /home/ftp/public/ftp -> /home/ftp/ is a symbolic link that points outside the 'jail' and will therefore be ignored completely.
  26.  * Also makes it impossible to import stuff like /home/ftp/public/../../../../../etc/passwd
  27.  *
  28.  * You can define ajax_path_conf in two places with different effects:
  29.  * 1. define the ajax_path_conf when you include the template:
  30.  *    {include file="bitpackage:kernel/ajax_file_browser.tpl" ajax_path_conf=treasury_file_import_path}
  31.  *    This will show a link to "Load Files" which will then load the file list when you click on the link.
  32.  * 2. If you provide $_REQUEST['ajax_path_conf'] when you include it from your php file, all files in the root directory will already be loaded.
  33.  *    $_REQUEST['ajax_path_conf'] = 'treasury_file_import_path';
  34.  *    require_once( KERNEL_PKG_PATH.'ajax_file_browser.php' );
  35.  *
  36.  * NOTE: when you process the imported files, make sure you use realpath() to check of files are really in your 'jail'.
  37.  */
  38. require_once( 'setup_inc.php' );
  39. // we need to set these global that we can include this file from functions
  40. global $gBitThemes, $gBitSystem, $gBitSmarty;
  41.  
  42. if( !empty( $_REQUEST['ajax_path_conf'] ) && $gBitSystem->isFeatureActive( $_REQUEST['ajax_path_conf'] ) ) {
  43.     $fileList = ajax_dir_list( $gBitSystem->getConfig( $_REQUEST['ajax_path_conf'] ), ( !empty( $_REQUEST['relpath'] ) ? $_REQUEST['relpath']."/" : NULL ));
  44.     $gBitSmarty->assign( 'fileList', $fileList );
  45. }
  46. $gBitThemes->loadAjax( 'mochikit', array( 'Iter.js', 'DOM.js', 'Async.js' ));
  47. $gBitThemes->loadJavascript( KERNEL_PKG_PATH."scripts/BitFileBrowser.js", TRUE );
  48.  
  49. if( $gBitThemes->isAjaxRequest() ) {
  50.     $gBitSmarty->display( 'bitpackage:kernel/ajax_file_browser_inc.tpl' );
  51. }
  52.  
  53. /**
  54.  * ajax_dir_list
  55.  *
  56.  * @param array $pDir Base directory
  57.  * @param array $pRelPath relative path on top of base directory
  58.  * @access public
  59.  * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure
  60.  */
  61. function ajax_dir_list( $pDir, $pRelPath = NULL ) {
  62.     global $gBitSystem;
  63.     $ret = $files = array();
  64.  
  65.     if( !empty( $pDir ) && is_dir( $pDir.$pRelPath )) {
  66.         if( $handle = opendir( $pDir.$pRelPath )) {
  67.             while( FALSE !== ( $file = readdir( $handle ))) {
  68.                 if( !preg_match( "#^\.#",$file ) && is_readable( $pDir.$pRelPath.$file )) {
  69.                     array_push( $files, $file );
  70.                 }
  71.             }
  72.             sort( $files );
  73.             foreach( $files as $i ) {
  74.                 $relFile = $pRelPath.$i;
  75.                 $file = realpath( $pDir.$relFile );
  76.                 if( strpos( $file, $pDir ) === 0 ) {
  77.                     $info = array(
  78.                         'name'    => $i,
  79.                         'relpath' => $relFile,
  80.                         'indent'  => ( count( explode( '/', $relFile )) * 10 ),
  81.                         'size'    => filesize( $file ),
  82.                         'mtime'   => filemtime( $file ),
  83.                     );
  84.                     if( is_dir( $file )) {
  85.                         $ret['dir'][$i] = $info;
  86.                     } elseif( !preg_match( EVIL_EXTENSION_PATTERN, $file )) {
  87.                         $ret['file'][$i] = $info;
  88.                     }
  89.                 }
  90.             }
  91.             closedir( $handle );
  92.         }
  93.     }
  94.  
  95.     if( empty( $ret )) {
  96.         $ret['file'][] = array(
  97.             'indent'  => ( count( explode( '/', $pRelPath )) * 10 ),
  98.         );
  99.     }
  100.  
  101.     return $ret;
  102. }
  103. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:33 +0000 by phpDocumentor 1.5.0-lsces