Skip to content

Commit

Permalink
FIRST
Browse files Browse the repository at this point in the history
  • Loading branch information
SRY-CTB committed Aug 1, 2023
0 parents commit 51463f3
Show file tree
Hide file tree
Showing 17 changed files with 661 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Options -Indexes
order deny,allow

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>

<Files ~ "^database.db">
deny from all
</Files>

RewriteEngine On
RewriteCond $1 !^(api|asset)
RewriteRule ^(\d+|\w+)$ index.php?id=$1
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# ShortLink

**简体中文** | [English](./README.en.md)

代码基于 [CRZ.im](https://github.com/Caringor/CRZ.im) 二次开发,感谢原作者的辛勤劳动。

## 概述

这是一个网址缩短服务的网站的源代码。

## 安装

### 环境准备

+ 兼容 PHP 7.x
+ `Nginx 1.15+`
+ ~~`MySQL 5.5+`~~ (目前还不需要)

提示:若访问量较大,建议安装在具有SSD硬盘的服务器上。

### 配置修改

修改 `config.php` 的相关配置并把 `inc` 目录权限设置为 `755` 即可。

### URL 重写规则

#### Apache 用户

直接使用 `.htaccess` 文件即可。

#### Nginx 用户

需要把 `nginx-rewrite.conf` 里面的内容添加到 `Nginx` 对欲使用网站的配置文件里。

## 功能

+ 长链转短链
+ 界面简洁
+ 一键复制

## Todo List

+ 增加 `MySQL` 支持
+ More...
31 changes: 31 additions & 0 deletions api/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
// 引入类
require_once('../inc/require.php');
global $config;
if($config['api']) {
$url_c = new url();

$opt = [];
$opt['success'] = 'false';

if(isset($_GET['url'])) {
// 添加 HTTP 协议前缀
if(!strstr($_GET['url'], 'http://') && !strstr($_GET['url'], 'https:')) $_GET['url'] = 'http://' . $_GET['url'];
// 检测网址格式是否正确
$is_link = preg_match('(http(|s)://([\w-]+\.)+[\w-]+(/)?)', $_GET['url']);
// 判断条件
if($_GET['url'] != '' && !strstr($_GET['url'], $_SERVER['HTTP_HOST']) && $is_link) {
$opt['success'] = 'true';
$opt['content']['url'] = $url_c->set_url($_GET['url'], $config['length']);
} else if(strstr($_GET['url'], $_SERVER['HTTP_HOST'])) {
$opt['content'] = '链接已经是短地址了。';
} else if(!$is_link) {
$opt['content'] = '请输入正确格式的网址。';
}
} else {
$opt['content'] = '调用参数不能为空。';
}
// 输出
echo json_encode($opt);
}
?>
1 change: 1 addition & 0 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php exit; ?>
30 changes: 30 additions & 0 deletions api/set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
// 引入类
require_once('../inc/require.php');
global $config;
$url_c = new url();

$opt = [];
$opt['success'] = 'false';

$request_arr = json_decode(file_get_contents('php://input'), true);
if(isset($request_arr['url'])) {
// 添加 HTTP 协议前缀
if(!strstr($request_arr['url'], 'http://') && !strstr($request_arr['url'], 'https:')) $request_arr['url'] = 'http://' . $request_arr['url'];
// 检测网址格式是否正确
$is_link = preg_match('(http(|s)://([\w-]+\.)+[\w-]+(/)?)', $request_arr['url']);
// 判断条件
if($request_arr['url'] != '' && !strstr($request_arr['url'], $_SERVER['HTTP_HOST']) && $is_link) {
$opt['success'] = 'true';
$opt['content']['url'] = $url_c->set_url($request_arr['url'], $config['length']);
} else if(strstr($request_arr['url'], $_SERVER['HTTP_HOST'])) {
$opt['content'] = '链接已经是短地址了。';
} else if(!$is_link) {
$opt['content'] = '请输入正确格式的网址。';
}
} else {
$opt['content'] = '调用参数不能为空。';
}
// 输出
echo json_encode($opt);
?>
103 changes: 103 additions & 0 deletions asset/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* 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,
.wrap .link-area #shorturl {
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 #shorturlcopy,
.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;
}
60 changes: 60 additions & 0 deletions asset/js/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

function copyText() {
var input = document.getElementById("shorturl");
input.select(); // 选中文本
document.execCommand("copy"); // 执行浏览器复制命令
}


var APP = (function(){

var fn = {

// 生成短地址
setUrl: function(self) {
var urlEl = document.getElementById('url'),
tips = 'https://',
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;
$res = document.getElementById('shorturl')
$res.className = 'focus';
$res.value = res.content.url;
} else {
urlEl.className = '';
urlEl.value = '';
urlEl.setAttribute('placeholder', res.content);
setTimeout(function() {
urlEl.setAttribute('placeholder', tips);
}, 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.className = 'on';
}, 10);
};
return {fn: fn, init: init}
})();

document.addEventListener('DOMContentLoaded', function() {APP.init();})
17 changes: 17 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

global $config;
$config = [];

// 程序安装路径
$config['path'] = '/';
// ID 长度
$config['length'] = 8;
// 网站标题
$config['title'] = '短链生成';
// 网站简介
$config['description'] = '在线网址缩短服务';

$config['api'] = true;

?>
Binary file added inc/class/database.db
Binary file not shown.
32 changes: 32 additions & 0 deletions inc/class/db.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
class db {
function __construct() {
$this->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();
}
}
?>
Loading

0 comments on commit 51463f3

Please sign in to comment.