PageRenderTime 62ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/site/components/com_chronocontact/libraries/chronoform.php

https://bitbucket.org/manchas/jrobotz
PHP | 544 lines | 438 code | 30 blank | 76 comment | 79 complexity | 46db89d54b9f819189c423e4592a2336 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /*
  3. /**
  4. * CHRONOFORMS version 3.0
  5. * Copyright (c) 2008 Chrono_Man, ChronoEngine.com. All rights reserved.
  6. * Author: Chrono_Man (ChronoEngine.com)
  7. * @license GNU/GPL
  8. * See readme.html.
  9. * Visit http://www.ChronoEngine.com for regular update and information.
  10. **/
  11. defined('_JEXEC') or die('Restricted access');
  12. class CFChronoForm extends JObject{
  13. var $formrow = NULL;
  14. var $formparams = NULL;
  15. var $formerrors = '';
  16. var $formdebug = '';
  17. var $error_found = false;
  18. var $stoprunning = false;
  19. var $haltFunction = array('emails' => false, 'uploads' => false, 'plugins_before_email' => false, 'plugins_after_email' => false, 'autogenerated_before_email' => false, 'autogenerated_after_email' => false, 'onsubmitcode' => false, 'onsubmitcodeb4' => false);
  20. var $tablerow = array();
  21. var $posted = array();
  22. var $pagetype = 'chronocontact';
  23. var $formname = '';
  24. var $stoploading = false;
  25. var $submission_complete = false;
  26. function __construct($formname = ''){
  27. if (!empty($formname)) {
  28. $this->getForm(trim($formname));
  29. }
  30. else
  31. {
  32. //todo: need to load default table properties
  33. //initialise
  34. $this->getForm(trim($formname));
  35. }
  36. }
  37. function &getInstance($formname = ''){
  38. static $instances;
  39. global $mainframe;
  40. if (!isset ($instances)) {
  41. $instances = array ();
  42. }
  43. if (empty($formname)) {
  44. if(JRequest::getVar('chronoformname')){
  45. JRequest::setVar('chronoformname', preg_replace('/[^A-Za-z0-9_]/', '', JRequest::getVar('chronoformname')));
  46. }
  47. $formname = JRequest::getVar('chronoformname');
  48. if ( !$formname ) {
  49. $params =& $mainframe->getPageParameters('com_chronocontact');
  50. $formname = $params->get('formname');
  51. }
  52. }
  53. if(empty($instances[trim($formname)])){
  54. $instances[trim($formname)] = new CFChronoForm($formname);
  55. return $instances[trim($formname)];
  56. }else{
  57. //$instances = array (new CFChronoForm($formname));
  58. return $instances[trim($formname)];
  59. }
  60. }
  61. function getForm( $formname )
  62. {
  63. global $mainframe;
  64. $database =& JFactory::getDBO();
  65. if(!trim($formname)){
  66. if(JRequest::getVar('chronoformname')){
  67. JRequest::setVar('chronoformname', preg_replace('/[^A-Za-z0-9_]/', '', JRequest::getVar('chronoformname')));
  68. }
  69. $formname = JRequest::getVar('chronoformname');
  70. if ( !$formname ) {
  71. $params =& $mainframe->getPageParameters('com_chronocontact');
  72. $formname = $params->get('formname');
  73. }
  74. }
  75. $query = "SELECT * FROM `#__chrono_contact` WHERE `name` = '".$formname."'";
  76. $database->setQuery( $query );
  77. $cf_rows = $database->loadObjectList();
  78. if(count($cf_rows)){
  79. $this->formrow = $cf_rows[0];
  80. $this->formname = "ChronoContact_".$this->formrow->name;
  81. //load titles
  82. $registry = new JRegistry();
  83. $registry->loadINI( $cf_rows[0]->titlesall );
  84. $titlesvalues = $registry->toObject( );
  85. //load params
  86. $paramsvalues = new JParameter($this->formrow->paramsall);
  87. $this->formparams = $paramsvalues;
  88. return true;
  89. }else{
  90. $emptyForm = new StdClass();
  91. $emptyForm->id = 0;
  92. $emptyForm->name = '';
  93. $this->formrow = $emptyForm;
  94. $paramsvalues = new JParameter('');
  95. $this->formparams = $paramsvalues;
  96. return false;
  97. }
  98. }
  99. function formparams($key, $default = '')
  100. {
  101. //$paramsvalues = new JParameter($this->formrow->paramsall);
  102. return $this->formparams->get($key, $default);
  103. }
  104. function setFormParam( $param, $value )
  105. {
  106. global $mainframe;
  107. $database =& JFactory::getDBO();
  108. $this->formparams->set($param, $value);
  109. }
  110. function setFormData( $key, $value )
  111. {
  112. global $mainframe;
  113. $database =& JFactory::getDBO();
  114. $this->formrow->$key = $value;
  115. }
  116. function getFormName( $formid )
  117. {
  118. global $mainframe;
  119. $database =& JFactory::getDBO();
  120. $query = "SELECT * FROM `#__chrono_contact` WHERE `id` = '".$formid."'";
  121. $database->setQuery( $query );
  122. $cf_rows = $database->loadObjectList();
  123. if(count($cf_rows)){
  124. return $cf_rows[0]->name;
  125. }else{
  126. return false;
  127. }
  128. }
  129. function setFormName( $newformname )
  130. {
  131. $this->formname = $newformname;
  132. }
  133. function addErrorMsg( $text )
  134. {
  135. $this->formerrors = $this->formerrors.'<li>'.$text.'</li>';
  136. }
  137. function addDebugMsg( $text )
  138. {
  139. $this->formdebug = $this->formdebug.'<li>'.$text.'</li>';
  140. }
  141. function showForm($formname, $posted = array(), $runplugins = true)
  142. {
  143. $MyForm =& CFChronoForm::getInstance($formname);
  144. if(!$MyForm->formrow->id){
  145. echo "There is no form with this name";
  146. return;
  147. }
  148. $session =& JFactory::getSession();
  149. //if(!trim($session->get('cfreturnurl_'.$formname, '', md5('chrono')))){
  150. $session->clear('chrono_form_errors_'.$formname, md5('chrono'));
  151. $session->clear('chrono_form_data_'.$formname, md5('chrono'));
  152. $session->clear('cfreturnurl_'.$formname, md5('chrono'));
  153. //}
  154. if( trim($MyForm->formparams('enmambots')) == 'Yes'){
  155. $MyForm->runMambots($MyForm->formrow->name);
  156. }
  157. if($runplugins){
  158. $MyPlugins =& CFPlugins::getInstance($MyForm->formrow->id);
  159. $MyPlugins->runPlugin('', array('ONLOAD', 'ONLOADONSUBMIT'));
  160. }
  161. if(!$MyForm->stoploading){
  162. HTML_ChronoContact::showform( $MyForm->formrow, $posted);
  163. }
  164. }
  165. function runMambots($formname){
  166. global $mainframe;
  167. $MyForm =& CFChronoForm::getInstance($formname);
  168. $params =& $mainframe->getParams('com_content');
  169. $dispatcher =& JDispatcher::getInstance();
  170. $type = 'content';
  171. JPluginHelper::importPlugin($type);
  172. $rowmam->text = $MyForm->formrow->html;
  173. $results_mambots = $mainframe->triggerEvent( 'onPrepareContent', array (&$rowmam, & $params, 0 ));
  174. $MyForm->formrow->html = $rowmam->text;
  175. }
  176. function getAction($formname){
  177. global $mainframe;
  178. $CF_PATH = ($mainframe->isSite()) ? JURI::Base() : $mainframe->getSiteURL();
  179. $MyForm =& CFChronoForm::getInstance($formname);
  180. if(!empty($MyForm->formrow->submiturl)){
  181. $actionurl = $MyForm->formrow->submiturl;
  182. } else {
  183. $actionurl = $CF_PATH.'index.php?option=com_chronocontact&task=send&chronoformname='.$MyForm->formrow->name;
  184. if((int)JRequest::getVar('Itemid')){
  185. $actionurl = $actionurl.'&Itemid='.JRequest::getVar('Itemid');
  186. }
  187. }
  188. return $actionurl;
  189. }
  190. function selfURL() {
  191. $uri =& JURI::getInstance();
  192. $inbetween = '';
  193. if($uri->getQuery())$inbetween = '?';
  194. return $uri->current().$inbetween.$uri->getQuery();
  195. }
  196. function checkSubmissionsLimit($formname)
  197. {
  198. global $mainframe;
  199. $session =& JFactory::getSession();
  200. $MyForm =& CFChronoForm::getInstance($formname);
  201. if(trim($MyForm->formparams('submissions_limit'))){
  202. if(!$session->get('chrono_submissions_limit_'.$formname, '', md5('chrono'))){
  203. $session->set("chrono_submissions_limit_".$formname, mktime(date("H"), date("i"), date("s"), date("m") , date("d")+1, date("Y")), md5('chrono'));
  204. }else{
  205. if(($session->get('chrono_submissions_limit_'.$formname, '', md5('chrono')) + ((int)trim($MyForm->formparams('submissions_limit')))) > mktime(date("H"), date("i"), date("s"), date("m") , date("d")+1, date("Y"))){
  206. $MyForm->addErrorMsg($MyForm->formparams('submissions_limit_error', 'Sorry but you can not submit the form again very soon like this!'));
  207. }else{
  208. $session->set("chrono_submissions_limit_".$formname, mktime(date("H"), date("i"), date("s"), date("m") , date("d")+1, date("Y")), md5('chrono'));
  209. }
  210. }
  211. }
  212. }
  213. function resetSubmissionsLimit($formname)
  214. {
  215. global $mainframe;
  216. $session =& JFactory::getSession();
  217. $MyForm =& CFChronoForm::getInstance($formname);
  218. $session->set("chrono_submissions_limit_".$formname, '', md5('chrono'));
  219. }
  220. function checkImageVerification($formname)
  221. {
  222. global $mainframe;
  223. $session =& JFactory::getSession();
  224. $MyForm =& CFChronoForm::getInstance($formname);
  225. if ( trim($MyForm->formparams('imagever')) == 'Yes' ) {
  226. $sessionvar = $session->get('chrono_verification', '', md5('chrono'));
  227. $chrono_verification = strtolower(JRequest::getVar('chrono_verification'));
  228. if ( md5($chrono_verification ) != $sessionvar ) {
  229. $MyForm->addErrorMsg($MyForm->formparams('imgver_error_msg', 'You have entered an incorrect verification code at the bottom of the form.'));
  230. }else{
  231. $session->clear('chrono_verification', md5('chrono'));
  232. //$session->clear('chrono_verification_msg');
  233. }
  234. }
  235. }
  236. function checkServerValidation($formname)
  237. {
  238. global $mainframe;
  239. $session =& JFactory::getSession();
  240. $MyForm =& CFChronoForm::getInstance($formname);
  241. if ( trim($MyForm->formparams('servervalidate')) == 'Yes' ) {
  242. if ($returnval = eval( "?>".$MyForm->formrow->server_validation )){
  243. $MyForm->resetSubmissionsLimit($formname);
  244. $MyForm->addErrorMsg($returnval);
  245. }
  246. }
  247. }
  248. function showFormErrors($formname)
  249. {
  250. global $mainframe;
  251. $session =& JFactory::getSession();
  252. $posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
  253. $MyForm =& CFChronoForm::getInstance($formname);
  254. if($MyForm->formerrors){
  255. if($session->get('cfreturnurl_'.$formname, '', md5('chrono'))){
  256. $session->set("chrono_form_errors_".$formname, $MyForm->formerrors, md5('chrono'));
  257. $session->set("chrono_form_data_".$formname, $MyForm->posted, md5('chrono'));
  258. //$mainframe->redirect(str_replace('&cfshowerrors=1', '', JRequest::getVar('cfreturnurl')).'&cfshowerrors=1');
  259. $mainframe->redirect($session->get('cfreturnurl_'.$formname, '', md5('chrono')));
  260. }
  261. //$MyForm->showForm($MyForm->formrow->name, $posted);
  262. return true;
  263. }else{
  264. //$session->clear('chrono_form_errors_'.$formname, md5('chrono'));
  265. //$session->clear('chrono_form_data_'.$formname, md5('chrono'));
  266. }
  267. return false;
  268. }
  269. function generateCFToken($formname)
  270. {
  271. global $mainframe;
  272. $session =& JFactory::getSession();
  273. $MyForm =& CFChronoForm::getInstance($formname);
  274. $secret = $MyForm->formparams('secret', 'ILIKECHRONOFORMS');
  275. /*if($session->get('chrono_secret_check_'.$formname, '', md5('chrono'))){
  276. return md5(trim($secret).$session->get('chrono_secret_check_'.$formname, '', md5('chrono'));
  277. }else{
  278. //srand((double)microtime()*10000);
  279. //$inum = substr(base64_encode(md5(rand())), 0, 16);
  280. //$session->set('chrono_secret_check_'.$formname, $inum, md5('chrono'));
  281. return md5(trim($secret).$session->getId());
  282. }*/
  283. return md5(trim($secret).$session->getId());
  284. }
  285. function checkCFToken($formname)
  286. {
  287. global $mainframe;
  288. $session =& JFactory::getSession();
  289. $MyForm =& CFChronoForm::getInstance($formname);
  290. $secret = $MyForm->formparams('secret', 'ILIKECHRONOFORMS');
  291. /*if($session->get('chrono_secret_check_'.$formname, '', md5('chrono'))){
  292. if(md5(trim($secret).$session->getId() == JRequest::getVar('1cf1'))){
  293. return true;
  294. }else{
  295. return false;
  296. }
  297. }else{
  298. return false;
  299. }*/
  300. if(md5(trim($secret).$session->getId() == JRequest::getVar('1cf1'))){
  301. return true;
  302. }else{
  303. return false;
  304. }
  305. }
  306. function handleArrays($formname){
  307. global $mainframe;
  308. $posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
  309. $MyForm =& CFChronoForm::getInstance($formname);
  310. /**
  311. * Associate field values with names and implode arrays
  312. */
  313. $fields = array();
  314. $names = explode(",", str_replace("[]", "" , $MyForm->formrow->fieldsnames));
  315. foreach ( $posted as $name => $post ) {
  316. if($MyForm->formparams('handlepostedarrays', 'Yes') == 'Yes'){
  317. if(isset($post)){
  318. if ( is_array($post)) {
  319. $fields[$name] = implode(", ", $post);
  320. JRequest::setVar($name, implode(", ", $post));
  321. } else {
  322. $fields[$name] = $post;
  323. }
  324. }else{
  325. $fields[$name] = '';
  326. }
  327. }else{
  328. $fields[$name] = $post;
  329. }
  330. }
  331. return $fields;
  332. }
  333. function submitForm($formname, $posted = array(), $useCurrent = false)
  334. {
  335. global $mainframe;
  336. $database =& JFactory::getDBO();
  337. if(empty($posted)){
  338. $posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
  339. }
  340. if($useCurrent){
  341. $MyForm = $this->getInstance($formname);
  342. }else{
  343. $MyForm =& CFChronoForm::getInstance($formname);
  344. }
  345. $MyFormEmails =& CFEMails::getInstance($MyForm->formrow->id);
  346. $MyCustomCode =& CFCustomCode::getInstance($MyForm->formrow->id);
  347. $MyPlugins =& CFPlugins::getInstance($MyForm->formrow->id);
  348. $MyForm->posted = $posted;
  349. $debug = $MyForm->formparams('debug');
  350. $MyForm->addDebugMsg('Form passed first SPAM check OK');
  351. $session =& JFactory::getSession();
  352. // Check how soon was the last submission
  353. $MyForm->checkSubmissionsLimit($MyForm->formrow->name);
  354. $MyForm->addDebugMsg('Form passed the submissions limit (if enabled) OK');
  355. /**
  356. * If imageversification is on check the code
  357. */
  358. $MyForm->checkImageVerification($MyForm->formrow->name);
  359. $MyForm->addDebugMsg('Form passed the Image verification (if enabled) OK');
  360. //Server side validation
  361. $MyForm->checkServerValidation($MyForm->formrow->name);
  362. $MyForm->addDebugMsg('Form passed the server side validation (if enabled) OK');
  363. //if any check steps failed, quit and reshow the form
  364. if($MyForm->showFormErrors($MyForm->formrow->name)){
  365. $MyForm->showForm($MyForm->formrow->name, $posted);
  366. return;
  367. }
  368. /**
  369. * if $debug is true then ChronoForms will show diagnostic output
  370. */
  371. $MyForm->addDebugMsg("\$_POST Array: ".print_r($posted, true));
  372. $MyForm->addDebugMsg("\$_FILES Array: ".print_r($_FILES, true));
  373. /**
  374. * Upload attachments
  375. */
  376. if(!$MyForm->haltFunction["uploads"]){
  377. $MyUploads =& CFUploads::getInstance($MyForm->formrow->id);
  378. $MyUploads->handleUploads();
  379. //show errors if any
  380. if($MyForm->showFormErrors($MyForm->formrow->name)){
  381. $MyForm->showForm($MyForm->formrow->name, $posted);
  382. return;
  383. }
  384. }
  385. /* Do Onsubmit before_email plugins*/
  386. if(!$MyForm->haltFunction["plugins_before_email"]){
  387. $MyPlugins->runPlugin('before_email');
  388. $MyForm->addDebugMsg('Form passed the plugins step (if enabled) OK');
  389. //show errors if any
  390. if($MyForm->showFormErrors($MyForm->formrow->name)){
  391. $MyForm->showForm($MyForm->formrow->name, $posted);
  392. return;
  393. }
  394. }
  395. //handle arrays
  396. $MyForm->handleArrays($MyForm->formrow->name);
  397. /**
  398. * If there are no errors and e-mail is required then build and send it.
  399. */
  400. if ( ($MyForm->formrow->emailresults) && !$MyForm->error_found && !$MyForm->stoprunning ) {
  401. //run before submit code
  402. if(!$MyForm->haltFunction["onsubmitcodeb4"]){
  403. $MyCustomCode->runCode( 'onsubmitcodeb4' );
  404. if($MyForm->showFormErrors($MyForm->formrow->name)){
  405. $MyForm->showForm($MyForm->formrow->name, $posted);
  406. return;
  407. }
  408. }
  409. if(!$MyForm->haltFunction["autogenerated_before_email"]){
  410. $MyCustomCode->runCode( 'autogenerated', 'before_email' );
  411. }
  412. //send emails
  413. if(!$MyForm->haltFunction["emails"]){
  414. $emails_result = $MyFormEmails->sendEmails($MyForm, $MyFormEmails->emails);
  415. }
  416. }
  417. if ( !$MyForm->error_found && !$MyForm->stoprunning ) {
  418. /*************** check to see if order was specified, if not then use the default old one ************************/
  419. if((!$MyForm->formparams('plugins_order'))&&(!$MyForm->formparams('onsubmitcode_order'))&&(!$MyForm->formparams('autogenerated_order'))){
  420. $MyForm->setFormParam('autogenerated_order', 3);
  421. $MyForm->setFormParam('onsubmitcode_order', 2);
  422. $MyForm->setFormParam('plugins_order', 1);
  423. }
  424. for($ixx = 1 ; $ixx <= 3; $ixx++){
  425. if($MyForm->formparams('plugins_order') == $ixx){
  426. if(!$MyForm->haltFunction["plugins_after_email"]){
  427. $MyPlugins->runPlugin('after_email');
  428. //show errors if any
  429. if($MyForm->showFormErrors($MyForm->formrow->name)){
  430. $MyForm->showForm($MyForm->formrow->name, $posted);
  431. return;
  432. }
  433. }
  434. }
  435. /**
  436. * Run the On-submit 'post e-mail' code if there is any
  437. */
  438. if($MyForm->formparams('onsubmitcode_order') == $ixx){
  439. if(!$MyForm->haltFunction["onsubmitcode"]){
  440. $MyCustomCode->runCode( 'onsubmitcode' );
  441. if($MyForm->showFormErrors($MyForm->formrow->name)){
  442. $MyForm->showForm($MyForm->formrow->name, $posted);
  443. return;
  444. }
  445. }
  446. }
  447. /**
  448. * Run the SQL query if there is one
  449. */
  450. if($MyForm->formparams('autogenerated_order') == $ixx){
  451. if(!$MyForm->haltFunction["autogenerated_after_email"]){
  452. $MyCustomCode->runCode( 'autogenerated', 'after_email' );
  453. }
  454. }
  455. }
  456. //Mark submission as complete!
  457. $MyForm->submission_complete = true;
  458. $MyForm->addDebugMsg('Debug End');
  459. /**
  460. * Redirect the page if requested
  461. */
  462. if ( !empty($MyForm->formrow->redirecturl) ) {
  463. if ( !$debug ) {
  464. $mainframe->redirect($MyForm->formrow->redirecturl);
  465. } else {
  466. $MyForm->addDebugMsg("<div class='debug' >Redirect link set, click to test:<br /><a href='".$MyForm->formrow->redirecturl."'>".$MyForm->formrow->redirecturl."</a></div>");
  467. }
  468. }
  469. }
  470. if((!empty($MyForm->formdebug))&&($MyForm->formparams('debug') == '1')){
  471. include_once(JPATH_COMPONENT.DS.'libraries'.DS.'includes'.DS.'Debug.php');
  472. }
  473. }
  474. function doExtra($formname, $extraid = 1, $posted = array())
  475. {
  476. global $mainframe;
  477. $database =& JFactory::getDBO();
  478. if(empty($posted)){
  479. $posted = JRequest::get( 'post' , JREQUEST_ALLOWRAW );
  480. }
  481. if(empty($extraid)){
  482. $extraid = 1;
  483. }
  484. if(((int)$extraid == 0)||($extraid > 5)){
  485. return false;
  486. }
  487. $MyForm =& CFChronoForm::getInstance($formname);
  488. if ( !empty($MyForm->formrow->{"extra".$extraid}) ){
  489. eval( "?>".$MyForm->formrow->{"extra".$extraid} );
  490. }else{
  491. return false;
  492. }
  493. }
  494. function addhash()
  495. {
  496. global $mainframe;
  497. $database =& JFactory::getDBO();
  498. $query = "SELECT * FROM `#__components` WHERE `option` = 'com_chronocontact' AND parent='0' AND admin_menu_link='option=com_chronocontact'";
  499. $database->setQuery( $query );
  500. $result = $database->loadObject();
  501. //$configs = JComponentHelper::getParams('com_chronocontact');
  502. $configs = new JParameter($result->params);
  503. if($configs->get('licensevalid', 0)){
  504. return '';
  505. }else{
  506. return base64_decode('PCEtLSBkb24ndCByZW1vdmUgdGhlIGZvbGxvd2luZyAzIGxpbmVzIGlmIHlvdSBkaWRuJ3QgYnV5IGEgc3Vic2NyaXB0aW9uIC0tPiANCjxkaXYgY2xhc3M9ImNocm9ub2Zvcm0iPg0KPGEgaHJlZj0iaHR0cDovL3d3dy5jaHJvbm9lbmdpbmUuY29tIj5Qb3dlcmVkIEJ5IENocm9ub0Zvcm1zIC0gQ2hyb25vRW5naW5lLmNvbTwvYT4NCg0KPC9kaXY+DQo8IS0tIGRvbid0IHJlbW92ZSB0aGUgMyBsaW5lcyBhYm92ZSBpZiB5b3UgZGlkbid0IGJ1eSBhIHN1YnNjcmlwdGlvbiAtLT4=');
  507. }
  508. }
  509. function cfskipregex($regex){
  510. $reserved = array('[', ']');
  511. $replace = array('\[', '\]');
  512. return str_replace($reserved, $replace, $regex);
  513. }
  514. }