Functions

/home/ivan/src/sit/branches/3.x/lib/user.inc.php File Reference

Go to the source code of this file.

Functions

if(realpath(__FILE__)==realpath($_SERVER['SCRIPT_FILENAME'])) user_permission ($userid, $permission)
 user_id ($username, $password)
 user_password ($id)
 user_realname ($id, $allowhtml=FALSE)
 user_email ($id)
 user_phone ($id)
 user_mobile ($id)
 user_signature ($id)
 user_message ($id)
 user_status ($id)
 user_accepting ($id)
 user_activeincidents ($userid)
 user_countincidents ($id)
 user_incidents ($id)
 user_holiday ($userid, $type=0, $year, $month, $day, $length=FALSE)
 user_count_holidays ($userid, $type, $date=0, $approved=array(HOL_APPROVAL_NONE, HOL_APPROVAL_GRANTED, HOL_APPROVAL_DENIED))
 user_holiday_entitlement ($userid)
 user_holiday_resetdate ($userid)
 user_emoticon_preference ($userid)
 user_drop_down ($name, $id, $accepting=TRUE, $exclude=FALSE, $attribs= '', $return=FALSE)
 user_group_id ($userid)
 user_online_icon ($user)
 user_online ($user)
 user_accepting_status ($userid)
 user_status_name ($userid)

Function Documentation

user_accepting ( id  ) 

Check whether the given user is accepting

Author:
Ivan Lucas
Parameters:
int $id The userid of the user to check
Returns:
string
Return values:
'Yes' User is accepting
'No' User is not accepting
'NoSuchUser' The given user does not exist

Definition at line 245 of file user.inc.php.

References $accepting, $id, and db_read_column().

Referenced by accepting_drop_down(), incident_backup_switchover(), software_backup_userid(), and user_accepting_status().

{
    $accepting = db_read_column('accepting', $GLOBALS['dbUsers'], $id);
    if ($accepting == '')  $accepting = "NoSuchUser";

    return $accepting;
}

user_accepting_status ( userid  ) 

Returns whether the user is accepting or not

Parameters:
int $userid ID of the user
Returns:
string 'accepting'|'not accepting'
Author:
Kieran Hogg

Definition at line 651 of file user.inc.php.

References $userid, and user_accepting().

{
    if (user_accepting($userid) == 'Yes')
    {
        return 'accepting';
    }
    else
    {
        return 'not accepting';
    }
}

user_activeincidents ( userid  ) 

Count the number of active incidents for a given user

Author:
Ivan Lucas
Parameters:
int $id The userid of the user to check
Returns:
int

Definition at line 260 of file user.inc.php.

References $CONFIG, $count, $dbContacts, $dbIncidents, $dbPriority, $now, $result, $sql, E_USER_WARNING, global, and mysql_error.

{
    global $CONFIG, $now, $dbIncidents, $dbContacts, $dbPriority;
    $count = 0;

    // This SQL must match the SQL in incidents.php
    $sql = "SELECT COUNT(i.id)  ";
    $sql .= "FROM `{$dbIncidents}` AS i, `{$dbContacts}` AS c, `{$dbPriority}` AS pr WHERE contact = c.id AND i.priority = pr.id ";
    $sql .= "AND (owner='{$userid}' OR towner='{$userid}') ";
    $sql .= "AND (status!='2') ";  // not closed
    // the "1=2" obviously false else expression is to prevent records from showing unless the IF condition is true
    $sql .= "AND ((timeofnextaction > 0 AND timeofnextaction < $now) OR ";
    $sql .= "(IF ((status >= 5 AND status <=8), ($now - lastupdated) > ({$CONFIG['regular_contact_days']} * 86400), 1=2 ) ";  // awaiting
    $sql .= "OR IF (status='1' OR status='3' OR status='4', 1=1 , 1=2) ";  // active, research, left message - show all
    $sql .= ") AND timeofnextaction < $now ) ";

    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    else list($count) = mysql_fetch_row($result);

    return ($count);
}

