Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Grüßung
committed
Mar 5, 2018
0 parents
commit 6238646
Showing
21 changed files
with
2,599 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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(); | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
function meaju_autoload($class) | ||
{ | ||
$parts = explode('\\', $class); | ||
$sFile = implode(DIRECTORY_SEPARATOR, $parts) . '.php'; | ||
if (file_exists($sFile)) { | ||
require_once $sFile; | ||
} else { | ||
return false; | ||
} | ||
} | ||
spl_autoload_register('meaju_autoload'); | ||
?> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!doctype html> | ||
<html lang="de"> | ||
<head> | ||
<!-- Required meta tags --> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
|
||
<!-- Bootstrap CSS --> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> | ||
|
||
<title>meaju</title> | ||
|
||
<style> | ||
body { | ||
background-image:url('images/background.png'); | ||
|
||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<nav class="navbar navbar-expand-lg navbar-dark bg-dark"> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo03" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<a class="navbar-brand" href="#">meaju</a> | ||
|
||
<div class="collapse navbar-collapse" id="navbarTogglerDemo03"> | ||
<ul class="navbar-nav mr-auto mt-2 mt-lg-0"> | ||
<li class="nav-item active"> | ||
<a class="nav-link" href="#">{} <span class="sr-only">(current)</span></a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="#">Regeln</a> | ||
</li> | ||
<li> | ||
<a class="nav-link" href="#">Impressum</a> | ||
</li> | ||
</ul> | ||
<form class="form-inline my-2 my-lg-0"> | ||
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search"> | ||
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> | ||
</form> | ||
</div> | ||
</nav> | ||
|
||
<div class="container-fluid"> | ||
Hello, World! | ||
</div> | ||
|
||
<!-- Optional JavaScript --> | ||
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | ||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.