util.php 1.7 KB

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