function write() { global $current_user; /* @var $current_user CurrentUser */ if (!$current_user->authorized) throw new Exception('Access denied'); $data = array( 'target_id' => max(0, (int) Request::$post['target_id']), 'target_type' => max(0, (int) Request::$post['target_type']), 'comment' => prepare_review(Request::$post['annotation']), 'rate' => min(6, max(0, (int) Request::$post['rate'])) + 1, ); $event = new Event(); if (!$data['comment']) { // inserting rate if ($data['rate'] && ($data['target_type'] == 0)) { $time = time(); if ($data['rate'] > 1) { $query = 'INSERT INTO `book_rate` SET `id_book`=' . $data['target_id'] . ',`id_user`=' . $current_user->id . ',`rate`=' . ($data['rate'] - 1) . ',`time`=' . $time . ' ON DUPLICATE KEY UPDATE `rate`=' . ($data['rate'] - 1) . ',`time`=' . $time . ''; Database::query($query); } //recalculating rate $query = 'SELECT COUNT(1) as cnt, SUM(`rate`) as rate FROM `book_rate` WHERE `id_book`=' . $data['target_id']; $res = Database::sql2row($query); $book_mark = round($res['rate'] / $res['cnt'] * 10); $query = 'UPDATE `book` SET `mark`=' . $book_mark . ' WHERE `id`=' . $data['target_id']; Database::query($query); $event->event_BookRateAdd($current_user->id, $data['target_id'], $data['rate'] - 1); } } else { if (!$data['target_id']) return; $query = 'INSERT INTO `reviews` SET `id_target`=' . $data['target_id'] . ', `target_type`=' . $data['target_type'] . ', `id_user`=' . $current_user->id . ', `time`=' . time() . ', `comment`=' . Database::escape($data['comment']) . ', `rate`=' . ($data['rate'] - 1) . ' ON DUPLICATE KEY UPDATE `time`=' . time() . ', `comment`=' . Database::escape($data['comment']) . ', `rate`=' . ($data['rate'] - 1) . ''; Database::query($query); //event $event->event_BookReviewAdd($current_user->id, $data['target_id'],$data['target_type'], $data['rate'] - 1 , $data['comment']); } $event->push(); }
function addPost() { global $current_user; if (!$current_user->id) { return; } $body = isset(Request::$post['body']) ? Request::$post['body'] : false; $subject = isset(Request::$post['subject']) ? Request::$post['subject'] : false; $body = prepare_review($body); $subject = prepare_review($subject, ''); if (!$body) { throw new Exception('post body missed'); } if ($body) { $event = new Event(); $event->event_PostAdd($current_user->id, $body, $subject); $event->push(); ob_end_clean(); header('Location: ' . Config::need('www_path') . '/me/wall/self'); exit; } }
function write() { global $current_user; if (!$current_user->authorized) { throw new Exception('Access Denied'); } $data = array('target_id' => max(0, (int) Request::$post['target_id']), 'target_type' => max(0, (int) Request::$post['target_type']), 'comment' => prepare_review(Request::$post['annotation']), 'rate' => min(6, max(0, (int) Request::$post['rate'])) + 1); $event = new Event(); $time = time(); //$old = MongoDatabase::findReviewEventData($current_user->id, $data['target_id']); //$with_review = (isset($old['body']) && $old['body']) ? 1 : 0; $with_review = 0; // upsert rate into database if ($data['rate']) { $query = 'INSERT INTO `book_rate` SET `with_review`=' . $with_review . ', `id_book`=' . $data['target_id'] . ',`id_user`=' . $current_user->id . ',`rate`=' . ($data['rate'] - 1) . ',`time`=' . $time . ' ON DUPLICATE KEY UPDATE `rate`=' . ($data['rate'] - 1) . ',`time`=' . $time . ',`with_review`=' . $with_review . ''; Database::query($query); //recalculating rate $query = 'SELECT COUNT(1) as cnt, SUM(`rate`) as rate FROM `book_rate` WHERE `id_book`=' . $data['target_id']; $res = Database::sql2row($query); $book_mark = round($res['rate'] / $res['cnt'] * 10); $query = 'UPDATE `book` SET `mark`=' . $book_mark . ' WHERE `id`=' . $data['target_id']; Database::query($query); } // insert data into mongo if (!$data['comment']) { unset($data['comment']); } if (isset($data['comment']) && $data['comment']) { $event->event_BookReviewAdd($current_user->id, $data); Notify::notifyNewBookReview($data['target_id'], $current_user->id); } else { if ($data['rate'] > 1) { $event->event_BookRateAdd($current_user->id, $data); } } $event->push(); }
function newBook() { // добавляем книгу global $current_user; $fields = array( 'title' => 'title', 'subtitle' => 'subtitle', 'isbn' => 'ISBN', 'year' => 'year', 'lang_code' => 'id_lang', //lang_code 'annotation' => 'description' ); Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']]; foreach ($fields as $field => $bookfield) { if (!isset(Request::$post[$field])) { throw new Exception('field missed #' . $field); } $to_update[$bookfield] = Request::$post[$field]; } $q = array(); foreach ($to_update as $field => $value) { if (in_array($field, array('ISBN', 'year'))) { $value = (int) $value; } $q[] = '`' . $field . '`=' . Database::escape($value) . ''; } $q[] = '`add_time`=' . time(); if (count($q)) { $query = 'INSERT INTO `book` SET ' . implode(',', $q); Database::query($query); if ($lid = Database::lastInsertId()) { if (Request::$post['n'] && Request::$post['m']) { // журнал - вставляем $query = 'INSERT INTO `book_magazines` SET `id_book`=' . $lid . ',id_magazine=' . (int) Request::$post['m'] . ',`year`=' . $to_update['year'] . ',`n`=' . (int) Request::$post['n']; Database::query($query, false); } if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) { $folder = Config::need('static_path') . '/upload/covers/' . (ceil($lid / 5000)); @mkdir($folder); chmod($folder, 755); $filename = $folder . '/' . $lid . '.jpg'; $upload = new UploadAvatar($_FILES['cover']['tmp_name'], 100, 100, "simple", $filename); if ($upload->out) { $query = 'UPDATE `book` SET `is_cover`=1 WHERE `id`=' . $lid; Database::query($query); } else { throw new Exception('cant copy file to ' . $filename, 100); } } BookLog::addLog($to_update, array()); BookLog::saveLog($lid, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookNew); ob_end_clean(); $event = new Event(); $event->event_BooksAdd($current_user->id, $lid); $event->push(); header('Location:' . Config::need('www_path') . '/b/' . $lid); exit(); } } }
function onNewFollowing($i_now_follow_id) { // все друзья кроме свежедобавленного должны узнать об этом! $event = new Event(); $event->event_FollowingAdd($this->id, $i_now_follow_id); $event->push(array($i_now_follow_id)); // а я получаю всю ленту свежедобавленного друга (последние 50 эвентов хотя бы) к себе на стену $wall = MongoDatabase::getUserWall($i_now_follow_id, 0, 50, 'self'); foreach ($wall as $wallItem) { if (isset($wallItem['_id'])) { MongoDatabase::pushEvents($i_now_follow_id, array($this->id), (string) $wallItem['id'], $wallItem['time']); } } }
function addLoved() { global $current_user; $event = new Event(); /* @var $current_user CurrentUser */ if (!$current_user->authorized) { $this->error('Auth'); return; } $item_type = isset($_POST['item_type']) ? $_POST['item_type'] : false; $item_id = isset($_POST['item_id']) ? (int) $_POST['item_id'] : false; if (!$item_type || !$item_id) { $this->error('item_id or item_type missed'); return; } if (!isset(Config::$loved_types[$item_type])) { $this->error('illegal item_type#' . $item_type); return; } $query = 'INSERT INTO `users_loved` SET `id_target`=' . $item_id . ',`target_type`=' . Config::$loved_types[$item_type] . ',`id_user`=' . $current_user->id; if (Database::query($query, false)) { $this->data['success'] = 1; $this->data['item_id'] = $item_id; $this->data['in_loved'] = 1; $event->event_LovedAdd($current_user->id, $item_id, $item_type); $event->push(); if ($item_type == 'book') { $time = time(); // inserting a new mark $query = 'INSERT INTO `book_rate` SET `id_book`=' . $item_id . ',`id_user`=' . $current_user->id . ',`rate`=5,`time`=' . $time . ' ON DUPLICATE KEY UPDATE `rate`=5 ,`time`=' . $time . ',`with_review`=0'; Database::query($query); //recalculating rate $query = 'SELECT COUNT(1) as cnt, SUM(`rate`) as rate FROM `book_rate` WHERE `id_book`=' . $item_id; $res = Database::sql2row($query); $book_mark = round($res['rate'] / $res['cnt'] * 10); $book = Books::getInstance()->getById($item_id); /* @var $book Book */ $book->updateLovedCount(); $query = 'UPDATE `book` SET `mark`=' . $book_mark . ' WHERE `id`=' . $item_id; Database::query($query); } return; } else { $query = 'DELETE FROM `users_loved` WHERE `id_target`=' . $item_id . ' AND `target_type`=' . Config::$loved_types[$item_type] . ' AND `id_user`=' . $current_user->id; if (Database::query($query, false)) { $this->data['success'] = 1; $this->data['item_id'] = $item_id; $this->data['in_loved'] = 0; if ($item_type == 'book') { $book = Books::getInstance()->getById($item_id); /* @var $book Book */ $book->updateLovedCount(); } return; } else { $this->data['success'] = 0; } } }
function AddBookShelf($id_book, $id_shelf) { $id_book = max(0, (int) $id_book); $id_shelf = max(0, (int) $id_shelf); $time = time(); $query = 'INSERT INTO `users_bookshelf` SET `id_user`=' . $this->id . ',`id_book`=' . $id_book . ', `bookshelf_type`=' . $id_shelf . ', `add_time`=' . $time . ' ON DUPLICATE KEY UPDATE `id_book`=' . $id_book . ', `bookshelf_type`=' . $id_shelf . ', `add_time`=' . $time . ''; Database::query($query); $this->shelf[$id_shelf][$id_book] = array('id_user' => $this->id, 'id_book' => $id_book, 'bookshelf_type' => $id_shelf, 'add_time' => $time); $event = new Event(); $event->event_addShelf($this->id, $id_book, $id_shelf); $event->push(); }
function newAuthor() { global $current_user; $current_user->can_throw('books_edit'); // добавляем книгу $fields = array('lang_code' => 'author_lang', 'bio' => 'bio', 'first_name' => 'first_name', 'middle_name' => 'middle_name', 'last_name' => 'last_name', 'homepage' => 'homepage', 'wiki_url' => 'wiki_url', 'date_birth' => 'date_birth', 'date_death' => 'date_death'); Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']]; if (!Request::$post['first_name'] || !Request::$post['last_name']) { throw new Exception('no author\'s name'); } if (!Request::$post['lang_code']) { throw new Exception('no author\'s language'); } $to_update = array(); foreach ($fields as $field => $personfield) { if (!isset(Request::$post[$field])) { throw new Exception('field missed #' . $field); } $to_update[$personfield] = Request::$post[$field]; } $q = array(); if (count($to_update)) { $to_update['authorlastSave'] = time(); } foreach ($to_update as $field => $value) { if ($field == 'date_birth' || $field == 'date_death') { $value = getDateFromString($value); } $person = new Person(); if ($field == 'bio') { list($full, $short) = $person->processBio($value); $q[] = '`bio`=' . Database::escape($full) . ''; $q[] = '`short_bio`=' . Database::escape($short) . ''; } else { $q[] = '`' . $field . '`=' . Database::escape($value) . ''; } } if (count($q)) { $q[] = '`a_add_time`=' . time(); $query = 'INSERT INTO `persons` SET ' . implode(',', $q); Database::query($query); $lid = Database::lastInsertId(); if ($lid) { if (isset($_FILES['picture']) && $_FILES['picture']['tmp_name']) { $folder = Config::need('static_path') . '/upload/authors/' . ceil($lid / 5000); @mkdir($folder); $query = 'INSERT INTO `person_covers` SET `id_person`=' . $lid; Database::query($query); $cover_id = Database::lastInsertId(); $filename_normal = $folder . '/default_' . $lid . '_' . $cover_id . '.jpg'; $filename_small = $folder . '/small_' . $lid . '_' . $cover_id . '.jpg'; $filename_big = $folder . '/big_' . $lid . '_' . $cover_id . '.jpg'; $filename_orig = $folder . '/orig_' . $lid . '_' . $cover_id . '.jpg'; $to_update['has_cover'] = $cover_id; $thumb = new Thumb(); $thumb->createThumbnails($_FILES['picture']['tmp_name'], array($filename_small, $filename_normal, $filename_big, $filename_orig), self::$cover_sizes); $query = 'UPDATE `persons` SET `has_cover`=' . $cover_id . ' WHERE `id`=' . $lid; Database::query($query); } unset($to_update['authorlastSave']); PersonLog::addLog($to_update, array(), $lid); PersonLog::saveLog($lid, BookLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personNew); $event = new Event(); $event->event_AuthorAdd($current_user->id, $lid); $event->push(); $search = Search::getInstance(); /* @var $search Search */ $search->setAuthorToFullUpdate($lid); ob_end_clean(); Persons::getInstance()->dropCache($lid); $current_user->gainActionPoints(BiberLog::$actionTypes[BiberLog::BiberLogType_personNew], $lid, BiberLog::TargetType_person); header('Location:' . Config::need('www_path') . '/a/' . $lid); exit; } } }
function write() { global $current_user; if (!$current_user->authorized) { throw new Exception('Access Denied'); } $id = isset(Request::$post['id']) ? Request::$post['id'] : 0; $id = max(0, (int) $id); if (isset(Request::$post['serie1_id'])) { $this->_glue(); return; } if (!$id) { $this->_new(); return; } $query = 'SELECT * FROM `series` WHERE `id`=' . $id; $old = Database::sql2row($query); if (!$old || !$old['id']) { throw new Exception('no such serie #' . $id); } $parent_id = isset(Request::$post['id_parent']) ? Request::$post['id_parent'] : false; $parent_id = max(0, (int) $parent_id); if (!$id) { throw new Exception('Illegal id'); } $title = isset(Request::$post['title']) ? Request::$post['title'] : false; $description = isset(Request::$post['description']) ? Request::$post['description'] : false; if ($parent_id == $id) { throw new Exception('Illegal parent'); } if ($parent_id) { $query = 'SELECT `id` FROM `series` WHERE `id`=' . $parent_id; if (!Database::sql2single($query)) { throw new Exception('No such parent'); } } if (!$title) { throw new Exception('Empty title'); } $description = prepare_review($description); $title = prepare_review($title, ''); $new = array('description' => $description, 'title' => $title, 'id_parent' => (int) $id_parent); Database::query('START TRANSACTION'); SerieLog::addLog($new, $old, $id); SerieLog::saveLog($id, BookLog::TargetType_serie, $current_user->id, BiberLog::BiberLogType_serieEdit); $query = 'UPDATE `series` SET `id_parent`=' . $parent_id . ',`title`=' . Database::escape($title) . ', `description`=' . Database::escape($description) . ' WHERE `id`=' . $id; Database::query($query); Database::query('COMMIT'); $event = new Event(); $event->event_SeriesEdit($current_user->id, $id); $event->push(); $search = Search::getInstance(); /* @var $search Search */ $search->setSerieToFullUpdate($id); }
function write() { global $current_user; $points_gained = false; /* @var $current_user CurrentUser */ Database::query('START TRANSACTION'); $current_user->can_throw('books_edit'); if (!isset(Request::$post['lang_code']) || !Request::$post['lang_code']) { throw new Exception('field missed #lang_code'); } $id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false; if (Request::post('isbn')) { Request::$post['isbn'] = extractISBN(Request::$post['isbn']); } if (!$id) { $this->newBook(); return; } $books = Books::getInstance()->getByIdsLoaded(array($id)); $book = is_array($books) ? $books[$id] : false; if (!$book) { return; } /* @var $book Book */ $fields = array('title' => 'title', 'subtitle' => 'subtitle', 'isbn' => 'ISBN', 'year' => 'year', 'lang_code' => 'id_lang', 'annotation' => 'description', 'rightholder' => 'id_rightholder'); Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']]; Request::$post['annotation'] = trim(prepare_review(Request::$post['annotation'], false, '<img>')); Request::$post['title'] = trim(prepare_review(Request::$post['title'], '')); Request::$post['year'] = (int) Request::$post['year']; $magazineData = array(); if ($book->data['book_type'] == Book::BOOK_TYPE_MAGAZINE) { $magazineData = Database::sql2row('SELECT * FROM `magazines` M LEFT JOIN book_magazines BM ON BM.id_magazine=M.id WHERE BM.id_book=' . $book->id); $book->data['n'] = max(0, $magazineData['n']); $book->data['year'] = $magazineData['year']; Request::$post['n'] = isset(Request::$post['n']) && Request::$post['n'] ? Request::$post['n'] : $magazineData['n']; } $to_update_m = array(); $to_update = array(); if (isset(Request::$post['quality'])) { if ($book->data['quality'] != (int) Request::$post['quality']) { $to_update['quality'] = (int) Request::$post['quality']; } } if (isset(Request::$post['n'])) { if (isset($book->data['n']) && $book->data['n'] != (int) Request::$post['n']) { $to_update_m['n'] = (int) Request::$post['n']; Request::$post['title'] = $magazineData['title']; Request::$post['subtitle'] = '№ ' . $to_update_m['n'] . ' за ' . Request::$post['year'] . ' год'; } if (isset($book->data['year']) && $book->data['year'] != (int) Request::$post['year']) { $to_update_m['n'] = (int) Request::$post['n']; Request::$post['title'] = $magazineData['title']; Request::$post['subtitle'] = '№ ' . $to_update_m['n'] . ' за ' . Request::$post['year'] . ' год'; } } if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) { $folder = Config::need('static_path') . '/upload/covers/' . ceil($book->id / 5000); @mkdir($folder); // inserting new cover $query = 'INSERT INTO `book_covers` SET `id_book`=' . $book->id; Database::query($query); $cover_id = Database::lastInsertId(); // generating file names $filename_normal = $folder . '/default_' . $book->id . '_' . $cover_id . '.jpg'; $filename_small = $folder . '/small_' . $book->id . '_' . $cover_id . '.jpg'; $filename_big = $folder . '/big_' . $book->id . '_' . $cover_id . '.jpg'; $filename_orig = $folder . '/orig_' . $book->id . '_' . $cover_id . '.jpg'; $to_update['is_cover'] = $cover_id; $thumb = new Thumb(); $thumb->createThumbnails($_FILES['cover']['tmp_name'], array($filename_small, $filename_normal, $filename_big, $filename_orig), self::$cover_sizes); if ($book->data['is_cover']) { $current_user->gainActionPoints('books_edit_cover', $book->id, BiberLog::TargetType_book); } else { $current_user->gainActionPoints('books_add_cover', $book->id, BiberLog::TargetType_book); } $points_gained = true; } // file loading if (isset($_FILES['file']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name']) { $filetype_ = explode('.', $_FILES['file']['name']); $filetype_ = isset($filetype_[count($filetype_) - 1]) ? $filetype_[count($filetype_) - 1] : ''; $fts = Config::need('filetypes'); $filetype = false; foreach ($fts as $ftid => $ftname) { if ($ftname == $filetype_) { $filetype = $ftid; } } if (!$filetype) { throw new Exception('wrong filetype:' . $filetype_); } $destinationDir = Config::need('files_path') . DIRECTORY_SEPARATOR . getBookFileDirectory($book->id, $filetype); @mkdir($destinationDir, 0755); // добавляем запись в базу $filesize = $_FILES['file']['size']; $query = 'SELECT * FROM `book_files` WHERE `id_book`=' . $book->id; $files = Database::sql2array($query, 'filetype'); // replacing file if (isset($files[$filetype])) { $old_id_file = $files[$filetype]['id']; $old_id_file_author = $files[$filetype]['id_file_author']; $old_filesize = $files[$filetype]['filesize']; $query = 'DELETE FROM `book_files` WHERE `id`=' . $old_id_file; Database::query($query); $query = 'INSERT IGNORE INTO `book_files` SET `id_book`=' . $book->id . ', `filetype`=' . $filetype . ', `id_file_author`=' . $current_user->id . ', `modify_time`=' . time() . ', `filesize`=' . $filesize; Database::query($query); $id_file = Database::lastInsertId(); BookLog::addLog(array('id_file' => $id_file, 'filetype' => $filetype, 'id_file_author' => $current_user->id, 'filesize' => $filesize), array('id_file' => $old_id_file, 'filetype' => 0, 'id_file_author' => $old_id_file_author, 'filesize' => $old_filesize), $book->id); Database::query($query); $current_user->gainActionPoints('books_edit_file', $book->id, BiberLog::TargetType_book); } else { $query = 'INSERT INTO `book_files` SET `id_book`=' . $book->id . ', `filetype`=' . $filetype . ', `id_file_author`=' . $current_user->id . ', `modify_time`=' . time() . ', `filesize`=' . $filesize; Database::query($query); $id_file = Database::lastInsertId(); BookLog::addLog(array('id_file' => $id_file, 'filetype' => $filetype, 'id_file_author' => $current_user->id, 'filesize' => $filesize), array('id_file' => 0, 'filetype' => 0, 'id_file_author' => 0, 'filesize' => 0), $book->id); $current_user->gainActionPoints('books_add_file', $book->id, BiberLog::TargetType_book); } if ($id_file) { $points_gained = true; if (!$book->data['id_main_file'] || isset($files[$filetype])) { $to_update['id_main_file'] = $id_file; } $destinationFile = getBookFilePath($id_file, $book->id, $filetype, Config::need('files_path')); if (!move_uploaded_file($_FILES['file']['tmp_name'], $destinationFile)) { throw new Exception('Cant save file to ' . $destinationFile); } // event for new File $event = new Event(); $event->event_BooksAddFile($current_user->id, $book->id); $event->push(); if ($filetype == 1) { // FB2 $parser = new FB2Parser($destinationFile); $parser->parseDescription(); $toc = $parser->getTOCHTML(); Request::$post['annotation'] = $parser->getProperty('annotation'); Request::$post['title'] = $parser->getProperty('book-title'); $to_update['table_of_contents'] = $toc; } } } foreach ($fields as $field => $bookfield) { if (!isset(Request::$post[$field])) { throw new Exception('field missed #[' . $field . ']'); } if ($book->data[$bookfield] != Request::$post[$field]) { $to_update[$bookfield] = Request::$post[$field]; } } $q = array(); foreach ($to_update as $field => &$value) { $q[] = '`' . $field . '`=' . Database::escape($value) . ''; } $push_event = true; if (count($q)) { if (count($to_update) == 1) { foreach ($to_update as $kk => $vv) { if ($kk == 'id_main_file') { $push_event = false; } } } $query = 'UPDATE `book` SET ' . implode(',', $q) . ' WHERE `id`=' . $book->id; Database::query($query); if (count($to_update_m)) { $to_update['n'] = $to_update_m['n']; } BookLog::addLog($to_update, $book->data, $book->id); foreach ($to_update as $f => $v) { $book->data[$f] = $v; } $search = Search::getInstance(); /* @var $search Search */ $search->updateBook($book); if ($push_event) { $event = new Event(); $event->event_BooksEdit($current_user->id, $book->id); $event->push(); } if (!$points_gained) { $current_user->gainActionPoints('books_edit', $book->id, BiberLog::TargetType_book); } } BookLog::saveLog($book->id, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEdit); Books::getInstance()->dropCache($book->id); if (count($to_update_m)) { if ($to_update_m['n'] && $book->data['book_type'] == Book::BOOK_TYPE_MAGAZINE) { Database::query('UPDATE `book_magazines` SET `n`=' . $to_update_m['n'] . ',`year`=' . (int) $book->data['year'] . ' WHERE `id_book`=' . $book->id); } } ob_end_clean(); header('Location:' . Config::need('www_path') . '/b/' . $book->id); Database::query('COMMIT'); exit; }