Refactoring code, etc.

This commit is contained in:
Jaidyn Lev 2018-10-07 03:25:06 -05:00
parent cea7abff34
commit 5b35bc1798
2 changed files with 140 additions and 57 deletions

View File

@ -37,3 +37,6 @@ $file_beam_die_alt = "A coin on fire.";
$file_beam_item = "coin"; // what the file is affectionately called $file_beam_item = "coin"; // what the file is affectionately called
$file_beam_dir = "p/"; // where the files go $file_beam_dir = "p/"; // where the files go
$image_sanitize_command = "mogrify";
$image_sanitize_args = "strip";

View File

@ -1,114 +1,195 @@
<?php <?php
include("res/config.php"); include("../config.php");
// FILEPATH URL --> RELATIVE_PATH
function write_alias($filename, $target) // SIDE-EFFECT: Written redirect PHP to filepath
// Create a simple PHP redirection file at $filepath to $url;
// return the relative path to it.
function write_alias($filepath, $url)
{ {
$file_p = fopen($filename, 'w'); $file_p = fopen($filepath, 'w');
fwrite($file_p, "<?php header('Location: " . $target . "'); ?>\n"); fwrite($file_p, "<?php header('Location: " . $url . "'); ?>\n");
fclose($file_p); fclose($file_p);
return 0; return $filepath;
} }
function write_metadata($filename, $source)
{
$metafile = fopen($filename . ".txt", 'w');
if (empty($source))
{ // FILEPATH SOURCE_STRING --> RELATIVE_PATH
// SIDE-EFFECT: Written metadata TXT at $filepath.txt
// Create a metadata file for set path;
// return the relative path to it.
function write_metadata($filepath, $source)
{
if (empty($source)) {
$source = "[citation needed]"; $source = "[citation needed]";
} }
$file_p = fopen($filepath . ".txt", 'w');
$source_string = "Source:\n"
. prefix_text(set_line_length($source, 60),
" ");
fwrite($metafile, "Source:\n\t" . implode('', sanitize_long_input($source, "\t")) . "\n"); fwrite($file_p, $source_string);
fclose($metafile); fclose($file_p);
} }
function sanitize_long_input($sanitizee, $preface)
{
$sanitized = [];
$sani_len = strlen($sanitizee) - 1;
if ($sani_len + 1 == 0)
{ // TEXT STRING_PREFIX --> TEXT_STARTING_WITH_PREFIX
$sanitized = "N/A"; // Places $prefix at the start of every line in $text.
return $sanitized; function prefix_text($text, $prefix)
{
$separator = "\r\n";
$line = strtok($text, $separator);
$prefixed_text="";
while ($line !== false) {
$prefixed_text=$prefixed_text . $prefix . $line . "\n";
$line = strtok($separator);
} }
return $prefixed_text;
}
// STRING COLUMN_SIZE_NUMBER --> TEXT_FORMATTED_TO_SIZED_LINES
// Turns all lines in a string into, at the max, sized at $column_size
// Perfect for consistent formatting <3
function set_line_length($original, $line_width)
{
$text_len = strlen($original) - 1;
$line_width = $line_width - 1;
$resized = [];
$new_line = true;
if ($text_len + 1 == 0) {
return 0;
}
// j is the index for $resized, the string deriving from $text
// they will both be different sizes, and so need different indexes
$j = 0; $j = 0;
for($i = 0; $i <= $sani_len; $i++) for($i = 0; $i <= $text_len; $i++) {
{ $cur_character = substr($original, $i, 1);
if ($i % 80 == 0 && $i != 0)
{ if ($i % $line_width == 0 && $i != 0) {
if (substr($sanitizee, $i, 1) == ' ') // if not in middle of word, just start a newline
{ // otherwise, hypenate the word across a newline
$sanitized[$j] = "\n"; if ($cur_character == ' ') {
$sanitized[$j + 1] = $preface; $resized[$j] = "\n";
$j++; $j++;
} }
else else if (is_punctuation($cur_character)) {
{ $resized[$j] = "$cur_character";
$sanitized[$j] = "-"; $resized[$j + 1] = "\n";
$sanitized[$j + 1] = "\n"; $j++;
$sanitized[$j + 2] = $preface;
$sanitized[$j + 3] = $sanitizee[$i];
$j += 3;
} }
else {
$resized[$j] = "-";
$resized[$j + 1] = "\n";
$resized[$j + 2] = $original[$i];
$j += 2;
} }
else
{ $new_line = true;
$sanitized[$j] = $sanitizee[$i]; }
else {
if ($new_line == true && $cur_character == ' ') {
}
else {
$resized[$j] = $original[$i];
}
$new_line = false;
} }
$j++; $j++;
} }
return $sanitized; return implode($resized);
} }
function sanitize_filename($sanitizee)
// CHARACTER --> BOOLEAN
// Check whether or not a character is punctuation.
function is_punctuation($character)
{ {
$sanitized = str_replace(" ", "_", $sanitizee); $punctuations = array("!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
$sanitized = str_replace("..", "_", $sanitized); "[", "{", "}", "]", "`", "~", ";", ":", "'",
$sanitized = str_replace("/", "_", $sanitized); "\"", ",", ".", "<", ">", "/", "?");
$sanitized = str_replace("\\", "_", $sanitized);
return $sanitized; return in_array($character, $punctuations);
} }
// FILENAME --> SAFE_FILENAME_STRING
// Sanitize a filename by replacing common suspicious characters with "_".
function sanitize_filename($filename)
{
$death_characters = array(" ", ",", "/", "\\", "%", "$", "^");
$sanitized_filename = str_replace($death_characters, "_", $filename);
return $sanitized_filename;
}
// ARRAY --> LAST_ELEMENT_OF_ARRAY
// Get the last element in an array.
function last($array)
{
$array[count($array) - 1];
}
// URL --> FILENAME_AT_END_OF_URL
// Return the filename of a URL.
function url_to_filename($url) function url_to_filename($url)
{ {
$working = explode('/', $url); return last(explode('/', $url));
$filename = $working[count($working) - 1];
return $filename;
} }
// ??? --> ???
// ???
function get_cwd($url) function get_cwd($url)
{ {
$url_array = (explode("/", $url)); $url_array = (explode("/", $url));
if (end($url_array)) if (end($url_array)) {
{
$parts = count($url_array); $parts = count($url_array);
$url = $_SERVER["HTTP_HOST"] . implode(array_splice($url_array, 1, $parts - 2)); $url = $_SERVER["HTTP_HOST"] . implode(array_splice($url_array, 1, $parts - 2));
return $url; return $url;
} }
else else {
{
return $url; return $url;
} }
} }
// IMAGE_PATH --> IMAGE_PATH
// Sanitize an image (EXIF, etc) with external program from config.php
function sanitize_image($path) function sanitize_image($path)
{ {
exec("mogrify -strip " . $path, $result); exec($image_sanitize_command . $image_sanitize_args . $path, $result);
return $path;
} }
@ -126,8 +207,7 @@ function celebrate($dest_file, $item_type, $image_url, $image_alt, $meta_data =
<a href="<?php echo ($dest_file); ?>">over here</a></p> <a href="<?php echo ($dest_file); ?>">over here</a></p>
<p><b><?php echo("https://" . get_cwd($_SERVER["REQUEST_URI"]) . "/" . $dest_file); ?></b></p> <p><b><?php echo("https://" . get_cwd($_SERVER["REQUEST_URI"]) . "/" . $dest_file); ?></b></p>
<?php <?php
if ($meta_data) if ($meta_data) {
{
?> ?>
<p>It's meta-stuff (or lack thereof) is <p>It's meta-stuff (or lack thereof) is
<a href="<?php echo($dest_file); ?>.txt">here</a></p> <a href="<?php echo($dest_file); ?>.txt">here</a></p>