FilemanagerAction.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002-2004 Jan Dankert, cms@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 
00022 
00029 class FilemanagerAction extends ObjectAction
00030 {
00031      var $command;
00032      var $resourceType;
00033      
00039      var $currentFolder;
00040      
00046      var $folder;
00047      
00051      function FilemanagerAction()
00052      {
00053           // PHP-Fehler ins Log schreiben, damit die Ausgabe nicht zerstört wird.
00054           set_error_handler('filemanagerErrorHandler',E_ALL & ~E_NOTICE);
00055 
00056           // Get the main request information.
00057           $this->command       = $this->getRequestVar('Command'      );
00058           $this->resourceType  = $this->getRequestVar('Type'         );
00059           $this->currentFolder = $this->getRequestVar('CurrentFolder');
00060 
00061           // Check if it is an allowed type.
00062           if ( !in_array( $this->resourceType, array('File','Image','Flash','Media') ) )
00063           {
00064                $this->sendErrorDocument(1,'unknown resource type');
00065                exit;
00066           }
00067      
00068           // Check the current folder syntax (must begin and end with a slash).
00069           if ( ! ereg( '/$', $this->currentFolder ) ) $this->currentFolder .= '/' ;
00070           if ( strpos( $this->currentFolder, '/' ) !== 0 ) $this->currentFolder = '/' . $this->currentFolder;      
00071           
00072           $this->investigateCurrentFolder();
00073      }
00074 
00075      
00080      function investigateCurrentFolder()
00081      {
00082           $project = Session::getProject();
00083           $folderid = $project->getRootObjectId();
00084           $this->folder = new Folder( $folderid );
00085           $parts = explode('/',$this->currentFolder);
00086 
00087           foreach( $parts as $part )
00088           {
00089                if   ( empty($part) )
00090                     continue;
00091 
00092                $oid = $this->folder->getObjectIdByFileName($part);
00093                
00094                if   ( !$this->folder->available($oid) )
00095                     $this->sendErrorDocument(102,"currentFolder is invalid (no folder inside): "+$this->currentFolder);
00096                
00097                $this->folder = new Folder($oid);
00098 
00099                if   ( ! $this->folder->isFolder )
00100                     $this->sendErrorDocument(102,"currentFolder is invalid (not a folder): "+$this->currentFolder);
00101           }
00102      }
00103 
00104      
00105      
00110      function show()
00111      {
00112           Logger::debug('Filemanager: '.getenv('REQUEST_URI'));
00113           Logger::debug($this->command);
00114           Logger::debug($this->resourceType);
00115           Logger::debug($this->currentFolder);
00116           Logger::debug($this->folder->objectid);
00117 
00118           // File Upload doesn't have to Return XML, so it must be intercepted before anything.
00119           if ( $this->command == 'FileUpload' )
00120           {
00121                $this->fileUpload() ;
00122                return ;
00123           }
00124           Logger::debug("Start");
00125           
00126           $this->setXmlHeaders();
00127           $this->createXmlHeader();
00128           
00129           // Execute the required command.
00130           switch ( $this->command )
00131           {
00132                case 'GetFolders':
00133                     $this->getFolders() ;
00134                     break ;
00135                     
00136                case 'GetFoldersAndFiles':
00137                     $this->getFoldersAndFiles() ;
00138                     break ;
00139                     
00140                case 'CreateFolder':
00141                     $this->createFolder() ;
00142                     break ;
00143                     
00144                default:
00145                     Logger::warn('Unknown Filemanager-Command: '.$this->command);
00146                     trigger_error('Unknown Command: '.$this->command);
00147                     $this->sendError( 1,"unknown command: ".$this->command ) ;
00148           }
00149           Logger::debug("ok");
00150 
00151           $this->createXmlFooter();
00152           exit;
00153      }
00154      
00155      
00156      function setXmlHeaders()
00157      {
00158           // Prevent the browser from caching the result.
00159           // Date in the past
00160           header("Expires: ".gmdate("D, d M Y H:i:s")." GMT");
00161           // always modified
00162           header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
00163           
00164           // HTTP/1.1
00165           header('Cache-Control: no-store, no-cache, must-revalidate') ;
00166           header('Cache-Control: post-check=0, pre-check=0', false) ;
00167 
00168           // HTTP/1.0
00169           header('Pragma: no-cache') ;
00170      }
00171 
00172 
00176      function createXmlHeader()
00177      {
00178           // Set the response format.
00179           header( 'Content-Type:text/xml' ) ;
00180 
00181           echo '<?xml version="1.0" encoding="utf-8" ?>' ;
00182           
00183           // Create the main "Connector" node.
00184           echo '<Connector command="' . $this->command . '" resourceType="' . $this->resourceType . '">' ;
00185           
00186           // Add the current folder node.
00187           echo '<CurrentFolder path="' . convertToXmlAttribute($this->currentFolder).'" url="'.convertToXmlAttribute($this->currentFolder) . '" />' ;
00188      }
00189 
00190      
00191      
00192      function createXmlFooter()
00193      {
00194           echo '</Connector>' ;
00195      }
00196      
00197 
00198      
00205      function sendError( $number, $text )
00206      {
00207           echo '<Error number="' . $number . '" text="' . htmlspecialchars( $text ) . '" />' ;
00208      }
00209      
00210      
00217      function sendErrorDocument( $number, $text )
00218      {
00219           $this->createXmlHeader();
00220           $this->sendError( $number, $text );
00221           $this->createXmlFooter();
00222           exit ;
00223      }
00224      
00225      
00230      function getFolders()
00231      {
00232           echo "<Folders>" ;
00233 
00234           foreach( $this->folder->getSubfolderFilenames() as $id=>$name )
00235                echo '<Folder name="'. convertToXmlAttribute($name).'" />';
00236      
00237           echo "</Folders>" ;
00238      }
00239      
00240      
00245      function getFoldersAndFiles()
00246      {
00247           echo '<Folders>' ;
00248 
00249           foreach( $this->folder->getSubfolderFilenames() as $id=>$name )
00250                echo '<Folder name="'. convertToXmlAttribute($name).'" />';
00251                
00252           echo '</Folders>' ;
00253           echo '<Files>' ;
00254 
00255           foreach( $this->folder->getFileFilenames() as $id=>$name )
00256                echo '<File name="' . convertToXmlAttribute( $name ).'" url="'.convertToXmlAttribute( Html::url('file','show',$id,array('oid'=>'__OID__'.$id.'__') ) ).'" size="' . '1' . '" />' ;
00257      
00258           echo '</Files>' ;
00259      }
00260      
00261      
00266      function createFolder()
00267      {    
00268           // Possible Error Numbers are: 
00269           //   0 : No Errors Found. The folder has been created. 
00270           // 101 : Folder already exists. 
00271           // 102 : Invalid folder name. 
00272           // 103 : You have no permissions to create the folder. 
00273           // 110 : Unknown error creating folder.
00274           
00275           $filename = $this->getRequestVar('NewFolderName');
00276           
00277           if ( empty($filename) )
00278           {
00279                $this->sendError(102,'missing name for new folder.');
00280           }
00281           elseif( !$this->folder->hasRight(ACL_CREATE_FOLDER) )
00282           {
00283                $this->sendError(103,'You have no permissions to create the folder.');
00284           }
00285           elseif( $this->folder->hasFilename( $filename ) )
00286           {
00287                $this->sendError(101,'Folder already exists.');
00288           }
00289           else
00290           {
00291                $newFolder = new Folder();
00292                $newFolder->parentid = $this->folder->objectid;
00293                $newFolder->filename = $filename;
00294                $newFolder->name     = $filename;
00295                
00296                $newFolder->add();
00297                
00298                $this->sendError(0,"OK");
00299           }
00300      }
00301      
00302      
00303 
00308      function fileUpload()
00309      {
00310           $upload = new Upload('NewFile');
00311           
00312           // From FCK-Editor-Doc:
00313           // The "OnUploadCompleted" is a JavaScript function that is called to expose the upload result. The possible values are: 
00314           // OnUploadCompleted( 0 ) : no errors found on the upload process. 
00315           // OnUploadCompleted( 1, , , 'Reason' ) : the upload filed because of "Reason". 
00316           // OnUploadCompleted( 201, ,'FileName(1).ext' ) : the file has been uploaded successfully, but its name has been changed to "FileName(1).ext". 
00317           // OnUploadCompleted( 202 ) : invalid file.
00318           if   ( !$upload->isValid() )
00319           {
00320                $errorNr   = 202;
00321                $errorText = 'Upload failed, reason: '.$upload->error; 
00322           }
00323           else
00324           {
00325                $file = new File();
00326                $file->parentid = $this->folder->objectid;
00327                $file->filename = $upload->filename;
00328                $file->value    = $upload->value;
00329                $file->add();
00330 
00331                $errorNr   = 0;
00332                $errorText = $file->filename; 
00333           }
00334      
00335           echo '<script type="text/javascript">' ;
00336           echo 'window.parent.frames["frmUpload"].OnUploadCompleted(' .$errorNr.',"' . str_replace( '"', '\\"', $errorText ) . '") ;' ;
00337           echo '</script>' ;
00338      }
00339      
00340 }
00341 
00342 
00348 function filemanagerErrorHandler($errno, $errstr, $errfile, $errline) 
00349 {
00350      Logger::warn('FCKEDITOR FILEMANAGER ERROR: '.$errno.'/'.$errstr.'/file:'.$errfile.'/line:'.$errline);
00351 
00352      // Wir teilen dem Client mit, dass auf dem Server was schief gelaufen ist. 
00353      Http::serverError('Filemanager failed with: '.$errno.'/'.$errstr.'/file:'.$errfile.'/line:'.$errline);
00354 }
00355 
00356 ?>

Erzeugt am Thu May 14 00:55:48 2009 für OpenRat von  doxygen 1.5.8