Ejemplo n.º 1
0
function upload_post_main_image($data)
{
    try {
        $cover_sizes = array(array(150, 150, false), array(400, 400, true));
        $content = $data['text'];
        $urls = array();
        preg_match_all("/(<img )(.+?)( \\/)?(>)/", $content, $images);
        foreach ($images[2] as $val) {
            if (preg_match("/(src=)('|\")(.+?)('|\")/", $val, $matches) == 1) {
                $urls[$matches[3]] = $matches[3];
            }
        }
        if (count($urls)) {
            foreach ($urls as $url) {
                $imgdata = grab($url);
                $tmp_name = '/tmp/image.jpg';
                file_put_contents($tmp_name, $imgdata);
                $size = getimagesize($tmp_name);
                if ($size) {
                    if ($size[0] >= 150) {
                        if ($size[1] >= 150) {
                            $folder = Config::need('static_path') . 'upload/post_images/' . $data['id'];
                            $filename = $folder . '/' . $data['id'] . '_' . $data['id_author'] . '.jpg';
                            $filename_o = $folder . '/' . $data['id'] . '_' . $data['id_author'] . '_big.jpg';
                            mkdir($folder);
                            $thumb = new Thumb();
                            try {
                                $thumb->createThumbnails($tmp_name, array($filename, $filename_o), $cover_sizes);
                            } catch (Exception $e) {
                                return false;
                            }
                            unlink($tmp_name);
                            return true;
                        }
                    }
                } else {
                    unlink($tmp_name);
                }
            }
        }
        return false;
    } catch (Exception $e) {
        return false;
    }
}
Ejemplo n.º 2
0
                if ($bio['foaf:img']) {
                    $pic = 0;
                    $imgdata = grab($bio['foaf:img']);
                    $tmp_name = '/tmp/aimage.jpg';
                    file_put_contents($tmp_name, $imgdata);
                    $size = getimagesize($tmp_name);
                    if ($size) {
                        if ($size[0] >= 10) {
                            if ($size[1] >= 10) {
                                $folder = Config::need('static_path') . 'upload/author_images/' . ceil($author['id'] / 500);
                                $filename = $folder . '/' . $author['id'] . '.jpg';
                                $filename1 = $folder . '/' . $author['id'] . '_small.jpg';
                                mkdir($folder);
                                $thumb = new Thumb();
                                try {
                                    $thumb->createThumbnails($tmp_name, array($filename, $filename1), $cover_sizes);
                                    $pic = 1;
                                } catch (Exception $e) {
                                    continue;
                                }
                                unlink($tmp_name);
                            }
                        }
                    } else {
                        unlink($tmp_name);
                    }
                } else {
                    $pic = 0;
                }
                $query = 'UPDATE `authors` SET
					`journaltitle`=' . Database::escape($bio['lj:journaltitle']) . ', 
