/var/www/hkosl.com/imusiccircle/webadmin/plupload/upload_post.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
    
include '../config.php';
    
// Check if the user is logged in

    
if ((!isSet($_SESSION['loginname'])) || ($loggin <> '1')) {
        
header("Location: ../login.php");
        exit;
    }

    require_once(
"../function_cropimg.php");
    require_once(
"../mime_type_lib.php");

    
error_reporting(E_ALL);
    
ini_set('display_errors'0);

    
/*if (!function_exists('random_string')) {
        function random_string( $length = 8 ) {
            $chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
            $random_string = substr( str_shuffle( $chars ), 0, $length );
            return $random_string;
        }
    }*/

    
if (!function_exists('validate_upload_file')) {
        function 
validate_upload_file($file){
            global 
$targetDir;
            
$max_file_size_server $_REQUEST["max_file_size_server"];

            
//die('{"jsonrpc" : "2.0","error" : "'.$file["name"].'"}');

            //check if file empty
            
if(empty($file["name"]) || $file['size'] <= 0){
                die(
'{"jsonrpc" : "2.0","error" : "Please upload a image file!"}');
            }

            
//check file size
            
if ($file['size'] > $max_file_size_server) {
                die(
'{"jsonrpc" : "2.0","error" : "You can only upload image files less than 2M!"}');
            }

            
//check file resolution
            
if(!check_image_resolution($file["tmp_name"])){
                die(
'{"jsonrpc" : "2.0","error" : "The resolution of this image is too large. Please resize it before uploading to server."}');
            }

            
//check file mime type
            
$mime get_file_mime_type($file["name"]);

            if(
$_REQUEST["upload_type"] == "1"){// 1="only jpg,png,gif" , 2="only pdf", 3="only doc, excel, ppt", 4="All";
                
if ((strpos($mime'image/gif') !== false) || (strpos($mime'image/jpeg') !== false) || (strpos($mime'image/png') !== false)) {
                }else{
                    die(
'{"jsonrpc" : "2.0","error" : "You can only upload image files!"}');
                }
            }else if(
$_REQUEST["upload_type"] == "2"){
                if ((
strpos($mime'application/pdf') !== false)) {
                }else{
                    die(
'{"jsonrpc" : "2.0","error" : "You can only upload pdf files!"}');
                }
            }elseif(
$_REQUEST["upload_type"] == "3"){
                if ((
strpos($mime'application/vnd.openxmlformats-officedocument.wordprocessingml.document') !== false) || (strpos($mime'application/vnd.openxmlformats-officedocument.presentationml.presentation') !== false) || (strpos($mime'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') !== false) || (strpos($mime'application/msword') !== false) || (strpos($mime'application/vnd.ms-powerpoint') !== false) || (strpos($mime'application/vnd.ms-excel') !== false)) {
                }else{
                    die(
'{"jsonrpc" : "2.0","error" : "You can only upload Word/Excel/Powerpoint files!"}');
                }
            }elseif(
$_REQUEST["upload_type"] == "4"){
                if ((
strpos($mime'image/gif') !== false) || (strpos($mime'image/jpeg') !== false) || (strpos($mime'image/png') !== false) || (strpos($mime'application/pdf') !== false) || (strpos($mime'application/vnd.openxmlformats-officedocument.wordprocessingml.document') !== false) || (strpos($mime'application/vnd.openxmlformats-officedocument.presentationml.presentation') !== false) || (strpos($mime'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') !== false) || (strpos($mime'application/msword') !== false) || (strpos($mime'application/vnd.ms-powerpoint') !== false) || (strpos($mime'application/vnd.ms-excel') !== false)) {
                }else{
                    die(
'{"jsonrpc" : "2.0","error" : "You can only upload image/pdf/Word/Excel/Powerpoint files!"}');
                }
            }else{
                exit;
            }



            
$file_token pathinfo($file["name"]);
            
$file_title $file_token['filename'];
            
$file_extension $file_token['extension'];
            
$file_extension_lower strtolower($file_extension);
            
//check file extension
            
if($_REQUEST["upload_type"] == "1"){// 1="only jpg,png,gif" , 2="only pdf", 3="only doc, excel, ppt", 4="All";
                
$whitelist = array("jpg","jpeg","png""gif");
            }else if(
$_REQUEST["upload_type"] == "2"){
                
$whitelist = array("pdf");
            }else if(
$_REQUEST["upload_type"] == "3"){
                
$whitelist = array("doc","docx","xls","xlsx","ppt","pptx");
            }else if(
$_REQUEST["upload_type"] == "4"){
                
$whitelist = array("jpg","jpeg","png""gif","pdf""doc","docx","xls","xlsx","ppt","pptx");
            }else{
                exit;
            }

            
$right_extention 0;
            foreach (
$whitelist as $item) {
                if(
$item == $file_extension_lower) {
                    
$right_extention 1;
                    break;
                }
            }

            if(
$right_extention != 1){
                die(
'{"jsonrpc" : "2.0","error" : "You cannot upload this type files!"}');
            }

            
//rename chinese word to random string
            /*            if(preg_match("/\p{Han}+/u", $file_title)){
                            $file_title = random_string(12);
                        }*/

            //filter special characters
            
$file_title preg_replace('/\W/''_'$file_title);


            
$file_name $file_title.".".$file_extension;
            
$filePath   $targetDir "/" $file_name;

            
//check if file exist and rename
            
if (is_file($filePath)) {
                
$i 1;
                do {
                    
$filePath $targetDir "/" $file_title "(" $i ")." $file_extension;
                    
$file_name $file_title "(" $i ")." .$file_extension;
                    
$i++;
                } while (
is_file($filePath));

                
//unlink($filePath);
            
}

            
$file_info = array($file_title$file_name$filePath$file_extension);
            return 
$file_info;
        }

    }


