chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
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
<?php
 
class notifymodel {
 
    var $base;
    var $db;
    var $operations;
 
    function __construct(&$base) {
        $this->notifymodel($base);
    }
 
    function notifymodel(&$base) {
        $this->base =& $base;
        $this->db =& $base->db;
        $this->operations = array(
            'updatesyncredit' => array('Cache', 'updatesyncredit'),
            'syncredit' => array('Credit', 'syncredit'),
            'altername' => array('User', 'alterName'),
            'deluser' => array('User', 'deluser')
        );
    }
 
    function add($type, $data, $exceptself = false, $exceptuc = false) {
        $myApp = $this->base->load('app');
        $applist = $myApp->applist();
    
        $pwSQL = array(
            'action' => $type,
            'param' => is_array($data) ? serialize($data) : '',
            'timestamp' => $this->base->time
        );
        $notify = array();
        foreach ($applist as $key => $app) {
            if (($exceptself && $key == $this->base->appid) || ($app['uc'] && $exceptuc)) {
                $pwSQL['app'.$key] = 1;
            } else {
                $notify[] = array('uc_notifyexists' . $key, 1);
            }
        }
        $this->db->update("INSERT INTO pw_ucnotify SET " . UC::sqlSingle($pwSQL));
        $nid = $this->db->insert_id();
        if ($notify) {
            $this->db->update("REPLACE INTO pw_config (db_name,db_value) VALUES " . UC::sqlMulti($notify));
        }
        return $nid;
    }
 
    function send($nid = null, $appid = null) {
        !$appid && $appid = $this->base->appid;
        register_shutdown_function(array($this, '_send'), $nid, $appid);
    }
 
    function _send($nid, $appid) {
        $myApp = $this->base->load('app');
        $applist = $myApp->applist();
        if (!$app = $applist[$appid]) {
            return false;
        }
        if ($nid) {
            $data = $this->get_by_id($nid);
        } else {
            $data = $this->get_by_one($appid);
            if (!$data) {
                $this->db->query("UPDATE pw_config SET db_value='0' WHERE db_name=" . UC::escape('uc_notifyexists'.$appid));
            }
            $nid = $data['nid'];
        }
        if (!$data) {
            return false;
        }
        $field = 'app' . $appid;
 
        if ($appid == $this->base->appid && file_exists(R_P . 'api/class_base.php')) {
            include_once(R_P . 'api/class_base.php');
            $api  = new api_client();
            $resp = $api->dataFormat($api->callback($this->operations[$data['action']][0], $this->operations[$data['action']][1], $data['param'] ? unserialize($data['param']) : array()));
            $success = isset($resp['result']);
        } else {
            $resp = $myApp->ucfopen($app['siteurl'], $app['interface'], $app['secretkey'], $this->operations[$data['action']][0], $this->operations[$data['action']][1], $data['param'] ? unserialize($data['param']) : array());
            $success = isset($resp['result']);
        }
        if ($success) {
            $data[$field] = 1;
            $pwSQL = array(
                $field => 1,
                'complete' => $this->isComplete($data, $applist) ? 1 : 0
            );
            $this->db->update("UPDATE pw_ucnotify SET " . UC::sqlSingle($pwSQL) . ' WHERE nid=' . UC::escape($nid));
        }
    }
 
    function send_by_id($nid) {
        $data = $this->get_by_id($nid);
        if ($data && !$data['complete']) {
            $pwSQL = array();
            $myApp = $this->base->load('app');
            $applist = $myApp->applist();
            foreach ($applist as $key => $app) {
                if ($data['app'.$key] < 1) {
                    $resp = $myApp->ucfopen($app['siteurl'], $app['interface'], $app['secretkey'], $this->operations[$data['action']][0], $this->operations[$data['action']][1], $data['param'] ? unserialize($data['param']) : array());
                    if (isset($resp['result'])) {
                        $data['app'.$key] = 1;
                        $pwSQL['app'.$key] = 1;
                    }
                }
            }
            $pwSQL['complete'] = $this->isComplete($data, $applist) ? 1 : 0;
            $this->db->update("UPDATE pw_ucnotify SET " . UC::sqlSingle($pwSQL) . ' WHERE nid=' . UC::escape($nid));
        }
    }
 
    function isComplete($notify, $apps) {
        foreach ($apps as $key => $app) {
            if ($notify['app'.$key] < 1) {
                return false;                
            }
        }
        return true;
    }
 
    function get_by_one($appid) {
        $field = 'app' . $appid;
        $data = $this->db->get_one("SELECT * FROM pw_ucnotify WHERE $field<'1' LIMIT 1");
        return $data;
    }
 
    function get_by_id($nid) {
        $data = $this->db->get_one("SELECT * FROM pw_ucnotify WHERE nid=" . UC::escape($nid));
        return $data;
    }
}
?>