The EuroCMS Core functions

Make PHPStorm know custom php functions: read https://www.jetbrains.com/help/phpstorm/symfony-creating-helper-functions.html#method-parameter

These are the core functions that are readily available for your usage.

__ecms_BlockChar()

In the url bar, only alpha numeric value are allowed and nothing else.

This function checks if the url bar has blocked charachters in it.

function __ecms_BlockChar($url) {
	$value = preg_replace('/[a-zA-Z0-9\/\-]/', '', $url);
	if(empty($value)) {
		return false
	} else {
		return true;		
	}
}

Usage

if(__ecms_BlockChar()) {
	die('Block chars detected.');
}

__ecms_dump($dump);

A pretty output of var_dump, using the html tag <pre> ... </pre>

Code

/**
* var dump array
*
* @param $array variable Dumps information about a variable
* 
*/
function __ecms_dump($array) {
    echo "<pre>" . var_dump("$array"); . "</pre>";
}

Usage

__ecms_dump($array);

__ecms_GLOBALS($TYPE,$Value)

Pretty <table> ... </table> output of the $GLOBALS.

  • _GET
  • _POST
  • _COOKIE
  • _FILES
  • argv
  • argc
  • _SERVER

$GLOBALS — References all variables available in global scope.

An associative array containing references to all variables which are currently defined in the global scope of the script.

The variable names are the keys of the array.

Code:

__ecms_GLOBALS("$FROM","$VALUE") {
     return filter_input($FROM, "$VALUE", FILTER_SANITIZE_STRING);
}

Usage:

echo __ecms_GLOBALS("SERVER","REMOTE_ADDR");

__ecms_MiniCSS($css)

Minify css content

Code

function __ecms_MiniCSS($CSS) {
    // Remove comments
    $CSS = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $CSS);

    // Remove space after colons
    $CSS = str_replace(': ', ':', $CSS);

    // Remove whitespace
    $CSS = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $CSS);
    return $CSS;
}

Usage

echo __ecms_MiniCSS($CSS);

__ecms_MiniJS($JavaScript)

Minify js content

Code

function __ecms_MiniJS($JavaScript) {
    $blocks = array('for', 'while', 'if', 'else');
    $javascript = preg_replace('/([-\+])\s+\+([^\s;]*)/', '$1 (+$2)', $javascript);

    $pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/';
    $javascript = preg_replace($pattern, '', $javascript);


    // remove new line in statements
    $javascript = preg_replace('/\s+\|\|\s+/', ' || ', $javascript);
    $javascript = preg_replace('/\s+\&\&\s+/', ' && ', $javascript);
    $javascript = preg_replace('/\s*([=+-\/\*:?])\s*/', '$1 ', $javascript);

    // handle missing brackets {}
    foreach ($blocks as $block){
        $javascript = preg_replace('/(\s*\b' . $block . '\b[^{\n]*)\n([^{\n]+)\n/i', '$1{$2}', $javascript);
    }

    // handle spaces
    $javascript = preg_replace(array("/\s*\n\s*/", "/\h+/"), array("\n", " "), $javascript); // \h+ horizontal white space
    $javascript = preg_replace(array('/([^a-z0-9\_])\h+/i', '/\h+([^a-z0-9\$\_])/i'), '$1', $javascript);
    $javascript = preg_replace('/\n?([[;{(\.+-\/\*:?&|])\n?/', '$1', $javascript);
    $javascript = preg_replace('/\n?([})\]])/', '$1', $javascript);
    $javascript = str_replace("\nelse", "else", $javascript);
    $javascript = preg_replace("/([^}])\n/", "$1;", $javascript);
    //$javascript = preg_replace("/;?\n/", ";", $javascript);
    return $javascript;
}

usage

echo __ecms_MiniJS($JavaScripts);

__ecms_GetClientIP

Get the visitor IP and such.

Code

function __ecms_GetClientIP(){

    if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)){

        return  $_SERVER["HTTP_X_FORWARDED_FOR"];

    }else if (array_key_exists('REMOTE_ADDR', $_SERVER)) {

        return $_SERVER["REMOTE_ADDR"];

    }else if (array_key_exists('HTTP_CLIENT_IP', $_SERVER)) {

        return $_SERVER["HTTP_CLIENT_IP"];
    }

    return '';
}

usage

echo __ecms_GetClientIP();

__ecms_IPInfo($IP)

Get IP information about the visitor.

__ecms_CronCheck()

Check if the code is being excecuted via cron.

Code

// Checks if you are being executed via cron
function __ecms_CronCheck() {

    // Lets see if our predefined constant exists
    if (defined('PHP_SAPI')) {

        $sapi_type = constant('PHP_SAPI');

    } else {

        $sapi_type = php_sapi_name();

    }

    if (preg_match('/cli/i', $sapi_type)) {

        return true;

    }

}

Usage

__ecms_HTMLENT($HTML)

Return only the html entity

Code

/**
* Convert all applicable characters to HTML entities
*
* @param $HMTL string string of HTML entities
* 
*/
__ecms_HTMLENT($HTML) {
	return htmlentities($HTML, ENT_QUOTES);	
}

Usage

echo __ecms_HTMLENT($HTML);

__ecms_IsBinary($content)

Check if the content given is a binary file

__ecms_BotCheck()

Checks if the current user agent is a bot

Code

function __ecms_BotCheck() {
    // Checking if the current HTTP_USER_AGENT is a bot
    $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
    if (preg_match('/bot|spider/i', $HTTP_USER_AGENT)) {
        // This is a bot
        return true;
    } else {
        return false;
    }
}

usage

if(__ecms_BotCheck()) {
	die('You are not allowed here...');
}

__ecms_UserAgent()

Put the browscap data in a variable. (Or just return the array?)

code

function __ecms_UserAgent() {
	return get_browser(null, true);
//	$browser = get_browser(null, true);
//	foreach($browser as $key => $value) {
//		define("browscap_$key", "$value");    
//		browscap_parent Firefox 0.9
//	}
}

usage

__ecms_UserAgent()

__ecms_Users()

Get user display name.

  • Make a class out of this?
  • Figure out the security implications with this
  • Make this only available for modules?
  • Create an admin namespace?

code

# Test this.
function __ecms_Users($IDs) {
	return DataBase::Query("SELECT dname,profile_pic from users WHERE user_id IN ($IDs);");
}

usage

__ecms_Users()