Example #1
2
 /**
  * get metadata info for a media file
  *
  * @param $path
  * @return array
  */
 public function extract($path)
 {
     if (ini_get('allow_url_fopen')) {
         $file = \OC\Files\Filesystem::getView()->getAbsolutePath($path);
         $data = @$this->getID3->analyze('oc://' . $file);
     } else {
         // Fallback to the local FS
         $file = \OC\Files\Filesystem::getLocalFile($path);
     }
     \getid3_lib::CopyTagsToComments($data);
     return $data;
 }
Example #2
0
 /**
  * Load the image.
  */
 protected function processImage()
 {
     $localPath = \OC\Files\Filesystem::getLocalFile($this->path);
     if (!file_exists($localPath)) {
         throw new \Exception('The file does not exist: ' . $localPath, Http::STATUS_NOT_FOUND);
     }
     $this->image = new \OCP\Image();
     $this->image->loadFromFile($localPath);
 }
Example #3
0
 /**
  * @param false|string $filename
  */
 private static function addSendfileHeader($filename)
 {
     if (isset($_SERVER['MOD_X_SENDFILE_ENABLED'])) {
         $filename = \OC\Files\Filesystem::getLocalFile($filename);
         header("X-Sendfile: " . $filename);
     }
     if (isset($_SERVER['MOD_X_SENDFILE2_ENABLED'])) {
         $filename = \OC\Files\Filesystem::getLocalFile($filename);
         if (isset($_SERVER['HTTP_RANGE']) && preg_match("/^bytes=([0-9]+)-([0-9]*)\$/", $_SERVER['HTTP_RANGE'], $range)) {
             $filelength = filesize($filename);
             if ($range[2] === "") {
                 $range[2] = $filelength - 1;
             }
             header("Content-Range: bytes {$range['1']}-{$range['2']}/" . $filelength);
             header("HTTP/1.1 206 Partial content");
             header("X-Sendfile2: " . str_replace(",", "%2c", rawurlencode($filename)) . " {$range['1']}-{$range['2']}");
         } else {
             header("X-Sendfile: " . $filename);
         }
     }
     if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
         if (isset($_SERVER['MOD_X_ACCEL_REDIRECT_PREFIX'])) {
             $filename = $_SERVER['MOD_X_ACCEL_REDIRECT_PREFIX'] . \OC\Files\Filesystem::getLocalFile($filename);
         } else {
             $filename = \OC::$WEBROOT . '/data' . \OC\Files\Filesystem::getRoot() . $filename;
         }
         header("X-Accel-Redirect: " . $filename);
     }
 }
 /**
  * Saves the photo from ownCloud FS to oC cache
  * @return JSONResponse with data.tmp set to the key in the cache.
  *
  * @NoAdminRequired
  * @NoCSRFRequired
  */
 public function cacheFileSystemPhoto()
 {
     $params = $this->request->urlParams;
     $response = new JSONResponse();
     if (!isset($this->request->get['path'])) {
         $response->bailOut(App::$l10n->t('No photo path was submitted.'));
     }
     $localpath = \OC\Files\Filesystem::getLocalFile($this->request->get['path']);
     $tmpkey = 'contact-photo-' . $params['contactId'];
     if (!file_exists($localpath)) {
         return $response->bailOut(App::$l10n->t('File doesn\'t exist:') . $localpath);
     }
     $image = new \OCP\Image();
     if (!$image) {
         return $response->bailOut(App::$l10n->t('Error loading image.'));
     }
     if (!$image->loadFromFile($localpath)) {
         return $response->bailOut(App::$l10n->t('Error loading image.'));
     }
     if ($image->width() > 400 || $image->height() > 400) {
         $image->resize(400);
         // Prettier resizing than with browser and saves bandwidth.
     }
     if (!$image->fixOrientation()) {
         // No fatal error so we don't bail out.
         $response->debug('Couldn\'t save correct image orientation: ' . $localpath);
     }
     if (!$this->server->getCache()->set($tmpkey, $image->data(), 600)) {
         return $response->bailOut('Couldn\'t save temporary image: ' . $tmpkey);
     }
     return $response->setData(array('tmp' => $tmpkey, 'metadata' => array('contactId' => $params['contactId'], 'addressBookId' => $params['addressBookId'], 'backend' => $params['backend'])));
 }
