Beispiel #1
0
	function write() {
		global $current_user;
		/* @var $current_user CurrentUser */
		if (!$current_user->authorized)
			throw new Exception('Access denied');

		$id = isset(Request::$post['id']) ? Request::$post['id'] : 0;
		$id = max(0, (int) $id);
		$parent_id = isset(Request::$post['parent_id']) ? Request::$post['parent_id'] : 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, '');

		$query = 'UPDATE `series` SET `id_parent`=' . $parent_id . ',`title`=' . Database::escape($title) . ', `description`=' . Database::escape($description) . ' WHERE `id`=' . $id;
		Database::query($query);
	}
Beispiel #2
0
    /**
     * сохраняем факт скачивания книги 
     * @param type $variables 
     */
    public function saveUserDownloads($user_id, $book_id, $time = false)
    {
        Database::query('START TRANSACTION');
        // for user
        $period = 24 * 60 * 60;
        // каждый день
        $time_normalized = $time ? $time : floor(time() / $period) * $period;
        // а не качал ли эту книгу юзер уже?
        $query = 'SELECT COUNT(1) FROM  `stat_user_download` WHERE 
			`id_user`=' . $user_id . ' AND
			`id_book`=' . $book_id . ' AND
			`time`>=' . $time_normalized . '';
        $cnt = Database::sql2single($query);
        if ($cnt) {
            // already have
            return;
        }
        $query = 'INSERT IGNORE INTO `stat_user_download` SET 
			`id_user`=' . $user_id . ', 
			`id_book`=' . $book_id . ',
			`time`=' . $time_normalized;
        Database::query($query);
        // for book stat
        $query = 'INSERT INTO `stat_book_download` SET 
			`id_book`=' . $book_id . ',
			`count` = 1,
			`time`=' . $time_normalized . ' ON DUPLICATE KEY UPDATE
			`count` = `count`+1';
        Database::query($query);
        // updating book download_count
        $query = 'UPDATE `book` SET `download_count`=`download_count`+1 WHERE `id`=' . $book_id;
        Database::query($query);
        // genres
        $book = Books::getInstance()->getByIdLoaded($book_id);
        /* @var $book Book */
        $genres = $book->getGenres();
        if (count($genres)) {
            foreach ($genres as $gid => $data) {
                $query = 'INSERT INTO `stat_genre_download` SET 
					`id_genre`=' . $gid . ',
					`count`=1,
					`time`=' . $time_normalized . ' ON DUPLICATE KEY UPDATE
					`count` = `count`+1';
                Database::query($query);
            }
        }
        // authors
        $authors = $book->getAuthors();
        if (count($authors)) {
            foreach ($authors as $id => $data) {
                $query = 'INSERT INTO `stat_author_download` SET 
					`id_author`=' . $data['id'] . ',
					`count`=1,
					`time`=' . $time_normalized . ' ON DUPLICATE KEY UPDATE
					`count` = `count`+1';
                Database::query($query);
            }
        }
        Database::query('COMMIT');
    }
 function getRightholders()
 {
     $cond = new Conditions();
     $per_page = 0;
     if (isset($this->params['per_page'])) {
         $per_page = (int) $this->params['per_page'];
     }
     $per_page = $per_page > 0 ? $per_page : 1;
     $pagingName = isset($this->params['paging_parameter_name']) ? $this->params['paging_parameter_name'] : 'p';
     $where = '';
     $order = 'ORDER BY `id` DESC ';
     $group_by = '';
     $query = 'SELECT COUNT(1) FROM `rightholders` ' . $where . ' ' . $group_by . '';
     $count = Database::sql2single($query);
     $cond->setPaging($count, $per_page, $pagingName);
     $limit = $cond->getLimit();
     $limit = ' LIMIT ' . $limit;
     $query = 'SELECT * FROM `rightholders`' . $where . ' ' . $group_by . ' ' . $order . ' ' . $limit;
     $data = Database::sql2array($query);
     foreach ($data as &$row) {
         $row['path'] = Config::need('www_path') . '/admin/rightholders/' . $row['id'];
     }
     $this->data['rightholders'] = $data;
     $this->data['rightholders']['title'] = 'Правообладатели';
     $this->data['rightholders']['count'] = $count;
     $this->data['conditions'] = $cond->getConditions();
 }
    function process()
    {
        global $current_user;
        /* @var $current_user CurrentUser */
        $current_user->can_throw('add_comments');
        /*
         [writemodule] => CommentsWriteModule
         [reply_to] => 1
         [doc_id] => 440
         [comment] => ghjkhjk
        */
        $document_id = max(0, (int) Request::post('doc_id'));
        $table = Request::post('table');
        $comment = Request::post('comment');
        $reply_to = max(0, (int) Request::post('reply_to'));
        Database::query('START TRANSACTION');
        $query = 'SELECT max(`id`) as `id` FROM `comments` WHERE `doc_id` = ' . $document_id . ' AND `table`=' . Database::escape($table) . '';
        $maxid = 1 + max(0, Database::sql2single($query));
        $query = 'INSERT INTO `comments` SET 
		`id`=' . $maxid . ',
		`table`=' . Database::escape($table) . ', 
		`comment`=' . Database::escape($comment) . ',
		`parent`=' . $reply_to . ',
		`doc_id`=' . $document_id . ',
		`id_author`=' . $current_user->id . ',
		`time`=' . time();
        Database::query($query);
        Database::query('COMMIT');
    }
 function getCityList()
 {
     $country = isset($_POST['country_id']) ? (int) $_POST['country_id'] : 1;
     $this->data['city_list'] = Database::sql2array('SELECT `id`,`name` FROM `lib_city` WHERE `country_id`=' . $country . ' LIMIT 1000', 'id');
     $this->data['country_id'] = $country;
     $this->data['city_id'] = Database::sql2single('SELECT `id` FROM `lib_city` WHERE `country_id`=' . $country . ' LIMIT 1');
 }
Beispiel #6
0
	function getCountBySQL($where = false) {
		if (isset($this->cachedCount[$where]))
			return $this->cachedCount[$where];
		$where = $where ? 'WHERE ' . $where : '';
		$query = 'SELECT COUNT(1) FROM ' . $this->Collection->tableName . ' ' . $where;
		$this->cachedCount[$where] = max(0, (int) Database::sql2single($query));
		return $this->cachedCount[$where];
	}
Beispiel #7
0
	public function getAvailableNickname($nickname, $additional = '') {
		$nickname = trim($nickname) . $additional;
		$query = 'SELECT `nickname` FROM `users` WHERE `nickname` LIKE \'' . $nickname . '\' LIMIT 1';
		$row = Database::sql2single($query);
		if ($row && $row['nickname']) {
			return $this->getAvailableNickname($nickname, $additional . rand(1, 99));
		}
		return $nickname;
	}
Beispiel #8
0
	function getNew() {
		$uid = Request::get(0);
		if ($uid != 'me') {
			if ($uid)
				$uid = Database::sql2single('SELECT `id` FROM `users` WHERE `nickname`=' . Database::escape($uid));
		}
		if($uid)
		XMLClass::$varNode->setAttribute('to', $uid);
		$this->data['message'] = array();
		$this->data['message']['thread_id'] = $this->thread_id;
	}
Beispiel #9
0
	function __construct($id = false, $data = false) {
		$this->loaded = false;
		if ($id && !is_numeric($id)) {
			$query = 'SELECT `id` FROM `users` WHERE `nickname`=' . Database::escape($id);
			$id = (int) Database::sql2single($query);
		}
		if ($id) {
			$this->id = max(0, $id);
		}
		if ($data)
			$this->load($data);
	}
Beispiel #10
0
 function getUserReview()
 {
     global $current_user;
     if (!$current_user->authorized) {
         return;
     }
     $res = MongoDatabase::getReviewEvent($current_user->id, $this->target_id);
     $this->data = $this->_item($res);
     $this->data['review']['target_id'] = $this->target_id;
     $this->data['review']['target_type'] = $this->target_type;
     $this->data['review']['rate'] = isset($this->data['review']['rate']) ? $this->data['review']['rate'] : Database::sql2single('SELECT `rate` FROM `book_rate` WHERE `id_book` =' . $this->target_id . ' AND `id_user`=' . $current_user->id);
 }
Beispiel #11
0
	function getUserReview() {
		global $current_user;
		if (!$current_user->authorized)
			return;
		$query = 'SELECT * FROM `reviews` WHERE `id_target`=' . $this->target_id . ' AND `target_type`=' . $this->target_type . ' AND `id_user`=' . $current_user->id;
		$res = Database::sql2array($query);
		$this->data = $this->_item($res);
		$this->data['review']['target_id'] = $this->target_id;
		$this->data['review']['target_type'] = $this->target_type;
		$this->data['review']['rate'] = isset($this->data['review']['rate']) ?
			$this->data['review']['rate'] :
			Database::sql2single('SELECT `rate` FROM `book_rate` WHERE `id_book` =' . $this->target_id . ' AND `id_user`=' . $current_user->id);
	}
