Your IP : 216.73.216.240


Current Path : /home/j/u/v/juvelize/www/modules/mod_geekfacebookembed/
Upload File :
Current File : /home/j/u/v/juvelize/www/modules/mod_geekfacebookembed/helper.php

<?php
/**
 * @package    mod_geekfacebookembed
 * @version    1.2.0
 *
 * @copyright  Copyright (C) 2015 - 2017 JoomlaGeek. All Rights Reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @author     JoomlaGeek <admin@joomlageek.com>
 * @link       http://www.joomlageek.com
 */

defined('JPATH_BASE') or die;

class modGeekFacebookEmbedHelper
{
	protected $params = null;
	protected $endpoint = '';

	public function __construct($params) {
		$this->params = $params;
		$this->endpoint = 'https://graph.facebook.com/v2.9/';
	}

	public function getAccessToken() {
		$appid = $this->params->get('appid', '');
		$appsecret = $this->params->get('appsecret', '');
		if(!$appid || !$appsecret) {
			throw new Exception(JText::_('MOD_GEEKFACEBOOKEMBED_FACEBOOK_APP_IS_REQUIRED'));
		}
		return $appid.'|'.$appsecret;
	}

	public function request($resource) {
		$accessToken = $this->getAccessToken();
		$url = $this->endpoint . $resource;
		$url .= (strpos($url, '?') !== false ? '&' : '?') . 'access_token='.$accessToken;

		$info = $this->getContent($url);
		$info = json_decode($info);

		return $info;
	}

	public function getData() {
		$width = (int) $this->params->get('width', 500);
		$items = array();

		switch($this->params->get('source', 'page')) {
			case 'post':
				$this->addItem($items, 'fb-post', $this->params->get('post_id'));
				break;
			case 'video':
				$this->addItem($items, 'fb-video', $this->params->get('video_id'));
				break;
			case 'page':
				$limit = (int) $this->params->get('display_items', 6);
				$page_id = trim($this->params->get('page_id'));
				if(!preg_match('/^[0-9]+$/', $page_id)) {
					//get page id from name
					$info = $this->request($page_id);
					if(is_object($info) && isset($info->id)) {
						$page_id = $info->id;
					}
				}
				if($page_id) {
					$resource = $page_id."/posts?fields=id,from{id,name},message,type,link&limit=".$limit;

					$response = $this->request($resource);
					if(is_object($response) && isset($response->data)) {
						foreach($response->data as $item) {
							//item type: link, status, photo, video, offer, event
							$postid = preg_replace('/^[0-9]+_/', '', $item->id);

							if($item->type == 'event') {
								$type = 'fb-event';
								$link = sprintf('https://www.facebook.com/%s/%s/%s', $page_id, 'events', $postid);
								$link = sprintf('https://www.facebook.com/plugins/post.php?href=%s&width=%d', urlencode($link), $width);
								$height = $width * 446 / 500;
								$link = '<iframe src="'.$link.'" width="'.$width.'" height="'.$height.'" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>';
							} elseif($item->type == 'video') {
								if(isset($item->link)) {
									$type = 'fb-post';
									$link = sprintf('https://www.facebook.com/%s/%s/%s', $page_id, 'posts', $postid);
								} else {
									$type = 'fb-video';
									$link = sprintf('https://www.facebook.com/%s/%s/%s', $page_id, 'videos', $postid);
								}
							} else {
								$type = 'fb-post';
								if(isset($item->link) && $item->type != 'link') {
									$link = $item->link;
								} else {
									$link = sprintf('https://www.facebook.com/%s/%s/%s', $page_id, 'posts', $postid);
								}
							}
							$this->addItem($items, $type, $link);
						}
					}
				}
				break;
		}
		return $items;
	}

	protected function addItem(&$items, $type, $url) {
		if(preg_match('#^(https?:)?\/\/([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?#i', $url)) {
			array_push($items, array($type, $url));
		}
	}

	private function getContent(&$url) {
		if (function_exists('curl_init')) {
			$options = new JRegistry();
			$transport = new JHttpTransportCurl($options);
		} else {
			$options = new JRegistry();
			$transport = new JHttpTransportSocket($options);
		}
		$uri = new JUri($url);
		$response = $transport->request('GET', $uri);
		$content = $response->body;

		return $content;
	}
}