Esempio n. 1
0
 static function handle($conf = array())
 {
     global $_FILES;
     // 5 minutes execution time
     @set_time_limit(5 * 60);
     self::$_error = null;
     // start fresh
     $conf = self::$conf = array_merge(array('file_data_name' => 'file', 'tmp_dir' => ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload", 'target_dir' => false, 'cleanup' => true, 'max_file_age' => 5 * 3600, 'chunk' => isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0, 'chunks' => isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0, 'file_name' => isset($_REQUEST['name']) ? $_REQUEST['name'] : false, 'allow_extensions' => false, 'delay' => 0, 'cb_sanitize_file_name' => array(__CLASS__, 'sanitize_file_name'), 'cb_check_file' => false), $conf);
     try {
         if (!$conf['file_name']) {
             if (!empty($_FILES)) {
                 $conf['file_name'] = $_FILES[$conf['file_data_name']]['name'];
             } else {
                 throw new Exception('', PLUPLOAD_INPUT_ERR);
             }
         }
         // Cleanup outdated temp files and folders
         if ($conf['cleanup']) {
             self::cleanup();
         }
         // Fake network congestion
         if ($conf['delay']) {
             usleep($conf['delay']);
         }
         if (is_callable($conf['cb_sanitize_file_name'])) {
             $file_name = call_user_func($conf['cb_sanitize_file_name'], $conf['file_name']);
         } else {
             $file_name = $conf['file_name'];
         }
         // Check if file type is allowed
         if ($conf['allow_extensions']) {
             if (is_string($conf['allow_extensions'])) {
                 $conf['allow_extensions'] = preg_split('{\\s*,\\s*}', $conf['allow_extensions']);
             }
             if (!in_array(strtolower(pathinfo($file_name, PATHINFO_EXTENSION)), $conf['allow_extensions'])) {
                 throw new Exception('', PLUPLOAD_TYPE_ERR);
             }
             /* TODO: Fix this
             			WHY THIS NO WORK
             			$mime_types = $conf['allow_extensions'];
             			array_walk($mime_types, function(&$value, $key) { $value = 'image/' . $value; });
             			// check mime type
             			$finfo = finfo_open(FILEINFO_MIME_TYPE);
             			if (!in_array(finfo_file($finfo, $_FILES[$conf['file_data_name']]['tmp_name'])) {
             				throw new Exception('', PLUPLOAD_TYPE_ERR);
             			}
             			finfo_close($finfo);*/
         }
         $file_path = rtrim($conf['target_dir'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file_name;
         $tmp_path = $file_path . ".part";
         // Write file or chunk to appropriate temp location
         if ($conf['chunks']) {
             self::write_file_to("{$file_path}.dir.part" . DIRECTORY_SEPARATOR . $conf['chunk']);
             // Check if all chunks already uploaded
             if ($conf['chunk'] == $conf['chunks'] - 1) {
                 self::write_chunks_to_file("{$file_path}.dir.part", $tmp_path);
             }
         } else {
             self::write_file_to($tmp_path);
         }
         // Upload complete write a temp file to the final destination
         if (!$conf['chunks'] || $conf['chunk'] == $conf['chunks'] - 1) {
             if (is_callable($conf['cb_check_file']) && !call_user_func($conf['cb_check_file'], $tmp_path)) {
                 @unlink($tmp_path);
                 throw new Exception('', PLUPLOAD_SECURITY_ERR);
             }
             $final_file_path = strtolower($file_path);
             rename($tmp_path, strtolower($final_file_path));
             return array('name' => strtolower($file_name), 'path' => $final_file_path, 'size' => filesize($final_file_path));
         }
         // ok so far
         return true;
     } catch (Exception $ex) {
         self::$_error = $ex->getCode();
         return false;
     }
 }
Esempio n. 2
0
	static function handle($conf = array())
	{
		
		@set_time_limit(5 * 60);

		self::$_error = null; 

		$conf = self::$conf = array_merge(array(
			'file_data_name'        => 'file',
			'tmp_dir'               => ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload",
			'target_dir'            => false,
			'cleanup'               => true,
			'max_file_age'          => 5 * 3600,
			'chunk'                 => isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0,
			'chunks'                => isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0,
			'file_name'             => isset($_REQUEST['name']) ? $_REQUEST['name'] : false,
			'allow_extensions'      => false,
			'delay'                 => 0,
			'cb_sanitize_file_name' => array(__CLASS__, 'sanitize_file_name'),
			'cb_check_file'         => false,
		), $conf);

		try
		{
			if (!$conf['file_name'])
			{
				if (!empty($_FILES))
				{
					$conf['file_name'] = $_FILES[$conf['file_data_name']]['name'];
				}
				else
				{
					throw new Exception('', PLUPLOAD_INPUT_ERR);
				}
			}

			
			if ($conf['cleanup'])
			{
				self::cleanup();
			}

			
			if ($conf['delay'])
			{
				usleep($conf['delay']);
			}

			if (is_callable($conf['cb_sanitize_file_name']))
			{
				$file_name = call_user_func($conf['cb_sanitize_file_name'], $conf['file_name']);
			}
			else
			{
				$file_name = $conf['file_name'];
			}

			
			if ($conf['allow_extensions'])
			{
				if (is_string($conf['allow_extensions']))
				{
					$conf['allow_extensions'] = preg_split('{\s*,\s*}', $conf['allow_extensions']);
				}

				if (!in_array(strtolower(pathinfo($file_name, PATHINFO_EXTENSION)), $conf['allow_extensions']))
				{
					throw new Exception('', PLUPLOAD_TYPE_ERR);
				}
			}

			$file_path = rtrim($conf['target_dir'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file_name;
			$tmp_path  = $file_path . ".part";

			
			if ($conf['chunks'])
			{
				self::write_file_to("$file_path.dir.part" . DIRECTORY_SEPARATOR . $conf['chunk']);

				
				if ($conf['chunk'] == $conf['chunks'] - 1)
				{
					self::write_chunks_to_file("$file_path.dir.part", $tmp_path);
				}
			}
			else
			{
				self::write_file_to($tmp_path);
			}

			
			if (!$conf['chunks'] || $conf['chunk'] == $conf['chunks'] - 1)
			{
				if (is_callable($conf['cb_check_file']) && !call_user_func($conf['cb_check_file'], $tmp_path))
				{
					@unlink($tmp_path);
					throw new Exception('', PLUPLOAD_SECURITY_ERR);
				}

				rename($tmp_path, $file_path);

				return array(
					'name' => $file_name,
					'path' => $file_path,
					'size' => filesize($file_path)
				);
			}

			
			return true;

		}
		catch (Exception $ex)
		{
			self::$_error = $ex->getCode();

			return false;
		}
	}
Esempio n. 3
0
 /**
  * 
  */
 static function handle($conf = array())
 {
     // 5 minutes execution time
     @set_time_limit(5 * 60);
     $conf = self::$conf = array_merge(array('file_data_name' => 'file', 'tmp_dir' => ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload", 'target_dir' => false, 'cleanup' => true, 'max_file_age' => 5 * 3600, 'chunk' => isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0, 'chunks' => isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0, 'file_name' => isset($_REQUEST['name']) ? $_REQUEST['name'] : uniqid('file_'), 'allow_extensions' => false, 'delay' => 0, 'cb_sanitize_file_name' => array(__CLASS__, 'sanitize_file_name'), 'cb_check_file' => false), $conf);
     self::$_error = null;
     // start fresh
     try {
         // Cleanup outdated temp files and folders
         if ($conf['cleanup']) {
             self::cleanup();
         }
         // Fake network congestion
         if ($conf['delay']) {
             usleep($conf['delay']);
         }
         if (is_callable($conf['cb_sanitize_file_name'])) {
             $file_name = call_user_func($conf['cb_sanitize_file_name'], $conf['file_name']);
         }
         $file_extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
         // Check if file type is allowed
         if ($conf['allow_extensions']) {
             if (is_string($conf['allow_extensions'])) {
                 $conf['allow_extensions'] = preg_split('{\\s*,\\s*}', $conf['allow_extensions']);
             }
             if (!in_array($file_extension, $conf['allow_extensions'])) {
                 throw new Exception('', PLUPLOAD_TYPE_ERR);
             }
         }
         $file_path = rtrim($conf['target_dir'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file_name;
         $tmp_path = $file_path . ".part";
         // Write file or chunk to appropriate temp location
         if ($conf['chunks']) {
             self::write_file_to("{$file_path}.dir.part" . DIRECTORY_SEPARATOR . $conf['chunk']);
             // Check if all chunks already uploaded
             if ($conf['chunk'] == $conf['chunks'] - 1) {
                 self::write_chunks_to_file("{$file_path}.dir.part", $tmp_path);
             }
         } else {
             self::write_file_to($tmp_path);
         }
         // Upload complete write a temp file to the final destination
         if (!$conf['chunks'] || $conf['chunk'] == $conf['chunks'] - 1) {
             rename($tmp_path, $file_path);
             if (is_callable($conf['cb_check_file']) && !call_user_func($conf['cb_check_file'], $file_path)) {
                 @unlink($file_path);
                 throw new Exception('', PLUPLOAD_SECURITY_ERR);
             }
         }
     } catch (Exception $ex) {
         self::$_error = $ex->getCode();
         return false;
     }
     return $file_path;
 }
Esempio n. 4
0
 /**
  * 
  */
 static function handle($conf = array())
 {
     // 2H execution time
     @set_time_limit(7200);
     self::$_error = null;
     // start fresh
     $conf = self::$conf = array_merge(array('file_data_name' => 'file', 'tmp_dir' => ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload", 'target_dir' => false, 'cleanup' => true, 'max_file_age' => 48 * 3600, 'chunk' => isset($_REQUEST['chunk']) ? intval($_REQUEST['chunk']) : 0, 'chunks' => isset($_REQUEST['chunks']) ? intval($_REQUEST['chunks']) : 0, 'file_name' => isset($_REQUEST['name']) ? $_REQUEST['name'] : false, 'allow_extensions' => false, 'delay' => 0, 'cb_sanitize_file_name' => array(__CLASS__, 'sanitize_file_name'), 'cb_check_file' => false), $conf);
     try {
         if (!$conf['file_name']) {
             if (!empty($_FILES)) {
                 $conf['file_name'] = $_FILES[$conf['file_data_name']]['name'];
             } else {
                 throw new Exception('', PLUPLOAD_INPUT_ERR);
             }
         }
         // Cleanup outdated temp files and folders
         if ($conf['cleanup']) {
             self::cleanup();
         }
         // Fake network congestion
         if ($conf['delay']) {
             usleep($conf['delay']);
         }
         //			if (is_callable($conf['cb_sanitize_file_name'])) {
         //				$file_name = call_user_func($conf['cb_sanitize_file_name'], $conf['file_name']);
         //			} else {
         //				$file_name = $conf['file_name'];
         //			}
         $file_name = $conf['file_name'];
         // Check if file type is allowed
         if ($conf['allow_extensions']) {
             if (is_string($conf['allow_extensions'])) {
                 $conf['allow_extensions'] = preg_split('{\\s*,\\s*}', $conf['allow_extensions']);
             }
             if (!in_array(strtolower(pathinfo($file_name, PATHINFO_EXTENSION)), $conf['allow_extensions'])) {
                 throw new Exception('', PLUPLOAD_TYPE_ERR);
             }
         }
         $file_path = rtrim($conf['target_dir'], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $file_name;
         if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
             $file_path = iconv('utf-8', 'gbk//IGNORE', $file_path);
         }
         $tmp_path = $file_path . ".part";
         // Write file or chunk to appropriate temp location
         if ($conf['chunks']) {
             self::write_file_to("{$file_path}.dir.part" . DIRECTORY_SEPARATOR . $conf['chunk']);
             // Check if all chunks already uploaded
             if ($conf['chunk'] == $conf['chunks'] - 1) {
                 self::write_chunks_to_file("{$file_path}.dir.part", $tmp_path);
             }
         } else {
             self::write_file_to($tmp_path);
         }
         // Upload complete write a temp file to the final destination
         if (!$conf['chunks'] || $conf['chunk'] == $conf['chunks'] - 1) {
             if (is_callable($conf['cb_check_file']) && !call_user_func($conf['cb_check_file'], $tmp_path)) {
                 @unlink($tmp_path);
                 throw new Exception('', PLUPLOAD_SECURITY_ERR);
             }
             rename($tmp_path, $file_path);
             return array('name' => $file_name, 'path' => $file_path, 'size' => filesize($file_path));
         }
         // ok so far
         return true;
     } catch (Exception $ex) {
         self::$_error = $ex->getCode();
         return false;
     }
 }