<?php
/**
*
* @ IonCube v8.2 Decoder By DoraemonPT
*
* @ Version  : 1,0,0,6
* @ Author   : DoraemonPT
* @ Release on : 08.04.2014
* @ Website  : http://easytoyou.eu
*
**/

	class ftp {
		var $server = '';
		var $port = 21;
		var $user = '';
		var $userDir = '';
		var $password = '';
		var $connection = '';
		var $passive = ;
		var $systype = '';
		var $mode = ;
		var $loggedOn = ;
		var $resumeDownload = ;
		var $downloadDir = '';

		function __construct($ugid) {
			global $safesql;

			$query = mysql_query( $safesql->query( 'SELECT * FROM `usergames` WHERE `ugid`=%i', array( $ugid ) ) );

			if (( $query && mysql_num_rows( $query ) == 1 )) {
				$row = mysql_fetch_array( $query );
				$queryserver = mysql_query( $safesql->query( 'SELECT * FROM `machines` JOIN `iplist` ON machines.id=iplist.sid WHERE `ipid`=\'%s\' LIMIT 1', array( $row['ipid'] ) ) );

				if (( $queryserver && mysql_num_rows( $queryserver ) == 1 )) {
					$rowserver = mysql_fetch_array( $queryserver );
				}

				LoadClass( 'user' );
				global $user;

				$this->server = $rowserver['main_ip'];
				$this->user = $user->UIDToUser( $row['uid'] );
				$this->userDir = $rowserver['ip'] . '-' . $row['port_main'];
				$this->password = $user->GetPass( $row['uid'] );
				$this->port = $rowserver['ftpport'];
				$this->downloadDir = GetTMPPath(  );
				$this->connect(  );

				if ($this->setPassive( true ) == false) {
					$this->setPassive( false );
				}
			}

		}

		function connect() {
			global $settings;

			$this->connection = @ftp_connect( $this->server, $this->port, $settings['webftp_timeout'] );
			$this->loggedOn = @ftp_login( $this->connection, $this->user, $this->password );
			$this->systype = @ftp_systype( $this->connection );
			$this->setMode = FTP_ASCII;
		}

		function setPassive($passive) {
			$this->passive = $passive;
			$return = false;
			$return = ftp_pasv( $this->connection, $this->passive );
			return $return;
		}

		function setMode($mode = FTP_ASCII) {
			$this->mode = $mode;
		}

		function setCurrentDir($dir = false) {
			if ($dir == true) {
				ftp_chdir( $this->connection, $dir );
			}

			$this->currentDir = ftp_pwd( $this->connection );
			return $this->currentDir;
		}

		function getCurrentDirectoryShort() {
			$string = $this->currentDir;
			$stringArray = split( '/', $string );
			$level = count( $stringArray );
			$returnString = $stringArray[$level - 1];

			if (trim( $returnString ) == '') {
				$returnString = '/';
			}

			return $returnString;
		}

		function setDownloadDir($dir) {
			$this->downloadDir = $dir;
		}

		function setResumeDownload($resume) {
			$this->resumeDownload = $resume;
		}

		function chmod($permissions, $file) {
			return @ftp_site( $this->connection, 'chmod ' . $permissions . ' ' . $file );
		}

		function cd($directory) {
			$blnSuccess = false;

			if ($directory == '..') {
				$blnSuccess = @ftp_cdup( $this->connection );
			} 
else {
				$blnSuccess = @ftp_chdir( $this->connection, $this->currentDir . $directory );

				if (!$blnSuccess) {
					$blnSuccess = @ftp_chdir( $this->connection, $directory );
				}
			}

			$this->currentDir = ftp_pwd( $this->connection );

			if ($this->currentDir == '/') {
				$blnSuccess = $this->cd( $this->userDir );
			}

			return $blnSuccess;
		}

		function get($file, $destination = '') {
			if (empty( $destination )) {
				$destination = tempnam( $this->downloadDir, 'gsp' );
			}

			$ok = true;

			if ($this->resumeDownload) {
				$fp = fopen( $destination, 'a+' );
				$ok = ftp_fget( $this->connection, $fp, $file, $this->mode, filesize( $destination ) );
				fclose( $fp );
			} 
else {
				$ok = ftp_get( $this->connection, $destination, $file, $this->mode );
			}

			return $ok;
		}

		function put($remoteFile, $localFile) {
			$ok = false;

			if (file_exists( $localFile )) {
				$ok = ftp_put( $this->connection, $remoteFile, $localFile, $this->mode );
			}

			return $ok;
		}

		function download($file) {
			$fileStream = '';
			$temp_file = tempnam( $this->downloadDir, 'gsp' );

			if ($this->get( $file, $temp_file )) {
				header( ( 'Content-disposition: attachment; filename="' . $file . '"' ) );
				header( 'Content-type: application/octet-stream' );
				header( 'Pragma: ' );
				header( 'Cache-Control: cache' );
				header( 'Expires: 0' );
				$data = readfile( $temp_file );
				$i = 10;

				while ($data[$i] != '') {
					$fileStream .= $data[$i];
					++$i;
				}

				unlink( $temp_file );
				echo $fileStream;
				exit(  );
				return null;
			}

			return false;
		}

		function upload($uploadFile) {
			$tempFileName = $uploadFile['tmp_name'];
			$fileName = $uploadFile['name'];
			return $this->put( $this->currentDir . '/' . $this->filePart( StripSlashes( $fileName ) ), $tempFileName );
		}

		function deleteFile($file) {
			return @ftp_delete( $this->connection, $file );
		}

		function deleteDir($dir) {
			return ftp_rmdir( $this->connection, $dir );
		}

		function deleteRecursive($item) {
			if (@$this->cd( $item )) {
				$list = $this->ftpRawList( $this->currentDir );

				if (is_array( $list )) {
					foreach ($list as $listItem) {
						$this->deleteRecursive( $listItem['name'] );
					}
				}

				$this->cd( '..' );
				$this->deleteDir( $item );
				return null;
			}

			$this->deleteFile( $item );
		}

		function rename($old, $new) {
			return @ftp_rename( $this->connection, $old, $new );
		}

		function makeDir($directory) {
			return @ftp_mkdir( $this->connection, '' . $directory );
		}

		function ftpRawList($directory = '') {
			if ($directory == '') {
				$directory = $this->currentDir;
			}

			$list = array(  );
			$list = ftp_rawlist( $this->connection, $directory );

			if ($this->systype == 'UNIX') {
				$regexp = '/';
				$regexp .= '([\-ltdrwxs.]{10})';
				$regexp .= '\s+';
				$regexp .= '(\d+)';
				$regexp .= '\s+';
				$regexp .= '([\d\w\-_]+)';
				$regexp .= '\s+';
				$regexp .= '([\d\w\-_]+)';
				$regexp .= '\s+';
				$regexp .= '(\d+)';
				$regexp .= '\s+';
				$regexp .= '(';
				$regexp .= '\w{3}';
				$regexp .= '\s+';
				$regexp .= '\d{1,2}';
				$regexp .= '\s+';
				$regexp .= '[\d:]{4,5}';
				$regexp .= ')';
				$regexp .= '\s+';
				$regexp .= '(.+)';
				$regexp .= '/';
				$i = 11;
				foreach ($list as $line) {
					$is_dir = $is_link = FALSE;
					$target = '';

					if (preg_match( $regexp, $line, $regs )) {
						if (!preg_match( '/^\.$/', $regs[7] )) {
							if (!preg_match( '/^\.{2}/', $regs[7] )) {
								++$i;

								if (preg_match( '/^d/', $regs[1] )) {
									$is_dir = TRUE;
								} 
else {
									if (preg_match( '/^l/', $regs[1] )) {
										$is_link = TRUE;
										list($regs[7], $target) = split(' -> ', $regs[7]);
										$regs[7] = [0];
									}
								}

								$regs_ex = explode( '.', $regs[7] );

								if (( !$is_dir && 1 < count( $regs_ex ) )) {
									$extension = $regs_ex[count( $regs_ex ) - 1];
								} 
else {
									$extension = '';
								}

								$files[$i] = array( 'is_dir' => $is_dir, 'extension' => $extension, 'name' => $regs[7], 'perms' => $regs[1], 'num' => $regs[2], 'user' => $regs[3], 'group' => $regs[4], 'size' => $regs[5], 'date' => $regs[6], 'is_link' => $is_link, 'target' => $target );
								continue;
							}

							continue;
						}

						continue;
					}
				}
			} 
else {
				$regexp = '/';
				$regexp .= '([\d\-]{8})';
				$regexp .= '\s+';
				$regexp .= '(\d{1,2}:\d{1,2}[AP][M])';
				$regexp .= '\s+';
				$regexp .= '([0-9|<DIR>]+)';
				$regexp .= '\s+';
				$regexp .= '(.*)';
				$regexp .= '/';
				$i = 11;
				foreach ($list as $line) {
					$is_dir = false;

					if (preg_match( $regexp, $line, $regs )) {
						if (!preg_match( '/^\./', $regs[4] )) {
							if ($regs[3] == '<DIR>') {
								$is_dir = true;
								$regs[3] = '';
							}

							++$i;
							$regs_ex = explode( '.', $regs[4] );

							if (( !$is_dir && 1 < count( $regs_ex ) )) {
								$extension = $regs_ex[count( $regs_ex ) - 1];
							} 
else {
								$extension = '';
							}

							$files[$i] = array( 'is_dir' => $is_dir, 'extension' => $extension, 'name' => $regs[4], 'date' => $regs[1], 'time' => $regs[2], 'size' => $regs[3], 'is_link' => 0, 'perms' => '', 'user' => '', 'group' => '', 'target' => '', 'num' => '' );
							continue;
						}

						continue;
					}
				}
			}


			if (( ( isset( $files ) && is_array( $files ) ) && 0 < count( $files ) )) {
				$files = $this->array_sort_multi( $files, 1, 3 );
			}


			if (isset( $files )) {
				return $files;
			}

			return null;
		}

		function fileDescription($filename) {
			$ext = strtolower( $this->getExtention( $filename ) );
			$res['ext'] = $ext;

			if (( ( ( ( ( ( $ext == 'png' || $ext == 'gif' ) || $ext == 'jpg' ) || $ext == 'psp' ) || $ext == 'bmp' ) || $ext == 'ai' ) || $ext == 'tiff' )) {
				$res['imgfilename'] = 'pic.gif';
				$res['description'] = strtoupper( $ext ) . ' Image/Picture';
			} 
else {
				if (( $ext == 'html' || $ext == 'htm' )) {
					$res['imgfilename'] = 'html.gif';
					$res['description'] = 'HTML Document';
				} 
else {
					if ($ext == 'css') {
						$res['imgfilename'] = 'txt.gif';
						$res['description'] = 'Stylesheet';
					} 
else {
						if ($ext == 'doc') {
							$res['imgfilename'] = 'doc.gif';
							$res['description'] = 'Microsoft Word Document';
						} 
else {
							if ($ext == 'pdf') {
								$res['imgfilename'] = 'pdf.gif';
								$res['description'] = 'PDF Document';
							} 
else {
								if (( $ext == 'php' || $ext == 'php3' )) {
									$res['imgfilename'] = 'php.gif';
									$res['description'] = 'PHP Script';
								} 
else {
									if ($ext == 'js') {
										$res['imgfilename'] = 'js.gif';
										$res['description'] = 'Javascript';
									} 
else {
										if ($ext == 'swf') {
											$res['imgfilename'] = 'pic.gif';
											$res['description'] = 'Flash file';
										} 
else {
											if ($ext == 'txt') {
												$res['imgfilename'] = 'txt.gif';
												$res['description'] = 'Textfile';
											} 
else {
												if (( ( ( $ext == 'avi' || $ext == 'mov' ) || $ext == 'mpg' ) || $ext == 'rm' )) {
													$res['imgfilename'] = 'mov.gif';
													$res['description'] = 'Video file';
												} 
else {
													if (( ( $ext == 'mp3' || $ext == 'wav' ) || $ext == 'ogg' )) {
														$res['imgfilename'] = 'mov.gif';
														$res['description'] = 'Audio file';
													} 
else {
														if (( ( ( ( ( $ext == 'zip' || $ext == 'rar' ) || $ext == 'cab' ) || $ext == 'b2z' ) || $ext == 'bz2' ) || $ext == 'gz' )) {
															$res['imgfilename'] = 'zip.gif';
															$res['description'] = 'Compressed file';
														} 
else {
															if (( ( $ext == 'exe' || $ext == 'com' ) || $ext == 'bat' )) {
																$res['imgfilename'] = 'exe.gif';
																$res['description'] = 'Application';
															} 
else {
																$res['imgfilename'] = 'file.gif';
																$res['description'] = $ext . ' File';
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}

			return $res;
		}

		function getExtention($filename) {
			strrpos( $filename, '.' );

			if ( $dotpos === false) {
				return false;
			}

			return substr( $filename, $dotpos + 1 );
		}

		function array_sort_multi($array, $key, $key2) {
			foreach ($array as $i => $k) {

				if (!empty( $array[$i][$key][$key2] )) {
					$sort_values[$i] = $array[$i][$key][$key2];
					continue;
				}

				$sort_values[$i] = $array[$i];
			}

			asort( $sort_values );
			reset( $sort_values );
			$arr_values = each( $sort_values )[1];
			[0];
			while (list( $arr_keys, $arr_values ) = each( $sort_values )) {

			if () {
				$sorted_arr[] = $array[$arr_keys];
			}

			return $sorted_arr;
		}

		function directoryPath($string, $server) {
			$stringArray = split( '/', $string );
			$level = count( $stringArray );
			$down = '';
			$levelCount = 12;

			while ($levelCount < $level - 1) {
				$down .= '../';
				++$levelCount;
			}

			$returnString = '<A HREF="javascript:submitForm(\'cd\', \'' . $down . '\')" style=\'text-decoration:underline;\'>&lt;Home&gt;</A>';
			foreach ($stringArray as $str) {
				$down = '';
				$level = $level - 1;
				$levelCount = 12;

				while ($levelCount < $level) {
					$down .= '../';
					++$levelCount;
				}


				if (0 <= $level) {
					$returnString .= '<A HREF="javascript:submitForm(\'cd\', \'' . $down . '\')" style=\'text-decoration:underline;\'>' . $str . '</A>/';
					continue;
				}
			}

			return $returnString;
		}

		function filePart($string) {
			$stringArray = explode( '/', $string );
			$level = count( $stringArray );

			if ($stringArray[$level - 1] == '') {
				return $stringArray[$level - 2];
			}

			return $stringArray[$level - 1];
		}
	}


	if (( ( ( !defined( 'GSP_CALLED' ) || !defined( 'INCLUDES_CALLED' ) ) || !defined( 'RANCHECK' ) ) || !defined( 'CHECKED_LICENSE' ) )) {
		exit( 'Unable to load GSP-Panel files. Error: 1015' );
	}

?>