Util.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * Utility function class
  4. *
  5. * @author Christopher Han <xiphux@gmail.com>
  6. * @copyright Copyright (c) 2010 Christopher Han
  7. * @package GitPHP
  8. */
  9. class GitPHP_Util
  10. {
  11. /**
  12. * Adds a trailing slash to a directory path if necessary
  13. *
  14. * @param string $path path to add slash to
  15. * @param boolean $filesystem true if this is a filesystem path (to also check for backslash for windows paths)
  16. * @return string path with a trailing slash
  17. */
  18. public static function AddSlash($path, $filesystem = true)
  19. {
  20. if (empty($path))
  21. return $path;
  22. $end = substr($path, -1);
  23. if (!(( ($end == '/') || ($end == ':')) || ($filesystem && GitPHP_Util::IsWindows() && ($end == '\\')))) {
  24. if (GitPHP_Util::IsWindows() && $filesystem) {
  25. $path .= '\\';
  26. } else {
  27. $path .= '/';
  28. }
  29. }
  30. return $path;
  31. }
  32. /**
  33. * Tests if this is running on windows
  34. *
  35. * @return bool true if on windows
  36. */
  37. public static function IsWindows()
  38. {
  39. return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
  40. }
  41. public static function NullFile()
  42. {
  43. return self::IsWindows() ? 'NUL' : '/dev/null';
  44. }
  45. /**
  46. * Tests if this is a 64 bit machine
  47. *
  48. * @return bool true if on 64 bit
  49. */
  50. public static function Is64Bit()
  51. {
  52. return (strpos(php_uname('m'), '64') !== false);
  53. }
  54. /**
  55. * Turn a string into a filename-friendly slug
  56. *
  57. * @param string $str string to slugify
  58. * @return string slug
  59. */
  60. public static function MakeSlug($str)
  61. {
  62. $from = array(
  63. '/&'
  64. );
  65. $to = array(
  66. '--'
  67. );
  68. return str_replace($from, $to, $str);
  69. }
  70. /**
  71. * Get the filename of a given path
  72. *
  73. * Based on Drupal's basename
  74. *
  75. * @param string $path path
  76. * @param string $suffix optionally trim this suffix
  77. * @return string filename
  78. */
  79. public static function BaseName($path, $suffix = null)
  80. {
  81. $sep = '/';
  82. if (GitPHP_Util::IsWindows()) {
  83. $sep .= '\\';
  84. }
  85. $path = rtrim($path, $sep);
  86. if (!preg_match('@[^' . preg_quote($sep) . ']+$@', $path, $matches)) {
  87. return '';
  88. }
  89. $filename = $matches[0];
  90. if ($suffix) {
  91. $filename = preg_replace('@' . preg_quote($suffix, '@') . '$@', '', $filename);
  92. }
  93. return $filename;
  94. }
  95. /**
  96. * Provides a geshi language for a given filename
  97. *
  98. * @param string $filename file name
  99. * @return string language
  100. */
  101. public static function GeshiFilenameToLanguage($filename)
  102. {
  103. if (strncasecmp($filename, 'Makefile', 8) === 0) {
  104. return 'make';
  105. }
  106. return null;
  107. }
  108. /**
  109. * Recurses into a directory and lists files inside
  110. *
  111. * @param string $dir directory
  112. * @return string[] array of filenames
  113. */
  114. public static function ListDir($dir)
  115. {
  116. $files = array();
  117. if ($dh = opendir($dir)) {
  118. while (($file = readdir($dh)) !== false) {
  119. if (($file == '.') || ($file == '..')) {
  120. continue;
  121. }
  122. $fullFile = $dir . '/' . $file;
  123. if (is_dir($fullFile)) {
  124. $subFiles = GitPHP_Util::ListDir($fullFile);
  125. if (count($subFiles) > 0) {
  126. $files = array_merge($files, $subFiles);
  127. }
  128. } else {
  129. $files[] = $fullFile;
  130. }
  131. }
  132. }
  133. return $files;
  134. }
  135. /**
  136. * Get the base install url (without index)
  137. *
  138. * @param boolean $full true to return full url (include protocol and hostname)
  139. * @return string base url
  140. */
  141. public static function BaseUrl($full = false)
  142. {
  143. $baseurl = $_SERVER['SCRIPT_NAME'];
  144. if (substr_compare($baseurl, 'index.php', -9) === 0)
  145. $baseurl = dirname($baseurl);
  146. if ($full) {
  147. $baseurl = $_SERVER['HTTP_HOST'] . $baseurl;
  148. if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on'))
  149. $baseurl = 'https://' . $baseurl;
  150. else
  151. $baseurl = 'http://' . $baseurl;
  152. }
  153. if (GitPHP_Util::IsWindows())
  154. $baseurl = rtrim($baseurl, "\\");
  155. return rtrim($baseurl, "/");
  156. }
  157. /**
  158. * Tests whether a function is allowed to be called
  159. *
  160. * @param string $function functio name
  161. * @return true if allowed
  162. */
  163. public static function FunctionAllowed($function)
  164. {
  165. if (empty($function))
  166. return false;
  167. $disabled = @ini_get('disable_functions');
  168. if (!$disabled) {
  169. // no disabled functions
  170. // or ini_get is disabled so we can't reliably figure this out
  171. return true;
  172. }
  173. $disabledlist = explode(', ', $disabled);
  174. return !in_array($function, $disabledlist);
  175. }
  176. }