]> git.mxchange.org Git - friendica.git/blob - mod/help.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / help.php
1 <?php
2 /**
3  * @file mod/help.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Text\Markdown;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Util\Strings;
14
15 function load_doc_file($s)
16 {
17         $lang = Config::get('system', 'language');
18         $b = basename($s);
19         $d = dirname($s);
20         if (file_exists("$d/$lang/$b")) {
21                 return file_get_contents("$d/$lang/$b");
22         }
23
24         if (file_exists($s)) {
25                 return file_get_contents($s);
26         }
27
28         return '';
29 }
30
31 function help_content(App $a)
32 {
33         Nav::setSelected('help');
34
35         $text = '';
36         $filename = '';
37
38         if ($a->argc > 1) {
39                 $path = '';
40                 // looping through the argv keys bigger than 0 to build
41                 // a path relative to /help
42                 for ($x = 1; $x < $a->argc; $x ++) {
43                         if (strlen($path)) {
44                                 $path .= '/';
45                         }
46
47                         $path .= $a->getArgumentValue($x);
48                 }
49                 $title = basename($path);
50                 $filename = $path;
51                 $text = load_doc_file('doc/' . $path . '.md');
52                 $a->page['title'] = L10n::t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
53         }
54
55         $home = load_doc_file('doc/Home.md');
56         if (!$text) {
57                 $text = $home;
58                 $filename = "Home";
59                 $a->page['title'] = L10n::t('Help');
60         } else {
61                 $a->page['aside'] = Markdown::convert($home, false);
62         }
63
64         if (!strlen($text)) {
65                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . L10n::t('Not Found'));
66                 $tpl = Renderer::getMarkupTemplate("404.tpl");
67                 return Renderer::replaceMacros($tpl, [
68                         '$message' => L10n::t('Page not found.')
69                 ]);
70         }
71
72         $html = Markdown::convert($text, false);
73
74         if ($filename !== "Home") {
75                 // create TOC but not for home
76                 $lines = explode("\n", $html);
77                 $toc = "<h2>TOC</h2><ul id='toc'>";
78                 $lastlevel = 1;
79                 $idnum = [0, 0, 0, 0, 0, 0, 0];
80                 foreach ($lines as &$line) {
81                         if (substr($line, 0, 2) == "<h") {
82                                 $level = substr($line, 2, 1);
83                                 if ($level != "r") {
84                                         $level = intval($level);
85                                         if ($level < $lastlevel) {
86                                                 for ($k = $level; $k < $lastlevel; $k++) {
87                                                         $toc .= "</ul></li>";
88                                                 }
89
90                                                 for ($k = $level + 1; $k < count($idnum); $k++) {
91                                                         $idnum[$k] = 0;
92                                                 }
93                                         }
94
95                                         if ($level > $lastlevel) {
96                                                 $toc .= "<li><ul>";
97                                         }
98
99                                         $idnum[$level] ++;
100                                         $id = implode("_", array_slice($idnum, 1, $level));
101                                         $href = System::baseUrl() . "/help/{$filename}#{$id}";
102                                         $toc .= "<li><a href='{$href}'>" . strip_tags($line) . "</a></li>";
103                                         $line = "<a name='{$id}'></a>" . $line;
104                                         $lastlevel = $level;
105                                 }
106                         }
107                 }
108
109                 for ($k = 0; $k < $lastlevel; $k++) {
110                         $toc .= "</ul>";
111                 }
112
113                 $html = implode("\n", $lines);
114
115                 $a->page['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . $a->page['aside'] . '</div>';
116         }
117
118         return $html;
119 }