playhp /src/_utilities.php

Language PHP Lines 177
MD5 Hash fa68870697a926e0c405c686ffac77fa Estimated Cost $2,256 (why?)
Repository https://bitbucket.org/snowqbe/playhp.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
 * Global, utility functions, PHP additions
 */


if (!defined('P_MODE')) {
    define('P_MODE', 'prod');
}
define('P_DEV', '0');
define('P_USER', '1');

// Root path
$currentDir = str_replace('\\', '/', __DIR__);
$currentDir = substr($currentDir, 0, strpos($currentDir, '/src'));

// Default paths
define('P_ROOT', $currentDir . '/');
define('P_SRC_PATH', P_ROOT . 'src/main/');
define('P_TEST_PATH', P_ROOT . 'src/test/');
define('P_LOCALE_PATH', P_ROOT . 'locale/');
define('P_VIEWS_PATH', P_ROOT . 'views/');
define('P_LAYOUTS_PATH', P_ROOT . 'views/layouts/');

// Cancel magic quotes: it sucks
if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) {
    unslashArray($_GET);
    unslashArray($_POST);
    unslashArray($_COOKIE);
}

/**
 * Lists the subclasses of a given class
 * @param string $parent Parent class name
 * @return array List of subclasses
 */
function getSubclassesOf($parent)
{
    $result = array();
    foreach (get_declared_classes() as $class) {
        if (is_subclass_of($class, $parent))
            $result[] = $class;
    }
    return $result;
}

/**
 * Validates an email
 * @param string $email Email to validate
 * @return bool Status of email validation
 */
function isEmailValid($email)
{
    $atom = '[-a-z0-9!#$%&\'*+\\/=?^_`{|}~]';
    $domain = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';
    $regex = '/^' . $atom . '+(\.' . $atom . '+)*@(' . $domain . '{1,63}\.)+{2,63}$/i';
    return preg_match($regex, $email) == 1;
}

/**
 * Debugging function
 * @param $msg
 * @param bool $bufferingActive
 */
function trace($msg, $bufferingActive = false)
{
    \PlayHP\PlayHP::trace($msg, $bufferingActive);
}

/**
 * Recursive glob call.
 * Does not support flag GLOB_BRACE
 * @link http://php.net/manual/fr/function.glob.php
 */
function globRecursive($pattern, $flags = 0)
{
    $files = glob($pattern, $flags);
    foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
        $files = array_merge($files, globRecursive($dir . '/' . basename($pattern), $flags));
    }
    return $files;
}

/**
 * Gettext shortcut for PlayHP strings
 * @param string $text Text to grab
 * @return string Localized text
 */
function _p($text)
{
    return dgettext(\PlayHP\PlayHP::GETTEXT_DOMAIN, $text);
}

/**
 * Returns a possible name based on the given filename and path.
 * If filename already exists in path, the function proposes a new filename with a counter.
 * Result can be retrieved with path in it or not.
 * @param string $filename The file name to test
 * @param string $path The path where the file should be
 * @param bool|string $get_as_path Flag telling to return the path with the new filename
 * @return string
 */
function getPossibleName($filename, $path, $get_as_path = true)
{
    if (substr($path, -1) == '/') {
        $path = substr($path, 0, strlen($path) - 1);
    }
    if (!file_exists("$path/$filename")) {
        return $get_as_path ? "$path/$filename" : $filename;
    }
    $i = 1;
    while (file_exists("$path/$filename")) {
        $i++;
        $res = pathinfo("$path/$filename");
        $filename = substr($res['basename'], 0, strrpos($res['basename'], '.')) . $i . '.' . $res['extension'];
    }
    return $get_as_path ? "$path/$filename" : $filename;
}


/**
 * Handles unslashing magic quoted elements
 */
function unslashArray(&$array)
{
    foreach ($array as $key => $val) {
        if (is_array($val))
            unslashArray($array[$key]);
        else
            $array[stripslashes($key)] = stripslashes($val);
    }
}

/**
 * @param string $key Key of the parameter to fetch
 * @param null $default Default value to use otherwise
 * @return mixed|null The parameter's value
 */
function mapParam($key, $default = null)
{
    return isset($_GET[$key]) ? $_GET[$key] : (isset($_POST[$key]) ? $_POST[$key] : $default);
}

/**
 * Implements default entities behavior as of PHP 5.4
 * @param string $toencode String to encode
 * @return string Encoded string (in UTF-8)
 */
function entities($toencode)
{
    return htmlentities($toencode, null, 'UTF-8');
}


/**
 * Returns a formatted messages based on an exception
 * @param Exception $e
 * @return string Formatted message
 */
function fmt(\Exception $e)
{
    return '<pre>' .
    $e->getMessage() . "\nIn" .
    $e->getFile() . ' at line ' . $e->getLine() . "\n" . $e->getTraceAsString() . '</pre>';
}


/**
 * Redirects response to a new URL using `header Location: $url`
 * @param string $url URL to redirect to
 */
function redirect($url)
{
    $redirect = 'Location: ' . filter_var($url, FILTER_SANITIZE_URL);
    header($redirect);
    exit;
}
Back to Top