Exemplo n.º 1
0
	function singleFileUpload(&$errUploadMsg, $file, $post) {
	
		$app		= JFactory::getApplication();;
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );
		jimport('joomla.client.helper');
		$user 				= JFactory::getUser();
		$ftp 		= JClientHelper::setCredentialsFromRequest('ftp');
		$path		= PhocaDownloadPath::getPathSet();
		$folder		= $app->input->get( 'folder', '', '', 'path' );
		$format		= $app->input->get( 'format', 'html', '', 'cmd');
		$return		= $app->input->get( 'return-url', null, 'post', 'base64' );
		$viewBack	= $app->input->get( 'viewback', '', 'post', 'string' );
		//$catid 		= $app->input->get( 'catid', '', '', 'int'  );
		$paramsC 	= JComponentHelper::getParams('com_phocadownload') ;

		$overwriteExistingFiles 	= $paramsC->get( 'overwrite_existing_files', 0 );
		
		// USER RIGHT - UPLOAD - - - - - - - - - - -
		// 2, 2 means that user access will be ignored in function getUserRight for display Delete button
		$rightDisplayUpload	= 0;
		$catAccess	= PhocaDownloadAccess::getCategoryAccess((int)$post['catidfiles']);
		if (!empty($catAccess)) {
			$rightDisplayUpload = PhocaDownloadAccess::getUserRight('uploaduserid', $catAccess->uploaduserid, 2, $user->getAuthorisedViewLevels(), 1, 0);
		}
		// - - - - - - - - - - - - - - - - - - - - - -	
		
		
		/*$post['sectionid'] = $this->getSection((int)$post['catidfiles']);
		if(!$post['sectionid']) {
			$errUploadMsg = JText::_('COM_PHOCADOWNLOAD_WRONG_SECTION');	
			return false;
		}*/
		
		//$userFolder = substr(md5($user->username),0, 10);
		$userFolder = PhocaDownloadUtils::cleanFolderUrlName(htmlspecialchars(strip_tags($user->username)));
		
		if ($rightDisplayUpload == 1) {

			// Make the filename safe
			if (isset($file['name'])) {
				$file['name']	= JFile::makeSafe($file['name']);
			}
			
			if($file['tmp_name'] == '') {
				$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_ERROR_SERVER_NOT_ABLE_TO_STORE_FILE_TEMP_FOLDER");
				return false;
			}
			
			if (isset($file['name'])) {
				$filepath 				= JPath::clean($path['orig_abs_user_upload']. DS. $userFolder . DS.$file['name']);
				$filepathUserFolder 	= JPath::clean($path['orig_abs_user_upload']. DS. $userFolder);
				if (!PhocaDownloadFileUpload::canUpload( $file, $errUploadMsg, 'file', 2 )) {
				
					if ($errUploadMsg == 'COM_PHOCADOWNLOAD_WARNUSERFILESTOOLARGE') {
						$errUploadMsg 	= JText::_($errUploadMsg) . ' ('.PhocaDownloadFile::getFileSizeReadable($file['size']).')';
					} else {
						$errUploadMsg 	= JText::_($errUploadMsg);
					}
					
					return false;
				}

				if (JFile::exists($filepath) && $overwriteExistingFiles == 0) {
					$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_FILE_ALREADY_EXISTS");
					return false;
				}
				
				// Overwrite file and add no new item to database
				$fileExists = 0;
				if (JFile::exists($filepath) && $overwriteExistingFiles == 1) {
					$fileExists = 1;
				}

				if (!JFile::upload($file['tmp_name'], $filepath, false, true)) {
					$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_UNABLE_TO_UPLOAD_FILE");
					return false;
				} else {
					
					// Saving file name into database with relative path
					if (!JFile::exists($filepathUserFolder . DS ."index.html")) {
						$data = "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>";
						JFile::write($filepathUserFolder . DS ."index.html", $data);
					}
					$file['namepap']	= $file['name'];
					$file['name']		=  'userupload/'.$userFolder.'/' . $file['name'];
					$succeeded 			= false;
					
					// =================================================
					// Make a copy for play and preview
					$papCopy 	= $paramsC->get( 'pap_copy', 0 );
					if ($papCopy == 1 || $papCopy == 3) {
						$canPlay	= PhocaDownloadFile::canPlay($file['namepap']);
						$canPreview = PhocaDownloadFile::canPreview($file['namepap']);
						$filepathPAP 			= JPath::clean($path['orig_abs_user_upload_pap']. DS. $userFolder . DS.$file['namepap']);
						$filepathUserFolderPAP 	= JPath::clean($path['orig_abs_user_upload_pap']. DS. $userFolder);
						
						if ($canPlay || $canPreview) {
							
							$uploadPAP = 1;// upload file for preview and play
							if (JFile::exists($filepathPAP) && $overwriteExistingFiles == 0) {
								//$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_FILE_ALREADY_EXISTS");
								//return false;
								$uploadPAP = 0; // don't upload if it exists, it is not main file, don't do false and exit
							}
							
							// Overwrite file and add no new item to database
							$fileExistsPAP = 0;
							if (JFile::exists($filepathPAP) && $overwriteExistingFiles == 1) {
								$fileExistsPAP = 1;
							}
							
							if ($uploadPAP == 0) {
							
							} else {
								if (!JFolder::exists($filepathUserFolderPAP)) {
									if (JFolder::create($filepathUserFolderPAP)) {
										$data = "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>";
										JFile::write($filepathUserFolderPAP . DS ."index.html", $data);
									}
									// else {
										//$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_UNABLE_TO_CREATE_FOLDER");
										//return false;
									//}
								}
								
								if (!JFile::copy($filepath, $filepathPAP)) {
							
									//$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_UNABLE_TO_UPLOAD_FILE");
									//return false;
								} else {
									// Saving file name into database with relative path
									if (!JFile::exists($filepathUserFolderPAP . DS ."index.html")) {
										$data = "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>";
										JFile::write($filepathUserFolderPAP . DS ."index.html", $data);
									}
									
									if ($canPlay == 1) {
										$post['filename_play']		=  'userupload/'.$userFolder.'/' . $file['namepap'];
									} else if ($canPreview == 1) {
										$post['filename_preview']	=  'userupload/'.$userFolder.'/' . $file['namepap'];
									}
								}
							}
						}
					}
					// ==============================================
					
					if ($this->_save($post, $file['name'], $errUploadMsg, $fileExists)) {
						
						return true;
					} else {
						return false;
					}
				}
			} else {				
				$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_WARNFILETYPE");	
				$redirectUrl = $return;				
				return false;
			}
		} else {			
			$errUploadMsg = JText::_("COM_PHOCADOWNLOAD_NOT_AUTHORISED_TO_UPLOAD");			
			
			return false;
		}
		return false;
		
		
	}