-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.php
108 lines (90 loc) · 3.37 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
require 'includes/vendor/autoload.php';
require 'functions.php';
require 'config.php';
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
/* debugging data */
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
$c = new \Slim\Container($configuration);
/* actual app */
$app = new \Slim\App($c);
$app->add(function (Request $request, Response $response, callable $next) {
$uri = $request->getUri();
$path = $uri->getPath();
if ($path != '/' && substr($path, -1) == '/') {
// permanently redirect paths with a trailing slash
// to their non-trailing counterpart
$uri = $uri->withPath(substr($path, 0, -1));
if($request->getMethod() == 'GET') {
return $response->withRedirect((string)$uri, 301);
}
else {
return $next($request->withUri($uri), $response);
}
}
return $next($request, $response);
});
/* Twig Settings */
$container = $app->getContainer();
// Register Twig View helper and configure it
$container['view'] = function ($c) {
//You can change this as you want
$view = new \Slim\Views\Twig('templates', [
'cache' => false //or specify a cache directory
]);
// Instantiate and add Slim specific extension
$view->addExtension(new Slim\Views\TwigExtension(
$c['router'],
$c['request']->getUri()
));
return $view;
};
$app->get( '/', function($request, $response, $args) {
$gen_uid = gen_uid();
return $response->withRedirect( $gen_uid );
});
$app->get('/{gen_uid}', function($request, $response, $args) {
// get from database if exists
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT user_text FROM textpad WHERE gen_uid=?';
$q = $pdo->prepare($sql);
$q->execute(array($args['gen_uid']));
$note_details = $q->fetch();
Database::disconnect();
return $this->view->render($response, 'note-template.twig',
['gen_uid' => $args['gen_uid'],
'user_text' => $note_details['user_text']]);
});
$app->get('/{gen_uid}/raw.txt', function($request, $response, $args) {
// get from database if exists
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT user_text FROM textpad WHERE gen_uid=?';
$q = $pdo->prepare($sql);
$q->execute(array($args['gen_uid']));
$note_details = $q->fetch();
Database::disconnect();
$response = $response->withHeader('Content-type', 'text/plain');
return $response->write($note_details['user_text']);
});
$app->post('/{gen_uid}', function($request, $response, $args) {
// add to database
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO textpad (gen_uid, user_text, last_updated) VALUES (?, ?, CURDATE()) ON DUPLICATE KEY UPDATE user_text=?, last_updated=CURDATE()";
$q = $pdo->prepare($sql);
// get content from note-template.twig
$post = $request->getParsedBody();
$user_text = $post['user_text'];
$q->execute(array($args['gen_uid'], $user_text, $user_text));
Database::disconnect();
return $this->view->render($response, 'note-template.twig', ['gen_uid' => $args['gen_uid'], 'user_text' => $user_text]);
});
// Run app
$app->run();