Beispiel #12
0
 function add_album_relation()
 {
     $album_id = $_POST['album_id'];
     $nick = $_POST['nick'];
     $role = $_POST['role'];
     $user_id = Database::sql2single('SELECT `id` FROM `user` WHERE `nickname`=' . Database::escape($nick));
     Database::query('INSERT INTO `album_family` SET
         `album_id`=' . $album_id . ',
         `user_id`=' . $user_id . ',
         `family_role`=' . $role . ',
         `add_time`=' . time() . '
             ON DUPLICATE KEY UPDATE
          `family_role`=' . $role . '');
 }
Beispiel #13
0
 function getOne($for_editing = false)
 {
     if ($this->genre_id) {
         $query = 'SELECT * FROM `genre` WHERE `id`=' . Database::escape($this->genre_id);
     } else {
         $query = 'SELECT * FROM `genre` WHERE `name`=' . Database::escape($this->genre_name);
     }
     $data = Database::sql2row($query);
     if (!isset($data['name'])) {
         return;
     }
     $this->data['genre'] = array('name' => $data['name'], 'id' => $data['id'], 'id_parent' => $data['id_parent'], 'title' => $data['title'], 'description' => $data['description'], 'books_count' => $data['books_count'], 'path' => Config::need('www_path') . '/genres/' . $data['id'], 'path_edit' => Config::need('www_path') . '/genres/' . $data['id'] . '/edit');
     Request::pass('genre-title', $data['title']);
     if (!$data['id_parent']) {
         $this->data['genre']['subgenres'] = $this->getAll($data['id']);
         return;
     }
     if (!$for_editing) {
         $query = 'SELECT COUNT(1) FROM `book_genre` BG JOIN `book` B ON B.id = BG.id_book WHERE BG.id_genre = ' . $data['id'] . '';
         $count = Database::sql2single($query);
         $cond = new Conditions();
         $cond->setPaging($count, 20);
         $limit = $cond->getLimit();
         $this->data['conditions'] = $cond->getConditions();
         $query = 'SELECT `id_book` FROM `book_genre` BG JOIN `book` B ON B.id = BG.id_book WHERE BG.id_genre = ' . $data['id'] . ' ORDER BY B.mark DESC LIMIT ' . $limit;
         $bids = Database::sql2array($query, 'id_book');
         $books = Books::getInstance()->getByIdsLoaded(array_keys($bids));
         Books::getInstance()->LoadBookPersons(array_keys($bids));
         $aids = array();
         foreach ($books as $book) {
             $book = Books::getInstance()->getById($book->id);
             list($aid, $aname) = $book->getAuthor(1, 1, 1);
             // именно наш автор, если их там много
             $this->data['genre']['books'][$book->id] = $book->getListData();
             $aids[$aid] = $aid;
         }
         if (count($aids)) {
             $persons = Persons::getInstance()->getByIdsLoaded($aids);
             foreach ($persons as $person) {
                 $this->data['genre']['authors'][] = $person->getListData();
             }
         }
         $this->data['parent'] = array();
         if ($data['id_parent']) {
             $data = Database::sql2row('SELECT * FROM `genre` WHERE `id`=' . Database::escape($data['id_parent']));
             $this->data['genre']['parent'][] = array('name' => $data['name'], 'id' => $data['id'], 'id_parent' => $data['id_parent'], 'title' => $data['title'], 'description' => $data['description'], 'books_count' => $data['books_count'], 'path' => Config::need('www_path') . '/genres/' . $data['id'], 'path_edit' => Config::need('www_path') . '/genres/' . $data['id'] . '/edit');
         }
     }
 }
Beispiel #14
0
    public static function addCity($id_country, $name)
    {
        if (!$id_country) {
            return false;
        }
        $query = 'INSERT INTO `lib_city` SET `verified`=0, `name`=' . Database::escape($name) . ',`country_id`=' . (int) $id_country . '
			ON DUPLICATE KEY UPDATE `country_id`=' . (int) $id_country;
        Database::query($query);
        $id = Database::lastInsertId();
        if (!$id) {
            $id = Database::sql2single('SELECT id FROM `lib_city` WHERE `country_id`=' . (int) $id_country . '
				AND  `name`=' . Database::escape($name));
        }
        return $id;
    }
Beispiel #15
0
 function addComment()
 {
     global $current_user;
     $subscribe = false;
     if (isset(Request::$post['subscribe'])) {
         if (Request::$post['subscribe']) {
             $subscribe = true;
         }
     }
     if (!$current_user->id) {
         return;
     }
     $comment = isset(Request::$post['comment']) ? Request::$post['comment'] : false;
     $comment = trim(prepare_review($comment, '<em><i><strong><b><u><s>'));
     if (!$comment) {
         throw new Exception('comment body expected');
     }
     $post_id = Request::$post['id'];
     $data = array();
     if ($post_id) {
         if (isset(Request::$post['comment_id']) && ($comment_id = Request::$post['comment_id'])) {
             $data = MongoDatabase::addEventComment($post_id, $current_user->id, $comment, $comment_id);
             if ($data) {
                 Notify::notifyEventCommentAnswer($data['commenter_id'], $post_id, $data['comment_id']);
             }
         } else {
             $data = MongoDatabase::addEventComment($post_id, $current_user->id, $comment);
             if ($data) {
                 Notify::notifyEventComment($data['user_id'], $post_id, $data['comment_id']);
             }
         }
     }
     if ($data) {
         if ($subscribe) {
             // на своё и так и так подписаны
             if ($data['post']['user_id'] != $current_user->id) {
                 $query = 'SELECT `id` FROM `events` WHERE `mongoid`=' . Database::escape($post_id);
                 $intid = Database::sql2single($query);
                 if ($intid) {
                     /* @var $current_user User */
                     $current_user->setNotifyRule(UserNotify::UN_COMMENT_ANSWER, UserNotify::UNT_NOTIFY);
                     $current_user->save();
                     Notify::notifySubscribe($current_user->id, $intid);
                 }
             }
         }
     }
 }
Beispiel #16
0
    public static function setStatus($id_user, $id_book, $status, $state)
    {
        global $current_user;
        $book = Books::getInstance()->getByIdLoaded($id_book);
        /* @var $book Book */
        if ($book->getQuality() >= BOOK::BOOK_QUALITY_BEST) {
            throw new Exception('book quality is best, you cant fix states');
        }
        if (!isset(self::$statuses[$status])) {
            throw new Exception('no status #' . $status);
        }
        if (!isset(self::$states[$state])) {
            throw new Exception('no status #' . $state);
        }
        $can_comment = false;
        if ($state > 0) {
            $query = 'SELECT `time` FROM `ocr` WHERE  `id_book`=' . $id_book . ' AND `id_user`=' . $id_user . ' AND `status`=' . $status . ' AND `state`=' . $state;
            $last_time = Database::sql2single($query);
            if (time() - $last_time > 24 * 60 * 60) {
                $can_comment = true;
            }
        }
        if ($state == 0 && $status !== 0) {
            // delete
            $query = 'DELETE FROM `ocr` WHERE  `id_book`=' . $id_book . ' AND `id_user`=' . $id_user . ' AND `status`=' . $status . '';
        } else {
            // upsert
            $query = 'INSERT INTO `ocr` SET `id_book`=' . $id_book . ', `id_user`=' . $id_user . ', `status`=' . $status . ',`state`=' . $state . ',`time`=' . time() . '
			ON DUPLICATE KEY UPDATE
			`time`=' . time() . ', `state`=' . $state;
        }
        if (!Database::query($query, false)) {
            throw new Exception('Duplicating #book ' . $id_book . ' #status' . $status . ' #state' . $state);
        }
        if ($state == 0) {
            $comment = 'User ' . $current_user->id . ' drop status ' . $status . ' state ' . $state . ' user_id ' . $id_user;
        } else {
            $comment = 'User ' . $current_user->id . ' set status ' . $status . ' state ' . $state . ' user_id ' . $id_user;
        }
        $comUser = Users::getById($id_user);
        /* @var $comUser User */
        if ($can_comment && ($part = self::getMessagePart($status, $state))) {
            $comment = mb_strtolower($part, 'UTF-8') . ' книгу';
            MongoDatabase::addSimpleComment(BiberLog::TargetType_book, $id_book, $id_user, $comment);
        }
    }
    function _list($opts = array())
    {
        $has_paging = !isset($opts['no_paging']);
        $show_sortings = isset($opts['show_sortings']);
        $per_page = isset($opts['per_page']) ? $opts['per_page'] : 10;
        $per_page = min(100, max(1, (int) $per_page));
        $cond = new Conditions();
        $cond->setSorting(array('created' => array('order' => 'desc', 'title' => 'по дате')));
        $cond->setPaging(100000, $per_page);
        $where = array('1');
        if (isset($opts['where'])) {
            foreach ($opts['where'] as $w) {
                $where[] = $w;
            }
        }
        $order = $cond->getSortingField() . ' ' . $cond->getSortingOrderSQL();
        $limit = $cond->getLimit();
        $query = 'SELECT SQL_CALC_FOUND_ROWS P. * , GROUP_CONCAT( T.title ) AS tags, GROUP_CONCAT( PT.tag_id ) AS tags_indexes
FROM `publications` P
LEFT JOIN `publications_tags` PT ON PT.publication_id = P.id
LEFT JOIN `tags` T ON T.id = PT.tag_id
WHERE (' . implode(' AND ', $where) . ')
GROUP BY P.id
ORDER BY ' . $order . ' LIMIT ' . $limit . '';
        $publications = Database::sql2array($query, 'id');
        foreach ($publications as $publication) {
            $uids[$publication['user_id']] = $publication['user_id'];
        }
        $users = Users::getByIdsLoaded($uids);
        foreach ($publications as &$publication) {
            $publication['user'] = isset($users[$publication['user_id']]) ? $users[$publication['user_id']]->data : array();
        }
        $cond->setPaging(Database::sql2single('SELECT FOUND_ROWS()'), $per_page);
        $data['publications'] = $publications;
        $data['conditions'] = $cond->getConditions();
        if (!$show_sortings) {
            foreach ($data['conditions'] as $key => $group) {
                if ($group['mode'] == 'sorting') {
                    unset($data['conditions'][$key]);
                }
            }
        }
        return $data;
    }
Beispiel #18
0
 function getEvent()
 {
     if (!$this->post_id) {
         throw new Exception('illegal event id');
     }
     $query = 'SELECT `mongoid` FROM `events` WHERE `id`=' . (int) $this->post_id;
     $integer_id = Database::sql2single($query);
     if (!(int) $integer_id) {
         return;
     }
     if ($this->user_id) {
         $wall = MongoDatabase::getUserWallItem($integer_id, $this->user_id);
         $events = MongoDatabase::getWallEvents($wall);
     } else {
         $events = MongoDatabase::getWallEvents(array(array('id' => $integer_id)));
     }
     Request::pass('post-subject', isset($events[0]['subject']) ? $events[0]['subject'] : 'запись');
     $this->_list($events, $item = true);
 }
Beispiel #19
0
 function addEventComment()
 {
     $parent_id = isset($_POST['parent_id']) ? (int) $_POST['parent_id'] : 0;
     $event_id = (int) $_POST['object_id'];
     $object_type = Config::COMMENT_OBJECT_ALBUM_EVENT;
     $user_id = CurrentUser::$id;
     $text = htmlspecialchars($_POST['text']);
     if ($user_id && $event_id && trim($text)) {
         $album_id = (int) Database::sql2single('SELECT album_id FROM album_events WHERE `id`=' . $event_id);
         if (!$parent_id) {
             Database::query('INSERT INTO `comments` SET
             `parent_id`=' . $parent_id . ',
             `object_type`=' . $object_type . ',
             `object_id`=' . $event_id . ',
             `user_id`=' . $user_id . ',
             `time`=' . time() . ',
             `text`=' . Database::escape($text));
             header('Location: /album/' . $album_id . '/event/' . $event_id . '#comment-' . Database::lastInsertId());
         } else {
             // parent
             $thread = Database::sql2single('SELECT `thread` FROM `comments` WHERE `id`=' . $parent_id);
             $thread = $thread ? $thread : $parent_id;
             Database::query('INSERT INTO `comments` SET
             `parent_id`=' . $parent_id . ',
             `object_type`=' . $object_type . ',
             `object_id`=' . $event_id . ',
             `user_id`=' . $user_id . ',
             `thread`=' . $thread . ',
             `time`=' . time() . ',
             `text`=' . Database::escape($text));
             header('Location: /album/' . $album_id . '/event/' . $event_id . '#comment-' . Database::lastInsertId());
         }
         Database::query('UPDATE `album_events` SET `comments_count` =
                 (SELECT COUNT(1) FROM `comments` WHERE `object_type`=' . Config::COMMENT_OBJECT_ALBUM_EVENT . ' AND `object_id`=' . $event_id . ') WHERE `id`=' . $event_id);
         $owner_id = (int) Database::sql2single('SELECT `creator_id` FROM album_events WHERE `id`=' . $event_id);
         if ($owner_id !== CurrentUser::$id) {
             Badges::progressAction($user_id, Badges::ACTION_TYPE_COMMENT);
             Badges::progressAction($owner_id, Badges::ACTION_TYPE_COMMENTED);
         }
     }
 }
Beispiel #20
0
 function getSynthesis($mode = 'synthesis')
 {
     $min_update_time = time() - 48 * 60 * 60;
     $query = 'SELECT COUNT(1) FROM `posts` WHERE `update_time`>' . $min_update_time;
     $count = min(200, Database::sql2single($query));
     $cond = new Conditions();
     $per_page = 20;
     $cond->setPaging($count, $per_page, 'p');
     $this->data['conditions'] = $cond->getConditions();
     $limit = $cond->getLimit();
     switch ($mode) {
         case 'synthesis':
             $order = 'rating';
             break;
         case 'links':
             $order = 'rating_links';
             break;
         case 'visits':
             $order = 'rating_visits';
             break;
         case 'comments':
             $order = 'rating_comments';
             break;
     }
     $posts = Database::sql2array('SELECT * FROM `posts` ORDER BY ' . $order . ' DESC LIMIT ' . $limit);
     $i = 0;
     foreach ($posts as $data) {
         $post = new Post($data);
         $this->data['posts'][$i] = $post->getShort();
         $this->data['posts'][$i]['num'] = ($cond->currentPage - 1) * $per_page + $i + 1;
         $aids[$post->data['id_author']] = $post->data['id_author'];
         $i++;
     }
     if (count($aids)) {
         $authors = Database::sql2array('SELECT * FROM `authors` WHERE `id` IN(' . implode(',', $aids) . ')');
         foreach ($authors as $data) {
             $author = new Author($data);
             $this->data['authors'][] = $author->getShort();
         }
     }
 }
Beispiel #21
0
 function getSettings()
 {
     $cond = new Conditions();
     $per_page = 0;
     if (isset($this->params['per_page'])) {
         $per_page = (int) $this->params['per_page'];
     }
     $per_page = $per_page > 0 ? $per_page : 1;
     $pagingName = isset($this->params['paging_parameter_name']) ? $this->params['paging_parameter_name'] : 'p';
     $where = '';
     $order = 'ORDER BY `id` DESC ';
     $group_by = '';
     $query = 'SELECT COUNT(1) FROM `settings` ' . $where . ' ' . $group_by . '';
     $count = Database::sql2single($query);
     $cond->setPaging($count, $per_page, $pagingName);
     $limit = $cond->getLimit();
     $limit = ' LIMIT ' . $limit;
     $query = 'SELECT * FROM `settings`' . $where . ' ' . $group_by . ' ' . $order . ' ' . $limit;
     $data = Database::sql2array($query);
     $this->data['settings'] = $data;
     $this->data['settings']['title'] = 'Настройки';
     $this->data['settings']['count'] = $count;
     $this->data['conditions'] = $cond->getConditions();
 }
Beispiel #22
0
 function getUniqueNickname($nickname, $email)
 {
     if (!$nickname) {
         $nickname = str_replace('.', '_', str_replace('@', '-', array_shift(explode('@', $email))));
     }
     $query = 'SELECT COUNT(1) FROM `user` WHERE `nickname`=' . Database::escape($nickname) . '';
     if (!Database::sql2single($query)) {
         return $nickname;
     } else {
         return $nickname . substr(time(), 5, 5) . rand(10, 20);
     }
 }
Beispiel #23
0
 public static function buildPageTitlePart($var)
 {
     $x = explode(':', $var[1]);
     $name = false;
     if (count($x) == 3) {
         list($name, $paramtype, $paramvalue) = $x;
     }
     if (count($x) == 2) {
         list($name, $paramvalue) = $x;
         $paramtype = 'raw_get';
     }
     if ($name) {
         $val = self::parseParams($paramtype, $paramvalue);
         switch ($name) {
             case 'profile-nickname':
                 $user = Users::getByIdsLoaded(array((int) $val));
                 $user = isset($user[$val]) ? $user[$val] : false;
                 /* @var $user User */
                 if ($user) {
                     return $user->getNickName();
                 }
                 break;
             case 'book-title':
                 $book = Books::getInstance()->getByIdLoaded((int) $val);
                 /* @var $book Book */
                 return $book->getTitle(1);
                 break;
             case 'person-title':
                 $person = Persons::getInstance()->getById((int) $val);
                 /* @var $person Person */
                 return $person->getName();
                 break;
             case 'genre-title':
                 return Request::pass('genre-title');
                 break;
             case 'forum-title':
                 $t = Request::pass('forum-title');
                 if (!$t) {
                     $t = Database::sql2single('SELECT name FROM `term_data` WHERE `tid`=' . (int) $val);
                 }
                 return $t;
                 break;
             case 'post-subject':
                 return Request::pass('post-subject');
                 break;
             case 'theme-title':
                 return Request::pass('theme-title');
                 break;
             case 'serie-title':
                 $t = Request::pass('serie-title');
                 if (!$t) {
                     $t = Database::sql2single('SELECT `title` FROM `series` WHERE `id`=' . (int) $val);
                 }
                 return $t;
                 break;
             case 'shelf-name':
                 if ($val == 'loved') {
                     return 'Любимые книги';
                 }
                 if (isset(Config::$shelfIdByNames[$val])) {
                     return isset(Config::$shelves[Config::$shelfIdByNames[$val]]) ? Config::$shelves[Config::$shelfIdByNames[$val]] : $val;
                 }
                 break;
             case 'magazine-title':
                 $query = 'SELECT `title` FROM `magazines` WHERE `id`=' . (int) $val;
                 return Database::sql2single($query);
                 break;
             case 'thread-subject':
                 $query = 'SELECT `subject` FROM `users_messages` WHERE `id`=' . (int) $val;
                 return Database::sql2single($query);
                 break;
             case 'get':
                 return $val;
                 break;
             default:
                 throw new Exception('Cant process title part "' . $var[1] . '"');
                 break;
         }
     }
 }
Beispiel #24
0
    $params['locale'] = 'ru';
    $params['v'] = '2';
    $pp = array();
    foreach ($params as $f => $v) {
        $pp[] = $f . '=' . $v;
    }
    $url .= implode('&', $pp);
    echo $url . "\n";
    $res = json_decode(file_get_contents($url), 1);
    return $res;
}
$lastId = isset($_GET['lastId']) ? $_GET['lastId'] : 0;
$query = 'SELECT CONCAT(lat,\',\',lon) FROM metro_stations WHERE lat>0 AND id> ' . $lastId . ' AND enabled=1 ORDER BY id LIMIT 1';
$latlon = Database::sql2single($query);
$query = 'SELECT title FROM metro_stations WHERE lat=0 AND id> ' . $lastId . ' AND enabled=1  ORDER BY id LIMIT 1';
$title = Database::sql2single($query);
$params['ll'] = $latlon;
$q = ' ' . $title;
echo $q . "\n";
$params['query'] = urlencode($q);
$params['limit'] = 300;
$params['intent'] = 'browse';
$params['radius'] = 90000;
$params['categoryId'] = '4bf58dd8d48988d1fd931735';
$places = fs_query('venues/search', $params);
$i = 1;
foreach ($places['response']['groups'][0]['items'] as $station) {
    $realName = str_replace('Метро', '', $station['name']);
    $realName = str_replace('метро', '', $realName);
    $realName = str_replace('станция', '', $realName);
    $realName = str_replace('Станция', '', $realName);
Beispiel #25
0
 function generateProfile()
 {
     global $current_user;
     /* @var $current_user CurrentUser */
     /* @var $user User */
     $user = $current_user->id === $this->id ? $current_user : Users::getById($this->id);
     $this->data['profile'] = $user->getXMLInfo();
     $this->data['profile']['role'] = $user->getRole();
     $this->data['profile']['lang'] = $user->getLanguage();
     $this->data['profile']['city_id'] = $user->getProperty('city_id');
     $this->data['profile']['city'] = Database::sql2single('SELECT `name` FROM `lib_city` WHERE `id`=' . $user->getProperty('city_id'));
     $this->data['profile']['picture'] = $user->getProperty('picture') ? $user->id . '.jpg' : 'default.jpg';
     $this->data['profile']['rolename'] = $user->getRoleName();
     $this->data['profile']['bday'] = $user->getBday(date('d-m-Y'), 'd-m-Y');
     $this->data['profile']['bdays'] = $user->getBday('неизвестно', 'd.m.Y');
     // additional
     $this->data['profile']['link_fb'] = $user->getPropertySerialized('link_fb');
     $this->data['profile']['link_vk'] = $user->getPropertySerialized('link_vk');
     $this->data['profile']['link_tw'] = $user->getPropertySerialized('link_tw');
     $this->data['profile']['link_lj'] = $user->getPropertySerialized('link_lj');
 }
 function getUserContribution()
 {
     global $current_user;
     $uid = $this->params['user_id'];
     $user = new User($uid);
     $user->load();
     $count = Database::sql2single('SELECT COUNT(1) FROM  `users_points_history` WHERE `id_user`=' . $user->id);
     //по книгам, по дате, по типам действий
     $sortings = array('time' => array('title' => 'по дате'), 'id_target' => array('title' => 'по книге'), 'id_action' => array('title' => 'по типу действий'));
     $dsortings = array('time' => array('title' => 'по дате', 'order' => 'desc'));
     $cond = new Conditions();
     $cond->setPaging($count, isset($this->params['per_page']) ? (int) $this->params['per_page'] : 40);
     $cond->setSorting($sortings, $dsortings);
     $order = 'ORDER BY ' . $cond->getSortingField() . ' ' . $cond->getSortingOrderSQL();
     $limit = $cond->getLimit();
     $this->data['conditions'] = $cond->getConditions();
     $query = 'SELECT * FROM `users_points_history` WHERE `id_user`=' . $user->id . ' ' . $order . ' LIMIT ' . $limit;
     $contributions = Database::sql2array($query);
     $bids = array();
     $aids = array();
     $sids = array();
     $mids = array();
     $gids = array();
     $uids = array($user->id);
     $tmp = array();
     foreach (Config::$points as $name => $p) {
         $tmp[$p['id']] = $name;
     }
     foreach ($contributions as &$contribution) {
         switch ($contribution['target_type']) {
             case BiberLog::TargetType_book:
                 $contribution['id_book'] = $contribution['id_target'];
                 $bids[$contribution['id_target']] = $contribution['id_target'];
                 break;
             case BiberLog::TargetType_person:
                 $contribution['id_author'] = $contribution['id_target'];
                 $aids[$contribution['id_target']] = $contribution['id_target'];
                 break;
             case BiberLog::TargetType_magazine:
                 $contribution['id_magazine'] = $contribution['id_target'];
                 $mids[$contribution['id_target']] = $contribution['id_target'];
                 break;
             case BiberLog::TargetType_serie:
                 $contribution['id_serie'] = $contribution['id_target'];
                 $sids[$contribution['id_target']] = $contribution['id_target'];
                 break;
             case BiberLog::TargetType_genre:
                 $contribution['id_genre'] = $contribution['id_target'];
                 $gids[$contribution['id_target']] = $contribution['id_target'];
                 break;
             default:
                 throw new Exception('cant process type #' . $contribution['target_type'] . ' for contribution');
                 break;
         }
         $contribution['action'] = $tmp[$contribution['id_action']];
         unset($contribution['id_action']);
         unset($contribution['id_target']);
         unset($contribution['target_type']);
         if (!$current_user->can('logs_view')) {
             unset($contribution['points']);
         }
         $contribution['date'] = date('Y/m/d H:i:s', $contribution['time']);
         unset($contribution['time']);
     }
     $this->data['contributions'] = $contributions;
     $aaids = array();
     if (count($bids)) {
         list($this->data['books'], $aaids) = $this->getContributionBooks($bids);
     }
     if (count($aaids)) {
         foreach ($aaids as $aid) {
             $aids[$aid] = $aid;
         }
     }
     if (count($aids)) {
         $this->data['authors'] = $this->getContributionAuthors($aids);
     }
     if (count($mids)) {
         $this->data['magazines'] = $this->getContributionMagazines($mids);
     }
     if (count($sids)) {
         $this->data['series'] = $this->getContributionSeries($sids);
     }
     if (count($gids)) {
         $this->data['genres'] = $this->getContributionGenres($gids);
     }
     if (count($uids)) {
         $this->data['users'] = $this->getContributionUsers($uids);
     }
 }
Beispiel #27
0
 function getMagazineId()
 {
     $query = 'SELECT `id_magazine` FROM `book_magazines` WHERE `id_book`=' . $this->id;
     return (int) Database::sql2single($query);
 }
Beispiel #28
0
 function checkLoved()
 {
     global $current_user;
     /* @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 = 'SELECT COUNT(1) as cnt FROM `users_loved` WHERE `id_target`=' . $item_id . ' AND `target_type`=' . Config::$loved_types[$item_type] . ' AND `id_user`=' . $current_user->id;
     if (Database::sql2single($query, false)) {
         $this->data['success'] = 1;
         $this->data['in_loved'] = 1;
         return;
     } else {
         $this->data['success'] = 1;
         $this->data['in_loved'] = 0;
     }
 }
Beispiel #29
0
 function getLog()
 {
     if ($this->target_type == 'user') {
         $query = 'SELECT COUNT(DISTINCT(id_log)) FROM `biber_log_index` WHERE `id_user`=' . $this->id_target . ' ';
     } else {
         if ($this->target_type == 'all') {
             $query = 'SELECT COUNT(DISTINCT(id_log)) FROM `biber_log_index` WHERE `is_copy`=0';
         } else {
             $query = 'SELECT COUNT(1) FROM `biber_log_index` WHERE `target_type`=' . $this->target_type . ' AND `id_target`=' . $this->id_target . ' ';
         }
     }
     $count = min(1000, Database::sql2single($query));
     $cond = new Conditions();
     $cond->setPaging($count, 10);
     $this->data['conditions'] = $cond->getConditions();
     $limit = $cond->getLimit();
     if ($this->target_type == 'user') {
         $query = 'SELECT * FROM `biber_log_index` WHERE
         `id_user`=' . $this->id_target . ' GROUP BY id_log
         ORDER BY `time` DESC LIMIT ' . $limit;
     } else {
         if ($this->target_type == 'all') {
             $query = 'SELECT * FROM `biber_log_index` WHERE `is_copy`=0 GROUP BY id_log
         ORDER BY `time` DESC LIMIT ' . $limit;
         } else {
             $query = 'SELECT * FROM `biber_log_index` WHERE
         `target_type`=' . $this->target_type . ' AND
         `id_target`=' . $this->id_target . '
         ORDER BY `time` DESC LIMIT ' . $limit;
         }
     }
     $book_ids = array();
     $person_ids = array();
     $serie_ids = array();
     $magazine_ids = array();
     $uids = array();
     if ($this->target_type == BiberLog::TargetType_book) {
         $book_ids[$this->id_target] = $this->id_target;
     }
     if ($this->target_type == BiberLog::TargetType_person) {
         $person_ids[$this->id_target] = $this->id_target;
     }
     if ($this->target_type == 'user') {
         $uids[$this->id_target] = $this->id_target;
     }
     $arr = array();
     $arri = Database::sql2array($query, 'id_log');
     $to_fetch_log = array();
     foreach ($arri as $row) {
         $to_fetch_log[(int) $row['id_log']] = (int) $row['id_log'];
     }
     if (count($to_fetch_log)) {
         $query = 'SELECT * FROM `biber_log` WHERE `id` IN (' . implode(',', $to_fetch_log) . ') ORDER BY `time` DESC';
         $arr = Database::sql2array($query);
         foreach ($arr as &$rowx) {
             foreach ($arri[$rowx['id']] as $f => $v) {
                 $rowx[$f] = $v;
             }
         }
     }
     foreach ($arr as &$row) {
         $book_id_s = 0;
         $uids[$row['id_user']] = $row['id_user'];
         $vals = unserialize($row['data']);
         if (isset($vals['id1'])) {
             $book_ids[$vals['id1'][0]] = $vals['id1'][0];
             $book_ids[$vals['id1'][1]] = $vals['id1'][1];
         }
         if (isset($vals['id2'])) {
             $book_ids[$vals['id2'][0]] = $vals['id2'][0];
             $book_ids[$vals['id2'][1]] = $vals['id2'][1];
         }
         if (isset($vals['id_person'])) {
             if (isset($vals['id_person'][0])) {
                 $person_ids[$vals['id_person'][0]] = (int) $vals['id_person'][0];
             }
             if (isset($vals['id_person'][1])) {
                 $person_ids[$vals['id_person'][1]] = (int) $vals['id_person'][1];
             }
         }
         if (isset($vals['is_duplicate'])) {
             $book_ids[$vals['is_duplicate'][0]] = $vals['is_duplicate'][0];
             $book_ids[$vals['is_duplicate'][1]] = $vals['is_duplicate'][1];
         }
         $book_id = 0;
         $person_id = 0;
         $serie_id = 0;
         $values = array();
         foreach ($vals as $field => $v) {
             if (!is_array($v)) {
                 if ($field == 'id_book') {
                     $book_id = $v;
                     $book_ids[$v] = $v;
                 }
                 if ($field == 'id_person') {
                     $person_id = $v;
                     $person_ids[$v] = $v;
                 }
                 if ($field == 'id_serie') {
                     $serie_id = $v;
                     $serie_ids[$v] = $v;
                 }
                 if ($field == 'id_magazine') {
                     $serie_id = $v;
                     $magazine_ids[$v] = $v;
                 }
                 continue;
             }
             $tmp = array();
             if ($row['target_type'] == BiberLog::TargetType_book) {
                 if ($field == 'new_relations') {
                     foreach ($v[1] as $new_relation_id) {
                         $book_ids[$new_relation_id] = $new_relation_id;
                         $tmp[] = array('book_id' => $new_relation_id);
                     }
                     $values['new_relations'] = $tmp;
                 } else {
                     if ($field == 'old_relations') {
                         foreach ($v[1] as $new_relation_id) {
                             $book_ids[$new_relation_id] = $new_relation_id;
                             $tmp[] = array('book_id' => $new_relation_id);
                         }
                         $values['old_relations'] = $tmp;
                     } else {
                         if ($field == 'deleted_relations') {
                             foreach ($v[1] as $new_relation_id) {
                                 $book_ids[$new_relation_id] = $new_relation_id;
                                 $tmp[] = array('book_id' => $new_relation_id);
                             }
                             $values['deleted_relations'] = $tmp;
                         } else {
                             $values[] = array('name' => $field, 'old' => $v[0], 'new' => $v[1]);
                         }
                     }
                 }
             } else {
                 if ($row['target_type'] == BiberLog::TargetType_person) {
                     if ($field == 'new_relations') {
                         foreach ($v[1] as $new_relation_id) {
                             $person_ids[$new_relation_id] = (int) $new_relation_id;
                             $tmp[] = array('author_id' => $new_relation_id);
                         }
                         $values['new_relations'] = $tmp;
                     } else {
                         if ($field == 'old_relations') {
                             foreach ($v[1] as $new_relation_id) {
                                 $person_ids[$new_relation_id] = (int) $new_relation_id;
                                 $tmp[] = array('author_id' => $new_relation_id);
                             }
                             $values['old_relations'] = $tmp;
                         } else {
                             if ($field == 'deleted_relations') {
                                 foreach ($v[1] as $new_relation_id) {
                                     $person_ids[$new_relation_id] = (int) $new_relation_id;
                                     $tmp[] = array('author_id' => $new_relation_id);
                                 }
                                 $values['deleted_relations'] = $tmp;
                             } else {
                                 $values[] = array('name' => $field, 'old' => $v[0], 'new' => $v[1]);
                             }
                         }
                     }
                 } else {
                     if ($row['target_type'] == BiberLog::TargetType_magazine) {
                         $values[] = array('name' => $field, 'old' => $v[0], 'new' => $v[1]);
                     } else {
                         if ($row['target_type'] == BiberLog::TargetType_serie) {
                             if ($field == 'id_book') {
                                 $book_id_s = $v[0] ? $v[0] : $v[1];
                                 if ($book_id_s) {
                                     $book_ids[$book_id_s] = $book_id_s;
                                 }
                                 continue;
                             }
                             $values[] = array('name' => $field, 'old' => $v[0], 'new' => $v[1]);
                         }
                     }
                 }
             }
         }
         if (in_array($row['target_type'], array(BiberLog::TargetType_book))) {
             $book_ids[$row['id_target']] = $row['id_target'];
             $book_id = $row['id_target'];
         }
         if (in_array($row['target_type'], array(BiberLog::TargetType_person))) {
             $person_ids[(int) $row['id_target']] = (int) $row['id_target'];
             $person_id = $row['id_target'];
         }
         if (in_array($row['target_type'], array(BiberLog::TargetType_serie))) {
             $serie_id = $row['id_target'];
             $serie_ids[$row['id_target']] = $row['id_target'];
         }
         if (in_array($row['target_type'], array(BiberLog::TargetType_magazine))) {
             $magazine_id = $row['id_target'];
             $magazine_ids[$row['id_target']] = $row['id_target'];
         }
         $this->data['logs'][] = array('id' => $row['id'], 'book_id' => max($book_id, $book_id_s), 'author_id' => $person_id, 'serie_id' => $serie_id, 'time' => date('Y/m/d H:i:s', $row['time']), 'action' => BiberLog::$actionTypes[$row['action_type']], 'id_user' => $row['id_user'], 'values' => $values, 'applied' => $row['undo'] ? 0 : 1);
     }
     $users = Users::getByIdsLoaded($uids);
     foreach ($users as $user) {
         $this->data['users'][$user->id] = $user->getListData();
     }
     if (count($serie_ids)) {
         $query = 'SELECT id,name,title FROM `series` WHERE `id` IN(' . implode(',', $serie_ids) . ')';
         $out = Database::sql2array($query);
         foreach ($out as &$r) {
             $r['path'] = Config::need('www_path') . '/s/' . $r['id'];
         }
         $this->data['series'] = $out;
     }
     if (count($book_ids)) {
         $this->data['books'] = $this->getLogBooks($book_ids);
     }
     if (count($person_ids)) {
         $this->data['authors'] = $this->getLogPersons($person_ids);
     }
     foreach (Config::$langRus as $code => $title) {
         $this->data['lang_codes'][] = array('id' => Config::$langs[$code], 'code' => $code, 'title' => $title);
     }
 }
Beispiel #30
0
 function del_author()
 {
     global $current_user;
     $this->ca();
     $id_person = (int) $_POST['item_id'];
     $id_book = (int) $_POST['id'];
     $query = 'SELECT `person_role` FROM `book_persons` WHERE `id_book`=' . $id_book . ' AND `id_person`=' . $id_person;
     $old_role = Database::sql2single($query);
     if ($old_role) {
         if ($id_person && $id_book) {
             $query = 'DELETE FROM `book_persons` WHERE `id_book`=' . $id_book . ' AND `id_person`=' . $id_person;
             Database::query($query);
             $this->data['success'] = 1;
             $this->data['item_id'] = $id_person;
             BookLog::addLog(array('id_person' => 0, 'person_role' => 0), array('id_person' => $id_person, 'person_role' => $old_role), $id_book);
             BookLog::saveLog($id_book, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEditPerson);
             $search = Search::getInstance();
             /* @var $search Search */
             $search->updateBook(new Book($id_book));
             return;
         }
     } else {
         $this->data['error'] = 'Нет такого автора';
     }
     $this->data['item_id'] = $id_person;
     $this->data['success'] = 0;
 }