<?php defined('SYSPATH') OR die('No direct access to this file is allowed.');
/**
* Creates a new unique identifier. Uses Fisher-Yates shuffle for better
* randomizing.
*
* @param int $length How many character in length should the UID be?
* @return string The new, randomly generated UID.
*/
function createuid($length = 32) {
$valid = array_merge(range('A', 'Z'), range('a', 'z'), range(0, 9));
$uid = '';
$valid = fyshuffle($valid);
for ($i = 0; $i < $length; $i++) {
$uid .= $valid[mt_rand(0, count($valid) - 1)];
}
return $uid;
}
function has_right($right) {
global $acl;
return $acl->$right;
}
function make_array($bunch) {
if (!is_array($bunch)) {
$bunch = explode(',', $bunch);
}
// Remove blank members of the array.
for ($i = 0, $c = count($bunch); $i < $c; $i++) {
if (trim($bunch[$i]) == '') {
unset($bunch[$i]);
}
}
// Re-index the array so keys are sequential, starting at zero.
if (is_array($bunch)) {
$bunch = array_values($bunch);
}
// If, for some reason, the final is not an array, force it.
if (!is_array($bunch)) {
$bunch = array($bunch);
}
return $bunch;
}
function bake($name, $value, $expiry) {
global $site;
setcookie($name, $value, $expiry, '/', '.'.$site->getrootdomain());
}
function cookie($cookie = '') {
$return = false;
if ($cookie != '') {
if (cookie_required($cookie)) {
$return = trim($_COOKIE[$cookie]);
}
}
return $return;
}
function cookie_required($bunch) {
$return = false;
$bunch = make_array($bunch);
$res = 0;
for ($i = 0, $c = count($bunch); $i < $c; $i++) {
if (isset($_COOKIE[trim($bunch[$i])]) && $_COOKIE[trim($bunch[$i])] != '') {
$res++;
}
}
if ($res == count($bunch)) {
$return = true;
}
return $return;
}
function form($post = '') {
$return = false;
if ($post != '') {
if (form_required($post)) {
$return = trim($_POST[$post]);
}
}
return $return;
}
function form_required($bunch) {
$return = false;
$bunch = make_array($bunch);
$res = 0;
for ($i = 0, $c = count($bunch); $i < $c; $i++) {
if (isset($_POST[trim($bunch[$i])]) && $_POST[trim($bunch[$i])] != '') {
$res++;
}
}
if ($res == count($bunch)) {
$return = true;
}
return $return;
}
function clean($dirty) {
$return = '';
$return = htmlentities(str_replace('</p>', "\n", $dirty), ENT_QUOTES);
return $return;
}
/**
* Performs a Fisher-Yates shuffle on input array. Does not affect original.
*
* @param array|mixed $array
* @return array New array of shuffled values.
*/
function fyshuffle($array) {
if (!is_array($array)) {
$array = array($array);
}
$i = count($array);
$j = 0;
$tmp = '';
while(--$i) {
$j = mt_rand(0, $i);
if ($i != $j) {
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
function locate($url, $nolog = false) {
if (isset($url) && $url != '') {
if (!$nolog) {
write_log(1);
}
//echo $url;
header('Location: '.$url);
}
}
function write_log($redirected = 0) {
global $logwritten, $user, $site, $template, $timerstart, $db;
if (!$logwritten && is_object($db)) {
$log = array();
$log['sessionid'] = null;
$log['debug'] = ini_get('display_errors');
$log['userid'] = null;
$log['userrole'] = null;
$log['redirected'] = $redirected;
$log['url'] = null;
$log['GET'] = null;
$log['POST'] = null;
$log['status'] = null;
$log['remoteip'] = null;
$log['useragent'] = null;
$log['referer'] = null;
$log['startdate'] = null;
$log['requesttime'] = null;
$log['querycount'] = null;
if (isset($user)) {
$log['sessionid'] = $user->loginhash;
$log['userid'] = $user->uid;
$log['userrole'] = $user->roleid;
}
if (isset($template)) {
$log['querycount'] = $template->get('querycount');
$log['status'] = $template->httpstatus;
}
$log['GET'] = json_encode($_GET);
if ($log['GET'] == '[]') {
$log['GET'] = null;
}
if (form_required('password')) {
$_POST['password'] = sha1(form('password'));
}
if (form_required('signuppass')) {
$_POST['signuppass'] = sha1(form('signuppass'));
}
if (form_required('signuppass2')) {
$_POST['signuppass2'] = sha1(form('signuppass2'));
}
$log['POST'] = json_encode($_POST);
if ($log['POST'] == '[]') {
$log['POST'] = null;
}
if (isset($_SERVER['REMOTE_ADDR'])) {
$log['remoteip'] = $_SERVER['REMOTE_ADDR'];
}
if (isset($_ENV['HTTP_USER_AGENT'])) {
$log['useragent'] = $_ENV['HTTP_USER_AGENT'];
}
if (isset($_SERVER['REMOTE_ADDR'])) {
$log['remoteip'] = $_SERVER['REMOTE_ADDR'];
}
if (isset($site)) {
$log['referer'] = $site->referrer;
$log['url'] = $site->geturi();
}
if (isset($timerstart)) {
$log['startdate'] = $timerstart;
$log['requesttime'] = microtime(true) - $timerstart;
}
//print_r($log);
$fields = '';
$values = array();
foreach ($log as $field => $value) {
$fields .= $field.',';
$values[] = $value;
}
$fields = substr($fields, 0, strlen($fields) - 1);
$counts = str_repeat('?,', count($values));
$counts = substr($counts, 0, strlen($counts) - 1);
$sql = 'insert into '.PREFIX.'log ('.$fields.') values ('.$counts.')';
$db->query($sql, $values);
$logwritten = true;
}
}
function form_preserve($mixed) {
if (!is_array($mixed)) {
$mixed = array($mixed => null);
}
bake('fpf', json_encode($mixed), 0);
}
function form_preserved() {
$return = false;
if (cookie_required('fpf')) {
$return = cookie('fpf');
bake('fpf', '', 0);
}
return json_decode($return, true);
}