|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione |
| 5 | + * Copyright (C) DevCode s.r.l. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (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 General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +use Illuminate\Database\Eloquent\Collection; |
| 22 | +use Models\Link; |
| 23 | +use Models\Module; |
| 24 | +use Models\Plugin; |
| 25 | + |
| 26 | +/** |
| 27 | + * Helper per il rendering delle voci navbar dalla tabella `zz_links`. |
| 28 | + * |
| 29 | + * `renderRight()` / `renderLeft()` ritornano HTML pronto per essere concatenato |
| 30 | + * dentro `include/top.php`. Ogni campo DB è escapato via `e()`. |
| 31 | + */ |
| 32 | +class NavbarLinks |
| 33 | +{ |
| 34 | + /** Regex di sicurezza per value di type=javascript (nome funzione globale). */ |
| 35 | + private const JS_VALUE_REGEX = '/^[a-zA-Z_$][a-zA-Z0-9_$.]*$/'; |
| 36 | + |
| 37 | + public static function renderRight(): string |
| 38 | + { |
| 39 | + return self::renderPosition('right'); |
| 40 | + } |
| 41 | + |
| 42 | + public static function renderLeft(): string |
| 43 | + { |
| 44 | + return self::renderPosition('left'); |
| 45 | + } |
| 46 | + |
| 47 | + private static function renderPosition(string $position): string |
| 48 | + { |
| 49 | + $links = self::getTopLevelLinks($position); |
| 50 | + |
| 51 | + $html = ''; |
| 52 | + foreach ($links as $link) { |
| 53 | + if (!self::visible($link)) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + $html .= self::renderItem($link); |
| 57 | + } |
| 58 | + |
| 59 | + return $html; |
| 60 | + } |
| 61 | + |
| 62 | + private static function getTopLevelLinks(string $position): Collection |
| 63 | + { |
| 64 | + return Link::query() |
| 65 | + ->where('position', $position) |
| 66 | + ->whereNull('parent') |
| 67 | + ->where('enabled', 1) |
| 68 | + ->orderBy('order') |
| 69 | + ->with(['children' => function ($q) { |
| 70 | + $q->where('enabled', 1)->orderBy('order'); |
| 71 | + }]) |
| 72 | + ->get(); |
| 73 | + } |
| 74 | + |
| 75 | + private static function renderItem(Link $link): string |
| 76 | + { |
| 77 | + if ($link->hasChildren()) { |
| 78 | + return self::renderDropdown($link); |
| 79 | + } |
| 80 | + |
| 81 | + return self::renderSingle($link); |
| 82 | + } |
| 83 | + |
| 84 | + private static function renderSingle(Link $link): string |
| 85 | + { |
| 86 | + $iconClass = trim($link->icon.' nav-icon '.(string) $link->color); |
| 87 | + |
| 88 | + return '<li class="nav-item">' |
| 89 | + .'<a href="'.e(self::url($link)).'" class="nav-link" ' |
| 90 | + .self::onclickAttr($link).' ' |
| 91 | + .self::targetAttr($link).' ' |
| 92 | + .'title="'.e($link->title).'">' |
| 93 | + .'<i class="'.e($iconClass).'"></i>' |
| 94 | + .'</a>' |
| 95 | + .'</li>'; |
| 96 | + } |
| 97 | + |
| 98 | + private static function renderDropdown(Link $link): string |
| 99 | + { |
| 100 | + $iconClass = trim($link->icon.' nav-icon '.(string) $link->color); |
| 101 | + |
| 102 | + $items = ''; |
| 103 | + foreach ($link->children as $child) { |
| 104 | + if (!self::visible($child)) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + $childIcon = trim($child->icon.' '.(string) $child->color); |
| 108 | + $items .= '<a class="dropdown-item" href="'.e(self::url($child)).'" ' |
| 109 | + .self::onclickAttr($child).' ' |
| 110 | + .self::targetAttr($child).' ' |
| 111 | + .'title="'.e($child->title).'">' |
| 112 | + .'<i class="'.e($childIcon).'"></i> ' |
| 113 | + .e($child->label) |
| 114 | + .'</a>'; |
| 115 | + } |
| 116 | + |
| 117 | + return '<li class="nav-item dropdown">' |
| 118 | + .'<a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" ' |
| 119 | + .'title="'.e($link->title).'">' |
| 120 | + .'<i class="'.e($iconClass).'"></i>' |
| 121 | + .'</a>' |
| 122 | + .'<div class="dropdown-menu dropdown-menu-right">' |
| 123 | + .$items |
| 124 | + .'</div>' |
| 125 | + .'</li>'; |
| 126 | + } |
| 127 | + |
| 128 | + public static function url(Link $link): string |
| 129 | + { |
| 130 | + switch ($link->type) { |
| 131 | + case 'link': |
| 132 | + return (string) $link->value; |
| 133 | + |
| 134 | + case 'javascript': |
| 135 | + return '#'; |
| 136 | + |
| 137 | + case 'module': |
| 138 | + $mod = Module::where('name', $link->value)->first(); |
| 139 | + if (!$mod) { |
| 140 | + return '#'; |
| 141 | + } |
| 142 | + |
| 143 | + return base_path_osm().'/controller.php?id_module='.$mod->id; |
| 144 | + |
| 145 | + case 'plugin': |
| 146 | + $plg = Plugin::where('name', $link->value)->first(); |
| 147 | + if (!$plg) { |
| 148 | + return '#'; |
| 149 | + } |
| 150 | + |
| 151 | + return base_path_osm().'/plugin_editor.php?id_plugin='.$plg->id; |
| 152 | + } |
| 153 | + |
| 154 | + return '#'; |
| 155 | + } |
| 156 | + |
| 157 | + public static function onclickAttr(Link $link): string |
| 158 | + { |
| 159 | + if ($link->type !== 'javascript') { |
| 160 | + return ''; |
| 161 | + } |
| 162 | + |
| 163 | + $fn = (string) $link->value; |
| 164 | + if (!preg_match(self::JS_VALUE_REGEX, $fn)) { |
| 165 | + return ''; |
| 166 | + } |
| 167 | + |
| 168 | + return 'onclick="if(typeof window[\''.$fn.'\']===\'function\'){window[\''.$fn.'\']();}return false;"'; |
| 169 | + } |
| 170 | + |
| 171 | + public static function targetAttr(Link $link): string |
| 172 | + { |
| 173 | + return $link->type === 'link' ? 'target="_blank"' : ''; |
| 174 | + } |
| 175 | + |
| 176 | + public static function visible(Link $link): bool |
| 177 | + { |
| 178 | + if (!$link->enabled) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + if ($link->type === 'module') { |
| 183 | + $mod = Module::where('name', $link->value)->first(); |
| 184 | + if (!$mod || !$mod->enabled) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + return Modules::getPermission($mod->id) !== '-'; |
| 189 | + } |
| 190 | + |
| 191 | + if ($link->type === 'plugin') { |
| 192 | + $plg = Plugin::where('name', $link->value)->first(); |
| 193 | + if (!$plg || !$plg->enabled) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + return $plg->permission !== '-'; |
| 198 | + } |
| 199 | + |
| 200 | + if ($link->id_module) { |
| 201 | + return Modules::getPermission($link->id_module) !== '-'; |
| 202 | + } |
| 203 | + |
| 204 | + return true; |
| 205 | + } |
| 206 | + |
| 207 | + public static function validateValue(string $type, string $value): bool |
| 208 | + { |
| 209 | + if ($type === 'javascript') { |
| 210 | + return (bool) preg_match(self::JS_VALUE_REGEX, $value); |
| 211 | + } |
| 212 | + |
| 213 | + return true; |
| 214 | + } |
| 215 | +} |
0 commit comments