Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 3 files changed
  • 0 comments
  • 1 contributor
107  lib/core/SCL_AbstractSocial.php
... ...
@@ -0,0 +1,107 @@
  1
+<?php
  2
+abstract class SCL_AbstractSocial {
  3
+	const FORMAT_JSON	= 'json';
  4
+	const FORMAT_XML	= 'xml';
  5
+	const FORMAT_ATOM	= 'atom';
  6
+	const FORMAT_RSS	= 'rss';
  7
+	
  8
+	protected $aResult = array('data' => '', 'meta' => '');
  9
+	
  10
+	protected $sFormat;
  11
+	
  12
+	protected $bIsSecure;
  13
+	
  14
+	protected $sAccessToken = '';
  15
+	
  16
+	protected $sAccessTokenSecret = '';
  17
+	
  18
+	protected $sApplicationKey = '';
  19
+	
  20
+	protected $sApplicationSecret = '';
  21
+	
  22
+	protected $sCallbackUrl = '';
  23
+	
  24
+	protected $sSignatureMethod = '';
  25
+	
  26
+	protected $sOAuthVersion = '';
  27
+	
  28
+	public function getResult() {
  29
+		return $this->aResult;
  30
+	}
  31
+	
  32
+	protected function setResultData($mData) {
  33
+		$this->aResult['data'] = $mData;
  34
+	}
  35
+	
  36
+	protected function setResultMeta($mDataMeta) {
  37
+		$this->aResult['meta'] = $mDataMeta;
  38
+	}
  39
+	
  40
+	/**
  41
+	 * <p>This functions is used for authorized get requests.</p>
  42
+	 * @param array $aParams <p>Various parameters, specific to child classes. Values are presented as key-values.</p>
  43
+	 * @return array <p>Result from the server, containing response data under "data" key and meta info about the request under "meta" key of the array.</p>
  44
+	 */
  45
+	abstract public function getData($aParams = array());
  46
+	
  47
+	/**
  48
+	 * <p>This functions is used for authorized post requests.</p>
  49
+	 * @param array $aParams <p>Various parameters, specific to child classes. Values are presented as key-values.</p>
  50
+	 * @return array <p>Result from the server, containing response data under "data" key and meta info about the request under "meta" key of the array.</p>
  51
+	 */
  52
+	abstract public function postData($aParams = array());
  53
+	
  54
+	public static function convertData($sFormat, $mData) {
  55
+		if(!strlen(trim($mData))) return trim($mData);
  56
+		switch($sFormat) {
  57
+			case self::FORMAT_JSON:
  58
+				return UTL_Utils::jsonDecode($mData);
  59
+			case self::FORMAT_XML:
  60
+			case self::FORMAT_RSS:
  61
+			case self::FORMAT_ATOM:
  62
+				return UTL_Utils::decodeXML($mData);
  63
+			default:
  64
+				return $mData;
  65
+		}
  66
+	}
  67
+	
  68
+	public function setAccessToken($sAccessToken) {
  69
+		$this->sAccessToken = $sAccessToken;
  70
+	}
  71
+	
  72
+	public function getAccessToken() {
  73
+		return $this->sAccessToken;
  74
+	}
  75
+	
  76
+	public function setAccessTokenSecret($sAccessTokenSecret) {
  77
+		$this->sAccessTokenSecret = $sAccessTokenSecret;
  78
+	}
  79
+	
  80
+	public function getAccessTokenSecret() {
  81
+		return $this->sAccessTokenSecret;
  82
+	}
  83
+	
  84
+	public function setApplicationKey($sApplicationKey) {
  85
+		$this->sApplicationKey = $sApplicationKey;
  86
+	}
  87
+	
  88
+	public function getApplicationKey() {
  89
+		return $this->sApplicationKey;
  90
+	}
  91
+	
  92
+	public function setApplicationSecret($sApplicationSecret) {
  93
+		$this->sApplicationSecret = $sApplicationSecret;
  94
+	}
  95
+	
  96
+	public function getApplicationSecret() {
  97
+		return $this->sApplicationSecret;
  98
+	}
  99
+	
  100
+	public function setCallbackUrl($sCallbackUrl) {
  101
+		$this->sCallbackUrl = $sCallbackUrl;
  102
+	}
  103
+	
  104
+	public function getCallbackUrl() {
  105
+		return $this->sCallbackUrl;
  106
+	}
  107
+}
201  lib/facebook/FCB_Facebook.php
... ...
@@ -0,0 +1,201 @@
  1
