HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/doco2/wp-content/themes/ormedia/key_alert_functions.php
<?php
require_once 'JwtAuthClass.php';
require_once 'jpush/autoload.php';

function show_php_error()
{
	ini_set('display_errors', 1); //错误信息
	ini_set('display_startup_errors', 1); //php启动错误信息
	error_reporting(-1);
}

// 新增用户
function insert_secu_user($login, $password, $username, $push_id = "")
{
	global $wpdb;
	$obj = new stdClass();
	$obj->rc = 1;
	$obj->msg = "success";
	if (empty($login) || empty($password) || empty($username)) {
		$obj->rc = -1;
		$obj->msg = "missing params";
	} else {
		if (strlen($password) >= 4) {
			$hash = wp_hash_password($password);
			$is_exist = get_secu_user($login) != null;
			if ($is_exist) {
				$obj->rc = -3;
				$obj->msg = "login already exists";
			} else {
				$res = $wpdb->insert(
					'keylab_secu',
					array(
						'user_login' => $login,
						'user_name' => $username,
						'password' => $hash,
						'push_id' => $push_id
					),
					array('%s', '%s', '%s', '%s')
				);
				if ($res === false) {
					$obj->rc = -4;
					$obj->msg = "failed to insert";
				}
			}
		} else {
			$obj->rc = -2;
			$obj->msg = "password length does not match";
		}
	}
	return $obj;
}

// 通过login获取用户
function get_secu_user($login)
{
	global $wpdb;
	$res = $wpdb->get_row($wpdb->prepare("select * from keylab_secu where user_login = %s", $login));
	return $res;
}

// 通过token获取用户
function get_secu_user_by_token($token)
{
	global $wpdb;
	$res = $wpdb->get_row($wpdb->prepare("select * from keylab_secu where token = %s", $token));
	return $res;
}

// 用户登入
function secu_do_login($login, $password, $push_id)
{
	date_default_timezone_set('Asia/Hong_Kong');
	global $wpdb;
	$obj = new stdClass();
	$obj->rc = 1;
	$obj->msg = "";
	$u = get_secu_user($login);
	if ($u == null) {
		$obj->rc = -1;
		$obj->msg = "login not exists";
	} else {
		$res = wp_check_password($password, $u->password);
		if ($res) {
			if (!empty($push_id)) {
				$old_push_id = r_push_id($u->id);
				if (!empty($old_push_id) && $old_push_id != $push_id) {
					// 提示另一设备登录
					$data = [
						'title' => "logout",
						'alert' => "logout",
						'extras' => [
							'logout' => 1
						]
					];
					send_jpush_msg($old_push_id, $data);
				}
				u_push_id($u->id, $push_id);
			}
			$obj->msg = "success";
			$obj->data = $u;
			$payload_new = array(
				'iss' => 'keylab',
				'iat' => time(),
				'exp' => time() + 3600 * 24 * 365 * 20,
				'nbf' => time() + 3600 * 24 * 365 * 20,
				'sub' => 'keylab.cc',
				'jti' => md5(uniqid('JWT') . time())
			);
			$token_new = JwtAuthClass::getToken($payload_new);
			$old_token = r_token($u->id);
			if ($old_token) {
				if (JwtAuthClass::verifyToken($old_token->token) == 1) {
					$obj->data->token = $old_token->token;
				} else {
					u_token($u->id, $token_new);
					$obj->data->token = $token_new;
				}
			} else {
				u_token($u->id, $token_new);
				$obj->data->token = $token_new;
			}
			unset($obj->data->token_creation_timestamp);
		} else {
			$obj->rc = -2;
			$obj->msg = "incorrect password";
		}
	}
	return $obj;
}

// 根据用户id读取token
function r_token($user_id)
{
	$rv = new stdClass();
	global $wpdb;
	$sql_statement = 'select * from keylab_secu where id = ' . $user_id;
	return $wpdb->get_row($sql_statement);
}

