]> git.mxchange.org Git - friendica.git/blob - src/Module/Help.php
Added type hints
[friendica.git] / src / Module / Help.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Text\Markdown;
8 use Friendica\Core\L10n;
9 use Friendica\DI;
10 use Friendica\Network\HTTPException;
11 use Friendica\Util\Strings;
12
13 /**
14  * Shows the friendica help based on the /doc/ directory
15  */
16 class Help extends BaseModule
17 {
18         public static function content(array $parameters = [])
19         {
20                 Nav::setSelected('help');
21
22                 $text = '';
23                 $filename = '';
24
25                 $a = DI::app();
26                 $config = DI::config();
27                 $lang = $config->get('system', 'language');
28
29                 // @TODO: Replace with parameter from router
30                 if ($a->argc > 1) {
31                         $path = '';
32                         // looping through the argv keys bigger than 0 to build
33                         // a path relative to /help
34                         for ($x = 1; $x < $a->argc; $x ++) {
35                                 if (strlen($path)) {
36                                         $path .= '/';
37                                 }
38
39                                 $path .= DI::args()->get($x);
40                         }
41                         $title = basename($path);
42                         $filename = $path;
43                         $text = self::loadDocFile('doc/' . $path . '.md', $lang);
44                         DI::page()['title'] = L10n::t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
45                 }
46
47                 $home = self::loadDocFile('doc/Home.md', $lang);
48                 if (!$text) {
49                         $text = $home;
50                         $filename = "Home";
51                         DI::page()['title'] = L10n::t('Help');
52                 } else {
53                         DI::page()['aside'] = Markdown::convert($home, false);
54                 }
55
56                 if (!strlen($text)) {
57                         throw new HTTPException\NotFoundException();
58                 }
59
60                 $html = Markdown::convert($text, false);
61
62                 if ($filename !== "Home") {
63                         // create TOC but not for home
64                         $lines = explode("\n", $html);
65                         $toc = "<h2>TOC</h2><ul id='toc'>";
66                         $lastLevel = 1;
67                         $idNum = [0, 0, 0, 0, 0, 0, 0];
68                         foreach ($lines as &$line) {
69                                 $matches = [];
70                                 foreach ($lines as &$line) {
71                                         if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
72                                                 $level = $matches[1];
73                                                 $anchor = urlencode($matches[2]);
74                                                 if ($level < $lastLevel) {
75                                                         for ($k = $level; $k < $lastLevel; $k++) {
76                                                                 $toc .= "</ul></li>";
77                                                         }
78
79                                                         for ($k = $level + 1; $k < count($idNum); $k++) {
80                                                                 $idNum[$k] = 0;
81                                                         }
82                                                 }
83
84                                                 if ($level > $lastLevel) {
85                                                         $toc .= "<li><ul>";
86                                                 }
87
88                                                 $idNum[$level] ++;
89
90                                                 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
91                                                 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
92                                                 $id = implode("_", array_slice($idNum, 1, $level));
93                                                 $line = "<a name=\"{$id}\"></a>" . $line;
94                                                 $line = "<a name=\"{$anchor}\"></a>" . $line;
95
96                                                 $lastLevel = $level;
97                                         }
98                                 }
99                         }
100
101                         for ($k = 0; $k < $lastLevel; $k++) {
102                                 $toc .= "</ul>";
103                         }
104
105                         $html = implode("\n", $lines);
106
107                         DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
108                 }
109
110                 return $html;
111         }
112
113         private static function loadDocFile($fileName, $lang = 'en')
114         {
115                 $baseName = basename($fileName);
116                 $dirName = dirname($fileName);
117                 if (file_exists("$dirName/$lang/$baseName")) {
118                         return file_get_contents("$dirName/$lang/$baseName");
119                 }
120
121                 if (file_exists($fileName)) {
122                         return file_get_contents($fileName);
123                 }
124
125                 return '';
126         }
127 }