示例#1
0
文件: index.php 项目: Devenet/MyBooks
function editBook()
{
    if (!isLogged()) {
        header('Location: ./');
        exit;
    }
    $books = new Books(isLogged());
    $id = (int) $_GET['edit'] + 0;
    if (!isset($books[$id])) {
        notFound();
    }
    $book = $books[$id];
    global $tpl;
    global $_CONFIG;
    // process to edit book in database
    if (isset($_POST) && !empty($_POST)) {
        if (!empty($_POST['token']) && acceptToken($_POST['token'])) {
            $inputs = array('title' => isset($_POST['title']) ? trim(htmlspecialchars($_POST['title'])) : NULL, 'author' => isset($_POST['author']) ? trim(htmlspecialchars($_POST['author'])) : NULL, 'summary' => isset($_POST['summary']) ? checkNewLineContent($_POST['summary']) : NULL, 'publisher' => isset($_POST['publisher']) ? trim(htmlspecialchars($_POST['publisher'])) : NULL, 'status' => isset($_POST['status']) ? Book::SEEN : NULL, 'note' => isset($_POST['note']) ? checkRatingNote($_POST['note'], isset($_POST['status']) ? Book::SEEN : NULL) : NULL, 'read_date' => isset($_POST['read_date']) ? checkInputDate($_POST['read_date']) : NULL, 'review' => isset($_POST['review']) ? checkNewLineContent($_POST['review']) : NULL, 'genre' => isset($_POST['genre']) ? checkGenre($_POST['genre']) : NULL, 'publication_year' => isset($_POST['publication_year']) ? checkInputYear($_POST['publication_year']) : NULL, 'pages' => isset($_POST['pages']) ? checkPages($_POST['pages']) : NULL, 'country' => isset($_POST['country']) ? checkCountry($_POST['country']) : NULL, 'link_website' => isset($_POST['link_website']) ? checkLink($_POST['link_website']) : NULL, 'link_image' => isset($_POST['link_image']) ? checkLink($_POST['link_image']) : NULL, 'link_image_import' => isset($_POST['link_image_import']) ? TRUE : NULL);
            try {
                if (empty($inputs['title'])) {
                    throw new \Exception('Title must not be empty.');
                }
                if (empty($inputs['author'])) {
                    throw new \Exception('Author must not be empty.');
                }
                if (empty($inputs['summary'])) {
                    throw new \Exception('Summary must not be empty.');
                }
                $book = array('id' => $id);
                // check if we need to get the image given with url
                if ($inputs['link_image_import']) {
                    importImage($inputs['link_image'], $book['id']);
                    $inputs['link_image'] = $_CONFIG['images'] . '/' . $id . '.jpg';
                }
                unset($inputs['link_image_import']);
                foreach ($inputs as $key => $value) {
                    $book[$key] = $value;
                }
                $books[$id] = $book;
                $books->save();
                header('Location: ' . Path::book($id));
                exit;
            } catch (\Exception $e) {
                $tpl->assign('error', $e->getMessage());
            }
        } else {
            errorPage('The received token was empty or invalid.', 'Invalid security token');
        }
    } else {
        $inputs = array('title' => $book['title'], 'author' => $book['author'], 'summary' => str_replace('<br />', '', $book['summary']), 'publisher' => $book['publisher'], 'status' => $book['status'], 'note' => $book['note'], 'read_date' => $book['read_date'], 'review' => str_replace('<br />', '', $book['review']), 'genre' => $book['genre'], 'publication_year' => $book['publication_year'], 'pages' => $book['pages'], 'country' => $book['country'], 'link_website' => preg_replace('#http://#', '', $book['link_website']), 'link_image' => preg_replace('#http://#', '', $book['link_image']));
    }
    $tpl->assign('page_title', 'Edit book');
    $tpl->assign('menu_links', Path::menu('edit'));
    $tpl->assign('menu_links_admin', Path::menuAdmin('edit'));
    $tpl->assign('inputs', $inputs);
    $tpl->assign('today', date('Y-m-d'));
    $tpl->assign('countries', displayCountryOptions($inputs['country']));
    $tpl->assign('token', getToken());
    $tpl->assign('target', Path::edit($id));
    $tpl->assign('delete', Path::delete($id));
    $tpl->draw('form.book');
    exit;
}
示例#2
0
function processImport()
{
    $nbImgProcessed = 0;
    $start = microtime(true);
    $sql = 'SELECT I.id, I.path, I.folder_id, F.thumb_img_id FROM files_to_import I, folders F WHERE I.folder_id=F.id ORDER BY I.id';
    $req = mysql_query($sql);
    $nbToProcess = mysql_num_rows($req);
    $setThumb = true;
    if ($nbToProcess == 0) {
        return -1;
    }
    $lastIdProcessed = 0;
    while ($infos = mysql_fetch_assoc($req)) {
        $path = substr($infos['path'], 0, strrpos($infos['path'], '/'));
        $fileName = substr(strrchr($infos['path'], '/'), 1);
        $imageId = importImage($path, $fileName, $infos['folder_id']);
        if ($setThumb && $infos['thumb_img_id'] == 0) {
            $sql = 'UPDATE folders SET thumb_img_id=' . $imageId . ' WHERE id=' . $infos['folder_id'] . '';
            mysql_query($sql);
            $setThumb = false;
        }
        $lastIdProcessed = $infos['id'];
        $nbImgProcessed++;
        // if( ( microtime( true ) - $start ) > 10 ){
        if ($nbImgProcessed == 1) {
            break;
        }
    }
    $sql = 'DELETE FROM files_to_import WHERE id<=' . $lastIdProcessed . '';
    mysql_query($sql);
    return $nbToProcess - $nbImgProcessed;
}