Root ourselves. date_default_timezone_set('Africa/Johannesburg'); chdir(dirname(__DIR__)); #-> Environmental awareness. $isLive = true; $isStage = false; $isDev = false; $uname = php_uname("n"); $appName = 'Bid4Cars'; if ('Production.localdomain' == $uname || 'VodacomStage' == $uname ) { $isStage = true; $appName = 'Bid4Cars (stage)'; } if ('nrad.local' == $uname) { $isDev = true; $appName = 'Bid4Cars (dev)'; } define('APP_KEY', 'namibia'); define('IS_DEV_ENV', $isDev); define('IS_STAGE_ENV', $isStage); define('IS_LIVE_ENV', $isLive); define('IS_BROCHURE', false); #-> Vendor auto-loading. require 'init_autoloader.php'; #-> Selective service inclusion. require 'module/Utility/src/Utility/Debug.php'; require 'module/Utility/src/Utility/Registry.php'; require 'module/Utility/src/Utility/FileStore.php'; require 'module/Utility/src/Utility/Comms/Email.php'; require 'module/Utility/src/Utility/Comms/Sms.php'; #-> Offline notification handler consuming from gearman queue. class Notifier { /** * @var \mysqli */ static protected $db; /** * @var \Utility\Comms\Email */ static protected $emailService; /** * @var \Utility\Comms\Sms */ static protected $smsService; /** * @var string */ static protected $instance; /** * @var string */ static protected $host; /** * @var string */ static protected $cashCarsHost; /** * @var string */ static protected $sourceEmailAddress; /** * @var string */ static protected $smsSourceAddress; /** * Setup database connection and notification services. */ static public function init() { self::connect(); self::$emailService = new \Utility\Comms\Email(); self::$smsService = new \Utility\Comms\Sms(); self::$instance = \Utility\Registry::getConfigParam('Instance'); self::$host = \Utility\Registry::getConfigParam('Host'); self::$cashCarsHost = \Utility\Registry::getConfigParam('CashCarsHost'); $result = self::$db->query("SELECT * FROM app_config"); if (!$result) { echo 'Notifier configuration error: ' . self::$db->error . "\n"; error_log('Notifier configuration error: ' . self::$db->error); exit(); } $row = $result->fetch_assoc(); self::$sourceEmailAddress = $row['source_email_address']; self::$smsSourceAddress = $row['source_mobile_address']; } /** * Connect to database. */ static protected function connect() { $global = include('config/autoload/global.php'); $global = $global['doctrine']['connection']['orm_default']['params']; $local = include('config/autoload/local.php'); $local = $local['doctrine']['connection']['orm_default']['params']; $config = array_merge($global, $local); self::$db = new mysqli( $config['host'], $config['user'], $config['password'], $config['dbname'] ); if (self::$db->connect_errno) { echo 'Notifier DB connection error: ' . self::$db->connect_error . "\n"; error_log('Notifier DB connection error: ' . self::$db->connect_error); exit(); } } /** * Send a notification. * @param GearmanJob $job * @return null */ static public function notify(GearmanJob $job) { #-> Ping the db connection. $id = $job->workload(); if (!self::$db->ping()) { self::connect(); } #-> Get data to work with. $data = \Utility\FileStore::fetchJson($id); #-> Send the email off, into the big wide world, with a message of hope, or something. try { if ($data['emailTemplate'] && $data['email']) { foreach ($data['attachments'] as $key => $value) { $data['attachments'][$key] = utf8_decode($value); } foreach ($data['complexAttachments'] as $key => $value) { $data['complexAttachments'][$key] = utf8_decode($value); } // \Utility\Debug::errorLog('notify IS_BROCHURE', IS_BROCHURE ? 'true' : 'false'); // $data['emailTemplate'] = str_replace('{APP_HOST}', self::$host, $data['emailTemplate']); $data['emailTemplate'] = str_replace('{APP_HOST}', !IS_BROCHURE ? self::$host : self::$cashCarsHost, $data['emailTemplate']); $mailer = new \Utility\Comms\Email(); $mailer->send( array( 'From' => self::$sourceEmailAddress, 'To' => $data['email'], 'Subject' => $data['subject'], 'Html' => $data['emailTemplate'], 'Attachment' => $data['attachments'], 'ComplexAttachment' => $data['complexAttachments'] ) ); } } catch (\Exception $e) { error_log('Notifier Email EXCEPTION: ' . "$e"); } #-> Send the sms hurtling through cyberspace at insane speeds. $apiMsgId = ''; try { \Utility\Debug::errorLog("trying","sms"); if (!$data['disableSms'] && $data['smsTemplate'] && $data['mobile']) { if (IS_STAGE_ENV || 'production' == self::$instance) { if (IS_STAGE_ENV) { $data['mobile'] = '+27722208069'; } $sms = new \Utility\Comms\Sms(); $apiMsgId = $sms->send(array( 'To' => $data['mobile'], 'From' => self::$smsSourceAddress, 'Subject' => 'Bid4Cars: ', 'Body' => $data['smsTemplate'] )); $apiMsgId = (false == $apiMsgId) ? '' : $apiMsgId; } } } catch (\Exception $e) { error_log('Notifier SMS EXCEPTION: ' . $e->getMessage()); } #-> Log notification entry. $tableData = array( 'from_company_id' => !is_null($data['fromCompanyId']) ? (int)$data['fromCompanyId'] : 'null', 'from_profile_id' => !is_null($data['fromProfileId']) ? (int)$data['fromProfileId'] : 'null', 'to_company_id' => !is_null($data['toCompanyId']) ? (int)$data['toCompanyId'] : 'null', 'to_profile_id' => !is_null($data['toProfileId']) ? (int)$data['toProfileId'] : 'null', 'email_to' => !is_null($data['email']) ? '"' . self::$db->escape_string($data['email']) . '"' : 'null', 'email_subject' => !is_null($data['subject']) ? '"' . self::$db->escape_string($data['subject']) . '"' : 'null', 'email_body' => !is_null($data['emailTemplate']) ? '"' . self::$db->escape_string($data['emailTemplate']) . '"' : '""', 'sms_to' => !is_null($data['mobile']) ? '"' . self::$db->escape_string($data['mobile']) . '"' : 'null', 'sms_body' => !is_null($data['smsTemplate']) ? '"' . self::$db->escape_string($data['smsTemplate']) . '"' : '""', 'api_msg_id' => '"' . self::$db->escape_string($apiMsgId) . '"', 'sms_status' => 'null', 'created' => '"' . date('Y-m-d H:i:s') . '"', 'updated' => 'null', 'archived' => 0 ); $query = 'INSERT INTO lib_notification_log ' . '(from_company_id,from_profile_id,to_company_id,to_profile_id,' . 'email_to,email_subject,email_body,' . 'sms_to,sms_body,api_msg_id,sms_status,' . 'created,updated,archived)' . ' VALUES ' . '(' . implode(',', $tableData) . ')'; $result = self::$db->query($query); if (!$result || self::$db->errno) { echo 'Notifier DB query error: ' . self::$db->error . "\n"; error_log('Notifier DB query error: ' . self::$db->error); exit(); } #-> Cleanup. \Utility\FileStore::deleteJson($id); #-> Give the server a breather. sleep(2); } } #-> Get up and running. Notifier::init(); #-> Register as a gearman worker. $worker = new GearmanWorker(); $worker->addServer(); $worker->addFunction('Notify', array('Notifier', 'notify')); #-> Wait for jobs to come in. while ($worker->work()) { ; }