CHips L MINI SHELL

CHips L pro

Current Path : /opt/cpanel/ea-php54/root/usr/share/pear/RVSeagullMod/lib/RVSGL/rvsLibs/
Upload File :
Current File : //opt/cpanel/ea-php54/root/usr/share/pear/RVSeagullMod/lib/RVSGL/rvsLibs/Archive.php

<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// WebSite:  http://www.rvglobalsoft.com
// Unauthorized copying is strictly forbidden and may result in severe legal action.
// Copyright (c) 2006 RV Global Soft Co.,Ltd. All rights reserved.
//
// =====YOU MUST KEEP THIS COPYRIGHTS NOTICE INTACT AND CAN NOT BE REMOVE =======
// Copyright (c) 2006 RV Global Soft Co.,Ltd. All rights reserved.
// This Agreement is a legal contract, which specifies the terms of the license
// and warranty limitation between you and RV Global Soft Co.,Ltd. and RV Site Builder.
// You should carefully read the following terms and conditions before
// installing or using this software.  Unless you have a different license
// agreement obtained from RV Global Soft Co.,Ltd., installation or use of this software
// indicates your acceptance of the license and warranty limitation terms
// contained in this Agreement. If you do not agree to the terms of this
// Agreement, promptly delete and destroy all copies of the Software.
//
// =====  Grant of License =======
// The Software may only be installed and used on a single host machine.
//
// =====  Disclaimer of Warranty =======
// THIS SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE PROVIDED "AS IS" AND
// WITHOUT WARRANTIES AS TO PERFORMANCE OF MERCHANTABILITY OR ANY OTHER
// WARRANTIES WHETHER EXPRESSED OR IMPLIED.   BECAUSE OF THE VARIOUS HARDWARE
// AND SOFTWARE ENVIRONMENTS INTO WHICH RV SITE BUILDER MAY BE USED, NO WARRANTY OF
// FITNESS FOR A PARTICULAR PURPOSE IS OFFERED.  THE USER MUST ASSUME THE
// ENTIRE RISK OF USING THIS PROGRAM.  ANY LIABILITY OF RV GLOBAL SOFT CO.,LTD. WILL BE
// LIMITED EXCLUSIVELY TO PRODUCT REPLACEMENT OR REFUND OF PURCHASE PRICE.
// IN NO CASE SHALL RV GLOBAL SOFT CO.,LTD. BE LIABLE FOR ANY INCIDENTAL, SPECIAL OR
// CONSEQUENTIAL DAMAGES OR LOSS, INCLUDING, WITHOUT LIMITATION, LOST PROFITS
// OR THE INABILITY TO USE EQUIPMENT OR ACCESS DATA, WHETHER SUCH DAMAGES ARE
// BASED UPON A BREACH OF EXPRESS OR IMPLIED WARRANTIES, BREACH OF CONTRACT,
// NEGLIGENCE, STRICT TORT, OR ANY OTHER LEGAL THEORY. THIS IS TRUE EVEN IF
// RV GLOBAL SOFT CO.,LTD. IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN NO CASE WILL
// RV GLOBAL SOFT CO.,LTD.'S LIABILITY EXCEED THE AMOUNT OF THE LICENSE FEE ACTUALLY PAID
// BY LICENSEE TO RV GLOBAL SOFT CO.,LTD.
// +---------------------------------------------------------------------------+
// $Id$
// +---------------------------------------------------------------------------+

/**
 * RvSiteBuilder Archive libary
 *
 * @package RvsLibs
 * @author  Pairote Manunphol <pairote@rvglobalsoft.com>
 * @version $Revision$
 * @since   PHP 5.1
 */
