english/components/com_aicontactsafe/models/message.php
// function to upload an attached file
function to upload an attached file
function to upload an attached file
function uploadFile() {
		$pf = JRequest::getVar('pf', 0, 'request', 'int');
		$field = JRequest::getVar('field', '', 'request', 'cmd');
		$r_id = JRequest::getVar('r_id', 0, 'request', 'int');
		$errorType = 0;
		$errorMessage = '';
		$attachedFileId = 0;
		$file_name = '';
		$file = JRequest::getVar( $field, 'post', 'files', 'array' );
		$file['name'] = JFilterInput::clean($file['name'], 'cmd');
		if ( strlen(trim($file['name'])) > 0 ) {
			jimport('joomla.filesystem.path');
			jimport('joomla.filesystem.file');
			$format = strtolower(JFile::getExt($file['name']));
			$allowable = explode( ',', $this->_config_values['attachments_types']);
			$fileError = $_FILES[$field]['error'];
			$errorMessage = $_FILES[$field]['error'];
			if ($fileError > 0) {
				$errorType = 3;
		        switch ($fileError) {
					case 1:
						$errorMessage = JText::_('COM_AICONTACTSAFE_FILE_TO_LARGE_THAN_PHP_INI_ALLOWS');
						break;
					case 2:
						$errorMessage = JText::_('COM_AICONTACTSAFE_FILE_TO_LARGE_THAN_HTML_FORM_ALLOWS');
						break;
					case 3:
						$errorMessage = JText::_('COM_AICONTACTSAFE_ERROR_PARTIAL_UPLOAD');
						break;
			        case 4:
						$errorMessage = JText::_('COM_AICONTACTSAFE_ERROR_NO_FILE');
						break;
			        case 6:
						$errorMessage = JText::_('COM_AICONTACTSAFE_MISSING_THE_TEMPORARY_FOLDER');
						break;
			        case 7:
						$errorMessage = JText::_('COM_AICONTACTSAFE_FAILED_TO_WRITE_FILE_TO_DISK');
						break;
			        case 8:
						$errorMessage = JText::_('COM_AICONTACTSAFE_A_PHP_EXTENSION_STOPPED_THE_FILE_UPLOAD_PHP_DOES_NOT_PROVIDE_A_WAY_TO_ASCERTAIN_WHICH_EXTENSION_CAUSED_THE_FILE_UPLOAD_TO_STOP;_EXAMINING_THE_LIST_OF_LOADED_EXTENSIONS_WITH_PHPINFO() may help.' );
						break;
				}
			} else if (strlen(trim($this->_config_values['attachments_types']))>0 && !in_array($format, $allowable)) {
				$errorType = 1;
				if (strlen($invalid_format_file) > 0) {
					$invalid_format_file .= ', ';
				}
				$invalid_format_file .= $format;
				$errorMessage = $invalid_format_file;
			} else {
				$maxSize = (int) $this->_config_values['maximum_size'];
				if ($maxSize > 0 && (int) $file['size'] > $maxSize) {
					$errorType = 2;
					if (strlen($files_to_big) > 0) {
						$files_to_big .= ', ';
					}
					$files_to_big .= JFile::getName($file['name']);
					$errorMessage = $files_to_big;
				}
			}
			if ( $errorType == 0 ) {
				$upload_folder = str_replace('\\',DS,$this->_config_values['upload_attachments']);
				$upload_folder = str_replace('/',DS,$upload_folder);
				$upload_folder = str_replace('&#92;',DS,$upload_folder);
				$path_upload = JPATH_ROOT.DS.$upload_folder;
				$newFile =& JTable::getInstance('aicontactsafe_messagefiles', 'Table');
				$newFile->id = null;
				$newFile->message_id = 0;
				$newFile->name = '';
				$newFile->r_id = $r_id;
				$datenow =& JFactory::getDate();
				$newFile->date_added = $datenow->toMySQL();
				$newFile->last_update = $datenow->toMySQL();
				$newFile->published = 1;
				$newFile->checked_out = 0;
				$newFile->checked_out_time = '0000-00-00';
				if ($newFile->store()) {
					$file_ext = JFile::getExt($file['name']);
					$file_stem = JFile::stripExt($file['name']);
					$file_stem = str_replace(' ','_',$file_stem);
					$file_name = JFile::makeSafe(strtolower($file_stem.'_'.$newFile->id.'.'.$file_ext));
					$attachedFileId = $newFile->id;
					$db = &JFactory::getDBO();
					$query = 'UPDATE #__aicontactsafe_messagefiles SET name = \'' . $file_name . '\' WHERE id = ' . $newFile->id;
					$db->setQuery($query);
					$db->query();
					$filepath = JPath::clean($path_upload.DS.$file_name);
					JFile::upload($file['tmp_name'], $filepath);
				}
			}
		}
		$script = "
			<script language=\"javascript\" type=\"text/javascript\">
				this.parent.endUploadFile(".$pf.", '".$field."', '".$file_name."', ".$attachedFileId.", ".$errorType.", '".$errorMessage."');
			</script>
		";
		echo 'Attached file id '.$attachedFileId.'<hr />Error type '.$errorType.'<hr />';
		if ( $errorType > 0 ) {
			echo 'Error message '.$errorMessage.'<hr />';
		}
		echo $script;
	}
