From 55c023b17e518ef7a2adf32c443769ea7e16ab73 Mon Sep 17 00:00:00 2001 From: ozh Date: Sat, 28 Dec 2013 13:00:18 +0100 Subject: [PATCH] Should fix PNS bookmarklet --- admin/index.php | 10 ++++++++-- includes/functions.php | 39 ++++++++++++++++++++++++++++++++++++++- yourls-loader.php | 10 +++++++--- 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/admin/index.php b/admin/index.php index 4011bec..68f1e7b 100644 --- a/admin/index.php +++ b/admin/index.php @@ -130,12 +130,18 @@ } // This is a bookmarklet -if ( isset( $_GET['u'] ) ) { +if ( isset( $_GET['u'] ) or isset( $_GET['up'] ) ) { $is_bookmark = true; yourls_do_action( 'bookmarklet' ); // No sanitization needed here: everything happens in yourls_add_new_link() - $url = ( $_GET['u'] ); + if( isset( $_GET['u'] ) ) { + // Old school bookmarklet: ?u= + $url = $_GET['u']; + } else { + // New style bookmarklet: ?up=&us=&ur= + $url = $_GET['up'] . $_GET['us'] . $_GET['ur']; + } $keyword = ( isset( $_GET['k'] ) ? ( $_GET['k'] ) : '' ); $title = ( isset( $_GET['t'] ) ? ( $_GET['t'] ) : '' ); $return = yourls_add_new_link( $url, $keyword, $title ); diff --git a/includes/functions.php b/includes/functions.php index 03a2681..00ec3b0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2198,4 +2198,41 @@ function yourls_debug_log( $msg ) { global $ydb; $ydb->debug_log[] = $msg; return $msg; -} \ No newline at end of file +} + +/** + * Explode a URL in an array of ( 'protocol' , 'slashes if any', 'rest of the URL' ) + * + * Some hosts trip up when a query string contains 'http://' - see http://git.io/j1FlJg + * The idea is that instead of passing the whole URL to a bookmarklet, eg index.php?u=http://blah.com, + * we pass it by pieces to fool the server, eg index.php?proto=http:&slashes=//&rest=blah.com + * + * Known limitation: this won't work if the rest of the URL itself contains 'http://', for example + * if rest = blah.com/file.php?url=http://foo.com + * + * Sample returns: + * + * with 'mailto:jsmith@example.com?subject=hey' : + * array( 'protocol' => 'mailto:', 'slashes' => '', 'rest' => 'jsmith@example.com?subject=hey' ) + * + * with 'http://example.com/blah.html' : + * array( 'protocol' => 'http:', 'slashes' => '//', 'rest' => 'example.com/blah.html' ) + * + * @since 1.7 + * @param string $url URL to be parsed + * @param array $array Optional, array of key names to be used in returned array + * @return mixed false if no protocol found, array of ('protocol' , 'slashes', 'rest') otherwise + */ +function yourls_get_protocol_slashes_and_rest( $url, $array = array( 'protocol', 'slashes', 'rest' ) ) { + $proto = yourls_get_protocol( $url ); + + if( !$proto or count( $array ) != 3 ) + return false; + + list( $null, $rest ) = explode( $proto, $url, 2 ); + + list( $proto, $slashes ) = explode( ':', $proto ); + + return array( $array[0] => $proto . ':', $array[1] => $slashes, $array[2] => $rest ); +} + diff --git a/yourls-loader.php b/yourls-loader.php index 7251016..1e588d5 100644 --- a/yourls-loader.php +++ b/yourls-loader.php @@ -51,9 +51,13 @@ // Prefix-n-Shorten sends to bookmarklet (doesn't work on Windows) if( preg_match( "@^[a-zA-Z]+://.+@", $request, $matches ) ) { $url = yourls_sanitize_url( $matches[0] ); - yourls_do_action( 'load_template_redirect_admin', $url ); - yourls_redirect( yourls_admin_url('index.php').'?u='.rawurlencode( $url ), 302 ); - exit; + if( $parse = yourls_get_protocol_slashes_and_rest( $url, array( 'up', 'us', 'ur' ) ) ) { + yourls_do_action( 'load_template_redirect_admin', $url ); + $parse = array_map( 'rawurlencode', $parse ); + // Redirect to /admin/index.php?up=&us=&ur= + yourls_redirect( yourls_add_query_arg( $parse , yourls_admin_url( 'index.php' ) ), 302 ); + exit; + } } // Past this point this is a request the loader could not understand -- 2.45.0