Example #1
0
 /**
  *	Get value of attribute
  *	
  *	This function search in order uri, user, domain and global tracks 
  *	for attribute value and return value from the first track where find it.
  *	
  *	Alowed options:
  *		- uid (string)	-	uid of user track
  *		- did (string)	-	did of domain track
  *		- uri (array)	-	identifies uri track. Have to have three 
  *							components: scheme, username and did
  *	
  *	@param	string	$name	name of the attribute
  *	@param	array	$opt	options
  *	@return	mixed			value of attribute or FALSE on error
  */
 function get_attribute($name, $opt)
 {
     /* set default values for options */
     $opt_uid = isset($opt["uid"]) ? $opt["uid"] : null;
     $opt_did = isset($opt["did"]) ? $opt["did"] : null;
     $opt_uri = isset($opt["uri"]) ? $opt["uri"] : null;
     if (!is_null($opt_uri)) {
         $attrs =& Uri_Attrs::singleton($opt_uri['scheme'], $opt_uri['username'], $opt_uri['did']);
         if (false === ($attr = $attrs->get_attribute($name))) {
             return false;
         }
         if (!is_null($attr)) {
             return $attr;
         }
     }
     if (!is_null($opt_uid)) {
         $attrs =& User_Attrs::singleton($opt_uid);
         if (false === ($attr = $attrs->get_attribute($name))) {
             return false;
         }
         if (!is_null($attr)) {
             return $attr;
         }
     }
     if (!is_null($opt_did)) {
         $attrs =& Domain_Attrs::singleton($opt_did);
         if (false === ($attr = $attrs->get_attribute($name))) {
             return false;
         }
         if (!is_null($attr)) {
             return $attr;
         }
     }
     $attrs =& Global_Attrs::singleton();
     if (false === ($attr = $attrs->get_attribute($name))) {
         return false;
     }
     if (!is_null($attr)) {
         return $attr;
     }
     /* attribute not found */
     return null;
 }
 function set_domain_admin($add)
 {
     global $config;
     $an =& $config->attr_names;
     $da =& Domain_Attrs::Singleton($this->controler->domain_id);
     $admins = $da->get_attribute($an['admin']);
     if (is_null($admins)) {
         $admins = array();
     }
     if ($add) {
         $admins[] = $this->controler->user_id->get_uid();
     } else {
         foreach ($admins as $k => $v) {
             if ($v == $this->controler->user_id->get_uid()) {
                 unset($admins[$k]);
                 break;
             }
         }
     }
     if (false === $da->set_attribute($an['admin'], $admins)) {
         return false;
     }
     return true;
 }
Example #3
0
/**
 *	Function read array of URIs and for each URI get the domain id,
 *	obtain value of attribute 'send_missed_calls'. If at least one attribute
 *	is true, this function return 1. Otherwise return 0. 
 *
 *	If no domain has the attribute set, the output of function depends on global 
 *	attribute 'send_missed_calls'
 *	
 *	@param	array	$uris
 *	@return	int				or FALSE on error
 */
function get_send_mc_of_dom($uris, &$mail_from)
{
    global $config;
    $an = $config->attr_names;
    $send = null;
    $mail_from = null;
    foreach ($uris as $uri) {
        $da =& Domain_Attrs::singleton($uri->get_did());
        if (false === ($s = $da->get_attribute($an['send_mc']))) {
            return false;
        }
        if (is_null($send)) {
            $send = $s;
        } else {
            $send = ($send or $s);
        }
        if ($s and !$mail_from) {
            $o = array('did' => $uri->get_did());
            if (false === ($from_header = Attributes::get_attribute($an['contact_email'], $o))) {
                return false;
            }
            if ($from_header) {
                $mail_from = $from_header;
            }
        }
    }
    if (is_null($send)) {
        $ga =& Global_Attrs::singleton();
        if (false === ($send = $ga->get_attribute($an['send_mc']))) {
            return false;
        }
    }
    return $send ? 1 : 0;
}
Example #4
0
 /**
  *  Method update the owner of domain, digest realm and sop_vm_domain. 
  *
  *  Attributes:
  *    - owner_id    - ID of owner (customer)
  *    - alias       - ???
  *
  *  @param string $did      doamin ID
  *  @param array  $attrs    array of attribute values
  *  @return bool            TRUE on success, FALSE on error
  *  @static
  */
 function update_domain_attrs($did, $attrs)
 {
     global $config;
     $an =& $config->attr_names;
     $da_h =& Domain_Attrs::singleton($did);
     if (false === ($domain_attrs = $da_h->get_attributes())) {
         return false;
     }
     if (isset($attrs['owner_id'])) {
         $cur_owner_id = null;
         if (isset($domain_attrs[$an['dom_owner']])) {
             $cur_owner_id = $domain_attrs[$an['dom_owner']];
         }
         if (!is_null($attrs['owner_id']) and $attrs['owner_id'] != $cur_owner_id) {
             if ($attrs['owner_id'] == -1) {
                 if (false === $da_h->unset_attribute($an['dom_owner'])) {
                     return false;
                 }
             } else {
                 if (false === $da_h->set_attribute($an['dom_owner'], $attrs['owner_id'])) {
                     return false;
                 }
             }
         }
     }
     /*
      *  If digest realm is not set, (or 'sop_vm_domain' is defined and is not set) 
      *  set it by the canonical domain name
      */
     $digest_realm = "";
     if (!isset($domain_attrs[$an['digest_realm']]) or !empty($an['sop_vm_domain']) and !isset($domain_attrs[$an['sop_vm_domain']])) {
         $dm_h =& DomainManipulator::singleton($did);
         if (false === ($dom_names = $dm_h->get_domain_names(null))) {
             return false;
         }
         foreach ($dom_names as $v) {
             if ($v['canon']) {
                 $digest_realm = $v['name'];
                 break;
             }
         }
         if (!$digest_realm and !empty($attrs['alias'])) {
             $digest_realm = $attrs['alias'];
         }
         if ($digest_realm and false === $da_h->set_attribute($an['digest_realm'], $digest_realm)) {
             return false;
         }
     }
     if ($digest_realm and !isset($domain_attrs[$an['digest_realm']])) {
         if (false === $da_h->set_attribute($an['digest_realm'], $digest_realm)) {
             return false;
         }
     }
     if ($digest_realm and !empty($an['sop_vm_domain']) and !isset($domain_attrs[$an['sop_vm_domain']])) {
         if (false === $da_h->set_attribute($an['sop_vm_domain'], $digest_realm)) {
             return false;
         }
     }
     /* set datetime_created attr */
     if (!isset($domain_attrs[$an['datetime_created']])) {
         if (false === $da_h->set_attribute($an['datetime_created'], gmdate("Y-m-d H:i:s"))) {
             return false;
         }
     }
     return true;
 }
 function action_register(&$errors)
 {
     global $config, $data, $lang_str;
     $an =& $config->attr_names;
     /* generate confirmation string */
     $confirm = md5(uniqid(rand()));
     /* obtain password */
     if ($this->opt['choose_passw']) {
         $password = $_POST['passwd'];
     } else {
         /* generate new password */
         $password = substr(md5(uniqid('')), 0, 5);
     }
     if (!$this->opt['create_new_domain']) {
         /* get domain name */
         $domains =& Domains::singleton();
         if (false === ($domain_name = $domains->get_domain_name($this->did))) {
             $data->transaction_rollback();
             return false;
         }
     } else {
         $domain_name = $this->opt['create_new_domain'];
     }
     /* set value of option 'require_confirmation' */
     if (is_null($this->opt['require_confirmation'])) {
         $o = array();
         /* if creating new domain we does not know the DID */
         if (!$this->opt['create_new_domain']) {
             $o['did'] = $this->did;
         }
         if (false === ($this->opt['require_confirmation'] = Attributes::get_attribute($an['require_conf'], $o))) {
             return false;
         }
     }
     if (false === $data->transaction_start()) {
         return false;
     }
     if ($this->opt['create_new_domain']) {
         $sem = new Shm_Semaphore(__FILE__, "s", 1, 0600);
         /* set semaphore to be sure there will not be generated same 
            domain id for two domains */
         if (!$sem->acquire()) {
             $data->transaction_rollback();
             return false;
         }
         if (false === ($this->did = Domains::generate_new_did($this->opt['create_new_domain']))) {
             $data->transaction_rollback();
             $sem->release();
             return false;
         }
         $opt = array("enabled" => !$this->opt['require_confirmation']);
         if (false === DomainManipulator::add_alias($this->did, $this->opt['create_new_domain'], $opt)) {
             $data->transaction_rollback();
             $sem->release();
             return false;
         }
         $a_vals = array("alias" => $this->opt['create_new_domain']);
         if (false === DomainManipulator::update_domain_attrs($this->did, $a_vals)) {
             $data->transaction_rollback();
             $sem->release();
             return false;
         }
         $sem->release();
     }
     /* prepare array of attributes */
     $opt = array();
     $attrs = Attributes::post_attrs_to_array($this->attributes, $opt);
     /* add subscriber */
     $opts = array("disabled" => $this->opt['require_confirmation']);
     if (false === Registration::add_subscriber($_POST['uname'], $this->did, $password, $attrs, $opts)) {
         $data->transaction_rollback();
         return false;
     }
     $uid = $opts['uid'];
     $realm = $opts['realm'];
     $serweb_user =& SerwebUser::instance($uid, $_POST['uname'], $this->did, $realm);
     $user_param = $serweb_user->to_get_param();
     /* get handler of user attrs */
     $ua =& User_Attrs::singleton($uid);
     /* get handler of domain attrs */
     $da =& Domain_Attrs::singleton($this->did);
     if (!is_null($this->opt['set_lang_attr'])) {
         $u_lang = $this->opt['set_lang_attr'];
         /* get the attr_type of the lang attribute */
         $at_handler =& Attr_types::singleton();
         if (false === ($lang_type = $at_handler->get_attr_type($an['lang']))) {
             $data->transaction_rollback();
             return false;
         }
         if (is_null($lang_type)) {
             ErrorHandler::add_error("Type of attribute 'lang' doesn't exists");
             $data->transaction_rollback();
             return false;
         }
         /* format the value */
         $lang_type->check_value($u_lang);
         /* store lang into DB */
         if (false === $ua->set_attribute($an['lang'], $u_lang)) {
             $data->transaction_rollback();
             return false;
         }
     }
     if ($this->opt['create_new_domain']) {
         /* when creating new domain, set admin privilege for the user */
         if (false === $ua->set_attribute($an['is_admin'], "1")) {
             $data->transaction_rollback();
             return false;
         }
         /* and assign user as admin of the domain */
         if (false === $da->set_attribute($an['admin'], array($uid))) {
             $data->transaction_rollback();
             return false;
         }
     }
     if ($this->opt['require_confirmation']) {
         if (false === $ua->set_attribute($an['confirmation'], $confirm)) {
             $data->transaction_rollback();
             return false;
         }
         if (false === $ua->set_attribute($an['pending_ts'], time())) {
             $data->transaction_rollback();
             return false;
         }
         if ($this->opt['create_new_domain']) {
             if (false === $da->set_attribute($an['confirmation'], $confirm)) {
                 $data->transaction_rollback();
                 return false;
             }
             if (false === $da->set_attribute($an['pending_ts'], time())) {
                 $data->transaction_rollback();
                 return false;
             }
         }
     }
     if ($this->opt['create_numeric_alias']) {
         $sem = new Shm_Semaphore(__FILE__, "s", 1, 0600);
         /* set semaphore to be sure there will not be same aliases for two users */
         if (!$sem->acquire()) {
             $data->transaction_rollback();
             return false;
         }
         // generate alias number
         if (false === ($alias = $data->get_new_alias_number($this->did, null))) {
             $data->transaction_rollback();
             $sem->release();
             return false;
         }
         /* store alias to URI table */
         $o = array('disabled' => $this->opt['require_confirmation'], 'canon' => false);
         if (false === $data->add_uri($uid, 'sip', $alias, $this->did, $o)) {
             $data->transaction_rollback();
             $sem->release();
             return false;
         }
         /* reset the semaphore */
         if (!$sem->release()) {
             $data->transaction_rollback();
             return false;
         }
     }
     $sip_address = "sip:" . $_POST['uname'] . "@" . $domain_name;
     $login_url = $config->root_uri . ($this->opt['admin_login'] ? $config->admin_pages_path : $config->user_pages_path) . $this->opt['login_script'];
     $admin_url = $config->root_uri . $config->admin_pages_path . $this->opt['login_script'];
     $username = $config->fully_qualified_name_on_login ? $_POST['uname'] . "@" . $domain_name : $_POST['uname'];
     $confirmation_url = $config->root_uri . $config->user_pages_path . $this->opt['confirmation_script'] . "?nr=" . $confirm . (isModuleLoaded('xxl') ? "&pr=" . RawURLEncode(base64_encode($proxy['proxy'])) : "");
     if (is_null($this->opt['mail_file_conf'])) {
         $this->opt['mail_file_conf'] = $this->opt['mail_file'];
     }
     if ($this->opt['create_new_domain']) {
         if ($this->opt['require_confirmation']) {
             $mail_file = $this->opt['mail_file_domain_conf'];
         } else {
             $mail_file = $this->opt['mail_file_domain'];
         }
     } else {
         if ($this->opt['require_confirmation']) {
             $mail_file = $this->opt['mail_file_conf'];
         } else {
             $mail_file = $this->opt['mail_file'];
         }
     }
     $mail = read_lang_txt_file($mail_file, "txt", $_SESSION['lang'], array(array("domain", $domain_name), array("sip_address", $sip_address), array("login_url", $login_url), array("admin_url", $admin_url), array("confirmation_url", $confirmation_url), array("username", $username), array("password", $password), array("email", isset($_POST[$an['email']]) ? $_POST[$an['email']] : ""), array("first_name", isset($_POST[$an['fname']]) ? $_POST[$an['fname']] : ""), array("last_name", isset($_POST[$an['lname']]) ? $_POST[$an['lname']] : "")));
     if ($mail === false) {
         /* needn't write message to log. It's written by function read_lang_txt_file */
         $errors[] = $lang_str['err_sending_mail'];
         $data->transaction_rollback();
         return false;
     }
     $o = array('did' => $this->did);
     if (false === ($from_header = Attributes::get_attribute($an['contact_email'], $o))) {
         return false;
     }
     if ($from_header) {
         $mail['headers']['from'] = $from_header;
     }
     if (!send_mail($_POST[$an['email']], $mail['body'], $mail['headers'])) {
         $errors[] = $lang_str['err_sending_mail'];
         $this->controler->_form_load_defaults();
         $data->transaction_rollback();
         return false;
     }
     if (false === $data->transaction_commit()) {
         return false;
     }
     if ($this->opt['redirect_on_register']) {
         $this->controler->change_url_for_reload($this->opt['redirect_on_register']);
     }
     return array("m_user_registered=" . RawURLEncode($this->opt['instance_id']), "reg_sip_adr=" . RawURLEncode($sip_address), "require_conf=" . RawURLEncode($this->opt['require_confirmation']), $user_param);
     //$user_param sets the user_id holding ny controller
 }
 function create_html_form(&$errors)
 {
     global $data, $config;
     parent::create_html_form($errors);
     $attr_types =& Attr_types::singleton();
     //get list of attributes
     if (false === ($this->attr_types =& $attr_types->get_attr_types())) {
         return false;
     }
     switch ($this->opt['attrs_kind']) {
         case "uri":
             // get uri_attrs
             $this->uri_attrs =& Uri_Attrs::singleton($this->uri_scheme, $this->uri_uname, $this->uri_did);
             if (false === ($uri_attrs = $this->uri_attrs->get_attributes())) {
                 return false;
             }
         case "user":
             // get user_attrs
             $this->user_attrs =& User_Attrs::singleton($this->uid);
             if (false === ($user_attrs = $this->user_attrs->get_attributes())) {
                 return false;
             }
         case "domain":
             // get domain_attrs
             $this->domain_attrs =& Domain_Attrs::singleton($this->did);
             if (false === ($domain_attrs = $this->domain_attrs->get_attributes())) {
                 return false;
             }
         case "global":
             // get global_attrs
             $this->global_attrs =& Global_Attrs::singleton();
             if (false === ($global_attrs = $this->global_attrs->get_attributes())) {
                 return false;
             }
     }
     $this->attr_values = array();
     foreach ($this->attr_types as $k => $v) {
         if ($this->opt['attrs_kind'] == 'uri' and !$this->attr_types[$k]->is_for_URIs()) {
             continue;
         } elseif ($this->opt['attrs_kind'] == 'user' and !$this->attr_types[$k]->is_for_users()) {
             continue;
         } elseif ($this->opt['attrs_kind'] == 'domain' and !$this->attr_types[$k]->is_for_domains()) {
             continue;
         } elseif ($this->opt['attrs_kind'] == 'global' and !$this->attr_types[$k]->is_for_globals()) {
             continue;
         }
         switch ($this->opt['attrs_kind']) {
             case "uri":
                 if (isset($uri_attrs[$k])) {
                     $this->attr_values[$k] = $uri_attrs[$k];
                     break;
                 }
             case "user":
                 if (isset($user_attrs[$k])) {
                     $this->attr_values[$k] = $user_attrs[$k];
                     break;
                 }
             case "domain":
                 if (isset($domain_attrs[$k])) {
                     $this->attr_values[$k] = $domain_attrs[$k];
                     break;
                 }
             case "global":
                 if (isset($global_attrs[$k])) {
                     $this->attr_values[$k] = $global_attrs[$k];
                     break;
                 }
         }
         /*
          *	If the value of attribute is not found, set it as null
          */
         if (!isset($this->attr_values[$k])) {
             $this->attr_values[$k] = null;
         }
     }
     // if option 'atributes' is not given, that mean we will work with all attributes
     if (empty($this->opt['attributes'])) {
         foreach ($this->attr_values as $k => $v) {
             // work only with attributes which have access to read
             if ($this->access_to_read($k)) {
                 $this->opt['attributes'][] = $k;
             }
         }
     } else {
         foreach ($this->opt['attributes'] as $k => $v) {
             if (!array_key_exists($v, $this->attr_values)) {
                 log_errors(PEAR::RaiseError("Attribute named '" . $v . "' does not exists"), $errors);
                 unset($this->opt['attributes'][$k]);
             }
         }
     }
     //except unwanted arguments
     $this->opt['attributes'] = array_diff($this->opt['attributes'], $this->opt['exclude_attributes']);
     //save avaiable attrs before are filtered by group
     $this->all_avaiable_attrs = $this->opt['attributes'];
     if (!empty($this->opt['attrs_group'])) {
         foreach ($this->opt['attributes'] as $k => $v) {
             // work only with attributes from specified group
             if ($this->attr_types[$v]->get_group() != $this->opt['attrs_group']) {
                 unset($this->opt['attributes'][$k]);
             }
         }
     }
     //set options to attributes
     foreach ($this->opt['attributes'] as $att) {
         if (isset($this->opt['attrs_options'][$att]) and is_array($this->opt['attrs_options'][$att])) {
             foreach ($this->opt['attrs_options'][$att] as $k => $v) {
                 $this->attr_types[$att]->set_opt($k, $v);
             }
         }
     }
     // add elements to form object
     foreach ($this->opt['attributes'] as $att) {
         if (!$this->access_to_change($att)) {
             continue;
         }
         //if attribute cannot be changed, do not add it ot the form
         $opt = array();
         $opt['err_msg'] = isset($this->opt['error_messages'][$att]) ? $this->opt['error_messages'][$att] : null;
         $this->attr_types[$att]->form_element($this->f, $this->attr_values[$att], $opt);
         $this->js_on_subm .= $this->attr_types[$att]->validation_js_before();
         $this->js_on_subm_2 .= $this->attr_types[$att]->validation_js_after();
     }
     if (!empty($this->opt['validate_js_funct'])) {
         $this->js_on_subm_2 .= $this->opt['validate_js_funct'];
     }
 }