Ejemplo n.º 3
0
 function write()
 {
     global $current_user;
     /* @var $current_user CurrentUser */
     $mask = array('id' => 'int', 'nickname' => array('type' => 'string', 'regexp' => '/^[A-Za-z][A-Za-z0-9_]+$/', 'min_length' => 3, 'max_length' => 26, '*' => true), 'role' => array('type' => 'int', '*' => true), 'link_fb' => array('type' => 'string', '*' => true), 'link_vk' => array('type' => 'string', '*' => true), 'link_lj' => array('type' => 'string', '*' => true), 'link_tw' => array('type' => 'string', '*' => true), 'quote' => array('type' => 'string', '*' => true), 'about' => array('type' => 'string', '*' => true));
     $params = Request::checkPostParameters($mask);
     $uid = isset($params['id']) ? $params['id'] : 0;
     if (!$uid) {
         throw new Exception('illegal user id');
     }
     if ($current_user->id != $params['id']) {
         if ($current_user->getRole() >= User::ROLE_BIBER) {
             $editing_user = Users::getByIdsLoaded(array($params['id']));
             $editing_user = isset($editing_user[$params['id']]) ? $editing_user[$params['id']] : false;
         }
     } else {
         $editing_user = $current_user;
     }
     $current_user->can_throw('users_edit', $editing_user);
     if ($editing_user) {
         if (trim($params['nickname']) != $editing_user->getNickName()) {
             if (!$editing_user->checkNickChanging()) {
                 throw new Exception('You can\'t change your nickname');
             }
         }
         //avatar
         if (isset($_FILES['picture']) && $_FILES['picture']['tmp_name']) {
             $filename = Config::need('avatar_upload_path') . '/' . $editing_user->id . '.jpg';
             $folder = Config::need('avatar_upload_path');
             $filename_normal = $folder . '/default_' . $editing_user->id . '.jpg';
             $filename_small = $folder . '/small_' . $editing_user->id . '.jpg';
             $filename_big = $folder . '/big_' . $editing_user->id . '.jpg';
             $filename_orig = $folder . '/orig_' . $editing_user->id . '.jpg';
             $thumb = new Thumb();
             $thumb->createThumbnails($_FILES['picture']['tmp_name'], array($filename_small, $filename_normal, $filename_big, $filename_orig), self::$cover_sizes);
             $editing_user->setProperty('picture', 1);
             $editing_user->setProperty('lastSave', time());
         }
         if ($editing_user->getRole() < User::ROLE_SITE_ADMIN) {
             if ($current_user->getRole() == User::ROLE_BIBER) {
                 if (($new_role = (int) $params['role']) !== false) {
                     foreach (Users::$rolenames as $id => $name) {
                         if ($id == $new_role) {
                             if ($new_role < User::ROLE_SITE_ADMIN) {
                                 $editing_user->setRole($new_role);
                             }
                         }
                     }
                 }
             }
             if ($current_user->getRole() > User::ROLE_BIBER) {
                 if (($new_role = (int) $params['role']) !== false) {
                     foreach (Users::$rolenames as $id => $name) {
                         if ($id == $new_role) {
                             if ($new_role <= User::ROLE_SITE_ADMIN) {
                                 $editing_user->setRole($new_role);
                             }
                         }
                     }
                 }
             }
         }
         $editing_user->save();
         // после редактирования профиля надо посбрасывать кеш со страницы профиля
         // и со страницы редактирования профиля
         // кеш в остальных модулях истечет сам
         Users::dropCache($editing_user->id);
     } else {
         Error::CheckThrowAuth(User::ROLE_SITE_ADMIN);
     }
 }
Ejemplo n.º 4
0
 function write()
 {
     global $current_user;
     $current_user->can_throw('books_edit');
     $id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;
     if (!$id) {
         $this->newAuthor();
         return;
     }
     $person = Persons::getInstance()->getByIdLoaded($id);
     if (!$person) {
         return;
     }
     $savedData = $person->data;
     /* @var $book Book */
     $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');
     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');
     }
     Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
     $to_update = array();
     if (isset($_FILES['picture']) && $_FILES['picture']['tmp_name']) {
         $folder = Config::need('static_path') . '/upload/authors/' . ceil($person->id / 5000);
         @mkdir($folder);
         // inserting new cover
         $query = 'INSERT INTO `person_covers` SET `id_person`=' . $person->id;
         Database::query($query);
         $cover_id = Database::lastInsertId();
         // generating file names
         $filename_normal = $folder . '/default_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_small = $folder . '/small_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_big = $folder . '/big_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_orig = $folder . '/orig_' . $person->id . '_' . $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);
         if ($savedData['has_cover']) {
             $current_user->gainActionPoints('authors_add_cover', $person->id, BiberLog::TargetType_person);
         } else {
             $current_user->gainActionPoints('authors_edit_cover', $person->id, BiberLog::TargetType_person);
         }
     }
     foreach ($fields as $field => $personfield) {
         if (!isset(Request::$post[$field])) {
             throw new Exception('field missed #' . $field);
         }
         if ($person->data[$personfield] != Request::$post[$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);
         }
         if ($field == 'bio') {
             list($full, $short) = $person->processBio($value);
             $q[] = '`bio`=' . Database::escape($full) . '';
             $q[] = '`short_bio`=' . Database::escape($short) . '';
             $value = $person->data['bio'] = $full;
             $person->data['short_bio'] = $short;
         } else {
             $q[] = '`' . $field . '`=' . Database::escape($value) . '';
             $person->data[$field] = $value;
         }
     }
     if (count($q)) {
         $query = 'UPDATE `persons` SET ' . implode(',', $q) . ' WHERE `id`=' . $person->id;
         Database::query($query);
         unset($to_update['authorlastSave']);
         PersonLog::addLog($to_update, $savedData, $person->id);
         PersonLog::saveLog($person->id, BiberLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personEdit);
         Persons::getInstance()->dropCache($person->id);
         $current_user->gainActionPoints(BiberLog::$actionTypes[BiberLog::BiberLogType_personEdit], $person->id, BiberLog::TargetType_person);
         $search = Search::getInstance();
         /* @var $search Search */
         $search->setAuthorToFullUpdate($person->id);
     }
     ob_end_clean();
     header('Location:' . Config::need('www_path') . '/a/' . $person->id);
     exit;
 }
Ejemplo n.º 5
0
    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;
    }