PageRenderTime 75ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/contact.php

https://github.com/swatCap/banan
PHP | 1205 lines | 402 code | 86 blank | 717 comment | 28 complexity | 92874635d7218bd5323f222f81ce33b2 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Class file to handle user requests for contact form
  4. *
  5. * LICENSE:
  6. *
  7. * This source file is subject to the licensing terms that
  8. * is available through the world-wide-web at the following URI:
  9. * http://codecanyon.net/wiki/support/legal-terms/licensing-terms/.
  10. * The buyers have extended license are hence certified to use or
  11. * extend the functionality of this file or re-sell after modification.
  12. *
  13. * This file is licensed to be used with Eventify theme and the
  14. * same can be resold/redistributed with the terms subject to those
  15. * are specified for extended license at Envato marketplace in the above link
  16. *
  17. * PHP version >= 5.3
  18. *
  19. * @category ContactForm
  20. * @package ContactForm
  21. * @author Kirti Kumar Nayak, India <thebestfreelancer.in@gmail.com>
  22. * @copyright 2013 TheBestFreelancer,
  23. * @license http://codecanyon.net/wiki/support/legal-terms/licensing-terms/ CodeCanyon
  24. * @version Release 1.9
  25. * @link http://demos.thebestfreelancer.in/phpcontact/
  26. * @tutorial http://demos.thebestfreelancer.in/phpcontact/documentation/
  27. */
  28. /**
  29. * Check if Contact class exists or not and define if not
  30. */
  31. if (!class_exists('Contact')) {
  32. /**
  33. * Class to handle contact form requests handled via url
  34. *
  35. * This is a singleton pattern class and can be called via static methods
  36. *
  37. * @category ContactForm
  38. * @package ContactForm
  39. * @author Kirti Kumar Nayak, India <thebestfreelancer.in@gmail.com>
  40. * @copyright 2013 TheBestFreelancer,
  41. * @license http://codecanyon.net/wiki/support/legal-terms/licensing-terms/ CodeCanyon
  42. * @version Release 1.9
  43. * @link http://demos.thebestfreelancer.in/phpcontact/
  44. * @tutorial http://demos.thebestfreelancer.in/phpcontact/documentation/
  45. */
  46. class Contact
  47. {
  48. // {{{ properties
  49. /**
  50. * private variable to store any string
  51. * mainly used to store user submitted data
  52. *
  53. * @access private
  54. * @var string The string submitted by user/visitor
  55. */
  56. private $_str;
  57. /**
  58. * private variable to store a string of allowable HTML tags
  59. * to be used against code / CSFR / XSS attacks by visitors/hackers
  60. *
  61. * @access private
  62. * @var string The allowable html tags for user
  63. */
  64. private $_allowedHTML;
  65. /**
  66. * private variable to store
  67. * an array of restricted HTML/special characters
  68. * to be used against code / CSFR / XSS attacks by visitors/hackers
  69. *
  70. * @access private
  71. * @var string The restricted special characters/strings for user
  72. */
  73. private $_restrictedChars;
  74. /**
  75. * private property to store mail receivers/admin
  76. *
  77. * @access private
  78. * @var string The e-mail id(s) who will receive the mail
  79. */
  80. private $_receiver;
  81. /**
  82. * private property to hold the lowercase letter set
  83. * to be used to generate random string
  84. *
  85. * @access private
  86. * @var string The lowercase character set string
  87. */
  88. private $_lowerCaseChars;
  89. /**
  90. * private property to hold the uppercase letter set
  91. * to be used to generate random string
  92. *
  93. * @access private
  94. * @var string The uppercase character set string
  95. */
  96. private $_upperCaseChars;
  97. /**
  98. * private property to hold the numeric character set
  99. * to be used to generate random string
  100. *
  101. * @access private
  102. * @var string The numeric character set string
  103. */
  104. private $_numericChars;
  105. /**
  106. * private property to hold the special character set
  107. * to be used to generate random string
  108. *
  109. * @access private
  110. * @var string The special character set string
  111. */
  112. private $_specialChars;
  113. /**
  114. * private variable for random characters used by captcha method
  115. *
  116. * @access private
  117. * @var string The character set from which captcha should be generated
  118. */
  119. private $_charSet;
  120. /**
  121. * Private variable to store captcha type
  122. * if you have no GD library installed you may switch over to
  123. * Javascript captcha
  124. *
  125. * @access private
  126. * @var string The type of the captcha wanted
  127. */
  128. private $_captchaType;
  129. /**
  130. * Private variable to store captcha width
  131. *
  132. * @access private
  133. * @var int The height of the captcha image
  134. */
  135. private $_captchaWidth;
  136. /**
  137. * Private variable to store the captcha height
  138. *
  139. * @access private
  140. * @var int The height of the captcha image
  141. */
  142. private $_captchaHeight;
  143. /**
  144. * Private variable to store the location of the font to be used in captcha image
  145. *
  146. * @access private
  147. * @var string The path string of the ttf font file
  148. */
  149. private $_captchaFontLocation;
  150. /**
  151. * Private variable to store the captcha image font size
  152. *
  153. * @access private
  154. * @var float The font size for the captcha image
  155. */
  156. private $_captchaFontSize;
  157. /**
  158. * Private variable to store the captcha string angle
  159. *
  160. * @access private
  161. * @var float The angle of the captcha string
  162. */
  163. private $_captchaCharAngle;
  164. /**
  165. * private variable to hold mail type
  166. * expected values 'plain' / 'html'
  167. *
  168. * @access private
  169. * @var string The string describing mail type
  170. */
  171. private $_mailType;
  172. /**
  173. * private variable to store plain template
  174. *
  175. * @access private
  176. * @var string The plain mail template string
  177. */
  178. private $_plainMailTemplate;
  179. /**
  180. * private variable to store html template
  181. *
  182. * @access private
  183. * @var string The html mail template string
  184. */
  185. private $_htmlMailTemplate;
  186. /**
  187. * private variable to store html reply template
  188. *
  189. * @access private
  190. * @var string The html reply mail template string
  191. */
  192. private $_replyHtmlMailTemplate;
  193. /**
  194. * private variable to contain response and to be converted into json
  195. *
  196. * @access private
  197. * @var mixed Json data object for page responses
  198. */
  199. private $_response;
  200. /**
  201. * private variable to store system auto response mail id
  202. *
  203. * @access private
  204. * @var string The name and e-mail string of the system
  205. */
  206. private $_autoResponder;
  207. /**
  208. * private static variable to hold class object
  209. *
  210. * @access private
  211. * @staticvar
  212. * @var object The current class object
  213. */
  214. private static $_classObject;
  215. // }}}
  216. // {{{ __construct()
  217. /**
  218. * Default constructor class to initialize variables and page data.
  219. * Accoring to singleton class costructor must be private
  220. *
  221. * @return void
  222. * @access private
  223. */
  224. private function __construct()
  225. {
  226. /*
  227. * set Error Reporting to all
  228. */
  229. error_reporting(E_ALL | E_STRICT);
  230. /*
  231. * Initialize a session if not started yet
  232. * YOU MUST ENABLE THIS IF WANT TO USE CAPTCHA !!!!!!!!!!!!
  233. */
  234. /*if (session_id() === '') {
  235. session_start();
  236. }
  237. /*
  238. * Set the receiver e-mail of the mail
  239. * CHANGE IT ACCORDING TO NEEDS
  240. */
  241. $this->_receiver = 'yourname@yourdomain.com';
  242. /*
  243. * initialize the allowed html tags which user/visitor can
  244. * use to send a html formatted message
  245. * define more if you want
  246. */
  247. $this->_allowedHTML = '<a><br><div><p><span><strong>';
  248. $this->_allowedHTML .= '<h1><h2><h3><h4><h5><h6><hr>';
  249. $this->_allowedHTML .= '<table><tr><td><th><thead><tfoot>';
  250. /*
  251. * initialize the allowed html tags which user/visitor can
  252. * use to send a html formatted message
  253. * define more if you want
  254. */
  255. $this->_restrictedChars = array('"', 'javascript', '()', '\\');
  256. /*
  257. * Main user configurations start
  258. * You may edit as per your need from here
  259. * Please refer to documentation if you face problems
  260. * Or you may ask me in the support section
  261. */
  262. // captcha configurations
  263. /*
  264. * define captcha type as php
  265. * you may use javascript captcha too
  266. * possible values: php, js
  267. */
  268. $this->_captchaType = 'php';
  269. /*
  270. * initialize captcha image width
  271. * it is defined as per the html design
  272. */
  273. $this->_captchaWidth = 70;
  274. /*
  275. * initialize the captcha image height
  276. * defined optimum for the design
  277. */
  278. $this->_captchaHeight = 30;
  279. /*
  280. * initialize the font file location to be used for captcha characters
  281. * it must be a valid ttf font file at the specified location
  282. */
  283. $this->_captchaFontLocation = './MONOFONT.TTF';
  284. /*
  285. * initialize the font size of the captcha string
  286. * by default the maximum defined i.e. 80% of the image height
  287. */
  288. $this->_captchaFontSize = $this->_captchaHeight * 0.8;
  289. /*
  290. * initialize the characters angle for the captcha
  291. * it is randomly set between -2 and 2
  292. * as the image height and font size are set
  293. */
  294. $this->_captchaCharAngle = rand(-2, 2);
  295. /*
  296. * initialize the lowercase character set from a-z
  297. */
  298. $this->_lowerCaseChars = 'abcdefghijklmopqrstuvwxyz';
  299. /*
  300. * initialize the uppercase character set from A-Z
  301. */
  302. $this->_upperCaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  303. /*
  304. * initalize the numeric character set from 0-9
  305. */
  306. $this->_numericChars = '0123456789';
  307. /*
  308. * initialize the special character set (add more or delete if you like)
  309. */
  310. $this->_specialChars = '!$%^&*+#~/|';
  311. /*
  312. * initialize the captcha characters as null
  313. */
  314. $this->_charSet = '';
  315. // mail configurations
  316. /*
  317. * Initialize mail type to catch user preferences
  318. * change it if you want plain mail witout any formatting
  319. */
  320. $this->_mailType = 'html';
  321. /*
  322. * initiate auto reply system name and e-mail
  323. * Change it accodting to your needs
  324. */
  325. $this->_autoResponder = 'noreply@yourdomain.com';
  326. /*
  327. * Initialize plain mail content template
  328. */
  329. $this->_plainMailTemplate = '{userMessage}';
  330. $this->_plainMailTemplate .= "\r\n\r\n\r\n" . 'From';
  331. $this->_plainMailTemplate .= "\r\n" . 'Name : {userFullName}';
  332. $this->_plainMailTemplate .= "\r\n" . 'E-Mail : {userEmail}';
  333. // check for optional fields and add them if they are set
  334. /*if (isset($_POST['phone']) and (trim($_POST['phone']) !== '')) {
  335. $this->_plainMailTemplate .= "\r\n" . 'Phone : {userPhone}';
  336. }
  337. if (isset($_POST['url']) and (trim($_POST['url']) !== '')) {
  338. $this->_plainMailTemplate .= "\r\n" . 'URL : {userUrl}';
  339. }
  340. /*
  341. * define html mail content template
  342. */
  343. $this->_htmlMailTemplate = '<html><body>';
  344. $this->_htmlMailTemplate .= '<table style="margin:0 auto;padding:20px;width:90%;display:block;color:#000;background-color:#dddfea;font-family:Tahoma;border-radius:10px"><tr>';
  345. $this->_htmlMailTemplate .= '<td style="padding:5px;width:100%;display:block">';
  346. $this->_htmlMailTemplate .= '<p>{userMessage}</p>';
  347. $this->_htmlMailTemplate .= '</td></tr>';
  348. $this->_htmlMailTemplate .= '<tr><td style="margin-top:10px;padding-left:10px;font-weight:700;font-family:seriff;font-style:italic">';
  349. $this->_htmlMailTemplate .= '<h4>From</h4></td></tr>';
  350. $this->_htmlMailTemplate .= '<tr><td><hr />Name : {userFullName}</td></tr>';
  351. $this->_htmlMailTemplate .= '<tr><td>E-Mail : {userEmail}</td></tr>';
  352. // check for optional fields and add them if they are set
  353. /*if (isset($_POST['phone']) and (trim($_POST['phone']) !== '')) {
  354. $this->_htmlMailTemplate .= '<tr><td>Phone : {userPhone}</td></tr>';
  355. }
  356. if (isset($_POST['url']) and (trim($_POST['url']) !== '')) {
  357. $this->_htmlMailTemplate .= '<tr><td>URL : {userUrl}</td></tr>';
  358. }*/
  359. $this->_htmlMailTemplate .= '</table></body></html>';
  360. /*
  361. * define reply html mail content template
  362. */
  363. $this->_replyHtmlMailTemplate = '<html><body>';
  364. $this->_replyHtmlMailTemplate .= '<table style="margin:0 auto;padding:20px;width:100%;display:block;color:#000;font-family:Tahoma">';
  365. $this->_replyHtmlMailTemplate .= '<tr><td style="padding:10px;width:90%;display:block">';
  366. $this->_replyHtmlMailTemplate .= 'Dear&nbsp;{userName}</td></tr>';
  367. $this->_replyHtmlMailTemplate .= '<tr><td><p>We just received your following mail.<br />We\'ll reach you as soon as possible.</p><br /></td></tr>';
  368. $this->_replyHtmlMailTemplate .= '<tr><td><i style="font-size:10px">This is an auto generated reply. Please <strong>do not reply to this e-mail</strong></i></td></tr>';
  369. $this->_replyHtmlMailTemplate .= '<tr><td style="padding:15px;font-weight:normal;font-size:11px;border:#ccc 1px solid;border-radius:8px">';
  370. $this->_replyHtmlMailTemplate .= '<p>{userMessage}</p></td></tr>';
  371. $this->_replyHtmlMailTemplate .= '<tr><td style="margin-top:10px;padding-left:10px;font-weight:700;font-family:seriff;font-style:italic">';
  372. $this->_replyHtmlMailTemplate .= '<h4>From</h4></td></tr><tr><td>';
  373. $this->_replyHtmlMailTemplate .= $this->_autoResponder;
  374. $this->_replyHtmlMailTemplate .= '</td></tr>';
  375. $this->_replyHtmlMailTemplate .= '</body></html>';
  376. // output configurations
  377. /*
  378. * initialize page contents as null
  379. */
  380. $this->_pageContents = '';
  381. /*
  382. * set page compression to true
  383. * you may set this false
  384. * if you don't like to compress the page contents
  385. */
  386. $this->_pageCompression = true;
  387. /*
  388. * Initialize the response variable as null
  389. */
  390. $this->_response = array(
  391. 'status' => '',
  392. 'message' => '',
  393. 'control' => ''
  394. );
  395. }
  396. // }}}
  397. // {{{ getObject()
  398. /**
  399. * Method to return singleton class object.
  400. * returns current class object if already present
  401. * else creates one
  402. *
  403. * @return object The current class object
  404. * @access public
  405. * @static
  406. *
  407. */
  408. public static function getObject()
  409. {
  410. /*
  411. * check if class not instantiated
  412. */
  413. if (self::$_classObject === null) {
  414. /*
  415. * then create a new instance
  416. */
  417. self::$_classObject = new self();
  418. }
  419. /*
  420. * return the class object to be used
  421. */
  422. return self::$_classObject;
  423. }
  424. // }}}
  425. // {{{ _getRandomChars
  426. /**
  427. * Generate string of random characters
  428. *
  429. * @param int $length Length of the string to generate
  430. * @param bool $lowerCaseChars Include lower case characters
  431. * @param bool $upperCaseChars Include uppercase characters
  432. * @param bool $numericChars Include numbers
  433. * @param bool $specialChars Include special characters
  434. *
  435. * @access private
  436. * @return string The random character string
  437. */
  438. private function _getRandomChars (
  439. $length = 5,
  440. $lowerCaseChars = true,
  441. $upperCaseChars = true,
  442. $numericChars = true,
  443. $specialChars = false
  444. ) {
  445. /**
  446. * variable to store a random character index every time
  447. * @access private
  448. * @var int The random character index out of character set
  449. */
  450. $charIndex = '';
  451. /**
  452. * variable to store a random character every time
  453. * @access private
  454. * @var char The random character out of character set
  455. */
  456. $char = '';
  457. /**
  458. * variable to store a random character set every time
  459. * @access private
  460. * @var int The random character setof length 5 out of character set
  461. */
  462. $resultChars = '';
  463. /*
  464. * check if user has opted for lowercase characters
  465. * if true, then add it to the character set
  466. */
  467. if ($lowerCaseChars === true) {
  468. $this->_charSet .= $this->_lowerCaseChars;
  469. }
  470. /*
  471. * Check if user has opted for uppercase characters
  472. * If true, add it to the character set
  473. */
  474. if ($upperCaseChars === true) {
  475. $this->_charSet .= $this->_upperCaseChars;
  476. }
  477. /*
  478. * Check if user has opted for numeric characters
  479. * If true, add it to the character set
  480. */
  481. if ($numericChars === true) {
  482. $this->_charSet .= $this->_numericChars;
  483. }
  484. /*
  485. * Check if user has opted for uppercase characters
  486. * If true, add it to the character set
  487. */
  488. if ($specialChars === true) {
  489. $this->_charSet .= $this->_specialChars;
  490. }
  491. /*
  492. * Check if length has given greater than 0 else return null
  493. */
  494. if (($length < 0) || ($length == 0)) {
  495. return $resultChars;
  496. }
  497. /*
  498. * create a loop to get random 5 characters from the character set
  499. *
  500. */
  501. for ($i = 0; $i < $length; $i++) {
  502. /*
  503. * get the character randomly
  504. * by selecting between 0 to length of the charSet
  505. */
  506. $charIndex = rand(0, strlen($this->_charSet));
  507. $char = substr($this->_charSet, $charIndex, 1);
  508. $resultChars .= $char;
  509. }
  510. return $resultChars;
  511. }
  512. // }}}
  513. // {{{ respondRequest()
  514. /**
  515. * Method to respond to the requests via url
  516. *
  517. * @param bool $pageCompression Option for the output data to be compressed or not
  518. * @param mixed $mapOptions The option to show/hide, logitude, latitude for Google Map
  519. * @param string $companyName The name of your company
  520. * @param string $address The address of your company
  521. * @param string $captchaType The type of captcha wanted, possible values: php, js
  522. * @param string $mailType The type of mail to be sent, possible values: html, text
  523. * @param string $emails The address of mail to send, possible values: as specified
  524. * @param string $autoResponder The address of reply mail to be sent back to the visitor
  525. *
  526. * @return mixed May output page HTML string or JSON validation data
  527. * @access public
  528. */
  529. public function respondRequest(
  530. $captchaType,
  531. $mailType,
  532. $emails,
  533. $autoResponder
  534. ) {
  535. /*
  536. * assign captcha type as user has defined
  537. * you may use javascript captcha too
  538. * possible values: php, js
  539. */
  540. $this->_captchaType = $captchaType;
  541. /*
  542. * assign mail type to catch user preferences
  543. * change it if you want plain mail witout any formatting
  544. */
  545. $this->_mailType = $mailType;
  546. /*
  547. * set page compression to user defined
  548. * you may set this false
  549. * if you don't like to compress the page contents
  550. */
  551. $this->_receiver = $emails;
  552. /*
  553. * set page compression to user defined
  554. * you may set this false
  555. * if you don't like to compress the page contents
  556. */
  557. $this->_autoResponder = $autoResponder;
  558. /*
  559. * catch the get variable
  560. * if set then act accordingly
  561. */
  562. if (isset($_GET['req'])) {
  563. /*
  564. * switch over the request and respond accordingly
  565. */
  566. switch ($_GET['req']) {
  567. case 'captcha':
  568. /*
  569. * call the method to create the captcha image
  570. */
  571. $this->createCaptcha();
  572. break;
  573. case 'captchaimg':
  574. /*
  575. * check if user has opted php captcha and GD library present
  576. * if user has them both then
  577. * call the method to create the captcha image
  578. * else just return the captcha characters
  579. */
  580. if (extension_loaded('gd') and ($this->_captchaType==='php')) {
  581. /*
  582. * return an image tag
  583. */
  584. echo '<img src="' . $_SERVER['PHP_SELF'] . '?req=captcha&tm='.time().'" alt="Captcha Image" title="Click to get new challenge" />';
  585. } else {
  586. /*
  587. * take two variables with random integer values,
  588. * then add them and save in session to verify
  589. * @var int Integers to work as captcha
  590. */
  591. $a = rand(1, 9);
  592. $b = rand(1, 9);
  593. /*
  594. * Assign the characters to a session variable
  595. */
  596. $_SESSION['CaptchaChars'] = $a+$b;
  597. /*
  598. * Close the session write buffer to avoid overwriting
  599. */
  600. session_write_close();
  601. /*
  602. * simply output the characters only
  603. */
  604. echo '<h5>'.$a.' + '.$b.' =</h5>';
  605. }
  606. break;
  607. default:
  608. /*
  609. * call the method to validate and send the message
  610. */
  611. $this->sendMail();
  612. break;
  613. }
  614. /*
  615. * validate the name must not be empty
  616. * and send json data
  617. */
  618. } else {
  619. /*
  620. * call the method to validate and send the message
  621. */
  622. $this->sendMail();
  623. }
  624. }
  625. // }}}
  626. // {{{ createCaptcha()
  627. /**
  628. * Method to create captcha image for bot verification
  629. *
  630. * @return mixed Image for captcha
  631. * @throws Exception GD or general exceptions
  632. * @access public
  633. */
  634. public function createCaptcha()
  635. {
  636. try {
  637. /*
  638. * Assign the characters to a session variable
  639. */
  640. $_SESSION['CaptchaChars'] = $this->_getRandomChars(5, false, true, true, false);
  641. /*
  642. * Close the session write buffer to avoid overwriting
  643. */
  644. session_write_close();
  645. /*
  646. * Create a 100 X 30 image and assign it to a var
  647. */
  648. $img = imagecreatetruecolor($this->_captchaWidth, $this->_captchaHeight);
  649. /*
  650. * create a white color
  651. */
  652. $white = imagecolorallocate($img, 255, 255, 255);
  653. /*
  654. * Create a black color to write the characters prominently
  655. */
  656. $black = imagecolorallocate($img, 0, 0, 0);
  657. /*
  658. * fill the rectangular image with white background
  659. */
  660. imagefilledrectangle($img, 0, 0, 399, 30, $white);
  661. /*
  662. * Write the string inside the image
  663. * with black color
  664. */
  665. imagettftext(
  666. $img,
  667. $this->_captchaFontSize,
  668. $this->_captchaCharAngle,
  669. 2,
  670. 25,
  671. $black,
  672. $this->_captchaFontLocation,
  673. $_SESSION['CaptchaChars']
  674. );
  675. /*
  676. * generating dots randomly in background
  677. * to make an image noise
  678. * if you want more noise replace the argument 5
  679. * as per your requirement
  680. */
  681. for ( $i=0; $i<5; $i++ ) {
  682. imagefilledellipse(
  683. $img,
  684. mt_rand(0, $this->_captchaWidth),
  685. mt_rand(0, $this->_captchaHeight),
  686. 2,
  687. 3,
  688. 0
  689. );
  690. }
  691. /*
  692. * generating lines randomly in background of image
  693. * for more noise
  694. * if you want more noise replace the argument 10
  695. * as per your requirement
  696. */
  697. for ( $i=0; $i<10; $i++ ) {
  698. imageline(
  699. $img,
  700. mt_rand(0, $this->_captchaWidth),
  701. mt_rand(0, $this->_captchaHeight),
  702. mt_rand(0, $this->_captchaWidth),
  703. mt_rand(0, $this->_captchaHeight),
  704. 0
  705. );
  706. }
  707. /*
  708. * Output the image
  709. */
  710. header('Content-Type: image/gif');
  711. /*
  712. * output a gif image
  713. */
  714. imagegif($img);
  715. /*
  716. * destroy the image to save server space
  717. */
  718. imagedestroy($img);
  719. }
  720. catch(Exception $ex) {
  721. die('Oh no.. Something gone wrong... Details: ' . $ex->getMessage());
  722. }
  723. }
  724. // }}}
  725. // {{{ _cleanSubmittedData()
  726. /**
  727. * The following method makes a variable safe
  728. * as that may contain unacceptable formats or data
  729. * to prevent security holes those may be a threat
  730. *
  731. * @param mixed $submittedData The data submitted by the user to be filtered
  732. *
  733. * @return mixed Cleaned data submitted by the user
  734. * @access protected
  735. */
  736. protected function _cleanSubmittedData($submittedData)
  737. {
  738. try {
  739. /*
  740. * check if the data is an array or not
  741. */
  742. if (!is_array($submittedData)) {
  743. /*
  744. * if that is not an array, treat that as a string
  745. */
  746. $this->_str = $submittedData;
  747. /*
  748. * trim the spaces if any
  749. */
  750. $this->_str = trim($this->_str);
  751. /*
  752. * check if magic quotes are on or not
  753. * if on then it must have inserted slashes before quotes and slashes
  754. */
  755. if (get_magic_quotes_gpc()) {
  756. /*
  757. * if magic quotes are on, it inserts a slash before any quotes, hence remove them
  758. */
  759. $this->_str = stripslashes($this->_str);
  760. }
  761. /*
  762. * escape the data and insert null where restricted characters found
  763. */
  764. $this->_str = str_ireplace($this->_restrictedChars, "", $this->_str);
  765. /*
  766. * allow the tags for user and strip off rest of them
  767. */
  768. $this->_str = strip_tags($this->_str, $this->_allowedHTML);
  769. /*
  770. * now return the cleaned data
  771. */
  772. return $this->_str;
  773. } else {
  774. /**
  775. * var to keep cleaned data array for a temporary period
  776. * so that they can be returned in cleaned state
  777. * and acceptable format
  778. *
  779. * @var mixed The injection cleaned data array
  780. * @access private
  781. */
  782. $cleanArr = array();
  783. /*
  784. * if the data is an array
  785. * fetch the array values one by one by the loop
  786. */
  787. foreach ($submittedData as $pointer=>$str) {
  788. /*
  789. * Recursively call clean function if the data is array
  790. */
  791. $cleanArr[$pointer]=$this->_cleanSubmittedData($str);
  792. }
  793. /*
  794. * return the cleaned data array
  795. */
  796. return $cleanArr;
  797. }
  798. }
  799. catch(Exception $ex) {
  800. /*
  801. * Catch any Exceptions occured
  802. */
  803. die('There seems an error while cleaning user submitted data. Description: '. $ex->getMessage());
  804. }
  805. }
  806. public function saveToFile($submittedData)
  807. {
  808. $file = 'participants.txt';
  809. // Open the file to get existing content
  810. $current = file_get_contents($file);
  811. // Append a new person to the file
  812. $current .= print_r($submittedData,true);
  813. $current .= "\n";
  814. // Write the contents back to the file
  815. file_put_contents($file, $current);
  816. }
  817. // }}}
  818. // {{{ sendMail()
  819. /**
  820. * Method to send normal mail
  821. *
  822. * @return string JSON data object for success or failure
  823. * @access public
  824. */
  825. public function sendMail()
  826. {
  827. /*
  828. * clean up the user submitted data with the defined method
  829. */
  830. $submittedData = $this->_cleanSubmittedData($_POST);
  831. /*
  832. * validate the form data
  833. */
  834. if (!isset($submittedData['contact_name']) or ($submittedData['contact_name'] === '')) {
  835. /*
  836. * fill out the response array variable
  837. * with alert/error message having bootstrap styles
  838. */
  839. $this->_response['status'] = 'error';
  840. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  841. $this->_response['message'] .= 'Please Enter your Name'.$submittedData['contact_name'];
  842. $this->_response['message'] .= '</div>';
  843. /*
  844. * output the json data by converting the response array into a json format
  845. */
  846. echo json_encode($this->_response);
  847. /*
  848. * exit the script
  849. */
  850. exit(0);
  851. }
  852. /*
  853. * validate email via php predefined (inbuilt) function
  854. */
  855. if (!isset($submittedData['contact_email']) or (strlen(filter_var($submittedData['contact_email'], FILTER_VALIDATE_EMAIL)) < 1)) {
  856. $this->_response['status'] = 'error';
  857. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  858. $this->_response['message'] .= 'Please Enter your Valid E-Mail';
  859. $this->_response['message'] .= '</div>';
  860. echo json_encode($this->_response);
  861. exit(0);
  862. }
  863. /*
  864. * validate the phone number correct or not if entered
  865. * enable if you want to validate phone number
  866. */
  867. /*if (isset($submittedData['phone']) and !empty($submittedData['phone']) and (!preg_match('/^(\+[1-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*$/', $submittedData['phone']))) {
  868. $this->_response['status'] = 'error';
  869. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable">';
  870. $this->_response['message'] .= 'Please Enter a correct phone number';
  871. $this->_response['message'] .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>';
  872. $this->_response['control'] = 'phone';
  873. echo json_encode($this->_response);
  874. exit(0);
  875. }
  876. /*
  877. * validate the url correct or not if entered
  878. * enable this if you want to validate url
  879. */
  880. /*if (isset($submittedData['url']) and !empty($submittedData['url']) and (!preg_match('/(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/', $submittedData['url']))) {
  881. $this->_response['status'] = 'error';
  882. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable">';
  883. $this->_response['message'] .= 'Please Enter a correct URL';
  884. $this->_response['message'] .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>';
  885. $this->_response['control'] = 'url';
  886. header("Content-Type: application/json");
  887. echo json_encode($this->_response);
  888. exit(0);
  889. }
  890. /*
  891. * validate subject for empty value
  892. */
  893. if (!isset($submittedData['contact_subject']) or ($submittedData['contact_subject']==='')) {
  894. $this->_response['status'] = 'error';
  895. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  896. $this->_response['message'] .= 'Please Enter a Subject';
  897. $this->_response['message'] .= '</div>';
  898. echo json_encode($this->_response);
  899. exit(0);
  900. }
  901. /*
  902. * validate the message empty or blank
  903. */
  904. if (!isset($submittedData['contact_message']) or ($submittedData['contact_message'] === '')) {
  905. $this->_response['status'] = 'error';
  906. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  907. $this->_response['message'] .= 'Please Enter your Message';
  908. $this->_response['message'] .= '</div>';
  909. echo json_encode($this->_response);
  910. exit(0);
  911. }
  912. /*
  913. * Enable this if you want to validate captcha
  914. */
  915. /*if (!isset($submittedData['captcha']) or !isset($_SESSION['CaptchaChars']) or ($submittedData['captcha'] !== (string)$_SESSION['CaptchaChars'])) {
  916. $this->_response['status'] = 'error';
  917. $this->_response['message'] = '<div class="alert alert-danger alert-dismissable">';
  918. $this->_response['message'] .= 'Please Enter The Captcha Correctly';
  919. $this->_response['message'] .= '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button></div>';
  920. $this->_response['control'] = 'captcha';
  921. echo json_encode($this->_response);
  922. exit(0);
  923. }
  924. /*
  925. * Build up the mail and
  926. * Set headers first, else the mail may be caught as spam
  927. */
  928. $headers = array();
  929. $headers[] = "MIME-Version: 1.0";
  930. // set content type according to the option supplied
  931. if ($this->_mailType === 'html') {
  932. $headers[] = "Content-type: text/html; charset=iso-8859-1";
  933. } else {
  934. $headers[] = "Content-type: text/plain; charset=iso-8859-1";
  935. }
  936. $headers[] = "From: {$submittedData['contact_name']} <{$submittedData['contact_email']}>";
  937. $headers[] = "Reply-To: {$submittedData['contact_name']} <{$submittedData['contact_email']}>";
  938. $headers[] = "Subject: {$submittedData['contact_subject']}";
  939. /*
  940. * Set final headers by separating them by newlines into a single string
  941. */
  942. $finalHeaderString = implode("\r\n", $headers);
  943. /*
  944. * Set up the message according to the template
  945. */
  946. if ($this->_mailType === 'html') {
  947. // set up the html template and add contents
  948. $message = str_replace(
  949. array(
  950. '{userMessage}',
  951. '{userFullName}',
  952. '{userEmail}',
  953. //'{userPhone}',
  954. //'{userUrl}'
  955. ),
  956. array(
  957. nl2br($submittedData['contact_message']),
  958. $submittedData['contact_name'],
  959. $submittedData['contact_email'],
  960. //$submittedData['phone'],
  961. //$submittedData['url']
  962. ),
  963. "{$this->_htmlMailTemplate}"
  964. );
  965. } else {
  966. // set up the plain mail template
  967. $message = str_replace(
  968. array(
  969. '{userMessage}',
  970. '{userFullName}',
  971. '{userEmail}',
  972. //'{userPhone}',
  973. //'{userUrl}'
  974. ),
  975. array(
  976. $submittedData['contact_message'],
  977. $submittedData['contact_name'],
  978. $submittedData['contact_email'],
  979. //$submittedData['phone'],
  980. //$submittedData['url']
  981. ),
  982. "{$this->_plainMailTemplate}"
  983. );
  984. }
  985. $this->saveToFile($submittedData);
  986. /*
  987. * Mail it and catch the result to further check if mail has sent or not
  988. */
  989. $mailed = mail($this->_receiver, $submittedData['contact_subject'], $message, $finalHeaderString);
  990. /*
  991. * Now check if the mailing was successful
  992. */
  993. // if ($mailed) {
  994. /*
  995. * if sender has checked the checkbox to get acknowledgement,
  996. * then send a confirmation mail
  997. * You dont have any checkbox hence commented the if condition
  998. */
  999. //if (isset($submittedData['acknowledge']) and ($submittedData['acknowledge']==='1')) {
  1000. // Build up the mail and
  1001. $headers = array();
  1002. $headers[] = "MIME-Version: 1.0";
  1003. $headers[] = "Content-type: text/html; charset=iso-8859-1";
  1004. $headers[] = "From: ".$this->_autoResponder;
  1005. $headers[] = "Reply-To: {$this->_receiver}";
  1006. $headers[] = "Subject: reply: {$submittedData['contact_subject']}";
  1007. $finalHeaderString = implode("\r\n", $headers);
  1008. // set up the html reply mail template
  1009. $message = str_replace(
  1010. array(
  1011. '{userName}',
  1012. '{userMessage}'
  1013. ),
  1014. array(
  1015. $submittedData['contact_name'],
  1016. nl2br($submittedData['contact_message'])
  1017. ),
  1018. $this->_replyHtmlMailTemplate
  1019. );
  1020. /*
  1021. * Mail it and catch the result to further check if mail has sent or not
  1022. */
  1023. $mail = mail($submittedData['contact_email'], 'reply: ' . $submittedData['contact_subject'], $message, $finalHeaderString);
  1024. //}
  1025. $this->_response['status'] = 'success';
  1026. $this->_response['message'] = '<div class="alert alert-success alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  1027. $this->_response['message'] .= 'Дякуємо за участь! Кожна людина для нас важлива! Чекайте на дзвінок.';
  1028. // if acknowledgement opted, then output a message to view inbox ## Commented as you need not
  1029. //if (isset($submittedData['acknowledge']) and ($submittedData['acknowledge']==='1')) {
  1030. //$this->_response['message'] .= ' Please check your acknowledgement in your email (inbox/spam folder).';
  1031. //}
  1032. $this->_response['message'] .= '</div>';
  1033. echo json_encode($this->_response);
  1034. exit(0);
  1035. // } else {
  1036. // /*
  1037. // * Else give Out an error message and reset
  1038. // */
  1039. // $this->_response['status'] = 'success';
  1040. // $this->_response['message'] = '<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>';
  1041. // $this->_response['message'] .= 'Some Error Occured!';
  1042. // $this->_response['message'] .= '</div>';
  1043. // echo json_encode($this->_response);
  1044. // exit(0);
  1045. // }
  1046. }
  1047. // }}}
  1048. // {{{ __clone()
  1049. /**
  1050. * According to singleton pattern instance, cloning is prihibited
  1051. *
  1052. * @return string A message that states, cloning is prohibited
  1053. * @access public
  1054. */
  1055. private function __clone()
  1056. {
  1057. /*
  1058. * only set an error message
  1059. */
  1060. die('Cloning is prohibited for singleton instance.');
  1061. }
  1062. // }}}
  1063. }
  1064. }
  1065. //echo 'error:'.implode(', '$_POST);exit();
  1066. /*
  1067. * Call Setup for the class
  1068. * you must set these options
  1069. * before using the class
  1070. * This configuration should be present
  1071. * when you call the class object
  1072. * Otherwise you may not get your desired results
  1073. *
  1074. * please contact the author in case
  1075. * any difficulties
  1076. */
  1077. /**
  1078. * Set the type of the captcha needed
  1079. * @var string The type of captcha needed
  1080. */
  1081. $captchaType = null;
  1082. /**
  1083. * Set the reciever email(s) to receive the mail(s)
  1084. *
  1085. * @var string The e-mails of contact mail receiver
  1086. */
  1087. $emails = 'yourname@yourdomain.com';
  1088. /**
  1089. * Set auto reply system name and e-mail
  1090. * from where the acknowledgement to be sent.
  1091. * Generally it is set to be the id where no reply to be sent
  1092. * Change it according to your needs
  1093. *
  1094. * @var string The e-mail for auto reply system
  1095. */
  1096. $autoResponder = 'noreply@yourdomain.com';
  1097. /**
  1098. * Set desired type of mail to send
  1099. * Possible values: html/text
  1100. * Change according to need
  1101. *
  1102. * @var string The type of mail to be sent
  1103. */
  1104. $mailType = 'html';
  1105. /*
  1106. * Now call the class method to implement all the options
  1107. * and get the response as per need
  1108. */
  1109. Contact::getObject()->respondRequest(
  1110. $captchaType,
  1111. $mailType,
  1112. $emails,
  1113. $autoResponder
  1114. );