-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
154 lines (131 loc) · 3.91 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
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
<?php
/**
* @title cappd.me
* @desc link shortener with caps
*
* @author Yu Chen Hou<me@yuchenhou.com>
* @copyright (c) 2012, Pierre-Henry Soria. All Rights Reserved.
* @license CC BY-SA 3.0
* @version 1.0
*/
//DEBUG
//TODO ue a debug variable
ini_set('display_errors', 1);
error_reporting(~0);
require "Slim/Slim.php";
require "NotORM.php";
require "linkmanager.class.php";
define('DB_DRIVER', 'mysql');
define('DB_HOST', 'INFO HERE');
define('DB_NAME', 'INFO HERE');
define('DB_USER', 'INFO HERE');
define('DB_PASS', 'INFO HERE');
//define base
define('ALLOWED_CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
//TODO use variable here
$pdo = new PDO(DB_DRIVER.":host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
$db = new NotORM($pdo);
$linkManager = new linkManager($db);
\Slim\Slim::registerAutoloader();
/**
* Step 2: Instantiate a Slim application
*
* This example instantiates a Slim application using
* its default settings. However, you will usually configure
* your Slim application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Slim\Slim(array(
'log.enabled' => true,
'templates.path' => './templates'
));
/**
* Step 3: Define the Slim application routes
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
* is an anonymous function.
*/
// homepage
$app->get('/', function () use ($app){
$app->render('main.php');
});
// faq route
$app->get('/faq', function () use($app) {
$app->render('faq.php');
});
// redirection
$app->get('/:uid+', function ($uid) use($app, $linkManager) {
//if ($uid[1] == "delete")) {
// echo "To be implemented.";
//}else{
$id =$linkManager->decodeShortenedURL($uid[0]);
$result = $linkManager->fetch($id);
if($result == null){
$app->render('404.php');
}else{
//Send the user on his way
$app->redirect($result);
}
//}
//TODO Set cookie
});
// API POST route
$app->post('/api/create', function () use($app, $linkManager) {
$app->response()->header("Content-Type", "application/json");
// Get request object
$req = $app->request();
//TODO Validation, Errors
$url = $req->post('url');
$expiration_time = $req->post('expire_time');
$daily_cap = $req->post('daily_cap');
$total_cap = $req->post('total_cap');
if($url == null){
echo json_encode(array("error" => "no url entered"));
return;
}
if($total_cap < 0){
echo json_encode(array("error" => "total cap is negative"));
return;
}
if(strtotime($expiration_time.' + 1 day') < time()){
echo json_encode(array("error" => "expiration date already passed!"));
return;
}
//Validate the input and put them in an array
if(strstr($url,"://") == false){
$url = "http://" . $url;
}
$data = array(
"url" =>filter_var($url, FILTER_SANITIZE_URL),
"expiration_time" => filter_var($expiration_time, FILTER_SANITIZE_FULL_SPECIAL_CHARS),
"daily_cap" => filter_var($daily_cap, FILTER_VALIDATE_INT),
"total_cap" => filter_var($total_cap, FILTER_VALIDATE_INT),
);
$result = $linkManager->save($data);
$permalink =$linkManager->encodeFromID($result);
$array = array("permalink" => $permalink,"generated_time" => time());
echo json_encode($array);
});
// API POST route
$app->post('/api/qr', function () use($app, $linkManager) {
});
// PUT route
$app->put('/put', function () {
echo 'This is a PUT route';
});
// DELETE route
$app->delete('/delete', function () {
echo 'This is a DELETE route';
});
$app->notFound(function () use ($app) {
$app->render('404.php');
});
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();