Files
Kleeja/includes/functions.php

1349 lines
36 KiB
PHP
Raw Normal View History

2018-01-09 02:09:07 +03:00
<?php
/**
*
* @package Kleeja
2020-04-11 22:45:48 +02:00
* @copyright (c) 2007 Kleeja.net
2018-01-09 02:09:07 +03:00
* @license ./docs/license.txt
*
*/
//no for directly open
2019-05-03 23:52:08 +03:00
if (! defined('IN_COMMON'))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
exit();
2018-01-09 02:09:07 +03:00
}
/**
* Detect a bot activity an record it
*/
function kleeja_detecting_bots()
{
global $SQL, $dbprefix, $config;
2019-05-03 23:52:08 +03:00
// get information ..
$agent = $SQL->escape($_SERVER['HTTP_USER_AGENT'] ?? '');
2019-05-18 01:47:17 +03:00
$time = time();
2019-05-03 23:52:08 +03:00
//for stats
if (strpos($agent, 'Google') !== false)
{
$update_query = [
2019-05-18 01:47:17 +03:00
'UPDATE' => "{$dbprefix}stats",
'SET' => "last_google=$time, google_num=google_num+1"
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_update_google_lst_num', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
}
elseif (strpos($agent, 'Bing') !== false)
{
$update_query = [
2019-05-18 01:47:17 +03:00
'UPDATE' => "{$dbprefix}stats",
'SET' => "last_bing=$time, bing_num=bing_num+1"
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_update_bing_lst_num', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//put another bots as a hook if you want !
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('anotherbots_onlline_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
//clean online table
if ((time() - $config['last_online_time_update']) >= 3600)
{
//what to add here ?
//update last_online_time_update
update_config('last_online_time_update', time());
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('KleejaOnline_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
}
/**
* Ban system
*/
function get_ban()
{
2019-05-18 20:55:28 +03:00
global $banss, $lang, $SQL, $usrcp;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//visitor ip now
2019-05-25 00:30:55 +03:00
$ip = get_ip();
2019-05-18 20:55:28 +03:00
$username = $usrcp->name();
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//now .. loop for banned ips
2019-05-18 20:55:28 +03:00
if (is_array($banss) && (! empty($ip) || ! empty($username)))
2019-05-03 23:52:08 +03:00
{
2019-05-18 20:55:28 +03:00
foreach ($banss as $banned_item)
2019-05-03 23:52:08 +03:00
{
2019-05-18 20:55:28 +03:00
$banned_item = trim($banned_item);
2018-01-09 02:09:07 +03:00
2019-05-18 20:55:28 +03:00
if (empty($banned_item))
2019-05-03 23:52:08 +03:00
{
continue;
}
2018-01-09 02:09:07 +03:00
2019-05-18 20:55:28 +03:00
$is_banned = false;
2019-05-03 23:52:08 +03:00
//first .. replace all * with something good .
2018-01-09 02:09:07 +03:00
2019-05-18 20:55:28 +03:00
if (! empty($ip) && strpos($banned_item, '.') !== false)
{
$replace_it = str_replace('*', '([0-9]{1,3})', $banned_item);
$replace_it = str_replace('.', '\.', $replace_it);
2019-05-25 00:30:55 +03:00
$is_banned = $ip == $banned_item || @preg_match('/' . preg_quote($replace_it, '/') . '/i', $ip);
2019-05-18 20:55:28 +03:00
}
2019-05-25 00:30:55 +03:00
elseif (! empty($username) && $banned_item == $username)
2019-05-18 20:55:28 +03:00
{
$is_banned = true;
}
if ($is_banned)
2019-05-03 23:52:08 +03:00
{
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('banned_get_ban_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
//
// if the request is an image
//
2019-05-03 23:52:08 +03:00
if (
2019-05-05 20:12:32 +03:00
(defined('IN_DOWNLOAD') && (ig('img') || ig('thmb') || ig('thmbf') || ig('imgf')))
2018-01-09 02:09:07 +03:00
|| g('go', 'str', '') == 'queue'
2019-05-03 23:52:08 +03:00
) {
2018-01-09 02:09:07 +03:00
@$SQL->close();
2019-05-03 23:52:08 +03:00
$fullname = 'images/banned_user.jpg';
2018-01-09 02:09:07 +03:00
$filesize = filesize($fullname);
header("Content-length: $filesize");
2019-05-03 23:52:08 +03:00
header('Content-type: image/jpg');
2018-01-09 02:09:07 +03:00
readfile($fullname);
2019-05-03 23:52:08 +03:00
2018-01-09 02:09:07 +03:00
exit;
}
else
{
kleeja_info($lang['U_R_BANNED'], $lang['U_R_BANNED'], true);
}
2019-05-03 23:52:08 +03:00
}
}
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('get_ban_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
}
/**
* Check if the given plugin installed ?
* @param $plugin_name
2018-01-09 02:09:07 +03:00
* @return bool
*/
function kleeja_plugin_exists($plugin_name)
{
2019-05-03 23:52:08 +03:00
global $SQL, $dbprefix;
$query = [
2019-05-18 01:47:17 +03:00
'SELECT' => 'p.plg_id',
'FROM' => "{$dbprefix}plugins p",
'WHERE' => "p.plg_name = '" . $SQL->escape($plugin_name) . "'",
2019-05-03 23:52:08 +03:00
];
2019-05-18 01:47:17 +03:00
$result = $SQL->build($query);
$num = $SQL->num_rows($result);
2019-05-03 23:52:08 +03:00
if ($num)
{
$d = $SQL->fetch($result);
$SQL->freeresult();
return $d['plg_id'];
}
return false;
2018-01-09 02:09:07 +03:00
}
/**
* Return current page url
*/
function kleeja_get_page()
{
2019-05-03 23:52:08 +03:00
if (isset($_SERVER['REQUEST_URI']))
{
$location = $_SERVER['REQUEST_URI'];
}
elseif (isset($_ENV['REQUEST_URI']))
{
2018-01-09 02:09:07 +03:00
$location = $_ENV['REQUEST_URI'];
2019-05-03 23:52:08 +03:00
}
else
{
if (isset($_SERVER['PATH_INFO']))
{
$location = $_SERVER['PATH_INFO'];
}
elseif (isset($_ENV['PATH_INFO']))
{
$location = $_SERVER['PATH_INFO'];
}
elseif (isset($_ENV['PHP_SELF']))
{
$location = $_ENV['PHP_SELF'];
}
else
{
$location = $_SERVER['PHP_SELF'];
}
if (isset($_SERVER['QUERY_STRING']))
{
$location .= '?' . $_SERVER['QUERY_STRING'];
}
elseif (isset($_ENV['QUERY_STRING']))
{
$location = '?' . $_ENV['QUERY_STRING'];
}
}
$return = str_replace(['&amp;'], ['&'], htmlspecialchars($location));
return $return;
2018-01-09 02:09:07 +03:00
}
/**
* Fix email string to be UTF8
* @param $text
2018-01-09 02:09:07 +03:00
* @return string
*/
function _sm_mk_utf8($text)
{
2019-05-22 19:33:52 +03:00
return '=?UTF-8?B?' . base64_encode($text) . '?=';
2018-01-09 02:09:07 +03:00
}
/**
* Send an email message
2019-05-03 23:52:08 +03:00
* @param string $to
* @param string $body
* @param string $subject
* @param string $fromAddress
* @param string $fromName
* @param string $bcc
2018-01-09 02:09:07 +03:00
* @return bool
*/
function send_mail($to, $body, $subject, $fromAddress, $fromName, $bcc = '')
{
2019-05-03 23:52:08 +03:00
$eol = "\r\n";
$headers = '';
2018-01-09 02:09:07 +03:00
$headers .= 'From: ' . _sm_mk_utf8(trim(preg_replace('#[\n\r:]+#s', '', $fromName))) . ' <' . trim(preg_replace('#[\n\r:]+#s', '', $fromAddress)) . '>' . $eol;
2019-05-03 23:52:08 +03:00
$headers .= 'MIME-Version: 1.0' . $eol;
$headers .= 'Content-transfer-encoding: 8bit' . $eol; // 7bit
$headers .= 'Content-Type: text/plain; charset=utf-8' . $eol; // format=flowed
$headers .= 'X-Mailer: Kleeja Mailer' . $eol;
2018-01-09 02:09:07 +03:00
$headers .= 'Reply-To: ' . _sm_mk_utf8(trim(preg_replace('#[\n\r:]+#s', '', $fromName))) . ' <' . trim(preg_replace('#[\n\r:]+#s', '', $fromAddress)) . '>' . $eol;
2019-05-03 23:52:08 +03:00
if (! empty($bcc))
{
2018-01-09 02:09:07 +03:00
$headers .= 'Bcc: ' . trim(preg_replace('#[\n\r:]+#s', '', $bcc)) . $eol;
}
is_array($plugin_run_result = Plugins::getInstance()->run('kleeja_send_mail', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$body = str_replace(["\n", "\0"], ["\r\n", ''], $body);
2018-01-09 02:09:07 +03:00
// Change the line breaks used in the headers according to OS
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN' && version_compare(PHP_VERSION, '8.0.0', '<'))
2019-05-03 23:52:08 +03:00
{
$headers = str_replace("\r\n", "\n", $headers);
}
$mail_sent = @mail(trim(preg_replace('#[\n\r]+#s', '', $to)), _sm_mk_utf8(trim(preg_replace('#[\n\r]+#s', '', $subject))), $body, $headers);
return $mail_sent;
2018-01-09 02:09:07 +03:00
}
/**
* Delete cache
2019-05-03 23:52:08 +03:00
* @param string $name
* @param bool $all if true, all cache in cache folder will be deleted
2018-01-09 02:09:07 +03:00
* @return bool
*/
function delete_cache($name, $all=false)
{
2019-05-03 23:52:08 +03:00
//Those files are exceptions and not for deletion
$exceptions = ['.htaccess', 'index.html', 'php.ini', 'web.config'];
2018-12-13 00:17:58 +03:00
2019-05-03 23:52:08 +03:00
//ignore kleeja_log in dev stage.
if (defined('DEV_STAGE'))
{
array_push($exceptions, 'kleeja_log.log');
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('delete_cache_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//handle array of cached files
if (is_array($name))
{
foreach ($name as $n)
{
delete_cache($n, false);
}
return true;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$path_to_cache = PATH . 'cache';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($all)
{
2018-01-09 02:09:07 +03:00
$del = true;
2019-05-03 23:52:08 +03:00
if ($dh = @opendir($path_to_cache))
{
while (($file = @readdir($dh)) !== false)
{
if ($file != '.' && $file != '..' && ! in_array($file, $exceptions))
{
2018-01-09 02:09:07 +03:00
kleeja_unlink($path_to_cache . '/' . $file, true);
2019-05-03 23:52:08 +03:00
}
}
@closedir($dh);
}
}
else
{
if (strpos($name, 'tpl_') !== false && strpos($name, '.html') !== false)
{
$name = str_replace('.html', '', $name);
}
$del = true;
$name = str_replace('.php', '', $name) . '.php';
if (file_exists($path_to_cache . '/' . $name))
{
2019-05-05 20:12:32 +03:00
$del = kleeja_unlink($path_to_cache . '/' . $name, true);
2019-05-03 23:52:08 +03:00
}
}
return $del;
2018-01-09 02:09:07 +03:00
}
/**
* Try delete files or at least change its name.
* for those who have dirty hosting
2019-05-03 23:52:08 +03:00
* @param string $filePath
* @param bool $cache_file
2018-01-09 02:09:07 +03:00
* @return bool
*/
function kleeja_unlink($filePath, $cache_file = false)
{
$return = false;
is_array($plugin_run_result = Plugins::getInstance()->run('kleeja_unlink_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
if ($return)
2018-01-09 02:09:07 +03:00
{
return true;
}
//99.9% who use this
2019-05-03 23:52:08 +03:00
if (function_exists('unlink'))
{
2019-05-05 20:12:32 +03:00
if (is_dir($filePath))
{
2019-05-08 03:53:46 +03:00
$it = new RecursiveDirectoryIterator($filePath, RecursiveDirectoryIterator::SKIP_DOTS);
2019-05-05 20:12:32 +03:00
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file)
{
2019-05-08 03:53:46 +03:00
if ($file->isDir())
2019-05-05 20:12:32 +03:00
{
rmdir($file->getPathname());
}
else
{
unlink($file->getPathname());
}
}
2019-05-08 22:14:28 +03:00
return rmdir($filePath);
2019-05-05 20:12:32 +03:00
}
else
{
return unlink($filePath);
}
2019-05-03 23:52:08 +03:00
}
//just rename cache file if there is new thing
elseif (function_exists('rename') && $cache_file)
{
2018-01-09 02:09:07 +03:00
$new_name = substr($filePath, 0, strrpos($filePath, '/') + 1) . 'old_' . md5($filePath . time()) . '.php';
return rename($filePath, $new_name);
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return false;
2018-01-09 02:09:07 +03:00
}
/**
* Get mime header
2019-05-03 23:52:08 +03:00
* @param string $ext file extension
2018-01-09 02:09:07 +03:00
* @return string mime
*/
function get_mime_for_header($ext)
{
$mime_types = include __DIR__ . '/mime_types.php';
2019-05-03 23:52:08 +03:00
//return mime
$ext = strtolower($ext);
if (in_array($ext, array_keys($mime_types)))
{
$return = $mime_types[$ext];
}
else
{
$return = 'application/force-download';
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('get_mime_for_header_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
return $return;
2018-01-09 02:09:07 +03:00
}
/**
* Include language file
2019-05-03 23:52:08 +03:00
* @param string $name language filename, 'acp, common..'
* @param string $folder
2018-01-09 02:09:07 +03:00
* @return bool
*/
function get_lang($name, $folder = '')
{
2019-05-03 23:52:08 +03:00
global $config, $lang;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (is_null($lang) || ! is_array($lang))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$lang = [];
2018-01-09 02:09:07 +03:00
}
is_array($plugin_run_result = Plugins::getInstance()->run('get_lang_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$name = str_replace('..', '', $name);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($folder != '')
{
$folder = str_replace(['..', '/'], '', $folder);
$name = $folder . '/' . $name;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$path = PATH . 'lang/' . $config['language'] . '/' . str_replace('.php', '', $name) . '.php';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$lang_to_add = @include_once $path;
if ($lang_to_add === false)
{
2018-01-09 02:09:07 +03:00
//fallback to English
2019-05-03 23:52:08 +03:00
$path_en = PATH . 'lang/en/' . str_replace('.php', '', $name) . '.php';
$lang_to_add = @include_once $path_en;
if ($lang_to_add === false)
{
big_error('There is no language file in the current path', 'lang/' . $config['language'] . '/' . str_replace('.php', '', $name) . '.php not found');
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (is_array($lang_to_add))
{
2018-01-09 02:09:07 +03:00
$lang = array_merge($lang, $lang_to_add);
}
2019-05-03 23:52:08 +03:00
return true;
2018-01-09 02:09:07 +03:00
}
/*
* Get fresh config value
* some time cache doesn't not work as well, so some important
* events need fresh version of config values ...
*/
function get_config($name)
{
2019-05-03 23:52:08 +03:00
global $dbprefix, $SQL, $d_groups, $userinfo;
$table = "{$dbprefix}config c";
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
//what if this config is a group-configs related ?
$group_id_sql = '';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (array_key_exists($name, $d_groups[$userinfo['group_id']]['configs']))
{
$table = "{$dbprefix}groups_data c";
$group_id_sql = ' AND c.group_id=' . $userinfo['group_id'];
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$query = [
2019-05-18 01:47:17 +03:00
'SELECT' => 'c.value',
'FROM' => $table,
'WHERE' => "c.name = '" . $SQL->escape($name) . "'" . $group_id_sql
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
2019-05-18 01:47:17 +03:00
$result = $SQL->build($query);
$v = $SQL->fetch($result);
$return = isset($v['value']) ? $v['value'] : null;
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('get_config_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
return $return;
2018-01-09 02:09:07 +03:00
}
/*
* Add new config option
* type: where does your config belone, 0 = system, genetal = has no specifc cat., other = other items.
* html: the input or radio to let the user type or choose from them, see the database:configs to understand.
* dynamic: every refresh of the page, the config data will be brought from db, not from the cache !
* plg_id: if this config belong to plugin .. see devKit.
*/
function add_config($name, $value, $order = '0', $html = '', $type = '0', $plg_id = '0', $dynamic = false)
{
2019-05-03 23:52:08 +03:00
global $dbprefix, $SQL, $config, $d_groups;
if (get_config($name))
{
return true;
}
if ($html != '' && $type == '0')
{
$type = 'other';
}
if ($type == 'groups')
{
//add this option to all groups
$group_ids = array_keys($d_groups);
foreach ($group_ids as $g_id)
{
2019-05-18 01:47:17 +03:00
$insert_query = [
'INSERT' => '`name`, `value`, `group_id`',
'INTO' => "{$dbprefix}groups_data",
'VALUES' => "'" . $SQL->escape($name) . "','" . $SQL->escape($value) . "', " . $g_id,
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('insert_sql_add_config_func_groups_data', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($insert_query);
}
}
2018-01-09 02:09:07 +03:00
2019-05-18 01:47:17 +03:00
$insert_query = [
'INSERT' => '`name` ,`value` ,`option` ,`display_order`, `type`, `plg_id`, `dynamic`',
'INTO' => "{$dbprefix}config",
'VALUES' => "'" . $SQL->escape($name) . "','" . $SQL->escape($value) . "', '" . $SQL->real_escape($html) . "','" . intval($order) . "','" . $SQL->escape($type) . "','" . intval($plg_id) . "','" . ($dynamic ? '1' : '0') . "'",
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('insert_sql_add_config_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($insert_query);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($SQL->affected())
{
delete_cache('data_config');
$config[$name] = $value;
return true;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return false;
2018-01-09 02:09:07 +03:00
}
/**
* add an array of new configs
* @param $configs
2018-01-09 02:09:07 +03:00
* @return bool
*/
function add_config_r($configs)
{
2019-05-03 23:52:08 +03:00
if (! is_array($configs))
{
return false;
}
//array(name=>array(value=>,order=>,html=>),...);
foreach ($configs as $n=>$m)
{
2018-01-09 02:09:07 +03:00
add_config(
$n,
empty($m['value']) ? '' : $m['value'],
empty($m['order']) ? 0 : $m['order'],
empty($m['html']) ? '' : $m['html'],
empty($m['type']) ? 'other' : $m['type'],
empty($m['plg_id']) ? 0 : $m['plg_id'],
empty($m['dynamic']) ? false : $m['dynamic']
);
}
return true;
}
function update_config($name, $value, $escape = true, $group = false)
{
2019-05-23 20:22:47 +03:00
global $SQL, $dbprefix, $d_groups, $userinfo, $config;
2018-01-09 02:09:07 +03:00
2019-05-20 21:56:29 +03:00
$value = $escape ? $SQL->escape($value) : $value;
2019-05-03 23:52:08 +03:00
$table = "{$dbprefix}config";
//what if this config is a group-configs related ?
$group_id_sql = '';
2018-01-09 02:09:07 +03:00
if (array_key_exists($name, $d_groups[$userinfo['group_id']]['configs']) && $group != false)
2019-05-03 23:52:08 +03:00
{
$table = "{$dbprefix}groups_data";
if ($group == -1)
{
$group_id_sql = ' AND group_id=' . $userinfo['group_id'];
}
elseif ($group)
{
$group_id_sql = ' AND group_id=' . intval($group);
}
}
2019-05-18 01:47:17 +03:00
$update_query = [
'UPDATE' => $table,
'SET' => "value='" . ($escape ? $SQL->escape($value) : $value) . "'",
'WHERE' => 'name = "' . $SQL->escape($name) . '"' . $group_id_sql
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('update_sql_update_config_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
if ($SQL->affected())
{
if ($table == "{$dbprefix}groups_data")
{
$d_groups[$userinfo['group_id']]['configs'][$name] = $value;
delete_cache('data_groups');
return true;
}
$config[$name] = $value;
delete_cache('data_config');
return true;
}
return false;
2018-01-09 02:09:07 +03:00
}
2019-05-03 23:52:08 +03:00
// Delete config
2018-01-09 02:09:07 +03:00
function delete_config($name)
{
2019-05-03 23:52:08 +03:00
if (is_array($name))
{
foreach ($name as $n)
{
delete_config($n);
}
2019-05-25 19:59:45 +03:00
return;
2019-05-03 23:52:08 +03:00
}
global $dbprefix, $SQL, $d_groups, $userinfo;
//
// 'IN' doesnt work here with delete, i dont know why ?
//
2019-05-18 01:47:17 +03:00
$delete_query = [
'DELETE' => "{$dbprefix}config",
'WHERE' => "name = '" . $SQL->escape($name) . "'"
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('del_sql_delete_config_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($delete_query);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (array_key_exists($name, $d_groups[$userinfo['group_id']]['configs']))
{
2019-05-18 01:47:17 +03:00
$delete_query = [
'DELETE' => "{$dbprefix}groups_data",
'WHERE' => "name = '" . $SQL->escape($name) . "'"
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('del_sql_delete_config_func2', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($delete_query);
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($SQL->affected())
{
return true;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return false;
2018-01-09 02:09:07 +03:00
}
//
//update words to lang
//
2022-08-24 20:56:41 +01:00
function update_olang($name, $lang = 'en', $value = '')
2018-01-09 02:09:07 +03:00
{
2019-06-02 05:46:15 +03:00
global $SQL, $dbprefix, $olang;
2018-01-09 02:09:07 +03:00
2019-05-18 01:47:17 +03:00
$update_query = [
'UPDATE' => "{$dbprefix}lang",
'SET' => "trans='" . $SQL->escape($value) . "'",
'WHERE' => 'word = "' . $SQL->escape($name) . '", lang_id = "' . $SQL->escape($lang) . '"'
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('update_sql_update_olang_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
if ($SQL->affected())
{
2018-01-09 02:09:07 +03:00
delete_cache('data_lang' . $lang);
$olang[$name] = htmlspecialchars($value);
2019-05-03 23:52:08 +03:00
return true;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return false;
2018-01-09 02:09:07 +03:00
}
//
//add words to lang
//
2019-05-03 23:52:08 +03:00
function add_olang($words = [], $lang = 'en', $plg_id = '0')
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
global $dbprefix, $SQL;
foreach ($words as $w=> $t)
{
$insert_query = [
2019-05-18 01:47:17 +03:00
'INSERT' => 'word ,trans ,lang_id, plg_id',
'INTO' => "{$dbprefix}lang",
'VALUES' => "'" . $SQL->escape($w) . "','" . $SQL->real_escape($t) . "', '" . $SQL->escape($lang) . "','" . intval($plg_id) . "'",
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('insert_sql_add_olang_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($insert_query);
}
2018-01-09 02:09:07 +03:00
delete_cache('data_lang' . $lang);
}
//
//delete words from lang
//
/**
2019-05-03 23:52:08 +03:00
* @param string|array $words language terms to use a in $olang[word] or olang.word
* @param string $lang langauge of given word
* @param string $plg_id plugin id associated with these words, optional
2018-01-09 02:09:07 +03:00
* @return bool
*/
function delete_olang($words = '', $lang = 'en', $plg_id = 0)
{
2019-05-03 23:52:08 +03:00
global $dbprefix, $SQL;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (is_array($words))
{
2019-06-07 04:03:25 +03:00
foreach ($words as $w)
2019-05-03 23:52:08 +03:00
{
2019-06-07 04:03:25 +03:00
delete_olang($w, $lang, $plg_id);
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
return true;
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
2019-05-18 01:47:17 +03:00
$delete_query = [
'DELETE' => "{$dbprefix}lang",
2019-06-07 04:03:25 +03:00
'WHERE' => empty($words) ? '' : "word = '" . $SQL->escape($words) . "'"
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
2019-06-07 04:03:25 +03:00
if (! empty($lang))
{
2021-06-06 04:57:30 +02:00
if (is_array($lang))
2019-06-07 04:03:25 +03:00
{
foreach ($lang as $index=>$current_lang)
{
2022-08-24 20:56:41 +01:00
$lang[$index] = $SQL->escape($lang[$index]);
}
$lang_sql = "(lang_id = '" . implode("' OR lang_id = '", $lang) . "')";
}
else
{
2022-08-24 20:56:41 +01:00
$lang_sql = "lang_id = '" . $SQL->escape($lang) . "'";
2019-06-07 04:03:25 +03:00
}
$delete_query['WHERE'] .= (empty($delete_query['WHERE']) ? '' : ' AND ') . $lang_sql;
}
2019-05-03 23:52:08 +03:00
if (! empty($plg_id))
{
2019-06-07 04:03:25 +03:00
$delete_query['WHERE'] .= (empty($delete_query['WHERE']) ? '' : ' AND ') . 'plg_id = ' . intval($plg_id);
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('del_sql_delete_olang_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2021-06-06 04:57:30 +02:00
if (empty($delete_query['WHERE']))
2019-06-07 04:03:25 +03:00
{
return false;
}
2019-05-03 23:52:08 +03:00
$SQL->build($delete_query);
2018-01-09 02:09:07 +03:00
return $SQL->affected();
}
/**
* Administrator sometime needs some files and delete other ..
* we do that for him .. because he has no time .. :)
* last_down - $config[del_f_day]
* @param int $from
*/
function klj_clean_old_files($from = 0)
{
2019-05-03 23:52:08 +03:00
global $config, $SQL, $stat_last_f_del, $dbprefix;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$return = false;
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('klj_clean_old_files_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
if ((int) $config['del_f_day'] <= 0 || $return)
{
return;
}
if (! $stat_last_f_del || empty($stat_last_f_del))
{
$stat_last_f_del = time();
}
if ((time() - $stat_last_f_del) >= 86400)
{
2019-05-18 01:47:17 +03:00
$totaldays = (time() - ($config['del_f_day']*86400));
$not_today = time() - 86400;
2019-05-03 23:52:08 +03:00
//This feature will work only if id_form is not empty or direct !
$query = [
2019-05-18 01:47:17 +03:00
'SELECT' => 'f.id, f.last_down, f.name, f.type, f.folder, f.time, f.size, f.id_form',
'FROM' => "{$dbprefix}files f",
'WHERE' => "f.last_down < $totaldays AND f.time < $not_today AND f.id > $from AND f.id_form <> '' AND f.id_form <> 'direct'",
'ORDER BY' => 'f.id ASC',
'LIMIT' => '20',
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_select_klj_clean_old_files_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-18 01:47:17 +03:00
$result = $SQL->build($query);
2019-05-03 23:52:08 +03:00
$num_of_files_to_delete = $SQL->num_rows($result);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($num_of_files_to_delete == 0)
{
//update $stat_last_f_del !!
$update_query = [
2019-05-18 01:47:17 +03:00
'UPDATE' => "{$dbprefix}stats",
'SET' => "last_f_del ='" . time() . "'",
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_update_lstf_del_date_kcof', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
//delete stats cache
delete_cache('data_stats');
update_config('klj_clean_files_from', '0');
$SQL->freeresult($result);
return;
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$last_id_from = $files_num = $imgs_num = $real_num = $sizes = 0;
$ids = [];
$ex_ids = [];
//$ex_types = explode(',', $config['livexts']);
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('beforewhile_klj_clean_old_files_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
//phpfalcon plugin
$exlive_types = explode(',', $config['imagefolderexts']);
2019-05-03 23:52:08 +03:00
//delete files
while ($row=$SQL->fetch_array($result))
{
$continue = true;
$real_num++;
$last_id_from = $row['id'];
$is_image = in_array(strtolower(trim($row['type'])), ['gif', 'jpg', 'jpeg', 'bmp', 'png']) ? true : false;
/*
//exceptions
if(in_array($row['type'], $ex_types) || $config['id_form'] == 'direct')
{
$ex_ids[] = $row['id'];
continue;
}
*/
2018-01-09 02:09:07 +03:00
//exceptions
2019-05-03 23:52:08 +03:00
//if($config['id_form'] == 'direct')
//{
//$ex_ids[] = $row['id'];
//move on
//continue;
//}
//your exepctions
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('while_klj_clean_old_files_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
if ($continue)
{
//delete from folder ..
if (file_exists($row['folder'] . '/' . $row['name']))
{
2019-05-05 20:12:32 +03:00
@kleeja_unlink($row['folder'] . '/' . $row['name']);
2019-05-03 23:52:08 +03:00
}
2019-05-03 23:52:08 +03:00
//delete thumb
2019-05-05 20:12:32 +03:00
if (file_exists($row['folder'] . '/thumbs/' . $row['name']))
2019-05-03 23:52:08 +03:00
{
2019-05-05 20:12:32 +03:00
@kleeja_unlink($row['folder'] . '/thumbs/' . $row['name']);
2019-05-03 23:52:08 +03:00
}
$ids[] = $row['id'];
if ($is_image)
{
$imgs_num++;
}
else
{
$files_num++;
}
$sizes += $row['size'];
}
}//END WHILE
$SQL->freeresult($result);
if (sizeof($ex_ids))
{
2019-05-18 01:47:17 +03:00
$update_query = [
'UPDATE' => "{$dbprefix}files",
'SET' => "last_down = '" . (time() + 2*86400) . "'",
'WHERE' => 'id IN (' . implode(',', $ex_ids) . ')'
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_update_lstdown_old_files', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($update_query);
}
if (sizeof($ids))
{
2019-05-18 01:47:17 +03:00
$query_del = [
'DELETE' => "{$dbprefix}files",
'WHERE' => 'id IN (' . implode(',', $ids) . ')'
2019-05-03 23:52:08 +03:00
];
//update number of stats
2019-05-18 01:47:17 +03:00
$update_query = [
'UPDATE' => "{$dbprefix}stats",
'SET' => "sizes=sizes-$sizes,files=files-$files_num, imgs=imgs-$imgs_num",
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_del_delf_old_files', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
$SQL->build($query_del);
$SQL->build($update_query);
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
update_config('klj_clean_files_from', $last_id_from);
2018-01-09 02:09:07 +03:00
} //stat_del
}
/**
* klj_clean_old
2019-05-03 23:52:08 +03:00
* @param string $table database table
* @param string|integer $for can be 'all, or a number of days like 30'
2018-01-09 02:09:07 +03:00
*/
function klj_clean_old($table, $for = 'all')
{
2019-05-03 23:52:08 +03:00
global $SQL, $config, $dbprefix;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$days = time() - (3600 * 24 * intval($for));
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$query = [
2019-05-18 01:47:17 +03:00
'SELECT' => 'f.id, f.time',
2021-06-06 04:57:30 +02:00
'FROM' => "`{$dbprefix}" . $table . '` f',
2019-05-18 01:47:17 +03:00
'ORDER BY' => 'f.id ASC',
'LIMIT' => '30',
2019-05-03 23:52:08 +03:00
];
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($for != 'all')
{
2019-05-18 01:47:17 +03:00
$query['WHERE'] = "f.time < $days";
2019-05-03 23:52:08 +03:00
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('qr_select_klj_clean_old_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-18 01:47:17 +03:00
$result = $SQL->build($query);
$num_to_delete = $SQL->num_rows($result);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($num_to_delete == 0)
{
$t = $table == 'call' ? 'calls' : $table;
update_config('queue', preg_match('/:del_' . $for . $t . ':/i', '', $config['queue']));
$SQL->freeresult($result);
return;
}
$ids = [];
while ($row=$SQL->fetch_array($result))
{
$ids[] = $row['id'];
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
$SQL->freeresult($result);
2018-01-09 02:09:07 +03:00
2019-05-18 01:47:17 +03:00
$query_del = [
'DELETE' => '`' . $dbprefix . $table . '`',
'WHERE' => 'id IN (' . implode(',', $ids) . ')'
2019-05-03 23:52:08 +03:00
];
is_array($plugin_run_result = Plugins::getInstance()->run('qr_del_delf_old_table', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
$SQL->build($query_del);
2018-01-09 02:09:07 +03:00
}
/**
* get_ip() for the user
*/
function get_ip()
{
2019-05-03 23:52:08 +03:00
$ip = '';
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if (! empty($_SERVER['HTTP_CF_CONNECTING_IP']))
{
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
}
elseif (! empty($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
//if IP chain
if (strpos($ip, ',') !== false)
{
2018-01-09 02:09:07 +03:00
$ip = explode(',', $ip);
$ip = trim($ip[0]);
}
2019-05-03 23:52:08 +03:00
//is it IPv6?
$ip_v6 = preg_match('/^[0-9a-f]{1,4}:([0-9a-f]{0,4}:){1,6}[0-9a-f]{1,4}$/', $ip);
if ($ip_v6)
{
//does it IPv4 hide in a IPv6 style
if (stripos($ip, '::ffff:') === 0)
{
2018-01-09 02:09:07 +03:00
$ip = substr($ip, 7);
}
}
$return = preg_replace('/[^0-9a-z.:]/i', '', $ip);
is_array($plugin_run_result = Plugins::getInstance()->run('kleeja_get_ip_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
return $return;
2018-01-09 02:09:07 +03:00
}
/**
* Check and verify captcha field after submit
* @return bool
*/
function kleeja_check_captcha()
{
2019-05-03 23:52:08 +03:00
global $config;
if ((int) $config['enable_captcha'] == 0 && ! defined('IN_REAL_INDEX') && ! defined('IN_ADMIN'))
{
return true;
}
$return = false;
if (! empty($_SESSION['klj_sec_code']) && ip('kleeja_code_answer'))
{
if ($_SESSION['klj_sec_code'] == trim(p('kleeja_code_answer')))
{
unset($_SESSION['klj_sec_code']);
$return = true;
}
}
2018-01-09 02:09:07 +03:00
is_array($plugin_run_result = Plugins::getInstance()->run('kleeja_check_captcha_func', get_defined_vars())) ? extract($plugin_run_result) : null; //run hook
2019-05-03 23:52:08 +03:00
return $return;
2018-01-09 02:09:07 +03:00
}
/**
* For logging and testing, enabled only for DEV_STAGE!
* @param string $text a string to log
*/
function kleeja_log($text)
{
2019-05-03 23:52:08 +03:00
if (! defined('DEV_STAGE'))
{
return;
}
file_put_contents(
2019-05-30 07:32:17 +03:00
__DIR__ . '/../cache/kleeja_log.log',
date_format(date_create(), 'Y-m-d h:i:s.ua') . ' | INFO | ' . $text . PHP_EOL,
FILE_APPEND | LOCK_EX
);
2018-01-09 02:09:07 +03:00
}
/**
* Return the first and last seek of range to be flushed.
* @param string $range
* @param $fileSize
2018-01-09 02:09:07 +03:00
* @return array
*/
function kleeja_set_range($range, $fileSize)
{
2019-05-18 01:47:17 +03:00
$dash = strpos($range, '-');
$first = trim(substr($range, 0, $dash));
$last = trim(substr($range, $dash+1));
2019-05-03 23:52:08 +03:00
if (! $first)
{
2019-05-18 01:47:17 +03:00
$suffix = $last;
$last = $fileSize - 1;
$first = $fileSize - $suffix;
2019-05-03 23:52:08 +03:00
if ($first < 0)
{
$first = 0;
}
}
else
{
if (! $last || $last > $fileSize - 1)
{
2018-01-09 02:09:07 +03:00
$last = $fileSize - 1;
2019-05-03 23:52:08 +03:00
}
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($first > $last)
{
//unsatisfiable range
header('Status: 416 Requested range not satisfiable');
2018-01-09 02:09:07 +03:00
header("Content-Range: */$fileSize");
2019-05-03 23:52:08 +03:00
exit;
}
return [$first, $last];
2018-01-09 02:09:07 +03:00
}
/**
* Outputs up to $bytes from the file $file to standard output,
* $buffer_size bytes at a time.
* @param resource $file
2019-05-03 23:52:08 +03:00
* @param integer $bytes
* @param integer $buffer_size
2018-01-09 02:09:07 +03:00
*/
function kleeja_buffered_range($file, $bytes, $buffer_size = 1024)
{
2019-05-03 23:52:08 +03:00
$bytes_left = $bytes;
while ($bytes_left > 0 && ! feof($file))
{
if ($bytes_left > $buffer_size)
{
$bytes_to_read = $buffer_size;
}
else
{
$bytes_to_read = $bytes_left;
}
2019-05-18 01:47:17 +03:00
$bytes_left -= $bytes_to_read;
$contents = fread($file, $bytes_to_read);
2019-05-03 23:52:08 +03:00
echo $contents;
@flush();
@ob_flush();
}
2018-01-09 02:09:07 +03:00
}
/**
* user_can, used for checking the acl for the current user
2019-05-03 23:52:08 +03:00
* @param string $acl_name
* @param int $group_id
2018-01-09 02:09:07 +03:00
* @return bool
*/
function user_can($acl_name, $group_id = 0)
{
2019-05-03 23:52:08 +03:00
global $d_groups, $userinfo;
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($group_id == 0)
{
$group_id = $userinfo['group_id'];
}
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
return (bool) $d_groups[$group_id]['acls'][$acl_name];
2018-01-09 02:09:07 +03:00
}
function ig($name)
{
2019-05-19 02:40:43 +03:00
return isset($_GET[$name]);
2018-01-09 02:09:07 +03:00
}
function ip($name)
{
2019-05-19 02:40:43 +03:00
return isset($_POST[$name]);
2018-01-09 02:09:07 +03:00
}
function g($name, $type = 'str', $default = '')
{
if (isset($_GET[$name]))
{
2021-06-06 04:57:30 +02:00
return $type == 'str' ? htmlspecialchars($_GET[$name], ENT_QUOTES) : intval($_GET[$name]);
2018-01-09 02:09:07 +03:00
}
2021-06-06 04:57:30 +02:00
return $type == 'str' ? htmlspecialchars($default, ENT_QUOTES) : intval($default);
2018-01-09 02:09:07 +03:00
}
function p($name, $type = 'str', $default = '')
{
if (isset($_POST[$name]))
{
return $type == 'str'
2021-06-06 04:57:30 +02:00
? str_replace(["\r\n", "\r", "\0"], ["\n", "\n", ''], htmlspecialchars(trim($_POST[$name]), ENT_QUOTES))
2018-01-09 02:09:07 +03:00
: intval($_POST[$name]);
}
return $type == 'str' ? htmlspecialchars($default) : intval($default);
}
/**
2019-02-25 20:09:42 +03:00
* add rewrite rules to the serve.php file
2019-05-03 23:52:08 +03:00
* @param array|string $rules
* @param string $unique_id useful for the deletion later
2018-01-09 02:09:07 +03:00
* @return bool
*/
2019-02-25 20:09:42 +03:00
function add_to_serve_rules($rules, $unique_id = '')
2018-01-09 02:09:07 +03:00
{
2019-05-25 00:30:55 +03:00
if (! file_exists(PATH . 'plugins_rules.php'))
2019-05-18 16:58:43 +03:00
{
2019-05-25 00:30:55 +03:00
if (! is_writable(PATH))
2019-05-18 16:58:43 +03:00
{
chmod(PATH, K_DIR_CHMOD);
}
file_put_contents(PATH . 'plugins_rules.php', '<?php return [' . PHP_EOL . '];');
}
$current_serve_content = file_get_contents(PATH . 'plugins_rules.php');
2018-01-09 02:09:07 +03:00
$rules = is_array($rules) ? implode(PHP_EOL, $rules) : $rules;
2019-05-03 23:52:08 +03:00
if (! empty($unique_id))
2018-01-09 02:09:07 +03:00
{
2019-05-03 23:52:08 +03:00
$rules = '#start_' . $unique_id . PHP_EOL . $rules . PHP_EOL . '#end_' . $unique_id;
2018-01-09 02:09:07 +03:00
}
2019-05-18 16:58:43 +03:00
$current_serve_content = preg_replace(
'/return\s{0,4}\[/',
'return [' . PHP_EOL . $rules,
$current_serve_content
);
2019-05-18 16:58:43 +03:00
2018-01-09 02:09:07 +03:00
2019-05-18 16:58:43 +03:00
if (! is_writable(PATH . 'plugins_rules.php'))
2019-05-16 02:54:41 +03:00
{
2019-05-18 16:58:43 +03:00
chmod(PATH . 'plugins_rules.php', K_FILE_CHMOD);
2019-05-16 02:54:41 +03:00
}
2019-05-18 16:58:43 +03:00
file_put_contents(PATH . 'plugins_rules.php', $current_serve_content);
2018-01-09 02:09:07 +03:00
return true;
}
/**
2019-02-25 20:09:42 +03:00
* remove rewrite rules by previously set unique id
2019-05-03 23:52:08 +03:00
* @param string $unique_id
2018-01-09 02:09:07 +03:00
* @return bool
*/
2019-02-25 20:09:42 +03:00
function remove_from_serve_rules($unique_id)
2018-01-09 02:09:07 +03:00
{
2019-05-18 16:58:43 +03:00
$file = PATH . 'plugins_rules.php';
2018-01-09 02:09:07 +03:00
2019-02-25 20:09:42 +03:00
$current_serve_content = file_get_contents($file);
2018-01-09 02:09:07 +03:00
2019-02-25 20:09:42 +03:00
$new_serve_content = preg_replace(
2018-01-09 02:09:07 +03:00
'/^#start_' . preg_quote($unique_id) . '.*' . '#end_' . preg_quote($unique_id) . '$/sm',
'',
2019-02-25 20:09:42 +03:00
$current_serve_content
);
2018-01-09 02:09:07 +03:00
2019-05-03 23:52:08 +03:00
if ($new_serve_content === $current_serve_content)
2018-01-09 02:09:07 +03:00
{
return false;
}
2019-05-18 16:58:43 +03:00
if (! is_writable(PATH . 'plugins_rules.php'))
2019-05-16 02:54:41 +03:00
{
2019-05-18 16:58:43 +03:00
chmod(PATH . 'plugins_rules.php', K_FILE_CHMOD);
2019-05-16 02:54:41 +03:00
}
2019-02-25 20:09:42 +03:00
file_put_contents($file, $new_serve_content);
2018-01-09 02:09:07 +03:00
return true;
2018-12-12 19:28:43 +00:00
}
2019-02-26 22:10:38 +03:00
/**
* parse rewrite rule. currently added separately for plugins
2019-05-03 23:52:08 +03:00
* @param string $regex
* @param array $args
* @param bool $is_unicode
2019-02-26 22:10:38 +03:00
* @return bool
*/
function parse_serve_rule($regex, $args, $is_unicode = false)
{
2019-05-03 23:52:08 +03:00
$request_uri = urldecode(
trim(strtok($_SERVER['REQUEST_URI'], '?'), '/')
);
if (preg_match("/{$regex}/" . ($is_unicode ? 'u' : ''), $request_uri, $matches))
{
if (! empty($args))
{
parse_str($args, $parsed_args);
foreach ($parsed_args as $arg_key => $arg_value)
{
2019-05-05 20:12:32 +03:00
if (preg_match('/^\$/', $arg_value))
2019-05-03 23:52:08 +03:00
{
$match_number = ltrim($arg_value, '$');
2019-05-05 20:12:32 +03:00
if (isset($matches[$match_number]))
2019-05-03 23:52:08 +03:00
{
$_GET[$arg_key] = $matches[$match_number];
}
}
else
{
$_GET[$arg_key] = $arg_value;
}
}
}
return true;
}
return false;
2019-02-26 22:10:38 +03:00
}