streams/Code/Module/Help.php

170 lines
5.5 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2016-04-19 03:38:38 +00:00
2021-03-02 02:50:41 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Lib\System;
use Code\Lib\Navbar;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2016-04-19 03:38:38 +00:00
require_once('include/help.php');
/**
2021-12-03 03:01:39 +00:00
* You can create local site resources in doc/site
2016-04-19 03:38:38 +00:00
*/
2021-12-02 23:02:31 +00:00
class Help extends Controller
{
public function get()
{
2022-01-25 04:16:38 +00:00
Navbar::set_selected('Help');
2021-12-02 23:02:31 +00:00
if ($_REQUEST['search']) {
2022-09-04 01:35:50 +00:00
$o = '<div id="help-content" class="generic-content-wrapper">';
2021-12-02 23:02:31 +00:00
$o .= '<div class="section-title-wrapper">';
$o .= '<h2>' . t('Documentation Search') . ' - ' . htmlspecialchars($_REQUEST['search']) . '</h2>';
$o .= '</div>';
$o .= '<div class="section-content-wrapper">';
$r = search_doc_files($_REQUEST['search']);
if ($r) {
$o .= '<ul class="help-searchlist">';
foreach ($r as $rr) {
$dirname = dirname($rr['v']);
$fname = basename($rr['v']);
$fname = substr($fname, 0, strrpos($fname, '.'));
$path = trim(substr($dirname, 4), '/');
$o .= '<li><a href="help/' . (($path) ? $path . '/' : '') . $fname . '" >' . ucwords(str_replace('_', ' ', notags($fname))) . '</a><br>'
. '<b><i>' . 'help/' . (($path) ? $path . '/' : '') . $fname . '</i></b><br>'
. '...' . str_replace('$Projectname', System::get_platform_name(), $rr['text']) . '...<br><br></li>';
}
$o .= '</ul>';
$o .= '</div>';
$o .= '</div>';
}
return $o;
}
if (argc() > 2 && argv(argc() - 2) === 'assets') {
$path = '';
for ($x = 1; $x < argc(); $x++) {
2021-12-03 03:01:39 +00:00
if (strlen($path)) {
2021-12-02 23:02:31 +00:00
$path .= '/';
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$path .= argv($x);
}
$realpath = 'doc/' . $path;
//Set the content-type header as appropriate
$imageInfo = getimagesize($realpath);
switch ($imageInfo[2]) {
case IMAGETYPE_JPEG:
header("Content-Type: image/jpeg");
break;
case IMAGETYPE_GIF:
header("Content-Type: image/gif");
break;
case IMAGETYPE_PNG:
header("Content-Type: image/png");
break;
default:
break;
}
header("Content-Length: " . filesize($realpath));
// dump the picture and stop the script
readfile($realpath);
killme();
}
2022-09-04 06:28:17 +00:00
$heading = '';
2023-02-09 19:45:30 +00:00
2021-12-02 23:02:31 +00:00
if (argc() === 1) {
2023-02-09 19:45:30 +00:00
$cmd = 'doc';
}
else {
$cmd = str_replace('help/', 'doc/', App::$cmd);
}
2022-09-04 06:28:17 +00:00
2023-02-09 19:45:30 +00:00
if (! is_dir($cmd)) {
$content = get_help_content();
}
else {
$files = self::listdir($cmd);
2021-12-02 23:02:31 +00:00
if ($files) {
2023-02-09 19:45:30 +00:00
usort($files, ['self', 'usort_basename']);
2021-12-02 23:02:31 +00:00
foreach ($files as $file) {
2023-02-09 19:45:30 +00:00
$language = '';
2023-02-01 20:45:21 +00:00
2021-12-02 23:02:31 +00:00
if ((!strpos($file, '/site/')) && file_exists(str_replace('doc/', 'doc/site/', $file))) {
2023-02-09 19:45:30 +00:00
$file = str_replace('doc/', 'doc/site/', $file);
}
if (!is_dir($file) && !str_contains(z_mime_content_type($file), 'text')) {
2021-12-02 23:02:31 +00:00
continue;
}
if (strpos($file, 'README')) {
continue;
}
2022-09-04 06:28:17 +00:00
if (preg_match('/\/(..|..-..)\//', $file, $matches)) {
2021-12-02 23:02:31 +00:00
$language = $matches[1];
2023-02-09 19:45:30 +00:00
if ($language === substr(App::$language, 0, 2)) {
$language = '';
}
2021-12-02 23:02:31 +00:00
}
2023-02-01 20:45:21 +00:00
$link = str_replace(['doc/', '.mc', '.txt'], ['help/', '', ''], $file);
2023-02-09 19:45:30 +00:00
$displayName = str_replace('_', ' ', $link);
2022-09-04 01:35:50 +00:00
if (str_contains($link, '/global/') || str_contains($link, '/media/')) {
2021-12-02 23:02:31 +00:00
continue;
}
2023-02-01 20:45:21 +00:00
$content .= '<div class="nav-pills"><a href="' . $link . '">' . ucfirst(basename($displayName)) . '</a>' . (($language) ? " [$language]" : '') . '</div>' . EOL;
2021-12-02 23:02:31 +00:00
}
}
}
2022-09-04 01:35:50 +00:00
return replace_macros(Theme::get_template('help.tpl'), [
2021-12-02 23:02:31 +00:00
'$title' => t('$Projectname Documentation'),
'$tocHeading' => t('Contents'),
'$content' => $content,
'$heading' => $heading,
'$language' => $language
2022-09-04 01:35:50 +00:00
]);
2021-12-02 23:02:31 +00:00
}
2023-02-01 09:06:24 +00:00
public static function usort_basename($a,$b) {
return strcasecmp(basename($a), basename($b));
}
2021-12-02 23:02:31 +00:00
public static function listdir($path)
{
$results = [];
$handle = opendir($path);
if (!$handle) {
return $results;
}
2023-02-09 19:45:30 +00:00
if ($path === 'doc') {
2023-02-11 05:33:51 +00:00
return ['doc/guide', 'doc/develop', 'doc/site'];
2023-02-09 19:45:30 +00:00
}
2021-12-02 23:02:31 +00:00
while (false !== ($file = readdir($handle))) {
if ($file === '.' || $file === '..') {
continue;
}
2023-02-13 10:51:17 +00:00
$fileName = $path . '/' . $file;
if (is_dir($fileName)) {
$results = array_merge($results, self::listdir($fileName));
2021-12-02 23:02:31 +00:00
} else {
2023-02-13 10:51:17 +00:00
$results[] = $fileName;
2021-12-02 23:02:31 +00:00
}
}
closedir($handle);
return $results;
}
2016-04-19 03:38:38 +00:00
}