/**
  *  Get array of aliases of user with given sip-uri
  *
  *  Possible options:
  *		none
  *
  *	@param	string	$sip_uri	URI of user
  *	@param	array	$opt		array of options
  *	@return	array				FALSE on error
  */
 function get_aliases_by_uri($sip_uri, $opt)
 {
     global $config;
     $errors = array();
     /* create connection to proxy where are stored data of user */
     if (isModuleLoaded('xxl') and $this->name != "get_aliases_tmp") {
         $tmp_data = CData_Layer::singleton("get_aliases_tmp", $errors);
         $tmp_data->set_xxl_user_id($sip_uri);
         $tmp_data->expect_user_id_may_not_exists();
         return $tmp_data->get_aliases_by_uri($sip_uri, $errors);
     }
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return false;
     }
     /* table's name */
     $tu_name =& $config->data_sql->uri->table_name;
     /* col names */
     $cu =& $config->data_sql->uri->cols;
     /* flags */
     $fu =& $config->data_sql->uri->flag_values;
     //parse username and domain from sip uri
     $reg =& Creg::singleton();
     $uname = $reg->get_username($sip_uri);
     $realm = $reg->get_domainname($sip_uri);
     if (!$uname or !$realm) {
         return array();
     }
     if ($config->multidomain) {
         if (false === ($did = $this->get_did_by_realm($realm, null))) {
             return false;
         }
         if (is_null($did)) {
             return array();
         }
     } else {
         $did = $config->default_did;
     }
     $flags_val = $fu['DB_DISABLED'] | $fu['DB_DELETED'];
     $q = "select " . $cu->uid . " as uid\n\t\t    from " . $tu_name . "\n\t\t\twhere  " . $cu->did . "      = " . $this->sql_format($did, "s") . " and \n\t\t\t       " . $cu->username . " = " . $this->sql_format($uname, "s") . " and \n\t\t\t\t  (" . $cu->flags . " & " . $flags_val . ") = 0";
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
     if (!$row) {
         unset($res);
         return array();
     }
     $uid = $row['uid'];
     if (is_null($uid)) {
         return array();
     }
     $uri_handler =& URIs::singleton($uid);
     if (false === ($out = $uri_handler->get_URIs())) {
         return false;
     }
     return $out;
 }
 function get_uri()
 {
     if (!is_null($this->uri)) {
         return $this->uri->to_string();
     }
     $uh =& URIs::singleton($this->uid);
     if (false === ($uri = $uh->get_URI())) {
         return false;
     }
     if (is_null($uri)) {
         return "";
     }
     $this->uri = $uri;
     return $this->uri->to_string();
 }
/**
 *	send missed calls to all subescribers
 */