function uploadFile ( ) { $ pf = JRequest :: getVar ( 'pf' , 0 , 'request' , 'int' ) ; $ field = JRequest :: getVar ( 'field' , '' , 'request' , 'cmd' ) ; $ r_id = JRequest :: getVar ( 'r_id' , 0 , 'request' , 'int' ) ; $ errorType = 0 ; $ errorMessage = '' ; $ attachedFileId = 0 ; $ file_name = '' ; $ file = JRequest :: getVar ( $ field , 'post' , 'files' , 'array' ) ; $ file [ 'name' ] = JFilterInput :: clean ( $ file [ 'name' ] , 'cmd' ) ; if ( strlen ( trim ( $ file [ 'name' ] ) ) > 0 ) { jimport ( 'joomla.filesystem.path' ) ; jimport ( 'joomla.filesystem.file' ) ; $ format = strtolower ( JFile :: getExt ( $ file [ 'name' ] ) ) ; $ allowable = explode ( ',' , $ this -> _config_values [ 'attachments_types' ] ) ; $ fileError = $ _FILES [ $ field ] [ 'error' ] ; $ errorMessage = $ _FILES [ $ field ] [ 'error' ] ; if ( $ fileError > 0 ) { $ errorType = 3 ; switch ( $ fileError ) { case 1 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_FILE_TO_LARGE_THAN_PHP_INI_ALLOWS' ) ; break ; case 2 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_FILE_TO_LARGE_THAN_HTML_FORM_ALLOWS' ) ; break ; case 3 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_ERROR_PARTIAL_UPLOAD' ) ; break ; case 4 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_ERROR_NO_FILE' ) ; break ; case 6 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_MISSING_THE_TEMPORARY_FOLDER' ) ; break ; case 7 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_FAILED_TO_WRITE_FILE_TO_DISK' ) ; break ; case 8 : $ errorMessage = JText :: _ ( 'COM_AICONTACTSAFE_A_PHP_EXTENSION_STOPPED_THE_FILE_UPLOAD_PHP_DOES_NOT_PROVIDE_A_WAY_TO_ASCERTAIN_WHICH_EXTENSION_CAUSED_THE_FILE_UPLOAD_TO_STOP;_EXAMINING_THE_LIST_OF_LOADED_EXTENSIONS_WITH_PHPINFO() may help.' ) ; break ; } } else if ( strlen ( trim ( $ this -> _config_values [ 'attachments_types' ] ) ) > 0 && ! in_array ( $ format , $ allowable ) ) { $ errorType = 1 ; if ( strlen ( $ invalid_format_file ) > 0 ) { $ invalid_format_file .= ', ' ; } $ invalid_format_file .= $ format ; $ errorMessage = $ invalid_format_file ; } else { $ maxSize = ( int ) $ this -> _config_values [ 'maximum_size' ] ; if ( $ maxSize > 0 && ( int ) $ file [ 'size' ] > $ maxSize ) { $ errorType = 2 ; if ( strlen ( $ files_to_big ) > 0 ) { $ files_to_big .= ', ' ; } $ files_to_big .= JFile :: getName ( $ file [ 'name' ] ) ; $ errorMessage = $ files_to_big ; } } if ( $ errorType == 0 ) { $ upload_folder = str_replace ( '\\' , DS , $ this -> _config_values [ 'upload_attachments' ] ) ; $ upload_folder = str_replace ( '/' , DS , $ upload_folder ) ; $ upload_folder = str_replace ( '&#92;' , DS , $ upload_folder ) ; $ path_upload = JPATH_ROOT . DS . $ upload_folder ; $ newFile = & JTable :: getInstance ( 'aicontactsafe_messagefiles' , 'Table' ) ; $ newFile -> id = null ; $ newFile -> message_id = 0 ; $ newFile -> name = '' ; $ newFile -> r_id = $ r_id ; $ datenow = & JFactory :: getDate ( ) ; $ newFile -> date_added = $ datenow -> toMySQL ( ) ; $ newFile -> last_update = $ datenow -> toMySQL ( ) ; $ newFile -> published = 1 ; $ newFile -> checked_out = 0 ; $ newFile -> checked_out_time = '0000-00-00' ; if ( $ newFile -> store ( ) ) { $ file_ext = JFile :: getExt ( $ file [ 'name' ] ) ; $ file_stem = JFile :: stripExt ( $ file [ 'name' ] ) ; $ file_stem = str_replace ( ' ' , '_' , $ file_stem ) ; $ file_name = JFile :: makeSafe ( strtolower ( $ file_stem . '_' . $ newFile -> id . '.' . $ file_ext ) ) ; $ attachedFileId = $ newFile -> id ; $ db = & JFactory :: getDBO ( ) ; $ query = 'UPDATE #__aicontactsafe_messagefiles SET name = \'' . $ file_name . '\' WHERE id = ' . $ newFile -> id ; $ db -> setQuery ( $ query ) ; $ db -> query ( ) ; $ filepath = JPath :: clean ( $ path_upload . DS . $ file_name ) ; JFile :: upload ( $ file [ 'tmp_name' ] , $ filepath ) ; } } } $ script = " 
			<script language= \" javascript \"  type= \" text/javascript \" >
				this.parent.endUploadFile( " . $ pf . " , ' " . $ field . " ', ' " . $ file_name . " ',  " . $ attachedFileId . " ,  " . $ errorType . " , ' " . $ errorMessage . " ');
			</script>
		 " ; echo 'Attached file id ' . $ attachedFileId . '<hr />Error type ' . $ errorType . '<hr />' ; if ( $ errorType > 0 ) { echo 'Error message ' . $ errorMessage . '<hr />' ; } echo $ script ; }
function to upload an attached file
function to upload an attached file
// import joomla clases to manage file system // check the extension of the file // check the size of the file // get the path to attachments upload // make sure the file name has no spaces // initialize the database