forked from malaohu/oneindex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImagesController.php
43 lines (38 loc) · 1.49 KB
/
ImagesController.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
<?php
class ImagesController{
function generateRandomString($length) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function index(){
if($this->is_image($_FILES["file"]) ){
$content = file_get_contents( $_FILES["file"]['tmp_name']);
$remotepath = 'images/'.date('Y/m/d/').$this->generateRandomString(10).'/';
$remotefile = $remotepath.$_FILES["file"]['name'];
$result = onedrive::upload(config('onedrive_root').$remotefile, $content);
if($result){
$root = get_absolute_path(dirname($_SERVER['SCRIPT_NAME'])).config('root_path');
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$url = $_SERVER['HTTP_HOST'].$root.'/'.$remotefile.((config('root_path') == '?')?'&s':'?s');
$url = $http_type.str_replace('//','/', $url);
view::direct($url);
}
}
return view::load('images/index');
}
function is_image($file){
$config = config('images@base');
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if(!in_array($ext,$config['exts'])){
return false;
}
if($file['size'] > 10485760 || $file['size'] == 0){
return false;
}
return true;
}
}