<?php Moriarti::register(0, '/http/*', function ($code, $data) { $view = substr($code, strrpos($code, "/") + 1); // implode('/',$tmp); $exclude = Moriarti::get('plugins/firewall')['data']['exclude']; if (!isset($_SESSION['USER']) && !in_array($view, $exclude)) { header("Location: login"); die; } });
<?php Moriarti::register(1, '/bd/query/*', function ($code, $data) { $queryName = explode("/", $code)[3]; $filename = dirname(__FILE__) . '/queries/' . $queryName . '.inc'; if (file_exists($filename)) { ob_start(); include $filename; $query = ob_get_clean(); } else { throw new \Exception("Can't find query " . $queryName); } $result = Moriarti::get('bd')->prepare($query); $result->execute($data); Moriarti::store($queryName, $result->fetchAll()); });
<?php Moriarti::register(1, '/http/*', function ($tipo, $data) { ini_set('display_errors', 1); if (isset($_SESSION['USER'])) { Moriarti::store('layout', 'private'); } else { Moriarti::store('layout', 'public'); } });
<?php Moriarti::register(3, '/view/adminlte/private/*', function ($code, $data) { $doc = Moriarti::get('view'); DOMUtil::findElementsByClassName('messages-text')->item(0)->nodeValue = 'Tienes 100 mensajes'; DOMUtil::findElementsByClassName('messages-count')->item(0)->nodeValue = 100; });
<?php Moriarti::register(0, '/on/exception/*', function ($tipo, $data) { new Message('/view/adminlte/public/500', $data); });
<?php // rewrite relative urls to match template location Moriarti::register(1, '/on/render/*', function ($code, $data) { $xpath = new DOMXPath($data); $template = Moriarti::get('plugins/template')['data']['active']; $notSrcAbs = "[not(starts-with(@src, 'http'))]"; $notHrefAbs = "[not(starts-with(@href, 'http'))]"; // rewrite <link href> <script src> and <img src> with relative urls $linkhrefs = $xpath->query("//link{$notHrefAbs}/@href | //script{$notSrcAbs}/@src | //img{$notSrcAbs}/@src"); foreach ($linkhrefs as $linkhref) { // add prefix with right location $linkhref->nodeValue = "/plugins/template/{$template}/layout/" . $linkhref->nodeValue; } });
<?php Moriarti::register(0, '/bd/*', function ($tipo, $data) { $settings = Moriarti::get('plugins/bd'); $bd = new PDO('mysql:host=' . $settings['data']['host'] . ';dbname=' . $settings['data']['database'], $settings['data']['user'], $settings['data']['password']); $bd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $bd->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); Moriarti::store('bd', $bd); });
<?php Moriarti::register(9, '/view/adminlte/public/500', function ($code, $data) { $doc = Moriarti::get('view')->getElementById('titleError')->nodeValue = 'Error'; $doc = Moriarti::get('view')->getElementById('msgError')->nodeValue = $data->getMessage(); $doc = Moriarti::get('view')->getElementById('lineError')->nodeValue = $data->getFile() . ' - ' . $data->getLine(); });
<?php Moriarti::register(1, '/view/adminlte/private/*', function ($code, $data) { Moriarti::store('userMenu', ['Control Panel', 'Profile', 'Settings']); });
<?php Moriarti::register(2, '/view/adminlte/*/*', function ($code, $data) { $viewName = explode('/', $code)[4]; $filename = dirname(__FILE__) . "/view/{$viewName}.html"; if (file_exists($filename)) { $doc = Moriarti::get('view'); $content = file_get_contents($filename); DOMUtil::importDOM($doc->getElementById('contentFragment'), DOMUtil::parseHTML($content)); } });
<?php Moriarti::register(1, '/http/*/login', function ($tipo, $data) { Moriarti::store('layout', 'login'); }); Moriarti::register(1, '/http/POST/login', function ($tipo, $data) { $user = $_POST["username"]; $pass = $_POST["password"]; $encrypted_password = sha1($pass); new Message("/bd/query/validateLogin", [':name' => $user, ':password' => $pass]); if (!empty(Moriarti::get('validateLogin'))) { $_SESSION['USER'] = Moriarti::get('validateLogin')[0]; header("Location: ."); die; } else { new Message("/on/warning", "User or password incorrect"); } }); Moriarti::register(1, '/http/GET/logout', function ($code, $data) { session_destroy(); header("Location: ."); die; });
<?php Moriarti::register(9, '/view/adminlte/private/*', function ($code, $data) { $view = explode('/', $code)[4]; $doc = Moriarti::get('view'); $list = DOMUtil::findElementsByClassName('sidebar-menu')->item(0); $item = $list->firstChild; foreach (Moriarti::get('userMenu') as $menu) { $menuref = str_replace(' ', '', strtolower($menu)); $newItem = $item->cloneNode(true); $list->appendChild($newItem); DOMUtil::xpathQuery('.//span', $newItem)->item(0)->nodeValue = $menu; DOMUtil::xpathQuery('.//a/@href', $newItem)->item(0)->nodeValue = $menuref; if ($view === $menuref) { $newItem->setAttribute('class', 'active'); } } $item->parentNode->removeChild($item); });
<?php Moriarti::register(0, '/on/warning', function ($tipo, $data) { Moriarti::storeArr('warning', $data); }); Moriarti::register(0, '/on/info', function ($tipo, $data) { Moriarti::storeArr('info', $data); }); Moriarti::register(0, '/on/error', function ($tipo, $data) { Moriarti::storeArr('error', $data); });
<?php Moriarti::register(1, '/http/*', function ($tipo, $data) { if (isset($_GET['lang'])) { $_SESSION['lang'] = $_GET['lang']; } }); Moriarti::register(1, '/on/render/*', function ($code, $data) { if (isset($_SESSION['lang'])) { $lang = $_SESSION['lang']; } else { $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); } $view = explode('/', $code)[3]; $xpath = new DOMXPath($data); // translate all texts $translations = Moriarti::get('plugins/template-translation')['data']['translations']; $texts = $xpath->evaluate('//text()[not(parent::script)][string-length() > 0]'); foreach ($texts as $node) { $key = trim($node->nodeValue); if ($key == '') { continue; } if (isset($translations[$lang][$key])) { $node->nodeValue = $translations[$lang][$key]; } if (isset($translations[$lang . '/' . $view][$key])) { $node->nodeValue = $translations[$lang . '/' . $view][$key]; } } });
<?php Moriarti::register(9, '/view/adminlte/*', function ($code, $data) { // show warnings $doc = Moriarti::get('view'); $notifications = $doc->getElementByID('notifications'); if ($notifications != null) { $content = file_get_contents(__DIR__ . '/view/notifications/warning.html'); $warningDoc = DOMUtil::parseHTML($content); foreach (Moriarti::getArr('warning') as $warningTxt) { DOMUtil::findElementsByClassName('warning-message', $warningDoc)->item(0)->nodeValue = $warningTxt; DOMUtil::importDOM($notifications, $warningDoc); } $content = file_get_contents(__DIR__ . '/view/notifications/error.html'); $errorDoc = DOMUtil::parseHTML($content); foreach (Moriarti::getArr('error') as $errorTxt) { DOMUtil::findElementsByClassName('error-message', $errorDoc)->item(0)->nodeValue = $errorTxt; DOMUtil::importDOM($notifications, $errorDoc); } $content = file_get_contents(__DIR__ . '/view/notifications/info.html'); $infoDoc = DOMUtil::parseHTML($content); foreach (Moriarti::getArr('info') as $infoTxt) { DOMUtil::findElementsByClassName('info-message', $infoDoc)->item(0)->nodeValue = $infoTxt; DOMUtil::importDOM($notifications, $infoDoc); } } });
<?php Moriarti::register(3, '/view/adminlte/private/*', function ($code, $data) { foreach (DOMUtil::xpathQuery("//text()[. = 'USER_NAME']") as $userName) { $userName->nodeValue = $_SESSION['USER']['NAME']; } });