/** * Listing the active users for ajax. * @return string|\yii\web\Response */ public function actionFieldlist($q = null) { if (Yii::$app->request->isAjax) { if (!is_null($q) && is_string($q)) { $cache = Cache::getInstance()->get('members.fieldlist'); if ($cache === false || empty($cache[$q])) { if ($cache === false) { $cache = []; } $users = User::find()->where(['and', ['status' => User::STATUS_ACTIVE], ['or', ['like', 'username', $q], ['username' => null]]])->orderBy('username, id'); $results = ['results' => []]; foreach ($users->each() as $user) { $results['results'][] = ['id' => $user->id, 'text' => $user->getPodiumTag(true)]; } if (!empty($results['results'])) { $cache[$q] = Json::encode($results); Cache::getInstance()->set('members.fieldlist', $cache); } else { return Json::encode(['results' => []]); } } return $cache[$q]; } else { return Json::encode(['results' => []]); } } else { return $this->redirect(['default/index']); } }
/** * Listing the active users for ajax. * Entering 'forum#XX' (or any last part of 'forum' with #XX) looks for * member of the XX ID without username. * Entering integer looks for member with XX in username or empty username * and that ID. * @param string $q Username query * @return string|\yii\web\Response */ public function actionFieldlist($q = null) { if (Yii::$app->request->isAjax) { if (!is_null($q) && is_string($q)) { $cache = Cache::getInstance()->get('members.fieldlist'); if ($cache === false || empty($cache[$q])) { if ($cache === false) { $cache = []; } $users = User::find()->andWhere(['status' => User::STATUS_ACTIVE]); $users->andWhere(['!=', 'id', User::loggedId()]); if (preg_match('/^(forum|orum|rum|um|m)?#([0-9]+)$/', strtolower($q), $matches)) { $users->andWhere(['username' => ['', null], 'id' => $matches[2]]); } elseif (preg_match('/^([0-9]+)$/', $q, $matches)) { $users->andWhere(['or', ['like', 'username', $q], ['username' => ['', null], 'id' => $matches[1]]]); } else { $users->andWhere(['like', 'username', $q]); } $users->orderBy(['username' => SORT_ASC, 'id' => SORT_ASC]); $results = ['results' => []]; foreach ($users->each() as $user) { $results['results'][] = ['id' => $user->id, 'text' => $user->getPodiumTag(true)]; } if (!empty($results['results'])) { $cache[$q] = Json::encode($results); Cache::getInstance()->set('members.fieldlist', $cache); } else { return Json::encode(['results' => []]); } } return $cache[$q]; } else { return Json::encode(['results' => []]); } } else { return $this->redirect(['default/index']); } }
/** * Counts number of created threads. * @return integer */ public static function totalThreads() { $threads = Cache::getInstance()->get('forum.threadscount'); if ($threads === false) { $threads = Thread::find()->count(); Cache::getInstance()->set('forum.threadscount', $threads); } return $threads; }
/** * Listing and updating moderation list for the forum of given ID. * @param integer $id forum ID * @return string|\yii\web\Response */ public function actionMods($id = null) { $mod = null; $moderators = User::find()->where(['role' => User::ROLE_MODERATOR])->indexBy('id')->all(); if (is_numeric($id) && $id > 0) { if (isset($moderators[$id])) { $mod = $moderators[$id]; } } else { reset($moderators); $mod = current($moderators); } $searchModel = new ForumSearch(); $dataProvider = $searchModel->searchForMods(Yii::$app->request->get()); $postData = Yii::$app->request->post(); if ($postData) { if (User::can(Rbac::PERM_PROMOTE_USER)) { $mod_id = !empty($postData['mod_id']) && is_numeric($postData['mod_id']) && $postData['mod_id'] > 0 ? $postData['mod_id'] : 0; $selection = !empty($postData['selection']) ? $postData['selection'] : []; $pre = !empty($postData['pre']) ? $postData['pre'] : []; if ($mod_id != $mod->id) { $this->error(Yii::t('podium/flash', 'Sorry! There was an error while selecting the moderator ID.')); } else { try { $add = []; foreach ($selection as $select) { if (!in_array($select, $pre)) { if ((new Query())->from(Forum::tableName())->where(['id' => $select])->exists() && (new Query())->from(Mod::tableName())->where(['forum_id' => $select, 'user_id' => $mod->id])->exists() === false) { $add[] = [$select, $mod->id]; } } } $remove = []; foreach ($pre as $p) { if (!in_array($p, $selection)) { if ((new Query())->from(Mod::tableName())->where(['forum_id' => $p, 'user_id' => $mod->id])->exists()) { $remove[] = $p; } } } if (!empty($add)) { Yii::$app->db->createCommand()->batchInsert(Mod::tableName(), ['forum_id', 'user_id'], $add)->execute(); } if (!empty($remove)) { Yii::$app->db->createCommand()->delete(Mod::tableName(), ['forum_id' => $remove, 'user_id' => $mod->id])->execute(); } Cache::getInstance()->delete('forum.moderators'); Log::info('Moderators updated', null, __METHOD__); $this->success(Yii::t('podium/flash', 'Moderation list has been saved.')); } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was an error while saving the moderatoration list.')); } return $this->refresh(); } } else { $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.')); } } return $this->render('mods', ['moderators' => $moderators, 'mod' => $mod, 'searchModel' => $searchModel, 'dataProvider' => $dataProvider]); }
/** * Marks post as unseen. * @return boolean */ public function unseen() { $this->post_seen = self::POST_NEW; if ($this->save()) { Cache::getInstance()->deleteElement('user.subscriptions', User::loggedId()); return true; } return false; }
/** * Marks message read. * @since 0.2 */ public function markRead() { if ($this->sender_status == self::STATUS_NEW) { $this->sender_status = self::STATUS_READ; if ($this->save()) { Cache::getInstance()->deleteElement('user.newmessages', $this->sender_id); } } }
/** * Returns latest post. * @return type */ public static function getLatest($posts = 5) { $latest = []; if (Yii::$app->user->isGuest) { $latest = Cache::getInstance()->getElement('forum.latestposts', 'guest'); if ($latest === false) { $posts = static::getLatestPostsForGuests($posts); foreach ($posts as $post) { $latest[] = ['id' => $post->id, 'title' => $post->thread->name, 'created' => $post->created_at, 'author' => $post->author->podiumTag]; } Cache::getInstance()->setElement('forum.latestposts', 'guest', $latest); } } else { $latest = Cache::getInstance()->getElement('forum.latestposts', 'member'); if ($latest === false) { $posts = static::getLatestPostsForMembers($posts); foreach ($posts as $post) { $latest[] = ['id' => $post->id, 'title' => $post->thread->name, 'created' => $post->created_at, 'author' => $post->author->podiumTag]; } Cache::getInstance()->setElement('forum.latestposts', 'member', $latest); } } return $latest; }
/** * Deleting the subscription of given ID. * @param integer $id * @return \yii\web\Response */ public function actionDelete($id = null) { $model = Subscription::findOne(['id' => (int) $id, 'user_id' => Yii::$app->user->id]); if (empty($model)) { $this->error('Sorry! We can not find Subscription with this ID.'); } else { if ($model->delete()) { Cache::getInstance()->deleteElement('user.subscriptions', Yii::$app->user->id); $this->success('Thread has been unsubscribed.'); } else { Log::error('Error while deleting subscription', !empty($model->id) ? $model->id : '', __METHOD__); $this->error('Sorry! There was some error while deleting the subscription.'); } } return $this->redirect(['profile/subscriptions']); }
/** * Performs vote processing. * @param boolean $up whether this is up or downvote * @param integer $count number of user's cached votes * @return boolean * @since 0.2 */ public function podiumThumb($up = true, $count = 0) { try { if ($this->thumb) { if ($this->thumb->thumb == 1 && !$up) { $this->thumb->thumb = -1; if ($this->thumb->save()) { $this->updateCounters(['likes' => -1, 'dislikes' => 1]); } } elseif ($this->thumb->thumb == -1 && $up) { $this->thumb->thumb = 1; if ($this->thumb->save()) { $this->updateCounters(['likes' => 1, 'dislikes' => -1]); } } } else { $postThumb = new PostThumb(); $postThumb->post_id = $this->id; $postThumb->user_id = User::loggedId(); $postThumb->thumb = $up ? 1 : -1; if ($postThumb->save()) { if ($postThumb->thumb) { $this->updateCounters(['likes' => 1]); } else { $this->updateCounters(['dislikes' => 1]); } } } if ($count == 0) { Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]); } else { Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1); } return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Removes message. * @return boolean */ public function remove() { $clearCache = false; if ($this->sender_status == self::STATUS_NEW) { $clearCache = true; } $transaction = static::getDb()->beginTransaction(); try { if (empty($this->messageReceivers)) { if ($this->delete()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', $this->sender_id); } $transaction->commit(); return true; } else { throw new Exception('Message removing error!'); } } else { $allDeleted = true; foreach ($this->messageReceivers as $mr) { if ($mr->receiver_status != MessageReceiver::STATUS_DELETED) { $allDeleted = false; break; } } if ($allDeleted) { foreach ($this->messageReceivers as $mr) { if (!$mr->delete()) { throw new Exception('Received message removing error!'); } } if ($this->delete()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', $this->sender_id); } $transaction->commit(); return true; } else { throw new Exception('Message removing error!'); } } else { $this->sender_status = self::STATUS_DELETED; if ($this->save()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', $this->sender_id); } $transaction->commit(); return true; } else { throw new Exception('Message status changing error!'); } } } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), $this->id, __METHOD__); } return false; }
/** * Viewing the message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionView($id = null) { $model = Message::find()->where(['and', ['id' => $id], ['or', 'receiver_id' => User::loggedId(), 'sender_id' => User::loggedId()]])->limit(1)->one(); if ($model) { if ($model->receiver_id == User::loggedId() && $model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } } return $this->render('view', ['model' => $model]); } else { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } }
/** * Removes message. * @return boolean */ public function remove() { $clearCache = false; if ($this->receiver_status == self::STATUS_NEW) { $clearCache = true; } $deleteParent = null; $transaction = static::getDb()->beginTransaction(); try { if ($this->message->sender_status != Message::STATUS_DELETED) { $this->receiver_status = self::STATUS_DELETED; if ($this->save()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', $this->receiver_id); } $transaction->commit(); return true; } else { throw new Exception('Message status changing error!'); } } else { if ($this->message->sender_status == Message::STATUS_DELETED && count($this->message->messageReceivers) == 1) { $deleteParent = $this->message; } if ($this->delete()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', $this->receiver_id); } if ($deleteParent) { if (!$deleteParent->delete()) { throw new Exception('Sender message deleting error!'); } } $transaction->commit(); return true; } else { throw new Exception('Message removing error!'); } } } catch (Exception $e) { $transaction->rollBack(); Log::error($e->getMessage(), $this->id, __METHOD__); } return false; }
/** * Clearing all cache. * @return string */ public function actionClear() { if (Cache::getInstance()->flush()) { $this->success(Yii::t('podium/flash', 'Cache has been cleared.')); } else { $this->error(Yii::t('podium/flash', 'Sorry! There was some error while clearing the cache.')); } return $this->redirect(['admin/settings']); }
/** * Viewing the message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionView($id = null) { $model = Message::findOne(['and', ['id' => $id], ['or', 'receiver_id' => Yii::$app->user->id, 'sender_id' => Yii::$app->user->id]]); if ($model) { if ($model->receiver_id == Yii::$app->user->id && $model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', Yii::$app->user->id); } } return $this->render('view', ['model' => $model]); } else { $this->error('Sorry! We can not find the message with the given ID.'); return $this->redirect(['inbox']); } }
/** * Removes message. * @param integer $perm Permanent removal flag * @return boolean */ public function remove($perm = 0) { $clearCache = false; if ($this->receiver_status == self::STATUS_NEW) { $clearCache = true; } if ($this->receiver_id == User::loggedId()) { $this->receiver_status = $perm ? self::STATUS_REMOVED : self::STATUS_DELETED; } if ($this->sender_id == User::loggedId()) { $this->sender_status = $perm ? self::STATUS_REMOVED : self::STATUS_DELETED; } if ($this->receiver_status == self::STATUS_REMOVED && $this->sender_status == self::STATUS_REMOVED) { if ($this->delete()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } return true; } else { return false; } } if ($this->save()) { if ($clearCache) { Cache::getInstance()->deleteElement('user.newmessages', User::loggedId()); } return true; } else { return false; } }
public function getMods() { $mods = Cache::getInstance()->getElement('forum.moderators', $this->id); if ($mods === false) { $mods = []; $modteam = User::find()->select(['id', 'role'])->where(['status' => User::STATUS_ACTIVE, 'role' => [User::ROLE_ADMIN, User::ROLE_MODERATOR]])->asArray()->all(); foreach ($modteam as $user) { if ($user['role'] == User::ROLE_ADMIN) { $mods[] = $user['id']; } else { if ((new Query())->from(Mod::tableName())->where(['forum_id' => $this->id, 'user_id' => $user->id])->exists()) { $mods[] = $user['id']; } } } Cache::getInstance()->setElement('forum.moderators', $this->id, $mods); } return $mods; }
/** * Updates database version number. * @param array $data * @return string result message. * @since 0.2 */ protected function _updateVersion($data) { try { if (empty($data['version'])) { throw new Exception(Yii::t('podium/flash', 'Version number missing.')); } Config::getInstance()->set('version', $data['version']); Cache::getInstance()->flush(); return $this->outputSuccess(Yii::t('podium/flash', 'Database version has been updated to {version}.', ['version' => $data['version']])); } catch (Exception $e) { Yii::error([$e->getName(), $e->getMessage()], __METHOD__); $this->setError(true); return $this->outputDanger(Yii::t('podium/flash', 'Error during version updating') . ': ' . Html::tag('pre', $e->getMessage())); } }
/** * Updates friend status for the user. * @return boolean * @since 0.2 */ public function updateFriend() { try { if ($this->isBefriendedBy(User::loggedId())) { Yii::$app->db->createCommand()->delete('{{%podium_user_friend}}', 'user_id = :uid AND friend_id = :iid', [':uid' => User::loggedId(), ':iid' => $this->id])->execute(); Log::info('User unfriended', $this->id, __METHOD__); } else { Yii::$app->db->createCommand()->insert('{{%podium_user_friend}}', ['user_id' => User::loggedId(), 'friend_id' => $this->id])->execute(); Log::info('User befriended', $this->id, __METHOD__); } Cache::getInstance()->deleteElement('user.friends', $this->id); return true; } catch (Exception $e) { Log::error($e->getMessage(), null, __METHOD__); } return false; }
/** * Viewing the received message of given ID. * @param integer $id * @return string|\yii\web\Response */ public function actionViewReceived($id = null) { $model = MessageReceiver::find()->where(['and', ['id' => $id, 'receiver_id' => User::loggedId()], ['!=', 'receiver_status', MessageReceiver::STATUS_DELETED]])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find the message with the given ID.')); return $this->redirect(['messages/inbox']); } if ($model->receiver_status == Message::STATUS_NEW) { $model->receiver_status = Message::STATUS_READ; if ($model->save()) { Cache::getInstance()->deleteElement('user.newmessages', $model->receiver_id); } } return $this->render('view', ['model' => $model->message, 'type' => 'received', 'id' => $model->id]); }
/** * Voting on the post. * @return string|\yii\web\Response */ public function actionThumb() { if (Yii::$app->request->isAjax) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Error while voting on this post!'), ['class' => 'text-danger'])]; if (!Yii::$app->user->isGuest) { $postId = Yii::$app->request->post('post'); $thumb = Yii::$app->request->post('thumb'); if (is_numeric($postId) && $postId > 0 && in_array($thumb, ['up', 'down'])) { $post = Post::findOne((int) $postId); if ($post) { if ($post->thread->locked) { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'This thread is locked.'), ['class' => 'text-info'])]; } else { if ($post->author_id == User::loggedId()) { return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'You can not vote on your own post!'), ['class' => 'text-info'])]); } $count = 0; $votes = Cache::getInstance()->get('user.votes.' . User::loggedId()); if ($votes !== false) { if ($votes['expire'] < time()) { $votes = false; } elseif ($votes['count'] >= 10) { return Json::encode(['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', '{max} votes per hour limit reached!', ['max' => 10]), ['class' => 'text-danger'])]); } else { $count = $votes['count']; } } if ($post->thumb) { if ($post->thumb->thumb == 1 && $thumb == 'down') { $post->thumb->thumb = -1; if ($post->thumb->save()) { $post->updateCounters(['likes' => -1, 'dislikes' => 1]); } } elseif ($post->thumb->thumb == -1 && $thumb == 'up') { $post->thumb->thumb = 1; if ($post->thumb->save()) { $post->updateCounters(['likes' => 1, 'dislikes' => -1]); } } } else { $postThumb = new PostThumb(); $postThumb->post_id = $post->id; $postThumb->user_id = User::loggedId(); $postThumb->thumb = $thumb == 'up' ? 1 : -1; if ($postThumb->save()) { if ($thumb == 'up') { $post->updateCounters(['likes' => 1]); } else { $post->updateCounters(['dislikes' => 1]); } } } $data = ['error' => 0, 'likes' => '+' . $post->likes, 'dislikes' => '-' . $post->dislikes, 'summ' => $post->likes - $post->dislikes, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-ok-circle']) . ' ' . Yii::t('podium/view', 'Your vote has been saved!'), ['class' => 'text-success'])]; if ($count == 0) { Cache::getInstance()->set('user.votes.' . User::loggedId(), ['count' => 1, 'expire' => time() + 3600]); } else { Cache::getInstance()->setElement('user.votes.' . User::loggedId(), 'count', $count + 1); } } } } } else { $data = ['error' => 1, 'msg' => Html::tag('span', Html::tag('span', '', ['class' => 'glyphicon glyphicon-warning-sign']) . ' ' . Yii::t('podium/view', 'Please sign in to vote on this post'), ['class' => 'text-info'])]; } return Json::encode($data); } else { return $this->redirect(['default/index']); } }
/** * Singleton construct. */ protected function __construct() { $this->cache = Cache::getInstance(); $this->_defaults = ['name' => self::PODIUM_NAME, 'version' => self::CURRENT_VERSION, 'hot_minimum' => self::HOT_MINIMUM, 'members_visible' => self::FLAG_MEMBERS_VISIBLE, 'from_email' => self::DEFAULT_FROM_EMAIL, 'from_name' => self::DEFAULT_FROM_NAME, 'maintenance_mode' => self::MAINTENANCE_MODE, 'max_attempts' => self::MAX_SEND_ATTEMPTS, 'use_captcha' => self::FLAG_USE_CAPTCHA, 'recaptcha_sitekey' => '', 'recaptcha_secretkey' => '', 'password_reset_token_expire' => self::SECONDS_PASSWORD_RESET_TOKEN_EXPIRE, 'email_token_expire' => self::SECONDS_EMAIL_TOKEN_EXPIRE, 'activation_token_expire' => self::SECONDS_ACTIVATION_TOKEN_EXPIRE, 'meta_keywords' => self::META_KEYWORDS, 'meta_description' => self::META_DESCRIPTION]; }
/** * Gets number of user subscribed threads with new posts. * @return integer */ public function getSubscriptionsCount() { $cache = Cache::getInstance()->getElement('user.subscriptions', $this->id); if ($cache === false) { $cache = (new Query())->from(Subscription::tableName())->where(['user_id' => $this->id, 'post_seen' => Subscription::POST_NEW])->count(); Cache::getInstance()->setElement('user.subscriptions', $this->id, $cache); } return $cache; }
/** * Registers user authorization. * @see \bizley\podium\components\Installation */ public function registerAuthorization() { Yii::$app->setComponents(['authManager' => ['class' => 'yii\\rbac\\DbManager', 'itemTable' => '{{%podium_auth_item}}', 'itemChildTable' => '{{%podium_auth_item_child}}', 'assignmentTable' => '{{%podium_auth_assignment}}', 'ruleTable' => '{{%podium_auth_rule}}', 'cache' => Cache::getInstance()->cache]]); }
/** * Deleting the subscription of given ID. * @param integer $id * @return \yii\web\Response */ public function actionDelete($id = null) { $model = Subscription::find()->where(['id' => (int) $id, 'user_id' => User::loggedId()])->limit(1)->one(); if (empty($model)) { $this->error(Yii::t('podium/flash', 'Sorry! We can not find Subscription with this ID.')); } else { if ($model->delete()) { Cache::getInstance()->deleteElement('user.subscriptions', User::loggedId()); $this->success(Yii::t('podium/flash', 'Thread has been unsubscribed.')); } else { Log::error('Error while deleting subscription', $model->id, __METHOD__); $this->error(Yii::t('podium/flash', 'Sorry! There was some error while deleting the subscription.')); } } return $this->redirect(['profile/subscriptions']); }
/** * Gets number of active threads added by user. * @return integer */ public function getThreadsCount($id = null) { $cache = Cache::getInstance()->getElement('user.threadscount', empty($id) ? $this->getId() : $id); if ($cache === false) { $cache = (new Query())->from(Thread::tableName())->where(['author_id' => empty($id) ? $this->getId() : $id])->count(); Cache::getInstance()->setElement('user.threadscount', empty($id) ? $this->getId() : $id, $cache); } return $cache; }