示例#1
0
	function write() {
		global $current_user;
		//$this->setWriteParameter('register_module', 'result', false);

		$id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;


		if (!$id) {
			$this->newBook();
			return;
		}


		$books = Books::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', //lang_code
		    'annotation' => 'description'
		);

		Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
		$to_update = array();
		if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) {
			$folder = Config::need('static_path') . '/upload/covers/' . (ceil($book->id / 5000));
			@mkdir($folder);
			chmod($folder, 755);
			$filename = $folder . '/' . $book->id . '.jpg';
			$upload = new UploadAvatar($_FILES['cover']['tmp_name'], 100, 100, "simple", $filename);
			if ($upload->out)
				$to_update['is_cover'] = 1;
			else {
				throw new Exception('cant copy file to ' . $filename, 100);
			}
		}

		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] : 'fb2';
			$filetype = $filetype == 'fb2' ? 1 : 0;
			if (!$filetype)
				throw new Exception('only fb2 allowed');
			$destinationDir = Config::need('files_path') . DIRECTORY_SEPARATOR . getBookFileDirectory($book->id, $filetype);
			@mkdir($destinationDir, 755);
			// добавляем запись в базу
			$filesize = $_FILES['file']['size'];
			$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);
			if ($id_file = 78037 || $id_file = Database::lastInsertId()) {
				$destinationFile = getBookFilePath($id_file, $book->id, $filetype, Config::need('files_path'));
				move_uploaded_file($_FILES['file']['tmp_name'], $destinationFile);
			}
			if ($filetype == 1) {
				$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) {
			if (in_array($field, array('ISBN', 'year'))) {
				$value = is_numeric($value) ? $value : 0;
			}
			$q[] = '`' . $field . '`=' . Database::escape($value) . '';
		}

		if (count($q)) {
			$query = 'UPDATE `book` SET ' . implode(',', $q) . ' WHERE `id`=' . $book->id;
			Database::query($query);
			BookLog::addLog($to_update, $book->data);
			BookLog::saveLog($book->id, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEdit);
		}
		ob_end_clean();
		header('Location:' . Config::need('www_path') . '/b/' . $book->id);
		exit();
	}
示例#2
0
 function generateData()
 {
     global $current_user;
     /* @var $current_user CurrentUser */
     if (!$current_user->authorized) {
         throw new Exception('Auth required');
     }
     $current_user->can_throw('books_download');
     $filetype = Request::get(0);
     list($id_file, $id_book) = explode('_', Request::get(1));
     $can_load = $current_user->canBookDownload($id_book, $id_file);
     if ($can_load !== true) {
         if ($can_load[1]) {
             throw new Exception('You cant download this book - limit of a ' . $can_load[1] . ' books in day exceed');
         } else {
             throw new Exception('Please prolong your subscription, сучечка!');
         }
     }
     $book = Books::getInstance()->getByIdLoaded($id_book);
     /* @var $book Book */
     if (!$book->loaded) {
         throw new Exception('Book doesn\'t exists');
     }
     if (!$filetype || !$id_file || !$id_book) {
         throw new Exception('Wrong download url');
     }
     $realPath = getBookFilePath($id_file, $id_book, $filetype, Config::need('files_path'));
     global $dev_mode;
     if (!is_readable($realPath)) {
         if ($dev_mode) {
             throw new Exception('Sorry, file ' . $realPath . ' doesn\'t exists');
         } else {
             throw new Exception('Sorry, file  doesn\'t exists');
         }
     }
     $current_user->onBookDownload($book->id);
     $current_user->save();
     if (Request::get('html') !== false) {
         // downloading generated html
         $book->getHTMLDownload();
         $realPath = getBookFilePathFB2HtmlDownload($id_file, $id_book, $filetype, Config::need('files_path'));
         $filetype = 4;
     }
     @ob_end_clean();
     $ft = Config::need('filetypes');
     $book->setReaded();
     if (Config::need('smart_download')) {
         header("Content-Type: application/force-download");
         header("Content-Type: application/octet-stream");
         header("Content-Type: application/download");
         header("Content-Description: File Transfer");
         header('Content-Disposition: attachment; filename="' . $book->getTitle(1) . '.' . $ft[$filetype]);
         header("X-Accel-Redirect: " . str_replace('/w/ru.jnpe.ls2/core', '', $realPath));
         exit;
     }
     //
     header('Content-Disposition: attachment; filename="' . $book->getTitle(1) . '.' . $ft[$filetype]);
     header("Content-Type: application/force-download");
     header("Content-Type: application/octet-stream");
     header("Content-Type: application/download");
     header("Content-Description: File Transfer");
     readfile($realPath);
     exit;
 }
示例#3
0
文件: Book.php 项目: rasstroen/metro
 function getReadableFile()
 {
     if ($this->readableFile !== null) {
         return $this->readableFile;
     }
     /*
      1 => 'fb2',
      2 => 'txt',
      3 => 'fbz',
      4 => 'html',
      5 => 'htm',
      6 => 'rtf',
      7 => 'epub',
      8 => 'mobi',
      9 => 'pdf',
      10 => 'djvu',
      11 => 'doc'
     */
     $this->loadFiles();
     $found_type = 0;
     $id_file = 0;
     foreach ($this->files as $file) {
         //fb2
         if (!$found_type && $file['filetype'] == 1) {
             $found_type = 1;
             $id_file = $file['id'];
         }
     }
     if (!$found_type) {
         throw new Exception('no any readable files for this book');
     }
     $realPath = getBookFilePath($id_file, $this->id, $found_type, Config::need('files_path'));
     global $dev_mode;
     if (!is_readable($realPath)) {
         if ($dev_mode) {
             throw new Exception('Sorry, file ' . $realPath . ' doesn\'t exists');
         } else {
             throw new Exception('Sorry, file  doesn\'t exists');
         }
     }
     $this->readableFile = array($realPath, $found_type, $id_file);
     return $this->readableFile;
 }
示例#4
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;
    }