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('