user_count_holidays ( userid,
type,
date = 0,
approved = array(HOL_APPROVAL_NONEHOL_APPROVAL_GRANTEDHOL_APPROVAL_DENIED) 
)

Count a users holidays of specified type

Author:
Ivan Lucas
Parameters:
integer $userid . User ID
integer $type. Holiday type
integer $date. (optional) UNIX timestamp. Only counts holidays before this date
array $approved (optional) An array of approval statuses to include when ommitted a default is used
Returns:
integer. Number of days holiday

Definition at line 402 of file user.inc.php.

References $approved, $date, $dbHolidays, $result, $sql, debug_log(), E_USER_WARNING, global, and mysql_error.

{
    global $dbHolidays;
    $sql = "SELECT id FROM `{$dbHolidays}` WHERE userid='$userid' ";
    $sql .= "AND type='$type' AND length='day' ";
    if ($date > 0) $sql .= "AND `date` < FROM_UNIXTIME({$date})";
    if (is_array($approved))
    {
        $sql .= "AND (";

        for ($i = 0; $i < sizeof($approved); $i++)
        {
            $sql .= "approved = {$approved[$i]} ";
            if ($i < sizeof($approved)-1) $sql .= "OR ";
        }

        $sql .= ") ";
    }
    else
    {
        $sql .= "AND (approved = ".HOL_APPROVAL_NONE." OR approved = ".HOL_APPROVAL_GRANTED.") ";
    }
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    $full_days = mysql_num_rows($result);

    $sql = "SELECT id FROM `{$dbHolidays}` ";
    $sql .= "WHERE userid='{$userid}' AND type='{$type}' AND (length='pm' OR length='am') ";
    if (is_array($approved))
    {
        $sql .= "AND (";

        for ($i = 0; $i < sizeof($approved); $i++)
        {
            $sql .= "approved = {$approved[$i]} ";
            if ($i < sizeof($approved)-1) $sql .= "OR ";
        }

        $sql .= ") ";
    }
    else
    {
        $sql .= "AND (approved = ".HOL_APPROVAL_NONE." OR approved = ".HOL_APPROVAL_GRANTED.") ";
    }

    if ($date > 0)
    {
        $sql .= "AND `date` < {$date}";
    }
debug_log($sql); // ###INL###
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    $half_days = mysql_num_rows($result);

    $days_holiday = $full_days + ($half_days / 2);
    return $days_holiday;
}

user_countincidents ( id  ) 

Counts a users open incidents

Author:
Ivan Lucas
Parameters:
int $id The userid of the user to check
Returns:
int
Note:
This number will never match the number shown in the active queue and is not meant to

Definition at line 291 of file user.inc.php.

References $count, $dbIncidents, $result, $sql, E_USER_WARNING, global, and mysql_error.

{
    global $dbIncidents;
    $count = 0;

    $sql = "SELECT COUNT(id) FROM `{$dbIncidents}` WHERE (owner='{$id}' OR towner='{$id}') AND (status!=2)";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    else list($count) = mysql_fetch_row($result);

    return ($count);
}

user_drop_down ( name,
id,
accepting = TRUE,
exclude = FALSE,
attribs = '',
return = FALSE 
)

Returns the HTML for a drop down list of users, with the given name and with the given id selected.

Author:
Ivan Lucas
Parameters:
string $name. Name attribute
int $id. User ID to pre-select
bool $accepting. when true displays the accepting status. hides it when false
int $exclude. User ID not to list
string $attribs. Extra attributes for the select control
Returns:
string HTML

Definition at line 509 of file user.inc.php.

References $accepting, $dbUsers, $id, $result, $return, $show, $sql, E_USER_WARNING, global, and mysql_error.

Referenced by cfgVarInput(), dashboard_engineer_edit(), dashboard_watch_incidents_edit(), and show_edit_site().