Example #5
0
 /**
  * @NoAdminRequired
  */
 public function getImageFromCloud()
 {
     $id = $this->params('id');
     $path = $this->params('path');
     $localpath = \OC\Files\Filesystem::getLocalFile($path);
     $tmpkey = 'editphoto';
     $size = getimagesize($localpath, $info);
     //$exif = @exif_read_data($localpath);
     $image = new \OCP\Image();
     $image->loadFromFile($localpath);
     $image->fixOrientation();
     $imgMimeType = $image->mimeType();
     \OC::$server->getCache()->remove($tmpkey);
     \OC::$server->getCache()->remove($tmpkey . 'ratio');
     $originalWidth = $image->width();
     if (\OC::$server->getCache()->set($tmpkey, $image->data(), 600)) {
         if ($image->width() > 400 || $image->height() > 400) {
             $image->resize(400);
         }
         $ratio = $originalWidth / $image->width();
         if (\OC::$server->getCache()->set($tmpkey . 'ratio', $ratio, 600)) {
             $imgString = $image->__toString();
             $resultData = array('id' => $id, 'tmp' => $tmpkey, 'imgdata' => $imgString, 'mimetype' => $imgMimeType);
             $response = new JSONResponse();
             $response->setData($resultData);
             return $response;
         }
     }
 }
Example #6
0
 /**
  * return the content of a file or return a zip file containing multiple files
  *
  * @param string $dir
  * @param string $file ; separated list of files to download
  * @param boolean $only_header ; boolean to only send header of the request
  */
 public static function get($dir, $files, $only_header = false)
 {
     $xsendfile = false;
     if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || isset($_SERVER['MOD_X_SENDFILE2_ENABLED']) || isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
         $xsendfile = true;
     }
     if (is_array($files) && count($files) == 1) {
         $files = $files[0];
     }
     if (is_array($files)) {
         self::validateZipDownload($dir, $files);
         $executionTime = intval(ini_get('max_execution_time'));
         set_time_limit(0);
         $zip = new ZipArchive();
         $filename = OC_Helper::tmpFile('.zip');
         if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
             $l = OC_L10N::get('lib');
             throw new Exception($l->t('cannot open "%s"', array($filename)));
         }
         foreach ($files as $file) {
             $file = $dir . '/' . $file;
             if (\OC\Files\Filesystem::is_file($file)) {
                 $tmpFile = \OC\Files\Filesystem::toTmpFile($file);
                 self::$tmpFiles[] = $tmpFile;
                 $zip->addFile($tmpFile, basename($file));
             } elseif (\OC\Files\Filesystem::is_dir($file)) {
                 self::zipAddDir($file, $zip);
             }
         }
         $zip->close();
         if ($xsendfile) {
             $filename = OC_Helper::moveToNoClean($filename);
         }
         $basename = basename($dir);
         if ($basename) {
             $name = $basename . '.zip';
         } else {
             $name = 'download.zip';
         }
         set_time_limit($executionTime);
     } elseif (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
         self::validateZipDownload($dir, $files);
         $executionTime = intval(ini_get('max_execution_time'));
         set_time_limit(0);
         $zip = new ZipArchive();
         $filename = OC_Helper::tmpFile('.zip');
         if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
             $l = OC_L10N::get('lib');
             throw new Exception($l->t('cannot open "%s"', array($filename)));
         }
         $file = $dir . '/' . $files;
         self::zipAddDir($file, $zip);
         $zip->close();
         if ($xsendfile) {
             $filename = OC_Helper::moveToNoClean($filename);
         }
         $name = $files . '.zip';
         set_time_limit($executionTime);
     } else {
         $zip = false;
         $filename = $dir . '/' . $files;
         $name = $files;
         if ($xsendfile && OC_App::isEnabled('files_encryption')) {
             $xsendfile = false;
         }
     }
     OC_Util::obEnd();
     if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
         OC_Response::setContentDispositionHeader($name, 'attachment');
         header('Content-Transfer-Encoding: binary');
         OC_Response::disableCaching();
         if ($zip) {
             ini_set('zlib.output_compression', 'off');
             header('Content-Type: application/zip');
             header('Content-Length: ' . filesize($filename));
             self::addSendfileHeader($filename);
         } else {
             $filesize = \OC\Files\Filesystem::filesize($filename);
             header('Content-Type: ' . \OC\Files\Filesystem::getMimeType($filename));
             if ($filesize > -1) {
                 header("Content-Length: " . $filesize);
             }
             if ($xsendfile) {
                 list($storage) = \OC\Files\Filesystem::resolvePath(\OC\Files\Filesystem::getView()->getAbsolutePath($filename));
                 /**
                  * @var \OC\Files\Storage\Storage $storage
                  */
                 if ($storage->instanceOfStorage('\\OC\\Files\\Storage\\Local')) {
                     self::addSendfileHeader(\OC\Files\Filesystem::getLocalFile($filename));
                 }
             }
         }
     } elseif ($zip or !\OC\Files\Filesystem::file_exists($filename)) {
         header("HTTP/1.0 404 Not Found");
         $tmpl = new OC_Template('', '404', 'guest');
         $tmpl->assign('file', $name);
         $tmpl->printPage();
     } else {
         header("HTTP/1.0 403 Forbidden");
         die('403 Forbidden');
     }
     if ($only_header) {
         return;
     }
     if ($zip) {
         $handle = fopen($filename, 'r');
         if ($handle) {
             $chunkSize = 8 * 1024;
             // 1 MB chunks
             while (!feof($handle)) {
                 echo fread($handle, $chunkSize);
                 flush();
             }
         }
         if (!$xsendfile) {
             unlink($filename);
         }
     } else {
         \OC\Files\Filesystem::readfile($filename);
     }
     foreach (self::$tmpFiles as $tmpFile) {
         if (file_exists($tmpFile) and is_file($tmpFile)) {
             unlink($tmpFile);
         }
     }
 }
 /**
  * return the path to a local version of the file
  * we need this because we can't know if a file is stored local or not from
  * outside the filestorage and for some purposes a local file is needed
  *
  * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem
  * @param string $path
  * @return string
  */
 public static function getLocalFile($path)
 {
     return \OC\Files\Filesystem::getLocalFile($path);
 }
Example #8
0
 /**
  * return the content of a file or return a zip file containing multiple files
  *
  * @param string $dir
  * @param string $file ; separated list of files to download
  * @param boolean $only_header ; boolean to only send header of the request
  */
 public static function get($dir, $files, $only_header = false)
 {
     $xsendfile = false;
     if (isset($_SERVER['MOD_X_SENDFILE_ENABLED']) || isset($_SERVER['MOD_X_ACCEL_REDIRECT_ENABLED'])) {
         $xsendfile = true;
     }
     if (is_array($files) && count($files) == 1) {
         $files = $files[0];
     }
     if (is_array($files)) {
         self::validateZipDownload($dir, $files);
         $executionTime = intval(ini_get('max_execution_time'));
         set_time_limit(0);
         $zip = new ZipArchive();
         if ($xsendfile) {
             $filename = OC_Helper::tmpFileNoClean('.zip');
         } else {
             $filename = OC_Helper::tmpFile('.zip');
         }
         if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
             exit("cannot open <{$filename}>\n");
         }
         foreach ($files as $file) {
             $file = $dir . '/' . $file;
             if (\OC\Files\Filesystem::is_file($file)) {
                 $tmpFile = \OC\Files\Filesystem::toTmpFile($file);
                 self::$tmpFiles[] = $tmpFile;
                 $zip->addFile($tmpFile, basename($file));
             } elseif (\OC\Files\Filesystem::is_dir($file)) {
                 self::zipAddDir($file, $zip);
             }
         }
         $zip->close();
         $basename = basename($dir);
         if ($basename) {
             $name = $basename . '.zip';
         } else {
             $name = 'owncloud.zip';
         }
         set_time_limit($executionTime);
     } elseif (\OC\Files\Filesystem::is_dir($dir . '/' . $files)) {
         self::validateZipDownload($dir, $files);
         $executionTime = intval(ini_get('max_execution_time'));
         set_time_limit(0);
         $zip = new ZipArchive();
         if ($xsendfile) {
             $filename = OC_Helper::tmpFileNoClean('.zip');
         } else {
             $filename = OC_Helper::tmpFile('.zip');
         }
         if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
             exit("cannot open <{$filename}>\n");
         }
         $file = $dir . '/' . $files;
         self::zipAddDir($file, $zip);
         $zip->close();
         $name = $files . '.zip';
         set_time_limit($executionTime);
     } else {
         $zip = false;
         $filename = $dir . '/' . $files;
         $name = $files;
     }
     OC_Util::obEnd();
     if ($zip or \OC\Files\Filesystem::isReadable($filename)) {
         if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
             header('Content-Disposition: attachment; filename="' . rawurlencode($name) . '"');
         } else {
             header('Content-Disposition: attachment; filename*=UTF-8\'\'' . rawurlencode($name) . '; filename="' . rawurlencode($name) . '"');
         }
         header('Content-Transfer-Encoding: binary');
         OC_Response::disableCaching();
         if ($zip) {
             ini_set('zlib.output_compression', 'off');
             header('Content-Type: application/zip');
             header('Content-Length: ' . filesize($filename));
             self::addSendfileHeader($filename);
         } else {
             header('Content-Type: ' . \OC\Files\Filesystem::getMimeType($filename));
             header("Content-Length: " . \OC\Files\Filesystem::filesize($filename));
             list($storage) = \OC\Files\Filesystem::resolvePath($filename);
             if ($storage instanceof \OC\Files\Storage\Local) {
                 self::addSendfileHeader(\OC\Files\Filesystem::getLocalFile($filename));
             }
         }
     } elseif ($zip or !\OC\Files\Filesystem::file_exists($filename)) {
         header("HTTP/1.0 404 Not Found");
         $tmpl = new OC_Template('', '404', 'guest');
         $tmpl->assign('file', $name);
         $tmpl->printPage();
     } else {
         header("HTTP/1.0 403 Forbidden");
         die('403 Forbidden');
     }
     if ($only_header) {
         return;
     }
     if ($zip) {
         $handle = fopen($filename, 'r');
         if ($handle) {
             $chunkSize = 8 * 1024;
             // 1 MB chunks
             while (!feof($handle)) {
                 echo fread($handle, $chunkSize);
                 flush();
             }
         }
         if (!$xsendfile) {
             unlink($filename);
         }
     } else {
         \OC\Files\Filesystem::readfile($filename);
     }
     foreach (self::$tmpFiles as $tmpFile) {
         if (file_exists($tmpFile) and is_file($tmpFile)) {
             unlink($tmpFile);
         }
     }
 }
