152 lines
4.3 KiB
PHP
152 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
* @author Sascha Nitsch (grumpydeveloper)
|
|
**/
|
|
|
|
namespace Federator\Connector;
|
|
|
|
/**
|
|
* Connector to ContentNation.net
|
|
*/
|
|
class ContentNation implements Connector
|
|
{
|
|
/**
|
|
* config parameter
|
|
*
|
|
* @var array<string, mixed> $config
|
|
*/
|
|
private $config;
|
|
|
|
/**
|
|
* service-URL
|
|
*
|
|
* @var string $service
|
|
*/
|
|
private $service;
|
|
|
|
/**
|
|
* constructor
|
|
*
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$config = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '../contentnation.ini');
|
|
$this->service = $config['service-uri'];
|
|
}
|
|
|
|
/**
|
|
* get statistics from remote system
|
|
*
|
|
* @return \Federator\Data\Stats|false
|
|
*/
|
|
public function getRemoteStats()
|
|
{
|
|
$remoteURL = $this->service . '/api/stats';
|
|
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, []);
|
|
if ($info['http_code'] != 200) {
|
|
print_r($info);
|
|
return false;
|
|
}
|
|
$r = json_decode($response, true);
|
|
if ($r === false || $r === null || !is_array($r)) {
|
|
return false;
|
|
}
|
|
$stats = new \Federator\Data\Stats();
|
|
$stats->userCount = array_key_exists('userCount', $r) ? $r['userCount'] : 0;
|
|
$stats->postCount = array_key_exists('pageCount', $r) ? $r['pageCount'] : 0;
|
|
$stats->commentCount = array_key_exists('commentCount', $r) ? $r['commentCount'] : 0;
|
|
return $stats;
|
|
}
|
|
|
|
/**
|
|
* get remote user by given name
|
|
*
|
|
* @param string $_name user/profile name
|
|
* @return \Federator\Data\User | false
|
|
*/
|
|
public function getRemoteUserByName(string $_name)
|
|
{
|
|
// validate name
|
|
if (preg_match("/^[a-zA-Z0-9_\-]+$/", $_name) != 1) {
|
|
return false;
|
|
}
|
|
$remoteURL = $this->service . '/api/users/info?user=' . urlencode($_name);
|
|
$headers = ['Accept: application/json'];
|
|
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
|
|
if ($info['http_code'] != 200) {
|
|
return false;
|
|
}
|
|
$r = json_decode($response, true);
|
|
if ($r === false || $r === null || !is_array($r)) {
|
|
return false;
|
|
}
|
|
$user = new \Federator\Data\User();
|
|
$user->externalid = $_name;
|
|
$user->iconMediaType = $r['iconMediaType'];
|
|
$user->iconURL = $r['iconURL'];
|
|
$user->imageMediaType = $r['imageMediaType'];
|
|
$user->imageURL = $r['imageURL'];
|
|
$user->name = $r['name'];
|
|
$user->summary = $r['summary'];
|
|
$user->type = $r['type'];
|
|
$user->registered = intval($r['registered'], 10);
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* get remote user by given session
|
|
*
|
|
* @param string $_session session id
|
|
* @param string $_user user or profile name
|
|
* @return \Federator\Data\User | false
|
|
*/
|
|
public function getRemoteUserBySession(string $_session, string $_user)
|
|
{
|
|
// validate $_session and $user
|
|
if (preg_match("/^[a-z0-9]{16}$/", $_session) != 1) {
|
|
return false;
|
|
}
|
|
if (preg_match("/^[a-zA-Z0-9_\-]+$/", $_user) != 1) {
|
|
return false;
|
|
}
|
|
$remoteURL = $this->service . '/api/users/permissions?profile=' . urlencode($_user);
|
|
$headers = ['Cookie: session=' . $_session, 'Accept: application/json'];
|
|
[$response, $info] = \Federator\Main::getFromRemote($remoteURL, $headers);
|
|
|
|
if ($info['http_code'] != 200) {
|
|
return false;
|
|
}
|
|
$r = json_decode($response, true);
|
|
if ($r === false || !is_array($r) || !array_key_exists($_user, $r)) {
|
|
return false;
|
|
}
|
|
$user = $this->getRemoteUserByName($_user);
|
|
if ($user === false) {
|
|
return false;
|
|
}
|
|
// extend with permissions
|
|
$user->permissions = [];
|
|
$user->session = $_session;
|
|
foreach ($r[$_user] as $p) {
|
|
$user->permissions[] = $p;
|
|
}
|
|
return $user;
|
|
}
|
|
}
|
|
|
|
namespace Federator;
|
|
|
|
/**
|
|
* Function to initialize plugin
|
|
*
|
|
* @param \Federator\Main $main main instance
|
|
* @return void
|
|
*/
|
|
function contentnation_load($main)
|
|
{
|
|
$cn = new Connector\ContentNation();
|
|
$main->setConnector($cn);
|
|
}
|