util.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 convetions
  5. function cleanSeries($series) {
  6. # Clearly unreasonable characters
  7. $series = preg_replace('/[\0\n\r]/', ' ', $series);
  8. # Not allowed on our filesystem
  9. $series = preg_replace('/\s*[\/\:]\s*/', ' - ', $series);
  10. # General string cleanup
  11. $series = preg_replace('/\s+/', ' ', $series);
  12. # Ensure we don't go out-of-scope
  13. $series = basename($series);
  14. return $series;
  15. }
  16. # True if the input path is junk -- self-links, OS X noise, etc.
  17. function isJunk($path) {
  18. $path = basename($path);
  19. # Ignore certain fixed paths
  20. foreach (array('.', '..', '.DS_Store') as $value) {
  21. if ($path == $value) {
  22. return true;
  23. }
  24. }
  25. # Ignore ._ paths
  26. if (preg_match('/^\.\_/', $path)) {
  27. return true;
  28. }
  29. # Otherwise the path is good
  30. return false;
  31. }
  32. function sortTitle($title) {
  33. $title = displayTitle($title);
  34. $title = preg_replace('/^The\s+/i', '', $title);
  35. $title = preg_replace('/^A\s+/i', '', $title);
  36. return $title;
  37. }
  38. function displayTitle($title) {
  39. $title = preg_replace('/\xEF\x80\xA9$/', '.', $title);
  40. $title = preg_replace('/\xEF\x80\xA5/', '?', $title);
  41. return $title;
  42. }
  43. function clearCache() {
  44. global $CACHE_FILE;
  45. if (file_exists($CACHE_FILE)) {
  46. rename($CACHE_FILE, staleCacheName($CACHE_FILE));
  47. }
  48. }
  49. function staleCacheName($file) {
  50. return $file . '.stale';
  51. }
  52. function protocolName() {
  53. $protocol = 'http';
  54. if ($_SERVER['HTTPS']) {
  55. $protocol = 'https';
  56. }
  57. return $protocol;
  58. }
  59. function adjustProtocol($url) {
  60. return preg_replace('/^https?\:/i', protocolName() . ':', $url);
  61. }
  62. ?>