public static function download($url, $timeout = 3000) { xapp_import('xapp.Commons.Error'); xapp_import('xapp.Utils.Strings'); xapp_import('xapp.Http.MiniHTTP'); xapp_import('xapp.Directory.Utils'); xapp_import('xapp.File.Utils'); if (!$url) { return new XApp_Error('http_no_url', 'Invalid URL Provided.'); } $tmpfname = XApp_Directory_Utils::tempname($url); if (!$tmpfname) { return new XApp_Error_Base('http_no_file', 'Could not create Temporary file.'); } $http = new XApp_Http(); $response = $http->request($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname)); /*xapp_clog($response);*/ if (XApp_Error_Base::is_error($response)) { unlink($tmpfname); return $response; } /* if ( 200 != self::remote_retrieve_response_code( $response ) ){ unlink( $tmpfname ); return self::remote_retrieve_response_message( $response ); }*/ $content_md5 = self::remote_retrieve_header($response, 'content-md5'); if ($content_md5) { $md5_check = XApp_File_Utils::verify_file_md5($tmpfname, $content_md5); if (XApp_Error::is_error($md5_check)) { unlink($tmpfname); return $md5_check; } } return $tmpfname; }
public static function add_ls_file_information($filepath, &$item, $field_options) { //sanity check if (empty($filepath) || !file_exists($filepath)) { $item->{'read'} = false; $item->{'write'} = false; $item->{XAPP_NODE_FIELD_IS_DIRECTORY} = false; return; } // show permissions if (($field_options & XAPP_XFILE_SHOW_PERMISSIONS) == XAPP_XFILE_SHOW_PERMISSIONS) { $item->{XAPP_NODE_FIELD_PERMISSIONS} = XApp_File_Utils::get_file_permissions($filepath); $item->{'read'} = is_readable($filepath); $item->{'write'} = is_writeable($filepath); } // show owner if (($field_options & XAPP_XFILE_SHOW_OWNER) == XAPP_XFILE_SHOW_OWNER) { $item->{XAPP_NODE_FIELD_OWNER} = XApp_File_Utils::get_file_ownership($filepath); } // force read only if (($field_options & XAPP_XFILE_SHOW_ISREADONLY) == XAPP_XFILE_SHOW_ISREADONLY) { $item->{XAPP_NODE_FIELD_READ_ONLY} = !is_writable($filepath); } // show is directory if (($field_options & XAPP_XFILE_SHOW_ISDIR) == XAPP_XFILE_SHOW_ISDIR) { $item->{XAPP_NODE_FIELD_IS_DIRECTORY} = is_dir($filepath); } // show size if (($field_options & XAPP_XFILE_SHOW_SIZE) == XAPP_XFILE_SHOW_SIZE) { if (!is_dir($filepath)) { $file_size = filesize($filepath); $item->{XAPP_NODE_FIELD_SIZE} = $file_size ? XApp_File_Utils::formatSizeUnits($file_size) : ""; $item->{XAPP_NODE_FIELD_SIZE_BYTES} = $file_size; } } // show folder size if (($field_options & XAPP_XFILE_SHOW_FOLDER_SIZE) == XAPP_XFILE_SHOW_FOLDER_SIZE) { if (is_dir($filepath)) { $size = XApp_Directory_Utils::getDirectorySize($filepath); $item->{XAPP_NODE_FIELD_SIZE} = XApp_File_Utils::formatSizeUnits($size); $item->{XAPP_NODE_FIELD_SIZE_BYTES} = $size; } } // show mime if (($field_options & XAPP_XFILE_SHOW_MIME) == XAPP_XFILE_SHOW_MIME) { $item->{XAPP_NODE_FIELD_MIME} = XApp_File_Utils::getMime($filepath); } // show time if (($field_options & XAPP_XFILE_SHOW_TIME) == XAPP_XFILE_SHOW_TIME) { $item->{XAPP_NODE_FIELD_TIME} = XApp_File_Utils::getFileTime($filepath); } }
public function setContent($scopeName, $path, $content) { xapp_import('xapp.Directory.Utils'); xapp_import('xapp.Path.Utils'); xapp_import('xapp.VFS.Local'); xapp_import('xapp.Commons.Exceptions'); $scope = $this->getScope($scopeName); if ($scope == null) { return false; } $return = null; $fullPath = XApp_Path_Utils::normalizePath($this->resolvePath($scopeName, DIRECTORY_SEPARATOR . $path, null, true, false), true, false); if (!file_exists($fullPath)) { XApp_File_Utils::createEmptyFile($fullPath); } $content = xapp_prettyPrint($content); if (file_exists($fullPath)) { if (!is_writeable($fullPath)) { throw new Xapp_Util_Exception_Storage(vsprintf('File: %s is not writable', array(basename($fullPath))), 1640102); } else { //write out $fp = fopen($fullPath, "w"); fputs($fp, $content); fclose($fp); clearstatcache(true, $fullPath); $return = true; } } else { throw new Xapp_Util_Exception_Storage('unable to write storage to file : ' . $path . ' at : ' . $fullPath, 1640104); } return $return; }
/** * @param $mount * @param $selection * @param string $type * @return mixed */ public function downloadTo($url, $mount, $to) { xapp_import('xapp.Path.Utils'); xapp_import('xapp.File.Utils'); xapp_import('xapp.Commons.ErrorHandler'); xapp_import('xapp.Utils.Download'); xapp_import('xapp.Utils.SystemTextEncoding'); $success = array(); $error = array(); $mount = XApp_Path_Utils::getMount($mount); $vfs = $this->getFileSystem($mount); $realSrcFile = '' . $url; XApp_ErrorHandler::start(); if ($this->isLocal($mount, $this->getFSResources())) { $srcFile = '' . XApp_Path_Utils::decodeSecureMagic($to); $destFile = $vfs->toRealPath($mount . DIRECTORY_SEPARATOR . $srcFile); if (is_dir($destFile) && (!file_exists($destFile) || !is_file($destFile))) { $destDirectory = '' . $destFile; //default file name $fileName = 'remoteFile.download'; //try to get a file name from the url through parse_url(pathinfo()) $urlParts = parse_url($url); if ($urlParts['path']) { $pathInfo = pathinfo($urlParts['path']); if ($pathInfo['basename']) { $fileName = $pathInfo['basename']; } } $destFile = XApp_File_Utils::unique_filename($destFile, $fileName); $destFile = $destDirectory . DIRECTORY_SEPARATOR . $destFile; touch($destFile); } try { $srcPath = XApp_Download::download($realSrcFile); if (!file_exists($srcPath)) { return self::toRPCError(1, 'Error downloading ' . $srcPath . ' Error : ' . $srcPath); } $src = fopen($srcPath, "r"); $dest = fopen($destFile, "w"); if ($dest !== false) { while (!feof($src)) { stream_copy_to_stream($src, $dest, 4096); } fclose($dest); } fclose($src); @unlink($srcPath); } catch (Exception $e) { return self::toRPCError(1, $e->getMessage()); } } else { $mountManager = $vfs->getMountManager(); $to = XApp_Path_Utils::normalizePath($to, false, false); $to = $mount . '://' . $to; $to = $vfs->cleanSlashesRemote($to); try { $src = fopen($realSrcFile, "r"); while (!feof($src)) { $mountManager->updateStream($to, $src); } fclose($src); } catch (Exception $e) { return self::toRPCError(1, $e->getMessage()); } } /* $errors = XApp_ErrorHandler::stop(); if($errors){ xapp_clog($errors); $this->logger->log(json_encode($errors)); } */ /*xapp_clog($errors);*/ return true; }
/** * @param $fileName * @param $content * @param array $options * @return mixed */ private function send($fileName, $content, $options = array()) { ini_set('memory_limit', '128M'); if (!isset($options[XApp_File_Utils::OPTION_SIZE_LIMIT])) { $options[XApp_File_Utils::OPTION_SIZE_LIMIT] = XApp_File_Utils::GET_FILE_SIZE_LIMIT; } if (!isset($options[XApp_File_Utils::OPTION_CHUNK_SIZE])) { $options[XApp_File_Utils::OPTION_CHUNK_SIZE] = XApp_File_Utils::GET_FILE_CHUNK_SIZE; } if (!isset($options[XApp_File_Utils::OPTION_TEMP_PATH])) { $options[XApp_File_Utils::OPTION_TEMP_PATH] = sys_get_temp_dir(); } if (!isset($options[XApp_File_Utils::OPTION_AS_ATTACHMENT])) { $options[XApp_File_Utils::OPTION_AS_ATTACHMENT] = false; } if (!isset($options[XApp_File_Utils::OPTION_TEST])) { $options[XApp_File_Utils::OPTION_TEST] = false; } if (!isset($options[XApp_File_Utils::OPTION_SEND])) { $options[XApp_File_Utils::OPTION_SEND] = true; } $mime = XApp_File_Utils::guessMime($fileName); if ($options[XApp_File_Utils::OPTION_SEND] === true) { //XApp_File_Utils::sendHeader($mime,($options[XApp_File_Utils::OPTION_AS_ATTACHMENT] ? $fileName:''), basename($fileName)); if (!$options[XApp_File_Utils::OPTION_TEST]) { XApp_File_Utils::sendHeader($mime, $options[XApp_File_Utils::OPTION_AS_ATTACHMENT] ? $fileName : '', basename($fileName)); if (strpos($mime, "text") !== FALSE) { if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) { ob_start("ob_gzhandler"); } else { ob_start(); } } } echo $content; } else { XApp_File_Utils::sendHeader($mime, '', basename($fileName)); return $content; } }
/** * Returns a filename of a Temporary unique file. * Please note that the calling function must unlink() this itself. * * The filename is based off the passed parameter or defaults to the current unix timestamp, * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory. * * @param string $filename (optional) Filename to base the Unique file off * @param string $dir (optional) Directory to store the file in * @return string a writable filename */ public static function tempname($filename = '', $dir = '') { xapp_import('xapp.File.Utils'); if (empty($dir)) { $dir = self::get_temp_dir(); } $filename = basename($filename); if (empty($filename)) { $filename = time(); } $filename = preg_replace('|\\..*$|', '.tmp', $filename); $filename = $dir . XApp_File_Utils::unique_filename($dir, $filename); touch($filename); return $filename; }