uploadFile.php 11.3 KB
<?php
 /**
 * @file uploadFile
 * @brief
 * @author Elena
 * @version $Id: uploadFile.php 2905 2015-05-18 10:08:24Z elena $
 *
 */

        function file_upload_error_message($error_code) {
        
            switch ($error_code) {
                case UPLOAD_ERR_INI_SIZE:
                    return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                case UPLOAD_ERR_FORM_SIZE:
                    return 'The uploaded file exceeds max file size of '.maxSize;
                case UPLOAD_ERR_PARTIAL:
                    return 'The uploaded file was only partially uploaded';
                case UPLOAD_ERR_NO_FILE:
                    return 'No file was uploaded';
                case UPLOAD_ERR_NO_TMP_DIR:
                    return 'Missing a temporary folder';
                case UPLOAD_ERR_CANT_WRITE:
                    return 'Failed to write file to disk';
                case UPLOAD_ERR_EXTENSION:
                    return 'File upload stopped by extension';
                case UPLOAD_ERR_EMPTY: 
                    return 'File is empty';
                default:
                    return 'Unknown upload error';
            }
        } 
 
        function getUrlFileSize($URL) {
                $ch = curl_init($URL);
                curl_setopt($ch, CURLOPT_NOBODY, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HEADER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                $data = curl_exec($ch);
                curl_close($ch);

                $ContentLength = 1000000000000000000;
                if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
                $ContentLength = (int)$matches[1];
                }
        return $ContentLength;
        }

    /**
    *  Main
    */ 
        define("UPLOAD_ERR_EMPTY",5);

        require_once 'config.php';

       // error_reporting(E_ERROR | E_WARNING | E_PARSE);

        if (!isset($_POST['sessionID'])) 
        {
            $response = array( 'success' => false, 'error' => ': Check that uploaded file size is less than '.ini_get('upload_max_filesize')); 
            die(json_encode($response));           
        }

        define('USERDATADIR', USERPATH."/".$_POST['sessionID']."/DATA/");
        define('USERTEMPDIR', USERPATH."/".$_POST['sessionID']."/TEMP/");
        define('USERWSDIR', USERPATH."/".$_POST['sessionID']."/WS/"); 
        define('USERWORKINGDIR', USERPATH."/".$_POST['sessionID']."/RES/");
        define('ATTACHMENTDIR', DATAPATH."/Feedback/Attach/");
        define('maxSize',$_POST['MAX_FILE_SIZE'] < ini_get('upload_max_filesize'));

        if (!is_dir(USERTEMPDIR)) mkdir(USERTEMPDIR.'/', 0755, true);
        if (!is_dir(ATTACHMENTDIR)) mkdir(ATTACHMENTDIR.'/', 0755, true);

        if (!$_FILES['attachment'] && !$_FILES['localFileName'] && !$_FILES['localTTName'] && 
            !$_POST['remoteFile'] && !$_POST['remoteTT']) 
        {
            $response = array( 'success' => false, 'error' => 'UNDEFINED ACTION'); 
            die(json_encode($response));
        }
 
        $fileFrmt = $_POST['filefrmt'];
        $timeFrmt = $_POST['timefrmt'] ?  $_POST['timefrmt'] : null;
        $timeSmplg = $_POST['timesmpl'] ?  $_POST['timesmpl'] : null;
        $nonStd = $_POST['nonstd'] ?  $_POST['nonstd'] : null;
        $timeLength = $_POST['timelength'] ?  $_POST['timelength'] : null;
        $doy = $_POST['doy'] ?  $_POST['doy'] : null;

        $allFormats = array('fileFormat' => $fileFrmt, 'timeFormat' => $timeFrmt, 'doy' => $doy,
                            'timeSampling' => $timeSmplg, 'nonStandard' => $nonStd, 'timeLength' => $timeLength);
 
        if ($_POST['filesrc'] == 'URL') 
                                $fromURL = true;  
        else  
                                $fromURL = false;
        // to check ws sizw
        $wsMgr = new UserMgr();

        if ($fromURL) 
        {
        // url files check
            if ($_POST['remoteFile']) 
            {
                $remoteName = $_POST['remoteFile'];
                $fileName = substr(strrchr($remoteName,"/"),1);
                $localName = USERDATADIR.$fileName; 		 
                $isFile = true;
            }
            else 
            {
                $remoteName = $_POST['remoteTT'];
                $fileName = substr(strrchr($remoteName,"/"),1);
                $localName = USERTEMPDIR.$fileName; 		 		 	     
                $isFile = false;
            }
      
            $url = null;

            if (substr($remoteName,0,4) == 'http')  
                                                $url = 'http';
            if (substr($remoteName,0,3) == 'ftp') 
                                                $url = 'ftp';

            if (!$url) 
            {
                $response = array( 'success' => false, 'error' => 'Unknown net protocol'); 
                die(json_encode($response));
            }

            if (file_exists($localName) && $isFile) 
            {
                $response = array( 'success' => false, 'error' => 'File  '.$fileName.' exists'); 
                die(json_encode($response));		 
            }

            if ($url == 'ftp') 
                                $fileSize = filesize($remoteName);
            else 
                                $fileSize = getUrlFileSize($remoteName);

            if (!$fileSize) 
            {
                $response = array( 'success' => false, 'error' => 'Can\'t estimate file size '.$fileName); 
                die(json_encode($response));
            }
               
            if ($file['size'] > DISK_QUOTA) 
            {
                $response = array( 'success' => false, 'error' => 'The file you selected is too big for allowed disk quota'); 
                die(json_encode($response));
            }
             
            if ($wsMgr->getWsSize() + $fileSize > DISK_QUOTA && $isFile) 
            {
                $response = array( 'success' => false, 'error' => 'Please clean up you workspace. You are about to exceed available disk space'); 
                die(json_encode($response));
            }

            if ($fileSize > maxSize) 
            {
                $response = array( 'success' => false, 'error' => 'The uploaded file exceeds '.maxSize); 
                die(json_encode($response));
            }

            if (!copy($remoteName, $localName)) 
            {
                $response = array( 'success' => false, 'error' => 'Can\'t copy '.$fileName); 
                die(json_encode($response));
            }

            if (is_executable($localName))
            {
                $response = array( 'success' => false, 'error' => 'File '.$fileName.' is executable');		   
                unlink($localName);
                die(json_encode($response));		   
            }

            if ($isFile) 
            {
                $fileMgr = new FilesMgr();
                $amdaStat = new AmdaStats($_POST['sessionID']);
                if ($amdaStat->success)
                    $amdaStat->addTask('upload', $_POST['sessionID'], null);
            }
            // Time Table
            else 
            {
                $response = array( 'success' => true, 'file' => $fileName, 'format' => $_POST['ttfrmt'] );	  	     
                die(json_encode($response)); 
            }      
        }
        else 
        {
            // local files check
            if ($_FILES['localFileName']) 
            {
                $file = $_FILES['localFileName']; 
                $localName = USERDATADIR.$file['name'];
                $isFile = true;
            }
            else if ($_FILES['attachment']) 
            {
                if (!is_dir(ATTACHMENTDIR))
			mkdir(ATTACHMENTDIR,755);
			
                $file = $_FILES['attachment'];
                $file['name'] = str_replace(" ","_",$file['name']);
                $file['name'] = $_POST['sessionID'].'_'.date("Y-m-d\TH:i:s").'_'.$file['name'];
                $localName = ATTACHMENTDIR.$file['name'];
                $isFile = false;
            }
            else 
            {
                $file = $_FILES['localTTName'];
                $localName = USERTEMPDIR.$file['name'];		     
                $isFile = false;
            }
 	
            if($file['size'] === 0 && $file['error'] === 0){
                $file['error'] = 5;
            }

            if ($file['error'] !== 0) 
            {
                $response = array( 'success' => false, 'error' => file_upload_error_message($file['error'])); 		   
                die(json_encode($response));
            }

            if (file_exists($localName) && $isFile) 
            {
                $response = array( 'success' => false, 'error' => 'File  '.$file['name'].' exists'); 		  
                die(json_encode($response));
            }
      
            if (is_executable($file['tmp_name']))
            {
                $response = array( 'success' => false, 'error' => 'File '.$file['name'].' is executable'); 		  
                unlink($file['tmp_name']);
                die(json_encode($response));		   
            }

            if (!is_uploaded_file($file['tmp_name'])) 
            {
                $response = array( 'success' => false, 'error' => 'File '.$file['name'].' WASN\'T UPLOADED');		   
                unlink($file['tmp_name']);
                die(json_encode($response));		    
            }	 
                
            if ($file['size'] > DISK_QUOTA) 
            {
                $response = array( 'success' => false, 'error' => 'The file you selected is too big for allowed disk quota'); 
                die(json_encode($response));
            }

            if ($wsMgr->getWsSize() + $file['size'] > DISK_QUOTA) 
            {
                $response = array( 'success' => false, 'error' => 'Please clean up your workspace. You are about to exceed available disk space'); 
                die(json_encode($response));
            }
                    
            if (!rename($file['tmp_name'], $localName)) 
            {          
                $response = array( 'success' => false, 'error' => 'Cannot copy file '.$file['name']);		    
                unlink($file['tmp_name']);
                die(json_encode($response));		    
            }
 	  
            if (file_exists($localName) && !$isFile) 
            {
                if ($_POST['ttfrmt'] == 'VOT') 
                {
                    $votMgr = new VOTableMgr();
                    if (!$votMgr->load($localName) || !$votMgr->isValidSchema())
                    {	   	
                        $response = array( 'success' => false, 'error' => 'File  '.$file['name'].' is not valid'); 			   
                        unlink($localName);
                        die(json_encode($response));
                    } 
                }  
            }

            $fileName = $file['name'];

            if ($isFile) 
            {
                $fileMgr = new FilesMgr();
                $amdaStat = new AmdaStats($_POST['sessionID']);
                if ($amdaStat->success)
                    $amdaStat->addTask('upload', $_POST['sessionID'], null);
            }
            // Time Table
            else 
            {
                $response = array( 'success' => true, 'file' => $fileName, 'format' => $_POST['ttfrmt'] );	  	    
                die(json_encode($response)); 
            } 
        }
 	 
        $response = $fileMgr->addFile($fileName, $allFormats);
 	         
    echo json_encode($response);
?>