Your IP : 216.73.217.53


Current Path : /home/bonneesp/www/administrator/components/com_tabulizer/assets/classes/dialog/
Upload File :
Current File : /home/bonneesp/www/administrator/components/com_tabulizer/assets/classes/dialog/upload_csv_file.php

<?php
/**
 * @version		6.5.0 tabulizer $
 * @package		tabulizer
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author		Dimitrios Mourloukos
 * @author mail	info@alterora.gr
 * @website		www.tabulizer.com
 * 
 */


// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

class CSVUploader {

	function file_upload_error_message($error_code) {
		$error_msg = '';
		switch ($error_code) {
			case UPLOAD_ERR_INI_SIZE:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_INI_SIZE');
			case UPLOAD_ERR_FORM_SIZE:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_FORM_SIZE');
			case UPLOAD_ERR_PARTIAL:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_PARTIAL');
			case UPLOAD_ERR_NO_FILE:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_NO_FILE');
			case UPLOAD_ERR_NO_TMP_DIR:
				$error_msg = JText::_('COM_TABULIZER_Missing a temporary folder');
			case UPLOAD_ERR_CANT_WRITE:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_CANT_WRITE');
			case UPLOAD_ERR_EXTENSION:
				$error_msg = JText::_('COM_TABULIZER_UPLOAD_ERR_EXTENSION');
			default:
				$error_msg = 'Unknown upload error';
		}
		return $error_msg;
	} 
	
	function verifyUploadedFile(&$csv_filename, &$error_msg) {
        $jinput = JFactory::getApplication()->input;
		$uploaded_fieldname = 'csv_file';
        $file = $jinput->files->get($uploaded_fieldname);
        foreach ($file as $key => &$value) {
            $value = trim(str_replace('&gt;','',$value));
        }

        if ($file["error"] > 0) {
			$error_msg = sprintf(JText::_('COM_TABULIZER_CSV_UPLOAD_FAILED'),$this->file_upload_error_message($file["error"]));
			return false;
		} else {
			$uploaded_filename = $file['name'];
			$uploaded_file = $file['tmp_name'];
			$uploaded_size = $file["size"];
			$uploaded_type = strtolower($file["type"]);
			
			# sanity checks
			// 1. File size
			if ($uploaded_size > MAX_UPLOAD_CSV_FILE_SIZE) {
				$error_msg = sprintf(JText::_('COM_TABULIZER_CSV_UPLOAD_INVALID_FILE_SIZE'),MAX_UPLOAD_CSV_FILE_SIZE);
				return false;
			}
						
			// 2. File type
			/*
			// Note: the official mine type is 'application/vnd.ms-excel' but each browser could set it's own variation
			// We decided to comment this check, because it's easily bypassed by changing the extension (xls), which is already checked at the client side.
			$valid_mime_types = array('application/vnd.ms-excel', 'application/msexcel', 'application/x-msexcel', 'application/x-ms-excel', 'application/x-excel', 'application/x-dos_ms_excel', 'application/xls', 'application/octet-stream');						
			if (!in_array($uploaded_type, $valid_mime_types)) { 
				$error_msg = sprintf(JText::_('COM_TABULIZER_SHEET_UPLOAD_INVALID_FILE_TYPE'), $uploaded_type);
				return false;
			} 
			*/
			
			// 3. File extension
			$path_parts = pathinfo($uploaded_filename);
			$filename_ext = strtolower($path_parts['extension']);
			$valid_extensions = array('csv','txt','text');
			if (!in_array($filename_ext, $valid_extensions)) {
				$error_msg = sprintf(JText::_('COM_TABULIZER_CSV_UPLOAD_INVALID_FILE_EXT'), $filename_ext);
				return false;				
			}
			
			# Copy file to temp dir
			$import_hash = time() . mt_rand();
			$csv_filename = 'csv' . $import_hash . '.utemp.'. $filename_ext;
			$temp_filename_path = TabulizerPath::getFilePath($csv_filename, 'temp');
			if(!move_uploaded_file($uploaded_file, $temp_filename_path)) { 
				$error_msg = sprintf(JText::_('COM_TABULIZER_CSV_UPLOAD_MOVE_FAILED'), $temp_filename_path);
				return false;
			}								
		}
		
		return true;
	}

	function process() {
		$error_msg = null;
		$sheet_filename = null;
		
		if ($this->verifyUploadedFile($sheet_filename, $error_msg)) {
			$this->outputData($sheet_filename, $error_msg);
		} else {
			$this->outputData(null, $error_msg);
		}		
	}

	
	function outputData($filename, $error_msg) {
		if (!empty($error_msg)) {
			$output = json_encode(array("error_msg" => $error_msg));
		} else {
			$output = json_encode(array("filename" => $filename));
		}
		
		echo '<div id="upload_file_return">'.$output.'</div>';
	}
			
}

$form = new CSVUploader();
$form->process();

?>