+<?php
  2
+class FCB_Facebook extends SCL_AbstractSocial {
  3
+	public function __construct($sApplicationId, $sApplicationSecret, $sAccessToken = null) {
  4
+		$this->sApplicationKey = $sApplicationId;
  5
+		$this->sApplicationSecret = $sApplicationSecret;
  6
+		$this->sAccessToken	= $sAccessToken;
  7
+		$this->sFormat = self::FORMAT_JSON;
  8
+		$this->bIsSecure = true;
  9
+	}
  10
+	
  11
+	/**
  12
+	 * <p>Parameters of the POST request.</p><p>Please see <b>getDefaultQueryParams()</b> function for other default params.</p>
  13
+	 * @param array aParams <p>Parameters to request POST method to server.</p><p><b>Params are:</b></p><p><ul><li>string <b>url</b> - URL of the request.</li><li>array <b>get_fields</b> - GET request fields as key-value; Default is empty.</li><li>array <b>post_fields</b> - POST request fields as key-value; Default is empty.</li><li>array <b>header_fields</b> - request HEADER fields as key-value; Default is empty.</li><li>bool <b>is_media_upload</b> - whether it is media upload or not. Default is false.</li></ul></p>
  14
+	 * @return <p>The request result.</p>
  15
+	 */
  16
+	public function postData($aParams = array()) {
  17
+		$aParams = array_merge($this->getDefaultQueryParams(true), $aParams);
  18
+		$sUrl = $aParams['url'];
  19
+		if($aParams['requires_auth']) {
  20
+			$aParams['get_fields']['access_token'] = $this->sAccessToken;	
  21
+		}	
  22
+		
  23
+		if(count($aParams['get_fields'])) {
  24
+			$sUrl .= '?' . http_build_query($aParams['get_fields']); 
  25
+		}
  26
+		
  27
+		if($aParams['is_media_upload']) {
  28
+			$aParams['header_fields']['Content-Type'] = 'multipart/form-data';
  29
+		}else {
  30
+			$aParams['header_fields']['Content-Type'] = 'application/x-www-form-urlencoded';
  31
+		}
  32
+		
  33
+		$oCurl = new CRL_Curl($sUrl, true, $aParams['post_method_name'], $aParams['is_secure']);
  34
+		$oCurl->setPostFields($aParams['post_fields']);
  35
+		$oCurl->setHeaders($aParams['header_fields']);
  36
+		
  37
+		if($aParams['returns_raw']) {
  38
+			$this->setResultData($oCurl->getResult());
  39
+		}else {
  40
+			$this->setResultData(self::convertData($aParams['format'], $oCurl->getResult()));
  41
+		}
  42
+		
  43
+		$this->setResultMeta($oCurl->getResultMeta());
  44
+		
  45
+		return $this->getResult();
  46
+	}
  47
+	
  48
+	/**
  49
+	 * <p>Parameters of the GET request.</p><p>Please see <b>getDefaultQueryParams()</b> function for other default params.</p>
  50
+	 * @param array aParams <p>Parameters to request GET method to server.</p><p><b>Params are:</b></p><p><ul><li>string <b>url</b> - URL of the request.</li><li>array <b>get_fields</b> - GET request fields as key-value; Default is empty.</li><li>array <b>header_fields</b> - request HEADER fields as key-value; Default is empty.</li></ul></p>
  51
+	 * @return <p>The request result.</p>
  52
+	 */
  53
+	public function getData($aParams = array()) {
  54
+		$aParams = array_merge($this->getDefaultQueryParams(false), $aParams);
  55
+		
  56
+		$sUrl = $aParams['url'];
  57
+		if($aParams['requires_auth']) {
  58
+			$aParams['get_fields']['access_token'] = $this->sAccessToken;	
  59
+		}	
  60
+		
  61
+		if(count($aParams['get_fields'])) {
  62
+			$sUrl .= '?' . http_build_query($aParams['get_fields']);
  63
+		}
  64
+		 
  65
+		
  66
+		$oCurl = new CRL_Curl($sUrl);
  67
+		if($aParams['returns_raw']) {
  68
+			$this->setResultData($oCurl->getResult());
  69
+		}else {
  70
+			$this->setResultData(self::convertData($aParams['format'], $oCurl->getResult()));
  71
+		}
  72
+		
  73
+		$this->setResultMeta($oCurl->getResultMeta());
  74
+		
  75
+		return $this->getResult();
  76
+	}
  77
+	
  78
+	private function getDefaultQueryParams($bIsPost) {
  79
+		return array(
  80
+						'is_post' => $bIsPost, 				// is the request POST request.
  81
+						'post_fields' => array(), 			// key-value fields of the POST request
  82
+						'get_fields' => array(), 			// key-value fields of the GET request
  83
+						'header_fields' => array(), 		// only value fields of the request header
  84
+						'returns_raw' => false, 			// whether to convert the result (to JSON) or not.
  85
+						'url' => '', 						// URL of the request
  86
+						'post_method_name' => 'POST', 		// if the request is not POST, then another (DELETE)
  87
+						'format' => $this->sFormat, 		// format of the request/response
  88
+						'requires_auth' => true, 			// whether the request requires authentication
  89
+						'is_secure' => $this->bIsSecure, 	// is the request over HTTPS/SSL
  90
+						'is_media_upload' => false 			// is the request is uploading a media
  91
+					);
  92
+	}
  93
+	
  94
+	private function upload($sUrl, $aGetFields, $aPostFields) {
  95
+		return $this->postData(array(
  96
+										'url' => $sUrl,
  97
+										'post_fields' => $aPostFields,
  98
+										'get_fields' => $aGetFields,
  99
+										'is_media_upload' => true
  100
+									)
  101
+							);
  102
+	}
  103
+	
  104
+	/**
  105
+	 * <p>Upload photo to target object (user, album and etc.) with the specified sTargetObjectId.</p> 
  106
+	 * @param string sFilePath <p>Absolute path of the file.</p>
  107
+	 * @param string sDescription[optional] <p>Description of the photo. Default is empty.</p>
  108
+	 * @param string sTargetObjectId[optional] <p>Destination object id, e.g. to wall or album. Default is 'me'.</p>
  109
+	 * @return string <p>Uploaded photo id.</p>
  110
+	 */
  111
+	public function uploadPhoto($sFilePath, $sDescription = '', $sTargetObjectId = 'me') {
  112
+		$sUrl = FCB_UrlFactory::create(FCB_UrlFactory::GRAPH_BASE) . '/' . $sTargetObjectId . '/photos';
  113
+		return $this->upload($sUrl,
  114
+							 array(),
  115
+							 array('source' => '@' . $sFilePath, 'message' => $sDescription)
  116
+							);
  117
+	}
  118
+	
  119
+	/**
  120
+	 * <p>Upload video to target object (user, wall and etc.) with the specified sTargetObjectId.</p> 
  121
+	 * @param string sFilePath <p>Absolute path of the file.</p>
  122
+	 * @param string sTitle <p>Title of the video.</p>
  123
+	 * @param string sDescription[optional] <p>Description of the video. Default is empty.</p>
  124
+	 * @param string sTargetObjectId[optional] <p>Destination object id, e.g. to wall or page. Default is 'me'.</p>
  125
+	 * @return string <p>Uploaded video id.</p>
  126
+	 */
  127
+	public function uploadVideo($sFilePath, $sTitle, $sDescription = '', $sTargetObjectId = 'me') {
  128
+		$sUrl = FCB_UrlFactory::create(FCB_UrlFactory::VIDEO) . '/' . $sTargetObjectId . '/videos';
  129
+		return $this->upload($sUrl,
  130
+							 array('title' => $sTitle, 'description' => $sDescription),
  131
+							 array('file' => '@' . $sFilePath)
  132
+							);
  133
+	}
  134
+	
  135
+	/**
  136
+	 * <p>Batch upload video of files.</p><p><b>Usage notes:</b></p>
  137
+	 * <p>An item of the aFilesOptions param is of an array type.</p><p>The array key-values are:</p><p><ul><li>string <b>file_path</b> - absolute path of the file.</li><li>string <b>relative_url</b> - relative url of the destination; for example, 'me' or album id '1848904015996522'.</li><li>array <b>body</b> - additional parameters for the file as key-value; for example, 'message' or 'title'.</li><li>string <b>field_name</b> - name of the field as if you would upload through HTML file input element; for example, 'file1'.</li></ul></p>
  138
+	 * @param array aFilesOptions <p>Parameters of each file to be uploaded.</p>
  139
+	 * @return array <p>Info about each uploaded file.</p>
  140
+	 */
  141
+	public function uploadFiles($aFilesOptions) {
  142
+		$aBatch = array();
  143
+		$aPostFields = array();
  144
+		foreach($aFilesOptions AS $aV) {
  145
+			$aBatch[] = array(
  146
+							'method'			=> 'POST',
  147
+							'relative_url'		=> UTL_Utils::getParam($aV, 'relative_url'),
  148
+							'body'				=> http_build_query(UTL_Utils::getParam($aV, 'body', array())),
  149
+							'attached_files'	=> UTL_Utils::getParam($aV, 'field_name')
  150
+						);
  151
+			$aPostFields[$aV['field_name']] = '@' . UTL_Utils::getParam($aV, 'file_path');
  152
+		}
  153
+		
  154
+		return $this->upload(FCB_UrlFactory::create(FCB_UrlFactory::GRAPH_BASE),
  155
+							 array('batch' => UTL_Utils::jsonEncode($aBatch), 'method' => 'post'),
  156
+							 $aPostFields
  157
+							 );
  158
+	}
  159
+	
  160
+	/**
  161
+	 * <p>Request access token from the server by specifing obtained code.</p>
  162
+	 * @param string sCode <p>Valid code obtained from server.</p>
  163
+	 */
  164
+	public function requestAccessToken($sCode) {
  165
+		$sUrl = FCB_UrlFactory::create(FCB_UrlFactory::GRAPH_BASE). '/oauth/access_token'; 
  166
+		$aParams = array(
  167
+						'url' => $sUrl,
  168
+						'get_fields' => array(
  169
+												'client_id' 	=> $this->sApplicationKey,
  170
+								  				'redirect_uri' 	=> $this->sCallbackUrl,
  171
+								  				'client_secret'	=> $this->sApplicationSecret,
  172
+								  				'code'			=> $sCode	
  173
+											),
  174
+						'requires_auth' => false,
  175
+						'returns_raw' => true 
  176
+					);
  177
+					
  178
+		$aResult = $this->getData($aParams);
  179
+		$aQueryParams = array();
  180
+		if(count(UTL_Utils::getParam($aResult['data'], 'error', array()))) {
  181
+			return $aResult['data'];
  182
+		}
  183
+		
  184
+		parse_str($aResult['data'], $aQueryParams);
  185
+		return $aQueryParams;
  186
+	}
  187
+	
  188
+	/**
  189
+	 * <p>Utility function to get request code url to get access code.</p>
  190
+	 * @param array $aOptions <p>Various options to pass as GET parameters as key-value. For example, 'scope' parameter.</p>
  191
+	 */
  192
+	public function buildRequestCodeUrl($aOptions = array()) {
  193
+		$sUrl = FCB_UrlFactory::create(FCB_UrlFactory::OAUTH);
  194
+		$aOptions['client_id'] = $this->sApplicationKey;
  195
+		$aOptions['redirect_uri'] = $this->sCallbackUrl;
  196
+		$sUrl .= '?' . http_build_query($aOptions);
  197
+		
  198
+		return $sUrl;
  199
+	}
  200
+	
  201
+}
11  lib/facebook/FCB_UrlFactory.php
... ...
@@ -0,0 +1,11 @@
  1
+<?php
  2
+class FCB_UrlFactory {
  3
+	const GRAPH_BASE	= 'graph.facebook.com';
  4
+	const OAUTH			= 'www.facebook.com/dialog/oauth';
  5
+	const VIDEO			= 'graph-video.facebook.com';
  6
+	
  7
+	public static function create($sRequestUrlBase, $bIsSecure = true) {
  8
+		$sProtocol = $bIsSecure ? 'https://' : 'http://';
  9
+		return $sProtocol . $sRequestUrlBase;
  10
+	}
  11
+}

No commit comments for this range

Something went wrong with that request. Please try again.