Files
visiome.neuroinf.jp/etc/common.inc.php
T

198 lines
5.7 KiB
PHP

<?php
require_once __DIR__.'/config.inc.php';
define('PROTECTOR_SKIP_DOS_CHECK', 1);
define('PROTECTOR_SKIP_FILESCHECKER', 1);
// disable query logger
define('XOOPS_LOGGER_ADDQUERY_DISABLED', true);
// ----
if (!file_exists($mainfile)) {
echo 'ERROR : mainfile.php not found'.PHP_EOL;
exit(1);
}
$xoops_path = '';
$xoops_url = '';
foreach (file($mainfile) as $line) {
if (preg_match('/^\s*define\s*\(\s*[\'"]XOOPS_ROOT_PATH[\'"]\s*,\s*[\'"](.+)[\'"]\)\s*;\s*$/', $line, $matches)) {
$xoops_path = $matches[1];
}
if (preg_match('/^\s*define\s*\(\s*[\'"]XOOPS_URL[\'"]\s*,\s*[\'"](.+)[\'"]\)\s*;\s*$/', $line, $matches)) {
$xoops_url = $matches[1];
}
}
if (isset($method) && 'POST' == strtoupper($method)) {
$method = 'POST';
} else {
$method = 'GET';
}
$_SERVER['HTTP_USER_AGENT'] = 'php-cli';
$_SERVER['REQUEST_METHOD'] = $method;
$_ENV['HTTP_REFERER'] = $xoops_url.'/index.php';
$_SERVER['QUERY_STRING'] = '/index.php';
$_SERVER['REMOTE_ADDR'] = '192.168.0.1';
if (file_exists($xoops_path.'/modules/xoonips/include/common.inc.php')) {
require_once $xoops_path.'/modules/xoonips/include/common.inc.php';
} else {
require_once $xoops_path.'/mainfile.php';
require_once $xoops_path.'/modules/xoonips/condefs.php';
require_once $xoops_path.'/modules/xoonips/include/functions.php';
}
if (defined('XOOPS_DB_PROXY') && XOOPS_DB_PROXY == 1 && 'POST' == $method) {
die('Error: not accept POST request'."\n");
}
error_reporting(E_ALL);
// remove ob fileters
while (ob_get_level()) {
ob_end_clean();
}
define('MYDUMPTOOL_OUTPUTDIR', __DIR__.'/data');
MyDumpTool::makeDirectory('');
MyDumpTool::makeDirectory('src');
MyDumpTool::makeDirectory('public');
class MyDumpTool
{
public static function decode(&$data)
{
$textutil = xoonips_getutility('text');
foreach ($data as $k => $v) {
if (is_string($v)) {
$v = $textutil->html_numeric_entities($v);
$v = mb_decode_numericentity($v, array(0x0, 0x10000, 0, 0xfffff), 'UTF-8');
$data[$k] = \Normalizer::normalize(trim($v), \Normalizer::FORM_C);
} elseif (null === $v) {
$data[$k] = '';
}
}
}
public static function convertToInt(&$data, $keys, $zerofill = false)
{
foreach ($keys as $key) {
if (isset($data[$key])) {
$data[$key] = '' === $data[$key] ? ($zerofill ? 0 : null) : (int) $data[$key];
}
}
}
public static function dropColumn(&$data, $keys)
{
foreach ($keys as $key) {
if (isset($data[$key])) {
unset($data[$key]);
}
}
}
public static function fileCopy($from, $to)
{
system('rsync -aq '.escapeshellarg($from).' '.escapeshellarg($to), $ret);
return 0 === $ret;
}
public static function fixHtml($text)
{
static $config = [
//'clean' => true,
'hide-comments' => true,
'indent' => true,
'output-xhtml' => true,
'preserve-entities' => true,
'show-body-only' => true,
'wrap' => 0,
'input-encoding' => 'utf8',
'output-encoding' => 'utf8',
'output-bom' => false,
];
$text = tidy_repair_string($text, $config);
$text = preg_replace('/<table([^>]*)>((?:\s*<caption[^>]*>.*<\/caption>)?\s*)<tr([^>]*)>/Us', '<table\1>\2<tbody><tr\3>', $text);
$text = preg_replace('/<\/tr>(\s*)<\/table>/s', '</tr></tbody>\1</table>', $text);
return $text;
}
public static function tableExists($name)
{
global $xoopsDB;
$prefix = $xoopsDB->prefix();
$sql = <<< SQL
SELECT 1
FROM `${prefix}_${name}`
LIMIT 1
SQL;
if (!($res = $xoopsDB->query($sql))) {
return false;
}
$xoopsDB->freeRecordSet($res);
return true;
}
public static function makeDirectory(string $dirname)
{
$path = MYDUMPTOOL_OUTPUTDIR.('' !== $dirname ? '/'.$dirname : '');
if (!is_dir($path)) {
if (!@mkdir($path)) {
exit('Failed to create directory: '.$path.PHP_EOL);
}
}
}
public static function saveJson(string $fname, $data)
{
$path = MYDUMPTOOL_OUTPUTDIR.'/'.$fname;
if (false === file_put_contents($path, json_encode($data, JSON_UNESCAPED_UNICODE))) {
exit('Failed to save json file: '.$path.PHP_EOL);
}
}
public static function fileDownload($url, $fpath)
{
$fp = fopen($fpath, 'w');
if (false === $fp) {
exit('Failed to open file: '.$fpath.PHP_EOL);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
$ret = curl_exec($curl);
fclose($fp);
if (false === $ret) {
exit('Failed to download file, unexpected error: '.$url.PHP_EOL);
}
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 !== $code) {
exit('Failed to download file, invalid status code('.$code.'): '.$url.PHP_EOL);
}
curl_close($curl);
return true;
}
public static function getMimeType($fpath)
{
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $fpath);
finfo_close($finfo);
$mime = preg_replace('/(;.*)$/', '', $mime);
return $mime;
}
}