dbHost, $cfg->dbSchema);
$params = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8', time_zone = '+1:00'");
$dbh = new PDO($dsn, $cfg->dbUser, $cfg->dbPassword, $params);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return new Daemon_DbClient($dbh);
}
//prepares multiline text for displaying, also inserts some basic tags
public static function formatMessage($txt, $markup = false)
{
$txt = nl2br(htmlspecialchars($txt, ENT_QUOTES));
if($markup)
{
$txt = preg_replace('@\[img\](.+)\[/img\]@uU', '', $txt);
$txt = preg_replace('@\[url=(.+)\](.+)\[/url\]@uU', '$2', $txt);
$txt = preg_replace('@\[url\]([img]){0}(.+)\[/url\]@uU', '$1', $txt);
$txt = preg_replace('@(^|>|\s)(https://\S+)($|<|\s)@muU', '$1$2$3', $txt);
$txt = preg_replace('@(^|>|\s)(http://\S+)($|<|\s)@muU', '$1$2$3', $txt);
$txt = preg_replace('@\[b\](.+)\[/b\]@uU', '$1', $txt);
$txt = preg_replace('@\[i\](.+)\[/i\]@uU', '$1', $txt);
$txt = preg_replace('@\[u\](.+)\[/u\]@uU', '$1', $txt);
$txt = preg_replace('@\[s\](.+)\[/s\]@uU', '$1', $txt);
$txt = preg_replace('@\[sub\](.+)\[/sub\]@uU', '$1', $txt);
$txt = preg_replace('@\[sup\](.+)\[/sup\]@uU', '$1', $txt);
}
return $txt;
}
//returns value from array, or defaults if it doesn't exist
public static function getArrayValue(array $a, $name, $default = null)
{
return isset($a[$name]) ? $a[$name] : $default;
}
//implodes whitespace, optionally preserving newlines
public static function normalizeString($string, $preserveNewlines = false)
{
$string = str_replace(array("\r\n","\r"), "\n", $string); //unix newlines
if($preserveNewlines)
$string = preg_replace('/[^\S\n]+/', ' ', $string);
else $string = preg_replace('/\s+/', ' ', $string);
$string = trim($string);
return $string;
}
//generates a password hash
public static function passwordHash($salt, $text)
{
return sha1($salt . $text);
}
//returns random salt for password hashing
public static function passwordSalt()
{
$c0 = ord('0');
$cA = ord('a');
$cZ = ord('z');
$max = 10+$cZ-$cA;
$salt = '';
for($i = 0; $i < 8; ++$i)
{
$x = mt_rand(0, $max);
if($x < 10)
$salt .= chr($c0 + $x);
else $salt .= chr($cA + $x - 10);
}
return $salt;
}
//redirects to selected url
public static function redirect($url)
{
session_write_close(); //just in case
header('Content-Type: text/html; charset=UTF-8');
header(sprintf('Location: %s', $url), true, 303); //"See Other" status
printf('