-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
259 lines (230 loc) · 6.58 KB
/
index.php
File metadata and controls
259 lines (230 loc) · 6.58 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
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
<?php
// Command Line Calls only
if( php_sapi_name() == 'cli' )
chdir(__DIR__);
// Configuration
DEFINE( 'HOST_NAME', 'meetable.io' );
DEFINE( 'SITE_TITLE', 'Meetable' );
DEFINE( 'SMTP_FROM_ADDRESS', 'no-reply@meetable.io' );
DEFINE( 'SMTP_USERNAME', 'meetable' );
DEFINE( 'SMTP_PASSWORD', '7nHHCkavhtv' );
DEFINE( 'SMTP_PORT', 587 );
DEFINE( 'SMTP_HOST', 'smtp.sendgrid.net' );
ini_set('default_charset', 'utf-8');
include_once('functions.php');
// autoloader
spl_autoload_register(function($classname) {
$filename = $classname.'.php';
if( file_exists( $filename ) )
include $filename;
});
$url = val( $_SERVER, 'REQUEST_URI' );
$strippedURL = current(explode('?', $url));
$urlParams = explode('/', $strippedURL);
if( isset( $urlParams[0] ) && $urlParams[0] == '' )
unset($urlParams[0]);
Database::initialize();
// process message queues
if( ( php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR']) ) || urlParam( 1 ) == 'cron' )
{
include 'cron.php';
exit;
}
$success = false;
$error = false;
switch( urlParam( 1 ) )
{
// api call
case 'api':
$version = urlParam( 2 );
switch( urlParam( 3 ) )
{
case 'meeting':
switch( urlParam( 4 ) )
{
case 'new':
$decode = json_decode(val($_POST,'data'),true);
// convert objects to array
$decode['attendeeNames'] = array();
$decode['attendeeEmails'] = array();
$decode['attendeePhones'] = array();
foreach( $decode['attendees'] as $a )
{
$decode['attendeeNames'][] = $a[ 'name' ];
$decode['attendeeEmails'][] = $a[ 'email' ];
$decode['attendeePhones'][] = $a[ 'phone' ];
}
$decode['creatorName'] = $decode['creator']['name'];
$decode['creatorEmail'] = $decode['creator']['email'];
$decode['creatorPhone'] = $decode['creator']['phone'];
// create the meeting
if( Meeting::create( $decode ) )
{
echo 'success';
}
else
{
echo $error;
}
break;
}
break;
}
break;
// sms response
case 'sms':
// sanitize phone numbers
$from = val( $_REQUEST, 'From' );
if( !Validate::phone( $from ) )
exit;
$to = val( $_REQUEST, 'To' );
if( !Validate::phone( $to ) )
exit;
// figure out if the message is legitimate and what meeting it belongs to
if( $info = Database::select(
'Attendees JOIN Users ON id = user',
'meeting,user',
array(
'where' => array(
'user = id',
'smsFrom' => $to,
'phone' => $from,
'active' => 1 ),
'singleRow' => true ) ) )
{
$body = val( $_REQUEST, 'Body' );
/* add the response to the message queue */
$ironmq = new IronMQ();
// put the message on the queue
$ironmq->postMessage("responses", array(
'body' => json_encode( array(
'meeting' => $info['meeting'],
'user' => $info['user'],
'response' => $body,
'method' => 'phone' ) ) ) );
}
// return no message
header("content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?><Response></Response>';
break;
case 'testDates':
$meeting = new Meeting( 33 );
$testCases = array(
1 => array( // NOTE: this test does not work after 11:30 PM
'start' => strtotime( '+30 minutes' ),
'end' => strtotime( '+45 minutes' ) ),
2 => array(
'start' => strtotime( '+1 day' ),
'end' => strtotime( '+1 day, 30 minutes' ) ),
3 => array( // NOTE: this test does not work after 11:30 PM
'start' => strtotime( '+2 days' ),
'end' => strtotime( '+2 days, 30 minutes' ) ),
4 => array(
'start' => strtotime( '+2 days' ),
'end' => strtotime( '+3 days, 30 minutes' ) ),
5 => array( // NOTE: this test does not work after 11:30 PM
'start' => strtotime( '+8 days' ),
'end' => strtotime( '+8 days, 30 minutes' ) ),
6 => array( // NOTE: does not work when < 10 days left in month
'start' => strtotime( '+8 day' ),
'end' => strtotime( '+10 days' ) ),
7 => array( // NOTE: this test does not work after 11:30 PM
'start' => strtotime( '+32 days' ),
'end' => strtotime( '+32 days, 30 minutes' ) ),
8 => array( // NOTE: this test does not work when there are < 34 days left in year
'start' => strtotime( '+32 days' ),
'end' => strtotime( '+34 days' ) ),
9 => array(
'start' => strtotime( '+2 years' ),
'end' => strtotime( '+2 years, 30 minutes' ) ),
10 => array(
'start' => strtotime( '+2 years' ),
'end' => strtotime( '+3 years' ) ) );
foreach( $testCases as $case => $range )
{
echo "Testing case $case<br />";
echo "Result: " . $meeting->humanReadableRange( $range ) . '<br />';
}
$testCases = array(
'4',
'3:30',
'5:26',
'10:30 am',
'4pm',
'4pm pst',
'friday',
'friday 4pm',
'friday 4pm pst',
'november 9',
'nov 9 4pm',
'november 9 5pm',
'november 9 5pm pst',
'november 9, 2014',
'november 9, 2014 5pm',
'november 9, 2014 6pm pst',
'4pm to 5pm',
'blah',
'is this a time?'
);
foreach( $testCases as $time )
{
echo "Testing case $time " . (($meeting->isValidTime( $time )) ? '<strong>succeeds</strong>' : '<em>fails</em>') . '<br />';
echo "Resulting timestamp is " . $meeting->humanReadableRange( $meeting->generateTimeRange( $time ) ) . '<br /><br />';
}
break;
// e-mail response
case 'email':
break;
// new meeting
case 'new':
$data = array(
'name' => val( $_POST, 'name' ),
'length' => val( $_POST, 'length' ),
'start' => strtotime( val( $_POST, 'start-date' ) . ' ' . val( $_POST, 'start-time' ) ),
'end' => strtotime( val( $_POST, 'end-date' ) . ' ' . val( $_POST, 'end-time' ) ),
'timeRange' => array(
'start-date' => val( $_POST, 'start-date' ),
'start-time' => val( $_POST, 'start-time' ),
'end-date' => val( $_POST, 'end-date' ),
'end-time' => val( $_POST, 'end-time' ) ),
'attendeeNames' => val( $_POST, 'attendeeNames' ),
'attendeeEmails' => val( $_POST, 'attendeeEmails' ),
'attendeePhones' => val( $_POST, 'attendeePhones' ),
'creatorName' => val( $_POST, 'creatorName' ),
'creatorEmail' => val( $_POST, 'creatorEmail' ),
'creatorPhone' => val( $_POST, 'creatorPhone' ),
'narrowToOne' => val( $_POST, 'narrowToOne' )
);
// create the meeting
if( Meeting::create( $data ) )
{
// success
$success = true;
}
else
{
// error
}
include 'home.php';
break;
// display the home page
default:
$data = array(
'name' => '',
'length' => '',
'timeRange' => array(
'start-date' => date( 'm/d/Y', strtotime( '+30 minutes' ) ) ,
'start-time' => date( 'h:i a', strtotime( '+30 minutes' ) ),
'end-date' => date( 'm/d/Y', strtotime( '+1 hour' ) ),
'end-time' => date( 'h:i a', strtotime( '+ 1 hour' ) ) ),
'attendeeNames' => array( '' ),
'attendeeEmails' => array( '' ),
'attendeePhones' => array( '' ),
'creatorName' => '',
'creatorEmail' => '',
'creatorPhone' => '',
'narrowToOne' => 0
);
include 'home.php';
break;
}