Плюшки админки - main/w2/admin.php
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <?php # For Licence and Disclaimer of this code, see http://coresky.net/license function pad($n = 3) { return str_repeat(' ', $n); } function pad00($str, $n = 2) { return str_pad($str, $n, 0, STR_PAD_LEFT); } class Admin { static $adm = ['first_page' => 'auth']; static $menu = []; private $_file = ''; const button = '<a href="?%2$s" class="admin-btn%3$s">%1$s</a>'; function __get($name) { global $sky; if ($name && '_' == $name[1]) switch($name[0]) { case 'a': return SKY::$mem['a'][3][substr($name, 2)]; case 's': return SKY::$mem['s'][3][substr($name, 2)]; case 't': $sky->me = substr($name, 2); case 'm': return MVC::instance()->$name; } return self::$adm[$name]; } function __set($name, $value) { global $sky; if ('a_' == substr($name, 0, 2)) { SKY::a(substr($name, 2), $value); } elseif ('y_' == substr($name, 0, 2)) { $sky->reg['_y'][$name] = $value; } else { self::$adm[$name] = $value; } } function use_front($name) { MVC::instance(); return view($name, true); # can be controller name or callback function } function get_file() { return $this->_file; } static function section($url, $re = false) { global $sky; $re = $re ? "($re|adm(\?.*)?)$~" : "adm(\?.*)?$~"; return preg_match("~$re", $url, $m) ? $m : false; // $sky->re . } static function out($out, $is_html = true) { # check Admin::out() for XSS if (is_array($out)) { echo th(0 === $is_html ? ['','',''] : ['', 'NAME', 'VALUE'], 'id="table"'); for ($i = 0; list($k, $v) = each($out); ) { is_string($v) or is_int($v) or $v = print_r($v, true); if ($is_html) $v = html($v); echo td([1 + $i, [$k, 'style="min-width:100px"'], $v], eval(zebra)); } echo '</table>'; } else { echo tag($out, 'id="pre-out"', 'pre'); } } static function top_menu($pid) { global $user; if ($user->pid != $pid) { $pos = array_search('main', self::$adm['files']); if (false === $pos || !in_array($pos, self::$adm['cr'])) return; } $ary = isset(self::$menu[$pid]) ? explode("\t", self::$menu[$pid]) : []; self::$adm['rows'] = $ary ? array_shift($ary) : 0; self::$adm['cr'] = $ary; } static function access($ajax_adm) { global $sky, $user; list(, $tmemo) = sqlf('-select imemo, tmemo from $_memory where id=8'); SKY::ghost('a', $tmemo, 'update $_memory set dt=now(), tmemo=%s where id=8'); $menu = SKY::$mem['a'][3]['menu'] or Rare::root_menu($sky); self::$menu = unserialize($menu); self::$adm = [ 'files' => explode("\t", self::$menu[-2]), 'names' => explode("\t", self::$menu[-1]), 'uris' => explode("\t", self::$menu[0]), ]; self::top_menu($user->pid); if (!self::$adm['rows'] || !self::$adm['cr']) return false; $sky->adm_able = true; self::$adm['first_page'] = 'adm?' . self::$adm['uris'][current(self::$adm['cr'])]; if ($adm_uri = 1 == $sky->ajax ? $ajax_adm : 1 == count($sky->surl) && 'adm' == $sky->surl[0]) $sky->is_front = $sky->extra = false; if (!$adm_uri || 2 != $user->auth) return $adm_uri; $me = new Admin; $pos = -1; if (1 == $sky->ajax) { $pos = array_search($sky->_0, $me->files); false !== $pos or $pos = -1; } else { $uri = substr(URI, 4); # cut `adm?` foreach ($me->uris as $p => $u) { if ($u === substr($uri, 0, strlen($u))) { $pos = $p; break; } } } if ($pos >= 0 && in_array($pos, $me->cr) && is_file($file = 'admin/_' . ($sky->me = $me->files[$pos]) . '.php')) $me->_file = $file; if (!$me->_file && $sky->ajax) throw new Err('admin ajax, no file: ' . $file); $me->_title = $me->_file ? $me->names[$pos] : 'File not found'; return $me; } static function pages($ipp, $cnt = null, $ipl = 7, $throw = false) { list($limit, $pages, $cnt) = pagination($ipp, $cnt, $ipl, null, $throw); if (!$pages) return [0, 'Pages: 1', $cnt]; $html = ''; $tpl = '<li%s><a href="%s">%s</a></li>'; $br = $pages->br[0] != 1 || $pages->br[1] != $pages->last; if ($br) $html .= sprintf($tpl . $tpl, '', $pages->a_first, '«', '', $pages->a_prev, '‹'); $html .= $pages->left . sprintf($tpl, ' class="active"', $pages->a_current, $pages->current) . $pages->right; if ($br) $html .= sprintf($tpl . $tpl, '', $pages->a_next, '›', '', $pages->a_last, '»'); return [$limit, "Pages: <ul class=\"pagination\">$html</ul>", $cnt]; } function process($delete = false) { global $sky; if ('list' == $sky->k_type) return qp(SQL::PARSE, 'select * from $_ '); if ('show' == $sky->k_type || 'edit' == $sky->k_type && !$_POST) return qp('select * from $_ where id=$.', $_GET['id']); if ('delete' == $sky->k_type) { $cnt = sql('delete from $_ where id=$.', $_GET['id']); if ($delete) $delete($cnt); jump(me); } if (!$_POST) return; if ('new' == $sky->k_type) (new Rare)->insert(substr(me, 1)); if ('edit' == $sky->k_type) (new Rare)->update(substr(me, 1), $_GET['id']); jump(me); } static function drop_all_cache() { global $sky; $sky->s_contr = ''; $dirs = ['var/cache', 'var/gate', 'var/jet', 'var/extra']; $result = 1; foreach ($dirs as $dir) { foreach (glob("$dir/*.php") as $fn) $result &= (int)unlink($fn); } return $result; } } |