Back to index

moin  1.9.0~rc2
AnyWikiDraw.body.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  * @(#)AnyWikiDraw_body.php  
00004  *
00005  * Copyright (c) 2007-2009 by the original authors of AnyWikiDraw
00006  * and all its contributors.
00007  * All rights reserved.
00008  *
00009  * The copyright of this software is owned by the authors and  
00010  * contributors of the AnyWikiDraw project ("the copyright holders").
00011  * You may not use, copy or modify this software, except in  
00012  * accordance with the license agreement you entered into with  
00013  * the copyright holders. For details see accompanying license terms. 
00014  */
00015 
00031 class AnyWikiDraw extends SpecialPage {
00035        var $mUploadDescription, $mLicense, $mUploadOldVersion;
00036        var $mUploadCopyStatus, $mUploadSource, $mWatchthis;
00037        
00038        static $version = "0.13";
00039        
00040        function AnyWikiDraw() {
00041               SpecialPage::SpecialPage("AnyWikiDraw");
00042               wfLoadExtensionMessages('AnyWikiDraw');
00043 
00044               $this->mUploadDescription = '';
00045               $this->mLicense = '';
00046               $this->mUploadCopyStatus = '';
00047               $this->mUploadSource = '';
00048               $this->mWatchthis = false;
00049        }
00050        
00051        function execute( $par ) {
00052               global $wgRequest, $wgOut;
00053               
00054               if ($wgRequest->wasPosted()) {
00055                      $this->processUpload();
00056                      
00057               } else if (strlen($wgRequest->getVal("image","")) > 0) {
00058                      $this->processDownload();
00059               } else {
00060                      $this->setHeaders();
00061               
00062                      # Get request data from, e.g.
00063                      # $param = $wgRequest->getText('param');
00064                      $hasErrors = false;
00065                             
00066             # Check uploading enabled
00067             global $wgEnableUploads, $wgSitename;
00068               if( !$wgEnableUploads ) {
00069                             $hasErrors = true;
00070                      $wgOut->addWikiText(
00071                             wfMsg('anywikidraw_upload_disabled', $wgSitename)
00072                 );
00073             } 
00074 
00075             # Check cookies available to Java
00076             global $wgCookieHttpOnly;
00077               if( $wgCookieHttpOnly ) {
00078                             $hasErrors = true;
00079                             $wgOut->addWikiText(
00080                             wfMsg('anywikidraw_cookie_http_only', $wgSitename)
00081                 );
00082             } 
00083 
00084 
00085             # Check file extensions enabled
00086             global $wgFileExtensions;
00087             $requiredExtensions = array("svg",/*"svgz",*/"png","jpg");
00088             $missingExtensions = array();
00089             foreach ($requiredExtensions as $ext) {
00090                 if (! in_array($ext, $wgFileExtensions)) {
00091                     $missingExtensions[] = $ext;
00092                 }
00093             }
00094             if (count($missingExtensions) == 1) {
00095                             $hasErrors = true;
00096                      $wgOut->addWikiText(
00097                             wfMsg('anywikidraw_extension_disabled', $wgSitename, ".".implode(", .", $missingExtensions) )
00098                 );
00099             } else if (count($missingExtensions) > 1) {
00100                             $hasErrors = true;
00101                      $wgOut->addWikiText(
00102                             wfMsg('anywikidraw_extensions_disabled', $wgSitename, ".".implode(", .", $missingExtensions) )
00103                 );
00104             }
00105               
00106                      # Show information about AnyWikiDraw
00107                      if (! $hasErrors) {
00108                global $wgAnyWikiDrawVersion;
00109                             $wgOut->addWikiText(
00110                                    wfMsg('anywikidraw_about', AnyWikiDraw::$version)
00111                             );
00112                      }
00113                      // Output
00114                      // $wgOut->addHTML( $output );
00115               }
00116        }
00117        
00118        function processUpload() {
00119               global $wgRequest, $wgOut;
00120               
00121               // Fill in the form data as needed by the upload form
00122               $wgRequest->data['wpDestFile'] = $wgRequest->data['DrawingName'];
00123               $wgRequest->data['wpIgnoreWarning'] = '1';
00124               $wgRequest->data['wpDestFileWarningAck'] ='1';
00125               $wgRequest->data['wpUploadDescription'] = $wgRequest->data['UploadSummary'];
00126               $wgRequest->data['wpUploadFile'] = $wgRequest->data['DrawingData'];
00127               $_FILES['wpUploadFile'] = $_FILES['DrawingData'];
00128               $wgRequest->data['action'] = $wgRequest->data['Action'];
00129 
00130               // Upload the drawing              
00131               $form = new UploadForm($wgRequest);
00132               $details = null;
00133               $outcome = $form->internalProcessUpload($details);
00134               
00135 
00136               $drawingTempFile = $wgRequest->getFileTempName('DrawingData');
00137               $renderedTempFile = $wgRequest->getFileTempName('RenderedImageData');
00138               $imageMapTempFile =  $wgRequest->getFileTempName('ImageMapData');
00139        
00140               // If we were successful so far, look whether a rendered image of the
00141               // drawing has been uploaded as well.
00142               if ($outcome == UploadForm::SUCCESS && $renderedTempFile != null) {
00143               
00144                      $img = $form->mLocalFile; 
00145                      $thumbDir = $img->getThumbPath();
00146                      $params = array( 'width' => $img->getWidth() );
00147                      $thumbName = $img->thumbName($params);
00148                      
00149                      if ($thumbName) {
00150                             // Look at the contents of the file; if we can recognize the
00151                             // type but it's corrupt or data of the wrong type, we should
00152                             // probably not accept it.
00153                             $veri = $form->verify( $renderedTempFile, 'png' );
00154                             if ($veri) {
00155                                    // Provide an opportunity for extensions to add further checks
00156                                    $error = '';
00157                                    if( !wfRunHooks( 'UploadVerification',
00158                                           array( $thumbName, $renderedTempFile, &$error ) ) ) {
00159                                           
00160                                           $veri = false;
00161                                    }
00162                             }
00163                             
00164                             
00165                             if ($veri) {
00166                                    if (!file_exists($thumbDir)) {
00167                                           $thumbDirExists = wfMkdirParents($thumbDir);
00168                                    } else {
00169                         $thumbDirExists = true;
00170                     }
00171                     if ($thumbDirExists) {
00172                         move_uploaded_file($renderedTempFile, $thumbDir.'/'.$thumbName);
00173                     }
00174                             }
00175                      }                           
00176               }
00177               
00178               // Get rid of uploaded files
00179               if (file_exists($drawingTempFile)) { unlink($drawingTempFile); }
00180               if (file_exists($renderedTempFile)) { unlink($renderedTempFile); }
00181               if (file_exists($imageMapTempFile)) { unlink($imageMapTempFile); }
00182                             
00183        
00184               // Return outcome along with an appropriate error message to the client      
00185               switch ($outcome) {
00186                      case  UploadForm::SUCCESS :
00187                             header('HTTP/1.0 200 OK');
00188                             echo('<html><body>Success.</body></html>');
00189                             break;
00190                             
00191                      case  UploadForm::BEFORE_PROCESSING :
00192                             header('HTTP/1.0 500 Internal Server Error');
00193                             echo('<html><body>Hook UploadForm:BeforeProcessing broke processing the file.</body></html>');
00194                             break;
00195                             
00196                      case  UploadForm::LARGE_FILE_SERVER :
00197                             header('HTTP/1.0 500 Internal Server Error');
00198                             echo('<html><body>'.wfMsgHtml( 'largefileserver' ).'</body></html>');
00199                             break;
00200                             
00201                      case  UploadForm::EMPTY_FILE :
00202                             header('HTTP/1.0 400 Bad Request');
00203                             echo('<html><body>'.wfMsgHtml( 'emptyfile' ).'</body></html>');
00204                             break;
00205                             
00206                      case  UploadForm::MIN_LENGTH_PARTNAME :
00207                             header('HTTP/1.0 400 Bad Request');
00208                             echo('<html><body>'.wfMsgHtml( 'minlength1' ).'</body></html>');
00209                             break;
00210                             
00211                      case  UploadForm::ILLEGAL_FILENAME :
00212                             header('HTTP/1.0 400 Bad Request');
00213                             echo('<html><body>'.wfMsgHtml( 'illegalfilename', htmlspecialchars($wgRequest->data('DrawingName'))).'</body></html>');
00214                             break;
00215                             
00216                      case  UploadForm::PROTECTED_PAGE :
00217                             header('HTTP/1.0 403 Forbidden');
00218                             echo('<html><body>');
00219                             echo('<p>You are not allowed to change this drawing:</p>');
00220                             $this->echoDetails($details['permissionserrors']);
00221                             echo('</body></html>');
00222                             break;
00223                             
00224                      case  UploadForm::OVERWRITE_EXISTING_FILE :
00225                             header('HTTP/1.0 403 Forbidden');
00226                             echo('<html><body>You may not overwrite the existing drawing.</body></html>');
00227                             break;
00228                             
00229                      case  UploadForm::FILETYPE_MISSING :
00230                             header('HTTP/1.0 400 Bad Request');
00231                             echo('<html><body>The type of the uploaded file is not explicitly allowed.</body></html>');
00232                             break;
00233                             
00234                      case  UploadForm::FILETYPE_BADTYPE :
00235                             header('HTTP/1.0 400 Bad Request');
00236                             echo('<html><body>The type of the uploaded file is explicitly disallowed.</body></html>');
00237                             break;
00238                             
00239                      case  UploadForm::VERIFICATION_ERROR :
00240                             header('HTTP/1.0 400 Bad Request');
00241                             echo('<html><body>');
00242                             echo('<p>The uploaded file did not pass server verification.</p>');
00243                             echo('</body></html>');
00244                             break;
00245                             
00246                      case  UploadForm::UPLOAD_VERIFICATION_ERROR :
00247                             header('HTTP/1.0 403 Bad Request');
00248                             echo('<html><body>');
00249                             echo('<p>The uploaded file did not pass server verification:</p>');
00250                             $this->echoDetails($details['error']);
00251                             echo('</body></html>');
00252                             break;
00253                             
00254                      case  UploadForm::UPLOAD_WARNING :
00255                             header('HTTP/1.0 400 Bad Request');
00256                             echo('<html><body>');
00257                             echo('<p>The server issued a warning for this file:</p>');
00258                             $this->echoDetails($details['warning']);
00259                             echo('</body></html>');
00260                             break;
00261                             
00262                      case  UploadForm::INTERNAL_ERROR :
00263                             header('HTTP/1.0 500 Internal Server Error');
00264                             echo('<html><body>');
00265                             echo('<p>Function UploadForm:internalProcessUpload encountered an internal error.</p>');
00266                             echo('<p>'.$details['internal'].'</p>');
00267                             echo('</body></html>');
00268                             break;
00269                             
00270                      default :
00271                             header('HTTP/1.0 500 Internal Server Error');
00272                             echo('<html><body>Function UploadForm:internalProcessUpload returned an unknown code: '.$outcome.'.</body></html>');
00273                             break;
00274               }
00275               exit();
00276        }
00277        
00278        function echoDetails($msg) {
00279               if (is_array($msg)) {
00280                      foreach ($msg as $submsg) {
00281                             $this->echoDetails($submsg);
00282                      }
00283               } else {
00284                      echo('</p>'.$msg.'</p>');
00285               }
00286        }
00287 }
00288 ?>