KPushNotify.php
5 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php namespace App\Jobs;
use App\Models\User;
use App\Models\Sipid;
use App\Models\Kqueue;
use Mail;
use Config;
class KPushNotify
{
public function __construct()
{
}
public function send($usrarray,$type,$dispname,$msg,$usrid,$params=array())
{
$ret=array();$sipmap=array();$mailnotify=array();$gcmnotify=array();
$sipserver=array();
$tstamp=time();
if(!empty($usrarray))
{
$nusers=User::whereIn('id',$usrarray)->get();
$sipids=Sipid::whereIn("user",$usrarray)->where("status","=","1")->get();
foreach($sipids as $tsip)
{
if(!isset($sipmap[$tsip->user]))$sipmap[$tsip->user]=array();
$sipmap[$tsip->user][]=$tsip;
}
foreach($nusers as $tuser)
{
if($tuser->presence>0)
{
if(is_array($sipmap[$tuser->id]))
{
$ret[$tuser->id]=1;
foreach($sipmap[$tuser->id] as $tsip)
{
$newqueue=new Kqueue();
$newqueue->sipNotify($tsip,$type,$dispname,$msg,$usrid);
$sipserver[$tsip->server]=1;
}
}
else $ret[$tuser->id]=0;
}
else $ret[$tuser->id]=-1;
if($type=="notify"||$type=="wallreload")//also add to userdata for persistance and mail if offline
{
$dataarr=$tuser->meta();
if(!isset($dataarr['notifyemailsettings']))$dataarr['notifyemailsettings']=array();
if(!isset($dataarr['notifyemailsettings'][$type]))$dataarr['notifyemailsettings'][$type]=1;
if(!isset($dataarr['notifications']))$dataarr['notifications']=array();
if(!isset($dataarr['ncnt']))$dataarr['ncnt']=0;
array_unshift($dataarr['notifications'], array($dispname,$msg,$usrid,$tstamp));
if(sizeof($dataarr['notifications'])>20)array_pop($dataarr['notifications']);
$dataarr['ncnt']++;
$tuser->meta=json_encode($dataarr);
$tuser->save();
if($tuser->presence<=0)
{
//check if user has mail settings enabled
if($dataarr['notifyemailsettings'][$type]==1)$mailnotify[]=$tuser->email;
}
//mobile
if(isset($dataarr['mobilegcm'])&&$dataarr['mobilegcm']!="")$gcmnotify[]=$dataarr['mobilegcm'];
}
}
//mail any notifications where user is offline (type=notify)
if(!empty($mailnotify))
{
try{
if(isset($params['custommail']))
{
$helo=$params['custommail']['helo'];
$line1=$params['custommail']['line1'];
$line2=$params['custommail']['line2'];
$line3=$params['custommail']['line3'];
$subject=$params['custommail']['subject'];
//TODO change to individual mails.. this will fail when more then 500 recipients in a single mail use array_chunk($input_array, 2)
Mail::send('emails.notification', array('heloname'=>$helo,'line1'=>$line1,'line2'=>$line2,'line3'=>$line3,'notifytype'=>$type), function($message) use ($mailnotify,$subject)
{
foreach($mailnotify as $temail)if(!empty(trim($temail)))$message->bcc($temail, $temail);
$message->subject(Config::get("app.name")." : $subject");
});
}
else
{
//TODO change to individual mails.. this will fail when more then 500 recipients in a single mail use array_chunk($input_array, 2)
Mail::send('emails.notification', array('heloname'=>'','line1'=>"<b>$dispname</b> $msg",'line2'=>'Please visit <a href="'.Config::get("app.protocol").Config::get("app.domain").'/"><b>'.Config::get("app.name").'</b></a> to view details.','line3'=>'','notifytype'=>$type), function($message) use ($mailnotify)
{
foreach($mailnotify as $temail)if(!empty(trim($temail)))$message->bcc($temail, $temail);
$message->subject(Config::get("app.name")." Alerts");
});
}
}catch(Exception $e){}
}
if(!empty($gcmnotify))$this->gcmnotify($gcmnotify,$dispname." ".$msg, false);
}
return $ret;
}
function gcmnotify($devices,$message, $data = false)
{
$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = Config::get("app.gcm_apikey");
if(!is_array($devices) || count($devices) == 0){
return;
}
if(strlen($serverApiKey) < 8){
return;
}
$fields = array(
'registration_ids' => $devices,
'data' => array( "message" => strip_tags($message), "title" => Config::get("app.name") ),
);
if(is_array($data)){
foreach ($data as $key => $value) {
$fields['data'][$key] = $value;
}
}
$headers = array(
'Authorization: key=' . $serverApiKey,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
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, json_encode( $fields ) );
// Avoids problem with https certificate
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
}