piler/webui/system/misc.php

544 lines
13 KiB
PHP
Raw Normal View History

2012-02-08 23:14:28 +01:00
<?php
function LOGGER($event = '', $username = '') {
if($event == "") { return 0; }
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
if($username == '') {
2013-11-18 19:24:33 +01:00
if($session->get("username")) { $username = $session->get("username"); }
2012-02-08 23:14:28 +01:00
else { $username = 'unknown'; }
}
syslog(LOG_INFO, "username=$username, event='$event'");
2012-02-08 23:14:28 +01:00
}
2012-09-14 11:54:09 +02:00
function AUDIT($action = 0, $email = '', $ipaddr = '', $id = 0, $description = '') {
2012-02-08 23:14:28 +01:00
if(ENABLE_AUDIT == 0) { return 0; }
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
if($ipaddr == '' && isset($_SERVER['REMOTE_ADDR'])) { $ipaddr = $_SERVER['REMOTE_ADDR']; }
2013-11-18 19:24:33 +01:00
if($email == '') { $email = $session->get("email"); }
2012-02-08 23:14:28 +01:00
2013-07-12 15:02:50 +02:00
$a = explode("@", $email);
2012-02-08 23:14:28 +01:00
$db = Registry::get('db');
2014-08-11 10:40:18 +02:00
$description = htmlspecialchars($description);
2013-07-12 15:02:50 +02:00
$query = $db->query("INSERT INTO " . TABLE_AUDIT . " (ts, email, domain, action, ipaddr, meta_id, description) VALUES(?,?,?,?,?,?,?)", array(time(), $email, $a[1], $action, $ipaddr, $id, $description));
2012-09-14 11:54:09 +02:00
2012-02-08 23:14:28 +01:00
return $db->countAffected();
}
function getAuthenticatedUsername() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
if($session->get("username")) { return $session->get("username"); }
2012-02-08 23:14:28 +01:00
return "";
}
function isAdminUser() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
if($session->get("admin_user") == 1){ return 1; }
2012-02-08 23:14:28 +01:00
return 0;
}
function isAuditorUser() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
if($session->get("admin_user") == 2){ return 1; }
2012-02-08 23:14:28 +01:00
return 0;
}
function isReadonlyAdmin() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
if($session->get("admin_user") == 3){ return 1; }
2012-02-08 23:14:28 +01:00
return 0;
}
function logout() {
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
AUDIT(ACTION_LOGOUT, $session->get("email"), '', '', '');
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session->set("username", "");
$session->set("admin_user", 0);
2012-02-08 23:14:28 +01:00
2013-11-18 19:24:33 +01:00
$session->remove("username");
$session->remove("admin_user");
2012-02-08 23:14:28 +01:00
Registry::set('username', '');
2012-09-21 20:26:49 +02:00
session_destroy();
2012-02-08 23:14:28 +01:00
}
function isBinary($num = '') {
if($num == 0 || $num == 1){ return 1; }
return 0;
}
function get_page_length() {
$page_len = PAGE_LEN;
2013-11-18 19:24:33 +01:00
$session = Registry::get('session');
if($session->get("pagelen") && is_numeric($session->get("pagelen")) && $session->get("pagelen") >= 10 && $session->get("pagelen") <= MAX_SEARCH_HITS) {
$page_len = $session->get("pagelen");
2012-02-08 23:14:28 +01:00
}
return $page_len;
}
function checkemail($email, $domains) {
if(validemail($email) == 0){
return 0;
}
if($email == 'admin@local') { return 1; }
list($u, $d) = explode('@', $email);
foreach ($domains as $domain) {
if($domain == $d){ return 1; }
}
return -1;
}
function validemail($email = '') {
if($email == '') { return 0; }
2012-07-27 10:01:28 +02:00
if(preg_match("/@local$/", $email)) { return 1; }
2012-02-08 23:14:28 +01:00
2014-02-26 12:37:17 +01:00
if(preg_match('/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,10})$/', $email)) {
2012-02-08 23:14:28 +01:00
return 1;
}
return 0;
}
2013-01-05 16:42:36 +01:00
function checkdomain($domain, $domains) {
if(validdomain($domain) == 0){
return 0;
}
if($domain == 'local') { return 1; }
if(in_array($domain, $domains) ) {
return 1;
} else {
return -1;
}
}
function validdomain($domain = '') {
if($domain == '') { return 0; }
if(preg_match("/@local$/", $domain)) { return 1; }
2014-02-26 12:37:17 +01:00
if(preg_match('/@?[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,10})$/', $domain)) {
2013-01-05 16:42:36 +01:00
return 1;
}
return 0;
}
2012-02-08 23:14:28 +01:00
function first_n_characters($what, $n){
$x = "";
$len = 0;
$a = explode(" ", $what);
while(list($k, $v) = each($a)){
$x .= "$v "; $len += strlen($v) + 1;
if($len >= $n){ return $x . "..."; }
}
return $x . "...";
}
function short_email($email) {
if(strlen($email) > 25) {
return substr($email, 0, 25) . "...";
}
else return $email;
}
function verify_piler_id($id = '') {
if($id == '') { return 0; }
if(preg_match("/^([0-9a-f]+)$/", $id)) { return 1; }
return 0;
}
2012-05-11 23:42:55 +02:00
function generate_random_string($length = 8) {
2012-02-08 23:14:28 +01:00
$rnd = "";
$aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9));
for($c=0; $c < $length; $c++) {
$rnd .= $aZ09[mt_rand(0, count($aZ09)-1)];
}
2012-05-11 23:42:55 +02:00
return $rnd;
}
function createTempName($dir = '', $prefix = '') {
return $dir . "/" . $prefix . generate_random_string(8);
2012-02-08 23:14:28 +01:00
}
function read_konfig($configfile = '') {
$cfg = array();
if($configfile == '') { return $cfg; }
$fp = fopen($configfile, "r");
if($fp) {
while(($line = fgets($fp, 4096))) {
$line = trim($line);
if($line[0] == '#' || $line[0] == ';') { continue; }
list($host, $basedn, $binddn, $bindpw, $type, $domain, $gid, $policy_group) = explode(":", $line);
$cfg[] = array(
'ldap_host' => $host,
'ldap_basedn' => $basedn,
'ldap_binddn' => $binddn,
'ldap_bindpw' => $bindpw,
'type' => $type,
'domain' => $domain,
'gid' => $gid,
'policy_group' => $policy_group
);
}
fclose($fp);
}
return $cfg;
}
function my_qp_encode($s){
$res = "";
$a = explode("\n", $s);
while(list($k, $v) = each($a)){
$part = "";
for($i=0; $i<strlen($v); $i++){
//if($i > 0 && ($i % 76) == 0) $part .= "=\r\n";
$c = substr($v, $i, 1);
if(ord($c) >= 128){
$c = "=" . strtoupper(dechex(ord($c)));
}
if($c == ' ') { $c = '_'; }
$part .= $c;
}
$res .= $part . "\n";
}
return $res;
}
2012-02-12 16:16:54 +01:00
function nice_size($size = 0, $space = '') {
2012-02-08 23:14:28 +01:00
if($size < 1000) return "1k";
2013-07-29 22:34:44 +02:00
if($size < 1000000) return round($size/1000) . $space . "k";
if($size < 1000000000) return round($size/1000000) . $space . "M";
2012-02-08 23:14:28 +01:00
2013-07-29 22:34:44 +02:00
return sprintf("%.1f", $size/1000000000) . $space . "G";
2012-02-08 23:14:28 +01:00
}
function assemble_search_url($term = '') {
$term_value = "";
if($term == '') { return $term_value; }
parse_str($term, $a);
if(isset($a['search'])) { $term_value = $a['search']; }
if(isset($a['f'])) {
foreach($a['f'] as $f) {
$val = array_shift($a['v']);
if($val == '') { continue; }
if($f == 'from') { $term_value .= ", from: " . $val; }
if($f == 'to') { $term_value .= ", to: " . $val; }
if($f == 'subject') { $term_value .= ", subj: " . $val; }
if($f == 'body') { $term_value .= ", body: " . $val; }
}
}
if(isset($a['from'])) { $term_value .= ", from: " . $a['from'];}
if(isset($a['to'])) { $term_value .= ", to: " . $a['to'];}
if(isset($a['subject'])) { $term_value .= ", text: " . $a['subject'];}
if(isset($a['date1'])) { $term_value .= ", date1: " . $a['date1']; }
if(isset($a['date2'])) { $term_value .= ", date2: " . $a['date2']; }
if(isset($a['direction'])) { $term_value .= ", d: " . $a['direction']; }
if(isset($a['sort'])) { $term_value .= ", sort: " . $a['sort']; }
if(isset($a['order'])) { $term_value .= ", order: " . $a['order']; }
$term_value = preg_replace("/^\, /", "", $term_value);
return $term_value;
}
function fix_email_address($email = '') {
return preg_replace("/(^\ {0,}|\ {0,}$)/", "", $email);
}
function assemble_search_term($data = array()) {
$term = "";
if(isset($data['advanced']) && $data['advanced'] == 1) {
$term = "advanced=1&date1=" . $data['date1'] . "&date2=" . $data['date2'];
$term .= "&from=" . $data['from'];
for($i=2; $i<MAX_NUMBER_OF_FROM_ITEMS; $i++) {
if(isset($data['from'.$i])) { $term .= "&from$i=" . $data['from'.$i]; }
}
$term .= "&to=" . $data['to'];
for($i=2; $i<MAX_NUMBER_OF_FROM_ITEMS; $i++) {
if(isset($data['to'.$i])) { $term .= "&to$i=" . $data['to'.$i]; }
}
$term .= "&subj=" . $data['subj'] . "&body=" . $data['body'] . "&tag=" . $data['tag'];
if(isset($data['w_attachment'])) { $term .= "&w_attachment=on"; }
if(isset($data['wo_attachment'])) { $term .= "&wo_attachment=on"; }
}
else {
$term = "date1=" . $data['date1'] . "&date2=" . $data['date2'] . "&subj=" . $data['subj'] . "&from=" . $data['from'] . "&to=" . $data['to'] . "&tag=" . $data['tag'];
if(strlen($term) < 36) { return ""; }
}
return $term;
}
function escape_gt_lt_quote_symbols($s = '') {
if($s == '') { return $s; }
$s = preg_replace("/\>/", "&gt;", $s);
$s = preg_replace("/\</", "&lt;", $s);
$s = preg_replace('/"/', "&quot;", $s);
return $s;
}
function get_search_url_by_page($page = 0) {
return "/search-helper.php?page=$page";
/*isset($_SERVER['REQUEST_URI']) ? $a = preg_replace("/\/([\w]+)\.php\?{0,1}/", "", $_SERVER['REQUEST_URI']) : "";
$a = preg_replace("/page=\d{0,100}\&{0,1}/", "", $a);
if($page <= 0) { return "search.php?$a"; }
if($page >= 1 && $a == "") { return "search.php?page=$page"; }
return "search.php?page=$page&$a";*/
}
function parse_string_to_array($s = '', $arr = array()) {
$a = array();
parse_str($s, $a);
while(list($k, $v) = each($a)) {
if(!isset($arr[$k]) || $arr[$k] == '') $arr[$k] = $v;
}
}
function fetch_url($url = '') {
if($url == '') { return ''; }
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function fixup_date_condition($field = '', $date1 = 0, $date2 = 0) {
$date = "";
2012-02-08 23:14:28 +01:00
if($date1) {
list($y,$m,$d) = preg_split("/(\.|\-|\/)/", $date1);
if(DATE_TEMPLATE == 'd/m/Y') { $a = $y; $y = $d; $d = $a; }
2013-08-02 20:59:56 +02:00
if($m == '*') { $m = 0; }
if($d == '*') { $d = 0; }
$date1 = mktime(0, 0, 0, $m, $d, $y);
2012-02-08 23:14:28 +01:00
if($date1 > 0) { $date .= "$field >= $date1 "; }
}
2012-02-08 23:14:28 +01:00
if($date2) {
list($y,$m,$d) = preg_split("/(\.|\-|\/)/", $date2);
if(DATE_TEMPLATE == 'd/m/Y') { $a = $y; $y = $d; $d = $a; }
$date2 = mktime(23, 59, 59, $m, $d, $y);
2012-02-08 23:14:28 +01:00
if($date2 > 0) {
if($date) { $date .= " AND "; }
$date .= "$field <= $date2 ";
2012-02-08 23:14:28 +01:00
}
}
2012-02-08 23:14:28 +01:00
return $date;
2012-02-08 23:14:28 +01:00
}
function make_short_string($what, $length) {
return strlen($what) > $length ? substr($what, 0, $length) . "..." : $what;
}
2013-02-16 12:33:25 +01:00
function convert_days_ymd($convert) {
$years = ($convert / 365) ; // days / 365 days
$years = floor($years); // Remove all decimals
$month = ($convert % 365) / 30.5; // I choose 30.5 for Month (30,31) ;)
$month = floor($month); // Remove all decimals
$days = ($convert % 365) % 30.5; // the rest of days
// Return array of years, months, days
return array( $years,$month,$days );
}
function fix_evolution_mime_name_crap($s = '') {
if(preg_match("/iso\-\d{1,4}\-\d{1,}\'\'/i", $s)) {
$s = preg_replace("/iso\-\d{1,4}\-\d{1,}\'\'/i", "", $s);
$s = utf8_encode(urldecode($s));
}
return $s;
}
2013-04-09 15:02:10 +02:00
function safe_feof($fp, &$start = NULL) {
$start = microtime(true);
return feof($fp);
}
function anonimize_ip_addr($ip = '') {
$ip = explode(".", $ip);
if(count($ip) == 4) {
$ip[0] = $ip[1] = 'x';
$ip = implode(".", $ip);
}
return $ip;
}
2013-07-20 11:15:13 +02:00
function get_ldap_attribute_names($ldap_type = '') {
$ldap_mail_attr = LDAP_MAIL_ATTR;
$ldap_account_objectclass = LDAP_ACCOUNT_OBJECTCLASS;
$ldap_distributionlist_attr = LDAP_DISTRIBUTIONLIST_ATTR;
$ldap_distributionlist_objectclass = LDAP_DISTRIBUTIONLIST_OBJECTCLASS;
switch ($ldap_type) {
case 'AD':
$ldap_mail_attr = 'proxyAddresses';
2013-07-20 11:15:13 +02:00
$ldap_account_objectclass = 'user';
$ldap_distributionlist_attr = 'member';
$ldap_distributionlist_objectclass = 'group';
break;
case 'zimbra':
$ldap_mail_attr = 'mail';
$ldap_account_objectclass = 'zimbraAccount';
$ldap_distributionlist_attr = 'zimbraMailForwardingAddress';
$ldap_distributionlist_objectclass = 'zimbraDistributionList';
break;
case 'iredmail':
$ldap_mail_attr = 'mail';
$ldap_account_objectclass = 'mailUser';
$ldap_distributionlist_attr = 'memberOfGroup';
$ldap_distributionlist_objectclass = 'mailList';
break;
case 'lotus':
$ldap_mail_attr = 'mail';
$ldap_account_objectclass = 'dominoPerson';
$ldap_distributionlist_attr = 'mail';
$ldap_distributionlist_objectclass = 'dominoGroup';
break;
}
return array($ldap_mail_attr, $ldap_account_objectclass, $ldap_distributionlist_attr, $ldap_distributionlist_objectclass);
}
2013-04-09 15:02:10 +02:00
2012-02-08 23:14:28 +01:00
?>