/**
  * Tries to save the passed file.
  * Therefore, the following post-params should be given:
  * action = fileUpload
  * folder = the folder to store the file within
  * systemid = the filemanagers' repo-id
  * inputElement = name of the inputElement
  *
  * @return string
  * @permissions right1
  */
 protected function actionFileupload()
 {
     $strReturn = "";
     /** @var class_module_mediamanager_repo|class_module_mediamanager_file $objFile */
     $objFile = class_objectfactory::getInstance()->getObject($this->getSystemid());
     /**
      * @var class_module_mediamanager_repo
      */
     $objRepo = null;
     if ($objFile instanceof class_module_mediamanager_file) {
         $strFolder = $objFile->getStrFilename();
         if (!$objFile->rightEdit() || $objFile->getIntType() != class_module_mediamanager_file::$INT_TYPE_FOLDER) {
             class_response_object::getInstance()->setStrStatusCode(class_http_statuscodes::SC_UNAUTHORIZED);
             $strReturn .= "<message><error>" . xmlSafeString($this->getLang("commons_error_permissions")) . "</error></message>";
             return $strReturn;
         }
         $objRepo = class_objectfactory::getInstance()->getObject($objFile->getPrevId());
         while (!$objRepo instanceof class_module_mediamanager_repo) {
             $objRepo = class_objectfactory::getInstance()->getObject($objRepo->getPrevId());
         }
     } elseif ($objFile instanceof class_module_mediamanager_repo) {
         $objRepo = $objFile;
         $strFolder = $objFile->getStrPath();
         if (!$objFile->rightEdit()) {
             class_response_object::getInstance()->setStrStatusCode(class_http_statuscodes::SC_UNAUTHORIZED);
             $strReturn .= "<message><error>" . xmlSafeString($this->getLang("commons_error_permissions")) . "</error></message>";
             return $strReturn;
         }
     } else {
         class_response_object::getInstance()->setStrStatusCode(class_http_statuscodes::SC_UNAUTHORIZED);
         $strReturn .= "<message><error>" . xmlSafeString($this->getLang("commons_error_permissions")) . "</error></message>";
         return $strReturn;
     }
     //Handle the fileupload
     $arrSource = $this->getParam($this->getParam("inputElement"));
     $bitJsonResponse = $this->getParam("jsonResponse") != "";
     $bitPostData = false;
     if (is_array($arrSource)) {
         $strFilename = $arrSource["name"];
     } else {
         $bitPostData = getPostRawData() != "";
         $strFilename = $arrSource;
     }
     $strTarget = $strFolder . "/" . createFilename($strFilename);
     $objFilesystem = new class_filesystem();
     if (!file_exists(_realpath_ . "/" . $strFolder)) {
         $objFilesystem->folderCreate($strFolder, true);
     }
     if ($objFilesystem->isWritable($strFolder)) {
         //Check file for correct filters
         $arrAllowed = explode(",", $objRepo->getStrUploadFilter());
         $strSuffix = uniStrtolower(uniSubstr($strFilename, uniStrrpos($strFilename, ".")));
         if ($objRepo->getStrUploadFilter() == "" || in_array($strSuffix, $arrAllowed)) {
             if ($bitPostData) {
                 $objFilesystem = new class_filesystem();
                 $objFilesystem->openFilePointer($strTarget);
                 $bitCopySuccess = $objFilesystem->writeToFile(getPostRawData());
                 $objFilesystem->closeFilePointer();
             } else {
                 $bitCopySuccess = $objFilesystem->copyUpload($strTarget, $arrSource["tmp_name"]);
             }
             if ($bitCopySuccess) {
                 if ($bitJsonResponse) {
                     $strReturn = json_encode(array('success' => true));
                 } else {
                     $strReturn .= "<message>" . $this->getLang("xmlupload_success") . "</message>";
                 }
                 class_logger::getInstance()->addLogRow("uploaded file " . $strTarget, class_logger::$levelInfo);
                 $objRepo->syncRepo();
             } else {
                 if ($bitJsonResponse) {
                     $strReturn .= json_encode(array('error' => $this->getLang("xmlupload_error_copyUpload")));
                 } else {
                     $strReturn .= "<message><error>" . $this->getLang("xmlupload_error_copyUpload") . "</error></message>";
                 }
             }
         } else {
             class_response_object::getInstance()->setStrStatusCode(class_http_statuscodes::SC_BADREQUEST);
             if ($bitJsonResponse) {
                 $strReturn .= json_encode(array('error' => $this->getLang("xmlupload_error_filter")));
             } else {
                 $strReturn .= "<message><error>" . $this->getLang("xmlupload_error_filter") . "</error></message>";
             }
         }
     } else {
         class_response_object::getInstance()->setStrStatusCode(class_http_statuscodes::SC_INTERNAL_SERVER_ERROR);
         if ($bitJsonResponse) {
             $strReturn .= json_encode(array('error' => $this->getLang("xmlupload_error_notWritable")));
         } else {
             $strReturn .= "<message><error>" . xmlSafeString($this->getLang("xmlupload_error_notWritable")) . "</error></message>";
         }
     }
     if ($bitJsonResponse) {
         //disabled for ie. otherwise the upload won't work due to the headers.
         class_response_object::getInstance()->setStrResponseType(class_http_responsetypes::STR_TYPE_HTML);
         //class_response_object::getInstance()->setStResponseType(class_http_responsetypes::STR_TYPE_JSON);
     }
     @unlink($arrSource["tmp_name"]);
     return $strReturn;
 }
