/**
  * Get File Revisions
  *
  * @param fileId - required -
  *		The id of the file.
  *
  * @return 	A File object is return with all the revisions
  * or the error code and message returned by the server.
  */
 public function getFileRevisions($fileId)
 {
     $urld = 'dpi/v1/folder/file/' . $fileId . '/revisions';
     $parameters = array();
     $this->response = $this->restTransportInstance->sendRequest($urld, $parameters, self::HTTP_GET, $this->authToken);
     $responseBody = simplexml_load_string($this->response);
     $returnObject = new File();
     if ($responseBody === false) {
         $errorCode = 'N/A';
         $errorMessage = 'The server has encountered an error, please try again.';
         $errorObject = new ErrorStatus($errorCode, $errorMessage);
         $returnObject->setErrorStatus($errorObject);
     } else {
         $errorStatus = $responseBody->errorStatus;
         if (empty($errorStatus)) {
             $returnObject->setRevision((string) $responseBody->attributes()->revision);
             $returnObject->setId((string) $responseBody->attributes()->id);
             $returnObject->setStatus((string) $responseBody->status);
             $returnObject->setCreatedOn((string) $responseBody->createdOn);
             $returnObject->setName((string) $responseBody->name);
             $returnObject->setOwnedByStorage((string) $responseBody->ownedByStorage);
             $returnObject->setParentId((string) $responseBody->parentid);
             $returnObject->setRevisionCount((string) $responseBody->revisionCount);
             $returnObject->setSize((string) $responseBody->size);
             $revisions = array();
             $revisionsTag = (string) $responseBody->revisions->count();
             if ($revisionsTag > 0) {
                 foreach ($responseBody->revisions->children() as $currentRevision) {
                     if ($currentRevision->count() > 0) {
                         $revision = new Revision();
                         $revision->setId((string) $currentRevision->attributes()->id);
                         $revision->setCurrent((string) $currentRevision->current);
                         $revision->setDownloadUrl((string) $currentRevision->downloadUrl);
                         $revision->setSize((string) $currentRevision->size);
                         array_push($revisions, $revision);
                     }
                 }
                 $returnObject->setRevisions($revisions);
             }
         } else {
             $errorCode = (string) $responseBody->errorStatus->code;
             $errorMessage = (string) $responseBody->errorStatus->message;
             $errorObject = new ErrorStatus($errorCode, $errorMessage);
             $returnObject->setErrorStatus($errorObject);
         }
     }
     return $returnObject;
 }
Example #2
0
 /**
  * Creates a Csv file consisting of the request body and then returns it.
  *
  * Called when this component receives an HTTP POST request to
  * /Csv/$orientation/temporary/$filename.
  * The request body should contain a JSON object representing the Csv file.
  *
  * @param string $orientation The orientation of the Csv file, e.g. portrait or landscape.
  * @param string $filename A freely chosen filename of the Csv file which should be stored.
  * @see http://wiki.spipu.net/doku.php
  */
 public function addCsvPermanent($callName, $input, $params = array())
 {
     $name = '';
     if ($input->getRows() != null) {
         foreach ($input->getRows() as $row) {
             $name .= implode(',', $row);
         }
     }
     $name = sha1($name);
     // generate Csv
     $filePath = FSCsv::generateFilePath($params['folder'], $name);
     unset($name);
     if (!file_exists($this->config['DIR']['files'] . '/' . $filePath)) {
         FSCsv::generatepath($this->config['DIR']['files'] . '/' . dirname($filePath));
         $result = $this->createCsv($input);
         // writes the file to filesystem
         $file = fopen($this->config['DIR']['files'] . '/' . $filePath, 'w');
         if ($file) {
             fwrite($file, $result);
             fclose($file);
         } else {
             $fileObject = new File();
             $fileObject->addMessage("Datei konnte nicht im Dateisystem angelegt werden.");
             $fileObject->setStatus(409);
             Logger::Log('POST postCsv failed', LogLevel::ERROR);
             return Model::isProblem($fileObject);
         }
     }
     if (isset($params['filename'])) {
         Model::header('Content-Type', 'text/csv');
         Model::header('Content-Disposition', "attachment; filename=\"" . $params['filename'] . "\"");
         Model::header('Accept-Ranges', 'none');
         if (isset($result)) {
             Model::header('Content-Length', strlen($result));
             return isCreated($result);
         } else {
             readfile($this->config['DIR']['files'] . '/' . $filePath);
             Model::header('Content-Length', filesize($this->config['DIR']['files'] . '/' . $filePath));
             return isCreated();
         }
     } else {
         $CsvFile = new File();
         $CsvFile->setStatus(201);
         $CsvFile->setAddress($filePath);
         $CsvFile->setMimeType("application/Csv");
         if (file_exists($this->config['DIR']['files'] . '/' . $filePath)) {
             $CsvFile->setFileSize(filesize($this->config['DIR']['files'] . '/' . $filePath));
             $hash = sha1(file_get_contents($this->config['DIR']['files'] . '/' . $filePath));
             $CsvFile->setHash($hash);
         }
         return Model::isCreated($CsvFile);
     }
 }
