ToX82/logHappens

View on GitHub
libs/utilities.php

Summary

Maintainability
A
1 hr
Test Coverage
<?php

/**
 * Transforms the date in a localized readable format
 *
 * @param string $date Date
 * @param bool $localized Localized date time format
 * @return string
 */
function toDateTime($date, $localized = false)
{
    if ($date === '0000-00-00') {
        return '-';
    }

    $date = date('Y-m-d H:i:s', strtotime($date));

    if ($localized === false) {
        return $date;
    }

    $lang = getFullBrowserLanguage();

    if (strpos($lang, ",") !== false) {
        $lang = substr($lang, 0, strpos($lang, ","));
    }

    if (strlen($lang) == 2) {
        $lang = substr($lang, 0, 2) . '_' . strtoupper(substr($lang, 0, 2));
    }

    \Moment\Moment::setLocale($lang);
    $m = new \Moment\Moment($date);

    return $m->format('LLLL:s', new \Moment\CustomFormats\MomentJs());
}

/**
 * Time tracker
 *
 * @return float|bool
 */
function benchmark()
{
    static $start = null;

    if (is_null($start)) {
        $start = getmicrotime();

        return true;
    } else {
        $benchmark = getmicrotime() - $start;
        $start = getmicrotime();

        return round($benchmark, 2);
    }
}

/**
 * Get timestamp
 *
 * @return float
 */
function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());

    return ((float)$usec + (float)$sec);
}

/**
 * Convert memory size in human readable format
 *
 * @param int $size Size
 * @return string
 */
function convert($size)
{
    $unit = ['b', 'kb', 'mb', 'gb', 'tb', 'pb'];

    return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}

/**
 * List settings
 *
 * @param string $parameter Which parameters we need
 * @return mixed
 */
function listSettings($parameter)
{
    $data = [
        'theme' => [
            'default' => 'bootstrap',
            'options' => [
                'bootstrap',
                'cerulean',
                'cosmo',
                'cyborg',
                'darkly',
                'flatly',
                'journal',
                'litera',
                'lumen',
                'lux',
                'materia',
                'minty',
                'morph',
                'pulse',
                'quartz',
                'sandstone',
                'simplex',
                'sketchy',
                'slate',
                'solar',
                'spacelab',
                'superhero',
                'united',
                'vapor',
                'yeti',
                'zephyr',
            ],
        ],
        'refresh' => [
            'default' => 5,
            'options' => [
                '5',
                '15',
                '30',
                '60',
                '120',
            ],
        ],
        'page-length' => [
            'default' => 10,
            'options' => [
                '10',
                '25',
                '50',
                '100',
            ]
        ]
    ];

    return $data[$parameter];
}

/**
 * Gets the user selected theme
 *
 * @param string $parameter Which parameters we need
 * @return mixed
 */
function setting($parameter)
{
    if (isset($_COOKIE[$parameter])) {
        $selected = $_COOKIE[$parameter];
    } else {
        $settings = listSettings($parameter);
        $selected = $settings['default'];
        $selected = writeSettingsCookie($parameter, $selected);
    }

    return $selected;
}

/**
 * Writes the user's selected value into a cookie
 *
 * @param string $parameter parameter's name
 * @param string $selected selected value
 * @return string
 */
function writeSettingsCookie($parameter, $selected)
{
    $settings = listSettings($parameter);

    if (!in_array($selected, $settings['options'])) {
        $selected = $settings['default'];
    }

    setcookie($parameter, $selected, strtotime('+1 year'), '/');

    return $selected;
}

function openFileOrDie($file)
{
    if (is_file($file)) {
        try {
            return file($file);
        } catch (Exception $e) {
            die('Unable to open file: ' . $file . '!');
        }
    }

    return ['Unable to open file: ' . $file . '!'];
}

/**
 * Select a random 404 error
 *
 * @return string
 */
function randomError()
{
    $haiku = [
        "A page like that?<br>It might be very useful.<br>But now it is gone.",
        "This page is not here,<br>The error message spoke clear,<br>Where could it have gone?",
        "Program aborting:<br>Close all that you have worked on.<br>You ask far too much.",
        "Yesterday it worked.<br>Today it is not working.<br>Servers are like that.",
        "A page once existed,<br>But now it's gone, just like that,<br>The internet's cruel.",
        "You step in the stream,<br>But the water has moved on.<br>This page is not here.",
        "The page you seek is lost,<br>Like socks in the dryer's frost,<br>Please try once again.",
        "Serious error.<br>All shortcuts have disappeared.<br>Screen. Mind. Both are blank.",
        "Something you entered<br>transcended parameters.<br>So much is unknown.",
        "Not a pretty sight<br>When the web dies screaming loud<br>The page is not found.",
        "Errors have occurred.<br>We won't tell you where or why.<br>Lazy programmers.",
        "Code path vanished,<br>404 error in sight,<br>Debugging my night.",
        "Algorithmic dance,<br>404 error interrupts,<br>Bug-fix waltz begins.",
    ];

    return $haiku[array_rand($haiku)];
}

