PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/class/integration/formdata.php

https://github.com/skamster/Peta
PHP | 286 lines | 174 code | 39 blank | 73 comment | 55 complexity | d0d9aa95a82e641bd890d049cd915b63 MD5 | raw file
  1. <?php
  2. /*-------------------------------USER MANUAL----------------------------------*/
  3. /*
  4. 1) Specific the fields in the Array
  5. name Must Set
  6. title Not necessary
  7. value Not necessary
  8. type default-value is an input field with type text
  9. style Not necessary
  10. extend_before Not necessary
  11. extend_tag Not necessary
  12. extend_after Not necessary
  13. save If its set on true, the field will write into the db. if its false, it does not save anything.
  14. check Give argument to check for send mail. "empty", "mail", "captcha", "website"
  15. specialrow If specialrow is true, don't display $extend_row_start and $extend_row_end
  16. 2) Output the fields with the function form_display_fields() and transmit the array with the attributes - Dont forget <form>
  17. 3) Save the fields in DB with function form_save_database() - Dont forget to etablish the db connection at first!
  18. First argument array The array with the attributes
  19. second argument true/false For Debugging, if true it outputs the sql statement and sql errors. default value is false
  20. third argument true/false For Saving, if true the sql statement will send to the DB - if false of course not. default value is true
  21. */
  22. /*-------------------------------DEFINE GENERAL----------------------------------*/
  23. /*
  24. $extend_row_start = "<tr><td class='titel'>".$out['title']."</td><td>"; // At the beginning of the loop
  25. $extend_row_end = "</tr>"; // At the end of the loop
  26. */
  27. /*-------------------------------DEFINE FIELDS----------------------------------*/
  28. $array = array();
  29. $array[0] = array(
  30. "name" => "prename",
  31. "title" => "Vornamen",
  32. "value" => "standard",
  33. "type" => "text",
  34. "style" => "border: 3px solid red;",
  35. "extend_before" => "vorher",
  36. "extend_tag" => " class=\"reset\"",
  37. "extend_after" => "nachher!",
  38. "save" => true,
  39. );
  40. $array[1] = array(
  41. "name" => "mail",
  42. "title" => "eMail",
  43. "value" => "x@y.ch",
  44. "type" => "pw",
  45. "save" => true
  46. );
  47. $array[2] = array(
  48. "name" => "web",
  49. "title" => "Website",
  50. "value" => "www.blubb.ch",
  51. "type" => "textarea",
  52. "style" => "border: 3px solid green;",
  53. "save" => true
  54. );
  55. $array[3] = array(
  56. "name" => "submit",
  57. "title" => "Submit",
  58. "value" => "Submit form",
  59. "type" => "submit",
  60. "save" => false
  61. );
  62. /*-------------------------------DISPLAY FIELDS----------------------------------*/
  63. function form_display_fields($array) {
  64. //Make vars aviable
  65. global $extend_row_start, $extend_row_end;
  66. //Reset the counter
  67. $i = 0;
  68. //Do it until the array is done
  69. foreach($array as $out) {
  70. //At the beginning of the loop
  71. if($out['specialrow'] != true) {
  72. if(isset($extend_row_start)) { echo $extend_row_start; }
  73. }
  74. //Return extension before if it is defined
  75. if(isset($out['extend_before'])) { echo $out['extend_before']."\n"; }
  76. //Open input/textarea field
  77. echo '<';
  78. //Check for tag-name in type
  79. if($out['type'] == "textarea") { echo 'textarea '; } else { echo 'input '; }
  80. //Output the field name
  81. echo 'name="formdata['.$i.']" ';
  82. //Output the type if set, otherwise use input
  83. echo 'type="'; if(isset($out['type'])) { echo $out['type']; } else { echo "input";} echo '" ';
  84. //Output the value
  85. echo 'value="'; if(!empty($_POST['formdata'][$i])) { echo $_POST['formdata'][$i]; } else { echo $out['value']; } echo '" ';
  86. //Output the style if it is defined
  87. if(isset($out['style'])) { echo 'style="'.$out['style'].'" '; }
  88. //Check for textarea and add some bug-workaround code
  89. if($out['type'] == "textarea") { echo 'cols="" rows="" '; }
  90. //Output the attributes if it is defined
  91. if(isset($out['extend_tag'])) { echo $out['extend_tag']; }
  92. //Close the input/textarea tag
  93. echo " >\n";
  94. //Close the textarea tag
  95. if($out['type'] == "textarea") { if(!empty($_POST['formdata'][$i])) { echo $_POST['formdata'][$i]; } else { echo $out['value']; } echo '</textarea>'; }
  96. //Output the attributes if it is defined
  97. if(isset($out['extend_after'])) { echo $out['extend_after']."\n"; }
  98. //At the end of the loop
  99. if($out['specialrow'] != true) {
  100. if(isset($extend_row_end)) { echo $extend_row_end; }
  101. }
  102. //Count one up
  103. $i++;
  104. }
  105. }
  106. /*-------------------------------SAVE INTO DATABASE----------------------------------*/
  107. function form_save_database($array, $insert_into, $debug = false, $submit = true, $log = true) {
  108. //Which table to use
  109. $sql = "INSERT INTO ". $insert_into ." (";
  110. //Build the tablecolumns
  111. foreach($array as $out) {
  112. //Check about the save-attribut
  113. if($out['save']!=false) {
  114. $sql .= $out['name'] .",";
  115. }
  116. }
  117. //Close the tablecolumns
  118. $sql .= ") VALUES (";
  119. //Reset counter
  120. $i = 0;
  121. //Build the values
  122. foreach($array as $out) {
  123. //Check about the save-attribut
  124. if($out['save'] != false) {
  125. //if the specific field is trasmitted, fill it up, otherwise clear the field
  126. if(isset($_POST['formdata'][$i])) {
  127. //Insert value and separator
  128. $sql .= $_POST['formdata'][$i] .",";
  129. // clear the field if nothing is submitted
  130. } else { $sql .=","; }
  131. //Count one up
  132. $i++;
  133. }
  134. }
  135. //Close sql statement
  136. $sql .= ");";
  137. //Write into the database, if its not deactivated
  138. if($submit == true) {
  139. mysql_query($sql);
  140. }
  141. // Output the sql statement if its true
  142. if($debug == true) {
  143. echo $sql;
  144. mysql_error();
  145. }
  146. // Log the sql statement if its true
  147. if($log == true) {
  148. log_write("system", $sql);
  149. }
  150. }
  151. /*-------------------------------SEND TO MAIL----------------------------------*/
  152. function form_send_mail($array, $debug = false, $submit = true, $log = true) {
  153. // Set defaults
  154. $error = false;
  155. $i = 0;
  156. global $captcha_input_field, $t, $s;
  157. //Check the forms
  158. foreach($array as $out) {
  159. //Switch on checktype
  160. switch($out['check']) {
  161. case "mail":
  162. break;
  163. case "mail":
  164. if(!ereg ("^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,3}$", $_POST['formdata'][$i])) {
  165. $ausgabe .= $t['contact']['invalid_mail'] ."<br />";
  166. $error = true;
  167. }
  168. break;
  169. case "empty":
  170. if(empty($_POST['formdata'][$i])) {
  171. $ausgabe .= $t['contact']['missing_thing_start'] . $out['name'] . $t['contact']['missing_thing_end'] ."<br />";
  172. $error = true;
  173. }
  174. break;
  175. case "captcha":
  176. if($_POST['formdata'][$i] != md5($_POST['formdata'][$captcha_input_field])) {
  177. $ausgabe .= $t['contact']['invalid_captcha'] ."<br />";
  178. $error = true;
  179. }
  180. break;
  181. case "website":
  182. if(trim($_POST['formdata'][$i]) != "")
  183. {
  184. if(substr($_POST['formdata'][$i], 0, 7) != "http://")
  185. $_POST['formdata'][$i] = "http://".$_POST['formdata'][$i];
  186. }
  187. break;
  188. }
  189. // echo "array foreach 1-".$i.": ". $out['check'] ." - STATUS "; if($error==true) { echo "TrUe"; } else { echo "false"; } echo " - formdata:" . $_POST['formdata'][$i] . "<br />\n";
  190. $i++;
  191. }
  192. if($error == false) {
  193. $name = nl2br(stripslashes(htmlspecialchars($_POST['name'])));
  194. $IP = getenv("REMOTE_ADDR");
  195. $absender = preg_replace( "/[^a-z0-9 !?:;,.\/_\-=+@#$&\*\(\)]/im", "", $_POST['email'] );
  196. $absender = preg_replace( "/(content-type:|bcc:|cc:|to:|from:)/im", "", $absender );
  197. $extra = "From: $name <$absender>\n";
  198. $extra .= "Content-Type: text/html\n Content-Transfer-Encoding: 8bit\n";
  199. $alternative = nl2br(stripslashes(htmlspecialchars($_POST['alternative'])));
  200. $homepage = nl2br(stripslashes(htmlspecialchars($_POST['homepage'])));
  201. $betreff = nl2br(stripslashes(htmlspecialchars($_POST['betreff'])));
  202. $nachricht = nl2br(stripslashes(htmlspecialchars($_POST['message'])));
  203. $mailnachricht .= $t['contact']['mail_headline'] ."<br /><br />
  204. <span style=\"font-weight: bold; border-bottom: 1px dotted gray; margin-bottom: 5px;\">".$t['contact']['mail_title_userdata']."</span><br />";
  205. $i = 0;
  206. foreach($array as $out) {
  207. if($out['type']!="hidden" AND $out['type']!="submit" AND $out['type']!="reset") {
  208. $mailnachricht .= $out['name'] . ": <b>" . nl2br(stripslashes(htmlspecialchars($_POST['formdata'][$i]))) ."</b><br />\n";
  209. }
  210. $i++;
  211. }
  212. $mailnachricht .= "<br /><span style=\"font-weight: bold; border-bottom: 1px dotted gray; margin-bottom: 5px;\">";
  213. $mailnachricht .= $t['contact']['mail_title_message'];
  214. $mailnachricht .= "</span><br /><br />";
  215. $i = 0;
  216. foreach($array as $out) {
  217. if($out['mail']=="header") { $betreff = $_POST['formdata'][$i]; }
  218. if($out['mail']=="message") { $nachricht = $_POST['formdata'][$i]; }
  219. $i++;
  220. }
  221. $mailnachricht .= "<b><span style='border: 1px solid gray; padding: 3px;font-size: 1.5em'>".$betreff ."</span></b><br/><br/>". $nachricht ."<br/><br />
  222. <span style='border-top: 1px dotted gray; font-size: 0.6em; color: gray'>".$t['contact']['mail_message_footer']."</span>";
  223. $ok = true;
  224. }
  225. // Output the mailcontent if is true
  226. if($debug == true) {
  227. echo $mailnachricht;
  228. }
  229. // Send the mail if its true
  230. if($submit == true) {
  231. mail($s['form_mail_addy'], $betreff . $t['contact']['mail_header_attend'], $mailnachricht, $extra);
  232. }
  233. // Log the sql statement if its true
  234. if($log == true) {
  235. //log_write("system", $s['form_mail_addy'] . $betreff . $t['contact']['mail_header_attend'] . $mailnachricht);
  236. }
  237. //Save Output
  238. if($error==false) {
  239. echo $s['m_tag_pro_start'] . $t['contact']['message_victory_content'] . $s['m_tag_pro_end'];
  240. }
  241. if ($error==true) {
  242. echo $s['m_tag_neg_start'] . $t['contact']['message_failure_content_start'] . "<div class=\"space\">" .$ausgabe . "</div>". $t['contact']['message_failure_content_end'] . $s['m_tag_neg_end'] ;
  243. }
  244. }
  245. /*-------------------------------DEMONSTRATION----------------------------------*/
  246. ?>