mostly filebased Content Presentation System
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 lines
2.8KB

  1. <?php
  2. namespace Controller;
  3. class Email {
  4. public $c = [];
  5. public $data = [];
  6. public $path = "";
  7. function __construct() {
  8. }
  9. function load_form_config($token) {
  10. $f3 = \Base::instance();
  11. $token_db = $f3->get('TEMP') . "CEform/";
  12. $db = new \DB\Jig($token_db,\DB\Jig::FORMAT_JSON);
  13. $formcall = new \DB\Jig\Mapper($db,'form_calls');
  14. if($formcall->load(['@token = ?', $token])) {
  15. $f3->config($formcall->form);
  16. $f3->set('form_path', $formcall->path);
  17. $this->path = $formcall->path;
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23. function get_post_data() {
  24. $f3 = \Base::instance();
  25. $token = $f3->get('POST.xss-token');
  26. if (ctype_alnum( $token )) {
  27. if ($this->load_form_config($token)) {
  28. foreach ($f3->get('fields') as $field => $def) {
  29. $this->data['fields'][$field] = $f3->get('POST.'.$field);
  30. }
  31. $this->data['private'] = $f3->get('private');
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. } else {
  37. // wrong xss-token supplied - malicous attac expected
  38. die;
  39. }
  40. }
  41. function send() {
  42. $f3 = \Base::instance();
  43. $this->get_post_data();
  44. $to = $this->data['private']['email'];
  45. $subject = $this->data['private']['subject'];
  46. $message = $this->data['fields']['message'];
  47. $c = $this->data['private']['emailconfig'];
  48. $template = substr($this->path,strlen($f3->get('CONTENT_BASE'))+2) . $this->data['private']['template'];
  49. $f3->set('fields', $this->data['fields']);
  50. $headers = [
  51. "MIME-Version"=>"1.0",
  52. "Content-type"=>"text/html",
  53. "From" => $c['from']
  54. ];
  55. //$c = $this->c;
  56. $smtp = new \SMTP(
  57. $c['host'],
  58. $c['port'],
  59. $c['scheme'],
  60. $c['user'],
  61. $c['pass'],
  62. );
  63. $smtp->set('To', $to);
  64. $smtp->set('Subject',$subject);
  65. foreach ($headers as $k=>$v) {
  66. $smtp->set($k,$v);
  67. }
  68. $email = \Template::instance()->render($template,'text/html');
  69. //echo $email;
  70. if ($smtp->send($email)) {
  71. $success = true;
  72. } else {
  73. $success = false;
  74. }
  75. if ($success) {
  76. $f3->reroute("/email/success?email=".urlsafe_b64encode($email)."&lang=".$f3->get('LANG'));
  77. } else {
  78. #$f3->reroute("/email/error");
  79. echo \Template::instance()->render($template,'text/html');
  80. die;
  81. }
  82. }
  83. }