Example #1
0
 /**
  * Process users badges
  */
 public function process()
 {
     $badges = $this->getBadges();
     foreach (Users::find() as $user) {
         $this->processUserBadges($user, $badges);
     }
 }
Example #2
0
 /**
  * Sends the digest
  */
 public function send()
 {
     $lastMonths = new \DateTime();
     $lastMonths->modify('-6 month');
     $parameters = array('modified_at >= ?0 AND digest = "Y" AND notifications <> "N"', 'bind' => array($lastMonths->getTimestamp()));
     $users = array();
     foreach (Users::find($parameters) as $user) {
         if ($user->email && strpos($user->email, '@') !== false && strpos($user->email, '@users.noreply.github.com') === false) {
             $users[trim($user->email)] = $user->name;
         }
     }
     $fromName = $this->config->mail->fromName;
     $fromEmail = $this->config->mail->fromEmail;
     $url = $this->config->site->url;
     $subject = 'Top Stories from Phosphorum ' . date('d/m/y');
     $lastWeek = new \DateTime();
     $lastWeek->modify('-1 week');
     $order = 'number_views + ' . '((IF(votes_up IS NOT NULL, votes_up, 0) - ' . 'IF(votes_down IS NOT NULL, votes_down, 0)) * 4) + ' . 'number_replies + IF(accepted_answer = "Y", 10, 0) DESC';
     $parameters = array('created_at >= ?0 AND deleted != 1 AND categories_id <> 4', 'bind' => array($lastWeek->getTimestamp()), 'order' => $order, 'limit' => 10);
     $e = $this->escaper;
     $content = '<html><head></head><body><p><h1 style="font-size:22px;color:#333;letter-spacing:-0.5px;line-height:1.25;font-weight:normal;padding:16px 0;border-bottom:1px solid #e2e2e2">Top Stories from Phosphorum</h1></p>';
     foreach (Posts::find($parameters) as $post) {
         $user = $post->user;
         if ($user == false) {
             continue;
         }
         $content .= '<p><a style="text-decoration:none;display:block;font-size:20px;color:#333;letter-spacing:-0.5px;line-height:1.25;font-weight:normal;color:#155fad" href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '">' . $e->escapeHtml($post->title) . '</a></p>';
         $content .= '<p><table width="100%"><td><table><tr><td>' . '<img src="https://secure.gravatar.com/avatar/' . $user->gravatar_id . '?s=32&amp;r=pg&amp;d=identicon" width="32" height="32" alt="' . $user->name . ' icon">' . '</td><td><a style="text-decoration:none;color:#155fad" href="' . $url . '/user/' . $user->id . '/' . $user->login . '">' . $user->name . '<br><span style="text-decoration:none;color:#999;text-decoration:none">' . $user->getHumanKarma() . '</span></a></td></tr></table></td><td align="right"><table style="border: 1px solid #dadada;" cellspacing=5>' . '<td align="center"><label style="color:#999;margin:0px;font-weight:normal;">Created</label><br>' . $post->getHumanCreatedAt() . '</td>' . '<td align="center"><label style="color:#999;margin:0px;font-weight:normal;">Replies</label><br>' . $post->number_replies . '</td>' . '<td align="center"><label style="color:#999;margin:0px;font-weight:normal;">Views</label><br>' . $post->number_views . '</td>' . '<td align="center"><label style="color:#999;margin:0px;font-weight:normal;">Votes</label><br>' . ($post->votes_up - $post->votes_down) . '</td>' . '</tr></table></td></tr></table></p>';
         $content .= $this->markdown->render($e->escapeHtml($post->content));
         $content .= '<p><a style="color:#155fad" href="' . $url . '/discussion/' . $post->id . '/' . $post->slug . '">Read more</a></p>';
         $content .= '<hr style="border: 1px solid #dadada">';
     }
     $textContent = strip_tags($content);
     $htmlContent = $content . '<p style="font-size:small;-webkit-text-size-adjust:none;color:#717171;">';
     $htmlContent .= PHP_EOL . 'This email was sent by Phalcon Framework. Change your e-mail preferences <a href="' . $url . '/settings">here</a></p>';
     foreach ($users as $email => $name) {
         try {
             $message = new \Swift_Message('[Phalcon Forum] ' . $subject);
             $message->setTo(array($email => $name));
             $message->setFrom(array($fromEmail => $fromName));
             $bodyMessage = new \Swift_MimePart($htmlContent, 'text/html');
             $bodyMessage->setCharset('UTF-8');
             $message->attach($bodyMessage);
             $bodyMessage = new \Swift_MimePart($textContent, 'text/plain');
             $bodyMessage->setCharset('UTF-8');
             $message->attach($bodyMessage);
             if (!$this->transport) {
                 $this->transport = \Swift_SmtpTransport::newInstance($this->config->smtp->host, $this->config->smtp->port, $this->config->smtp->security);
                 $this->transport->setUsername($this->config->smtp->username);
                 $this->transport->setPassword($this->config->smtp->password);
             }
             if (!$this->mailer) {
                 $this->mailer = \Swift_Mailer::newInstance($this->transport);
             }
             $this->mailer->send($message);
         } catch (\Exception $e) {
             echo $e->getMessage(), PHP_EOL;
         }
     }
 }
