<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class YoutubeDownloadModel extends CI_Model {
    
    public function __construct() {
        parent::__construct();
    }
    
    function curlGet($URL) {
        $ch = curl_init();
        $timeout = 3;
        curl_setopt( $ch , CURLOPT_URL , $URL );
        curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );
        curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT , $timeout );
        /* if you want to force to ipv6, uncomment the following line */ 
        //curl_setopt( $ch , CURLOPT_IPRESOLVE , 'CURLOPT_IPRESOLVE_V6');
        $tmp = curl_exec( $ch );
        curl_close( $ch );
        return $tmp;
    }  

    /* 
     * function to use cUrl to get the headers of the file 
     */ 
    function get_location($url) {
        $my_ch = curl_init();
        curl_setopt($my_ch, CURLOPT_URL,$url);
        curl_setopt($my_ch, CURLOPT_HEADER,         true);
        curl_setopt($my_ch, CURLOPT_NOBODY,         true);
        curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($my_ch, CURLOPT_TIMEOUT,        10);
        $r = curl_exec($my_ch);
         foreach(explode("\n", $r) as $header) {
            if(strpos($header, 'Location: ') === 0) {
                return trim(substr($header,10)); 
            }
         }
        return '';
    }

    function get_size($url) {
        $my_ch = curl_init();
        curl_setopt($my_ch, CURLOPT_URL,$url);
        curl_setopt($my_ch, CURLOPT_HEADER,         true);
        curl_setopt($my_ch, CURLOPT_NOBODY,         true);
        curl_setopt($my_ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($my_ch, CURLOPT_TIMEOUT,        10);
        $r = curl_exec($my_ch);
         foreach(explode("\n", $r) as $header) {
            if(strpos($header, 'Content-Length:') === 0) {
                return trim(substr($header,16)); 
            }
         }
        return '';
    }

    function get_description($url) {
        $fullpage = curlGet($url);
        $dom = new DOMDocument();
        @$dom->loadHTML($fullpage);
        $xpath = new DOMXPath($dom); 
        $tags = $xpath->query('//div[@class="info-description-body"]');
        foreach ($tags as $tag) {
            $my_description .= (trim($tag->nodeValue));
        }	

        return utf8_decode($my_description);
    }
    
    function clean($string) {
       $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
       return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
    }

    function formatBytes($bytes, $precision = 2) { 
        $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 
        $bytes = max($bytes, 0); 
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
        $pow = min($pow, count($units) - 1); 
        $bytes /= pow(1024, $pow);
        return round($bytes, $precision) . ' ' . $units[$pow]; 
    } 
    function is_chrome(){
        $agent=$_SERVER['HTTP_USER_AGENT'];
        if( preg_match("/like\sGecko\)\sChrome\//", $agent) ){	// if user agent is google chrome
            if(!strstr($agent, 'Iron')) // but not Iron
                return true;
        }
        return false;	// if isn't chrome return false
    }
}