/** * Generate UID for new subscriber * * @static */ function get_uid($username, $realm) { global $config, $data; $an =& $config->attr_names; $data->add_method('get_new_user_id'); $data->add_method('does_uid_exists'); $ga =& Global_attrs::singleton(); if (false === ($format = $ga->get_attribute($an['uid_format']))) { return false; } switch ($format) { /* numeric UID */ case 1: if (false === ($uid = $data->get_new_user_id(null))) { return false; } break; /* UUID by rfc4122 */ /* UUID by rfc4122 */ case 2: $uid = rfc4122_uuid(); /* check if uid doesn't exists */ if (0 > ($exists = $data->does_uid_exists($uid, null))) { return false; } while ($exists) { $uid = rfc4122_uuid(); if (0 > ($exists = $data->does_uid_exists($uid, null))) { return false; } } break; /* UID in format 'username@realm' */ /* UID in format 'username@realm' */ case 0: default: /* if format of UIDs is not set, assume the first choice */ $uid = $username . "@" . $realm; /* check if uid doesn't exists */ if (0 > ($exists = $data->does_uid_exists($uid, null))) { return false; } $i = 0; while ($exists) { $uid = $username . "@" . $realm . "_" . $i++; if (0 > ($exists = $data->does_uid_exists($uid, null))) { return false; } } break; } return $uid; }
/** * Add new URI to DB * * On error this method returning FALSE. * * Possible options: * - disabled (bool) - set flag disabled (default: false) * - canon (bool) - set flag canonical (default: false) * - flags (int) - Set value of flags directly. If is set, options * 'canon' and 'disabled' are ignored. (default: null) * * @return bool */ function add_uri($uid, $scheme, $uname, $did, $opt) { global $config; $errors = array(); if (!$this->connect_to_db($errors)) { ErrorHandler::add_error($errors); return false; } /* 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; $an =& $config->attr_names; /* set default values for options */ $opt_disabled = isset($opt["disabled"]) ? (bool) $opt["disabled"] : false; $opt_canon = isset($opt["canon"]) ? (bool) $opt["canon"] : false; $opt_flags = isset($opt["flags"]) ? $opt["flags"] : null; if (!is_null($opt_flags)) { $flags = $opt_flags; } else { $ga =& Global_attrs::singleton(); if (false === ($flags = $ga->get_attribute($an['uri_default_flags']))) { return false; } if (!is_numeric($flags)) { ErrorHandler::log_errors(PEAR::raiseError("Global attribute '" . $an['uri_default_flags'] . "' is not defined or is not a number Can't create URI.")); return false; } if ($opt_disabled) { $flags = $flags | $f['DB_DISABLED']; } if ($opt_canon) { $flags = $flags | $f['DB_CANON']; } } $q = "insert into " . $t_name . "(\n\t " . $c->uid . ", " . $c->scheme . ", " . $c->username . ", " . $c->did . ", " . $c->flags . ")\n\t\t values (" . $this->sql_format($uid, "s") . ", \n\t\t\t " . $this->sql_format($scheme, "s") . ", \n\t\t\t " . $this->sql_format($uname, "s") . ", \n\t\t\t\t\t " . $this->sql_format($did, "s") . ", \n\t\t\t\t\t " . $this->sql_format($flags, "n") . ")"; $res = $this->db->query($q); if (DB::isError($res)) { ErrorHandler::log_errors($res); return false; } return true; }
/** * Add new credentials to DB * * On error this method returning FALSE. * * Possible options: * - disabled (bool) - set flag disabled (default: false) * - for_ser (bool) - set flag DB_LOAD_SER. By default value of * this flag depends on global attribute * 'credential_default_flags' (default: null) * - for_serweb (bool) - set flag DB_FOR_SERWEB. By default value of * this flag depends on global attribute * 'credential_default_flags' (default: null) * * @return bool */ function add_credentials($uid, $did, $uname, $realm, $passw, $opt) { global $config; $errors = array(); if (!$this->connect_to_db($errors)) { ErrorHandler::add_error($errors); return false; } /* table name */ $t_name =& $config->data_sql->credentials->table_name; /* col names */ $c =& $config->data_sql->credentials->cols; /* flags */ $f =& $config->data_sql->credentials->flag_values; $an =& $config->attr_names; /* set default values for options */ $opt_disabled = isset($opt["disabled"]) ? (bool) $opt["disabled"] : false; $opt_for_ser = isset($opt["for_ser"]) ? (bool) $opt["for_ser"] : null; $opt_for_serweb = isset($opt["for_serweb"]) ? (bool) $opt["for_serweb"] : null; $ga =& Global_attrs::singleton(); if (false === ($flags = $ga->get_attribute($an['credential_default_flags']))) { return false; } if (!is_numeric($flags)) { ErrorHandler::log_errors(PEAR::raiseError("Global attribute '" . $an['credential_default_flags'] . "' is not defined or is not a number Can't create credentials.")); return false; } if ($opt_disabled) { $flags = $flags | $f['DB_DISABLED']; } if (!is_null($opt_for_ser)) { if ($opt_for_ser) { $flags = $flags | $f['DB_LOAD_SER']; } else { $flags = $flags & ~$f['DB_LOAD_SER']; } } if (!is_null($opt_for_serweb)) { if ($opt_for_serweb) { $flags = $flags | $f['DB_FOR_SERWEB']; } else { $flags = $flags & ~$f['DB_FOR_SERWEB']; } } $did_c = $did_v = ""; $pw_c = $pw_v = ""; if ($config->auth['use_did']) { $did_c = $c->did . ", "; $did_v = $this->sql_format($did, "s") . ", "; } if ($config->clear_text_pw) { $pw_c = $c->password . ", "; $pw_v = $this->sql_format($passw, "s") . ", "; } $ha1 = md5($uname . ":" . $realm . ":" . $passw); $ha1b = md5($uname . "@" . $realm . ":" . $realm . ":" . $passw); $q = "insert into " . $t_name . "(\n\t " . $c->uid . ", " . $did_c . $c->uname . ", " . $c->realm . ", " . $pw_c . " \n\t\t\t\t " . $c->ha1 . ", " . $c->ha1b . ", " . $c->flags . ")\n\t\t values (" . $this->sql_format($uid, "s") . ", \n\t\t " . $did_v . "\n\t\t\t " . $this->sql_format($uname, "s") . ", \n\t\t\t\t\t " . $this->sql_format($realm, "s") . ", \n\t\t " . $pw_v . "\n\t\t\t " . $this->sql_format($ha1, "s") . ", \n\t\t\t\t\t " . $this->sql_format($ha1b, "s") . ", \n\t\t\t\t\t " . $this->sql_format($flags, "n") . ")"; $res = $this->db->query($q); if (DB::isError($res)) { ErrorHandler::log_errors($res); return false; } return true; }
/** * Generate DID for new domain * * @static * @param string domainname new name of domain * @return string did or FALSE on error */ function generate_new_did($domainname) { global $data, $config; $an =& $config->attr_names; $errors = array(); /* get format of did to generate */ $ga =& Global_attrs::singleton(); if (false === ($format = $ga->get_attribute($an['did_format']))) { return false; } switch ($format) { /* numeric DID */ case 1: $data->add_method('get_new_domain_id'); if (false === ($did = $data->get_new_domain_id(null, $errors))) { ErrorHandler::add_error($errors); return false; } break; /* UUID by rfc4122 */ /* UUID by rfc4122 */ case 2: $did = rfc4122_uuid(); /* check if did doesn't exists */ $dh =& Domains::singleton(); if (false === ($dids = $dh->get_all_dids())) { return false; } while (in_array($did, $dids, true)) { $did = rfc4122_uuid(); } break; /* DID as 'domainname' */ /* DID as 'domainname' */ case 0: default: /* if format of UIDs is not set, assume the first choice */ if (!$domainname) { $domainname = "default_domain"; } // if domain name is not provided $did = $domainname; /* check if did doesn't exists */ $dh =& Domains::singleton(); if (false === ($dids = $dh->get_all_dids())) { return false; } $i = 0; while (in_array($did, $dids, true)) { $did = $domainname . "_" . $i++; } break; } return $did; }
function create_html_form(&$errors) { parent::create_html_form($errors); global $lang_str, $config, $sess; $domains =& Domains::singleton(); if (false === ($this->domain_names = $domains->get_id_name_pairs())) { return false; } if ($this->opt['allow_edit']) { $an =& $config->attr_names; $f =& $config->data_sql->uri->flag_values; /* create a list of allowed domain names for the form */ if (is_array($this->opt['allowed_domains'])) { $alowed_domain_names = array(); foreach ($this->opt['allowed_domains'] as $v) { $alowed_domain_names[$v] = $this->domain_names[$v]; } } else { $alowed_domain_names =& $this->domain_names; } /* if flags is not set, get the default flags */ if (!isset($this->act_alias['flags'])) { $ga_handler =& Global_attrs::singleton(); if (false === ($this->act_alias['flags'] = $ga_handler->get_attribute($an['uri_default_flags']))) { return false; } if (!is_numeric($this->act_alias['flags'])) { ErrorHandler::log_errors(PEAR::raiseError("Global attribute '" . $an['uri_default_flags'] . "' is not defined or is not a number Can't create URI.")); return false; } /* if user ha not aliases set he 'canon' flag to true */ if (false === $this->get_aliases($errors)) { return false; } if (!count($this->aliases)) { $this->act_alias['flags'] |= $f['DB_CANON']; } } $f_canon = (bool) ($this->act_alias['flags'] & $f['DB_CANON']); $f_is_to = (bool) ($this->act_alias['flags'] & $f['DB_IS_TO']); $f_is_from = (bool) ($this->act_alias['flags'] & $f['DB_IS_FROM']); $dom_options = array(); foreach ($alowed_domain_names as $k => $v) { $dom_options[] = array("label" => $v, "value" => $k); } $reg =& Creg::singleton(); $this->f->add_element(array("type" => "text", "name" => "al_username", "size" => 16, "maxlength" => 64, "minlength" => 1, "valid_regex" => "^" . $reg->user . "\$", "valid_e" => $lang_str['fe_not_valid_username'], "length_e" => $lang_str['fe_not_filled_username'], "value" => isset($this->act_alias['username']) ? $this->act_alias['username'] : "", "extrahtml" => "onkeyup='alias_ctl.onAliasChange();' oncut='alias_ctl.onAliasChange();' onpaste='alias_ctl.onAliasChange();'")); $this->f->add_element(array("type" => "select", "name" => "al_domain", "options" => $dom_options, "value" => isset($this->act_alias['did']) ? $this->act_alias['did'] : $this->user_id->get_did(), "size" => 1, "extrahtml" => "onchange='alias_ctl.onAliasChange();' onkeyup='alias_ctl.onAliasChange();'")); $this->f->add_element(array("type" => "checkbox", "name" => "al_is_canon", "checked" => $f_canon, "value" => 1)); $this->f->add_element(array("type" => "checkbox", "name" => "al_is_from", "checked" => $f_is_from, "value" => 1)); $this->f->add_element(array("type" => "checkbox", "name" => "al_is_to", "checked" => $f_is_to, "value" => 1)); $this->f->add_element(array("type" => "hidden", "name" => "al_id_u", "value" => isset($this->act_alias['username']) ? $this->act_alias['username'] : "")); $this->f->add_element(array("type" => "hidden", "name" => "al_id_d", "value" => isset($this->act_alias['did']) ? $this->act_alias['did'] : "")); $this->f->add_element(array("type" => "hidden", "name" => "al_id_f", "value" => $this->act_alias['flags'])); $onload_js = "\n var alias_ctl;\n alias_ctl = new Aliases_ctl('alias_ctl');\n alias_ctl.init('" . $this->opt['form_name'] . "', 'al_username', 'al_domain');\n alias_ctl.onAliasChangeUrl='" . $sess->url($_SERVER['PHP_SELF'] . "?kvrk=" . uniqID("") . "&uri_usage=1") . "';\n alias_ctl.aliasSuggestUrl='" . $sess->url($_SERVER['PHP_SELF'] . "?kvrk=" . uniqID("") . "&uri_suggest=1") . "';\n alias_ctl.aliasGenerateUrl='" . $sess->url($_SERVER['PHP_SELF'] . "?kvrk=" . uniqID("") . "&uri_generate=1") . "';\n alias_ctl.lang_str.no_suggestions='" . js_escape($lang_str['no_suggestions']) . "' \n "; $this->controler->set_onload_js($onload_js); } }