Skip to content

Instantly share code, notes, and snippets.

@edavis25
Last active May 17, 2017 21:39
Show Gist options
  • Save edavis25/943d50ef602232c891b5193ca3e92671 to your computer and use it in GitHub Desktop.
Save edavis25/943d50ef602232c891b5193ca3e92671 to your computer and use it in GitHub Desktop.
A small collection of helpful PHP functions
<?php
/**
* PHP utility functions, parameter processing functions, and the renderTemplate functions
* Taken from Franklin's WEBD-236 course
* Authors: Todd Whittaker & Scott Sharkey
*/
function markdown($str) {
$str = htmlspecialchars(ltrim($str),ENT_QUOTES);
$str = preg_replace('/\*\*(.+)\*\*/u', '<b>$1</b>', $str);
$str = preg_replace('/\*([^\*]+)\*/u', '<i>$1</i>', $str);
$str = preg_replace('/#### ([^\n]*)\n/', "<h4>$1</h4>\n", $str);
$str = preg_replace('/### ([^\n]*)\n/', "<h3>$1</h3>\n", $str);
$str = preg_replace('/## ([^\n]*)\n/', "<h2>$1</h2>\n", $str);
$str = preg_replace('/# ([^\n]*)\n/', "<h1>$1</h1>\n", $str);
$str = preg_replace('/\[([^\]]+)\]\(([^\)]+)\)/', '<a href=\'$2\'>$1</a>', $str);
$str = preg_replace('/([^\n\r]{2,})(?:(\r\n){2,}|\r{2,}|\n{2,}|$)/u', "<p>$1</p>\n\n", $str);
return $str;
}
function currentUser() {
$inSession = session_id();
if (!empty($inSession)) {
if (isset($_SESSION['UserID'])) {
return $_SESSION['UserID'];
}
}
return 0;
}
function isLoggedIn() {
$inSession = session_id();
if (!empty($inSession)) {
if (isset($_SESSION['loggedin'])) {
return $_SESSION['loggedin'];
}
}
return false;
}
function ensureLoggedIn() {
if (!isLoggedIn()) {
$_SESSION['redirect'] = $_SERVER['REQUEST_URI'];
redirectRelative('login');
exit();
}
}
function ensureLoggedInUserIs($id) {
ensureLoggedIn();
if ($_SESSION['UserID'] != $id) {
Logger::instance() -> warn("User $id attempted unauthorized access to: " . $_SERVER['REQUEST_URI']);
die("You aren't authorized for this action. This attempt has been logged.");
}
}
function validate_present($elements) {
$errors = '';
foreach ($elements as $element) {
if (!isset($_POST[$element]) || empty($_POST[$element])) {
$errors .= "&bull; Missing $element<br />\n";
}
}
return $errors;
}
// Dump an array in a pretty format...
function dumpArray($elements) {
$result = "<ol>\n";
foreach ($elements as $key => $value) {
if (is_array($value)) {
$result .= "<li>Key <b>$key</b> is an array
containing:\n" . dumpArray($value) . "</li>";
} else {
$value = nl2br(htmlspecialchars($value));
$result .= "<li>Key <b>$key</b> has value <b>$value</b></li>\n";
}
}
return $result . "</ol>\n";
}
// Function to safely fetch a value from an array, supplying a default
// if no key is present.
function safeGet($array, $key, $default=false) {
if (isset($array[$key])) {
$value = $array[$key];
if (!is_array($value)) {
$value = htmlspecialchars(trim($array[$key]));
}
if ($value) {
return $value;
}
}
return $default;
}
// Function to safely get a variable from an array by index, supplying
// a default if no key is present.
function safeParam($arr, $index, $default=false) {
if ($arr && isset($arr[$index])) {
return htmlentities($arr[$index]);
}
return $default;
}
// Dump a variable in a nice debug <div>...
function debug($something) {
echo "<div class='debug'>\n";
print_r($something);
echo "\n</div>\n";
}
// Redirect to a different location....
function redirect($url) {
header("Location: $url");
exit();
}
// Functions for handling relative urls...
function redirectRelative($url) {
redirect(relativeURL($url));
}
function relativeUrl($url) {
$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$scriptName = explode('/', $_SERVER['SCRIPT_NAME']);
$dir = array();
for ($i = 0; $i < sizeof($scriptName); $i++) {
if ($requestURI[$i] == $scriptName[$i]) {
$dir[] = $requestURI[$i];
} else {
break;
}
}
return implode('/', $dir) . '/' . $url;
}
function __resolveRelativeUrls($matches) {
return relativeUrl($matches[1]);
}
// Template utility functions...
function __importTemplate($matches) {
$fileName = $matches[1];
if (!file_exists($fileName)) {
die("Template $fileName doesn't exist.");
}
$contents = file_get_contents($fileName);
$contents = preg_replace_callback('/%%\s*(.*)\s*%%/', '__importTemplate', $contents);
return $contents;
}
function __cacheName($view) {
$cachePath = explode('/', $view);
$idx = sizeof($cachePath) - 1;
$cachePath[$idx] = 'cache_' . $cachePath[$idx];
return implode('/', $cachePath);
}
// Function to render a template. $view is the view template, $params are
// passed to the template.
function renderTemplate($view, $params, $asString = false) {
if (!file_exists($view)) {
die("File $view doesn't exist.");
}
# do we have a cached version?
clearstatcache();
$cacheName = __cacheName($view);
if (file_exists($cacheName) && (filemtime($cacheName) >= filemtime($view))) {
$contents = file_get_contents($cacheName);
} else {
# we need to build the file (doesn't exist or template is newer)
$contents = __importTemplate(array('unused', $view));
$contents = preg_replace_callback('/@@\s*(.*)\s*@@/U', '__resolveRelativeUrls', $contents);
$patterns = array(
array('src' => '/{{/', 'dst' => '<?php echo('),
array('src' => '/}}/', 'dst' => '); ?>'),
array('src' => '/\[\[/', 'dst' => '<?php '),
array('src' => '/\]\]/', 'dst' => '?>')
);
foreach ($patterns as $pattern) {
$contents = preg_replace($pattern['src'], $pattern['dst'], $contents);
}
file_put_contents($cacheName, $contents);
}
extract($params);
ob_start();
eval("?>" . $contents);
$result = ob_get_contents();
ob_end_clean();
if (!$asString) {
echo $result;
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment