Here this might help you out.
<?php
class InvisionBridge {
private static $instance;
/**
* InvisionAPI constructor.
*/
public function __construct() {
$this->requireIPS();
$this->refreshSession();
$this->session = \IPS\Session::i();
$this->database = \IPS\Db::i();
$this->settings = \IPS\Settings::i();
}
/**
* @return InvisionBridge
* Constructs an instance of this class if one does not exist already.
*/
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new InvisionBridge();
}
return self::$instance;
}
/**
* Returns the current logged in user
* @return Member
*/
public function getCachedMember() {
\IPS\Session\Front::i(); // Refreshes our IPS session, if we ever had one.
$member = \IPS\Member::loggedIn();
if ($this->isGuest($member)) {
return null;
}
return new Member($member);
}
/**
* @param $username
* @return Member|null
*/
public function loadMember($username) {
try {
$member = \IPS\Member::load($username, 'name');
if ($this->isGuest($member)) {
return null;
}
return new Member($member);
} catch (Exception $e) {
return null;
}
}
/**
* @param int $member_id
* @return Member|null
*/
public function loadMemberById($member_id) {
try {
$member = \IPS\Member::load($member_id, 'member_id');
if ($this->isGuest($member)) {
return null;
}
return new Member($member);
} catch (Exception $e) {
return null;
}
}
/**
* @param $username
* @param $password
* @param bool $remember
* @return Member|null
* @throws Exception
*/
public function login($username, $password, $remember = false) {
$member = null;
try {
$member = $this->loadMember($username);
} catch (Exception $e) {
return null;
}
if ($member == null) {
return null;
}
if (!$this->verifyPassword($member, $password)) {
return null;
}
$this->setSession($member, $remember);
return $member;
}
/**
* Logs out of the current user.
*/
public function logout() {
$member = $this->getCachedMember();
if ($member == null) {
return; // We are already logged out
}
session_destroy();
\IPS\Request::i()->clearLoginCookies();
$member->getRaw()->memberSync('onLogout', array(\IPS\Http\Url::internal('')));
}
/**
* Sets the user session after use has been verified.
* @param $username
* @return null
* @throws Exception
*/
public function setSession($member, $rememberMe) {
\IPS\Session::i()->setMember($member->getRaw());
$device = \IPS\Member\Device::loadOrCreate($member->getRaw());
$member->getRaw()->last_visit = $member->last_activity;
$member->getRaw()->save();
$device->anonymous = false;
$device->updateAfterAuthentication($rememberMe, null);
$member->getRaw()->memberSync('onLogin');
$member->getRaw()->profileSync();
}
/**
* Checks if the user is a guest of not.
* @param $member
* @return bool
*/
public function isGuest($member) {
return $member->member_group_id == \IPS\Settings::i()->guest_group;
}
/**
* Finds multi-factor authentication information of the specified type for the specified member.
* @param $member
* @param $type
* @return null
*/
public function findMfa($member, $type) {
if ($member == null) {
return null;
}
$mfaDetails = $member->getRaw()->get_mfa_details();
if (count($mfaDetails) == 0) {
return null;
}
$mfaToken = $mfaDetails[$type];
if (isset($mfaToken)) {
return $mfaToken;
}
return null;
}
/**
* Finds and returns the latest topics in the specified board.
* @param $boards array The board to find topics in
* @param int $maximum The maximum amount of topics to find (default 100)
* @return array Found topics, empty array if criteria is not matched
*/
public function findLatestTopics($boards, $maximum = 100) {
$query = "(forum_id = ".implode(' OR forum_id = ', $boards).")";
$select = $this->database->select("*",
'forums_topics',
array("approved = 1 AND $query"),
"start_date DESC",
array(0, $maximum)
);
$topics = array();
foreach ($select as $topic) {
$topics[] = $topic;
}
return $topics;
}
/**
* Finds and returns the latest posts in the specified topic.
* @param $topic int The topic to find posts in
* @param int $maximum The maximum amount of posts to find (default 100)
* @return array Found posts, empty array if criteria is not matched
*/
public function findPosts($topic, $maximum=100) {
$select = $this->database->select("*",
'forums_posts',
array("topic_id = ?", $topic),
"post_date ASC",
array(0, $maximum)
);
$posts = array();
foreach($select as $post) {
$posts[] = $post;
}
return $posts;
}
/**
* Finds and returns the latest posts in the specified topic.
* @param $postId int The post id
* @return array Found posts, empty array if criteria is not matched
*/
public function findPost($postId) {
$select = $this->database->select("*",
'forums_posts',
array("pid = ?", $postId),
"post_date ASC"
);
return $select->first();
}
/**
* Fetches a member's info directly from database to avoid loading using IPB files.
* @param $mid integer
* @return mixed
*/
public function getMemberInfo($mid) {
$select = $this->database->select("*",
'core_members',
array("member_id = ?", $mid),
'member_id ASC',
array(0, 1)
);
return $select->first();
}
/**
* Verifies that the password entered is correct
* @param $member
* @param $password
* @return bool
*/
public function verifyPassword($member, $password) {
return password_verify($password, $member->getPassword()) === true;
}
/**
* Refreshes our IPS session, if we ever had one.
* Required if for some reason our session has timed out and we have yet to revisit the suite.
*/
public function refreshSession() {
\IPS\Session\Front::i();
}
/**
* Includes the required file from the forum
*/
private function requireIPS() {
require_once FORUM_PATH . 'init.php';
}
}