Example #1
0
 /**
  * Ban user
  * @since Version 3.2
  * @version 3.2
  * @param int|boolean $userId
  * @param string|boolean $reason
  * @param int|boolean $expiry
  * @param int|boolean $adminUserId
  * @return boolean
  */
 public function banUser($userId = null, $reason = null, $expiry = "0", $adminUserId = null)
 {
     if (!filter_var($userId, FILTER_VALIDATE_INT)) {
         throw new InvalidArgumentException("No user ID supplied");
     }
     if (is_null($reason)) {
         throw new InvalidArgumentException("No reason was supplied");
     }
     if (!filter_var($adminUserId, FILTER_VALIDATE_INT)) {
         throw new InvalidArgumentException("No administrative user ID was supplied");
     }
     /**
      * Empty the cache
      */
     $this->Memcached = AppCore::getMemcached();
     try {
         if ($this->Memcached->Fetch("railpage:bancontrol.users")) {
             $this->Memcached->delete("railpage:bancontrol.users");
         }
         if ($this->Memcached->Fetch(self::CACHE_KEY_ALL)) {
             $this->Memcached->delete(self::CACHE_KEY_ALL);
         }
     } catch (Exception $e) {
         // throw it away
     }
     try {
         $this->Redis->delete("railpage:bancontrol");
     } catch (Exception $e) {
         // throw it away
     }
     $data = array("user_id" => $userId, "ban_active" => 1, "ban_time" => time(), "ban_reason" => $reason, "banned_by" => $adminUserId, "ban_expire" => $expiry);
     $this->db->insert("bancontrol", $data);
     $cachekey_user = sprintf(self::CACHE_KEY_USER, $userId);
     $expire = $expiry > 0 ? $expiry : 0;
     $this->Memcached->save($cachekey_user, true, $expire);
     /**
      * Update the cache
      */
     $this->cacheAll(true);
     /**
      * Tell the world that they've been naughty
      */
     $ThisUser = UserFactory::CreateUser($userId);
     $ThisUser->active = 0;
     $ThisUser->location = "Banned";
     $ThisUser->signature = "Banned";
     $ThisUser->avatar = "";
     $ThisUser->interests = "";
     $ThisUser->occupation = "";
     $ThisUser->commit(true);
     $ThisUser->addNote("Banned", $adminUserId);
     $Smarty = AppCore::GetSmarty();
     // Send the ban email
     $Smarty->Assign("userdata_username", $ThisUser->username);
     $Smarty->Assign("ban_reason", $reason);
     if ($expiry > 0) {
         $Smarty->Assign("ban_expire_nice", date($ThisUser->date_format, $expiry));
     }
     // Send the confirmation email
     $Notification = new Notification();
     if ($adminUserId !== false) {
         $Notification->setAuthor(UserFactory::CreateUser($adminUserId));
     }
     $Notification->addRecipient($ThisUser->id, $ThisUser->username, $ThisUser->contact_email);
     $Notification->body = $Smarty->Fetch($Smarty->ResolveTemplate("email.ban"));
     $Notification->subject = "Railpage account suspension";
     $Notification->transport = Notifications::TRANSPORT_EMAIL;
     $Notification->commit()->dispatch();
     return true;
 }