Repository: takashiki/Ourls Branch: master Commit: e7789805f044 Files: 15 Total size: 15.7 KB Directory structure: gitextract_il3ij_3v/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── app/ │ ├── components/ │ │ └── Hash.php │ ├── config.sample.php │ ├── helpers.php │ ├── routes.php │ └── views/ │ └── index.php ├── composer.json ├── public/ │ ├── .htaccess │ ├── css/ │ │ └── app.css │ ├── index.php │ └── js/ │ └── index.js └── urls.sql ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ .idea /app/config.php vendor /config.yaml ================================================ FILE: .travis.yml ================================================ language: php php: - '5.6' - '7.0' - '7.1' before_install: - composer install ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2011 Mike Cao 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. ================================================ FILE: README.md ================================================ # Ourls [![Latest Stable Version](https://poser.pugx.org/takashiki/ourls/v/stable)](https://packagist.org/packages/takashiki/ourls) [![Total Downloads](https://poser.pugx.org/takashiki/ourls/downloads)](https://packagist.org/packages/takashiki/ourls) [![Latest Unstable Version](https://poser.pugx.org/takashiki/ourls/v/unstable)](https://packagist.org/packages/takashiki/ourls) [![License](https://poser.pugx.org/takashiki/ourls/license)](https://packagist.org/packages/takashiki/ourls) Ourls是一个基于发号和hashid的短网址服务,灵感来源于知乎上关于短址算法的一个讨论—— [http://www.zhihu.com/question/29270034](http://www.zhihu.com/question/29270034)。 ## 特征/Feature Ourls会根据sha1值来判断原url在数据库中是否已存在,若不存在则新增记录后对记录id进行hash,产生短网址。 Ourls会对输入的url进行标准化处理,若为缺少scheme的url,会默认自动加上`http://`, 并且会对url的query参数进行排序和urlencode等。 ## 演示/Demo [在线演示/Online Demo](http://skyx.in) ## 安装/Install 下载源码后运行`composer install`安装依赖包,或者运行`composer create-project takashiki/ourls`。 然后将urls.sql导入数据库中,将app目录下config.sample.php重命名为config.php并按自己实际情况修改相关配置项。 > git clone and composer install or composer create-project takashiki/ourls > import urls.sql to your database > rename app/config.sample.php to app/config.php > modify the config file according to your situation ### License Ourls is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT) ================================================ FILE: app/components/Hash.php ================================================ hashids = new Hashids( $params['salt'], $params['length'], $params['alphabet'] ); } public function encode($id) { return $this->hashids->encode($id); } public function decode($hash) { $id = $this->hashids->decode($hash); return $id ? $id[0] : false; } } ================================================ FILE: app/config.sample.php ================================================ true, 'base_url' => 'YourSiteUrl', 'hash' => [ 'salt' => 'SomeRandomKey', 'length' => 5, 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', ], 'db' => [ 'database_type' => 'mysql', 'database_name' => 'name', 'server' => 'localhost', 'username' => 'your_username', 'password' => 'your_password', 'charset' => 'utf8', 'port' => 3306, 'option' => [ PDO::ATTR_CASE => PDO::CASE_NATURAL, ], ], 'db_read' => [ 'database_type' => 'mysql', 'database_name' => 'name', 'server' => 'localhost', 'username' => 'your_username', 'password' => 'your_password', 'charset' => 'utf8', 'port' => 3306, 'option' => [ PDO::ATTR_CASE => PDO::CASE_NATURAL, ], ], 'settings' => [ 'external_js' => null, ], 'proxies' => [ '127.0.0.0/8', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fd00::/8', ], ]; ================================================ FILE: app/helpers.php ================================================ normalize(); if (filter_var(IdnaConvert::encodeString($url), FILTER_VALIDATE_URL) === false) { return false; } else { $fragment = parse_url($url, PHP_URL_FRAGMENT); return str_replace('#'.$fragment, '#'.urldecode($fragment), $url); } } } if (!function_exists('real_remote_addr')) { function real_remote_addr() { $ip = Flight::request()->ip; $proxy = Flight::request()->proxy_ip; if ('' != $proxy && Flight::get('proxies')->match($ip)) { return $proxy; } else { return $ip; } } } /* * Registers a class and set a variable to framework method. * * @param string $name Method name * @param string $class Class name * @param array $params Class initialization parameters * @param callback $callback Function to call after object instantiation * @throws \Exception If trying to map over a framework method */ Flight::map('instance', function ($name, $class, array $params = [], $callback = null) { Flight::register($name, $class, $params, $callback); Flight::set($name, Flight::{$name}()); }); ================================================ FILE: app/routes.php ================================================ query['url']); if ($url) { if (strpos($url, Flight::get('flight.base_url')) !== false) { Flight::json(['status' => 0, 'msg' => '该地址无法被缩短']); } else { $sha1 = sha1($url); $store = Flight::get('db_read')->select('urls', ['id'], [ 'sha1' => $sha1, ]); if (!$store) { $id = Flight::get('db')->insert('urls', [ 'sha1' => $sha1, 'url' => $url, 'create_at' => time(), 'creator' => ip2long(real_remote_addr()), ]); } else { $id = $store[0]['id']; } $s_url = Flight::get('flight.base_url').Flight::get('hash')->encode($id); Flight::json(['status' => 1, 's_url' => $s_url]); } } else { Flight::json(['status' => 0, 'msg' => '请传入正确的url']); } }); Flight::route('/expand', function () { $s_url = Flight::request()->query['s_url']; if ($s_url) { $hash = str_replace(Flight::get('flight.base_url'), '', $s_url); if (!preg_match('/^['.Flight::get('alphabet').']+$/', $hash)) { Flight::json(['status' => 0, 'msg' => '短址不正确']); } else { $id = Flight::get('hash')->decode($hash); if (!$id) { Flight::json(['status' => 0, 'msg' => '短址无法解析']); } else { $store = Flight::get('db_read')->select('urls', ['url'], [ 'id' => $id, ]); if (!$store) { Flight::json(['status' => 0, 'msg' => '地址不存在']); } else { Flight::json(['status' => 1, 'url' => $store[0]['url']]); } } } } }); Flight::route('/@hash', function ($hash) { $id = Flight::get('hash')->decode($hash); if (!$id) { Flight::notFound('短址无法解析'); } else { $store = Flight::get('db_read')->select('urls', ['url'], [ 'id' => $id, ]); if (!$store) { Flight::notFound('地址不存在'); } else { Flight::get('db')->update('urls', ['count[+]' => 1], [ 'id' => $id, ]); Flight::redirect($store[0]['url'], 302); } } }); Flight::map('notFound', function ($message) { Flight::response()->status(404) ->header('content-type', 'text/html; charset=utf-8') ->write( '

