]> git.mxchange.org Git - friendica.git/blob - src/Module/Help.php
Merge pull request #10166 from mexon/mat/refactor-user-arguments
[friendica.git] / src / Module / Help.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 use Friendica\Util\Strings;
30
31 /**
32  * Shows the friendica help based on the /doc/ directory
33  */
34 class Help extends BaseModule
35 {
36         public static function content(array $parameters = [])
37         {
38                 Nav::setSelected('help');
39
40                 $text = '';
41                 $filename = '';
42
43                 $a = DI::app();
44                 $config = DI::config();
45                 $lang = $config->get('system', 'language');
46
47                 // @TODO: Replace with parameter from router
48                 if ($a->argc > 1) {
49                         $path = '';
50                         // looping through the argv keys bigger than 0 to build
51                         // a path relative to /help
52                         for ($x = 1; $x < $a->argc; $x ++) {
53                                 if (strlen($path)) {
54                                         $path .= '/';
55                                 }
56
57                                 $path .= DI::args()->get($x);
58                         }
59                         $title = basename($path);
60                         $filename = $path;
61                         $text = self::loadDocFile('doc/' . $path . '.md', $lang);
62                         DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
63                 }
64
65                 $home = self::loadDocFile('doc/Home.md', $lang);
66                 if (!$text) {
67                         $text = $home;
68                         $filename = "Home";
69                         DI::page()['title'] = DI::l10n()->t('Help');
70                 } else {
71                         DI::page()['aside'] = Markdown::convert($home, false);
72                 }
73
74                 if (!strlen($text)) {
75                         throw new HTTPException\NotFoundException();
76                 }
77
78                 $html = Markdown::convert($text, false);
79
80                 if ($filename !== "Home") {
81                         // create TOC but not for home
82                         $lines = explode("\n", $html);
83                         $toc = "<h2>TOC</h2><ul id='toc'>";
84                         $lastLevel = 1;
85                         $idNum = [0, 0, 0, 0, 0, 0, 0];
86                         foreach ($lines as &$line) {
87                                 $matches = [];
88                                 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
89                                         $level = $matches[1];
90                                         $anchor = urlencode($matches[2]);
91                                         if ($level < $lastLevel) {
92                                                 for ($k = $level; $k < $lastLevel; $k++) {
93                                                         $toc .= "</ul></li>";
94                                                 }
95
96                                                 for ($k = $level + 1; $k < count($idNum); $k++) {
97                                                         $idNum[$k] = 0;
98                                                 }
99                                         }
100
101                                         if ($level > $lastLevel) {
102                                                 $toc .= "<li><ul>";
103                                         }
104
105                                         $idNum[$level] ++;
106
107                                         $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
108                                         $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
109                                         $id = implode("_", array_slice($idNum, 1, $level));
110                                         $line = "<a name=\"{$id}\"></a>" . $line;
111                                         $line = "<a name=\"{$anchor}\"></a>" . $line;
112
113                                         $lastLevel = $level;
114                                 }
115                         }
116
117                         for ($k = 0; $k < $lastLevel; $k++) {
118                                 $toc .= "</ul>";
119                         }
120
121                         $html = implode("\n", $lines);
122
123                         DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
124                 }
125
126                 return $html;
127         }
128
129         private static function loadDocFile($fileName, $lang = 'en')
130         {
131                 $baseName = basename($fileName);
132                 $dirName = dirname($fileName);
133                 if (file_exists("$dirName/$lang/$baseName")) {
134                         return file_get_contents("$dirName/$lang/$baseName");
135                 }
136
137                 if (file_exists($fileName)) {
138                         return file_get_contents($fileName);
139                 }
140
141                 return '';
142         }
143 }