示例#2
0
 /**
  * Writes the current array of data to the given csv-file or directly to the browser.
  * Make sure to have set all needed values before, otherwise
  * an exception is thrown
  *
  * @return bool
  *
  * @param bool $bitStreamToBrowser
  * @param bool $bitExcludeHeaders skip the header-row in the output, generated based on the mapping
  *
  * @throws class_exception
  */
 public function writeArrayToFile($bitStreamToBrowser = false, $bitExcludeHeaders = false)
 {
     //all needed values set before?
     if ($this->arrData != null && $this->arrMapping != null && $this->strFilename != null) {
         //create file-content. use a file-pointer to avoid max-mem-errors
         $objFilesystem = new class_filesystem();
         //open file
         if ($bitStreamToBrowser) {
             class_response_object::getInstance()->addHeader('Pragma: private');
             class_response_object::getInstance()->addHeader('Cache-control: private, must-revalidate');
             class_response_object::getInstance()->setStrResponseType(class_http_responsetypes::STR_TYPE_CSV);
             class_response_object::getInstance()->addHeader("Content-Disposition: attachment; filename=" . saveUrlEncode(trim(basename($this->strFilename))));
             class_response_object::getInstance()->sendHeaders();
         } else {
             $objFilesystem->openFilePointer($this->strFilename);
         }
         //the first row should contain the row-names
         if (!$bitExcludeHeaders) {
             $strRow = "";
             foreach ($this->arrMapping as $strTagetCol) {
                 //add enclosers?
                 if ($this->strTextEncloser != null) {
                     $strTagetCol = $this->strTextEncloser . $strTagetCol . $this->strTextEncloser;
                 }
                 $strRow .= $strTagetCol . $this->strDelimiter;
             }
             //remove last delimiter, eol
             $strRow = uniSubstr($strRow, 0, uniStrlen($this->strDelimiter) * -1);
             //add a linebreak
             $strRow .= "\n";
             //write header to file
             if ($bitStreamToBrowser) {
                 echo $strRow;
             } else {
                 $objFilesystem->writeToFile($strRow);
             }
         }
         //iterate over the data array to write it to the file
         foreach ($this->arrData as $arrOneRow) {
             $strRow = "";
             foreach ($this->arrMapping as $strSourceCol => $strTargetCol) {
                 if (isset($arrOneRow[$strSourceCol])) {
                     $strEntry = $arrOneRow[$strSourceCol];
                     //escape the delimiter maybe occuring in the text
                     $strEntry = uniStrReplace($this->strDelimiter, "\\" . $this->strDelimiter, $strEntry);
                     //add enclosers?
                     if ($this->strTextEncloser != null) {
                         $strEntry = $this->strTextEncloser . $strEntry . $this->strTextEncloser;
                     }
                 } else {
                     $strEntry = "";
                 }
                 $strRow .= $strEntry . $this->strDelimiter;
             }
             //remove last delimiter, eol
             $strRow = uniSubstr($strRow, 0, uniStrlen($this->strDelimiter) * -1);
             //add linebreak
             $strRow .= "\n";
             //and write to file
             if ($bitStreamToBrowser) {
                 echo $strRow;
             } else {
                 $objFilesystem->writeToFile($strRow);
             }
         }
         //and close the filepointer...
         if (!$bitStreamToBrowser) {
             $objFilesystem->closeFilePointer();
         }
         if ($bitStreamToBrowser) {
             flush();
             die;
         }
         return true;
     } else {
         throw new class_exception("can't proceed, needed values missing", class_exception::$level_ERROR);
     }
 }