Exemplo n.º 1
1
 /**
  * Creates inherited user account.
  */
 public function init()
 {
     parent::init();
     if (!Yii::$app->user->isGuest) {
         if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) {
             $user = User::findMe();
             if (empty($user)) {
                 $new = new User();
                 $new->setScenario('installation');
                 $new->inherited_id = Yii::$app->user->id;
                 $new->status = User::STATUS_ACTIVE;
                 $new->role = User::ROLE_MEMBER;
                 $new->timezone = User::DEFAULT_TIMEZONE;
                 if ($new->save()) {
                     $this->success(Yii::t('podium/flash', 'Hey! Your new forum account has just been automatically created! Go to {link} to complement it.', ['link' => Html::a(Yii::t('podium/view', 'Profile'))]));
                     Cache::clearAfterActivate();
                     Log::info('Inherited account created', $new->id, __METHOD__);
                 } else {
                     throw new Exception(Yii::t('podium/view', 'There was an error while creating inherited user account. Podium can not run with the current configuration. Please contact administrator about this problem.'));
                 }
             } elseif ($user->status == User::STATUS_BANNED) {
                 return $this->redirect(['default/ban']);
             }
         } else {
             $user = Yii::$app->user->identity;
         }
         if ($user && !empty($user->timezone)) {
             Yii::$app->formatter->timeZone = $user->timezone;
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Creates inherited user account.
  */
 public function init()
 {
     parent::init();
     if (!Yii::$app->user->isGuest) {
         if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) {
             $user = User::findMe();
             if (empty($user)) {
                 $new = new User();
                 $new->setScenario('installation');
                 $new->inherited_id = Yii::$app->user->id;
                 $new->status = User::STATUS_ACTIVE;
                 $new->role = User::ROLE_MEMBER;
                 $new->timezone = User::DEFAULT_TIMEZONE;
                 if ($new->save()) {
                     $this->success(Yii::t('podium/flash', Messages::ACCOUNT_INHERITED, ['link' => Html::a(Yii::t('podium/layout', 'Profile'))]));
                     Cache::clearAfterActivate();
                     Log::info('Inherited account created', $new->id, __METHOD__);
                 } else {
                     throw new Exception(Yii::t('podium/view', Messages::ACCOUNT_INHERITED_ERROR));
                 }
             } elseif ($user->status == User::STATUS_BANNED) {
                 return $this->redirect(['default/ban']);
             }
         } else {
             $user = Yii::$app->user->identity;
         }
         if ($user && !empty($user->timezone)) {
             Yii::$app->formatter->timeZone = $user->timezone;
         }
     }
 }
Exemplo n.º 3
0
 /**
  * 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']);
     }
 }
Exemplo n.º 4
0
 /**
  * Activating the account based on the provided activation token.
  * @param string $token
  * @return \yii\web\Response
  */
 public function actionActivate($token)
 {
     $model = User::findByActivationToken($token);
     if ($model) {
         $model->setScenario('token');
         if ($model->activate()) {
             Cache::clearAfterActivate();
             Log::info('Account activated', !empty($model->id) ? $model->id : '', __METHOD__);
             $this->success('Your account has been activated. You can sign in now.');
         } else {
             Log::error('Error while activating account', !empty($model->id) ? $model->id : '', __METHOD__);
             $this->error('Sorry! There was some error while activating your account. Contact administrator about this problem.');
         }
         return $this->module->goPodium();
     } else {
         $this->error('The provided activation token is invalid or expired.');
         return $this->module->goPodium();
     }
 }
Exemplo n.º 5
0
 /**
  * Activating the account based on the provided activation token.
  * @param string $token
  * @return \yii\web\Response
  */
 public function actionActivate($token)
 {
     if (PodiumModule::getInstance()->userComponent == PodiumModule::USER_INHERIT) {
         $this->info(Yii::t('podium/flash', 'Please contact the administrator to activate your account.'));
         return $this->module->goPodium();
     }
     $model = User::findByActivationToken($token);
     if (empty($model)) {
         $this->error(Yii::t('podium/flash', 'The provided activation token is invalid or expired.'));
         return $this->module->goPodium();
     }
     $model->scenario = 'token';
     if ($model->activate()) {
         Cache::clearAfter('activate');
         Log::info('Account activated', $model->id, __METHOD__);
         $this->success(Yii::t('podium/flash', 'Your account has been activated. You can sign in now.'));
     } else {
         Log::error('Error while activating account', $model->id, __METHOD__);
         $this->error(Yii::t('podium/flash', 'Sorry! There was some error while activating your account. Contact administrator about this problem.'));
     }
     return $this->module->goPodium();
 }
 /**
  * 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']);
     }
 }
Exemplo n.º 7
0
 /**
  * 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]);
 }
Exemplo n.º 8
0
 /**
  * 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;
 }
 /**
  * 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]);
 }
Exemplo n.º 10
0
 /**
  * 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;
 }
Exemplo n.º 11
0
 /**
  * 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);
         }
     }
 }
Exemplo n.º 12
0
 /**
  * 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;
 }
Exemplo n.º 13
0
 /**
  * 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;
     }
 }
Exemplo n.º 14
0
 /**
  * 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()));
     }
 }
Exemplo n.º 15
0
 /**
  * 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;
 }
Exemplo n.º 16
0
 /**
  * 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;
 }
Exemplo n.º 17
0
 /**
  * Performs new thread with first post creation and subscription.
  * @return boolean
  * @since 0.2
  */
 public function podiumNew()
 {
     $transaction = static::getDb()->beginTransaction();
     try {
         if ($this->save()) {
             $this->forum->updateCounters(['threads' => 1]);
             $post = new Post();
             $post->content = $this->post;
             $post->thread_id = $this->id;
             $post->forum_id = $this->forum_id;
             $post->author_id = User::loggedId();
             $post->likes = 0;
             $post->dislikes = 0;
             if ($post->save()) {
                 $post->markSeen();
                 $this->forum->updateCounters(['posts' => 1]);
                 $this->updateCounters(['posts' => 1]);
                 $this->touch('new_post_at');
                 $this->touch('edited_post_at');
                 if ($this->subscribe) {
                     $subscription = new Subscription();
                     $subscription->user_id = User::loggedId();
                     $subscription->thread_id = $this->id;
                     $subscription->post_seen = Subscription::POST_SEEN;
                     $subscription->save();
                 }
             }
         }
         $transaction->commit();
         Cache::clearAfter('newThread');
         Log::info('Thread added', $this->id, __METHOD__);
         return true;
     } catch (Exception $e) {
         $transaction->rollBack();
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
Exemplo n.º 18
0
 /**
  * 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;
 }
Exemplo n.º 19
0
 /**
  * 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']);
     }
 }
Exemplo n.º 20
0
 /**
  * Deleting the forum of given ID.
  * @param integer $cid parent category ID
  * @param integer $id forum ID
  * @return \yii\web\Response
  */
 public function actionDeleteForum($cid = null, $id = null)
 {
     if (!User::can(Rbac::PERM_DELETE_FORUM)) {
         $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.'));
     } else {
         $model = Forum::find()->where(['id' => $id, 'category_id' => $cid])->limit(1)->one();
         if (empty($model)) {
             $this->error(Yii::t('podium/flash', 'Sorry! We can not find Forum with this ID.'));
         } else {
             if ($model->delete()) {
                 Cache::clearAfter('forumDelete');
                 Log::info('Forum deleted', $model->id, __METHOD__);
                 $this->success(Yii::t('podium/flash', 'Forum has been deleted.'));
             } else {
                 Log::error('Error while deleting forum', $model->id, __METHOD__);
                 $this->error(Yii::t('podium/flash', 'Sorry! There was some error while deleting the forum.'));
             }
         }
     }
     return $this->redirect(['admin/forums', 'cid' => $cid]);
 }
Exemplo n.º 21
0
 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;
 }
Exemplo n.º 22
0
 /**
  * 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']);
 }
Exemplo n.º 23
0
 /**
  * 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;
 }
Exemplo n.º 24
0
 /**
  * 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]]);
 }
Exemplo n.º 25
0
 /**
  * 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']);
     }
 }
Exemplo n.º 26
0
 /**
  * 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']);
     }
 }
Exemplo n.º 27
0
 /**
  * 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;
 }
Exemplo n.º 28
0
 /**
  * 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];
 }
Exemplo n.º 29
0
 /**
  * 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']);
 }
Exemplo n.º 30
0
 /**
  * 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;
 }