/**
  *  update customer table by $values
  *
  *  Keys of associative array $values:
  *    name
  *    address
  *    email
  *    phone
  *
  *  Possible options:
  *
  *    primary_key	(array) required
  *      contain primary key of record which should be updated
  *      The array contain the same keys as functon get_customers returned in entry 'primary_key'
  *
  *    insert  	(bool) default:true
  *      if true, function insert new record, otherwise update old record
  *
  *	  new_id	(bool)
  *		In this option is returned ID of new created customer. 
  *		Option is created only if 'insert'=true
  *
  *	@param array $values	values
  *	@param array $opt		associative array of options
  *	@param array $errors	error messages
  *	@return bool			TRUE on success, FALSE on failure
  */
 function update_customer($values, &$opt, &$errors)
 {
     global $config;
     if (!$this->connect_to_db($errors)) {
         return false;
     }
     /* table's name */
     $tc_name =& $config->data_sql->customers->table_name;
     /* col names */
     $cc =& $config->data_sql->customers->cols;
     $opt_insert = isset($opt['insert']) ? (bool) $opt['insert'] : false;
     if (!$opt_insert and (!isset($opt['primary_key']) or !is_array($opt['primary_key']) or empty($opt['primary_key']))) {
         log_errors(PEAR::raiseError('primary key is missing'), $errors);
         return false;
     }
     if ($opt_insert) {
         $sem = new Shm_Semaphore(__FILE__, "s", 1, 0600);
         /* set semaphore to be sure there will not be generated same id for two customers */
         if (!$sem->acquire()) {
             return false;
         }
         $q = "select max(" . $cc->cid . ") from " . $tc_name;
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             ErrorHandler::log_errors($res);
             $sem->release();
             return false;
         }
         $next_id = 1;
         if ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) {
             if (!is_null($row[0])) {
                 $next_id = $row[0] + 1;
             }
         }
         $q = "insert into " . $tc_name . " (\n\t\t\t\t\t   " . $cc->cid . ", " . $cc->name . ", " . $cc->address . ", " . $cc->email . ", " . $cc->phone . "\n\t\t\t    ) \n\t\t\t\tvalues (\n\t\t\t\t\t   " . $this->sql_format($next_id, "n") . ", \n\t\t\t\t\t   " . $this->sql_format($values['name'], "s") . ", \n\t\t\t\t\t   " . $this->sql_format($values['address'], "s") . ", \n\t\t\t\t\t   " . $this->sql_format($values['email'], "s") . ", \n\t\t\t\t\t   " . $this->sql_format($values['phone'], "s") . "\n\t\t\t\t )";
         $opt['new_id'] = $next_id;
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             log_errors($res, $errors);
             $sem->release();
             return false;
         }
         $sem->release();
     } else {
         $q = "update " . $tc_name . " \n\t\t\t    set " . $cc->name . "   =" . $this->sql_format($values['name'], "s") . ",  \n\t\t\t        " . $cc->address . "=" . $this->sql_format($values['address'], "s") . ", \n\t\t\t        " . $cc->email . "  =" . $this->sql_format($values['email'], "s") . ", \n\t\t\t        " . $cc->phone . "  =" . $this->sql_format($values['phone'], "s") . "\n\t\t\t\twhere " . $cc->cid . "  =" . $this->sql_format($opt['primary_key']['cid'], "n");
         $res = $this->db->query($q);
         if (DB::isError($res)) {
             log_errors($res, $errors);
             return false;
         }
     }
     return true;
 }
Пример #2
0
 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
 }
Пример #3
0
 function update_domain($domainname, $customer, &$errors)
 {
     global $data;
     $sem = new Shm_Semaphore(__FILE__, "s", 1, 0600);
     if (false === $data->transaction_start()) {
         return false;
     }
     FileJournal::clear();
     /* 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->generate_domain_id($domainname, $errors)) {
         return false;
     }
     if (!empty($domainname)) {
         if (false === DomainManipulator::add_alias($this->id, $domainname, null)) {
             FileJournal::rollback();
             $data->transaction_rollback();
             $this->revert_domain_id();
             $sem->release();
             return false;
         }
     }
     $a_vals = array("owner_id" => $customer, "alias" => $domainname);
     if (false === DomainManipulator::update_domain_attrs($this->id, $a_vals)) {
         FileJournal::rollback();
         $data->transaction_rollback();
         $this->revert_domain_id();
         $sem->release();
         return false;
     }
     if (false === $data->transaction_commit()) {
         return false;
     }
     $sem->release();
     return true;
 }
 /**
  *	store content of file specific for a domain to db
  *
  *  Possible options parameters:
  *	  deleted		(bool)
  *		set to true if the file has been deleted
  *
  *	  dir			(bool)
  *		set to true if the file is directory
  *
  *	  new_version	(int)
  *		In this option is returned next version of file 
  *
  *
  *	@param string $did		domain id
  *	@param string $file		filename (with path)
  *	@param string $content	content of file
  *	@param array $opt		associative array of options
  *	@return bool			TRUE on success, FALSE on failure
  */
 function write_file_content($did, $file, $content, &$opt)
 {
     global $config, $lang_set;
     $errors = array();
     if (!$this->connect_to_db($errors)) {
         ErrorHandler::add_error($errors);
         return false;
     }
     /* table's name */
     $td_name =& $config->data_sql->domain_settings->table_name;
     /* col names */
     $cd =& $config->data_sql->domain_settings->cols;
     /* flags */
     $fd =& $config->data_sql->domain_settings->flag_values;
     $o_deleted = isset($opt['deleted']) ? (bool) $opt['deleted'] : false;
     $o_dir = isset($opt['dir']) ? (bool) $opt['dir'] : false;
     $sem = new Shm_Semaphore(__FILE__, "s", 1, 0600);
     /* set semaphore to be sure there will not be generated same versions */
     if (!$sem->acquire()) {
         return false;
     }
     $q = "select max(" . $cd->version . ") \n\t\t      from " . $td_name . " \n\t\t\t  where " . $cd->did . " = " . $this->sql_format($did, "s") . " and\n\t\t\t        " . $cd->filename . " = " . $this->sql_format($file, "s");
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         $sem->release();
         return false;
     }
     $version = 1;
     if ($row = $res->fetchRow(DB_FETCHMODE_ORDERED)) {
         if (!is_null($row[0])) {
             $version = $row[0] + 1;
         }
     }
     /* set default charset for sql query */
     if (!empty($config->data_sql->set_charset)) {
         if (false === $this->set_db_charset('default', null, $errors)) {
             ErrorHandler::add_error($errors);
             $sem->release();
             return false;
         }
     }
     $flags = 0;
     if ($o_deleted) {
         $flags |= $fd["DB_DELETED"];
     }
     if ($o_dir) {
         $flags |= $fd["DB_DIR"];
     }
     $q = "insert into " . $td_name . " (\n\t\t\t\t   " . $cd->did . ",\n\t\t\t\t   " . $cd->filename . ",\n\t\t\t\t   " . $cd->version . ",\n\t\t\t\t   " . $cd->timestamp . ",\n\t\t\t\t   " . $cd->content . ",\n\t\t\t\t   " . $cd->flags . "\n\t\t    ) \n\t\t\tvalues (\n\t\t\t\t   " . $this->sql_format($did, "s") . ", \n\t\t\t\t   " . $this->sql_format($file, "s") . ", \n\t\t\t\t   " . $this->sql_format($version, "n") . ",\n\t\t\t\t   " . $this->sql_format(time(), "n") . ",\n\t\t\t\t   " . $this->sql_format($content, "I") . ",\n\t\t\t\t   " . $this->sql_format($flags, "n") . "\n\t\t\t )";
     $opt['new_version'] = $version;
     $res = $this->db->query($q);
     if (DB::isError($res)) {
         ErrorHandler::log_errors($res);
         $sem->release();
         return false;
     }
     $sem->release();
     /* restore previous charset for sql queries */
     if (!empty($config->data_sql->set_charset)) {
         if (false === $this->set_db_charset($lang_set['charset'], null, $errors)) {
             ErrorHandler::add_error($errors);
             return false;
         }
     }
     return true;
 }