Example #9
0
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
// Check if we are a user
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts');
require_once 'loghandler.php';
if (!isset($_GET['id'])) {
    bailOut(OCA\Contacts\App::$l10n->t('No contact ID was submitted.'));
}
if (!isset($_GET['path'])) {
    bailOut(OCA\Contacts\App::$l10n->t('No photo path was submitted.'));
}
$localpath = \OC\Files\Filesystem::getLocalFile($_GET['path']);
$tmpkey = 'contact-photo-' . $_GET['id'];
if (!file_exists($localpath)) {
    bailOut(OCA\Contacts\App::$l10n->t('File doesn\'t exist:') . $localpath);
}
$image = new OC_Image();
if (!$image) {
    bailOut(OCA\Contacts\App::$l10n->t('Error loading image.'));
}
if (!$image->loadFromFile($localpath)) {
    bailOut(OCA\Contacts\App::$l10n->t('Error loading image.'));
}
if ($image->width() > 400 || $image->height() > 400) {
    $image->resize(400);
    // Prettier resizing than with browser and saves bandwidth.
}
 /**
  * Compile Latex File
  *
  * @NoAdminRequired
  *
  * @param string $path
  * @param string $filename
  * @param string $compiler
  * @return DataResponse
  */
 public function doCompile($path, $filename, $compiler)
 {
     $success = 'success';
     $error = 'error';
     try {
         set_time_limit(0);
         //scanning can take ages
         // If they've set the compiler to something other than an allowable option....
         if (!($compiler === 'xelatex' || $compiler === 'pdflatex' || $compiler === 'latex')) {
             $compiler = 'latex';
         }
         // The real directory file
         $workdir = dirname(\OC\Files\Filesystem::getLocalFile(stripslashes($path) . $filename));
         $info = pathinfo($filename);
         $fileext = '.' . $info['extension'];
         $projectname = trim(basename($filename, $fileext));
         $pdffile = $projectname . '.pdf';
         $dvifile = $projectname . '.dvi';
         $psfile = $projectname . '.ps';
         $tocfile = $projectname . '.toc';
         $logfile = $projectname . '.log';
         $bibfile = $projectname;
         // Bibtex File is without extension
         // As we will write pdf/ps file(s) in the $path, we need to known if it's writable
         if (!\OC\Files\Filesystem::isCreatable(stripslashes($path))) {
             return new JSONResponse(array('data' => array('message' => 'As you don\'t have write permission in the owner directory, it\'s not possible to create output latex files.', 'output' => '')), Http::STATUS_BAD_REQUEST);
         }
         // Command to jump into directory
         $cd_command = "cd " . str_replace(' ', '\\ ', trim($workdir));
         // PDFLatex command preparation
         if ($compiler == 'xelatex' || $compiler == 'pdflatex') {
             $latex_command = $compiler . ' ' . $filename;
             $bibtex_command = 'bibtex ' . $bibfile;
         } else {
             $latex_command = "latex -output-directory={$outpath}  {$filename} ; cd {$outpath}; dvips  {$dvifile} ; ps2pdf {$psfile}";
         }
         $output = "<b>========BEGIN COMPILE========</b>\n{$psfile} \n";
         $output .= $cd_command . "\n";
         $output .= getcwd() . "\n";
         // First Compile
         $output .= shell_exec($cd_command . " && pwd");
         $return = shell_exec($cd_command . " && " . $latex_command);
         $output .= getcwd() . "\n";
         // For BibTeX
         if ($compiler == 'pdflatex') {
             // Second compile step with bibtex
             $return .= shell_exec($cd_command . " && " . $bibtex_command);
             // compile again after bibtex
             $return .= shell_exec($cd_command . " && " . $latex_command);
         }
         $logfile = $workdir . '/' . $logfile;
         $log = file_get_contents($logfile);
         while (preg_match('/Return to get cross-references right/', $log) || preg_match('/No file ' . $tocfile . '/', $log)) {
             $return .= shell_exec($cd_command . " && " . $this->latex_command);
             $log = file_get_contents($logfile);
         }
         // ! at begining of a line indicate an error!
         $errors = preg_grep("/^!/", explode("\n", $log));
         if (empty($errors) === false) {
             $log_array = explode("\n", $log);
             $error = "\n";
             foreach ($errors as $line => $msg) {
                 for ($i = $line; $i <= $line + 5; $i++) {
                     $error .= $log_array[$i] . "\n";
                 }
             }
             return new JSONResponse(array('data' => array('message' => $this->l->t('Compile failed with errors') . ' - <br/>', 'output' => nl2br($output . " % " . $latex_command . "\n" . $error)), 'status' => $error), Http::STATUS_OK);
         }
         // No PDF File !?
         if (!file_exists($workdir . '/' . $pdffile)) {
             return new JSONResponse(array('data' => array('message' => $this->l->t('Compile failed with errors') . ':<br/>', 'output' => nl2br($output . " % " . $latex_command . "\n" . file_get_contents($outpath . '/' . $logfile))), 'status' => $error), Http::STATUS_OK);
         }
         $output .= $return;
         $output .= "\n========END COMPILE==========\n";
         $oc_workdir = stripslashes(\OC\Files\Filesystem::getLocalPath($workdir));
         $target = \OCP\Files::buildNotExistingFileName($oc_workdir, $pdffile);
         $target = \OC\Files\Filesystem::normalizePath($target);
         $meta = \OC\Files\Filesystem::getFileInfo($target);
         if ($compiler === 'latex') {
             $target = \OCP\Files::buildNotExistingFileName($oc_workdir, $psfile);
             $target = \OC\Files\Filesystem::normalizePath($target);
             $meta = \OC\Files\Filesystem::getFileInfo($target);
         }
         return new JSONResponse(array('data' => array('output' => nl2br($output), 'path' => $path, 'pdffile' => $pdffile, 'psfile' => $psfile, 'logfile' => $logfile), 'status' => $success), Http::STATUS_OK);
     } catch (\Exception $e) {
         return new DataResponse(['message' => $e], Http::STATUS_BAD_REQUEST);
     }
 }
