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


if ( ! function_exists('curl_data')) {
	function curl_data($opt, $cookie_file ='') {

		if ( ! element('url', $opt)) return; 

		if ($cookie_file) {
			$fake_cookie_file = FCPATH . "uploads/" . $cookie_file;
		}

		$headers   = array();

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, element('url', $opt));
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		//쿠키설정
		if (isset($opt['cookie'])) {
			$headers[] = 'Cookie: ' . $opt['cookie'];
			curl_setopt($ch, CURLOPT_COOKIE, $opt['cookie']);
		}
		if ($cookie_file) {
			curl_setopt($ch, CURLOPT_COOKIEJAR, $fake_cookie_file);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $fake_cookie_file);
		}
		curl_setopt($ch, CURLOPT_COOKIESESSION, true);

		if(element('header', $opt)) {
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if(element('httpheader', $opt)) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, element('httpheader', $opt));
		}
		if(element('post', $opt)) {
			curl_setopt($ch, CURLOPT_POST, true);
		}
		if(element('postfields', $opt) && is_array($opt['postfields'])) {
			if (element('no_http_build_query', $opt)) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, $opt['postfields']);
			} else {
				curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($opt['postfields']));
			}
		}
		if(element('postfields', $opt) && is_string($opt['postfields'])) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $opt['postfields']);
		}
		if(element('followlocation', $opt)) {
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION,	TRUE);
		}
		if(element('referer', $opt)) {
			curl_setopt($ch, CURLOPT_REFERER, element('referer', $opt));
		}
		if(element('nobody', $opt)) {
			curl_setopt($ch, CURLOPT_NOBODY, true);
		}
		if(element('patch', $opt)) {
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
		}

		if(element('useragent', $opt)) {
			curl_setopt($ch, CURLOPT_USERAGENT, element('useragent', $opt));
		}

		if(element('ret_string', $opt)) {
			$output = (string) curl_exec($ch);
		} else {
			$output = curl_exec($ch);
		}

		curl_close($ch);

		return $output;
	}
}



if ( ! function_exists('get_file_txt')) {
    function get_file_txt($filename) {
		$fp = fopen($filename, "r") or die("파일열기에 실패하였습니다");
		$buffer = fread($fp, filesize($filename));
		fclose($fp);
		return $buffer;
	}
}

if ( ! function_exists('isJson')) {
	function isJson($string) {
		return ((is_string($string) &&
				(is_object(json_decode($string)) ||
				is_array(json_decode($string))))) ? true : false;
	}
}

if ( ! function_exists('is_url_exist')) {
	function is_url_exist($url){
		$ch = curl_init($url);    
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_exec($ch);
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

		if($code == 200){
		   $status = true;
		}else{
		  $status = false;
		}
		curl_close($ch);
	   return $status;
	}
}
