PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/catalog/controller/information/contact.php

https://github.com/opencartlite/opencart
PHP | 229 lines | 180 code | 49 blank | 0 comment | 33 complexity | b20d727d43d0a14192768939531f9830 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. class ControllerInformationContact extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->data += $this->language->load('information/contact');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
  8. $mail = new Mail();
  9. $mail->protocol = $this->config->get('config_mail_protocol');
  10. $mail->parameter = $this->config->get('config_mail_parameter');
  11. $mail->hostname = $this->config->get('config_smtp_host');
  12. $mail->username = $this->config->get('config_smtp_username');
  13. $mail->password = $this->config->get('config_smtp_password');
  14. $mail->port = $this->config->get('config_smtp_port');
  15. $mail->timeout = $this->config->get('config_smtp_timeout');
  16. $mail->setTo($this->config->get('config_email'));
  17. $mail->setFrom($this->request->post['email']);
  18. $mail->setSender($this->request->post['name']);
  19. $mail->setSubject(html_entity_decode(sprintf($this->language->get('email_subject'), $this->request->post['name']), ENT_QUOTES, 'UTF-8'));
  20. $mail->setText(strip_tags(html_entity_decode($this->request->post['enquiry'], ENT_QUOTES, 'UTF-8')));
  21. $mail->send();
  22. $this->redirect($this->url->link('information/contact/success'));
  23. }
  24. $this->data['breadcrumbs'] = array();
  25. $this->data['breadcrumbs'][] = array(
  26. 'text' => $this->language->get('text_home'),
  27. 'href' => $this->url->link('common/home')
  28. );
  29. $this->data['breadcrumbs'][] = array(
  30. 'text' => $this->language->get('heading_title'),
  31. 'href' => $this->url->link('information/contact')
  32. );
  33. if (isset($this->error['name'])) {
  34. $this->data['error_name'] = $this->error['name'];
  35. } else {
  36. $this->data['error_name'] = '';
  37. }
  38. if (isset($this->error['email'])) {
  39. $this->data['error_email'] = $this->error['email'];
  40. } else {
  41. $this->data['error_email'] = '';
  42. }
  43. if (isset($this->error['enquiry'])) {
  44. $this->data['error_enquiry'] = $this->error['enquiry'];
  45. } else {
  46. $this->data['error_enquiry'] = '';
  47. }
  48. if (isset($this->error['captcha'])) {
  49. $this->data['error_captcha'] = $this->error['captcha'];
  50. } else {
  51. $this->data['error_captcha'] = '';
  52. }
  53. $this->data['action'] = $this->url->link('information/contact');
  54. $this->data['store'] = $this->config->get('config_name');
  55. $this->data['address'] = nl2br($this->config->get('config_address'));
  56. $this->data['telephone'] = $this->config->get('config_telephone');
  57. $this->data['fax'] = $this->config->get('config_fax');
  58. if (isset($this->request->post['name'])) {
  59. $this->data['name'] = $this->request->post['name'];
  60. } else {
  61. $this->data['name'] = $this->customer->getFirstName();
  62. }
  63. if (isset($this->request->post['email'])) {
  64. $this->data['email'] = $this->request->post['email'];
  65. } else {
  66. $this->data['email'] = $this->customer->getEmail();
  67. }
  68. if (isset($this->request->post['enquiry'])) {
  69. $this->data['enquiry'] = $this->request->post['enquiry'];
  70. } else {
  71. $this->data['enquiry'] = '';
  72. }
  73. if (isset($this->request->post['captcha'])) {
  74. $this->data['captcha'] = $this->request->post['captcha'];
  75. } else {
  76. $this->data['captcha'] = '';
  77. }
  78. $this->data['locations'] = array();
  79. $this->load->model('setting/location');
  80. $results = $this->model_setting_location->getLocations();
  81. foreach($results as $result) {
  82. $this->data['locations'][] = array(
  83. 'location_id' => $result['location_id'],
  84. 'name' => $result['name'],
  85. 'address_1' => $result['address_1'],
  86. 'address_2' => $result['address_2'],
  87. 'city' => $result['city'],
  88. 'postcode' => $result['postcode'],
  89. 'geocode' => $result['geocode'],
  90. 'open' => $result['open'],
  91. 'comment' => $result['comment']
  92. );
  93. }
  94. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/contact.tpl')) {
  95. $this->template = $this->config->get('config_template') . '/template/information/contact.tpl';
  96. } else {
  97. $this->template = 'default/template/information/contact.tpl';
  98. }
  99. $this->children = array(
  100. 'common/column_left',
  101. 'common/column_right',
  102. 'common/content_top',
  103. 'common/content_bottom',
  104. 'common/footer',
  105. 'common/header'
  106. );
  107. $this->response->setOutput($this->render());
  108. }
  109. public function success() {
  110. $this->data += $this->language->load('information/contact');
  111. $this->document->setTitle($this->language->get('heading_title'));
  112. $this->data['breadcrumbs'] = array();
  113. $this->data['breadcrumbs'][] = array(
  114. 'text' => $this->language->get('text_home'),
  115. 'href' => $this->url->link('common/home')
  116. );
  117. $this->data['breadcrumbs'][] = array(
  118. 'text' => $this->language->get('heading_title'),
  119. 'href' => $this->url->link('information/contact')
  120. );
  121. $this->data['continue'] = $this->url->link('common/home');
  122. if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/success.tpl')) {
  123. $this->template = $this->config->get('config_template') . '/template/common/success.tpl';
  124. } else {
  125. $this->template = 'default/template/common/success.tpl';
  126. }
  127. $this->children = array(
  128. 'common/column_left',
  129. 'common/column_right',
  130. 'common/content_top',
  131. 'common/content_bottom',
  132. 'common/footer',
  133. 'common/header'
  134. );
  135. $this->response->setOutput($this->render());
  136. }
  137. protected function validate() {
  138. if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 32)) {
  139. $this->error['name'] = $this->language->get('error_name');
  140. }
  141. if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
  142. $this->error['email'] = $this->language->get('error_email');
  143. }
  144. if ((utf8_strlen($this->request->post['enquiry']) < 10) || (utf8_strlen($this->request->post['enquiry']) > 3000)) {
  145. $this->error['enquiry'] = $this->language->get('error_enquiry');
  146. }
  147. if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
  148. $this->error['captcha'] = $this->language->get('error_captcha');
  149. }
  150. if (!$this->error) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function captcha() {
  157. $this->session->data['captcha'] = substr(sha1(mt_rand()), 17, 6);
  158. $image = imagecreatetruecolor(150, 35);
  159. $width = imagesx($image);
  160. $height = imagesy($image);
  161. $black = imagecolorallocate($image, 0, 0, 0);
  162. $white = imagecolorallocate($image, 255, 255, 255);
  163. $red = imagecolorallocatealpha($image, 255, 0, 0, 75);
  164. $green = imagecolorallocatealpha($image, 0, 255, 0, 75);
  165. $blue = imagecolorallocatealpha($image, 0, 0, 255, 75);
  166. imagefilledrectangle($image, 0, 0, $width, $height, $white);
  167. imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $red);
  168. imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $green);
  169. imagefilledellipse($image, ceil(rand(5, 145)), ceil(rand(0, 35)), 30, 30, $blue);
  170. imagefilledrectangle($image, 0, 0, $width, 0, $black);
  171. imagefilledrectangle($image, $width - 1, 0, $width - 1, $height - 1, $black);
  172. imagefilledrectangle($image, 0, 0, 0, $height - 1, $black);
  173. imagefilledrectangle($image, 0, $height - 1, $width, $height - 1, $black);
  174. imagestring($image, 10, intval(($width - (strlen($this->session->data['captcha']) * 9)) / 2), intval(($height - 15) / 2), $this->session->data['captcha'], $black);
  175. header('Content-type: image/jpeg');
  176. imagejpeg($image);
  177. imagedestroy($image);
  178. }
  179. }
  180. ?>