Utils.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. class Utils {
  3. const METRES_TO_FEET=3.2808399;
  4. const KILOMETRES_TO_MILES=0.621371192;
  5. public static function TableMaker($data, $buttonContent="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><button name="submit_form" type="submit">', $buttonContent, '</button></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 MakeStringUrlSafe($string) {
  61. $string=strtolower($string);
  62. $string=preg_replace('/[^a-zA-Z0-9\.]+/', '-', $string);
  63. return $string;
  64. }
  65. public static function MakeUniqueURL($table, $field, $string) {
  66. $PDO=BaseRepository::GetPDO();
  67. $baseUrl=Utils::MakeStringUrlSafe($string);
  68. $prep=$PDO->prepare("SELECT COUNT(*) FROM blog_posts WHERE post_url=?");
  69. $prep->execute(array($baseUrl));
  70. $found=$prep->fetchColumn()>0;
  71. $url=$baseUrl;
  72. $count=0;
  73. while ($found) {
  74. $prep->execute(array($url));
  75. $found=(int)$prep->fetchColumn()>0;
  76. if ($found) {
  77. $count++;
  78. $url=$baseUrl.'-'.$count;
  79. }
  80. }
  81. return $url;
  82. }
  83. //stolen (with a few edits) from http://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
  84. public static function WriteINIFile($array, $path, $hasSections) {
  85. $content="";
  86. if ($hasSections) {
  87. foreach ($array as $key=> $elem) {
  88. $content.="\n[".$key."]\n";
  89. foreach ($elem as $key2=> $elem2) {
  90. if (is_array($elem2)) {
  91. for ($i=0; $i<count($elem2); $i++) {
  92. $content.=$key2."[]=".$elem2[$i]."\n";
  93. }
  94. } else if ($elem2=="")
  95. $content.=$key2."=\"\"\n";
  96. else
  97. $content.=$key2."=".$elem2."\n";
  98. }
  99. }
  100. } else {
  101. foreach ($array as $key=> $elem) {
  102. if (is_array($elem)) {
  103. for ($i=0; $i<count($elem); $i++) {
  104. $content.=$key."[]=".$elem[$i]."\n";
  105. }
  106. } else if ($elem=="")
  107. $content.=$key."=\"\"\n";
  108. else
  109. $content.=$key."=".$elem."\n";
  110. }
  111. }
  112. if (!$handle=fopen($path, 'w')) {
  113. return false;
  114. }
  115. $content=trim($content);
  116. $success=fwrite($handle, $content);
  117. fclose($handle);
  118. return $success;
  119. }
  120. public static function CloseOpenTags($html) {
  121. preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  122. $openedtags=$result[1];
  123. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  124. $closedtags=$result[1];
  125. $len_opened=count($openedtags);
  126. if (count($closedtags)==$len_opened) {
  127. return $html;
  128. }
  129. $openedtags=array_reverse($openedtags);
  130. for ($i=0; $i<$len_opened; $i++) {
  131. if (!in_array($openedtags[$i], $closedtags)) {
  132. $html.='</'.$openedtags[$i].'>';
  133. } else {
  134. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  135. }
  136. }
  137. return $html;
  138. }
  139. public static function FileUploadErrorToMessage($errorNo) {
  140. switch ($errorNo) {
  141. case UPLOAD_ERR_OK:
  142. return "Success";
  143. case UPLOAD_ERR_INI_SIZE:
  144. case UPLOAD_ERR_FORM_SIZE:
  145. return "Exceeded file size limit";
  146. case UPLOAD_ERR_PARTIAL:
  147. return "File not fully uploaded";
  148. case UPLOAD_ERR_NO_FILE:
  149. return "No file uploaded";
  150. case UPLOAD_ERR_NO_TMP_DIR:
  151. return "No temp dir";
  152. case UPLOAD_ERR_CANT_WRITE:
  153. return "Failed to write file to disc";
  154. case UPLOAD_ERR_EXTENSION:
  155. return "Upload stopped by PHP extension";
  156. default:
  157. return "Uknown error";
  158. }
  159. }
  160. public static function GenerateRandomString($length=10) {
  161. $characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  162. $charactersLength=strlen($characters);
  163. $randomString='';
  164. for ($i=0; $i<$length; $i++) {
  165. $randomString.=$characters[rand(0, $charactersLength-1)];
  166. }
  167. return $randomString;
  168. }
  169. public static function var_dump_email() {
  170. ob_start();
  171. debug_print_backtrace();
  172. var_dump(func_get_args());
  173. $output=ob_get_clean();
  174. mail("var_dump@robware.uk", "var_dump", $output);
  175. }
  176. public static function ParseSize($size) {
  177. $unit=preg_replace('/[^bkmgtpezy]/i', '', $size);
  178. $size=preg_replace('/[^0-9\.]/', '', $size);
  179. return round($unit ? $size*pow(1024, stripos('bkmgtpezy', $unit[0])) : $size);
  180. }
  181. public static function GetMaxUploadSize() {
  182. $maxUpload=Utils::ParseSize(ini_get('upload_max_filesize'));
  183. $maxPost=Utils::ParseSize(ini_get('post_max_size'));
  184. $memoryLimit=Utils::ParseSize(ini_get('memory_limit'));
  185. return min($maxUpload, $maxPost, $memoryLimit);
  186. }
  187. }