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