From version 3.1
edited by Sylvain Berfini
on 2017/02/21 14:51
To version 4.1
edited by Sylvain Berfini
on 2018/02/28 11:41
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -23,6 +23,50 @@
23 23   along with this program. If not, see <http://www.gnu.org/licenses/>.
24 24  -->
25 25  
26 +function custom_log($message) {
27 + $now = getdate();
28 + $month = sprintf("%02d", $now["mon"]);
29 + $day = sprintf("%02d", $now["mday"]);
30 + $hours = sprintf("%02d", $now["hours"]);
31 + $minutes = sprintf("%02d", $now["minutes"]);
32 + $seconds = sprintf("%02d", $now["seconds"]);
33 +
34 + error_log("[" . $day . "/" . $month . "/" . $now["year"] . " " . $hours . ":" . $minutes . ":" . $seconds . "] " . $message . "\r\n", 3, "/var/log/trace_file_sharing.log");
35 +}
36 +
37 +function upload_error_message($error) {
38 + $message = 'Error uploading file';
39 + switch($error) {
40 + case UPLOAD_ERR_OK:
41 + $message = false;;
42 + break;
43 + case UPLOAD_ERR_INI_SIZE:
44 + case UPLOAD_ERR_FORM_SIZE:
45 + $message .= ' - file too large (limit of '.get_max_upload().' bytes).';
46 + break;
47 + case UPLOAD_ERR_PARTIAL:
48 + $message .= ' - file upload was not completed.';
49 + break;
50 + case UPLOAD_ERR_NO_FILE:
51 + $message .= ' - zero-length file uploaded.';
52 + break;
53 + default:
54 + $message .= ' - internal error #'. $error;
55 + break;
56 + }
57 + return $message;
58 +}
59 +
60 +function file_too_large() {
61 + if (!function_exists('http_response_code')) {
62 + $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
63 + header($protocol . ' 400 Bad Request');
64 + $GLOBALS['http_response_code'] = 400;
65 + } else {
66 + http_response_code(400);
67 + }
68 +}
69 +
26 26  date_default_timezone_set("UTC");
27 27  if (count($_FILES) != 0) {
28 28   $uploaddir = dirname(__FILE__).'/tmp/';
... ... @@ -35,33 +35,43 @@
35 35   custom_log(upload_error_message($_FILES['File']['error']));
36 36   }
37 37  
38 - custom_log('Uploaded '.$rcvname.' to '.$tmpfile);
39 - //$uploadfile = $uploaddir.time().md5_file($tmpfile).".".$ext;
40 - $uploadfile = $uploaddir.uniqid()."_".bin2hex(openssl_random_pseudo_bytes(10)).".$ext";
82 + if ($_FILES['File']['error'] === UPLOAD_ERR_INI_SIZE) {
83 + //File too large
84 + custom_log(upload_error_message($_FILES['File']['error']));
85 + file_too_large();
86 + } else {
87 + custom_log('Uploaded '.$rcvname.' to '.$tmpfile);
88 + //$uploadfile = $uploaddir.time().md5_file($tmpfile).".".$ext;
89 + $uploadfile = $uploaddir.uniqid()."_".bin2hex(openssl_random_pseudo_bytes(10)).".$ext";
41 41  
42 - if (move_uploaded_file($tmpfile, $uploadfile)) {
43 - custom_log('Moved to '.$uploadfile);
44 - $ipport = $_SERVER['HTTP_HOST'];
45 - $prefix= (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"])=="on")?"https":"http";
46 - $start= $prefix."://".$ipport.dirname($_SERVER['REQUEST_URI']);
47 - $http_url = $start."/tmp/".basename($uploadfile);
91 + if (move_uploaded_file($tmpfile, $uploadfile)) {
92 + custom_log('Moved to '.$uploadfile);
93 + $ipport = $_SERVER['HTTP_HOST'];
94 + $prefix= (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"])=="on")?"https":"http";
95 + $start= $prefix."://".$ipport.dirname($_SERVER['REQUEST_URI']);
96 + $http_url = $start."/tmp/".basename($uploadfile);
48 48  
49 - // validity time is one week ahead from now
50 - $until = date("Y-m-d\TH:i:s\Z",time()+7*24*60*60);
51 - echo '<file xmlns="urn:gsma:params:xml:ns:rcs:rcs:fthttp">
52 - <file-info type="file">
53 - <file-size>'.$_FILES['File']['size'].'</file-size>
54 - <file-name>'.$_FILES['File']['name'].'</file-name>
55 - <content-type>'.$_FILES['File']['type'].'</content-type>
56 - <data url = "'.$http_url.'" until = "'.$until.'"/>
57 - </file-info>
58 - </file>';
98 + // validity time is one week ahead from now
99 + $until = date("Y-m-d\TH:i:s\Z",time()+7*24*60*60);
100 + echo '<file xmlns="urn:gsma:params:xml:ns:rcs:rcs:fthttp">
101 +<file-info type="file">
102 +<file-size>'.$_FILES['File']['size'].'</file-size>
103 +<file-name>'.$_FILES['File']['name'].'</file-name>
104 +<content-type>'.$_FILES['File']['type'].'</content-type>
105 +<data url = "'.$http_url.'" until = "'.$until.'"/>
106 +</file-info>
107 +</file>';
108 + }
59 59   }
60 60  }
61 -if ((count($_POST) == 0) && (count($_FILES) == 0)) {
111 +
112 +if (isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > (1024*1024*(int) ini_get('post_max_size'))) {
113 + // File too large
114 + file_too_large();
115 +} else if ((count($_POST) == 0) && (count($_FILES) == 0)) {
62 62   if (!function_exists('http_response_code')) {
63 63   $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
64 - header($protocol . ' 204 No Content');
118 + header($protocol . ' 204 No Content');
65 65   $GLOBALS['http_response_code'] = 204;
66 66   } else {
67 67   http_response_code(204);