Skip to content

Instantly share code, notes, and snippets.

@Exadra37
Last active August 29, 2015 14:13
Show Gist options
  • Save Exadra37/89be313b369686f603a8 to your computer and use it in GitHub Desktop.
Save Exadra37/89be313b369686f603a8 to your computer and use it in GitHub Desktop.
<?php
/**
* The helper function inside section DEBUG will allow to use var_dump and print_r
* with an formatted output.
*
* The functions ddv(), ddp(), dv(), dp() will cal dump(),
* that for is turn will call isAllowedToDebug() to check if we need to check the ip address
* and in that case if the remote ip address is in the allowed ip addresses.
*
* Function name convention:
* - ddv() stands for Dump Die var_dump()
* - ddp() stands for Dump Die print_r()
* - dv() stands for Dump var_dump()
* - dp() stands for Dump print_r()
*
* Configuration is available in Nn4m/Common/src/config/helpers.php:
* - put all the ip addresses allowed to debug inside of array debug.dev_ip_addresses
* - Enable or disable check for ip address using debug.check_ip_address
*
* @author Paulo Silva(Exadra37) <[email protected]>
* @package Exadra37\Debug
* @version 1.0.1
* @since 29/05/2014 - v.1.0.1
* @since 08/05/2014 - v.1.0.0
*/
/**
* START SECTION DEBUG
*/
if (!function_exists('isAllowedToDebug')) {
/**
* Check if the current Ip Address is allowed to debug
*
* @date 2014-05-08T18:58:02+0000
* @return boolean [description]
*/
function isAllowedToDebug($checkIpAddress = true)
{
$debugByIpAddress = isset($_GET['debug']) && $_GET['debug'] == 'true';
$devIpAddresses = array('12.345.67.89');
$remoteIpAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
if ($debugByIpAddress && $checkIpAddress && isset($remoteIpAddress) && !in_array($remoteIpAddress, $devIpAddresses)) {
$debugByIpAddress = false;
$allowedIpAddresses = implode(',', $devIpAddresses);
$logFile = '/tmp/log/debugIp/isAlllowedToDebug.log';
$msg = "The {$remote_ip_address} have attempted to use debug mode, but is not in allowed ip addresses '{$allowedIpAddresses}'.\n\n";
@file_put_contents($logFile, $msg, FILE_APPEND | LOCK_EX);
//@\Log::warning("The {$remote_ip_address} have attempted to use debug mode, but is not in allowed ip addresses '{$allowedIpAddresses}'.");
//die('Not Allowed to Debug');
}
return $debugByIpAddress;
}
}
if (!function_exists('ddv')) {
/**
* Dump Die Vardump(ddv)
* Wrap it in a pre tag for formatted output.
* Dies after dump.
*
* @date 2014-05-07T21:11:05+0000
* @param mixed $var - var to be dumped
* @return void
*/
function ddv($var, $msg = null)
{
if (function_exists('debugThisVar')) {
debugThisVar($var, $msg, true, true);
}
}
}
if (!function_exists('ddp')) {
/**
* Dump Die Printr(ddp)
* Wrap it in a pre tag for formatted output.
* Dies after dump.
*
* @date 2014-05-07T21:13:05+0000
* @param mixed $var - var to be dumped
* @return void
*/
function ddp($var, $msg = null)
{
if (function_exists('debugThisVar')) {
debugThisVar($var, $msg, false, true);
}
}
}
if (!function_exists('dv')) {
/**
* Dump Vardump(dv)
* Wrap it in a pre tag for formatted output.
* Continue execution after dump.
*
* @date 2014-05-07T21:16:05+0000
* @param mixed $var - var to be dumped
* @return void
*/
function dv($var, $msg = null)
{
if (function_exists('debugThisVar')) {
debugThisVar($var, $msg, true, false);
}
}
}
if (!function_exists('dp')) {
/**
* Dump Printr(dp)
* Wrap it in a pre tag for formatted output.
* Continue execution after dump.
*
* @date 2014-05-07T21:20:05+0000
* @param mixed $var - var to be dumped
* @return void
*/
function dp($var, $msg = null)
{
if (function_exists('debugThisVar')) {
debugThisVar($var, $msg, false, false);
}
}
}
if (!function_exists('debugThisVar')) {
/**
* Dump any type of var.
* Wraps the dump in pre tag for formatted output.
* Support the use of var_dump($var) or print_r($var).
* By default will stop script execution after dump, but can be changed.
*
* @date 2014-05-07T21:08:24+0000
* @param mixed $var - var to be dumped
* @param boolean $varDump - Enable or disable var_dump($var)
* @param boolean $die - Enable or disable print_r($var)
* @return void
*/
function debugThisVar($var, $msg = null, $varDump = false, $die = true)
{
if (function_exists('isAllowedToDebug')) {
if (isAllowedToDebug()) {
echo "<hr>";
if (!empty($msg)) {
echo "<strong>{$msg}: </strong>";
}
if ($varDump) {
echo"\n<pre>";
var_dump($var);
} else {
echo"\n<pre>";
print_r($var);
}
echo"</pre>";
echo "<hr>";
if ($die) {
die();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment