federator/php/federator/main.php
Yannis Vogel d9b02bd95b
fix phan errors
fix lots of phan errors, now we only have phan errors left from temporary implementations. These will be fixed in the next commits as we properly re-integrate and remove temporary-code
2025-04-21 21:06:03 +02:00

326 lines
8 KiB
PHP

<?php
/**
* SPDX-FileCopyrightText: 2024 Sascha Nitsch (grumpydeveloper) https://contentnation.net/@grumpydevelop
* SPDX-License-Identifier: GPL-3.0-or-later
*
* @author Author: Sascha Nitsch (grumpydeveloper)
**/
namespace Federator;
/**
* Base class for Api and related classes
* @author Sascha Nitsch
*/
class Main
{
/**
* cache instance
*
* @var Cache\Cache $cache
*/
protected $cache;
/**
* current config
*
* @var array<string,mixed> $config
*/
protected $config;
/**
* remote connector
*
* @var Connector\Connector $connector
*/
protected $connector = null;
/**
* remote host (f.e. https://contentnation.net)
*
* @var string $host
*/
protected $host = null;
/**
* response content type
*
* @var string $contentType
*/
protected $contentType = "text/html";
/**
* database instance
*
* @var \Mysqli $dbh
*/
protected $dbh;
/**
* extra headers
*
* @var array<string,string> $headers
*/
protected $headers = [];
/**
* redirect URL
*
* @var ?string $redirect
*/
protected $redirect = null;
/**
* response code
*
* @var int $responseCode
*/
protected $responseCode = 200;
/**
* constructor
*
*/
public function __construct()
{
require_once($_SERVER['DOCUMENT_ROOT'] . '../vendor/autoload.php');
$this->responseCode = 200;
$rootDir = $_SERVER['DOCUMENT_ROOT'] . '../';
$config = parse_ini_file($rootDir . 'config.ini', true);
if ($config !== false) {
$this->config = $config;
}
}
/**
* extract parameter from URI
*
* @param string $param
* parameter to extract
* @param string $fallback
* optional fallback
* @return string
*/
public static function extractFromURI($param, $fallback = '')
{
$uri = $_SERVER['REQUEST_URI'];
$params = substr($uri, (int) (strpos($uri, '?') + 1));
$params = explode('&', $params);
foreach ($params as $p) {
$tokens = explode('=', $p);
if ($tokens[0] === $param) {
return urldecode($tokens[1]);
}
}
return $fallback;
}
/**
* do a remote call and return results
* @param string $remoteURL remote URL
* @param string[]|null $headers optional headers to send
* @return array{string, mixed} response and status information
*/
public static function getFromRemote(string $remoteURL, $headers)
{
$ch = curl_init();
if ($ch === false) {
return ['', null];
}
curl_setopt($ch, CURLOPT_URL, $remoteURL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($headers !== null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$ret = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return [$ret, $info];
}
/**
* get cache
*
* @return \Federator\Cache\Cache
*/
public function getCache()
{
return $this->cache;
}
/**
* get connector
*
* @return \Federator\Connector\Connector
*/
public function getConnector()
{
return $this->connector;
}
/**
* get host (f.e. https://contentnation.net)
*
* @return string
*/
public function getHost()
{
return $this->host;
}
/**
* get config
* @return array<string, mixed>
*/
public function getConfig()
{
return $this->config;
}
/**
* get database handle
*
* @return \mysqli database handle
*/
public function getDatabase()
{
return $this->dbh;
}
/**
* load plugins
*/
public function loadPlugins(): void
{
if (array_key_exists('plugins', $this->config)) {
$basepath = $_SERVER['DOCUMENT_ROOT'] . '../plugins/federator/';
$plugins = $this->config['plugins'];
foreach ($plugins as $name => $file) {
require_once($basepath . $file);
$fktn = 'Federator\\' . $name . '_load';
if (function_exists($fktn)) {
$fktn($this);
}
}
}
}
/**
* open database
*
* @param string|null $usernameOverride optional username override
* @param string|null $passwordOverride optional password override
* @return void
*/
public function openDatabase($usernameOverride = null, $passwordOverride = null)
{
$dbconf = $this->config["database"];
$this->dbh = new \mysqli(
$dbconf['host'],
$usernameOverride ?? (string) $dbconf['username'],
$passwordOverride ?? (string) $dbconf['password'],
$dbconf['database']
);
if ($this->dbh->connect_error !== null) {
http_response_code(500);
die('Database Connect Error');
}
}
/**
* render template
*
* @param string $template template file to render
* @param array<string, mixed> $data template variables
* @return string
*/
public function renderTemplate($template, $data)
{
$smarty = new \Smarty\Smarty();
$root = $_SERVER['DOCUMENT_ROOT'];
$smarty->setCompileDir($root . $this->config['templates']['compiledir']);
$smarty->setTemplateDir((string) realpath($root . $this->config['templates']['path']));
$smarty->assign('database', $this->dbh);
$smarty->assign('maininstance', $this);
foreach ($data as $key => $value) {
$smarty->assign($key, $value);
}
return $smarty->fetch($template);
}
/**
* set cache
*
* @param \Federator\Cache\Cache $cache the new cache
*/
public function setCache(Cache\Cache $cache): void
{
$this->cache = $cache;
}
/**
* set connector
*
* @param \Federator\Connector\Connector $connector the new connector
*/
public function setConnector(Connector\Connector $connector) : void
{
if (isset($this->connector)) {
# echo "main::setConnector Setting new connector will override old one.\n"; // TODO CHANGE TO LOG WARNING
}
$this->connector = $connector;
}
/**
* set host
*
* @param string $host the new host url
*/
public function setHost(string $host) : void
{
if (isset($this->host)) {
# echo "main::setHost Setting new host will override old one.\n"; // TODO CHANGE TO LOG WARNING
}
$this->host = $host;
}
/**
* set response code
*
* @param int $code
* new response code
*/
public function setResponseCode(int $code): void
{
$this->responseCode = $code;
}
/**
* translate given group and key using given language
*
* @param ?string $lang
* language to use
* @param string $group
* language group to use
* @param string $key
* language key to use
* @param mixed[] $parameters
* optional parameters
* @return string translation
*/
public static function translate(?string $lang, string $group, string $key, array $parameters = array()): string
{
$l = new Language($lang);
return $l->printlang($group, $key, $parameters);
}
/**
* check if language is valid by loading it
*
* @param ?string $lang
*/
public static function validLanguage(?string $lang): bool
{
$language = new Language($lang);
if ($language->getLang() === $lang) {
return true;
}
return false;
}
}