forked from oviedo/Cuidemos-el-Voto
/
alerts.php
368 lines (308 loc) · 12.5 KB
/
alerts.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php defined('SYSPATH') or die('No direct script access.');
/**
* This controller handles requests for SMS/ Email alerts
*
* PHP version 5
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi - http://source.ushahididev.com
* @module Alerts Controller
* @copyright Ushahidi - http://www.ushahidi.com
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
*/
class Alerts_Controller extends Main_Controller {
const MOBILE_ALERT = 1;
const EMAIL_ALERT = 2;
const CODE_LENGTH = 6;
private $code_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
function __construct()
{
parent::__construct();
$this->session = Session::instance();
}
public function index()
{
// Create new session
$this->session->create();
$this->template->header->this_page = 'alerts';
$this->template->content = new View('alerts');
// Display news feeds?
$this->template->content->allow_feed = Kohana::config('settings.allow_feed');
// Retrieve default country, latitude, longitude
$default_country = Kohana::config('settings.default_country');
// Retrieve Country Cities
$this->template->content->cities = $this->_get_cities($default_country);
// setup and initialize form field names
$form = array (
'alert_mobile' => '',
'alert_mobile_yes' => '',
'alert_email' => '',
'alert_email_yes' => '',
'alert_lat' => '',
'alert_lon' => ''
);
// copy the form as errors, so the errors will be stored with keys
// corresponding to the form field names
$errors = $form;
$form_error = FALSE;
$form_saved = FALSE;
// check, has the form been submitted, if so, setup validation
if ($_POST)
{
// Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
$post = new Validation($_POST);
// Add some filters
$post->pre_filter('trim', TRUE);
// Add some rules, the input field, followed by a list of checks, carried out in order
if ( !empty($_POST['alert_mobile']) || isset($_POST['alert_mobile_yes']) )
{
$post->add_rules('alert_mobile', 'required', 'numeric', 'length[6,20]');
}
if ( !empty($_POST['alert_email']) || isset($_POST['alert_email_yes']) )
{
$post->add_rules('alert_email', 'required', 'email', 'length[3,64]');
}
if ( empty($_POST['alert_email']) && empty($_POST['alert_mobile']) )
{
$post->add_error('alert_mobile','one_required');
$post->add_error('alert_email','one_required');
}
$post->add_rules('alert_lat','required','between[-90,90]'); // Validate for maximum and minimum latitude values
$post->add_rules('alert_lon','required','between[-180,180]'); // Validate for maximum and minimum longitude values
// Add a callback, to validate the mobile phone/email (See the methods below)
$post->add_callbacks('alert_mobile', array($this, 'mobile_check'));
$post->add_callbacks('alert_email', array($this, 'email_check'));
// Test to see if things passed the rule checks
if ($post->validate())
{
// Yes! everything is valid
// Save alert and send out confirmation code
$email_confirmation_saved = FALSE;
$sms_confirmation_saved = FALSE;
if (!empty($post->alert_mobile))
{
$alert_code = $this->_mk_code();
$settings = ORM::factory('settings', 1);
if ($settings->loaded == true)
{
// Get SMS Numbers
if (!empty($settings->sms_no3)) {
$sms_from = $settings->sms_no3;
}elseif (!empty($settings->sms_no2)) {
$sms_from = $settings->sms_no2;
}elseif (!empty($settings->sms_no1)) {
$sms_from = $settings->sms_no1;
}else{
$sms_from = "000";// User needs to set up an SMS number
}
$sms = new Eflyer();
$sms->user = $settings->eflyer_username;
$sms->password = $settings->eflyer_password;
$sms->use_ssl = false;
$sms->sms();
$message = "Tú código de alerta es: ".$alert_code." www.cuidemoselvoto.org";
if ($sms->send ($post->alert_mobile, $message) == "OK")
{
$alert = ORM::factory('alert');
$alert->alert_type = self::MOBILE_ALERT;
$alert->alert_recipient = $post->alert_mobile;
$alert->alert_code = $alert_code;
$alert->alert_lon = $post->alert_lon;
$alert->alert_lat = $post->alert_lat;
$alert->save();
if ($alert->saved == TRUE)
$sms_confirmation_saved = TRUE;
}
}
}
if (!empty($post->alert_email))
{
$alert_code = $this->_mk_code();
//Send verification email
$config = kohana::config('alerts');
$settings = kohana::config('settings');
$to = $post->alert_email;
$from = $config['alerts_email'];
$subject = $settings['site_name'].' alertas - verificación';
$message = "Por favor da click en http://www.cuidemoselvoto.org/alerts/verify/".$alert_code.
" para confirmar tu e-mail y recibir alertas.<br/><br/>Atte<br/>El equipo de Cuidemoselvoto.org";
if (email::send($to, $from, $subject, $message, TRUE) == 1)
{
$alert = ORM::factory('alert');
$alert->alert_type = self::EMAIL_ALERT;
$alert->alert_recipient = $post->alert_email;
$alert->alert_code = $alert_code;
$alert->alert_lon = $post->alert_lon;
$alert->alert_lat = $post->alert_lat;
$alert->save();
if ($alert->saved == TRUE)
$email_confirmation_saved = TRUE;
}
}
$this->session->set('alert_mobile', $post->alert_mobile);
$this->session->set('alert_email', $post->alert_email);
$this->session->set('sms_confirmation_saved',
$sms_confirmation_saved);
$this->session->set('email_confirmation_saved',
$email_confirmation_saved);
url::redirect('alerts/confirm');
}
// No! We have validation errors, we need to show the form again, with the errors
else
{
// repopulate the form fields
$form = arr::overwrite($form, $post->as_array());
// populate the error fields, if any
$errors = arr::overwrite($errors, $post->errors('alerts'));
$form_error = TRUE;
}
}
else
{
$form['alert_lat'] = Kohana::config('settings.default_lat');
$form['alert_lon'] = Kohana::config('settings.default_lon');
}
$this->template->content->form = $form;
$this->template->content->errors = $errors;
$this->template->content->form_error = $form_error;
$this->template->content->form_saved = $form_saved;
// Javascript Header
$this->template->header->map_enabled = TRUE;
$this->template->header->js = new View('alerts_js');
$this->template->header->js->default_map = Kohana::config('settings.default_map');
$this->template->header->js->default_zoom = Kohana::config('settings.default_zoom');
$this->template->header->js->latitude = $form['alert_lat'];
$this->template->header->js->longitude = $form['alert_lon'];
}
/**
* Alerts Confirmation Page
*/
function confirm ()
{
//$this->template->header->this_page = 'alerts';
$this->template->content = new View('alerts_confirm');
if (isset($_SESSION['alert_mobile']) && isset($_SESSION['alert_email'])) {
$this->template->content->alert_mobile = $_SESSION['alert_mobile'];
$this->template->content->alert_email = $_SESSION['alert_email'];
}
$this->template->content->email_confirmation_saved =
isset($_SESSION['email_confirmation_saved']) ? $_SESSION['email_confirmation_saved'] : FALSE;
$this->template->content->sms_confirmation_saved =
isset($_SESSION['sms_confirmation_saved']) ? $_SESSION['sms_confirmation_saved'] : FALSE;
}
/**
* Verifies a previously sent alert confirmation code
*/
function verify($code=NULL)
{
$errno = NULL;
define("ER_CODE_VERIFIED", 0);
define("ER_CODE_NOT_FOUND", 1);
define("ER_CODE_ALREADY_VERIFIED", 2);
if (isset($_POST['alert_code']))
$code = trim($_POST['alert_code']);
if ($code != NULL)
{
$code = ORM::factory('alert')
->where('alert_code', $code)->find();
if (!$code->id)
{
$errno = ER_CODE_NOT_FOUND;
}
elseif ($code->alert_confirmed == 1)
{
$errno = ER_CODE_ALREADY_VERIFIED;
}
else
{
$code->alert_confirmed = 1;
$code->save($code->id);
if ($code->saved == true)
$errno = ER_CODE_VERIFIED;
}
}
else
$errno = ER_CODE_NOT_FOUND;
$this->template->header->this_page = 'alerts';
$this->template->content = new View('alerts_verify');
$this->template->content->errno = $errno;
}
/*
* Retrieves Previously Cached Geonames Cities
*/
private function _get_cities()
{
$cities = ORM::factory('city')->orderby('city', 'asc')->find_all();
$city_select = array('' => Kohana::lang('ui_main.alerts_select_city'));
foreach ($cities as $city) {
$city_select[$city->city_lon . "," . $city->city_lat] = $city->city;
}
return $city_select;
}
/*
* Checks to see if a previous alert has been set for the mobile phone
*/
public function mobile_check(Validation $post)
{
// If add->rules validation found any errors, get me out of here!
if (array_key_exists('alert_mobile', $post->errors())
|| array_key_exists('alert_lat', $post->errors())
|| array_key_exists('alert_lon', $post->errors()))
return;
// Now check for similar alert in system
$mobile_check = ORM::factory('alert')
->where('alert_type', self::MOBILE_ALERT)
->where('alert_recipient', $post->alert_mobile)
->where('alert_lat', $post->alert_lat)
->where('alert_lon', $post->alert_lon)->find();
if ( $mobile_check->id )
{
// Add a validation error, this will cause $post->validate() to return FALSE
$post->add_error( 'alert_mobile', 'mobile_check');
}
}
/*
* Checks to see if a previous alert has been set for the email address
*/
public function email_check(Validation $post)
{
// If add->rules validation found any errors, get me out of here!
if (array_key_exists('alert_email', $post->errors())
|| array_key_exists('alert_lat', $post->errors())
|| array_key_exists('alert_lon', $post->errors()))
return;
// Now check for similar alert in system
$email_check = ORM::factory('alert')
->where('alert_type', self::EMAIL_ALERT)
->where('alert_recipient', $post->alert_email)
->where('alert_lat', $post->alert_lat)
->where('alert_lon', $post->alert_lon)->find();
if ( $email_check->id )
{
// Add a validation error, this will cause $post->validate() to return FALSE
$post->add_error( 'alert_email', 'email_check');
}
}
/**
* Creates a confirmation code for use with email or sms verification
*/
private function _mk_code()
{
$max_char_pos = strlen($this->code_chars)-1;
$code = NULL;
for($i = 0; $i < self::CODE_LENGTH; $i++)
{
$pos = mt_rand(0, $max_char_pos);
$code.=$this->code_chars[$pos];
}
// Only generate unique codes. If a code has been used before, generate
// a new one.
$code_check = ORM::factory('alert')
->where('alert_code', $code)->find();
if (!$code_check->id)
return $code;
$this->_mk_code();
}
}