// 是否存在该token
function has_token($token)
{
	$rv = new stdClass();
	global $wpdb;
	$sql_statement = "select id from keylab_secu where token = '$token'";
	return $wpdb->get_row($sql_statement);
}

// 更新用户token
function u_token($user_id, $token)
{
	date_default_timezone_set('Asia/Hong_Kong');
	$rv = new stdClass();
	$status = false;
	global $wpdb;
	$status = $wpdb->update(
		"keylab_secu",
		array(
			'token'       => $token,
			'token_creation_timestamp'  => current_time('mysql')
		),
		array(
			'id'    => $user_id
		),
		array('%s', '%s'),
		array('%d')
	);
	return $status;
}

// 获取push id
function r_push_id($user_id)
{
	$rv = new stdClass();
	global $wpdb;
	$sql_statement = 'select push_id from keylab_secu where id = ' . $user_id;
	return $wpdb->get_var($sql_statement);
}

// 更新push id
function u_push_id($user_id, $push_id)
{
	$rv = new stdClass();
	$status = false;
	global $wpdb;
	$status = $wpdb->update(
		"keylab_secu",
		array(
			'push_id'  => $push_id
		),
		array(
			'id'    => $user_id
		),
		array('%s'),
		array('%d')
	);
	return $status;
}

// 普通推送
function send_jpush($reg_id, $data)
{
	$client = new \JPush\Client('53ffa35a64073e4adaf09822', 'da47a09999ca109e3f30c3d9');
	if (!empty($data)) {
		$push = $client->push();
		$push->setPlatform('all');
		if (empty($reg_id) || $reg_id == 0) {
			$push->addAllAudience();
		} else if (is_array($reg_id)) {
			foreach ($reg_id as $r_id) {
				$push->addRegistrationId($r_id);
			}
		} else {
			$push->addRegistrationId($reg_id);
		}
		$push->androidNotification($data['alert'], [
			'title' => $data['title'],
			'alert_type' => 6,
			'extras' => $data['extras']
		]);
		$ios_sound = "boom.caf";
		if ($data['extras']['sound_type'] == "carpark") {
			$ios_sound = "carpark.caf";
		}
		$push->iosNotification($data['title'], [
			'badge' => '0',
			'sound' => $ios_sound,
			'extras' => $data['extras']
		]);
		$res = $push->send();
		return $res['http_code'] == 200;
	} else {
		return false;
	}
}

// 静默推送
function send_jpush_msg($reg_id, $data)
{
	$client = new \JPush\Client('53ffa35a64073e4adaf09822', 'da47a09999ca109e3f30c3d9');
	if (!empty($data)) {
		$push = $client->push();
		$push->setPlatform('all');
		if (empty($reg_id) || $reg_id == 0) {
			$push->addAllAudience();
		} else if (is_array($reg_id)) {
			foreach ($reg_id as $r_id) {
				$push->addRegistrationId($r_id);
			}
		} else {
			$push->addRegistrationId($reg_id);
		}
		$push->message($data['alert'], [
			'title' => $data['title'],
			'content_type' => 'text',
			'extras' => $data['extras']
		]);
		$push->iosNotification('', [
			'badge' => '0',
			'extras' => $data['extras'],
			'content-available' => true
		]);
		$res = $push->send();
		return $res['http_code'] == 200;
	} else {
		return false;
	}
}

// function c_alert($uid,$title,$content,$image){
// 	date_default_timezone_set('Asia/Hong_Kong');
// 	global $wpdb;
// 	$res = $wpdb->insert(
// 		'keylab_alert',
// 		array(
// 			'uid'=>$uid,
// 			'title'=>$title,
// 			'content'=>$content,
// 			'image'=>$image,
// 			'created_time'=>current_time( 'mysql' ),
// 			'pushed'=>0
// 		),
// 		array('%d','%s','%s','%d','%s','%d')
// 	);
// 	return $res;
// }

// 创建新alert记录
function c_alert($title, $content, $cat = 0, $msg_id = 0)
{
	date_default_timezone_set('Asia/Hong_Kong');
	global $wpdb;
	if ($msg_id > 0) {
		$m_a = r_alert_by_msgid($msg_id);
		if ($m_a != null) {
			return 0;
		}
	}
	$res = $wpdb->insert(
		'keylab_alert_msg',
		array(
			'title' => $title,
			'content' => $content,
			'creation_time' => current_time('mysql'),
			'update_time' => current_time('mysql'),
			'msgid' => $msg_id,
			'pushed' => 0,
			'cat' => $cat
		),
		array('%s', '%s', '%s', '%s', '%d', '%d', '%d')
	);
	if ($res !== false) {
		$res = $wpdb->insert_id;
	}else{
		$res = -1;
	}
	return $res;
}

// 获取alert详情
function r_alert($id)
{
	global $wpdb;
	return $wpdb->get_row($wpdb->prepare("select * from keylab_alert_msg where id = %d", $id));
}

function r_alert_by_msgid($msg_id)
{
	global $wpdb;
	return $wpdb->get_row($wpdb->prepare("select * from keylab_alert_msg where msgid = %d", $msg_id));
}

// 更新alert状态
function u_alert_status($id, $status)
{
	date_default_timezone_set('Asia/Hong_Kong');
	global $wpdb;
	$res = $wpdb->update(
		'keylab_alert_msg',
		array('pushed' => $status, 'update_time' => current_time('mysql')),
		array('id' => $id),
		array('%d', '%s'),
		array('%d')
	);
	return $res;
}

// 获取对应分类/状态的alerts
function get_alerts($status = 0, $cat = 0)
{
	global $wpdb;
	if ($status == -1) {
		$sql = $wpdb->prepare("select * from keylab_alert_msg where cat = %d", $cat);
	} else {
		$sql = $wpdb->prepare("select * from keylab_alert_msg where cat = %d and pushed = %d", $cat, $status);
	}
	$rows = $wpdb->get_results($sql);
	foreach ($rows as $row) {
		if (strpos($row->title, "Notification") == 0) {
			$row->title = str_replace("Notification", "Notification " . ((int)$row->id - 12300), $row->title);
		}
	}
	return $rows;
}

// 获取对应分类/状态的alerts
function get_alerts_by_cat($status = 0, $cat = 0, $offset = 0, $limit = 20)
{
	global $wpdb;
	if ($status == -1) {
		$sql = $wpdb->prepare("select * from keylab_alert_msg where cat = %d order by creation_time desc limit %d,%d", $cat, $offset, $limit);
	} else if ($status == -2) {
		$sql = $wpdb->prepare("select * from keylab_alert_msg where cat in (0,%d) order by creation_time desc limit %d,%d", $cat, $offset, $limit);
	} else {
		$sql = $wpdb->prepare("select * from keylab_alert_msg where cat = %d and pushed = %d order by creation_time desc limit %d,%d", $cat, $status, $offset, $limit);
	}
	$rows = $wpdb->get_results($sql);
	foreach ($rows as $row) {
		if (strpos($row->title, "Notification") == 0) {
			$row->title = str_replace("Notification", "Notification " . ((int)$row->id - 12300), $row->title);
		}
	}
	return $rows;
}

// 获取用户所属组
function get_secu_cat($uid, $is_implode = false)
{
	global $wpdb;
	$res = $wpdb->get_results($wpdb->prepare("select meta_value from keylab_secu_meta where uid = %d and meta_key = %s", $uid, 'cat'));
	$cats = ["0"];
	foreach ($res as $key => $value) {
		$cats[] = $value->meta_value;
	}
	return $is_implode ? implode(',', $cats) : $cats;
}