Example #3
0
 /**
  * Shows the user profile
  *
  * @param int    $id       User id
  * @param string $username User name
  */
 public function viewAction($id, $username)
 {
     $user = $id ? Users::findFirstById($id) : Users::findFirstByLogin($username);
     if (!$user) {
         $user = Users::findFirstByName($username);
     }
     if (!$user) {
         $this->flashSession->error('The user does not exist');
         $this->response->redirect();
         return;
     }
     $this->view->setVar('user', $user);
     $parametersNumberPosts = ['users_id = ?0 AND deleted = 0', 'bind' => [$user->id]];
     $this->view->setVar('numberPosts', Posts::count($parametersNumberPosts));
     $parametersNumberReplies = ['users_id = ?0', 'bind' => [$user->id]];
     $this->view->setVar('numberReplies', PostsReplies::count($parametersNumberReplies));
     $parametersActivities = ['users_id = ?0', 'bind' => [$user->id], 'order' => 'created_at DESC', 'limit' => 15];
     $this->view->setVar('activities', Activities::find($parametersActivities));
     $users = Users::find(['columns' => 'id', 'conditions' => 'karma != 0', 'order' => 'karma DESC']);
     $ranking = count($users);
     foreach ($users as $position => $everyUser) {
         if ($everyUser->id == $user->id) {
             $ranking = $position + 1;
             break;
         }
     }
     $this->gravatar->setSize(220);
     $this->view->setVars(['ranking' => $ranking, 'total_ranking' => count($users), 'avatar' => $this->gravatar->getAvatar($user->email)]);
     $this->tag->setTitle('Profile - ' . $this->escaper->escapeHtml($user->name));
 }
Example #4
0
 public function onConstruct()
 {
     $lastThreads = $this->modelsManager->createBuilder()->from(['p' => 'Phosphorum\\Models\\Posts'])->groupBy("p.id")->join('Phosphorum\\Models\\Categories', "r.id = p.categories_id", 'r')->join('Phosphorum\\Models\\Users', "u.id = p.users_id", 'u')->columns(['p.title as title_post', 'p.id as id_post', 'p.slug as slug_post', 'r.name as name_category', 'u.name as name_user'])->where('p.deleted = 0')->orderBy('p.created_at DESC')->limit(3)->getQuery()->execute();
     /** @var Simple $lastMember */
     $lastMember = Users::find(['order' => 'created_at DESC', 'limit' => 1, 'columns' => 'login']);
     $login = null;
     if ($lastMember->valid()) {
         $login = $lastMember->getFirst()->login;
     }
     $this->view->setVars(['app_name' => $this->config->get('site')->name, 'app_version' => VERSION, 'threads' => Posts::count(), 'last_threads' => $lastThreads, 'users' => Users::count(), 'users_latest' => $login, 'actionName' => $this->dispatcher->getActionName(), 'controllerName' => $this->dispatcher->getControllerName()]);
 }
Example #5
0
 public function karmaAction()
 {
     foreach (Users::find() as $user) {
         if ($user->karma === null) {
             $parametersNumbersPost = array('users_id = ?0', 'bind' => array($user->id));
             $numberPosts = Posts::count($parametersNumbersPost);
             $parametersNumberReplies = array('users_id = ?0', 'bind' => array($user->id));
             $numberReplies = PostsReplies::count($parametersNumberReplies);
             $user->karma = $numberReplies * 10 + $numberPosts * 5;
             $user->votes = intval($user->karma / 50);
             $user->save();
         }
     }
 }
Example #6
0
 public function afterCreate()
 {
     /**
      * Register a new activity
      */
     if ($this->id > 0) {
         /**
          * Register the activity
          */
         $activity = new Activities();
         $activity->users_id = $this->users_id;
         $activity->posts_id = $this->id;
         $activity->type = Activities::NEW_POST;
         $activity->save();
         /**
          * Notify users that always want notifications
          */
         $notification = new PostsNotifications();
         $notification->users_id = $this->users_id;
         $notification->posts_id = $this->id;
         $notification->save();
         /**
          * Notify users that always want notifications
          */
         $toNotify = [];
         foreach (Users::find(['notifications = "Y"', 'columns' => 'id']) as $user) {
             if ($this->users_id != $user->id) {
                 $notification = new Notifications();
                 $notification->users_id = $user->id;
                 $notification->posts_id = $this->id;
                 $notification->type = 'P';
                 $notification->save();
                 $toNotify[$user->id] = $notification->id;
             }
         }
         /**
          * Update the total of posts related to a category
          */
         $this->category->number_posts++;
         $this->category->save();
         /**
          * Queue notifications to be sent
          */
         $this->getDI()->getQueue()->put($toNotify);
     }
 }
Example #7
0
 /**
  * Sends the digest
  */
 public function send()
 {
     $lastMonths = new \DateTime();
     $lastMonths->modify('-6 month');
     $parameters = ['modified_at >= ?0 AND digest = "Y" AND notifications <> "N"', 'bind' => [$lastMonths->getTimestamp()]];
     $users = [];
     foreach (Users::find($parameters) as $user) {
         if ($user->email && strpos($user->email, '@') !== false && strpos($user->email, '@users.noreply.github.com') === false) {
             $users[trim($user->email)] = $user->name;
         }
     }
     $view = new View();
     $view->setViewsDir($this->config->application->viewsDir);
     $fromName = $this->config->mail->fromName;
     $fromEmail = $this->config->mail->fromEmail;
     $url = rtrim($this->config->site->url, '/');
     $subject = sprintf('Top Stories from Phosphorum %s', date('d/m/y'));
     $view->setVar('title', $subject);
     $lastWeek = new \DateTime();
     $lastWeek->modify('-1 week');
     $order = 'number_views + ' . '((IF(votes_up IS NOT NULL, votes_up, 0) - ' . 'IF(votes_down IS NOT NULL, votes_down, 0)) * 4) + ' . 'number_replies + IF(accepted_answer = "Y", 10, 0) DESC';
     $parameters = ['created_at >= ?0 AND deleted != 1 AND categories_id <> 4', 'bind' => [$lastWeek->getTimestamp()], 'order' => $order, 'limit' => 10];
     $e = $this->escaper;
     /** @var \Phalcon\Logger\AdapterInterface $logger */
     $logger = $this->getDI()->get('logger', ['mail']);
     $stories = [];
     foreach (Posts::find($parameters) as $i => $post) {
         $user = $post->user;
         if ($user == false) {
             continue;
         }
         $this->gravatar->setSize(32);
         $stories[$i]['user_name'] = $user->name;
         $stories[$i]['user_avatar'] = $this->gravatar->getAvatar($user->email);
         $stories[$i]['user_url'] = "{$url}/user/{$user->id}/{$user->login}";
         $stories[$i]['user_karma'] = $user->getHumanKarma();
         $stories[$i]['post_title'] = $e->escapeHtml($post->title);
         $stories[$i]['post_created'] = $post->getHumanCreatedAt();
         $stories[$i]['post_replies'] = (int) $post->number_replies;
         $stories[$i]['post_views'] = (int) $post->number_views;
         $stories[$i]['post_votes'] = $post->votes_up - $post->votes_down;
         $stories[$i]['post_content'] = $this->markdown->render($e->escapeHtml($post->content));
         $stories[$i]['post_url'] = "{$url}/discussion/{$post->id}/{$post->slug}";
     }
     if (empty($stories)) {
         return;
     }
     $view->setVar('stories', $stories);
     $view->setVar('notice', sprintf('This email was sent by %s mail sender. Change your e-mail preferences <a href="%s/settings">here</a>', $this->config->site->name, $url));
     $content = $view->render('mail/digest.phtml');
     $textContent = preg_replace('#<a[^>]+href="([^"]+)"[^>]*>([^<]+)<\\/a>#', '$2:' . "\n" . '$1', $content);
     $textContent = str_replace('<span class="foot-line"></span>', "--\n", $textContent);
     $textContent = trim(strip_tags($textContent));
     $textContent = str_replace('&nbsp;', ' ', $textContent);
     $textContent = preg_replace('#\\t+#', '', $textContent);
     $textContent = preg_replace('# {2,}#', ' ', $textContent);
     $textContent = preg_split('#(\\r|\\n)#', $textContent);
     $textContent = join("\n\n", array_filter($textContent, function ($line) {
         return '' !== trim($line);
     }));
     $textContent = preg_replace('#^[ \\t]+#m', '', $textContent);
     foreach ($users as $email => $name) {
         try {
             $message = new \Swift_Message("[{$this->config->site->name} Forum] " . $subject);
             $message->setTo([$email => $name]);
             $message->setFrom([$fromEmail => $fromName]);
             $bodyMessage = new \Swift_MimePart($content, 'text/html');
             $bodyMessage->setCharset('UTF-8');
             $message->attach($bodyMessage);
             $bodyMessage = new \Swift_MimePart($textContent, 'text/plain');
             $bodyMessage->setCharset('UTF-8');
             $message->attach($bodyMessage);
             if (!$this->transport) {
                 $this->transport = \Swift_SmtpTransport::newInstance($this->config->smtp->host, $this->config->smtp->port, $this->config->smtp->security);
                 $this->transport->setUsername($this->config->smtp->username);
                 $this->transport->setPassword($this->config->smtp->password);
             }
             if (!$this->mailer) {
                 $this->mailer = \Swift_Mailer::newInstance($this->transport);
             }
             $failedRecipients = [];
             $this->mailer->send($message, $failedRecipients);
             if (empty($failedRecipients)) {
                 $logger->info("Sent an email to {$email}");
             } else {
                 $logger->error("Unable to sent an email to " . join(', ', $failedRecipients));
             }
         } catch (\Exception $e) {
             $logger->error($e->getMessage());
             throw new \Exception($e->getMessage(), $e->getCode(), $e);
         }
     }
 }
