mostly filebased Content Presentation System
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

107 lines
2.7KB

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