scratch – Diff between revs 14 and 16

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 14 Rev 16
1 <?php 1 <?php
2   2  
3 ########################################################################### 3 ###########################################################################
4 ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ## 4 ## Copyright (C) Wizardry and Steamworks 2017 - License: GNU GPLv3 ##
5 ########################################################################### 5 ###########################################################################
6   6  
7 require_once('inc/pseudocrypt.php'); 7 require_once('inc/pseudocrypt.php');
8 require_once('inc/functions.php'); 8 require_once('inc/functions.php');
9 require_once('config.php'); 9 require_once('config.php');
10   10  
11 #### Retrieve uploaded file. 11 #### Retrieve uploaded file.
12 if (!empty($_FILES['file']) and 12 if (!empty($_FILES['file']) and
13 is_uploaded_file($_FILES['file']['tmp_name'])) { 13 is_uploaded_file($_FILES['file']['tmp_name'])) {
14 # Regular multipart/form-data upload. 14 # Regular multipart/form-data upload.
15 $name = $_FILES['file']['name']; 15 $name = $_FILES['file']['name'];
16 $data = file_get_contents($_FILES['file']['tmp_name']); 16 $data = file_get_contents($_FILES['file']['tmp_name']);
17 } else { 17 } else {
18 # Raw POST data. 18 # Raw POST data.
19 $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']); 19 $name = urldecode(@$_SERVER['HTTP_X_FILE_NAME']);
20 $data = file_get_contents("php://input"); 20 $data = file_get_contents("php://input");
21 } 21 }
22   22  
23 #### Grab the file extension. 23 #### Grab the file extension.
24 $fileExtension = pathinfo($name, PATHINFO_EXTENSION); 24 $fileExtension = pathinfo($name, PATHINFO_EXTENSION);
25   25  
26 #### If the extension is not allowed then change it to a text extension. 26 #### If the extension is not allowed then change it to a text extension.
27 if (!isset($fileExtension) || 27 if (!isset($fileExtension) ||
-   28 !in_array(strtoupper($fileExtension),
28 !in_array(strtoupper($fileExtension), $ALLOWED_FILE_EXTENSIONS)) 29 array_map('strtoupper', $ALLOWED_FILE_EXTENSIONS)))
29 $fileExtension = 'txt'; 30 $fileExtension = 'txt';
30 31
31 #### Hash filename. 32 #### Hash filename.
32 $file = strtolower( 33 $file = strtolower(
33 PseudoCrypt::hash( 34 PseudoCrypt::hash(
34 preg_replace( 35 preg_replace(
35 '/\D/', 36 '/\D/',
36 '', 37 '',
37 hash( 38 hash(
38 'sha512', 39 'sha512',
39 $name 40 $name
40 ) 41 )
41 ) 42 )
42 ). '.'. 43 ). '.'.
43 $fileExtension 44 $fileExtension
44 ); 45 );
45   46  
46 #### Build the user path. 47 #### Build the user path.
47 $userPath = join( 48 $userPath = join(
48 DIRECTORY_SEPARATOR, 49 DIRECTORY_SEPARATOR,
49 array( 50 array(
50 $STORE_FOLDER, 51 $STORE_FOLDER,
51 $file 52 $file
52 ) 53 )
53 ); 54 );
54   55  
55 #### Check for path traversals 56 #### Check for path traversals
56 $pathPart = pathinfo($userPath); 57 $pathPart = pathinfo($userPath);
57 if (realpath($pathPart['dirname']) != realpath($STORE_FOLDER)) 58 if (strcasecmp(realpath($pathPart['dirname']), realpath($STORE_FOLDER)) != 0)
58 return; 59 return;
59   60  
60 #### Store the file. 61 #### Store the file.
61 atomized_put_contents($userPath, $data); 62 atomized_put_contents($userPath, $data);
62   63  
63 ### Return the URL to the file. 64 ### Return the URL to the file.
64 header('Content-Type: text/plain; charset=utf-8'); 65 header('Content-Type: text/plain; charset=utf-8');
65 echo sprintf('%s/%s', trim($URL_PATH, '/'), $file); 66 echo sprintf('%s/%s', trim($URL_PATH, '/'), $file);
66   67