All pastes #2059529 Raw Edit

Someone

public text v1 · immutable
#2059529 ·published 2011-05-14 21:49 UTC
rendered paste body
	public function process_upload()
	{
		if(isset($_REQUEST['browser_cookie']))
		{
			// We have a valid session here, lets get down to it.
			$data = urldecode($_REQUEST['browser_cookie']);
			preg_match("/\"([a-zA-Z0-9]{32})\"/", $data, $sid);
			
			$theSession = $sid[1]; // Now we should have a valid session id to lookup
			
			$this->db->where('session_id', $theSession);
			$query = $this->db->get('ci_sessions');
			
			foreach($query->result() as $val)
			{
				$sqlOutput = $val['user_data'];	
			}
			// Ok now here, Im not familliar with CI sql, so do the following sql query: SELECT user_data FROM ci_sessions WHERE session_id='$theSession'
		
			preg_match('/user_id";s:\d{1,}:"(\d{1,})"/', $sqlOutput, $uid);
			
			$user_id = $uid[1]; // $user_id is the uploaders USERID! phew..
			
			$fh = fopen("debug.txt", 'a+');
			fwrite($fh, "UserID: ".$user_id."\n");
			fclose($fh);

		}
		else
		{
			// We have no session, Show a warning message and redirect to upload? Just make sure you handle stuff here correctly
		}
		if(isset($_REQUEST['browser_cookie'])) setcookie("ci_session", $_REQUEST['browser_cookie']);
		
		
		$debug = $_COOKIE['ci_session'] . " ---- " . $this->g_auth->get_user_id() . " ------ " . $this->session->userdata('user_id')." COOKIE: " . $_REQUEST['browser_cookie'];
		$fh = fopen("debug.txt", "a+");
		fwrite($fh, $debug."\n");
		fclose($fh);


		/*
		* USEing Default code from image uploader
		*/
		if(isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post' && !empty($_POST))
		{
			//echo 'user id before upload'.$user_id;
			//restore session code begin. if you don't use session or cookie just ignor below code section.
			if( isset($_POST['browser_cookie']) && $_POST['browser_cookie'] != "")
			{
				//retrive cookie from form field value. The EAFlashUpload sends the cookie as a value of form field due to Flash API limitations.
				$cookie = split(";", $_POST['browser_cookie']);
				
				foreach($cookie as $value)
				{
					$nvpair = split("=", $value);	
					$parsedcookie[trim($nvpair[0])] = $nvpair[1];
				}
				$_COOKIE = $parsedcookie;
				
				
				
				session_start();
				//echo 'uid'.$parsedcookie['ci_session']['user_id'];	
				//echo print_r($parsedcookie['ci_session']).'<br><br>';
				//echo urldecode($_COOKIE['ci_session']);
			
				//echo "<br>";
				

			}
			//restore session code end
			//!!! If you don't use session or cookie then delete above code. !!!//
			
			if ( !empty($_FILES) )
			{
				//existing folder on the server for files storing with write access
				$uploaddir = dirname($_SERVER['SCRIPT_FILENAME'])."/UploadedFiles/";
				
				// define encoding for path names
				$codepage = "ISO-8859-1";
				
				//check file existence in the request
			
				$file = $_FILES["Filedata"];
				
				
				
				//check on upload errors
				if ( $file['error'] != UPLOAD_ERR_OK )
				{
					switch( $file['error'] )
					{
						case UPLOAD_ERR_INI_SIZE:
							echo "<eaferror>The uploaded file exceeds the upload_max_filesize directive in php.ini.</eaferror>";
							break;
						case UPLOAD_ERR_FORM_SIZE:
							echo "<eaferror>Uploader didn't allow such file size</eaferror>";
							break;
						case UPLOAD_ERR_PARTIAL:
							echo "<eaferror>Uploaded file hasn't been complete uploaded</eaferror>";
							break;
						case UPLOAD_ERR_NO_FILE:
							echo "<eaferror>File hasn't been uploaded</eaferror>";
							break;
					}
					
					return;
				}
				
				//Use this code if the existing files might be rewritten.
				//$uploadfile = $uploaddir . mb_convert_encoding( basename($file['name']), $codepage , 'UTF-8' );
			
				//define a full file path
				if (extension_loaded('mbstring'))
					$fileName = $uploaddir . mb_convert_encoding( basename($file['name']), $codepage , 'UTF-8' );
				else if (extension_loaded('iconv'))
					$fileName = $uploaddir . iconv("UTF-8", $codepage, basename($file['name']));
				else
				{
					echo "<eaferror>Please enable mbstring extension or iconv extension in your php.ini file.</eaferror>";	
				}
				
				// check on duplicate file names and if there is a file with the same name add "_(<counter>)" at the end of the name of the new file
				$uniqueFileName = $fileName;
				for($k = 1; $k < 50; $k++)
				{
					if(!file_exists($uniqueFileName))
					{
						break;
					}
					else
					{
						$pathInfo = pathinfo($fileName);
						$uniqueFileName = sprintf("%s/%s_(%s).%s", $pathInfo['dirname'], $pathInfo['filename'], $k, $pathInfo['extension']);
					}
			
				}
				
					$link = mysql_connect('localhost', 'thotis_ptd', 'hockey12');
					if (!$link) {
						die('Not connected : ' . mysql_error());
					}
					
					// make foo the current db
					$db_selected = mysql_select_db('thotis_ptd', $link);
					if (!$db_selected) {
						die ('Can\'t use foo : ' . mysql_error());
					}
					mysql_query("INSERT INTO test (name) VALUES('".basename($uniqueFileName)."')") or die(mysql_error());
					$id = mysql_insert_id();
				

				//move uploaded file from temp location	
				
				

				$hashed_id = $this->g_main->get_unique_hash($id);
				$file_name = basename($uniqueFileName);
				$file_size = round(filesize($_FILES['Filedata']['tmp_name'])/1024, 2);
				$extension = end(explode(".", $file_name)); 
				$description = $this->input->post('file_Description');
				
				$data = array(
							'hashed_id'=>$hashed_id , 
							'file_name'=>$file_name, 
							'name'=>'', 
							'size'=>$file_size, 
							'extension'=>$extension, 
							'description'=>$this->input->post('file_Description'), 
							'downloads'=>'0', 
							'user_id'=>$user_id, 
							'date_added'=>time(), 
							);
				
				$this->file_model->add_file($data);
				
				if ( move_uploaded_file( $file['tmp_name'], $uniqueFileName ) )
				{
					echo "File " . basename($uniqueFileName) . " has been uploaded!";
								
					// retrive form values
					if( isset($_POST['file_Description']) )
					{
						echo "<br />";
						echo "Description: " . $_POST['file_Description'];
					}
					
					if( isset($_POST['menu']) )
					{
						echo "<br />";
						echo "Some menu: " . $_POST['menu'];
					}
					
					if( isset($_POST['author']) )
					{

						echo 'User ID'.$user_id;
						
						echo $this->g_main->get_unique_hash($id);
						
						$idhash = substr(md5('22'),0,8);
						echo 'hash: '.$idhash.'<br>';
						echo "<br />";
						echo "Author: " . $_POST['author'];
					}
					
					echo "<hr />";
				}
				else
				{
					echo "<eaferror>Can't move file from temporary directory to destination. Please check read/write permissions of destination folder: $uploaddir.</eaferror>";
				}
				
			}
			else
			{
				echo "<eaferror>Request didn't contain the file. Usually this situation occures if request size exceeds the post_max_size directive in php.ini.</eaferror>";
			}
			exit;
		
			
		}	
	}