<?php
require '../../../wp-blog-header.php';

set_time_limit(200);
function return_300($seconds) {
	return 300;
}

error_reporting(0);

add_filter('wp_feed_cache_transient_lifetime', 'return_300');

if (isset($_GET["campaign"])) {
	$campaign = $_GET["campaign"];
} else {
	$campaign = smSelectCampaign();
}

smGetPageData($campaign);
echo "DONE";

function smGetPageData($campaign) {
	$keyword1 = get_post_meta(111111113, $campaign . 'smkeyword1', true);
	$keyword2 = get_post_meta(111111113, $campaign . 'smkeyword2', true);
	$keyword3 = get_post_meta(111111113, $campaign . 'smkeyword3', true);
	$keyword4 = get_post_meta(111111113, $campaign . 'smkeyword4', true);
	$keyword5 = get_post_meta(111111113, $campaign . 'smkeyword5', true);

	if (!empty($keyword1)) {
		smProcesskeyword($keyword1, $campaign);
	}

	if (!empty($keyword2)) {
		smProcesskeyword($keyword2, $campaign);
	}

	if (!empty($keyword3)) {
		smProcesskeyword($keyword3, $campaign);
	}

	if (!empty($keyword4)) {
		smProcesskeyword($keyword4, $campaign);
	}

	if (!empty($keyword5)) {
		smProcesskeyword($keyword5, $campaign);
	}
}

function smProcesskeyword($keyword, $campaign) {
	global $wpdb;

	echo "<br/>processing: " . $keyword;

	$tbl_kws_fb = $wpdb->prefix . "smpages";
	$per_page = 5000;
	$keyword = urlencode($keyword);
	$limit = $per_page;
	$offset = 0;
	$searchtype_3 = 'page';
	$query = "search";

	$params = '&q=' . $keyword . '&type=' . $searchtype_3 . '&fields=name,id,updated_time,link,fan_count,talking_about_count&limit=' . $limit . '&offset=' . $offset;
	$raw = smFacebookQuery($query, $params, $campaign);

	$result_data = $raw["data"];
	$result_data = array_unique($result_data, SORT_REGULAR);

	foreach ($result_data as $dt_3) {
		$nodeid = $dt_3['id'];
		$row = $wpdb->get_row(" SELECT * FROM $tbl_kws_fb WHERE node_id = $nodeid  ");

		if (empty($row)) {
			$wpdb->insert(
				$tbl_kws_fb,
				array(
					'keyword' => $keyword,
					'node_id' => $nodeid,
					'title' => $dt_3["name"],
					'talking_about' => $dt_3['talking_about_count'],
					'likes' => $dt_3['fan_count'],
					'supressed' => 'NO',
				),
				array(
					'%s',
					'%s',
					'%s',
					'%d',
					'%d',
				)
			);
		} else {
			$talkingabout = $dt_3['talking_about_count'];
			$likes = $dt_3['fan_count'];
			$wpdb->update(
				$tbl_kws_fb,
				array(
					'talking_about' => $talkingabout,
					'likes' => $likes,
				),
				array('node_id' => $nodeid)
			);
		}
	}
}

function smFacebookQuery($query, $params, $campaign) {
	$post_url = 'https://graph.facebook.com/' . $query;
	$accesstoken = get_post_meta(111111113, $campaign . 'smaccesstoken', true);
	$post_url = $post_url . '?access_token=' . $accesstoken . $params;

	echo $post_url;

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $post_url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	$result = curl_exec($ch);
	curl_close($ch);

	$result = json_decode($result, true);
	var_dump($result);
	return $result;
}
