PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/tdo-mini-forms/tdomf-form-ajax.php

https://github.com/firebelly/Radio-Arte-Wordpress
PHP | 234 lines | 191 code | 9 blank | 34 comment | 38 complexity | 327bea2883de723a841ee706acd39074 MD5 | raw file
  1. <?php
  2. //////////////////////////
  3. // Process Form Request //
  4. //////////////////////////
  5. // Load up Wordpress
  6. //
  7. $wp_load = realpath("../../../wp-load.php");
  8. if(!file_exists($wp_load)) {
  9. $wp_config = realpath("../../../wp-config.php");
  10. if (!file_exists($wp_config)) {
  11. exit("Can't find wp-config.php or wp-load.php");
  12. } else {
  13. require_once($wp_config);
  14. }
  15. } else {
  16. require_once($wp_load);
  17. }
  18. global $wpdb, $tdomf_form_widgets_validate, $tdomf_form_widgets_preview;
  19. // loading text domain for language translation
  20. //
  21. load_plugin_textdomain('tdomf',PLUGINDIR.DIRECTORY_SEPARATOR.TDOMF_FOLDER);
  22. // Now using jquery to pre-seralise form output but must still support the old
  23. // way of non-seralized (so people don't have to modify their hacked forms)
  24. // - Note: "action" is still used in _POST
  25. //
  26. global $tdomf_args;
  27. if(isset($_POST['tdomf_args'])) {
  28. parse_str($_POST['tdomf_args'],$tdomf_args);
  29. } else {
  30. tdomf_log_message("AJAX: Using old argument method");
  31. $tdomf_args = $_POST;
  32. }
  33. // Form id
  34. //
  35. if(!isset($tdomf_args['tdomf_form_id'])) {
  36. tdomf_log_message("tdomf-form-ajax: No Form ID set!",TDOMF_LOG_BAD);
  37. die( "alert('".__("TDOMF: No Form id!","tdomf")."');" );
  38. }
  39. $form_id = intval($tdomf_args['tdomf_form_id']);
  40. if(!tdomf_form_exists($form_id)){
  41. tdomf_log_message("tdomf-form-ajax: Bad form id %d!",TDOMF_LOG_BAD);
  42. #die( "tdomfDisplayMessage$form_id('TDOMF: Bad Form Id','full');" );
  43. die( "alert('".__("TDOMF: Bad Form id!","tdomf")."');" );
  44. }
  45. // Submit or Edit?
  46. //
  47. $is_edit = tdomf_get_option_form(TDOMF_OPTION_FORM_EDIT,$form_id);
  48. // Get Form Data for verficiation check
  49. //
  50. $form_data = tdomf_get_form_data($form_id);
  51. function tdomf_ajax_exit($form_id, $message, $full = false, $preview = false, $post_id = false) {
  52. global $form_id;
  53. $is_edit = tdomf_get_option_form(TDOMF_OPTION_FORM_EDIT,$form_id);
  54. if($is_edit) {
  55. $form_tag = $form_id.'_'.$post_id;
  56. } else {
  57. $form_tag = $form_id;
  58. }
  59. #$message = str_replace("'","\\'",$message);
  60. #$message = str_replace("\n"," ",$message);
  61. $message = preg_replace('/\r\n|\n\r|\r/', '\n', str_replace('\'', '\\' . '\'', str_replace('\\', '\\\\', $message)));
  62. $message = str_replace("\n"," ",$message);
  63. #tdomf_log_message("sending '$message' via ajax (tdomfDisplayMessage$form_tag)...");
  64. #$message = htmlentities($message,ENT_COMPAT);
  65. if($full) {
  66. die( "tdomfDisplayMessage$form_tag('$message','full');" );
  67. } else if ($preview) {
  68. die( "tdomfDisplayMessage$form_tag('$message','preview');" );
  69. } else {
  70. die( "tdomfDisplayMessage$form_tag('$message','');" );
  71. }
  72. }
  73. // Get Post ID if there is one
  74. //
  75. $post_id = false;
  76. if($is_edit) {
  77. if(isset($form_data['tdomf_post_id'])) {
  78. $post_id = $form_data['tdomf_post_id'];
  79. } else if(isset($tdomf_args['tdomf_post_id'])) {
  80. $post_id = $tdomf_args['tdomf_post_id'];
  81. } else {
  82. tdomf_log_message("tdomf-form-ajax: Edit form %d but no post id!",TDOMF_LOG_BAD);
  83. #tdomf_ajax_exit($form_id,__("TDOMF (AJAX) ERROR: Missing Post Id!","tdomf"),true,false,$post_id);
  84. die( "alert('".__("TDOMF (AJAX) ERROR: Missing Post Id!","tdomf")."');" );
  85. }
  86. }
  87. // Security Check
  88. //
  89. $tdomf_verify = get_option(TDOMF_OPTION_VERIFICATION_METHOD);
  90. if($tdomf_verify == false || $tdomf_verify == 'default') {
  91. if(!isset($form_data['tdomf_key_'.$form_id]) || $form_data['tdomf_key_'.$form_id] != $tdomf_args['tdomf_key_'.$form_id]) {
  92. if(!isset($form_data) || !isset($form_data['tdomf_key_'.$form_id]) || trim($form_data['tdomf_key_'.$form_id]) == "") {
  93. tdomf_log_message('Key is missing from $form_data: contents of $form_data:<pre>'.var_export($form_data,true)."</pre>",TDOMF_LOG_BAD);
  94. }
  95. $session_key = $form_data['tdomf_key_'.$form_id];
  96. $post_key = $tdomf_args['tdomf_key_'.$form_id];
  97. $ip = $_SERVER['REMOTE_ADDR'];
  98. tdomf_log_message("Form ($form_id) submitted with bad key (session = $session_key, post = $post_key) from $ip !",TDOMF_LOG_BAD);
  99. unset($form_data['tdomf_key_'.$form_id]);
  100. tdomf_save_form_data($form_id,$form_data);
  101. tdomf_ajax_exit($form_id,__("<font color='red'>TDOMF: Bad data submitted. Please reload the page and try submitting your post again.</font>","tdomf"),true,false,$post_id);
  102. }
  103. unset($form_data['tdomf_key_'.$form_id]);
  104. } else if($tdomf_verify == 'wordpress_nonce') {
  105. if(!wp_verify_nonce($tdomf_args['tdomf_key_'.$form_id],'tdomf-form-'.$form_id)) {
  106. $post_key = $tdomf_args['tdomf_key_'.$form_id];
  107. $ip = $_SERVER['REMOTE_ADDR'];
  108. tdomf_log_message("Form ($form_id) submitted with bad nonce key (post = $post_key) from $ip !",TDOMF_LOG_BAD);
  109. tdomf_ajax_exit($form_id,__("<font color='red'>TDOMF: Bad data submitted. Please reload the page and try submitting your post again.</font>","tdomf"),false,false,$post_id);
  110. }
  111. }
  112. function tdomf_fixslashesargs() {
  113. global $tdomf_args;
  114. #if (get_magic_quotes_gpc()) {
  115. tdomf_log_message_extra("Magic quotes is enabled. Stripping slashes!");
  116. if(!function_exists('stripslashes_array')) {
  117. function stripslashes_array($array) {
  118. return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array);
  119. }
  120. }
  121. $_COOKIE = stripslashes_array($_COOKIE);
  122. #$_FILES = stripslashes_array($_FILES);
  123. #$_GET = stripslashes_array($_GET);
  124. $tdomf_args = stripslashes_array($tdomf_args);
  125. $_REQUEST = stripslashes_array($_REQUEST);
  126. #}
  127. }
  128. // Double check user permissions
  129. //
  130. $message = tdomf_check_permissions_form($form_id,$post_id);
  131. if($message != NULL) {
  132. tdomf_ajax_exit($form_id,$message,true,false,$post_id);
  133. }
  134. if(!isset($_POST['tdomf_action'])) {
  135. tdomf_ajax_exit($form_id,__("TDOMF (AJAX) ERROR: no action set!","tdomf"),true,false,$post_id);
  136. }
  137. // Now either generate a preview or create a post
  138. //
  139. if($_POST['tdomf_action'] == "post") {
  140. tdomf_log_message("Someone is attempting to submit something");
  141. $message = tdomf_validate_form($tdomf_args,false);
  142. if($message == NULL) {
  143. $args = $tdomf_args;
  144. $args['ip'] = $_SERVER['REMOTE_ADDR'];
  145. $retVal = tdomf_create_post($args);
  146. // If retVal is an int it's a post id
  147. if(is_int($retVal)) {
  148. if($is_edit) {
  149. $edit_id = $retVal;
  150. $edit = tdomf_get_edit($edit_id);
  151. // @todo could probably test if $edit is real or not before proceeding
  152. $post_id = $edit->post_id;
  153. if($edit->state == 'approved') {
  154. if(tdomf_get_option_form(TDOMF_OPTION_REDIRECT,$form_id)) {
  155. die( "tdomfRedirect$form_id('".get_permalink($post_id)."');" );
  156. // Hack: set your own URL here if you wish to redirect to a
  157. // different URL (and comment out the 'die' line above)
  158. // Future versions of TDOMF will provide this as an option.
  159. //
  160. #die( "tdomfRedirect$form_id('http://thedeadone.net/download/tdo-mini-forms-wordpress-plugin/');" );
  161. } else {
  162. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_PUBLISH,$form_id,false,$post_id),true,false,$post_id);
  163. }
  164. } else if($edit->state == 'spam') {
  165. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_SPAM,$form_id),true,false,$post_id);
  166. } else { // unapproved
  167. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_MOD,$form_id,false,$post_id),true,false,$post_id);
  168. }
  169. } else {
  170. $post_id = $retVal;
  171. if(get_post_status($post_id) == 'publish') {
  172. if(tdomf_get_option_form(TDOMF_OPTION_REDIRECT,$form_id)) {
  173. die( "tdomfRedirect$form_id('".get_permalink($post_id)."');" );
  174. // Hack: set your own URL here if you wish to redirect to a
  175. // different URL (and comment out the 'die' line above)
  176. // Future versions of TDOMF will provide this as an option.
  177. //
  178. #die( "tdomfRedirect$form_id('http://thedeadone.net/download/tdo-mini-forms-wordpress-plugin/');" );
  179. } else {
  180. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_PUBLISH,$form_id,false,$post_id),true,false,$post_id);
  181. }
  182. } else if(get_post_status($post_id) == 'future') {
  183. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_FUTURE,$form_id,false,$post_id),true,false,$post_id);
  184. } else if(get_post_meta($post_id, TDOMF_KEY_SPAM)) {
  185. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_SPAM,$form_id),true,false,$post_id);
  186. } else {
  187. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_MOD,$form_id,false,$post_id),true,false,$post_id);
  188. // Hack: set your own URL here if you wish to redirect to a
  189. // different URL (and comment out the 'die' line above)
  190. // Future versions of TDOMF will provide this as an option.
  191. //
  192. #die( "tdomfRedirect$form_id('http://thedeadone.net/download/tdo-mini-forms-wordpress-plugin/');" );
  193. }
  194. }
  195. // If retVal is a string, something went wrong!
  196. } else {
  197. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_ERROR,$form_id,false,false,$retVal),false,false,$post_id);
  198. }
  199. } else {
  200. tdomf_ajax_exit($form_id,tdomf_get_message_instance(TDOMF_OPTION_MSG_SUB_ERROR,$form_id,false,false,$message),false,false,$post_id);
  201. }
  202. } else if($_POST['tdomf_action'] == "preview") {
  203. // For preview, remove magic quote slashes!
  204. tdomf_fixslashesargs();
  205. $message = tdomf_validate_form($tdomf_args,true);
  206. if($message == NULL) {
  207. $message = tdomf_preview_form($tdomf_args);
  208. tdomf_ajax_exit($form_id,$message,false,true,$post_id);
  209. } else {
  210. tdomf_ajax_exit($form_id,sprintf(__("Your submission contained errors:<br/><br/>%s<br/><br/>Please correct and resubmit.","tdomf"),$message),false,false,$post_id);
  211. }
  212. } else {
  213. tdomf_ajax_exit($form_id,sprintf(__("TDOMF (AJAX) ERROR: unrecognised action %s!","tdomf"),$_POST['action']),true,false,$post_id);
  214. }
  215. tdomf_ajax_exit($form_id,__("ERROR! Should never reach here.","tdomf"),true,false,$post_id);
  216. ?>