| Current Path : /home/musicpassiondjs40/djandreidis/modules/mod_otfacebookreviews/ |
| Current File : /home/musicpassiondjs40/djandreidis/modules/mod_otfacebookreviews/helper.php |
<?php
/**
* @package OT Facebook Reviews for Joomla! 3
* @version $Id: mod_otfacebookreviews.php - Aug 2017 OmegaTheme
* @author OmegaTheme Extensions (contact@omegatheme.com) - http://omegatheme.com
* @copyright Copyright(C) 2017 - OmegaTheme Extensions
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
**/
// no direct access
defined('_JEXEC') or die( 'Restricted access' );
jimport('joomla.utilities.string');
jimport( 'joomla.application.module.helper' );
abstract class ModOTFacebookReviewsHelper {
public static function getReview(&$params) {
$url = 'https://graph.facebook.com/' . $params->get('ot_facebookid') . '/ratings?access_token=' . $params->get('ot_facebooktoken').'&limit='.$params->get('ot_limit');
$api_response = otfbrv_urlopen($url);
$response_data = $api_response['data'];
$response_json = otfbrv_json_decode($response_data);
if(isset($response_json->data)) {
$reviews = $response_json->data;
}
return $reviews;
}
}
function otfbrv_json_decode($data) {
return json_decode($data);
}
function otfbrv_json_urlopen($url, $postdata=false, $headers=array()) {
if (!($response = otfbrv_urlopen($url, $postdata, $headers)) || !$response['code']) {
//$this->last_error = 'COULDNT_CONNECT';
return false;
}
return otfbrv_json_urlopen($response['data']);
}
function otfbrv_urlopen($url, $postdata=false, $headers=array()) {
$response = array(
'data' => '',
'code' => 0
);
$url = preg_replace('/\s+/', '+', $url);
if(function_exists('curl_init')) {
if (!function_exists('curl_setopt_array')) {
function curl_setopt_array(&$ch, $curl_options) {
foreach ($curl_options as $option => $value) {
if (!curl_setopt($ch, $option, $value)) {
return false;
}
}
return true;
}
}
_otfbrv_curl_urlopen($url, $postdata, $headers, $response);
} else if(ini_get('allow_url_fopen') && function_exists('stream_get_contents')) {
_otfbrv_fopen_urlopen($url, $postdata, $headers, $response);
} else {
_otfbrv_fsockopen_urlopen($url, $postdata, $headers, $response);
}
return $response;
}
/*-------------------------------- curl --------------------------------*/
function _otfbrv_curl_urlopen($url, $postdata, $headers, &$response) {
$c = curl_init($url);
$postdata_str = otfbrv_get_query_string($postdata);
$c_options = array(
CURLOPT_USERAGENT => '',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => ($postdata_str ? 1 : 0),
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array_merge(array('Expect:'), $headers),
CURLOPT_TIMEOUT => 10
);
if($postdata) {
$c_options[CURLOPT_POSTFIELDS] = $postdata_str;
}
curl_setopt_array($c, $c_options);
$open_basedir = ini_get('open_basedir');
if(empty($open_basedir) && filter_var(ini_get('safe_mode'), FILTER_VALIDATE_BOOLEAN) === false) {
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
}
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($c);
// cURL automatically handles Proxy rewrites, remove the "HTTP/1.0 200 Connection established" string
if (stripos($data, "HTTP/1.0 200 Connection established\r\n\r\n") !== false) {
$data = str_ireplace("HTTP/1.0 200 Connection established\r\n\r\n", '', $data);
}
list($resp_headers, $response['data']) = explode("\r\n\r\n", $data, 2);
$response['headers'] = _otfbrv_get_response_headers($resp_headers, $response);
$response['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
}
/*-------------------------------- fopen --------------------------------*/
function _otfbrv_fopen_urlopen($url, $postdata, $headers, &$response) {
$params = array();
if($postdata) {
$params = array('http' => array(
'method' => 'POST',
'header' => implode("\r\n", array_merge(array('Content-Type: application/x-www-form-urlencoded'), $headers)),
'content' => otfbrv_get_query_string($postdata),
'timeout' => 10
));
} else {
$params = array('http' => array(
'header' => implode("\r\n", $headers)
));
}
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if(!$fp) { return false; }
list($unused, $response['code'], $unused) = explode(' ', $http_response_header[0], 3);
$resp_headers = array_slice($http_response_header, 1);
foreach($resp_headers as $unused=>$header) {
$header = explode(':', $header);
$header[0] = trim($header[0]);
$header[1] = trim($header[1]);
$resp_headers[strtolower($header[0])] = strtolower($header[1]);
}
$response['data'] = stream_get_contents($fp);
$response['headers'] = $resp_headers;
}
/*-------------------------------- fsockpen --------------------------------*/
function _otfbrv_fsockopen_urlopen($url, $postdata, $headers, &$response) {
$buf = '';
$req = '';
$length = 0;
$postdata_str = otfbrv_get_query_string($postdata);
$url_pieces = parse_url($url);
$host = $url_pieces['host'];
if(!isset($url_pieces['port'])) {
switch($url_pieces['scheme']) {
case 'http':
$url_pieces['port'] = 80;
break;
case 'https':
$url_pieces['port'] = 443;
$host = 'ssl://' . $url_pieces['host'];
break;
}
}
if(!isset($url_pieces['path'])) { $url_pieces['path'] = '/'; }
if(($url_pieces['port'] == 80 && $url_pieces['scheme'] == 'http') ||
($url_pieces['port'] == 443 && $url_pieces['scheme'] == 'https')) {
$req_host = $url_pieces['host'];
} else {
$req_host = $url_pieces['host'] . ':' . $url_pieces['port'];
}
$fp = @fsockopen($host, $url_pieces['port'], $errno, $errstr, 10);
if(!$fp) { return false; }
$path = $url_pieces['path'];
if (isset($url_pieces['query'])) $path .= '?'.$url_pieces['query'];
$req .= ($postdata_str ? 'POST' : 'GET') . ' ' . $path . " HTTP/1.1\r\n";
$req .= 'Host: ' . $req_host . "\r\n";
$req .= otfbrv_get_http_headers_for_request($postdata_str, $headers);
if($postdata_str) {
$req .= "\r\n\r\n" . $postdata_str;
}
$req .= "\r\n\r\n";
fwrite($fp, $req);
while(!feof($fp)) {
$buf .= fgets($fp, 4096);
}
list($headers, $response['data']) = explode("\r\n\r\n", $buf, 2);
$headers = _otfbrv_get_response_headers($headers, $response);
if(isset($headers['transfer-encoding']) && 'chunked' == strtolower($headers['transfer-encoding'])) {
$chunk_data = $response['data'];
$joined_data = '';
while(true) {
list($chunk_length, $chunk_data) = explode("\r\n", $chunk_data, 2);
$chunk_length = hexdec($chunk_length);
if(!$chunk_length || !strlen($chunk_data)) { break; }
$joined_data .= substr($chunk_data, 0, $chunk_length);
$chunk_data = substr($chunk_data, $chunk_length + 1);
$length += $chunk_length;
}
$response['data'] = $joined_data;
} else {
$length = $headers['content-length'];
}
$response['headers'] = $headers;
}
function otfbrv_get_query_string($params) {
$query = '';
if($params) {
foreach($params as $key=>$value) {
$query .= urlencode($key) . '=' . urlencode($value) . '&';
}
}
return $query;
}
function _otfbrv_get_response_headers($headers, &$response) {
$headers = explode("\r\n", $headers);
list($unused, $response['code'], $unused) = explode(' ', $headers[0], 3);
$headers = array_slice($headers, 1);
foreach($headers as $unused=>$header) {
$header = explode(':', $header);
$header[0] = trim($header[0]);
$header[1] = trim($header[1]);
$headers[strtolower($header[0])] = $header[1];
}
return $headers;
}
function otfbrv_get_http_headers_for_request($content, $headers) {
$req_headers = array();
$req_headers[] = '';
$req_headers[] = 'Connection: close';
if($content) {
$req_headers[] = 'Content-Length: ' . strlen($content);
$req_headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
return implode("\r\n", array_merge($req_headers, $headers));
}
function otfbrv_url_method() {
if(function_exists('curl_init')) {
return 'curl';
} else if(ini_get('allow_url_fopen') && function_exists('stream_get_contents')) {
return 'fopen';
} else {
return 'fsockopen';
}
}
function getRating($ratings, &$params) {
$result = '';
if($params->get('ot_starimage') == 2) {
$image = 'medium';
} elseif ($params->get('ot_starimage') == 3) {
$image = 'small';
} else {
$image = 'default';
}
for ($i = 1; $i <= $ratings; $i++) { // go through each star
$result .= '<img src="'.JURI::root().'modules/mod_otfacebookreviews/assets/images/star_'.$image.'_full.png" />'; // show it filled
}
$starsLeft = 5 - $ratings;
if ($starsLeft > 0) { // if there are any more stars left
for ($i = 1; $i <= $starsLeft; $i++) { // go through each remaining star
$result .= '<img src="'.JURI::root().'modules/mod_otfacebookreviews/assets/images/star_'.$image.'_empty.png" />'; // show it empty
}
}
return $result;
}