[ Index ]

PHP Cross Reference of MyBB 1.8.30

title

Body

[close]

/inc/datahandlers/ -> event.php (source)

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


2005 - 2021 © MyBB.de | Alle Rechte vorbehalten! | Sponsor: netcup Cross-referenced by PHPXref