<?php
/* * ********************************************************************************
 *																				*
 *	Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.		*
 *																				*
 *	This  is  commercial  software,  only  users  who have purchased a valid	*
 *	license  and  accept  to the terms of the  License Agreement can install	*
 *	and use this program.														*
 *																				*
 *********************************************************************************
 *	PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE	*
 *	"copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.				*
 * ****************************************************************************/ //
//$Id: QrModel.php, v 1.0 2013/03/15 11:43:11
//
class QrModel extends Model {
    private $data;
    //FUNCTION FOR FORMATTING URL
    public function getFormattedLink($url) {
        if (preg_match('/^http:\/\//', $url) || preg_match('/^https:\/\//', $url)) {
            $this->data = $url;
        } else {
            $this->data = "http://" . $url;
        }
        return $this->data;
    }
    //FUNCTION TO RETRIVE THE URL FOR CREATING QR CODE USING GOOGLE API
    public function getQrLink($formattedURl) {
        $data = urlencode($formattedURl);
        return 'http://chart.apis.google.com/chart?chs=200x200&cht=qr&chld=M|4&chl=' . $data;
    }
    public function getLinkImage() {
        $link  = $this->getQrLink();
        $image = "<image src='" . $link . "' src=QR Code Image />";
        return $image;
    }
    //FUNCTION TO SHORTEN URL USING GOOGLE API
    public function shortenURL($longUrl, $apiKey) {
        //$apiKey	=	'AIzaSyCVbxc6WMv6beG8bTn00h2Sicsb1UAZSD4';// API KEY FROM http://code.google.com/apis/console/ 
        $postData = array(
            'longUrl' => $longUrl,
            'key' => $apiKey
        );
        $jsonData = json_encode($postData);
        $curlObj  = curl_init();
        curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
        curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curlObj, CURLOPT_HEADER, 0);
        curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(
            'Content-type:application/json'
        ));
        curl_setopt($curlObj, CURLOPT_POST, 1);
        curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
        $response = curl_exec($curlObj);
        //change the response json string to object
        $json     = json_decode($response);
        curl_close($curlObj);
        return $json->id; //SHORTENED URL
    }
}
// End of Class 