Example #7
0
 /**
  *	create html form 
  *
  *	@param array $errors	array with error messages
  *	@return null			FALSE on failure
  */
 function create_html_form(&$errors)
 {
     global $data, $lang_str, $config;
     parent::create_html_form($errors);
     $an =& $config->attr_names;
     /* get list of customers */
     if (false === ($this->customers = $data->get_customers(array(), $errors))) {
         return false;
     }
     /* if domain id is set */
     if (!is_null($this->id)) {
         $domain_attrs =& Domain_Attrs::singleton($this->id);
         /* get domain attributes */
         if (false === ($this->domain_attrs = $domain_attrs->get_attributes())) {
             return false;
         }
     }
     $options = array();
     $options[] = array('label' => "--- " . $lang_str['none'] . " ---", 'value' => -1);
     foreach ($this->customers as $v) {
         $options[] = array('label' => $v['name'], 'value' => $v['cid']);
     }
     $selected_owner = null;
     /* set preselected customer */
     if (!is_null($this->opt['preselected_customer'])) {
         $selected_owner = $this->opt['preselected_customer'];
     }
     /* if domain id is set */
     if (!is_null($this->id)) {
         /* get owner of the domain */
         if (isset($this->domain_attrs[$an['dom_owner']])) {
             $this->owner['id'] = $this->domain_attrs[$an['dom_owner']];
             $this->owner['name'] = $this->customers[$this->owner['id']]['name'];
             $selected_owner = $this->owner['id'];
         }
         /* get list of the domain names */
         if (false === $this->get_domain_names($errors)) {
             return false;
         }
     }
     $reg =& CReg::singleton();
     $this->f->add_element(array("type" => "text", "name" => "do_new_name", "size" => 16, "maxlength" => 128, "value" => "", "valid_regex" => "^(" . $reg->host . ")?\$", "valid_e" => $lang_str['fe_not_valid_domainname']));
     $this->f->add_extra_submit("do_okey_add", $this->opt['form_add_submit']);
     $this->f->add_element(array("type" => "select", "name" => "do_customer", "size" => 1, "options" => $options, "value" => $selected_owner));
 }
 /**
  *  Mark domain as deleted
  *
  *  Possible options:
  *
  *    - did		(int)   - REQUIRED - id of domain which will be deleted 
  *                        (default: null)
  *	  - undelete (bool) - undelete domain, setting this to true will 
  *                        undelete only domain names and domain attrs. Not
  *                        URIs and credentials within the domain
  *                        (default: false)
  *      
  *	@param array $opt		associative array of options
  *	@return bool			TRUE on success, FALSE on failure
  */
 function mark_domain_deleted($opt)
 {
     global $config;
     $errors = array();
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return false;
     }
     /* table's name */
     $td_name =& $config->data_sql->domain->table_name;
     $ta_name =& $config->data_sql->domain_attrs->table_name;
     $tu_name =& $config->data_sql->uri->table_name;
     $tc_name =& $config->data_sql->credentials->table_name;
     /* col names */
     $cd =& $config->data_sql->domain->cols;
     $ca =& $config->data_sql->domain_attrs->cols;
     $cu =& $config->data_sql->uri->cols;
     $cc =& $config->data_sql->credentials->cols;
     /* flags */
     $fd =& $config->data_sql->domain->flag_values;
     $fa =& $config->data_sql->domain_attrs->flag_values;
     $fu =& $config->data_sql->uri->flag_values;
     $fc =& $config->data_sql->credentials->flag_values;
     $an =& $config->attr_names;
     $o_did = isset($opt['did']) ? $opt['did'] : null;
     $o_undelete = isset($opt['undelete']) ? (bool) $opt['undelete'] : false;
     if (is_null($o_did)) {
         ErrorHandler::log_errors(PEAR::raiseError('domain for mark as deleted is not specified'));
         return false;
     }
     /* if 'did' column in credentials table is not used, make list of all
          realms matching this domain
        */
     if (!$config->auth['use_did']) {
         $dh =& Domains::singleton();
         if (false === ($dom_names = $dh->get_domain_names($o_did))) {
             return false;
         }
         $da =& Domain_Attrs::singleton($o_did);
         if (false === ($realm = $da->get_attribute($config->attr_names['digest_realm']))) {
             return false;
         }
         $realms_w = array();
         if (!is_null($realm)) {
             $realms_w[] = $cc->realm . " = " . $this->sql_format($realm, "s");
         }
         foreach ($dom_names as $v) {
             $realms_w[] = $cc->realm . " = " . $this->sql_format($v, "s");
         }
     }
     if (false === $this->transaction_start()) {
         return false;
     }
     $domain_attrs =& Domain_Attrs::singleton($o_did);
     if ($o_undelete) {
         if (false === $domain_attrs->unset_attribute($an['deleted_ts'])) {
             $this->transaction_rollback();
             return false;
         }
     } else {
         if (false === $domain_attrs->set_attribute($an['deleted_ts'], time())) {
             $this->transaction_rollback();
             return false;
         }
     }
     $q = "update " . $td_name . " set ";
     if ($o_undelete) {
         $q .= $cd->flags . " = " . $cd->flags . " & ~" . $fd['DB_DELETED'];
     } else {
         $q .= $cd->flags . " = " . $cd->flags . " | " . $fd['DB_DELETED'];
     }
     $q .= " where " . $cd->did . " = " . $this->sql_format($o_did, "s");
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         $this->transaction_rollback();
         return false;
     }
     $q = "update " . $ta_name . " set ";
     if ($o_undelete) {
         $q .= $ca->flags . " = " . $ca->flags . " & ~" . $fa['DB_DELETED'];
     } else {
         $q .= $ca->flags . " = " . $ca->flags . " | " . $fa['DB_DELETED'];
     }
     $q .= " where " . $ca->did . " = " . $this->sql_format($o_did, "s");
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         $this->transaction_rollback();
         return false;
     }
     if (!$o_undelete) {
         $q = "update " . $tu_name . " set ";
         $q .= $cu->flags . " = " . $cu->flags . " | " . $fu['DB_DELETED'];
         $q .= " where " . $cu->did . " = " . $this->sql_format($o_did, "s");
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             ErrorHandler::log_errors($res);
             $this->transaction_rollback();
             return false;
         }
         $q = "update " . $tc_name . " set ";
         $q .= $cc->flags . " = " . $cc->flags . " | " . $fc['DB_DELETED'];
         if ($config->auth['use_did']) {
             $q .= " where " . $cc->did . " = " . $this->sql_format($o_did, "s");
         } else {
             if (!$realms_w) {
                 $q .= " where " . $this - sql_format(false, "b");
             } else {
                 $q .= " where " . implode($realms_w, " or ");
             }
         }
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             ErrorHandler::log_errors($res);
             $this->transaction_rollback();
             return false;
         }
     }
     if (false === $this->transaction_commit()) {
         return false;
     }
     return true;
 }
 /**
  *	generate new alias number
  */
 function get_new_alias_number($did, $opt)
 {
     global $config;
     $errors = array();
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return 0;
     }
     /* table name */
     $t_name =& $config->data_sql->uri->table_name;
     /* col names */
     $c =& $config->data_sql->uri->cols;
     /* flags */
     $f =& $config->data_sql->uri->flag_values;
     if ($config->alias_generation == 'rand' or isModuleLoaded('xxl')) {
         //random alias generation
         $retries = 0;
         do {
             //create alias
             $alias = $config->alias_prefix;
             for ($i = 0; $i < $config->alias_lenght; $i++) {
                 $alias .= mt_rand(0, 9);
             }
             $alias .= $config->alias_postfix;
             //check if alias isn't used
             $q = "select count(username) \n\t\t\t\t    from " . $t_name . " \n\t\t\t\t\twhere " . $c->did . "      = " . $this->sql_format($did, "s") . " and \n\t\t\t\t\t      " . $c->username . " = " . $this->sql_format($alias, "s");
             $res = $this->db->query($q);
             if (DB::isError($res)) {
                 ErrorHandler::log_errors($res);
                 return false;
             }
             $row = $res->fetchRow(DB_FETCHMODE_ORDERED);
             $res->free();
             if ($row[0] == 0) {
                 break;
             }
             $retries++;
         } while ($retries < $config->alias_generation_retries);
         if ($retries < $config->alias_generation_retries) {
             return $alias;
         } else {
             ErrorHandler::log_errors(PEAR::raiseError("can't find any unused alias number"));
             return false;
         }
     } else {
         //incremental alias generation
         // get value for new alias
         $o = array('did' => $did);
         if (false === ($alias = Attributes::get_Attribute($config->attr_names['highest_alias_number'], $o))) {
             return false;
         }
         $alias = (int) $alias;
         // if value is not set, use the config value
         if (!$alias or $alias < $config->first_alias_number) {
             $alias = $config->first_alias_number;
         }
         do {
             // check if the username is aready used
             $q = "select count(*) \n                    from " . $t_name . " \n                    where " . $c->did . " = " . $this->sql_format($did, "s") . " and \n                        " . $c->username . " = " . $this->sql_format($alias, "n");
             $res = $this->db->query($q);
             if (DB::isError($res)) {
                 ErrorHandler::log_errors($res);
                 return false;
             }
             $row = $res->fetchRow(DB_FETCHMODE_ORDERED);
             $res->free();
             // if is used, increment it and try again
             if ($row[0]) {
                 $alias++;
             }
         } while ($row[0]);
         $da_h =& Domain_Attrs::singleton($did);
         if (false === $da_h->set_attribute($config->attr_names['highest_alias_number'], $alias)) {
             return false;
         }
         return $alias;
     }
 }
 function action_confirm_reg(&$errors)
 {
     global $data, $config, $lang_str;
     if (isset($_GET['pr'])) {
         $proxy['proxy'] = base64_decode($_GET['pr']);
         if ($proxy['proxy']) {
             if (false === $data->set_home_proxy($proxy['proxy'])) {
                 return false;
             }
         }
     }
     if (isModuleLoaded('xxl') and !$proxy['proxy']) {
         $errors[] = $lang_str['err_reg_conf_not_exists_conf_num'];
         return false;
     }
     $an =& $config->attr_names;
     /* get uid */
     $o = array('name' => $an['confirmation'], 'value' => $this->nr);
     if (false === ($attrs = $data->get_attr_by_val("user", $o))) {
         return false;
     }
     if (empty($attrs[0]['id'])) {
         $this->wrong_nr = true;
         ErrorHandler::add_error($lang_str['err_reg_conf_not_exists_conf_num']);
         return false;
     }
     $uid = $attrs[0]['id'];
     /* get did - for the case that domain has been created during registration */
     $o = array('name' => $an['confirmation'], 'value' => $this->nr);
     if (false === ($attrs = $data->get_attr_by_val("domain", $o))) {
         return false;
     }
     $did = null;
     if (!empty($attrs[0]['id'])) {
         $did = $attrs[0]['id'];
     }
     if (false === $data->transaction_start()) {
         return false;
     }
     // first enable domain
     if (!is_null($did)) {
         $dm_h =& DomainManipulator::singleton($did);
         if (false === $dm_h->enable_domain(true)) {
             return false;
         }
         $domain_attrs =& Domain_Attrs::singleton($did);
         if (false === $domain_attrs->unset_attribute($an['confirmation'])) {
             $data->transaction_rollback();
             return false;
         }
         if (false === $domain_attrs->unset_attribute($an['pending_ts'])) {
             $data->transaction_rollback();
             return false;
         }
     }
     $o = array("uid" => $uid, "disable" => false);
     if (false === $data->enable_user($o)) {
         $data->transaction_rollback();
         return false;
     }
     $user_attrs =& User_Attrs::singleton($uid);
     if (false === $user_attrs->unset_attribute($an['confirmation'])) {
         $data->transaction_rollback();
         return false;
     }
     if (false === $user_attrs->unset_attribute($an['pending_ts'])) {
         $data->transaction_rollback();
         return false;
     }
     if (false === $data->transaction_commit()) {
         return false;
     }
     if ($this->opt['setup_jabber_account']) {
         ErrorHandler::add_error("Registration in jabber not maintained, please set \$config->setup_jabber_account=false in config file.");
         # Jabber Gateway registration
         $res = reg_jab($user_id->uname);
         if ($res != 0) {
             $res = $res + 1;
             log_errors(PEAR::raise_error("jabber registration failed: <" . $user_id->uname . "> [" . $res . "]"), $errors);
             return array("confirmation_ok=1", "conf_jabber_failed=1");
         }
     }
     return array("confirmation_ok=1");
 }
 /**
  *  Get array of uids which URIs and credentials asociated ONLY with 
  *  the domain. And not with any other domain.
  *
  *  Possible options:
  *   - none
  *
  *  @param  string  $did        Domain ID
  *  @param  array   $opt        array of options
  *  @return array               FALSE on error
  */
 function get_uid_of_domain($did, $opt)
 {
     global $config;
     $errors = array();
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return false;
     }
     /* table's name */
     $tu_name =& $config->data_sql->uri->table_name;
     $tc_name =& $config->data_sql->credentials->table_name;
     /* col names */
     $cu =& $config->data_sql->uri->cols;
     $cc =& $config->data_sql->credentials->cols;
     /* flags */
     $fu =& $config->data_sql->uri->flag_values;
     $fc =& $config->data_sql->credentials->flag_values;
     /* if 'did' column in credentials table is not used, make list of all
          realms matching this domain
        */
     if (!$config->auth['use_did']) {
         $dh =& Domains::singleton();
         if (false === ($dom_names = $dh->get_domain_names($did))) {
             return false;
         }
         $da =& Domain_Attrs::singleton($did);
         if (false === ($realm = $da->get_attribute($config->attr_names['digest_realm']))) {
             return false;
         }
         $realms_w1 = array();
         $realms_w2 = array();
         if (!is_null($realm)) {
             $realms_w1[] = $cc->realm . " = " . $this->sql_format($realm, "s");
             $realms_w2[] = $cc->realm . " != " . $this->sql_format($realm, "s");
         }
         foreach ($dom_names as $v) {
             $realms_w1[] = $cc->realm . " = " . $this->sql_format($v, "s");
             $realms_w2[] = $cc->realm . " != " . $this->sql_format($v, "s");
         }
     }
     $uids = array();
     /* get list of UIDs which have URI asociated with the domain */
     $q = "select distinct " . $cu->uid . " as uid\n            from " . $tu_name . "\n            where  " . $cu->did . " = " . $this->sql_format($did, "s") . " and \n                  (" . $cu->flags . " & " . $fu['DB_DISABLED'] . ") = 0";
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     /* add the list to UIDs array */
     while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
         $uids[$row['uid']] = true;
     }
     /* get list of UIDs which have credentials asociated with the domain */
     if ($config->auth['use_did']) {
         $q = "select distinct " . $cc->uid . " as uid\n                from " . $tc_name . "\n                where  " . $cc->did . " = " . $this->sql_format($did, "s") . " and \n                      (" . $cc->flags . " & " . $fc['DB_DISABLED'] . ") = 0";
     } else {
         if (!$realms_w1) {
             $realms_w1 = array($this - sql_format(false, "b"));
         }
         $q = "select distinct " . $cc->uid . " as uid\n                from " . $tc_name . "\n                where  (" . implode($realms_w1, " or ") . ") and \n                      (" . $cc->flags . " & " . $fc['DB_DISABLED'] . ") = 0";
     }
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     /* add the list to UIDs array */
     while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
         $uids[$row['uid']] = true;
     }
     /* get list of UIDs which have URI asociated with other domains */
     $q = "select distinct " . $cu->uid . " as uid\n            from " . $tu_name . "\n            where  " . $cu->did . " != " . $this->sql_format($did, "s") . " and \n                  (" . $cu->flags . " & " . $fu['DB_DISABLED'] . ") = 0";
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     /* and remove them from UIDs array */
     while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
         if (isset($uids[$row['uid']])) {
             unset($uids[$row['uid']]);
         }
     }
     /* get list of UIDs which have credentials asociated with other domains */
     if ($config->auth['use_did']) {
         $q = "select distinct " . $cc->uid . " as uid\n                from " . $tc_name . "\n                where  " . $cc->did . " != " . $this->sql_format($did, "s") . " and \n                      (" . $cc->flags . " & " . $fc['DB_DISABLED'] . ") = 0";
     } else {
         if (!$realms_w2) {
             $realms_w1 = array($this - sql_format(true, "b"));
         }
         $q = "select distinct " . $cc->uid . " as uid\n                from " . $tc_name . "\n                where  (" . implode($realms_w2, " and ") . ") and \n                      (" . $cc->flags . " & " . $fc['DB_DISABLED'] . ") = 0";
     }
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         return false;
     }
     /* and remove them from UIDs array */
     while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
         if (isset($uids[$row['uid']])) {
             unset($uids[$row['uid']]);
         }
     }
     return array_keys($uids);
 }