<?php

function isYoutubeVideo($url) {
    $yt1 = strpos($url, "youtube.com");
    $yt2 = strpos($url, "youtu.be");

    return (($yt1) OR ($yt2));
}

function isVimeoVideo($url) {
    return strpos($url, "vimeo.com");
}

function getVimeoVideoID($url) {
    $url = trim($url);
    $matches = array();
    
    $result = preg_match('/(\d+)/', $url, $matches);
    if($result && isset($matches[0]) && $matches[0] != '') {
        return $matches[0];
    }
}

function getYouTubeVideoID($url) {
    $url = trim($url);
    $matches = array();
    
    $result = preg_match('#(\.be/|/embed/|/v/|/watch\?v=)([A-Za-z0-9_-]{5,11})#', $url, $matches);
    if($result && isset($matches[2]) && $matches[2] != '') {
        return $matches[2];
    }
}

function checkYoutubeId($id) {
    if (!$data = @file_get_contents("http://gdata.youtube.com/feeds/api/videos/" . $id))
        return 0;
    if ($data == "Video not found")
        return 0;
    return 1;
}

function checkVimeoId($id) {
    if (!$data = @file_get_contents("http://vimeo.com/api/v2/video/" . $id . ".json"))
        return 0;
    if ($data == "Video not found")
        return 0;
    return 1;
}


function getYouTubeVideoDetails($id) {
    // get the xml data from youtube
    $url = "http://gdata.youtube.com/feeds/api/videos/" . $id;
    $xml = simplexml_load_file($url);
    return $xml;
}

function getVimeoVideoDetails($id) {
    // get the xml data from youtube
    $url = "http://vimeo.com/api/v2/video/" . $id . ".xml";
    $xml = simplexml_load_file($url);
    return $xml;
}


function fetch_image($url) {
    if (function_exists("curl_init")) {
        return curl_fetch_image($url);
    } elseif (ini_get("allow_url_fopen")) {
        return fopen_fetch_image($url);
    }
}

function curl_fetch_image($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $image = curl_exec($ch);
    curl_close($ch);
    return $image;
}

function fopen_fetch_image($url) {
    $image = file_get_contents($url, false, $context);
    return $image;
}

if ( !defined( 'DECODE_ENCODE_KEY' ) )
    define( 'DECODE_ENCODE_KEY', 'asong2u' );

function bp_dedication_encode($string) {
    return base64_encode($string);
//    $key = sha1($key);
//    $strLen = strlen($string);
//    $keyLen = strlen($key);
//    for ($i = 0; $i < $strLen; $i++) {
//        $ordStr = ord(substr($string,$i,1));
//        if ($j == $keyLen) { $j = 0; }
//        $ordKey = ord(substr($key,$j,1));
//        $j++;
//        $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
//    }
//    return $hash;
}

function bp_dedication_decode($string) {
    return base64_decode($string);
//    $key = sha1($key);
//    $strLen = strlen($string);
//    $keyLen = strlen($key);
//    for ($i = 0; $i < $strLen; $i+=2) {
//        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
//        if ($j == $keyLen) { $j = 0; }
//        $ordKey = ord(substr($key,$j,1));
//        $j++;
//        $hash .= chr($ordStr - $ordKey);
//    }
//    return $hash;
}


?>
