/hooks/quicksocial/libraries/QuickSocial-0.1.0/quicksocial.php

https://github.com/nguyennamtien/appleseed · PHP · 302 lines · 178 code · 78 blank · 46 comment · 35 complexity · 1082f0595ae0d47f5526dc810447376e MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package QuickSocial.Library
  5. * @subpackage QuickSocial
  6. * @copyright Copyright (C) 2010 Michael Chisari. All rights reserved.
  7. * @link http://opensource.appleseedproject.org/quicksocial/
  8. * @license GNU Lesser General Public License (LGPL) version 3.0
  9. */
  10. define ( 'QUICKSOCIAL_VERSION', 'QS/0.1.1' );
  11. define ( 'QUICKSOCIAL_DOMAIN', $_SERVER['HTTP_HOST'] );
  12. /** QuickSocial Class
  13. *
  14. * Base class for QuickSocial subclasses.
  15. *
  16. * @package QuickSocial.Framework
  17. * @subpackage QuickSocial
  18. */
  19. class cQuickSocial {
  20. /**
  21. * Constructor
  22. *
  23. * @access public
  24. */
  25. public function __construct ( ) {
  26. // @todo Further sanitizing of data may be necessary.
  27. foreach ( $_GET as $g => $get ) {
  28. if ( is_array ( $get ) ) {
  29. foreach ( $get as $gg => $gget ) {
  30. $this->_GET[$g][$gg] = strip_tags ( $gget );
  31. }
  32. } else {
  33. $this->_GET[$g] = strip_tags ( $get );
  34. }
  35. }
  36. foreach ( $_POST as $p => $post ) {
  37. if ( is_array ( $post ) ) {
  38. foreach ( $post as $pp => $ppost ) {
  39. $this->_POST[$p][$pp] = strip_tags ( $ppost );
  40. }
  41. } else {
  42. $this->_POST[$p] = strip_tags ( $post );
  43. }
  44. }
  45. }
  46. /*
  47. * Send a token verification request.
  48. *
  49. */
  50. public function Verify ( $pUsername, $pTarget, $pToken ) {
  51. $fCheckRemoteToken = $this->GetCallback ( 'CheckRemoteToken' );
  52. if ( !is_callable ( $fCheckRemoteToken ) ) $this->_Error ( 'Invalid Callback: CheckRemoteToken' );
  53. $token = @call_user_func ( $fCheckRemoteToken, $pUsername, $pTarget );
  54. if ( ( !$token ) or ( $token != $pToken ) ) {
  55. $source = QUICKSOCIAL_DOMAIN;
  56. // Do a verification on this token.
  57. $data = array (
  58. '_social' => 'true',
  59. '_task' => 'verify',
  60. '_token' => $pToken,
  61. '_username' => $pUsername,
  62. '_source' => $source
  63. );
  64. $result = $this->_Communicate ( $pTarget, $data );
  65. if ( $result->success == 'true' ) {
  66. $fCreateRemoteToken = $this->GetCallback ( 'CreateRemoteToken' );
  67. if ( is_callable ( $fCreateRemoteToken ) ) {
  68. $token = @call_user_func ( $fCreateRemoteToken, $pUsername, $pTarget, $pToken );
  69. }
  70. $result->username = $pUsername;
  71. $result->domain = $pTarget;
  72. return ( $result );
  73. }
  74. } else if ( $token == $pToken ) {
  75. $return = new stdClass();
  76. $return->success = 'true';
  77. $return->error = '';
  78. return ( $return );
  79. } else {
  80. return ( false );
  81. }
  82. return ( false );
  83. }
  84. /*
  85. * Send a remote token verification request.
  86. *
  87. */
  88. public function RemoteVerify ( $pUsername, $pDomain, $pSource, $pToken ) {
  89. $source = $pSource;
  90. // Do a verification on this token.
  91. $data = array (
  92. '_social' => 'true',
  93. '_task' => 'verify.remote',
  94. '_token' => $pToken,
  95. '_username' => $pUsername,
  96. '_source' => $source
  97. );
  98. $result = $this->_Communicate ( $pDomain, $data );
  99. if ( $result->success == 'true' ) return ( true );
  100. return ( false );
  101. }
  102. /*
  103. * Reply to a verification request.
  104. *
  105. */
  106. public function ReplyToVerify ( ) {
  107. $social = $this->_GET['_social'];
  108. $task = $this->_GET['_task'];
  109. if ( $social != 'true' ) return ( false );
  110. if ( $task != 'verify' ) return ( false );
  111. $source = $this->_GET['_source'];
  112. $username = $this->_GET['_username'];
  113. $token = $this->_GET['_token'];
  114. $fCheckLocalToken = $this->GetCallback ( 'CheckLocalToken' );
  115. if ( !is_callable ( $fCheckLocalToken ) ) $this->_Error ( 'Invalid Callback: CheckLocalToken' );
  116. $verified = @call_user_func ( $fCheckLocalToken, $username, $source, $token );
  117. if ( $verified ) {
  118. $data['success'] = 'true';
  119. $data['error'] = '';
  120. } else {
  121. $this->_Error ( 'Invalid Token' );
  122. }
  123. echo json_encode ( $data );
  124. exit;
  125. }
  126. /*
  127. * Reply to a remote verification request.
  128. *
  129. */
  130. public function ReplyToRemoteVerify ( ) {
  131. $social = $this->_GET['_social'];
  132. $task = $this->_GET['_task'];
  133. if ( $social != 'true' ) return ( false );
  134. if ( $task != 'verify.remote' ) return ( false );
  135. $source = $this->_GET['_source'];
  136. $username = $this->_GET['_username'];
  137. $token = $this->_GET['_token'];
  138. $fCheckLocalToken = $this->GetCallback ( 'CheckLocalToken' );
  139. if ( !is_callable ( $fCheckLocalToken ) ) $this->_Error ( 'Invalid Callback: CheckLocalToken' );
  140. $verified = @call_user_func ( $fCheckLocalToken, $username, $source, $token, true );
  141. if ( $verified ) {
  142. $data['success'] = 'true';
  143. $data['error'] = '';
  144. } else {
  145. $this->_Error ( 'Invalid Token' );
  146. }
  147. echo json_encode ( $data );
  148. exit;
  149. }
  150. public function Blocked ( ) {
  151. $fCheckBlocked = $this->GetCallback ( 'CheckBlocked' );
  152. if ( !is_callable ( $fCheckBlocked ) ) $this->_Error ( 'Invalid Callback: CheckBlocked' );
  153. $source = $this->_GET['_source'];
  154. $result = @call_user_func ( $fCheckBlocked, $source );
  155. if ( !$result ) $this->_Error ( 'Blocked' );
  156. return ( true );
  157. }
  158. protected function _Error ( $pError ) {
  159. $return = array (
  160. 'success' => 'false',
  161. 'error' => $pError
  162. );
  163. echo json_encode ( $return );
  164. exit;
  165. }
  166. // Single requests
  167. protected function _Communicate ( $pTarget, $pData, $pMethod = 'http' ) {
  168. switch ( $pMethod ) {
  169. case 'https':
  170. $http = 'https://';
  171. break;
  172. default:
  173. $http = 'http://';
  174. break;
  175. }
  176. // Send the data
  177. $url = $http . $pTarget;
  178. $url .= '/?' . http_build_query ($pData );
  179. $curl = curl_init();
  180. $options = array(
  181. CURLOPT_URL => $url,
  182. CURLOPT_RETURNTRANSFER => true,
  183. CURLOPT_HEADER => false,
  184. CURLOPT_FOLLOWLOCATION => true,
  185. CURLOPT_ENCODING => '',
  186. CURLOPT_VERBOSE => true,
  187. CURLOPT_USERAGENT => 'Appleseed QuickSocial API v0.1',
  188. CURLOPT_AUTOREFERER => true,
  189. CURLOPT_CONNECTTIMEOUT => 10,
  190. CURLOPT_TIMEOUT => 20,
  191. CURLOPT_MAXREDIRS => 10,
  192. );
  193. curl_setopt_array( $curl, $options );
  194. // Retrieve the result
  195. $curl_response = curl_exec ( $curl ) ;
  196. curl_close($curl);
  197. // Optionally log the request and result if callback exists.
  198. $fLogNetworkRequest = $this->GetCallback ( 'LogNetworkRequest' );
  199. if ( is_callable ( $fLogNetworkRequest ) ) {
  200. @call_user_func ( $fLogNetworkRequest, $url, $curl_response );
  201. }
  202. // Decode the result
  203. $result = json_decode ( $curl_response );
  204. return ( $result );
  205. }
  206. // Concurrent requests
  207. protected function _Queue ( $pTarget, $pData, $pMethod = 'http' ) {
  208. }
  209. // Generate a random 64 byte token
  210. public function Token ( ) {
  211. $token = substr(md5(uniqid(rand(), true)), 0, 128);
  212. $token .= substr(md5(uniqid(rand(), true)), 0, 128);
  213. return ( $token );
  214. }
  215. public function SetCallback ( $pName, $pFunction ) {
  216. $this->_Callbacks[$pName] = $pFunction;
  217. return ( true );
  218. }
  219. public function GetCallback ( $pName ) {
  220. $callback = $this->_Callbacks[$pName];
  221. if ( !$callback ) return ( false );
  222. return ( $callback );
  223. }
  224. }