PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/utils.php

http://0byte.googlecode.com/
PHP | 155 lines | 47 code | 6 blank | 102 comment | 11 complexity | 0996cbe99e3fb482ea424f685e5988ce MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * This file is part of 0byte.
  4. *
  5. * 0byte is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License.
  8. *
  9. * 0byte is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * See <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. /**
  18. * Send email
  19. *
  20. * @param string $to email recipient
  21. * @param string $subject email subject
  22. * @param string $message email body
  23. * @param bool $html is email message html
  24. * @param array $headers additional email headers
  25. * @return bool
  26. */
  27. function nullbyte_mail($to, $subject, $message, $html = true, $headers = array()) {
  28. $mail_headers = $headers;
  29. if ($html) $mail_headers[] = "Content-type: text/html; charset='UTF-8'";
  30. $mail_headers[] = 'X-Mailer: PHP/' . phpversion();
  31. return mail($to, $subject, $message, implode("\r\n", $mail_headers));
  32. }
  33. /**
  34. * Generate random alpha-numeric string of the following length
  35. *
  36. * @param int $len string length
  37. * @return string
  38. */
  39. function nullbute_generate_pwd($len) {
  40. $pwd = '';
  41. // generate random string
  42. while (strlen($pwd) < $len) {
  43. $pwd .= md5(uniqid());
  44. }
  45. $pwd = substr($pwd, 0, $len);
  46. // more entropy by capitalizing some letters
  47. for ($i = 0; $i < $len; $i++) {
  48. if (!is_numeric($pwd[$i]) && rand() % 2) $pwd[$i] = strtoupper($pwd[$i]);
  49. }
  50. return $pwd;
  51. }
  52. /**
  53. * Encode special characters in a plain-text string for display as HTML.
  54. *
  55. * Uses drupal_validate_utf8 to prevent cross site scripting attacks on
  56. * Internet Explorer 6.
  57. */
  58. function check_plain($text) {
  59. return validate_utf8($text) ? htmlspecialchars($text, ENT_QUOTES) : '';
  60. }
  61. /**
  62. * Checks whether a string is valid UTF-8.
  63. *
  64. * All functions designed to filter input should use drupal_validate_utf8
  65. * to ensure they operate on valid UTF-8 strings to prevent bypass of the
  66. * filter.
  67. *
  68. * When text containing an invalid UTF-8 lead byte (0xC0 - 0xFF) is presented
  69. * as UTF-8 to Internet Explorer 6, the program may misinterpret subsequent
  70. * bytes. When these subsequent bytes are HTML control characters such as
  71. * quotes or angle brackets, parts of the text that were deemed safe by filters
  72. * end up in locations that are potentially unsafe; An onerror attribute that
  73. * is outside of a tag, and thus deemed safe by a filter, can be interpreted
  74. * by the browser as if it were inside the tag.
  75. *
  76. * This function exploits preg_match behaviour (since PHP 4.3.5) when used
  77. * with the u modifier, as a fast way to find invalid UTF-8. When the matched
  78. * string contains an invalid byte sequence, it will fail silently.
  79. *
  80. * preg_match may not fail on 4 and 5 octet sequences, even though they
  81. * are not supported by the specification.
  82. *
  83. * The specific preg_match behaviour is present since PHP 4.3.5.
  84. *
  85. * @param $text
  86. * The text to check.
  87. * @return
  88. * TRUE if the text is valid UTF-8, FALSE if not.
  89. */
  90. function validate_utf8($text) {
  91. if (strlen($text) == 0) {
  92. return TRUE;
  93. }
  94. return (preg_match('/^./us', $text) == 1);
  95. }
  96. /**
  97. * Trim array values
  98. *
  99. * @param array $list
  100. * @param string $apply_function apply additional function to all array values
  101. */
  102. function trim_array(array &$list, $apply_function = '') {
  103. if ($apply_function) {
  104. $func = '$v = ' . $apply_function . '(trim($v));';
  105. } else {
  106. $func = '$v = trim($v);';
  107. }
  108. array_walk($list, create_function('&$v,$k', $func));
  109. }
  110. /**
  111. * Redirect user to some location
  112. *
  113. * @param string $location
  114. * @param int $http_response_code
  115. * Valid values for an actual "goto" as per RFC 2616 section 10.3 are:
  116. * - 301 Moved Permanently (the recommended value for most redirects)
  117. * - 302 Found (default in Drupal and PHP, sometimes used for spamming search
  118. * engines)
  119. * - 303 See Other
  120. * - 304 Not Modified
  121. * - 305 Use Proxy
  122. * - 307 Temporary Redirect (alternative to "503 Site Down for Maintenance")
  123. * Note: Other values are defined by RFC 2616, but are rarely used and poorly
  124. * supported.
  125. */
  126. function redirect($location, $http_response_code = 302) {
  127. header('Location: '. $location, true, $http_response_code);
  128. die;
  129. }
  130. /**
  131. * Get favicon of requested url
  132. *
  133. * @param string $url
  134. * @return url
  135. */
  136. function get_favicon_url($url) {
  137. $out=explode('/',$url);
  138. return 'http://'.$out[2].'/favicon.ico';
  139. }
  140. /**
  141. * Check numeric and return it
  142. *
  143. * @param int $value
  144. * @return int
  145. */
  146. function get_int($value) {
  147. return is_numeric($value) && !preg_match('/x/i', $value) ? $value : '0';
  148. }
  149. ?>