if (class_exists('RvsLibs_Archive') === false) {
	class RvsLibs_Archive
	{
		public static function extract($source, $target = null)
		{
			$compressType = RvsLibs_Archive::getCompressType($source);
			 
			switch ($compressType) {
				case 'tar':
				case 'bz2':
				case 'gz':
					return RvsLibs_Archive_Tar::extract($source, $target);
					break;
				case 'zip':
					return RvsLibs_Archive_Zip::extract($source, $target);
					break;
				default:
				    return SGL::raiseError(
					    RvsLibs_String::translate(
                               'invalid extrac file extention'
                               , 'vprintf'
                               ,array('FILETYPE' => $compressType)
                        )
                  );
			}
		}
		 
		/**
		 * Compress the data to a tar, gz, bz2 or zip format..
		 *
		 * @param String $srcDir source directory for created
		 * @param String $srcFolderName folder name for created
		 * @param String $compressType can be one of tar, gz, bz2, zip
		 * 				(default $compressType is gz(the extension file .tar.gz))
		 * @return true on success , PEAR error or false on failue.
		 */
		public static function compressionFile($srcDir, $srcFolderName, $compressType = "gz")
		{
			switch ($compressType) {
				case 'tar':
				case 'bz2':
				case 'gz':
					return RvsLibs_Archive_Tar::create($srcDir, $srcFolderName, $compressType);
					break;
				case 'zip':
					return RvsLibs_Archive_Zip::create($srcDir, $srcFolderName);
					break;
				default:
					return SGL::raiseError(
					    RvsLibs_String::translate(
                               'invalid compress file extention'
                               , 'vprintf'
                               ,array('FILETYPE' => $compressType)
                        )
                  );
			}
		}
		 
		public static function getCompressType($tarname)
		{
			$compressType = '';
			if (RvsLibs_String::dbeSubstr($tarname, -2) == 'gz' || RvsLibs_String::dbeSubstr($tarname, -3) == 'tgz') {
				$compressType = 'gz';
			} elseif ((RvsLibs_String::dbeSubstr($tarname, -3) == 'bz2') ||
			(RvsLibs_String::dbeSubstr($tarname, -2) == 'bz')) {
				$compressType = 'bz2';
			} elseif ((RvsLibs_String::dbeSubstr($tarname, -3) == 'tar')) {
				$compressType = 'tar';
			} elseif ((RvsLibs_String::dbeSubstr($tarname, -3) == 'zip')) {
				$compressType = 'zip';
			} else {
				//if cannot get compresstype goto match for return type
				$compressType = RvsLibs_Archive::_getCompressType2($tarname);
			}
			return $compressType;
		}
		 
		/**
		 * match นามสกุลที่ลงท้ายแล้ว return นามสกุลนั้นออกไป
		 *
		 * @param STRING $tarname
		 * @return STRING compressType
		 */
		public static function _getCompressType2($tarname)
		{
			$compressType = '';
			if (RvsLibs_String::preg_match('/(\.gz|\.tgz)$/i', $tarname)) {
				$compressType = 'gz';
			} elseif (RvsLibs_String::preg_match('/(\.bz|\.bz2)$/i', $tarname)) {
				$compressType = 'bz2';
			} elseif (RvsLibs_String::preg_match('/(\.tar)$/i', $tarname)) {
				$compressType = 'tar';
			} elseif (RvsLibs_String::preg_match('/(\.zip)$/i', $tarname)) {
				$compressType = 'zip';
			}
			return $compressType;
		}
		
		/**
		 * add backslash $source file
		 * 
		 * @param String $source
		 * @return String $source
		 */
		public static function _addBackslash($source)
		{
			$source = RvsLibs_String::preg_replace('/(\s)/', '\ ', $source);		
			return $source; 
		}		
	}
}

if (class_exists('RvsLibs_Archive_Tar') === false) {
	class RvsLibs_Archive_Tar
	{
		public static function create($srcDir, $srcFolderName, $compressType = 'tar')
		{
			@chdir($srcDir);
			if (RvsLibs_System::function_exists('system')) {
				return RvsLibs_Archive_Tar::_systemCompression($srcFolderName, $compressType);
			} else {
				return RvsLibs_Archive_Tar::_pearCompression($srcFolderName, $compressType);
			}
		}

		public static function extract($source, $target = null)
		{
			if (RvsLibs_System::function_exists('system')) {
				return RvsLibs_Archive_Tar::_systemArchiveTar($source, $target);
			} else {
				return RvsLibs_Archive_Tar::_PearArchiveTar($source, $target);
			}
		}

		public static function _systemArchiveTar($source, $target = null)
		{
			$compressType = RvsLibs_Archive::getCompressType($source);
			$source = RvsLibs_Archive::_addBackslash($source);
			
			SGL::logMessage("compressType===>". $compressType, PEAR_LOG_DEBUG);
			switch ($compressType) {
				case 'bz2':
					$opt = 'jxvf';
					break;
				case 'tar' :
					$opt = 'xvf';
					break;
				case 'gz':
					$cmdFileIsOk = 'gzip -t ' . $source;
					system("{$cmdFileIsOk}", $returnValueFileIsOk);
					// 0 is complete
					if ($returnValueFileIsOk != 0) {
						return SGL::raiseError("Sorry, your backup file $source is error.");
					}
					$opt = 'zxvf';
					break;
				default:
					return SGL::raiseError(
					    RvsLibs_String::translate(
                               'invalid extrac file extention'
                               , 'vprintf'
                               ,array('FILETYPE' => $compressType)
                        )
                  );
			}
			if (isset($target) === true) {
				$target = '-C ' . $target;
			}
			system("tar -{$opt} {$source} $target > /dev/null", $isOk);
			if ($isOk != 0) {
				return SGL::raiseError("command \"tar -{$opt} {$source} $target\" has return error code \"{$isOk}\".");
			} else {
				return true;
			}
		}

		public static function _pearArchiveTar($source, $target = null)
		{
			require_once SGL_LIB_PEAR_DIR . '/Archive/Tar.php';
			if ( !class_exists('Archive_Tar') ) {
				return SGL::raiseError("Archive_Tar class name invalid /Archive/Tar.php");
			}
			$tar = new Archive_Tar($source);
			//validate file is complete before extract
			$res = $tar->extract($target);
			if (SGL_Error::count() || !$res) {
				return SGL::raiseError("Could not extract files $source");
			} else {
				return true;
			}
		}

		public static function _pearCompression($srcFolderName, $compressType)
		{
			switch ($compressType) {
				case 'tar':
					$fileName = $srcFolderName . '.tar';
					$compressType = 'gz';
					break;
				case 'gz':
					$fileName = $srcFolderName . '.tar.gz';
					break;
				case 'bz2':
					$fileName = $srcFolderName . '.tar.bz2';
					break;
				default:
					return SGL::raiseError(
					    RvsLibs_String::translate(
                               'invalid compress file extention'
                               , 'vprintf'
                               ,array('FILETYPE' => $compressType)
                        )
                  );
			}
			require_once SGL_LIB_PEAR_DIR . '/Archive/Tar.php';
			if ( !class_exists('Archive_Tar') ) {
				return SGL::raiseError("Archive_Tar class name invalid /Archive/Tar.php");
			}
			$obj = new Archive_Tar($fileName, $compressType);
			$res = $obj->create(array($srcFolderName));
			if (SGL::isError($res) === true) {
				return $res;
			}
			return true;
		}

		public static function _systemCompression($srcFolderName, $compressType)
		{
			switch ($compressType) {
				case 'tar':
					$opt = '-cvf';
					$fileName = $srcFolderName . '.tar';
					break;
				case 'gz':
					$opt = '-czf';
					$fileName = $srcFolderName . '.tar.gz';
					break;
				case 'bz2':
					$opt = '-cjf';
					$fileName = $srcFolderName . '.tar.bz2';
					break;
				default:
					return SGL::raiseError(
					    RvsLibs_String::translate(
                               'invalid compress file extention'
                               , 'vprintf'
                               ,array('FILETYPE' => $compressType)
                        )
                  );
			}
			$command = 'tar ' . $opt . ' ' . $fileName . ' ' . $srcFolderName;
			system("$command > /dev/null", $returnValue);
			if ($returnValue === 0) {
				return true;
			} else {
				return SGL::raiseError(
				RvsLibs_String::translate(
                    '<font color=red>return val: %RETURN<br>returned an error: %COMMAND</font>'
                    , 'vprintf'
                    , array('RETURN' => $returnValue, 'COMMAND' => $command)
                    )
                    );
			}
		}

		public static function validateGzCompression($filename)
		{
			if (RvsLibs_System::function_exists('system') === true) {
				//validate gzip -t  for create file uncomplete
				$validateFileCmd = 'gzip -t ' . $filename;
				system("$validateFileCmd", $returnValue1);
				if ($returnValue1 != 0) {
					return false;
				}
				return true;
			} else {
				///Extract flie if complete return true else return false
				$res = RvsLibs_Archive_Tar::extract($filename);
				if (SGL::isError($res) === true) {
					return false;
				} else {
					///TODO :: delete extract file and return true
					return true;
				}
			}
		}
	}
}

if (class_exists('RvsLibs_Archive_Zip') === false) {
	class RvsLibs_Archive_Zip
	{
		public static function extract($source, $target)
		{
			require_once SGL_LIB_PEAR_DIR . '/Archive/Zip.php';
			if ( !class_exists('Archive_Zip') ) {
				return SGL::raiseError("Archive_Tar class name invalid /Archive/Zip.php");
			}
			$zip = new Archive_Zip($source);
			$aParams = array('add_path' => $target);
			$res = $zip->extract($aParams);
			if(SGL_Error::count() || !$res) {
				return SGL::raiseError("Could not extract files $source");
			} else {
				return true;
			}
		}

		public static function create($srcDir, $srcFolderName)
		{
			@chdir($srcDir);
			if (file_exists($srcDir) && $srcFolderName) {
				require_once SGL_LIB_PEAR_DIR . '/Archive/Zip.php';
				if ( !class_exists('Archive_Zip') ) {
					return SGL::raiseError("Archive_Zip class name invalid /Archive/Zip.php");
				}
				$obj = new Archive_Zip($srcFolderName . '.zip');
				$res = $obj->create($srcFolderName);
				if (SGL::isError($res) === true) {
					return $res;
				} else {
					return true;
				}
			}
		}
	}
}
?>

Copyright 2K16 - 2K18 Indonesian Hacker Rulez