/**
 * No errors hakus
 *
 * @return string
 */
function randomHappyness()
{
    $haiku = [
        "No error logs found,<br>Troubleshooting is a maze,<br>Where did the bug hide?",
        "The code runs just fine,<br>And the logs are empty still,<br>No errors to find.",
        "In the land of code,<br>Where magic meets technology,<br>No error logs were found.",
    ];

    return $haiku[array_rand($haiku)];
}

/**
 * Normalize characters in an input string
 *
 * @param string $inputString Input string
 * @return string
 */
function normalizeChars($inputString)
{
    $fix_list = array(
        // 3 char errors first
        '‚' => '‚', '„' => '„', '…' => '…', '‡' => '‡',
        '‰' => '‰', '‹' => '‹', '‘' => '‘', '’' => '’',
        '“' => '“', '•' => '•', '–' => '–', '—' => '—',
        'â„¢' => '™', '›' => '›', '€' => '€',
        // 2 char errors
        'Â'  => 'Â', 'Æ’'  => 'ƒ', 'Ã'  => 'Ã', 'Ä'  => 'Ä',
        'Ã…'  => 'Å', 'â€'  => '†', 'Æ'  => 'Æ', 'Ç'  => 'Ç',
        'ˆ'  => 'ˆ', 'È'  => 'È', 'É'  => 'É', 'Ê'  => 'Ê',
        'Ë'  => 'Ë', 'Å’'  => 'Œ', 'ÃŒ'  => 'Ì', 'Ž'  => 'Ž',
        'ÃŽ'  => 'Î', 'Ñ'  => 'Ñ', 'Ã’'  => 'Ò', 'Ó'  => 'Ó',
        'Ô'  => 'Ô', 'Õ'  => 'Õ', 'Ö'  => 'Ö', 'À'  => 'À',
        '×'  => '×', 'Ëœ'  => '˜', 'Ø'  => 'Ø', 'Ù'  => 'Ù',
        'Å¡'  => 'š', 'Ú'  => 'Ú', 'Û'  => 'Û', 'Å“'  => 'œ',
        'Ãœ'  => 'Ü', 'ž'  => 'ž', 'Þ'  => 'Þ', 'Ÿ'  => 'Ÿ',
        'ß'  => 'ß', '¡'  => '¡', 'á'  => 'á', '¢'  => '¢',
        'â'  => 'â', '£'  => '£', 'ã'  => 'ã', '¤'  => '¤',
        'ä'  => 'ä', 'Â¥'  => '¥', 'Ã¥'  => 'å', '¦'  => '¦',
        'æ'  => 'æ', '§'  => '§', 'ç'  => 'ç', '¨'  => '¨',
        'è'  => 'è', '©'  => '©', 'é'  => 'é', 'ª'  => 'ª',
        'ê'  => 'ê', '«'  => '«', 'ë'  => 'ë', '¬'  => '¬',
        'ì'  => 'ì', '®'  => '®', 'î'  => 'î', '¯'  => '¯',
        'ï'  => 'ï', '°'  => '°', 'ð'  => 'ð', '±'  => '±',
        'ñ'  => 'ñ', '²'  => '²', 'ò'  => 'ò', '³'  => '³',
        'ó'  => 'ó', '´'  => '´', 'ô'  => 'ô', 'µ'  => 'µ',
        'õ'  => 'õ', '¶'  => '¶', 'ö'  => 'ö', '·'  => '·',
        '÷'  => '÷', '¸'  => '¸', 'ø'  => 'ø', '¹'  => '¹',
        'ù'  => 'ù', 'º'  => 'º', 'ú'  => 'ú', '»'  => '»',
        'û'  => 'û', '¼'  => '¼', 'ü'  => 'ü', '½'  => '½',
        'ý'  => 'ý', '¾'  => '¾', 'þ'  => 'þ', '¿'  => '¿',
        'ÿ'  => 'ÿ',

        // 1 char errors last
        'Ã' => 'Á', 'Å' => 'Š', 'í' => 'í', '�' => 'à'
    );

    $error_chars = array_keys($fix_list);
    $real_chars  = array_values($fix_list);

    return str_replace($error_chars, $real_chars, $inputString);
}