[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/ezwebdav/classes/ -> ezwebdavfileserver.php (source)

   1  <?php
   2  //
   3  // This is the index_webdav.php file. Manages WebDAV sessions.
   4  //
   5  // Created on: <18-Aug-2003 15:15:15 bh>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZWebDAVFileServer ezwebdavfileserver.php
  31    \ingroup eZWebDAV
  32    \brief A simple file based WebDAV server
  33  
  34    Enables local file administration/management through the WebDAV interface.
  35  
  36    Usage:
  37    \code
  38    $myserver = new eZWebDAVFileServer();
  39    $myserver->processClientRequest();
  40    \endcode
  41  */
  42  
  43  include_once ( "lib/ezwebdav/classes/ezwebdavserver.php" );
  44  include_once ( "lib/ezutils/classes/ezmimetype.php" );
  45  
  46  // Get and return the files/dir-names that reside at a given path.
  47  function getDirEntries( $targetPath )
  48  {
  49      $files = array();
  50  
  51      // Attempt to open the target dir for listing.
  52      if ( $handle = opendir( $targetPath ) )
  53      {
  54          // For all entries in target dir: get filename.
  55          while ( false !== ( $file = readdir( $handle ) ) )
  56          {
  57              if ( $file != "." && $file != ".." )
  58              {
  59                  $files[] = $file;
  60              }
  61          }
  62          closedir( $handle );
  63      }
  64      // Else: unable to open the dir for listing, bail out...
  65      else
  66      {
  67          return false;
  68      }
  69  
  70      // Return array of filenames.
  71      return $files;
  72  }
  73  
  74  // Recursively copies the contents of a directory.
  75  function copyDir( $source, $destination )
  76  {
  77      // Attempt to create destination dir.
  78      $status = mkdir( $destination );
  79  
  80      // If no success: bail out.
  81      if ( !$status )
  82      {
  83          return false;
  84      }
  85  
  86      // Get the contents of the directory.
  87      $entries = getDirEntries( $source );
  88  
  89      // Bail if contents is unavailable.
  90      if ( $entries == false )
  91      {
  92          return false;
  93      }
  94      // Else: contents is OK:
  95      else
  96      {
  97          // Copy each and every entry:
  98          foreach ( $entries as $entry )
  99          {
 100              if ( $entry )
 101              {
 102                  $from = "$source/$entry";
 103                  $to   = "$destination/$entry";
 104  
 105                  // Is this a directory? -> special case.
 106                  if ( is_dir( $from ) )
 107                  {
 108                      $status = copyDir( $from, $to );
 109                      if (!$status)
 110                      {
 111                          return false;
 112                      }
 113                  }
 114                  // Else: simple file case.
 115                  else
 116                  {
 117                      $status = copy( $from, $to );
 118                      if (!$status)
 119                      {
 120                          return false;
 121                      }
 122                  }
 123              }
 124          }
 125  
 126      }
 127  
 128      // Phew: if we got this far then everything is OK.
 129      return true;
 130  }
 131  
 132  // Recursively deletes the contents of a directory.
 133  function delDir( $dir )
 134  {
 135      // Attempt to open the target dir.
 136      $currentDir = opendir( $dir );
 137  
 138      // Bail if unable to open dir.
 139      if ( $currentDir == false )
 140      {
 141          return false;
 142      }
 143      // Else, dir is available, do the thing:
 144      else
 145      {
 146          // For all entires in the dir:
 147          while ( false !== ( $entry = readdir( $currentDir ) ) )
 148          {
 149              // If entry is a directory and not . && .. :
 150              if ( is_dir( "$dir/$entry" ) and
 151                   ( $entry != "." and $entry!="..") )
 152              {
 153                  // Delete the dir.
 154                  $status = deldir( "$dir}/$entry}" );
 155  
 156                  // Bail if unable to delete the dir.
 157                  if ( !$status )
 158                  {
 159                      return false;
 160                  }
 161              }
 162              // Else: not dir but plain file.
 163              elseif ( $entry != "." and $entry != ".." )
 164              {
 165                  // Simply unlink the file.
 166                  $status = unlink( "$dir}/$entry}" );
 167  
 168                  // Bail if unable to delete the file.
 169                  if ( !$status )
 170                  {
 171                      return false;
 172                  }
 173              }
 174          }
 175      }
 176  
 177      // We're finished going through the contents of the target dir.
 178      closedir( $currentDir );
 179  
 180      // Attempt to remove the target dir itself & return status (should be
 181      // OK as soon as we get this far...
 182      $status = rmdir( $dir} );
 183  
 184      return $status;
 185  }
 186  
 187  /* getFileInfo
 188     Gathers information about a specific file,
 189     stores it in an associative array and returns it.
 190  */
 191  function getFileInfo( $dir, $file )
 192  {
 193      append_to_log( "inside getFileInfo, dir: $dir, file: $file");
 194      $realPath = $dir.'/'.$file;
 195      $fileInfo = array();
 196  
 197      $fileInfo["name"] = $file;
 198  
 199      // If the file is a directory:
 200      if ( is_dir( $realPath ) )
 201      {
 202          $fileInfo["size"]     = 0;
 203          $fileInfo["mimetype"] = "httpd/unix-directory";
 204  
 205          // Get the dir's creation & modification times.
 206          $fileInfo["ctime"] = filectime( $realPath.'/.' );
 207          $fileInfo["mtime"] = filemtime( $realPath.'/.' );
 208  
 209      }
 210      // Else: The file is an actual file (not a dir):
 211      else
 212      {
 213          // Get the file's creation & modification times.
 214          $fileInfo["ctime"] = filectime( $realPath );
 215          $fileInfo["mtime"] = filemtime( $realPath );
 216  
 217          // Get the file size (bytes).
 218          $fileInfo["size"] = filesize( $realPath );
 219  
 220          // Check if the filename exists and is readable:
 221          if ( is_readable( $realPath ) )
 222          {
 223              // Attempt to get & set the MIME type.
 224              $mimeInfo = eZMimeType::findByURL( $dir . '/' . $file );
 225              $fileInfo['mimetype'] = $mimeInfo['name'];
 226          }
 227          // Non-readable? -> MIME type fallback to 'application/x-non-readable'
 228          else
 229          {
 230              $fileInfo["mimetype"] = "application/x-non-readable";
 231          }
 232       }
 233  
 234      // Return the array (hopefully containing correct info).
 235      return $fileInfo;
 236  }
 237  
 238  
 239  class eZWebDAVFileServer extends eZWebDAVServer
 240  {
 241      function eZWebDAVFileServer()
 242      {
 243          $this->eZWebDAVServer();
 244      }
 245  
 246      /*!
 247       \reimp
 248       Returns if the file \a $target exists or not
 249      */
 250      function head( $target )
 251      {
 252          // Make real path.
 253          $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
 254  
 255          append_to_log( "HEAD: realPath is $realPath");
 256  
 257          // Check if the target file/dir really exists:
 258          if ( file_exists( $realPath ) )
 259          {
 260              return EZ_WEBDAV_OK_CREATED;
 261          }
 262          else
 263          {
 264              return EZ_WEBDAV_FAILED_NOT_FOUND;
 265          }
 266      }
 267  
 268      /*!
 269       \reimp
 270       Renames the temp file \a $tempFile to \a $target.
 271      */
 272      function put( $target, $tempFile )
 273      {
 274          // Make real path.
 275          $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
 276  
 277          append_to_log( "PUT: realPath is $realPath" );
 278          append_to_log( "PUT: tempfile is $tempFile" );
 279  
 280          // Attempt to move the file from temp to desired location.
 281          $status = rename( $tempFile, $realPath );
 282  
 283          // Check status & return corresponding code:
 284          if ( $status )
 285          {
 286              append_to_log( "move of tempfile was OK" );
 287              return EZ_WEBDAV_OK_CREATED;
 288          }
 289          else
 290          {
 291              append_to_log( "move of tempfile FAILED" );
 292              return EZ_WEBDAV_FAILED_FORBIDDEN;
 293          }
 294      }
 295  
 296      /*!
 297       \reimp
 298       \return An information structure with the filename.
 299      */
 300      function get( $target )
 301      {
 302          $result         = array();
 303          $result["data"] = false;
 304          $result["file"] = false;
 305  
 306          // Set the file.
 307          $result["file"] = $_SERVER["DOCUMENT_ROOT"] . $target;
 308  
 309          append_to_log( "GET: file is ".$result["file"]);
 310  
 311          return $result;
 312      }
 313  
 314      /*!
 315       \reimp
 316       Creates the directory \a $target
 317      */
 318      function mkcol( $target )
 319      {
 320          // Make real path.
 321          $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
 322  
 323          append_to_log( "attempting to create dir: $realPath" );
 324  
 325          // Proceed only if the dir/file-name doesn't exist:
 326          if ( !file_exists( $realPath ) )
 327          {
 328              // Attempt to create the directory.
 329              $status = mkdir( $realPath );
 330  
 331              // Check status:
 332              if ( $status )
 333              {
 334                  // OK:
 335                  return EZ_WEBDAV_OK_CREATED;
 336              }
 337              else
 338              {
 339                  // No deal.
 340                  return EZ_WEBDAV_FAILED_FORBIDDEN;
 341              }
 342          }
 343          // Else: a dir/file with that name already exists:
 344          else
 345          {
 346              return EZ_WEBDAV_FAILED_EXISTS;
 347          }
 348      }
 349  
 350      /*!
 351       \reimp
 352       Removes the directory or file \a $target
 353      */
 354      function delete( $target )
 355      {
 356          // Make real path.
 357          $realPath = $_SERVER["DOCUMENT_ROOT"] . $target;
 358  
 359          append_to_log( "attempting to DELETE: $realPath" );
 360  
 361          // Check if the file actually exists (NULL compliance).
 362          if ( file_exists( $realPath ) )
 363          {
 364              append_to_log( "File/dir exists..." );
 365  
 366              if ( is_dir( $realPath ) )
 367              {
 368                  // Attempt to remove the target directory.
 369                  $status = delDir( $realPath );
 370              }
 371              else
 372              {
 373                  append_to_log( "File is a file..." );
 374  
 375                  // Attempt to remove the file.
 376                  $status = unlink( $realPath );
 377              }
 378  
 379              // Check the return code:
 380              if ( $status )
 381              {
 382                  append_to_log( "delete was OK" );
 383                  return EZ_WEBDAV_OK;
 384              }
 385              else
 386              {
 387                  append_to_log( "delete FAILED" );
 388                  return EZ_WEBDAV_FAILED_FORBIDDEN;
 389              }
 390          }
 391          else
 392          {
 393              return EZ_WEBDAV_FAILED_NOT_FOUND;
 394          }
 395      }
 396  
 397      /*!
 398       \reimp
 399       Moves the file or directory \a $source to \a $destination by trying to rename it.
 400      */
 401      function move( $source, $destination )
 402      {
 403          append_to_log( "Source: $source   Destination: $destination" );
 404  
 405          // Make real path to source and destination.
 406          $realSource      = $_SERVER["DOCUMENT_ROOT"] . $source;
 407          $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
 408  
 409          append_to_log( "RealSource: $realSource   RealDestination: $realDestination" );
 410          $status = rename( $realSource, $realDestination );
 411  
 412          if ( $status )
 413          {
 414              append_to_log( "move was OK" );
 415              return EZ_WEBDAV_OK_CREATED;
 416          }
 417          else
 418          {
 419              append_to_log( "move FAILED" );
 420              return EZ_WEBDAV_FAILED_CONFLICT;
 421          }
 422      }
 423  
 424      /*!
 425       \reimp
 426       Copies the file or directory \a $source to \a $destination.
 427      */
 428      function copy( $source, $destination )
 429      {
 430          append_to_log( "Source: $source   Destination: $destination" );
 431          ob_start(); var_dump( $_SERVER ); $m = ob_get_contents(); ob_end_clean(); append_to_log( $m );
 432  
 433          // Make real path to source and destination.
 434          $realSource      = $_SERVER["DOCUMENT_ROOT"] . $source;
 435          $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
 436  
 437          append_to_log( "RealSource: $realSource   RealDestination: $realDestination" );
 438          $status = copyDir( $realSource, $realDestination );
 439  
 440          if ( $status )
 441          {
 442              append_to_log( "copy was OK" );
 443              return EZ_WEBDAV_OK_CREATED;
 444          }
 445          else
 446          {
 447              append_to_log( "copy FAILED" );
 448              return EZ_WEBDAV_FAILED_CONFLICT;
 449          }
 450      }
 451  
 452      /*!
 453       \reimp
 454       Finds all files and directories in the directory \a $dir and return an element list of it.
 455      */
 456      function getCollectionContent( $dir )
 457      {
 458          $directory = dirname( $_SERVER["PATH_TRANSLATED"] ) . $dir;
 459  
 460          $files  = array();
 461  
 462          append_to_log( "inside getDirectoryContent, dir: $directory" );
 463          $handle = opendir( $directory );
 464  
 465          // For all the entries in the directory:
 466          while ( false !== ( $filename = readdir( $handle ) ) )
 467          {
 468              // Skip current and parent dirs ('.' and '..').
 469              if ( $filename == '.' or $filename == '..' )
 470                  continue;
 471              $files[] = getFileInfo( $directory, $filename );
 472              append_to_log( "inside getDirectoryContent, dir: $directory, fil: $filename" );
 473          }
 474          return $files;
 475      }
 476  }
 477  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7