Example #3
0
}
// loop through messages
foreach ($data->files as $item) {
    // check if has been successfully downloaded
    if ($item->status >= 4) {
        // check if file already exists
        $file = FileQuery::create()->filterByHash($item->hash)->findOne();
        if (!$file) {
            // put file information into data base
            $file = new File();
            $file->setHash($item->hash);
            $file->setSuffix($item->suffix);
            $file->setDescription($item->description);
            $file->setSize($item->size);
            $file->setTime($item->time);
            $file->setStatus($item->status);
            $file->save();
            // advertise this file via twitter
            // TODO: send images < 3MB directly to twitter
            $txt = $file->getDescription() . " " . twitter_file_link($file);
            twitter_send2twitter($txt);
            if (get_debug()) {
                echo "advertised file on twitter: {$txt}\n";
            }
        }
    } else {
        if ($item->status == -2) {
            // delete file
            $file = FileQuery::create()->filterByHash($item->hash)->delete();
        }
    }
 /**
  * Send a file from the folder
  *
  * @param fileId - required -
  * 		Id of the file to be sent.
  * @param recipients - required -
  * 		Comma separated email addresses
  * 		of the users to whom the file link will be sent.
  * @return The File object containing the status of the operation.
  */
 public function sendFile($fileId, $recipients)
 {
     $parameters = array('recipients' => $recipients);
     $urld = 'dpi/v1/folder/file/' . $fileId . '/send';
     $this->response = $this->restTransportInstance->sendRequest($urld, $parameters, self::HTTP_POST, $this->authToken);
     $responseBody = simplexml_load_string($this->response);
     $returnObject = new File();
     if ($responseBody === false) {
         $errorCode = 'N/A';
         $errorMessage = 'The server has encountered an error, please try again.';
         $errorObject = new ErrorStatus($errorCode, $errorMessage);
         $returnObject->setErrorStatus($errorObject);
     } else {
         $errorStatus = $responseBody->errorStatus;
         if (empty($errorStatus)) {
             $returnObject->setStatus((string) $responseBody->status);
         } else {
             $errorCode = (string) $responseBody->errorStatus->code;
             $errorMessage = (string) $responseBody->errorStatus->message;
             $errorObject = new ErrorStatus($errorCode, $errorMessage);
             $returnObject->setErrorStatus($errorObject);
         }
     }
     return $returnObject;
 }
Example #5
0
 public function addPdfFromFile3($callName, $files, $params = array())
 {
     // merge all file objects to one pdf
     $hashArray = array();
     foreach ($files as $part) {
         if ($part->getBody() !== null) {
             $hashArray[] = $part->getBody();
         } else {
             $hashArray[] = $part->getAddress() . $part->getDisplayName();
         }
     }
     $name = sha1(implode("\n", $hashArray));
     $targetPath = FSPdf::generateFilePath($params['folder'], $name);
     if (!file_exists($this->config['DIR']['files'] . '/' . $targetPath)) {
         $body = "";
         foreach ($files as $part) {
             if ($part->getBody() !== null) {
                 // use body
                 $body .= $part->getBody(true) . '<br>';
             } else {
                 $file = $this->config['DIR']['files'] . '/' . $part->getAddress();
                 if (file_exists($file)) {
                     $text = file_get_contents($file);
                     if (mb_detect_encoding($text, 'UTF-8', true) === false) {
                         $text = utf8_encode($text);
                     }
                     $text = htmlentities(htmlentities($text));
                     $body .= $text . '<br>';
                 } else {
                     // failure
                 }
             }
         }
         ///echo $body;
         FSPdf::generatepath($this->config['DIR']['files'] . '/' . dirname($targetPath));
         $data = new Pdf();
         $data->setText($body);
         $result = FSPdf::createPdf($data);
         // writes the file to filesystem
         $file = fopen($this->config['DIR']['files'] . '/' . $targetPath, 'w');
         if ($file) {
             fwrite($file, $result);
             fclose($file);
         } else {
             $fileObject = new File();
             $fileObject->addMessage("Datei konnte nicht im Dateisystem angelegt werden.");
             $fileObject->setStatus(409);
             Logger::Log('POST postPdf failed', LogLevel::ERROR);
             return Model::isProblem($fileObject);
         }
     }
     $pdfFile = new File();
     $pdfFile->setStatus(201);
     $pdfFile->setAddress($targetPath);
     $pdfFile->setMimeType("application/pdf");
     if (file_exists($this->config['DIR']['files'] . '/' . $targetPath)) {
         $pdfFile->setFileSize(filesize($this->config['DIR']['files'] . '/' . $targetPath));
         $hash = sha1(file_get_contents($this->config['DIR']['files'] . '/' . $targetPath));
         $pdfFile->setHash($hash);
     }
     return Model::isCreated($pdfFile);
 }
Example #6
0
 /**
  * Deletes a file.
  *
  * Called when this component receives an HTTP DELETE request to
  * /file(/)
  */
 public function deleteFile($fileid)
 {
     Logger::Log('starts Delete deleteFile', LogLevel::DEBUG);
     $this->_app->response->setStatus(201);
     $fileObject = new File();
     $fileObject->setFileId($fileid);
     $res = null;
     if ($fileObject !== null && $fileObject !== array()) {
         $result = LFileHandler2::delete($this->_db, $this->_fs, array(), $fileObject);
     } else {
         $result = null;
     }
     if ($result !== null) {
         if (is_array($result)) {
             $result = $result[0];
         }
         $result->setStatus(201);
         $res = $result;
     } else {
         $result = new File();
         $result->getMessages()[] = "Die Datei konnte nicht gelöscht werden.";
         $result->setStatus(409);
         $res = $result;
         $this->_app->response->setStatus(409);
     }
     $this->_app->response->setBody(File::encodeFile($res));
 }