<?php


class fileUpload
	{
	
		public function upload_file($file_arr)
			{
				$filename 	= $file_arr['name'];
				$type 		= $file_arr['type'];
				$error 		= $file_arr['error'];
				$tmp_name 	= $file_arr['tmp_name'];
				$msg 		= '';
				$full_name 	= '';
				$file_name 	= '';
				
				if ($filename != '')
					{
						if ($error == 0)
							{
								if ($type == "image/png" || $type == "image/jpeg" || $type == "image/jpg" ||
									$type == "image/gif" || $type == "image/bmp" || $type == "application/pdf")
									{
										$fileformat = ($type == "image/jpeg") ? substr($filename, -4) : substr($filename, -3);
										$file_name 	= ($type == "image/jpeg") ? substr($filename, 0, -5) : substr($filename, 0, -4);
										
										$filename2 	= "file_" . $this->generatePassword(12) . "." . $fileformat;
										
										if (move_uploaded_file($tmp_name, "uploads/".$filename2)) {
										
											$full_name = $filename2;
										
										} else {
											
											$msg = "Error occured while uploading file.";
										}
									}
								else
									{
										$msg = "Error: Invalid file to upload.";
									}
							}
						else
							{
								$msg = "Error occured while uploading file.";
							}
					}
				else
					{
						$msg = "Error: Please select a file to upload.<br />";
					}
				
				return array($msg, $file_name, $full_name);
			}
		
		public function saveUploadFile ($file_name, $full_name, $loc_id)
			{
				if ($file_name == '' && $loc_id == '') return false;
				
				$sql = sprintf("insert into location_file set file_name = '%s', full_name = '%s', loc_id = '%s'", 
								$file_name, $full_name, $loc_id);
				
				if (mysql_query($sql))
					return true;
				else
					return false;
			}
		
		function getFileName($file_id)
			{
				$file_id = ($file_id != '') ? $file_id : 0;
				$sql = sprintf("select full_name from location_file where location_file_id = '%s'", $file_id);
				$res = mysql_query($sql);
				$row = mysql_fetch_array($res);
				return $row['full_name'];
			}
			
		public function deleteFile($file_id)
			{
				$file_id = ($file_id != '') ? $file_id : 0;
				$full_name = $this->getFileName($file_id);
				if (unlink('uploads/'.$full_name))
					{
						$sql = sprintf("delete from location_file where location_file_id = '%s'", $file_id);
						if (mysql_query($sql))
							return true;
						else
							return false;
					}
				else	
					{
						return false;
					}
			}
			
		public function getFilesHtml($loc_id)
			{
				
				$loc_id = ($loc_id != '') ? $loc_id : 0;
				$htm = '';
				$i=1;
				
				$sql = sprintf("select * from location_file where loc_id = '%s'", $loc_id);
				$res = mysql_query($sql);
				if (mysql_num_rows($res) > 0)
					{
						while($row = mysql_fetch_array($res))
							{
								$htm .= "<div id=\"file_".$i."\" style=\"border-bottom: 1px dashed rgb(236, 236, 236);\">
											<table width=95% cellpadding=0 cellspacing=2 class=text1ColB>
												<tr>
													<td>".$row['file_name']."</td>
													<td width=15 valign=middle>
														<a href=\"admin/uploads/".$row['full_name']."\" target=\"_blank\" title=\"Preview\">
															<img src=\"images/ico_preview.gif\" width=\"20\" height=\"17\" alt=\"Preview\" border=\"0\">
														</a>
													</td>
												  </tr>
											 </table>
										   </div>";
								$i ++;		  
							}
					}
				
				$htm = ($htm != '') ? $htm : "<table width=\"98%\" cellpadding=\"0\" cellspacing=\"2\" class=\"text1ColB\"><tr><td><font color=red>Files Not Found.</font></td></tr></table>";
				
				return htmlspecialchars($htm);
				
			}
			
		public function getUploadedFiles($loc_id)
			{
				$loc_id = ($loc_id != '') ? $loc_id : 0;
				$html = '';
				$i=1;
				
				$sql = sprintf("select * from location_file where loc_id = '%s'", $loc_id);
				$res = mysql_query($sql);
				if (mysql_num_rows($res) > 0)
					{
						while($row = mysql_fetch_array($res))
							{
								$html .= "<div id='file_".$i."'>
											<table width='98%' cellpadding='0' cellspacing='2' class='text1ColB'>
												<tr>
													<td>".$row['file_name']."</td>
													<td width=15 valign=middle>
														<a href='uploads/".$row['full_name']."' target='_blank' title='Preview'>
															<img src='images/ico_preview.gif' width='20' height='17' alt='Preview' border='0'>
														</a>
													</td>
													<td width=15 valign=middle>
														<a href='javascript: void(0);' onclick='deleteFile(".$row['location_file_id'].", ".$i.");' title='Delete'>
															<img name='image" .$i. "' src='images/ico_del.gif' width='15' height='15' alt='Delete' border='0' onmouseover='image" .$i. ".src="."&#39;"."images/ico_del_on.gif"."&#39;"."' onmouseout='image" .$i. ".src="."&#39;"."images/ico_del.gif"."&#39;"."' >
														</a>
													</td>
												  </tr>
											 </table>
										   </div>";
								$i ++;		  
							}
					}
				
				$html = ($html != '') ? $html : "<table width='98%' cellpadding='0' cellspacing='2' class='text1ColB'><tr><td><font color=red>Files Not Found.</font></td></tr></table>";
				
				return array($html, $i);	
			}
			
		/*
		** function to generate a randam string of given length 
		*/
		function generatePassword($len)
			{
				$al = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
				$date = date("Hs");
				$password = "$date";
						
				for ($index = 1; $index <= $len; $index++) 
					{
						$randomNumber = rand(1,strlen($al));
						$password .= substr($al,$randomNumber-1,1);
					}
				
				return $password;
						
			}		
	}
	
?>