Browse Source

init commit

Domenic Horner 3 years ago
commit
bc95f4f18d
9 changed files with 424 additions and 0 deletions
  1. 23 0
      get.php
  2. 8 0
      include/config.php
  3. 92 0
      include/func.php
  4. 25 0
      index.php
  5. 132 0
      index.php1
  6. BIN
      load.gif
  7. 61 0
      main.js
  8. 47 0
      style.css
  9. 36 0
      visit.php

+ 23 - 0
get.php

@@ -0,0 +1,23 @@
+<?php
+require_once("include/config.php");
+require_once("include/func.php");
+
+header('Access-Control-Allow-Origin: *');
+
+	if(isset($_GET['url'])) {
+		$long_url = $_GET['url']; //get URL to shorten
+		// grab the codes array
+		$codes = getCodes();
+		$names = getNames();
+		// grab the unique code
+		$short_code = getUniqueHash($codes, $names, $long_url); //get either a new code or the allready existing one for the long url based on what is in the arrays
+		if(storeNewCode($short_code, $long_url)) { //worked
+			$short_url = 'http://s.tgxn.net/'.$short_code.'/';
+			echo ''.$short_url.'';
+		} else { //failed to store
+			$short_url = 'http://s.tgxn.net/fail/';
+			echo ''.$short_url.'';
+		}
+		die();
+	}
+?>

+ 8 - 0
include/config.php

@@ -0,0 +1,8 @@
+<?php
+error_reporting(1);
+ini_set('display_errors', 1);
+
+//$mysql_server = 'localhost';
+//$mysql_db = 'short';
+//$mysql_user = 'short';
+//$mysql_password = 'juedwGFzjSBCTLKh';

+ 92 - 0
include/func.php

@@ -0,0 +1,92 @@
+<?php
+
+$dsn = 'mysql:host=' . getenv("MYSQL_HOST") . ';port=' . getenv("MYSQL_PORT") . ';dbname=' . getenv("MYSQL_DATABASE");
+
+$pdo = new PDO($dsn, getenv("MYSQL_USERNAME"), getenv("MYSQL_PASSWORD"), array(
+    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
+));
+$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
+
+// Return all current short codes in an array (xxxx).
+function getCodes() {
+	global $pdo;
+
+	$stmt = $pdo->query("SELECT `short` FROM short");
+
+	$codes = array(); //init array
+
+	while ($row = $stmt->fetch()) {
+    		$codes[] =  $row['short'];
+	}
+
+	return $codes;
+}
+
+// Get all long URL's and pass to array.
+function getNames() {
+	global $pdo;
+
+	$stmt = $pdo->query("SELECT `long` FROM short");
+
+	$names = array();
+	while ($row = $stmt->fetch()) {
+    		$names[] =  $row['long'];
+	}
+
+	return $names;
+}
+
+// Get the short URL for an allready-existing long url.
+function getCodeForName($long_url) {
+	global $pdo;
+
+	$stmt = $pdo->query("SELECT `short` FROM short WHERE `long` = '$long_url' LIMIT 1");
+	$result = $stmt->fetch();
+
+	if($result) { //do the query
+		return $result['short']; //return the short part
+	} else {
+		return false;
+	}
+	
+}
+
+
+// this generates the unique code.
+function getUniqueHash($codes, $names, $long_url) {
+	$code = substr(md5(uniqid(rand(), true)), 0, 4); //random 4-digit alfanumerical code
+	if(in_array($code, $codes)) {
+		return getUniqueHash($codes, $names, $long_url); //loop this is the newly generated randon code is in the current codes
+	} else if(in_array($long_url, $names)) {
+		return getCodeForName($long_url); //if the URL allready has a short code, return that instead
+	} else {
+		return $code;
+	}
+}
+
+
+function storeNewCode($short, $long) { //run this if we want to insert the code
+global $pdo;
+	$updatedb = false; //need to update
+	if(!getCodeForName($long)) { //if it isnt allready in the DB.
+		$updatedb = false;
+		$vis_ip = $_SERVER["REMOTE_ADDR"];
+
+		$stmt = $pdo->prepare("INSERT INTO short(`short`, `long`, `ip`) VALUES (:short, :long, :visip)");
+$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$stmt = $stmt->execute(array(
+  ':short'=>$short,
+  ':long'=>$long,
+  ':visip'=>$vis_ip
+));
+
+if (!$stmt) {
+    echo "\PDO::errorInfo():\n";
+    print_r($pdo->errorInfo());
+}
+
+		return $stmt ;
+	} else {
+		return true; //donot need to store
+	}
+}

+ 25 - 0
index.php

