"; $warning_subject = "Koumbit: Your account has exceeded its monthly bandwidth quota"; $url_info = "http://www.koumbit.org/hosting/overquota"; $template_directory = "/usr/share/alternc/templates"; // if this is set, all mails will be sent to this address: $simulate = "mlutfy@koumbit.org"; // Fetch a template with the text to send to the user // the template is a PHP file with a function having the same name. // EX: file warning_overquota_bandwidth.php, function warning_overquota_bandwidth($tokens). // to provide a custom message, create a file with the same filename + '.custom' at the end, // EX: file warning_overquota_bandwidth.php.custom, function warning_overquota_bandwidth($tokens). function get_text_from_template($template, $tokens) { global $template_directory; $t = $template_directory . "/" . $template . '.php'; if (file_exists($t . '.custom')) { $t = $t . '.custom'; } if (file_exists($t)) { include_once($t); if (function_exists($template)) { return $template($tokens); } else { die("$t: missing function $template"); } } die("Could not find template: $template"); } // Taken from Drupal 6, except that we do not accept empty addresses // http://api.drupal.org/api/function/valid_email_address/6 function is_valid_email_address($mail) { if (! $mail) return false; $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+'; $domain = '(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.?)+'; $ipv4 = '[0-9]{1,3}(\.[0-9]{1,3}){3}'; $ipv6 = '[0-9a-fA-F]{1,4}(\:[0-9a-fA-F]{1,4}){7}'; return preg_match("/^$user@($domain|(\[($ipv4|$ipv6)\]))$/", $mail); } function send_email($mail_to, $subject, $template, $tokens) { global $accountancy_mail; global $accountancy_from; global $simulate; if (! is_valid_email_address($mail_to)) { return FALSE; } if ($simulate) { $mail_to = $simulate; } $headers = "From: $accountancy_from\n". "MIME-Version: 1.0\n". "Content-Type: text/plain; charset=utf-8\n". "Content-Transfer-Encoding: 8bit\n"; // encode the subjet if possible if(function_exists('mb_encode_mimeheader') AND @mb_internal_encoding('utf-8')) { $subject = mb_encode_mimeheader($subject, 'utf-8', 'Q'); } // fetch the template "hi there, you're over quota" $text = get_text_from_template($template, $tokens); return mail($mail_to, $subject, $text, $headers); } function main() { global $err, $db, $cuid; global $warning_subject, $accountancy_mail, $url_info; $hosting_tld = variable_get('hosting_tld'); $month = 0; $q = "SELECT h.uid, m.login, m.mail, sum(h.size) as webusage, q.total as quota, YEAR(NOW()) as year, MONTH(NOW()) as month FROM stat_http as h LEFT JOIN quotas as q on (q.uid = h.uid and q.name = 'bw_web') LEFT JOIN membres as m on (m.uid = h.uid) WHERE YEAR(day) = YEAR(NOW()) AND MONTH(day) = MONTH(NOW()) GROUP BY h.uid HAVING webusage > total"; $db->query($q); while($db->next_record()) { $warning_already_sent = false; $login = $db->f("login"); $mail_to = $db->f("mail"); $uid = $db->f("uid"); $tokens = array( 'uid' => $db->f("uid"), 'login' => $db->f("login"), 'mail' => $db->f("mail"), 'webusage' => format_size($db->f("webusage")), 'quota' => format_size($db->f("quota")), 'contactmail' => $accountancy_mail, 'url_info' => $url_info, 'hosting_tld' => $hosting_tld, 'year' => $db->f("year"), 'month' => $db->f("month"), 'domains' => array(), ); // echo $db->f("uid") . ': ' . $db->f("webusage") . ' / ' . $db->f("total") . ' -- ' . $db->f("mail") . "\n"; // Check if a warning was already sent. For now, we send only // one warning, and it's up to people to monitor their stats so // that they do not go too much over-quota // // The logic is silly: if the user was over-quota yesterday, then // assume that a warning was sent. $q = "SELECT sum(h.size) as webusage FROM stat_http as h WHERE YEAR(day) = YEAR(NOW()) AND MONTH(day) = MONTH(NOW()) AND DAY(day) < DAY(NOW()) AND h.uid = $uid"; $db->query($q); while ($db->next_record()) { if ($db->f('webusage') > $tokens['quota']) { $warning_already_sent = true; } } if ($warning_already_sent) continue; // Get the list of domains and their bandwidth usage $q = "SELECT h.domain, sum(h.size) as webusage FROM stat_http as h WHERE YEAR(day) = YEAR(NOW()) AND MONTH(day) = MONTH(NOW()) AND h.uid = $uid GROUP BY h.domain"; $db->query($q); while($db->next_record()) { $domain = $db->f('domain'); $webusage = format_size($db->f('webusage')); $tokens['domains'][$domain] = $webusage; } // Send the e-mail to the user if (is_valid_email_address($mail_to)) { $result = send_email($mail_to, $warning_subject, 'warning_overquota_bandwidth', $tokens); if ($result !== TRUE) { echo "$uid: failed to send e-mail to $mail_to\n"; } } else { echo "Account $uid ($login) has an invalid e-mail\n"; } die("test"); } } main();