// 获取组内所有push id
function get_push_id_by_cat($cat)
{
	global $wpdb;
	if ($cat == 0 || $cat == null) {
		$res = $wpdb->get_results("select push_id from keylab_secu");
	} else {
		$res = $wpdb->get_results($wpdb->prepare("select push_id from keylab_secu where id in (select uid from keylab_secu_meta where meta_key = %s and meta_value = %s)", 'cat', "" . $cat));
	}
	$ids = [];
	foreach ($res as $key => $value) {
		if (!empty($value->push_id)) {
			$ids[] = $value->push_id;
		}
	}
	return $ids;
}

// 获取已打开该推送消息的所有push id
function get_opened_push_id($alert_id)
{
	global $wpdb;
	$res = $wpdb->get_results($wpdb->prepare("select push_id from keylab_secu where id in (select uid from keylab_alert_opened where msg_id = %d)", $alert_id));
	$ids = [];
	foreach ($res as $key => $value) {
		if (!empty($value->push_id)) {
			$ids[] = $value->push_id;
		}
	}
	return $ids;
}

// 添加用户到组
// 0 - 所有
// 1 - 测试组1(lau、aiden)
// 2 - 测试组2(lau)
function add_secu_cat($uid, $cat)
{
	global $wpdb;
	$cats = get_secu_cat($uid);
	if (!in_array($cat, $cats)) {
		$res = $wpdb->insert(
			'keylab_secu_meta',
			array(
				'uid' => $uid,
				'meta_key' => 'cat',
				'meta_value' => $cat
			),
			array('%d', '%s', '%s')
		);
		return $res !== false ? 1 : -2;
	} else {
		return -1;
	}
}

// 通过uid获取alerts
function get_alerts_by_uid($status, $uid, $offset = 0, $limit = 20)
{
	global $wpdb;

	// Jacky temporarily displayed status =1 
	// 06 2020 for eas point
	$cats = get_secu_cat($uid, true);
	$cats = empty($cats) ? "0" : $cats;
	// if ($uid==8) {
	// 	$sql = $wpdb->prepare("select am.*,ao.uid,ao.creation_time as opened_time from keylab_alert_msg as am left join keylab_alert_opened as ao on am.id = ao.msg_id and ao.uid = %d where am.cat in (" . $cats . ") order by am.creation_time desc limit %d,%d", $uid, $offset, $limit);
	// } else {
	// 	$sql = $wpdb->prepare("select am.*,ao.uid,ao.creation_time as opened_time from keylab_alert_msg as am left join keylab_alert_opened as ao on am.id = ao.msg_id and ao.uid = %d where am.pushed = %d and am.cat in (" . $cats . ") order by am.creation_time desc limit %d,%d", $uid, $status, $offset, $limit);
	// }

	$sql = $wpdb->prepare("select * from keylab_alert_msg where pushed = %d and cat in (" . $cats . ") order by creation_time desc limit %d,%d", $status, $offset, $limit);
	$result = $wpdb->get_results($sql);
	foreach ($result as $r) {
		if (strpos($r->title, "Notification") == 0) {
			$r->title = str_replace("Notification", "Notification " . ((int)$r->id - 12300), $r->title);
		}
		$row = $wpdb->get_row($wpdb->prepare("select * from keylab_alert_opened where msg_id = %d and uid = %d", $r->id, $uid));
		if ($row == null) {
			$r->uid = "";
			$r->opened_time = "";
		} else {
			$r->uid = $row->uid;
			$r->opened_time = $row->creation_time;
		}
	}
	return $result;
}

// 获取alert详情
function get_alert_by_id($id)
{
	global $wpdb;
	$row = $wpdb->get_row($wpdb->prepare("select * from keylab_alert_msg where id = %d", $id));
	if (strpos($row->title, "Notification") == 0) {
		$row->title = str_replace("Notification", "Notification " . ((int)$row->id - 12300), $row->title);
	}
	return $row;
}

// 根据alert发送推送
function send_alert($alert, $push_ids = 0)
{
	if ($alert->pushed == 0) {
		$content = strip_tags($alert->content);
		if (strlen($content) > 1000) {
			$content = mb_substr($content, 0, 1000);
		}
		$data = [
			'title' => $alert->title,
			'alert' => $content,
			'extras' => [
				'msg_id' => $alert->id
			]
		];
		$res = send_jpush($push_ids, $data);
		if ($res) {
			u_alert_status($alert->id, 1);
		}
		return $res;
	} else {
		return false;
	}
}