Example #8
0
 public function afterCreate()
 {
     if ($this->id > 0) {
         $activity = new Activities();
         $activity->users_id = $this->users_id;
         $activity->posts_id = $this->posts_id;
         $activity->type = Activities::NEW_REPLY;
         $activity->save();
         $toNotify = array();
         /**
          * Notify users that always want notifications
          */
         foreach (Users::find(array('notifications = "Y"', 'columns' => 'id')) as $user) {
             if ($this->users_id != $user->id) {
                 $notification = new Notifications();
                 $notification->users_id = $user->id;
                 $notification->posts_id = $this->posts_id;
                 $notification->posts_replies_id = $this->id;
                 $notification->type = 'C';
                 $notification->save();
                 $activity = new ActivityNotifications();
                 $activity->users_id = $user->id;
                 $activity->posts_id = $this->posts_id;
                 $activity->posts_replies_id = $this->id;
                 $activity->users_origin_id = $this->users_id;
                 $activity->type = 'C';
                 $activity->save();
                 $toNotify[$user->id] = $notification->id;
             }
         }
         /**
          * Register users subscribed to the post
          */
         foreach (PostsSubscribers::findByPostsId($this->posts_id) as $subscriber) {
             if (!isset($toNotify[$subscriber->users_id])) {
                 $notification = new Notifications();
                 $notification->users_id = $subscriber->users_id;
                 $notification->posts_id = $this->posts_id;
                 $notification->posts_replies_id = $this->id;
                 $notification->type = 'C';
                 $notification->save();
                 $activity = new ActivityNotifications();
                 $activity->users_id = $subscriber->users_id;
                 $activity->posts_id = $this->posts_id;
                 $activity->posts_replies_id = $this->id;
                 $activity->users_origin_id = $this->users_id;
                 $activity->type = 'C';
                 $activity->save();
                 $toNotify[$subscriber->users_id] = $notification->id;
             }
         }
         /**
          * Register the user in the post's notifications
          */
         if (!isset($toNotify[$this->users_id])) {
             $parameters = array('users_id = ?0 AND posts_id = ?1', 'bind' => array($this->users_id, $this->posts_id));
             $hasNotifications = PostsNotifications::count($parameters);
             if (!$hasNotifications) {
                 $notification = new PostsNotifications();
                 $notification->users_id = $this->users_id;
                 $notification->posts_id = $this->posts_id;
                 $notification->save();
             }
         }
         /**
          * Notify users that have commented in the same post
          */
         $postsNotifications = PostsNotifications::findByPostsId($this->posts_id);
         foreach ($postsNotifications as $postNotification) {
             if (!isset($toNotify[$postNotification->users_id])) {
                 if ($postNotification->users_id != $this->users_id) {
                     /**
                      * Generate an e-mail notification
                      */
                     $notification = new Notifications();
                     $notification->users_id = $postNotification->users_id;
                     $notification->posts_id = $this->posts_id;
                     $notification->posts_replies_id = $this->id;
                     $notification->type = 'C';
                     $notification->save();
                     $activity = new ActivityNotifications();
                     $activity->users_id = $postNotification->users_id;
                     $activity->posts_id = $this->posts_id;
                     $activity->posts_replies_id = $this->id;
                     $activity->users_origin_id = $this->users_id;
                     $activity->type = 'C';
                     $activity->save();
                     $toNotify[$postNotification->users_id] = $notification->id;
                 }
             }
         }
         /**
          * Queue notifications to be sent
          */
         $this->getDI()->getQueue()->put($toNotify);
     }
 }
 /**
  * Shows the user profile
  */
 public function userAction($id, $username)
 {
     if ($id) {
         $user = Users::findFirstById($id);
     } else {
         $user = Users::findFirstByLogin($username);
         if (!$user) {
             $user = Users::findFirstByName($username);
         }
     }
     if (!$user) {
         $this->flashSession->error('The user does not exist');
         return $this->response->redirect();
     }
     $this->view->user = $user;
     $parametersNumberPosts = array('users_id = ?0 AND deleted = 0', 'bind' => array($user->id));
     $this->view->numberPosts = Posts::count($parametersNumberPosts);
     $parametersNumberReplies = array('users_id = ?0', 'bind' => array($user->id));
     $this->view->numberReplies = PostsReplies::count($parametersNumberReplies);
     $parametersActivities = array('users_id = ?0', 'bind' => array($user->id), 'order' => 'created_at DESC', 'limit' => 15);
     $this->view->activities = Activities::find($parametersActivities);
     $users = Users::find(array('columns' => 'id', 'conditions' => 'karma != 0', 'order' => 'karma DESC'));
     $ranking = count($users);
     foreach ($users as $position => $everyUser) {
         if ($everyUser->id == $user->id) {
             $ranking = $position + 1;
             break;
         }
     }
     $this->view->ranking = $ranking;
     $this->view->total_ranking = count($users);
     $this->tag->setTitle('Profile - ' . $this->escaper->escapeHtml($user->name));
 }
Example #10
0
}
for ($i = 0; $i <= 50; $i++) {
    $user = new Users();
    $user->name = $faker->name;
    $user->login = $faker->userName;
    $user->email = $faker->email;
    $user->timezone = $faker->timezone;
    if (!$user->save()) {
        $database->rollback();
        die(join(PHP_EOL, $user->getMessages()));
    }
    $log->info('User: '******'columns' => 'id'])->toArray();
$userIds = Users::find(['columns' => 'id'])->toArray();
$database->begin();
for ($i = 0; $i <= 500; $i++) {
    $title = $faker->company;
    $post = new Posts();
    $post->title = $title;
    $post->slug = Tag::friendlyTitle($title);
    $post->content = $faker->text();
    $userRandId = array_rand($userIds);
    $post->users_id = $userIds[$userRandId]['id'];
    $categoryRandId = array_rand($categoryIds);
    $post->categories_id = $categoryIds[$categoryRandId]['id'];
    if (!$post->save()) {
        $database->rollback();
        die(join(PHP_EOL, $post->getMessages()));
    }
Example #11
0
 public function onConstruct()
 {
     $last_threads = $this->modelsManager->createBuilder()->from(array('p' => 'Phosphorum\\Models\\Posts'))->groupBy("p.id")->join('Phosphorum\\Models\\Categories', "r.id = p.categories_id", 'r')->join('Phosphorum\\Models\\Users', "u.id = p.users_id", 'u')->columns(array('p.title as title_post', 'p.id as id_post', 'p.slug as slug_post', 'r.name as name_category', 'u.name as name_user'))->orderBy('p.created_at DESC')->limit(3)->getQuery()->execute();
     $users = Users::find()->getLast();
     $this->view->setVars(['threads' => Posts::count(), 'last_threads' => $last_threads, 'users' => Users::count(), 'users_latest' => $users->login, 'actionName' => $this->dispatcher->getActionName()]);
 }