Chris@76: 0, Chris@76: 'current_time' => time(), Chris@76: ) Chris@76: ); Chris@76: if ($smcFunc['db_num_rows']($request) != 0) Chris@76: { Chris@76: // The two important things really... Chris@76: $row = $smcFunc['db_fetch_assoc']($request); Chris@76: Chris@76: // When should this next be run? Chris@76: $next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']); Chris@76: Chris@76: // How long in seconds it the gap? Chris@76: $duration = $row['time_regularity']; Chris@76: if ($row['time_unit'] == 'm') Chris@76: $duration *= 60; Chris@76: elseif ($row['time_unit'] == 'h') Chris@76: $duration *= 3600; Chris@76: elseif ($row['time_unit'] == 'd') Chris@76: $duration *= 86400; Chris@76: elseif ($row['time_unit'] == 'w') Chris@76: $duration *= 604800; Chris@76: Chris@76: // If we were really late running this task actually skip the next one. Chris@76: if (time() + ($duration / 2) > $next_time) Chris@76: $next_time += $duration; Chris@76: Chris@76: // Update it now, so no others run this! Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}scheduled_tasks Chris@76: SET next_time = {int:next_time} Chris@76: WHERE id_task = {int:id_task} Chris@76: AND next_time = {int:current_next_time}', Chris@76: array( Chris@76: 'next_time' => $next_time, Chris@76: 'id_task' => $row['id_task'], Chris@76: 'current_next_time' => $row['next_time'], Chris@76: ) Chris@76: ); Chris@76: $affected_rows = $smcFunc['db_affected_rows'](); Chris@76: Chris@76: // The function must exist or we are wasting our time, plus do some timestamp checking, and database check! Chris@76: if (function_exists('scheduled_' . $row['task']) && (!isset($_GET['ts']) || $_GET['ts'] == $row['next_time']) && $affected_rows) Chris@76: { Chris@76: ignore_user_abort(true); Chris@76: Chris@76: // Do the task... Chris@76: $completed = call_user_func('scheduled_' . $row['task']); Chris@76: Chris@76: // Log that we did it ;) Chris@76: if ($completed) Chris@76: { Chris@76: $total_time = round(array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)), 3); Chris@76: $smcFunc['db_insert']('', Chris@76: '{db_prefix}log_scheduled_tasks', Chris@76: array( Chris@76: 'id_task' => 'int', 'time_run' => 'int', 'time_taken' => 'float', Chris@76: ), Chris@76: array( Chris@76: $row['id_task'], time(), (int) $total_time, Chris@76: ), Chris@76: array() Chris@76: ); Chris@76: } Chris@76: } Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Get the next timestamp right. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT next_time Chris@76: FROM {db_prefix}scheduled_tasks Chris@76: WHERE disabled = {int:not_disabled} Chris@76: ORDER BY next_time ASC Chris@76: LIMIT 1', Chris@76: array( Chris@76: 'not_disabled' => 0, Chris@76: ) Chris@76: ); Chris@76: // No new task scheduled yet? Chris@76: if ($smcFunc['db_num_rows']($request) === 0) Chris@76: $nextEvent = time() + 86400; Chris@76: else Chris@76: list ($nextEvent) = $smcFunc['db_fetch_row']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: updateSettings(array('next_task_time' => $nextEvent)); Chris@76: } Chris@76: Chris@76: // Shall we return? Chris@76: if (!isset($_GET['scheduled'])) Chris@76: return true; Chris@76: Chris@76: // Finally, send some stuff... Chris@76: header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); Chris@76: header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); Chris@76: header('Content-Type: image/gif'); Chris@76: die("\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00\x21\xF9\x04\x01\x00\x00\x00\x00\x2C\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02\x44\x01\x00\x3B"); Chris@76: } Chris@76: Chris@76: // Function to sending out approval notices to moderators etc. Chris@76: function scheduled_approval_notification() Chris@76: { Chris@76: global $scripturl, $modSettings, $mbname, $txt, $sourcedir, $smcFunc; Chris@76: Chris@76: // Grab all the items awaiting approval and sort type then board - clear up any things that are no longer relevant. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT aq.id_msg, aq.id_attach, aq.id_event, m.id_topic, m.id_board, m.subject, t.id_first_msg, Chris@76: b.id_profile Chris@76: FROM {db_prefix}approval_queue AS aq Chris@76: INNER JOIN {db_prefix}messages AS m ON (m.id_msg = aq.id_msg) Chris@76: INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) Chris@76: INNER JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: $notices = array(); Chris@76: $profiles = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // If this is no longer around we'll ignore it. Chris@76: if (empty($row['id_topic'])) Chris@76: continue; Chris@76: Chris@76: // What type is it? Chris@76: if ($row['id_first_msg'] && $row['id_first_msg'] == $row['id_msg']) Chris@76: $type = 'topic'; Chris@76: elseif ($row['id_attach']) Chris@76: $type = 'attach'; Chris@76: else Chris@76: $type = 'msg'; Chris@76: Chris@76: // Add it to the array otherwise. Chris@76: $notices[$row['id_board']][$type][] = array( Chris@76: 'subject' => $row['subject'], Chris@76: 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'], Chris@76: ); Chris@76: Chris@76: // Store the profile for a bit later. Chris@76: $profiles[$row['id_board']] = $row['id_profile']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Delete it all! Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}approval_queue', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: Chris@76: // If nothing quit now. Chris@76: if (empty($notices)) Chris@76: return true; Chris@76: Chris@76: // Now we need to think about finding out *who* can approve - this is hard! Chris@76: Chris@76: // First off, get all the groups with this permission and sort by board. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_group, id_profile, add_deny Chris@76: FROM {db_prefix}board_permissions Chris@76: WHERE permission = {string:approve_posts} Chris@76: AND id_profile IN ({array_int:profile_list})', Chris@76: array( Chris@76: 'profile_list' => $profiles, Chris@76: 'approve_posts' => 'approve_posts', Chris@76: ) Chris@76: ); Chris@76: $perms = array(); Chris@76: $addGroups = array(1); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // Sorry guys, but we have to ignore guests AND members - it would be too many otherwise. Chris@76: if ($row['id_group'] < 2) Chris@76: continue; Chris@76: Chris@76: $perms[$row['id_profile']][$row['add_deny'] ? 'add' : 'deny'][] = $row['id_group']; Chris@76: Chris@76: // Anyone who can access has to be considered. Chris@76: if ($row['add_deny']) Chris@76: $addGroups[] = $row['id_group']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Grab the moderators if they have permission! Chris@76: $mods = array(); Chris@76: $members = array(); Chris@76: if (in_array(2, $addGroups)) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member, id_board Chris@76: FROM {db_prefix}moderators', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $mods[$row['id_member']][$row['id_board']] = true; Chris@76: // Make sure they get included in the big loop. Chris@76: $members[] = $row['id_member']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: } Chris@76: Chris@76: // Come along one and all... until we reject you ;) Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member, real_name, email_address, lngfile, id_group, additional_groups, mod_prefs Chris@76: FROM {db_prefix}members Chris@76: WHERE id_group IN ({array_int:additional_group_list}) Chris@76: OR FIND_IN_SET({raw:additional_group_list_implode}, additional_groups) != 0' . (empty($members) ? '' : ' Chris@76: OR id_member IN ({array_int:member_list})') . ' Chris@76: ORDER BY lngfile', Chris@76: array( Chris@76: 'additional_group_list' => $addGroups, Chris@76: 'member_list' => $members, Chris@76: 'additional_group_list_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $addGroups), Chris@76: ) Chris@76: ); Chris@76: $members = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // Check whether they are interested. Chris@76: if (!empty($row['mod_prefs'])) Chris@76: { Chris@76: list(,, $pref_binary) = explode('|', $row['mod_prefs']); Chris@76: if (!($pref_binary & 4)) Chris@76: continue; Chris@76: } Chris@76: Chris@76: $members[$row['id_member']] = array( Chris@76: 'id' => $row['id_member'], Chris@76: 'groups' => array_merge(explode(',', $row['additional_groups']), array($row['id_group'])), Chris@76: 'language' => $row['lngfile'], Chris@76: 'email' => $row['email_address'], Chris@76: 'name' => $row['real_name'], Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Get the mailing stuff. Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: // Need the below for loadLanguage to work! Chris@76: loadEssentialThemeData(); Chris@76: Chris@76: // Finally, loop through each member, work out what they can do, and send it. Chris@76: foreach ($members as $id => $member) Chris@76: { Chris@76: $emailbody = ''; Chris@76: Chris@76: // Load the language file as required. Chris@76: if (empty($current_language) || $current_language != $member['language']) Chris@76: $current_language = loadLanguage('EmailTemplates', $member['language'], false); Chris@76: Chris@76: // Loop through each notice... Chris@76: foreach ($notices as $board => $notice) Chris@76: { Chris@76: $access = false; Chris@76: Chris@76: // Can they mod in this board? Chris@76: if (isset($mods[$id][$board])) Chris@76: $access = true; Chris@76: Chris@76: // Do the group check... Chris@76: if (!$access && isset($perms[$profiles[$board]]['add'])) Chris@76: { Chris@76: // They can access?! Chris@76: if (array_intersect($perms[$profiles[$board]]['add'], $member['groups'])) Chris@76: $access = true; Chris@76: Chris@76: // If they have deny rights don't consider them! Chris@76: if (isset($perms[$profiles[$board]]['deny'])) Chris@76: if (array_intersect($perms[$profiles[$board]]['deny'], $member['groups'])) Chris@76: $access = false; Chris@76: } Chris@76: Chris@76: // Finally, fix it for admins! Chris@76: if (in_array(1, $member['groups'])) Chris@76: $access = true; Chris@76: Chris@76: // If they can't access it then give it a break! Chris@76: if (!$access) Chris@76: continue; Chris@76: Chris@76: foreach ($notice as $type => $items) Chris@76: { Chris@76: // Build up the top of this section. Chris@76: $emailbody .= $txt['scheduled_approval_email_' . $type] . "\n" . Chris@76: '------------------------------------------------------' . "\n"; Chris@76: Chris@76: foreach ($items as $item) Chris@76: $emailbody .= $item['subject'] . ' - ' . $item['href'] . "\n"; Chris@76: Chris@76: $emailbody .= "\n"; Chris@76: } Chris@76: } Chris@76: Chris@76: if ($emailbody == '') Chris@76: continue; Chris@76: Chris@76: $replacements = array( Chris@76: 'REALNAME' => $member['name'], Chris@76: 'BODY' => $emailbody, Chris@76: ); Chris@76: Chris@76: $emaildata = loadEmailTemplate('scheduled_approval', $replacements, $current_language); Chris@76: Chris@76: // Send the actual email. Chris@76: sendmail($member['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 2); Chris@76: } Chris@76: Chris@76: // All went well! Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Do some daily cleaning up. Chris@76: function scheduled_daily_maintenance() Chris@76: { Chris@76: global $smcFunc, $modSettings, $sourcedir, $db_type; Chris@76: Chris@76: // First clean out the cache. Chris@76: clean_cache(); Chris@76: Chris@76: // If warning decrement is enabled and we have people who have not had a new warning in 24 hours, lower their warning level. Chris@76: list (, , $modSettings['warning_decrement']) = explode(',', $modSettings['warning_settings']); Chris@76: if ($modSettings['warning_decrement']) Chris@76: { Chris@76: // Find every member who has a warning level... Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member, warning Chris@76: FROM {db_prefix}members Chris@76: WHERE warning > {int:no_warning}', Chris@76: array( Chris@76: 'no_warning' => 0, Chris@76: ) Chris@76: ); Chris@76: $members = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $members[$row['id_member']] = $row['warning']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Have some members to check? Chris@76: if (!empty($members)) Chris@76: { Chris@76: // Find out when they were last warned. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_recipient, MAX(log_time) AS last_warning Chris@76: FROM {db_prefix}log_comments Chris@76: WHERE id_recipient IN ({array_int:member_list}) Chris@76: AND comment_type = {string:warning} Chris@76: GROUP BY id_recipient', Chris@76: array( Chris@76: 'member_list' => array_keys($members), Chris@76: 'warning' => 'warning', Chris@76: ) Chris@76: ); Chris@76: $member_changes = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // More than 24 hours ago? Chris@76: if ($row['last_warning'] <= time() - 86400) Chris@76: $member_changes[] = array( Chris@76: 'id' => $row['id_recipient'], Chris@76: 'warning' => $members[$row['id_recipient']] >= $modSettings['warning_decrement'] ? $members[$row['id_recipient']] - $modSettings['warning_decrement'] : 0, Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Have some members to change? Chris@76: if (!empty($member_changes)) Chris@76: foreach ($member_changes as $change) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}members Chris@76: SET warning = {int:warning} Chris@76: WHERE id_member = {int:id_member}', Chris@76: array( Chris@76: 'warning' => $change['warning'], Chris@76: 'id_member' => $change['id'], Chris@76: ) Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Do any spider stuff. Chris@76: if (!empty($modSettings['spider_mode']) && $modSettings['spider_mode'] > 1) Chris@76: { Chris@76: require_once($sourcedir . '/ManageSearchEngines.php'); Chris@76: consolidateSpiderStats(); Chris@76: } Chris@76: Chris@76: // Check the database version - for some buggy MySQL version. Chris@76: $server_version = $smcFunc['db_server_info'](); Chris@76: if ($db_type == 'mysql' && in_array(substr($server_version, 0, 6), array('5.0.50', '5.0.51'))) Chris@76: updateSettings(array('db_mysql_group_by_fix' => '1')); Chris@76: elseif (!empty($modSettings['db_mysql_group_by_fix'])) Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}settings Chris@76: WHERE variable = {string:mysql_fix}', Chris@76: array( Chris@76: 'mysql_fix' => 'db_mysql_group_by_fix', Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Regenerate the Diffie-Hellman keys if OpenID is enabled. Chris@76: if (!empty($modSettings['enableOpenID'])) Chris@76: { Chris@76: require_once($sourcedir . '/Subs-OpenID.php'); Chris@76: smf_openID_setup_DH(true); Chris@76: } Chris@76: elseif (!empty($modSettings['dh_keys'])) Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}settings Chris@76: WHERE variable = {string:dh_keys}', Chris@76: array( Chris@76: 'dh_keys' => 'dh_keys', Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Log we've done it... Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Auto optimize the database? Chris@76: function scheduled_auto_optimize() Chris@76: { Chris@76: global $modSettings, $smcFunc, $db_prefix, $db_type; Chris@76: Chris@76: // By default do it now! Chris@76: $delay = false; Chris@76: Chris@76: // As a kind of hack, if the server load is too great delay, but only by a bit! Chris@76: if (!empty($modSettings['load_average']) && !empty($modSettings['loadavg_auto_opt']) && $modSettings['load_average'] >= $modSettings['loadavg_auto_opt']) Chris@76: $delay = true; Chris@76: Chris@76: // Otherwise are we restricting the number of people online for this? Chris@76: if (!empty($modSettings['autoOptMaxOnline'])) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT COUNT(*) Chris@76: FROM {db_prefix}log_online', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: list ($dont_do_it) = $smcFunc['db_fetch_row']($request); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if ($dont_do_it > $modSettings['autoOptMaxOnline']) Chris@76: $delay = true; Chris@76: } Chris@76: Chris@76: // If we are gonna delay, do so now! Chris@76: if ($delay) Chris@76: return false; Chris@76: Chris@76: db_extend(); Chris@76: Chris@76: // Get all the tables. Chris@76: $tables = $smcFunc['db_list_tables'](false, $db_prefix . '%'); Chris@76: Chris@76: // Actually do the optimisation. Chris@76: if ($db_type == 'sqlite') Chris@76: $smcFunc['db_optimize_table']($table[0]); Chris@76: else Chris@76: foreach ($tables as $table) Chris@76: $smcFunc['db_optimize_table']($table); Chris@76: Chris@76: // Return for the log... Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Send out a daily email of all subscribed topics. Chris@76: function scheduled_daily_digest() Chris@76: { Chris@76: global $is_weekly, $txt, $mbname, $scripturl, $sourcedir, $smcFunc, $context, $modSettings; Chris@76: Chris@76: // We'll want this... Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: loadEssentialThemeData(); Chris@76: Chris@76: $is_weekly = !empty($is_weekly) ? 1 : 0; Chris@76: Chris@76: // Right - get all the notification data FIRST. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT ln.id_topic, COALESCE(t.id_board, ln.id_board) AS id_board, mem.email_address, mem.member_name, mem.notify_types, Chris@76: mem.lngfile, mem.id_member Chris@76: FROM {db_prefix}log_notify AS ln Chris@76: INNER JOIN {db_prefix}members AS mem ON (mem.id_member = ln.id_member) Chris@76: LEFT JOIN {db_prefix}topics AS t ON (ln.id_topic != {int:empty_topic} AND t.id_topic = ln.id_topic) Chris@76: WHERE mem.notify_regularity = {int:notify_regularity} Chris@76: AND mem.is_activated = {int:is_activated}', Chris@76: array( Chris@76: 'empty_topic' => 0, Chris@76: 'notify_regularity' => $is_weekly ? '3' : '2', Chris@76: 'is_activated' => 1, Chris@76: ) Chris@76: ); Chris@76: $members = array(); Chris@76: $langs = array(); Chris@76: $notify = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: if (!isset($members[$row['id_member']])) Chris@76: { Chris@76: $members[$row['id_member']] = array( Chris@76: 'email' => $row['email_address'], Chris@76: 'name' => $row['member_name'], Chris@76: 'id' => $row['id_member'], Chris@76: 'notifyMod' => $row['notify_types'] < 3 ? true : false, Chris@76: 'lang' => $row['lngfile'], Chris@76: ); Chris@76: $langs[$row['lngfile']] = $row['lngfile']; Chris@76: } Chris@76: Chris@76: // Store this useful data! Chris@76: $boards[$row['id_board']] = $row['id_board']; Chris@76: if ($row['id_topic']) Chris@76: $notify['topics'][$row['id_topic']][] = $row['id_member']; Chris@76: else Chris@76: $notify['boards'][$row['id_board']][] = $row['id_member']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (empty($boards)) Chris@76: return true; Chris@76: Chris@76: // Just get the board names. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_board, name Chris@76: FROM {db_prefix}boards Chris@76: WHERE id_board IN ({array_int:board_list})', Chris@76: array( Chris@76: 'board_list' => $boards, Chris@76: ) Chris@76: ); Chris@76: $boards = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $boards[$row['id_board']] = $row['name']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (empty($boards)) Chris@76: return true; Chris@76: Chris@76: // Get the actual topics... Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT ld.note_type, t.id_topic, t.id_board, t.id_member_started, m.id_msg, m.subject, Chris@76: b.name AS board_name Chris@76: FROM {db_prefix}log_digest AS ld Chris@76: INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ld.id_topic Chris@76: AND t.id_board IN ({array_int:board_list})) Chris@76: INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_first_msg) Chris@76: INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) Chris@76: WHERE ' . ($is_weekly ? 'ld.daily != {int:daily_value}' : 'ld.daily IN (0, 2)'), Chris@76: array( Chris@76: 'board_list' => array_keys($boards), Chris@76: 'daily_value' => 2, Chris@76: ) Chris@76: ); Chris@76: $types = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: if (!isset($types[$row['note_type']][$row['id_board']])) Chris@76: $types[$row['note_type']][$row['id_board']] = array( Chris@76: 'lines' => array(), Chris@76: 'name' => $row['board_name'], Chris@76: 'id' => $row['id_board'], Chris@76: ); Chris@76: Chris@76: if ($row['note_type'] == 'reply') Chris@76: { Chris@76: if (isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['count']++; Chris@76: else Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( Chris@76: 'id' => $row['id_topic'], Chris@76: 'subject' => un_htmlspecialchars($row['subject']), Chris@76: 'count' => 1, Chris@76: ); Chris@76: } Chris@76: elseif ($row['note_type'] == 'topic') Chris@76: { Chris@76: if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( Chris@76: 'id' => $row['id_topic'], Chris@76: 'subject' => un_htmlspecialchars($row['subject']), Chris@76: ); Chris@76: } Chris@76: else Chris@76: { Chris@76: if (!isset($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']])) Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']] = array( Chris@76: 'id' => $row['id_topic'], Chris@76: 'subject' => un_htmlspecialchars($row['subject']), Chris@76: 'starter' => $row['id_member_started'], Chris@76: ); Chris@76: } Chris@76: Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array(); Chris@76: if (!empty($notify['topics'][$row['id_topic']])) Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['topics'][$row['id_topic']]); Chris@76: if (!empty($notify['boards'][$row['id_board']])) Chris@76: $types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'] = array_merge($types[$row['note_type']][$row['id_board']]['lines'][$row['id_topic']]['members'], $notify['boards'][$row['id_board']]); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (empty($types)) Chris@76: return true; Chris@76: Chris@76: // Let's load all the languages into a cache thingy. Chris@76: $langtxt = array(); Chris@76: foreach ($langs as $lang) Chris@76: { Chris@76: loadLanguage('Post', $lang); Chris@76: loadLanguage('index', $lang); Chris@76: loadLanguage('EmailTemplates', $lang); Chris@76: $langtxt[$lang] = array( Chris@76: 'subject' => $txt['digest_subject_' . ($is_weekly ? 'weekly' : 'daily')], Chris@76: 'char_set' => $txt['lang_character_set'], Chris@76: 'intro' => sprintf($txt['digest_intro_' . ($is_weekly ? 'weekly' : 'daily')], $mbname), Chris@76: 'new_topics' => $txt['digest_new_topics'], Chris@76: 'topic_lines' => $txt['digest_new_topics_line'], Chris@76: 'new_replies' => $txt['digest_new_replies'], Chris@76: 'mod_actions' => $txt['digest_mod_actions'], Chris@76: 'replies_one' => $txt['digest_new_replies_one'], Chris@76: 'replies_many' => $txt['digest_new_replies_many'], Chris@76: 'sticky' => $txt['digest_mod_act_sticky'], Chris@76: 'lock' => $txt['digest_mod_act_lock'], Chris@76: 'unlock' => $txt['digest_mod_act_unlock'], Chris@76: 'remove' => $txt['digest_mod_act_remove'], Chris@76: 'move' => $txt['digest_mod_act_move'], Chris@76: 'merge' => $txt['digest_mod_act_merge'], Chris@76: 'split' => $txt['digest_mod_act_split'], Chris@76: 'bye' => $txt['regards_team'], Chris@76: ); Chris@76: } Chris@76: Chris@76: // Right - send out the silly things - this will take quite some space! Chris@76: $emails = array(); Chris@76: foreach ($members as $mid => $member) Chris@76: { Chris@76: // Right character set! Chris@76: $context['character_set'] = empty($modSettings['global_character_set']) ? $langtxt[$lang]['char_set'] : $modSettings['global_character_set']; Chris@76: Chris@76: // Do the start stuff! Chris@76: $email = array( Chris@76: 'subject' => $mbname . ' - ' . $langtxt[$lang]['subject'], Chris@76: 'body' => $member['name'] . ',' . "\n\n" . $langtxt[$lang]['intro'] . "\n" . $scripturl . '?action=profile;area=notification;u=' . $member['id'] . "\n", Chris@76: 'email' => $member['email'], Chris@76: ); Chris@76: Chris@76: // All new topics? Chris@76: if (isset($types['topic'])) Chris@76: { Chris@76: $titled = false; Chris@76: foreach ($types['topic'] as $id => $board) Chris@76: foreach ($board['lines'] as $topic) Chris@76: if (in_array($mid, $topic['members'])) Chris@76: { Chris@76: if (!$titled) Chris@76: { Chris@76: $email['body'] .= "\n" . $langtxt[$lang]['new_topics'] . ':' . "\n" . '-----------------------------------------------'; Chris@76: $titled = true; Chris@76: } Chris@76: $email['body'] .= "\n" . sprintf($langtxt[$lang]['topic_lines'], $topic['subject'], $board['name']); Chris@76: } Chris@76: if ($titled) Chris@76: $email['body'] .= "\n"; Chris@76: } Chris@76: Chris@76: // What about replies? Chris@76: if (isset($types['reply'])) Chris@76: { Chris@76: $titled = false; Chris@76: foreach ($types['reply'] as $id => $board) Chris@76: foreach ($board['lines'] as $topic) Chris@76: if (in_array($mid, $topic['members'])) Chris@76: { Chris@76: if (!$titled) Chris@76: { Chris@76: $email['body'] .= "\n" . $langtxt[$lang]['new_replies'] . ':' . "\n" . '-----------------------------------------------'; Chris@76: $titled = true; Chris@76: } Chris@76: $email['body'] .= "\n" . ($topic['count'] == 1 ? sprintf($langtxt[$lang]['replies_one'], $topic['subject']) : sprintf($langtxt[$lang]['replies_many'], $topic['count'], $topic['subject'])); Chris@76: } Chris@76: Chris@76: if ($titled) Chris@76: $email['body'] .= "\n"; Chris@76: } Chris@76: Chris@76: // Finally, moderation actions! Chris@76: $titled = false; Chris@76: foreach ($types as $note_type => $type) Chris@76: { Chris@76: if ($note_type == 'topic' || $note_type == 'reply') Chris@76: continue; Chris@76: Chris@76: foreach ($type as $id => $board) Chris@76: foreach ($board['lines'] as $topic) Chris@76: if (in_array($mid, $topic['members'])) Chris@76: { Chris@76: if (!$titled) Chris@76: { Chris@76: $email['body'] .= "\n" . $langtxt[$lang]['mod_actions'] . ':' . "\n" . '-----------------------------------------------'; Chris@76: $titled = true; Chris@76: } Chris@76: $email['body'] .= "\n" . sprintf($langtxt[$lang][$note_type], $topic['subject']); Chris@76: } Chris@76: Chris@76: } Chris@76: if ($titled) Chris@76: $email['body'] .= "\n"; Chris@76: Chris@76: // Then just say our goodbyes! Chris@76: $email['body'] .= "\n\n" . $txt['regards_team']; Chris@76: Chris@76: // Send it - low priority! Chris@76: sendmail($email['email'], $email['subject'], $email['body'], null, null, false, 4); Chris@76: } Chris@76: Chris@76: // Clean up... Chris@76: if ($is_weekly) Chris@76: { Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_digest Chris@76: WHERE daily != {int:not_daily}', Chris@76: array( Chris@76: 'not_daily' => 0, Chris@76: ) Chris@76: ); Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}log_digest Chris@76: SET daily = {int:daily_value} Chris@76: WHERE daily = {int:not_daily}', Chris@76: array( Chris@76: 'daily_value' => 2, Chris@76: 'not_daily' => 0, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: else Chris@76: { Chris@76: // Clear any only weekly ones, and stop us from sending daily again. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_digest Chris@76: WHERE daily = {int:daily_value}', Chris@76: array( Chris@76: 'daily_value' => 2, Chris@76: ) Chris@76: ); Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}log_digest Chris@76: SET daily = {int:both_value} Chris@76: WHERE daily = {int:no_value}', Chris@76: array( Chris@76: 'both_value' => 1, Chris@76: 'no_value' => 0, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: // Just in case the member changes their settings mark this as sent. Chris@76: $members = array_keys($members); Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}log_notify Chris@76: SET sent = {int:is_sent} Chris@76: WHERE id_member IN ({array_int:member_list})', Chris@76: array( Chris@76: 'member_list' => $members, Chris@76: 'is_sent' => 1, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Log we've done it... Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Like the daily stuff - just seven times less regular ;) Chris@76: function scheduled_weekly_digest() Chris@76: { Chris@76: global $is_weekly; Chris@76: Chris@76: // We just pass through to the daily function - avoid duplication! Chris@76: $is_weekly = true; Chris@76: return scheduled_daily_digest(); Chris@76: } Chris@76: Chris@76: // Send a bunch of emails from the mail queue. Chris@76: function ReduceMailQueue($number = false, $override_limit = false, $force_send = false) Chris@76: { Chris@76: global $modSettings, $smcFunc, $sourcedir; Chris@76: Chris@76: // Are we intending another script to be sending out the queue? Chris@76: if (!empty($modSettings['mail_queue_use_cron']) && empty($force_send)) Chris@76: return false; Chris@76: Chris@76: // By default send 5 at once. Chris@76: if (!$number) Chris@76: $number = empty($modSettings['mail_quantity']) ? 5 : $modSettings['mail_quantity']; Chris@76: Chris@76: // If we came with a timestamp, and that doesn't match the next event, then someone else has beaten us. Chris@76: if (isset($_GET['ts']) && $_GET['ts'] != $modSettings['mail_next_send'] && empty($force_send)) Chris@76: return false; Chris@76: Chris@76: // By default move the next sending on by 10 seconds, and require an affected row. Chris@76: if (!$override_limit) Chris@76: { Chris@76: $delay = !empty($modSettings['mail_queue_delay']) ? $modSettings['mail_queue_delay'] : (!empty($modSettings['mail_limit']) && $modSettings['mail_limit'] < 5 ? 10 : 5); Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}settings Chris@76: SET value = {string:next_mail_send} Chris@76: WHERE variable = {string:mail_next_send} Chris@76: AND value = {string:last_send}', Chris@76: array( Chris@76: 'next_mail_send' => time() + $delay, Chris@76: 'mail_next_send' => 'mail_next_send', Chris@76: 'last_send' => $modSettings['mail_next_send'], Chris@76: ) Chris@76: ); Chris@76: if ($smcFunc['db_affected_rows']() == 0) Chris@76: return false; Chris@76: $modSettings['mail_next_send'] = time() + $delay; Chris@76: } Chris@76: Chris@76: // If we're not overriding how many are we allow to send? Chris@76: if (!$override_limit && !empty($modSettings['mail_limit'])) Chris@76: { Chris@76: list ($mt, $mn) = @explode('|', $modSettings['mail_recent']); Chris@76: Chris@76: // Nothing worth noting... Chris@76: if (empty($mn) || $mt < time() - 60) Chris@76: { Chris@76: $mt = time(); Chris@76: $mn = $number; Chris@76: } Chris@76: // Otherwise we have a few more we can spend? Chris@76: elseif ($mn < $modSettings['mail_limit']) Chris@76: { Chris@76: $mn += $number; Chris@76: } Chris@76: // No more I'm afraid, return! Chris@76: else Chris@76: return false; Chris@76: Chris@76: // Reflect that we're about to send some, do it now to be safe. Chris@76: updateSettings(array('mail_recent' => $mt . '|' . $mn)); Chris@76: } Chris@76: Chris@76: // Now we know how many we're sending, let's send them. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT /*!40001 SQL_NO_CACHE */ id_mail, recipient, body, subject, headers, send_html Chris@76: FROM {db_prefix}mail_queue Chris@76: ORDER BY priority ASC, id_mail ASC Chris@76: LIMIT ' . $number, Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: $ids = array(); Chris@76: $emails = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // We want to delete these from the database ASAP, so just get the data and go. Chris@76: $ids[] = $row['id_mail']; Chris@76: $emails[] = array( Chris@76: 'to' => $row['recipient'], Chris@76: 'body' => $row['body'], Chris@76: 'subject' => $row['subject'], Chris@76: 'headers' => $row['headers'], Chris@76: 'send_html' => $row['send_html'], Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Delete, delete, delete!!! Chris@76: if (!empty($ids)) Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}mail_queue Chris@76: WHERE id_mail IN ({array_int:mail_list})', Chris@76: array( Chris@76: 'mail_list' => $ids, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Don't believe we have any left? Chris@76: if (count($ids) < $number) Chris@76: { Chris@76: // Only update the setting if no-one else has beaten us to it. Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}settings Chris@76: SET value = {string:no_send} Chris@76: WHERE variable = {string:mail_next_send} Chris@76: AND value = {string:last_mail_send}', Chris@76: array( Chris@76: 'no_send' => '0', Chris@76: 'mail_next_send' => 'mail_next_send', Chris@76: 'last_mail_send' => $modSettings['mail_next_send'], Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: if (empty($ids)) Chris@76: return false; Chris@76: Chris@76: if (!empty($modSettings['mail_type']) && $modSettings['smtp_host'] != '') Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: Chris@76: // Send each email, yea! Chris@76: $failed_emails = array(); Chris@76: foreach ($emails as $key => $email) Chris@76: { Chris@76: if (empty($modSettings['mail_type']) || $modSettings['smtp_host'] == '') Chris@76: { Chris@76: $email['subject'] = strtr($email['subject'], array("\r" => '', "\n" => '')); Chris@76: if (!empty($modSettings['mail_strip_carriage'])) Chris@76: { Chris@76: $email['body'] = strtr($email['body'], array("\r" => '')); Chris@76: $email['headers'] = strtr($email['headers'], array("\r" => '')); Chris@76: } Chris@76: Chris@76: // No point logging a specific error here, as we have no language. PHP error is helpful anyway... Chris@76: $result = mail(strtr($email['to'], array("\r" => '', "\n" => '')), $email['subject'], $email['body'], $email['headers']); Chris@76: Chris@76: // Try to stop a timeout, this would be bad... Chris@76: @set_time_limit(300); Chris@76: if (function_exists('apache_reset_timeout')) Chris@76: @apache_reset_timeout(); Chris@76: } Chris@76: else Chris@76: $result = smtp_mail(array($email['to']), $email['subject'], $email['body'], $email['send_html'] ? $email['headers'] : 'Mime-Version: 1.0' . "\r\n" . $email['headers']); Chris@76: Chris@76: // Hopefully it sent? Chris@76: if (!$result) Chris@76: $failed_emails[] = array($email['to'], $email['body'], $email['subject'], $email['headers'], $email['send_html']); Chris@76: } Chris@76: Chris@76: // Any emails that didn't send? Chris@76: if (!empty($failed_emails)) Chris@76: { Chris@76: // Update the failed attempts check. Chris@76: $smcFunc['db_insert']('replace', Chris@76: '{db_prefix}settings', Chris@76: array('variable' => 'string', 'value' => 'string'), Chris@76: array('mail_failed_attempts', empty($modSettings['mail_failed_attempts']) ? 1 : ++$modSettings['mail_failed_attempts']), Chris@76: array('variable') Chris@76: ); Chris@76: Chris@76: // If we have failed to many times, tell mail to wait a bit and try again. Chris@76: if ($modSettings['mail_failed_attempts'] > 5) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}settings Chris@76: SET value = {string:mail_next_send} Chris@76: WHERE variable = {string:next_mail_send} Chris@76: AND value = {string:last_send}', Chris@76: array( Chris@76: 'next_mail_send' => time() + 60, Chris@76: 'mail_next_send' => 'mail_next_send', Chris@76: 'last_send' => $modSettings['mail_next_send'], Chris@76: )); Chris@76: Chris@76: // Add our email back to the queue, manually. Chris@76: $smcFunc['db_insert']('insert', Chris@76: '{db_prefix}mail_queue', Chris@76: array('recipient' => 'string', 'body' => 'string', 'subject' => 'string', 'headers' => 'string', 'send_html' => 'string'), Chris@76: $failed_emails, Chris@76: array('id_mail') Chris@76: ); Chris@76: Chris@76: return false; Chris@76: } Chris@76: // We where unable to send the email, clear our failed attempts. Chris@76: elseif (!empty($modSettings['mail_failed_attempts'])) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}settings Chris@76: SET value = {string:zero} Chris@76: WHERE variable = {string:mail_failed_attempts}', Chris@76: array( Chris@76: 'zero' => '0', Chris@76: 'mail_failed_attempts' => 'mail_failed_attempts', Chris@76: )); Chris@76: Chris@76: // Had something to send... Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Calculate the next time the passed tasks should be triggered. Chris@76: function CalculateNextTrigger($tasks = array(), $forceUpdate = false) Chris@76: { Chris@76: global $modSettings, $smcFunc; Chris@76: Chris@76: $task_query = ''; Chris@76: if (!is_array($tasks)) Chris@76: $tasks = array($tasks); Chris@76: Chris@76: // Actually have something passed? Chris@76: if (!empty($tasks)) Chris@76: { Chris@76: if (!isset($tasks[0]) || is_numeric($tasks[0])) Chris@76: $task_query = ' AND id_task IN ({array_int:tasks})'; Chris@76: else Chris@76: $task_query = ' AND task IN ({array_string:tasks})'; Chris@76: } Chris@76: $nextTaskTime = empty($tasks) ? time() + 86400 : $modSettings['next_task_time']; Chris@76: Chris@76: // Get the critical info for the tasks. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_task, next_time, time_offset, time_regularity, time_unit Chris@76: FROM {db_prefix}scheduled_tasks Chris@76: WHERE disabled = {int:no_disabled} Chris@76: ' . $task_query, Chris@76: array( Chris@76: 'no_disabled' => 0, Chris@76: 'tasks' => $tasks, Chris@76: ) Chris@76: ); Chris@76: $tasks = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $next_time = next_time($row['time_regularity'], $row['time_unit'], $row['time_offset']); Chris@76: Chris@76: // Only bother moving the task if it's out of place or we're forcing it! Chris@76: if ($forceUpdate || $next_time < $row['next_time'] || $row['next_time'] < time()) Chris@76: $tasks[$row['id_task']] = $next_time; Chris@76: else Chris@76: $next_time = $row['next_time']; Chris@76: Chris@76: // If this is sooner than the current next task, make this the next task. Chris@76: if ($next_time < $nextTaskTime) Chris@76: $nextTaskTime = $next_time; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Now make the changes! Chris@76: foreach ($tasks as $id => $time) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}scheduled_tasks Chris@76: SET next_time = {int:next_time} Chris@76: WHERE id_task = {int:id_task}', Chris@76: array( Chris@76: 'next_time' => $time, Chris@76: 'id_task' => $id, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // If the next task is now different update. Chris@76: if ($modSettings['next_task_time'] != $nextTaskTime) Chris@76: updateSettings(array('next_task_time' => $nextTaskTime)); Chris@76: } Chris@76: Chris@76: // Simply returns a time stamp of the next instance of these time parameters. Chris@76: function next_time($regularity, $unit, $offset) Chris@76: { Chris@76: // Just in case! Chris@76: if ($regularity == 0) Chris@76: $regularity = 2; Chris@76: Chris@76: $curHour = date('H', time()); Chris@76: $curMin = date('i', time()); Chris@76: $next_time = 9999999999; Chris@76: Chris@76: // If the unit is minutes only check regularity in minutes. Chris@76: if ($unit == 'm') Chris@76: { Chris@76: $off = date('i', $offset); Chris@76: Chris@76: // If it's now just pretend it ain't, Chris@76: if ($off == $curMin) Chris@76: $next_time = time() + $regularity; Chris@76: else Chris@76: { Chris@76: // Make sure that the offset is always in the past. Chris@76: $off = $off > $curMin ? $off - 60 : $off; Chris@76: Chris@76: while ($off <= $curMin) Chris@76: $off += $regularity; Chris@76: Chris@76: // Now we know when the time should be! Chris@76: $next_time = time() + 60 * ($off - $curMin); Chris@76: } Chris@76: } Chris@76: // Otherwise, work out what the offset would be with todays date. Chris@76: else Chris@76: { Chris@76: $next_time = mktime(date('H', $offset), date('i', $offset), 0, date('m'), date('d'), date('Y')); Chris@76: Chris@76: // Make the time offset in the past! Chris@76: if ($next_time > time()) Chris@76: { Chris@76: $next_time -= 86400; Chris@76: } Chris@76: Chris@76: // Default we'll jump in hours. Chris@76: $applyOffset = 3600; Chris@76: // 24 hours = 1 day. Chris@76: if ($unit == 'd') Chris@76: $applyOffset = 86400; Chris@76: // Otherwise a week. Chris@76: if ($unit == 'w') Chris@76: $applyOffset = 604800; Chris@76: Chris@76: $applyOffset *= $regularity; Chris@76: Chris@76: // Just add on the offset. Chris@76: while ($next_time <= time()) Chris@76: { Chris@76: $next_time += $applyOffset; Chris@76: } Chris@76: } Chris@76: Chris@76: return $next_time; Chris@76: } Chris@76: Chris@76: // This loads the bare minimum data to allow us to load language files! Chris@76: function loadEssentialThemeData() Chris@76: { Chris@76: global $settings, $modSettings, $smcFunc, $mbname, $context, $sourcedir; Chris@76: Chris@76: // Get all the default theme variables. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_theme, variable, value Chris@76: FROM {db_prefix}themes Chris@76: WHERE id_member = {int:no_member} Chris@76: AND id_theme IN (1, {int:theme_guests})', Chris@76: array( Chris@76: 'no_member' => 0, Chris@76: 'theme_guests' => $modSettings['theme_guests'], Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: $settings[$row['variable']] = $row['value']; Chris@76: Chris@76: // Is this the default theme? Chris@76: if (in_array($row['variable'], array('theme_dir', 'theme_url', 'images_url')) && $row['id_theme'] == '1') Chris@76: $settings['default_' . $row['variable']] = $row['value']; Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // Check we have some directories setup. Chris@76: if (empty($settings['template_dirs'])) Chris@76: { Chris@76: $settings['template_dirs'] = array($settings['theme_dir']); Chris@76: Chris@76: // Based on theme (if there is one). Chris@76: if (!empty($settings['base_theme_dir'])) Chris@76: $settings['template_dirs'][] = $settings['base_theme_dir']; Chris@76: Chris@76: // Lastly the default theme. Chris@76: if ($settings['theme_dir'] != $settings['default_theme_dir']) Chris@76: $settings['template_dirs'][] = $settings['default_theme_dir']; Chris@76: } Chris@76: Chris@76: // Assume we want this. Chris@76: $context['forum_name'] = $mbname; Chris@76: Chris@76: // Check loadLanguage actually exists! Chris@76: if (!function_exists('loadLanguage')) Chris@76: { Chris@76: require_once($sourcedir . '/Load.php'); Chris@76: require_once($sourcedir . '/Subs.php'); Chris@76: } Chris@76: Chris@76: loadLanguage('index+Modifications'); Chris@76: } Chris@76: Chris@76: function scheduled_fetchSMfiles() Chris@76: { Chris@76: global $sourcedir, $txt, $language, $settings, $forum_version, $modSettings, $smcFunc; Chris@76: Chris@76: // What files do we want to get Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_file, filename, path, parameters Chris@76: FROM {db_prefix}admin_info_files', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: Chris@76: $js_files = array(); Chris@76: Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $js_files[$row['id_file']] = array( Chris@76: 'filename' => $row['filename'], Chris@76: 'path' => $row['path'], Chris@76: 'parameters' => sprintf($row['parameters'], $language, urlencode($modSettings['time_format']), urlencode($forum_version)), Chris@76: ); Chris@76: } Chris@76: Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // We're gonna need fetch_web_data() to pull this off. Chris@76: require_once($sourcedir . '/Subs-Package.php'); Chris@76: Chris@76: // Just in case we run into a problem. Chris@76: loadEssentialThemeData(); Chris@76: loadLanguage('Errors', $language, false); Chris@76: Chris@76: foreach ($js_files as $ID_FILE => $file) Chris@76: { Chris@76: // Create the url Chris@76: $server = empty($file['path']) || substr($file['path'], 0, 7) != 'http://' ? 'http://www.simplemachines.org' : ''; Chris@76: $url = $server . (!empty($file['path']) ? $file['path'] : $file['path']) . $file['filename'] . (!empty($file['parameters']) ? '?' . $file['parameters'] : ''); Chris@76: Chris@76: // Get the file Chris@76: $file_data = fetch_web_data($url); Chris@76: Chris@76: // If we got an error - give up - the site might be down. Chris@76: if ($file_data === false) Chris@76: { Chris@76: log_error(sprintf($txt['st_cannot_retrieve_file'], $url)); Chris@76: return false; Chris@76: } Chris@76: Chris@76: // Save the file to the database. Chris@76: $smcFunc['db_query']('substring', ' Chris@76: UPDATE {db_prefix}admin_info_files Chris@76: SET data = SUBSTRING({string:file_data}, 1, 65534) Chris@76: WHERE id_file = {int:id_file}', Chris@76: array( Chris@76: 'id_file' => $ID_FILE, Chris@76: 'file_data' => $file_data, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: return true; Chris@76: } Chris@76: Chris@76: function scheduled_birthdayemails() Chris@76: { Chris@76: global $modSettings, $sourcedir, $mbname, $txt, $smcFunc, $birthdayEmails; Chris@76: Chris@76: // Need this in order to load the language files. Chris@76: loadEssentialThemeData(); Chris@76: Chris@76: // Going to need this to send the emails. Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: Chris@76: $greeting = isset($modSettings['birthday_email']) ? $modSettings['birthday_email'] : 'happy_birthday'; Chris@76: Chris@76: // Get the month and day of today. Chris@76: $month = date('n'); // Month without leading zeros. Chris@76: $day = date('j'); // Day without leading zeros. Chris@76: Chris@76: // So who are the lucky ones? Don't include those who are banned and those who don't want them. Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_member, real_name, lngfile, email_address Chris@76: FROM {db_prefix}members Chris@76: WHERE is_activated < 10 Chris@76: AND MONTH(birthdate) = {int:month} Chris@76: AND DAYOFMONTH(birthdate) = {int:day} Chris@76: AND notify_announcements = {int:notify_announcements} Chris@76: AND YEAR(birthdate) > {int:year}', Chris@76: array( Chris@76: 'notify_announcements' => 1, Chris@76: 'year' => 1, Chris@76: 'month' => $month, Chris@76: 'day' => $day, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Group them by languages. Chris@76: $birthdays = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($result)) Chris@76: { Chris@76: if (!isset($birthdays[$row['lngfile']])) Chris@76: $birthdays[$row['lngfile']] = array(); Chris@76: $birthdays[$row['lngfile']][$row['id_member']] = array( Chris@76: 'name' => $row['real_name'], Chris@76: 'email' => $row['email_address'] Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: // Send out the greetings! Chris@76: foreach ($birthdays as $lang => $recps) Chris@76: { Chris@76: // We need to do some shuffling to make this work properly. Chris@76: loadLanguage('EmailTemplates', $lang); Chris@76: $txt['emails']['happy_birthday'] = $birthdayEmails[$greeting]; Chris@76: Chris@76: foreach ($recps as $recp) Chris@76: { Chris@76: $replacements = array( Chris@76: 'REALNAME' => $recp['name'], Chris@76: ); Chris@76: Chris@76: $emaildata = loadEmailTemplate('happy_birthday', $replacements, $lang, false); Chris@76: Chris@76: sendmail($recp['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 4); Chris@76: Chris@76: // Try to stop a timeout, this would be bad... Chris@76: @set_time_limit(300); Chris@76: if (function_exists('apache_reset_timeout')) Chris@76: @apache_reset_timeout(); Chris@76: Chris@76: } Chris@76: } Chris@76: Chris@76: // Flush the mail queue, just in case. Chris@76: AddMailQueue(true); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: function scheduled_weekly_maintenance() Chris@76: { Chris@76: global $modSettings, $smcFunc; Chris@76: Chris@76: // Delete some settings that needn't be set if they are otherwise empty. Chris@76: $emptySettings = array( Chris@76: 'warning_mute', 'warning_moderate', 'warning_watch', 'warning_show', 'disableCustomPerPage', 'spider_mode', 'spider_group', Chris@76: 'paid_currency_code', 'paid_currency_symbol', 'paid_email_to', 'paid_email', 'paid_enabled', 'paypal_email', Chris@76: 'search_enable_captcha', 'search_floodcontrol_time', 'show_spider_online', Chris@76: ); Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}settings Chris@76: WHERE variable IN ({array_string:setting_list}) Chris@76: AND (value = {string:zero_value} OR value = {string:blank_value})', Chris@76: array( Chris@76: 'zero_value' => '0', Chris@76: 'blank_value' => '', Chris@76: 'setting_list' => $emptySettings, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Some settings we never want to keep - they are just there for temporary purposes. Chris@76: $deleteAnywaySettings = array( Chris@76: 'attachment_full_notified', Chris@76: ); Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}settings Chris@76: WHERE variable IN ({array_string:setting_list})', Chris@76: array( Chris@76: 'setting_list' => $deleteAnywaySettings, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Ok should we prune the logs? Chris@76: if (!empty($modSettings['pruningOptions'])) Chris@76: { Chris@76: if (!empty($modSettings['pruningOptions']) && strpos($modSettings['pruningOptions'], ',') !== false) Chris@76: list ($modSettings['pruneErrorLog'], $modSettings['pruneModLog'], $modSettings['pruneBanLog'], $modSettings['pruneReportLog'], $modSettings['pruneScheduledTaskLog'], $modSettings['pruneSpiderHitLog']) = explode(',', $modSettings['pruningOptions']); Chris@76: Chris@76: if (!empty($modSettings['pruneErrorLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneErrorLog'] * 86400; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_errors Chris@76: WHERE log_time < {int:log_time}', Chris@76: array( Chris@76: 'log_time' => $t, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: if (!empty($modSettings['pruneModLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneModLog'] * 86400; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_actions Chris@76: WHERE log_time < {int:log_time} Chris@76: AND id_log = {int:moderation_log}', Chris@76: array( Chris@76: 'log_time' => $t, Chris@76: 'moderation_log' => 1, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: if (!empty($modSettings['pruneBanLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneBanLog'] * 86400; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_banned Chris@76: WHERE log_time < {int:log_time}', Chris@76: array( Chris@76: 'log_time' => $t, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: if (!empty($modSettings['pruneReportLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneReportLog'] * 86400; Chris@76: Chris@76: // This one is more complex then the other logs. First we need to figure out which reports are too old. Chris@76: $reports = array(); Chris@76: $result = $smcFunc['db_query']('', ' Chris@76: SELECT id_report Chris@76: FROM {db_prefix}log_reported Chris@76: WHERE time_started < {int:time_started}', Chris@76: array( Chris@76: 'time_started' => $t, Chris@76: ) Chris@76: ); Chris@76: Chris@76: while ($row = $smcFunc['db_fetch_row']($result)) Chris@76: $reports[] = $row[0]; Chris@76: Chris@76: $smcFunc['db_free_result']($result); Chris@76: Chris@76: if (!empty($reports)) Chris@76: { Chris@76: // Now delete the reports... Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_reported Chris@76: WHERE id_report IN ({array_int:report_list})', Chris@76: array( Chris@76: 'report_list' => $reports, Chris@76: ) Chris@76: ); Chris@76: // And delete the comments for those reports... Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_reported_comments Chris@76: WHERE id_report IN ({array_int:report_list})', Chris@76: array( Chris@76: 'report_list' => $reports, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: if (!empty($modSettings['pruneScheduledTaskLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneScheduledTaskLog'] * 86400; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_scheduled_tasks Chris@76: WHERE time_run < {int:time_run}', Chris@76: array( Chris@76: 'time_run' => $t, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: Chris@76: if (!empty($modSettings['pruneSpiderHitLog'])) Chris@76: { Chris@76: // Figure out when our cutoff time is. 1 day = 86400 seconds. Chris@76: $t = time() - $modSettings['pruneSpiderHitLog'] * 86400; Chris@76: Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_spider_hits Chris@76: WHERE log_time < {int:log_time}', Chris@76: array( Chris@76: 'log_time' => $t, Chris@76: ) Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: // Get rid of any paid subscriptions that were never actioned. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}log_subscribed Chris@76: WHERE end_time = {int:no_end_time} Chris@76: AND status = {int:not_active} Chris@76: AND start_time < {int:start_time} Chris@76: AND payments_pending < {int:payments_pending}', Chris@76: array( Chris@76: 'no_end_time' => 0, Chris@76: 'not_active' => 0, Chris@76: 'start_time' => time() - 60, Chris@76: 'payments_pending' => 1, Chris@76: ) Chris@76: ); Chris@76: Chris@76: // Some OS's don't seem to clean out their sessions. Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}sessions Chris@76: WHERE last_update < {int:last_update}', Chris@76: array( Chris@76: 'last_update' => time() - 86400, Chris@76: ) Chris@76: ); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Perform the standard checks on expiring/near expiring subscriptions. Chris@76: function scheduled_paid_subscriptions() Chris@76: { Chris@76: global $txt, $sourcedir, $scripturl, $smcFunc, $modSettings, $language; Chris@76: Chris@76: // Start off by checking for removed subscriptions. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_subscribe, id_member Chris@76: FROM {db_prefix}log_subscribed Chris@76: WHERE status = {int:is_active} Chris@76: AND end_time < {int:time_now}', Chris@76: array( Chris@76: 'is_active' => 1, Chris@76: 'time_now' => time(), Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: require_once($sourcedir . '/ManagePaid.php'); Chris@76: removeSubscription($row['id_subscribe'], $row['id_member']); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Get all those about to expire that have not had a reminder sent. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT ls.id_sublog, m.id_member, m.member_name, m.email_address, m.lngfile, s.name, ls.end_time Chris@76: FROM {db_prefix}log_subscribed AS ls Chris@76: INNER JOIN {db_prefix}subscriptions AS s ON (s.id_subscribe = ls.id_subscribe) Chris@76: INNER JOIN {db_prefix}members AS m ON (m.id_member = ls.id_member) Chris@76: WHERE ls.status = {int:is_active} Chris@76: AND ls.reminder_sent = {int:reminder_sent} Chris@76: AND s.reminder > {int:reminder_wanted} Chris@76: AND ls.end_time < ({int:time_now} + s.reminder * 86400)', Chris@76: array( Chris@76: 'is_active' => 1, Chris@76: 'reminder_sent' => 0, Chris@76: 'reminder_wanted' => 0, Chris@76: 'time_now' => time(), Chris@76: ) Chris@76: ); Chris@76: $subs_reminded = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: // If this is the first one load the important bits. Chris@76: if (empty($subs_reminded)) Chris@76: { Chris@76: require_once($sourcedir . '/Subs-Post.php'); Chris@76: // Need the below for loadLanguage to work! Chris@76: loadEssentialThemeData(); Chris@76: } Chris@76: Chris@76: $subs_reminded[] = $row['id_sublog']; Chris@76: Chris@76: $replacements = array( Chris@76: 'PROFILE_LINK' => $scripturl . '?action=profile;area=subscriptions;u=' . $row['id_member'], Chris@76: 'REALNAME' => $row['member_name'], Chris@76: 'SUBSCRIPTION' => $row['name'], Chris@76: 'END_DATE' => strip_tags(timeformat($row['end_time'])), Chris@76: ); Chris@76: Chris@76: $emaildata = loadEmailTemplate('paid_subscription_reminder', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']); Chris@76: Chris@76: // Send the actual email. Chris@76: sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 2); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: // Mark the reminder as sent. Chris@76: if (!empty($subs_reminded)) Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}log_subscribed Chris@76: SET reminder_sent = {int:reminder_sent} Chris@76: WHERE id_sublog IN ({array_int:subscription_list})', Chris@76: array( Chris@76: 'subscription_list' => $subs_reminded, Chris@76: 'reminder_sent' => 1, Chris@76: ) Chris@76: ); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: ?>