<?php

// chech if app is enabled
OCP\JSON::checkAppEnabled('checksums');
OCP\JSON::checkLoggedIn();
OCP\JSON::callCheck();
// get file
$source = $_GET['source'];
$dir = $_GET['dir'];
$file = $dir . $source;
if ($info = \OC\Files\Filesystem::getLocalFile($file)) {
    $md5 = md5_file($info);
    OCP\JSON::success(array('datamd5' => array($md5)));
} else {
    OCP\JSON::error(array('datamd5' => array('An Error occured.')));
}
 public function delete($oclink)
 {
     $link = \OC\Files\Filesystem::getLocalFile($oclink);
     $result = "";
     $message = "";
     try {
         if (file_exists($link)) {
             if (is_link($link)) {
                 unlink($link);
                 $result = "ok";
                 $message = "{$link} deleted";
             } else {
                 $result = "fail";
                 $message = "{$link} exists but not symbolic link";
             }
         } else {
             $result = "fail";
             $message = "{$link} does not exist";
         }
         return $this->resultReturn($result, $message);
     } catch (Exception $e) {
         $this->handleException($e);
     }
 }
Example #13
0
 /**
  * @brief scan files for metadata
  *
  * @param int $id fileid
  * @return array $meta metadata
  */
 public static function scan($id)
 {
     $meta = self::create($id);
     $path = \OC\Files\Filesystem::getLocalFile(\OC\Files\Filesystem::getPath($id));
     /* scan for Calibre-generated metadata.opf first. If found, use it as metadata source,
      * if not found continue file/isbn/etc scan
      */
     if (!Calibre::get($path, $meta)) {
         /* try to call function named 'type' with signature type($path,$meta)
          * eg, pdf(), epub(), etc
          */
         $type = strtolower(substr(strrchr($path, "."), 1));
         if (is_callable(array(__CLASS__, $type))) {
             try {
                 self::$type($path, $meta);
             } catch (Exception $e) {
                 Util::logWarn("no metadata scanner for format " . $type);
             }
         }
     }
     /* if title is not set, assume metadata was invalid or not present
      * use filename as title
      */
     if (!$meta['title']) {
         $info = pathinfo($path);
         $meta['title'] = basename($path, '.' . $info['extension']);
     }
     self::save($meta);
     return $meta;
 }