Merge pull request #14039 from annando/widget-features
[friendica.git/.git] / src / Module / Help.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Text\Markdown;
27 use Friendica\DI;
28 use Friendica\Network\HTTPException;
29
30 /**
31  * Shows the friendica help based on the /doc/ directory
32  */
33 class Help extends BaseModule
34 {
35         protected function content(array $request = []): string
36         {
37                 Nav::setSelected('help');
38
39                 $text = '';
40                 $filename = '';
41
42                 $config = DI::config();
43                 $lang = DI::session()->get('language', $config->get('system', 'language'));
44
45                 // @TODO: Replace with parameter from router
46                 if (DI::args()->getArgc() > 1) {
47                         $path = '';
48                         // looping through the argv keys bigger than 0 to build
49                         // a path relative to /help
50                         for ($x = 1; $x < DI::args()->getArgc(); $x ++) {
51                                 if (strlen($path)) {
52                                         $path .= '/';
53                                 }
54
55                                 $path .= DI::args()->get($x);
56                         }
57                         $title = basename($path);
58                         $filename = $path;
59                         $text = self::loadDocFile('doc/' . $path . '.md', $lang);
60                         DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', $title);
61                 }
62
63                 $home = self::loadDocFile('doc/Home.md', $lang);
64                 if (!$text) {
65                         $text = $home;
66                         $filename = "Home";
67                         DI::page()['title'] = DI::l10n()->t('Help');
68                 } else {
69                         DI::page()['aside'] = Markdown::convert($home, false);
70                 }
71
72                 if (!strlen($text)) {
73                         throw new HTTPException\NotFoundException();
74                 }
75
76                 $html = Markdown::convert($text, false);
77
78                 if ($filename !== "Home") {
79                         // create TOC but not for home
80                         $lines = explode("\n", $html);
81                         $toc = "<h2>TOC</h2><ul id='toc'>";
82                         $lastLevel = 1;
83                         $idNum = [0, 0, 0, 0, 0, 0, 0];
84                         foreach ($lines as &$line) {
85                                 $matches = [];
86                                 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
87                                         $level = $matches[1];
88                                         $anchor = urlencode($matches[2]);
89                                         if ($level < $lastLevel) {
90                                                 for ($k = $level; $k < $lastLevel; $k++) {
91                                                         $toc .= "</ul></li>";
92                                                 }
93
94                                                 for ($k = $level + 1; $k < count($idNum); $k++) {
95                                                         $idNum[$k] = 0;
96                                                 }
97                                         }
98
99                                         if ($level > $lastLevel) {
100                                                 $toc .= "<li><ul>";
101                                         }
102
103                                         $idNum[$level] ++;
104
105                                         $href = "help/{$filename}#{$anchor}";
106                                         $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
107                                         $id = implode("_", array_slice($idNum, 1, $level));
108                                         $line = "<a name=\"{$id}\"></a>" . $line;
109                                         $line = "<a name=\"{$anchor}\"></a>" . $line;
110
111                                         $lastLevel = $level;
112                                 }
113                         }
114
115                         for ($k = 0; $k < $lastLevel; $k++) {
116                                 $toc .= "</ul>";
117                         }
118
119                         $html = implode("\n", $lines);
120
121                         DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
122                 }
123
124                 return $html;
125         }
126
127         private static function loadDocFile($fileName, $lang = 'en')
128         {
129                 $baseName = basename($fileName);
130                 $dirName = dirname($fileName);
131                 if (file_exists("$dirName/$lang/$baseName")) {
132                         return file_get_contents("$dirName/$lang/$baseName");
133                 }
134
135                 if (file_exists($fileName)) {
136                         return file_get_contents($fileName);
137                 }
138
139                 return '';
140         }
141 }