1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }
class Inbound_API_Keys_Generation {
public function __construct() {
$this->generate_keys();
}
public function generate_keys() {
if (!isset($_REQUEST['inbound_action'])) {
return;
}
if( isset( $_REQUEST['user_id'] ) ) {
$userdata = get_user_by( 'id', intval($_REQUEST['user_id']) );
if (isset($userdata->ID)) {
$user_id = $userdata->ID;
} else {
wp_redirect( add_query_arg( 'inbound-message', 'user-id-does-not-exits', 'edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys' ) ); exit();
}
}
switch( $_REQUEST['inbound_action'] ) {
case 'generate-api-keys':
if( $this->generate_api_key( $user_id ) ) {
delete_transient( 'inbound-total-api-keys' );
wp_redirect( add_query_arg( 'inbound-message', 'api-key-generated', 'edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys' ) ); exit();
} else {
wp_redirect( add_query_arg( 'inbound-message', 'api-key-exists', 'edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys' ) ); exit();
}
break;
case 'regenerate-api-keys':
$this->generate_api_key( $user_id, true );
delete_transient( 'inbound-total-api-keys' );
wp_redirect( add_query_arg( 'inbound-message', 'api-key-regenerated', 'edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys' ) ); exit();
break;
case 'revoke-api-keys':
$this->revoke_api_key( $user_id );
delete_transient( 'inbound-total-api-keys' );
wp_redirect( add_query_arg( 'inbound-message', 'api-key-revoked', 'edit.php?post_type=wp-lead&page=wpleads_global_settings&tab=tabs-wpleads-apikeys' ) ); exit();
break;
default;
break;
}
}
public function generate_api_key( $user_id, $regenerate = false ) {
$user = get_userdata( $user_id );
if ( empty( $user->inbound_user_public_key ) ) {
update_user_meta( $user_id, 'inbound_user_public_key', $this->generate_public_key( $user->user_email ) );
update_user_meta( $user_id, 'inbound_user_secret_key', $this->generate_private_key( $user->ID ) );
} elseif( $regenerate == true ) {
$this->revoke_api_key( $user->ID );
update_user_meta( $user_id, 'inbound_user_public_key', $this->generate_public_key( $user->user_email ) );
update_user_meta( $user_id, 'inbound_user_secret_key', $this->generate_private_key( $user->ID ) );
} else {
return false;
}
return true;
}
public function revoke_api_key( $user_id ) {
$user = get_userdata( $user_id );
if ( ! empty( $user->inbound_user_public_key ) ) {
delete_transient( md5( 'inbound_api_user_' . $user->inbound_user_public_key ) );
delete_user_meta( $user_id, 'inbound_user_public_key' );
delete_user_meta( $user_id, 'inbound_user_secret_key' );
} else {
return false;
}
return true;
}
private function generate_public_key( $user_email = '' ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$public = hash( 'md5', $user_email . $auth_key . date( 'U' ) );
return $public;
}
private function generate_private_key( $user_id = 0 ) {
$auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
$secret = hash( 'md5', $user_id . $auth_key . date( 'U' ) );
return $secret;
}
}
add_action('admin_init', 'load_Inbound_API_Keys_Generation');
function load_Inbound_API_Keys_Generation() {
$Inbound_API_Keys_Generation = new Inbound_API_Keys_Generation();
}