false, 'view' => new \Slim\Views\Smarty(), 'settings.path' => './settings', 'app.path' => './app', 'public.path' => __DIR__, 'root.path' => __DIR__ )); //potrebne redirecty kvuli seo if ('/' != substr($app->request->getResourceUri(), -1, 1)) { if (strpos($app->request->getResourceUri(), '.') === false) { header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $app->request->getRootUri() . $app->request->getResourceUri() . '/'); exit; } } $app->config('migrations.path', $app->config('app.path') . '/migrations'); $app->config('hooks.path', $app->config('app.path') . '/hooks'); $app->config('uploads.path', $app->config('root.path') . '/uploads'); $app->config('uploads.dir', $app->request->getScriptName() . '/uploads'); $app->config('root.dir', $app->request->getScriptName()); $app->config('thumb.path', $app->config('root.path') . '/thumb'); $app->config('thumb.dir', $app->request->getScriptName() . '/thumb'); $app->config('log.writer', new \Slim\Logger\DateTimeFileWriter(array('path' => $app->config('app.path') . '/log'))); $app->config('cache.path', $app->config('app.path') . '/cache'); require $app->config('settings.path') . '/api_key.php'; require $app->config('settings.path') . '/common.php'; require $app->config('settings.path') . '/web.php'; require $app->config('settings.path') . '/web_overrides.php'; //zaheslovani webu if ($app->config('structure')->settings['http_auth_user'] AND $app->config('structure')->settings['http_auth_pass']) { /*$app->add(new \Slim\Middleware\HttpBasicAuthentication([ "secure" => false, "users" => [ $app->config('structure')->settings['http_auth_user'] => $app->config('structure')->settings['http_auth_pass'] ] ]));*/ } //session $app->add(new \Slim\Middleware\SessionCookie(array( 'expires' => '30 days', 'path' => '/', 'domain' => null, 'secure' => false, 'httponly' => false, 'name' => 'slim_session', 'secret' => 'd5fldXlQJBN wuhShtcL|^TyINZDsnw5n%2o|_MuJDlFfCGVoB+|Ay8nx2XX', 'cipher' => MCRYPT_RIJNDAEL_256, 'cipher_mode' => MCRYPT_MODE_CBC ))); //smarty $view = $app->view(); $view->parserCompileDirectory = $app->config('cache.path') . '/smarty_compiled'; $view->parserCacheDirectory = $app->config('cache.path') . '/smarty_cache'; $view->parserExtensions = array( $app->config('app.path') . '/vendor/slim/views/SmartyPlugins', $app->config('app.path') . '/lib/SmartyPlugins', ); //dependency injection /*$app->form = function() use ($app) { $form = new HTML_QuickForm2('cms-form-' . $app->formCounter, 'post', array( 'class' => 'uk-form uk-form-horizontal', 'action' => $app->request->getRootUri() . $app->request->getResourceUri() )); $form->addRecursiveFilter('trim'); return $form; };*/ $app->container->singleton('tree', function () use ($app) { $tree = new \DW\Tree($app); $tree->setDefaultLanguage($app->config('language')); return $tree; }); $app->container->singleton('dw', function () use ($app) { return new \DW\DW($app); }); $app->container->singleton('intl', function () use ($app) { return new \DW\Internationalization($app); }); $app->container->singleton('migrate', function () use ($app) { return \Voilab\Migrate\Migrate::getInstance(array( 'migrations_path' => $app->config('migrations.path') . '/', 'database' => $app->config('database'), )); }); $app->container->singleton('db', function () use ($app) { try { $config = $app->config('database'); $db = new \PDO( $config['adapter'] . ':host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['user'], $config['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") ); return new NotORM($db); } catch (\Exception $e) { throw new \Exception('Could not connect to database. Message: ' . $e->getMessage()); } }); $app->container->singleton('cart', function () use ($app) { //konvence pro pojmenovani session promenne nakupniho kosiku je basket, podivame se, zda tam neco nemame $basket = array('items' => array(), 'count' => 0, 'data' => array(), 'currency_code' => $app->config('currency_code'), 'currency_char', $app->config('currency_char')); if (isset($_SESSION['app']['basket'])) { foreach ($_SESSION['app']['basket'] as $product => $count) { if ($count > 0) { $product = $app->tree->getContent($product); $item = array( 'count' => $count, 'data' => $product, ); $basket['count'] += $count; $basket['items'][] = $item; } } } if (isset($_SESSION['app']['basketdata'])) { $basket['data'] = $_SESSION['app']['basketdata']; } return $basket; }); //ukladani dat do sessiony z odkazu $app->get('/session/:key/:value/', function ($key, $value) use ($app) { if (!isset($_SESSION['app'])) { $_SESSION['app'] = array(); } $_SESSION['app'][$key] = $value; //$url = $app->request()->getRootUri(); if ($app->request()->getReferer()) { $url = $app->request()->getReferer(); $app->redirect($url); } else { //vratime json odpoved... //var_dump($_SESSION['app']); } $app->stop(); })->name('session'); //kosik $app->map('/cart-add/:id/:count/:basket/', function ($id, $count, $basket) use ($app) { if (!isset($_SESSION['app']['basket'])) { $_SESSION['app']['basket'] = array(); } if (!isset($_SESSION['app']['basket'][$id])) { $_SESSION['app']['basket'][$id] = 0; } $_SESSION['app']['basket'][$id] += $count; if ($basket AND $app->request()->getReferer()) { $url = $app->request()->getReferer(); $app->redirect($url); } else { $cart = $app->cart; header("Content-Type: application/json"); echo json_encode($cart); } })->name('cart/add')->via('GET','POST'); $app->get('/cart-remove/:id/:count/', function ($id, $count) use ($app) { if (isset($_SESSION['app']['basket'][$id])) { if ($count == 0) { $_SESSION['app']['basket'][$id] = 0; } else { --$_SESSION['app']['basket'][$id]; if ($_SESSION['app']['basket'][$id] < 0) { $_SESSION['app']['basket'][$id] = 0; } } } if ($app->request()->getReferer()) { $app->flashKeep(); $url = $app->request()->getReferer(); $app->redirect($url); } else { $cart = $app->cart; header("Content-Type: application/json"); echo json_encode($cart); } })->name('cart/remove'); $app->get('/cart/', function () use ($app) { $cart = $app->cart; header("Content-Type: application/json"); echo json_encode($cart); })->name('cart'); //vsechno ostatni jde do jedne routy $app->map('/(.+)', function () use ($app) { //migrace db $app->dw->migrate(); //jakou mame mutaci $language = $app->intl->getLanguageByUrl(); $app->config('language', $language->id); $app->config('structure')->languages[$language->id]->active = true; $app->intl->setLocaleByLanguage($language); $languages = array(); foreach ($app->config('structure')->languages as $item) { $item = (array) $item; $languages[$item['id']] = $item; } $dictionary = $app->config('site.path') . $app->intl->getCurrentLocale() . '.json'; if (file_exists($dictionary)) { $app->intl->setDictionary(json_decode(file_get_contents($dictionary))); } //systemove stranky if (strpos($app->dw->getFullUrl(), 'sitemap.xml') !== false) { $tree = $app->tree->getPublicSitemap(); $app->config('page', array( 'language' => (array) $language, 'tree' => $tree )); $app->response->headers->set('Content-Type', 'text/xml'); $app->render('sitemap.tpl', array( 'items' => $tree )); $app->stop(); } //uz dal nepokracujeme... //zjistime url soucasne stranky a zjistime, zda existuje. potrebujeme znat rodice, //potomky a dynamicky generovane parametry $path = str_replace($language->url, '', $app->dw->getFullUrl()); if ($path == $app->dw->getFullUrl()) { $path = ''; //homepage pod jazykovou mutaci, web.cz chceme udelat jako web.cz/cz/ (pokud je samozrejme tak nastaveno, ze web.cz/cz/ existuje) } $path = array_filter(explode('/', substr($path, 0, -1))); $slug = end($path); if (!$slug) { $slug = null; } $app->config('currentPath', $path); $app->config('currentSlug', $slug); //zacneme skladat strom webu, tak abychom mohli vykreslit konkretni stranku a znali jeji potomky i rodice $tree = $app->tree->getPublicTree(); $blocks = array(); foreach ($tree as $key => $item) { if ($item['type'] == $app->tree->getBlocksDataType()->id) { $blocks = $item['system']; //jedna ze stranek je systemova, v ni jsou ulozeny spolecne bloky kodu unset($tree[$key]); break; } } //zjistime ID stranky $id = $app->tree->getIdFromSlug($slug); //tady muze byt false, pokud stranka neni vytvorena //mame validni cestu ke koreni webu? $pathToTrunk = $app->tree->getPathToPublicTrunk($id); //tady muze byt false, pokud je nektry rodic stranky skryty if (!$pathToTrunk) { $app->notFound(); } else { //nemame spatne url rodicu? $correct = implode('/', array_filter($pathToTrunk['path'])); $current = implode('/', $path); if ($correct != $current) { header('HTTP/1.1 301 Moved Permanently'); header('Location: ' . $language->url . $correct . '/'); exit; } //tady uz by mela byt stranka spravne nactena $content = $app->tree->getContentFromPublicTree($id, $tree); if (!$content) { //obsah neni ve stromu stranek, nacteme cely (tabulkovy vypis) $content = $app->tree->getContent($id); } //prilohy $attachments = $app->tree->getAttachments($id); //menu druhe urovne $top = current(array_keys(array_filter($pathToTrunk['path']))); if ($top) { $submenu = $app->tree->getContentFromPublicTree($top, $tree); } //tabulkovy vypis $items = array(); $datatype = $app->tree->getDatatype($content['type']); foreach ($datatype['descentants'] as $type) { if ($type['display'] == 'table') { $condition = array(); if ($app->config('table_condition')) { $condition = $app->config('table_condition'); } $sort = false; if ($app->config('table_sort')) { if (1 == $app->config('table_sort')) { $sort = 'id DESC'; } elseif (2 == $app->config('table_sort')) { $sort = 'sort1'; } elseif (3 == $app->config('table_sort')) { $sort = 'sort2'; } elseif (4 == $app->config('table_sort')) { $sort = 'sort2 DESC'; } } $search = false; if ($app->config('table_search')) { $search = $app->config('table_search'); } $items[$type['slug']] = $app->tree->getPublicTableByDataType($type['id'], $id, $content['path'], $type['items_on_page'], $condition, $sort, $search); } } //formulare $forms = $app->tree->getForms(); foreach ($forms as $key => $form) { if (isset($_POST['form'][$key])) { $result = $app->tree->validateForm($form, $_POST['form'][$key]); $forms[$key]['errors'] = $result; if ($result === true AND !isset($_POST['form'][$key]['only_validate'])) { $result = $app->tree->processForm($content, $form, $_POST['form'][$key]); if ($result) { $app->redirect($language->url . '/' . $current); $app->stop(); } } if (is_array($result)) { // $app->flash('error', 'Formulář nebyl správně vyplněn, zkontrolujte chyby uvedené níže.'); } $forms[$key]['values'] = $_POST['form'][$key]; } } //vsechny udaje vykreslitelne na strance chci mit v jedne promenne $page = array( //id webu 'name' => $app->getName(), //url webu 'website' => $app->config('structure')->websites[$app->getName()], //nastaveni z bile administrace 'settings' => $app->config('structure')->settings, //seznamy 'lists' => $app->config('structure')->lists, //seznam mutaci 'languages' => $languages, //aktualni mutace 'language' => (array) $language, //spolecne bloky stranek z cms 'blocks' => $blocks, //kompletni strom stranek (stromove zaznamy) 'tree' => $tree, //drobeckovka 'breadcrumb' => isset($pathToTrunk['breadcrumb']) ? $pathToTrunk['breadcrumb'] : array(), //cesta ke koreni webu 'path' => isset($pathToTrunk['path']) ? array_keys($pathToTrunk['path']) : array(), //polozky obsahu 'items' => $items, //menu druhe urovne 'submenu' => isset($submenu) ? $submenu : array(), //aktualni content - id 'id' => $id, //aktualni content 'content' => $content, //prilohy 'attachments' => $attachments, //aktualni data type 'datatype' => $datatype, //formular 'forms' => $forms, //kosik 'cart' => $app->cart, ); $app->config('page', $page); //specialni kus kodu pro konkretni datovy typ if ($datatype['hook_before_dispatch']) { $app->dw->hook($datatype['hook_before_dispatch'], $page); //todo zmenit na before_render } $app->render('page.tpl', array( 'page' => $app->config('page') )); } })->name('web')->via('GET', 'POST'); $app->notFound(function () use ($app) { $app->render('404.tpl'); }); $app->run();