| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | #====================================================== |
|---|
| 5 | | <#TAG_NAME_TAG#> |
|---|
| 6 | | ===================================== |
|---|
| 7 | | <#TAG_DEVELOPERS_TAG#> |
|---|
| 8 | | <#TAG_COPYRIGHT_TAG#> |
|---|
| 9 | | <#TAG_URL_TAG#> |
|---|
| 10 | | ===================================== |
|---|
| 11 | | Email: <#TAG_EMAIL_TAG#> |
|---|
| 12 | #====================================================== |
|---|
| 13 | | @ Version: <#TAG_VER_HUMAN_TAG#> |
|---|
| 14 | | @ Version Int: <#TAG_VER_INT_TAG#> |
|---|
| 15 | | @ Version Num: <#TAG_VER_NUM_TAG#> |
|---|
| 16 | | @ Build: <#TAG_VER_BUILD_TAG#> |
|---|
| 17 | #====================================================== |
|---|
| 18 | | | Classes :: Email |
|---|
| 19 | #====================================================== |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | class td_class_email { |
|---|
| 23 | |
|---|
| 24 | private $transport; |
|---|
| 25 | private $mailer; |
|---|
| 26 | private $message; |
|---|
| 27 | public $failures; |
|---|
| 28 | private $swift_exception; |
|---|
| 29 | private $config = array(); |
|---|
| 30 | private $to_send = array(); |
|---|
| 31 | private $to_send_id = 0; |
|---|
| 32 | |
|---|
| 33 | #======================================= |
|---|
| 34 | # @ Constructor |
|---|
| 35 | #======================================= |
|---|
| 36 | |
|---|
| 37 | function __construct($trellis, $config=array()) |
|---|
| 38 | { |
|---|
| 39 | $this->trellis = &$trellis; |
|---|
| 40 | |
|---|
| 41 | #============================= |
|---|
| 42 | # Update Configuration |
|---|
| 43 | #============================= |
|---|
| 44 | |
|---|
| 45 | if ( ! empty( $config ) ) $this->update_config( $config ); |
|---|
| 46 | |
|---|
| 47 | #============================= |
|---|
| 48 | # Let's Get Swift! |
|---|
| 49 | #============================= |
|---|
| 50 | |
|---|
| 51 | require_once HD_INC .'swift/swift_required.php'; |
|---|
| 52 | |
|---|
| 53 | #============================= |
|---|
| 54 | # Connect to Transport |
|---|
| 55 | #============================= |
|---|
| 56 | |
|---|
| 57 | if ( $this->config['transport'] == 'smtp' ) |
|---|
| 58 | { |
|---|
| 59 | $this->transport = Swift_SmtpTransport::newInstance( $this->config['smtp_host'] ); |
|---|
| 60 | |
|---|
| 61 | if ( $this->config['smtp_port'] ) $this->transport->setPort( $this->config['smtp_port'] ); |
|---|
| 62 | |
|---|
| 63 | if ( $this->config['smtp_encryption'] ) $this->transport->setEncryption( $this->config['smtp_encryption'] ); |
|---|
| 64 | |
|---|
| 65 | if ( $this->config['smtp_user'] ) $this->transport->setUsername( $this->config['smtp_user'] ); |
|---|
| 66 | if ( $this->config['smtp_pass'] ) $this->transport->setPassword( $this->config['smtp_pass'] ); |
|---|
| 67 | |
|---|
| 68 | if ( $this->config['smtp_timeout'] ) $this->transport->setTimeout( $this->config['smtp_timeout'] ); |
|---|
| 69 | } |
|---|
| 70 | elseif ( $this->config['transport'] == 'sendmail' ) |
|---|
| 71 | { |
|---|
| 72 | $this->transport = Swift_SendmailTransport::newInstance(); |
|---|
| 73 | |
|---|
| 74 | if ( $this->config['sendmail_command'] ) $this->transport->setCommand( $this->config['sendmail_command'] ); |
|---|
| 75 | } |
|---|
| 76 | elseif ( $this->config['transport'] == 'mail' ) |
|---|
| 77 | { |
|---|
| 78 | $this->transport = Swift_MailTransport::newInstance(); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $this->mailer = Swift_Mailer::newInstance( $this->transport ); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #======================================= |
|---|
| 85 | # @ Update Config |
|---|
| 86 | #======================================= |
|---|
| 87 | |
|---|
| 88 | private function update_config($config) |
|---|
| 89 | { |
|---|
| 90 | if ( ! is_array( $config ) ) trigger_error( "Email - Variable passed to update_config() is not an array", E_USER_WARNING ); |
|---|
| 91 | |
|---|
| 92 | foreach ( $config as $key => $value ) |
|---|
| 93 | { |
|---|
| 94 | $this->config[ $key ] = $value; |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | #======================================= |
|---|
| 99 | # @ Test Transport |
|---|
| 100 | #======================================= |
|---|
| 101 | |
|---|
| 102 | public function test() |
|---|
| 103 | { |
|---|
| 104 | try |
|---|
| 105 | { |
|---|
| 106 | $this->transport->start(); |
|---|
| 107 | } |
|---|
| 108 | catch ( Exception $e ) |
|---|
| 109 | { |
|---|
| 110 | $this->swift_exception = $e->getMessage(); |
|---|
| 111 | |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | if ( ! $this->transport->isStarted() ) return false; |
|---|
| 116 | |
|---|
| 117 | return true; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | #======================================= |
|---|
| 121 | # @ Get Swift Exception |
|---|
| 122 | #======================================= |
|---|
| 123 | |
|---|
| 124 | public function get_exception() |
|---|
| 125 | { |
|---|
| 126 | return $this->swift_exception; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | #======================================= |
|---|
| 130 | # @ Create Message |
|---|
| 131 | #======================================= |
|---|
| 132 | |
|---|
| 133 | private function create_message() |
|---|
| 134 | { |
|---|
| 135 | $this->message = Swift_Message::newInstance(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | #======================================= |
|---|
| 139 | # @ Add To |
|---|
| 140 | #======================================= |
|---|
| 141 | |
|---|
| 142 | private function add_to($addresses, $name=null) |
|---|
| 143 | { |
|---|
| 144 | $this->message->addTo( $addresses, $name ); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | #======================================= |
|---|
| 148 | # @ Add CC |
|---|
| 149 | #======================================= |
|---|
| 150 | |
|---|
| 151 | private function add_cc($addresses, $name=null) |
|---|
| 152 | { |
|---|
| 153 | $this->message->addCc( $addresses, $name ); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | #======================================= |
|---|
| 157 | # @ Add BCC |
|---|
| 158 | #======================================= |
|---|
| 159 | |
|---|
| 160 | private function add_bcc($addresses, $name=null) |
|---|
| 161 | { |
|---|
| 162 | $this->message->addBcc( $addresses, $name ); |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | #======================================= |
|---|
| 166 | # @ Set From |
|---|
| 167 | #======================================= |
|---|
| 168 | |
|---|
| 169 | private function set_from($address, $name=null) |
|---|
| 170 | { |
|---|
| 171 | $this->message->setFrom( $address, $name ); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | #======================================= |
|---|
| 175 | # @ Set Subject |
|---|
| 176 | #======================================= |
|---|
| 177 | |
|---|
| 178 | private function set_subject($subject) |
|---|
| 179 | { |
|---|
| 180 | $this->message->setSubject( $subject ); |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | #======================================= |
|---|
| 184 | # @ Set Body |
|---|
| 185 | #======================================= |
|---|
| 186 | |
|---|
| 187 | private function set_body($body, $type=null) |
|---|
| 188 | { |
|---|
| 189 | $this->message->setBody( $body, $type ); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | #======================================= |
|---|
| 193 | # @ Add Part |
|---|
| 194 | #======================================= |
|---|
| 195 | |
|---|
| 196 | private function add_part($part, $type=null) |
|---|
| 197 | { |
|---|
| 198 | $this->message->addPart( $part, $type ); |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | #======================================= |
|---|
| 202 | # @ Attach |
|---|
| 203 | #======================================= |
|---|
| 204 | |
|---|
| 205 | private function attach($path, $type=null) |
|---|
| 206 | { |
|---|
| 207 | $attachment = Swift_Attachment::fromPath( $path, $type ); |
|---|
| 208 | |
|---|
| 209 | $this->message->attach( $attachment ); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | #======================================= |
|---|
| 213 | # @ Set Replacements |
|---|
| 214 | #======================================= |
|---|
| 215 | |
|---|
| 216 | private function set_replacements($replacements) |
|---|
| 217 | { |
|---|
| 218 | $decorator = new Swift_Plugins_DecoratorPlugin( $replacements ); |
|---|
| 219 | |
|---|
| 220 | $this->mailer->registerPlugin( $decorator ); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | #======================================= |
|---|
| 224 | # @ Send |
|---|
| 225 | #======================================= |
|---|
| 226 | |
|---|
| 227 | private function send() |
|---|
| 228 | { |
|---|
| 229 | return $this->mailer->send( $this->message, $this->failures ); #* handle failures |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | #======================================= |
|---|
| 233 | # @ Send Email |
|---|
| 234 | #======================================= |
|---|
| 235 | |
|---|
| 236 | public function send_email($params=array()) |
|---|
| 237 | { |
|---|
| 238 | if ( ! $this->trellis->cache->data['settings']['email']['enable'] && ! $params['bypass'] ) return false; |
|---|
| 239 | |
|---|
| 240 | #============================= |
|---|
| 241 | # Add Lookup User |
|---|
| 242 | #============================= |
|---|
| 243 | |
|---|
| 244 | if ( ! $params['to'] ) |
|---|
| 245 | { |
|---|
| 246 | trigger_error( "Email - Recipient not specified", E_USER_NOTICE ); |
|---|
| 247 | |
|---|
| 248 | return false; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | if ( ! $params['msg'] ) |
|---|
| 252 | { |
|---|
| 253 | trigger_error( "Email - Message not specified", E_USER_NOTICE ); |
|---|
| 254 | |
|---|
| 255 | return false; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | if ( is_numeric( $params['to'] ) ) |
|---|
| 259 | { |
|---|
| 260 | $this->to_send[ ++$this->to_send_id ] = array( 'id' => $params['to'], 'name' => $params['name'], 'msg' => $params['msg'], 'from' => $params['from'], 'replace' => $params['replace'], 'lang' => $params['lang'], 'preference' => $params['preference'], 'type' => $params['type'] ); |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | { |
|---|
| 264 | $this->to_send[ ++$this->to_send_id ] = array( 'email' => $params['to'], 'name' => $params['name'], 'msg' => $params['msg'], 'from' => $params['from'], 'replace' => $params['replace'], 'lang' => $params['lang'], 'preference' => $params['preference'], 'type' => $params['type'] ); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | return true; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | #======================================= |
|---|
| 271 | # @ Send Email Now |
|---|
| 272 | #======================================= |
|---|
| 273 | |
|---|
| 274 | public function send_email_now($params=array()) |
|---|
| 275 | { |
|---|
| 276 | $this->send_email( $params ); |
|---|
| 277 | |
|---|
| 278 | return $this->send_emails(); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | #======================================= |
|---|
| 282 | # @ User Notification |
|---|
| 283 | #======================================= |
|---|
| 284 | |
|---|
| 285 | public function user_notification($params=array()) |
|---|
| 286 | { |
|---|
| 287 | if ( ! $this->trellis->cache->data['settings']['eunotify']['enable'] ) return false; |
|---|
| 288 | if ( ! $params['id'] && ! $this->trellis->cache->data['settings']['eunotify']['guest'] ) return false; |
|---|
| 289 | |
|---|
| 290 | return $this->send_email( $params ); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | #======================================= |
|---|
| 294 | # @ Staff Notification |
|---|
| 295 | #======================================= |
|---|
| 296 | |
|---|
| 297 | public function staff_notification($params=array()) |
|---|
| 298 | { |
|---|
| 299 | if ( ! $this->trellis->cache->data['settings']['esnotify']['enable'] ) return false; |
|---|
| 300 | |
|---|
| 301 | return $this->send_email( $params ); |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | #======================================= |
|---|
| 305 | # @ Send Emails |
|---|
| 306 | #======================================= |
|---|
| 307 | |
|---|
| 308 | public function send_emails() |
|---|
| 309 | { |
|---|
| 310 | if ( ! is_array( $this->to_send ) ) return false; |
|---|
| 311 | |
|---|
| 312 | $emails = array(); |
|---|
| 313 | $languages = array(); |
|---|
| 314 | |
|---|
| 315 | #============================= |
|---|
| 316 | # Lookup Users |
|---|
| 317 | #============================= |
|---|
| 318 | |
|---|
| 319 | #* |
|---|
| 320 | #* we need to properly check their preference. allow function to take in a paramater that gives the notification type. |
|---|
| 321 | #* then check the notification type against the user preference in the database. also respect email_enable. |
|---|
| 322 | #* |
|---|
| 323 | |
|---|
| 324 | #* |
|---|
| 325 | #* add new notifications types to my acp settings and add / edit use. be sure to include setting warning. |
|---|
| 326 | #* |
|---|
| 327 | |
|---|
| 328 | $lookup_ids = array(); |
|---|
| 329 | |
|---|
| 330 | foreach( $this->to_send as $id => $data ) |
|---|
| 331 | { |
|---|
| 332 | if ( ! $data['email'] && ! in_array( $data['id'], $lookup_ids ) ) $lookup_ids[] = $data['id']; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | if ( ! empty( $lookup_ids ) ) |
|---|
| 336 | { |
|---|
| 337 | $users = array(); |
|---|
| 338 | |
|---|
| 339 | $this->trellis->load_functions('users'); |
|---|
| 340 | |
|---|
| 341 | if ( count( $lookup_ids ) == 1 ) |
|---|
| 342 | { |
|---|
| 343 | if ( ! $u = $this->trellis->func->users->get_single( array( 'id', 'name', 'email', 'lang', 'email_enable', 'email_type' ), array( 'id', '=', $lookup_ids[0] ) ) ) trigger_error( "Email - User not found: ". $lookup_ids[0], E_USER_NOTICE ); |
|---|
| 344 | |
|---|
| 345 | $users[ $u['id'] ] = $u; |
|---|
| 346 | } |
|---|
| 347 | else |
|---|
| 348 | { |
|---|
| 349 | if ( ! $users = $this->trellis->func->users->get( array( 'select' => array( 'id', 'name', 'email', 'lang', 'email_enable', 'email_type' ), 'where' => array( 'id', 'in', $lookup_ids ) ) ) ) trigger_error( "Email - No users found.", E_USER_NOTICE ); |
|---|
| 350 | } |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | #============================= |
|---|
| 354 | # Organize Emails |
|---|
| 355 | #============================= |
|---|
| 356 | |
|---|
| 357 | foreach( $this->to_send as $id => $data ) |
|---|
| 358 | { |
|---|
| 359 | if ( ! $data['lang'] && $data['id'] ) $data['lang'] = $users[ $data['id'] ]['lang']; |
|---|
| 360 | if ( ! $data['type'] && $data['id'] ) $this->to_send[ $id ]['type'] = $users[ $data['id'] ]['email_type']; |
|---|
| 361 | |
|---|
| 362 | if ( ! $data['lang'] || ! $this->trellis->cache->data['langs'][ $data['lang'] ] ) $data['lang'] = $this->trellis->cache->data['misc']['default_lang']; |
|---|
| 363 | |
|---|
| 364 | if ( $this->to_send[ $id ]['type'] == 1 ) |
|---|
| 365 | { |
|---|
| 366 | $this->to_send[ $id ]['type'] = 'html'; |
|---|
| 367 | } |
|---|
| 368 | elseif ( $this->to_send[ $id ]['type'] == 2 ) |
|---|
| 369 | { |
|---|
| 370 | $this->to_send[ $id ]['type'] = 'text'; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | if ( ! $this->to_send[ $id ]['type'] ) $this->to_send[ $id ]['type'] = 'html'; |
|---|
| 374 | |
|---|
| 375 | if ( ! $data['name'] && $data['id'] ) $this->to_send[ $id ]['name'] = $users[ $data['id'] ]['name']; |
|---|
| 376 | if ( ! $data['email'] && $data['id'] ) $this->to_send[ $id ]['email'] = $users[ $data['id'] ]['email']; |
|---|
| 377 | if ( ! $data['preference'] && $data['id'] ) $this->to_send[ $id ]['preference'] = $users[ $data['id'] ]['email_enable']; |
|---|
| 378 | |
|---|
| 379 | #if ( isset( $this->to_send[ $id ]['preference'] ) && $this->to_send[ $id ]['preference'] ) $languages[ $data['lang'] ][] = $id; |
|---|
| 380 | $languages[ $data['lang'] ][] = $id; |
|---|
| 381 | } |
|---|
| 382 | |
|---|
| 383 | #============================= |
|---|
| 384 | # Process Emails |
|---|
| 385 | #============================= |
|---|
| 386 | |
|---|
| 387 | foreach( $languages as $lkey => $emails ) |
|---|
| 388 | { |
|---|
| 389 | if ( ! ( include HD_PATH .'language/'. $lkey .'/lang_email_content.php' ) ) trigger_error( "Email - Unable to load email content language file: ". $lkey, E_USER_WARNING ); |
|---|
| 390 | |
|---|
| 391 | foreach( $emails as $id ) |
|---|
| 392 | { |
|---|
| 393 | $msg = &$this->to_send[ $id ]['msg']; |
|---|
| 394 | |
|---|
| 395 | if ( ! $subject = $lang[ $msg .'_sub' ] ) |
|---|
| 396 | { |
|---|
| 397 | trigger_error( "Email - Language subject missing: ". $msg, E_USER_NOTICE ); |
|---|
| 398 | |
|---|
| 399 | continue; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | if ( ! $lang[ $msg ] ) |
|---|
| 403 | { |
|---|
| 404 | trigger_error( "Email - Language message missing: ". $msg, E_USER_NOTICE ); |
|---|
| 405 | |
|---|
| 406 | continue; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | if ( $this->to_send[ $id ]['type'] == 'html' && ! $lang[ $msg .'_html' ] ) |
|---|
| 410 | { |
|---|
| 411 | trigger_error( "Email - Language message missing: ". $msg .'_html', E_USER_NOTICE ); |
|---|
| 412 | |
|---|
| 413 | continue; |
|---|
| 414 | } |
|---|
| 415 | |
|---|
| 416 | $message_text = $lang['header'] . $lang[ $msg ] . $lang['footer']; |
|---|
| 417 | $message_html = $lang['header_html'] . $lang[ $msg .'_html' ] . $lang['footer_html']; |
|---|
| 418 | |
|---|
| 419 | $this->create_message( $id ); |
|---|
| 420 | |
|---|
| 421 | $this->set_subject( $subject ); #* use htmlentitydecode |
|---|
| 422 | |
|---|
| 423 | ( $this->to_send[ $id ]['from'] ) ? $from = $this->to_send[ $id ]['from'] : $from = $this->trellis->cache->data['settings']['email']['out_address']; |
|---|
| 424 | |
|---|
| 425 | $this->set_from( $from, $this->trellis->cache->data['settings']['general']['hd_name'] ); |
|---|
| 426 | |
|---|
| 427 | $this->add_to( $this->to_send[ $id ]['email'], $this->to_send[ $id ]['name'] ); |
|---|
| 428 | |
|---|
| 429 | if ( $this->trellis->cache->data['settings']['email']['html'] && $this->to_send[ $id ]['type'] == 'html' ) |
|---|
| 430 | { |
|---|
| 431 | $this->set_body( $message_html, 'text/html' ); |
|---|
| 432 | |
|---|
| 433 | $this->add_part( $message_text, 'text/plain' ); |
|---|
| 434 | } |
|---|
| 435 | else |
|---|
| 436 | { |
|---|
| 437 | $this->set_body( $message_text, 'text/plain' ); |
|---|
| 438 | } |
|---|
| 439 | |
|---|
| 440 | $replace = array( |
|---|
| 441 | '{HD_NAME}' => $this->trellis->cache->data['settings']['general']['hd_name'], |
|---|
| 442 | '{HD_URL}' => $this->trellis->config['hd_url'], |
|---|
| 443 | '{USER_NAME}' => $this->to_send[ $id ]['name'], |
|---|
| 444 | '{USER_ID}' => $this->to_send[ $id ]['id'], |
|---|
| 445 | ); |
|---|
| 446 | |
|---|
| 447 | if ( is_array( $this->to_send[ $id ]['replace'] ) ) $replace = array_merge( $replace, $this->to_send[ $id ]['replace'] ); |
|---|
| 448 | |
|---|
| 449 | $this->set_replacements( array( $this->to_send[ $id ]['email'] => $replace ) ); |
|---|
| 450 | |
|---|
| 451 | $status = $this->send(); |
|---|
| 452 | |
|---|
| 453 | $this->to_send = array(); // Clear emails in queue |
|---|
| 454 | |
|---|
| 455 | return $status; |
|---|
| 456 | } |
|---|
| 457 | } |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | } |
|---|
| 461 | ?> |
|---|