404 页面未找到

'. "

{$message}

". '

回到首页

'. str_repeat(' ', 512) ) ->send(); }); Flight::map('error', function (Exception $ex) { $message = Flight::get('flight.log_errors') ? $ex->getTraceAsString() : '出错了'; Flight::response()->status(500) ->header('content-type', 'text/html; charset=utf-8') ->write( '

500 服务器内部错误

'. "

{$message}

". '

回到首页

'. str_repeat(' ', 512) ) ->send(); }); ================================================ FILE: app/views/index.php ================================================ Ourls Fork me on GitHub

Ourls

Url Shorten Service
基于发号加hash id的短网址服务




© Ourls . Licensed under MIT license.

================================================ FILE: composer.json ================================================ { "name": "takashiki/ourls", "description": "A url shorten service system base on hash id", "keywords": ["url shorten", "hashids"], "homepage": "https://github.com/takashiki/ourls", "license": "MIT", "authors": [ { "name": "takashiki", "email": "857995137@qq.com", "homepage": "http://blog.skyx.in/" } ], "require": { "php": ">=5.6.4", "catfan/medoo": "^0.9.8", "etechnika/idna-convert": "^1.1", "glenscott/url-normalizer": "*", "hashids/hashids": "^2.0", "mikecao/flight": "^1.2", "wikimedia/ip-set": "1.1.0" }, "autoload": { "psr-4": { "app\\": "app/" } }, "config": { "preferred-install": "dist", "sort-packages": true, "optimize-autoloader": true } } ================================================ FILE: public/.htaccess ================================================ RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] ================================================ FILE: public/css/app.css ================================================ .header { text-align: center; } .header h1 { font-size: 200%; color: #333; margin-top: 30px; } ================================================ FILE: public/index.php ================================================