Example #1
0
 /**
  * Method to extract archive using stream objects
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  */
 protected function extractStream($archive, $destination, $options = array())
 {
     // New style! streams!
     $input = JFactory::getStream();
     // Use bzip
     $input->set('processingmethod', 'bz');
     if (!$input->open($archive)) {
         throw new RuntimeException('Unable to read archive (bz2)');
     }
     $output = JFactory::getStream();
     if (!$output->open($destination, 'w')) {
         $input->close();
         throw new RuntimeException('Unable to write archive (bz2)');
     }
     do {
         $this->_data = $input->read($input->get('chunksize', 8196));
         if ($this->_data && !$output->write($this->_data)) {
             $input->close();
             throw new RuntimeException('Unable to write archive (bz2)');
         }
     } while ($this->_data);
     $output->close();
     $input->close();
     return true;
 }
Example #2
0
 /**
  * Extract a ZIP compressed file to a given path
  *
  * @param	string	$archive		Path to ZIP archive to extract
  * @param	string	$destination	Path to extract archive into
  * @param	array	$options		Extraction options [unused]
  *
  * @return	boolean	True if successful
  * @since	1.5
  */
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     $this->_metadata = null;
     $stream = JFactory::getStream();
     if (!$stream->open($archive, 'rb')) {
         $this->set('error.message', JText::_('JLIB_FILESYSTEM_TAR_UNABLE_TO_READ'));
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     $position = 0;
     $return_array = array();
     $i = 0;
     $chunksize = 512;
     // tar has items in 512 byte packets
     while ($entry = $stream->read($chunksize)) {
         //$entry = &$this->_data[$i];
         $info = @unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $entry);
         if (!$info) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_TAR_UNABLE_TO_DECOMPRESS'));
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $size = octdec($info['size']);
         $bsize = ceil($size / $chunksize) * $chunksize;
         $contents = '';
         if ($size) {
             //$contents = fread($this->_fh, $size);
             $contents = substr($stream->read($bsize), 0, octdec($info['size']));
         }
         if ($info['filename']) {
             $file = array('attr' => null, 'data' => null, 'date' => octdec($info['mtime']), 'name' => trim($info['filename']), 'size' => octdec($info['size']), 'type' => isset($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null);
             if ($info['typeflag'] == 0 || $info['typeflag'] == 0x30 || $info['typeflag'] == 0x35) {
                 /* File or folder. */
                 $file['data'] = $contents;
                 $mode = hexdec(substr($info['mode'], 4, 3));
                 $file['attr'] = ($info['typeflag'] == 0x35 ? 'd' : '-') . ($mode & 0x400 ? 'r' : '-') . ($mode & 0x200 ? 'w' : '-') . ($mode & 0x100 ? 'x' : '-') . ($mode & 0x40 ? 'r' : '-') . ($mode & 0x20 ? 'w' : '-') . ($mode & 0x10 ? 'x' : '-') . ($mode & 0x4 ? 'r' : '-') . ($mode & 0x2 ? 'w' : '-') . ($mode & 0x1 ? 'x' : '-');
             } else {
                 /* Some other type. */
             }
             $type = strtolower($file['type']);
             if ($type == 'file' || $type == 'unix file') {
                 $path = JPath::clean($destination . DS . $file['name']);
                 // Make sure the destination folder exists
                 if (!JFolder::create(dirname($path))) {
                     $this->set('error.message', JText::_('JLIB_FILESYSTEM_TAR_UNABLE_TO_CREATE_DESTINATION'));
                     return JError::raiseWarning(100, $this->get('error.message'));
                 }
                 if (JFile::write($path, $contents, true) === false) {
                     $this->set('error.message', JText::_('JLIB_FILESYSTEM_TAR_UNABLE_TO_WRITE_ENTRY'));
                     return JError::raiseWarning(100, $this->get('error.message'));
                 }
                 $contents = '';
                 // reclaim some memory
             }
         }
     }
     $stream->close();
     return true;
 }
Example #3
0
 /**
  * Extract a Gzip compressed file to a given path
  *
  * @access	public
  * @param	string	$archive		Path to ZIP archive to extract
  * @param	string	$destination	Path to extract archive to
  * @param	array	$options		Extraction options [unused]
  * @return	boolean	True if successful
  * @since	1.5
  */
 function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('zlib')) {
         $this->set('error.message', 'Zlib Not Supported');
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     /*
     if (!$this->_data = JFile::read($archive)) {
     	$this->set('error.message', 'Unable to read archive');
     	return JError::raiseWarning(100, $this->get('error.message'));
     }
     
     $position = $this->_getFilePosition();
     $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position));
     if (empty ($buffer)) {
     	$this->set('error.message', 'Unable to decompress data');
     	return JError::raiseWarning(100, $this->get('error.message'));
     }
     
     if (JFile::write($destination, $buffer) === false) {
     	$this->set('error.message', 'Unable to write archive');
     	return JError::raiseWarning(100, $this->get('error.message'));
     }
     return true;
     */
     // New style! streams!
     $input =& JFactory::getStream();
     $input->set('processingmethod', 'gz');
     // use gz
     if (!$input->open($archive)) {
         $this->set('error.message', 'Unable to read archive (gz)');
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     $output =& JFactory::getStream();
     if (!$output->open($destination, 'w')) {
         $this->set('error.message', 'Unable to write archive (gz)');
         $input->close();
         // close the previous file
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     $written = 0;
     do {
         $this->_data = $input->read($input->get('chunksize', 8196));
         if ($this->_data) {
             if (!$output->write($this->_data)) {
                 $this->set('error.message', 'Unable to write file (gz)');
                 return JError::raiseWarning(100, $this->get('error.message'));
             }
         }
     } while ($this->_data);
     $output->close();
     $input->close();
     return true;
 }
Example #4
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string   $archive		Path to Bzip2 archive to extract
  * @param   string   $destination	Path to extract archive to
  * @param   array    $options		Extraction options [unused]
  *
  * @return  boolean  True if successful
  * @since   11.1
  */
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_NOT_SUPPORTED'));
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         // old style: read the whole file and then parse it
         if (!($this->_data = JFile::read($archive))) {
             $this->set('error.message', 'Unable to read archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $buffer = bzdecompress($this->_data);
         unset($this->_data);
         if (empty($buffer)) {
             $this->set('error.message', 'Unable to decompress data');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         if (JFile::write($destination, $buffer) === false) {
             $this->set('error.message', 'Unable to write archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         $input->set('processingmethod', 'bz');
         // use bzip
         if (!$input->open($archive)) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_READ'));
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE'));
             $input->close();
             // close the previous file
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $written = 0;
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $this->set('error.message', JText::_('JLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE_FILE'));
                     return JError::raiseWarning(100, $this->get('error.message'));
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Example #5
0
 /**
  * Extract a Gzip compressed file to a given path
  *
  * @param   string  $archive      Path to ZIP archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  *
  * @since   11.1
  */
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('zlib')) {
         $this->set('error.message', JText::_('JLIB_FILESYSTEM_GZIP_NOT_SUPPORTED'));
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         if (!($this->_data = JFile::read($archive))) {
             $this->set('error.message', 'Unable to read archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $position = $this->_getFilePosition();
         $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position));
         if (empty($buffer)) {
             $this->set('error.message', 'Unable to decompress data');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         if (JFile::write($destination, $buffer) === false) {
             $this->set('error.message', 'Unable to write archive');
             return JError::raiseWarning(100, $this->get('error.message'));
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         $input->set('processingmethod', 'gz');
         // use gz
         if (!$input->open($archive)) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_GZIP_UNABLE_TO_READ'));
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $this->set('error.message', JText::_('JLIB_FILESYSTEM_GZIP_UNABLE_TO_WRITE'));
             $input->close();
             // close the previous file
             return JError::raiseWarning(100, $this->get('error.message'));
         }
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $this->set('error.message', JText::_('JLIB_FILESYSTEM_GZIP_UNABLE_TO_WRITE_FILE'));
                     return JError::raiseWarning(100, $this->get('error.message'));
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Example #6
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public function extract($archive, $destination, array $options = array())
 {
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         throw new RuntimeException('The bz2 extension is not available.');
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         $this->_data = file_get_contents($archive);
         if (!$this->_data) {
             throw new RuntimeException('Unable to read archive');
         }
         $buffer = bzdecompress($this->_data);
         unset($this->_data);
         if (empty($buffer)) {
             throw new RuntimeException('Unable to decompress data');
         }
         if (JFile::write($destination, $buffer) === false) {
             throw new RuntimeException('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         // Use bzip
         $input->set('processingmethod', 'bz');
         if (!$input->open($archive)) {
             throw new RuntimeException('Unable to read archive (bz2)');
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new RuntimeException('Unable to write archive (bz2)');
         }
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $input->close();
                     throw new RuntimeException('Unable to write archive (bz2)');
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Example #7
0
 /**
  * Extract a Gzip compressed file to a given path
  *
  * @param   string  $archive      Path to ZIP archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public function extract($archive, $destination, array $options = array())
 {
     $this->_data = null;
     if (!extension_loaded('zlib')) {
         throw new RuntimeException('The zlib extension is not available.');
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         $this->_data = file_get_contents($archive);
         if (!$this->_data) {
             throw new RuntimeException('Unable to read archive');
         }
         $position = $this->_getFilePosition();
         $buffer = gzinflate(substr($this->_data, $position, strlen($this->_data) - $position));
         if (empty($buffer)) {
             throw new RuntimeException('Unable to decompress data');
         }
         if (JFile::write($destination, $buffer) === false) {
             throw new RuntimeException('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = JFactory::getStream();
         // Use gz
         $input->set('processingmethod', 'gz');
         if (!$input->open($archive)) {
             throw new RuntimeException('Unable to read archive (gz)');
         }
         $output = JFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new RuntimeException('Unable to write archive (gz)');
         }
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $input->close();
                     throw new RuntimeException('Unable to write file (gz)');
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Example #8
0
File: file.php Project: akksi/jcg
 /**
  * Moves an uploaded file to a destination folder
  *
  * @param string $src The name of the php (temporary) uploaded file
  * @param string $dest The path (including filename) to move the uploaded file to
  *
  * @return boolean True on success
  * @since 1.5
  */
 public static function upload($src, $dest, $use_streams = false)
 {
     // Ensure that the path is valid and clean
     $dest = JPath::clean($dest);
     // Create the destination directory if it does not exist
     $baseDir = dirname($dest);
     if (!file_exists($baseDir)) {
         jimport('joomla.filesystem.folder');
         JFolder::create($baseDir);
     }
     if ($use_streams) {
         $stream = JFactory::getStream();
         if (!$stream->upload($src, $dest)) {
             JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()));
             return false;
         }
         return true;
     } else {
         // Initialise variables.
         jimport('joomla.client.helper');
         $FTPOptions = JClientHelper::getCredentials('ftp');
         $ret = false;
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             jimport('joomla.client.ftp');
             $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
             //Translate path for the FTP account
             $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Copy the file to the destination directory
             if (is_uploaded_file($src) && $ftp->store($src, $dest)) {
                 unlink($src);
                 $ret = true;
             } else {
                 JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'));
             }
         } else {
             if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
                 // Short circuit to prevent file permission errors
                 if (JPath::setPermissions($dest)) {
                     $ret = true;
                 } else {
                     JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'));
                 }
             } else {
                 JError::raiseWarning(21, JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR02'));
             }
         }
         return $ret;
     }
 }
Example #9
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @access	public
  * @param	string	$archive		Path to Bzip2 archive to extract
  * @param	string	$destination	Path to extract archive to
  * @param	array	$options		Extraction options [unused]
  * @return	boolean	True if successful
  * @since	1.5
  */
 function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         $this->set('error.message', 'BZip2 Not Supported');
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     /* // old style: read the whole file and then parse it
     		if (!$this->_data = JFile::read($archive)) {
     			$this->set('error.message', 'Unable to read archive');
     			return JError::raiseWarning(100, $this->get('error.message'));
     		}
     
     		$buffer = bzdecompress($this->_data);
     		unset($this->_data);
     		if (empty ($buffer)) {
     			$this->set('error.message', 'Unable to decompress data');
     			return JError::raiseWarning(100, $this->get('error.message'));
     		}
     
     		if (JFile::write($destination, $buffer) === false) {
     			$this->set('error.message', 'Unable to write archive');
     			return JError::raiseWarning(100, $this->get('error.message'));
     		}
     		//*/
     // New style! streams!
     $input =& JFactory::getStream();
     $input->set('processingmethod', 'bz');
     // use bzip
     if (!$input->open($archive)) {
         $this->set('error.message', 'Unable to read archive (bz2)');
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     $output =& JFactory::getStream();
     if (!$output->open($destination, 'w')) {
         $this->set('error.message', 'Unable to write archive (bz2)');
         $input->close();
         // close the previous file
         return JError::raiseWarning(100, $this->get('error.message'));
     }
     $written = 0;
     do {
         $this->_data = $input->read($input->get('chunksize', 8196));
         if ($this->_data) {
             if (!$output->write($this->_data)) {
                 $this->set('error.message', 'Unable to write file (bz2)');
                 return JError::raiseWarning(100, $this->get('error.message'));
             }
         }
     } while ($this->_data);
     $output->close();
     $input->close();
     return true;
 }
Example #10
0
 public static function realMultipleUpload($frontEnd = 0)
 {
     $paramsC = JComponentHelper::getParams('com_phocagallery');
     $chunkMethod = $paramsC->get('multiple_upload_chunk', 0);
     $uploadMethod = $paramsC->get('multiple_upload_method', 1);
     JResponse::allowCache(false);
     // Chunk Files
     header('Content-type: text/plain; charset=UTF-8');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
     header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
     header("Cache-Control: no-store, no-cache, must-revalidate");
     header("Cache-Control: post-check=0, pre-check=0", false);
     header("Pragma: no-cache");
     // Invalid Token
     JRequest::checkToken('request') or jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 100, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_INVALID_TOKEN'))));
     // Set FTP credentials, if given
     $ftp = JClientHelper::setCredentialsFromRequest('ftp');
     $path = PhocaGalleryPath::getPath();
     $file = JRequest::getVar('file', '', 'files', 'array');
     $chunk = JRequest::getVar('chunk', 0, '', 'int');
     $chunks = JRequest::getVar('chunks', 0, '', 'int');
     $folder = JRequest::getVar('folder', '', '', 'path');
     // Make the filename safe
     if (isset($file['name'])) {
         $file['name'] = JFile::makeSafe($file['name']);
     }
     if (isset($folder) && $folder != '') {
         $folder = $folder . DS;
     }
     $chunkEnabled = 0;
     // Chunk only if is enabled and only if flash is enabled
     if ($chunkMethod == 1 && $uploadMethod == 1 || $frontEnd == 0 && $chunkMethod == 0 && $uploadMethod == 1) {
         $chunkEnabled = 1;
     }
     if (isset($file['name'])) {
         // - - - - - - - - - -
         // Chunk Method
         // - - - - - - - - - -
         // $chunkMethod = 1, for frontend and backend
         // $chunkMethod = 0, only for backend
         if ($chunkEnabled == 1) {
             // If chunk files are used, we need to upload parts to temp directory
             // and then we can run e.g. the condition to recognize if the file already exists
             // We must upload the parts to temp, in other case we get everytime the info
             // that the file exists (because the part has the same name as the file)
             // so after first part is uploaded, in fact the file already exists
             // Example: NOT USING CHUNK
             // If we upload abc.jpg file to server and there is the same file
             // we compare it and can recognize, there is one, don't upload it again.
             // Example: USING CHUNK
             // If we upload abc.jpg file to server and there is the same file
             // the part of current file will overwrite the same file
             // and then (after all parts will be uploaded) we can make the condition to compare the file
             // and we recognize there is one - ok don't upload it BUT the file will be damaged by
             // parts uploaded by the new file - so this is why we are using temp file in Chunk method
             $stream = JFactory::getStream();
             // Chunk Files
             $tempFolder = 'pgpluploadtmpfolder' . DS;
             $filepathImgFinal = JPath::clean($path->image_abs . $folder . strtolower($file['name']));
             $filepathImgTemp = JPath::clean($path->image_abs . $folder . $tempFolder . strtolower($file['name']));
             $filepathFolderFinal = JPath::clean($path->image_abs . $folder);
             $filepathFolderTemp = JPath::clean($path->image_abs . $folder . $tempFolder);
             $maxFileAge = 60 * 60;
             // Temp file age in seconds
             $lastChunk = $chunk + 1;
             $realSize = 0;
             // Get the real size - if chunk is uploaded, it is only a part size, so we must compute all size
             // If there is last chunk we can computhe the whole size
             if ($lastChunk == $chunks) {
                 if (JFile::exists($filepathImgTemp) && JFile::exists($file['tmp_name'])) {
                     $realSize = filesize($filepathImgTemp) + filesize($file['tmp_name']);
                 }
             }
             // 5 minutes execution time
             @set_time_limit(5 * 60);
             // usleep(5000);
             // If the file already exists on the server:
             // - don't copy the temp file to final
             // - remove all parts in temp file
             // Because some parts are uploaded before we can run the condition
             // to recognize if the file already exists.
             if (JFile::exists($filepathImgFinal)) {
                 if ($lastChunk == $chunks) {
                     @JFolder::delete($filepathFolderTemp);
                 }
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 108, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'))));
             }
             if (!PhocaGalleryFileUpload::canUpload($file, $errUploadMsg, $frontEnd, $chunkEnabled, $realSize)) {
                 // If there is some error, remove the temp folder with temp files
                 if ($lastChunk == $chunks) {
                     @JFolder::delete($filepathFolderTemp);
                 }
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 104, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_($errUploadMsg))));
             }
             // Ok create temp folder and add chunks
             if (!JFolder::exists($filepathFolderTemp)) {
                 @JFolder::create($filepathFolderTemp);
             }
             // Remove old temp files
             if (JFolder::exists($filepathFolderTemp)) {
                 $dirFiles = JFolder::files($filepathFolderTemp);
                 if (!empty($dirFiles)) {
                     foreach ($dirFiles as $fileS) {
                         $filePathImgS = $filepathFolderTemp . $fileS;
                         // Remove temp files if they are older than the max age
                         if (preg_match('/\\.tmp$/', $fileS) && filemtime($filepathImgTemp) < time() - $maxFileAge) {
                             @JFile::delete($filePathImgS);
                         }
                     }
                 }
             } else {
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 100, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_FOLDER_UPLOAD_NOT_EXISTS'))));
             }
             // Look for the content type header
             if (isset($_SERVER["HTTP_CONTENT_TYPE"])) {
                 $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
             }
             if (isset($_SERVER["CONTENT_TYPE"])) {
                 $contentType = $_SERVER["CONTENT_TYPE"];
             }
             if (strpos($contentType, "multipart") !== false) {
                 if (isset($file['tmp_name']) && is_uploaded_file($file['tmp_name'])) {
                     // Open temp file
                     $out = $stream->open($filepathImgTemp, $chunk == 0 ? "wb" : "ab");
                     //$out = fopen($filepathImgTemp, $chunk == 0 ? "wb" : "ab");
                     if ($out) {
                         // Read binary input stream and append it to temp file
                         $in = fopen($file['tmp_name'], "rb");
                         if ($in) {
                             while ($buff = fread($in, 4096)) {
                                 $stream->write($buff);
                                 //fwrite($out, $buff);
                             }
                         } else {
                             jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 101, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_OPEN_INPUT_STREAM'))));
                         }
                         $stream->close();
                         //fclose($out);
                         @JFile::delete($file['tmp_name']);
                     } else {
                         jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 102, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_OPEN_OUTPUT_STREAM'))));
                     }
                 } else {
                     jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 103, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_MOVE_UPLOADED_FILE'))));
                 }
             } else {
                 // Open temp file
                 $out = $stream->open($filepathImgTemp, $chunk == 0 ? "wb" : "ab");
                 //$out = JFile::read($filepathImg);
                 if ($out) {
                     // Read binary input stream and append it to temp file
                     $in = fopen("php://input", "rb");
                     if ($in) {
                         while ($buff = fread($in, 4096)) {
                             $stream->write($buff);
                         }
                     } else {
                         jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 101, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_OPEN_INPUT_STREAM'))));
                     }
                     $stream->close();
                     //fclose($out);
                 } else {
                     jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 102, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_OPEN_OUTPUT_STREAM'))));
                 }
             }
             // Rename the Temp File to Final File
             if ($lastChunk == $chunks) {
                 if (($imginfo = getimagesize($filepathImgTemp)) === FALSE) {
                     JFolder::delete($filepathFolderTemp);
                     jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 110, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_WARNING_INVALIDIMG'))));
                 }
                 if (!JFile::move($filepathImgTemp, $filepathImgFinal)) {
                     JFolder::delete($filepathFolderTemp);
                     jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 109, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_MOVE_FILE') . '<br />' . JText::_('COM_PHOCAGALLERY_CHECK_PERMISSIONS_OWNERSHIP'))));
                 }
                 JFolder::delete($filepathFolderTemp);
             }
             if ((int) $frontEnd > 0) {
                 return $file['name'];
             }
             jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'OK', 'code' => 200, 'message' => JText::_('COM_PHOCAGALLERY_SUCCESS') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_IMAGES_UPLOADED'))));
         } else {
             // No Chunk Method
             $filepathImgFinal = JPath::clean($path->image_abs . $folder . strtolower($file['name']));
             $filepathFolderFinal = JPath::clean($path->image_abs . $folder);
             if (!PhocaGalleryFileUpload::canUpload($file, $errUploadMsg, $frontEnd, $chunkMethod, 0)) {
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 104, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_($errUploadMsg))));
             }
             if (JFile::exists($filepathImgFinal)) {
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 108, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_FILE_ALREADY_EXISTS'))));
             }
             if (!JFile::upload($file['tmp_name'], $filepathImgFinal)) {
                 jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 109, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_UPLOAD_FILE') . '<br />' . JText::_('COM_PHOCAGALLERY_CHECK_PERMISSIONS_OWNERSHIP'))));
             }
             if ((int) $frontEnd > 0) {
                 return $file['name'];
             }
             jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'OK', 'code' => 200, 'message' => JText::_('COM_PHOCAGALLERY_SUCCESS') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_IMAGES_UPLOADED'))));
         }
     } else {
         // No isset $file['name']
         jexit(json_encode(array('jsonrpc' => '2.0', 'result' => 'error', 'code' => 104, 'message' => JText::_('COM_PHOCAGALLERY_ERROR') . ': ', 'details' => JText::_('COM_PHOCAGALLERY_ERROR_UNABLE_TO_UPLOAD_FILE'))));
     }
 }
Example #11
0
 /**
  * Moves a folder.
  *
  * @param   string   $src          The path to the source folder.
  * @param   string   $dest         The path to the destination folder.
  * @param   string   $path         An optional base path to prefix to the file names.
  * @param   boolean  $use_streams  Optionally use streams.
  *
  * @return  mixed  Error message on false or boolean true on success.
  *
  * @since   11.1
  */
 public static function move($src, $dest, $path = '', $use_streams = false)
 {
     // Initialise variables.
     $FTPOptions = JClientHelper::getCredentials('ftp');
     if ($path) {
         $src = JPath::clean($path . '/' . $src);
         $dest = JPath::clean($path . '/' . $dest);
     }
     if (!self::exists($src)) {
         return JText::_('JLIB_FILESYSTEM_ERROR_FIND_SOURCE_FOLDER');
     }
     if (self::exists($dest)) {
         return JText::_('JLIB_FILESYSTEM_ERROR_FOLDER_EXISTS');
     }
     if ($use_streams) {
         $stream = JFactory::getStream();
         if (!$stream->move($src, $dest)) {
             return JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_RENAME', $stream->getError());
         }
         $ret = true;
     } else {
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             jimport('joomla.client.ftp');
             $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
             //Translate path for the FTP account
             $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
             $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Use FTP rename to simulate move
             if (!$ftp->rename($src, $dest)) {
                 return JText::_('Rename failed');
             }
             $ret = true;
         } else {
             if (!@rename($src, $dest)) {
                 return JText::_('Rename failed');
             }
             $ret = true;
         }
     }
     return $ret;
 }
Example #12
0
 /**
  * Moves an uploaded file to a destination folder
  *
  * @param   string   $src              The name of the php (temporary) uploaded file
  * @param   string   $dest             The path (including filename) to move the uploaded file to
  * @param   boolean  $use_streams      True to use streams
  * @param   boolean  $allow_unsafe     Allow the upload of unsafe files
  * @param   boolean  $safeFileOptions  Options to JFilterInput::isSafeFile
  *
  * @return  boolean  True on success
  *
  * @since   11.1
  */
 public static function upload($src, $dest, $use_streams = false, $allow_unsafe = false, $safeFileOptions = array())
 {
     if (!$allow_unsafe) {
         $descriptor = array('tmp_name' => $src, 'name' => basename($dest), 'type' => '', 'error' => '', 'size' => '');
         $isSafe = JFilterInput::isSafeFile($descriptor, $safeFileOptions);
         if (!$isSafe) {
             JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR03', $dest), JLog::WARNING, 'jerror');
             return false;
         }
     }
     // Ensure that the path is valid and clean
     $pathObject = new JFilesystemWrapperPath();
     $dest = $pathObject->clean($dest);
     // Create the destination directory if it does not exist
     $baseDir = dirname($dest);
     if (!file_exists($baseDir)) {
         $folderObject = new JFilesystemWrapperFolder();
         $folderObject->create($baseDir);
     }
     if ($use_streams) {
         $stream = JFactory::getStream();
         if (!$stream->upload($src, $dest)) {
             JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_UPLOAD', $stream->getError()), JLog::WARNING, 'jerror');
             return false;
         }
         return true;
     } else {
         $FTPOptions = JClientHelper::getCredentials('ftp');
         $ret = false;
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             $ftp = JClientFtp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
             // Translate path for the FTP account
             $dest = $pathObject->clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Copy the file to the destination directory
             if (is_uploaded_file($src) && $ftp->store($src, $dest)) {
                 unlink($src);
                 $ret = true;
             } else {
                 JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), JLog::WARNING, 'jerror');
             }
         } else {
             if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
                 // Short circuit to prevent file permission errors
                 if ($pathObject->setPermissions($dest)) {
                     $ret = true;
                 } else {
                     JLog::add(JText::_('JLIB_FILESYSTEM_ERROR_WARNFS_ERR01'), JLog::WARNING, 'jerror');
                 }
             } else {
                 JLog::add(JText::sprintf('JLIB_FILESYSTEM_ERROR_WARNFS_ERR04', $src, $dest), JLog::WARNING, 'jerror');
             }
         }
         return $ret;
     }
 }
