From 8630dcd89dc166baaa780f8216711d2eef06ac69 Mon Sep 17 00:00:00 2001 From: Caringor Date: Fri, 25 Aug 2017 14:57:12 +0800 Subject: [PATCH] First commit --- .htaccess | 14 ++++++ LICENSE | 21 +++++++++ README.md | 9 ++++ api/index.php | 1 + api/set.php | 21 +++++++++ asset/css/main.css | 101 ++++++++++++++++++++++++++++++++++++++++ asset/js/app.js | 62 ++++++++++++++++++++++++ config.php | 15 ++++++ inc/class/database.db | Bin 0 -> 3072 bytes inc/class/db.class.php | 45 ++++++++++++++++++ inc/class/url.class.php | 70 ++++++++++++++++++++++++++++ inc/functions.php | 59 +++++++++++++++++++++++ inc/index.php | 1 + inc/require.php | 13 ++++++ index.php | 46 ++++++++++++++++++ info.php | 1 + nginx.txt | 11 +++++ 17 files changed, 490 insertions(+) create mode 100644 .htaccess create mode 100644 LICENSE create mode 100644 README.md create mode 100644 api/index.php create mode 100644 api/set.php create mode 100644 asset/css/main.css create mode 100644 asset/js/app.js create mode 100644 config.php create mode 100644 inc/class/database.db create mode 100644 inc/class/db.class.php create mode 100644 inc/class/url.class.php create mode 100644 inc/functions.php create mode 100644 inc/index.php create mode 100644 inc/require.php create mode 100644 index.php create mode 100644 info.php create mode 100644 nginx.txt diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..5f20b32 --- /dev/null +++ b/.htaccess @@ -0,0 +1,14 @@ +Options -Indexes +order deny,allow + + +deny from all + + + +deny from all + + +RewriteEngine On +RewriteCond $1 !^(api|asset) +RewriteRule ^(\d+|\w+)$ index.php?id=$1 \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f292d5e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Caringor + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..979b441 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# CRZ.im +--- +> 一个支持域名缩短网站的源代码 + +## 概述 +[CRZ.im](http://crz.im/) 是一个提供网址缩短服务的网站,这是它的源代码。 + +## 安装 +[CRZ.im](http://crz.im/) 基于 PHP 进行开发,下载后您只需要修改 `config.php` 的相关配置并为 `inc/class` 文件夹提供可读写权限。对于 Nginx 用户,还需把 `nginx.txt` 里面的内容添加到 Nginx 的配置文件内。 \ No newline at end of file diff --git a/api/index.php b/api/index.php new file mode 100644 index 0000000..6c9ed5c --- /dev/null +++ b/api/index.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/api/set.php b/api/set.php new file mode 100644 index 0000000..fc34013 --- /dev/null +++ b/api/set.php @@ -0,0 +1,21 @@ +set_url($request_arr['url'], $config['length']); + } else { + $opt['content'] = '缂哄皯蹇呰鍙傛暟!'; + } + + echo json_encode($opt); + +?> \ No newline at end of file diff --git a/asset/css/main.css b/asset/css/main.css new file mode 100644 index 0000000..f1e57e8 --- /dev/null +++ b/asset/css/main.css @@ -0,0 +1,101 @@ +/* Reset */ +* { + margin: 0; + padding: 0; +} +html, body, input, text, textarea { + outline: none; + font-family: 'Arial', 'Microsoft YaHei', '榛戜綋', '瀹嬩綋', sans-serif; + font-size: 12px; +} +html, body { + background: #fff; +} +a { + text-decoration: none; +} +a:hover { + text-decoration: underline; +} + +/* Main */ +.wrap { + text-align: center; + overflow: hidden; +} +.wrap .meta { + margin: 160px 0 0 0; + opacity: 0; + transform: translateY(-150px); + transition: .5s all ease; +} +.on .wrap .meta { + opacity: 1; + transform: translateY(0); +} +.wrap .meta .title { + line-height: 1em; + color: #ff4665; + font-size: 42px; + text-transform: uppercase; +} +.wrap .meta .description { + margin: 10px 0 0 0; + line-height: 1em; + color: #7e7e7e; + font-size: 16px; + font-weight: normal; +} +.wrap .link-area { + margin: 50px 0 0 0; + opacity: 0; + transition: .5s opacity ease; +} +.on .wrap .link-area { + opacity: 1; +} +.wrap .link-area input { + display: inline-block; + vertical-align: middle; +} +.wrap .link-area #url { + width: 320px; + height: 32px; + line-height: 32px; + padding: 0 10px; + border: 3px solid #bdc3c7; + border-radius: 5px; + color: #333; +} +.wrap .link-area #url.focus, +.wrap .link-area #url:focus { + border-color: #ff4665; + transition: .2s border ease; +} +.wrap .link-area #submit { + width: 90px; + height: 38px; + margin: 0 0 0 5px; + background: #ff4665; + border-radius: 5px; + color: #fff; + border: none; + cursor: pointer; + transition: .2s opacity ease; +} +.wrap .link-area #submit:hover { + opacity: .75; +} +.wrap .link-area #submit:active { + opacity: .9; +} +.wrap .footer { + width: 100%; + bottom: 80px; + left: 0; + position: absolute; + color: #7e7e7e; +} +.wrap .footer a { + color:#ff4665; +} \ No newline at end of file diff --git a/asset/js/app.js b/asset/js/app.js new file mode 100644 index 0000000..3419f8b --- /dev/null +++ b/asset/js/app.js @@ -0,0 +1,62 @@ + +var APP = (function(){ + + var fn = { + + // 鐢熸垚鐭湴鍧 + setUrl: function(self) { + var urlEl = document.getElementById('url'); + if(urlEl.value) { + var request = { + "url": urlEl.value + }; + fn.getJson('api/set.php', true, JSON.stringify(request), function(res) { + if(res.success == 'true') { + urlEl.className = 'focus'; + urlEl.value = res.content.url; + } + }); + } else { + urlEl.setAttribute('placeholder', '缃戝潃涓嶈兘涓虹┖鍝'); + setTimeout(function() { + urlEl.setAttribute('placeholder', 'https://'); + }, 2000); + } + }, + + // 鑾峰彇 JSON 鏁版嵁 + getJson: function(url, post, data, callback) { + var xhr = new XMLHttpRequest(), + type = (post) ? 'POST' : 'GET'; + xhr.onreadystatechange = function() { + if(xhr.readyState == 4 && xhr.status == 200) { + var json = JSON.parse(xhr.responseText); + callback(json); + } else if(xhr.readyState == 4) { + callback(false); + } + } + xhr.open(type, url, true); + xhr.send(data); + } + + }, + + init = function() { + setTimeout(function() { + var el = document.getElementsByTagName('html')[0]; + el.setAttribute('class', 'on'); + }, 10); + }; + + return { + fn: fn, + init: init + } + +})(); + + +document.addEventListener('DOMContentLoaded', function() { + APP.init(); +}) \ No newline at end of file diff --git a/config.php b/config.php new file mode 100644 index 0000000..fde5f95 --- /dev/null +++ b/config.php @@ -0,0 +1,15 @@ + \ No newline at end of file diff --git a/inc/class/database.db b/inc/class/database.db new file mode 100644 index 0000000000000000000000000000000000000000..8c86ca72165033c9261842b7f6a14ea1301f8c71 GIT binary patch literal 3072 zcmeHI&2HL25MFzUNh9TGMZF|*iHfCk?ZrPpxs*nzjS~8kAVs+pIk3Pc27}p@v^V!z z`Vf7G9D3=s*WRkCw;a1mp(TfiTayDLjXmSp(SH7RX6NY50T&F~ancI~xF~}(4Lt=w z2xX;WsWZ`}k(oL2t^7rjMUQ`dUdPu+#{^-5zbr~v#+MA-Uk1`mExTcw+Q%&so<{5< ziAGBG-NS07UWIz)*+CW5m)!aqOog|g*$tCi(Sp~9d$r2p3G7!-b^!h@kr@!h{f=Ol z;zocE;9ZzZ(rIAr%w>ds19{)KW&!JwZfulI4e@@9U5xVOP{Evk4eP%@S=YPc0F2fNLTbC>XE!lT%XV)t;HXB6os}=9LOC@gz>m1T%LFtYDarjC=q7@@h0R~#|m;i;VsseNQq;m zkTJ}Xl@41)8mb*fQ=;C7-8i0XnlDJ4B|?3V(1QE_IY|Nkf!J=l?YX1u#BnEL_XA>4 zH%u;BYz9Fv2l2LQ^F4<){7JptIlG&hVfqq!yVxwu3ShqG?i%2l>VJHN@D=`wf8uZW x8h>Bd{>ueR29^xmHwIR*si}EvC_|076=kT&lTn7c-?1{(Evjd+`9R%CzX3ClXI20J literal 0 HcmV?d00001 diff --git a/inc/class/db.class.php b/inc/class/db.class.php new file mode 100644 index 0000000..b251bfc --- /dev/null +++ b/inc/class/db.class.php @@ -0,0 +1,45 @@ +db = new PDO('sqlite:' . dirname(__FILE__) . '/database.db'); + $this->init_tab(); + } + + // 鍒濆鍖栨暟鎹簱缁撴瀯 + function init_tab() { + // 缃戝潃琛 + $this->db->exec("CREATE TABLE urls( + id char(8) PRIMARY KEY, + url longtext, + ip varchar(16), + ua varchar(255) + )"); + } + + // 鏌ヨ琛ㄥ唴瀹 + function query($name, $rule = '') { + $query = $this->db->prepare("SELECT * FROM $name $rule"); + $query->execute(); + $result = $query->fetchAll(); + return $result; + } + + // 鎻掑叆琛ㄥ唴瀹 + function insert($tab, $key, $val) { + $exec = $this->db->exec("INSERT INTO $tab ($key) VALUES($val)"); + if(!$exec) return false; + $this->db->beginTransaction(); + } + + // 鍒犻櫎琛ㄥ唴瀹 + function delete($tab, $rule = '') { + $exec = $this->db->exec("DELETE FROM $tab $rule"); + if(!$exec) return false; + $this->db->beginTransaction(); + } + + } + +?> \ No newline at end of file diff --git a/inc/class/url.class.php b/inc/class/url.class.php new file mode 100644 index 0000000..9e2e10c --- /dev/null +++ b/inc/class/url.class.php @@ -0,0 +1,70 @@ +db = $db_c; + } + + // 鐢熸垚鐭湴鍧 + public function set_url($url, $size = 4) { + $id = $this->get_id($url); + if(!$id) { + $id = $this->create_id($url, $size); + $ip = get_ip(); + $ua = get_ua(); + $this->db->insert('urls', 'id, url, ip, ua', "'$id', '$url', '$ip', '$ua'"); + } + $s_url = get_uri() . $id; + return $s_url; + } + + // 鐢熸垚鍦板潃 ID + public function create_id($url, $size = 4) { + $md5 = md5($url); + // 闅忔満鎶藉彇 MD5 涓殑瀛楃浣滀负 ID + $id = ''; + for($i = 0; $i < $size; $i++) { + $rand_id = rand(0, strlen($md5) - 1); + $id .= $md5[$rand_id]; + } + // ID 妫娴 + if($this->has_id($id)) { + return $this->create_id($url, $size); + } else { + return $id; + } + } + + // 鏌ヨ ID 鍙 + public function get_id($url) { + $result = $this->db->query('urls', "WHERE url = '$url'"); + (count($result) > 0) ? $opt = $result[0]['id'] : $opt = false; + return $opt; + } + + // 鏌ヨ鐩爣鍦板潃 + public function get_url($id) { + $result = $this->db->query('urls', "WHERE id = '$id'"); + (count($result) > 0) ? $opt = $result[0]['url'] : $opt = false; + return $opt; + } + + // 妫娴 ID 鏄惁宸茬粡瀛樺湪 + public function has_id($id) { + $result = $this->db->query('urls', "WHERE id = '$id'"); + (count($result) > 0) ? $opt = true : $opt = false; + return $opt; + } + + // 娓呯┖鐭湴鍧 + public function clean_urls() { + $del = $this->db->delete('urls'); + if($del) return true; + return false; + } + + } + +?> \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php new file mode 100644 index 0000000..b7e07c1 --- /dev/null +++ b/inc/functions.php @@ -0,0 +1,59 @@ + \ No newline at end of file diff --git a/inc/index.php b/inc/index.php new file mode 100644 index 0000000..6c9ed5c --- /dev/null +++ b/inc/index.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/inc/require.php b/inc/require.php new file mode 100644 index 0000000..9d34574 --- /dev/null +++ b/inc/require.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..65aee88 --- /dev/null +++ b/index.php @@ -0,0 +1,46 @@ +get_url($_GET['id']); + // 閲嶅畾鍚戣嚦鐩爣缃戝潃 + if($url) { + header('Location: ' . $url); + exit; + } + + } + +?> + + + + + + <?php echo get_title() . ' - ' . get_description(); ?> + + + + + +
+
+

+

+
+ + +
+ + + + \ No newline at end of file diff --git a/info.php b/info.php new file mode 100644 index 0000000..c9f5eeb --- /dev/null +++ b/info.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/nginx.txt b/nginx.txt new file mode 100644 index 0000000..1d7cc2c --- /dev/null +++ b/nginx.txt @@ -0,0 +1,11 @@ +location / { + try_files $uri $uri/ =404; +} + +location ~ /inc/ { + return 403; +} + +location ~* ^/(?![asset|api]) { + rewrite (\d+|\w+)$ /index.php?id=$1; +} \ No newline at end of file