Created
January 2, 2014 23:46
-
-
Save hemache/8229590 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require './vendor/autoload.php'; | |
| // Prepare app | |
| $app = new \Slim\Slim(array( | |
| 'templates.path' => './templates', | |
| )); | |
| // Setup session | |
| session_cache_limiter(false); | |
| session_start(); | |
| if(! isset($_SESSION['items'])) $_SESSION['items'] = array(); | |
| // Setup database connection | |
| try { | |
| $dbh = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', '0000'); | |
| //$dbh = new PDO('mysql:dbname=bucket;host=127.0.0.1', 'root', 'azerty911'); | |
| $dbh->exec('SET character_set_results = utf8, character_set_client = utf8, character_set_connection = utf8, character_set_database = utf8, character_set_server = utf8'); | |
| $dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); | |
| $app->dbh = $dbh; | |
| } catch (PDOException $error) { | |
| exit($error->getMessage()); | |
| } | |
| // Create monolog logger and store logger in container as singleton | |
| // (Singleton resources retrieve the same log resource definition each time) | |
| /* | |
| $app->container->singleton('log', function () { | |
| $log = new \Monolog\Logger('bucket'); | |
| $log->pushHandler(new \Monolog\Handler\StreamHandler('./logs/app.log', \Monolog\Logger::DEBUG)); | |
| return $log; | |
| }); | |
| */ | |
| // Prepare view | |
| $app->view(new \Slim\Views\Twig()); | |
| $app->view->parserOptions = array( | |
| 'charset' => 'utf-8', | |
| 'cache' => realpath('./templates/cache'), | |
| 'auto_reload' => true, | |
| 'strict_variables' => false, | |
| 'autoescape' => true | |
| ); | |
| $app->view->parserExtensions = array(new \Slim\Views\TwigExtension()); | |
| $app->view->getInstance()->addGlobal('session', $_SESSION); | |
| $app->view->getInstance()->addFunction(new Twig_SimpleFunction('pad', function($input, $pad_length, $pad_string=' ', $pad_style=STR_PAD_RIGHT, $encoding="UTF-8") { | |
| return str_pad($input, strlen($input)-mb_strlen($input,$encoding)+$pad_length, $pad_string, $pad_style); | |
| })); | |
| // Helper functions | |
| function getItems($ids) { | |
| global $app; | |
| $ids = is_string($ids) ? explode(',', $ids) : array_values(array_filter($ids)); | |
| if(count($ids) == 0) return false; | |
| //var_dump($ids); | |
| $placeholders = rtrim(str_repeat('?, ', count($ids)), ', ') ; | |
| //var_dump("SELECT * FROM item WHERE id IN ($placeholders)"); | |
| $items = $app->dbh->prepare("SELECT * FROM item WHERE id IN ($placeholders)"); | |
| $items->execute($ids); | |
| return $items->fetchAll(); | |
| }; | |
| // Define routes | |
| $app->get('/', function () use ($app) { | |
| // Sample log message | |
| //$app->log->info("Slim-Skeleton '/' route"); | |
| $view['sections'] = $app->dbh->query('SELECT * FROM section ORDER BY title ASC'); | |
| // Render index view | |
| $app->render('index.html', $view); | |
| })->name('index'); | |
| $app->get('/s/:id', function ($id) use($app) { | |
| $section = $app->dbh->prepare('SELECT * FROM section WHERE id = ?'); | |
| $section->execute(array($id)); | |
| $view['section'] = $section->fetchObject(); | |
| $items = $app->dbh->prepare('SELECT * FROM item WHERE section_id = ? ORDER BY name ASC, id ASC'); | |
| $items->execute(array($id)); | |
| $view['items'] = $items->fetchAll(); | |
| $app->render('section.html', $view); | |
| })->name('section'); | |
| // Bucket management | |
| $app->get('/b/p', function () use($app) { | |
| //var_dump($_SESSION['items']); | |
| $view['items'] = getItems($_SESSION['items']); | |
| if(! $view['items']) { | |
| $app->halt(404, 'Bucket Not Found Or Empty'); | |
| } | |
| $app->render('bucket.pre.html', $view); | |
| })->name('bucket.pre'); | |
| $app->get('/b/g/(:id.zip)', function ($id=null) use($app) { | |
| if(! $id) $id = session_id(); | |
| $bucket = $app->dbh->prepare('SELECT * FROM bucket WHERE id = ? LIMIT 1'); | |
| $bucket->execute(array($id)); | |
| $bucket = $bucket->fetchObject(); | |
| if(! $bucket) { | |
| $app->dbh->prepare('INSERT INTO bucket VALUES (?, ?)')->execute(array($id, implode(',', $_SESSION['items']))); | |
| //$_SESSION['items'] = array(); | |
| session_regenerate_id(); | |
| $app->redirect($app->urlFor('bucket.get', array('id'=>$id))); | |
| } | |
| // UPSTREAM NGINX MOD_ZIP ... | |
| //var_dump($bucket); | |
| $view['items'] = getItems($bucket->items); | |
| if(! $view['items']) { | |
| $app->halt(404, 'Bucket Not Found Or Empty'); | |
| } | |
| //var_dump($items); | |
| $app->etag($bucket->id); | |
| $app->response->headers->set('Content-Type', 'text/plain'); | |
| $app->response->headers->set('X-Archive-Files', 'zip'); | |
| $app->render('bucket.get.txt', $view); | |
| })->name('bucket.get'); | |
| $app->get('/b/a/:item_id', function ($item_id) use($app) { | |
| if(! in_array(intval($item_id), $_SESSION['items'])) $_SESSION['items'][] = $item_id; | |
| $app->redirect($app->request->getReferrer()); | |
| })->name('bucket.add'); | |
| $app->get('/b/d/:item_id', function ($item_id) use($app) { | |
| if(in_array(intval($item_id), $_SESSION['items'])) | |
| unset($_SESSION['items'][ | |
| array_keys($_SESSION['items'], $item_id)[0] | |
| ]); | |
| $app->redirect($app->request->getReferrer()); | |
| })->name('bucket.del'); | |
| // Run app | |
| $app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment