[ Index ]
 

Code source de b2evolution 2.1.0-beta

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/blogs/inc/files/ -> files.ctrl.php (source)

   1  <?php
   2  /**

   3   * This file implements the UI controller for file management.

   4   *

   5   * NOTE: $fm_mode is used for modes. Modes stay visible when browsing to a different location.

   6   * Examples of modes: link item, copy file.

   7   * Actions disappear if you browse to a different location.

   8   * Examples of actions: file properties, file edit.

   9   *

  10   * fp>> Movr/copy should not be a mode (too geeky). All we need is a dir selection tree inside of upload and move.

  11   *

  12   * This file is part of the evoCore framework - {@link http://evocore.net/}

  13   * See also {@link http://sourceforge.net/projects/evocms/}.

  14   *

  15   * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/}

  16   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.

  17   * Parts of this file are copyright (c)2005-2006 by PROGIDISTRI - {@link http://progidistri.com/}.

  18   *

  19   * {@internal License choice

  20   * - If you have received this file as part of a package, please find the license.txt file in

  21   *   the same folder or the closest folder above for complete license terms.

  22   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  23   *   then you must choose one of the following licenses before using the file:

  24   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  25   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  26   * }}

  27   *

  28   * {@internal Open Source relicensing agreement:

  29   * Daniel HAHLER grants Francois PLANQUE the right to license

  30   * Daniel HAHLER's contributions to this file and the b2evolution project

  31   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  32   *

  33   * PROGIDISTRI S.A.S. grants Francois PLANQUE the right to license

  34   * PROGIDISTRI S.A.S.'s contributions to this file and the b2evolution project

  35   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  36   * }}

  37   *

  38   * @package admin

  39   *

  40   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  41   * @author blueyed: Daniel HAHLER.

  42   * @author fplanque: Francois PLANQUE.

  43   *

  44   * @version $Id: files.ctrl.php,v 1.5 2007/11/01 01:41:00 fplanque Exp $

  45   */
  46  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  47  
  48  /**

  49   * Filelist

  50   * fp>> TODO: When the user is viewing details for a file he should (by default) not be presented with the filelist in addition to the file properties

  51   * In cases like that, we should try to avoid instanciating a Filelist.

  52   */
  53  load_class('files/model/_filelist.class.php');
  54  
  55  /**

  56   * @var User

  57   */
  58  global $current_User;
  59  
  60  // Check global access permissions:

  61  if( ! $Settings->get( 'fm_enabled' ) )
  62  {
  63      bad_request_die( 'The filemanager is disabled.' );
  64  }
  65  
  66  // Check permission:

  67  $current_User->check_perm( 'files', 'view', true );
  68  
  69  $AdminUI->set_path( 'files', 'browse' );
  70  
  71  
  72  // INIT params:

  73  if( param( 'root_and_path', 'string', '', false ) /* not memorized (default) */ && strpos( $root_and_path, '::' ) )
  74  { // root and path together: decode and override (used by "radio-click-dirtree")
  75      list( $root, $path ) = explode( '::', $root_and_path, 2 );
  76      // Memorize new root:

  77      memorize_param( 'root', 'string', NULL );
  78      memorize_param( 'path', 'string', NULL );
  79  }
  80  else
  81  {
  82      param( 'root', 'string', NULL, true ); // the root directory from the dropdown box (user_X or blog_X; X is ID - 'user' for current user (default))

  83      param( 'path', 'string', '/', true );  // the path relative to the root dir

  84      if( param( 'new_root', 'string', '' )
  85          && $new_root != $root )
  86      { // We have changed root in the select list
  87          $root = $new_root;
  88          $path = '';
  89      }
  90  }
  91  
  92  
  93  /**

  94   * @global string The file manager mode we're in ('fm_upload', 'fm_move')

  95   */
  96  $fm_mode = param( 'fm_mode', 'string', NULL, true );
  97  
  98  $action = param_action();
  99  
 100  if( !empty($action) && $fm_mode != 'link_item' )
 101  {    // The only mode which can tolerate simultaneous actions at this time is link_item
 102      // file_move & file_copy shouldn't actually be modes

 103      $fm_mode = '';
 104  }
 105  
 106  // Get root:

 107  $ads_list_path = false; // false by default, gets set if we have a valid root

 108  /**

 109   * @var FileRoot

 110   */
 111  $fm_FileRoot = NULL;
 112  
 113  $FileRootCache = & get_Cache( 'FileRootCache' );
 114  
 115  $available_Roots = $FileRootCache->get_available_FileRoots();
 116  
 117  if( ! empty($root) )
 118  { // We have requested a root folder by string:
 119      $fm_FileRoot = & $FileRootCache->get_by_ID($root, true);
 120  
 121      if( ! $fm_FileRoot || ! isset( $available_Roots[$fm_FileRoot->ID] ) )
 122      { // Root not found or not in list of available ones
 123          $Messages->add( T_('You don\'t have access to the requested root directory.'), 'error' );
 124          $fm_FileRoot = false;
 125      }
 126  }
 127  
 128  if( ! $fm_FileRoot )
 129  { // No root requested (or the requested is invalid), get the first one available:
 130      if( $available_Roots
 131          && ( $tmp_keys = array_keys( $available_Roots ) )
 132          && $first_Root = & $available_Roots[ $tmp_keys[0] ] )
 133      { // get the first one
 134          $fm_FileRoot = & $first_Root;
 135      }
 136      else
 137      {
 138          $Messages->add( T_('You don\'t have access to any root directory.'), 'error' );
 139      }
 140  }
 141  
 142  if( $fm_FileRoot )
 143  { // We have access to a file root:
 144      if( empty($fm_FileRoot->ads_path) )
 145      {    // Not sure it's possible to get this far, but just in case...
 146          $Messages->add( sprintf( T_('The root directory &laquo;%s&raquo; does not exist.'), $fm_FileRoot->ads_path ), 'error' );
 147      }
 148      else
 149      { // Root exists
 150          // Let's get into requested list dir...

 151          $non_canonical_list_path = $fm_FileRoot->ads_path.$path;
 152  
 153          // Dereference any /../ just to make sure, and CHECK if directory exists:

 154          $ads_list_path = get_canonical_path( $non_canonical_list_path );
 155  
 156          if( !is_dir( $ads_list_path ) )
 157          { // This should never happen, but just in case the diretory does not exist:
 158              $Messages->add( sprintf( T_('The directory &laquo;%s&raquo; does not exist.'), $path ), 'error' );
 159              $path = '';        // fp> added

 160              $ads_list_path = NULL;
 161          }
 162          elseif( ! preg_match( '#^'.preg_quote($fm_FileRoot->ads_path, '#').'#', $ads_list_path ) )
 163          { // cwd is OUTSIDE OF root!
 164              $Messages->add( T_( 'You are not allowed to go outside your root directory!' ), 'error' );
 165              $path = '';        // fp> added

 166              $ads_list_path = $fm_FileRoot->ads_path;
 167          }
 168          elseif( $ads_list_path != $non_canonical_list_path )
 169          {    // We have reduced the absolute path, we should also reduce the relative $path (used in urls params)
 170              $path = get_canonical_path( $path );
 171          }
 172      }
 173  }
 174  
 175  
 176  if( empty($ads_list_path) )
 177  { // We have no Root / list path, there was an error. Unset any action or mode.
 178      $action = 'nil';
 179      $fm_mode = NULL;
 180  
 181      $AdminUI->disp_html_head();
 182      // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)

 183      $AdminUI->disp_body_top();
 184      $AdminUI->disp_global_footer();
 185      exit();
 186  }
 187  
 188  
 189  $Debuglog->add( 'FM root: '.var_export( $fm_FileRoot, true ), 'files' );
 190  $Debuglog->add( 'FM _ads_list_path: '.var_export( $ads_list_path, true ), 'files' );
 191  $Debuglog->add( 'path: '.var_export( $path, true ), 'files' );
 192  
 193  
 194  /**

 195   * A list of filepaths which are selected in the FM list.

 196   *

 197   * @todo fp> This could probably be further simpplified by using "fm_sources" for selections.

 198   * Note: fm_sources is better because it also handles sources/selections on a different fileroot

 199   *

 200   * @global array

 201   */
 202  $fm_selected = param( 'fm_selected', 'array', array(), true );
 203  $Debuglog->add( count($fm_selected).' selected files/directories', 'files' );
 204  /**

 205   * The selected files (must be within current fileroot)

 206   *

 207   * @global Filelist

 208   */
 209  $selected_Filelist = & new Filelist( $fm_FileRoot, false );
 210  foreach( $fm_selected as $l_source_path )
 211  {
 212      // echo '<br>'.$l_source_path;

 213      $selected_Filelist->add_by_subpath( urldecode($l_source_path), true );
 214  }
 215  
 216  
 217  
 218  /*

 219   * Load editable objects:

 220   */
 221  if( param( 'link_ID', 'integer', NULL, false, false, false ) )
 222  {
 223      $LinkCache = & get_Cache( 'LinkCache' );
 224      if( ($edited_Link = & $LinkCache->get_by_ID( $link_ID, false )) === false )
 225      {    // We could not find the link to edit:
 226          $Messages->add( T_('Requested link does not exist any longer.'), 'error' );
 227          unset( $edited_Link );
 228          forget_param( 'link_ID' );
 229          unset( $link_ID );
 230      }
 231  }
 232  
 233  
 234  /*

 235   * Load linkable objects:

 236   */
 237  if( param( 'item_ID', 'integer', NULL, true, false, false ) )
 238  { // Load Requested iem:
 239      $ItemCache = & get_Cache( 'ItemCache' );
 240      if( ($edited_Item = & $ItemCache->get_by_ID( $item_ID, false )) === false )
 241      {    // We could not find the contact to link:
 242          $Messages->head = T_('Cannot link Item!');
 243          $Messages->add( T_('Requested item does not exist any longer.'), 'error' );
 244          unset( $edited_Item );
 245          forget_param( 'item_ID' );
 246          unset( $item_ID );
 247      }
 248  }
 249  
 250  
 251  
 252  // Check actions that need early processing:

 253  if( $action == 'createnew' )
 254  {
 255      // Check permission:

 256      $current_User->check_perm( 'files', 'add', true );
 257  
 258      // create new file/dir

 259      param( 'create_type', 'string', true ); // 'file', 'dir'

 260  
 261      $action = ( $create_type == 'file' ? 'createnew_file' : 'createnew_dir'  );
 262  }
 263  
 264  switch( $action )
 265  {
 266      case 'filter':
 267          $action = 'list';
 268          break;
 269  
 270      case 'filter_unset':
 271          // Clear filters!

 272          $fm_filter = '';
 273          $action = 'list';
 274          break;
 275  
 276      case 'createnew_dir':
 277          // We are probably comming from 'createnew' but there is no guarantee!

 278          // Check permission:

 279          $current_User->check_perm( 'files', 'add', true );
 280  
 281          if( ! $Settings->get( 'fm_enable_create_dir' ) )
 282          { // Directory creation is gloablly disabled:
 283              $Messages->add( T_('Directory creation is disabled.'), 'error' );
 284              break;
 285          }
 286  
 287          if( !    param( 'create_name', 'string', '' ) )
 288          { // No name was supplied:
 289              $Messages->add( T_('Cannot create a directory without name.'), 'error' );
 290              break;
 291          }
 292          if( $error_dirname = validate_dirname( $create_name ) )
 293          { // Not valid dirname
 294              $Messages->add( $error_dirname, 'error' );
 295              break;
 296          }
 297  
 298          // Try to get File object:

 299          /**

 300           * @var FileCache

 301           */
 302          $FileCache = & get_Cache( 'FileCache' );
 303          /**

 304           * @var File

 305           */
 306          $newFile = & $FileCache->get_by_root_and_path( $fm_FileRoot->type, $fm_FileRoot->in_type_ID, $path.$create_name );
 307  
 308          if( $newFile->exists() )
 309          {
 310              $Messages->add( sprintf( T_('The file &laquo;%s&raquo; already exists.'), $create_name ), 'error' );
 311              break;
 312          }
 313  
 314          if( ! $newFile->create( $create_type ) )
 315          {
 316              $Messages->add( sprintf( T_('Could not create directory &laquo;%s&raquo; in &laquo;%s&raquo;.'), $create_name, $fm_Filelist->_rds_list_path ), 'error' );
 317          }
 318  
 319          $Messages->add( sprintf( T_('The directory &laquo;%s&raquo; has been created.'), $create_name ), 'success' );
 320  
 321          header_redirect( regenerate_url( '', '', '', '&' ) );
 322          // $action = 'list';

 323          break;
 324  
 325  
 326      case 'createnew_file':
 327          // We are probably comming from 'createnew' but there is no guarantee!

 328          // Check permission:

 329          $current_User->check_perm( 'files', 'add', true );
 330  
 331          if( ! $Settings->get( 'fm_enable_create_file' ) )
 332          { // File creation is gloablly disabled:
 333              $Messages->add( T_('File creation is disabled.'), 'error' );
 334              break;
 335          }
 336  
 337          if( !    param( 'create_name', 'string', '' ) )
 338          { // No name was supplied:
 339              $Messages->add( T_('Cannot create a file without name.'), 'error' );
 340              break;
 341          }
 342          if( $error_filename = validate_filename( $create_name, $current_User->check_perm( 'files', 'all' ) ) )
 343          { // Not valid filename or extension
 344              $Messages->add( $error_filename, 'error' );
 345              break;
 346          }
 347  
 348          // Try to get File object:

 349          $FileCache = & get_Cache( 'FileCache' );
 350          $newFile = & $FileCache->get_by_root_and_path( $fm_FileRoot->type, $fm_FileRoot->in_type_ID, $path.$create_name );
 351  
 352          if( $newFile->exists() )
 353          {
 354              $Messages->add( sprintf( T_('The file &laquo;%s&raquo; already exists.'), $create_name ), 'error' );
 355              break;
 356          }
 357  
 358          if( ! $newFile->create( $create_type ) )
 359          {
 360              $Messages->add( sprintf( T_('Could not create file &laquo;%s&raquo; in &laquo;%s&raquo;.'), $create_name, $fm_Filelist->_rds_list_path ), 'error' );
 361          }
 362  
 363          $Messages->add( sprintf( T_('The file &laquo;%s&raquo; has been created.'), $create_name ), 'success' );
 364  
 365          header_redirect( regenerate_url( '', '', '', '&' ) );
 366          // $action = 'list';

 367          break;
 368  
 369  
 370    case 'update_settings':
 371          // Update settings NOW since they may affect the FileList

 372          $UserSettings->set( 'fm_dirsnotattop',   1-param( 'option_dirsattop',        'integer', 0 ) );
 373          $UserSettings->set( 'fm_permlikelsl',      param( 'option_permlikelsl',      'integer', 0 ) );
 374          $UserSettings->set( 'fm_imglistpreview',   param( 'option_imglistpreview',   'integer', 0 ) );
 375          $UserSettings->set( 'fm_getimagesizes',    param( 'option_getimagesizes',    'integer', 0 ) );
 376  
 377          $UserSettings->set( 'fm_showtypes',        param( 'option_showtypes',        'integer', 0 ) );
 378          $UserSettings->set( 'fm_showdate',         param( 'option_showdate',         'string', 'compact' ) );
 379          $UserSettings->set( 'fm_showfsperms',      param( 'option_showfsperms',      'integer', 0 ) );
 380          $UserSettings->set( 'fm_showfsowner',      param( 'option_showfsowner',      'integer', 0 ) );
 381          $UserSettings->set( 'fm_showfsgroup',      param( 'option_showfsgroup',      'integer', 0 ) );
 382  
 383          $UserSettings->set( 'fm_showhidden',       param( 'option_showhidden',       'integer', 0 ) );
 384          $UserSettings->set( 'fm_recursivedirsize', param( 'option_recursivedirsize', 'integer', 0 ) );
 385          $UserSettings->set( 'fm_uploadwithproperties', param( 'option_uploadwithproperties', 'integer', 0 ) );
 386          $UserSettings->set( 'fm_allowfiltering',   param( 'option_allowfiltering', 'string', 'simple' ) );
 387  
 388          if( $UserSettings->dbupdate() )
 389          {
 390              $Messages->add( T_('Your user settings have been updated.'), 'success' );
 391          }
 392  
 393          header_redirect( regenerate_url( '', '', '', '&' ) );
 394          // $action = 'list';

 395          break;
 396  
 397      case 'update_file':
 398          // Update File:

 399  
 400          if( $demo_mode )
 401          {
 402              $Messages->add( 'Sorry, you cannot update files in demo mode!', 'error' );
 403              break;
 404          }
 405  
 406          // Check permission!

 407           $current_User->check_perm( 'files', 'edit', true );
 408  
 409          $edit_File = & $selected_Filelist->get_by_idx(0);
 410  
 411          // Check that the file is editable:

 412          if( ! $edit_File->is_editable( $current_User->check_perm( 'files', 'all' ) ) )
 413          {
 414              $Messages->add( sprintf( T_( 'You are not allowed to edit &laquo;%s&raquo;.' ), $edit_File->dget('name') ), 'error' );
 415              break;
 416          }
 417  
 418          param( 'file_content', 'html', '', false );
 419  
 420  
 421      $full_path = $edit_File->get_full_path();
 422          if( $rsc_handle = fopen( $full_path, 'w+') )
 423          {
 424              fwrite( $rsc_handle, $file_content );
 425              fclose( $rsc_handle );
 426              $Messages->add( sprintf( T_( 'The file &laquo;%s&raquo; has been updated.' ), $edit_File->dget('name') ), 'success' );
 427          }
 428          else
 429          {
 430              $Messages->add( sprintf( T_( 'The file &laquo;%s&raquo; could not be updated.' ), $edit_File->dget('name') ), 'error' );
 431          }
 432  
 433          header_redirect( regenerate_url( '', '', '', '&' ) );
 434          // $action = 'list';

 435          break;
 436  }
 437  
 438  
 439  // Do we want to display the directory tree next to the files table

 440  $UserSettings->param_Request( 'fm_hide_dirtree', 'fm_hide_dirtree', 'integer', 0, true );
 441  
 442  
 443  /**

 444   * Filelist

 445   */
 446  $fm_Filelist = new Filelist( $fm_FileRoot, $ads_list_path );
 447  $Debuglog->add( 'FM _rds_list_path: '.var_export( $fm_Filelist->_rds_list_path, true ), 'files' );
 448  
 449  param( 'fm_filter', '', NULL, true );
 450  param( 'fm_filter_regex', 'integer', 0, true );
 451  $fm_Filelist->set_Filter( $fm_filter, $fm_filter_regex );
 452  
 453  if( $UserSettings->param_Request( 'fm_dirsnotattop', 'fm_dirsnotattop', 'integer', 0 ) )
 454  {
 455      $fm_Filelist->_dirs_not_at_top = true;
 456  }
 457  if( $UserSettings->param_Request( 'fm_recursivedirsize', 'fm_recursivedirsize', 'integer', 0 ) ) // TODO: check for permission? (Server load)
 458  {
 459      $fm_Filelist->_use_recursive_dirsize = true;
 460  }
 461  if( $UserSettings->param_Request( 'fm_showhidden', 'fm_showhidden', 'integer', 0 ) )
 462  {
 463      $fm_Filelist->_show_hidden_files = true;
 464  }
 465  if( param( 'fm_flatmode', '', NULL, true ) )
 466  {
 467      $fm_Filelist->flatmode = true;
 468  }
 469  
 470  /*

 471   * Load Filelist (with meta data):

 472   */
 473  $fm_Filelist->load();
 474  
 475  // Sort Filelist

 476  param( 'fm_order', 'string', NULL, true );
 477  if( ! in_array( $fm_order, array( 'name', 'path', 'type', 'size', 'lastmod', 'perms', 'fsowner', 'fsgroup' ) ) )
 478  {
 479      $fm_order = NULL;
 480  }
 481  param( 'fm_orderasc', '', NULL, true );
 482  $fm_Filelist->sort( $fm_order, $fm_orderasc );
 483  
 484  
 485  switch( $action )
 486  {
 487      case 'download':
 488          // TODO: We don't need the Filelist, move UP!

 489          // TODO: provide optional zip formats (tgz, ..) - the used lib provides more..

 490          // TODO: use "inmemory"=>false, so that you can download bigger archives faster!

 491  
 492          $action_title = T_('Download');
 493  
 494          if( !$selected_Filelist->count() )
 495          {
 496              $Messages->add( T_('Nothing selected.'), 'error' );
 497              break;
 498          }
 499  
 500          param( 'zipname', 'string', '' );
 501          param( 'exclude_sd', 'integer', 0 );
 502  
 503          if( empty($zipname) )
 504          {
 505              if( param( 'action_invoked', 'integer', 0 ) )
 506              { // Action was invoked, add "hint"
 507                  param_error( 'zipname', T_('Please provide the name of the archive.') );
 508              }
 509              if( $selected_Filelist->count() == 1 )
 510              {
 511                  $only_File = $selected_Filelist->get_array();
 512                  $only_File = $only_File[0];
 513  
 514                  // TODO: once we support additional formats, use the default extension here:

 515                  $zipname = $only_File->get_name().'.zip';
 516              }
 517              break;
 518          }
 519  
 520          // Downloading

 521          load_class('_ext/_zip_archives.php');
 522  
 523          $arraylist = $selected_Filelist->get_array( 'get_rdfs_rel_path' );
 524  
 525          $options = array (
 526              'basedir' => $fm_Filelist->get_ads_list_path(),
 527              'inmemory' => 1,
 528              'recurse' => (1 - $exclude_sd),
 529          );
 530  
 531          $zipfile = & new zip_file( $zipname );
 532          $zipfile->set_options( $options );
 533          $zipfile->add_files( $arraylist );
 534          $zipfile->create_archive();
 535  
 536          if( $zipfile->error )
 537          {
 538              foreach($zipfile->error as $v)
 539              {
 540                  $Messages->add( $v, 'error' );
 541              }
 542              break;
 543          }
 544  
 545          $zipfile->download_file();
 546          exit;
 547          /* EXITED! */

 548  
 549  
 550      case 'rename':
 551          // TODO: We don't need the Filelist, move UP!

 552          // Rename a file:

 553  
 554          // This will not allow to overwrite existing files, the same way Windows and MacOS do not allow it. Adding an option will only clutter the interface and satisfy geeks only.

 555          if( ! $current_User->check_perm( 'files', 'edit' ) )
 556          { // We do not have permission to edit files
 557              $Messages->add( T_('You have no permission to edit/modify files.'), 'error' );
 558              $action = 'list';
 559              break;
 560          }
 561  
 562          $allow_locked_filetypes = $current_User->check_perm( 'files', 'all' );
 563  
 564          if( ! $selected_Filelist->count() )
 565          { // There is nothing to rename
 566              $Messages->add( T_('Nothing selected.'), 'error' );
 567              $action = 'list';
 568              break;
 569          }
 570  
 571          param( 'confirmed', 'integer', 0 );
 572          param( 'new_names', 'array', array() );
 573  
 574          // Check params for each file to rename:

 575          while( $loop_src_File = & $selected_Filelist->get_next() )
 576          {
 577              if( ! isset( $new_names[$loop_src_File->get_md5_ID()] ) )
 578              { // We have not yet provided a name to rename to...
 579                  $confirmed = 0;
 580                  $new_names[$loop_src_File->get_md5_ID()] = $loop_src_File->get_name();
 581                  continue;
 582              }
 583  
 584              // Check if provided name is okay:

 585              $new_names[$loop_src_File->get_md5_ID()] = trim(strip_tags($new_names[$loop_src_File->get_md5_ID()]));
 586  
 587              if( !$loop_src_File->is_dir() )
 588              {
 589                  if( $error_filename = validate_filename( $new_names[$loop_src_File->get_md5_ID()], $allow_locked_filetypes ) )
 590                  { // Not a file name or not an allowed extension
 591                      $confirmed = 0;
 592                      param_error( 'new_names['.$loop_src_File->get_md5_ID().']', $error_filename );
 593                      continue;
 594                  }
 595              }
 596              elseif( $error_dirname = validate_dirname( $new_names[$loop_src_File->get_md5_ID()], $allow_locked_filetypes ) )
 597              { // directory name
 598                  $confirmed = 0;
 599                  param_error( 'new_names['.$loop_src_File->get_md5_ID().']', $error_dirname );
 600                  continue;
 601              }
 602          }
 603  
 604          if( $confirmed )
 605          { // Rename is confirmed, let's proceed:
 606              $selected_Filelist->restart();
 607              while( $loop_src_File = & $selected_Filelist->get_next() )
 608              {
 609                  $old_name = $loop_src_File->get_name();
 610                  $new_name = $new_names[$loop_src_File->get_md5_ID()];
 611  
 612                  if( $new_name == $old_name )
 613                  { // Name has not changed...
 614                      $Messages->add( sprintf( T_('&laquo;%s&raquo; has not been renamed'), $old_name ), 'note' );
 615                      continue;
 616                  }
 617                  // Perform rename:

 618                  if( ! $loop_src_File->rename_to( $new_name ) )
 619                  {
 620                      $Messages->add( sprintf( T_('&laquo;%s&raquo; could not be renamed to &laquo;%s&raquo;'),
 621                          $old_name, $new_name ), 'error' );
 622                      continue;
 623                  }
 624  
 625                  // We have moved in same dir, update caches:

 626                  $fm_Filelist->update_caches();
 627  
 628                  if( $fm_Filelist->contains( $loop_src_File ) === false )
 629                  { // File not in filelist (expected if not same dir)
 630                      $fm_Filelist->add( $File );
 631                  }
 632  
 633  
 634                  $Messages->add( sprintf( T_('&laquo;%s&raquo; has been successfully renamed to &laquo;%s&raquo;'),
 635                          $old_name, $new_name ), 'success' );
 636              }
 637  
 638              // REDIRECT / EXIT

 639               header_redirect( regenerate_url( '', '', '', '&' ) );
 640             // $action = 'list';

 641          }
 642          break;
 643  
 644  
 645      case 'delete':
 646          // TODO: We don't need the Filelist, move UP!

 647          // Delete a file or directory:

 648  
 649          if( ! $current_User->check_perm( 'files', 'edit' ) )
 650          { // We do not have permission to edit files
 651              $Messages->add( T_('You have no permission to edit/modify files.'), 'error' );
 652              $action = 'list';
 653              break;
 654          }
 655  
 656          if( ! $selected_Filelist->count() )
 657          {
 658              $Messages->add( T_('Nothing selected.'), 'error' );
 659              $action = 'list';
 660              break;
 661          }
 662  
 663          param( 'confirmed', 'integer', 0 );
 664          // fplanque>> We cannot actually offer to delete subdirs since we cannot pre-check DB

 665  
 666          $selected_Filelist->restart();
 667          if( $confirmed )
 668          { // Unlink files:
 669              while( $l_File = & $selected_Filelist->get_next() )
 670              {
 671                  if( $l_File->unlink() )
 672                  {
 673                      $Messages->add( sprintf( ( $l_File->is_dir() ? T_('The directory &laquo;%s&raquo; has been deleted.')
 674                                      : T_('The file &laquo;%s&raquo; has been deleted.') ), $l_File->dget('name') ), 'success' );
 675                      $fm_Filelist->remove( $l_File );
 676                  }
 677                  else
 678                  {
 679                      $Messages->add( sprintf( ( $l_File->is_dir() ? T_('Could not delete the directory &laquo;%s&raquo; (not empty?).')
 680                                      : T_('Could not delete the file &laquo;%s&raquo;.') ), $l_File->dget('name') ), 'error' );
 681                  }
 682              }
 683              $action = 'list';
 684          }
 685          else
 686          {
 687              // make sure we have loaded metas for all files in selection!

 688              $selected_Filelist->load_meta();
 689  
 690              // Check if there are delete restrictions on the files:

 691              while( $l_File = & $selected_Filelist->get_next() )
 692              {
 693                  // Check if there are delete restrictions on this file:

 694                  $l_File->check_relations( 'delete_restrictions' );
 695  
 696                  if( $Messages->count('restrict') )
 697                  { // There are restrictions:
 698                      $Messages->add( $l_File->get_prefixed_name().': '.T_('cannot be deleted because of the following relations')
 699                          .$Messages->display( NULL, NULL, false, 'restrict', '', 'ul', false ) );
 700                      $Messages->clear( 'restrict' );
 701  
 702                      // remove it from the list of selected files (that will be offered to delete):

 703                      $selected_Filelist->remove( $l_File );
 704                  }
 705              }
 706  
 707              if( ! $selected_Filelist->count() )
 708              { // no files left in list, cancel action
 709                  $action = 'list';
 710              }
 711          }
 712          break;
 713  
 714  
 715      case 'make_posts':
 716          // TODO: We don't need the Filelist, move UP!

 717          // Make posts with selected images:

 718  
 719          if( ! $selected_Filelist->count() )
 720          {
 721              $Messages->add( T_('Nothing selected.'), 'error' );
 722              $action = 'list';
 723              break;
 724          }
 725  
 726          // fp> TODO: this block should move to a general level

 727          // Try to go to the right blog:

 728          if( $fm_Filelist->get_root_type() == 'collection' )
 729          {
 730              set_working_blog( $fm_Filelist->get_root_ID() );
 731        // Load the blog we're in:

 732              $Blog = & $BlogCache->get_by_ID( $blog );
 733          }
 734          // ---

 735  
 736  
 737          if( empty( $Blog ) )
 738          {
 739              $Messages->add( T_('No destination blog is selected.'), 'error' );
 740              break;
 741          }
 742          //$Blog->disp('name');

 743  
 744          // Get default status (includes PERM CHECK):

 745          $item_status = $Blog->get_allowed_item_status();
 746          if( empty($item_status) )
 747          {
 748              $Messages->add( T_('Sorry, you have no permission to post into this blog.'), 'error' );
 749              break;
 750          }
 751  
 752          // make sure we have loaded metas for all files in selection!

 753          $selected_Filelist->load_meta();
 754  
 755          // Ready to create posts:

 756          load_class('items/model/_item.class.php');
 757          while( $l_File = & $selected_Filelist->get_next() )
 758          {
 759              if( ! $l_File->is_image() )
 760              {
 761                  $Messages->add( sprintf( T_('Cannot post &laquo;%s&raquo; because it is not an image.'), $l_File->dget('name') ), 'error' );
 762                  continue;
 763              }
 764  
 765              // Create a post:

 766              $edited_Item = & new Item();
 767  
 768              $edited_Item->blog_ID = $blog;
 769  
 770              $edited_Item->set( 'status', $item_status );
 771  
 772              $edited_Item->set( 'main_cat_ID', $Blog->get_default_cat_ID() );
 773  
 774              $title = $l_File->get('title');
 775              if( empty($title) )
 776              {
 777                  $title = $l_File->get('name');
 778              }
 779              $edited_Item->set( 'title', $title );
 780  
 781              $DB->begin();
 782  
 783              // INSERT NEW POST INTO DB:

 784              $edited_Item->dbinsert();
 785  
 786              // echo '<br>file meta: '.$l_File->meta;

 787              if(    $l_File->meta == 'notfound' )
 788              {    // That file has no meta data yet, create it now!
 789                  $l_File->dbsave();
 790              }
 791  
 792              // Let's make the link!

 793              $edited_Link = & new Link();
 794              $edited_Link->set( 'itm_ID', $edited_Item->ID );
 795              $edited_Link->set( 'file_ID', $l_File->ID );
 796              $edited_Link->dbinsert();
 797  
 798              $DB->commit();
 799  
 800              $Messages->add( sprintf( T_('&laquo;%s&raquo; has been posted.'), $l_File->dget('name') ), 'success' );
 801          }
 802  
 803          // Note: we redirect without restoring filter. This should allow to see the new files.

 804          // &filter=restore

 805          header_redirect( 'admin.php?ctrl=items&blog='.$blog );    // Will save $Messages

 806  
 807          // Note: we have EXITED here. In case we remove the redir, we need this:

 808  
 809          // Reset stuff so it doesn't interfere with upcomming display

 810          unset( $edited_Item );
 811          unset( $edited_Link );
 812          $selected_Filelist = & new Filelist( $fm_Filelist->get_FileRoot(), false );
 813          break;
 814  
 815  
 816      case 'edit_file':
 817          // TODO: We don't need the Filelist, move UP!

 818          // Edit Text File

 819  
 820          // Check permission!

 821           $current_User->check_perm( 'files', 'edit', true );
 822  
 823          $edit_File = & $selected_Filelist->get_by_idx(0);
 824  
 825          // Check that the file is editable:

 826          if( ! $edit_File->is_editable( $current_User->check_perm( 'files', 'all' ) ) )
 827          {
 828              $Messages->add( sprintf( T_( 'You are not allowed to edit &laquo;%s&raquo;.' ), $edit_File->dget('name') ), 'error' );
 829               // Leave special display mode:

 830              $action = 'list';
 831              break;
 832          }
 833  
 834          $full_path = $edit_File->get_full_path();
 835          if( $size = filesize($full_path) )
 836          {
 837              $rsc_handle = fopen( $full_path, 'r');
 838              $edit_File->content = fread( $rsc_handle, $size );
 839              fclose( $rsc_handle );
 840          }
 841          else
 842          {    // Empty file
 843              $edit_File->content = '';
 844          }
 845          break;
 846  
 847  
 848      case 'edit_properties':
 849          // TODO: We don't need the Filelist, move UP!

 850          // Edit File properties (Meta Data)

 851  
 852          // Check permission!

 853           $current_User->check_perm( 'files', 'edit', true );
 854  
 855          $edit_File = & $selected_Filelist->get_by_idx(0);
 856          $edit_File->load_meta();
 857          break;
 858  
 859  
 860      case 'update_properties':
 861          // TODO: We don't need the Filelist, move UP!

 862          // Update File properties (Meta Data); on success this ends the file_properties mode:

 863  
 864          // Check permission!

 865           $current_User->check_perm( 'files', 'edit', true );
 866  
 867          $edit_File = & $selected_Filelist->get_by_idx(0);
 868          // Load meta data:

 869          $edit_File->load_meta();
 870  
 871          $edit_File->set( 'title', param( 'title', 'string', '' ) );
 872          $edit_File->set( 'alt', param( 'alt', 'string', '' ) );
 873          $edit_File->set( 'desc', param( 'desc', 'string', '' ) );
 874  
 875          // Store File object into DB:

 876          if( $edit_File->dbsave() )
 877          {
 878              $Messages->add( sprintf( T_( 'File properties for &laquo;%s&raquo; have been updated.' ), $edit_File->dget('name') ), 'success' );
 879          }
 880          else
 881          {
 882              $Messages->add( sprintf( T_( 'File properties for &laquo;%s&raquo; have not changed.' ), $edit_File->dget('name') ), 'note' );
 883          }
 884          break;
 885  
 886  
 887      case 'link':
 888          // TODO: We don't need the Filelist, move UP!

 889          // Link File to Item (or other object if extended below):

 890  
 891          // Note: we are not modifying any file here, we're just linking it

 892          // we only need read perm on file, but we'll need write perm on destination object (to be checked below)

 893  
 894          if( isset($edited_Item) )
 895          {
 896              if( !$selected_Filelist->count() )
 897              {
 898                  $Messages->add( T_('Nothing selected.'), 'error' );
 899                  break;
 900              }
 901  
 902              $edit_File = & $selected_Filelist->get_by_idx(0);
 903  
 904              // check item EDIT permissions:

 905              $current_User->check_perm( 'item', 'edit', true, $edited_Item );
 906  
 907              $DB->begin();
 908  
 909              // Load meta data AND MAKE SURE IT IS CREATED IN DB:

 910              $edit_File->load_meta( true );
 911  
 912              // Let's make the link!

 913              $edited_Link = & new Link();
 914              $edited_Link->set( 'itm_ID', $edited_Item->ID );
 915              $edited_Link->set( 'file_ID', $edit_File->ID );
 916              $edited_Link->dbinsert();
 917  
 918              $DB->commit();
 919  
 920              $Messages->add( T_('Selected file has been linked to item.'), 'success' );
 921  
 922              // In case the mode had been closed, reopen it:

 923              $fm_mode = 'link_item';
 924          }
 925          // Plug extensions/hacks here!

 926          else
 927          {    // No Item to link to - end link_item mode.
 928              $fm_mode = NULL;
 929          }
 930  
 931          // REDIRECT / EXIT

 932           header_redirect( regenerate_url( '', '', '', '&' ) );
 933          break;
 934  
 935  
 936      case 'unlink':
 937          // TODO: We don't need the Filelist, move UP!

 938          // Unlink File from Item (or other object if extended):

 939  
 940          // Note: we are not modifying any file here, we're just linking it

 941          // we only need read perm on file, but we'll need write perm on destination object (to be checked below)

 942  
 943          if( !isset( $edited_Link ) )
 944          {
 945              $action = 'list';
 946              break;
 947          }
 948  
 949          // get Item (or other object) from Link to check perm

 950          $edited_Item = & $edited_Link->Item;
 951  
 952          // Check that we have permission to edit item:

 953          $current_User->check_perm( 'item', 'edit', true, $edited_Item );
 954  
 955          // Delete from DB:

 956          $msg = sprintf( T_('Link from &laquo;%s&raquo; deleted.'), $edited_Link->Item->dget('title') );
 957          $edited_Link->dbdelete( true );
 958          unset( $edited_Link );
 959          forget_param( 'link_ID' );
 960  
 961          $Messages->add( $msg, 'success' );
 962          $action = 'list';
 963          // REDIRECT / EXIT

 964           header_redirect( regenerate_url( '', '', '', '&' ) );
 965          break;
 966  
 967  
 968      case 'edit_perms':
 969          // TODO: We don't need the Filelist, move UP!

 970          // Edit file or directory permissions:

 971  
 972          if( ! $current_User->check_perm( 'files', 'edit' ) )
 973          { // We do not have permission to edit files
 974              $Messages->add( T_('You have no permission to edit/modify files.'), 'error' );
 975              $action = 'list';
 976              break;
 977          }
 978  
 979          if( ! $selected_Filelist->count() )
 980          {
 981              $Messages->add( T_('Nothing selected.'), 'error' );
 982              $action = 'list';
 983              break;
 984          }
 985  
 986  
 987          param( 'perms', 'array', array() );
 988          param( 'edit_perms_default' ); // default value when multiple files are selected

 989          param( 'use_default_perms', 'array', array() ); // array of file IDs that should be set to default

 990  
 991          if( count( $use_default_perms ) && $edit_perms_default === '' )
 992          {
 993              param_error( 'edit_perms_default', T_('You have to give a default permission!') );
 994              break;
 995          }
 996  
 997          // form params

 998          $perms_read_readonly = is_windows();
 999          $field_options_read_readonly = array(
1000                  array( 'value' => 444, 'label' => T_('Read-only') ),
1001                  array( 'value' => 666, 'label' => T_('Read and write') ) );
1002          $more_than_one_selected_file = ( $selected_Filelist->count() > 1 );
1003  
1004          if( count( $perms ) || count( $use_default_perms ) )
1005          { // New permissions given, change them
1006              $selected_Filelist->restart();
1007              while( $l_File = & $selected_Filelist->get_next() )
1008              {
1009                  if( in_array( $l_File->get_md5_ID(), $use_default_perms ) )
1010                  { // use default
1011                      $chmod = $edit_perms_default;
1012                  }
1013                  elseif( !isset($perms[ $l_File->get_md5_ID() ]) )
1014                  { // happens for an empty text input or when no radio option is selected
1015                      $Messages->add( sprintf( T_('Permissions for &laquo;%s&raquo; have not been changed.'), $l_File->dget('name') ), 'note' );
1016                      continue;
1017                  }
1018                  else
1019                  { // provided for this file
1020                      $chmod = $perms[ $l_File->get_md5_ID() ];
1021                  }
1022  
1023                  $oldperms = $l_File->get_perms( 'raw' );
1024                  $newperms = $l_File->chmod( octdec( $chmod ) );
1025  
1026                  if( $newperms === false )
1027                  {
1028                      $Messages->add( sprintf( T_('Failed to set permissions on &laquo;%s&raquo; to &laquo;%s&raquo;.'), $l_File->dget('name'), $chmod ), 'error' );
1029                  }
1030                  else
1031                  {
1032                      // Success, remove the file from the list of selected files:

1033                      $selected_Filelist->remove( $l_File );
1034  
1035                      if( $newperms === $oldperms )
1036                      {
1037                          $Messages->add( sprintf( T_('Permissions for &laquo;%s&raquo; have not been changed.'), $l_File->dget('name') ), 'note' );
1038                      }
1039                      else
1040                      {
1041                          $Messages->add( sprintf( T_('Permissions for &laquo;%s&raquo; changed to &laquo;%s&raquo;.'), $l_File->dget('name'), $l_File->get_perms() ), 'success' );
1042                      }
1043                  }
1044              }
1045          }
1046  
1047          if( !$selected_Filelist->count() )
1048          { // No file left selected... (everything worked fine)
1049              $action = 'list';
1050          }
1051          break;
1052  }
1053  
1054  
1055  /*

1056   * Prepare for modes:

1057   */
1058  switch( $fm_mode )
1059  {
1060      case 'file_copy':
1061      case 'file_move':
1062          // ------------------------

1063          // copy/move a file:

1064          // ------------------------

1065          /*

1066           * fplanque>> This whole thing is flawed:

1067           * 1) only geeks can possibly like to use the same interface for renaming, moving and copying

1068           * 2) even the geeky unix commands won't pretend copying and moving are the same thing. They are not!

1069           *    Only moving and renaming are similar, and again FOR GEEKS ONLY.

1070           * 3) The way this works it breaks the File meta data (I'm working on it).

1071           * 4) For Move and Copy, this should use a "destination directory tree" on the right (same as for upload)

1072           * 5) Given all the reasons above copy, move and rename should be clearly separated into 3 different interfaces.

1073           *

1074           * blueyed>> it was never meant to only use a single interface. The original mode

1075           *   'file_cmr' was just a mode to handle it internally easier/more central.

1076           *   'copy' is just 'move and keep the source', while 'rename' is 'move in the same dir'

1077           *

1078           */
1079  
1080          /*

1081          TODO: On error notes use prefixed names, if the roots differ.

1082                Something like $fm_Filelist->get_names_realtive_to( $a_File, $b_File, $root_type, $root_ID, $rel_path )

1083                that returns an array containing the name of $a_File and $b_File relative to the Root path given as

1084                param 3, 4, 5.

1085                This would allow to say "Copied «users/admin/test_me.jpg» to «test_me.jpg»." rather than just

1086                "Copied «test_me.jpg» to «test_me.jpg».".

1087              // fp>> I don't really understand this (probably missing a verb) but I do think that extending the Fileman object is not the right direction to go on the long term

1088              // blueyed>> Tried to make it clearer. If it wasn't a Filemanager method, it has to be a function or

1089              //   a method of the File class. IMHO it should be a method of the (to be killed) Filemanager object.

1090              // fp>> Okay. It should *definitely* be a method of the File object and we should ask for ONE file at a time. Any question about 'where is the file?' (what/where/when/who, etc) should be asked to the File object itself.

1091          */
1092  
1093          if( ! $current_User->check_perm( 'files', 'edit' ) )
1094          { // We do not have permission to edit files
1095              $Messages->add( T_('You have no permission to edit/modify files.'), 'error' );
1096              $fm_mode = NULL;
1097              break;
1098          }
1099  
1100          // Get the source list

1101          if( $fm_sources = param( 'fm_sources', 'array', array(), true ) )
1102          {
1103              $fm_sources_root = param( 'fm_sources_root', 'string', '', true );
1104  
1105              $sources_Root = & $FileRootCache->get_by_ID( $fm_sources_root );
1106  
1107              if( $sources_Root )
1108              { // instantiate the source list for the selected sources
1109                  $fm_source_Filelist = & new Filelist( $sources_Root );
1110              }
1111              else
1112              { // Fallback: source files are considered to be in the current root
1113                  $fm_source_Filelist = & new Filelist( $fm_Filelist->get_FileRoot() );
1114                  $Debuglog->add( 'SourceList without explicit root!', 'error' );
1115              }
1116  
1117              if( $fm_source_Filelist )
1118              {
1119                  // TODO: should fail for non-existant sources, or sources where no read-perm

1120                  foreach( $fm_sources as $l_source_path )
1121                  {
1122                      // echo '<br>'.$lSourcePath;

1123                      $fm_source_Filelist->add_by_subpath( urldecode($l_source_path), true );
1124                  }
1125              }
1126              else
1127              { // Without SourceList there's no mode
1128                  $fm_mode = false;
1129              }
1130          }
1131          else
1132          {
1133              $fm_source_Filelist = false;
1134              $fm_sources = NULL;
1135              $fm_sources_root = NULL;
1136          }
1137  
1138          if( ! $fm_source_Filelist || ! $fm_source_Filelist->count() )
1139          {
1140              $Messages->add( T_('No source files!'), 'error' );
1141              $fm_mode = NULL;
1142              break;
1143          }
1144  
1145          param( 'confirm', 'integer', 0 );
1146          param( 'new_names', 'array', array() );
1147          param( 'overwrite', 'array', array() );
1148  
1149          // Check params for each file to rename:

1150          while( $loop_src_File = & $fm_source_Filelist->get_next() )
1151          {
1152              if( ! $loop_src_File->exists() )
1153              { // this can happen on reloading the page
1154                  $fm_source_Filelist->remove($loop_src_File);
1155                  continue;
1156              }
1157              if( ! isset( $new_names[$loop_src_File->get_md5_ID()] ) )
1158              { // We have not yet provided a name to rename to...
1159                  $confirm = 0;
1160                  $new_names[$loop_src_File->get_md5_ID()] = $loop_src_File->get('name');
1161                  continue;
1162              }
1163  
1164              // Check if provided name is okay:

1165              $new_names[$loop_src_File->get_md5_ID()] = trim(strip_tags($new_names[$loop_src_File->get_md5_ID()]));
1166  
1167              if( !$loop_src_File->is_dir() )
1168              {
1169                  if( $error_filename = validate_filename( $new_names[$loop_src_File->get_md5_ID()] ) )
1170                  { // Not a file name or not an allowed extension
1171                      $confirm = 0;
1172                      $Messages->add( $error_filename , 'error' );
1173                      continue;
1174                  }
1175              }
1176              elseif( $error_dirname = validate_dirname( $new_names[$loop_src_File->get_md5_ID()] ) )
1177              { // Not a directory name
1178                  $confirm = 0;
1179                  $Messages->add( $error_dirname, 'error' );
1180                  continue;
1181              }
1182  
1183              // Check if destination file exists:

1184              $FileCache = & get_Cache( 'FileCache' );
1185              if( ($dest_File = & $FileCache->get_by_root_and_path( $fm_Filelist->get_root_type(), $fm_Filelist->get_root_ID(), $fm_Filelist->get_rds_list_path().$new_names[$loop_src_File->get_md5_ID()] ))
1186                              && $dest_File->exists() )
1187              { // Target exists
1188                  if( $dest_File === $loop_src_File )
1189                  {
1190                      param_error( 'new_names['.$loop_src_File->get_md5_ID().']', T_('Source and target files are the same. Please choose another name or directory.') );
1191                      $confirm = 0;
1192                      continue;
1193                  }
1194  
1195                  if( ! isset( $overwrite[$loop_src_File->get_md5_ID()] ) )
1196                  { // We have not yet asked to overwrite:
1197                      param_error( 'new_names['.$loop_src_File->get_md5_ID().']', sprintf( T_('The file &laquo;%s&raquo; already exists.'), $dest_File->get_rdfp_rel_path() ) );
1198                      $overwrite[$loop_src_File->get_md5_ID()] = 0;
1199                      $confirm = 0;
1200                      continue;
1201                  }
1202  
1203                  // We have asked to overwite...

1204                  if( $fm_mode == 'file_copy' )
1205                  { // We are making a copy: no problem, we'll recycle the file ID anyway.
1206                      continue;
1207                  }
1208  
1209                  // We are moving, we'll need to unlink the target file and drop it's meta data:

1210                  // Check if there are delete restrictions on this file:

1211                  $dest_File->check_relations( 'delete_restrictions' );
1212  
1213                  if( $Messages->count('restrict') )
1214                  { // There are restrictions:
1215                      // TODO: work on a better output display here...

1216                      param_error( 'new_names['.$loop_src_File->get_md5_ID().']', sprintf( T_('Cannot overwrite the file &laquo;%s&raquo; because of the following relations'), $dest_File->get_rdfp_rel_path() ) );
1217  
1218                      $confirm = 0;
1219                      break;    // stop whole file list processing

1220                  }
1221              }
1222          }
1223  
1224          if( $confirm && $fm_source_Filelist->count() )
1225          { // Copy/move is confirmed (and we still have files to copy/move), let's proceed:
1226  
1227              // Loop through files:

1228              $fm_source_Filelist->restart();
1229              while( $loop_src_File = & $fm_source_Filelist->get_next() )
1230              {
1231                  // Get a pointer on dest file

1232                  $FileCache = & get_Cache( 'FileCache' );
1233                  $dest_File = & $FileCache->get_by_root_and_path( $fm_Filelist->get_root_type(), $fm_Filelist->get_root_ID(), $fm_Filelist->get_rds_list_path().$new_names[$loop_src_File->get_md5_ID()] );
1234  
1235                  if( $fm_mode == 'file_copy' )
1236                  { // COPY
1237  
1238                      // Do the copy

1239                      if( $loop_src_File->copy_to( $dest_File ) )
1240                      { // Success:
1241                          $Messages->add( sprintf( T_('Copied &laquo;%s&raquo; to &laquo;%s&raquo;.'),
1242                                                                          $loop_src_File->get_rdfp_rel_path(), $dest_File->get_rdfp_rel_path() ), 'success' );
1243  
1244                          if( $fm_Filelist->contains( $dest_File ) === false )
1245                          {
1246                              $fm_Filelist->add( $dest_File );
1247                          }
1248                      }
1249                      else
1250                      { // Failure:
1251                          param_error( 'new_names['.$loop_src_File->get_md5_ID().']', sprintf( T_('Could not copy &laquo;%s&raquo; to &laquo;%s&raquo;.'),
1252                                                                          $loop_src_File->get_rdfp_rel_path(), $dest_File->get_rdfp_rel_path() ) );
1253                      }
1254                  }
1255                  elseif( $fm_mode == 'file_move' )
1256                  { // MOVE
1257                      // NOTE: DB integrity is handled by the File object itself

1258                      $DB->begin();
1259  
1260                      if( isset( $overwrite[$loop_src_File->get_md5_ID()] )
1261                              && $overwrite[$loop_src_File->get_md5_ID()] )
1262                      { // We want to overwrite, let's unlink the old file:
1263                          if( ! $dest_File->unlink() )
1264                          { // Unlink failed:
1265                              $DB->rollback();
1266  
1267                              $Messages->add( sprintf( ( $dest_File->is_dir() ? T_('Could not delete the directory &laquo;%s&raquo; (not empty?).') : T_('Could not delete the file &laquo;%s&raquo;.') ), $dest_File->dget('name') ), 'error' );
1268  
1269                              // Move on to next file:

1270                              continue;
1271                          }
1272                      }
1273  
1274                      // Do the move:

1275                      $rdfp_oldpath = $loop_src_File->get_rdfp_rel_path();
1276                      $rdfp_newpath = $fm_Filelist->get_rds_list_path().$new_names[$loop_src_File->get_md5_ID()];
1277  
1278                      if( $loop_src_File->move_to( $fm_Filelist->get_root_type(), $fm_Filelist->get_root_ID(), $rdfp_newpath ) )
1279                      { // successfully moved
1280                          $Messages->add( sprintf( T_('Moved &laquo;%s&raquo; to &laquo;%s&raquo;.'), $rdfp_oldpath, $rdfp_newpath ), 'success' );
1281  
1282                          // We may have moved in same dir, update caches:

1283                          $fm_Filelist->update_caches();
1284                          // We remove the file from the source list, after refreshing the cache

1285                          $fm_source_Filelist->update_caches();
1286                          $fm_source_Filelist->remove( $loop_src_File );
1287  
1288                          if( $fm_Filelist->contains( $loop_src_File ) === false )
1289                          { // File not in filelist (expected if not same dir)
1290                              $fm_Filelist->add( $loop_src_File );
1291                          }
1292                      }
1293                      else
1294                      { // move failed
1295                          param_error( 'new_names['.$loop_src_File->get_md5_ID().']', sprintf( T_('Could not move &laquo;%s&raquo; to &laquo;%s&raquo;.'), $rdfp_oldpath, $rdfp_newpath ) );
1296                          // Note: we do not rollback, since unlinking is already done on disk :'(

1297                      }
1298  
1299                      $DB->commit();
1300                  }
1301                  else debug_die( 'Unhandled file copy/move mode' );
1302              }
1303  
1304              // EXIT MODE:

1305              $fm_mode = NULL;
1306          }
1307          break;
1308  
1309  
1310      case 'link_item':
1311          // We want to link file(s) to an item:

1312  
1313          // TODO: maybe this should not be a mode and maybe we should handle linking as soon as we have an $edited_Item ...

1314  
1315          if( !isset($edited_Item) )
1316          { // No Item to link to...
1317              $fm_mode = NULL;
1318              break;
1319          }
1320  
1321          // TODO: check EDIT permissions!

1322          break;
1323  
1324  }
1325  
1326  
1327  // Update sub-menu:

1328  if( $current_User->check_perm( 'files', 'add' ) )
1329  { // Permission to upload: (no subtabs needed otherwise)
1330      $AdminUI->add_menu_entries(
1331              'files',
1332              array(
1333                      'browse' => array(
1334                          'text' => T_('Browse'),
1335                          'href' => regenerate_url( 'ctrl', 'ctrl=files' ) ),
1336                      'upload' => array(
1337                          'text' => T_('Upload multiple'),
1338                          'href' => regenerate_url( 'ctrl', 'ctrl=upload' ) ),
1339                  )
1340          );
1341  }
1342  
1343  
1344  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)

1345  $AdminUI->disp_html_head();
1346  
1347  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)

1348  $AdminUI->disp_body_top();
1349  
1350  
1351  // Display reload-icon in the opener window if we're a popup in the same CWD and the

1352  // Filemanager content differs.

1353  ?>
1354  <script type="text/javascript">
1355      <!--
1356      if( opener
1357              && opener.document.FilesForm
1358              && typeof(opener.document.FilesForm.md5_filelist.value) != 'undefined'
1359              && typeof(opener.document.FilesForm.md5_cwd.value) != 'undefined'
1360              && opener.document.FilesForm.md5_cwd.value == '<?php echo md5($fm_Filelist->get_ads_list_path()); ?>'
1361          )
1362      {
1363          opener.document.getElementById( 'fm_reloadhint' ).style.display =
1364              opener.document.FilesForm.md5_filelist.value == '<?php echo $fm_Filelist->md5_checksum(); ?>'
1365              ? 'none'
1366              : 'inline';
1367      }
1368      // -->

1369  </script>
1370  <?php
1371  
1372  $AdminUI->disp_payload_begin();
1373  
1374  /*

1375   * Display payload:

1376   */
1377  if( !empty($action ) && $action != 'list' && $action != 'nil' )
1378  {
1379  
1380      // Action displays:

1381      switch( $action )
1382      {
1383          case 'rename':
1384              // Rename files dialog:

1385              $AdminUI->disp_view( 'files/views/_file_rename.form.php' );
1386              break;
1387  
1388          case 'delete':
1389              // Delete file(s). We arrive here either if not confirmed or in case of error(s).

1390              $AdminUI->disp_view( 'files/views/_file_delete.form.php' );
1391              break;
1392  
1393          case 'download':
1394              $AdminUI->disp_view( 'files/views/_file_download.form.php' );
1395              break;
1396  
1397          case 'edit_perms':
1398              // Filesystem permissions for specific files

1399              $AdminUI->disp_view( 'files/views/_file_permissions.form.php' );
1400              break;
1401  
1402          case 'edit_file':
1403              // File Edit dialog:

1404              $AdminUI->disp_view( 'files/views/_file_edit.form.php' );
1405              break;
1406  
1407          case 'edit_properties':
1408              // File properties (Meta data) dialog:

1409              $AdminUI->disp_view( 'files/views/_file_properties.form.php' );
1410              break;
1411  
1412          case 'edit_settings':
1413              // Display settings dialog:

1414              $AdminUI->disp_view( 'files/views/_file_browse_set.form.php' );
1415              break;
1416  
1417          case 'download':
1418              // Deferred action message:

1419              if( isset($action_title) )
1420              {
1421                  echo "\n<h2>$action_title</h2>\n";
1422              }
1423  
1424              if( isset($action_msg) )
1425              {
1426                  echo $action_msg;
1427  
1428                  if( isset( $js_focus ) )
1429                  { // we want to auto-focus a field
1430                      echo '
1431                      <script type="text/javascript">
1432                          <!--
1433                          '.$js_focus.'.focus();
1434                          // -->

1435                      </script>';
1436                  }
1437              }
1438      }
1439  }
1440  
1441  
1442  /*

1443   * Diplay mode payload:

1444   */
1445  switch( $fm_mode )
1446  {
1447      case 'file_copy':
1448      case 'file_move':
1449          // CMR dialog:

1450          $AdminUI->disp_view( 'files/views/_file_copy_move.form.php' );
1451          break;
1452  
1453      case 'link_item':
1454          // Links dialog:

1455          $AdminUI->disp_view( 'files/views/_file_links.view.php' );
1456          break;
1457  }
1458  
1459  
1460  // -------------------

1461  // Browsing interface:

1462  // -------------------

1463  // Display VIEW:

1464  $AdminUI->disp_view( 'files/views/_file_browse.view.php' );
1465  
1466  
1467  // End payload block:

1468  $AdminUI->disp_payload_end();
1469  
1470  // Display body bottom, debug info and close </html>:

1471  $AdminUI->disp_global_footer();
1472  
1473  
1474  /*

1475   * $Log: files.ctrl.php,v $

1476   * Revision 1.5  2007/11/01 01:41:00  fplanque

1477   * fixed : dir creation was losing item_ID

1478   *

1479   * Revision 1.4  2007/09/26 23:32:39  fplanque

1480   * upload context saving

1481   *

1482   * Revision 1.3  2007/09/26 21:53:24  fplanque

1483   * file manager / file linking enhancements

1484   *

1485   * Revision 1.2  2007/06/26 02:19:47  fplanque

1486   * fix

1487   *

1488   * Revision 1.1  2007/06/25 10:59:52  fplanque

1489   * MODULES (refactored MVC)

1490   *

1491   * Revision 1.59  2007/06/16 19:40:18  blueyed

1492   * Added handling of $zipfile->error

1493   *

1494   * Revision 1.58  2007/04/26 00:11:13  fplanque

1495   * (c) 2007

1496   *

1497   * Revision 1.57  2007/03/20 07:39:08  fplanque

1498   * filemanager fixes, including the chmod octal stuff

1499   *

1500   * Revision 1.56  2007/03/07 04:50:04  fplanque

1501   * fixed perm checks

1502   *

1503   * Revision 1.55  2007/01/25 05:09:06  fplanque

1504   * i18n update

1505   *

1506   * Revision 1.54  2007/01/25 03:17:00  fplanque

1507   * visual cleanup for average users

1508   * geeky stuff preserved as options

1509   *

1510   * Revision 1.53  2007/01/25 02:41:10  fplanque

1511   * refactoring / decrap

1512   *

1513   * Revision 1.52  2007/01/24 13:44:56  fplanque

1514   * cleaned up upload

1515   *

1516   * Revision 1.51  2007/01/24 06:31:41  fplanque

1517   * decrap refactoring

1518   *

1519   * Revision 1.50  2007/01/24 05:57:55  fplanque

1520   * cleanup / settings

1521   *

1522   * Revision 1.49  2007/01/24 03:45:29  fplanque

1523   * decrap / removed a lot of bloat...

1524   *

1525   * Revision 1.48  2007/01/24 02:35:42  fplanque

1526   * refactoring

1527   *

1528   * Revision 1.47  2007/01/24 01:40:15  fplanque

1529   * Upload tab now stays in context

1530   *

1531   * Revision 1.46  2007/01/07 05:28:15  fplanque

1532   * i18n wording

1533   *

1534   * Revision 1.45  2006/12/24 00:52:57  fplanque

1535   * Make posts with images - Proof of concept

1536   *

1537   * Revision 1.44  2006/12/23 22:53:11  fplanque

1538   * extra security

1539   *

1540   * Revision 1.43  2006/12/22 01:09:30  fplanque

1541   * cleanup

1542   *

1543   * Revision 1.42  2006/12/22 00:58:02  fplanque

1544   * fix

1545   *

1546   * Revision 1.40  2006/12/22 00:50:33  fplanque

1547   * improved path cleaning

1548   *

1549   * Revision 1.39  2006/12/12 19:39:07  fplanque

1550   * enhanced file links / permissions

1551   *

1552   * Revision 1.38  2006/12/07 23:13:10  fplanque

1553   * @var needs to have only one argument: the variable type

1554   * Otherwise, I can't code!

1555   *

1556   * Revision 1.37  2006/12/07 20:03:32  fplanque

1557   * Woohoo! File editing... means all skin editing.

1558   *

1559   * Revision 1.36  2006/12/07 15:23:42  fplanque

1560   * filemanager enhanced, refactored, extended to skins directory

1561   *

1562   * Revision 1.35  2006/11/24 18:27:23  blueyed

1563   * Fixed link to b2evo CVS browsing interface in file docblocks

1564   */
1565  ?>


Généré le : Thu Nov 29 23:58:50 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics