function CalendarMain() { global $txt, $context, $modSettings, $scripturl, $options; // If we are posting a new event defect to the posting function. if (isset($_GET['sa']) && $_GET['sa'] == 'post') { return CalendarPost(); } // This is gonna be needed... loadTemplate('Calendar'); // Permissions, permissions, permissions. isAllowedTo('calendar_view'); // You can't do anything if the calendar is off. if (empty($modSettings['cal_enabled'])) { fatal_lang_error('calendar_off', false); } // Set the page title to mention the calendar ;). $context['page_title'] = $context['forum_name'] . ': ' . $txt['calendar24']; // Get the current day of month... $today = array('day' => (int) strftime('%d', forum_time()), 'month' => (int) strftime('%m', forum_time()), 'year' => (int) strftime('%Y', forum_time())); $today['date'] = sprintf('%04d-%02d-%02d', $today['year'], $today['month'], $today['day']); // If the month and year are not passed in, use today's date as a starting point. $curPage = array('month' => isset($_REQUEST['month']) ? (int) $_REQUEST['month'] : $today['month'], 'year' => isset($_REQUEST['year']) ? (int) $_REQUEST['year'] : $today['year']); // Make sure the year and month are in valid ranges. if ($curPage['month'] < 1 || $curPage['month'] > 12) { fatal_lang_error('calendar1', false); } if ($curPage['year'] < $modSettings['cal_minyear'] || $curPage['year'] > $modSettings['cal_maxyear']) { fatal_lang_error('calendar2', false); } // Get information about the first day of this month. $firstDayOfMonth = array('dayOfWeek' => (int) strftime('%w', mktime(0, 0, 0, $curPage['month'], 1, $curPage['year'])), 'weekNum' => (int) strftime('%U', mktime(0, 0, 0, $curPage['month'], 1, $curPage['year']))); // Find the last day of the month. $nLastDay = (int) strftime('%d', mktime(0, 0, 0, $curPage['month'] == 12 ? 1 : $curPage['month'] + 1, 0, $curPage['month'] == 12 ? $curPage['year'] + 1 : $curPage['year'])); // The number of days the first row is shifted to the right for the starting day. $nShift = $firstDayOfMonth['dayOfWeek']; // Calendar start day- default Sunday. $nStartDay = !empty($options['calendar_start_day']) ? $options['calendar_start_day'] : 0; // Starting any day other than Sunday means a shift... if ($nStartDay) { $nShift -= $nStartDay; if ($nShift < 0) { $nShift = 7 + $nShift; } } // Number of rows required to fit the month. $nRows = floor(($nLastDay + $nShift) / 7); if (($nLastDay + $nShift) % 7) { $nRows++; } // Get the lowest and highest days of this month, in YYYY-MM-DD format. ($nLastDay is always 2 digits.) $low = $curPage['year'] . '-' . sprintf('%02d', $curPage['month']) . '-01'; $high = $curPage['year'] . '-' . sprintf('%02d', $curPage['month']) . '-' . $nLastDay; // Fetch the arrays for birthdays, posted events, and holidays. $bday = !empty($modSettings['cal_showbdaysoncalendar']) ? calendarBirthdayArray($low, $high) : array(); $events = !empty($modSettings['cal_showeventsoncalendar']) ? calendarEventArray($low, $high) : array(); $holidays = !empty($modSettings['cal_showholidaysoncalendar']) ? calendarHolidayArray($low, $high) : array(); // Days of the week taking into consideration that they may want it to start on any day. $context['week_days'] = array(); $count = $nStartDay; for ($i = 0; $i < 7; $i++) { $context['week_days'][] = $count; $count++; if ($count == 7) { $count = 0; } } // An adjustment value to apply to all calculated week numbers. if (!empty($modSettings['cal_showweeknum'])) { // Need to know what day the first of the year was on. $foy = (int) strftime('%w', mktime(0, 0, 0, 1, 1, $curPage['year'])); // If the first day of the year is a Sunday, then there is no adjustment // to be made. However, if the first day of the year is not a Sunday, then there is a partial // week at the start of the year that needs to be accounted for. if ($nStartDay == 0) { $nWeekAdjust = $foy == 0 ? 0 : 1; } else { $nWeekAdjust = $nStartDay > $foy && $foy != 0 ? 2 : 1; } // If our week starts on a day greater than the day the month starts on, then our week numbers will be one too high. // So we need to reduce it by one - all these thoughts of offsets makes my head hurt... if ($firstDayOfMonth['dayOfWeek'] < $nStartDay) { $nWeekAdjust--; } } else { $nWeekAdjust = 0; } // Basic template stuff. $context['can_post'] = allowedTo('calendar_post'); $context['last_day'] = $nLastDay; $context['current_month'] = $curPage['month']; $context['current_year'] = $curPage['year']; // Load up the linktree! $context['linktree'][] = array('url' => $scripturl . '?action=calendar;year=' . $context['current_year'] . ';month=' . $context['current_month'], 'name' => $txt['months'][$context['current_month']] . ' ' . $context['current_year']); // Iterate through each week. $context['weeks'] = array(); for ($nRow = 0; $nRow < $nRows; $nRow++) { // Start off the week - and don't let it go above 52, since that's the number of weeks in a year. $context['weeks'][$nRow] = array('days' => array(), 'number' => $firstDayOfMonth['weekNum'] + $nRow + $nWeekAdjust); // Handle the dreaded "week 53", it can happen, but only once in a blue moon ;) if ($context['weeks'][$nRow]['number'] == 53 && $nShift != 4) { $context['weeks'][$nRow]['number'] = 1; } // And figure out all the days. for ($nCol = 0; $nCol < 7; $nCol++) { $nDay = $nRow * 7 + $nCol - $nShift + 1; if ($nDay < 1 || $nDay > $context['last_day']) { $nDay = 0; } $date = sprintf('%04d-%02d-%02d', $curPage['year'], $curPage['month'], $nDay); $context['weeks'][$nRow]['days'][$nCol] = array('day' => $nDay, 'date' => $date, 'is_today' => $date == $today['date'], 'is_first_day' => !empty($modSettings['cal_showweeknum']) && ($firstDayOfMonth['dayOfWeek'] + $nDay - 1) % 7 == $nStartDay, 'holidays' => !empty($holidays[$date]) ? $holidays[$date] : array(), 'events' => !empty($events[$date]) ? $events[$date] : array(), 'birthdays' => !empty($bday[$date]) ? $bday[$date] : array()); } } // Find the previous month. (if we can go back that far.) if ($curPage['month'] > 1 || $curPage['month'] == 1 && $curPage['year'] > $modSettings['cal_minyear']) { // Need to roll the year back one? $context['previous_calendar'] = array('year' => $curPage['month'] == 1 ? $curPage['year'] - 1 : $curPage['year'], 'month' => $curPage['month'] == 1 ? 12 : $curPage['month'] - 1); $context['previous_calendar']['href'] = $scripturl . '?action=calendar;year=' . $context['previous_calendar']['year'] . ';month=' . $context['previous_calendar']['month']; } // The next month... (or can we go that far?) if ($curPage['month'] < 12 || $curPage['month'] == 12 && $curPage['year'] < $modSettings['cal_maxyear']) { $context['next_calendar'] = array('year' => $curPage['month'] == 12 ? $curPage['year'] + 1 : $curPage['year'], 'month' => $curPage['month'] == 12 ? 1 : $curPage['month'] + 1); $context['next_calendar']['href'] = $scripturl . '?action=calendar;year=' . $context['next_calendar']['year'] . ';month=' . $context['next_calendar']['month']; } }
function updateStats($type, $parameter1 = null, $parameter2 = null) { global $db_prefix, $sourcedir, $modSettings; switch ($type) { case 'member': $changes = array('memberlist_updated' => time()); // Are we using registration approval? if (!empty($modSettings['registration_method']) && $modSettings['registration_method'] == 2) { // Update the latest activated member (highest ID_MEMBER) and count. $result = db_query("\n\t\t\t\tSELECT COUNT(*), MAX(ID_MEMBER)\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE is_activated = 1", __FILE__, __LINE__); list($changes['totalMembers'], $changes['latestMember']) = mysql_fetch_row($result); mysql_free_result($result); // Get the latest activated member's display name. $result = db_query("\n\t\t\t\tSELECT realName\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE ID_MEMBER = " . (int) $changes['latestMember'] . "\n\t\t\t\tLIMIT 1", __FILE__, __LINE__); list($changes['latestRealName']) = mysql_fetch_row($result); mysql_free_result($result); // Update the amount of members awaiting approval - ignoring COPPA accounts, as you can't approve them until you get permission. $result = db_query("\n\t\t\t\tSELECT COUNT(*)\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE is_activated IN (3, 4)", __FILE__, __LINE__); list($changes['unapprovedMembers']) = mysql_fetch_row($result); mysql_free_result($result); } elseif ($parameter1 !== null && $parameter1 !== false) { $changes['latestMember'] = $parameter1; $changes['latestRealName'] = $parameter2; updateSettings(array('totalMembers' => true), true); } elseif ($parameter1 !== false) { // Update the latest member (highest ID_MEMBER) and count. $result = db_query("\n\t\t\t\tSELECT COUNT(*), MAX(ID_MEMBER)\n\t\t\t\tFROM {$db_prefix}members", __FILE__, __LINE__); list($changes['totalMembers'], $changes['latestMember']) = mysql_fetch_row($result); mysql_free_result($result); // Get the latest member's display name. $result = db_query("\n\t\t\t\tSELECT realName\n\t\t\t\tFROM {$db_prefix}members\n\t\t\t\tWHERE ID_MEMBER = " . (int) $changes['latestMember'] . "\n\t\t\t\tLIMIT 1", __FILE__, __LINE__); list($changes['latestRealName']) = mysql_fetch_row($result); mysql_free_result($result); } updateSettings($changes); break; case 'message': if ($parameter1 === true && $parameter2 !== null) { updateSettings(array('totalMessages' => true, 'maxMsgID' => $parameter2), true); } else { // SUM and MAX on a smaller table is better for InnoDB tables. $result = db_query("\n\t\t\t\tSELECT SUM(numPosts) AS totalMessages, MAX(ID_LAST_MSG) AS maxMsgID\n\t\t\t\tFROM {$db_prefix}boards", __FILE__, __LINE__); $row = mysql_fetch_assoc($result); mysql_free_result($result); updateSettings(array('totalMessages' => $row['totalMessages'], 'maxMsgID' => $row['maxMsgID'] === null ? 0 : $row['maxMsgID'])); } break; case 'subject': // Remove the previous subject (if any). db_query("\n\t\t\tDELETE FROM {$db_prefix}log_search_subjects\n\t\t\tWHERE ID_TOPIC = " . (int) $parameter1, __FILE__, __LINE__); // Insert the new subject. if ($parameter2 !== null) { $parameter1 = (int) $parameter1; $parameter2 = text2words($parameter2); $inserts = array(); foreach ($parameter2 as $word) { $inserts[] = "'{$word}', {$parameter1}"; } if (!empty($inserts)) { db_query("\n\t\t\t\t\tINSERT IGNORE INTO {$db_prefix}log_search_subjects\n\t\t\t\t\t\t(word, ID_TOPIC)\n\t\t\t\t\tVALUES (" . implode('), (', array_unique($inserts)) . ")", __FILE__, __LINE__); } } break; case 'topic': if ($parameter1 === true) { updateSettings(array('totalTopics' => true), true); } else { // Get the number of topics - a SUM is better for InnoDB tables. // We also ignore the recycle bin here because there will probably be a bunch of one-post topics there. $result = db_query("\n\t\t\t\tSELECT SUM(numTopics) AS totalTopics\n\t\t\t\tFROM {$db_prefix}boards" . (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? "\n\t\t\t\tWHERE ID_BOARD != {$modSettings['recycle_board']}" : ''), __FILE__, __LINE__); $row = mysql_fetch_assoc($result); mysql_free_result($result); updateSettings(array('totalTopics' => $row['totalTopics'])); } break; case 'calendar': require_once $sourcedir . '/Calendar.php'; // Calculate the YYYY-MM-DD of the lowest and highest days. $low_date = strftime('%Y-%m-%d', forum_time(false) - 24 * 3600); $high_date = strftime('%Y-%m-%d', forum_time(false) + $modSettings['cal_days_for_index'] * 24 * 3600); $holidays = calendarHolidayArray($low_date, $high_date); $bday = calendarBirthdayArray($low_date, $high_date); $events = calendarEventArray($low_date, $high_date, false); // Cache the results in the settings. updateSettings(array('cal_today_updated' => strftime('%Y%m%d', forum_time(false)), 'cal_today_holiday' => addslashes(serialize($holidays)), 'cal_today_birthday' => addslashes(serialize($bday)), 'cal_today_event' => addslashes(serialize($events)))); break; case 'postgroups': // Parameter two is the updated columns: we should check to see if we base groups off any of these. if ($parameter2 !== null && !in_array('posts', $parameter2)) { return; } if (($postgroups = cache_get_data('updateStats:postgroups', 360)) == null) { // Fetch the postgroups! $request = db_query("\n\t\t\t\tSELECT ID_GROUP, minPosts\n\t\t\t\tFROM {$db_prefix}membergroups\n\t\t\t\tWHERE minPosts != -1", __FILE__, __LINE__); $postgroups = array(); while ($row = mysql_fetch_assoc($request)) { $postgroups[$row['ID_GROUP']] = $row['minPosts']; } mysql_free_result($request); // Sort them this way because if it's done with MySQL it causes a filesort :(. arsort($postgroups); cache_put_data('updateStats:postgroups', $postgroups, 360); } // Oh great, they've screwed their post groups. if (empty($postgroups)) { return; } // Set all membergroups from most posts to least posts. $conditions = ''; foreach ($postgroups as $id => $minPosts) { $conditions .= ' WHEN posts >= ' . $minPosts . (!empty($lastMin) ? ' AND posts <= ' . $lastMin : '') . ' THEN ' . $id; $lastMin = $minPosts; } // A big fat CASE WHEN... END is faster than a zillion UPDATE's ;). db_query("\n\t\t\tUPDATE {$db_prefix}members\n\t\t\tSET ID_POST_GROUP = CASE{$conditions}\n\t\t\t\t\tELSE 0\n\t\t\t\tEND" . ($parameter1 != null ? "\n\t\t\tWHERE {$parameter1}" : ''), __FILE__, __LINE__); break; default: trigger_error('updateStats(): Invalid statistic type \'' . $type . '\'', E_USER_NOTICE); } }