function testParseFileSizeString()
 {
     $this->assertEquals(parse_file_size_string("1234"), 1234);
     $this->assertEquals(parse_file_size_string("0b"), 0);
     $this->assertEquals(parse_file_size_string("5b"), 5);
     $this->assertEquals(parse_file_size_string("1k"), 1024);
     $this->assertEquals(parse_file_size_string("16k"), 16 * 1024);
     $this->assertEquals(parse_file_size_string("2M"), 2 * 1024 * 1024);
     $this->assertEquals(parse_file_size_string("123M"), 123 * 1024 * 1024);
     $this->assertEquals(parse_file_size_string("2G"), 2 * 1024 * 1024 * 1024);
 }
예제 #2
0
 /**
  * Uploads the file
  * @access public
  * @params
  * $uploaddir : Directory Name in which uploaded file is placed
  * $name : file input type field name
  * $rename : you may pass string or boolean
     true : rename the file if it already exists and returns the renamed file name.
  *  String : rename the file to given string.
  *  $replace =true : replace the file if it is already existing
  *  $file_max_size : file size in bytes. 0 for default
  *  $check_type : checks file type exp ."(jpg|gif|jpeg)"
  *  Example upload_file("temp","file",true,true,0,"jpg|jpeg|bmp|gif")
  * return : On success it will return file name else return (boolean)false
 */
 public function upload_file($uploaddir, $name, $rename = null, $replace = false, $check_type = "")
 {
     $this->set_file_size($_FILES[$name]['size']);
     $this->error = $_FILES[$name]['error'];
     $this->set_temp_name($_FILES[$name]['tmp_name']);
     $this->set_directory($uploaddir);
     $this->check_for_directory();
     $this->set_file_name($_FILES[$name]['name']);
     $file_size = $this->file_type_info[$check_type]['max_file_size'] / 1000000;
     if ($this->error == 1) {
         $this->error = sprintf(__("Your file is too large for the web server.  The largest file you can upload here is %.1fM.  If this is too small, please ask the administrator to increase the <code>upload_max_filesize</code> directive in <code>php.ini</code>."), floatval(parse_file_size_string(ini_get("upload_max_filesize"))) / 1048576.0);
     } elseif ($this->error == 3) {
         $this->error = __('The uploaded file was only partially uploaded.');
     } elseif ($this->error == 4) {
         $this->error = __('No file was uploaded');
     } elseif (!is_uploaded_file($this->tmp_name)) {
         $this->error = "File " . $this->tmp_name . " is not uploaded correctly.";
     }
     if (empty($this->file_name)) {
         $this->error = "File is not uploaded correctly.";
     }
     if ($this->error != "") {
         return false;
     }
     //check here for valid file
     if (!empty($check_type)) {
         // set max upload size
         if (array_key_exists($check_type, $this->file_type_info)) {
             $this->set_max_size($this->file_type_info[$check_type]['max_file_size']);
         }
         // check file size against maximum
         if ($this->file_size > $this->max_filesize) {
             $this->error = sprintf(__("File too large; %s file uploads are limited to %s.  If this is too small, please ask the administrator to increase the limit."), $check_type, format_file_size($this->max_filesize));
             return false;
         }
         //if $check_type is just image then we can check it via getImagesize() function if the file is valid or not
         // does this check always for image
         if ($check_type == 'image') {
             $sizeCheck = @getImagesize($this->tmp_name);
             if (!$sizeCheck) {
                 $this->error = __("Invalid image file.");
                 return false;
             }
             //additional check to see if the image has any extension
             $ext = explode('.', $this->file_name);
             $ext = strtolower(end($ext));
             $img_mime = explode('/', $sizeCheck['mime']);
             $img_mime = strtolower(end($img_mime));
             //jpeg and jpg may have different extension and mime so handled specially
             if ($ext == 'jpg' || $ext == 'jpeg') {
             } else {
                 if ($ext != $img_mime) {
                     //means there is no image extension so lets add it
                     $this->file_name .= '.' . $img_mime;
                 }
             }
         }
         //check for other media types
         // can be turned of from config.inc define('CHECK_MIME_TYPE',0);
         if ($check_type == 'audio' || $check_type == 'video') {
             if (CHECK_MIME_TYPE == 1) {
                 $mime_type = exec('file -bi ' . $this->tmp_name);
                 // TO DO:: enalbe for audio/video //application/octet-stream
                 if (empty($mime_type)) {
                     $this->error = __("Invalid media file.");
                     return false;
                 }
                 if (strstr($mime_type, $check_type)) {
                 } else {
                     if (strstr($mime_type, 'media')) {
                     } else {
                         if (strstr($mime_type, 'octet-stream')) {
                             // Temporarily added for .wav file -- need to do something else
                         } else {
                             $this->error = __("Invalid media file.");
                             return false;
                         }
                     }
                 }
             }
         }
         //special treatment for doc types
         if ($check_type == 'doc') {
             if (CHECK_MIME_TYPE == 1) {
                 $mime_type = exec('file -bi ' . $this->tmp_name);
                 if (strstr($mime_type, 'msword') || strstr($mime_type, 'pdf')) {
                 } else {
                     $this->error = sprintf(__("Invalid document type - supported formats are: %s"), ".doc, .pdf");
                     return false;
                 }
             }
         }
     }
     //check_type
     if (!is_bool($rename) && !empty($rename)) {
         if (preg_match("/\\..*+\$/", $this->file_name, $matches)) {
             $this->set_file_name($rename . $matches[0]);
         }
     } elseif ($rename && file_exists($this->full_name)) {
         if (preg_match("/\\..*+\$/", $this->file_name, $matches)) {
             $this->set_file_name(substr_replace($this->file_name, "_" . rand(0, rand(0, 99)), -strlen($matches[0]), 0));
         }
     }
     if (file_exists($this->full_name)) {
         if ($replace) {
             @unlink($this->full_name);
         } else {
             $this->error = __("File error: File already exists");
             return false;
         }
     }
     $this->start_upload();
     if ($this->error != "") {
         return false;
     } else {
         return $this->file_name;
     }
 }
 private function collectSystemData()
 {
     //
     // collect server data
     //
     define('PA_DOCUMENT_ROOT', realpath($_SERVER['DOCUMENT_ROOT']));
     $this->document_root = PA_DOCUMENT_ROOT;
     $dir_info = pathinfo(realpath($_SERVER['SCRIPT_FILENAME']));
     $path_info = pathinfo($_SERVER['SCRIPT_NAME']);
     define('PA_SCRIPT_DIR', @$dir_info['dirname']);
     define('PA_SCRIPT_PATH', @$path_info['dirname']);
     $this->script_dir = PA_SCRIPT_DIR;
     $this->script_path = PA_SCRIPT_PATH;
     if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
         define('PA_REQUEST_METHOD', 'AJAX');
     } else {
         define('PA_REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
     }
     $this->request_method = PA_REQUEST_METHOD;
     $scheme = sprintf('http%s', isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == TRUE ? 's' : '');
     define('PA_CURRENT_SCHEME', $scheme);
     $this->current_scheme = PA_CURRENT_SCHEME;
     isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? define('PA_HTTP_HOST', $_SERVER['HTTP_X_FORWARDED_HOST']) : define('PA_HTTP_HOST', $_SERVER['HTTP_HOST']);
     $this->http_host = PA_HTTP_HOST;
     isset($_SERVER['HTTP_X_FORWARDED_SERVER']) ? define('PA_SERVER_NAME', $_SERVER['HTTP_X_FORWARDED_SERVER']) : define('PA_SERVER_NAME', $_SERVER['SERVER_NAME']);
     $this->server_name = PA_SERVER_NAME;
     $domain_parts = explode(".", PA_SERVER_NAME);
     if (count($domain_parts) > 2 && !preg_match("|^\\d+\\.\\d+\\.\\d+\\.\\d+|", PA_SERVER_NAME)) {
         $domain_match = array();
         if (preg_match("/([^\\.\\/]+).([^\\.\\/]+)\$/", PA_SERVER_NAME, $domain_match)) {
             $domain_suffix = $domain_match[0];
             $domain_prefix = substr(PA_SERVER_NAME, 0, strlen(PA_SERVER_NAME) - strlen($domain_suffix) - 1);
         } else {
             throw new BootStrapException("BootStrap::collectSystemData() - Unable to detect load domain suffix!", 1);
         }
     } else {
         $domain_suffix = implode(".", $domain_parts);
         $domain_prefix = false;
     }
     define('PA_DOMAIN_SUFFIX', $domain_suffix);
     define('PA_DOMAIN_PREFIX', $domain_prefix);
     $this->domain_suffix = PA_DOMAIN_SUFFIX;
     $this->domain_prefix = PA_DOMAIN_PREFIX;
     $this->remote_addr = $this->getIP();
     $this->request_uri = $this->_normalize_URI($_SERVER['REQUEST_URI']);
     $_SERVER['REQUEST_URI'] = $this->request_uri;
     define('PA_BASE_URL', PA_CURRENT_SCHEME . '://' . PA_SERVER_NAME);
     $this->base_url = PA_BASE_URL;
     define('PA_INSTALL_DIR', $this->install_dir);
     define('PA_USER_AGENT', implode(' ', $this->getUserAgent($_SERVER['HTTP_USER_AGENT'])));
     $this->user_agent = PA_USER_AGENT;
     $this->request_data = $_REQUEST;
     $this->upload_max_filesize = parse_file_size_string(ini_get("upload_max_filesize"));
 }