Menu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. namespace Widget;
  3. use Typecho\Common;
  4. use Widget\Plugins\Config;
  5. use Widget\Themes\Files;
  6. use Widget\Users\Edit as UsersEdit;
  7. use Widget\Contents\Attachment\Edit as AttachmentEdit;
  8. use Widget\Contents\Post\Edit as PostEdit;
  9. use Widget\Contents\Page\Edit as PageEdit;
  10. use Widget\Contents\Post\Admin as PostAdmin;
  11. use Widget\Comments\Admin as CommentsAdmin;
  12. use Widget\Metas\Category\Admin as CategoryAdmin;
  13. use Widget\Metas\Category\Edit as CategoryEdit;
  14. use Widget\Metas\Tag\Admin as TagAdmin;
  15. if (!defined('__TYPECHO_ROOT_DIR__')) {
  16. exit;
  17. }
  18. /**
  19. * 后台菜单显示
  20. *
  21. * @package Widget
  22. */
  23. class Menu extends Base
  24. {
  25. /**
  26. * 当前菜单标题
  27. * @var string
  28. */
  29. public $title;
  30. /**
  31. * 当前增加项目链接
  32. * @var string
  33. */
  34. public $addLink;
  35. /**
  36. * 父菜单列表
  37. *
  38. * @var array
  39. */
  40. private $menu = [];
  41. /**
  42. * 当前父菜单
  43. *
  44. * @var integer
  45. */
  46. private $currentParent = 1;
  47. /**
  48. * 当前子菜单
  49. *
  50. * @var integer
  51. */
  52. private $currentChild = 0;
  53. /**
  54. * 当前页面
  55. *
  56. * @var string
  57. */
  58. private $currentUrl;
  59. /**
  60. * 执行函数,初始化菜单
  61. */
  62. public function execute()
  63. {
  64. $parentNodes = [null, _t('控制台'), _t('撰写'), _t('管理'), _t('设置')];
  65. $childNodes = [
  66. [
  67. [_t('登录'), _t('登录到%s', $this->options->title), 'login.php', 'visitor'],
  68. [_t('注册'), _t('注册到%s', $this->options->title), 'register.php', 'visitor']
  69. ],
  70. [
  71. [_t('概要'), _t('网站概要'), 'index.php', 'subscriber'],
  72. [_t('个人设置'), _t('个人设置'), 'profile.php', 'subscriber'],
  73. [_t('插件'), _t('插件管理'), 'plugins.php', 'administrator'],
  74. [[Config::class, 'getMenuTitle'], [Config::class, 'getMenuTitle'], 'options-plugin.php?config=', 'administrator', true],
  75. [_t('外观'), _t('网站外观'), 'themes.php', 'administrator'],
  76. [[Files::class, 'getMenuTitle'], [Files::class, 'getMenuTitle'], 'theme-editor.php', 'administrator', true],
  77. [_t('设置外观'), _t('设置外观'), 'options-theme.php', 'administrator', true],
  78. [_t('备份'), _t('备份'), 'backup.php', 'administrator'],
  79. [_t('升级'), _t('升级程序'), 'upgrade.php', 'administrator', true],
  80. [_t('欢迎'), _t('欢迎使用'), 'welcome.php', 'subscriber', true]
  81. ],
  82. [
  83. [_t('撰写文章'), _t('撰写新文章'), 'write-post.php', 'contributor'],
  84. [[PostEdit::class, 'getMenuTitle'], [PostEdit::class, 'getMenuTitle'], 'write-post.php?cid=', 'contributor', true],
  85. [_t('创建页面'), _t('创建新页面'), 'write-page.php', 'editor'],
  86. [[PageEdit::class, 'getMenuTitle'], [PageEdit::class, 'getMenuTitle'], 'write-page.php?cid=', 'editor', true],
  87. ],
  88. [
  89. [_t('文章'), _t('管理文章'), 'manage-posts.php', 'contributor', false, 'write-post.php'],
  90. [[PostAdmin::class, 'getMenuTitle'], [PostAdmin::class, 'getMenuTitle'], 'manage-posts.php?uid=', 'contributor', true],
  91. [_t('独立页面'), _t('管理独立页面'), 'manage-pages.php', 'editor', false, 'write-page.php'],
  92. [_t('评论'), _t('管理评论'), 'manage-comments.php', 'contributor'],
  93. [[CommentsAdmin::class, 'getMenuTitle'], [CommentsAdmin::class, 'getMenuTitle'], 'manage-comments.php?cid=', 'contributor', true],
  94. [_t('分类'), _t('管理分类'), 'manage-categories.php', 'editor', false, 'category.php'],
  95. [_t('新增分类'), _t('新增分类'), 'category.php', 'editor', true],
  96. [[CategoryAdmin::class, 'getMenuTitle'], [CategoryAdmin::class, 'getMenuTitle'], 'manage-categories.php?parent=', 'editor', true, [CategoryAdmin::class, 'getAddLink']],
  97. [[CategoryEdit::class, 'getMenuTitle'], [CategoryEdit::class, 'getMenuTitle'], 'category.php?mid=', 'editor', true],
  98. [[CategoryEdit::class, 'getMenuTitle'], [CategoryEdit::class, 'getMenuTitle'], 'category.php?parent=', 'editor', true],
  99. [_t('标签'), _t('管理标签'), 'manage-tags.php', 'editor'],
  100. [[TagAdmin::class, 'getMenuTitle'], [TagAdmin::class, 'getMenuTitle'], 'manage-tags.php?mid=', 'editor', true],
  101. [_t('文件'), _t('管理文件'), 'manage-medias.php', 'editor'],
  102. [[AttachmentEdit::class, 'getMenuTitle'], [AttachmentEdit::class, 'getMenuTitle'], 'media.php?cid=', 'contributor', true],
  103. [_t('用户'), _t('管理用户'), 'manage-users.php', 'administrator', false, 'user.php'],
  104. [_t('新增用户'), _t('新增用户'), 'user.php', 'administrator', true],
  105. [[UsersEdit::class, 'getMenuTitle'], [UsersEdit::class, 'getMenuTitle'], 'user.php?uid=', 'administrator', true],
  106. ],
  107. [
  108. [_t('基本'), _t('基本设置'), 'options-general.php', 'administrator'],
  109. [_t('评论'), _t('评论设置'), 'options-discussion.php', 'administrator'],
  110. [_t('阅读'), _t('阅读设置'), 'options-reading.php', 'administrator'],
  111. [_t('永久链接'), _t('永久链接设置'), 'options-permalink.php', 'administrator'],
  112. ]
  113. ];
  114. /** 获取扩展菜单 */
  115. $panelTable = unserialize($this->options->panelTable);
  116. $extendingParentMenu = empty($panelTable['parent']) ? [] : $panelTable['parent'];
  117. $extendingChildMenu = empty($panelTable['child']) ? [] : $panelTable['child'];
  118. $currentUrl = $this->request->getRequestUrl();
  119. $adminUrl = $this->options->adminUrl;
  120. $menu = [];
  121. $defaultChildNode = [null, null, null, 'administrator', false, null];
  122. $currentUrlParts = parse_url($currentUrl);
  123. $currentUrlParams = [];
  124. if (!empty($currentUrlParts['query'])) {
  125. parse_str($currentUrlParts['query'], $currentUrlParams);
  126. }
  127. if ('/' == $currentUrlParts['path'][strlen($currentUrlParts['path']) - 1]) {
  128. $currentUrlParts['path'] .= 'index.php';
  129. }
  130. foreach ($extendingParentMenu as $key => $val) {
  131. $parentNodes[10 + $key] = $val;
  132. }
  133. foreach ($extendingChildMenu as $key => $val) {
  134. $childNodes[$key] = array_merge($childNodes[$key] ?? [], $val);
  135. }
  136. foreach ($parentNodes as $key => $parentNode) {
  137. // this is a simple struct than before
  138. $children = [];
  139. $showedChildrenCount = 0;
  140. $firstUrl = null;
  141. foreach ($childNodes[$key] as $inKey => $childNode) {
  142. // magic merge
  143. $childNode += $defaultChildNode;
  144. [$name, $title, $url, $access] = $childNode;
  145. $hidden = $childNode[4] ?? false;
  146. $addLink = $childNode[5] ?? null;
  147. // 保存最原始的hidden信息
  148. $orgHidden = $hidden;
  149. // parse url
  150. $url = Common::url($url, $adminUrl);
  151. // compare url
  152. $urlParts = parse_url($url);
  153. $urlParams = [];
  154. if (!empty($urlParts['query'])) {
  155. parse_str($urlParts['query'], $urlParams);
  156. }
  157. $validate = true;
  158. if ($urlParts['path'] != $currentUrlParts['path']) {
  159. $validate = false;
  160. } else {
  161. foreach ($urlParams as $paramName => $paramValue) {
  162. if (!isset($currentUrlParams[$paramName])) {
  163. $validate = false;
  164. break;
  165. }
  166. }
  167. }
  168. if (
  169. $validate
  170. && basename($urlParts['path']) == 'extending.php'
  171. && !empty($currentUrlParams['panel']) && !empty($urlParams['panel'])
  172. && $urlParams['panel'] != $currentUrlParams['panel']
  173. ) {
  174. $validate = false;
  175. }
  176. if ($hidden && $validate) {
  177. $hidden = false;
  178. }
  179. if (!$hidden && !$this->user->pass($access, true)) {
  180. $hidden = true;
  181. }
  182. if (!$hidden) {
  183. $showedChildrenCount++;
  184. if (empty($firstUrl)) {
  185. $firstUrl = $url;
  186. }
  187. if (is_array($name)) {
  188. [$widget, $method] = $name;
  189. $name = self::widget($widget)->$method();
  190. }
  191. if (is_array($title)) {
  192. [$widget, $method] = $title;
  193. $title = self::widget($widget)->$method();
  194. }
  195. if (is_array($addLink)) {
  196. [$widget, $method] = $addLink;
  197. $addLink = self::widget($widget)->$method();
  198. }
  199. }
  200. if ($validate) {
  201. if ('visitor' != $access) {
  202. $this->user->pass($access);
  203. }
  204. $this->currentParent = $key;
  205. $this->currentChild = $inKey;
  206. $this->title = $title;
  207. $this->addLink = $addLink ? Common::url($addLink, $adminUrl) : null;
  208. }
  209. $children[$inKey] = [
  210. $name,
  211. $title,
  212. $url,
  213. $access,
  214. $hidden,
  215. $addLink,
  216. $orgHidden
  217. ];
  218. }
  219. $menu[$key] = [$parentNode, $showedChildrenCount > 0, $firstUrl, $children];
  220. }
  221. $this->menu = $menu;
  222. $this->currentUrl = Common::safeUrl($currentUrl);
  223. }
  224. /**
  225. * 获取当前菜单
  226. *
  227. * @return array
  228. */
  229. public function getCurrentMenu(): ?array
  230. {
  231. return $this->currentParent > 0 ? $this->menu[$this->currentParent][3][$this->currentChild] : null;
  232. }
  233. /**
  234. * 输出父级菜单
  235. */
  236. public function output($class = 'focus', $childClass = 'focus')
  237. {
  238. foreach ($this->menu as $key => $node) {
  239. if (!$node[1] || !$key) {
  240. continue;
  241. }
  242. echo "<ul class=\"root" . ($key == $this->currentParent ? ' ' . $class : null)
  243. . "\"><li class=\"parent\"><a href=\"{$node[2]}\">{$node[0]}</a>"
  244. . "</li><ul class=\"child\">";
  245. $last = 0;
  246. foreach ($node[3] as $inKey => $inNode) {
  247. if (!$inNode[4]) {
  248. $last = $inKey;
  249. }
  250. }
  251. foreach ($node[3] as $inKey => $inNode) {
  252. if ($inNode[4]) {
  253. continue;
  254. }
  255. $classes = [];
  256. if ($key == $this->currentParent && $inKey == $this->currentChild) {
  257. $classes[] = $childClass;
  258. } elseif ($inNode[6]) {
  259. continue;
  260. }
  261. if ($inKey == $last) {
  262. $classes[] = 'last';
  263. }
  264. echo "<li" . (!empty($classes) ? ' class="' . implode(' ', $classes) . '"' : null) . "><a href=\""
  265. . ($key == $this->currentParent && $inKey == $this->currentChild ? $this->currentUrl : $inNode[2])
  266. . "\">{$inNode[0]}</a></li>";
  267. }
  268. echo "</ul></ul>";
  269. }
  270. }
  271. }