Helper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. class Helper{
  3. const METRES_TO_FEET=3.2808399;
  4. const KILOMETRES_TO_MILES=0.621371192;
  5. public static function TableMaker($data,$buttonText="Submit",$action="",$method="post") {
  6. ob_start();
  7. echo '<form action="',$action,'" method="',$method,'"><table>';
  8. foreach ($data as $datum){
  9. if (!isset($datum['value']))
  10. $datum['value']="";
  11. if (!isset($datum['type']))
  12. $datum['type']="text";
  13. $html="";
  14. switch ($datum['type']) {
  15. case "select":
  16. $html="<select id=\"$datum[name]\" name=\"$datum[name]\">";
  17. foreach ($datum['options'] as $value=>$option){
  18. $html.="<option value=\"$value\"";
  19. if ($value==$datum['value'])
  20. $html.="selected=\"selected\"";
  21. $html.=">$option</option>";
  22. }
  23. $html.="</select>";
  24. break;
  25. case "checkbox":
  26. $checked="";
  27. if ($datum['checked'])
  28. $checked='checked="checked"';
  29. $html="<input type=\"checkbox\" id=\"$datum[name]\" name=\"$datum[name]\" $checked />";
  30. break;
  31. default:
  32. $html="<input id=\"$datum[name]\" name=\"$datum[name]\" type=\"$datum[type]\" value=\"$datum[value]\" />";
  33. break;
  34. }
  35. if ($datum['type']=="hidden"){
  36. echo $html;
  37. continue;
  38. }
  39. echo '<tr><td><label for="',$datum['name'],'">',$datum['display'],': </label></td>',
  40. '<td>',$html,'</td></tr>';
  41. }
  42. echo '<tr><td></td><td><input name="submit_form" type="submit" value="',$buttonText,'" /></td></tr>',
  43. '</table></form>';
  44. return ob_get_clean();
  45. }
  46. public static function IsValidEmail($email){
  47. if( !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$email))
  48. return false;
  49. return true;
  50. list($userName, $mailDomain)=explode("@", $email);
  51. if (checkdnsrr($mailDomain,"MX"))
  52. return true;
  53. else
  54. return false;
  55. }
  56. public static function DeleteCookie($cookie){
  57. unset($_COOKIE[$cookie]);
  58. setcookie($cookie,null,-1,'/');
  59. }
  60. public static function GetMaxUploadString(){
  61. $maxUpload=(int)ini_get('upload_max_filesize');
  62. $maxPost=(int)ini_get('post_max_size');
  63. $memoryLimit=(int)ini_get('memory_limit');
  64. $size=min($maxUpload, $maxPost, $memoryLimit);
  65. return $size.'MB';
  66. }
  67. public static function MakeStringUrlSafe($string){
  68. $string=strtolower($string);
  69. $string=preg_replace('/[^a-zA-Z0-9]+/', '-', $string);
  70. return $string;
  71. }
  72. //stolen (with a few edits) from http://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
  73. public static function WriteINIFile($array,$path,$hasSections){
  74. $content="";
  75. if ($hasSections) {
  76. foreach ($array as $key=>$elem) {
  77. $content.="\n[".$key."]\n";
  78. foreach ($elem as $key2=>$elem2) {
  79. if (is_array($elem2)) {
  80. for ($i=0; $i<count($elem2); $i++) {
  81. $content.=$key2."[]=".$elem2[$i]."\n";
  82. }
  83. } else if ($elem2=="")
  84. $content.=$key2."=\"\"\n";
  85. else
  86. $content.=$key2."=".$elem2."\n";
  87. }
  88. }
  89. } else {
  90. foreach ($array as $key=>$elem) {
  91. if (is_array($elem)) {
  92. for ($i=0; $i<count($elem); $i++) {
  93. $content.=$key."[]=".$elem[$i]."\n";
  94. }
  95. } else if ($elem=="")
  96. $content.=$key."=\"\"\n";
  97. else
  98. $content.=$key."=".$elem."\n";
  99. }
  100. }
  101. if (!$handle=fopen($path, 'w')) {
  102. return false;
  103. }
  104. $content=trim($content);
  105. $success=fwrite($handle, $content);
  106. fclose($handle);
  107. return $success;
  108. }
  109. public static function CloseOpenTags($html) {
  110. preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  111. $openedtags=$result[1];
  112. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  113. $closedtags=$result[1];
  114. $len_opened=count($openedtags);
  115. if (count($closedtags)==$len_opened) {
  116. return $html;
  117. }
  118. $openedtags=array_reverse($openedtags);
  119. for ($i=0; $i<$len_opened; $i++) {
  120. if (!in_array($openedtags[$i], $closedtags)) {
  121. $html .= '</'.$openedtags[$i].'>';
  122. } else {
  123. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  124. }
  125. }
  126. return $html;
  127. }
  128. }