{
    // INL 1Jul03 Now only shows users with status > 0 (ie current users)
    // INL 2Nov04 Optional accepting field, to hide the status 'Not Accepting'
    // INL 19Jan05 Option exclude field to exclude a user, or an array of
    // users
    global $dbUsers;
    $sql  = "SELECT id, realname, accepting FROM `{$dbUsers}` WHERE status > 0 ORDER BY realname ASC";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);

    $html .= "<select name='{$name}' id='{$name}' ";
    if (!empty($attribs))
    {
        $html .= " $attribs";
    }

    $html .= ">\n";
    if ($id == 0)
    {
        $html .= "<option selected='selected' value='0'></option>\n";
    }

    while ($users = mysql_fetch_object($result))
    {
        $show = TRUE;
        if ($exclude != FALSE)
        {
            if (is_array($exclude))
            {
                if (!in_array($users->id, $exclude)) $show = TRUE;
                else $show = FALSE;
            }
            else
            {
                if ($exclude!=$users->id) $show = TRUE;
                else $show = FALSE;
            }
        }
        if ($show == TRUE)
        {
            $html .= "<option ";
            if ($users->id == $id) $html .= "selected='selected' ";
            if ($users->accepting == 'No' AND $accepting == TRUE)
            {
                $html .= " class='expired' ";
            }

            $html .= "value='{$users->id}'>";
            $html .= "{$users->realname}";
            if ($users->accepting == 'No' AND $accepting == TRUE)
            {
                $html .= ", {$GLOBALS['strNotAccepting']}";
            }
            $html .= "</option>\n";
        }
    }
    $html .= "</select>\n";

    if ($return)
    {
        return $html;
    }
    else
    {
        echo $html;
    }
}

user_email ( id  ) 

Return a users email address

Author:
Ivan Lucas
Parameters:
id int. User ID
Note:
Obtained from session if possible

Definition at line 166 of file user.inc.php.

References $_SESSION, $dbUsers, $id, db_read_column(), and global.

Referenced by emailtype_replace_jobs_specials().

{
    global $dbUsers;
    if ($id == $_SESSION['userid'])
    {
        return $_SESSION['email'];
    }
    else
    {
        return db_read_column('email', $dbUsers, $id);
    }
}

user_emoticon_preference ( userid  ) 

Returns the users preference for showing emoticons

Author:
Paul Heaney
Parameters:
int $userid User ID
Returns:
string true or false
Return values:
'true' means the user wants emoticons shown
'false' means the user doesn't want emoticons

Definition at line 494 of file user.inc.php.

References $userid, and db_read_column().

{
    return db_read_column('var_emoticons', $GLOBALS['dbUsers'], $userid);
}

user_group_id ( userid  ) 
Author:
Paul Heaney
Parameters:
int $userid - userid to find group for
Returns:
int the groupid

Definition at line 584 of file user.inc.php.

References $dbUsers, $groupid, $result, $sql, E_USER_WARNING, global, and mysql_error.

Referenced by check_group_holiday().

{
    global $dbUsers;
    // get groupid
    $sql = "SELECT groupid FROM `{$dbUsers}` WHERE id='{$userid}' ";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    list($groupid) = mysql_fetch_row($result);
    return $groupid;
}

user_holiday ( userid,
type = 0,
year,
month,
day,
length = FALSE 
)

gets users holiday information for a certain day given an optional type and optional length returns both type and length and approved as an array

Author:
Ivan Lucas
Parameters:
int $userid. The userid of the holiday to retrieve
int $type. The holiday type. e.g. sickness
int $year. Year. eg. 2008
int $month. Month. eg. 11 = November
int $day. Day
string $length. 'am', 'pm', 'day' or FALSE to list all
Returns:
array

Definition at line 346 of file user.inc.php.

References $approved, $dbHolidays, $length, $result, $sql, $type, E_USER_WARNING, global, and mysql_error.

Referenced by draw_calendar().

{
    global $dbHolidays;
    $sql = "SELECT * FROM `{$dbHolidays}` WHERE `date` = '{$year}-{$month}-{$day}' ";
    if ($type !=0 )
    {
        $sql .= "AND (type='$type' OR type='".HOL_PUBLIC."' OR type='".HOL_FREE."') ";
        $sql .= "AND IF(type!=".HOL_PUBLIC.", userid='$userid', 1=1) ";
    }
    else
    {
        $sql .= " AND userid='$userid' ";
    }

    if ($length != FALSE)
    {
        $sql .= "AND length='$length' ";
    }
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);

    if (mysql_num_rows($result) == 0)
    {
        return FALSE;
    }
    else
    {
        $totallength = 0;
        while ($holiday = mysql_fetch_object($result))
        {
            $type = $holiday->type;
            $length = $holiday->length;
            $approved = $holiday->approved;
            $approvedby = $holiday->approvedby;
            // hmm... not sure these next lines are required.
            if ($length == 'am' && $totallength == 0) $totallength = 'am';
            if ($length == 'pm' && $totallength == 0) $totallength = 'pm';
            if ($length == 'am' && $totallength == 'pm') $totallength = 'day';
            if ($length == 'pm' && $totallength == 'am') $totallength = 'day';
            if ($length == 'day') $totallength = 'day';
        }
        return array($type, $totallength, $approved, $approvedby);
    }
}

user_holiday_entitlement ( userid  ) 

Return the users holiday entitlement

Author:
Ivan Lucas
Parameters:
integer $userid. User ID
Returns:
integer. Number of days holiday a user is entitled to

Definition at line 468 of file user.inc.php.

References $userid, and db_read_column().

{
    return db_read_column('holiday_entitlement', $GLOBALS['dbUsers'], $userid);
}

user_holiday_resetdate ( userid  ) 

Return the users holiday entitlement reset/rollover date

Author:
Ivan Lucas
Parameters:
integer $userid. User ID
Returns:
integer. UNIX Timestamp date

Definition at line 480 of file user.inc.php.

References $userid, db_read_column(), and mysql2date().

{
    return mysql2date(db_read_column('holiday_resetdate', $GLOBALS['dbUsers'], $userid) . ' 17:00:00');
}

user_id ( username,
password 
)

Returns an integer representing the id of the user identified by his/her username and password

Author:
Ivan Lucas
Parameters:
string $username. A username
string $password. An MD5 hashed password
Returns:
integer. the users ID or 0 if the user does not exist (username/password did not match)
Return values:
int 0 The user did not exist
int >=1 The userid of the matching user
Note:
Returns 0 if the given user does not exist

Definition at line 55 of file user.inc.php.

References $dbUsers, $result, $sql, $user, $userid, E_USER_WARNING, global, and mysql_error.

{
    global $dbUsers;
    $sql  = "SELECT id FROM `{$dbUsers}` ";
    $sql .= "WHERE username='$username' AND password='$password'";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    if (mysql_num_rows($result) == 0)
    {
        $userid= 0;
    }
    else
    {
        $user = mysql_fetch_object($result);
        $userid = $user->id;
    }
    return $userid;
}

user_incidents ( id  ) 

Counts number of incidents and priorty for a given user

Author:
Ivan Lucas
Parameters:
int $id The userid of the user to check
Returns:
array

Definition at line 311 of file user.inc.php.

References $arr, $dbIncidents, $obj, $result, $sql, E_USER_WARNING, global, and mysql_error.

{
    global $dbIncidents;
    $sql = "SELECT priority, count(priority) AS num FROM `{$dbIncidents}` ";
    $sql .= "WHERE (owner = $id OR towner = $id) AND status != 2 ";
    $sql .= "GROUP BY priority";

    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);

    $arr = array('1' => '0', '2' => '0', '3' => '0', '4' => '0');

    if (mysql_num_rows($result) > 0)
    {
        while ($obj = mysql_fetch_object($result))
        {
            $arr[$obj->priority] = $obj->num;
        }
    }
    return $arr;
}

