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