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/functions.php
<?php
add_theme_support('post-thumbnails'); // 缩略图支持

add_action('admin_menu', 'my_admin_menu');

function my_admin_menu()
{
    add_menu_page('custom_site_info', 'Site 設定', 'edit_others_posts', 'custom_site_info', 'custom_site_info_callback', '', 3);
}

function custom_site_info_callback(){
    require_once 'wp-admin-site-info.php';
}

/**
 * 注册post类型,用于获取缩略图信息
 * @return [type] [description]
 */
function yt_setup_post()
{
    $args_block = array(
        'label'        => __('大廈', 'ormedia'),
        'show_ui'      => true,
        'show_in_menu' => true,
        'supports'     => array('title', 'author'),
    );
    $args_entity = array(
        'label'        => __('個人或公司', 'ormedia'),
        'show_ui'      => true,
        'show_in_menu' => true,
        'supports'     => array('title', 'editor', 'author'),
    );
    $args_contract = array(
        'label'        => __('物業合約', 'ormedia'),
        'show_ui'      => true,
        'show_in_menu' => true,
        'supports'     => array('title', 'author'),
    );
    $args_car_park = array(
        'labels'             => array(
            'name'           => __('Car Park', 'ormedia'),
            'singular_name'  => __('Car Park', 'ormedia')
        ),
        'public'             => true,
        'menu_icon'          => 'dashicons-images-alt2',
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'rewrite'            => array('slug' => 'orm_car_park'),
        'supports'           => array('title'),
        'capabilities' => array(
            'edit_post' => 'edit_car_park',
            'edit_posts' => 'edit_car_park',
            'edit_others_posts' => 'edit_other_car_park',
            'publish_posts' => 'publish_car_park',
            'read_post' => 'read_car_park',
            'read_private_posts' => 'read_private_car_park',
            'delete_post' => 'delete_car_park',
            'delete_posts' => 'delete_car_park',
            'delete_others_posts' => 'delete_other_car_park',
        ),
    );
    register_post_type('orm_block', $args_block);
    register_post_type('orm_entity', $args_entity);
    register_post_type('orm_contract', $args_contract);
    register_post_type('orm_car_park', $args_car_park);
}

add_action('init', 'yt_setup_post');


function log_data($data)
{
    $file = fopen(get_template_directory() . "/tmp/log.log", "w");
    if (!empty($data)) {
        fwrite($file, serialize($data));
    } else {
        fwrite($file, "empty");
    }
    fclose($file);
}

function get_property_meta($property_id, $key)
{
    global $wpdb;
    $res = $wpdb->get_var($wpdb->prepare("select meta_value from keylab_meta where property_id = %d and meta_key = %s", $property_id, $key));
    return $res;
}

function delete_property_meta($property_id)
{
    global $wpdb;
    $status = $wpdb->delete("keylab_meta", array("property_id" => $property_id));
    return $status;
}

function update_property_meta($property_id, $key, $val)
{
    global $wpdb;
    if (get_property_meta($property_id, $key) !== "0" && empty(get_property_meta($property_id, $key))) {
        $res = $wpdb->insert(
            'keylab_meta',
            array(
                'property_id' => $property_id,
                'meta_key' => $key,
                'meta_value' => empty($val) ? '0' : $val
            ),
            array('%d', '%s', '%s')
        );
    } else {
        $res = $wpdb->update(
            'keylab_meta',
            array('meta_value' => empty($val) ? '0' : $val),
            array('property_id' => $property_id, 'meta_key' => $key),
            array('%s'),
            array('%d', '%s')
        );
    }
    return $res !== false;
}

function insert_calendar($value_arr)
{
    global $wpdb;
    return $wpdb->insert("keylab_calendar", $value_arr);
}

function update_calendar($value_arr, $where_arr)
{
    global $wpdb;
    return $wpdb->update("keylab_calendar", $value_arr, $where_arr);
}

function delete_calendar($where_arr)
{
    global $wpdb;
    return $wpdb->delete("keylab_calendar", $where_arr);
}

function computed_period_day($first_report_date, $period_value, $period_unit)
{
    switch ($period_unit) {
        case '天':
            $first_report_date = strtotime("$first_report_date+" . $period_value . "day");
            break;
        case '週':
            $first_report_date = strtotime("$first_report_date+" . $period_value . "week");
            break;
        case '月':
            $first_report_date =  strtotime("$first_report_date+" . $period_value . "month");
            break;
        case '年':
            $first_report_date =  strtotime("$first_report_date+" . $period_value . "year");
            break;
        default:
            $first_report_date =  strtotime($first_report_date);
            break;
    }
    return date('Y-m-d', $first_report_date);
}

function get_role_name($role)
{
    global $wp_roles;
    if (!isset($wp_roles))
        $wp_roles = new WP_Roles();
    return $wp_roles->roles[$role]['name'];
}

function get_table_name($table_name)
{
    global $wpdb;

    $sql_statment = "select table_comment from information_schema.tables  where table_name = '$table_name' && table_schema = 'keylab'";
    return $wpdb->get_var($sql_statment);
}

function get_col_name($table_data, $table_name)
{
    global $wpdb;

    $result_arr = [];

    $sql_statment = "select COLUMN_NAME,column_comment from INFORMATION_SCHEMA.Columns where table_name='$table_name' and table_schema='keylab'";
    $col_arr = $wpdb->get_results($sql_statment);
    foreach ($table_data as $key => $value) {
        foreach ($col_arr as $value2) {
            if ($value2->COLUMN_NAME == $key) {
                if ($value2->column_comment != '') {
                    $result_arr[$value2->column_comment] = $value;
                } else {
                    $result_arr[$value2->COLUMN_NAME] = $value;
                }
                break;
            }
        }
    }

    return $result_arr;
}

function keylab_data_log_by_user2($status, $old_data, $new_data, $sql, $table_name, $relate_id)
{
    global $wpdb;

    $allheader = array_change_key_case(getallheaders());

    if (empty($allheader['log_admin_id']) || empty($allheader['log_user_id'])) {
    } else {
        if ($status) {
            $data = [
                'admin_id'  => $allheader['log_admin_id'],
                'user_id'   => $allheader['log_user_id'],
                'user_name' => get_user_by('ID', $allheader['log_user_id'])->user_login,
                'time'      => date("Y-m-d H:i:s", strtotime("+8 hour")),
                'sql_panel' => $sql,
                'table_name' => $table_name,
                'log_relate_id' => intval($relate_id)
            ];

            if ($old_data !== false && $new_data == false) {
                $data['old_data'] = json_encode(get_col_name($old_data, $table_name));
                $data['action'] = 'delete';

                $data['remark'] = '';
                if (!empty($allheader['log_property_id'])) {
                    $data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
                }
                $data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 刪除了' . get_table_name($table_name) . ',編號為' . $relate_id;
            }

            if ($old_data !== false && $new_data !== false) {
                $data['old_data'] = json_encode(get_col_name($old_data, $table_name));
                $data['new_data'] = json_encode(get_col_name($new_data, $table_name));
                $data['action'] = 'update';

                $data['remark'] = '';
                if (!empty($allheader['log_property_id'])) {
                    $data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
                }
                $data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 更新了' . get_table_name($table_name) . ',編號為' . $relate_id;
            }

            if ($old_data == false && $new_data !== false) {
                $data['new_data'] = json_encode(get_col_name($new_data, $table_name));
                $data['action'] = 'create';

                $data['remark'] = '';
                if (!empty($allheader['log_property_id'])) {
                    $data['remark'] = $data['remark'] . '物業編號:' . $allheader['log_property_id'] . '; ';
                }
                $data['remark'] = $data['remark'] . '用戶 ' . get_user_by('ID', $allheader['log_user_id'])->user_login . ' 新增了' . get_table_name($table_name) . ',編號為' . $relate_id;
            }

            $wpdb->insert("keylab_log", $data);
        }
    }

    return $data;
}

function keylab_data_log_by_user($status, $old_data, $new_data, $sql)
{
    global $wpdb;

    $allheader = array_change_key_case(getallheaders());
    if ($status) {
        $data = [
            'admin_id'  => $allheader['log_admin_id'],
            'user_id'   => $allheader['log_user_id'],
            'time'      => date("Y-m-d H:i:s", strtotime("+8 hour")),
            'sql_panel' => $sql
        ];

        if ($old_data !== false && $new_data == false) {
            $data['old_data'] = json_encode($old_data);
            $data['action'] = 'delete';
        }

        if ($old_data !== false && $new_data !== false) {
            $data['old_data'] = json_encode($old_data);
            $data['new_data'] = json_encode($new_data);
            $data['action'] = 'update';
        }

        if ($old_data == false && $new_data !== false) {
            $data['new_data'] = json_encode($new_data);
            $data['action'] = 'create';
        }

        $wpdb->insert("keylab_log", $data);
    } else {
        $wpdb->insert("keylab_log", [
            'admin_id'  => $allheader['log_admin_id'],
            'user_id'   => $allheader['log_user_id'],
            'action'    => 'error',
            'time'      => date("Y-m-d H:i:s", strtotime("+8 hour")),
            'sql_panel' => $sql
        ]);
    }

    return $data;
}

function keylab_login_log($log_user_id, $admin_id)
{
    global $wpdb;

    $wpdb->insert("keylab_log", [
        'admin_id'  => $admin_id,
        'user_id'   => $log_user_id,
        'user_name' => get_user_by('ID', $log_user_id)->user_login,
        'action'    => 'login',
        'remark'    => '用戶登錄',
        'time'      => date("Y-m-d H:i:s", strtotime("+8 hour"))
    ]);
}

function keylab_else_log($status, $remark)
{
    global $wpdb;

    $allheader = array_change_key_case(getallheaders());

    if (empty($allheader['log_admin_id']) || empty($allheader['log_user_id'])) {
    } else {

        $wpdb->insert("keylab_log", [
            'admin_id'  => $allheader['log_admin_id'],
            'user_id'   => $allheader['log_user_id'],
            'user_name' => get_user_by('ID', $allheader['log_user_id'])->user_login,
            'action'    => 'login',
            'remark'    => $remark,
            'time'      => date("Y-m-d H:i:s", strtotime("+8 hour"))
        ]);
    }
}

function update_file_in_use($file)
{
    foreach ($file as $key => $value) {
        update_post_meta($value['uid'], 'in_use', 1);
    }
}

function delete_file_in_use($file)
{
    foreach ($file as $key => $value) {
        if (!empty(wp_delete_attachment($value['uid']))) {
            delete_post_meta($value['uid'], 'in_use');
        } else {
            return false;
        }
    }
}

function find_diff_file($new_file, $old_file, &$a_delete)
{
    foreach ($new_file as $key => $value) {
        update_post_meta($value['uid'], 'in_use', 1);
    }
    foreach ($old_file as $key1 => $value1) {
        array_push($a_delete, $value1['uid']);
        foreach ($new_file as $key2 => $value2) {
            if ($value1['uid'] == $value2['uid']) {
                array_pop($a_delete);
            }
        }
    }
}



function send_mailjet_email($args)
{

    require_once "/var/www/html/wp-content/plugins/mailjet-for-wordpress/vendor/autoload.php";

    $body = [
        'FromEmail'  => "keylab@keylab.cc",
        'FromName'   => "keylab manage",
        'Subject'    => $args['subject'],
        'Html-part'  => $args['html'],
        'Recipients' => [],
    ];
    if (is_array($args['to'])) {
        if (!count($args['to'])) {
            return true;
        }
        foreach ($args['to'] as $t) {
            $to = ['Email' => $t];
            array_push($body['Recipients'], $to);
        }
    }


    $mailjet    = new Mailjet\Client("fdc0ca324e6e4bc2ecdeb4d78744aebc", "e1dc650b7700a6f110373e6cb433f29a");
    $mailjet->setConnectionTimeout(120);
    $response   = $mailjet->post(Mailjet\Resources::$Email, ['body' => $body]);
    $response->success() && var_dump($response->getData());

    return true;
}

