<?php
class afeblogs
{
	private $db;
	function __construct() 
	{
		$this->db = $GLOBALS['wpdb'];
	}

	function perfecttense_afe($pt_api_key,$pt_app_key,$text)
	{		
		if(!empty($pt_api_key) && !empty($pt_app_key) && !empty($text))
		{
			$ch = curl_init();

			curl_setopt($ch, CURLOPT_URL, 'https://api.perfecttense.com/correct');
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"text\": \"'".$text."'\", \"responseType\": [\"corrected\", \"grammarScore\", \"rulesApplied\", \"offset\", \"summary\"]}");

			$headers = array();
			$headers[] = 'Authorization: '.$pt_api_key;
			$headers[] = 'Appauthorization: '.$pt_app_key;
			$headers[] = 'Content-Type: application/json';
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
			$result11 = curl_exec($ch);
			curl_close($ch);
			if(!empty($result11))
			{
				$pt_response = json_decode($result11);	
				if(!empty($pt_response->offset[0]->correctedSentence))
				{						
					$api_response = $pt_response->offset[0]->correctedSentence;			
				}
				else
				{
					$api_response = "";
				}
			}			
			else
			{
				$api_response = "";
			}
			
			//echo"<pre>";
			//print_r($ss->offset[0]->originalSentence);
			//print_r($ss->offset[0]->correctedSentence);
			//echo"</pre>";
			
		}	
		else
		{
			$api_response = "";
		}
		return $api_response;
	}		
	
	function spin_rewriter_afe($email_address,$api_key,$text)
	{
		require_once("SpinRewriterAPI.php");
		//require_once plugin_dir_path( __FILE__ ) .'/SpinRewriterAPI.php';
		
		if(!empty($email_address) && !empty($api_key) && !empty($text))
		{
			$spinrewriter_api = new SpinRewriterAPI($email_address, $api_key);
			
			// Make the actual API request and save the response as a native PHP array.
			$api_response1 = $spinrewriter_api->getTextWithSpintax($text);
			$res = $api_response1['response'];
			$prot = $api_response1['protected_terms'];
			$confi_level = $api_response1['confidence_level'];

			$spinrewriter_api->setProtectedTerms($prot);

			// (optional) Set whether the One-Click Rewrite process automatically protects Capitalized Words outside the article's title.
			$spinrewriter_api->setAutoProtectedTerms(false);
			// (optional) Set the confidence level of the One-Click Rewrite process.
			$spinrewriter_api->setConfidenceLevel($confi_level);
			// (optional) Set whether the One-Click Rewrite process uses nested spinning syntax (multi-level spinning) or not.
			$spinrewriter_api->setNestedSpintax(true);
			// (optional) Set whether Spin Rewriter rewrites complete sentences on its own.
			$spinrewriter_api->setAutoSentences(false);
			// (optional) Set whether Spin Rewriter rewrites entire paragraphs on its own.
			$spinrewriter_api->setAutoParagraphs(false);
			// (optional) Set whether Spin Rewriter writes additional paragraphs on its own.
			$spinrewriter_api->setAutoNewParagraphs(false);
			// (optional) Set whether Spin Rewriter changes the entire structure of phrases and sentences.
			$spinrewriter_api->setAutoSentenceTrees(false);
			// (optional) Sets whether Spin Rewriter should only use synonyms (where available) when generating spun text.
			$spinrewriter_api->setUseOnlySynonyms(false);
			// (optional) Sets whether Spin Rewriter should intelligently randomize the order of paragraphs and lists when generating spun text.
			$spinrewriter_api->setReorderParagraphs(false);
			// (optional) Sets whether Spin Rewriter should automatically enrich generated articles with headings, bullet points, etc.
			$spinrewriter_api->setAddHTMLMarkup(false);
			// (optional) Sets whether Spin Rewriter should automatically convert line-breaks to HTML tags.
			$spinrewriter_api->setUseHTMLLinebreaks(false);

			$api_response = $spinrewriter_api->getUniqueVariation($res);
		}	
		else
		{
			$api_response = "";
		}
		return $api_response;
	}		
    function spin_article($article_content)
    {
        $api_url = "https://wprankbuddy.com/turbospinners/api.php";
        $api_key = "11cf79d44024d6608eca69d6e381bcf4";
        $article = $article_content;
        $lang = "en";
        
        //$cookie=tempnam("/tmp","CURLCOOKIE");
        $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7";
        $ch = curl_init();
        
        $data = "api_key=$api_key&article=$article&lang=$lang";
        curl_setopt($ch, CURLOPT_URL,"$api_url");    
    	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    	curl_setopt($ch,CURLOPT_ENCODING,"gzip,deflate");
    	curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded","Accept: */*"));
    	//curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_setopt($ch, CURLOPT_REFERER, "http://prothemes.biz");
    	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS,"$data");
    	$html=curl_exec($ch);
		return $html;
        
    }
	function afe_article_category($cat_id)
	{
		$api_key='12345678';
		$category='';

		if(!empty($api_key)){
            $url='https://mycontentcreatorpro.com/api/v1/get_categories.php?apikey='.$api_key;            
			$ch = curl_init($url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			$data = curl_exec($ch);
			curl_close($ch);
            $categories = json_decode($data);
            $category = $categories->results;						
		}
		
		return $category;
	}

	function afe_search_result($afe_subcat,$afe_key)
	{
		global $wpdb;
		$afe_output = array();
		$afe_cat = $afe_subcat;
        $afe_key = str_replace( ' ', '+', $afe_key );
        
		$afe_api_key='12345678';
		if(!empty($afe_api_key))
		{
            $afe_url = 'https://mycontentcreatorpro.com/api/v1/get_articles.php?apikey='.$afe_api_key.'&keyword='.trim($afe_key).'&category='.$afe_cat;                
  
    		$ch = curl_init($afe_url);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    		$afe_data = curl_exec($ch);
    		 
    		curl_close($ch);
            $afe_respss=json_decode($afe_data);
            $afe_resp=$afe_respss->results;
	        foreach((array)$afe_resp as $afe_resp11)
	        {
				$afe_output[]= $afe_resp11;					  
	        }
			//echo "<pre>";
			//print_r($afe_output);
			//echo "</pre>";
            
            if(is_array($afe_output))
            {
    		    $afe_tmp_table = $wpdb->base_prefix . "ch_tmp_table";
        	    $wpdb->query('DELETE  FROM '.$afe_tmp_table.' WHERE type = "article"');
	            foreach($afe_output as $afe_resp1)
        		{
					$text = str_replace('"', "'", $afe_resp1->title);
                    $afe_result_detail_camp_ac = $wpdb->get_results('SELECT * FROM '.$wpdb->base_prefix.'ch_tmp_table WHERE title = "'.trim($text).'"');
                    if (count($afe_result_detail_camp_ac) == 0)
                    {                    
	                    if(!empty($afe_resp1->summary) || !empty($afe_resp1->content))
	                    {
	                       if(!empty($afe_resp1->summary))
	                       {
	                            $summary = wp_trim_words($afe_resp1->summary, 50, '...' );
	                       }
	                       else
	                       {
	                            $summary = wp_trim_words($afe_resp1->content, 50, '...' );                    
	                       } 

	                  	   $afe_tmp_data=array(
	                                            'catg_id' => $afe_resp1->category_id, 
	                                            'title' => trim($text), 
	                                            'link' => '', 
	                                            'short_des' => $summary, 
	                                            'long_desc' => $afe_resp1->content, 
	                                            'type' => 'article' 
	                                           );


	                        $wpdb->insert( $afe_tmp_table, $afe_tmp_data);
	                    }
	                }
	            }
	        }
		}
		return $afe_resp;
	}
	
	function afe_youtube_search_keyword($afe_vid_keyword)
	{
		global $wpdb;
		
		$video_key = get_option('wpch_ytube_api_key');
		
		if(!empty($video_key))
		{
			$googleApiUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $afe_vid_keyword . '&key=' . $video_key . '&maxResults=50';
			
			$ch = curl_init();

			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_VERBOSE, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			$response = curl_exec($ch);

			curl_close($ch);
			$data = json_decode($response);
			$searchResponse = json_decode(json_encode($data), true);
			
			$page_token = $searchResponse['nextPageToken'];
			
			foreach ($searchResponse['items'] as $afe_key_resp) 
			{
				if ($afe_key_resp['id']['kind'] == "youtube#video") 
				{
					$afe_name = stripslashes(str_replace('"','`',$afe_key_resp['snippet']['title']));
					
					$afe_vid_id = $afe_key_resp['id']['videoId'];
					
					$afe_vid_desc = stripslashes(str_replace('"','`',$afe_key_resp['snippet']['description']));
					
					$afe_thum_img_vid_url = $afe_key_resp['snippet']['thumbnails']['default']['url'];
					$afe_link_array=array('link'=>$afe_vid_id,'thum_img_url'=>$afe_thum_img_vid_url);
					if($afe_vid_keyword)
					{
						$result_output[]=array('title'=>$afe_name,'link'=>$afe_link_array,'short_desc'=>$afe_vid_desc);
					}
				}
			}

			$googleApiUrl_nextpage = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $afe_vid_keyword . '&maxResults=50&key=' . $video_key . '&pageToken=' . $page_token;
			
			$ch_nextpage = curl_init();

			curl_setopt($ch_nextpage, CURLOPT_HEADER, 0);
			curl_setopt($ch_nextpage, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch_nextpage, CURLOPT_URL, $googleApiUrl_nextpage);
			curl_setopt($ch_nextpage, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch_nextpage, CURLOPT_VERBOSE, 0);
			curl_setopt($ch_nextpage, CURLOPT_SSL_VERIFYPEER, false);
			$response_nextpage = curl_exec($ch_nextpage);

			curl_close($ch_nextpage);
			$data_nextpage = json_decode($response_nextpage);
			$searchResponse_nextpage = json_decode(json_encode($data_nextpage), true);
			
			foreach ($searchResponse_nextpage['items'] as $afe_key_resp_nextpage) 
			{
				if ($afe_key_resp_nextpage['id']['kind'] == "youtube#video") 
				{
					$afe_name_nextpage = stripslashes(str_replace('"','`',$afe_key_resp_nextpage['snippet']['title']));
					
					$afe_vid_id_nextpage = $afe_key_resp_nextpage['id']['videoId'];
					
					$afe_vid_desc_nextpage = stripslashes(str_replace('"','`',$afe_key_resp_nextpage['snippet']['description']));
					
					$afe_thum_img_vid_url_nextpage = $afe_key_resp_nextpage['snippet']['thumbnails']['default']['url'];
					$afe_link_array_nextpage=array('link'=>$afe_vid_id_nextpage,'thum_img_url'=>$afe_thum_img_vid_url_nextpage);
					if($afe_vid_keyword)
					{
						$result_output[]=array('title'=>$afe_name_nextpage,'link'=>$afe_link_array_nextpage,'short_desc'=>$afe_vid_desc_nextpage);
					}
				}
			}
			
			if(is_array($result_output))
			{
				$afe_video_tmp_table = $wpdb->base_prefix . "ch_tmp_table";
				$wpdb->query('DELETE  FROM '.$afe_video_tmp_table.' WHERE type = "video"');
				foreach($result_output as $result_output1)
				{
					$afe_link_data=serialize($result_output1["link"]);
					$afe_video_tmp_data=array(
										'catg_id' => '', 
										'title' => $result_output1["title"], 
										'link' => $afe_link_data, 
										'short_des' => $result_output1["short_desc"], 
										'long_desc' => '', 
										'type' => 'video' 
									   );


					$wpdb->insert( $afe_video_tmp_table, $afe_video_tmp_data);
				}
			}
		}
		else
		{
			$afe_key_resp = '';
		}
        return $afe_key_resp;
	}
	
	function afe_youtube_search_keyword1($afe_vid_keyword1)
	{
		global $wpdb;
		
		$video_key1 = get_option('wpch_ytube_api_key');
		
		if(!empty($video_key))
		{
			$googleApiUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $afe_vid_keyword1 . '&maxResults=50&key=' . $video_key1;

			$ch = curl_init();

			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_VERBOSE, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			$response = curl_exec($ch);

			curl_close($ch);
			$data = json_decode($response);
			$searchResponse1 = json_decode(json_encode($data), true);
			
			$page_token = $searchResponse1['nextPageToken'];
			
			foreach ($searchResponse1['items'] as $afe_key_resp1) 
			{
				if ($afe_key_resp1['id']['kind'] == "youtube#video") 
				{
					$afe_name1 = stripslashes(str_replace('"','`',$afe_key_resp1['snippet']['title']));
					
					$afe_vid_id1 = $afe_key_resp1['id']['videoId'];
					
					$afe_vid_desc1 = stripslashes(str_replace('"','`',$afe_key_resp1['snippet']['description']));
					
					$afe_thum_img_vid_url1 = $afe_key_resp1['snippet']['thumbnails']['default']['url'];
					$afe_link_array1=array('link'=>$afe_vid_id1,'thum_img_url'=>$afe_thum_img_vid_url1);
					if($afe_vid_keyword1)
					{
						$result_output1[]=array('link'=>$afe_link_array1);
					}	
				}
			}

			
			$googleApiUrl1_nextpage = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' . $afe_vid_keyword1 . '&maxResults=50&key=' . $video_key1 . '&pageToken=' . $page_token;

			$ch1_nextpage = curl_init();

			curl_setopt($ch1_nextpage, CURLOPT_HEADER, 0);
			curl_setopt($ch1_nextpage, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch1_nextpage, CURLOPT_URL, $googleApiUrl1_nextpage);
			curl_setopt($ch1_nextpage, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch1_nextpage, CURLOPT_VERBOSE, 0);
			curl_setopt($ch1_nextpage, CURLOPT_SSL_VERIFYPEER, false);
			$response1_nextpage = curl_exec($ch1_nextpage);

			curl_close($ch1_nextpage);
			$data1_nextpage = json_decode($response1_nextpage);
			$searchResponse1_nextpage = json_decode(json_encode($data1_nextpage), true);
			
			foreach ($searchResponse1_nextpage['items'] as $afe_key_resp1_nextpage) 
			{
				if ($afe_key_resp1_nextpage['id']['kind'] == "youtube#video") 
				{
					$afe_name1_nextpage = stripslashes(str_replace('"','`',$afe_key_resp1_nextpage['snippet']['title']));
					
					$afe_vid_id1_nextpage = $afe_key_resp1_nextpage['id']['videoId'];
					
					$afe_vid_desc1_nextpage = stripslashes(str_replace('"','`',$afe_key_resp1_nextpage['snippet']['description']));
					
					$afe_thum_img_vid_url1_nextpage = $afe_key_resp1_nextpage['snippet']['thumbnails']['default']['url'];
					$afe_link_array1_nextpage=array('link'=>$afe_vid_id1_nextpage,'thum_img_url'=>$afe_thum_img_vid_url1_nextpage);
					if($afe_vid_keyword1)
					{
						if(count($result_output1) < 1)
						{
							$result_output1[]=array('link'=>$afe_link_array1_nextpage);
						}
					}	
				}
			}
			
			foreach($result_output1 as $result_output11)
			{
				$option_name = 'wpch_vid_tmp_camp';
				$afe_link_data1= $result_output11["link"];
				$campaign_post_vid = $afe_link_data1['link'];
				if ( get_option( $option_name ) !== false ) 
				{
					update_option( $option_name, $campaign_post_vid );
				 
				} 
				else 
				{
					add_option( $option_name, $campaign_post_vid );
				}
			}
		}
		else
		{
			$afe_key_resp1 = '';
		}
		return $afe_key_resp1;
	}
}


?>