// Make sure file is not cached (as it happens for example on iOS devices)
    
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
    
header("Cache-Control: no-store, no-cache, must-revalidate");
    
header("Cache-Control: post-check=0, pre-check=0"false);
    
header("Pragma: no-cache");

// 5 minutes execution time
    
@set_time_limit(60);

// Uncomment this one to fake upload time
// usleep(5000);

// Settings

    
$targetDir        'uploads';
    
$cleanupTargetDir true// Remove old files
    
$maxFileAge       3600// Temp file age in seconds
    
$valid_file       0;

    
//get uploader id
    
$uploader_id $_REQUEST['uploader_id'];

// Get a file name
    /*    if (isset($_REQUEST["name"])) {
            $fileName = $_REQUEST["name"];
        } elseif (!empty($_FILES)) {
            $fileName = $_FILES["file"]["name"];
        } else {
            $fileName = uniqid("file_");
        }*/

    
$file_info validate_upload_file($_FILES["file"]);

    
//check function return
    
if(!empty($file_info)){ //valid return
        
$file_title =  $file_info["0"];
        
$file_name $file_info["1"];
        
$filePath $file_info["2"];
        
$file_extension $file_info["3"];
    }else{  
//invalid return
        
exit;
    }


// Chunking might be enabled
    /*    $chunk  = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
        $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;*/

// Open temp file
    //set output file
    /*if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
        die('{"jsonrpc" : "2.0", "error" : "Failed to open output stream."}');
    }*/

    
if (!empty($_FILES)) {

        if (
$_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
            die(
'{"jsonrpc" : "2.0", "error" : "Failed to move uploaded file."}');
        }

        
// Read binary input stream and append it to temp file
        /*if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
            die('{"jsonrpc" : "2.0", "error" : "Failed to open input stream."}');
        }*/
        //}

        
$filename $_FILES["file"]['name'];
        
move_uploaded_file($_FILES["file"]['tmp_name'], $filePath) or die('{"jsonrpc" : "2.0","error" : "Failed to move uploaded file."}');

    } else {
        
/*if (!$in = @fopen("php://input", "rb")) {
            die('{"jsonrpc" : "2.0", "error" : "Failed to open input stream."');
        }*/
        
die('{"jsonrpc" : "2.0", "error" : "Failed to get uploaded file."}');
    }

    
/*while ($buff = fread($in, 4096)) {
        fwrite($out, $buff);
    }*/

//    @fclose($out);
//    @fclose($in);

// Check if file has been uploaded
    /*if (!$chunks || $chunk == $chunks - 1) {
        // Strip the temp .part suffix off
        rename("{$filePath}.part", $filePath);
    }*/



    //insert into table temp file
    
$sql "select max(file_id) as maxid from temp_file ";
    if (!(
$sth $dbh->prepare($sql))) {
        throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
    }
    if (!
$sth->execute()) {
        throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
    }
    
$row $sth->fetch(PDO::FETCH_ASSOC);
    
$max_id $row{"maxid"} + 1;


    
$sql "insert into temp_file (file_id, file_name, uploader_id, createby, createdate) values (?, ?, ?, ?, ?)";
    
$parameters = array($max_id,$file_name,$uploader_id$_SESSION['cmsloginid'], date("Y-m-d H:i:s"));
    if (!(
$sth $dbh->prepare($sql))) {
        throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
    }

    if (!
$sth->execute($parameters)) {
        throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
    }


    
// Return Success JSON-RPC response, define the json value
    
die('{"jsonrpc" : "2.0","success" : "1","file_id" : "'.$max_id.'", "file_title" : "' $file_title '", "file_name" : "' $file_name '", "file_extension" : "'.$file_extension.'"}');