-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
### URL Shortener | ||
|
||
#### 简述 | ||
|
||
轻量级自定义短链接生成 | ||
|
||
#### 依赖 | ||
|
||
[vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) | ||
|
||
[catfan/medoo](https://github.com/catfan/Medoo) | ||
|
||
#### 安装 | ||
|
||
`composer require laijingwu/url-shortener` | ||
|
||
#### Intro | ||
|
||
A light url shortener for customizing links. | ||
|
||
#### Dependence | ||
|
||
[vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) | ||
|
||
[catfan/medoo](https://github.com/catfan/Medoo) | ||
|
||
#### Installation | ||
|
||
`composer require laijingwu/url-shortener` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
{ | ||
"name": "laijingwu", | ||
"email": "[email protected]", | ||
"homepage": "http://laijingwu.com" | ||
"homepage": "https://laijingwu.com" | ||
} | ||
], | ||
"require": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<IfModule mod_rewrite.c> | ||
Options +FollowSymLinks | ||
RewriteEngine On | ||
|
||
RewriteCond %{REQUEST_FILENAME} !-d | ||
RewriteCond %{REQUEST_URI} (.+)/$ | ||
RewriteRule ^ %1 [L,R=301] | ||
|
||
RewriteCond %{REQUEST_FILENAME} !-d | ||
RewriteCond %{REQUEST_FILENAME} !-f | ||
RewriteRule ^ index.php [L] | ||
</IfModule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
require __DIR__."/../vendor/autoload.php"; | ||
header("Content-Type: application/json"); | ||
|
||
$raw_post_data = file_get_contents('php://input', 'r'); | ||
$data = json_decode($raw_post_data); | ||
if (!$data || !isset($data->type) || !isset($data->uri)) { | ||
echo json_encode([ | ||
'code' => 1000, | ||
'errmsg' => "invalid request." | ||
]); | ||
exit; | ||
} | ||
|
||
$dotenv = new Dotenv\Dotenv(__DIR__."/../"); | ||
$dotenv->load(); | ||
$dotenv->required(['DB_DATABASE', 'DB_USERNAME', 'DB_PASSWORD', 'DB_TABLE']); | ||
$config = require __DIR__."/../config.php"; | ||
$shortener = UrlShortener\UrlShortener::getInstance($config['db'], $config['options']); | ||
|
||
$type = $data->type; | ||
$uri = $data->uri; | ||
if ($type == "short") { | ||
$long = $shortener->toLong($uri); | ||
if ($long == false) { | ||
echo json_encode([ | ||
'code' => 1001, | ||
'errmsg' => "invalid short uri." | ||
]); | ||
exit; | ||
} | ||
echo json_encode([ | ||
'code' => 0, | ||
'data' => $long | ||
]); | ||
} elseif ($type == "long") { | ||
$short = $shortener->toShort($uri); | ||
if ($short == false) { | ||
echo json_encode([ | ||
'code' => -1, | ||
'errmsg' => "database error." | ||
]); | ||
exit; | ||
} | ||
echo json_encode([ | ||
'code' => 0, | ||
'data' => $short | ||
]); | ||
} else { | ||
echo json_encode([ | ||
'code' => 1000, | ||
'errmsg' => "invalid request." | ||
]); | ||
exit; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters