function cw_user_update($userinfo, $customer_id, $by_customer_id)
{
    global $tables, $addons;
    foreach (array('email', 'status', 'membership_id', 'language', 'change_password') as $fld) {
        if (isset($userinfo[$fld])) {
            $customer[$fld] = $userinfo[$fld];
        }
    }
    if (AREA_TYPE == 'A' && $userinfo['usertype']) {
        $customer['usertype'] = $userinfo['usertype'];
    }
    if (AREA_TYPE == 'A' && $userinfo['customer_id']) {
        $customer['customer_id'] = $userinfo['customer_id'];
    }
    $current = cw_user_get_info($customer_id, 0);
    //allow password update only by the profile owner or by admin
    if (isset($userinfo['password'])) {
        if (!empty($userinfo['password']) && ($customer_id == $by_customer_id || AREA_TYPE == 'A')) {
            $customer['password'] = cw_call('cw_user_get_hashed_password', array($userinfo['password']));
        }
    }
    cw_event('on_user_update', array($customer_id, $by_customer_id, $customer, $userinfo));
    cw_array2update('customers', $customer, "customer_id='{$customer_id}'");
    $additional_info = $userinfo['additional_info'];
    $customer_info_fields = array('ssn', 'tax_number', 'birthday', 'birthday_place', 'sex', 'married', 'nationality', 'company', 'employees', 'foundation', 'foundation_place', 'company_type', 'company_id', 'can_change_company_id', 'contact_date', 'tax_id', 'payment_id', 'payment_note', 'tax_exempt', 'separate_invoices', 'shipping_operated', 'shipment_paid', 'shipping_company_to_carrier_id', 'shipping_company_from_carrier_id', 'cod_delivery_type_id', 'leaving_type', 'department_id', 'division_id', 'doc_prefix', 'order_entering_format', 'status_note');
    if (!cw_query_first_cell("select count(*) from {$tables['customers_customer_info']} where customer_id='{$customer_id}'")) {
        cw_array2insert('customers_customer_info', array('customer_id' => $customer_id));
    }
    cw_array2update('customers_customer_info', $additional_info, "customer_id='{$customer_id}'", $customer_info_fields);
    /*
          db_query("update $tables[customers_addresses] set main=0, current=0 where customer_id = '$customer_id'");
        foreach(array('main_address', 'current_address') as $addr)  {
            if (!$userinfo[$addr]) continue;
    
            $userinfo[$addr]['main'] = $addr == 'main_address';
            $userinfo[$addr]['current'] = $addr == 'current_address';
    
            $address_id = $userinfo[$addr]['address_id'];
            if (!$address_id) $address_id = cw_array2insert('customers_addresses', array('customer_id' => $customer_id, 'main' => $userinfo[$addr]['main'], 'current' => $userinfo[$addr]['current']));
    
            cw_user_update_address($customer_id, $address_id, $userinfo[$addr]);
        }
    */
    foreach (cw_user_address_array($userinfo['addresses']) as $address_id => $address) {
        cw_user_update_address($customer_id, $address['address_id'], $address);
    }
    cw_user_check_addresses($customer_id);
    $relations = $userinfo['relations'];
    $relations_info = array('salesman_customer_id' => $relations['salesman_customer_id'], 'employee_customer_id' => $relations['employee_customer_id'], 'warehouse_customer_id' => $relations['warehouse_customer_id']);
    if (!cw_query_first_cell("select count(*) from {$tables['customers_relations']} where customer_id='{$customer_id}'")) {
        db_query("insert into {$tables['customers_relations']}(customer_id) values('{$customer_id}')");
    }
    cw_array2update('customers_relations', $relations_info, "customer_id='{$customer_id}'");
    if ($userinfo['chamber_certificate_uploaded']['file_path']) {
        db_query("delete from {$tables['customers_chamber_certificates']} where customer_id='{$customer_id}'");
        cw_file_area_save('customers_chamber_certificates', $customer_id, $userinfo['chamber_certificate_uploaded']);
    }
    if (is_array($userinfo['custom_fields'])) {
        foreach ($userinfo['custom_fields'] as $field_id => $value) {
            db_query("delete from {$tables['register_fields_values']} where customer_id='{$customer_id}' AND field_id='{$field_id}'");
            cw_array2insert('register_fields_values', array('field_id' => $field_id, 'value' => $value, 'customer_id' => $customer_id));
        }
    }
    $customer_system_info = cw_query_first("select * from {$tables['customers_system_info']} where customer_id='{$customer_id}'");
    $customer_system_info['customer_id'] = $customer_id;
    if (!$customer_system_info['creation_customer_id']) {
        $customer_system_info['creation_customer_id'] = $by_customer_id;
        $customer_system_info['creation_date'] = cw_core_get_time();
    }
    $customer_system_info['modification_customer_id'] = $by_customer_id;
    $customer_system_info['modification_date'] = cw_core_get_time();
    cw_array2insert('customers_system_info', $customer_system_info, true);
    $salesman_info = cw_query_first("select * from {$tables['customers_salesman_info']} where customer_id = '{$customer_id}'");
    $salesman_info['parent_customer_id'] = $userinfo['salesman_info']['parent_customer_id'];
    cw_array2insert('customers_salesman_info', $salesman_info, true);
}
<?php

if ($action == 'delete' && is_array($del)) {
    cw_load('file_area');
    foreach ($del as $file_id => $v) {
        cw_file_area_delete('customers_letters', $file_id);
    }
    cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}");
}
if ($action == 'upload') {
    cw_load('file_area');
    $data = array();
    $data['file_path'] = cw_move_uploaded_file('userfile');
    $data['filename'] = basename($_FILES['userfile']['name']);
    if ($data['file_path']) {
        cw_file_area_save('customers_letters', $user, $data);
    }
    cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}");
}
$search =& cw_session_register('search');
if (isset($_GET['substring'])) {
    $search['letters']['substring'] = $substring;
}
if (isset($_GET['fromdate'])) {
    $search['letters']['fromdate'] = cw_core_strtotime($fromdate);
}
if (isset($_GET['dateto'])) {
    $search['letters']['todate'] = cw_core_strtotime($todate) + 86399;
}
$conditions = '';
$data = $search['letters'];
     if (!$record_id) {
         $record_id = cw_array2insert('accounting_records', array('date' => cw_core_get_time()));
     }
     $to_update = array('customer_id' => $record_data['customer_id'], 'accounting_category_id' => $record_data['accounting_category_id'], 'descr' => $record_data['descr'], 'date_ref' => cw_core_strtotime($record_data['date_ref']), 'bank_code' => $record_data['bank_code'], 'payment_code' => $record_data['payment_code'], 'status' => $record_data['status']);
     if (!$record['reference']) {
         $to_update['total'] = $record_data['total'];
     }
     cw_array2update('accounting_records', $to_update, "accounting_record_id='{$record_id}'");
     cw_accounting_category_recalc_subcat_count($record_data['accounting_category_id']);
     if (count($_FILES)) {
         foreach ($_FILES['enfiles']['name'] as $index => $name) {
             $data = array();
             $data['file_path'] = cw_move_uploaded_file('enfiles', '', $index);
             $data['filename'] = basename($name);
             if ($data['file_path']) {
                 cw_file_area_save('accounting_records_files', $record_id, $data);
             }
         }
     }
     cw_session_unregister('record_saved_data');
     cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}&record_id={$record_id}");
 }
 if ($action == 'delete_enfile' && $file_id) {
     $record_id = cw_query_first_cell("select customer_id from {$tables['accounting_records_files']} where file_id='{$file_id}'");
     cw_file_area_delete('accounting_records_files', $file_id);
     cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}&record_id={$record_id}");
 }
 if ($record_saved_data) {
     $record = array_merge($record, $record_saved_data);
     cw_session_unregister('record_saved_data');
 }
if ($action == 'delete' && is_array($del)) {
    cw_load('file_area');
    foreach ($del as $file_id => $v) {
        cw_file_area_delete('customers_generated_docs', $file_id);
    }
    cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}");
}
if ($action == 'upload') {
    cw_load('file_area');
    $data = array();
    $data['file_path'] = cw_move_uploaded_file('userfile');
    $data['filename'] = basename($_FILES['userfile']['name']);
    $data['descr'] = $descr;
    if ($data['file_path']) {
        cw_file_area_save('customers_generated_docs', $user, $data);
    }
    cw_header_location("index.php?target={$target}&mode={$mode}&user={$user}");
}
$search =& cw_session_register('search');
if (isset($_GET['substring'])) {
    $search['generated_docs']['substring'] = $substring;
}
if (isset($_GET['fromdate'])) {
    $search['generated_docs']['fromdate'] = cw_core_strtotime($fromdate);
}
if (isset($_GET['dateto'])) {
    $search['generated_docs']['todate'] = cw_core_strtotime($todate) + 86399;
}
$conditions = '';
$data = $search['generated_docs'];
        $to_insert['content'] = $data['content'];
    }
    if (!$field_errors) {
        if ($question_id) {
            cw_array2update('faq_questions', $to_insert, "question_id='{$question_id}'");
        } else {
            $question_id = cw_array2insert('faq_questions', $to_insert, "question_id='{$question_id}'");
        }
        $lng_insert = array('question_id' => $question_id, 'thema' => $data['thema'], 'content' => $data['content'], 'code' => $current_language);
        cw_array2insert('faq_questions_lng', $lng_insert, true);
        if ($_FILES) {
            $data = array();
            $data['file_path'] = cw_move_uploaded_file('file', '');
            $data['filename'] = $_FILES['file']['name'];
            if ($data['file_path']) {
                cw_file_area_save('faq_files', $question_id, $data);
            }
        }
        //        cw_faq_recalc_counters(array_shift(cw_faq_rubrik_path($to_insert['rubrik_id'])));
        $parents = cw_faq_parent_rubriks($to_insert['rubrik_id']);
        cw_faq_recalc_counters($parents[0]['rubrik_id']);
        cw_header_location('index.php?target=faq&mode=question&question_id=' . $question_id);
    }
    $saved_question = $to_insert;
    cw_header_location('index.php?target=faq&mode=question&error=1&question_id=' . $question_id);
}
if ($error) {
    $smarty->assign('question', array_map('stripslashes', $saved_question));
    $smarty->assign('field_errors', $field_errors);
    $field_errors = array();
    $saved_question = array();