<?php
/*
	File = class.function.php
	Date = 22-2-2018
*/

class AI_Core
{
	// define variable
	public $def_set = array();
	public $def_lang = array();
	
	public function __construct()
	{
		
	}
	
	public function aiGetValue($tbl, $fld, $whr, $val)
	{
		if($tbl == '' && $fld == '' && $whr == '' && $val == '')
			return false;
		
		global $ai_db;
		
		$qry = "SELECT $fld FROM $tbl WHERE $whr='".$val."'";
		$row = $ai_db->aiGetQuery($qry);
		
		return $row[0][$fld];
	}
	public function aiGetCount( $tbl = '', $fld = '', $whr = '1=1' )
	{
		if($tbl=='' && $fld=='')
			return '0';
		
		global $ai_db;
		
		$qry = "SELECT COUNT(".$fld.") AS cnt_fld FROM ".$tbl." WHERE ".$whr;
		$row = $ai_db->bcGetQuery($qry);
		
		return $row[0]['cnt_fld'];
	}
	public function aiIncrementCounter( $tbl, $fld, $whr, $val, $counter = 1 )
	{
		if($tbl == '' && $fld == '' && $whr == '' && $val == '')
			return false;
		
		global $ai_db;
		
		$qry = "UPDATE $tbl SET $fld=$fld+$counter WHERE $whr='".$val."'";
		$row = $ai_db->bcQuery($qry);
		
		return $row;
	}	
	public function aiGet404()
	{
		
	}
	public function aiGetString( $val = '' )
	{
		if($val=='')
			return '';
		
		$val = stripslashes($val);
		return $val;
	}
	
	public function aiGetError( $msg = '' )
	{
		echo '<pre class="error_msg">'.$msg.'</pre>'; exit;
	}
	
	public function aiGoPage( $url = '' )
	{
		echo '<script>window.location="'.$url.'";</script>'; exit;
	}
	
	public function aiGetDate( $dt = '', $fmt = 'm/d/Y' )
	{
		if($dt == '')
			return '';
		
		return date($fmt, strtotime($dt));
	}
	
	public function aiGetDefault( $tbl = '' )
	{
		if($tbl == '')
			return false;
		
		global $ai_db;
		
		$def_arr = $ai_db->aiGetTable( $tbl );
		foreach( $def_arr as $def_data)
		{
			$this->def_set[$def_data['slug']] = $def_data['value'];
		}
		
		
	}
	
	public function aiUpload( $img_path, $fld = '', $ftype = 'image', $old_img = '' )
	{
		if($img_path['error']!=0)
			return $old_img;
		$t = time();
		$file_nm = $t.'_'.$img_path['name'];
		$fnl_file_nm = str_replace(" ", "_", $file_nm);
		move_uploaded_file($img_path['tmp_name'], $fld.$fnl_file_nm);
		
		if($old_img != '')
			unlink($fld.$old_img);
		
		return $fnl_file_nm;
	}
	
	
	
	public function aiDelete( $old_img = '' )
	{
		if($old_img != '')
			unlink($old_img);
	}
	
	public function aiGetAgo( $tp_date = '' )
	{
		if($tp_date=='')
			return '';
		
		$curr_time = strtotime(date('d-m-Y H:i:s'));
		$tp_time = strtotime($tp_date);
		$time_diff = $curr_time - $tp_time;
		
		if($time_diff < 0)
			return '';
		
		$tp_time_date = $this->bcTimeToDate( $time_diff );
		
		if( $tp_time_date['day'] > 0 )
			$tp_msg = $tp_time_date['day'].' days ago';
		elseif( $tp_time_date['hour'] > 0 )
			$tp_msg = $tp_time_date['hour'].' hours ago';
		elseif( $tp_time_date['minute'] > 0 )
			$tp_msg = $tp_time_date['minute'].' minutes ago';
		else
			$tp_msg = $tp_time_date['second'].' seconds ago';
			
		return $tp_msg;
	}
	
