eZ Publish  [4.0]
ezwebdavfileserver.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // This is the index_webdav.php file. Manages WebDAV sessions.
00004 //
00005 // Created on: <18-Aug-2003 15:15:15 bh>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*!
00032   \class eZWebDAVFileServer ezwebdavfileserver.php
00033   \ingroup eZWebDAV
00034   \brief A simple file based WebDAV server
00035 
00036   Enables local file administration/management through the WebDAV interface.
00037 
00038   Usage:
00039   \code
00040   $myserver = new eZWebDAVFileServer();
00041   $myserver->processClientRequest();
00042   \endcode
00043 */
00044 
00045 //include_once( "lib/ezwebdav/classes/ezwebdavserver.php" );
00046 //include_once( "lib/ezutils/classes/ezmimetype.php" );
00047 
00048 // Get and return the files/dir-names that reside at a given path.
00049 function getDirEntries( $targetPath )
00050 {
00051     $files = array();
00052 
00053     // Attempt to open the target dir for listing.
00054     if ( $handle = opendir( $targetPath ) )
00055     {
00056         // For all entries in target dir: get filename.
00057         while ( false !== ( $file = readdir( $handle ) ) )
00058         {
00059             if ( $file != "." && $file != ".." )
00060             {
00061                 $files[] = $file;
00062             }
00063         }
00064         closedir( $handle );
00065     }
00066     // Else: unable to open the dir for listing, bail out...
00067     else
00068     {
00069         return false;
00070     }
00071 
00072     // Return array of filenames.
00073     return $files;
00074 }
00075 
00076 // Recursively copies the contents of a directory.
00077 function copyDir( $source, $destination )
00078 {
00079     // Attempt to create destination dir.
00080     $status = eZDir::mkdir( $destination );
00081 
00082     // If no success: bail out.
00083     if ( !$status )
00084     {
00085         return false;
00086     }
00087 
00088     // Get the contents of the directory.
00089     $entries = getDirEntries( $source );
00090 
00091     // Bail if contents is unavailable.
00092     if ( $entries == false )
00093     {
00094         return false;
00095     }
00096     // Else: contents is OK:
00097     else
00098     {
00099         // Copy each and every entry:
00100         foreach ( $entries as $entry )
00101         {
00102             if ( $entry )
00103             {
00104                 $from = "$source/$entry";
00105                 $to   = "$destination/$entry";
00106 
00107                 // Is this a directory? -> special case.
00108                 if ( is_dir( $from ) )
00109                 {
00110                     $status = copyDir( $from, $to );
00111                     if (!$status)
00112                     {
00113                         return false;
00114                     }
00115                 }
00116                 // Else: simple file case.
00117                 else
00118                 {
00119                     $status = copy( $from, $to );
00120                     if (!$status)
00121                     {
00122                         return false;
00123                     }
00124                 }
00125             }
00126         }
00127 
00128     }
00129 
00130     // Phew: if we got this far then everything is OK.
00131     return true;
00132 }
00133 
00134 // Recursively deletes the contents of a directory.
00135 function delDir( $dir )
00136 {
00137     // Attempt to open the target dir.
00138     $currentDir = opendir( $dir );
00139 
00140     // Bail if unable to open dir.
00141     if ( $currentDir == false )
00142     {
00143         return false;
00144     }
00145     // Else, dir is available, do the thing:
00146     else
00147     {
00148         // For all entires in the dir:
00149         while ( false !== ( $entry = readdir( $currentDir ) ) )
00150         {
00151             // If entry is a directory and not . && .. :
00152             if ( is_dir( "$dir/$entry" ) and
00153                  ( $entry != "." and $entry!="..") )
00154             {
00155                 // Delete the dir.
00156                 $status = deldir( "${dir}/${entry}" );
00157 
00158                 // Bail if unable to delete the dir.
00159                 if ( !$status )
00160                 {
00161                     return false;
00162                 }
00163             }
00164             // Else: not dir but plain file.
00165             elseif ( $entry != "." and $entry != ".." )
00166             {
00167                 // Simply unlink the file.
00168                 $status = unlink( "${dir}/${entry}" );
00169 
00170                 // Bail if unable to delete the file.
00171                 if ( !$status )
00172                 {
00173                     return false;
00174                 }
00175             }
00176         }
00177     }
00178 
00179     // We're finished going through the contents of the target dir.
00180     closedir( $currentDir );
00181 
00182     // Attempt to remove the target dir itself & return status (should be
00183     // OK as soon as we get this far...
00184     $status = rmdir( ${dir} );
00185 
00186     return $status;
00187 }
00188 
00189 /* getFileInfo
00190    Gathers information about a specific file,
00191    stores it in an associative array and returns it.
00192 */
00193 function getFileInfo( $dir, $file )
00194 {
00195     append_to_log( "inside getFileInfo, dir: $dir, file: $file");
00196     $realPath = $dir.'/'.$file;
00197     $fileInfo = array();
00198 
00199     $fileInfo["name"] = $file;
00200 
00201     // If the file is a directory:
00202     if ( is_dir( $realPath ) )
00203     {
00204         $fileInfo["size"]     = 0;
00205         $fileInfo["mimetype"] = "httpd/unix-directory";
00206 
00207         // Get the dir's creation & modification times.
00208         $fileInfo["ctime"] = filectime( $realPath.'/.' );
00209         $fileInfo["mtime"] = filemtime( $realPath.'/.' );
00210 
00211     }
00212     // Else: The file is an actual file (not a dir):
00213     else
00214     {
00215         // Get the file's creation & modification times.
00216         $fileInfo["ctime"] = filectime( $realPath );
00217         $fileInfo["mtime"] = filemtime( $realPath );
00218 
00219         // Get the file size (bytes).
00220         $fileInfo["size"] = filesize( $realPath );
00221 
00222         // Check if the filename exists and is readable:
00223         if ( is_readable( $realPath ) )
00224         {
00225             // Attempt to get & set the MIME type.
00226             $mimeInfo = eZMimeType::findByURL( $dir . '/' . $file );
00227             $fileInfo['mimetype'] = $mimeInfo['name'];
00228         }
00229         // Non-readable? -> MIME type fallback to 'application/x-non-readable'
00230         else
00231         {
00232             $fileInfo["mimetype"] = "application/x-non-readable";
00233         }
00234      }
00235 
00236     // Return the array (hopefully containing correct info).
00237     return $fileInfo;
00238 }
00239 
00240 
00241 class eZWebDAVFileServer extends eZWebDAVServer
00242 {
00243     function eZWebDAVFileServer()
00244     {
00245         $this->eZWebDAVServer();
00246     }
00247 
00248     /*!
00249      \reimp
00250      Returns if the file \a $target exists or not
00251     */
00252     function head( $target )
00253     {
00254         // Make real path.
00255         $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
00256 
00257         append_to_log( "HEAD: realPath is $realPath");
00258 
00259         // Check if the target file/dir really exists:
00260         if ( file_exists( $realPath ) )
00261         {
00262             return eZWebDAVServer::OK_CREATED;
00263         }
00264         else
00265         {
00266             return eZWebDAVServer::FAILED_NOT_FOUND;
00267         }
00268     }
00269 
00270     /*!
00271      \reimp
00272      Renames the temp file \a $tempFile to \a $target.
00273     */
00274     function put( $target, $tempFile )
00275     {
00276         // Make real path.
00277         $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
00278 
00279         append_to_log( "PUT: realPath is $realPath" );
00280         append_to_log( "PUT: tempfile is $tempFile" );
00281 
00282         // Attempt to move the file from temp to desired location.
00283         //include_once( 'lib/ezfile/classes/ezfile.php' );
00284         eZFile::rename( $tempFile, $realPath );
00285 
00286         // Check status & return corresponding code:
00287         if ( $status )
00288         {
00289             append_to_log( "move of tempfile was OK" );
00290             return eZWebDAVServer::OK_CREATED;
00291         }
00292         else
00293         {
00294             append_to_log( "move of tempfile FAILED" );
00295             return eZWebDAVServer::FAILED_FORBIDDEN;
00296         }
00297     }
00298 
00299     /*!
00300      \reimp
00301      \return An information structure with the filename.
00302     */
00303     function get( $target )
00304     {
00305         $result         = array();
00306         $result["data"] = false;
00307         $result["file"] = false;
00308 
00309         // Set the file.
00310         $result["file"] = $_SERVER["DOCUMENT_ROOT"] . $target;
00311 
00312         append_to_log( "GET: file is ".$result["file"]);
00313 
00314         return $result;
00315     }
00316 
00317     /*!
00318      \reimp
00319      Creates the directory \a $target
00320     */
00321     function mkcol( $target )
00322     {
00323         // Make real path.
00324         $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
00325 
00326         append_to_log( "attempting to create dir: $realPath" );
00327 
00328         // Proceed only if the dir/file-name doesn't exist:
00329         if ( !file_exists( $realPath ) )
00330         {
00331             // Attempt to create the directory.
00332             $status = mkdir( $realPath );
00333 
00334             // Check status:
00335             if ( $status )
00336             {
00337                 // OK:
00338                 return eZWebDAVServer::OK_CREATED;
00339             }
00340             else
00341             {
00342                 // No deal.
00343                 return eZWebDAVServer::FAILED_FORBIDDEN;
00344             }
00345         }
00346         // Else: a dir/file with that name already exists:
00347         else
00348         {
00349             return eZWebDAVServer::FAILED_EXISTS;
00350         }
00351     }
00352 
00353     /*!
00354      \reimp
00355      Removes the directory or file \a $target
00356     */
00357     function delete( $target )
00358     {
00359         // Make real path.
00360         $realPath = $_SERVER["DOCUMENT_ROOT"] . $target;
00361 
00362         append_to_log( "attempting to DELETE: $realPath" );
00363 
00364         // Check if the file actually exists (NULL compliance).
00365         if ( file_exists( $realPath ) )
00366         {
00367             append_to_log( "File/dir exists..." );
00368 
00369             if ( is_dir( $realPath ) )
00370             {
00371                 // Attempt to remove the target directory.
00372                 $status = delDir( $realPath );
00373             }
00374             else
00375             {
00376                 append_to_log( "File is a file..." );
00377 
00378                 // Attempt to remove the file.
00379                 $status = unlink( $realPath );
00380             }
00381 
00382             // Check the return code:
00383             if ( $status )
00384             {
00385                 append_to_log( "delete was OK" );
00386                 return eZWebDAVServer::OK;
00387             }
00388             else
00389             {
00390                 append_to_log( "delete FAILED" );
00391                 return eZWebDAVServer::FAILED_FORBIDDEN;
00392             }
00393         }
00394         else
00395         {
00396             return eZWebDAVServer::FAILED_NOT_FOUND;
00397         }
00398     }
00399 
00400     /*!
00401      \reimp
00402      Moves the file or directory \a $source to \a $destination by trying to rename it.
00403     */
00404     function move( $source, $destination )
00405     {
00406         append_to_log( "Source: $source   Destination: $destination" );
00407 
00408         // Make real path to source and destination.
00409         $realSource      = $_SERVER["DOCUMENT_ROOT"] . $source;
00410         $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
00411 
00412         append_to_log( "RealSource: $realSource   RealDestination: $realDestination" );
00413         //include_once( 'lib/ezfile/classes/ezfile.php' );
00414         $status = eZFile::rename( $realSource, $realDestination );
00415 
00416         if ( $status )
00417         {
00418             append_to_log( "move was OK" );
00419             return eZWebDAVServer::OK_CREATED;
00420         }
00421         else
00422         {
00423             append_to_log( "move FAILED" );
00424             return eZWebDAVServer::FAILED_CONFLICT;
00425         }
00426     }
00427 
00428     /*!
00429      \reimp
00430      Copies the file or directory \a $source to \a $destination.
00431     */
00432     function copy( $source, $destination )
00433     {
00434         append_to_log( "Source: $source   Destination: $destination" );
00435         ob_start(); var_dump( $_SERVER ); $m = ob_get_contents(); ob_end_clean(); append_to_log( $m );
00436 
00437         // Make real path to source and destination.
00438         $realSource      = $_SERVER["DOCUMENT_ROOT"] . $source;
00439         $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
00440 
00441         append_to_log( "RealSource: $realSource   RealDestination: $realDestination" );
00442         $status = copyDir( $realSource, $realDestination );
00443 
00444         if ( $status )
00445         {
00446             append_to_log( "copy was OK" );
00447             return eZWebDAVServer::OK_CREATED;
00448         }
00449         else
00450         {
00451             append_to_log( "copy FAILED" );
00452             return eZWebDAVServer::FAILED_CONFLICT;
00453         }
00454     }
00455 
00456     /*!
00457      \reimp
00458      Finds all files and directories in the directory \a $dir and return an element list of it.
00459     */
00460     function getCollectionContent( $dir, $depth = false, $properties = false )
00461     {
00462         $directory = dirname( $_SERVER['SCRIPT_FILENAME'] ) . $dir;
00463 
00464         $files  = array();
00465 
00466         append_to_log( "inside getDirectoryContent, dir: $directory" );
00467         $handle = opendir( $directory );
00468 
00469         // For all the entries in the directory:
00470         while ( false !== ( $filename = readdir( $handle ) ) )
00471         {
00472             // Skip current and parent dirs ('.' and '..').
00473             if ( $filename == '.' or $filename == '..' )
00474                 continue;
00475             $files[] = getFileInfo( $directory, $filename );
00476             append_to_log( "inside getDirectoryContent, dir: $directory, fil: $filename" );
00477         }
00478         return $files;
00479     }
00480 }
00481 ?>