HEX
Server: nginx/1.22.1
System: Linux VM-16-9-centos 3.10.0-1160.99.1.el7.x86_64 #1 SMP Wed Sep 13 14:19:20 UTC 2023 x86_64
User: www (1001)
PHP: 7.3.31
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,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_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/oa.sanjiangapp.com/lib/tpns/tpns.class.php
<?php
/**
 * Tencent Push Notification Service lib.
 *
 * @copyright Copyright 2009-2020 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
 * @author    Wenrui LI <liwenrui@cnezsoft.com>
 * @package   tpns
 * @license   ZOSL (http://zpl.pub/page/zoslv1.html)
 * @version   $Id$
 * @Link      http://xuan.im
 */
class tpns
{
    /**
     * TPNS Endpoints.
     */
    const ENDPOINT_PUSH = 'https://api.tpns.tencent.com/v3/push/app';

    /**
     * Set accessId and secretKey.
     *
     * @param string $accessId
     * @param string $secretKey
     */
    public function __construct($accessId, $secretKey)
    {
        assert(isset($accessId) && isset($secretKey));

        $this->accessId  = $accessId;
        $this->secretKey = $secretKey;
    }

    /**
     * Execute curl, POST only.
     *
     * @param  string      $url     url of API endpoint.
     * @param  array       $params  POST params.
     * @param  string      $header  auth header, or any other headers.
     * @access private
     * @return object|bool returns false if failed to curl.
     */
    private function curl($url, $params = array(), $header = '')
    {
        $params = is_array($params) ? json_encode($params) : $params;

        $curlConfig = array(
            CURLOPT_URL            => $url,
            CURLOPT_HEADER         => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 3,
            CURLOPT_POST           => true,
            CURLOPT_POSTFIELDS     => $params,
            CURLOPT_HTTPHEADER     => $header
        );

        $ch = curl_init();
        curl_setopt_array($ch, $curlConfig);
        $data = curl_exec($ch);
        curl_close($ch);

        if(!empty($data))
        {
            $data = stripslashes($data);
            $data = json_decode($data);
        }
        if(json_last_error() !== JSON_ERROR_NONE) return false;
        return $data;
    }

    /**
     * Sign and send push request to TPNS.
     *
     * @param  object      $params
     * @access private
     * @return object|bool
     */
    public function push($params)
    {
        $authHeader = array('Authorization: Basic ' . base64_encode($this->accessId . ':' . $this->secretKey));
        if(is_object($params)) $params = (array)$params;
        return $this->curl(tpns::ENDPOINT_PUSH, $params, $authHeader);
    }

    /**
     * Make a tpns push pack with message and tokens.
     *
     * @param  object $message
     * @param  array  $tokenList
     * @access public
     * @return object
     */
    public function makePushPack($message, $tokenList)
    {
        $push = new stdclass();
        $push->audience_type = 'token_list';
        $push->token_list    = $tokenList;
        $push->message       = $message;
        $push->message_type  = 'notify';

        return $push;
    }

    /**
     * Make message for a push pack.
     *
     * @param  string $title
     * @param  string $content
     * @param  string $intent
     * @access public
     * @return object
     */
    public function makeMessagePack($title, $content = ' ', $intent = '')
    {
        $message = new stdclass();
        $message->title   = $title;
        $message->content = $content;
        /* Set android pack to message if intent presents. */
        if(!empty($intent)) $message->android = $this->makeAndroidPack($intent);

        return $message;
    }

    /**
     * Make the android part of a message pack.
     *
     * @param  string  $intent
     * @access private
     * @return object
     */
    private function makeAndroidPack($intent)
    {
        $android = new stdclass();
        $android->action = new stdclass();
        $android->action->action_type = 3; // 1 = open app, 2 = open browser, 3 = open intent.
        $android->action->intent = $intent;

        return $android;
    }
}