Example #13
0
 /**
  * Method to extract archive using stream objects
  *
  * @param   string  $archive      Path to ZIP archive to extract
  * @param   string  $destination  Path to extract archive to
  * @param   array   $options      Extraction options [unused]
  *
  * @return  boolean  True if successful
  */
 protected function extractStream($archive, $destination, $options = array())
 {
     // New style! streams!
     $input = JFactory::getStream();
     // Use gz
     $input->set('processingmethod', 'gz');
     if (!$input->open($archive)) {
         return $this->raiseWarning(100, 'Unable to read archive (gz)');
     }
     $output = JFactory::getStream();
     if (!$output->open($destination, 'w')) {
         $input->close();
         return $this->raiseWarning(100, 'Unable to write archive (gz)');
     }
     do {
         $this->_data = $input->read($input->get('chunksize', 8196));
         if ($this->_data && !$output->write($this->_data)) {
             $input->close();
             return $this->raiseWarning(100, 'Unable to write file (gz)');
         }
     } while ($this->_data);
     $output->close();
     $input->close();
     return true;
 }
Example #14
0
 /**
  * Copy a folder.
  *
  * @param   string   $src          The path to the source folder.
  * @param   string   $dest         The path to the destination folder.
  * @param   string   $path         An optional base path to prefix to the file names.
  * @param   boolean  $force        Force copy.
  * @param   boolean  $use_streams  Optionally force folder/file overwrites.
  *
  * @return  boolean  True on success.
  *
  * @since   11.1
  * @throws  RuntimeException
  */
 public static function copy($src, $dest, $path = '', $force = false, $use_streams = false)
 {
     @set_time_limit(ini_get('max_execution_time'));
     //if (Zend_Registry::isRegistered('logger')):
     //  $logger = Zend_Registry::get('logger');
     //endif;
     if ($path) {
         $src = JPath::clean($path . '/' . $src);
         $dest = JPath::clean($path . '/' . $dest);
     }
     // Eliminate trailing directory separators, if any
     $src = rtrim($src, DIRECTORY_SEPARATOR);
     $dest = rtrim($dest, DIRECTORY_SEPARATOR);
     if (!self::exists($src)) {
         throw new RuntimeException('Source folder not found', -1);
     }
     if (self::exists($dest) && !$force) {
         throw new RuntimeException('Destination folder not found', -1);
     }
     // Make sure the destination exists
     if (!self::create($dest)) {
         throw new RuntimeException('Cannot create destination folder', -1);
     }
     if (!($dh = @opendir($src))) {
         throw new RuntimeException('Cannot open source folder', -1);
     }
     // Walk through the directory copying files and recursing into folders.
     while (($file = readdir($dh)) !== false) {
         $sfid = $src . '/' . $file;
         $dfid = $dest . '/' . $file;
         switch (filetype($sfid)) {
             case 'dir':
                 if ($file != '.' && $file != '..') {
                     $ret = self::copy($sfid, $dfid, null, $force, $use_streams);
                     if ($ret !== true) {
                         return $ret;
                     }
                 }
                 break;
             case 'file':
                 if ($use_streams) {
                     $stream = JFactory::getStream();
                     if (!$stream->copy($sfid, $dfid)) {
                         throw new RuntimeException('Cannot copy file: ' . $stream->getError(), -1);
                     }
                 } else {
                     if (!@copy($sfid, $dfid)) {
                         throw new RuntimeException('Copy file failed', -1);
                     }
                 }
                 break;
         }
     }
     return true;
 }
