2022-11-21 22:56:44 +00:00
|
|
|
<?php
|
2022-11-22 14:25:59 +00:00
|
|
|
require 'config/application.php';
|
2022-11-21 23:57:29 +00:00
|
|
|
|
2022-12-03 15:49:40 +00:00
|
|
|
$session = null;
|
|
|
|
if (isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
if (!isset($_COOKIE['_session']) || !file_exists('/tmp/apcontrol-sessions')) {
|
|
|
|
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
|
|
|
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
|
|
|
}
|
|
|
|
header('Location: login.php'); die;
|
2022-11-22 01:22:58 +00:00
|
|
|
}
|
2022-12-03 15:49:40 +00:00
|
|
|
$session = trim($_COOKIE['_session']);
|
2022-12-10 16:25:03 +00:00
|
|
|
$ps_sess = explode('.', $session);
|
2022-12-03 15:49:40 +00:00
|
|
|
$sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions')));
|
2022-12-10 16:25:03 +00:00
|
|
|
if (!in_array($session, $sessions) || !isset($GLOBALS['appconf']['users'][$ps_sess[0]])) {
|
2022-12-03 15:49:40 +00:00
|
|
|
if (substr($_SERVER['REQUEST_URI'],0,5) === '/api/') {
|
|
|
|
http_response_code(403); die('<h3>403, API Forbidden</h3>');
|
|
|
|
}
|
|
|
|
header('Location: login.php'); die;
|
2022-11-22 01:22:58 +00:00
|
|
|
}
|
2022-12-03 15:49:40 +00:00
|
|
|
unset($sessions);
|
2022-12-10 16:25:03 +00:00
|
|
|
unset($ps_sess);
|
2022-11-21 23:57:29 +00:00
|
|
|
}
|
2022-11-22 01:22:58 +00:00
|
|
|
|
2022-12-10 16:19:51 +00:00
|
|
|
function __session_delete($user) {
|
|
|
|
if (!file_exists('/tmp/apcontrol-sessions'))
|
|
|
|
return false;
|
|
|
|
$newsessions = [];
|
|
|
|
$sessions = explode("\n", trim(file_get_contents('/tmp/apcontrol-sessions')));
|
|
|
|
foreach ($sessions as $session) {
|
|
|
|
$ps = explode('.', $session);
|
|
|
|
if ($ps[0] !== $user)
|
|
|
|
$newsessions []= $session;
|
|
|
|
}
|
|
|
|
file_put_contents('/tmp/apcontrol-sessions', implode("\n", $newsessions));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-11-27 20:18:56 +00:00
|
|
|
if (!file_exists($GLOBALS['appconf']['data_dir']))
|
|
|
|
mkdir($GLOBALS['appconf']['data_dir']);
|
2022-11-22 14:25:59 +00:00
|
|
|
|
|
|
|
|
2022-11-22 01:22:58 +00:00
|
|
|
// global variables
|
2022-11-27 23:50:33 +00:00
|
|
|
$GLOBALS['_cache'] = [];
|
2022-11-27 20:18:56 +00:00
|
|
|
$GLOBALS['supported_ap_software'] = [
|
2022-11-22 01:22:58 +00:00
|
|
|
'mastodon',
|
|
|
|
];
|
|
|
|
|
|
|
|
// functions
|
2022-11-22 10:55:58 +00:00
|
|
|
function apiresult($data, $code=200) {
|
2022-11-27 20:18:56 +00:00
|
|
|
if (isset($GLOBALS['IS_PHP']) && $GLOBALS['IS_PHP']) {
|
|
|
|
$GLOBALS['api_data'] = $data;
|
|
|
|
return false;
|
|
|
|
}
|
2022-12-03 16:03:23 +00:00
|
|
|
if (isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
if ($code !== 200) http_response_code($code);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
}
|
2022-12-17 17:07:13 +00:00
|
|
|
|
2022-12-17 21:27:45 +00:00
|
|
|
if (isset($GLOBALS['http_cache_config'])
|
|
|
|
&& gettype($data) !== 'string'
|
|
|
|
&& !isset($data['error'])
|
|
|
|
&& !isset($data['ok']))
|
2022-12-17 17:07:13 +00:00
|
|
|
content_cache__put($GLOBALS['http_cache_config']['key'],
|
|
|
|
'ondemand,'.$GLOBALS['http_cache_config']['seconds'], $data);
|
2022-12-17 21:27:45 +00:00
|
|
|
|
2022-11-28 15:42:58 +00:00
|
|
|
if (gettype($data) !== 'string')
|
|
|
|
echo json_encode($data);
|
|
|
|
else echo $data;
|
|
|
|
die;
|
2022-11-22 01:22:58 +00:00
|
|
|
}
|
2022-11-24 22:41:05 +00:00
|
|
|
|
2022-12-12 19:46:06 +00:00
|
|
|
function no_http() {
|
|
|
|
if (isset($_SERVER['REQUEST_URI']))
|
|
|
|
die('<h3>Forbidden, this method can only be called from shell</h3>');
|
|
|
|
}
|
|
|
|
|
2022-12-08 17:10:18 +00:00
|
|
|
function mod_php($module_name=null) {
|
|
|
|
$null_mod = false;
|
|
|
|
if ($module_name === null) {
|
|
|
|
$null_mod = true;
|
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
|
|
|
|
$fl = preg_replace('#^'.__DIR__.'#', '', $trace[0]['file']);
|
|
|
|
$fl = trim($fl, '/');
|
|
|
|
$module_name = substr($fl, 0, strrpos($fl,'/'));
|
|
|
|
}
|
|
|
|
|
2022-12-08 20:08:23 +00:00
|
|
|
$is_php_set = isset($GLOBALS['IS_PHP']) && $GLOBALS['IS_PHP'];
|
2022-12-08 17:10:18 +00:00
|
|
|
if (!$null_mod) {
|
|
|
|
$GLOBALS['IS_PHP'] = true;
|
|
|
|
unset($GLOBALS['api_data']);
|
|
|
|
}
|
2022-12-08 22:35:45 +00:00
|
|
|
if (!file_exists($module_name.'/mod.php'))
|
|
|
|
return 'does_not_exist';
|
2022-12-05 15:48:31 +00:00
|
|
|
require $module_name.'/mod.php';
|
2022-12-08 17:10:18 +00:00
|
|
|
if (!$null_mod) {
|
2022-12-08 20:08:23 +00:00
|
|
|
if (!$is_php_set)
|
|
|
|
unset($GLOBALS['IS_PHP']);
|
2022-12-08 17:10:18 +00:00
|
|
|
return $GLOBALS['api_data'];
|
|
|
|
}
|
2022-12-05 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 19:46:06 +00:00
|
|
|
function filedb_exists($ns, $file=''){ return file_exists(filedb_getdir($ns,$file)); }
|
|
|
|
function filedb_getdir($ns, $file='') {
|
|
|
|
$dir = $GLOBALS['appconf']['data_dir'];
|
|
|
|
$ns = trim($ns, '/');
|
|
|
|
$dir = realpath($dir.'/'.$ns);
|
|
|
|
if (strpos($dir, $GLOBALS['appconf']['data_dir']) === false)
|
|
|
|
return false;
|
|
|
|
return $dir.'/'.$file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function filedb_put($ns, $file='', $content=null) {
|
|
|
|
$dir = $GLOBALS['appconf']['data_dir'];
|
|
|
|
$ns = trim($ns, '/');
|
|
|
|
foreach (explode('/', $ns) as $d) {
|
|
|
|
$dir .= '/'.$d;
|
|
|
|
if (!file_exists($dir))
|
|
|
|
mkdir($dir);
|
|
|
|
}
|
|
|
|
$dir = realpath($dir);
|
|
|
|
if (strpos($dir, $GLOBALS['appconf']['data_dir']) === false)
|
|
|
|
return false;
|
|
|
|
if ($file === '')
|
|
|
|
return true;
|
|
|
|
$content_file = $dir.'/'.$file;
|
|
|
|
if ($content === null)
|
|
|
|
return touch($content_file);
|
|
|
|
return file_put_contents($content_file, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
function filedb_del($ns, $file='') {
|
|
|
|
$file = filedb_getdir($ns, $file);
|
|
|
|
if ($file === false || !file_exists($file))
|
|
|
|
return false;
|
|
|
|
if (is_dir($file))
|
|
|
|
return rmdir($file);
|
|
|
|
return unlink($file);
|
|
|
|
}
|
|
|
|
|
2022-12-13 11:12:21 +00:00
|
|
|
function filedb_delall($ns) {
|
|
|
|
if (!filedb_exists($ns))
|
|
|
|
return false;
|
|
|
|
foreach (scandir(filedb_getdir($ns)) as $fil) {
|
|
|
|
if (in_array($fil, ['.','..']))
|
|
|
|
continue;
|
|
|
|
filedb_del($ns, $fil);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-12 19:46:06 +00:00
|
|
|
function filedb_get($ns, $file) {
|
|
|
|
$file = filedb_getdir($ns, $file);
|
|
|
|
if ($file === false)
|
|
|
|
return false;
|
|
|
|
if (!file_exists($file))
|
|
|
|
return null;
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
2022-12-07 01:41:59 +00:00
|
|
|
function instance_config($software=null, $instance=null) {
|
|
|
|
if ($software === null) {
|
|
|
|
foreach ($GLOBALS['supported_ap_software'] as $sf) {
|
|
|
|
$result = instance_config($sf);
|
|
|
|
if ($result !== null)
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-11-27 23:50:33 +00:00
|
|
|
if (!in_array($software, $GLOBALS['supported_ap_software']))
|
|
|
|
return null;
|
|
|
|
|
2022-12-01 00:05:56 +00:00
|
|
|
if ($instance === null && isset($_GET['instance']))
|
2022-11-27 23:50:33 +00:00
|
|
|
$instance = trim($_GET['instance']);
|
|
|
|
|
2022-12-01 00:05:56 +00:00
|
|
|
if ($instance === null)
|
|
|
|
return apiresult(['error' => '<instance> GET argument must be provided'], 400);
|
|
|
|
|
2022-12-03 14:59:01 +00:00
|
|
|
$GLOBALS['ap_instance'] = $instance;
|
|
|
|
$GLOBALS['ap_software'] = $software;
|
2022-11-27 23:50:33 +00:00
|
|
|
if (isset($GLOBALS['_cache'][$software.$instance]))
|
|
|
|
return $GLOBALS['_cache'][$software.$instance];
|
|
|
|
|
2022-12-08 17:10:18 +00:00
|
|
|
mod_php('api/v1/config/get');
|
2022-11-27 23:50:33 +00:00
|
|
|
if (!isset($GLOBALS['api_data']))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
$found = false;
|
|
|
|
foreach ($GLOBALS['api_data']['hosts'][$software] as $ins_cfg) {
|
|
|
|
if ($ins_cfg['instance'] === $instance) {
|
|
|
|
$found = $ins_cfg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($found === false)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
$config_raw = explode("\n", trim($found['config']));
|
|
|
|
foreach ($config_raw as $ln) {
|
|
|
|
$k = substr($ln, 0, strpos($ln,'='));
|
|
|
|
$v = substr($ln, strpos($ln,'=')+1);
|
|
|
|
$config[$k] = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
$GLOBALS['_cache'][$software.$instance] = $config;
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function instance_config_require($software, $instance=null) {
|
|
|
|
$config = instance_config($software, $instance);
|
|
|
|
if ($config === null)
|
|
|
|
apiresult(['error' => 'instance parameter is incorrect. Does not exist'], 400);
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function instance_http_get($url) { return instance_http_request($url, 'GET'); }
|
|
|
|
function instance_http_post($url, $data=null) { return instance_http_request($url, 'POST', $data); }
|
2022-12-08 20:21:57 +00:00
|
|
|
function instance_http_delete($url) { return instance_http_request($url, 'DELETE'); }
|
2022-11-27 23:50:33 +00:00
|
|
|
function instance_http_request($url, $method, $data=null) {
|
2022-12-08 22:35:45 +00:00
|
|
|
$software = null;
|
|
|
|
if (isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
$huri = substr(trim($_SERVER['REQUEST_URI']), 7);
|
|
|
|
$ps = explode('/', trim($huri, '/'));
|
|
|
|
if ($ps[0] !== 'http' || !in_array($ps[1], $GLOBALS['supported_ap_software']))
|
|
|
|
return apiresult(['error' => 'this method can only be called from api/v1/http/<software> URIs'], 500);
|
|
|
|
$software = $ps[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($software === null && isset($GLOBALS['ap_software']))
|
|
|
|
$software = $GLOBALS['ap_software'];
|
2022-11-27 23:50:33 +00:00
|
|
|
|
|
|
|
$config = instance_config($software);
|
|
|
|
$url = $config['instance_url'].$url;
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
2022-12-08 20:21:57 +00:00
|
|
|
if ($method === 'POST' || $method === 'DELETE') {
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
2022-11-27 23:50:33 +00:00
|
|
|
if ($data !== null)
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
|
}
|
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
if (isset($config['api_authkey'])) {
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
|
|
'Authorization: Bearer '.$config['api_authkey'],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function valid_mastodon_account_id($id) {
|
|
|
|
return preg_match('/^\d+$/', strval($id));
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:42:53 +00:00
|
|
|
function valid_for_path($str) {
|
|
|
|
return strpos($str, '/') === false;
|
|
|
|
}
|
|
|
|
|
2022-12-07 01:41:59 +00:00
|
|
|
function valid_uuid($str) {
|
|
|
|
return preg_match('/^[a-z0-9\-]+$/', trim($str));
|
|
|
|
}
|
|
|
|
|
2022-12-07 00:15:39 +00:00
|
|
|
// source: https://stackoverflow.com/questions/1755144/how-to-validate-domain-name-in-php
|
|
|
|
function valid_domain_name($domain_name)
|
|
|
|
{
|
|
|
|
return (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $domain_name)
|
|
|
|
&& preg_match("/^.{1,253}$/", $domain_name)
|
|
|
|
&& preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $domain_name));
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:05:56 +00:00
|
|
|
function remove_accents($string) {
|
|
|
|
if ( !preg_match('/[\x80-\xff]/', $string) )
|
|
|
|
return $string;
|
|
|
|
|
|
|
|
$chars = array(
|
|
|
|
// Decompositions for Latin-1 Supplement
|
|
|
|
chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
|
|
|
|
chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
|
|
|
|
chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
|
|
|
|
chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
|
|
|
|
chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
|
|
|
|
chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
|
|
|
|
chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
|
|
|
|
chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
|
|
|
|
chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
|
|
|
|
chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
|
|
|
|
chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
|
|
|
|
chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
|
|
|
|
chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
|
|
|
|
chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
|
|
|
|
chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
|
|
|
|
chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
|
|
|
|
chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
|
|
|
|
chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
|
|
|
|
chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
|
|
|
|
chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
|
|
|
|
chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
|
|
|
|
chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
|
|
|
|
chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
|
|
|
|
chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
|
|
|
|
chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
|
|
|
|
chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
|
|
|
|
chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
|
|
|
|
chr(195).chr(191) => 'y',
|
|
|
|
// Decompositions for Latin Extended-A
|
|
|
|
chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
|
|
|
|
chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
|
|
|
|
chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
|
|
|
|
chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
|
|
|
|
chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
|
|
|
|
chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
|
|
|
|
chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
|
|
|
|
chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
|
|
|
|
chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
|
|
|
|
chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
|
|
|
|
chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
|
|
|
|
chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
|
|
|
|
chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
|
|
|
|
chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
|
|
|
|
chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
|
|
|
|
chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
|
|
|
|
chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
|
|
|
|
chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
|
|
|
|
chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
|
|
|
|
chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
|
|
|
|
chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
|
|
|
|
chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
|
|
|
|
chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
|
|
|
|
chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
|
|
|
|
chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
|
|
|
|
chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
|
|
|
|
chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
|
|
|
|
chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
|
|
|
|
chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
|
|
|
|
chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
|
|
|
|
chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
|
|
|
|
chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
|
|
|
|
chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
|
|
|
|
chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
|
|
|
|
chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
|
|
|
|
chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
|
|
|
|
chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
|
|
|
|
chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
|
|
|
|
chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
|
|
|
|
chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
|
|
|
|
chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
|
|
|
|
chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
|
|
|
|
chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
|
|
|
|
chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
|
|
|
|
chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
|
|
|
|
chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
|
|
|
|
chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
|
|
|
|
chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
|
|
|
|
chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
|
|
|
|
chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
|
|
|
|
chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
|
|
|
|
chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
|
|
|
|
chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
|
|
|
|
chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
|
|
|
|
chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
|
|
|
|
chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
|
|
|
|
chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
|
|
|
|
chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
|
|
|
|
chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
|
|
|
|
chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
|
|
|
|
chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
|
|
|
|
chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
|
|
|
|
chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
|
|
|
|
chr(197).chr(190) => 'z', chr(197).chr(191) => 's'
|
|
|
|
);
|
|
|
|
|
|
|
|
$string = strtr($string, $chars);
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!function_exists('str_starts_with')) {
|
|
|
|
function str_starts_with($haystack, $needle) {
|
|
|
|
return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-16 02:36:14 +00:00
|
|
|
function set_chat_at($str, $i, $c) {
|
|
|
|
return substr($str, 0, $i).$c.substr($str, $i+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalize_word_sound($word, $cback=null) {
|
|
|
|
$hashes = [
|
|
|
|
'4:a', '3:e', '1:i', '0:o',
|
|
|
|
'5:s', 'b:v', '8:b', 'k:c',
|
|
|
|
'y:i', 'que:kee', 'q:k',
|
|
|
|
];
|
|
|
|
foreach ($hashes as $hash) {
|
|
|
|
$hash = explode(':', $hash);
|
|
|
|
if ($cback !== null)
|
|
|
|
$word = $cback($word, $hash);
|
|
|
|
else $word = str_replace($hash[0], $hash[1], $word);
|
|
|
|
}
|
|
|
|
return $word;
|
|
|
|
}
|
|
|
|
|
2022-12-01 00:05:56 +00:00
|
|
|
function normalize_for_search($str) {
|
|
|
|
if (trim($str) === '') return '';
|
2022-12-17 21:05:44 +00:00
|
|
|
$str = trim(strip_tags(str_replace('>', '> ', $str)));
|
2022-12-16 02:36:14 +00:00
|
|
|
$str = strtolower(remove_accents($str));
|
2022-12-15 02:23:55 +00:00
|
|
|
$str = preg_replace('/[^a-z0-9]+/', ' ', $str);
|
|
|
|
$str = preg_replace('/\s+/', ' ', $str);
|
|
|
|
$words = explode(' ', trim($str));
|
2022-12-01 00:05:56 +00:00
|
|
|
$newwords = [];
|
|
|
|
foreach ($words as $word) {
|
|
|
|
if (trim($word) === '')
|
|
|
|
continue;
|
2022-12-16 02:36:14 +00:00
|
|
|
$word = normalize_word_sound($word);
|
2022-12-01 00:05:56 +00:00
|
|
|
$nword = '';
|
2022-12-17 21:41:46 +00:00
|
|
|
if (strlen($word) <= 3) {
|
|
|
|
$newwords [] = $word;
|
|
|
|
continue;
|
|
|
|
}
|
2022-12-01 00:05:56 +00:00
|
|
|
for ($i = 0; $i < strlen($word); $i++) {
|
|
|
|
if ($i === 0) {
|
|
|
|
$nword .= $word[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($word[$i] !== $word[$i-1]) {
|
|
|
|
$nword .= $word[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$newwords [] = $nword;
|
|
|
|
}
|
|
|
|
return trim(implode(' ', $newwords));
|
|
|
|
}
|
|
|
|
|
2022-12-11 15:46:41 +00:00
|
|
|
function parse_comparing_expression($expr) {
|
2022-12-15 02:23:55 +00:00
|
|
|
$expr = preg_replace('/\n/', ' ', $expr);
|
|
|
|
$expr = preg_replace('/\s+/', ' ', $expr);
|
2022-12-16 02:36:14 +00:00
|
|
|
$p_expr = $expr;
|
|
|
|
$quot = false;
|
|
|
|
for ($i = 0; $i < strlen($p_expr); $i++) {
|
|
|
|
if ($p_expr[$i] === '"') {
|
|
|
|
$quot = !$quot;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($quot) {
|
|
|
|
if ($p_expr[$i] === '(')
|
|
|
|
$p_expr = set_chat_at($p_expr, $i, 'º');
|
|
|
|
else if ($p_expr[$i] === ')')
|
|
|
|
$p_expr = set_chat_at($p_expr, $i, 'ª');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$p_expr = preg_replace('/\(|\)/', ' ', $p_expr);
|
2022-12-15 02:23:55 +00:00
|
|
|
$p_expr = preg_replace('/\s+/', ' ', $p_expr);
|
|
|
|
$p_expr = preg_split('/OR|AND(\sNOT)?/', $p_expr);
|
2022-12-16 02:36:14 +00:00
|
|
|
$p_expr = str_replace('º', '(', $p_expr);
|
|
|
|
$p_expr = str_replace('ª', ')', $p_expr);
|
2022-12-15 02:23:55 +00:00
|
|
|
foreach ($p_expr as &$e) $e = trim($e);
|
2022-12-11 15:46:41 +00:00
|
|
|
return [
|
|
|
|
'original' => $expr,
|
2022-12-15 02:23:55 +00:00
|
|
|
'parsed' => $p_expr,
|
2022-12-11 15:46:41 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-12-17 01:52:48 +00:00
|
|
|
function matches_comparing_expression($expr, $text, $debug=false) {
|
2022-12-11 15:46:41 +00:00
|
|
|
if (gettype($expr) === 'string')
|
|
|
|
$expr = parse_comparing_expression($expr);
|
|
|
|
$result = $expr['original'];
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<h2>Original</h2>';
|
|
|
|
echo '<pre>'.$text.'</pre>';
|
|
|
|
}
|
2022-12-16 02:36:14 +00:00
|
|
|
$text = normalize_for_search($text);
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<h2>Normalized</h2>';
|
|
|
|
echo '<pre>'.$text.'</pre>';
|
|
|
|
echo '<br><br>';
|
|
|
|
}
|
2022-12-11 18:32:50 +00:00
|
|
|
$text_words = explode(' ', $text);
|
2022-12-15 02:23:55 +00:00
|
|
|
foreach ($expr['parsed'] as $t)
|
|
|
|
{
|
|
|
|
$bool = false;
|
|
|
|
$t = trim($t);
|
|
|
|
$selector = substr($t, 0, strpos($t, ' '));
|
|
|
|
$content = substr($t, strpos($t, ' ')+1);
|
|
|
|
$content = trim(trim(trim($content), '"'));
|
|
|
|
switch ($selector)
|
|
|
|
{
|
|
|
|
case 'words':
|
|
|
|
case 'hasall':
|
|
|
|
$ws = explode(' ', $content);
|
|
|
|
$hasall = true;
|
2022-12-11 15:46:41 +00:00
|
|
|
foreach ($ws as $w) {
|
2022-12-11 18:32:50 +00:00
|
|
|
if (!in_array(normalize_for_search($w), $text_words)) {
|
2022-12-15 02:23:55 +00:00
|
|
|
$hasall = false;
|
2022-12-11 15:46:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
$bool = $hasall;
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br><br>';
|
|
|
|
echo '<h3>words</h3>';
|
|
|
|
echo '<pre>';
|
|
|
|
echo $content;
|
|
|
|
echo '</pre>';
|
|
|
|
echo '<br>Result: ';
|
|
|
|
var_dump($bool);
|
|
|
|
echo '<br>';
|
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'anyword':
|
|
|
|
case 'hasany':
|
|
|
|
$ws = explode(' ', $content);
|
|
|
|
$hasany = false;
|
|
|
|
foreach ($ws as $w) {
|
|
|
|
if (in_array(normalize_for_search($w), $text_words)) {
|
|
|
|
$hasany = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$bool = $hasany;
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br><br>';
|
|
|
|
echo '<h3>hasany</h3>';
|
|
|
|
echo '<pre>';
|
|
|
|
echo $content;
|
|
|
|
echo '</pre>';
|
|
|
|
echo '<br>Result: ';
|
|
|
|
var_dump($bool);
|
|
|
|
echo '<br>';
|
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'has':
|
|
|
|
case 'includes':
|
|
|
|
case 'contains':
|
2022-12-15 13:12:39 +00:00
|
|
|
$a = str_replace(' ', '', $text);
|
|
|
|
$b = str_replace(' ', '', normalize_for_search($content));
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br><br>';
|
|
|
|
echo '<h3>contains</h3>';
|
|
|
|
echo '<pre>';
|
|
|
|
echo $content;
|
|
|
|
echo '</pre>';
|
|
|
|
}
|
2022-12-15 13:12:39 +00:00
|
|
|
$bool = strpos($a, $b) !== false;
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br>Result: ';
|
|
|
|
var_dump($bool);
|
|
|
|
echo '<br>';
|
|
|
|
}
|
2022-12-15 13:12:39 +00:00
|
|
|
unset($a); unset($b);
|
2022-12-15 02:23:55 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'regex':
|
|
|
|
case 'matches':
|
|
|
|
if (strlen($content) <= 0)
|
|
|
|
continue 2;
|
2022-12-16 02:36:14 +00:00
|
|
|
$content = normalize_word_sound($content, function($w, $h) {
|
|
|
|
if ($h[0] === 'b')
|
|
|
|
return preg_replace('/([^\\\])b/', '\1'.$h[1], $w);
|
|
|
|
if (preg_match('/^\d$/', $h[0])) {
|
|
|
|
$b = false;
|
|
|
|
for ($i = 0; $i < strlen($w); $i++) {
|
|
|
|
if (in_array($w[$i], ['{','}','[',']'])) {
|
|
|
|
$b = !$b;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!$b && $w[$i] === $h[0])
|
|
|
|
$w = set_chat_at($w, $i, $h[1]);
|
|
|
|
}
|
|
|
|
return $w;
|
|
|
|
}
|
|
|
|
return str_replace($h[0], $h[1], $w);
|
|
|
|
});
|
2022-12-17 21:41:46 +00:00
|
|
|
preg_match_all('/\w{4,}/', $content, $mts);
|
|
|
|
foreach ((isset($mts[0]) ? $mts[0] : []) as $m)
|
|
|
|
$content = preg_replace('/\b'.$m.'\b/', normalize_for_search($m), $content);
|
2022-12-15 02:23:55 +00:00
|
|
|
if ($content[0] != '^')
|
|
|
|
$content = '^.*'.$content;
|
|
|
|
if ($content[strlen($content)-1] != '$')
|
|
|
|
$content .= '.*$';
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br><br>';
|
|
|
|
echo '<h3>regex</h3>';
|
|
|
|
echo '<pre>';
|
|
|
|
echo $content;
|
|
|
|
echo '</pre>';
|
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
$bool = preg_match('#'.$content.'#', $text) === 1;
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br>Result: ';
|
|
|
|
var_dump($bool);
|
|
|
|
echo '<br>';
|
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
break;
|
|
|
|
|
2022-12-11 15:46:41 +00:00
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
$result = str_replace($t, $bool ? 'true' : 'false', $result);
|
2022-12-11 15:46:41 +00:00
|
|
|
}
|
2022-12-15 02:23:55 +00:00
|
|
|
$result = str_replace('OR', '||', $result);
|
|
|
|
$result = str_replace('AND', '&&', $result);
|
|
|
|
$result = str_replace('NOT ', '!', $result);
|
|
|
|
$result = str_replace('NOT', '!', $result);
|
2022-12-12 22:18:49 +00:00
|
|
|
try {
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br><hr>';
|
|
|
|
echo '<h3>Final result</h3>';
|
|
|
|
echo '<br>eval: ';
|
|
|
|
var_dump($result);
|
|
|
|
}
|
2022-12-12 22:18:49 +00:00
|
|
|
eval('$result = '.$result.';');
|
2022-12-17 01:52:48 +00:00
|
|
|
if ($debug) {
|
|
|
|
echo '<br>';
|
|
|
|
echo '<br>result: ';
|
|
|
|
var_dump($result);
|
|
|
|
die;
|
|
|
|
}
|
2022-12-12 22:18:49 +00:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
return apiresult(['error' => 'Query contains errors, please correct it and try again.']);
|
|
|
|
}
|
2022-12-11 16:11:41 +00:00
|
|
|
return in_array($result, [0,false]) ? false : true;
|
2022-12-05 12:35:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-17 17:07:13 +00:00
|
|
|
function http_cache($seconds, $data=null) {
|
|
|
|
if ($data === null)
|
|
|
|
$data = $_GET;
|
|
|
|
$cache_key = sha1('http_cache'.json_encode($data));
|
|
|
|
$cache = content_cache__get($cache_key);
|
|
|
|
if ($cache !== null)
|
|
|
|
return apiresult($cache);
|
|
|
|
$GLOBALS['http_cache_config'] = [
|
|
|
|
'seconds' => $seconds,
|
|
|
|
'key' => $cache_key
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-12-13 15:37:48 +00:00
|
|
|
function _cachekey_construct(...$keys) {
|
|
|
|
$nkeys = [];
|
|
|
|
foreach ($keys as $key)
|
|
|
|
$nkeys []= _cachekey($key);
|
|
|
|
return implode(',', $nkeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _cachekey($str) {
|
|
|
|
return preg_replace('/[^a-z0-9\_]+/', '', strtolower($str));
|
|
|
|
}
|
|
|
|
|
2022-12-02 15:53:24 +00:00
|
|
|
function content_cache__exists($key) {
|
|
|
|
$cache_dir = $GLOBALS['appconf']['data_dir'].'/cache';
|
|
|
|
if (!file_exists($cache_dir)) {
|
|
|
|
mkdir($cache_dir);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$key = md5($key);
|
|
|
|
$cache_file = null;
|
|
|
|
foreach (scandir($cache_dir) as $fil) {
|
|
|
|
if (strpos($fil, $key) !== false) {
|
|
|
|
$cache_file = $cache_dir.'/'.$fil;
|
|
|
|
$ps = explode(',',$fil);
|
|
|
|
array_splice($ps, 0, 1);
|
2022-12-03 16:05:38 +00:00
|
|
|
if ($ps[0] !== 'always' && count($ps) > 1)
|
|
|
|
$ps[1] = intval($ps[1]);
|
2022-12-02 15:53:24 +00:00
|
|
|
return (object)[
|
|
|
|
'file' => $cache_file,
|
|
|
|
'config' => $ps
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function content_cache__get($key) {
|
|
|
|
$cfg = content_cache__exists($key);
|
|
|
|
if ($cfg === false)
|
|
|
|
return null;
|
|
|
|
$ms = time() - filemtime($cfg->file);
|
|
|
|
if ($cfg->config[0] === 'ondemand' &&
|
|
|
|
$ms > $cfg->config[1]) {
|
|
|
|
unlink($cfg->file);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return unserialize(file_get_contents($cfg->file));
|
|
|
|
}
|
|
|
|
|
|
|
|
function content_cache__put($key, $config, $data) {
|
|
|
|
$cache_dir = $GLOBALS['appconf']['data_dir'].'/cache';
|
|
|
|
if (!file_exists($cache_dir))
|
|
|
|
mkdir($cache_dir);
|
2022-12-03 15:49:40 +00:00
|
|
|
$ps = explode(',', trim($config));
|
2022-12-02 15:53:24 +00:00
|
|
|
$cache_file = $cache_dir.'/'.md5($key);
|
2022-12-03 15:49:40 +00:00
|
|
|
if ($ps[0] === 'always')
|
|
|
|
$cache_file .= ','.$ps[0];
|
|
|
|
else if ($ps[0] === 'ondemand')
|
2022-12-06 14:31:16 +00:00
|
|
|
$cache_file .= ','.$config;
|
2022-12-13 15:37:48 +00:00
|
|
|
return file_put_contents($cache_file, serialize($data));
|
2022-12-02 15:53:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 14:59:01 +00:00
|
|
|
function cronjob_db_create__check_cachecfg($software, $instance, $sql, $cache=null) {
|
|
|
|
if ($cache === null)
|
|
|
|
return false;
|
|
|
|
$ps = explode(',',trim($cache));
|
|
|
|
if ($ps[0] !== 'always')
|
|
|
|
return false;
|
|
|
|
return cronjob_db_create($software, $instance, $sql, intval($ps[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
function cronjob_db_create($software, $instance, $sql, $time=3600) {
|
|
|
|
$job_key = md5($software.$instance.$sql);
|
|
|
|
$dir_crons = $GLOBALS['appconf']['data_dir'].'/cron';
|
|
|
|
if (!file_exists($dir_crons))
|
|
|
|
mkdir($dir_crons);
|
|
|
|
$dir_crons_db = $dir_crons.'/db';
|
|
|
|
if (!file_exists($dir_crons_db))
|
|
|
|
mkdir($dir_crons_db);
|
|
|
|
$cron_file = $software.','.$instance.','.$job_key;
|
2022-12-15 02:23:55 +00:00
|
|
|
$result_file = $dir_crons_db.'/'.$cron_file.','.$time;
|
|
|
|
$touch_1970 = !file_exists($result_file);
|
2022-12-03 14:59:01 +00:00
|
|
|
foreach (scandir($dir_crons_db) as $fl) {
|
|
|
|
if (strpos($fl, $cron_file) !== false)
|
|
|
|
unlink($dir_crons_db.'/'.$fl);
|
|
|
|
}
|
2022-12-11 17:01:03 +00:00
|
|
|
file_put_contents($result_file, $sql);
|
|
|
|
if ($touch_1970) touch($result_file, 1000);
|
2022-12-03 14:59:01 +00:00
|
|
|
return $cron_file;
|
|
|
|
}
|
|
|
|
|
2022-11-24 22:41:05 +00:00
|
|
|
// classes
|
|
|
|
require 'classes/PgDatabase.php';
|
2022-12-12 12:51:20 +00:00
|
|
|
require 'classes/RedisDatabase.php';
|