-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.php
More file actions
80 lines (64 loc) · 1.45 KB
/
common.php
File metadata and controls
80 lines (64 loc) · 1.45 KB
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
<?php
require_once 'config.php';
date_default_timezone_set('UTC');
// Along the lines of '/path/to/file.png'
$uri = $_SERVER['REQUEST_URI'];
if ( strpos($uri,'?') !== false )
{
list ($uri, ) = explode("?",$uri);
}
define('URI', $uri);
// We support GET and POST methods
define('METHOD', $_SERVER['REQUEST_METHOD']);
// Current time in ISO-8601 format
define('NOW', date('Y-m-d\TH:i:s\Z'));
// Compute the HMAC of a payload
function hmac($request)
{
ksort($request);
$payload = array();
foreach ($request as $key => $value)
$payload[] = urlencode($key)."=".urlencode($value);
$payload = implode("&",$payload);
return hash_hmac('sha1',$payload,API_KEY);
}
// Respond with some JSON
function respond($json)
{
header("Content-Type: application/json");
echo json_encode($json);
exit;
}
// Displays an error JSON
function error($text = "500 Internal server error")
{
header("HTTP/1.1 $text");
respond(array("status" => "error"));
}
// Redirects to an URL
function redirect($url)
{
header("HTTP/1.1 303 See Other");
header("Location: $url");
exit;
}
// Returns the contents of a file
function return_file($path,$mime,$filename)
{
$file = fopen(file_path($path),"r");
if ( $file )
{
header("Content-Type: $mime");
if ( $filename !== null )
{
$filename = addcslashes($filename,"\"\\\n");
header("Content-Disposition: attachment; filename=\"$filename\"");
}
fpassthru($file);
fclose($file);
}
else
{
error("404 Not Found");
}
}