Skip to content

Instantly share code, notes, and snippets.

@shawn-crigger
Created October 27, 2020 19:42
Show Gist options
  • Select an option

  • Save shawn-crigger/a9fca97092b08d9be856897d9545173e to your computer and use it in GitHub Desktop.

Select an option

Save shawn-crigger/a9fca97092b08d9be856897d9545173e to your computer and use it in GitHub Desktop.
set expires
<?php
// vim: set et ts=3 sts=3 sw=3 fdm=marker:
// ------------------------------------------------------------
// Author: Shawn Crigger (ithippyshawn@gmail.com)
// File: setexpires.cron.php
// Created: 10:00:00 AM EDT Mon, June 15, 2020
// ------------------------------------------------------------
$debug_mode = true; // WILL STIL CHARGE - Should be verbose mode
$debug_email = true; // Set to true to send all emails to my email
$email_out = true; // Set to true to send output via email
$debug_fbo = 1161;
if (php_sapi_name() != 'cli') {
exit;
}
function emailOutput() {
global $email_out;
return $email_out;
}
function isDebugMode($email = false) {
global $debug_email, $debug_mode;
if ($email) {
return (bool) $debug_email;
}
return (bool) $debug_mode;
}
function isDebugFBO() {
global $debug_fbo;
return $debug_fbo;
}
if (emailOutput()) {
//ob_start();
}
error_reporting(E_ALL);
require_once('/var/www/jb-virts/redbird.aero/v1/lib/helper/helpers.php');
require_once('/var/www/jb-virts/redbird.aero/v1/lib/local.inc.php');
require_once('/var/www/jb-virts/redbird.aero/v1/lib/Carbon.php');
use Carbon\Carbon;
$ETnow = Carbon::now('America/New_York');
// {{{ configuration details
// Template engine
require_once($site_lib_dir . 'Smarty-3.1.8/libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->loadPlugin('smarty_compiler_switch');
$smarty->setTemplateDir($site_base_dir . 'templates/');
$smarty->setCompileDir($site_base_dir . 'templates_c/');
$smarty->debugging = 0;
// Make these globals smarty variables
$smarty->assign('site_base_dir', $site_base_dir);
$smarty->assign('site_lib_dir', $site_lib_dir);
$smarty->assign('http_domain', $http_domain);
// PEAR for error handling (temporary)
require('PEAR.php');
class RB_Exception extends Exception { }
require_once($site_lib_dir . 'meekrodb.2.0.class.php');
DB::$host = $db_host;
DB::$user = $db_user;
DB::$password = $db_pass;
DB::$dbName = $db_db;
DB::$error_handler = false; // since we're catching errors, don't need error handler
DB::$throw_exception_on_error = true;
DB::$param_char = ':';
DB::debugMode('queryDebugHandler');
// Authentication/Identity
require_once($site_lib_dir . 'class-phpass.php');
$hasher = new PasswordHash(8, false);
// Mail
require_once($site_lib_dir . 'mail.class.php');
$mailer = new RB_Mail(null);
// }}}
// {{{ randomID
function randomID() {
return sha1(microtime(true).mt_rand(10000,90000));
}
// }}}
$email_message = "";
function sendEmail($email_message) {
$mailfrom = "Flight Circle Server <josh@flightcircle.com>";
$mailto = "josh@digitaloffice.com, ithippyshawn@gmail.com";
$mailto = "ithippyshawn@gmail.com";
$message = '<html>
<head>
<style>
hr {
display: block;
margin-top: 0.5em;
margin-bottom: 0.5em;
margin-left: auto;
margin-right: auto;
border-style: inset;
border-width: 1px;
}
</style>
</head>
<body>
';
$message .= $email_message;
$message .= "\n</body></html>\n";
$headers = "From: " . $mailfrom . "\r\n";
$headers .= "Reply-To: " . $mailfrom . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$subject = "Set Expires CRON: " . date("l - F j, Y");
mail($mailto, $subject, $message, $headers);
}
function getFieldTypeID() {
return "2";
}
function setExpired() {
// set field value to expired.
$where = new WhereClause('and');
$where->add('f.FieldTypeID = :s', getFieldTypeID());
$where->add('c.expired = :s', '0');
$where->add('f.expires = :s', '1');
if (isDebugMode() && isDebugFBO()) {
$where->add('f.FboID = :s',isDebugFBO());
}
$where->add('NOW() <= DATE_FORMAT(STR_TO_DATE(c.field_value, :s), :s)', "%m-%d-%Y", "%Y-%m-%d %H:%i:00");
$update = array('expired' => 2);
$expired = DB::query("SELECT c.field_value,
c.ID,
c.UserID,
c.CustomerID,
c.FboID
FROM customercustomfields c
LEFT JOIN fboscustomfields f ON c.FieldID = f.ID
WHERE :l", $where->text());
foreach ($expired as $row) {
if (isDebugMode() || emailOutput()) {
echo "Setting CustomerID:{$row['CustomerID']} FieldID:{$row['ID']} to EXPIRED\n";
}
DB::update('customercustomfields', $update, 'ID = :s AND CustomerID = :s AND UserID = :s AND FboID = :s', $row['ID'], $row['CustomerID'], $row['UserID'], $row['FboID']);
}
}
function sendNotifications($row) {
global $smarty, $mailer;
$row['fboName'] = $row['known_as'] ?? $row['fboName'];
try {
$row['formatedDate'] = new DateTime('@'.strtotime($row['field_value']));
$row['formatedDate'] = $row['formatedDate']->format('l - F j, Y');
} catch (Exception $e) {
$row['formatedDate'] = $row['field_value'];
}
if (isDebugMode() || emailOutput()) {
echo "Sending notification to CustomerID:{$row['CustomerID']} EMAIL:{$row['email']}\n";
}
//DB::update('customercustomfields', array('expired' => 1), 'ID = :s AND CustomerID = :s AND UserID = :s', $row['FieldID'], $row['CustomerID'], $row['UserID']);
foreach ($row as $key => $value) {
$smarty->assign($key, $value);
}
DB::$param_char = '%';
$mailer->reply_to = $row['replyto'];
$mailer->sendMail(
(isDebugMode(true) ? 'ithippyshawn+debug@gmail.com' : $row['email']),
sprintf('Flight Circle <%s>', $GLOBALS['email_default_sender']),
sprintf('%s - Expiration Warning: %s', $row['fboName'], $row['field_name']),
$row,
'expired.reminder.tpl'
);
DB::$param_char = ':';
}
function setWheres() {
$where = new WhereClause('and');
if (isDebugMode()) {
DB::debugMode('queryDebugHandler');
if (isDebugFBO() > 0) {
$where->add('f.FboID = :s',isDebugFBO());
}
}
$where->add('f.FieldTypeID = :s', getFieldTypeID());
//$where->add('c.field_value != %s', '');
$where->add('c.expired = :s', '0');
$where->add('f.expires = :s', '1');
return $where;
}
$dateWhere = "AND (DATE_FORMAT(STR_TO_DATE(c.field_value, '%m-%d-%Y'), '%Y-%m-%d %H:%i:00') <= DATE_ADD(NOW(), INTERVAL +1 MONTH))";
$records = DB::query("SELECT
u.email,
ct.first_name,
CONCAT(ct.first_name,' ', ct.last_name) AS full_name,
f.field_name,
c.field_value,
NOW(),
c.expired,
c.ID as FieldID,
f.ID as TypeID,
c.FboID,
c.CustomerID,
c.UserID,
fbo.name AS fboName,
fbo.phone,
fbo.known_as,
fbo.replyto
FROM customercustomfields c
LEFT JOIN fboscustomfields f ON c.FieldID = f.ID
LEFT JOIN fbos fbo ON fbo.ID = f.FboID
LEFT JOIN users u ON u.ID = c.UserID
LEFT JOIN customers ct ON ct.ID = c.CustomerID WHERE :l {$dateWhere}", setWheres()->text());
foreach ($records as $row) {
sendNotifications($row);
}
setExpired();
if (emailOutput()) {
$email_message = ob_get_clean();
if (!empty($email_message)) {
$now = date("l - F j, Y @ h:i:s a");
$email_message = str_replace(array("\r","\n"),PHP_EOL, $email_message);
$email_message = "set-expires.cron ran successfully at {$now}<br>" . $email_message;
sendEmail($email_message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment