| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @package    CleverStyle CMS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @subpackage System module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @category   modules | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author     Nazar Mokrynskyi <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright  Copyright (c) 2015, Nazar Mokrynskyi | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license    MIT License, see license.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace cs\modules\System\api\Controller\admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	cs\ExitException, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	cs\Language, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	cs\Page, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  | 	cs\Session; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | trait upload { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	/** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 	 * @throws ExitException | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	static function admin_upload_post () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		if (!isset($_FILES['file']) || !$_FILES['file']['tmp_name']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 			throw new ExitException(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		$L    = Language::instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 		$file = $_FILES['file']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		switch ($file['error']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			case UPLOAD_ERR_INI_SIZE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			case UPLOAD_ERR_FORM_SIZE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 				throw new ExitException($L->file_too_large, 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			case UPLOAD_ERR_NO_TMP_DIR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 				throw new ExitException($L->temporary_folder_is_missing, 400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			case UPLOAD_ERR_CANT_WRITE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 				throw new ExitException($L->cant_write_file_to_disk, 500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			case UPLOAD_ERR_PARTIAL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			case UPLOAD_ERR_NO_FILE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 				throw new ExitException(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$target_directory = TEMP.'/System/admin'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		if (!is_dir($target_directory) && !mkdir($target_directory, 0770, true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 			throw new ExitException(500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$tmp_filename = Session::instance()->get_id().'.phar'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		$tmp_location = "$target_directory/$tmp_filename"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		// Cleanup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		get_files_list( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			$target_directory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			'/.*\.phar$/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			'f', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 			false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 			false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 			false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 			function ($file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 				unlink($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		if (!move_uploaded_file($file['tmp_name'], $tmp_location)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			throw new ExitException(500); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$tmp_dir = "phar://$tmp_location"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		if (!file_exists("$tmp_dir/meta.json")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			unlink($tmp_location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			throw new ExitException(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$meta = file_get_json("$tmp_dir/meta.json"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		if (!isset($meta['category'], $meta['package'], $meta['version'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			unlink($tmp_location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			throw new ExitException(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		Page::instance()->json($meta); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 73 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |