<?php
// prevenir invocacion directa del script
defined('PRONTUS') OR exit('');

require_once(dirname(__FILE__) . '/config.php');
class Utiles {
    function getShortURL($large_url) {
        $shortURL = $this->bitlyUrl($large_url);
        if ($large_url == $shortURL) {
            $shortURL = $this->googleShortener($large_url);
            if ($shortURL == $large_url && $large_url == $shortURL) {
                $shortURL = $this->tinyUrl($large_url);
            }
        }
        return $shortURL;
    }

    // ---------------------------------------------------------
    function bitlyUrl($large_url) {

        if ($large_url == '') {
            return $large_url;
        }
        $format = 'txt';
        $bitly = 'http://api.bit.ly/v3/shorten?longUrl=' . urlencode($large_url) . '&login=' . BITLY_USER . '&apiKey=' . BITLY_KEY . '&format=' . $format;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $bitly);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $short_url = curl_exec($ch);
        $short_url = preg_replace('/\s+$/', '', $short_url);
        $short_url = preg_replace('/^\s+/', '', $short_url);
        curl_close($ch);
        if (empty($short_url) || strpos($short_url, 'http') === false) {
            return $large_url;
        } else {
            return $short_url;
        }
    }

    // ---------------------------------------------------------
    function desBitlyUrl($short_url) {
        //  https://dev.bitly.com/links.html#v3_expand
        if ($short_url == '') {
            return $large_url;
        }

        $format = 'txt';
        $bitly = 'http://api.bit.ly/v3/expand?shortUrl=' . urlencode($short_url) . '&login=' . BITLY_USER . '&apiKey=' . BITLY_KEY . '&format=' . $format;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $bitly);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        $large_url = curl_exec($ch);

        if (empty($large_url) || strpos($large_url, 'http') === false) {
            return $short_url;
        } else {
            return $large_url;
        }
    }

    // ---------------------------------------------------------
    function googleShortener($large_url) {
        // https://developers.google.com/url-shortener/v1/getting_started#shorten
        error_log("GOOGL_KEY -> " . GOOGL_KEY);
        $postfields = array('key' => GOOGL_KEY,
            'longUrl' => $large_url);
        $data_string = json_encode($postfields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string)));

        $short_url = curl_exec($ch);
        // Comprobar si occurió algún error
        if (curl_errno($ch)) {
            error_log("Error al acortar la URL [$large_url] con google [" . curl_error($ch) . "]");
            return $large_url;
        }
        $arr = json_decode($short_url, true);
        if (!empty($arr['id'])) {
            return $arr['id'];
        } else {
            return $large_url;
        }
    }

    // ---------------------------------------------------------
    function desGoogleShortener($short_url) {
        // https://developers.google.com/url-shortener/v1/getting_started#expand
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=' . $short_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);

        $long_url = curl_exec($ch);
        $arr = json_decode($long_url, true);
        if (!empty($arr['status']) && $arr['status'] == 'OK') {
            return $arr['longUrl'];
        } else {
            return $short_url;
        }
    }

    // ---------------------------------------------------------
    function tinyUrl($large_url) {
        if ($large_url == '') {
            return $large_url;
        }
        $tiny = "http://tinyurl.com/api-create.php?url=" . ($large_url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $tiny);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $short_url = curl_exec($ch);
        $short_url = preg_replace('/\s+$/', '', $short_url);
        $short_url = preg_replace('/^\s+/', '', $short_url);
        curl_close($ch);
        if (empty($short_url) || strpos($short_url, 'http') === false) {
            return $large_url;
        } else {
            return $short_url;
        }
    }

    function escapeEntities($titular) {
        $titular = preg_replace('/á/', 'a', $titular);
        $titular = preg_replace('/é/', 'e', $titular);
        $titular = preg_replace('/í/', 'i', $titular);
        $titular = preg_replace('/ó/', 'o', $titular);
        $titular = preg_replace('/ú/', 'u', $titular);
        $titular = preg_replace('/Á/', 'a', $titular);
        $titular = preg_replace('/É/', 'e', $titular);
        $titular = preg_replace('/Í/', 'i', $titular);
        $titular = preg_replace('/Ó/', 'o', $titular);
        $titular = preg_replace('/Ú/', 'u', $titular);
        $titular = preg_replace('/ü/', 'u', $titular);
        $titular = preg_replace('/Ü/', 'u', $titular);
        $titular = preg_replace('/ñ/', 'n', $titular);
        $titular = preg_replace('/Ñ/', 'n', $titular);

        $titular = preg_replace('/&(.)acute;/', "$1", $titular);
        $titular = preg_replace('/&(.)acute;/', "$1", $titular);
        $titular = preg_replace('/&(.)uml;/', "$1", $titular);
        $titular = preg_replace('/&(.)circ;/', "$1", $titular);
        $titular = preg_replace('/&(.)grave;/', "$1", $titular);
        return preg_replace('/&[^;]+;/', '', $titular);
    }

    // ---------------------------------------------------------
    function clearTags($titular) {

        $titular = preg_replace('/\r\n/', ' ', $titular);
        $titular = preg_replace('/\n/', ' ', $titular);
        $titular = preg_replace('/\r/', ' ', $titular);
        $titular = preg_replace('/<.*?>/', ' ', $titular);
        $titular = preg_replace('/ {2,}/', ' ', $titular);
        $titular = preg_replace('/ $/', '', $titular);
        $titular = preg_replace('/^ /', '', $titular);
        $titular = preg_replace('/&#39;/', "'", $titular);
        return preg_replace('/&quot;/', '"', $titular);
    }

    // ---------------------------------------------------------
    function clearXmlRelato($txt) {
        $txt = urldecode($txt);

        $txt = preg_replace('/<.*?>/', ' ', $txt);
        $txt = preg_replace('/ {2,}/', ' ', $txt);
        $txt = preg_replace('/ $/', '', $txt);
        return preg_replace('/^ /', '', $txt);
    }

    // ---------------------------------------------------------
    function getTwitterConf($tipo_cuenta) {
        if ($tipo_cuenta == '@AlAireLibre') {
            $OAUTH_TOKEN = OAUTH_TOKEN2;
            $OAUTH_TOKEN_SECRET = OAUTH_TOKEN_SECRET2;
            $CONSUMER_KEY = CONSUMER_KEY2;
            $CONSUMER_SECRET = CONSUMER_SECRET2;
        } else if ($tipo_cuenta == '@CooperativaTV') {
            $OAUTH_TOKEN = OAUTH_TOKEN3;
            $OAUTH_TOKEN_SECRET = OAUTH_TOKEN_SECRET3;
            $CONSUMER_KEY = CONSUMER_KEY3;
            $CONSUMER_SECRET = CONSUMER_SECRET3;
        } else {
            $OAUTH_TOKEN = OAUTH_TOKEN;
            $OAUTH_TOKEN_SECRET = OAUTH_TOKEN_SECRET;
            $CONSUMER_KEY = CONSUMER_KEY;
            $CONSUMER_SECRET = CONSUMER_SECRET;
        }
        return array($OAUTH_TOKEN, $OAUTH_TOKEN_SECRET, $CONSUMER_KEY, $CONSUMER_SECRET);
    }
}
