-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.php
More file actions
94 lines (78 loc) · 2.25 KB
/
Copy pathapi.php
File metadata and controls
94 lines (78 loc) · 2.25 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
81
82
83
84
85
86
87
88
89
90
91
<?php
require_once('autoload.php');
require_once('includes/config.php');
use includes\system;
/**
* Class API
*/
class API
{
/**
* @var system|null
*/
private $oSys = null;
/**
* API constructor.
*/
public function __construct()
{
$this->oSys = system::getInstance();
$this->execute();
}
/**
*
*/
private function execute()
{
$sLong = $_POST['url'];
if (strpos($sLong, 'http') === false) {
$sLong = 'http://'.$sLong;
}
if(preg_match( '/^(http|https):\\/\\/[a-z0-9_]+([\\-\\.]{1}[a-z_0-9]+)*\\.[_a-z]{2,5}'.'((:[0-9]{1,5})?\\/.*)?$/i' ,$sLong) == false){
$this->returnData(403, L::wrong_format);
}
$bDone = false;
while (!$bDone) {
$sShort = $this->generateRandomString(URL_LENGTH);
$oResult = $this->oSys->oDB->query('SELECT COUNT(*) as cnt FROM urls WHERE short = \''.$sShort.'\'');
$aResult = $oResult->fetch();
if ($aResult['cnt'] == 0) {
$bDone = true;
$oStmt = $this->oSys->oDB->prepare('INSERT INTO urls (`short`, `long`, `userid`) VALUES (?,?,0)');
if($oStmt->execute(array($sShort, $sLong))) {
$this->returnData(200, 'ok',$sLong, L::siteurl.'/'.$sShort);
}
}
}
}
/**
* @param $iStatus
* @param $sMessage
* @param string $sLong
* @param string $sShort
*/
private function returnData($iStatus, $sMessage, $sLong = '', $sShort = '') {
$aData = array();
$aData['version'] = '0.1';
$aData['status'] = $iStatus;
$aData['message'] = $sMessage;
$aData['long'] = $sLong;
$aData['short'] = $sShort;
echo json_encode($aData);
die();
}
/**
* @param int $length
* @return string
*/
private function generateRandomString($length = 10)
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
}
new API();