PageRenderTime 32ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/rpx/rpx_c.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1372 lines | 1255 code | 86 blank | 31 comment | 337 complexity | c145c2532d29efdcaf5af6e5a5f9d43c MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. function rpx_bootstrap() {
  3. if (defined('RPX_BOOT')) {
  4. return true;
  5. }
  6. define('RPX_PLUGIN_URL', plugins_url('rpx/', RPX_PATH_ROOT));
  7. define('RPX_IMAGE_URL', plugins_url('rpx/images/', RPX_PATH_ROOT));
  8. define('RPX_FILES_URL', plugins_url('rpx/files/', RPX_PATH_ROOT));
  9. if (get_option(RPX_REALM_SCHEME) == 'http'){
  10. $rpx_scheme = 'http://static.';
  11. }else{
  12. $rpx_scheme = 'https://';
  13. }
  14. define('RPX_URL_SCHEME', $rpx_scheme);
  15. define('RPX_BOOT', true);
  16. return RPX_BOOT;
  17. }
  18. function rpx_stylesheet() {
  19. $rpx_style_url = WP_PLUGIN_URL . '/rpx/files/stylesheet.css';
  20. $rpx_style_file = WP_PLUGIN_DIR . '/rpx/files/stylesheet.css';
  21. if ( file_exists($rpx_style_file) ) {
  22. wp_register_style('rpx_style', $rpx_style_url);
  23. wp_enqueue_style('rpx_style');
  24. }
  25. }
  26. function rpx_admin_stylesheet() {
  27. $rpx_style_url = WP_PLUGIN_URL . '/rpx/files/stylesheet.css';
  28. $rpx_style_file = WP_PLUGIN_DIR . '/rpx/files/stylesheet.css';
  29. if ( file_exists($rpx_style_file) ) {
  30. wp_register_style('rpx_style', $rpx_style_url);
  31. wp_enqueue_style('rpx_style');
  32. }
  33. }
  34. function rpx_javascript($rpx_echo=false) {
  35. $rpx_js_url = WP_PLUGIN_URL . '/rpx/files/javascript.js';
  36. $rpx_js_file = WP_PLUGIN_DIR . '/rpx/files/javascript.js';
  37. if ( file_exists($rpx_js_file) ) {
  38. wp_register_script('rpx_javascript', $rpx_js_url);
  39. wp_enqueue_script('rpx_javascript');
  40. $rpx_social_option = get_option(RPX_SOCIAL_OPTION);
  41. $rpx_social_pub = get_option(RPX_SOCIAL_PUB);
  42. if ($rpx_social_option == 'true' && !empty($rpx_social_pub) ){
  43. wp_register_script('rpx_js', RPX_URL_SCHEME.RPX_SERVER.'/js/lib/rpx.js');
  44. wp_enqueue_script('rpx_js');
  45. }
  46. }
  47. if (get_option(RPX_SHARE_COUNT_OPTION) == 'hover') {
  48. $rpx_jq_url = WP_PLUGIN_URL . '/rpx/files/javascript-jquery.js';
  49. $rpx_jq_file = WP_PLUGIN_DIR . '/rpx/files/javascript-jquery.js';
  50. if ( file_exists($rpx_jq_file) ) {
  51. wp_register_script('rpx_javascript_jq', $rpx_jq_url);
  52. wp_enqueue_script('rpx_javascript_jq');
  53. }
  54. }
  55. }
  56. function rpx_test_api(){
  57. /* example successful test array
  58. test {
  59. [curl] = true,
  60. [curl_ssl] = true,
  61. [wp_http] = true,
  62. [php_ssl] = true,
  63. [api_tested] = true,
  64. [post] = true,
  65. [ssl_valid] = true,
  66. [api] = true,
  67. [select] = wp_html
  68. }
  69. the select value is one of the following
  70. 'wp_http'
  71. 'curl'
  72. false
  73. */
  74. $test = array();
  75. //curl test
  76. if (function_exists('curl_version')){
  77. $test['curl'] = true;
  78. $curl = curl_version();
  79. if ( !empty($curl['ssl_version']) ){
  80. $test['curl_ssl'] = true;
  81. }else{
  82. $test['curl_ssl'] = false;
  83. }
  84. }else{
  85. $test['curl'] = false;
  86. }
  87. //wp http test
  88. if (function_exists('wp_remote_get')){
  89. $test['wp_http'] = true;
  90. $xports = stream_get_transports();
  91. if (in_array('ssl',$xports)){
  92. $test['php_ssl'] = true;
  93. }else{
  94. $test['php_ssl'] = false;
  95. }
  96. }else{
  97. $test['wp_http'] = false;
  98. }
  99. //jr api test
  100. if ($test['php_ssl'] === true || $test['curl_ssl'] === true){
  101. $test['api_tested'] = true;
  102. $rpx_post_array = array('apiKey' => 'JanrainEngagePluginForWordpress','format' => 'json');
  103. if ($rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array,true,true) !== false){//test with ssl validation
  104. update_option(RPX_SSL_VALID_OPTION, 'true');
  105. $test['ssl_valid'] = true;
  106. $test['post'] = true;
  107. }elseif ($rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array,false,true) !== false){//test without ssl validation
  108. update_option(RPX_SSL_VALID_OPTION, 'false');
  109. $test['ssl_valid'] = false;
  110. $test['post'] = true;
  111. }else{
  112. $test['post'] = false;
  113. $test['ssl_valid'] = false;
  114. }
  115. if ($rpx_reply == 'No RP found'){
  116. $test['api'] = true;/*in this case getting the error proves connectivity*/
  117. }else{
  118. $test['api'] = false;
  119. }
  120. }else{
  121. $test['api_tested'] = false;
  122. }
  123. //select http method
  124. $http_option = get_option(RPX_HTTP_OPTION);
  125. if (empty($http_option)) {
  126. $http_option = false;
  127. }
  128. if ($test['api_tested'] === true && $http_option === false){
  129. if ($test['wp_http'] === true){
  130. update_option(RPX_HTTP_OPTION, 'wp_http');
  131. $test['select'] = 'wp_http';
  132. }else{
  133. update_option(RPX_HTTP_OPTION, 'curl');
  134. $test['select'] = 'curl';
  135. }
  136. }else{
  137. $test['select'] = $http_option;
  138. }
  139. return $test;
  140. }
  141. function rpx_configured(){
  142. rpx_bootstrap();
  143. $required_options = array(
  144. RPX_API_KEY_OPTION => 'apiKey',
  145. RPX_REALM_OPTION => 'realm',
  146. RPX_REALM_SCHEME => 'realmScheme',
  147. RPX_ADMIN_URL_OPTION => 'adminUrl'
  148. );
  149. foreach($required_options as $key => $val){
  150. $option = get_option($key);
  151. if ( empty($option) || $option === false){
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. function rpx_allow_register(){
  158. if (get_option('users_can_register') == 1 && get_option(RPX_AUTOREG_OPTION) == 'true'){
  159. return true;
  160. }
  161. return false;
  162. }
  163. function rpx_user_data(){
  164. if (is_user_logged_in() == true){
  165. global $current_user;
  166. return $current_user;
  167. }
  168. return false;
  169. }
  170. function rpx_bp_init(){
  171. define('RPX_BP_ACTIVE', true);
  172. }
  173. function rpx_redirect($redirect_to=''){
  174. if ( empty($redirect_to) ){
  175. $url = RPX_DEFAULT_REDIRECT;
  176. }else{
  177. $url = $redirect_to;
  178. }
  179. wp_safe_redirect($url);
  180. exit;
  181. }
  182. function rpx_register_widget(){
  183. return register_widget('RPX_Widget');
  184. }
  185. function rpx_admin_menu(){
  186. add_utility_page(RPX_OPTIONS_TITLE, RPX_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_MENU_SLUG, 'rpx_admin_menu_view', WP_PLUGIN_URL.RPX_IMAGE_PATH.'janrain_icon_small.png');
  187. add_submenu_page(RPX_MENU_SLUG, RPX_OPTIONS_TITLE, RPX_MENU_MAIN, RPX_OPTIONS_ROLE, RPX_MENU_SLUG, 'rpx_admin_menu_view');
  188. add_submenu_page(RPX_MENU_SLUG, RPX_STRING_OPTIONS_TITLE, RPX_STRING_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_STRING_MENU_SLUG, 'rpx_admin_string_menu_view');
  189. add_submenu_page(RPX_MENU_SLUG, RPX_HELP_OPTIONS_TITLE, RPX_HELP_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_HELP_MENU_SLUG, 'rpx_admin_help_menu_view');
  190. add_submenu_page(RPX_MENU_SLUG, RPX_ADVANCED_OPTIONS_TITLE, RPX_ADVANCED_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_ADVANCED_MENU_SLUG, 'rpx_admin_advanced_menu_view');
  191. add_action( 'admin_init', 'rpx_admin_menu_register' );
  192. return true;
  193. }
  194. function rpx_admin_menu_register(){
  195. register_setting( 'rpx_settings_group', RPX_API_KEY_OPTION, 'rpx_process_api_key' );
  196. register_setting( 'rpx_settings_group', RPX_VEMAIL_OPTION, 'rpx_process_bool' );
  197. register_setting( 'rpx_settings_group', RPX_COMMENT_OPTION, 'rpx_process_clog' );
  198. register_setting( 'rpx_settings_group', RPX_SOCIAL_OPTION, 'rpx_process_bool' );
  199. register_setting( 'rpx_settings_group', RPX_SOCIAL_COMMENT_OPTION, 'rpx_process_bool' );
  200. register_setting( 'rpx_settings_group', RPX_S_LOC_OPTION, 'rpx_process_sloc' );
  201. register_setting( 'rpx_settings_group', RPX_AUTOREG_OPTION, 'rpx_process_bool' );
  202. register_setting( 'rpx_settings_group', RPX_VERIFYNAME_OPTION, 'rpx_process_bool' );
  203. register_setting( 'rpx_settings_group', RPX_AVATAR_OPTION, 'rpx_process_bool' );
  204. register_setting( 'rpx_settings_group', RPX_S_STYLE_OPTION, 'rpx_process_sstyle' );
  205. register_setting( 'rpx_settings_group', RPX_S_TXT_OPTION, 'rpx_process_txt' );
  206. register_setting( 'rpx_settings_group', RPX_PARAMS_OPTION, 'rpx_process_params' );
  207. register_setting( 'rpx_settings_group', RPX_REMOVABLE_OPTION, 'rpx_process_bool' );
  208. register_setting( 'rpx_settings_group', RPX_SHARE_COUNT_OPTION, 'rpx_process_shct' );
  209. register_setting( 'rpx_string_settings_group', RPX_STRINGS_OPTION, 'rpx_process_strings' );
  210. register_setting( 'rpx_advanced_settings_group', RPX_ADVANCED_OPTION, 'rpx_process_strings' );
  211. return true;
  212. }
  213. function rpx_process_bool($bool){
  214. if ($bool == 'true' || $bool == 'false'){
  215. return $bool;
  216. }else{
  217. return 'false';
  218. }
  219. }
  220. function rpx_process_sloc($sloc){
  221. if ($sloc == 'top' || $sloc == 'bottom' || $sloc == 'none'){
  222. return $sloc;
  223. }else{
  224. return 'none';
  225. }
  226. }
  227. function rpx_process_shct($shct){
  228. if ($shct == 'always' || $shct == 'hover' || $shct == 'false'){
  229. return $shct;
  230. }else{
  231. return 'false';
  232. }
  233. }
  234. function rpx_process_sstyle($sstyle){
  235. if ($sstyle == 'icon' || $sstyle == 'label'){
  236. return $sstyle;
  237. }else{
  238. return 'none';
  239. }
  240. }
  241. function rpx_process_clog($clog){
  242. global $rpx_comment_actions;
  243. if (in_array($clog, $rpx_comment_actions)){
  244. return $clog;
  245. }
  246. return false;
  247. }
  248. function rpx_process_txt($txt){
  249. $clean = strip_tags($txt);
  250. if ($txt === $clean){
  251. return $txt;
  252. }else{
  253. return $clean;
  254. }
  255. }
  256. function rpx_process_params($params){
  257. if ($params === '') {
  258. return $params;
  259. }
  260. if ($params !== trim($params,'&')) {
  261. return false;
  262. }
  263. if ($params !== str_replace(' ','',$params)) {
  264. return false;
  265. }
  266. if ($params !== strip_tags($params)) {
  267. return false;
  268. }
  269. $pairs = explode('&', $params);
  270. if ($pairs[0] === $params) {
  271. if (strstr($params,'=') === false) {
  272. return false;
  273. }
  274. if (strpos($params,'=') !== strrpos($params,'=')) {
  275. return false;
  276. }
  277. return $params;
  278. }
  279. $param_array = array();
  280. foreach ($pairs as $key=>$val) {
  281. if (strstr($val,'=') === false) {
  282. return false;
  283. }
  284. if (strpos($val,'=') !== strrpos($val,'=')) {
  285. return false;
  286. }
  287. }
  288. return $params;
  289. }
  290. function rpx_process_strings($strings){
  291. if ( is_array($strings) ) {
  292. return $strings;
  293. }
  294. return false;
  295. }
  296. function rpx_get_comment_option(){
  297. $rpx_comment_option = get_option(RPX_COMMENT_OPTION);
  298. if ( empty($rpx_comment_option) ){
  299. return RPX_COMMENT_OPTION_DEFAULT;
  300. }
  301. return $rpx_comment_option;
  302. }
  303. function rpx_update_options($rpx_api_key){
  304. if ($rpx_rp = rpx_get_rp($rpx_api_key)){
  305. update_option(RPX_REALM_OPTION, $rpx_rp['realm']);
  306. update_option(RPX_REALM_SCHEME, $rpx_rp['realmScheme']);
  307. update_option(RPX_APP_ID_OPTION, $rpx_rp['appId']);
  308. update_option(RPX_ADMIN_URL_OPTION, $rpx_rp['adminUrl']);
  309. update_option(RPX_SOCIAL_PUB, $rpx_rp['socialPub']);
  310. update_option(RPX_PROVIDERS_OPTION, $rpx_rp['signinProviders']);
  311. return true;
  312. }
  313. rpx_message('API key failed test.', 'error');
  314. return false;
  315. }
  316. function rpx_process_api_key($rpx_api_key){
  317. $rpx_api_key = strip_tags($rpx_api_key);
  318. rpx_update_options($rpx_api_key);
  319. return $rpx_api_key;
  320. }
  321. function rpx_get_rp($rpx_api_key){
  322. if (strlen($rpx_api_key) == 40){
  323. $rpx_post_array = array('apiKey' => $rpx_api_key,'pluginName' => RPX_PLUGIN_NAME,'pluginVersion' => RPX_PLUGIN_VERSION, 'format' => 'json');
  324. if ($rpx_json = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array)){
  325. $rpx_rp = json_decode($rpx_json,true);
  326. if ($rpx_rp['apiKey'] == $rpx_api_key){
  327. return $rpx_rp;
  328. }
  329. }
  330. rpx_message('Unable to validate API key. Please verify your PHP CURL version.', 'error');
  331. }
  332. return false;
  333. }
  334. function rpx_post($url,$post_data,$ssl=NULL,$track=false){
  335. if (get_option(RPX_SSL_VALID_OPTION) == 'false' && $ssl === NULL){
  336. $ssl = false;
  337. }else{
  338. $ssl = true;
  339. }
  340. if ($track === true){
  341. $user_agent = 'Janrain_Engage_Wordpress_Plugin';
  342. }else{
  343. $user_agent = 'Wordpress';
  344. }
  345. if (function_exists('wp_remote_get') && get_option(RPX_HTTP_OPTION) !== 'curl'){
  346. $headers = array('Referer'=>get_bloginfo('url'));
  347. $wp_get_args = array(
  348. 'method' => 'GET',
  349. 'timeout' => 5,
  350. 'redirection' => 5,
  351. 'user-agent' => $user_agent,
  352. 'blocking' => true,
  353. 'compress' => true,
  354. 'decompress' => true,
  355. 'sslverify' => $ssl,
  356. 'headers' => $headers
  357. );
  358. $parms = array();
  359. foreach ($post_data as $key => $val){
  360. $parms[] = urlencode($key).'='.urlencode($val);
  361. }
  362. $parms = implode('&',$parms);
  363. if ( !empty($parms) ){
  364. $wp_get_url = $url.'?'.$parms;
  365. $wp_get = @wp_remote_get($wp_get_url,$wp_get_args);
  366. if (is_wp_error($wp_get)){
  367. update_option(RPX_HTTP_OPTION, '');
  368. rpx_message('WP_HTTP error:"'.serialize($wp_get).'"', 'message');
  369. }else{
  370. update_option(RPX_HTTP_OPTION, 'wp_http');
  371. return $wp_get["body"];
  372. }
  373. }else{
  374. rpx_message('WP_HTTP error:"Parameters missing"', 'error');
  375. return false;
  376. }
  377. }
  378. if (function_exists('curl_init') && get_option(RPX_HTTP_OPTION) !== 'wp_http'){
  379. $ch = curl_init();
  380. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  381. curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
  382. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  383. curl_setopt($ch, CURLOPT_URL, $url);
  384. curl_setopt($ch, CURLOPT_POST, true);
  385. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  386. curl_setopt($ch, CURLOPT_HEADER, false);
  387. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  388. if ($ssl === true){
  389. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  390. }
  391. $post_result = @curl_exec($ch);
  392. $curl_error = curl_error($ch);
  393. curl_close($ch);
  394. if ( empty($curl_error) ){
  395. update_option(RPX_HTTP_OPTION, 'curl');
  396. return $post_result;
  397. }
  398. update_option(RPX_HTTP_OPTION, '');
  399. rpx_message('CURL error:"'.$curl_error.'"', 'error');
  400. }else{
  401. update_option(RPX_HTTP_OPTION, '');
  402. rpx_message('CURL support not found.', 'error');
  403. }
  404. update_option(RPX_HTTP_OPTION, '');
  405. rpx_message('No supported HTTP access found.', 'error');
  406. return false;
  407. }
  408. /**
  409. * Remove the current user's Engage usermeta.
  410. */
  411. function rpx_remove_usermeta(){
  412. global $rpx_http_vars;
  413. global $current_user;
  414. if ( $rpx_http_vars['action'] != RPX_REMOVE_ACTION ) {
  415. return false;
  416. }
  417. $removable = get_option(RPX_REMOVABLE_OPTION);
  418. if($removable !== 'true'){
  419. return false;
  420. }
  421. if ( $current_user->id === '' || $current_user->id === null ) {
  422. return false;
  423. }
  424. $user_id = $current_user->id;
  425. delete_user_meta($user_id, RPX_META_IDENTIFIER);
  426. delete_user_meta($user_id, RPX_META_PROVIDER);
  427. delete_user_meta($user_id, RPX_META_LOCKED);
  428. delete_user_meta($user_id, RPX_META_SESSION);
  429. delete_user_meta($user_id, RPX_META_URL);
  430. delete_user_meta($user_id, RPX_META_PHOTO);
  431. delete_user_meta($user_id, RPX_META_PROFILE);
  432. delete_user_meta($user_id, RPX_META_CONTACTS);
  433. wp_safe_redirect(get_edit_profile_url($user_id));
  434. }
  435. function rpx_process_token(){
  436. global $rpx_http_vars;
  437. global $rpx_auth_info;
  438. if ( $rpx_http_vars['action'] == RPX_TOKEN_ACTION && !empty($rpx_http_vars['token']) ){
  439. if (RPX_AUTH_INFO_EXTENDED === 'true') {
  440. $extended = 'true';
  441. } else {
  442. $extended = 'false';
  443. }
  444. $post_data = array(
  445. 'token' => $rpx_http_vars['token'],
  446. 'apiKey' => get_option(RPX_API_KEY_OPTION),
  447. 'extended' => $extended,
  448. 'format' => 'json'
  449. );
  450. if ($rpx_response = rpx_post(RPX_URL_SCHEME.RPX_SERVER.RPX_API_PATH.'auth_info', $post_data)){
  451. if ($rpx_auth_info = json_decode($rpx_response,true)){
  452. if (rpx_new_profile($rpx_auth_info)){
  453. if ( !empty($rpx_http_vars['rpx_username']) ){
  454. global $rpx_wp_profile;
  455. $rpx_wp_profile['rpx_username'] = $rpx_http_vars['rpx_username'];
  456. }
  457. return rpx_process_user(rpx_test_wp_user());
  458. }
  459. }
  460. }
  461. }
  462. rpx_message(rpx_get_message($rpx_auth_info));
  463. return false;
  464. }
  465. function rpx_process_session(){
  466. global $rpx_wp_profile;
  467. global $rpx_http_vars;
  468. if ( !empty($rpx_http_vars['rpx_session']) ){
  469. if (rpx_session_identifier() === true){
  470. if (strlen($rpx_http_vars['rpx_email']) > 5){
  471. $rpx_wp_profile['rpx_email'] = $rpx_http_vars['rpx_email'];
  472. }
  473. return rpx_process_user(rpx_test_wp_user());
  474. }
  475. }
  476. rpx_message('unable to process session', 'error');
  477. return false;
  478. }
  479. function rpx_process_user($action){/*Using a switch for visual clarity, this may be cleaned up when conditionals are no longer in flux.*/
  480. if (RPX_VERBOSE == 'true'){
  481. error_log('WP-RPX '.$action);/*This will enter every RPX action into your php log (usually apache error log).*/
  482. }
  483. switch ($action){
  484. case 'signon':
  485. rpx_signon_wp_user();
  486. return true;
  487. break;
  488. case 'create':
  489. $create = rpx_create_wp_user();
  490. if ($create !== false) $create = rpx_signon_wp_user();
  491. if ($create !== false) return true;
  492. rpx_message('create failure', 'debug');
  493. rpx_process_user('regdirect');
  494. return false;
  495. break;
  496. case 'vemail':
  497. rpx_get_vemail_id();
  498. rpx_update_wp_user();
  499. rpx_signon_wp_user();
  500. return true;
  501. break;
  502. case 'engage':
  503. rpx_add_engage();
  504. rpx_update_wp_user();
  505. rpx_signon_wp_user();
  506. return true;
  507. break;
  508. case 'register':
  509. $register = rpx_unlock_user();
  510. if ($register !== false) $register = rpx_new_session();
  511. if ($register !== false) $register = rpx_update_wp_user(true,true);
  512. if ($register !== false) $register = rpx_signon_wp_user();
  513. if ($register !== false) return true;
  514. rpx_message('register failure', 'debug');
  515. rpx_process_user('regdirect');
  516. return false;
  517. break;
  518. case 'regdirect':
  519. rpx_redirect(rpx_get_reg_url());
  520. break;
  521. case 'getemail':
  522. $getemail = rpx_lock_user();
  523. if ($getemail !== false) $getemail = rpx_new_session();
  524. if ($getemail !== false) $getemail = rpx_placeholder_email();
  525. if ($getemail !== false) $getemail = rpx_create_wp_user();
  526. if ($getemail !== false) $getemail = rpx_register_wp_user();
  527. if ($getemail !== false) return true;
  528. rpx_message('getemail failure', 'debug');
  529. rpx_process_user('regdirect');
  530. return false;
  531. break;
  532. case 'retryemail':
  533. $retryemail = rpx_lock_user();
  534. if ($retryemail !== false) $retryemail = rpx_new_session();
  535. if ($retryemail !== false) $retryemail = rpx_placeholder_email();
  536. if ($retryemail !== false) $retryemail = rpx_update_wp_user(true,true);
  537. if ($retryemail !== false) $retryemail = rpx_register_wp_user();
  538. if ($retryemail !== false) return true;
  539. rpx_message('retryemail failure', 'debug');
  540. return false;
  541. break;
  542. case 'getuser':
  543. rpx_register_wp_user('username');
  544. return true;
  545. break;
  546. case 'error':
  547. add_action('wp_footer','rpx_message_box',20);
  548. return false;
  549. break;
  550. }
  551. rpx_message('User action unmatched.', 'error');
  552. return false;
  553. }
  554. function rpx_test_wp_user(){
  555. global $rpx_wp_profile;
  556. global $rpx_http_vars;
  557. $tests = array();
  558. rpx_message('user processing begin', 'debug');
  559. /*The tests all assume this is an Engage auth so the id is required.*/
  560. if ( empty($rpx_wp_profile['rpx_identifier']) ){
  561. rpx_message('Empty identifier.', 'error');
  562. return 'error';
  563. }
  564. /*Sequential state tests, boolean*/
  565. $user_data = rpx_user_data();
  566. if ($user_data === false){
  567. $active_user = false;
  568. }else{
  569. $active_user = true;
  570. }
  571. $tests['active_user'] = $active_user;
  572. if ( empty($rpx_wp_profile['rpx_wp_id']) ){
  573. if(rpx_get_wpid() === true){
  574. $rpx_match = true;
  575. rpx_get_meta();
  576. }else{
  577. $rpx_match = false;
  578. }
  579. }else{
  580. if ($rpx_wp_profile['rpx_wp_id'] > 1 && $rpx_wp_profile['rpx_wp_id'] != '1'){
  581. $rpx_match = true;
  582. rpx_get_meta();
  583. }else{
  584. $rpx_match = false;
  585. }
  586. }
  587. $tests['rpx_match'] = $rpx_match;
  588. if ( empty($rpx_wp_profile['rpx_locked']) ) {
  589. if ($rpx_match === false){
  590. $rpx_wp_profile['rpx_locked'] = false;
  591. }else{
  592. $rpx_wp_profile['rpx_locked'] = true;
  593. }
  594. }
  595. if ($rpx_wp_profile['rpx_locked'] == 'true'){
  596. $rpx_locked = true;
  597. }else{
  598. $rpx_wp_profile['rpx_locked'] = 'false';
  599. $rpx_locked = false;
  600. }
  601. $tests['rpx_locked'] = $rpx_locked;
  602. if ( empty($rpx_wp_profile['rpx_verifiedEmail']) ){
  603. $rpx_verified_email = false;
  604. if ( empty($rpx_wp_profile['rpx_email']) ){
  605. $rpx_email = false;
  606. }else{
  607. $rpx_email = true;
  608. }
  609. }else{
  610. $rpx_verified_email = true;
  611. $rpx_email = true;
  612. }
  613. $tests['rpx_verified_email'] = $rpx_verified_email;
  614. $tests['rpx_email'] = $rpx_email;
  615. if ( empty($rpx_wp_profile['rpx_username']) ) {
  616. $rpx_wp_profile['rpx_username'] = '';
  617. }
  618. if (strlen(strip_tags($rpx_wp_profile['rpx_username'])) > 1){
  619. $rpx_username = true;
  620. $rpx_wp_username_id = username_exists($rpx_wp_profile['rpx_username']);
  621. if ($rpx_wp_username_id === false || $rpx_wp_username_id === NULL){//Ok who ruturns NULL? Seriously!
  622. $username_match = false;
  623. }else{
  624. $username_match = true;
  625. }
  626. }else{
  627. $rpx_username = false;
  628. $username_match = false;
  629. }
  630. $tests['rpx_username'] = $rpx_username;
  631. $tests['username_match'] = $username_match;
  632. $email_match = false;
  633. if (strlen(strip_tags($rpx_wp_profile['rpx_email'])) > 5){
  634. $wp_email_id = email_exists($rpx_wp_profile['rpx_email']);
  635. if ($wp_email_id != false){
  636. $email_found = true;
  637. $rpx_wp_profile['user_email'] = $rpx_wp_profile['rpx_email'];
  638. if ( !empty($rpx_wp_profile['rpx_wp_id']) ) {
  639. if ( $rpx_wp_profile['rpx_wp_id'] == $wp_email_id){
  640. $email_match = true;
  641. }
  642. }
  643. }else{
  644. $email_found = false;
  645. }
  646. }else{
  647. $email_found = false;
  648. }
  649. $tests['email_found'] = $email_found;
  650. $tests['email_match'] = $email_match;
  651. $wptest = rpx_validate_user();
  652. if ($wptest === false){
  653. $rpx_valid = false;
  654. }else{
  655. $rpx_valid = true;
  656. }
  657. $tests['rpx_valid'] = $rpx_valid;
  658. if (rpx_allow_register() === true){
  659. $autoreg = true;
  660. }else{
  661. $autoreg = false;
  662. }
  663. $tests['autoreg'] = $autoreg;
  664. /*End of sequential tests*/
  665. //var_dump($tests); exit;//expert debug point
  666. /*Sequential conditions for action*/
  667. if ($rpx_match === true && $rpx_locked === false){
  668. return 'signon';
  669. }
  670. if ($active_user === true && $rpx_locked === false){
  671. return 'engage';
  672. }
  673. if ($rpx_match === true && $rpx_locked === true && ($rpx_email === false || $email_found === true || $rpx_valid === false)){
  674. return 'retryemail';
  675. }
  676. if ($rpx_match === true && $rpx_locked === true && $rpx_email === true && $email_found === false){
  677. return 'register';
  678. }
  679. if ($email_found === true && $rpx_verified_email === true && $rpx_locked === false && get_option(RPX_VEMAIL_OPTION) == 'true'){
  680. return 'vemail';
  681. }
  682. if ($autoreg === true && $rpx_match === false && $rpx_email === true && $email_found === false && $rpx_username === true && $username_match === false){
  683. return 'create';
  684. }
  685. if ($autoreg === false && $rpx_match === false){
  686. return 'regdirect';
  687. }
  688. if ($autoreg === true && $rpx_match === false && ($rpx_username === false || $username_match === true)){
  689. return 'getuser';
  690. }
  691. if ($autoreg === true && $rpx_match === false && ($rpx_email === false || $email_found === true)){
  692. return 'getemail';
  693. }
  694. /*Conditions for error action*/
  695. if ($email_found === true){
  696. rpx_message('The email address '.$rpx_wp_profile['rpx_email'].' is already registered with another account.', 'message');
  697. }
  698. if ($rpx_match === true && $rpx_locked === true && $email_found === false){
  699. rpx_message('Session ID does not match. Unable to unlock unverified account. Contact site admin to reset the account for "'.$rpx_wp_profile['rpx_username'].'"', 'message');
  700. }
  701. rpx_message('user processing end', 'debug');
  702. return 'error';
  703. }
  704. function rpx_create_wp_user(){
  705. global $rpx_wp_profile;
  706. global $rpx_wp_user;
  707. rpx_new_wp_user();
  708. if ($rpx_wp_user['user_pass'] = wp_generate_password( 12, false )){
  709. $insert_user = wp_insert_user($rpx_wp_user);
  710. if (is_wp_error($insert_user)) {
  711. rpx_message('WP insert user fail', 'debug');
  712. return false;
  713. }
  714. $rpx_wp_profile['rpx_wp_id'] = $insert_user;
  715. if ($rpx_wp_profile['rpx_wp_id'] != false && $rpx_wp_profile['rpx_wp_id'] != 1){
  716. if (rpx_update_meta()){
  717. if (RPX_REQUIRE_EULA == 'true') {
  718. rpx_eula_user();
  719. }
  720. return true;
  721. }
  722. }
  723. }
  724. rpx_message('Create user failed.', 'error');
  725. return false;
  726. }
  727. function rpx_get_vemail_id(){
  728. global $rpx_wp_profile;
  729. if (get_option(RPX_VEMAIL_OPTION) == 'true' && !empty($rpx_wp_profile['rpx_verifiedEmail']) ){
  730. $rpx_wp_profile['rpx_wp_id'] = email_exists($rpx_wp_profile['rpx_verifiedEmail']);
  731. return true;
  732. }
  733. return false;
  734. }
  735. function rpx_update_wp_user($force_email=false,$force_reg=false){
  736. global $rpx_wp_profile;
  737. global $rpx_wp_user;
  738. $user_data = rpx_user_data();
  739. if ( $user_data !== false ) {
  740. if ($user_data->id != $rpx_wp_profile['rpx_wp_id'] && $force_reg === false){
  741. rpx_message('ruwu user id '.$user_data->id.'!='.$rpx_wp_profile['rpx_wp_id'], 'debug');
  742. return false;
  743. }
  744. }
  745. $rpx_wp_user['ID'] = $rpx_wp_profile['rpx_wp_id'];
  746. $rpx_wp_user['id'] = $rpx_wp_profile['rpx_wp_id'];
  747. if (!empty($rpx_wp_profile['rpx_provider'])){
  748. $rpx_wp_user['rpx_provider'] = $rpx_wp_profile['rpx_provider'];
  749. }
  750. if (!empty($rpx_wp_profile['rpx_url'])){
  751. $rpx_wp_user['rpx_url'] = $rpx_wp_profile['rpx_url'];
  752. }
  753. if (!empty($rpx_wp_profile['rpx_photo'])){
  754. $rpx_wp_user['rpx_photo'] = $rpx_wp_profile['rpx_photo'];
  755. }
  756. if (!empty($user_data->email) && $force_email === false){
  757. $rpx_wp_user['user_email'] = $user_data->email;
  758. }elseif (!empty($rpx_wp_profile['rpx_email'])){
  759. $rpx_wp_user['user_email'] = $rpx_wp_profile['rpx_email'];
  760. }
  761. if (!empty($user_data->user_url)){
  762. $rpx_wp_user['user_url'] = $user_data->user_url;
  763. }elseif (!empty($rpx_wp_profile['rpx_url'])){
  764. $rpx_wp_user['user_url'] = $rpx_wp_profile['rpx_url'];
  765. }
  766. if ($rpx_wp_profile['rpx_wp_id'] = wp_update_user($rpx_wp_user)){
  767. if (rpx_update_meta()){
  768. return true;
  769. }
  770. }
  771. rpx_message('Update user failed.', 'error');
  772. return false;
  773. }
  774. function rpx_add_engage(){
  775. global $rpx_wp_profile;
  776. $user_data = rpx_user_data();
  777. if ($user_data === false || empty($user_data->id)){
  778. rpx_message('Add Engage failed.', 'error');
  779. return false;
  780. }
  781. $rpx_wp_profile['rpx_wp_id'] = $user_data->id;
  782. }
  783. function rpx_register_wp_user($collect='email'){
  784. global $rpx_wp_profile;
  785. global $rpx_http_vars;
  786. global $rpx_wp_user_map;
  787. foreach ($rpx_wp_user_map as $key => $val){
  788. if ( empty($rpx_wp_profile[$key]) && !empty($rpx_http_vars[$key]) ){
  789. $rpx_wp_profile[$key] = $rpx_http_vars[$key];
  790. }
  791. }
  792. if ($collect == 'email'){
  793. $rpx_email = urlencode($rpx_http_vars['rpx_email']);
  794. } else {
  795. $rpx_email = urlencode($rpx_wp_profile['rpx_email']);
  796. }
  797. if ( !empty($rpx_wp_profile['user_email']) ){
  798. $rpx_http_vars['user_email'] = $rpx_wp_profile['user_email'];
  799. }
  800. if ( !empty($rpx_wp_profile['user_name']) ){
  801. $rpx_http_vars['user_name'] = $rpx_wp_profile['user_name'];
  802. }
  803. rpx_set_redirect();
  804. $reg_url = $rpx_http_vars['redirect_to'];
  805. $anchor = strstr($reg_url,'#');
  806. if ($anchor !== false){
  807. $reg_url = str_replace($anchor,'',$reg_url);//strip any anchor tag
  808. }
  809. if (strstr($reg_url, '?') === false){
  810. $connect = '?';
  811. }else{
  812. $connect = '&';
  813. }
  814. $url = $reg_url.$connect.'action='.RPX_REGISTER_FORM_ACTION.
  815. '&rpx_session='.urlencode($rpx_wp_profile['rpx_session']).
  816. '&rpx_username='.urlencode($rpx_wp_profile['rpx_username']).
  817. '&rpx_provider='.urlencode($rpx_wp_profile['rpx_provider']).
  818. '&rpx_email='.urlencode($rpx_email).
  819. '&redirect_to='.urlencode($rpx_http_vars['redirect_to']).
  820. '&rpx_collect='.$collect;
  821. if ( !empty($rpx_http_vars['user_email']) ){
  822. $url .= '&user_email='.urlencode($rpx_http_vars['user_email']);
  823. }
  824. if ( !empty($rpx_http_vars['user_name']) ){
  825. $url .= '&user_name='.urlencode($rpx_http_vars['user_name']);
  826. }
  827. rpx_redirect($url);
  828. return true;
  829. }
  830. function rpx_get_reg_url(){
  831. $reg_url = site_url().'/';
  832. if (!defined('RPX_BP_ACTIVE')) {
  833. define ('RPX_BP_ACTIVE', false);
  834. }
  835. if (RPX_BP_ACTIVE === true){
  836. $reg_url .= RPX_BP_REG_PATH;
  837. }else{
  838. $reg_url .= RPX_WP_REG_PATH;
  839. }
  840. return $reg_url;
  841. }
  842. function rpx_reset_session(){
  843. global $rpx_wp_profile;
  844. global $rpx_http_vars;
  845. $rpx_wp_profile['rpx_session'] = $rpx_http_vars['rpx_session'];
  846. $rpx_wp_profile['rpx_username'] = $rpx_http_vars['rpx_username'];
  847. $rpx_wp_profile['rpx_provider'] = $rpx_http_vars['rpx_provider'];
  848. return true;
  849. }
  850. function rpx_register() {
  851. global $rpx_http_vars;
  852. if ($rpx_http_vars['action'] != RPX_REGISTER_FORM_ACTION){
  853. return true;
  854. }
  855. if ($rpx_http_vars['rpx_collect'] == 'email'){
  856. if ( !empty($rpx_http_vars['rpx_session']) ){
  857. if ( !empty($rpx_http_vars['user_email']) ){
  858. rpx_message($rpx_http_vars['user_email']."\n".'The email address is already in use. '."\n".'Use another email address or login to that account.', 'rpxmessage');
  859. }else{
  860. if ( empty($rpx_http_vars['rpx_email']) ){
  861. rpx_message('This '.$rpx_http_vars['rpx_provider'].' account did not provide an email address. '."\n".'Enter a valid email address to register this account.','rpxmessage');
  862. }else{
  863. $wptest = rpx_validate_user($rpx_http_vars['rpx_email']);
  864. if ($wptest === false){
  865. rpx_message('The email address entered is not valid. '."\n".'Enter a valid email address to register this account.', 'rpxmessage');
  866. }else{
  867. global $rpx_wp_profile;
  868. $rpx_wp_profile['rpx_email'] = $rpx_http_vars['rpx_email'];
  869. return rpx_process_session();
  870. }
  871. }
  872. }
  873. }
  874. }
  875. if ($rpx_http_vars['rpx_collect'] == 'username'){
  876. $eula = true;
  877. if (RPX_REQUIRE_EULA == 'true') {
  878. if ($rpx_http_vars['rpx_eula'] != 'eula') {
  879. $eula = false;
  880. $rpx_http_vars['rpx_username'] = '';
  881. }
  882. }
  883. if ( !empty($rpx_http_vars['rpx_username']) && $eula === true ){
  884. $wptest = rpx_validate_user($rpx_http_vars['rpx_email'],$rpx_http_vars['rpx_username']);
  885. $user_login_result = get_user_by('login', $rpx_http_vars['rpx_username']);
  886. if (!is_object($user_login_result)) {
  887. $wptest = true;
  888. }
  889. if ($wptest === true){
  890. add_action('wp_footer','rpx_open_widget',12);
  891. $rpx_http_vars['action'] = '';
  892. return true;
  893. }
  894. }
  895. if ( !empty($rpx_http_vars['rpx_username']) ){
  896. $username = $rpx_http_vars['rpx_username'];
  897. $message = '"'.$username.'"'."\n".RPX_NAME_EXISTS_REASON."\n".RPX_NAME_PROMPT;
  898. }else{
  899. $message = RPX_NAME_PROMPT;
  900. }
  901. rpx_message($message, 'rpxmessage');
  902. }
  903. }
  904. function rpx_signon_wp_user(){
  905. global $current_user;
  906. global $rpx_wp_profile;
  907. global $rpx_http_vars;
  908. global $rpx_auth_info;
  909. $user = rpx_wp_signon();
  910. if ($user != false && $user->ID != false && $user->ID != 0 && !empty($user->ID) ){
  911. $current_user = new WP_User($user->ID, $user->user_login && false);
  912. $current_user = wp_get_current_user();
  913. if ($user->ID == $current_user->id){
  914. if (RPX_SERIAL_PROFILE == 'true') {
  915. rpx_update_user_meta($current_user->id, RPX_META_PROFILE, $rpx_auth_info);
  916. }
  917. if (RPX_GET_CONTACTS == 'true') {
  918. global $rpx_contacts;
  919. $rpx_contacts = rpx_get_contacts();
  920. }
  921. $remember = false;
  922. if (RPX_REMEMBER_WP_SIGNON == 'true'){
  923. $remember = true;
  924. }
  925. wp_set_auth_cookie($current_user->id, $remember);
  926. do_action('wp_login', $user->user_login);
  927. rpx_set_redirect();
  928. rpx_redirect($rpx_http_vars['redirect_to']);
  929. return true;
  930. }else{
  931. error_log('Janrain Engage Wordpress user mismatch '.$user->ID.'!='.$current_user->id);
  932. return false;
  933. }
  934. }else{
  935. rpx_message('Unable to sign on as '.$rpx_wp_profile['rpx_username'].'.', 'error');
  936. return false;
  937. }
  938. }
  939. function rpx_get_contacts($user_id='') {
  940. $get_contacts_providers = array ('google', 'live_id', 'facebook', 'myspace', 'twitter', 'linkedin', 'yahoo');
  941. if (empty($user_id)) {
  942. $user_id = get_current_user_id();
  943. }
  944. if (!empty($user_id)) {
  945. $provider = get_user_meta($user_id, 'rpx_provider', true);
  946. }
  947. if (!empty($provider)) {
  948. global $rpx_providers;
  949. $provider = $rpx_providers[$provider];
  950. if (in_array($provider, $get_contacts_providers)) {
  951. $identifier = get_user_meta($user_id, 'rpx_identifier', true);
  952. }
  953. }
  954. if (!empty($identifier)) {
  955. $api_key = get_option(RPX_API_KEY_OPTION);
  956. }
  957. if (!empty($api_key)) {
  958. $rpx_post_array = array('apiKey' => $api_key, 'identifier' => $identifier, 'format' => 'json');
  959. $rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/api/v2/get_contacts', $rpx_post_array);
  960. if ($rpx_reply !== false) {
  961. $rpx_contacts = json_decode($rpx_reply,true);
  962. if (RPX_SERIAL_CONTACTS == 'true') {
  963. rpx_update_user_meta($user_id, RPX_META_CONTACTS, $rpx_contacts);
  964. }
  965. return $rpx_contacts;
  966. }
  967. }
  968. return false;
  969. }
  970. function rpx_new_profile($rpx_auth_info){
  971. global $rpx_profile_map;
  972. global $rpx_wp_profile;
  973. if ( get_option(RPX_VERIFYNAME_OPTION) == 'true' ) {
  974. $rpx_profile_map['preferredUsername'] = 'blocked';
  975. }
  976. if ($rpx_auth_info['stat'] == 'ok'){
  977. $rpx_profile = $rpx_auth_info['profile'];
  978. foreach ($rpx_profile_map as $key => $value){
  979. if (is_array($value)){
  980. foreach ($value as $skey => $svalue){
  981. if ( !empty($rpx_profile["$key"]["$skey"]) ){
  982. $rpx_wp_profile["$svalue"] = $rpx_profile["$key"]["$skey"];
  983. }else{
  984. $rpx_wp_profile["$svalue"] = '';
  985. }
  986. }
  987. }elseif ( !empty($rpx_profile["$key"]) ){
  988. $rpx_wp_profile["$value"] = $rpx_profile["$key"];
  989. }else{
  990. $rpx_wp_profile["$value"] = '';
  991. }
  992. }
  993. return true;
  994. }
  995. return false;
  996. }
  997. function rpx_new_wp_user(){
  998. global $rpx_wp_user_map;
  999. global $rpx_wp_profile;
  1000. global $rpx_wp_user;
  1001. foreach ($rpx_wp_user_map as $key => $value){
  1002. if ( !empty($rpx_wp_profile["$key"]) ){
  1003. $rpx_wp_user["$value"] = $rpx_wp_profile["$key"];
  1004. }
  1005. }
  1006. return true;
  1007. }
  1008. function rpx_validate_user($email='',$username=''){
  1009. global $rpx_wp_profile;
  1010. if ( empty($email) && !empty($rpx_wp_profile['rpx_email']) ){
  1011. $email = $rpx_wp_profile['rpx_email'];
  1012. }
  1013. if ( empty($username) && !empty($rpx_wp_profile['rpx_username']) ){
  1014. $username = $rpx_wp_profile['rpx_username'];
  1015. }
  1016. if ( !empty($email) ) {
  1017. if ( !empty($username) ) {
  1018. if (RPX_IS_WPMU === true){
  1019. $wpmutest = wpmu_validate_user_signup($username, $email);
  1020. if (is_wp_error($wpmutest)){
  1021. $errors = $wpmutest->get_error_messages();
  1022. /* convert wp errors into rpx messages here */
  1023. return false;
  1024. }else{
  1025. return true;
  1026. }
  1027. }else{
  1028. $wpuser = username_exists($username);
  1029. if ($wpuser == NULL){
  1030. $wpuser = false;
  1031. }else{
  1032. $wpuser = true;
  1033. }
  1034. $wpemail = email_exists($email);
  1035. $wptest = is_email($email);
  1036. if ($wptest == $email){
  1037. $wptest = true;
  1038. }else{
  1039. $wptest = false;
  1040. }
  1041. if ($wpuser === false && $wpemail === false && $wptest === true){
  1042. return true;
  1043. }
  1044. return false;
  1045. }
  1046. }else{
  1047. $wptest = is_email($email);
  1048. if ($wptest == $email){
  1049. return true;
  1050. }else{
  1051. return false;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. function rpx_get_message($rpx_auth_info){
  1057. if ($rpx_auth_info['stat'] == 'fail'){
  1058. $message = $rpx_auth_info['err']['msg'];
  1059. }else{
  1060. $message = 'Message missing.';
  1061. }
  1062. return $message;
  1063. }
  1064. function rpx_wp_signon() {
  1065. global $rpx_wp_profile;
  1066. if ($rpx_wp_profile['rpx_wp_id'] == 1 && RPX_BLOCK_ADMIN == 'true') {
  1067. return false;
  1068. }
  1069. $user = get_userdata($rpx_wp_profile['rpx_wp_id']);
  1070. $username = $user->user_login;
  1071. if ( is_a($user, 'WP_User') ) {
  1072. return $user;
  1073. }
  1074. if ( empty($username) ) {
  1075. rpx_message('The username field is empty.', 'message');
  1076. return false;
  1077. }
  1078. if ( is_multisite() ) {
  1079. if ( 1 == $user->spam){
  1080. rpx_message('Your account has been marked as a spammer.', 'message');
  1081. return false;
  1082. }
  1083. if ( !is_super_admin( $user->ID ) && isset($user->primary_blog) ) {
  1084. $details = get_blog_details( $user->primary_blog );
  1085. if ( is_object( $details ) && $details->spam == 1 ){
  1086. rpx_message('Site Suspended.', 'message');
  1087. return false;
  1088. }
  1089. }
  1090. }
  1091. return $user;
  1092. }
  1093. function rpx_get_wpid() {
  1094. global $wpdb;
  1095. global $rpx_wp_profile;
  1096. if ( empty($rpx_wp_profile['rpx_identifier']) ){
  1097. rpx_message('Empty ID', 'debug');
  1098. return false;
  1099. }
  1100. $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = %s AND meta_value = %s';
  1101. $sql = $wpdb->prepare($sql, RPX_META_IDENTIFIER, addslashes($rpx_wp_profile['rpx_identifier']));
  1102. $result = $wpdb->get_var($sql);
  1103. if ($result != NULL){
  1104. if ( !empty($result) && $result != false){
  1105. $rpx_wp_profile['rpx_wp_id'] = $result;
  1106. return true;
  1107. }
  1108. }
  1109. rpx_message('No user found.', 'debug');
  1110. return false;
  1111. }
  1112. function rpx_get_wpuser() {
  1113. global $rpx_wp_profile;
  1114. global $rpx_wp_user_map;
  1115. $user = get_userdata($rpx_wp_profile['rpx_wp_id']);
  1116. foreach ($rpx_wp_user_map as $key => $val){
  1117. if ( empty($rpx_wp_profile[$key]) && !empty($user->$val) ){
  1118. $rpx_wp_profile[$key] = $user->$val;
  1119. }
  1120. }
  1121. }
  1122. function rpx_clean_locked() {
  1123. $cleanup_age = RPX_CLEANUP_AGE;
  1124. settype($cleanup_age, 'int');
  1125. global $wpdb;
  1126. $count = 0;
  1127. $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = %s AND meta_value = %s';
  1128. $sql = $wpdb->prepare($sql, RPX_META_LOCKED, 'true');
  1129. $result = $wpdb->get_col($sql);
  1130. foreach ($result as $key=>$val){
  1131. $rpx_clean_meta = false;
  1132. if ($val != NULL){
  1133. if ($val > 1){
  1134. $sql = 'SELECT UTC_TIMESTAMP() FROM '.$wpdb->usermeta;
  1135. $sql = $wpdb->prepare($sql);
  1136. $sqlnow = $wpdb->get_var($sql);
  1137. $user = get_userdata($val);
  1138. if (strlen($user->rpx_session) > 1){
  1139. $sqlnow = strtotime($sqlnow);
  1140. $usertime = $user->user_registered;
  1141. $usertime = strtotime($usertime);
  1142. $user_aged = 0;
  1143. if ($sqlnow >= $usertime && $usertime >= 0){
  1144. $user_aged = $sqlnow - $usertime;
  1145. $user_aged = $user_aged / 60;
  1146. }
  1147. if ($user_aged > RPX_CLEANUP_AGE){
  1148. if(strpos($user->user_email,$user->rpx_session) === false){//test if the email address contains the session_id
  1149. $rpx_clean_meta = true;
  1150. }else{
  1151. wp_delete_user($val);
  1152. $count++;
  1153. $rpx_clean_meta = false;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. if ($rpx_clean_meta === true){
  1159. $sql = 'DELETE FROM '.$wpdb->usermeta.' WHERE user_id = %d AND meta_key LIKE %s';
  1160. $sql = $wpdb->prepare($sql, $val, 'rpx_%%');
  1161. $del_result = $wpdb->query($sql);
  1162. $count++;
  1163. }
  1164. }
  1165. }
  1166. rpx_message('Cleaned '.$count.' users.', 'message');
  1167. return true;
  1168. }
  1169. function rpx_session_identifier() {
  1170. global $wpdb;
  1171. global $rpx_wp_profile;
  1172. global $rpx_http_vars;
  1173. $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = \''.RPX_META_SESSION.'\' AND meta_value = %s';
  1174. $sql = $wpdb->prepare($sql, $rpx_http_vars['rpx_session']);
  1175. $result = $wpdb->get_var($sql);
  1176. if ($result != NULL){
  1177. $rpx_wp_profile['rpx_wp_id'] = $result;
  1178. if ($rpx_wp_profile['rpx_identifier'] = rpx_get_user_meta($result, RPX_META_IDENTIFIER)){
  1179. return true;
  1180. }
  1181. rpx_message('identifier not found', 'debug');
  1182. return false;
  1183. }
  1184. rpx_message('session not found', 'debug');
  1185. return false;
  1186. }
  1187. function rpx_get_meta() {/*no point in trying to catch errors since empty fields return false*/
  1188. global $rpx_wp_profile;
  1189. $rpx_wp_profile['rpx_identifier'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_IDENTIFIER);
  1190. $rpx_wp_profile['rpx_provider'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PROVIDER);
  1191. $rpx_wp_profile['rpx_session'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_SESSION);
  1192. $rpx_wp_profile['rpx_locked'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_LOCKED);
  1193. $rpx_wp_profile['rpx_photo'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PHOTO);
  1194. $rpx_wp_profile['rpx_url'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_URL);
  1195. }
  1196. function rpx_update_meta() {/*Wordpress uses update for insert and update.*/
  1197. global $rpx_wp_profile;
  1198. $results = array();
  1199. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_IDENTIFIER, $rpx_wp_profile['rpx_identifier']);
  1200. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PROVIDER, $rpx_wp_profile['rpx_provider']);
  1201. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_SESSION, $rpx_wp_profile['rpx_session']);
  1202. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_LOCKED, $rpx_wp_profile['rpx_locked']);
  1203. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PHOTO, $rpx_wp_profile['rpx_photo']);
  1204. $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_URL, $rpx_wp_profile['rpx_url']);
  1205. if (in_array(false,$results)){
  1206. return false;
  1207. }
  1208. return true;
  1209. }
  1210. function rpx_update_user_meta($wp_id, $meta_label, $value) {/*wrapper for Wordpress update to avoid returning false on updates that match current values*/
  1211. $result = rpx_get_user_meta($wp_id, $meta_label);
  1212. if ($result == $value){
  1213. return true;
  1214. }
  1215. if(update_user_meta($wp_id, $meta_label, $value, $result) === false){
  1216. rpx_message('Meta update failed', 'error');
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. function rpx_get_user_meta($wp_id, $meta_label, $single = true) {/*wrapper to make single result the default*/
  1222. $result = get_user_meta($wp_id, $meta_label, $single);
  1223. return $result;
  1224. }
  1225. function rpx_eula_user(){
  1226. global $rpx_wp_profile;
  1227. rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_EULA, 'true');
  1228. }
  1229. function rpx_lock_user(){
  1230. global $rpx_wp_profile;
  1231. $rpx_wp_profile['rpx_locked'] = 'true';
  1232. }
  1233. function rpx_unlock_user(){
  1234. global $rpx_wp_profile;
  1235. $rpx_wp_profile['rpx_locked'] = 'false';
  1236. }
  1237. function rpx_new_session(){
  1238. global $rpx_wp_profile;
  1239. $rpx_wp_profile['rpx_session'] = uniqid('rpx_',true);
  1240. }
  1241. function rpx_placeholder_email(){
  1242. global $rpx_wp_profile;
  1243. $rpx_wp_profile['rpx_email'] = $rpx_wp_profile['rpx_session'].'@'.get_option(RPX_REALM_OPTION);
  1244. }
  1245. function rpx_set_redirect($url=''){
  1246. global $rpx_http_vars;
  1247. if ( !empty($url) ){
  1248. $rpx_http_vars['redirect_to'] = strip_tags($url);
  1249. }
  1250. if ( empty($rpx_http_vars['redirect_to']) ){
  1251. if (get_post_type() != false){
  1252. $rpx_http_vars['redirect_to'] = get_permalink();
  1253. $rpx_http_vars['redirect_to'] .= '#respond';
  1254. }else{
  1255. $rpx_http_vars['redirect_to'] = get_bloginfo('url');
  1256. }
  1257. }
  1258. }
  1259. function rpx_message($message, $class='message') {
  1260. global $rpx_messages;
  1261. if (RPX_VERBOSE == 'true'){
  1262. error_log('WP-RPX '.$class.'='.$message);/*ouput all messages to log*/
  1263. }
  1264. $rpx_messages[] = array( 'message' => $message, 'class' => $class);
  1265. }
  1266. function puke_die($var=''){/*This is a debug function, it is never called in relased code*/
  1267. global $rpx_http_vars;
  1268. global $rpx_wp_profile;
  1269. global $rpx_wp_user;
  1270. global $rpx_messages;
  1271. echo '<pre>';
  1272. echo '$_REQUEST
  1273. '; var_dump($_REQUEST);
  1274. echo '$rpx_http_vars
  1275. '; var_dump($rpx_http_vars);
  1276. echo '$rpx_wp_profile
  1277. '; var_dump($rpx_wp_profile);
  1278. echo '$rpx_wp_user
  1279. '; var_dump($rpx_wp_user);
  1280. echo '$rpx_messages
  1281. '; var_dump($rpx_messages);
  1282. var_dump($var);
  1283. echo '</pre>';
  1284. exit;
  1285. }
  1286. ?>