Guest User

Untitled

a guest
May 19th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.38 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package OneAll Social Login Mod
  4. * @copyright Copyright 2013 http://www.oneall.com - All rights reserved.
  5. * @license GNU/GPL 2 or later
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
  20. *
  21. * The "GNU General Public License" (GPL) is available at
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. *
  24. */
  25.  
  26. if (!defined ('IN_PHPBB'))
  27. {
  28. exit;
  29. }
  30.  
  31.  
  32. //Oneall Social Login
  33. class oa_social_login
  34. {
  35. const OA_SOCIAL_LOGIN_VERSION = '3.1.0';
  36.  
  37. /**
  38. * Inject variables into template
  39. */
  40. public function setup_template ($template)
  41. {
  42. global $config, $user;
  43.  
  44. // Add our language file.
  45. $user->add_lang ('info_acp_oa_social_login');
  46.  
  47. //Enabled
  48. if (empty ($config ['oa_social_login_disable']))
  49. {
  50. // This will flag the widget to be shown.
  51. $template->assign_var ('OA_SOCIAL_LOGIN_DISABLE', false);
  52.  
  53. //Subdomain is required
  54. if (!empty ($config ['oa_social_login_api_subdomain']))
  55. {
  56. //Providers are required
  57. if (!empty ($config ['oa_social_login_providers']))
  58. {
  59. $oa_social_login_providers = explode (",", $config ['oa_social_login_providers']);
  60.  
  61. //HTTP / HTTPS
  62. $server_protocol = (!empty ($config ['server_protocol'])) ? (str_replace ('://', '', $config ['server_protocol'])) : ($config ['cookie_secure'] ? 'https' : 'http');
  63.  
  64. //Set Placeholders
  65. $template->assign_var ('OA_SOCIAL_LOGIN_CALLBACK_URI', self::get_current_url ());
  66. $template->assign_var ('OA_SOCIAL_LOGIN_VERSION', self::OA_SOCIAL_LOGIN_VERSION);
  67. $template->assign_var ('OA_SOCIAL_LOGIN_RAND', mt_rand (99999, 9999999));
  68. $template->assign_var ('OA_SOCIAL_LOGIN_PROTOCOL', $server_protocol);
  69. $template->assign_var ('OA_SOCIAL_LOGIN_LIBRARY', ($server_protocol . '://' . trim ($config ['oa_social_login_api_subdomain']) . '.api.oneall.com/socialize/library.js'));
  70. $template->assign_var ('OA_SOCIAL_LOGIN_PROVIDERS', implode ("','", $oa_social_login_providers));
  71.  
  72. //Small Icons
  73. // $template->assign_var ('OA_SOCIAL_LOGIN_CSS_THEME', ($server_protocol == "https" ? "https://secure." : "http://public.") . 'oneallcdn.com/css/api/socialize/themes/phpbb/small.css');
  74.  
  75. //User must be logged in and not a bot
  76. if (is_object ($user) && empty ($user->data ['isbot']) && (!empty ($user->data ['user_id']) && $user->data ['user_id'] != ANONYMOUS))
  77. {
  78. //Only display this in the UCP
  79. if (!empty ($user->page ['page_name']) && strpos ($user->page ['page_name'], 'ucp') !== false)
  80. {
  81. //User token
  82. if (($user_token = self::get_user_token_for_user_id ($user->data ['user_id'])) !== false)
  83. {
  84. $template->assign_var ('OA_SOCIAL_LOGIN_USER_TOKEN', $user_token);
  85. }
  86.  
  87. //Link token
  88. $template->assign_var ('OA_SOCIAL_LOGIN_CALLBACK_URI', self::get_current_url () . '&amp;oa_social_login_login_token=' . self::create_login_token_for_user_id ($user->data ['user_id']));
  89. }
  90. }
  91. }
  92. else
  93. {
  94. $template->assign_var ('OA_SOCIAL_LOGIN_ERROR', $user->lang ['OASL_ENABLE_SOCIAL_NETWORK']);
  95. }
  96. }
  97. else
  98. {
  99. $template->assign_var ('OA_SOCIAL_LOGIN_ERROR', $user->lang ['OASL_ENTER_CREDENTIALS']);
  100. }
  101. }
  102. //Disabled
  103. else
  104. {
  105. $template->assign_var ('OA_SOCIAL_LOGIN_DISABLE', true);
  106. }
  107.  
  108. //Done
  109. return $template;
  110. }
  111.  
  112.  
  113. /**
  114. * Callback Handler
  115. */
  116. public function handle_callback ()
  117. {
  118. //Global Variables
  119. global $db, $auth, $user, $config, $user, $template, $phpbb_root_path, $phpbb_admin_path, $phpEx;
  120.  
  121. //Callback Handler
  122. if (isset ($_POST) && !empty ($_POST ['oa_action']) && !empty ($_POST ['connection_token']))
  123. {
  124. //Read arguments
  125. $connection_token = request_var('connection_token', '');
  126. $login_token = request_var('oa_social_login_login_token', '');
  127. $oa_action = strtolower (request_var('oa_action', ''));
  128.  
  129. //Add language file
  130. $user->add_lang ('info_acp_oa_social_login');
  131.  
  132. //Check if enabled
  133. if (empty ($config ['oa_social_login_disable']))
  134. {
  135. //Required settings
  136. if (!empty ($config ['oa_social_login_api_subdomain']) && !empty ($config ['oa_social_login_api_key']) && !empty ($config ['oa_social_login_api_secret']))
  137. {
  138. //API Settings
  139. $api_connection_handler = ((!empty ($config ['oa_social_login_api_connection_handler']) && $config ['oa_social_login_api_connection_handler'] == 'fsockopen') ? 'fsockopen' : 'curl');
  140. $api_connection_use_https = ((!empty ($config ['oa_social_login_api_connection_port']) && $config ['oa_social_login_api_connection_port'] == '80') ? false : true);
  141.  
  142. //API Resource
  143. $api_connection_url = ($api_connection_use_https ? 'https' : 'http') . '://' . $config ['oa_social_login_api_subdomain'] . '.api.oneall.com/connections/' . $connection_token . '.json';
  144.  
  145. //API Credentials
  146. $api_credentials = array ();
  147. $api_credentials ['api_key'] = $config ['oa_social_login_api_key'];
  148. $api_credentials ['api_secret'] = $config ['oa_social_login_api_secret'];
  149.  
  150. //Make Request
  151. $result = self::do_api_request ($api_connection_handler, $api_connection_url, $api_credentials);
  152.  
  153. //Parse result
  154. if (is_object ($result) && property_exists ($result, 'http_code') && $result->http_code == 200)
  155. {
  156. //Extract data
  157. if (($user_data = self::extract_social_network_profile ($result)) !== false)
  158. {
  159. //This is the user to process
  160. $user_id = null;
  161.  
  162. //Social Login
  163. if ($oa_action == 'social_login')
  164. {
  165. // Get user_id by token.
  166. $user_id_tmp = self::get_user_id_for_user_token ($user_data ['user_token']);
  167.  
  168. // We already have a user for this token.
  169. if (is_numeric ($user_id_tmp))
  170. {
  171. // Process this user.
  172. $user_id = $user_id_tmp;
  173.  
  174. // Load user data.
  175. $user_profile = self::get_user_data_by_user_id ($user_id);
  176.  
  177. // The user account needs to be activated.
  178. if (!empty ($user_profile ['user_inactive_reason']))
  179. {
  180. if ($config ['require_activation'] == USER_ACTIVATION_ADMIN)
  181. {
  182. $error_message = $user->lang ['OASL_ACCOUNT_INACTIVE_ADMIN'];
  183. }
  184. else
  185. {
  186. $error_message = $user->lang ['OASL_ACCOUNT_INACTIVE_OTHER'];
  187. }
  188. }
  189. }
  190. // No user has been found for this token.
  191. else
  192. {
  193. // Make sur that account linking is enabled.
  194. if (empty ($config ['oa_social_login_disable_linking']))
  195. {
  196. // Make sure that the email has been verified.
  197. if (!empty ($user_data ['user_email']) && isset ($user_data ['user_email_is_verified']) && $user_data ['user_email_is_verified'] === true)
  198. {
  199. // Read existing user
  200. $user_id_tmp = self::get_user_id_by_email ($user_data ['user_email']);
  201.  
  202. // Existing user found
  203. if (is_numeric ($user_id_tmp))
  204. {
  205. // Link the user to this social network.
  206. if (self::link_tokens_to_user_id ($user_id_tmp, $user_data ['user_token'], $user_data ['identity_token'], $user_data ['identity_provider']) !== false)
  207. {
  208. $user_id = $user_id_tmp;
  209. }
  210. }
  211. }
  212. }
  213.  
  214. //No user has been linked to this token yet
  215. if (!is_numeric ($user_id))
  216. {
  217. //User functions
  218. if (!function_exists ('user_add'))
  219. {
  220. require ($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  221. }
  222.  
  223. //Username is mandatory
  224. if (!isset ($user_data ['user_login']) || strlen (trim ($user_data ['user_login'])) == 0)
  225. {
  226. $user_data ['user_login'] = $user_data ['identity_provider'] . 'User';
  227. }
  228.  
  229. //Username must be unique
  230. if (self::get_user_id_by_username ($user_data ['user_login']) !== false)
  231. {
  232. $i = 1;
  233. $user_login_tmp = $user_data ['user_login'] . ($i);
  234. while (self::get_user_id_by_username ($user_login_tmp) !== false)
  235. {
  236. $user_login_tmp = $user_data ['user_login'] . ($i++);
  237. }
  238. $user_data ['user_login'] = $user_login_tmp;
  239. }
  240.  
  241. //Email must be unique
  242. if (!isset ($user_data ['user_email']) || self::get_user_id_by_email ($user_data ['user_email']) !== false)
  243. {
  244. //Create a random email
  245. $user_data ['user_email'] = self::generate_random_email ();
  246.  
  247. //Used below
  248. $user_random_email = true;
  249. }
  250. else
  251. {
  252. $user_random_email = false;
  253. }
  254.  
  255. //Default group_id is required
  256. $group_id = self::get_default_group_id ();
  257. if (!is_numeric ($group_id))
  258. {
  259. trigger_error ('NO_GROUP');
  260. }
  261.  
  262. //Activation Required
  263. if (!$user_random_email && ($config ['require_activation'] == USER_ACTIVATION_SELF || $config ['require_activation'] == USER_ACTIVATION_ADMIN) && $config ['email_enable'])
  264. {
  265. $user_type = USER_INACTIVE;
  266. $user_actkey = gen_rand_string (mt_rand (6, 10));
  267. $user_inactive_reason = INACTIVE_REGISTER;
  268. $user_inactive_time = time ();
  269. }
  270. //No Activation Required
  271. else
  272. {
  273. $user_type = USER_NORMAL;
  274. $user_actkey = '';
  275. $user_inactive_reason = 0;
  276. $user_inactive_time = 0;
  277. }
  278.  
  279. //Generate a random password
  280. $new_password = self::generate_hash ($config ['min_pass_chars'] + rand (3, 5));
  281.  
  282. //User Details
  283. $user_row = array (
  284. 'group_id' => $group_id,
  285. 'user_type' => $user_type,
  286. 'user_actkey' => $user_actkey,
  287. 'user_password' => phpbb_hash ($new_password),
  288. 'user_ip' => $user->ip,
  289. 'user_inactive_reason' => $user_inactive_reason,
  290. 'user_inactive_time' => $user_inactive_time,
  291. 'user_lastvisit' => time (),
  292. 'username' => $user_data ['user_login'],
  293. 'user_email' => $user_data ['user_email'],
  294. 'user_from' => $user_data ['user_location'],
  295. 'user_interests' => $user_data ['users_interests'],
  296. 'user_website' => $user_data ['user_website']
  297. );
  298.  
  299. // Register user
  300. $user_id_tmp = user_add ($user_row, false);
  301.  
  302. // This should not happen, because the required variables are listed above...
  303. if ($user_id_tmp === false)
  304. {
  305. trigger_error ('NO_USER', E_USER_ERROR);
  306. }
  307. //User Added
  308. else
  309. {
  310. // Link the user to this social network.
  311. if (self::link_tokens_to_user_id ($user_id_tmp, $user_data ['user_token'], $user_data ['identity_token'], $user_data ['identity_provider']) !== false)
  312. {
  313. // Process this user.
  314. $user_id = $user_id_tmp;
  315.  
  316. //Send Email
  317. if ($config ['email_enable'] && !$user_random_email)
  318. {
  319. //Include Messenger
  320. if (!class_exists ('messenger'))
  321. {
  322. require ($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
  323. }
  324.  
  325. //Activation Type
  326. if ($config ['require_activation'] == USER_ACTIVATION_SELF)
  327. {
  328. $error_message = $user->lang ['OASL_ACCOUNT_INACTIVE_OTHER'];
  329. $email_template = 'user_welcome_inactive';
  330. }
  331. else if ($config ['require_activation'] == USER_ACTIVATION_ADMIN)
  332. {
  333. $error_message = $user->lang ['OASL_ACCOUNT_INACTIVE_ADMIN'];
  334. $email_template = 'admin_welcome_inactive';
  335. }
  336. else
  337. {
  338. $email_template = 'user_welcome';
  339. }
  340.  
  341. //Current url
  342. $server_url = generate_board_url ();
  343.  
  344. //Send email to new user
  345. $messenger = new messenger (false);
  346. $messenger->template ($email_template, 'en');
  347. $messenger->to ($user_row ['user_email'], $user_row ['username']);
  348. $messenger->anti_abuse_headers ($config, $user);
  349. $messenger->assign_vars (array (
  350. 'WELCOME_MSG' => htmlspecialchars_decode (sprintf ($user->lang ['WELCOME_SUBJECT'], $config ['sitename'])),
  351. 'USERNAME' => htmlspecialchars_decode ($user_row ['username']),
  352. 'PASSWORD' => htmlspecialchars_decode ($new_password),
  353. 'U_ACTIVATE' => $server_url . '/ucp.' . $phpEx . '?mode=activate&u=' . $user_id . '&k=' . $user_actkey
  354. ));
  355. $messenger->send (NOTIFY_EMAIL);
  356.  
  357. //Send email to administrators
  358. if ($config ['require_activation'] == USER_ACTIVATION_ADMIN)
  359. {
  360. // Grab an array of user_id's with a_user permissions ... these users can activate a user
  361. $acl_admins = $auth->acl_get_list (false, 'a_user', false);
  362. $acl_admins = (!empty ($acl_admins [0] ['a_user'])) ? $acl_admins [0] ['a_user'] : array ();
  363.  
  364. // Read administrators
  365. $sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type
  366. FROM ' . USERS_TABLE . '
  367. WHERE user_type = ' . USER_FOUNDER;
  368.  
  369. if (is_array ($acl_admins) && count ($acl_admins) > 0)
  370. {
  371. $sql .= ' OR ' . $db->sql_in_set ('user_id', $acl_admins);
  372. }
  373.  
  374. $query = $db->sql_query ($sql);
  375. while ($row = $db->sql_fetchrow ($query))
  376. {
  377. $messenger->template ('admin_activate', $row ['user_lang']);
  378. $messenger->to ($row ['user_email'], $row ['username']);
  379. $messenger->im ($row ['user_jabber'], $row ['username']);
  380.  
  381. $messenger->assign_vars (array (
  382. 'USERNAME' => htmlspecialchars_decode ($user_row ['username']),
  383. 'U_USER_DETAILS' => $server_url . '/memberlist.' . $phpEx . '?mode=viewprofile&u=' . $user_id,
  384. 'U_ACTIVATE' => $server_url . '/ucp.' . $phpEx . '?mode=activate&u=' . $user_id . '&k=' . $user_actkey
  385. ));
  386.  
  387. $messenger->send ($row ['user_notify_type']);
  388. }
  389. $db->sql_freeresult ($query);
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396.  
  397. //Display an error message
  398. if (isset ($error_message))
  399. {
  400. $error_message = $error_message . '<br /><br />' . sprintf ($user->lang ['RETURN_INDEX'], '<a href="' . append_sid ("{$phpbb_root_path}index.$phpEx") . '">', '</a>');
  401. trigger_error ($error_message);
  402. }
  403. //Process
  404. else
  405. {
  406. if (isset ($user_id) && is_numeric ($user_id))
  407. {
  408. //Update statistics
  409. self::count_login_identity_token ($user_data ['identity_token']);
  410.  
  411. //Log the user in
  412. $user->session_create ($user_id);
  413.  
  414. //Redirect to a custom page
  415. if (!empty ($config ['oa_social_login_redirect']))
  416. {
  417. redirect ($config ['oa_social_login_redirect'], false, true);
  418. }
  419. }
  420. }
  421. }
  422. //Social Link
  423. elseif ($oa_action == 'social_link')
  424. {
  425. // This argument is required.
  426. if (!empty ($login_token))
  427. {
  428. // Read the user_id for this login_token.
  429. $user_id_login_token = self::get_user_id_for_login_token ($login_token);
  430.  
  431. //We have a user for this login token
  432. if (is_numeric ($user_id_login_token))
  433. {
  434. //Update the tokens?
  435. $update_tokens = true;
  436.  
  437. //Read the user_id for this user_token
  438. $user_id_user_token = self::get_user_id_for_user_token ($user_data ['user_token']);
  439.  
  440. // There is already a user_id for this token
  441. if (!empty ($user_id_user_token))
  442. {
  443. // The existing user_id does not match the logged in user
  444. if ($user_id_user_token != $user_id_login_token)
  445. {
  446. // Show an error to the user.
  447. $template->assign_var ('OA_SOCIAL_LINK_ERROR', $user->lang['OASL_ACCOUNT_ALREADY_LINKED']);
  448.  
  449. // Do not updated the tokens.
  450. $update_tokens = false;
  451. }
  452. }
  453.  
  454. // Update token?
  455. if ($update_tokens === true)
  456. {
  457. if (!empty ($user_data ['plugin_action']) && $user_data ['plugin_action'] == 'link_identity')
  458. {
  459. self::link_tokens_to_user_id ($user_id_login_token, $user_data ['user_token'], $user_data ['identity_token'], $user_data ['identity_provider']);
  460. }
  461. else
  462. {
  463. self::unlink_identity_token ($user_data ['identity_token']);
  464. }
  465. }
  466.  
  467. //Relogin the user
  468. $user->session_create ($user_id_login_token);
  469. }
  470. }
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478.  
  479.  
  480. /**
  481. * Extracts the social network data from a result-set returned by the OneAll API.
  482. */
  483. public static function extract_social_network_profile ($social_data)
  484. {
  485. // Check API result.
  486. if (is_object ($social_data) && property_exists ($social_data, 'http_code') && $social_data->http_code == 200 && property_exists ($social_data, 'http_data'))
  487. {
  488. // Decode the social network profile Data.
  489. $social_data = json_decode ($social_data->http_data);
  490.  
  491. // Make sur that the data has beeen decoded properly
  492. if (is_object ($social_data))
  493. {
  494. // Container for user data
  495. $data = array ();
  496.  
  497. // Parse plugin data.
  498. if (isset ($social_data->response->result->data->plugin))
  499. {
  500. $plugin = $social_data->response->result->data->plugin;
  501. $data ['plugin_key'] = $plugin->key;
  502. $data ['plugin_action'] = (isset ($plugin->data->action) ? $plugin->data->action : null);
  503. $data ['plugin_operation'] = (isset ($plugin->data->operation) ? $plugin->data->operation : null);
  504. $data ['plugin_reason'] = (isset ($plugin->data->reason) ? $plugin->data->reason : null);
  505. $data ['plugin_status'] = (isset ($plugin->data->status) ? $plugin->data->status : null);
  506. }
  507.  
  508. // Parse Social Profile Data.
  509. $identity = $social_data->response->result->data->user->identity;
  510.  
  511. $data ['identity_token'] = $identity->identity_token;
  512. $data ['identity_provider'] = $identity->source->name;
  513.  
  514. $data ['user_token'] = $social_data->response->result->data->user->user_token;
  515. $data ['user_first_name'] = !empty ($identity->name->givenName) ? $identity->name->givenName : '';
  516. $data ['user_last_name'] = !empty ($identity->name->familyName) ? $identity->name->familyName : '';
  517. $data ['user_location'] = !empty ($identity->currentLocation) ? $identity->currentLocation : '';
  518. $data ['user_constructed_name'] = trim ($data ['user_first_name'] . ' ' . $data ['user_last_name']);
  519. $data ['user_picture'] = !empty ($identity->pictureUrl) ? $identity->pictureUrl : '';
  520. $data ['user_thumbnail'] = !empty ($identity->thumbnailUrl) ? $identity->thumbnailUrl : '';
  521. $data ['user_about_me'] = !empty ($identity->aboutMe) ? $identity->aboutMe : '';
  522.  
  523. //User Interests
  524. $data ['users_interests'] = '';
  525. if (isset ($identity->interests) && is_array ($identity->interests))
  526. {
  527. $data ['users_interests'] = array ();
  528. foreach ($identity->interests AS $interest)
  529. {
  530. $data ['users_interests'] [] = $interest->value;
  531. }
  532. $data ['users_interests'] = implode (", ", $data ['users_interests']);
  533. }
  534.  
  535. // Birthdate - MM/DD/YYYY
  536. if (!empty ($identity->birthday) && preg_match ('/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/', $identity->birthday, $matches))
  537. {
  538. $data ['user_birthdate'] = str_pad ($matches [2], 2, '0', STR_PAD_LEFT);
  539. $data ['user_birthdate'] .= '/' . str_pad ($matches [1], 2, '0', STR_PAD_LEFT);
  540. $data ['user_birthdate'] .= '/' . str_pad ($matches [3], 4, '0', STR_PAD_LEFT);
  541. }
  542. else
  543. {
  544. $data ['user_birthdate'] = '';
  545. }
  546.  
  547. // Fullname.
  548. if (!empty ($identity->name->formatted))
  549. {
  550. $data ['user_full_name'] = $identity->name->formatted;
  551. }
  552. elseif (!empty ($identity->name->displayName))
  553. {
  554. $data ['user_full_name'] = $identity->name->displayName;
  555. }
  556. else
  557. {
  558. $data ['user_full_name'] = $data ['user_constructed_name'];
  559. }
  560.  
  561. // Preferred Username.
  562. if (!empty ($identity->preferredUsername))
  563. {
  564. $data ['user_login'] = $identity->preferredUsername;
  565. }
  566. elseif (!empty ($identity->displayName))
  567. {
  568. $data ['user_login'] = $identity->displayName;
  569. }
  570. else
  571. {
  572. $data ['user_login'] = $data ['user_full_name'];
  573. }
  574.  
  575. //phpBB does not like spaces here
  576. $data ['user_login'] = str_replace (' ', '', trim ($data ['user_login']));
  577.  
  578. // Email Address.
  579. $data ['user_email'] = '';
  580. if (property_exists ($identity, 'emails') && is_array ($identity->emails))
  581. {
  582. $data ['user_email_is_verified'] = false;
  583. while ($data ['user_email_is_verified'] !== true && (list(, $obj) = each ($identity->emails)))
  584. {
  585. $data ['user_email'] = $obj->value;
  586. $data ['user_email_is_verified'] = !empty ($obj->is_verified);
  587. }
  588. }
  589.  
  590. // Website/Homepage.
  591. $data ['user_website'] = '';
  592. if (!empty ($identity->profileUrl))
  593. {
  594. $data ['user_website'] = $identity->profileUrl;
  595. }
  596. elseif (!empty ($identity->urls [0]->value))
  597. {
  598. $data ['user_website'] = $identity->urls [0]->value;
  599. }
  600.  
  601. // Gender.
  602. $data ['user_gender'] = '';
  603. if (!empty ($identity->gender))
  604. {
  605. switch ($identity->gender)
  606. {
  607. case 'male':
  608. $data ['user_gender'] = 'm';
  609. break;
  610.  
  611. case 'female':
  612. $data ['user_gender'] = 'f';
  613. break;
  614. }
  615. }
  616.  
  617. return $data;
  618. }
  619. }
  620. return false;
  621. }
  622.  
  623. /**
  624. * Return the current url
  625. */
  626. private static function get_current_url ()
  627. {
  628. global $user;
  629.  
  630. //Read the current url.
  631. $current_url = generate_board_url() . '/' . $user->page['page'];
  632.  
  633. //Check if it contains the oa_social_login_login_token argument.
  634. if (strpos ($current_url, 'oa_social_login_login_token') !== false)
  635. {
  636. //Break up the url.
  637. list($url_part, $query_part) = array_pad (explode ('?', $current_url), 2, '');
  638. parse_str ($query_part, $query_vars);
  639.  
  640. //Remove the oa_social_login_source argument.
  641. if (is_array ($query_vars) && isset ($query_vars ['oa_social_login_login_token']))
  642. {
  643. unset ($query_vars ['oa_social_login_login_token']);
  644. }
  645.  
  646. //Build a new url.
  647. $current_url = $url_part . ((is_array ($query_vars) && count ($query_vars) > 0) ? ('?' . http_build_query ($query_vars)) : '');
  648. }
  649.  
  650. return $current_url;
  651. }
  652.  
  653.  
  654. /**
  655. * Counts a login for the identity token
  656. */
  657. public static function count_login_identity_token ($identity_token)
  658. {
  659. global $db;
  660.  
  661. // Update the counter for the given identity_token.
  662. $sql = "UPDATE " . OASL_IDENTITY_TABLE ." SET num_logins=num_logins+1, date_updated='" . time () . "'
  663. WHERE identity_token = '" . $db->sql_escape ($identity_token) . "'";
  664. $query = $db->sql_query ($sql);
  665. }
  666.  
  667. /**
  668. * Unlinks the identity token
  669. */
  670. public static function unlink_identity_token ($identity_token)
  671. {
  672. global $db;
  673.  
  674. // Delete the identity_token.
  675. $sql = "DELETE FROM " . OASL_IDENTITY_TABLE . "
  676. WHERE identity_token = '" . $db->sql_escape ($identity_token) . "'";
  677. $query = $db->sql_query ($sql);
  678. }
  679.  
  680.  
  681. /**
  682. * Links the user/identity tokens to a user
  683. */
  684. public static function link_tokens_to_user_id ($user_id, $user_token, $identity_token, $identity_provider)
  685. {
  686. global $db;
  687.  
  688. // Make sure that that the user exists.
  689. $sql = "SELECT user_id
  690. FROM " . USERS_TABLE . "
  691. WHERE user_id = '" . intval ($user_id) . "'";
  692. $query = $db->sql_query_limit ($sql, 1);
  693. $result = $db->sql_fetchrow ($query);
  694. $db->sql_freeresult($query);
  695.  
  696. // The user exists.
  697. if (is_array ($result) && !empty ($result ['user_id']))
  698. {
  699. $user_id = $result ['user_id'];
  700.  
  701. $oasl_user_id = null;
  702. $oasl_identity_id = null;
  703.  
  704. // Delete superfluous user_token.
  705. $sql = "SELECT oasl_user_id
  706. FROM " . OASL_USER_TABLE . "
  707. WHERE user_id = '" . intval ($user_id) . "' AND user_token <> '" . $db->sql_escape ($user_token) . "'";
  708. $query = $db->sql_query ($sql);
  709. while ($row = $db->sql_fetchrow ($query))
  710. {
  711. // Delete the wrongly linked user_token.
  712. $sql = "DELETE FROM " . OASL_USER_TABLE . "
  713. WHERE oasl_user_id = '" . $db->sql_escape ($row ['oasl_user_id']) . "'";
  714. $query = $db->sql_query ($sql);
  715.  
  716. // Delete the wrongly linked identity_token.
  717. $sql = "DELETE FROM " . OASL_IDENTITY_TABLE . "
  718. WHERE oasl_user_id = '" . $db->sql_escape ($row ['oasl_user_id']) . "'";
  719. $query = $db->sql_query ($sql);
  720. }
  721. $db->sql_freeresult($query);
  722.  
  723. // Read the entry for the given user_token.
  724. $sql = "SELECT oasl_user_id, user_id
  725. FROM " . OASL_USER_TABLE . "
  726. WHERE user_token = '" . $db->sql_escape ($user_token) . "'";
  727. $query = $db->sql_query ($sql);
  728. $result = $db->sql_fetchrow ($query);
  729. $db->sql_freeresult($query);
  730.  
  731. // The user_token exists
  732. if (is_array ($result) && !empty ($result ['oasl_user_id']))
  733. {
  734. $oasl_user_id = $result ['oasl_user_id'];
  735. }
  736.  
  737. // The user_token either does not exist or has been reset.
  738. if (empty ($oasl_user_id))
  739. {
  740. // Add new link.
  741. $sql_arr = array(
  742. 'user_id' => intval ($user_id),
  743. 'user_token' => $user_token,
  744. 'date_added' => time()
  745. );
  746. $sql = "INSERT INTO " . OASL_USER_TABLE." ".$db->sql_build_array('INSERT', $sql_arr);
  747. $query = $db->sql_query ($sql);
  748.  
  749. // Identifier of the newly created user_token entry.
  750. $oasl_user_id = $db->sql_nextid ();
  751. }
  752.  
  753. // Read the entry for the given identity_token.
  754. $sql = "SELECT oasl_identity_id, oasl_user_id, identity_token
  755. FROM " . OASL_IDENTITY_TABLE . "
  756. WHERE identity_token = '" . $db->sql_escape ($identity_token) . "'";
  757. $query = $db->sql_query ($sql);
  758. $result = $db->sql_fetchrow ($query);
  759. $db->sql_freeresult($query);
  760.  
  761. // The identity_token exists
  762. if (is_array ($result) && !empty ($result ['oasl_identity_id']))
  763. {
  764. $oasl_identity_id = $result ['oasl_identity_id'];
  765.  
  766. // The identity_token is linked to another user_token.
  767. if (!empty ($result ['oasl_user_id']) && $result ['oasl_user_id'] != $oasl_user_id)
  768. {
  769. // Delete the wrongly linked identity_token.
  770. $sql = "DELETE FROM " . OASL_IDENTITY_TABLE . "
  771. WHERE oasl_identity_id = '" . intval ($oasl_identity_id) . "' LIMIT 1";
  772. $query = $db->sql_query_limit ($sql, 1);
  773.  
  774. // Reset the identifier
  775. $oasl_identity_id = null;
  776. }
  777. }
  778.  
  779. // The identity_token either does not exist or has been reset.
  780. if (empty ($oasl_identity_id))
  781. {
  782. // Add new link.
  783. $sql_arr = array(
  784. 'oasl_user_id' => intval ($oasl_user_id),
  785. 'identity_token' => $identity_token,
  786. 'identity_provider' => $identity_provider,
  787. 'num_logins' => 1,
  788. 'date_added' => time(),
  789. 'date_updated' => time()
  790. );
  791. $sql = "INSERT INTO " . OASL_IDENTITY_TABLE . " ".$db->sql_build_array('INSERT', $sql_arr);
  792. $query = $db->sql_query ($sql);
  793.  
  794. // Identifier of the newly created identity_token entry.
  795. $oasl_identity_id = $db->sql_nextid ();
  796. }
  797.  
  798. // Done.
  799. return true;
  800. }
  801.  
  802. // An error occured.
  803. return false;
  804. }
  805.  
  806.  
  807. /**
  808. * Generate a random email address
  809. */
  810. protected static function generate_random_email ()
  811. {
  812. do
  813. {
  814. $email = self::generate_hash (10) . "@example.com";
  815. }
  816. while (self::get_user_id_by_email ($email) !== false);
  817.  
  818. //Done
  819. return $email;
  820. }
  821.  
  822.  
  823. /**
  824. * Generate a random hash of the given length
  825. */
  826. protected static function generate_hash ($length)
  827. {
  828. $hash = '';
  829.  
  830. for ($i = 0; $i < $length; $i++)
  831. {
  832. do
  833. {
  834. $char = chr (mt_rand (48, 122));
  835. }
  836. while (!preg_match ('/[a-zA-Z0-9]/', $char));
  837. $hash .= $char;
  838. }
  839.  
  840. //Done
  841. return $hash;
  842. }
  843.  
  844.  
  845. /**
  846. * Get the user_id for a given email address.
  847. */
  848. protected static function get_user_id_by_email ($email)
  849. {
  850. global $db;
  851.  
  852. // Read the user_id for this email address.
  853. $sql = "SELECT user_id
  854. FROM " . USERS_TABLE . "
  855. WHERE user_email = '" . $db->sql_escape ($email) . "'";
  856. $query = $db->sql_query_limit ($sql, 1);
  857. $result = $db->sql_fetchrow ($query);
  858. $db->sql_freeresult($query);
  859.  
  860. // We have found an user_id.
  861. if (is_array ($result) && !empty ($result ['user_id']))
  862. {
  863. return $result ['user_id'];
  864. }
  865.  
  866. // Not found.
  867. return false;
  868. }
  869.  
  870.  
  871. /**
  872. * Get the user_id for a given a username.
  873. */
  874. protected static function get_user_id_by_username ($user_login)
  875. {
  876. global $db;
  877.  
  878. // Read the user_id for this login
  879. $sql = "SELECT user_id
  880. FROM " . USERS_TABLE . "
  881. WHERE username = '" . $db->sql_escape ($user_login) . "'";
  882. $query = $db->sql_query_limit ($sql, 1);
  883. $result = $db->sql_fetchrow ($query);
  884. $db->sql_freeresult($query);
  885.  
  886. // We have found an user_id.
  887. if (is_array ($result) && !empty ($result ['user_id']))
  888. {
  889. return $result ['user_id'];
  890. }
  891.  
  892. // Not found.
  893. return false;
  894. }
  895.  
  896.  
  897. /**
  898. * Returns the user_id for a given token.
  899. */
  900. protected static function get_user_id_for_user_token ($user_token)
  901. {
  902. global $db;
  903.  
  904. // Make sure it is not empty.
  905. $user_token = trim ($user_token);
  906. if (strlen ($user_token) == 0)
  907. {
  908. return false;
  909. }
  910.  
  911. // Read the user_id for this user_token.
  912. $sql = "SELECT oasl_user_id, user_id
  913. FROM " . OASL_USER_TABLE . "
  914. WHERE user_token = '" . $db->sql_escape ($user_token) . "'";
  915. $query = $db->sql_query ($sql);
  916. $result = $db->sql_fetchrow ($query);
  917. $db->sql_freeresult($query);
  918.  
  919. // The user_token exists
  920. if (is_array ($result) && !empty ($result ['oasl_user_id']))
  921. {
  922. $user_id = intval ($result ['user_id']);
  923. $oasl_user_id = intval ($result ['oasl_user_id']);
  924.  
  925. // Check if the user account exists.
  926. $sql = "SELECT user_id
  927. FROM " . USERS_TABLE . "
  928. WHERE user_id = '" . intval ($user_id) . "'";
  929. $query = $db->sql_query_limit ($sql, 1);
  930. $result = $db->sql_fetchrow ($query);
  931. $db->sql_freeresult($query);
  932.  
  933. // The user account exists, return it's identifier.
  934. if (is_array ($result) && !empty ($result ['user_id']))
  935. {
  936. return $result ['user_id'];
  937. }
  938.  
  939. // Delete the wrongly linked user_token.
  940. $sql = "DELETE FROM " . OASL_USER_TABLE . "
  941. WHERE user_token = '" . $db->sql_escape ($user_token) . "'";
  942. $query = $db->sql_query_limit ($sql, 1);
  943.  
  944. // Delete the wrongly linked identity_token.
  945. $sql = "DELETE FROM " . OASL_IDENTITY_TABLE . "
  946. WHERE oasl_user_id = '" . intval ($oasl_user_id) . "'";
  947. $query = $db->sql_query ($sql);
  948. }
  949.  
  950. // No entry found.
  951. return false;
  952. }
  953.  
  954.  
  955. /**
  956. * Get the user_token from a user_id
  957. */
  958. private static function get_user_token_for_user_id ($user_id)
  959. {
  960. global $db;
  961.  
  962. // Read the user_id for this user_token.
  963. $sql = "SELECT user_token
  964. FROM " . OASL_USER_TABLE . "
  965. WHERE user_id = '" . intval ($user_id) . "'";
  966. $query = $db->sql_query ($sql);
  967. $result = $db->sql_fetchrow ($query);
  968. $db->sql_freeresult($query);
  969.  
  970. // The user_token exists
  971. if (is_array ($result) && !empty ($result ['user_token']))
  972. {
  973. return $result ['user_token'];
  974. }
  975.  
  976. //Not found
  977. return false;
  978. }
  979.  
  980.  
  981. /**
  982. * Return the user_id for a login token
  983. */
  984. protected static function get_user_id_for_login_token ($login_token)
  985. {
  986. global $db;
  987.  
  988. //Read the user_id for this login_token
  989. $sql = "SELECT user_id
  990. FROM " .OASL_LOGIN_TOKEN_TABLE . "
  991. WHERE login_token = '" . $db->sql_escape ($login_token) . "'";
  992. $query = $db->sql_query_limit ($sql, 1);
  993. $result = $db->sql_fetchrow ($query);
  994. $db->sql_freeresult($query);
  995.  
  996. //The login_token exists
  997. if (is_array ($result) && !empty ($result ['user_id']))
  998. {
  999. return $result ['user_id'];
  1000. }
  1001.  
  1002. //Not found
  1003. return false;
  1004. }
  1005.  
  1006.  
  1007. /**
  1008. * Create a login token for a user_id
  1009. */
  1010. private static function create_login_token_for_user_id ($user_id)
  1011. {
  1012. global $db;
  1013.  
  1014. //Remove old or existing login token
  1015. $sql = "DELETE FROM " . OASL_LOGIN_TOKEN_TABLE ."
  1016. WHERE (user_id = '" . intval ($user_id) . "' OR date_creation < '" . (time () - 60 * 5) . "')";
  1017. $query = $db->sql_query ($sql);
  1018.  
  1019. //Create a new and unique token
  1020. do
  1021. {
  1022. $login_token = self::get_uuid_v4 ();
  1023. }
  1024. while (self::get_user_id_for_login_token ($login_token) !== false);
  1025.  
  1026. //Add the new token.
  1027. $sql_arr = array(
  1028. 'login_token' => $login_token,
  1029. 'user_id' => $user_id,
  1030. 'date_creation' => time()
  1031. );
  1032. $sql = "INSERT INTO " . OASL_LOGIN_TOKEN_TABLE ." ".$db->sql_build_array('INSERT', $sql_arr);
  1033. $query = $db->sql_query ($sql);
  1034.  
  1035. //Done
  1036. return $login_token;
  1037. }
  1038.  
  1039.  
  1040. /**
  1041. * Get the default group_id for new users
  1042. */
  1043. private static function get_default_group_id ()
  1044. {
  1045. global $db;
  1046.  
  1047. // Read the default group.
  1048. $sql = "SELECT group_id
  1049. FROM " . GROUPS_TABLE . "
  1050. WHERE group_name = 'REGISTERED' AND group_type = " . GROUP_SPECIAL;
  1051. $query = $db->sql_query ($sql);
  1052. $result = $db->sql_fetchrow ($query);
  1053. $db->sql_freeresult($query);
  1054.  
  1055. //Group found;
  1056. if (is_array ($result) && isset ($result ['group_id']))
  1057. {
  1058. return $result ['group_id'];
  1059. }
  1060.  
  1061. //Not found
  1062. return false;
  1063. }
  1064.  
  1065.  
  1066. /**
  1067. * Get the user data for a user_id
  1068. */
  1069. private static function get_user_data_by_user_id ($user_id)
  1070. {
  1071. global $db;
  1072.  
  1073. // Read the user data.
  1074. $sql = "SELECT *
  1075. FROM " . USERS_TABLE . "
  1076. WHERE user_id = '" . intval ($user_id) . "'";
  1077. $query = $db->sql_query_limit ($sql, 1);
  1078. $result = $db->sql_fetchrow ($query);
  1079. $db->sql_freeresult($query);
  1080.  
  1081. // The user has been found.
  1082. if (is_array ($result))
  1083. {
  1084. return $result;
  1085. }
  1086.  
  1087. // Not found.
  1088. return array ();
  1089. }
  1090.  
  1091. /**
  1092. * Generates a v4 UUID
  1093. */
  1094. private static function get_uuid_v4 ()
  1095. {
  1096. return sprintf ('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand (0, 0xffff), mt_rand (0, 0xffff), mt_rand (0, 0xffff), mt_rand (0, 0x0fff) | 0x4000, mt_rand (0, 0x3fff) | 0x8000, mt_rand (0, 0xffff), mt_rand (0, 0xffff), mt_rand (0, 0xffff));
  1097. }
  1098.  
  1099. /**
  1100. * Return the list of available providers
  1101. */
  1102. public static function get_providers ()
  1103. {
  1104. return array (
  1105. 'facebook' => array (
  1106. 'name' => 'Facebook'
  1107. ),
  1108. 'twitter' => array (
  1109. 'name' => 'Twitter'
  1110. ),
  1111. 'google' => array (
  1112. 'name' => 'Google'
  1113. ),
  1114. 'linkedin' => array (
  1115. 'name' => 'LinkedIn'
  1116. ),
  1117. 'yahoo' => array (
  1118. 'name' => 'Yahoo'
  1119. ),
  1120. 'github' => array (
  1121. 'name' => 'Github.com'
  1122. ),
  1123. 'foursquare' => array (
  1124. 'name' => 'Foursquare'
  1125. ),
  1126. 'youtube' => array (
  1127. 'name' => 'YouTube'
  1128. ),
  1129. 'skyrock' => array (
  1130. 'name' => 'Skyrock.com'
  1131. ),
  1132. 'openid' => array (
  1133. 'name' => 'OpenID'
  1134. ),
  1135. 'wordpress' => array (
  1136. 'name' => 'Wordpress.com'
  1137. ),
  1138. 'hyves' => array (
  1139. 'name' => 'Hyves'
  1140. ),
  1141. 'paypal' => array (
  1142. 'name' => 'PayPal'
  1143. ),
  1144. 'livejournal' => array (
  1145. 'name' => 'LiveJournal'
  1146. ),
  1147. 'steam' => array (
  1148. 'name' => 'Steam Community'
  1149. ),
  1150. 'windowslive' => array (
  1151. 'name' => 'Windows Live'
  1152. ),
  1153. 'blogger' => array (
  1154. 'name' => 'Blogger'
  1155. ),
  1156. 'disqus' => array (
  1157. 'name' => 'Disqus'
  1158. ),
  1159. 'stackexchange' => array (
  1160. 'name' => 'StackExchange'
  1161. ),
  1162. 'vkontakte' => array (
  1163. 'name' => 'VKontakte (Вконтакте)'
  1164. ),
  1165. 'odnoklassniki' => array (
  1166. 'name' => 'Odnoklassniki.ru'
  1167. ),
  1168. 'mailru' => array (
  1169. 'name' => 'Mail.ru'
  1170. )
  1171. );
  1172. }
  1173.  
  1174. /**
  1175. * Returns a list of disabled functions.
  1176. */
  1177. protected static function get_php_disabled_functions ()
  1178. {
  1179. $disabled_functions = trim (ini_get ('disable_functions'));
  1180. if (strlen ($disabled_functions) == 0)
  1181. {
  1182. $disabled_functions = array ();
  1183. }
  1184. else
  1185. {
  1186. $disabled_functions = explode (',', $disabled_functions);
  1187. $disabled_functions = array_map ('trim', $disabled_functions);
  1188. }
  1189. return $disabled_functions;
  1190. }
  1191.  
  1192.  
  1193. /**
  1194. * Send an API request by using the given handler
  1195. */
  1196. public static function do_api_request ($handler, $url, $options = array (), $timeout = 30)
  1197. {
  1198. //FSOCKOPEN
  1199. if ($handler == 'fsockopen')
  1200. {
  1201. return self::fsockopen_request ($url, $options, $timeout);
  1202. }
  1203. //CURL
  1204. else
  1205. {
  1206. return self::curl_request ($url, $options, $timeout);
  1207. }
  1208. }
  1209.  
  1210.  
  1211. /**
  1212. * Check if CURL can be used
  1213. */
  1214. public static function check_curl ($secure = true)
  1215. {
  1216. if (in_array ('curl', get_loaded_extensions ()) && function_exists ('curl_exec') && !in_array ('curl_exec', self::get_php_disabled_functions ()))
  1217. {
  1218. $result = self::curl_request (($secure ? 'https' : 'http') . '://www.oneall.com/ping.html');
  1219. if (is_object ($result) && property_exists ($result, 'http_code') && $result->http_code == 200)
  1220. {
  1221. if (property_exists ($result, 'http_data'))
  1222. {
  1223. if (strtolower ($result->http_data) == 'ok')
  1224. {
  1225. return true;
  1226. }
  1227. }
  1228. }
  1229. }
  1230. return false;
  1231. }
  1232.  
  1233.  
  1234. /**
  1235. * Check if fsockopen can be used
  1236. */
  1237. public static function check_fsockopen ($secure = true)
  1238. {
  1239. if (function_exists ('fsockopen') && !in_array ('fsockopen', self::get_php_disabled_functions ()))
  1240. {
  1241. $result = self::fsockopen_request (($secure ? 'https' : 'http') . '://www.oneall.com/ping.html');
  1242. if (is_object ($result) && property_exists ($result, 'http_code') && $result->http_code == 200)
  1243. {
  1244. if (property_exists ($result, 'http_data'))
  1245. {
  1246. if (strtolower ($result->http_data) == 'ok')
  1247. {
  1248. return true;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. return false;
  1254. }
  1255.  
  1256.  
  1257. /**
  1258. * Sends a CURL request
  1259. */
  1260. protected static function curl_request ($url, $options = array (), $timeout = 30)
  1261. {
  1262. //Store the result
  1263. $result = new stdClass ();
  1264.  
  1265. //Send request
  1266. $curl = curl_init ();
  1267. curl_setopt ($curl, CURLOPT_URL, $url);
  1268. curl_setopt ($curl, CURLOPT_HEADER, 0);
  1269. curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
  1270. curl_setopt ($curl, CURLOPT_VERBOSE, 0);
  1271. curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
  1272. curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
  1273. curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
  1274. curl_setopt ($curl, CURLOPT_USERAGENT, 'SocialLogin ' . self::OA_SOCIAL_LOGIN_VERSION . ' phpBB3 (+http://www.oneall.com/)');
  1275.  
  1276. // BASIC AUTH?
  1277. if (isset ($options ['api_key']) && isset ($options ['api_secret']))
  1278. {
  1279. curl_setopt ($curl, CURLOPT_USERPWD, $options ['api_key'] . ":" . $options ['api_secret']);
  1280. }
  1281.  
  1282. //Make request
  1283. if (($http_data = curl_exec ($curl)) !== false)
  1284. {
  1285. $result->http_code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
  1286. $result->http_data = $http_data;
  1287. $result->http_error = null;
  1288. }
  1289. else
  1290. {
  1291. $result->http_code = -1;
  1292. $result->http_data = null;
  1293. $result->http_error = curl_error ($curl);
  1294. }
  1295.  
  1296. //Done
  1297. return $result;
  1298. }
  1299.  
  1300.  
  1301. /**
  1302. * Send an fsockopen request
  1303. */
  1304. protected static function fsockopen_request ($url, $options = array (), $timeout = 30)
  1305. {
  1306. //Store the result
  1307. $result = new stdClass ();
  1308.  
  1309. //Make that this is a valid URL
  1310. if (($uri = parse_url ($url)) == false)
  1311. {
  1312. $result->http_code = -1;
  1313. $result->http_data = null;
  1314. $result->http_error = 'invalid_uri';
  1315. return $result;
  1316. }
  1317.  
  1318. //Make sure we can handle the schema
  1319. switch ($uri ['scheme'])
  1320. {
  1321. case 'http':
  1322. $port = (isset ($uri ['port']) ? $uri ['port'] : 80);
  1323. $host = ($uri ['host'] . ($port != 80 ? ':' . $port : ''));
  1324. $fp = @fsockopen ($uri ['host'], $port, $errno, $errstr, $timeout);
  1325. break;
  1326.  
  1327. case 'https':
  1328. $port = (isset ($uri ['port']) ? $uri ['port'] : 443);
  1329. $host = ($uri ['host'] . ($port != 443 ? ':' . $port : ''));
  1330. $fp = @fsockopen ('ssl://' . $uri ['host'], $port, $errno, $errstr, $timeout);
  1331. break;
  1332.  
  1333. default:
  1334. $result->http_code = -1;
  1335. $result->http_data = null;
  1336. $result->http_error = 'invalid_schema';
  1337. return $result;
  1338. break;
  1339. }
  1340.  
  1341. //Make sure the socket opened properly
  1342. if (!$fp)
  1343. {
  1344. $result->http_code = -$errno;
  1345. $result->http_data = null;
  1346. $result->http_error = trim ($errstr);
  1347. return $result;
  1348. }
  1349.  
  1350. //Construct the path to act on
  1351. $path = (isset ($uri ['path']) ? $uri ['path'] : '/');
  1352. if (isset ($uri ['query']))
  1353. {
  1354. $path .= '?' . $uri ['query'];
  1355. }
  1356.  
  1357. //Create HTTP request
  1358. $defaults = array ();
  1359. $defaults ['Host'] = 'Host: ' . $host;
  1360. $defaults ['User-Agent'] = 'User-Agent: SocialLogin ' . self::OA_SOCIAL_LOGIN_VERSION . ' phpBB3 (+http://www.oneall.com/)';
  1361.  
  1362. // BASIC AUTH?
  1363. if (isset ($options ['api_key']) && isset ($options ['api_secret']))
  1364. {
  1365. $defaults ['Authorization'] = 'Authorization: Basic ' . base64_encode ($options ['api_key'] . ":" . $options ['api_secret']);
  1366. }
  1367.  
  1368. //Build and send request
  1369. $request = 'GET ' . $path . " HTTP/1.0\r\n";
  1370. $request .= implode ("\r\n", $defaults);
  1371. $request .= "\r\n\r\n";
  1372. fwrite ($fp, $request);
  1373.  
  1374. //Fetch response
  1375. $response = '';
  1376. while (!feof ($fp))
  1377. {
  1378. $response .= fread ($fp, 1024);
  1379. }
  1380.  
  1381. //Close connection
  1382. fclose ($fp);
  1383.  
  1384. //Parse response
  1385. list($response_header, $response_body) = explode ("\r\n\r\n", $response, 2);
  1386.  
  1387. //Parse header
  1388. $response_header = preg_split ("/\r\n|\n|\r/", $response_header);
  1389. list($header_protocol, $header_code, $header_status_message) = explode (' ', trim (array_shift ($response_header)), 3);
  1390.  
  1391. //Build result
  1392. $result->http_code = $header_code;
  1393. $result->http_data = $response_body;
  1394.  
  1395. //Done
  1396. return $result;
  1397. }
  1398. }
Add Comment
Please, Sign In to add comment