function send_email_by_mailjet($args)
{
    require_once "/var/www/html/wp-content/plugins/mailjet-for-wordpress/vendor/autoload.php";

    $body = [
        'FromEmail'  => "keylab@keylab.cc",
        'FromName'   => "keylab manage",
        'Subject'    => $args['subject'],
        'Html-part'  => $args['html'],
        'Recipients' => [],
    ];
    if (is_array($args['to'])) {
        if (!count($args['to'])) {
            return true;
        }
        foreach ($args['to'] as $t) {
            $to = ['Email' => $t];
            array_push($body['Recipients'], $to);
        }
    }

    $mailjet    = new Mailjet\Client("fdc0ca324e6e4bc2ecdeb4d78744aebc", "e1dc650b7700a6f110373e6cb433f29a");
    $mailjet->setConnectionTimeout(120);
    $response   = $mailjet->post(Mailjet\Resources::$Email, ['body' => $body]);
    return $response->success();
}

function send_app_fcm($tokens, $title, $content)
{
    $url = "https://fcm.googleapis.com/fcm/send";
    $fields = array(
        'to' => ($tokens == "all") ? "/topics/all" : $tokens,
        'priority' => 'high',
        'forceStart' => '1',
        'notification' => array(
            'title' => $title,
            'body' => $content
        )
    );

    $fields = json_encode($fields);

    $headers = array(
        'Authorization: key=AAAARfV3Nf4:APA91bGHgZ4RBeLy5jqN5ETIB_9zhSBFhenEXzM8JeKSvcU_v847gy4wA0btf5EqMpJanR3UahL8rTMepxx_tZfcmm0e2kbEtLTZ9I77Zz_5DCG_slIuuzy2HMpkRoHIQdhXJjXP_-sN',
        'Content-Type: application/json',
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}

function get_real_property_id($user_id)
{
    global $wpdb;

    $all_property = get_user_meta($user_id, 'all_property', true);
    if (!empty($all_property) && $all_property == 1) {
        //新增all_property,包含主账户所有大厦
        $relate_wp_id = get_user_meta($user_id, 'relate_wp_id', true);
        $a_property_id_row = $wpdb->get_results("SELECT property_id FROM keylab_property where admin_wp_id = $relate_wp_id order by property_id asc");
        $a_property_id = [];
        foreach ($a_property_id_row as $row) {
            $a_property_id[] = $row->property_id;
        }
        return $a_property_id;
    } else {
        $real_property_id = get_user_meta($user_id, 'real_property_id', true);

        if (empty($real_property_id)) {
            return [];
        } else {
            return explode(',', $real_property_id);
        }
    }
}

function get_admin_property_id($user_id)
{
    global $wpdb;
    $sql_statment = "select property_id from keylab_property where admin_wp_id = '$user_id'";
    return $wpdb->get_results($sql_statment);
}

function get_users_meta_null($users_id, $key)
{
    global $wpdb;
    $res = $wpdb->get_results($wpdb->prepare("select meta_value from keylab_users_meta where users_id = %d and meta_key = %s", $users_id, $key));
    // echo $wpdb->last_query;
    if (count($res) == 0) {
        return null;
    } else {
        return $res[0]->meta_value;
    }
}

function get_users_meta($users_id, $key)
{
    global $wpdb;
    $res = $wpdb->get_results($wpdb->prepare("select meta_value from keylab_users_meta where users_id = %d and meta_key = %s", $users_id, $key));
    // echo $wpdb->last_query;
    if (count($res) == 0) {
        return '';
    } else {
        return $res[0]->meta_value;
    }
}

function delete_users_meta($users_id)
{
    global $wpdb;
    $status = $wpdb->delete("keylab_users_meta", array("users_id" => $users_id));
    return $status;
}

function update_users_meta($users_id, $key, $val)
{
    global $wpdb;
    if (get_users_meta_null($users_id, $key) === null) {
        $res = $wpdb->insert(
            'keylab_users_meta',
            array(
                'users_id' => $users_id,
                'meta_key' => $key,
                'meta_value' => $val
            ),
            array('%d', '%s', '%s')
        );
    } else {
        $res = $wpdb->update(
            'keylab_users_meta',
            array('meta_value' => $val),
            array('users_id' => $users_id, 'meta_key' => $key),
            array('%s'),
            array('%d', '%s')
        );
    }
    return $res !== false;
}

function get_positions($uid)
{
    global $wpdb;
    return $wpdb->get_results($wpdb->prepare("select * from keylab_position where uid = %d or uid = 0 order by uid asc", $uid));
}