PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/components/com_chronocontact/chronocontact.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 540 lines | 374 code | 31 blank | 135 comment | 83 complexity | de9d5f9d412cbf5984312e706970cc58 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * CHRONOFORMS version 1.0 stable
  4. * Copyright (c) 2006 Chrono_Man, ChronoEngine.com. All rights reserved.
  5. * Author: Chrono_Man (ChronoEngine.com)
  6. * See readme.html.
  7. * Visit http://www.ChronoEngine.com for regular update and information.
  8. **/
  9. /* ensure that this file is called by another file */
  10. defined( '_VALID_MOS' ) or die( 'Direct access of this file is prohibited.' );
  11. global $mosConfig_lang, $mosConfig_absolute_path, $chronocontact_params;
  12. /**
  13. * Load the HTML class
  14. */
  15. require_once( $mainframe->getPath( 'front_html' ) );
  16. require_once( $mainframe->getPath( 'class' ) );
  17. $ChronoContact = new mosChronoContact($database);
  18. $posted = array();
  19. $posted = $_POST;
  20. /**
  21. * Main switch statement
  22. */
  23. switch( $task ) {
  24. case 'send':
  25. uploadandmail();
  26. break;
  27. default:
  28. showform($posted);
  29. break;
  30. }
  31. /**
  32. * End of main page
  33. *
  34. */
  35. /**
  36. * Display the form for entry
  37. *
  38. */
  39. function showform($posted)
  40. {
  41. global $database, $mosConfig_live_site, $mosConfig_absolute_path;
  42. $formname = mosGetParam( $_GET, 'chronoformname', '0' );
  43. //$formname = $_GET['chronoformname'];
  44. if ( !$formname ) {
  45. $query = "
  46. SELECT params
  47. FROM #__menu
  48. WHERE id='".$_GET['Itemid']."' AND type='components'";
  49. $database->setQuery( $query );
  50. $menudata = $database->loadResult();
  51. if ( $menudata ) {
  52. $configs = mosParseParams($menudata);
  53. $formname = $configs->formname;
  54. }
  55. }
  56. $query = "
  57. SELECT *
  58. FROM #__chrono_contact
  59. WHERE name = '$formname'";
  60. $database->setQuery( $query );
  61. $rows = $database->loadObjectList();
  62. $paramsvalues = mosParseParams( $rows[0]->paramsall );
  63. if ( trim($paramsvalues->imagever) == 'Yes' ) {
  64. $imver = '<input name="chrono_verification" type="text" id="chrono_verification" value="" />
  65. &nbsp;&nbsp;<img src="'.$mosConfig_live_site
  66. .'/administrator/components/com_chronocontact/chrono_verification.php" alt="Verification Security Image" />';
  67. }
  68. $htmlstring = $rows[0]->html;
  69. if( trim($paramsvalues->validate) == 'Yes'){
  70. // Easy Validation //
  71. preg_match_all('/name=("|\').*?("|\')/i', $htmlstring, $matches);
  72. $arr_required = explode(",", $paramsvalues->val_required);
  73. $arr_validate_number = explode(",", $paramsvalues->val_validate_number);
  74. $arr_validate_digits = explode(",", $paramsvalues->val_validate_digits);
  75. $arr_validate_alpha = explode(",", $paramsvalues->val_validate_alpha);
  76. $arr_validate_alphanum = explode(",", $paramsvalues->val_validate_alphanum);
  77. $arr_validate_date = explode(",", $paramsvalues->val_validate_date);
  78. $arr_validate_email = explode(",", $paramsvalues->val_validate_email);
  79. $arr_validate_url = explode(",", $paramsvalues->val_validate_url);
  80. $arr_validate_date_au = explode(",", $paramsvalues->val_validate_date_au);
  81. $arr_validate_currency_dollar = explode(",", $paramsvalues->val_validate_currency_dollar);
  82. $arr_validate_selection = explode(",", $paramsvalues->val_validate_selection);
  83. $arr_validate_one_required = explode(",", $paramsvalues->val_validate_one_required);
  84. $arr_all = array_merge($arr_required, $arr_validate_number, $arr_validate_digits, $arr_validate_alpha, $arr_validate_alphanum, $arr_validate_date, $arr_validate_email, $arr_validate_url, $arr_validate_date_au,
  85. $arr_validate_currency_dollar, $arr_validate_selection, $arr_validate_one_required);
  86. foreach ($matches[0] as $match)
  87. {
  88. $new_match = preg_replace('/name=("|\')/i', '', $match);
  89. $new_match2 = preg_replace('/("|\')/', '', $new_match);
  90. $name = preg_replace('/name=("|\')/', '', $new_match2);
  91. $class_array = array();
  92. if(in_array($name,$arr_all)){
  93. if(in_array($name,$arr_required)){
  94. $class_array[] = "required";
  95. }
  96. if(in_array($name,$arr_validate_number)){
  97. $class_array[] = "validate-number";
  98. }
  99. if(in_array($name,$arr_validate_digits)){
  100. $class_array[] = "validate-digits";
  101. }
  102. if(in_array($name,$arr_validate_alpha)){
  103. $class_array[] = "validate-alpha";
  104. }
  105. if(in_array($name,$arr_validate_alphanum)){
  106. $class_array[] = "validate-alphanum";
  107. }
  108. if(in_array($name,$arr_validate_date)){
  109. $class_array[] = "validate-date";
  110. }
  111. if(in_array($name,$arr_validate_email)){
  112. $class_array[] = "validate-email";
  113. }
  114. if(in_array($name,$arr_validate_url)){
  115. $class_array[] = "validate-url";
  116. }
  117. if(in_array($name,$arr_validate_date_au)){
  118. $class_array[] = "validate-date-au";
  119. }
  120. if(in_array($name,$arr_validate_currency_dollar)){
  121. $class_array[] = "validate-currency-dollar";
  122. }
  123. if(in_array($name,$arr_validate_selection)){
  124. $class_array[] = "validate-selection";
  125. }
  126. if(in_array($name,$arr_validate_one_required)){
  127. $class_array[] = "validate-one-required";
  128. }
  129. $class_string = implode(" ",$class_array);
  130. $htmlstring = str_replace($match,$match.' class="'.$class_string.'"',$htmlstring);
  131. }
  132. }
  133. $rows[0]->html = $htmlstring;
  134. }
  135. /// end validation //
  136. HTML_ChronoContact::showform( $rows , $imver);
  137. }
  138. /**
  139. * Respond to a submitted form
  140. *
  141. */
  142. function uploadandmail()
  143. {
  144. global $database, $mosConfig_mailfrom, $mosConfig_fromname, $my, $chronocontact_params,
  145. $mosConfig_live_site, $mosConfig_absolute_path;
  146. // Block SPAM through the submit URL
  147. if ( empty($_POST) ) {
  148. echo "You are not allowed to access this URL directly, POST array is empty";
  149. return;
  150. }
  151. /**
  152. * Retrieve form data from the database
  153. */
  154. $formname = mosGetParam( $_GET, 'chronoformname', '0' );
  155. //$formname = $_GET['chronoformname'];
  156. $query = "
  157. SELECT *
  158. FROM #__chrono_contact
  159. WHERE name='$formname'";
  160. $database->setQuery( $query );
  161. $rows = $database->loadObjectList();
  162. $titlesvalues = mosParseParams($rows[0]->titlesall);
  163. $paramsvalues = mosParseParams($rows[0]->paramsall);
  164. $error_found = false;
  165. /**
  166. * If imageversification is on check the code
  167. */
  168. if ( trim($paramsvalues->imagever) == 'Yes' ) {
  169. session_start();
  170. $chrono_verification = strtolower($_POST['chrono_verification']);
  171. if ( md5($chrono_verification ) != $_SESSION['chrono_verification'] ) {
  172. showErrorMessage('Sorry, You have entered a wrong verification code');
  173. showform($_POST);
  174. return;
  175. }else{
  176. unset($_SESSION['chrono_verification']);
  177. }
  178. }
  179. /**
  180. * if $debug is true then ChronoForms will show diagnostic output
  181. */
  182. $debug = $paramsvalues->debug;
  183. if ( $debug ) {
  184. echo "_POST: ";
  185. print_r($_POST);
  186. echo "<br />";
  187. }
  188. /**
  189. * Upload attachments
  190. */
  191. $attachments = array();
  192. if ( trim($paramsvalues->uploads == 'Yes' ) && trim($paramsvalues->uploadfields) ) {
  193. $allowed_s1 = explode(",", trim($paramsvalues->uploadfields));
  194. foreach ( $allowed_s1 as $allowed_1 ) {
  195. $allowed_s2 = explode(":", trim($allowed_1));
  196. $allowed_s3 = explode("|", trim($allowed_s2[1]));
  197. $original_name = $_FILES[$allowed_s2[0]]['tmp_name'];
  198. $filename = date('YmdHis').'_'.preg_replace('`[^a-z0-9-_.]`i','',$_FILES[$allowed_s2[0]]['name']);
  199. $fileok = true;
  200. if ( $original_name ) {
  201. if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) > trim($paramsvalues->uploadmax) ) {
  202. $fileok = false;
  203. showErrorMessage('Sorry, Your uploaded file size exceeds the allowed limit.');
  204. exit();
  205. }
  206. if ( ($_FILES[$allowed_s2[0]]["size"] / 1024) < trim($paramsvalues->uploadmin) ) {
  207. $fileok = false;
  208. showErrorMessage('Sorry, Your uploaded file size is less than the allowed limit');
  209. exit();
  210. }
  211. $fn = $_FILES[$allowed_s2[0]]['name'];
  212. $fext = substr($fn, strrpos($fn, '.') + 1);
  213. if ( !in_array($fext, $allowed_s3) ) {
  214. $fileok = true;
  215. showErrorMessage('Sorry, Your uploaded file type is not allowed');
  216. exit();
  217. }
  218. if ( $fileok ) {
  219. $uploadedfile = handle_uploaded_files($original_name, $filename);
  220. if ( $uploadedfile ) {
  221. $attachments[$allowed_s2[0]] = $uploadedfile;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. /**
  228. * If there are no errors and e-mail is required then build and send it.
  229. */
  230. if ( ($rows[0]->emailresults != 0) && !$error_found ) {
  231. /**
  232. * Clean the list of fields to be omitted from the results email
  233. */
  234. if ( trim($paramsvalues->omittedfields ) != '' ) {
  235. $omittedlist = explode(",", $paramsvalues->omittedfields);
  236. }
  237. $htmlstring = $rows[0]->html;
  238. /**
  239. * Find all the 'name's in the html-string and add to the $matches array
  240. */
  241. preg_match_all('/name=("|\').*?("|\')/i', $htmlstring, $matches);
  242. /**
  243. * clean the matches array
  244. */
  245. $names = array();
  246. foreach ( $matches[0] as $name ) {
  247. $name = preg_replace('/name=("|\')/i', '', $name);
  248. $name = preg_replace('/("|\')/', '', $name);
  249. $name = preg_replace('/name=("|\')/', '', $name);
  250. if ( strpos($name, '[]') ) {
  251. $name = str_replace('[]', '', $name);
  252. }
  253. $names[] = trim($name);
  254. }
  255. $names = array_unique($names);
  256. /**
  257. * Associate field values with names and implode arrays
  258. */
  259. $fields = array();
  260. foreach ( $names as $name ) {
  261. if ( is_array($_POST[$name])) {
  262. $fields[$name] = implode(", ", $_POST[$name]);
  263. } else {
  264. $fields[$name] = $_POST[$name];
  265. }
  266. }
  267. /**
  268. * Main E-mail type switch
  269. *
  270. * Case 2: Use 'my template'
  271. * Case 3: Not in use
  272. * Case 1 & default: use field titles
  273. */
  274. switch ($paramsvalues->email_type) {
  275. case 2:
  276. /**
  277. * Use 'my template'
  278. */
  279. if ( $debug ) { echo "Case 2: Use template<br />"; }
  280. $html_message = $rows[0]->emailtemplate;
  281. ob_start();
  282. eval( "?>".$html_message );
  283. $html_message = ob_get_clean();
  284. //ob_end_clean();
  285. foreach ( $fields as $name => $post) {
  286. $html_message = preg_replace("/\\{".$name."\\}/", $post, $html_message);
  287. }
  288. break;
  289. case 3:
  290. /**
  291. * Not in use
  292. $htmlstring2 = preg_replace('/(<input.*name=)(")(.*?)(")(.*>)/', '$3', $htmlstring);
  293. $html_message = preg_replace('/(<select.*?name=)(")(.*?)(")(.*?select>)/s',
  294. '$3', $htmlstring2);
  295. */
  296. if ( $debug ) { echo "Case 3: Under development<br />"; }
  297. $html_message = 'Still under development';
  298. break;
  299. case 1:
  300. default:
  301. /**
  302. * Use Field Titles and table layout (default)
  303. */
  304. if ( $debug ) { echo "Case 1: Use table layout<br />"; }
  305. $col_names = array();
  306. if ( !is_array($omittedlist) ) {
  307. $omittedlist = array();
  308. }
  309. $html_message = "<table cellpadding='0' cellspacing='0' border='0' width='100%'>";
  310. foreach ( $fields as $name => $post) {
  311. if ( in_array($name, $omittedlist) ) {
  312. continue;
  313. }
  314. /**
  315. * Substitute element titles for field names
  316. */
  317. if ( trim($titlesvalues->$name) ) {
  318. $name = $titlesvalues->$name;
  319. }
  320. $html_message .= "<tr height='10'>
  321. <td width='40%' class='tablecell1'>$name</td>
  322. <td width='60%' class='tablecell2'>$post</td>
  323. </tr>";
  324. }
  325. $html_message .= "</table>";
  326. break;
  327. }
  328. /**
  329. * Add IP address if required
  330. */
  331. if ( $paramsvalues->recip == "Yes" ) {
  332. $html_message .= "<br /><br />Submitted by ".$_SERVER['REMOTE_ADDR'];
  333. }
  334. /**
  335. * Wrap page code around the html message body
  336. */
  337. $html_message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
  338. <html>
  339. <head>
  340. <title></title>
  341. <base href=\"$mosConfig_live_site/\" />
  342. <style type=\"text/css\">
  343. ".strip_tags( $chronocontact_params->get('newsletter_css'))."
  344. </style>
  345. </head>
  346. <body>$html_message</body></html>";
  347. /**
  348. * Run the On-submit 'pre e-mail' code if there is any
  349. */
  350. if ( !empty($rows[0]->onsubmitcodeb4) ) {
  351. eval( "?>".$rows[0]->onsubmitcodeb4 );
  352. }
  353. /**
  354. * The send e-mail switch
  355. * Case 1: 'Yes' standard (not in use)
  356. * Case 2: 'Yes' custom (default)
  357. */
  358. switch ( $rows[0]->emailresults ) {
  359. /**
  360. * E-mail the results - 'Yes' standard
  361. * Not in use!
  362. */
  363. case 1:
  364. if ( $debug) { echo "E-mail: 'Yes' standard<br />"; }
  365. $from = $chronocontact_params->get('from_email');
  366. $fromname = $chronocontact_params->get('from_name');
  367. $subject = $chronocontact_params->get('subject');
  368. $recipient[] = $chronocontact_params->get('primary_email');
  369. break;
  370. case 2:
  371. default:
  372. /**
  373. * E-mail the results - 'Yes' custom
  374. */
  375. if ( $debug) { echo "E-mail: 'Yes' custom<br />"; }
  376. $from = $paramsvalues->from_email;
  377. $fromname = $paramsvalues->from_name;
  378. $subject = $rows[0]->emailsubject;
  379. $recipient = str_replace(" ","",$rows[0]->extraemail);
  380. $recipient = explode(",", $recipient);
  381. if(trim($paramsvalues->ccemail)){
  382. $ccemails = str_replace(" ","",$paramsvalues->ccemail);
  383. $ccemails = explode(",", $ccemails);
  384. }else{
  385. $ccemails = NULL;
  386. }
  387. if(trim($paramsvalues->bccemail)){
  388. $bccemails = str_replace(" ","",$paramsvalues->bccemail);
  389. $bccemails = explode(",", $bccemails);
  390. }else{
  391. $bccemails = NULL;
  392. }
  393. if(trim($paramsvalues->replyto_email)){
  394. $replyto_email = str_replace(" ","",$paramsvalues->replyto_email);
  395. $replyto_email = explode(",", $replyto_email);
  396. }else{
  397. $replyto_email = NULL;
  398. }
  399. if(trim($paramsvalues->replyto_name)){
  400. $replyto_name = str_replace(" ","",$paramsvalues->replyto_name);
  401. $replyto_name = explode(",", $replyto_name);
  402. }else{
  403. $replyto_name = NULL;
  404. }
  405. break;
  406. }
  407. //$replyto = $chronocontact_params->get('replyto_email');
  408. /**
  409. * Substitute field values if they are set
  410. */
  411. if ( trim($paramsvalues->subjectfield) != "" ) {
  412. $subject = $_POST[$paramsvalues->subjectfield];
  413. }
  414. if ( trim($paramsvalues->fromemailfield) != "" ) {
  415. $from = $_POST[$paramsvalues->fromemailfield];
  416. }
  417. if ( trim($paramsvalues->fromnamefield) != "" ) {
  418. $fromname = $_POST[$paramsvalues->fromnamefield];
  419. }
  420. if ( trim($paramsvalues->emailfield) != "" ) {
  421. $recipient[] = $_POST[$paramsvalues->emailfield];
  422. }
  423. if ( trim($paramsvalues->ccfield) != "" ) {
  424. $ccemails[] = $_POST[$paramsvalues->ccfield];
  425. }
  426. if ( trim($paramsvalues->bccfield) != "" ) {
  427. $bccemails[] = $_POST[$paramsvalues->bccfield];
  428. }
  429. /**
  430. * Send the email(s)
  431. */
  432. $email_sent = mosMail($from, $fromname, $recipient, $subject, $html_message, true,
  433. $ccemails, $bccemails, $attachments, $replyto_email, $replyto_name );
  434. if ( $debug ) {
  435. if ($email_sent)echo "Email sent ";
  436. if (!$email_sent)echo "Email not sent ";
  437. }
  438. // :: HACK :: insert debug
  439. if ( $debug ) {
  440. echo "<h4>E-mail message</h4>
  441. <div style='border:1px solid black; padding:6px;margin:6px;'>
  442. <p>From: $fromname [$from]<br />
  443. To: ".implode($recipient,', ')."<br />
  444. Subject: $subject</p>
  445. $html_message<br /></div>";
  446. }
  447. // :: end hack ::
  448. }
  449. if ( !$error_found ) {
  450. /**
  451. * Run the On-submit 'post e-mail' code if there is any
  452. */
  453. if ( !empty($rows[0]->onsubmitcode) ) {
  454. eval( "?>".$rows[0]->onsubmitcode );
  455. }
  456. /**
  457. * Run the SQL query if there is one
  458. */
  459. if ( !empty($rows[0]->autogenerated) ) {
  460. eval( "?>".$rows[0]->autogenerated );
  461. }
  462. /**
  463. * Redirect the page if requested
  464. */
  465. if ( !empty($rows[0]->redirecturl) ) {
  466. mosRedirect($rows[0]->redirecturl);
  467. }
  468. }
  469. }
  470. /**
  471. * Handle uploaded files
  472. *
  473. * @param unknown_type $uploadedfile
  474. * @param string $filename
  475. * @param string $limits
  476. * @param string $directory
  477. * @return unknown
  478. */
  479. function handle_uploaded_files($uploadedfile, $filename, $limits = TRUE, $directory = FALSE)
  480. {
  481. global $mosConfig_absolute_path, $mosConfig_fileperms;
  482. if ( strlen($mosConfig_fileperms) > 0 ) {
  483. $fileperms = octdec($mosConfig_fileperms);
  484. }
  485. $uploaded_files = "";
  486. $upload_path = $mosConfig_absolute_path.'/components/com_chronocontact/upload/';
  487. if ( is_file($uploadedfile) ) {
  488. $targetfile = $upload_path.$filename;
  489. while ( file_exists($targetfile) ) {
  490. $targetfile = $upload_path.rand(1,1000).'_'.$filename;
  491. }
  492. move_uploaded_file($uploadedfile, $targetfile);
  493. if ( strlen($fileperms) > 0 ) {
  494. chmod($targetfile, $fileperms);
  495. }
  496. $uploaded_files = $targetfile;
  497. }
  498. return $uploaded_files;
  499. }
  500. /**
  501. * Display JavaScript alert box as error message
  502. *
  503. * @param string $message
  504. */
  505. function showErrorMessage($message) {
  506. echo "<script> alert('$message'); </script>\n";
  507. }
  508. ?>