ok, i'll post it here, and then a link to the gist.
<?php
/*
Plugin Name: Flarum Forums Auto Login
Description: This is specific to a website who asked for it. There are several specific considerations before implmentation.
- Change the default FORUM_MASTER_PW -- this is basically a password the user never will see or know.
- You have disabled the ability to login from Flarum. This is basically now handled by WordPress only.
- Avatars are handled on your backend in database logic.
- New user creation is also handled by database logic with triggers. I'll post those after this.
- I made this because it was good enough for me. I have other implementations and if I needed to add more, I would. It's all kind of up to you from here!
Version: 1.0
*/
defined('ABSPATH') OR exit;
/ cURL seems to work sometimes, other times it won't unless this is set. /
set_time_limit(128);
/ I don't know a single wordpress installation which should willy nilly update and auto-break things. /
add_filter('auto_update_plugin', 'return_false');
add_filter('auto_update_theme', 'return_false');
add_filter('auto_update_translation', 'return_false');
add_filter('automatic_updater_disabled', 'return_false');
add_filter('auto_update_core', '__return_false');
/
Class flarum_forum_auth
/
final class flarum_forum_auth
{
/
/
const FORUM_MASTER_PW = 'pick_whatever';
/**
* @var string
*/
public $userpass_from_check = '';
/**
* flarum_forum_auth constructor.
*
* What to do and when
*/
public function __construct()
{
add_action('send_headers', [$this, 'action_add_header_auth_token']);
add_action('wp_authenticate', [$this, 'action_wp_authenticate'], 10, 1);
add_action('wp_logout', [$this, 'action_user_site_logout']);
add_action('user_register', [$this, 'action_registered']);
add_action('personal_options_update', [$this, 'action_updated']);
}
/**
*
*/
public function action_user_site_logout()
{
setcookie('flarum_remember', '', time() - 3600, '/', '.' . $_SERVER['HTTP_HOST'], FALSE, TRUE);
setcookie('flarum_remember', '', time() - 3600, '/', $_SERVER['HTTP_HOST'], FALSE, TRUE);
setcookie('flarum_session', '', time() - 3600, '/', $_SERVER['HTTP_HOST'], FALSE, TRUE);
}
/**
*
*/
public function action_add_header_auth_token()
{
global $current_user;
if(!is_user_logged_in())
{
return;
}
$current_user = wp_get_current_user();
$token = get_user_meta($current_user->ID, 'flarum-forum-auth-token', TRUE);
if(strlen($token) > 10)
{
header('Authorization: Token ' . $token);
}
}
/**
* @param $user_id
*
* @return bool
*
* Adds user meta fields when a new user is registered
*/
public function action_registered($user_id)
{
$userdata = get_user_by('id', $user_id);
if(!$userdata)
{
return;
}
$data = ['identification' => $userdata->user_login, 'password' => self::FORUM_MASTER_PW];
$data_string = json_encode($data);
$ch = curl_init('http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/forum/api/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, 'http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/index.php');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]);
$result = curl_exec($ch);
curl_close($ch);
$return = json_decode($result);
$token = (empty($return->token) ? '' : $return->token);
update_user_meta($user_id, 'flarum-forum-auth', 1);
update_user_meta($user_id, 'flarum-forum-auth-token', $token);
}
/**
* @param $username
*
* @return void
* @internal param $user_info_user_login
*/
public function action_wp_authenticate($username)
{
if(!username_exists($username))
{
return;
}
$userinfo = get_user_by('login', $username);
if((int) $userinfo->ID < 1)
{
return;
}
$data = ['identification' => $userinfo->user_login, 'password' => self::FORUM_MASTER_PW];
$data_string = json_encode($data);
$ch = curl_init('http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/forum/api/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, 'http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/index.php');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]);
$result = curl_exec($ch);
curl_close($ch);
$return = json_decode($result);
$token = (empty($return->token) ? '' : $return->token);
update_user_meta($userinfo->ID, 'flarum-forum-auth', 1);
update_user_meta($userinfo->ID, 'flarum-forum-auth-token', $token);
setcookie('flarum_remember', $token, time() + (86400 * 1), '/', $_SERVER['HTTP_HOST'], FALSE, TRUE);
}
/**
* @param $user_id
*/
public function action_updated($user_id)
{
$pass1 = $pass2 = '';
if(array_key_exists('pass1', $_POST) && strlen($_POST['pass1']) > 2)
{
$pass1 = $_POST['pass1'];
}
if(array_key_exists('pass2', $_POST) && strlen($_POST['pass2']) > 2)
{
$pass2 = $_POST['pass2'];
}
if($pass1 != $pass2 || empty($pass1) || empty($pass2) || strpos(stripslashes($pass1), "\\") !== FALSE)
{
return;
}
$userinfo = get_user_by('id', $user_id);
$data = ['identification' => $userinfo->user_login, 'password' => self::FORUM_MASTER_PW];
$data_string = json_encode($data);
$ch = curl_init('http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/forum/api/token');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_REFERER, 'http' . ($_SERVER['HTTPS'] === 'on' ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/index.php');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
]);
$result = curl_exec($ch);
curl_close($ch);
$return = json_decode($result);
$token = (empty($return->token) ? '' : $return->token);
update_user_meta($user_id, 'flarum-forum-auth', 1);
update_user_meta($user_id, 'flarum-forum-auth-token', $token);
}
}
$flarumforumauth = new flarum_forum_auth();