util.php 1.7 KB

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