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/page-lau-test.php
<?php
/* Template Name: lau-test */
// ini_set("display_errors", "On");
// error_reporting(E_ALL | E_STRICT);

require_once('key_alert_functions.php');

// c_alert("测试3下","这里是测试内容3哦",651);
// $alert = r_alert(3);
// send_alert($alert);

if ($_REQUEST['key']) {
	$key = $_REQUEST['key'];
	$data = [
		'title' => "logout",
		'alert' => "logout",
		'extras' => [
			'logout' => 1
		]
	];
	$res = send_jpush_msg($key, $data);
	var_dump($res);
} else if ($_REQUEST['action'] == "test") {
	$alerts = get_alerts(1);
	var_dump($alerts);
} else if ($_REQUEST['test'] == "push") {
	$login = $_REQUEST['login'];
	$pass = $_REQUEST['password'];
	$username = $_REQUEST['username'];
	$res = insert_secu_user($login, $pass, $username);
	echo json_encode($res);
} else if ($_REQUEST['test'] == "secu_meta") {
	// $cats = get_alerts_by_uid(1,$_REQUEST['uid']);
	// var_dump($cats);
	var_dump(get_push_id_by_cat(1));
} else if ($_REQUEST['test'] == "users") {

	$users = get_secu_users(1);
	echo json_encode($users);
} else if ($_REQUEST['test'] == "add_cat") {
	$res = add_secu_cat(5, 2);
	var_dump($res);
} else if ($_REQUEST['test'] == "log") {
	// insert_log($_REQUEST['uid],'',get_request_string());
} else if ($_REQUEST['test'] == "logout") {

	$rv = new stdClass();
	$uid = $_REQUEST['uid'];
	$push_id = $_REQUEST['push_id'];
	if ($uid > 0 && !empty($push_id)) {
		$old_push_id = r_push_id($uid);
		if ($old_push_id == $push_id) {
			$res = u_push_id($uid, "");
			if ($res !== false) {
				$rv->rc = 1;
				$rv->msg = "success";
			} else {
				$rv->rc = -3;
				$rv->msg = "update failed";
			}
		} else {
			$rv->rc = -2;
			$rv->msg = "push id does not match";
		}
	} else {
		$rv->rc = -1;
		$rv->msg = "missing params";
	}
} else if ($_REQUEST['test'] == "filter") {
	send_alert_to_unopened_users(2);
} else if ($_REQUEST['test'] == "send_notification") {
	$token = "dkjwn5WQTmehXQ67WoRugY:APA91bHtx9vW9rBsiF2Ex3uwKPkdlyD-a3bkJGplSien-Fw1fzccztymz-horQJheMyHnOd1pacdXIkjpXeWkrN53Mgm70tMlj3zpZhvNNmAopfGO4mLErybVYvA-x-OeiHbVavdedgg";
	$title = "test";
	$content = "test123";
	$res = send_app_fcm($token, $title, $content);
	var_dump($res);
} else if ($_REQUEST['test'] == "get_alerts") {
	function get_alerts_by_uid2($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 * from keylab_alert_msg where pushed = %d and cat in (" . $cats . ") order by creation_time desc limit %d,%d", $status, $offset, $limit);
			// $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 ao.uid asc, am.creation_time desc,ao.creation_time desc limit %d,%d", $uid, $status, $offset, $limit);
			// $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);
		}
		$result = $wpdb->get_results($sql);
		foreach ($result as $r) {
			$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;
			}
		}
		echo $sql;
		return $result;
	}
	var_dump(get_alerts_by_uid2(1, 5));
	// var_dump(get_push_id_by_cat($cat));
} else if ($_REQUEST['test'] == "send_alert") {
	send_alert(r_alert(12399), "18071adc038ba8e3ada");
} else if ($_REQUEST['json']) {
	$time = date("Y-m-d H:i:s", time());
	$log = "Date: " . $time . PHP_EOL .
		"Content: " . PHP_EOL
		. $_REQUEST['json'] . PHP_EOL .
		"--------------------------------" . PHP_EOL;
	file_put_contents(get_stylesheet_directory() . '/tmp/log_car_park.log', $log, FILE_APPEND);
} else if ($_REQUEST['test'] == "add_epc") {
	$res = insert_secu_user("epc_parking", "9aXBQmUUh6nbN&", "epc_parking", "");
	var_dump($res);
} else if ($_REQUEST['test'] == "token_e") {
	function secu_do_login_4api($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,
					'nbf' => time() + 4800,
					'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;
						$obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($old_token->token));
					} else {
						u_token($u->id, $token_new);
						$obj->data->token = $token_new;
						$obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($token_new));
					}
				} else {
					u_token($u->id, $token_new);
					$obj->data->token = $token_new;
					$obj->data->expiry = date('Y-m-d H:i:s', JwtAuthClass::getTokenExp($token_new));
				}
				unset($obj->data->token_creation_timestamp);
			} else {
				$obj->rc = -2;
				$obj->msg = "incorrect password";
			}
		}
		return $obj;
	}
	$login = $_REQUEST['login'];
	$password = $_REQUEST['password'];
	$push_id = $_REQUEST['push_id'];
	$push_id = empty($push_id) ? "" : $push_id;
	$rv = secu_do_login_4api($login, $password, $push_id);

	// insert_log(0, "", 0, get_request_string(), "登入:rc=" . $rv->rc . ",msg=" . $rv->msg);
	echo json_encode($rv);
} else if ($_REQUEST['test'] == "test_push") {
	$cat = 1;
	$title = "Notification - Car Park Activity";
	$content = '
	<style>
		table{
			border-collapse: collapse;
			width: 100%;
		}
		td{
			border: 1px solid #000;
			padding: 2px 5px;
		}
	</style>
	<table border="1" cellspacing="0">
		<tr>
			<td colspan="3"><b style="display: inline-block;width:100px;">Carpark ID: </b>1</td>
		</tr>
		<tr>
			<td colspan="3"><b style="display: inline-block;width:100px;">Area ID: </b>test_area</td>
		</tr>
		<tr style="background-color: #ccc;font-weight: bold;">
			<td>Number Plate</td>
			<td>Time</td>
			<td>Status</td>
		</tr>
		<tr style="background-color: lightskyblue;">
			<td>A123456</td>
			<td>2020-02-18</td>
			<td>parked</td>
		</tr>
		<tr style="background-color: lightskyblue;">
			<td>A123457</td>
			<td>2020-08-14</td>
			<td>parked</td>
		</tr>
	</table>
	';
	$id = c_alert($title, $content, $cat);
	$res = send_to_carpark($id, $cat);
	var_dump($res);
} else if ($_REQUEST['test'] == "position") {
	// function get_positions()
	// {
	// 	global $wpdb;
	// 	return $wpdb->get_results("SELECT * FROM `keylab_meta` WHERE property_id = 0 and meta_key = 'position'");
	// }
	// $positions = get_positions();
	// global $wpdb;
	// $sql_statment = "select a.*,b.index_id,b.type,b.name_zh,b.login_tel,c.name_zh as property_name
	//  from keylab_property_propman as a left join keylab_users as b on a.user_id = b.user_id left join keylab_property as c on a.property_id = c.property_id";
	// $users = $wpdb->get_results($sql_statment);
	// $result = new stdClass();
	// $result->rc = 1;
	// $result->msg = "success";
	// $result->list = [];
	// foreach($positions as $p){
	// 	$item = new stdClass();
	// 	$item = $p;
	// 	$sub = array_filter($users,function($u) use($p){
	// 		return $u->position == $p->meta_value;
	// 	});
	// 	$item->list = array_values($sub);
	// 	array_push($result->list,$item);
	// }
	// echo json_encode($result);

	date_default_timezone_set('Asia/Hong_Kong');
	global $wpdb;
	// $args = [
	// 	'role' => 'company'
	// ];
	// $users = get_users($args);
	// $sql_statment = "select a.*,b.user_id,b.index_id,b.type,b.name_zh,b.login_tel,c.name_zh as property_name
	//  from keylab_property_propman as a left join keylab_users as b on a.user_id = b.user_id left join keylab_property as c on a.property_id = c.property_id";
	$sql_statment = "select IF(STRCMP(a.position,'保安員') = 0,concat(a.position,'(',c.shift,')'),a.position) as display_position,a.*,
    b.index_id,b.name_zh,b.name_en,b.login_tel,b.email,b.position as new_position,c.birth_date,c.cert_due_date,
    c.cert_due_date2,c.cert_due_date3,c.cert_due_date4,c.physical_date,c.cert_no,c.identity_card,c.body_check_file,c.shift,
    c.license_no,
    c.propman_no,
    c.separation,
    c.exchange_date,
    c.departure_date,
    c.on_work_time,
    c.out_work_time,
    c.morning,
    c.noon,
    c.extra,
    c.daily_wage,
    c.wage,
    c.address,
    c.post_rank,
    c.HKID,
    c.certificate,
    c.QAS,
    c.bank_account,
    c.bank_name,
    c.personal_file
     from keylab_property_propman as a left join keylab_users as b on a.user_id = b.user_id left join keylab_property_propman_security as c on a.propman_id = c.propman_id";
	$rows = $wpdb->get_results($sql_statment);
	foreach ($rows as $row) {
		// var_dump($row);
		$id_card = empty($row->identity_card) ? "" : $row->identity_card;
		var_dump($id_card);
		// $wpdb->update(
		// 	'keylab_users',
		// 	array('identity_card' => $id_card),
		// 	array('user_id' => $row->user_id),
		// 	array('%s'),
		// 	array('%d'),
		// );
	}

	// 	$uid = 6;
	// 	$positions = get_positions($uid);
	// 	// var_dump($positions);
	// 	global $wpdb;
	// 	$sql_statment = "select a.*,b.index_id,b.type,b.name_zh,b.login_tel,c.name_zh as property_name
	//  from keylab_property_propman as a left join keylab_users as b on a.user_id = b.user_id left join keylab_property as c on a.property_id = c.property_id";
	// 	$sql_statment = $sql_statment . " where c.admin_wp_id = " . $uid;
	// 	$users = $wpdb->get_results($sql_statment);
	// 	$result = new stdClass();
	// 	$result->rc = 1;
	// 	$result->msg = "success";
	// 	$result->list = [];
	// 	foreach ($positions as $p) {
	// 		$item = new stdClass();
	// 		$item = $p;
	// 		$sub = array_filter($users, function ($u) use ($p) {
	// 			return $u->position == $p->position;
	// 		});
	// 		$item->list = array_values($sub);
	// 		array_push($result->list, $item);
	// 	}
	// 	echo json_encode($result);
} else if ($_REQUEST['test'] == "unit-custom") {
	global $wpdb;
	$property_list = $wpdb->get_results($wpdb->prepare("select property_id from keylab_property"));
	// var_dump($property_list);
	foreach ($property_list as $p) {
		$unit_list = $wpdb->get_results($wpdb->prepare("select unit_id,property_id,block,floor,unit from keylab_property_unit_list where property_id = %d order by floor asc, unit asc", $p->property_id));
		// var_dump($unit_list);
		$block_list = [];
		// $results = [];
		foreach ($unit_list as $k => $v) {
			if (!in_array($v->block, $block_list)) {
				$block_list[] = $v->block;
			}
		}
		// var_dump($block_list);
		foreach ($block_list as $block) {
			$block_unit = array_filter($unit_list, function ($v, $k) use ($block) {
				return $v->block == $block;
			}, 1);
			// var_dump($block);
			// var_dump($block_unit);
			$floors = [];
			$units = [];
			foreach ($block_unit as $b_unit) {
				if (!in_array($b_unit->floor, $floors)) {
					$floors[] = $b_unit->floor;
				}
				if (!in_array($b_unit->unit, $units)) {
					$units[] = $b_unit->unit;
				}
			}
			asort($floors);
			asort($units);
			$floors_str = implode(",", $floors);
			$units_str = implode(",", $units);
			// var_dump($floors_str);
			// var_dump($units_str);
			$data = [
				'property_id' => $p->property_id,
				'block' => $block,
				'floor_range' => $floors_str,
				'unit_range' => $units_str,
			];
			// var_dump($data);
			// $res = $wpdb->insert('keylab_property_unit',$data);
			// var_dump($res);
			// var_dump($wpdb->insert_id);
		}
	}
} else if ($_REQUEST['action'] == "phpword") {
	require_once '/home/lijunjie/vendor/autoload.php';
	show_php_error();
	$phpWord = new \PhpOffice\PhpWord\PhpWord();
	$section = $phpWord->addSection();
	// Adding Text element to the Section having font styled by default...
	$section->addText(
		'"Learn from yesterday, live for today, hope for tomorrow. '
			. 'The important thing is not to stop questioning." '
			. '(Albert Einstein)'
	);
	// Adding Text element with font customized inline...
	$section->addText(
		'"Great achievement is usually born of great sacrifice, '
			. 'and is never the result of selfishness." '
			. '(Napoleon Hill)',
		array('name' => 'Tahoma', 'size' => 10)
	);

	// Adding Text element with font customized using named font style...
	$fontStyleName = 'oneUserDefinedStyle';
	$phpWord->addFontStyle(
		$fontStyleName,
		array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
	);
	$section->addText(
		'"The greatest accomplishment is not in never falling, '
			. 'but in rising again after you fall." '
			. '(Vince Lombardi)',
		$fontStyleName
	);

	// Adding Text element with font customized using explicitly created font style object...
	$fontStyle = new \PhpOffice\PhpWord\Style\Font();
	$fontStyle->setBold(true);
	$fontStyle->setName('Tahoma');
	$fontStyle->setSize(13);
	$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
	$myTextElement->setFontStyle($fontStyle);
	$file = 'HelloWorld.docx';
	header("Content-Description: File Transfer");
	header('Content-Disposition: attachment; filename="' . $file . '"');
	header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
	header('Content-Transfer-Encoding: binary');
	header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	header('Expires: 0');
	$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
	$objWriter->save('php://output');
} else if ($_REQUEST['action'] == "get_alert_by") {
	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 " . $row->id, $row->title);
			}
		}
		return $rows;
	}
} else if ($_REQUEST['action'] == "test_incident") {

	$rv = new stdClass();

	$property_id = $_REQUEST['property_id'];
	$assigned_to = $_REQUEST['assigned_to'];

	$status = false;

	$sql_statment = "SELECT * FROM keylab_property_incident";
	$where = " where 1";

	if (!empty($property_id)) {
		$where = $where . " and property_id = " . $property_id;
	}
	if (!empty($assigned_to)) {
		$where = $where . " and assigned_to = " . $assigned_to;
	}

	$sql_statment = $sql_statment . $where . ' ORDER BY incident_id DESC';

	$result = $wpdb->get_results($sql_statment);
	$rv->list = $result;
	foreach ($rv->list as $item) {
		$item->incident_file = unserialize($item->incident_file) ?: [];
		$item->assigned_to_user = $wpdb->get_row($wpdb->prepare("SELECT * FROM keylab_users WHERE user_id = %d", $item->assigned_to));
		$item->property = $wpdb->get_row($wpdb->prepare("SELECT name_zh,name_en FROM keylab_property WHERE property_id = %d", $item->property_id));
	}
	exit(json_encode($rv));
} else if ($_REQUEST['action'] == "test_auth") {
	$sql = "select distinct k_role from keylab_permission";
	$roles = $wpdb->get_results($sql);
	// $roles = ['company', 'owners', 'owners_read', 'accountant', 'director'];
	// $names = ['物管公司', '法團成員', '只讀賬戶', 	 '會計', 		'物管主任' ];

	// $arr = [];
	// foreach ($roles as $role) {
	// 	$tmp['key'] = $role->k_role;
	// 	$tmp['name'] = get_role_name($role->k_role);
	// 	$arr[] = $tmp;
	// }
	// var_dump($arr);


	// 24 通訊組列表
	// 25 採購
	// 26 事件管理
	$tmp_permission = [
		'company' => [
			['item' => 24, 'caps' => 2],
			['item' => 25, 'caps' => 2],
			['item' => 26, 'caps' => 2],
		],
		'owners' => [
			['item' => 24, 'caps' => 2],
			['item' => 25, 'caps' => 2],
			['item' => 26, 'caps' => 2],
		],
		'owners_read' => [
			['item' => 24, 'caps' => 1],
			['item' => 25, 'caps' => 1],
			['item' => 26, 'caps' => 1],
		],
		'accountant' => [
			['item' => 24, 'caps' => 1],
			['item' => 25, 'caps' => 2],
			['item' => 26, 'caps' => 1],
		],
		'director' => [
			['item' => 24, 'caps' => 2],
			['item' => 25, 'caps' => 2],
			['item' => 26, 'caps' => 2],
		],
	];

	$arr = [];
	foreach ($tmp_permission as $role => $permission) {
		foreach ($permission as $p) {
			$p['k_role'] = $role;
			$arr[] = $p;
			// insert into keylab_permission
			// $wpdb->insert('keylab_permission',$p);
		}
	}
	var_dump($arr);
} else if ($_REQUEST['test'] == "ios_push") {
	date_default_timezone_set('Asia/Hong_Kong');
	
	$user_id = 57;
	$token_type = "ios_fcm_token";
	$ios_token = get_user_meta(57, $token_type, true);
	$arr = [
		'user_id' => $user_id,
		'type' => $token_type,
		'token' => $ios_token,
		'title' => "test " . date("Y-m-d H:i:s", time()),
		'content' => "test by lau"
	];
	$res = send_app_fcm($arr['token'], $arr['title'], $arr['content']);
	$result = new stdClass();
	$result->params = $arr;
	$result->result = json_decode($res);
	echo json_encode($result);
}