PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/src/site/components/com_chronocontact/plugins/cf_redirect.php

https://bitbucket.org/manchas/jrobotz
PHP | 294 lines | 223 code | 37 blank | 34 comment | 18 complexity | 9ee5dd9379c40f638314d351a81b0baa MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. /**
  3. * Copyright (c) 2009 Bob Janes
  4. * Author: Bob Janes
  5. * License : GPL
  6. * Visit http://www.ChronoEngine.com for regular update and information.
  7. **/
  8. defined('_JEXEC') or die('Restricted access');
  9. global $mainframe;
  10. require_once( $mainframe->getPath( 'class', 'com_chronocontact' ) );
  11. // the class name must be the same as the file name without the .php at the end
  12. class cf_redirect
  13. {
  14. //the next 3 fields must be defined for every plugin
  15. var $result_TITLE = "Redirect";
  16. var $result_TOOLTIP = "Submit form data to another URL using the Redirect method.
  17. Use this plugin to submit data to another website e.g. PayPal
  18. that you want the user to visit.";
  19. var $plugin_name = "cf_redirect"; // must be the same as the class name
  20. var $event = "ONSUBMIT"; // must be defined and in Uppercase, should be ONSUBMIT or ONLOAD
  21. // the next function must exist and will have the backend config code
  22. function show_conf($row, $id, $form_id, $option)
  23. {
  24. global $mainframe;
  25. require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_chronocontact'.DS.'helpers'.DS.'plugin.php');
  26. $helper = new ChronoContactHelperPlugin();
  27. $query = "
  28. SELECT *
  29. FROM `#__chrono_contact`
  30. WHERE id = ".$db->Quote($form_id) ;
  31. $db->setQuery($query);
  32. $form = $db->loadObject();
  33. $htmlstring = $form->html;
  34. preg_match_all('/name=("|\').*?("|\')/i', $htmlstring, $matches);
  35. $names = array();
  36. foreach ( $matches[0] as $name ) {
  37. $name = preg_replace('/name=("|\')/i', '', $name);
  38. $name = preg_replace('/("|\')/', '', $name);
  39. $name = preg_replace('/name=("|\')/', '', $name);
  40. if ( strpos($name, '[]') ) {
  41. $name = str_replace('[]', '', $name);
  42. }
  43. $names[] = trim($name);
  44. }
  45. $names = array_unique($names);
  46. // identify and initialise the parameters used in this plugin
  47. $params_array = array(
  48. 'debugging' => '0',
  49. 'target_url' => 'http://',
  50. 'onsubmit' => 'before_email');
  51. $params = $helper->loadParams($row, $params_array);
  52. $messages[] = '$params: '.print_r($params, true);
  53. if ( $params->get('debugging') ) {
  54. $helper->showPluginDebugMessages($messages);
  55. }
  56. ?>
  57. <form action="index2.php" method="post" name="adminForm" id="adminForm" class="adminForm" >
  58. <?php
  59. echo $pane->startPane("cf_redirect");
  60. echo $pane->startPanel( 'General', "general" );
  61. ?>
  62. <table border="0" cellpadding="3" cellspacing="0" class='cf_table' >
  63. <?php
  64. $input = $helper->createHeaderTD('Field names from your form', '', true, $attribs['header']);
  65. echo $helper->wrapTR($input);
  66. foreach ( $names as $name ) {
  67. $tooltip = "Enter the other site field name that matches '$name'";
  68. $input = $helper->createInputTD("'$name' field",
  69. "extra2[$name]", $extra2->get($name), '', $attribs['input'], $tooltip);
  70. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  71. }
  72. $input = $helper->createHeaderTD('Extra field values to send', '',
  73. true, array('colspan' => '4', 'class' => 'cf_header'));
  74. echo $helper->wrapTR($input);
  75. $tooltip = "Extra Fields, enter data in this format : ship_to_name=field_name<br />Take care to add each entry to a new line";
  76. $input = $helper->createTextareaTD('Extra fields Data', 'extra1', $row->extra1, $attribs['textarea'], $tooltip );
  77. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  78. ?>
  79. </table>
  80. <?php
  81. echo $pane->endPanel();
  82. echo $pane->startPanel( "URL parameters", 'url_params' );
  83. ?>
  84. <table border="0" cellpadding="3" cellspacing="0" class='cf_table' >
  85. <?php
  86. $input = $helper->createHeaderTD('Plugin parameters', '',
  87. true, array('colspan' => '4', 'class' => 'cf_header'));
  88. echo $helper->wrapTR($input);
  89. $tooltip = "The target URL to redirect to";
  90. $input = $helper->createInputTD("Target URL",
  91. "params[target_url]", $params->get('target_url'), '', $attribs['input'], $tooltip);
  92. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  93. $option_array = array('before_email' => 'Before Email', 'after_email' => 'After Email' );
  94. foreach ( $option_array as $k => $v ) {
  95. $option_array[$k] = JHTML::_('select.option', $k, JText::_($v));
  96. }
  97. $tooltip = "Run the plugin before or after the email.<br />
  98. Running it before the email may be necessary to include some data into the email";
  99. $input = $helper->createSelectTD("Flow control", "params[onsubmit]",
  100. $option_array, $params->get('onsubmit'), $attribs['select'], $tooltip );
  101. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  102. $tooltip = "Show debug information on Submit?";
  103. $input = $helper->createYesNoTD("Debugging", "params[debugging]", '',
  104. $params->get('debugging'), '', $tooltip);
  105. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  106. ?>
  107. </table>
  108. <?php
  109. echo $pane->endPanel();
  110. echo $pane->startPanel( "Extra code", 'extracode' );
  111. ?>
  112. <table border="0" cellpadding="3" cellspacing="0" class='cf_table' >
  113. <?php
  114. $input = $helper->createHeaderTD('Extra code', '',
  115. true, array('colspan' => '4', 'class' => 'cf_header'));
  116. echo $helper->wrapTR($input);
  117. $tooltip = "Execute some code just before the Redirect transaction is executed";
  118. $input = $helper->createTextareaTD('Extra before CURL code', 'extra4',
  119. $row->extra4, $attribs['textarea'], $tooltip );
  120. echo $helper->wrapTR($input, array('class' => 'cf_config'));
  121. /*$tooltip = "Execute some code just after the CURL transaction is executed";
  122. $input = $helper->createTextareaTD('Extra after CURL code', 'extra5',
  123. $row->extra5, $attribs['textarea'], $tooltip );
  124. echo $helper->wrapTR($input, array('class' => 'cf_config'));*/
  125. ?>
  126. </table>
  127. <?php
  128. echo $pane->endPanel();
  129. echo $pane->startPanel( 'Help', 'help' );
  130. ?>
  131. <table border="0" cellpadding="3" cellspacing="0" class='cf_table' >
  132. <?php
  133. $input = $helper->createHeaderTD('How to configure the Redirect plugin', '',
  134. true, array('colspan' => '4', 'class' => 'cf_header'));
  135. echo $helper->wrapTR($input);
  136. ?>
  137. <tr>
  138. <td colspan='4' style='border:1px solid silver; padding:6px;'>
  139. <div>The plugin allows you to use the Redirect URL to send data and the user from a ChronoForms Form to
  140. another site or to another application on your site. Use this when you need to redirect the user
  141. for example to PayPal to authorise a payment, if you just want to transfer data from the form and keep teh user on
  142. your site, then the CURL plugin is better.</div>
  143. <ul><li>First, open the URL params tab and put the url that you want to submit the information to
  144. in the Target URL box e.g. http://www.paypal.com/cgi-bin/webscr </li>
  145. <li>Next click the General Tab and you will see a list of the fields in your form.
  146. Put the corresponding names for the site you are sending to in the boxes along-side
  147. (often these will be the same as the field names in your form.)</li>
  148. <li>In the Extra Fields Data box you can enter values for fields that will be the same
  149. for each submission. These will often be client or transaction identifiers for the other site.
  150. (Note: Putting this information here means that it is never exposed in your form.)</li>
  151. <li>Lastly on the Redirect params tab, you can set the Redirect Plugin to run before or after Emails are sent.
  152. Use this if you need to fine-tune the process flow. Note that the Redirection will be done after any emails are
  153. sent and the data saved.</li>
  154. <li>The Extra Code tab allows you to add extra PHP to run before the Redirection Plugin
  155. (running code after redirection is not possible).
  156. Normally you will leave these boxes empty; use them if you need to alter the submitted data in some way.</li>
  157. </ul>
  158. </td>
  159. </tr>
  160. </table>
  161. <?php
  162. echo $pane->endPane();
  163. echo $pane->endPane();
  164. $hidden_array = array (
  165. 'id' => $id,
  166. 'form_id' => $form_id,
  167. 'name' => $this->plugin_name,
  168. 'event' => $this->event,
  169. 'option' => $option,
  170. 'task' => 'save_conf');
  171. $hidden_array['params[onsubmit]'] = 'before_email';
  172. echo $helper->createHiddenArray( $hidden_array );
  173. ?>
  174. </form>
  175. <?php
  176. if ( $style ) $doc->addStyleDeclaration($style);
  177. if ( $script ) $doc->addScriptDeclaration($script);
  178. }
  179. // this function must exist and may not be changed unless you need to customize something
  180. function save_conf( $option )
  181. {
  182. global $mainframe;
  183. // turn extras2 into params list
  184. //$mainframe->enqueuemessage('$_POST: '.print_r($_POST, true));
  185. //$extra1 = JRequest::getVar( 'extra1', '', 'post', 'array' );
  186. //$mainframe->enqueuemessage('Extra1: '.print_r($extra1, true));
  187. //$extra2 = JRequest::getVar( 'extra2', '', 'post', 'array' );
  188. //$mainframe->enqueuemessage('Extra2: '.print_r($extra2, true));
  189. //$mainframe->enqueuemessage(print_r($row->extra2, true));
  190. require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_chronocontact'
  191. .DS.'helpers'.DS.'plugin.php');
  192. $helper = new ChronoContactHelperPlugin();
  193. $helper->save_conf($option);
  194. }
  195. /**
  196. * The function that will be executed when the form is submitted
  197. *
  198. */
  199. function onsubmit( $option, $params, $row )
  200. {
  201. global $mainframe;
  202. require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_chronocontact'
  203. .DS.'helpers'.DS.'plugin.php');
  204. $helper = new ChronoContactHelperPlugin();
  205. $doc =& JFactory::getDocument();
  206. $doc->addStyleDeclaration("div.debug {border:1px solid red; padding:3px; margin-bottom:3px;}");
  207. $mainframe->enqueuemessage('$_POST: '.print_r($_POST, true));
  208. $messages = array();
  209. /*********do the before onsubmit code**********/
  210. if ( !empty($row->extra4) ) {
  211. eval( "?>".$row->extra4 );
  212. }
  213. $url_values = array();
  214. /// add main fields
  215. if ( trim($row->extra2) ) {
  216. $extras2 = explode("\n", $row->extra2);
  217. foreach ( $extras2 as $extra2 ) {
  218. $values = array();
  219. $values = explode( "=", $extra2 );
  220. if ( $values[1] ) {
  221. $v = urlencode(trim($values[1]));
  222. $url_values[$v] = JRequest::getVar(trim($values[0]), '', 'post', 'string', '');
  223. }
  224. }
  225. }
  226. if ( trim($row->extra1) ) {
  227. $extras = explode("\n", $row->extra1);
  228. foreach ( $extras as $extra ) {
  229. // Note: accept only the first parameter pair on each line
  230. $values = explode("=", $extra, 2);
  231. if ( isset($values[1]) ) {
  232. $url_values[$values[0]] = trim($values[1]);
  233. }
  234. }
  235. }
  236. $query = JURI::buildQuery($url_values);
  237. $uri = $params->get('target_url').'?'.$query;
  238. $parts['query'] = $query;
  239. $parts['scheme'] = 'http';
  240. $parts['host'] = 'bobjanes.com';
  241. //$parts['host'] = $params->get('target_url');
  242. //$uri = JURI::toString($parts);
  243. $MyForm =& CFChronoForm::getInstance();
  244. $MyForm->formrow->redirecturl = $uri;
  245. $messages[] = '<b>cf_redirect debug info</b>';
  246. $messages[] = '$url: '.print_r($uri, true);
  247. $messages[] = '$_POST: '.print_r($_POST, true);
  248. /*
  249. * Build query into url and set CF redirect url
  250. */
  251. $helper->showCFDebugMessage('Redirect URL set');
  252. if ( $params->get('debugging') ) {
  253. $helper->showPluginDebugMessages($messages);
  254. }
  255. }
  256. }
  257. ?>