function send_missed_calls()
{
    global $config, $data;
    $an = $config->attr_names;
    $opt = array('count_only' => true);
    /* count users */
    if (false === ($users_cnt = $data->get_users(array(), $opt))) {
        return false;
    }
    $step = 500;
    $data->set_showed_rows($step);
    for ($i = 0; $i < $users_cnt; $i += $step) {
        $data->set_act_row($i);
        $opt = array('order_by' => "uid", 'get_aliases' => true);
        /* get list of users and values of theirs attributes up_send_daily_missed_calls */
        if (false === ($users = $data->get_users(array(), $opt))) {
            return false;
        }
        foreach ($users as $row) {
            $ua =& User_Attrs::singleton($row['uid']);
            if (false === ($send = $ua->get_attribute($an['send_mc']))) {
                return false;
            }
            /* if email address is not filled skip this user */
            if (!$row['email_address']) {
                continue;
            }
            $mail_from = null;
            if (false === ($dom_send = get_send_mc_of_dom($row['uris'], $mail_from))) {
                return false;
            }
            if (is_null($send)) {
                $send = $dom_send;
            }
            if ($send) {
                if (false === send_mail_with_missed_calls($row['uid'], $row['email_address'], $mail_from)) {
                    return false;
                }
            }
            //free memory allocated by user attributes and uris
            URIs::free($row['uid']);
            User_Attrs::free($row['uid']);
        }
        unset($users);
    }
}
 /**
  *  Function return array of associtive arrays containig subscribers
  *
  *  Keys of associative arrays:
  *   - username
  *   - domain
  *   - name
  *   - fname
  *   - lname
  *   - phone
  *   - email_address
  *   - get_param
  *   - aliases
  *   - disabled
  *
  *  Possible options parameters:
  *    - from_domains    (array) - array of domain IDs from which are 
  *                                returned subscribers. By default are 
  *                                returned all subscribers. (default:null)
  *    - get_user_aliases (bool) - should return aliases of users? Could be 
  *                                disabled from performance reasons. 
  *                                (default: true)
  *    - get_sip_uri      (bool) - return sip address of user (default: false)
  *    - get_timezones    (bool) - return timezone of users
  *    - only_users      (array)	- Array of user IDs. if is set, only users 
  *                                from this array are returned (default:null)
  *    - return_all       (bool)	- if true, the result isn't limited by LIMIT
  *                                sql phrase (default: false)
  *    - only_agreeing    (bool)	- if true, only subscribers agreeing to look
  *                                up for them are returned (default: false)
  *    - get_credentials  (bool) - return credentials of users in output 
  *                                array (default: false)
  *    - count_only       (bool) - just count users matching the filter. 
  *                                If this option is true, integer is 
  *                                returned instead of array
  *    - get_disabled     (bool) - include disabled users to the result(default: true)
  *    - get_deleted      (bool) - include deleted users to the result(default: false)
  *	
  *	@return array	array of users or FALSE on error
  */
 function get_users($filter, $opt)
 {
     global $config;
     $errors = array();
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return false;
     }
     /* table's name */
     $ta_name =& $config->data_sql->user_attrs->table_name;
     $tc_name =& $config->data_sql->credentials->table_name;
     $tu_name =& $config->data_sql->uri->table_name;
     $tl_name =& $config->data_sql->location->table_name;
     $td_name =& $config->data_sql->domain->table_name;
     $tda_name =& $config->data_sql->domain_attrs->table_name;
     /* col names */
     $ca =& $config->data_sql->user_attrs->cols;
     $cc =& $config->data_sql->credentials->cols;
     $cu =& $config->data_sql->uri->cols;
     $cl =& $config->data_sql->location->cols;
     $cd =& $config->data_sql->domain->cols;
     $cda =& $config->data_sql->domain_attrs->cols;
     /* flags */
     $fa =& $config->data_sql->user_attrs->flag_values;
     $fc =& $config->data_sql->credentials->flag_values;
     $fu =& $config->data_sql->uri->flag_values;
     $an =& $config->attr_names;
     $opt_from_domains = isset($opt['from_domains']) ? $opt['from_domains'] : null;
     $opt_get_aliases = isset($opt['get_user_aliases']) ? (bool) $opt['get_user_aliases'] : true;
     $opt_get_sip_uri = isset($opt['get_sip_uri']) ? (bool) $opt['get_sip_uri'] : false;
     $opt_get_timezones = isset($opt['get_timezones']) ? (bool) $opt['get_timezones'] : false;
     $opt_uid_filter = isset($opt['only_users']) ? $opt['only_users'] : null;
     $opt_return_all = isset($opt['return_all']) ? (bool) $opt['return_all'] : false;
     $opt_count_only = isset($opt['count_only']) ? (bool) $opt['count_only'] : false;
     $opt_agreeing = isset($opt['only_agreeing']) ? (bool) $opt['only_agreeing'] : false;
     $opt_get_disabled = isset($opt['get_disabled']) ? (bool) $opt['get_disabled'] : true;
     $opt_get_deleted = isset($opt['get_deleted']) ? (bool) $opt['get_deleted'] : false;
     $opt_get_credentials = isset($opt['get_credentials']) ? (bool) $opt['get_credentials'] : false;
     $o_order_by = isset($opt['order_by']) ? $opt['order_by'] : "";
     $o_order_desc = !empty($opt['order_desc']) ? "desc" : "";
     $filter_join_fn = $filter_join_ln = $filter_join_ph = $filter_join_em = false;
     $qw = array();
     if (!empty($filter['username'])) {
         $qw[] = $filter['username']->to_sql("cr." . $cc->uname);
     }
     if (!empty($filter['realm'])) {
         $qw[] = $filter['realm']->to_sql("cr." . $cc->realm);
     }
     if (!empty($filter['uid'])) {
         $qw[] = $filter['uid']->to_sql("cr." . $cc->uid);
     }
     if (!empty($filter['fname'])) {
         $qw[] = $filter['fname']->to_sql("afn." . $ca->value);
         $filter_join_fn = true;
     }
     if (!empty($filter['lname'])) {
         $qw[] = $filter['lname']->to_sql("aln." . $ca->value);
         $filter_join_ln = true;
     }
     if (!empty($filter['email'])) {
         $qw[] = $filter['email']->to_sql("aem." . $ca->value);
         $filter_join_em = true;
     }
     if (!empty($filter['phone'])) {
         $qw[] = $filter['phone']->to_sql("aph." . $ca->value);
         $filter_join_ph = true;
     }
     if (!$opt_get_disabled) {
         $qw[] = "(cr." . $cc->flags . " & " . $fc['DB_DISABLED'] . ") = 0";
     }
     if (!$opt_get_deleted) {
         $qw[] = "(cr." . $cc->flags . " & " . $fc['DB_DELETED'] . ") = 0";
     }
     if (!empty($filter['sipuri'])) {
         $q_uri = "select " . $cu->uid . " \n                      from " . $tu_name . " u join " . $td_name . " d\n                            on u." . $cu->did . " = d." . $cd->did . "\n                      where " . $filter['sipuri']->to_sql("concat('sip:', " . $cu->username . ", '@', " . $cd->name . ")");
         $qw[] = "(cr." . $cc->uid . " IN (" . $q_uri . "))";
     }
     $query_c = "";
     if ($qw) {
         $query_c = implode(" and ", $qw);
     }
     $q_online = "";
     if (!empty($filter['onlineonly']) and $filter['onlineonly']->value) {
         $q_online = " join " . $tl_name . " loc on (cr." . $cc->uid . " = loc." . $cl->uid . ") ";
     }
     $q_admins = "";
     if (!empty($filter['adminsonly']) and $filter['adminsonly']->value) {
         $q_admins = " join " . $ta_name . " adm \n\t\t\t            on (cr." . $cc->uid . " = adm." . $ca->uid . " and \n\t\t\t\t\t\t    adm." . $ca->name . "='" . $an['is_admin'] . "' and\n\t\t\t\t\t\t\tadm." . $ca->value . "='1') ";
     }
     $q_agree = "";
     if ($opt_agreeing) {
         $q_agree = " join " . $ta_name . " aag \n\t\t\t            on (cr." . $cc->uid . " = aag." . $ca->uid . " and \n\t\t\t\t\t\t    aag." . $ca->name . "='" . $an['allow_find'] . "' and\n\t\t\t\t\t\t\taag." . $ca->value . "='1') ";
     }
     $q_uri = "";
     if (!empty($filter['alias'])) {
         $q_uri = " join (select distinct " . $cu->uid . " \n                             from " . $tu_name . " \n                             where " . $filter['alias']->to_sql($cu->username) . ") auri \n                       on cr." . $cc->uid . " = auri." . $cu->uid . " ";
     }
     $q_dom_filter = "";
     if (!empty($filter['domain'])) {
         if ($config->auth['use_did']) {
             $q_dom_filter = " join (select distinct " . $cd->did . " \n                                        from " . $td_name . " \n                                        where " . $filter['domain']->to_sql($cd->name) . ") dom \n                                  on cr." . $cc->did . "=dom." . $cd->did . " ";
         } else {
             $q_dom_filter = " join (select distinct doa." . $cda->value . " as realm \n                                        from " . $tda_name . " doa \n                                            join " . $td_name . " dom \n                                                on (doa." . $cda->did . " = dom." . $cd->did . " and \n                                                    " . $filter['domain']->to_sql("dom." . $cd->name) . " )\n                                        where doa." . $cda->name . " = '" . $an['digest_realm'] . "') idom \n                                  on idom.realm = cr." . $cc->realm . " ";
         }
     }
     $q_domains = "";
     if (!is_null($opt_from_domains)) {
         if (!$opt_get_deleted) {
             $q_domains_w = " and " . $cu->flags . " & " . $fu['DB_DELETED'] . " = 0";
         }
         $q_domains = " join (select distinct " . $cu->uid . " \n                                from " . $tu_name . " \n                                where  " . $this->get_sql_in($cu->did, $opt_from_domains, true) . $q_domains_w . ") iuri \n                                on cr." . $cc->uid . " = iuri." . $cu->uid . " ";
     }
     $q_uid_filter = "";
     if (!is_null($opt_uid_filter)) {
         $q_uid_filter = " and " . $this->get_sql_in("cr." . $cc->uid, $opt_uid_filter, true);
     }
     $q_tz_cols = $q_tz_from = "";
     if ($opt_get_timezones) {
         $q_tz_from = " left outer join " . $ta_name . " atz \n\t\t\t            on (cr." . $cc->uid . " = atz." . $ca->uid . " and \n\t\t\t\t\t\t    atz." . $ca->name . "='" . $an['timezone'] . "') ";
         $q_tz_cols = ", atz." . $ca->value . " as timezone ";
     }
     if (!$opt_return_all or $opt_count_only) {
         /* get num rows */
         $q = "select count(*) \n\t\t\t\t  from " . $tc_name . " cr " . $q_online . $q_admins . $q_dom_filter . $q_domains . $q_uri . $q_agree;
         if ($filter_join_fn) {
             $q .= " left outer join " . $ta_name . " afn\n                            on (cr." . $cc->uid . " = afn." . $ca->uid . " and afn." . $ca->name . "='" . $an['fname'] . "')";
         }
         if ($filter_join_ln) {
             $q .= "left outer join " . $ta_name . " aln\n                            on (cr." . $cc->uid . " = aln." . $ca->uid . " and aln." . $ca->name . "='" . $an['lname'] . "')";
         }
         if ($filter_join_ph) {
             $q .= "left outer join " . $ta_name . " aph\n                            on (cr." . $cc->uid . " = aph." . $ca->uid . " and aph." . $ca->name . "='" . $an['phone'] . "')";
         }
         if ($filter_join_em) {
             $q .= "left outer join " . $ta_name . " aem\n                            on (cr." . $cc->uid . " = aem." . $ca->uid . " and aem." . $ca->name . "='" . $an['email'] . "')";
         }
         if ($query_c or $q_uid_filter) {
             $q .= " where " . $query_c . $q_uid_filter;
         }
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             ErrorHandler::log_errors($res);
             return false;
         }
         $row = $res->fetchRow(DB_FETCHMODE_ORDERED);
         $this->set_num_rows($row[0]);
         $res->free();
         /* if act_row is bigger then num_rows, correct it */
         $this->correct_act_row();
         if ($opt_count_only) {
             return $row[0];
         }
     }
     $q = "select cr." . $cc->uid . " as uid,\n\t\t\t         cr." . $cc->uname . " as username,\n\t\t             cr." . $cc->did . " as did,\n\t\t\t         cr." . $cc->realm . " as realm,\n\t\t\t\t\t afn." . $ca->value . " as fname,\n\t\t\t\t\t aln." . $ca->value . " as lname,\n\t\t\t\t\t aph." . $ca->value . " as phone,\n\t\t\t\t\t aem." . $ca->value . " as email,\n\t\t\t\t\t cr." . $cc->flags . " & " . $fc['DB_DISABLED'] . " as disabled,\n\t\t\t\t\t cr." . $cc->flags . " & " . $fc['DB_DELETED'] . " as deleted,\n\t\t\t\t\t trim(concat(afn." . $ca->value . ", ' ', aln." . $ca->value . ")) as name\n\t\t\t\t\t " . $q_tz_cols . "\n\t\t\t  from " . $tc_name . " cr " . $q_online . $q_admins . $q_dom_filter . $q_domains . $q_uri . $q_agree . $q_tz_from . "\n\t\t\t        left outer join " . $ta_name . " afn\n\t\t\t            on (cr." . $cc->uid . " = afn." . $ca->uid . " and afn." . $ca->name . "='" . $an['fname'] . "')\n\t\t\t        left outer join " . $ta_name . " aln\n\t\t\t            on (cr." . $cc->uid . " = aln." . $ca->uid . " and aln." . $ca->name . "='" . $an['lname'] . "')\n\t\t\t        left outer join " . $ta_name . " aph\n\t\t\t            on (cr." . $cc->uid . " = aph." . $ca->uid . " and aph." . $ca->name . "='" . $an['phone'] . "')\n\t\t\t        left outer join " . $ta_name . " aem\n\t\t\t            on (cr." . $cc->uid . " = aem." . $ca->uid . " and aem." . $ca->name . "='" . $an['email'] . "')";
     if ($query_c or $q_uid_filter) {
         $q .= " where " . $query_c . $q_uid_filter;
     }
     if ($o_order_by) {
         $q .= " order by " . $o_order_by . " " . $o_order_desc;
     }
     $q .= $opt_return_all ? "" : $this->get_sql_limit_phrase();
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     $out = array();
     while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
         $i = $row['uid'];
         $out[$i]['uid'] = $row['uid'];
         $out[$i]['username'] = $row['username'];
         $out[$i]['realm'] = $row['realm'];
         $out[$i]['serweb_auth'] =& SerwebUser::instance($row['uid'], $row['username'], $config->auth['use_did'] ? $row['did'] : null, $row['realm']);
         $out[$i]['domain'] = $out[$i]['serweb_auth']->get_domainname();
         $out[$i]['name'] = $row['name'];
         $out[$i]['fname'] = $row['fname'];
         $out[$i]['lname'] = $row['lname'];
         $out[$i]['phone'] = $row['phone'];
         $out[$i]['email_address'] = $row['email'];
         $out[$i]['get_param'] = $out[$i]['serweb_auth']->to_get_param();
         $out[$i]['disabled'] = (bool) $row['disabled'];
         $out[$i]['deleted'] = (bool) $row['deleted'];
         if ($opt_get_timezones) {
             $out[$i]['timezone'] = $row['timezone'];
         }
         if ($opt_get_aliases or $opt_get_sip_uri) {
             $out[$i]['aliases'] = '';
             $out[$i]['sip_uri'] = '';
             $uri_handler =& URIs::singleton($row['uid']);
             if (false === ($uris = $uri_handler->get_URIs())) {
                 return false;
             }
             if ($opt_get_aliases) {
                 $alias_arr = array();
                 foreach ($uris as $val) {
                     $alias_arr[] = $val->get_username();
                 }
                 $out[$i]['aliases'] = implode(", ", $alias_arr);
                 $out[$i]['uris'] = $uris;
             }
             if ($opt_get_sip_uri) {
                 if (false === ($uri = $uri_handler->get_URI())) {
                     return false;
                 }
                 if (!is_null($uri)) {
                     if (false === ($out[$i]['sip_uri'] = $uri->to_string())) {
                         return false;
                     }
                 }
             }
         }
         if ($opt_get_credentials) {
             if (false === ($credentials = $this->get_credentials($row['uid'], null))) {
                 return false;
             }
             $out[$i]['credentials'] = array();
             foreach ($credentials as $k => $v) {
                 if (false === ($out[$i]['credentials'][] = $v->to_smarty())) {
                     return false;
                 }
             }
         }
     }
     $res->free();
     return $out;
 }
 function validate_form(&$errors)
 {
     global $lang_str, $data;
     if (false === parent::validate_form($errors)) {
         return false;
     }
     $did = $_POST['al_domain'];
     if (!$this->check_did($did)) {
         $d =& Domain::singleton();
         $errors[] = "You haven't access to domain which you selected: " . $d->get_domain_name($did);
         return false;
     }
     // check if admin has parmission to changed URI and if has not, return flase
     if ($_POST['al_id_d'] != "" and !$this->check_did($_POST['al_id_d'])) {
         $errors[] = "Can not update, you have not access to this URI";
         return false;
     }
     if (!empty($_POST['al_is_canon'])) {
         // chenck if cannonical flag in other URI may be cleared
         $uri_handler =& URIs::singleton($this->user_id->get_uid());
         if (false === ($uris = $uri_handler->get_URIs())) {
             return false;
         }
         foreach ($uris as $k => $v) {
             if ($v->is_canonical() and !$this->check_did($v->get_did())) {
                 $errors[] = $lang_str['err_canon_uri_exists'];
                 return false;
             }
         }
     }
     return true;
 }