util.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?
  2. # Cleanup the provided series name
  3. # This does NOT provide comprehensively safe output,
  4. # it merely attempts to make names compatible with the filesystem and local conventions
  5. function cleanSeries($series) {
  6. # Clearly unreasonable characters
  7. $series = preg_replace('/[\0\n\r]/', ' ', $series);
  8. # Not allowed by the SMB filesystem -- */:?
  9. $series = preg_replace('/\*'/, '_', $series);
  10. $series = preg_replace('/\s*[\/\:]\s*/', ' - ', $series);
  11. $series = preg_replace('/\?/', "\xef\x80\xa5", $series);
  12. # General string cleanup
  13. $series = preg_replace('/\s+/', ' ', $series);
  14. $series = trim($series);
  15. # Ensure we don't go out-of-scope
  16. $series = basename($series);
  17. return $series;
  18. }
  19. # True if the input path is junk -- self-links, OS X noise, etc.
  20. function isJunk($path) {
  21. $path = basename($path);
  22. # Ignore certain fixed paths
  23. foreach (array('.', '..', '.DS_Store') as $value) {
  24. if ($path == $value) {
  25. return true;
  26. }
  27. }
  28. # Ignore ._ paths
  29. if (preg_match('/^\.\_/', $path)) {
  30. return true;
  31. }
  32. # Otherwise the path is good
  33. return false;
  34. }
  35. function sortTitle($title) {
  36. $title = displayTitle($title);
  37. $title = preg_replace('/^The\s+/i', '', $title);
  38. $title = preg_replace('/^A\s+/i', '', $title);
  39. return $title;
  40. }
  41. function displayTitle($title) {
  42. $title = preg_replace('/\xEF\x80\xA9$/', '.', $title);
  43. $title = preg_replace('/\xEF\x80\xA5/', '?', $title);
  44. return $title;
  45. }
  46. function clearCache() {
  47. global $CACHE_FILE;
  48. if (file_exists($CACHE_FILE)) {
  49. rename($CACHE_FILE, staleCacheName($CACHE_FILE));
  50. }
  51. }
  52. function staleCacheName($file) {
  53. return $file . '.stale';
  54. }
  55. function protocolName() {
  56. $protocol = 'http';
  57. if ($_SERVER['HTTPS']) {
  58. $protocol = 'https';
  59. }
  60. return $protocol;
  61. }
  62. function adjustProtocol($url) {
  63. return preg_replace('/^https?\:/i', protocolName() . ':', $url);
  64. }
  65. ?>