-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_output_niazpardaz.php
192 lines (162 loc) · 5.9 KB
/
message_output_niazpardaz.php
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* niazpardaz message processor to send messages by niazpardaz
*
* @package message_niazpardaz
* @copyright 2008 Luis Rodrigues
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
require_once($CFG->dirroot . '/message/output/lib.php');
//require_once($CFG->dirroot . '/message/output/lib/nusoap.php');
/**
* The niazpardaz message processor
*
* @package message_niazpardaz
* @copyright 2008 Luis Rodrigues
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class message_output_niazpardaz extends message_output {
/**
* Processes the message and sends a notification via niazpardaz
*
* @param stdClass $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
* @return true if ok, false if error
*/
function send_message($eventdata) {
global $CFG;
// Skip any messaging of suspended and deleted users.
if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
return true;
}
if (!empty($CFG->nosmsever)) {
// hidden setting for development sites, set in config.php if needed
debugging('$CFG->nosmsever is active, no niazpardaz message sent.', DEBUG_MINIMAL);
return true;
}
if (PHPUNIT_TEST) {
// No connection to external servers allowed in phpunit tests.
return true;
}
//hold onto niazpardaz id preference because /admin/cron.php sends a lot of messages at once
static $numbers = array();
if (!array_key_exists($eventdata->userto->id, $numbers)) {
$phone2 = $eventdata->userto->phone2;
// validate $phone2
$phone2 = $this->mobileValidation($phone2);
$numbers[$eventdata->userto->id] = $phone2;
}
$number = $numbers[$eventdata->userto->id];
//calling s() on smallmessage causes niazpardaz to display things like < niazpardaz != a browser
$message = !empty($eventdata->smallmessage) ? $eventdata->smallmessage : $eventdata->fullmessage;
$message = strip_tags($message);
try {
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient('http://5.9.76.186/SendService.svc?wsdl', array('encoding' => 'UTF-8'));
$parameters['userName'] = $CFG->niazpardazusername;
$parameters['password'] = $CFG->niazpardazpassword;
$parameters['fromNumber'] = $CFG->niazpardaznumber;
$parameters['toNumbers'] = array($number);
$parameters['messageContent'] = $message;
$parameters['isFlash'] = false;
$recId = array();
$status = array();
$parameters['recId'] = &$recId;
$parameters['status'] = &$status;
$client->SendSMS($parameters)->SendSMSResult;
} catch (SoapFault $e) {
debugging($e->getMessage());
return false;
}
return true;
}
//define "98" to first of the numbet
function mobileValidation($number) {
$number = (int) $number;
if (strpos($number, "98") === 0) {
$number = substr($number, 2);
}
$final = "0" . $number;
return $final;
}
/**
* Creates necessary fields in the messaging config form.
*
* @param array $preferences An array of user preferences
*/
function config_form($preferences) {
global $CFG, $USER;
if (!$this->is_system_configured()) {
return get_string('notconfigured', 'message_niazpardaz');
} else {
return get_string('niazpardazmobilenumber', 'message_niazpardaz') . ': ' . $USER->phone2;
}
}
/**
* Parses the submitted form data and saves it into preferences array.
*
* @param stdClass $form preferences form class
* @param array $preferences preferences array
*/
function process_form($form, &$preferences) {
}
/**
* Loads the config data from database to put on the form during initial form display
*
* @param array $preferences preferences array
* @param int $userid the user id
*/
function load_data(&$preferences, $userid) {
}
/**
* Tests whether the niazpardaz settings have been configured
* @return boolean true if niazpardaz is configured
*/
function is_system_configured() {
global $CFG;
return (!empty($CFG->niazpardaznumber) && !empty($CFG->niazpardazusername) && !empty($CFG->niazpardazpassword));
}
/**
* Tests whether the niazpardaz settings have been configured on user level
* @param object $user the user object, defaults to $USER.
* @return bool has the user made all the necessary settings
* in their profile to allow this plugin to be used.
*/
function is_user_configured($user = null) {
global $USER;
if (is_null($user)) {
$user = $USER;
}
return (bool) $user->phone2;
}
}
/*
*
* $f = fopen('/tmp/event_niazpardazx', 'a+');
fwrite($f, date('l dS \of F Y h:i:s A')."\n");
fwrite($f, "from: $message->userfromid\n");
fwrite($f, "userto: $message->usertoid\n");
fwrite($f, "subject: $message->subject\n");
fclose($f);
$savemessage = new stdClass();
$savemessage->useridfrom = 3;
$savemessage->useridto = 2;
$savemessage->subject = 'IM';
$savemessage->fullmessage = 'full';
$savemessage->timecreated = time();
$a = new message_output_niazpardaz();
$a->send_message($savemessage);
* */