<?php
/*
*XenForo Support Functions
*/
class appXenForo {
//Singleton Storage
private static $bInitialized = false;
/**
* Initialise XenForo Functions
*/
function __construct(){}
/*
*Initialise the XenForom Controllers:
*/
public static function init() {
define('XF_ROOT', '/var/www/clients/client123/web567/web/'); // set this!
define('TIMENOW', time());define('SESSION_BYPASS', false);
require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
XenForo_Application::set('page_start_time', TIMENOW);
XenForo_Application::disablePhpErrorHandler();
XenForo_Session::startPublicSession();
error_reporting(E_ALL & ~E_NOTICE);
}
/*
*Set the user password.
*/
public static function setPassword($iID, $sPassword) {
$sQuery = "UPDATE xenforo.xf_user_authenticate SET data = BINARY
CONCAT(CONCAT(CONCAT('a:3:{s:4:\"hash\";s:40:\"',
SHA1(CONCAT(SHA1('$sPassword'), SHA1('salt')))),
CONCAT('\";s:4:\"salt\";s:40:\"', SHA1('salt'))),
'\";s:8:\"hashFunc\";s:4:\"sha1\";}'),scheme_class = 'XenForo_Authentication_Core'
WHERE user_id = $iID;";
//QF($sQuery);
}
/*
*Get an array of the latest posts:
*/
public static function getLatestPosts($iMax) {
$sQuery = "
SELECT thread.last_post_id as post_id,
thread.last_post_user_id as user_id,
thread.last_post_username as username ,
thread.discussion_state,
thread.last_post_date,
thread.title as threadtitle,
thread.thread_id as thread_id,
forum.title as node_title, forum.node_id as node_id
FROM xf_thread as thread
LEFT JOIN xf_node as forum ON (forum.node_id = thread.node_id)
ORDER BY thread.last_post_date DESC
LIMIT $iMax";
//Get the rows:
$aLatest = query($sQuery);
// Loop over each post, get the message
foreach ($aLatest as &$cPost) {
//Get the message:
$aRow = query("SELECT * FROM xf_post WHERE post_id = ? LIMIT 1", array($cPost['post_id']));
$cPost['message'] = self::stripBBCode($aRow['message']);
}
return $aLatest;
}
/*
*Get a filtered string URL for the thread:
*/
public static function getThreadURL($sThreadTitle, $iThreadID) {
$sThreadURL = strtolower(str_replace(" ", "-", $sThreadTitle));
$sThreadURL = preg_replace("/[^A-Za-z0-9-]/",'', $sThreadURL);
return "forum/index.php?threads/{$sThreadURL}.{$iThreadID}";
}
/*
*Get a filtered string URL for a node / forum/.
*/
public static function getNodeURL($sNodeTitle, $iNodeID) {
$sForumURL = strtolower(str_replace(" ", "-", $sNodeTitle));
$sForumURL = preg_replace("/[^A-Za-z0-9-]/",'', $sForumURL);
return "forum/index.php?forums/{$sNodeURL}.{$cPost['node_id']}/";
}
/*
*Strip out BB code from xfPosts
*/
public static function stripBBCode($sMessage) {
return strip_tags(str_replace(array('[',']'), array('<','>'), $sMessage));
}
/*
*Create a XenForo User:
*/
public static function createUser($sUsername, $sEmail, $sPassword, array $aAdditionalData = array()) {
//Create the username from the person's name:
$sUsername = str_replace(' ', "_", $sUsername);
//Set User Data
$cWriter = XenForo_DataWriter::create('XenForo_DataWriter_User');
$cWriter->set('username', $sUsername);
$cWriter->set('email', $sEmail);
$cWriter->setPassword($sPassword);
$cWriter->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
$cWriter->set('user_state', 'email_confirm');
foreach ($aAdditionalData AS $data => $key) {
$cWriter->set($data, $key);
}
$cWriter->save();
$cUser = $cWriter->getMergedData();
//Login new user: Log the ip of the user registering
XenForo_Model_Ip::log($cUser['user_id'], 'user', $cUser['user_id'], 'register');
//Set the user back to the browser session
XenForo_Application::get('session')->changeUserId($cUser['user_id']);
XenForo_Visitor::setup($cUser['user_id']);
return $cUser['user_id'];
}
/*
*Get the current user:
*/
public static function getCurrentUser() {
XenForo_Session::startPublicSession();
$cVisitor = XenForo_Visitor::getInstance();
if($cVisitor->getUserId()){
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$cUserInfo = $dbUserModel->getFullUserById($cVisitor->getUserId());
}
return $cUserInfo;
}
/*
*Get the current user:
*/
public static function getUserByEmail($sEmail) {
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$cUser = $dbUserModel->getUserByEmail($sEmail, array('join' => XenForo_Model_User::FETCH_USER_PROFILE + XenForo_Model_User::FETCH_LAST_ACTIVITY));
return $cUser;
}
/*
*Set the user state: from email_confirm to valid.
*/
public static function setUserState($iXFID, $sState) {
//'valid'
//'email_confirm'
//query("UPDATE xf_user SET user_state = ? WHERE user_id = ? LIMIT 1", array($sState, $iXFID));
}
/*
*Login a XenForo User // Set the cookie.
*/
public static function login($sEmail, $sPassword, $bRemember = true) {
//Get this class; delete existing login information
error_reporting(E_ALL);
restore_error_handler();
restore_exception_handler();
$dbLoginModel = XenForo_Model::create('XenForo_Model_Login');
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$sError = "";
$iUserID = $dbUserModel->validateAuthentication($sEmail, $sPassword, $sError);
if (!$iUserID) {
$dbLoginModel->logLoginAttempt($sEmail);
return $sError;
}
$dbLoginModel->clearLoginAttempts($sEmail);
if ($bRemember) {
$dbUserModel->setUserRememberCookie($iUserID);
}
XenForo_Model_Ip::log($iUserID, 'user', $iUserID, 'login');
$dbUserModel->deleteSessionActivity(0, $_SERVER['REMOTE_ADDR']);
$cSession = XenForo_Application::get('session');
$cSession->changeUserId($iUserID);
XenForo_Visitor::setup($iUserID);
return $iUserID;
}
/*
*Set this user ID as logged in.
*/
public static function setLogin($iUserID) {
$dbUserModel = XenForo_Model::create('XenForo_Model_User');
$dbUserModel->setUserRememberCookie($iUserID);
XenForo_Model_Ip::log($iUserID, 'user', $iUserID, 'login');
$dbUserModel->deleteSessionActivity(0, $_SERVER['REMOTE_ADDR']);
$cSession = XenForo_Application::get('session');
$cSession->changeUserId($iUserID);
XenForo_Visitor::setup($iUserID);
}
/*
*Check if this user name is in use, return true for already exists.
*/
public static function bUsernameInUse($sUsername) {
$cUser = query("SELECT * FROM xf_user WHERE `username` = '$sUsername' LIMIT 1");
if (is_numeric($cUser['user_id'])) {
return true;
} else {
return false;
}
}
/*
*Delete the current session and log out.
*/
public static function logout() {
if (XenForo_Visitor::getInstance()->get('is_admin')) {
$adminSession = new XenForo_Session(array('admin' => true));
$adminSession->start();
if ($adminSession->get('user_id') == XenForo_Visitor::getUserId()) {
$adminSession->delete();
}
}
XenForo_Model::create('XenForo_Model_Session')->processLastActivityUpdateForLogOut(XenForo_Visitor::getUserId());
XenForo_Application::get('session')->delete();
XenForo_Helper_Cookie::deleteAllCookies(
array('session'),
array('user' => array('httpOnly' => false))
);
XenForo_Visitor::setup(0);
return;
}
}
?>