You are here

function timezone_detect_set_timezone in Timezone Detect 6

Same name and namespace in other branches
  1. 7 timezone_detect.module \timezone_detect_set_timezone()

Ajax callback function to set current user's timezone.

1 string reference to 'timezone_detect_set_timezone'
timezone_detect_menu in ./timezone_detect.module
Implements hook_menu().

File

./timezone_detect.module, line 89
Module provides automatic timezone detection via javascript.

Code

function timezone_detect_set_timezone() {
  global $user;

  // If they are logged in, set some data.
  if (!user_is_anonymous()) {

    // Check for $_POST data.
    // Timezone should be an IANA/Olson timezone id provided via $_POST.
    if (!isset($_POST['timezone'])) {
      watchdog('timezone_detect', 'Attempting to set timezone for user @uid, but no timezone found in $_POST data; aborting.', array(
        '@uid' => $user->uid,
      ), WATCHDOG_ERROR);
      return '';
    }
    $timezone = check_plain($_POST['timezone']);

    // Keep track of the last submitted timezone in case it's not valid so that
    // we don't keep POSTing it on every request.
    $_SESSION['timezone_detect']['current_timezone'] = $timezone;

    // Check valid timezone id.
    $zone_list = timezone_identifiers_list();
    if (!in_array($timezone, $zone_list)) {
      watchdog('timezone_detect', 'Attempting to set timezone for user @uid to @timezone, but that does not appear to be a valid timezone id; aborting.', array(
        '@uid' => $user->uid,
        '@timezone' => $timezone,
      ), WATCHDOG_ERROR);
      return '';
    }

    // Save timezone to account.
    $account = user_load($user->uid);
    $edit['timezone'] = $timezone;
    user_save($account, $edit);
    if (variable_get('timezone_detect_success_watchdog', TRUE)) {
      watchdog('timezone_detect', 'Set timezone for user @uid to @timezone.', array(
        '@uid' => $user->uid,
        '@timezone' => $timezone,
      ));
    }
  }

  // Unset session flag regarldess of whether they are logged in or not to avoid
  // repeated attempts at this process that are likely to fail.
  unset($_SESSION['timezone_detect']['update_timezone']);
}