static function search($search_options = array(), $limit = -1, $offset = 0) { $sql_options = array('joins' => self::$joins, 'select' => self::$select); $user = new VBX_User(); $users = parent::search(self::$__CLASS__, $user->table, $search_options, $sql_options, $limit, $offset); if (empty($users)) { return $users; } if ($limit == 1) { $users = array($users); } $ci =& get_instance(); $ci->load->model('vbx_device'); foreach ($users as $i => $user) { $users[$i]->devices = VBX_Device::search(array('user_id' => $user->id), 100); } if ($limit == 1 && count($users) == 1) { return $users[0]; } return $users; }
public static function search($search_options = array(), $limit = -1, $offset = 0) { $ci =& get_instance(); $sql_options = array('joins' => self::$joins, 'select' => self::$select); $user = new VBX_User(); $users = parent::search(self::$__CLASS__, $user->table, $search_options, $sql_options, $limit, $offset); if (empty($users)) { return $users; } if ($limit == 1) { $users = array($users); } $ci->load->model('vbx_device'); foreach ($users as $i => $user) { $users[$i]->devices = VBX_Device::search(array('user_id' => $user->id), 100); if ($users[$i]->setting('online') && $users[$i]->setting('online') != 9) { array_unshift($users[$i]->devices, new VBX_Device((object) array('id' => 0, 'name' => 'client', 'value' => 'client:' . $users[$i]->id, 'sms' => 0, 'sequence' => -99, 'is_active' => 1, 'user_id' => $users[$i]->id))); } } if ($limit == 1 && count($users) == 1) { return $users[0]; } return $users; }
private function update_order() { $data['json'] = array('error' => false, 'message' => ''); $order = $this->input->post('order'); try { foreach ($order as $sequence => $device_id) { $params = array('sequence' => $sequence); $device = VBX_Device::get(array('id' => $device_id, 'user_id' => $this->user_id)); if (!$device) { error_log('Device no longer exists: ' . $device_id); continue; } $device->sequence = $sequence; $device->save(); } } catch (VBX_DeviceException $e) { $data['json']['error'] = true; $data['json']['message'] = 'One or more device sequences were not updated'; } if ($this->response_type == 'html') { redirect('account/number'); } $this->respond('', 'account/number', $data); }
protected function after_login_completed($user, $redirect) { $last_seen = $user->last_seen; /* Redirect to flows if this is an admin and his inbox is zero (but not if the caller is hitting the REST api)*/ if ($this->response_type != 'json') { $is_admin = $this->session->userdata('is_admin'); if ($is_admin) { $this->load->model('vbx_incoming_numbers'); $twilio_numbers = array(); try { $twilio_numbers = $this->vbx_incoming_numbers->get_numbers(); if (empty($twilio_numbers)) { $banner = array('id' => 'first-login', 'html' => 'To start setting up OpenVBX, we suggest you start out with building your first <a href="' . site_url('flows') . '">call flow</a>. ', 'title' => 'Welcome to OpenVBX'); setrawcookie('banner', rawurlencode(json_encode($banner)), 0, '/' . ($this->tenant->id > 1 ? $this->tenant->name : '')); return redirect('numbers'); } } catch (VBX_IncomingNumberException $e) { /* Handle gracefully but log it */ error_log($e->getMessage()); } } $devices = VBX_Device::search(array('user_id' => $user->id)); if (empty($devices)) { return redirect('devices'); } } return $this->redirect($redirect); }
function notify_message($message) { $ci =& get_instance(); $ci->load->model('vbx_user'); $ci->load->model('vbx_group'); $ci->load->model('vbx_incoming_numbers'); $recording_host = $ci->settings->get('recording_host', VBX_PARENT_TENANT); $vm_url = $message->content_url; if (!empty($recording_host) && trim($recording_host) != '') { $vm_url = str_replace('api.twilio.com', trim($recording_host), $vm_url); } $message->content_url = $vm_url; $users = array(); $notify = array(); if ($message->owner_type == 'user') { $user = VBX_User::get($message->owner_id); if (!empty($user->email)) { $notify[] = $user->email; } } $group_users = array(); if ($message->owner_type == 'group') { $user_ids = $ci->vbx_group->get_user_ids($message->owner_id); $group = $ci->vbx_group->get_by_id($message->owner_id); $owner = $group->name; } else { if ($message->owner_type == 'user') { $user_ids = array($message->owner_id); $owner = 'Personal'; } } $notification_setting = 'email_notifications_' . $message->type; $email_notify = $ci->vbx_settings->get($notification_setting, $ci->tenant->id); // check the incoming number's capabilities and don't even try to send // an SMS notification if the number is not allowed to send SMS messages $incoming_number = VBX_Incoming_numbers::get(array('phone_number' => normalize_phone_to_E164($message->called))); if (!empty($incoming_number) && $incoming_number->capabilities->sms == 1) { $sms_notify = true; } if ($email_notify || $sms_notify) { foreach ($user_ids as $user_id) { $user = VBX_User::get($user_id); $ci->load->model('vbx_device'); $ci->load->model('vbx_sms_message'); $numbers = VBX_Device::search(array('user_id' => $user_id)); $message_type = 'Voicemail'; if ($message->type == 'sms') { $message_type = 'SMS'; $owner = ''; } if ($email_notify) { $email_subject = "New {$owner} {$message_type} Notification - {$message->caller}"; openvbx_mail($user->email, $email_subject, 'message', compact('message')); } if ($sms_notify) { foreach ($numbers as $number) { if ($number->value && $number->sms) { try { $ci->vbx_sms_message->send_message($message->called, $number->value, $this->tiny_notification_message($message)); } catch (VBX_Sms_messageException $e) { log_message('error', 'unable to send sms alert, reason: ' . $e->getMessage()); } } } } // if ($sms_notify) } } // if ($email_notify || $sms_notify) }
function process_device_for_number($caller_id) { if (preg_match('|device:[0-9]{1,3}|', $caller_id)) { $device_id = str_replace('device:', '', $caller_id); $device = VBX_Device::get(array('id' => $device_id)); if ($device instanceof VBX_Device) { $caller_id = $device->value; } } return $caller_id; }
private function save_user() { $errors = array(); $user = false; $id = intval($this->input->post('id')); $auth_type = $this->input->post('auth_type'); $error = false; $message = "Failed to save user for unknown reason."; $shouldGenerateNewPassword = false; $device_id_str = trim($this->input->post('device_id')); $device_number = trim($this->input->post('device_number')); $shouldSendWelcome = false; try { PhoneNumber::validatePhoneNumber($device_number); } catch (PhoneNumberException $e) { $data['json'] = array('error' => true, 'message' => $e->getMessage()); return $this->respond('', 'accounts', $data); } if (!empty($auth_type)) { $auth_type = $this->vbx_user->get_auth_type($auth_type); } if ($id > 0) { $user = VBX_User::get($id); } else { $user = VBX_User::get(array('email' => $this->input->post('email'))); if (!empty($user) && $user->is_active == 1) { $error = true; $message = 'Email address is already in use.'; } elseif (!empty($user) && $user->is_active == 0) { // It's an old account that was made inactive. By re-adding it, we're // assuming the user wants to re-instate the old account. $shouldSendWelcome = true; } else { // It's a new user $user = new VBX_User(); $user->online = 9; $shouldSendWelcome = true; } } if (!$error) { $fields = array('first_name', 'last_name', 'email', 'is_admin'); foreach ($fields as $field) { $user->{$field} = $this->input->post($field); } $user->is_active = TRUE; $user->auth_type = isset($auth_type->id) ? $auth_type->id : 1; try { $user->save(); if ($shouldSendWelcome) { $user->send_new_user_notification(); } } catch (VBX_UserException $e) { $error = true; $message = $e->getMessage(); log_message('error', 'Unable to send new user notification: ' . $message); } if (!$error) { if (strlen($device_number) > 0) { // We're adding or modifying an existing device if (strlen($device_id_str) > 0) { // We're updating an existing record $device_id = intval($device_id_str); $device = VBX_Device::get($device_id); $device->value = normalize_phone_to_E164($device_number); try { $device->save(); } catch (VBX_DeviceException $e) { $error = true; $message = 'Failed to update device: ' . $e->getMessage(); } } else { // We're creating a new device record $number = array("name" => "Primary Device", "value" => normalize_phone_to_E164($device_number), "user_id" => $user->id, "sms" => 1); try { $new_device_id = $this->vbx_device->add($number); } catch (VBX_DeviceException $e) { $error = true; $message = "Failed to add device: " . $e->getMessage(); } } } else { if (strlen($device_number) == 0 && strlen($device_id_str) > 0) { // We're deleting a device try { $this->vbx_device->delete(intval($device_id_str), $user->id); } catch (VBX_DeviceException $e) { $error = true; $message = "Unable to delete device entry: " . $e->getMessage(); } } } } } if ($error) { $json = array('error' => $error, 'message' => $message); } else { $json = array('id' => $user->id, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'is_active' => $user->is_active, 'is_admin' => $user->is_admin, 'notification' => $user->notification, 'auth_type' => isset($auth_type->description) ? $auth_type->description : 'openvbx', 'email' => $user->email, 'error' => false, 'message' => '', 'online' => $user->online); } $data['json'] = $json; $this->respond('', 'accounts', $data); }
function notify_message($message, $notify = false) { error_log("Notify message: " . ($notify ? 'true' : 'false')); if ($notify === false) { return; } $ci =& get_instance(); $ci->load->model('vbx_user'); $ci->load->model('vbx_group'); $recording_host = $ci->settings->get('recording_host', VBX_PARENT_TENANT); $vm_url = $message->content_url; if (trim($recording_host) != '') { $vm_url = str_replace('api.twilio.com', trim($recording_host), $vm_url); } $message->content_url = $vm_url; $users = array(); if ($message->owner_type == 'user') { $user = VBX_User::get($message->owner_id); if (!empty($user->email)) { $notify[] = $user->email; } } $group_users = array(); if ($message->owner_type == 'group') { $user_ids = $ci->vbx_group->get_user_ids($message->owner_id); $group = $ci->vbx_group->get_by_id($message->owner_id); $owner = $group->name; } else { if ($message->owner_type == 'user') { $user_ids = array($message->owner_id); $owner = 'Personal'; } } foreach ($user_ids as $user_id) { $user = VBX_User::get($user_id); $ci->load->model('vbx_device'); $ci->load->model('vbx_sms_message'); $numbers = VBX_Device::search(array('user_id' => $user_id)); $message_type = 'Voicemail'; if ($message->type == 'sms') { $message_type = 'SMS'; $owner = ''; } if ($message->type == 'voice') { openvbx_mail($user->email, "New {$owner} {$message_type} Notification - {$message->caller}", 'message', compact('message')); error_log("message queued for {$user->email}"); } foreach ($numbers as $number) { error_log($number->value); error_log(var_export($number->values, true)); if ($number->value && $number->sms) { try { $ci->vbx_sms_message->send_message($message->called, $number->value, $this->tiny_notification_message($message)); error_log("sms queued for {$number->value}"); } catch (Sms_messageException $e) { error_log("unable to send sms alert, reason: " . $e->getMessage()); } } } } }
public static function load($data) { $devices = array(); $user = VBX_User::get($data['user_id']); // load devices (handle device_id 0 as special condition as a Client reference) if (!empty($data['device_ids'])) { foreach ($data['device_ids'] as $device_id) { if ($device_id > 0) { array_push($devices, VBX_Device::get($device_id)); } else { array_push($devices, new VBX_Device((object) array('id' => 0, 'name' => 'client', 'value' => 'client:' . $user->id, 'sms' => 0, 'sequence' => -99, 'is_active' => 1, 'user_id' => $user->id))); } } } // replace device list $user->devices = $devices; return new DialListUser(array($user)); }
protected function after_login_completed($user, $redirect) { $last_seen = $user->last_seen; // if the redirect would take us back to // the iframe the nuke it if ($redirect == site_url()) { $redirect = ''; } // Redirect to flows if this is an admin and his inbox is zero // (but not if the caller is hitting the REST api) if ($this->response_type != 'json' && empty($redirect)) { $is_admin = $this->session->userdata('is_admin'); if ($is_admin) { $this->load->model('vbx_incoming_numbers'); $twilio_numbers = array(); try { $twilio_numbers = $this->vbx_incoming_numbers->get_numbers(); if (empty($twilio_numbers)) { $banner = array('id' => 'first-login', 'html' => $this->load->view('banners/first-login', array(), true), 'title' => 'Welcome to OpenVBX'); $path = '/' . ($this->tenant->id > 1 ? $this->tenant->name : ''); setrawcookie('banner', rawurlencode(json_encode($banner)), 0, $path); set_last_known_url(site_url('/numbers')); redirect(''); } } catch (VBX_IncomingNumberException $e) { // Handle gracefully but log it log_message('error', $e->getMessage()); } } $devices = VBX_Device::search(array('user_id' => $user->id)); if (empty($devices)) { set_last_known_url(site_url('/devices')); redirect(''); } } set_last_known_url($redirect); $this->redirect(''); }