Created
September 26, 2010 20:18
-
-
Save sivel/598286 to your computer and use it in GitHub Desktop.
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
<?php | |
/* | |
This PHP script will function as a URL Shortening endpoint for Google Apps | |
Short Links. | |
Copyright (C) 2010 Matt Martz <matt@sivel.net> (http://sivel.net/) | |
This program is released under the GNU General Public License version 2 | |
http://www.gnu.org/licenses/gpl-2.0.txt | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
version 2 as published by the Free Software Foundation. | |
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 version 2 | |
along with this program; if not, see <http://www.gnu.org/licenses/gpl-2.0.txt> | |
*/ | |
/* | |
Sample requests: | |
Hashed shortlink: | |
http://s.example.org/google-shortlinks-endpoint.php?url=http://google.com | |
Custom shortlink: | |
http://s.example.org/google-shortlinks-endpoint.php?url=http://google.com&shortcut=google | |
Generally a request coming from something like TweetDeck will just be a hashed | |
shortlink request. | |
TweetDeck Directions: | |
- Click the 'Settings' Button (the wrench icon) | |
- Click 'Services' | |
- Select 'Other' for the service you wish to use to shorten URLs | |
- Enter the URL to google-shortlinks-endpoint.php. ex: http://example.org/google-shortlinks-endpoint.php?url=%@ | |
- Click 'Save settings' and enjoy! | |
*/ | |
/* Modify the following 3 variables */ | |
$hostname = 's.example.org'; # The hostname specified for use by Google Apps Short Links | |
$secret = 'mfkeOkdugagcavTedyupcleqokDodken'; # Your Short Links HMAC secret | |
$email = 'email@example.org'; # Your email address which is allowed to use the API | |
/* That's all, stop editing! */ | |
/** | |
* Set HTTP status header. | |
* | |
* Accepts an int and responds with the proper HTTP status header. | |
* Code was borowed from a WordPress function of the same name to | |
* make this work. | |
* | |
* @param int $header HTTP status code | |
*/ | |
function status_header( $header ) { | |
switch ( $header ) { | |
case 400: | |
$text = 'Bad Request'; | |
break; | |
case 500: | |
$text = 'Internal Server Error'; | |
break; | |
} | |
$protocol = $_SERVER["SERVER_PROTOCOL"]; | |
if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol ) | |
$protocol = 'HTTP/1.0'; | |
header( "$protocol $header $text", true, $header ); | |
die(); | |
} | |
// Bail if a URL was not specified | |
if ( ! isset( $_GET['url'] ) ) { | |
status_header( 400 ); | |
} | |
// Determine if a shortcut name was provied if not we are only creating a random hash | |
$method = isset( $_GET['shortcut'] ) ? 'get_or_create_shortlink' : 'get_or_create_hash'; | |
$base_url = "http://$hostname/js/$method"; | |
// Build the array of query variables | |
$args = array( | |
'is_public' => 'true', | |
'oauth_signature_method' => 'HMAC-SHA1', | |
'shortcut' => isset( $_GET['shortcut'] ) ? $_GET['shortcut'] : 'shortlink', | |
'timestamp' => time() . '.0', | |
'url' => $_GET['url'], | |
'user' => $email, | |
); | |
// Build the query string from the above array | |
$query = http_build_query( $args ); | |
// Build the signature base string to be used in creating the OAuth Signature | |
$signature_base_string = sprintf( "GET&%s&%s", urlencode( $base_url ), urlencode( $query ) ); | |
// Build the OAuth Signature using the above base string and our HMAC secret, using HMAC-SHA1 | |
$signature = urlencode( base64_encode( hash_hmac( 'sha1', $signature_base_string, $secret, true ) ) ); | |
// Build the URL with the base url, query string and auth signature | |
$url = "$base_url?$query&oauth_signature=$signature"; | |
// Build the cURL request | |
$request = curl_init( $url ); | |
curl_setopt( $request, CURLOPT_CONNECTTIMEOUT, 5 ); | |
curl_setopt( $request, CURLOPT_HEADER, false ); | |
curl_setopt( $request, CURLOPT_RETURNTRANSFER, true ); | |
curl_setopt( $request, CURLOPT_FOLLOWLOCATION, true ); | |
curl_setopt( $request, CURLOPT_MAXREDIRS, 5 ); | |
curl_setopt( $request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); | |
// Execute the cURL request | |
$response = curl_exec( $request ); | |
$http_code = curl_getinfo( $request, CURLINFO_HTTP_CODE ); | |
curl_close( $request ); | |
header( 'Content-Type: text/plain' ); | |
// Send the resultant URL back | |
if ( $http_code != 200 ) { | |
status_header( 500 ); | |
} else { | |
$response_decode = json_decode( $response ); | |
if ( $response_decode->status != 'ok' ) | |
status_header( 500 ); | |
echo "http://$hostname/{$response_decode->shortcut}"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment