[ Index ] |
PHP Cross Reference of MyBB |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.6 4 * Copyright 2010 MyBB Group, All Rights Reserved 5 * 6 * Website: http://mybb.com 7 * License: http://mybb.com/about/license 8 * 9 * $Id$ 10 */ 11 12 // Disallow direct access to this file for security reasons 13 if(!defined("IN_MYBB")) 14 { 15 die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined."); 16 } 17 18 /** 19 * Event handling class, provides common structure to handle event data. 20 * 21 */ 22 class EventDataHandler extends DataHandler 23 { 24 /** 25 * The language file used in the data handler. 26 * 27 * @var string 28 */ 29 public $language_file = 'datahandler_event'; 30 31 /** 32 * The prefix for the language variables used in the data handler. 33 * 34 * @var string 35 */ 36 public $language_prefix = 'eventdata'; 37 38 /** 39 * Array of data inserted in to an event. 40 * 41 * @var array 42 */ 43 public $event_insert_data = array(); 44 45 /** 46 * Array of data used to update an event. 47 * 48 * @var array 49 */ 50 public $event_update_data = array(); 51 52 /** 53 * Event ID currently being manipulated by the datahandlers. 54 */ 55 public $eid = 0; 56 57 /** 58 * Verifies if an event name is valid or not and attempts to fix it 59 * 60 * @return boolean True if valid, false if invalid. 61 */ 62 function verify_name() 63 { 64 $name = &$this->data['name']; 65 $name = utf8_handle_4byte_string($name); 66 $name = trim($name); 67 if(!$name) 68 { 69 $this->set_error("missing_name"); 70 return false; 71 } 72 return true; 73 } 74 75 /** 76 * Verifies if an event description is valid or not and attempts to fix it 77 * 78 * @return boolean True if valid, false if invalid. 79 */ 80 function verify_description() 81 { 82 $description = &$this->data['description']; 83 $description = utf8_handle_4byte_string($description); 84 $description = trim($description); 85 if(!$description) 86 { 87 $this->set_error("missing_description"); 88 return false; 89 } 90 return true; 91 } 92 93 /** 94 * Verifies if an event date is valid or not and attempts to fix it 95 * 96 * @return boolean True if valid, false if invalid. 97 */ 98 function verify_date() 99 { 100 $event = &$this->data; 101 102 // All types of events require a start date 103 if(!$event['start_date']['day'] || !$event['start_date']['month'] || !$event['start_date']['year']) 104 { 105 $this->set_error("invalid_start_date"); 106 return false; 107 } 108 109 $event['start_date']['day'] = intval($event['start_date']['day']); 110 $event['start_date']['month'] = intval($event['start_date']['month']); 111 $event['start_date']['year'] = intval($event['start_date']['year']); 112 113 if($event['start_date']['day'] > date("t", mktime(0, 0, 0, $event['start_date']['month'], 1, $event['start_date']['year']))) 114 { 115 $this->set_error("invalid_start_date"); 116 return false; 117 } 118 119 // Calendar events can only be within the next 5 years 120 if($event['start_date']['year'] > date("Y") + 5) 121 { 122 $this->set_error("invalid_start_year"); 123 return false; 124 } 125 126 //Check to see if the month is within 1 and 12 127 if($event['start_date']['month'] > 12 || $event['start_date']['month'] < 1) 128 { 129 $this->set_error("invalid_start_month"); 130 return false; 131 } 132 133 // For ranged events, we check the end date & times too 134 if($event['type'] == "ranged") 135 { 136 if(!$event['end_date']['day'] || !$event['end_date']['month'] || !$event['end_date']['year']) 137 { 138 $this->set_error("invalid_end_date"); 139 return false; 140 } 141 142 $event['end_date']['day'] = intval($event['end_date']['day']); 143 $event['end_date']['month'] = intval($event['end_date']['month']); 144 $event['end_date']['year'] = intval($event['end_date']['year']); 145 146 if($event['end_date']['day'] > date("t", mktime(0, 0, 0, $event['end_date']['month'], 1, $event['end_date']['year']))) 147 { 148 $this->set_error("invalid_end_date"); 149 return false; 150 } 151 152 // Calendar events can only be within the next 5 years 153 if($event['end_date']['year'] > date("Y") + 5) 154 { 155 $this->set_error("invalid_end_year"); 156 return false; 157 } 158 159 //Check to see if the month is within 1 and 12 160 if($event['end_date']['month'] > 12 || $event['end_date']['month'] < 1) 161 { 162 $this->set_error("invalid_end_month"); 163 return false; 164 } 165 166 // Validate time input 167 if($event['start_date']['time'] || $event['end_date']['time']) 168 { 169 if(($event['start_date']['time'] && !$event['end_date']['time']) || ($event['end_date']['time'] && !$event['start_date']['time'])) 170 { 171 $this->set_error("cant_specify_one_time"); 172 return false; 173 } 174 175 // Begin start time validation 176 $start_time = $this->verify_time($event['start_date']['time']); 177 if(!is_array($start_time)) 178 { 179 $this->set_error("start_time_invalid"); 180 return false; 181 } 182 183 // End time validation 184 $end_time = $this->verify_time($event['end_date']['time']); 185 if(!is_array($end_time)) 186 { 187 $this->set_error("end_time_invalid"); 188 return false; 189 } 190 $event['usingtime'] = 1; 191 } 192 else 193 { 194 $start_time = array("hour" => 0, "min" => 0); 195 $end_time = array("hour" => 23, "min" => 59); 196 $event['usingtime'] = 0; 197 } 198 } 199 200 if(array_key_exists('timezone', $event)) 201 { 202 $event['timezone'] = floatval($event['timezone']); 203 if($event['timezone'] > 12 || $event['timezone'] < -12) 204 { 205 $this->set_error("invalid_timezone"); 206 return false; 207 } 208 $start_time['hour'] -= $event['timezone']; 209 $end_time['hour'] -= $event['timezone']; 210 } 211 212 $start_timestamp = gmmktime($start_time['hour'], $start_time['min'], 0, $event['start_date']['month'], $event['start_date']['day'], $event['start_date']['year']); 213 214 if($event['type'] == "ranged") 215 { 216 $end_timestamp = gmmktime($end_time['hour'], $end_time['min'], 0, $event['end_date']['month'], $event['end_date']['day'], $event['end_date']['year']); 217 218 if($end_timestamp <= $start_timestamp) 219 { 220 $this->set_error("end_in_past"); 221 return false; 222 } 223 } 224 225 // Save our time stamps for saving 226 $event['starttime'] = $start_timestamp; 227 $event['endtime'] = $end_timestamp; 228 229 return true; 230 } 231 232 function verify_time($time) 233 { 234 preg_match('#^(0?[1-9]|1[012])\s?([:\.]?)\s?([0-5][0-9])?(\s?[ap]m)|([01][0-9]|2[0-3])\s?([:\.])\s?([0-5][0-9])$#i', $time, $matches); 235 if(count($matches) == 0) 236 { 237 return false; 238 } 239 240 // 24h time 241 if(count($matches) == 8) 242 { 243 $hour = $matches[5]; 244 $min = $matches[7]; 245 } 246 // 12 hour time 247 else 248 { 249 $hour = $matches[1]; 250 $min = intval($matches[3]); 251 $matches[4] = trim($matches[4]); 252 if(my_strtolower($matches[4]) == "pm" && $hour != 12) 253 { 254 $hour += 12; 255 } 256 else if(my_strtolower($matches[4]) == "am" && $hour == 12) 257 { 258 $hour = 0; 259 } 260 } 261 return array("hour" => $hour, "min" => $min); 262 } 263 264 function verify_repeats() 265 { 266 global $mybb; 267 $event = &$this->data; 268 269 if(!is_array($event['repeats']) || !$event['repeats']['repeats']) 270 { 271 return true; 272 } 273 274 if(!$event['endtime']) 275 { 276 $this->set_error("only_ranged_events_repeat"); 277 return false; 278 } 279 280 switch($event['repeats']['repeats']) 281 { 282 case 1: 283 $event['repeats']['days'] = intval($event['repeats']['days']); 284 if($event['repeats']['days'] <= 0) 285 { 286 $this->set_error("invalid_repeat_day_interval"); 287 return false; 288 } 289 case 2: 290 break; 291 case 3: 292 $event['repeats']['weeks'] = intval($event['repeats']['weeks']); 293 if($event['repeats']['weeks'] <= 0) 294 { 295 $this->set_error("invalid_repeat_week_interval"); 296 return false; 297 } 298 if(count($event['repeats']['days']) == 0) 299 { 300 $this->set_error("invalid_repeat_weekly_days"); 301 return false; 302 } 303 asort($event['repeats']['days']); 304 break; 305 case 4: 306 if($event['repeats']['day']) 307 { 308 $event['repeats']['day'] = intval($event['repeats']['day']); 309 if($event['repeats']['day'] <= 0 || $event['repeats']['day'] > 31) 310 { 311 $this->set_error("invalid_repeat_day_interval"); 312 return false; 313 } 314 } 315 else 316 { 317 if($event['repeats']['occurance'] != "last") 318 { 319 $event['repeats']['occurance'] = intval($event['repeats']['occurance']); 320 } 321 $event['repeats']['weekday'] = intval($event['repeats']['weekday']); 322 } 323 $event['repeats']['months'] = intval($event['repeats']['months']); 324 if($event['repeats']['months'] <= 0 || $event['repeats']['months'] > 12) 325 { 326 $this->set_error("invalid_repeat_month_interval"); 327 return false; 328 } 329 break; 330 case 5: 331 if($event['repeats']['day']) 332 { 333 $event['repeats']['day'] = intval($event['repeats']['day']); 334 if($event['repeats']['day'] <= 0 || $event['repeats']['day'] > 31) 335 { 336 $this->set_error("invalid_repeat_day_interval"); 337 return false; 338 } 339 } 340 else 341 { 342 if($event['repeats']['occurance'] != "last") 343 { 344 $event['repeats']['occurance'] = intval($event['repeats']['occurance']); 345 } 346 $event['repeats']['weekday'] = intval($event['repeats']['weekday']); 347 } 348 $event['repeats']['month'] = intval($event['repeats']['month']); 349 if($event['repeats']['month'] <= 0 || $event['repeats']['month'] > 12) 350 { 351 $this->set_error("invalid_repeat_month_interval"); 352 return false; 353 } 354 $event['repeats']['years'] = intval($event['repeats']['years']); 355 if($event['repeats']['years'] <= 0 || $event['repeats']['years'] > 4) 356 { 357 $this->set_error("invalid_repeat_year_interval"); 358 return false; 359 } 360 break; 361 default: 362 $event['repeats'] = array(); 363 } 364 require_once MYBB_ROOT."inc/functions_calendar.php"; 365 $event['starttime_user'] = $event['starttime']; 366 $event['endtime_user'] = $event['endtime']; 367 $next_occurance = fetch_next_occurance($event, array('start' => $event['starttime'], 'end' => $event['endtime']), $event['starttime'], true); 368 if($next_occurance > $event['endtime']) 369 { 370 $this->set_error("event_wont_occur"); 371 return false; 372 } 373 return true; 374 } 375 376 /** 377 * Validate an event. 378 * 379 * @param array The event data array. 380 */ 381 function validate_event() 382 { 383 global $plugins; 384 385 $event = &$this->data; 386 387 if($this->method == "insert" || array_key_exists('name', $event)) 388 { 389 $this->verify_name(); 390 } 391 392 if($this->method == "insert" || array_key_exists('description', $event)) 393 { 394 $this->verify_description(); 395 } 396 397 if($this->method == "insert" || array_key_exists('start_date', $event) || array_key_exists('end_date', $event)) 398 { 399 $this->verify_date(); 400 } 401 402 if(($this->method == "insert" && $event['endtime']) || array_key_exists('repeats', $event)) 403 { 404 $this->verify_repeats(); 405 } 406 407 $plugins->run_hooks("datahandler_event_validate", $this); 408 409 // We are done validating, return. 410 $this->set_validated(true); 411 if(count($this->get_errors()) > 0) 412 { 413 return false; 414 } 415 else 416 { 417 return true; 418 } 419 } 420 421 /** 422 * Insert an event into the database. 423 * 424 * @param array The array of event data. 425 * @return array Array of new event details, eid and private. 426 */ 427 function insert_event() 428 { 429 global $db, $mybb, $plugins; 430 431 // Yes, validating is required. 432 if(!$this->get_validated()) 433 { 434 die("The event needs to be validated before inserting it into the DB."); 435 } 436 437 if(count($this->get_errors()) > 0) 438 { 439 die("The event is not valid."); 440 } 441 442 $event = &$this->data; 443 444 $query = $db->simple_select("calendars", "*", "cid='".intval($event['cid'])."'"); 445 $calendar_moderation = $db->fetch_field($query, "moderation"); 446 if($calendar_moderation == 1 && intval($event['private']) != 1) 447 { 448 $visible = 0; 449 if($event['uid'] == $mybb->user['uid']) 450 { 451 $calendar_permissions = get_calendar_permissions($event['cid']); 452 if($calendar_permissions['canbypasseventmod'] == 1) 453 { 454 $visible = 1; 455 } 456 } 457 } 458 else 459 { 460 $visible = 1; 461 } 462 463 // Prepare an array for insertion into the database. 464 $this->event_insert_data = array( 465 'cid' => intval($event['cid']), 466 'uid' => intval($event['uid']), 467 'name' => $db->escape_string($event['name']), 468 'description' => $db->escape_string($event['description']), 469 'visible' => $visible, 470 'private' => intval($event['private']), 471 'dateline' => TIME_NOW, 472 'starttime' => intval($event['starttime']), 473 'endtime' => intval($event['endtime']), 474 'timezone' => $db->escape_string(floatval($event['timezone'])), 475 'ignoretimezone' => intval($event['ignoretimezone']), 476 'usingtime' => intval($event['usingtime']), 477 'repeats' => $db->escape_string(serialize($event['repeats'])) 478 ); 479 480 $plugins->run_hooks("datahandler_event_insert", $this); 481 482 $this->eid = $db->insert_query("events", $this->event_insert_data); 483 484 // Return the event's eid and whether or not it is private. 485 return array( 486 'eid' => $this->eid, 487 'private' => $event['private'], 488 'visible' => $visible 489 ); 490 } 491 492 /** 493 * Updates an event that is already in the database. 494 * 495 * @param array The event data array. 496 */ 497 function update_event() 498 { 499 global $db, $plugins; 500 501 // Yes, validating is required. 502 if(!$this->get_validated()) 503 { 504 die("The event needs to be validated before inserting it into the DB."); 505 } 506 507 if(count($this->get_errors()) > 0) 508 { 509 die("The event is not valid."); 510 } 511 512 $event = &$this->data; 513 514 $this->eid = $event['eid']; 515 516 if(isset($event['cid'])) 517 { 518 $this->event_update_data['cid'] = $db->escape_string($event['cid']); 519 } 520 521 if(isset($event['name'])) 522 { 523 $this->event_update_data['name'] = $db->escape_string($event['name']); 524 } 525 526 if(isset($event['description'])) 527 { 528 $this->event_update_data['description'] = $db->escape_string($event['description']); 529 } 530 531 if(isset($event['starttime'])) 532 { 533 $this->event_update_data['starttime'] = intval($event['starttime']); 534 $this->event_update_data['usingtime'] = intval($event['usingtime']); 535 } 536 537 if(isset($event['endtime'])) 538 { 539 $this->event_update_data['endtime'] = intval($event['endtime']); 540 $this->event_update_data['usingtime'] = intval($event['usingtime']); 541 } 542 else 543 { 544 $this->event_update_data['endtime'] = 0; 545 $this->event_update_data['usingtime'] = 0; 546 } 547 548 if(isset($event['repeats'])) 549 { 550 if(!empty($event['repeats'])) 551 { 552 $event['repeats'] = serialize($event['repeats']); 553 } 554 $this->event_update_data['repeats'] = $db->escape_string($event['repeats']); 555 } 556 557 if(isset($event['timezone'])) 558 { 559 $this->event_update_data['timezone'] = $db->escape_string(floatval($event['timezone'])); 560 } 561 562 if(isset($event['ignoretimezone'])) 563 { 564 $this->event_update_data['ignoretimezone'] = intval($event['ignoretimezone']); 565 } 566 567 if(isset($event['private'])) 568 { 569 $this->event_update_data['private'] = intval($event['private']); 570 } 571 572 if(isset($event['visible'])) 573 { 574 $this->event_update_data['visible'] = $db->escape_string($event['visible']); 575 } 576 577 if(isset($event['uid'])) 578 { 579 $this->event_update_data['uid'] = intval($event['uid']); 580 } 581 582 $plugins->run_hooks("datahandler_event_update", $this); 583 584 $db->update_query("events", $this->event_update_data, "eid='".intval($event['eid'])."'"); 585 586 // Return the event's eid and whether or not it is private. 587 return array( 588 'eid' => $event['eid'], 589 'private' => $event['private'] 590 ); 591 } 592 } 593 594 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Oct 8 19:19:50 2013 | Cross-referenced by PHPXref 0.7.1 |