PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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