Utils.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 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. public static function MakeUniqueURL($table,$field,$string){
  73. $PDO=DBObject::GetPDO();
  74. $baseUrl=Utils::MakeStringUrlSafe($string);
  75. $prep=$PDO->prepare("SELECT COUNT(*) FROM blog_posts WHERE post_url=?");
  76. $prep->execute(array($baseUrl));
  77. $found=$prep->fetchColumn()>0;
  78. $url=$baseUrl;
  79. $count=0;
  80. while($found) {
  81. $prep->execute(array($url));
  82. $found=(int)$prep->fetchColumn()>0;
  83. if ($found){
  84. $count++;
  85. $url=$baseUrl.'-'.$count;
  86. }
  87. }
  88. return $url;
  89. }
  90. //stolen (with a few edits) from http://stackoverflow.com/questions/5695145/how-to-read-and-write-to-an-ini-file-with-php
  91. public static function WriteINIFile($array,$path,$hasSections){
  92. $content="";
  93. if ($hasSections) {
  94. foreach ($array as $key=>$elem) {
  95. $content.="\n[".$key."]\n";
  96. foreach ($elem as $key2=>$elem2) {
  97. if (is_array($elem2)) {
  98. for ($i=0; $i<count($elem2); $i++) {
  99. $content.=$key2."[]=".$elem2[$i]."\n";
  100. }
  101. } else if ($elem2=="")
  102. $content.=$key2."=\"\"\n";
  103. else
  104. $content.=$key2."=".$elem2."\n";
  105. }
  106. }
  107. } else {
  108. foreach ($array as $key=>$elem) {
  109. if (is_array($elem)) {
  110. for ($i=0; $i<count($elem); $i++) {
  111. $content.=$key."[]=".$elem[$i]."\n";
  112. }
  113. } else if ($elem=="")
  114. $content.=$key."=\"\"\n";
  115. else
  116. $content.=$key."=".$elem."\n";
  117. }
  118. }
  119. if (!$handle=fopen($path, 'w')) {
  120. return false;
  121. }
  122. $content=trim($content);
  123. $success=fwrite($handle, $content);
  124. fclose($handle);
  125. return $success;
  126. }
  127. public static function CloseOpenTags($html) {
  128. preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  129. $openedtags=$result[1];
  130. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  131. $closedtags=$result[1];
  132. $len_opened=count($openedtags);
  133. if (count($closedtags)==$len_opened) {
  134. return $html;
  135. }
  136. $openedtags=array_reverse($openedtags);
  137. for ($i=0; $i<$len_opened; $i++) {
  138. if (!in_array($openedtags[$i], $closedtags)) {
  139. $html .= '</'.$openedtags[$i].'>';
  140. } else {
  141. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  142. }
  143. }
  144. return $html;
  145. }
  146. public static function FileUploadErrorToMessage($errorNo){
  147. switch($errorNo){
  148. case UPLOAD_ERR_OK:
  149. return "Success";
  150. case UPLOAD_ERR_INI_SIZE:
  151. case UPLOAD_ERR_FORM_SIZE:
  152. return "Exceeded file size limit";
  153. case UPLOAD_ERR_PARTIAL:
  154. return "File not fully uploaded";
  155. case UPLOAD_ERR_NO_FILE:
  156. return "No file uploaded";
  157. case UPLOAD_ERR_NO_TMP_DIR:
  158. return "No temp dir";
  159. case UPLOAD_ERR_CANT_WRITE:
  160. return "Failed to write file to disc";
  161. case UPLOAD_ERR_EXTENSION:
  162. return "Upload stopped by PHP extension";
  163. default:
  164. return "Uknown error";
  165. }
  166. }
  167. public static function GenerateRandomString($length=10) {
  168. $characters='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  169. $charactersLength=strlen($characters);
  170. $randomString='';
  171. for ($i=0; $i<$length; $i++) {
  172. $randomString .= $characters[rand(0, $charactersLength-1)];
  173. }
  174. return $randomString;
  175. }
  176. }