Permalink
Browse files

Added comments

  • Loading branch information...
callumacrae committed Mar 23, 2011
1 parent 4210e0a commit e15a300e6bd3acf3ebf8e68350e9cba94c3642bc
Showing with 55 additions and 2 deletions.
  1. +55 −2 url-shortener.php
View
@@ -17,8 +17,16 @@ public function __construct(&$db = false, $table = false)
require_once('config.php');
$this->config = $config;
+ /**
+ * If no PDO object is sent, create one. It is preferred that if you
+ * already have a PDO object it should be sent. It will be used as
+ * a reference and will not be changed, but will speed up the
+ * construction of the shortener class as it won't have to create a
+ * new connection to the database.
+ */
if ($db)
{
+ //$db can only be PDO, here we check for that
if (!is_a($db, 'PDO'))
{
trigger_error('Must be PDO', E_USER_ERROR);
@@ -31,6 +39,11 @@ public function __construct(&$db = false, $table = false)
$this->db = new PDO($dsn, $this->config['user'], $this->config['pass']);
}
+ /**
+ * Generate a prepared statement, so that if the get method is
+ * called more than one, the prepared statement is only called
+ * once, making the script faster.
+ */
$this->prepared = $this->db->prepare('SELECT url FROM ' . $this->config['table'] . ' WHERE s_key = ?');
if ($table)
@@ -39,11 +52,20 @@ public function __construct(&$db = false, $table = false)
}
}
+ /**
+ * Gets one or more values from the database. If $key is a number, it
+ * will get that many values and order them by $type, but if $key is the
+ * key, it will return the corresponding URL
+ *
+ * @param mixed $key The key to search for / the amount of rows to
+ * return
+ * @param string $type If $key is an int, order by this
+ */
public function get($key, $type = false)
{
if (is_int($key) && $type)
{
- if (preg_match('/author:(?<author>.*)/', $type, $matches))
+ if (preg_match('/(/<searchfor>.*):(?<author>.*)/', $type, $matches))
{
/**
* @todo Add this!
@@ -58,17 +80,45 @@ public function get($key, $type = false)
}
else
{
+ //get single entry
$statement = $this->prepared;
$statement->execute(array($key));
$statement = $statement->fetchObject();
return $statement->url;
}
}
+ /**
+ * Submit a URL into the database. Will return the key.
+ *
+ * @param string $url The URL to insert
+ */
public function submit($url)
{
/**
- * @todo Validate URL
+ * Primative regex check
+ */
+ if (!preg_match('/^(?<proto>https?:\/{2})(?<domain>[a-zA-Z0-9\-.]+\.[a-zA-Z]{2,3})(?<path>\/\S*)?$/', $url, $matches))
+ {
+ $this->error = 'Invalid domain';
+ return false;
+ }
+
+ /**
+ * Checking for an MX record will slow down the script. If you are
+ * finding the script too slow, disable this option using the 'mx'
+ * option in the configuration
+ */
+ if ($config['mx'] && !checkdnsrr($matches['domain'], 'MX'))
+ {
+ $this->error = 'No MX record found for domain';
+ return false;
+ }
+
+ /**
+ * If allowed, check whether the URL is already in the database. If
+ * it is, then just return the key for the old one as there is no
+ * point in creating a new one.
*/
if (!$this->config['unique'])
{
@@ -89,11 +139,14 @@ public function submit($url)
$key = substr(md5(uniqid(rand(), true)), 0, $this->config['length']);
$statement->execute(array($key));
+ //check whether the key exists. If it does, make a new one.
while ($statement->rowCount() > 0)
{
$key = substr(md5(uniqid(rand(), true)), 0, $this->config['length']);
$statement->execute(array($key));
}
+
+ //insert into database
$statement = $this->db->prepare('INSERT INTO ' . $this->config['table'] . ' (s_key, url) VALUES (?, ?)');
$statement->execute(array($key, $url));
return $key;

0 comments on commit e15a300

Please sign in to comment.