Пример #1
0
function importPage()
{
    if (!isLogged()) {
        header('Location: ' . Path::signin() . '&target=import');
        exit;
    }
    global $tpl;
    if (isset($_GET['imported'])) {
        $tpl->assign('imported', TRUE);
    } else {
        if (!empty($_FILES)) {
            if (!empty($_POST['token']) && acceptToken($_POST['token'])) {
                try {
                    $ext_array = explode('.', $_FILES['file']['name']);
                    $extension = end($ext_array);
                    $mime = $_FILES['file']['type'];
                    // check extension et mime type
                    if ($extension != 'json') {
                        throw new \Exception('<br />Extension of the file is not allowed! Please import a JSON file.');
                    }
                    if (!($mime == 'application/octet-stream' || $mime == 'application/json')) {
                        throw new \Exception('<br />MIME type of the file is not allowed! Please import a JSON file.');
                    }
                    $file = file_get_contents($_FILES['file']['tmp_name']);
                    if (!$file) {
                        throw new \Exception('An error occured while reading the file.');
                    }
                    $result = Books::import($file, isset($_POST['keep_ids']), isLogged());
                    if (!$result) {
                        throw new \Exception('An error occured while importing the file.');
                    }
                    header('Location: ' . Path::import() . '&imported');
                    exit;
                } catch (\Exception $e) {
                    $tpl->assign('error', $e->getMessage());
                }
            } else {
                errorPage('The received token was empty or invalid.', 'Invalid security token');
            }
        }
    }
    $tpl->assign('page_title', 'Import books');
    $tpl->assign('menu_links', Path::menu('import'));
    $tpl->assign('menu_links_admin', Path::menuAdmin('admin'));
    $tpl->assign('token', getToken());
    $tpl->draw('admin.import');
    exit;
}