-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
346 lines (294 loc) · 8.29 KB
/
User.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
class User
{
private static $ourPhoneNumbers = array(
'19183763307',
'19183763309',
'19183767984',
'19187169428',
'19185746923' );
private static $ourEmailAddresses = array(
'[email protected]' );
private $id;
private $info;
private $infoLoaded;
/**
* Constructor
*
* @param int|string $id List ID
*/
function __construct($id = -1) {
if( is_numeric( $id ) )
$this->id = $id;
else
$this->id = -1;
}
//////////////////////////////
// GETTERS
//////////////////////////////
/**
* Gets the list ID
*
* @return int ID
*/
function id() {
return $this->id;
}
/**
* Caches all of the columns from the main Users table
*
* @return bool Indicates if the information was loaded successfully
*/
function loadInfo()
{
if( $this->id() == -1 )
return false;
$info = Database::select(
'Users',
'*',
array(
'where' => array(
'id' => $this->id ),
'singleRow' => true ) );
foreach( (array)$info as $key => $item )
$this->info[ $key ] = $item;
$this->infoLoaded = true;
return true;
}
/**
* Gets a column from the main Users table
*
* @param $piece Column name
*
* @return string|null Requested info or not found
*/
function info( $piece )
{
if( $this->infoLoaded || isset( $this->info[ $piece ] ) )
return (isset( $this->info[ $piece ] )) ? $this->info[ $piece ] : null;
$value = Database::select(
'Users',
$piece,
array(
'where' => array(
'id' => $this->id ),
'single' => true ) );
$this->info[ $piece ] = $value;
return $value;
}
/**
* Gets the user name
*
* @param boolean $strip_chars true if
*
* @return string|false Name of the list or error
*/
function name( $htmlentities = true )
{
$name = stripslashes( $this->info( 'name' ) );
return ($htmlentities) ? htmlentities( $name ) : $name;
}
/**
* Gets a unique number/email to contact the user with
*
*
*/
function getUniqueFrom( $meeting, $type )
{
$valid = array();
$fromField = '';
if( $type == 'phone' )
{
// our numbers
$valid = self::$ourPhoneNumbers;
shuffle($valid); // shuffle
$fromField = 'smsFrom';
} elseif( $type == 'email' )
{
// our e-mail addresses
$valid = self::$ourEmailAddresses;
$fromField = 'emailFrom';
}
else
return false;
// check if the user already has a from setup for this meeting
if( $from = Database::select(
'Attendees',
$fromField,
array(
'where' => array(
'meeting' => $meeting->id(),
'user' => $this->id ),
'single' => true ) ) )
return $from;
// choose a valid unused candidate
$from = reset( $valid );
$found = false;
foreach( $valid as $f )
{
// check that the address is not in use by the attendee with:
// i) unsolved meeting
// ii) unexpired meeting
if( Database::select(
'Attendees AS a1',
'count(*)',
array(
'where' => array(
'a1.user' => $this->id,
"NOT EXISTS (
SELECT *
FROM Attendees AS a2 JOIN Meetings as m ON a2.meeting = m.id JOIN Time_Frame as t ON m.time_frame = t.id
WHERE ( m.status <> 0 OR t.end > '" . time() . "' ) AND ( a2.active = 1 AND a2.user = a1.user AND a1.$fromField = '$f' ) )"
),
'single' => true ) ) > 0 )
{
$from = $f;
$found = true;
break;
}
}
if( !$found )
return false;
// save for the future
Database::update(
'Attendees',
array(
'meeting' => $meeting->id(),
'user' => $this->id,
$fromField => $from ),
array( 'meeting', 'user' ) );
return $from;
}
////////////////////
// SETTERS
////////////////////
static function create( $name, $phone, $email )
{
// check if the user already exists
$uid = $uid = Database::select(
'Users',
'id',
array(
'where' => array(
'name' => $name,
'phone' => $phone,
'email' => $email ),
'single' => true ) );
if( $uid > 0 )
return $uid;
if( Database::insert(
'Users',
array(
'name' => $name,
'phone' => $phone,
'email' => $email,
'created' => time() ) ) )
return Database::lastInsertID();
return false;
}
////////////////////
// UTILITIES
////////////////////
/**
* Sends the user a message about a meeting
*
*/
function message( $messageID, $meeting, $method = 'both', $solution = null )
{
// get all of the contact methods for the user
$phone = $this->info( 'phone' );
$email = $this->info( 'email' );
$creatorName = $meeting->creator()->name();
$userName = $this->name();
$meetingName = $meeting->name();
$meetingRange = $meeting->getValidTimeFrame(true);
$meetingLength = $meeting->meetingLength( true );
$userRange = $meeting->getUserTimeFrame( $this, true );
$attendees = $meeting->attendees();
global $reply;
$attNames = array();
foreach( $attendees as $a )
$attNames[] = $a->name();
$attendeeNames = implode( ', ', $attNames );
if( $phone && in_array( $method, array( 'both', 'phone' ) ) )
{
// get a phone number to contact this user from
$from = $this->getUniqueFrom( $meeting, 'phone' );
if( !$from )
{
// send the user an error message
$messageID = 'exceeded-max-meetings';
$from = reset( self::$ourPhoneNumbers );
}
// generate the message
$message = str_replace(
array( '{CREATOR_NAME}', '{USER_NAME}', '{MEETING_NAME}', '{MEETING_RANGE}', '{MEETING_LENGTH}', '{USER_RANGE}', '{SOLUTION}', '{ATTENDEE_NAMES}' ),
array( $creatorName, $userName, $meetingName, $meetingRange, $meetingLength, $userRange, $solution, $attendeeNames ),
MeetableMessages::$smsMessages[ $messageID ] );
echo 'Sending ' . $this->name() . " a text message for $messageID from $from<br />";
// break up message if > 160 characters
$messages = array( $message );
if( strlen( $message ) > 160 )
{
// break up into messages of 150 characters + (page/total pages)
/// lets use 152 characters and keep room for message number like (1/10),
/// we can have upto 99 parts of the message (99/99)
$messagesSplit = str_split($message , 152);
$how_many = count($messagesSplit);
$messages = array();
foreach($messagesSplit as $index => $m)
$messages[] = "(".($index+1)."/".$how_many.") ".$m;
}
// instantiate a new Twilio Rest Client
require_once 'Twilio.php';
$client = new Services_Twilio( 'ACef536c27dac7efda7fe758844e6665ef', 'dd5ca9994edbb122adcd294c81c8524a' );
foreach( $messages as $message )
{
// send the message
$sms = $client->account->sms_messages->create(
$from, // from phone number (ours)
$phone, // the number we are sending to (theirs)
$message // the sms body
);
}
}
if( $email && in_array( $method, array( 'both', 'email' ) ) )
{
// get a phone number to contact this user from
$from = $this->getUniqueFrom( $meeting, 'email' );
if( !$from )
{
// send the user an error message
$messageID = 'exceeded-max-meetings';
$from = reset( self::$ourEmailAddresses );
}
// generate the message
$message = str_replace(
array( '{CREATOR_NAME}', '{USER_NAME}', '{MEETING_NAME}', '{MEETING_RANGE}', '{MEETING_LENGTH}', '{USER_RANGE}', '{SOLUTION}', '{ATTENDEE_NAMES}', '{REPLY}' ),
array( $creatorName, $userName, $meetingName, $meetingRange, $meetingLength, $userRange, $solution, $attendeeNames, $reply ),
MeetableMessages::$emailMessages[ $messageID ] );
echo 'Sending ' . $this->name() . ' an e-mail for ' . $messageID . '<br />';
// instantiate PHPMailer
$mail = new Mail;
// basic e-mail info
$mail->From = $from;
$mail->FromName = $meeting->creator()->name();
$mail->Subject = str_replace(
array( '{CREATOR_NAME}', '{USER_NAME}', '{MEETING_NAME}', '{MEETING_RANGE}', '{MEETING_LENGTH}', '{USER_RANGE}', '{SOLUTION}', '{ATTENDEE_NAMES}', '{REPLY}' ),
array( $creatorName, $userName, $meetingName, $meetingRange, $meetingLength, $userRange, $solution, $attendeeNames, $reply ),
MeetableMessages::$emailSubjects[ $messageID ] );
// text body
$mail->AltBody = $message;
// html body
$mail->MsgHTML( nl2br($message) );
// send it to the user
$mail->AddAddress( $email );
// send the e-mail
return $mail->Send();
}
}
}