-
Notifications
You must be signed in to change notification settings - Fork 2
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
2 changed files
with
116 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,2 +1,39 @@ | ||
# YOURLS-doTLS | ||
Always use TLS for a destination url in YOURLS if available | ||
Always use SSL/TLS for a destination url in YOURLS if available | ||
|
||
#### Function: | ||
This plugin will upgrade links to SSL/TLS when | ||
|
||
- adding new links | ||
- redirecting short links | ||
- checking link stats | ||
- etc. | ||
|
||
#### Installation | ||
Works with YOURLS 1.7.3(+) or YOURLS 1.7.2(-?) | ||
|
||
As any YOURLS plugin: | ||
|
||
- Extract the `doTLS` folder in this archive into `user/plugins/` | ||
- Enable in the admin interface. | ||
|
||
#### Note: | ||
This may cause some small delays on page loads in the admin interface if there are a lot of links to check. This _should_ pass. | ||
|
||
=========================== | ||
|
||
Copyright (C) 2019 Josh Panter | ||
|
||
This program is free software: you can redistribute it and/or modify | ||
|
||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
|
||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. |
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,78 @@ | ||
<?php | ||
/* | ||
Plugin Name: Do TLS | ||
Plugin URI: https://github.com/joshp23/YOURLS-doTLS | ||
Description: Always use TLS for destination url if available | ||
Version: 0.0.1 | ||
Author: Josh Panter | ||
Author URI: https://unfettered.net | ||
*/ | ||
|
||
// No direct call | ||
if( !defined( 'YOURLS_ABSPATH' ) ) die(); | ||
|
||
/* | ||
Ties into YOURLS core | ||
*/ | ||
|
||
// do TLS when adding new links, etc | ||
yourls_add_filter( 'esc_url', 'doTLS_esc' ); | ||
function doTLS_esc ( $url, $original_url, $context ) { | ||
if ( doTLS_is_valid( $url ) ) | ||
$url = doTLS_check ( $url ); | ||
return $url; | ||
} | ||
|
||
// do TLS when redirecting short links, stats, etc | ||
yourls_add_filter( 'get_keyword_info', 'doTLS_info' ); | ||
function doTLS_info ( $value, $keyword, $field, $notfound ) { | ||
if ( $value && $field == 'url' ) | ||
if ( doTLS_is_valid( $value ) ) { | ||
$value = doTLS_check ( $value ); | ||
if ( yourls_get_protocol( $value ) == 'https://' ) | ||
doTLS_update ( $value, $keyword ); | ||
} | ||
return $value; | ||
} | ||
|
||
/* | ||
doTLS jobs | ||
*/ | ||
|
||
function doTLS_is_valid ( $url ) { | ||
$value = ( ! yourls_get_protocol( $url ) || yourls_get_protocol( $url ) == 'http://' ) ? true : false; | ||
return $value; | ||
} | ||
|
||
function doTLS_check ( $url ) { | ||
|
||
if ( ! yourls_get_protocol( $url ) ) | ||
$doTLS = 'https://'.$url; | ||
elseif ( yourls_get_protocol( $url ) == 'http://' ) | ||
$doTLS = substr_replace($url, 's', 4, 0); | ||
|
||
if ($doTLS) { | ||
$ch = curl_init( $doTLS ); | ||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE); | ||
curl_setopt( $ch, CURLOPT_NOBODY, TRUE ); | ||
curl_exec($ch); | ||
|
||
if ( curl_errno($ch) == 0 ) | ||
$url = $doTLS; | ||
|
||
curl_close($ch); | ||
} | ||
return $url; | ||
} | ||
|
||
function doTLS_update ( $url, $keyword ) { | ||
global $ydb; | ||
$table = defined( 'YOURLS_DB_PREFIX' ) ? YOURLS_DB_PREFIX . 'url' : 'url'; | ||
if (version_compare(YOURLS_VERSION, '1.7.3') >= 0) { | ||
$binds = array('keyword' => $keyword, 'url' => $url); | ||
$sql = "UPDATE `$table` SET `url` = :url WHERE `keyword` = :keyword;"; | ||
$update = $ydb->fetchAffected($sql, $binds); | ||
} else { | ||
$update = $ydb->query("UPDATE `$table` SET `url` = $url WHERE `keyword` = $keyword;"); | ||
} | ||
} |