diff --git a/index.php b/index.php new file mode 100644 index 0000000..36b89ab --- /dev/null +++ b/index.php @@ -0,0 +1,130 @@ +mysqli = @new mysqli($this->db_ip, $this->db_id, $this->db_pw, $this->db_name); + if ($this->mysqli->connect_errno) die('DB Connect ERROR!!!'); + } + /** + * Generate a random string, using a cryptographically secure + * pseudorandom number generator (random_int) + * + * For PHP 7, random_int is a PHP core function + * For PHP 5.x, depends on https://github.com/paragonie/random_compat + * + * @param int $length How many characters do we want? + * @param string $keyspace A string of all possible characters + * to select from + * @return string + */ + private function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') + { + $pieces = []; + $max = mb_strlen($keyspace, '8bit') - 1; + for ($i = 0; $i < $length; ++$i) { + $pieces []= $keyspace[random_int(0, $max)]; + } + return implode('', $pieces); + } + /** + * DB Select Function + * @param string $text Short URL data + * @param string $url URL data (optional) + * @return array Output DB data + */ + private function Select($text, $url = NULL) { + if (!is_null($text)) $text = htmlspecialchars($text); + if (!is_null($url)) $url = htmlspecialchars($url); + return $this->mysqli->query('select randstr, url from shortlink where url = \'' . $url . '\' or randstr = \'' . $text . '\';')->fetch_array(MYSQLI_ASSOC); + } + /** + * Create a short URL address. + * @param string $url URL data + * @param string $text Short URL data (optional) + * @return array/null When a valid value comes in, + * it returns the source address and the short URL. + * If it is not a valid value, it returns NULL. + */ + public function Make($url, $text = NULL) { + if (!is_NULL($text) && + (strlen($text) > 16 || strlen($text < 3))) return NULL; + $select = $this->Select($text, $url); + $url = htmlspecialchars($url); + if ($text === NULL) + $text = $this->random_str(rand(3, 16)); + if (!is_null($select['url']) && + $url === $select['url']) { + return array('randstr' => $select['randstr'], 'url' => $select['url']); + } + if (!is_null($select['randstr']) && + $select['randstr'] === $text && + $select['url'] !== $url) + return NULL; + if (!is_null($select['randstr'])) + return $this->Make($url); + $text = htmlspecialchars($text); + return ($this->mysqli->query('INSERT INTO shortlink(randstr, url) VALUES (\'' . $text . '\', \'' . $url . '\');')) ? array('randstr' => $text, 'url' => $url) : 0; + } + /** + * Returns the short URL as the source URL. + * @param string $text Short URL + * @return string/null Returns the source URL, + * if there is a source URL, or NULL if there is no source URL. + */ + public function LinkCheck($text) { + $data = $this->Select($text); + return (!is_null($data['url']) && !is_null($data['randstr'])) ? $data['url'] : NULL; + } + /** + * Move the source URL to the page. + * @param string Short URL + * @return null + */ + public function move_header($text) { + $data = $this->LinkCheck($text); + if (!is_null($data)) { + header($_SERVER['SERVER_PROTOCOL'] . ' 301 Moved Permanently'); + header('Location: ' . $data); + die('MoveMove'); + } + return NULL; + } +} + +$short = new ShortLink(); + +if ($_POST['url']) { + $data = $short->Make($_POST['url'], $_POST['str']); + if (!is_null($_POST['str']) && + (strlen($_POST['str']) > 16 || strlen($_POST['str']) < 3)) die('Short URL must be between 3 and 16 characters long.'); + else if ($data['randstr'] !== $_POST['str'] && + !is_null($_POST['str']) && + $data['url'] === $_POST['url']) die('Already registered - https://' . $_SERVER['HTTP_HOST'] . '/' . $data['randstr']); + else if (!is_null($data['url'])) die('https://' . $_SERVER['HTTP_HOST'] . '/' . $data['randstr']); + else die('Already registered short URL.'); +} +if (strlen($_SERVER['QUERY_STRING']) >= 1) + $short->move_header($_SERVER['QUERY_STRING']); +?> +
+ + +
+Hakase's Short Link Service diff --git a/shortlink.sql b/shortlink.sql new file mode 100644 index 0000000..fe6e3a2 --- /dev/null +++ b/shortlink.sql @@ -0,0 +1,13 @@ +CREATE TABLE `shortlink` ( + `id` int(11) NOT NULL, + `randstr` varchar(16) NOT NULL, + `url` varchar(255) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +ALTER TABLE `shortlink` + ADD PRIMARY KEY (`id`) USING BTREE, + ADD UNIQUE KEY `randstr` (`randstr`); + +ALTER TABLE `shortlink` + MODIFY `id` int(11) NOT NULL AUTO_INCREMENT; +COMMIT;