lib.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. include("res/config.php");
  3. function write_alias($filename, $target)
  4. {
  5. $file_p = fopen($filename, 'w');
  6. fwrite($file_p, "<?php header('Location: " . $target . "'); ?>\n");
  7. fclose($file_p);
  8. return 0;
  9. }
  10. function write_metadata($filename, $source)
  11. {
  12. $metafile = fopen($filename . ".txt", 'w');
  13. if (empty($source))
  14. {
  15. $source = "[citation needed]";
  16. }
  17. fwrite($metafile, "Source:\n\t" . implode('', sanitize_long_input($source, "\t")) . "\n");
  18. fclose($metafile);
  19. }
  20. function sanitize_long_input($sanitizee, $preface)
  21. {
  22. $sanitized = [];
  23. $sani_len = strlen($sanitizee) - 1;
  24. if ($sani_len + 1 == 0)
  25. {
  26. $sanitized = "N/A";
  27. return $sanitized;
  28. }
  29. $j = 0;
  30. for($i = 0; $i <= $sani_len; $i++)
  31. {
  32. if ($i % 80 == 0 && $i != 0)
  33. {
  34. if (substr($sanitizee, $i, 1) == ' ')
  35. {
  36. $sanitized[$j] = "\n";
  37. $sanitized[$j + 1] = $preface;
  38. $j++;
  39. }
  40. else
  41. {
  42. $sanitized[$j] = "-";
  43. $sanitized[$j + 1] = "\n";
  44. $sanitized[$j + 2] = $preface;
  45. $sanitized[$j + 3] = $sanitizee[$i];
  46. $j += 3;
  47. }
  48. }
  49. else
  50. {
  51. $sanitized[$j] = $sanitizee[$i];
  52. }
  53. $j++;
  54. }
  55. return $sanitized;
  56. }
  57. function sanitize_filename($sanitizee)
  58. {
  59. $sanitized = str_replace(" ", "_", $sanitizee);
  60. $sanitized = str_replace("..", "_", $sanitized);
  61. $sanitized = str_replace("/", "_", $sanitized);
  62. $sanitized = str_replace("\\", "_", $sanitized);
  63. return $sanitized;
  64. }
  65. function url_to_filename($url)
  66. {
  67. $working = explode('/', $url);
  68. $filename = $working[count($working) - 1];
  69. return $filename;
  70. }
  71. function get_cwd($url)
  72. {
  73. $url_array = (explode("/", $url));
  74. if (end($url_array))
  75. {
  76. $parts = count($url_array);
  77. $url = $_SERVER["HTTP_HOST"] . implode(array_splice($url_array, 1, $parts - 2));
  78. return $url;
  79. }
  80. else
  81. {
  82. return $url;
  83. }
  84. }
  85. function sanitize_image($path)
  86. {
  87. exec("mogrify -strip " . $path, $result);
  88. }
  89. function celebrate($dest_file, $item_type, $image_url, $image_alt, $meta_data = false)
  90. {
  91. ?>
  92. <title><?php echo($site_name . ": " . $item_type); ?> inserted <3</title>
  93. <p>Thanks for feeding me</p>
  94. <p>Your <?php echo($item_type); ?>'s in safe hands</p>
  95. <img alt="<?php echo($image_alt); ?>" src="<?php echo($image_url); ?>">
  96. <p>Your <?php echo($item_type); ?>'s
  97. <a href="<?php echo ($dest_file); ?>">over here</a></p>
  98. <p><b><?php echo("https://" . get_cwd($_SERVER["REQUEST_URI"]) . "/" . $dest_file); ?></b></p>
  99. <?php
  100. if ($meta_data)
  101. {
  102. ?>
  103. <p>It's meta-stuff (or lack thereof) is
  104. <a href="<?php echo($dest_file); ?>.txt">here</a></p>
  105. <?php
  106. }
  107. }
  108. function celebrate_death($dest_file, $item_type, $image_url, $image_alt, $message)
  109. {
  110. ?>
  111. <title><?php echo($site_name . ": " . $item_type); ?> burned </3</title>
  112. <p>Oh God, I've never seen so much blood—</p>
  113. <p>err, I mean, there're just some minor problems under the hood!</p>
  114. <img alt="<?php echo($image_alt); ?>" src="<?php echo($image_url); ?>">
  115. <?php echo($message); ?>
  116. <?php
  117. }