user_message ( id  ) 

Return a users away message

Author:
Ivan Lucas
Parameters:
id int. User ID

Definition at line 218 of file user.inc.php.

References $id, and db_read_column().

{
    return db_read_column('message', $GLOBALS['dbUsers'], $id);
}

user_mobile ( id  ) 

Return a users mobile phone number

Author:
Ivan Lucas
Parameters:
id int. User ID

Definition at line 196 of file user.inc.php.

References $id, and db_read_column().

{
    return db_read_column('mobile', $GLOBALS['dbUsers'], $id);
}

user_online ( user  ) 

Returns users online status

Author:
Kieran Hogg
Parameters:
int $user The user ID of the user to check
Returns:
boolean. TRUE if online, FALSE if not

Definition at line 626 of file user.inc.php.

References $dbUsers, $now, $result, $sql, $startofsession, E_USER_WARNING, global, mysql2date(), and mysql_error.

{
    global $iconset, $now, $dbUsers, $startofsession;
    $sql = "SELECT lastseen FROM `{$dbUsers}` WHERE id={$user}";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    $users = mysql_fetch_object($result);
    if (mysql2date($users->lastseen) > $startofsession)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

user_online_icon ( user  ) 

An icon showing a users online status

Author:
Kieran Hogg
Parameters:
int $user The user ID of the user to check
Returns:
string. HTML of a 16x16 status icon.

Definition at line 602 of file user.inc.php.

References $dbUsers, $now, $result, $sql, $startofsession, $strOffline, $strOnline, E_USER_WARNING, global, icon(), mysql2date(), and mysql_error.

{
    global $iconset, $now, $dbUsers, $strOffline, $strOnline, $startofsession;
    $sql = "SELECT lastseen FROM `{$dbUsers}` WHERE id={$user}";
    $result = mysql_query($sql);
    if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
    $users = mysql_fetch_object($result);
    if (mysql2date($users->lastseen) > $startofsession)
    {
        return icon('online', 16, $strOnline);
    }
    else
    {
        return icon('offline', 16, $strOffline);
    }
}

user_password ( id  ) 

Return a users password

Author:
Ivan Lucas
Parameters:
int $id User ID
Returns:
string
Note:
this is an MD5 hash

Definition at line 82 of file user.inc.php.

References $dbUsers, $id, db_read_column(), and global.

{
    global $dbUsers;
    return db_read_column('password', $dbUsers, $id);
}

if (realpath(__FILE__)==realpath($_SERVER['SCRIPT_FILENAME'])) user_permission ( userid,
permission 
)

Returns TRUE or FALSE to indicate whether a given user has a given permission

Author:
Ivan Lucas
Parameters:
int $userid. The userid to check
int $permission or array. The permission id to check, or an array of id's to check
Returns:
boolean. TRUE if the user has the permission (or all the permissions in the array), otherwise FALSE

Definition at line 24 of file user.inc.php.

References $_SESSION, and $permission.

Referenced by dashboard_incoming_display(), sit_error_handler(), and user_contracts_table().

{
    // Default is no access
    $accessgranted = FALSE;

    if (!is_array($permission))
    {
        $permission = array($permission);
    }

    foreach ($permission AS $perm)
    {
        if (@in_array($perm, $_SESSION['permissions']) == TRUE) $accessgranted = TRUE;
        else $accessgranted = FALSE;
        // Permission 0 is always TRUE (general acess)
        if ($perm == 0) $accessgranted = TRUE;
    }
    return $accessgranted;
}

user_phone ( id  ) 

Return a users phone number

Author:
Ivan Lucas
Parameters:
id int. User ID

Definition at line 185 of file user.inc.php.

References $id, and db_read_column().

{
    return db_read_column('phone', $GLOBALS['dbUsers'], $id);
}

user_realname ( id,
allowhtml = FALSE 
)

Return a users real name

Author:
Ivan Lucas
Parameters:
int $id. A user ID
bool $allowhtml. may return HTML if TRUE, only ever returns plain text if FALSE
Returns:
string
Note:
If $allowhtml is TRUE disabled user accounts are returned as HTML with span class 'deleted'

Definition at line 97 of file user.inc.php.

References $_SESSION, $CONFIG, $dbEscalationPaths, $dbUsers, $from, $id, $result, $sql, $status, E_USER_WARNING, elseif, global, and mysql_error.

Referenced by add_note_form(), check_group_holiday(), dashboard_tasks(), dashboard_user_incidents(), display_holiday_table(), draw_job_chart(), emailtype_replace_jobs_specials(), generate_row(), incident_backup_switchover(), kb_article(), make_incident_billing_array(), show_notes(), and site_salesperson().

{
    global $update_body;
    global $incidents;
    global $CONFIG;
    global $dbUsers, $dbEscalationPaths;
    if ($id >= 1)
    {
        if ($id == $_SESSION['userid'])
        {
            return $_SESSION['realname'];
        }
        else
        {
            $sql = "SELECT realname, status FROM `{$dbUsers}` WHERE id='$id' LIMIT 1";
            $result = mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
            list($realname, $status) = mysql_fetch_row($result);
            if ($allowhtml == FALSE OR $status > 0)
            {
                return $realname;
            }
            else
            {
                return "<span class='deleted'>{$realname}</span>";
            }
        }
    }
    elseif (!empty($incidents['email']))
    {
        // TODO this code does not belong here
        // The SQL is also looking at all escalation paths not just the relevant
        // one.
        //an an incident
        preg_match('/From:[ A-Za-z@\.]*/', $update_body, $from);
        if (!empty($from))
        {
            $frommail = strtolower(substr(strstr($from[0], '@'), 1));
            $customerdomain = strtolower(substr(strstr($incidents['email'], '@'), 1));

            if ($frommail == $customerdomain) return $GLOBALS['strCustomer'];

            $sql = "SELECT name, email_domain FROM `{$dbEscalationPaths}`";
            $result = mysql_query($sql);
            if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
            while ($escpath = mysql_fetch_object($result))
            {
                if (!empty($escpath->email_domain))
                {
                    if (strstr(strtolower($frommail), strtolower($escpath->email_domain)))
                    {
                        return $escpath->name;
                    }
                }
            }
        }
    }

    //Got this far not returned anything so
    return $CONFIG['application_shortname']; // No from email address
}

user_signature ( id  ) 

Return a users email signature

Author:
Ivan Lucas
Parameters:
id int. User ID

Definition at line 207 of file user.inc.php.

References $id, and db_read_column().

Referenced by emailtype_replace_jobs_specials().

{
    return db_read_column('signature', $GLOBALS['dbUsers'], $id);
}

user_status ( id  ) 

Return a users current away status

Author:
Ivan Lucas
Parameters:
id int. User ID
Note:
0 means user account disabled

Definition at line 230 of file user.inc.php.

References $id, and db_read_column().

Referenced by saction_SetUserStatus().

{
    return db_read_column('status', $GLOBALS['dbUsers'], $id);
}

user_status_name ( userid  ) 

Returns the status of a user

Parameters:
int $userid ID of the user
Returns:
string user status
Author:
Kieran Hogg

Definition at line 670 of file user.inc.php.

References $status, $userid, and db_read_column().

{
    $status = db_read_column('name', $GLOBALS['dbUserStatus'], $userid);
    return $GLOBALS[$status];
}

For more help developing with SiT! see http://sitracker.org/wiki/DevelopmentHowTo

© 2008-2011 Support Incident Tracker

Tsohost Logo