4 date_default_timezone_set('Africa/Johannesburg');
5 chdir(dirname(__DIR__));
7 #-> Environmental awareness.
11 $uname = php_uname("n");
12 $appName = 'Bid4Cars';
13 if ('Production.localdomain' == $uname
14 || 'VodacomStage' == $uname
18 $appName = 'Bid4Cars (stage)';
20 if ('nrad.local' == $uname)
23 $appName = 'Bid4Cars (dev)';
25 define('APP_KEY', 'namibia');
26 define('IS_DEV_ENV', $isDev);
27 define('IS_STAGE_ENV', $isStage);
28 define('IS_LIVE_ENV', $isLive);
29 define('IS_BROCHURE', false);
31 #-> Vendor auto-loading.
32 require 'init_autoloader.php';
34 #-> Selective service inclusion.
35 require 'module/Utility/src/Utility/Debug.php';
36 require 'module/Utility/src/Utility/Registry.php';
37 require 'module/Utility/src/Utility/FileStore.php';
38 require 'module/Utility/src/Utility/Comms/Email.php';
39 require 'module/Utility/src/Utility/Comms/Sms.php';
43 #-> Offline notification handler consuming from gearman queue.
52 * @var \Utility\Comms\Email
54 static protected $emailService;
56 * @var \Utility\Comms\Sms
58 static protected $smsService;
62 static protected $instance;
66 static protected $host;
70 static protected $cashCarsHost;
74 static protected $sourceEmailAddress;
78 static protected $smsSourceAddress;
82 * Setup database connection and notification services.
84 static public function init()
87 self::$emailService = new \Utility\Comms\Email();
88 self::$smsService = new \Utility\Comms\Sms();
89 self::$instance = \Utility\Registry::getConfigParam('Instance');
90 self::$host = \Utility\Registry::getConfigParam('Host');
91 self::$cashCarsHost = \Utility\Registry::getConfigParam('CashCarsHost');
93 $result = self::$db->query("SELECT * FROM app_config");
96 echo 'Notifier configuration error: ' . self::$db->error . "\n";
97 error_log('Notifier configuration error: ' . self::$db->error);
100 $row = $result->fetch_assoc();
101 self::$sourceEmailAddress = $row['source_email_address'];
102 self::$smsSourceAddress = $row['source_mobile_address'];
106 * Connect to database.
108 static protected function connect()
110 $global = include('config/autoload/global.php');
111 $global = $global['doctrine']['connection']['orm_default']['params'];
112 $local = include('config/autoload/local.php');
113 $local = $local['doctrine']['connection']['orm_default']['params'];
114 $config = array_merge($global, $local);
115 self::$db = new mysqli(
121 if (self::$db->connect_errno)
123 echo 'Notifier DB connection error: ' . self::$db->connect_error . "\n";
124 error_log('Notifier DB connection error: ' . self::$db->connect_error);
131 * Send a notification.
132 * @param GearmanJob $job
135 static public function notify(GearmanJob $job)
137 #-> Ping the db connection.
138 $id = $job->workload();
139 if (!self::$db->ping())
144 #-> Get data to work with.
145 $data = \Utility\FileStore::fetchJson($id);
147 #-> Send the email off, into the big wide world, with a message of hope, or something.
150 if ($data['emailTemplate'] && $data['email'])
152 foreach ($data['attachments'] as $key => $value)
154 $data['attachments'][$key] = utf8_decode($value);
156 foreach ($data['complexAttachments'] as $key => $value)
158 $data['complexAttachments'][$key] = utf8_decode($value);
161 // \Utility\Debug::errorLog('notify IS_BROCHURE', IS_BROCHURE ? 'true' : 'false');
163 // $data['emailTemplate'] = str_replace('{APP_HOST}', self::$host, $data['emailTemplate']);
164 $data['emailTemplate'] = str_replace('{APP_HOST}', !IS_BROCHURE ? self::$host : self::$cashCarsHost, $data['emailTemplate']);
165 $mailer = new \Utility\Comms\Email();
168 'From' => self::$sourceEmailAddress,
169 'To' => $data['email'],
170 'Subject' => $data['subject'],
171 'Html' => $data['emailTemplate'],
172 'Attachment' => $data['attachments'],
173 'ComplexAttachment' => $data['complexAttachments']
178 catch (\Exception $e)
180 error_log('Notifier Email EXCEPTION: ' . "$e");
183 #-> Send the sms hurtling through cyberspace at insane speeds.
187 \Utility\Debug::errorLog("trying","sms");
188 if (!$data['disableSms'] && $data['smsTemplate'] && $data['mobile'])
190 if (IS_STAGE_ENV || 'production' == self::$instance)
194 $data['mobile'] = '+27722208069';
196 $sms = new \Utility\Comms\Sms();
197 $apiMsgId = $sms->send(array(
198 'To' => $data['mobile'],
199 'From' => self::$smsSourceAddress,
200 'Subject' => 'Bid4Cars: ',
201 'Body' => $data['smsTemplate']
203 $apiMsgId = (false == $apiMsgId)
209 catch (\Exception $e)
211 error_log('Notifier SMS EXCEPTION: ' . $e->getMessage());
214 #-> Log notification entry.
216 'from_company_id' => !is_null($data['fromCompanyId'])
217 ? (int)$data['fromCompanyId']
219 'from_profile_id' => !is_null($data['fromProfileId'])
220 ? (int)$data['fromProfileId']
222 'to_company_id' => !is_null($data['toCompanyId'])
223 ? (int)$data['toCompanyId']
225 'to_profile_id' => !is_null($data['toProfileId'])
226 ? (int)$data['toProfileId']
228 'email_to' => !is_null($data['email'])
229 ? '"' . self::$db->escape_string($data['email']) . '"'
231 'email_subject' => !is_null($data['subject'])
232 ? '"' . self::$db->escape_string($data['subject']) . '"'
234 'email_body' => !is_null($data['emailTemplate'])
235 ? '"' . self::$db->escape_string($data['emailTemplate']) . '"'
237 'sms_to' => !is_null($data['mobile'])
238 ? '"' . self::$db->escape_string($data['mobile']) . '"'
240 'sms_body' => !is_null($data['smsTemplate'])
241 ? '"' . self::$db->escape_string($data['smsTemplate']) . '"'
243 'api_msg_id' => '"' . self::$db->escape_string($apiMsgId) . '"',
244 'sms_status' => 'null',
245 'created' => '"' . date('Y-m-d H:i:s') . '"',
249 $query = 'INSERT INTO lib_notification_log '
250 . '(from_company_id,from_profile_id,to_company_id,to_profile_id,'
251 . 'email_to,email_subject,email_body,'
252 . 'sms_to,sms_body,api_msg_id,sms_status,'
253 . 'created,updated,archived)'
255 . '(' . implode(',', $tableData) . ')';
256 $result = self::$db->query($query);
257 if (!$result || self::$db->errno)
259 echo 'Notifier DB query error: ' . self::$db->error . "\n";
260 error_log('Notifier DB query error: ' . self::$db->error);
265 \Utility\FileStore::deleteJson($id);
267 #-> Give the server a breather.
276 #-> Get up and running.
279 #-> Register as a gearman worker.
280 $worker = new GearmanWorker();
281 $worker->addServer();
282 $worker->addFunction('Notify', array('Notifier', 'notify'));
284 #-> Wait for jobs to come in.
285 while ($worker->work())