Beispiel #1
0
function file_handler()
{
    list($uploads_dir, $thumbs_dir) = setup_dir();
    $files = $_FILES['files'];
    $results = array();
    foreach ($files['error'] as $key => $error) {
        $result = isset($_POST['qid']) ? array('qid' => $_POST['qid']) : array();
        $name = escape_special_char($files['name'][$key]);
        $host = get_cdn();
        if ($error == UPLOAD_ERR_OK) {
            if ($files['size'][$key] > get_size_limit()) {
                $result['status'] = 'failed';
                $result['err'] = 'size_limit';
            } else {
                $temp = $files['tmp_name'][$key];
                if ($duplicate = is_duplicate($temp)) {
                    $result['status'] = 'success';
                    $result['thumb'] = ($duplicate['thumb'] == 'none' ? '' : $host) . $duplicate['thumb'];
                    $result['path'] = $host . $duplicate['path'];
                    $result['name'] = $duplicate['name'];
                    $result['width'] = $duplicate['width'];
                    $result['height'] = $duplicate['height'];
                    $result['exlong'] = $duplicate['exlong'];
                    $result['extiny'] = $duplicate['extiny'];
                } else {
                    $mime = file_mime_type($temp);
                    switch ($mime) {
                        case 'image/jpeg':
                            if (!preg_match('/\\.(jpg|jpeg|jpe|jfif|jfi|jif)$/i', $name)) {
                                $name .= '.jpg';
                            }
                            break;
                        case 'image/png':
                            if (!preg_match('/\\.(png)$/i', $name)) {
                                $name .= '.png';
                            }
                            break;
                        case 'image/gif':
                            if (!preg_match('/\\.(gif)$/i', $name)) {
                                $name .= '.gif';
                            }
                            break;
                        case 'image/svg+xml':
                            if (!preg_match('/\\.(svg)$/i', $name)) {
                                $name .= '.svg';
                            }
                            break;
                        default:
                            $result['status'] = 'failed';
                            $result['err'] = 'wrong_type';
                    }
                    if (!isset($result['status']) || !$result['status'] == 'failed') {
                        $name = rename_if_exists($name, $uploads_dir);
                        $path = "{$uploads_dir}/{$name}";
                        if (!move_uploaded_file($temp, ABSPATH . '/' . $path)) {
                            $result['status'] = 'failed';
                            $result['err'] = 'write_prohibited';
                        } else {
                            watermark($path);
                            $thumb = make_thumb($name, $path, $thumbs_dir);
                            if ($duplicate = duplicate_hash($name, $path, $thumb)) {
                                $result['status'] = 'success';
                            } else {
                                $result['status'] = 'error';
                                $result['err'] = 'fail_duplicate';
                            }
                            $result['path'] = $host . $path;
                            $result['name'] = $name;
                            $result['thumb'] = $thumb['generated'] ? $host . $thumb['path'] : 'none';
                            if (isset($thumb['width'])) {
                                $result['width'] = $thumb['width'];
                                $result['height'] = $thumb['height'];
                                $result['exlong'] = $thumb['exlong'];
                                $result['extiny'] = $thumb['extiny'];
                            }
                        }
                    }
                }
            }
        } else {
            switch ($error) {
                case UPLOAD_ERR_INI_SIZE:
                    $result['status'] = 'failed';
                    $result['err'] = 'php_upload_size_limit';
                    break;
                case UPLOAD_ERR_FORM_SIZE:
                    $result['status'] = 'failed';
                    $result['err'] = 'size_limit';
                    break;
                case UPLOAD_ERR_PARTIAL:
                    $result['status'] = 'failed';
                    $result['err'] = 'part_upload';
                    break;
                case UPLOAD_ERR_NO_FILE:
                    $result['status'] = 'failed';
                    $result['err'] = 'no_file';
                    break;
                case UPLOAD_ERR_NO_TMP_DIR:
                    $result['status'] = 'failed';
                    $result['err'] = 'no_tmp';
                    break;
                case UPLOAD_ERR_CANT_WRITE:
                    $result['status'] = 'failed';
                    $result['err'] = 'write_prohibited';
                    break;
            }
        }
        array_push($results, $result);
    }
    return $results;
}
<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
require_once __DIR__ . '/helpers.php';
Route::get('/', function () {
    return view('welcome');
});
// Supported themes
Route::pattern('theme', 'bootstrap|foundation|materialize');
Route::get('users/{theme?}', function ($theme = 'bootstrap') {
    // Change pagination theme
    Config::set('blade-pagination.theme', $theme);
    // Get CSS framework CDN
    $cdn = get_cdn($theme);
    // Paginate users and render the view
    $users = App\User::paginate(10);
    // Render the view
    return view('users', compact('users', 'theme', 'cdn'));
});