Example #15
0
 /**
  * Moves a folder.
  *
  * @param string The path to the source folder.
  * @param string The path to the destination folder.
  * @param string An optional base path to prefix to the file names.
  * @return mixed Error message on false or boolean true on success.
  * @since 1.5
  */
 function move($src, $dest, $path = '', $use_streams = false)
 {
     // Initialise variables.
     jimport('joomla.client.helper');
     $FTPOptions = JClientHelper::getCredentials('ftp');
     if ($path) {
         $src = JPath::clean($path . DS . $src);
         $dest = JPath::clean($path . DS . $dest);
     }
     if (!JFolder::exists($src)) {
         return JText::_('Cannot find source folder');
     }
     if (JFolder::exists($dest)) {
         return JText::_('Folder already exists');
     }
     if ($use_streams) {
         $stream =& JFactory::getStream();
         if (!$stream->move($src, $dest)) {
             return JText::_('Rename failed') . ': ' . $stream->getError();
             //return JError::raiseError(-1, JText::_('Rename failed').': '. $stream->getError()));
         }
         $ret = true;
     } else {
         if ($FTPOptions['enabled'] == 1) {
             // Connect the FTP client
             jimport('joomla.client.ftp');
             $ftp =& JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
             //Translate path for the FTP account
             $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
             $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
             // Use FTP rename to simulate move
             if (!$ftp->rename($src, $dest)) {
                 return JText::_('Rename failed');
             }
             $ret = true;
         } else {
             if (!@rename($src, $dest)) {
                 return JText::_('Rename failed');
             }
             $ret = true;
         }
     }
     return $ret;
 }