| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Controller name: User |
| 5 |
Controller description: User Registration, Authentication, User Info, User Meta, FB Login, BuddyPress xProfile Fields methods |
| 6 |
Controller Author: Ali Qureshi |
| 7 |
Controller Author Twitter: @parorrey |
| 8 |
Controller Author Website: parorrey.com |
| 9 |
|
| 10 |
*/ |
| 11 |
class JSON_API_User_Controller { |
| 12 |
|
| 13 |
/** |
| 14 |
* Returns an Array with registered userid & valid cookie |
| 15 |
* @param String username: username to register |
| 16 |
* @param String email: email address for user registration |
| 17 |
* @param String user_pass: user_pass to be set (optional) |
| 18 |
* @param String display_name: display_name for user |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
global $json_api; |
| 22 |
// allow only connection over https. because, well, you care about your passwords and sniffing. |
| 23 |
// turn this sanity-check off if you feel safe inside your localhost or intranet. |
| 24 |
// send an extra POST parameter: insecure=cool |
| 25 |
if (empty($_SERVER['HTTPS']) || |
| 26 |
(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'off')) { |
| 27 |
if (empty($_REQUEST['insecure']) || $_REQUEST['insecure'] != 'cool') { |
| 28 |
$json_api->error("SSL is not enabled. Either use _https_ or provide 'insecure' var as insecure=cool to confirm you want to use http protocol."); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
} |
| 34 |
|
| 35 |
public function info(){ |
| 36 |
|
| 37 |
global $json_api; |
| 38 |
|
| 39 |
return array( |
| 40 |
"version" => JAU_VERSION, |
| 41 |
"php" => PHP_VERSION |
| 42 |
); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
public function register(){ |
| 47 |
|
| 48 |
global $json_api; |
| 49 |
|
| 50 |
|
| 51 |
if (!get_option('users_can_register')) { |
| 52 |
$json_api->error("User registration is disabled. Please enable it in Settings > Gereral."); |
| 53 |
} |
| 54 |
|
| 55 |
if (!$json_api->query->username) { |
| 56 |
$json_api->error("You must include 'username' var in your request. "); |
| 57 |
} |
| 58 |
else $username = sanitize_user( $json_api->query->username ); |
| 59 |
|
| 60 |
|
| 61 |
if (!$json_api->query->email) { |
| 62 |
$json_api->error("You must include 'email' var in your request. "); |
| 63 |
} |
| 64 |
else $email = sanitize_email( $json_api->query->email ); |
| 65 |
|
| 66 |
if (!$json_api->query->nonce) { |
| 67 |
$json_api->error("You must include 'nonce' var in your request. Use the 'get_nonce' Core API method. "); |
| 68 |
} |
| 69 |
else $nonce = sanitize_text_field( $json_api->query->nonce ) ; |
| 70 |
|
| 71 |
if ($json_api->query->display_name) { |
| 72 |
$display_name = sanitize_text_field( $json_api->query->display_name ); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$user_pass = sanitize_text_field( $_REQUEST['user_pass'] ); |
| 77 |
|
| 78 |
if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds; |
| 79 |
|
| 80 |
else $seconds = 1209600;//14 days |
| 81 |
|
| 82 |
//Add usernames we don't want used |
| 83 |
|
| 84 |
$invalid_usernames = array( 'admin' ); |
| 85 |
|
| 86 |
//Do username validation |
| 87 |
|
| 88 |
$nonce_id = $json_api->get_nonce_id('user', 'register'); |
| 89 |
|
| 90 |
if( !wp_verify_nonce($json_api->query->nonce, $nonce_id) ) { |
| 91 |
|
| 92 |
$json_api->error("Invalid access, unverifiable 'nonce' value. Use the 'get_nonce' Core API method. "); |
| 93 |
} |
| 94 |
|
| 95 |
else { |
| 96 |
|
| 97 |
if ( !validate_username( $username ) || in_array( $username, $invalid_usernames ) ) { |
| 98 |
|
| 99 |
$json_api->error("Username is invalid."); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
elseif ( username_exists( $username ) ) { |
| 104 |
|
| 105 |
$json_api->error("Username already exists."); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
else{ |
| 110 |
|
| 111 |
|
| 112 |
if ( !is_email( $email ) ) { |
| 113 |
$json_api->error("E-mail address is invalid."); |
| 114 |
} |
| 115 |
elseif (email_exists($email)) { |
| 116 |
|
| 117 |
$json_api->error("E-mail address is already in use."); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
else { |
| 122 |
|
| 123 |
//Everything has been validated, proceed with creating the user |
| 124 |
|
| 125 |
//Create the user |
| 126 |
|
| 127 |
if( !isset($_REQUEST['user_pass']) ) { |
| 128 |
$user_pass = wp_generate_password(); |
| 129 |
$_REQUEST['user_pass'] = $user_pass; |
| 130 |
} |
| 131 |
|
| 132 |
$_REQUEST['user_login'] = $username; |
| 133 |
$_REQUEST['user_email'] = $email; |
| 134 |
|
| 135 |
$allowed_params = array('user_login', 'user_email', 'user_pass', 'display_name', 'user_nicename', 'user_url', 'nickname', 'first_name', |
| 136 |
'last_name', 'description', 'rich_editing', 'user_registered', 'role', 'jabber', 'aim', 'yim', |
| 137 |
'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' |
| 138 |
); |
| 139 |
|
| 140 |
|
| 141 |
foreach($_REQUEST as $field => $value){ |
| 142 |
|
| 143 |
if( in_array($field, $allowed_params) ) $user[$field] = trim(sanitize_text_field($value)); |
| 144 |
|
| 145 |
} |
| 146 |
$user['role'] = get_option('default_role'); |
| 147 |
$user_id = wp_insert_user( $user ); |
| 148 |
|
| 149 |
/*Send e-mail to admin and new user - |
| 150 |
You could create your own e-mail instead of using this function*/ |
| 151 |
|
| 152 |
if( isset($_REQUEST['user_pass']) && $_REQUEST['notify']=='no') { |
| 153 |
$notify = ''; |
| 154 |
}elseif($_REQUEST['notify']!='no') $notify = $_REQUEST['notify']; |
| 155 |
|
| 156 |
|
| 157 |
if($user_id) wp_new_user_notification( $user_id, '',$notify ); |
| 158 |
|
| 159 |
|
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if($user_id){ |
| 165 |
|
| 166 |
if(is_array($_REQUEST['custom_fields'])){ |
| 167 |
|
| 168 |
foreach ($_REQUEST['custom_fields'] as $field=>$val) { |
| 169 |
$data[$field] = update_user_meta( $user_id, $field, $val ); |
| 170 |
|
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user_id, true); |
| 175 |
|
| 176 |
$cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in'); |
| 177 |
$cookie_admin = wp_generate_auth_cookie($user_id, $expiration, 'secure_auth'); |
| 178 |
|
| 179 |
$user_info = get_userdata($user_id); |
| 180 |
} |
| 181 |
|
| 182 |
return array( |
| 183 |
"cookie" => $cookie, |
| 184 |
"cookie_admin" => $cookie_admin, |
| 185 |
"cookie_name" => LOGGED_IN_COOKIE, |
| 186 |
"user_id" => $user_id, |
| 187 |
"username" => $user_info->user_login |
| 188 |
); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
public function get_avatar(){ |
| 193 |
|
| 194 |
global $json_api; |
| 195 |
|
| 196 |
if (function_exists('bp_is_active')) { |
| 197 |
|
| 198 |
if (!$json_api->query->user_id) { |
| 199 |
$json_api->error("You must include 'user_id' var in your request. "); |
| 200 |
} |
| 201 |
|
| 202 |
if (!$json_api->query->type) { |
| 203 |
$json_api->error("You must include 'type' var in your request. possible values 'full' or 'thumb' "); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
$avatar = bp_core_fetch_avatar ( array( 'item_id' => $json_api->query->user_id, 'type' => $json_api->query->type, 'html'=>false )); |
| 208 |
|
| 209 |
return array('avatar'=>$avatar); |
| 210 |
} else { |
| 211 |
|
| 212 |
$json_api->error("You must install and activate BuddyPress plugin to use this method."); |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
public function get_userinfo(){ |
| 219 |
|
| 220 |
global $json_api; |
| 221 |
|
| 222 |
if (!$json_api->query->user_id) { |
| 223 |
$json_api->error("You must include 'user_id' var in your request. "); |
| 224 |
} |
| 225 |
|
| 226 |
$user = get_userdata($json_api->query->user_id); |
| 227 |
|
| 228 |
preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); |
| 229 |
|
| 230 |
return array( |
| 231 |
"id" => $user->ID, |
| 232 |
//"username" => $user->user_login, |
| 233 |
"nicename" => $user->user_nicename, |
| 234 |
//"email" => $user->user_email, |
| 235 |
"url" => $user->user_url, |
| 236 |
"displayname" => $user->display_name, |
| 237 |
"firstname" => $user->user_firstname, |
| 238 |
"lastname" => $user->last_name, |
| 239 |
"nickname" => $user->nickname, |
| 240 |
"avatar" => $avatar[1] |
| 241 |
); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
public function retrieve_password(){ |
| 246 |
|
| 247 |
global $wpdb, $json_api, $wp_hasher; |
| 248 |
|
| 249 |
if (!$json_api->query->user_login) { |
| 250 |
|
| 251 |
$json_api->error("You must include 'user_login' var in your request. "); |
| 252 |
|
| 253 |
} |
| 254 |
|
| 255 |
$user_login = $json_api->query->user_login; |
| 256 |
|
| 257 |
if ( strpos( $user_login, '@' ) ) { |
| 258 |
|
| 259 |
$user_data = get_user_by( 'email', trim( $user_login ) ); |
| 260 |
|
| 261 |
if ( empty( $user_data ) ) |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
$json_api->error("Your email address not found! "); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
} else { |
| 270 |
|
| 271 |
$login = trim($user_login); |
| 272 |
|
| 273 |
$user_data = get_user_by('login', $login); |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
// redefining user_login ensures we return the right case in the email |
| 280 |
|
| 281 |
$user_login = $user_data->user_login; |
| 282 |
|
| 283 |
$user_email = $user_data->user_email; |
| 284 |
|
| 285 |
|
| 286 |
do_action('retrieve_password', $user_login); |
| 287 |
|
| 288 |
|
| 289 |
$allow = apply_filters('allow_password_reset', true, $user_data->ID); |
| 290 |
|
| 291 |
if ( ! $allow ) $json_api->error("password reset not allowed! "); |
| 292 |
|
| 293 |
elseif ( is_wp_error($allow) ) $json_api->error("An error occured! "); |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
$key = wp_generate_password( 20, false ); |
| 298 |
|
| 299 |
do_action( 'retrieve_password_key', $user_login, $key ); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
if ( empty( $wp_hasher ) ) { |
| 304 |
|
| 305 |
require_once ABSPATH . 'wp-includes/class-phpass.php'; |
| 306 |
|
| 307 |
$wp_hasher = new PasswordHash( 8, true ); |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
$hashed = time() . ':' . $wp_hasher->HashPassword( $key ); |
| 313 |
|
| 314 |
$wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) ); |
| 315 |
|
| 316 |
$message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n"; |
| 317 |
|
| 318 |
$message .= network_home_url( '/' ) . "\r\n\r\n"; |
| 319 |
|
| 320 |
$message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; |
| 321 |
|
| 322 |
$message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n"; |
| 323 |
|
| 324 |
$message .= __('To reset your password, visit the following address:') . "\r\n\r\n"; |
| 325 |
|
| 326 |
$message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n"; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
if ( is_multisite() ) |
| 331 |
|
| 332 |
$blogname = $GLOBALS['current_site']->site_name; |
| 333 |
|
| 334 |
else |
| 335 |
|
| 336 |
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
$title = sprintf( __('[%s] Password Reset'), $blogname ); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
$title = apply_filters('retrieve_password_title', $title); |
| 345 |
|
| 346 |
$message = apply_filters('retrieve_password_message', $message, $key); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
if ( $message && !wp_mail($user_email, $title, $message) ) |
| 351 |
|
| 352 |
$json_api->error("The e-mail could not be sent. Possible reason: your host may have disabled the mail() function..."); |
| 353 |
|
| 354 |
else |
| 355 |
|
| 356 |
return array( |
| 357 |
|
| 358 |
"msg" => 'Link for password reset has been emailed to you. Please check your email.', |
| 359 |
|
| 360 |
); |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
public function validate_auth_cookie() { |
| 365 |
|
| 366 |
global $json_api; |
| 367 |
|
| 368 |
|
| 369 |
if ( !$json_api->query->cookie ) { |
| 370 |
|
| 371 |
$json_api->error( "You must include a 'cookie' authentication cookie. Use the `generate_auth_cookie` method." ); |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
$user_id = wp_validate_auth_cookie( $json_api->query->cookie, 'logged_in' ); |
| 376 |
|
| 377 |
$valid = $user_id ? true : false; |
| 378 |
|
| 379 |
return array( |
| 380 |
|
| 381 |
"valid" => $valid, |
| 382 |
"user_id" =>$user_id |
| 383 |
|
| 384 |
); |
| 385 |
|
| 386 |
} |
| 387 |
|
| 388 |
public function generate_auth_cookie() { |
| 389 |
|
| 390 |
global $json_api; |
| 391 |
|
| 392 |
foreach($_POST as $k=>$val) { |
| 393 |
if (isset($_POST[$k])) { |
| 394 |
$json_api->query->$k = $val; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
if (!$json_api->query->username && !$json_api->query->email) { |
| 400 |
|
| 401 |
$json_api->error("You must include 'username' or 'email' var in your request to generate cookie."); |
| 402 |
|
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
if (!$json_api->query->password) { |
| 407 |
|
| 408 |
$json_api->error("You must include a 'password' var in your request."); |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
if ($json_api->query->seconds) $seconds = (int) $json_api->query->seconds; |
| 413 |
|
| 414 |
else $seconds = 1209600;//14 days |
| 415 |
|
| 416 |
if ( $json_api->query->email ) { |
| 417 |
|
| 418 |
|
| 419 |
if ( is_email( $json_api->query->email ) ) { |
| 420 |
if( !email_exists( $json_api->query->email)) { |
| 421 |
$json_api->error("email does not exist."); |
| 422 |
} |
| 423 |
}else $json_api->error("Invalid email address."); |
| 424 |
|
| 425 |
$user_obj = get_user_by( 'email', $json_api->query->email ); |
| 426 |
|
| 427 |
|
| 428 |
$user = wp_authenticate($user_obj->data->user_login, $json_api->query->password); |
| 429 |
}else { |
| 430 |
|
| 431 |
$user = wp_authenticate($json_api->query->username, $json_api->query->password); |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
if (is_wp_error($user)) { |
| 436 |
|
| 437 |
remove_action('wp_login_failed', $json_api->query->username); |
| 438 |
$json_api->error("Invalid username/email and/or password.", 'error', '401'); |
| 439 |
|
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
$expiration = time() + apply_filters('auth_cookie_expiration', $seconds, $user->ID, true); |
| 444 |
|
| 445 |
$cookie = wp_generate_auth_cookie($user->ID, $expiration, 'logged_in'); |
| 446 |
$cookie_admin = wp_generate_auth_cookie($user->ID, $expiration, 'secure_auth'); |
| 447 |
|
| 448 |
preg_match('|src="(.+?)"|', get_avatar( $user->ID, 512 ), $avatar); |
| 449 |
|
| 450 |
return array( |
| 451 |
"cookie" => $cookie, |
| 452 |
"cookie_admin" => $cookie_admin, |
| 453 |
"cookie_name" => LOGGED_IN_COOKIE, |
| 454 |
"user" => array( |
| 455 |
"id" => $user->ID, |
| 456 |
"username" => $user->user_login, |
| 457 |
"nicename" => $user->user_nicename, |
| 458 |
"email" => $user->user_email, |
| 459 |
"url" => $user->user_url, |
| 460 |
"registered" => $user->user_registered, |
| 461 |
"displayname" => $user->display_name, |
| 462 |
"firstname" => $user->user_firstname, |
| 463 |
"lastname" => $user->last_name, |
| 464 |
"nickname" => $user->nickname, |
| 465 |
"description" => $user->user_description, |
| 466 |
"capabilities" => $user->wp_capabilities, |
| 467 |
"avatar" => $avatar[1] |
| 468 |
|
| 469 |
), |
| 470 |
); |
| 471 |
} |
| 472 |
|
| 473 |
public function get_currentuserinfo() { |
| 474 |
|
| 475 |
global $json_api; |
| 476 |
|
| 477 |
if (!$json_api->query->cookie) { |
| 478 |
|
| 479 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` Auth API method."); |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 484 |
|
| 485 |
|
| 486 |
if (!$user_id) { |
| 487 |
$json_api->error("Invalid authentication cookie. Use the `generate_auth_cookie` method."); |
| 488 |
} |
| 489 |
|
| 490 |
$user = get_userdata($user_id); |
| 491 |
|
| 492 |
preg_match('|src="(.+?)"|', get_avatar( $user->ID, 32 ), $avatar); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
return array( |
| 497 |
|
| 498 |
"user" => array( |
| 499 |
|
| 500 |
"id" => $user->ID, |
| 501 |
|
| 502 |
"username" => $user->user_login, |
| 503 |
|
| 504 |
"nicename" => $user->user_nicename, |
| 505 |
|
| 506 |
"email" => $user->user_email, |
| 507 |
|
| 508 |
"url" => $user->user_url, |
| 509 |
|
| 510 |
"registered" => $user->user_registered, |
| 511 |
|
| 512 |
"displayname" => $user->display_name, |
| 513 |
|
| 514 |
"firstname" => $user->user_firstname, |
| 515 |
|
| 516 |
"lastname" => $user->last_name, |
| 517 |
|
| 518 |
"nickname" => $user->nickname, |
| 519 |
|
| 520 |
"description" => $user->user_description, |
| 521 |
|
| 522 |
"capabilities" => $user->wp_capabilities, |
| 523 |
|
| 524 |
"avatar" => $avatar[1] |
| 525 |
|
| 526 |
) |
| 527 |
|
| 528 |
); |
| 529 |
|
| 530 |
} |
| 531 |
|
| 532 |
public function get_user_meta() { |
| 533 |
|
| 534 |
global $json_api; |
| 535 |
|
| 536 |
if (!$json_api->query->cookie) { |
| 537 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 538 |
} |
| 539 |
|
| 540 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 541 |
|
| 542 |
if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 543 |
|
| 544 |
$meta_key = sanitize_text_field($json_api->query->meta_key); |
| 545 |
|
| 546 |
|
| 547 |
if($meta_key) $data[$meta_key] = get_user_meta( $user_id, $meta_key); |
| 548 |
else { |
| 549 |
// Get all user meta data for $user_id |
| 550 |
$meta = get_user_meta( $user_id ); |
| 551 |
|
| 552 |
// Filter out empty meta data |
| 553 |
$data = array_filter( array_map( function( $a ) { |
| 554 |
return $a[0]; |
| 555 |
}, $meta ) ); |
| 556 |
|
| 557 |
} |
| 558 |
//d($data); |
| 559 |
return $data; |
| 560 |
|
| 561 |
|
| 562 |
} |
| 563 |
|
| 564 |
public function update_user_meta() { |
| 565 |
|
| 566 |
global $json_api; |
| 567 |
|
| 568 |
if (!$json_api->query->cookie) { |
| 569 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 570 |
} |
| 571 |
|
| 572 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 573 |
|
| 574 |
if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 575 |
|
| 576 |
|
| 577 |
if (!$json_api->query->meta_key) $json_api->error("You must include a 'meta_key' var in your request."); |
| 578 |
|
| 579 |
else $meta_key = $json_api->query->meta_key; |
| 580 |
|
| 581 |
if (!$json_api->query->meta_value) { |
| 582 |
$json_api->error("You must include a 'meta_value' var in your request. If you have multiple values for any meta_key, you must send it as an array meta_value[] in POST method."); |
| 583 |
} |
| 584 |
else $meta_value = $json_api->query->meta_value; |
| 585 |
|
| 586 |
if( is_array($meta_value) ) { |
| 587 |
|
| 588 |
$meta_values = array_map('trim',$meta_value); |
| 589 |
|
| 590 |
$data['updated'] = update_user_meta( $user_id, $meta_key, $meta_values); |
| 591 |
} |
| 592 |
else $data['updated'] = update_user_meta( $user_id, $meta_key, $meta_value); |
| 593 |
|
| 594 |
|
| 595 |
return $data; |
| 596 |
|
| 597 |
} |
| 598 |
|
| 599 |
public function delete_user_meta() { |
| 600 |
|
| 601 |
global $json_api; |
| 602 |
|
| 603 |
if (!$json_api->query->cookie) { |
| 604 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 605 |
} |
| 606 |
|
| 607 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 608 |
|
| 609 |
if (!$user_id) $json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 610 |
|
| 611 |
|
| 612 |
if (!$json_api->query->meta_key) $json_api->error("You must include a 'meta_key' var in your request."); |
| 613 |
|
| 614 |
else $meta_key = $json_api->query->meta_key; |
| 615 |
|
| 616 |
if (!$json_api->query->meta_value) { |
| 617 |
$json_api->error("You must include a 'meta_value' var in your request."); |
| 618 |
} |
| 619 |
else $meta_value = sanitize_text_field($json_api->query->meta_value); |
| 620 |
|
| 621 |
|
| 622 |
$data['deleted'] = delete_user_meta( $user_id, $meta_key, $meta_value); |
| 623 |
|
| 624 |
return $data; |
| 625 |
|
| 626 |
} |
| 627 |
|
| 628 |
public function update_user_meta_vars() { |
| 629 |
|
| 630 |
global $json_api; |
| 631 |
|
| 632 |
if (!$json_api->query->cookie) { |
| 633 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 634 |
} |
| 635 |
|
| 636 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 637 |
// echo '$user_id: '.$user_id; |
| 638 |
|
| 639 |
if (!$user_id) { |
| 640 |
$json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 641 |
} |
| 642 |
|
| 643 |
if( sizeof($_REQUEST) <=1) $json_api->error("You must include one or more vars in your request to add or update as user_meta. e.g. 'name', 'website', 'skills'. You must provide multiple meta_key vars in this format: &name=Ali&website=parorrey.com&description=This is test description. If any field has the possibility to hold more than one value for any multi-select fields or check boxes, you must provide an array of values and use POST method."); |
| 644 |
|
| 645 |
|
| 646 |
foreach($_REQUEST as $field => $value){ |
| 647 |
|
| 648 |
if($field=='cookie') continue; |
| 649 |
|
| 650 |
$field_label = str_replace('_',' ',$field); |
| 651 |
|
| 652 |
if( is_array($value) ) { |
| 653 |
//$values = explode(",", $value); |
| 654 |
$values = array_map('trim',$values); |
| 655 |
} |
| 656 |
else $values = trim($value); |
| 657 |
|
| 658 |
|
| 659 |
$result[$field_label]['updated'] = update_user_meta( $user_id, $field, $values); |
| 660 |
|
| 661 |
} |
| 662 |
|
| 663 |
return $result; |
| 664 |
|
| 665 |
|
| 666 |
} |
| 667 |
|
| 668 |
public function xprofile() { |
| 669 |
|
| 670 |
global $json_api; |
| 671 |
|
| 672 |
if (function_exists('bp_is_active')) { |
| 673 |
|
| 674 |
if (!$json_api->query->user_id) { |
| 675 |
$json_api->error("You must include a 'user_id' var in your request."); |
| 676 |
} |
| 677 |
else $user_id = $json_api->query->user_id; |
| 678 |
|
| 679 |
|
| 680 |
if (!$json_api->query->field) { |
| 681 |
$json_api->error("You must include a 'field' var in your request. Use 'field=default' for all default fields."); |
| 682 |
} |
| 683 |
elseif ($json_api->query->field=='default') { |
| 684 |
$field_label='First Name, Last Name, Bio';/*you should add your own field labels here for quick viewing*/ |
| 685 |
} |
| 686 |
else $field_label = sanitize_text_field($json_api->query->field); |
| 687 |
|
| 688 |
|
| 689 |
$fields = explode(",", $field_label); |
| 690 |
|
| 691 |
if(is_array($fields)){ |
| 692 |
|
| 693 |
foreach($fields as $k){ |
| 694 |
|
| 695 |
$fields_data[$k] = xprofile_get_field_data( $k, $user_id ); |
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
return $fields_data; |
| 700 |
|
| 701 |
|
| 702 |
} |
| 703 |
|
| 704 |
} |
| 705 |
|
| 706 |
else { |
| 707 |
|
| 708 |
$json_api->error("You must install and activate BuddyPress plugin to use this method."); |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
} |
| 713 |
|
| 714 |
public function xprofile_update() { |
| 715 |
|
| 716 |
global $json_api; |
| 717 |
|
| 718 |
if (function_exists('bp_is_active')) { |
| 719 |
|
| 720 |
if (!$json_api->query->cookie) { |
| 721 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 722 |
} |
| 723 |
|
| 724 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 725 |
// echo '$user_id: '.$user_id; |
| 726 |
|
| 727 |
if (!$user_id) { |
| 728 |
$json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
foreach($_REQUEST as $field => $value){ |
| 733 |
|
| 734 |
if($field=='cookie') continue; |
| 735 |
|
| 736 |
$field_label = str_replace('_',' ',$field); |
| 737 |
|
| 738 |
if( strpos($value,',') !== false ) { |
| 739 |
$values = explode(",", $value); |
| 740 |
$values = array_map('trim',$values); |
| 741 |
} |
| 742 |
else $values = trim($value); |
| 743 |
//echo 'field-values: '.$field.'=>'.$value; |
| 744 |
//d($values); |
| 745 |
|
| 746 |
$result[$field_label]['updated'] = xprofile_set_field_data( $field_label, $user_id, $values, $is_required = true ); |
| 747 |
|
| 748 |
} |
| 749 |
|
| 750 |
return $result; |
| 751 |
} |
| 752 |
|
| 753 |
else { |
| 754 |
|
| 755 |
$json_api->error("You must install and activate BuddyPress plugin to use this method."); |
| 756 |
|
| 757 |
} |
| 758 |
|
| 759 |
} |
| 760 |
|
| 761 |
public function fb_connect(){ |
| 762 |
|
| 763 |
global $json_api; |
| 764 |
|
| 765 |
if ($json_api->query->fields) { |
| 766 |
|
| 767 |
$fields = $json_api->query->fields; |
| 768 |
|
| 769 |
}else $fields = 'id,name,first_name,last_name,email'; |
| 770 |
|
| 771 |
if ($json_api->query->ssl) { |
| 772 |
$enable_ssl = $json_api->query->ssl; |
| 773 |
}else $enable_ssl = true; |
| 774 |
|
| 775 |
if (!$json_api->query->access_token) { |
| 776 |
$json_api->error("You must include a 'access_token' variable. Get the valid access_token for this app from Facebook API."); |
| 777 |
}else{ |
| 778 |
|
| 779 |
$url='https://graph.facebook.com/me/?fields='.$fields.'&access_token='.$json_api->query->access_token; |
| 780 |
|
| 781 |
// Initiate curl |
| 782 |
$ch = curl_init(); |
| 783 |
// Enable SSL verification |
| 784 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $enable_ssl); |
| 785 |
// Will return the response, if false it print the response |
| 786 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 787 |
// Set the url |
| 788 |
curl_setopt($ch, CURLOPT_URL,$url); |
| 789 |
// Execute |
| 790 |
$result=curl_exec($ch); |
| 791 |
// Closing |
| 792 |
curl_close($ch); |
| 793 |
|
| 794 |
$result = json_decode($result, true); |
| 795 |
|
| 796 |
if(isset($result["email"])){ |
| 797 |
|
| 798 |
$user_email = $result["email"]; |
| 799 |
$email_exists = email_exists($user_email); |
| 800 |
|
| 801 |
if($email_exists) { |
| 802 |
$user = get_user_by( 'email', $user_email ); |
| 803 |
$user_id = $user->ID; |
| 804 |
$user_name = $user->user_login; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
if ( !$user_id && $email_exists == false ) { |
| 810 |
|
| 811 |
$user_name = strtolower($result['first_name'].'.'.$result['last_name']); |
| 812 |
|
| 813 |
while(username_exists($user_name)){ |
| 814 |
$i++; |
| 815 |
$user_name = strtolower($result['first_name'].'.'.$result['last_name']).'.'.$i; |
| 816 |
|
| 817 |
} |
| 818 |
|
| 819 |
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false ); |
| 820 |
$userdata = array( |
| 821 |
'user_login' => $user_name, |
| 822 |
'user_email' => $user_email, |
| 823 |
'user_pass' => $random_password, |
| 824 |
'display_name' => $result["name"], |
| 825 |
'first_name' => $result['first_name'], |
| 826 |
'last_name' => $result['last_name'] |
| 827 |
); |
| 828 |
|
| 829 |
$user_id = wp_insert_user( $userdata ) ; |
| 830 |
if($user_id) $user_account = 'user registered.'; |
| 831 |
|
| 832 |
} else { |
| 833 |
|
| 834 |
if($user_id) $user_account = 'user logged in.'; |
| 835 |
} |
| 836 |
|
| 837 |
$expiration = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, true); |
| 838 |
$cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in'); |
| 839 |
|
| 840 |
$response['msg'] = $user_account; |
| 841 |
$response['wp_user_id'] = $user_id; |
| 842 |
$response['cookie'] = $cookie; |
| 843 |
$response['user_login'] = $user_name; |
| 844 |
|
| 845 |
} |
| 846 |
else { |
| 847 |
$response['msg'] = "Your 'access_token' did not return email of the user. Without 'email' user can't be logged in or registered. Get user email extended permission while joining the Facebook app."; |
| 848 |
|
| 849 |
} |
| 850 |
|
| 851 |
} |
| 852 |
|
| 853 |
return $response; |
| 854 |
|
| 855 |
} |
| 856 |
|
| 857 |
public function post_comment(){ |
| 858 |
global $json_api; |
| 859 |
|
| 860 |
if (!$json_api->query->cookie) { |
| 861 |
$json_api->error("You must include a 'cookie' var in your request. Use the `generate_auth_cookie` method."); |
| 862 |
} |
| 863 |
|
| 864 |
$user_id = wp_validate_auth_cookie($json_api->query->cookie, 'logged_in'); |
| 865 |
|
| 866 |
if (!$user_id) { |
| 867 |
$json_api->error("Invalid cookie. Use the `generate_auth_cookie` method."); |
| 868 |
} |
| 869 |
|
| 870 |
if ( !$json_api->query->post_id ) { |
| 871 |
$json_api->error("No post specified. Include 'post_id' var in your request."); |
| 872 |
} elseif (!$json_api->query->content ) { |
| 873 |
$json_api->error("Please include 'content' var in your request."); |
| 874 |
} |
| 875 |
|
| 876 |
if (!isset($json_api->query->comment_status) ) { |
| 877 |
$json_api->error("Please include 'comment_status' var in your request. Possible values are comment_status=1 (approved) or comment_status=hold (not-approved)"); |
| 878 |
}else $comment_status = $json_api->query->comment_status; |
| 879 |
|
| 880 |
if($comment_status=='hold') $comment_status = 0; |
| 881 |
|
| 882 |
$user_info = get_userdata( $user_id ); |
| 883 |
|
| 884 |
$time = current_time('mysql'); |
| 885 |
$agent = $_SERVER['HTTP_USER_AGENT']; |
| 886 |
$ip=$_SERVER['REMOTE_ADDR']; |
| 887 |
|
| 888 |
$data = array( |
| 889 |
'comment_post_ID' => $json_api->query->post_id, |
| 890 |
'comment_author' => $user_info->user_login, |
| 891 |
'comment_author_email' => $user_info->user_email, |
| 892 |
'comment_author_url' => $user_info->user_url, |
| 893 |
'comment_content' => $json_api->query->content, |
| 894 |
'comment_type' => '', |
| 895 |
'comment_parent' => 0, |
| 896 |
'user_id' => $user_info->ID, |
| 897 |
'comment_author_IP' => $ip, |
| 898 |
'comment_agent' => $agent, |
| 899 |
'comment_date' => $time, |
| 900 |
'comment_approved' => $comment_status, |
| 901 |
); |
| 902 |
|
| 903 |
//print_r($data); |
| 904 |
|
| 905 |
$comment_id = wp_insert_comment($data); |
| 906 |
|
| 907 |
return array( |
| 908 |
"comment_id" => $comment_id |
| 909 |
); |
| 910 |
} |
| 911 |
|
| 912 |
} |