Files
invbrain.neuroinf.jp/etc/dumpPico.php
T
2020-01-06 13:03:45 +09:00

187 lines
7.0 KiB
PHP

<?php
require_once __DIR__.'/common.inc.php';
if (!isset($XOOPS_MODULE_PICO) || empty($XOOPS_MODULE_PICO)) {
exit('Not configured for pico module'.PHP_EOL);
}
error_reporting(0);
require_once XOOPS_TRUST_PATH.'/modules/pico/include/main_functions.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/include/common_functions.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/class/pico.textsanitizer.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/class/PicoUriMapper.class.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/class/PicoPermission.class.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/class/PicoModelCategory.class.php';
require_once XOOPS_TRUST_PATH.'/modules/pico/class/PicoModelContent.class.php';
require_once XOOPS_TRUST_PATH.'/libs/altsys/class/AltsysBreadcrumbs.class.php';
error_reporting(E_ALL);
foreach ($XOOPS_MODULE_PICO as $pico) {
dumpPico($pico);
}
function dumpPico($pico)
{
global $xoopsDB;
$prefix = $xoopsDB->prefix();
$table = $pico.'_contents';
if (!MyDumpTool::tableExists($table)) {
exit('pico('.$pico.') module not found'.PHP_EOL);
}
MyDumpTool::makeDirectory('public/modules/'.$pico);
$moduleHandler = xoops_gethandler('module');
$module = $moduleHandler->getByDirname($pico);
$moduleConfigHandler = xoops_gethandler('config');
$moduleConfig = $moduleConfigHandler->getConfigsByDirname($pico);
$permObj = PicoPermission::getInstance();
$perms = $permObj->getPermissions($pico);
$categoryHandler = new PicoCategoryHandler($pico, $perms);
$contentHandler = new PicoContentHandler($pico);
$cats = $categoryHandler->getAllCategories();
$confContents = [];
$confCategories = [];
$dataMap = [];
$dataSeq = 1;
foreach ($cats as $cat) {
$catData = $cat->getData();
$modConfig = $cat->getOverriddenModConfig();
$link = pico_common_make_category_link4html($modConfig, $catData, $pico);
MyDumpTool::decode($catData);
$confCategories[] = [
'id' => $catData['id'],
'title' => $catData['cat_title'],
'desc' => (string) $catData['cat_desc'],
'pid' => (int) $catData['pid'],
'weight' => (int) $catData['cat_weight'],
'link' => $link,
];
$contents = $contentHandler->getCategoryContents($cat);
foreach ($contents as $content) {
$data = $content->getData();
$link = pico_common_make_content_link4html($modConfig, $data);
MyDumpTool::decode($data);
$url = XOOPS_URL.'/modules/'.$pico.'/'.$link;
$content_en = normalizeHtml(MyDumpTool::mlang($data['body_cached'], $url, 'en'), $url);
$content_ja = normalizeHtml(MyDumpTool::mlang($data['body_cached'], $url, 'ja'), $url);
$item = [
'id' => $data['id'],
'title' => $data['subject_raw'],
'content' => [
'en' => $content_en,
'ja' => $content_ja,
],
];
MyDumpTool::saveJson('public/modules/'.$pico.'/'.$data['id'].'.json', $item);
$confContents[] = [
'id' => $item['id'],
'title' => $data['subject_raw'],
'cat_id' => (int) $data['cat_id'],
'weight' => (int) $data['weight'],
'link' => $link,
];
}
}
$moduleinfo = [
'name' => $module->get('name'),
'dirname' => $pico,
'message' => $moduleConfig['top_message'],
'show_menuinmoduletop' => (int) $moduleConfig['show_menuinmoduletop'],
'show_listasindex' => (int) $moduleConfig['show_listasindex'],
'show_breadcrumbs' => (int) $moduleConfig['show_breadcrumbs'],
'show_pagenavi' => (int) $moduleConfig['show_pagenavi'],
];
MyDumpTool::decode($moduleinfo);
$config = [
'module' => $moduleinfo,
'categories' => $confCategories,
'contents' => $confContents,
];
MyDumpTool::saveJson('public/modules/'.$pico.'/index.json', $config);
}
function normalizeUrl($url, $base)
{
$localpath = function ($url) {
$purl1 = parse_url($url);
$purl2 = parse_url(XOOPS_URL);
if ($purl1['host'] === $purl2['host']) {
$url = preg_replace('/^http:/', 'https:', $url);
}
return $url;
};
if ('' === $url) {
return $localpath($base);
}
$purl = parse_url($url);
if (isset($purl['scheme'])) {
return $localpath($url);
}
$pburl = parse_url($base);
$path = $pburl['path'];
if ('/' == $url[0]) {
$path = '';
} elseif ('#' !== $url[0] && '?' !== $url[0]) {
$path = preg_replace('#/[^/]*$#', '', $path).'/';
}
$path .= $url;
$pattern = ['#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#'];
do {
$path = preg_replace($pattern, '/', $path, -1, $count);
} while (0 < $count);
return $localpath($pburl['scheme'].'://'.$pburl['host'].$path);
}
function downloadLocalFile($url)
{
$url2 = preg_replace('/'.preg_quote(XOOPS_URL, '/').'/', '', $url);
if (preg_match('/^\//', $url2)) {
if (preg_match('/\/[^\/]+\.(zip|pdf|doc|docx|xls|xlsx|ppt|pptx|txt|gif|jpg|jpeg|png|wmv|tar\.gz|mpg|obj|mtl|tif|avi)(\?.+)?$/i', $url2)) {
$spath = urldecode($url2);
$opath = MYDUMPTOOL_OUTPUTDIR.'/public'.$spath;
if (!file_exists($opath)) {
$odir = dirname($opath);
mkdir($odir, 0755, true);
MyDumpTool::fileDownload($url, $opath);
}
} elseif (!preg_match('/\/([^\/]+\.(php|html))?(\?.+)?(#.+)?$/i', $url2)) {
var_dump($url2);
}
$url = $url2;
}
return $url;
}
function normalizeHtml($text, $path)
{
$text = MyDumpTool::fixHtml($text);
$text = preg_replace_callback('/<img ([^>]*)src="(.*)"([^>]*)\/>/Us', function ($matches) use ($path) {
$url = htmlspecialchars_decode($matches[2], ENT_QUOTES);
$url = normalizeUrl($url, $path);
$url = downloadLocalFile($url);
return '<img '.$matches[1].'src="'.htmlspecialchars($url, ENT_QUOTES).'"'.$matches[3].'/>';
}, $text);
$text = preg_replace_callback('/<a ([^>]*)href="([^"]+)"([^>]*)>/Us', function ($matches) use ($path) {
$url = htmlspecialchars_decode($matches[2], ENT_QUOTES);
$url = normalizeUrl($url, $path);
$url = downloadLocalFile($url);
return '<a '.$matches[1].'href="'.htmlspecialchars($url, ENT_QUOTES).'"'.$matches[3].'>';
}, $text);
$text = preg_replace_callback('/<embed ([^>]*)src="(.*)"([^>]*)\/>/Us', function ($matches) use ($path) {
$url = htmlspecialchars_decode($matches[2], ENT_QUOTES);
$url = normalizeUrl($url, $path);
$url = downloadLocalFile($url);
return '<embed '.$matches[1].'src="'.htmlspecialchars($url, ENT_QUOTES).'"'.$matches[3].'/>';
}, $text);
$text = preg_replace('/ lang="([^"]+)"/Us', '', $text);
$text = preg_replace('/ xml:lang="([^"]+)"/Us', '', $text);
return $text;
}