// 推送carpark消息
function send_to_carpark($id, $cat = 0)
{
	// 创建carpark alert(不创建的话不会显示在app list)
	// $id = c_alert($title, $content, $cat);
	if ($id > 0) {
		// 读取carpark alert
		$alert = r_alert($id);
		// 截取content防止超出长度
		$content = strip_tags($alert->content);
		if (strlen($content) > 1000) {
			$content = mb_substr($content, 0, 1000);
		}
		// alert data
		$data = [
			'title' => $alert->title,
			'alert' => $content,
			'extras' => [
				'msg_id' => $alert->id,
				'sound_type' => 'carpark'
			]
		];
		// send
		$res = send_jpush(get_push_id_by_cat($cat), $data);
		if ($res) {
			u_alert_status($alert->id, 1);
		}
		return $res ? 1 : -2;
	} else {
		return -1;
	}
}

// log
function insert_log($uid, $token, $token_status, $action, $remark = "")
{
	date_default_timezone_set('Asia/Hong_Kong');
	global $wpdb;
	$res = $wpdb->insert(
		'keylab_secu_log',
		array(
			'uid' => $uid,
			'token' => $token,
			'token_status' => $token_status,
			'date' => current_time('mysql'),
			'action' => $action,
			'remark' => $remark
		),
		array('%d', '%s', '%d', '%s', '%s', '%s')
	);
	return $res !== false;
}

function get_request_string()
{
	$str = "";
	foreach ($_REQUEST as $k => $r) {
		$str .= $k . "=" . $r . "&";
	}
	return mb_substr($str, 0, -1);
}

// 获取分组内所有users
function get_secu_users($cat = 0, $page = 1, $limit = 10)
{
	global $wpdb;
	$limit = $limit > 0 ? $limit : 10;
	$page = $page > 0 ? $page : 1;
	$offset = ($page - 1) * $limit;
	if ($cat == 0) {
		$users = $wpdb->get_results($wpdb->prepare("select * from keylab_secu limit %d,%d", $offset, $limit));
	} else {
		$users = $wpdb->get_results($wpdb->prepare("select * from keylab_secu where id in (select uid from keylab_secu_meta where meta_key = %s and meta_value = %s) limit %d,%d", 'cat', (string) $cat, $offset, $limit));
	}
	foreach ($users as $u) {
		$u->cat = get_secu_cat($u->id, true);
	}
	return $users;
}

function send_alert_to_unopened_users($cat = 0)
{
	date_default_timezone_set('Asia/Hong_Kong');
	// 获取分组0所有推送消息
	$alerts = get_alerts(1, $cat);
	// 获取分组0所有push id
	$ids = get_push_id_by_cat($cat);
	// 遍历
	foreach ($alerts as $alert) {
		if ($alert->msgid > 0) {
			// 获取当前alert已读用户push id
			$opened_ids = get_opened_push_id($alert->id);
			// var_dump($opened_ids);
			// 获取当前alert未读用户push id
			$last_ids = array_diff($ids, $opened_ids);
			if (!empty($last_ids)) {
				// var_dump($alert);
				// var_dump($last_ids);
				// 判断当前时间 如果超过alert更新时间1分钟,给未读用户再次发送推送
				$send_time = strtotime($alert->update_time . " +1 minute");
				$now_time = strtotime(current_time('mysql'));
				if ($now_time > $send_time) {
					$content = strip_tags($alert->content);
					if (strlen($content) > 1000) {
						$content = mb_substr($content, 0, 1000);
					}
					$data = [
						'title' => $alert->title,
						'alert' => $content,
						'extras' => [
							'msg_id' => $alert->id
						]
					];
					send_jpush($last_ids, $data);
				}
			}
		}
	}
}