<?php

/* ---------------------------------------------------------------------------------------------------------------------------+
+ Warning and Disclaimer:                                                                                                     +
+    Google and all its related logos & trademarks are properties of Google Inc. (http://www.google.com/)                     +
+    Your use of Google API and goo.gl service are subject to your agreement of acceptance of Google's terms and conditions.  +
+        See http://goo.gl/, http://www.google.com/accounts/TOS and http://code.google.com/terms.html for more details.       +
+    The author of the following code is not related to Google and does not represent Google.                                 +
+ ----------------------------------------------------------------------------------------------------------------------------+ */

/*
******************************************************************************************
*                                                                                        *
*    goo.gl URL shortner for PmWiki - Shorten URLs in your wiki using Google's service   *
*    Copyright (C) Sept 20, 2011  Subhrajit Bhattacharya                                 *
*                                                                                        *
*    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 <http://www.gnu.org/licenses/>.         *
*                                                                                        *
*    Additional conditions apply for your use of Google APIs.                            *
*    See http://code.google.com/terms.html for details.                                  *
*                                                                                        *
*    Contact: subhrajit@gmail.com, http://www.subhrajit.net/                             *
*                                                                                        *
*                                                                                        *
******************************************************************************************
*/

// goo.gl URL shortener.

function Get_goo_gl_URL($LongURL) {
    $opts = array( 'http'=>array(
                                 'method' => "POST",
                                 'header' => "Content-Type: application/json",
                                 'content' => "{\"longUrl\": \"".$LongURL."\"}"  )  );
    $context = stream_context_create($opts);
    
    $JSON_content = file_get_contents("https://www.googleapis.com/urlshortener/v1/url", false, $context);
    if ( preg_match("/\"id\"\\s*:\\s*\"(.*?)\"/i", $JSON_content, $matches) )
        return($matches[1]);
    else
        return("ERROR in retrieving goo.gl URL.");
}

// PHP variable
$ThisPageGooGlURL = Get_goo_gl_URL( $ScriptUrl.'?'.preg_replace("/\\?/","&",$_SERVER['QUERY_STRING'],1) );

//------------------------------------------------------
// Page variables

// Just the URL
$FmtPV['$ThisPageGooGlURL'] = '"'.$ThisPageGooGlURL.'"';


?>