	public function aiTimeToDate( $tp_time = '' )
	{
		if($tp_time=='')
			return '';
		
		$dt_arr = array();
		
		$d = floor($tp_time/86400);
		$dt_arr['day'] = ($d < 10 ? '0' : '').$d;
		
		$h = floor(($tp_time-$d*86400)/3600);
		$dt_arr['hour'] = ($h < 10 ? '0' : '').$h;
		
		$m = floor(($tp_time-($d*86400+$h*3600))/60);
		$dt_arr['minute'] = ($m < 10 ? '0' : '').$m;
		
		$s = $tp_time-($d*86400+$h*3600+$m*60);
		$dt_arr['second'] = ($s < 10 ? '0' : '').$s;
		
		return $dt_arr;
	}
	
	
	
	public function aiCheckLogin()
	{
		if(!(isset($_SESSION['id']) && $_SESSION['id']!=''))
		$this->bcGoPage("index.php");
	}
	
	
	
	
	public function aiUpdateSetting( $slug = '', $val = '' )
	{
		if($slug == '')
			return false;
		
		global $ai_db;
		
		$qry = "UPDATE ".DB_PREFIX."setting SET meta_value='".$val."' WHERE meta_key='".$slug."'";
		$res = $ai_db->bcQuery($qry);
		
		return true;
	}
	
	
	
	public function aiSlug( $name = '' )
	{
		if($name == '')
			return '';
		
		$spec_char = array(' ','--','&','\\','\'','"');
		
		$name = strtolower($name);
		$name = str_replace($spec_char,'-',$name);
		
		return $name;
	}
	
	public function aiGetLang( $lang = 'en' )
	{
		if($lang=='')
			return 'en';
		
		foreach( $this->lang_arr as $key => $val)
		{
			if($key==$lang)
				return $lang;
		}
		
		return 'en';
	}
	
	
	
	public function aiGetPassword()
	{
		$seed = str_split('abcdefghijklmnopqrstuvwxyz'
						 .'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
						 .'0123456789'); // and any other characters
		shuffle($seed); // probably optional since array_is randomized; this may be redundant
		$rand = '';
		foreach (array_rand($seed, 6) as $k) $rand .= $seed[$k];
		
		return $rand;
	}
	
	
	public function aiSendNotification( $registatoin_ids, $message )
	{
		// Set POST variables
        $url = 'https://android.googleapis.com/gcm/send';

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

        $headers = array(
            'Authorization: key='.GOOGLE_API_KEY,
            'Content-Type: application/json'
        );
        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }

        // Close connection
        curl_close($ch);
        echo $result;
	}
	
	public function aiSendHtmlEmail($to, $sub, $body)
	{
		
		if($_SERVER['HTTP_HOST']=='localhost')
		{
			return '';
		}
		else
		{
			require 'includes/PHPMailerAutoload.php';

			$mail = new PHPMailer(); 
	        $mail->From = FROM_EMAIL; 
	        $mail->FromName = EMAIL_FNAME; 
	        $mail->Host = HOST_NAME;
	        $mail->SMTPDebug = 0; 
	        $mail->SMTPAuth = true;
	        $mail->Port = PORT;
	        $mail->AddAddress($to); 
	        $mail->Subject = $sub; 
	        $mail->Body = $body;
	        $mail->Username = EMAIL_USER;
	        $mail->Password = EMAIL_PASS;
	      
			
			if (!$mail->send())
			{
				return "fail";
			} else {
				return "success";
			}
		}
	
	}
	
	
	
	public function aiTranslate($text, $from_lan = 'en', $to_lan = 'en')
	{
		$from_lan = 'en';
		if(isset($_SESSION['curr_lang']) && $_SESSION['curr_lang']!='')
			$to_lan = $_SESSION['curr_lang'];
		else
			$to_lan = 'en';
		
		if($to_lan=='en')
		{
			$translated_text = $text;
		}
		else
		{
			$json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
			//print_r($json);
			$translated_text = $json->responseData->translatedText;
		}
		return $translated_text;
	}
	
	public function getTranslation($txt)
	{
		if($this->def_lang[$txt]!='')
		{
			return $this->def_lang[$txt];
		}
		else
		{
			return $txt;
		}
	}
	public function aiUpdateGCM( $tbl, $field, $val, $whr)
	{
		global $ai_db;
		
		$qry2 = "UPDATE ".$tbl." SET ".$field."='".$val."' WHERE ".$whr;
		$res2 = $ai_db->aiQuery($qry2);
	}

	public function aiGeneratePIN($digits = 4){
	    $i = 0; 
	    $pin = ""; 
	    while($i < $digits){
	        
	        $pin .= mt_rand(0, 9);
	        $i++;
	    }
    return $pin;
	}
	

	function sendPushNotification($tokens,$payload,$title)
    {
     //$tokens = $arr;
            //print_r($payload);die;
            $url = 'https://fcm.googleapis.com/fcm/send';
            $priority="high";
            $notification=array_merge(array('title' => $title,'body' => $payload['message']),$payload);
            //print_r($notification);die;
            $fields = array(
                 'registration_ids' => $tokens, 
                 'notification' => $notification,
                 'data' => $notification,
                 'content_available'=> true
                );

            $headers = array(
                'Authorization:key='.PUSH_NOTIFICATION.'',
                'Content-Type: application/json'
                );

           $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $url);
           curl_setopt($ch, CURLOPT_POST, true);
           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
           curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);  
           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
           curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            // echo json_encode($fields);
           $result = curl_exec($ch);
           //print_r($result);die;           
           echo curl_error($ch);
           if ($result === FALSE) {
               die('Curl failed: ' . curl_error($ch));
           }
           curl_close($ch);
           return $result;
	}

	public function checkAPISecurity($SECRET) {  
	      
        
    }

	public function aiGetPaging($qry, $paged=1, $per_page = 10, $url = '')
	{
		global $ai_db;
		$paged = ($paged!='')?$paged:1;
		if(strpos($url,'?')===false)
			$url = $url.'?';
		else
			$url = $url.'&';
		
		$qry3 = $qry.' LIMIT '.($per_page*($paged-1)).', '.$per_page;
		$res3 = $ai_db->aiGetQuery($qry3);
		
		$res2 = $ai_db->aiGetQuery($qry);
		
		$total_rec = count($res2);
		$total_page = ceil($total_rec/$per_page);
		
		$data = '<ul class="list-inline">
		<li><a href="'.$url.'paged=1">&laquo;</a></li>';
		
		for($i=1;$i<=$total_page;$i++)
		{
			$data .= '<li><a class="';
			$data .= ($paged==$i)?'active':'';
			$data .= '" href="'.$url.'paged='.$i.'">'.$i.'</a></li>';
		}
		
		$data .= '<li><a href="'.$url.'paged='.$total_page.'">&raquo;</a></li>';
		
		return array($res3, $data);
		
	}

	public function base64fileupload($target_dir,$encoded_string){
	 	
	   	$filedata = explode(',', $encoded_string); 
	   	$decoded_file = base64_decode($filedata[1]); 
	  
	   	
	    $file = uniqid() .'.'. 'jpg'; // rename file as a unique name
	    $file_dir = $target_dir . uniqid() .'.'. 'jpg';

	    try {
	        file_put_contents($file_dir, $decoded_file);
	        return $file;	       
	    } catch (Exception $e) {
	        return false;
	    }

	}

	public function Translate( $name = '' )
	{
		if($name == ''){
			return false;
		}else{
			$res = $this->aiGetValue(DB_PREFIX."language","name_hnd","slug",$name);
			if($res != "")
			{
				return $res;
			}else
			{
				return $name;
			}
		}
		
	}


	
}

$ai_core = new AI_Core();
?>