@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="utf-8" />
+	<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" />
+	<title>URL Shortener</title>
+	<link rel="stylesheet" type="text/css" href="style.css" />
+	<script src="main.js" type="text/javascript" charset="utf-8"></script>
+	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
+</head>
+<body>
+	<div id="request">
+		<p id="textEntURL" class="item" >Paste your long URL here</p>
+		<p>
+		<input id="longURL" value="" onclick="this.select()" placeholder="http://www.example.com/example" onkeyup="updateBTN();" />
+		</p>
+		<p id="shortenBTNCont">
+			<span id="shortenBTN" onClick="checkLong();" class="item" >Shorten!</span>
+			<span id="shortenIMG"><img src="load.gif" /></span>
+		</p>
+		<p id="shortURLCont" >
+		<input id="shortURL" onclick="this.select()" <?php if(isset($_GET['notfound'])) { echo 'value="Could not find entry. :\'("';} ?> readonly />
+		</p>
+	</div>
+</body>

+ 132 - 0
index.php1

@@ -0,0 +1,132 @@
+<html>
+<head>
+  <title> Cloudron LAMP app </title>
+
+  <style>
+
+    body {
+      width: 50%;
+      min-width: 640px;
+      margin: auto;
+      font-family: Helvetica;
+      color: #333;
+    }
+
+    pre {
+      font-family: monospace;
+      background: #333;
+      color: white;
+      border: none;
+      width: 99%;
+      padding: 10px;
+      text-align: left;
+      font-size: 13px;
+      border-radius: 5px;
+      margin-bottom: 15px;
+      box-shadow: 0px 1px 12px rgba(0, 0, 0, 0.176);
+    }
+
+    h1 {
+      text-align: center;
+    }
+
+    .center > table {
+      width: 100%;
+    }
+
+    .h, .e {
+      background-color: white !important;
+    }
+
+  </style>
+
+</head>
+<body>
+
+<h1>Cloudron LAMP App</h1>
+
+<br/>
+
+<h2>MySQL credentials</h2>
+<p>Use the following environment variables in the PHP code to access MySQL:</p>
+<pre>
+getenv("MYSQL_HOST")
+getenv("MYSQL_PORT")
+getenv("MYSQL_USERNAME")
+getenv("MYSQL_PASSWORD")
+getenv("MYSQL_DATABASE")
+</pre>
+
+<br/>
+
+<h2>SFTP Transfer</h2>
+<p>
+  You can SFTP files to the <b>public</b> folder using  <a href="https://cyberduck.io/" target="_blank">Cyberduck</a>,
+  <a href="https://filezilla-project.org/" target="_blank">FileZilla</a> or <a href="https://www.gftp.org/" target="_blank">gFTP</a>
+  (use your cloudron credentials to authenticate).
+</p>
+<p>
+  We recommend disabling SFTP access (from Cloudron's configuration panel) once you have uploaded your code.
+</p>
+<pre>
+sftp -P 2222 s.tgxn.net:public/
+</pre>
+
+<br/>
+
+<h2>phpMyAdmin access</h2>
+<p>
+  You can access phpMyAdmin using your Cloudron credentials <a href="/phpmyadmin" target="_blank">here</a>.
+</p>
+<p>
+  Note that disabling SFTP access also disables phpMyAdmin.
+</p>
+
+<br/>
+
+<h2>Cron</h2>
+<p>
+  Just put a file called crontab to the directory /app/data and it will picked up at next app restart. It has to be in the cron syntax without username
+  and must end with a newline. For example, the following crontab updates feeds every hour:
+</p>
+<pre>
+0 * * * * php /app/code/update.php --feeds
+
+</pre>
+<p>
+  Commands are executed as the user www-data. See <a href="https://linux.die.net/man/5/crontab">manpage</a> for a description of the file.
+</p>
+
+<br/>
+
+<h2>Logs</h2>
+<p>Apache logs can be viewed using the <a href="https://cloudron.io/references/cli.html" target="_blank">cloudron commandline tool</a>.</p>
+<pre>
+cloudron logs -f
+</pre>
+
+<br/>
+
+<h2>Addons</h2>
+<p>The app is configured to have access to the following Cloudron addons:</p>
+<ul>
+  <li><a href="https://cloudron.io/references/addons.html#mysql" target="_blank">mysql</a></li>
+  <li><a href="https://cloudron.io/references/addons.html#localstorage" target="_blank">localstorage</a></li>
+  <li><a href="https://cloudron.io/references/addons.html#sendmail" target="_blank">sendmail</a></li>
+  <li><a href="https://cloudron.io/references/addons.html#redis" target="_blank">redis</a></li>
+  <li><a href="https://cloudron.io/references/addons.html#ldap" target="_blank">ldap</a></li>
+  <li><a href="https://cloudron.io/references/addons.html#oauth" target="_blank">oauth</a></li>
+</ul>
+<p>Read more about Cloudron addons and how to use them <a href="https://cloudron.io/references/addons.html" target="_blank">here</a>.</p>
+
+<br/>
+
+<h2>PHP Setup</h2>
+<?php
+
+echo phpInfo();
+
+?>
+
+</body>
+</html>

BIN
load.gif


+ 61 - 0
main.js

@@ -0,0 +1,61 @@
+function getShort(longURL) {
+	if(longURL != "") {
+		$("#shortenIMG").show();
+		$("#shortenBTN").hide();
+		$.ajax({
+			type: "GET",
+			url: "get.php",
+			data: "url="+encodeURIComponent(longURL),
+			success: function(msg) {
+				$("#shortURL").val(msg);
+				$("#shortenIMG").hide();
+				$("#shortenBTN").show();
+				$("#shortURL").focus();
+				$("#shortURL").select();
+			}
+		});
+	} else {
+		//Please check URL message
+	}
+}
+
+function updateBTN() { //make the button pressable when the user types something
+	var longURL = $("#longURL").val();
+	
+	if(longURL == "") {
+		$("#shortenBTNCont").css("background","#CDCDCD");
+		$("#shortenBTNCont").css("cursor","default");
+	} else {
+		$("#shortenBTNCont").css("background","#C0D9D9");
+		$("#shortenBTNCont").css("cursor","pointer");
+	}
+}
+
+function checkLong() { //check long url for valadidity
+	var longURL = $("#longURL").val();
+	
+	if(longURL == "") {
+		$("#shortURL").val("Please enter a URL.");
+		return;
+	}
+	
+	if(longURL == "http://") {
+		$("#shortURL").val("Please enter a website.");
+		return;
+	}
+	
+	if(longURL == "https://") {
+		$("#shortURL").val("Please enter a website.");
+		return;
+	}
+	
+	if(longURL.search(/s.tgxn.net/i) != "-1") {
+		$("#shortURL").val("You cannot shorten short URL's.");
+		return;
+	}
+	
+	if(longURL.search(/http/i) == "-1")
+		longURL = "http://" + longURL;
+		
+	getShort(longURL);
+}

+ 47 - 0
style.css

@@ -0,0 +1,47 @@
+html, body {
+	background: #F6F6F6;
+	margin: 10px;
+	padding: 0;
+	text-align: center;
+	height: 100%;
+	font-family: sans-serif;
+	font-size: 24pt;
+	color: #302831;
+}
+
+input {
+	font-size: 24pt;
+	width: 500px;
+	text-align: center;
+	color: #302831;
+}
+
+p {
+	margin: 0.2em 0;
+}
+
+#request {
+	margin-top: 150px;
+}
+
+#textEntURL {
+	font-size: 24pt;
+	text-align: center;
+}
+
+#shortenBTNCont {
+	background: #CDCDCD;
+	width: 300px;
+	height: 36px;
+	margin: 0 auto;
+	cursor: default;
+	border-radius: 2px;
+	-moz-border-radius: 2px;
+	-webkit-border-radius: 2px;
+}
+
+#shortenIMG {
+	border: 5px #d60;
+	font-size: 24pt;
+	display: none;
+}

+ 36 - 0
visit.php

@@ -0,0 +1,36 @@
+<?php
+require_once("include/config.php");
+require_once("include/func.php");
+
+$dsn = 'mysql:host=' . getenv("MYSQL_HOST") . ';port=' . getenv("MYSQL_PORT") . ';dbname=' . getenv("MYSQL_DATABASE");
+
+$pdo = new PDO($dsn, getenv("MYSQL_USERNAME"), getenv("MYSQL_PASSWORD"), array(
+    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
+));
+
+if(isset($_GET['q'])) { //viewing individual
+
+		$short = $_GET['q'];
+
+	$stmt = $pdo->query("SELECT * FROM short WHERE `short` = '$short' LIMIT 1");
+	$result = $stmt->fetch();
+
+
+
+		if($result) {
+
+
+			$id = $result['id'];
+			$long = $result['long'];
+			$visits = $result['visits'] + 1;
+
+$stmt = $pdo->prepare("UPDATE short SET visits = ? WHERE id = ?");
+$stmt->execute( [ $visits, $id ] );  
+
+			header('Location: '.$long);
+		} else {
+			header('Location: http://s.tgxn.net/index.php?notfound');
+		}
+
+
+	}