/** * Update each tenant to sub-account status * Pull full list of sub-accounts to make sure that accounts being upgraded are sub-accounts * Any account found to not be a sub-account of tenant 1 is assumed to be a full account * * @return void */ function runUpdate_55_update_tenant_type_status() { $ci =& get_instance(); $parent_account_sid = $ci->vbx_settings->get('twilio_sid', 1); $parent_account_token = $ci->vbx_settings->get('twilio_token', 1); $parent_account = OpenVBX::getAccount($parent_account_sid, $parent_account_token); $subaccount_sids = array(); foreach ($parent_account->accounts as $account) { array_push($subaccount_sids, $account->sid); } $tenants = $ci->db->from('tenants')->where('id >', '1')->get()->result(); if (!empty($tenants)) { foreach ($tenants as $tenant) { $tenant_sid = $ci->vbx_settings->get('twilio_sid', $tenant->id); $tenant_token = $ci->vbx_settings->get('twilio_token', $tenant->id); if (in_array($tenant_sid, $subaccount_sids)) { // tenant is a sub-account of the parent $type = 2; } else { // tenant is a regular account, not a sub-account // may still be a sub of someone else, but not of this parent $type = 1; } $ci->db->set('type', $type)->where('id', $tenant->id)->update('tenants'); } } }
function create_application($name, $tenant_id) { $ci =& get_instance(); $ci->load->model('vbx_settings'); $app_name = "OpenVBX :: {$name}"; $twilio_sid = $ci->vbx_settings->get('twilio_sid', $tenant_id); $twilio_token = $ci->vbx_settings->get('twilio_token', $tenant_id); // Rare event, sid and/or token may be empty if (!empty($twilio_sid) && !empty($twilio_token)) { error_log('Processing tenant: ' . $tenant_id); $account = OpenVBX::getAccount($twilio_sid, $twilio_token); $applications = $account->applications->getIterator(0, 1, array('FriendlyName' => $app_name)); $application = false; foreach ($applications as $_application) { if ($_application->friendly_name == $app_name) { $application = $_application; } } $params = array('FriendlyName' => $app_name, 'VoiceUrl' => tenant_url('twiml/dial', $tenant_id), 'VoiceFallbackUrl' => asset_url('fallback/voice.php'), 'VoiceMethod' => 'POST', 'SmsUrl' => '', 'SmsFallbackUrl' => '', 'SmsMethod' => 'POST'); if (!empty($application)) { error_log('Modifying app: ' . $app_name); $application->update($params); } else { error_log('Creating app: ' . $app_name); $application = $account->applications->create($app_name, $params); } error_log('Created/Updated app for tenant id: ' . $tenant_id . ' - Application Sid: ' . $application->sid); $ci->vbx_settings->add('application_sid', $application->sid, $tenant_id); } else { error_log('Skipped app creation for tenant "' . $tenant_id . '" - incomplete account Sid/Token pair.'); } }
function send_message($from, $to, $message) { $from = PhoneNumber::normalizePhoneNumberToE164($from); $to = PhoneNumber::normalizePhoneNumberToE164($to); try { $account = OpenVBX::getAccount(); $response = $account->messages->sendMessage($from, $to, $message); } catch (Exception $e) { throw new VBX_Sms_messageException($e->getMessage()); } if (!in_array($response->status, array('sent', 'queued'))) { throw new VBX_Sms_messageException('SMS delivery failed. An unknown error occurred' . ' during delivery.'); } }
function getAccountType() { $ci =& get_instance(); if ($cache = $ci->api_cache->get('account-type', __CLASS__, $ci->tenant->id)) { return $cache; } try { $account = OpenVBX::getAccount(); $account_type = $account->type; } catch (Exception $e) { throw new VBX_AccountsException($e->getMessage()); } $ci->api_cache->set('account-type', $account_type, __CLASS__, $ci->tenant->id); return $account_type; }
function getAccountType() { if (function_exists('apc_fetch')) { $success = FALSE; $type = apc_fetch($this->cache_key, $success); if ($type and $success) { return $type; } } try { $account = OpenVBX::getAccount(); $account_type = $account->type; } catch (Exception $e) { throw new VBX_AccountsException($e->getMessage()); } if (function_exists('apc_store')) { $success = apc_store($this->cache_key, $account_type, self::CACHE_TIME_SEC); } return $account_type; }
public function finish() { $error = false; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $app_name = 'OpenVBX :: ' . $this->tenant->url_prefix; try { $account = OpenVBX::getAccount(); /** @var Services_Twilio_Rest_Application[] $applications */ $applications = $account->applications->getIterator(0, 10, array('FriendlyName' => $app_name)); $application = false; foreach ($applications as $_application) { if ($_application->friendly_name == $app_name) { $application = $_application; } } $params = array('FriendlyName' => $app_name, 'VoiceUrl' => site_url('twiml/dial'), 'VoiceFallbackUrl' => asset_url('fallback/voice.php'), 'VoiceMethod' => 'POST', 'SmsUrl' => '', 'SmsFallbackUrl' => '', 'SmsMethod' => 'POST'); if ($application) { $application->update($params); } else { $application = $account->applications->create($app_name, $params); } $this->vbx_settings->add('application_sid', $application->sid, $this->tenant->id); $this->vbx_settings->delete('tenant_first_run', $this->tenant->id); } catch (Exception $e) { switch ($e->getCode()) { case '20003': $error = 'Authentication Failed. Invalid Twilio SID or Token (' . $e->getCode() . ')'; break; default: $error = $e->getMessage() . ' (' . $e->getCode() . ')'; } } } if ($error) { $data['json'] = array('error' => true, 'message' => $error); } else { $data['json'] = array('error' => false, 'message' => null); } $this->respond('', 'welcome', $data); }
function runUpdate_45() { set_time_limit(3600); $ci =& get_instance(); $tenants = $ci->db->from('tenants')->get()->result(); if (count($tenants)) { $ci->load->model('vbx_incoming_numbers'); $numbers = $ci->vbx_incoming_numbers->get_numbers($retrieve_sandbox = true); foreach ($tenants as $tenant) { error_log("Updating to 2010: " . var_export($tenant, true)); $twilio_sid = $ci->settings->get('twilio_sid', $tenant->id); $twilio_token = $ci->settings->get('twilio_token', $tenant->id); if (!empty($twilio_sid) && !empty($twilio_token)) { $account = OpenVBX::getAccount($twilio_sid, $twilio_token); foreach ($account->incoming_phone_numbers as $number) { $number->update(array('ApiVersion' => '2010-04-01')); } } else { error_log('Skipped number updates for tenant "' . $tenant->id . '" - incomplete account Sid/Token pair.'); } } } $ci->settings->set('schema-version', '45', 1); }
/** * Remove a phone number from the current account * * @param string $phone_id * @return bool */ public function delete_number($phone_id) { try { $account = OpenVBX::getAccount(); $account->incoming_phone_numbers->delete($phone_id); } catch (Exception $e) { throw new VBX_IncomingNumberException($e->getMessage()); } $this->clear_cache(); return TRUE; }
if ($ci->db->affected_rows()) { $ci->db->delete('subscribers', array('list' => $remove)); } } die; } if (!empty($_POST['type'])) { $list = intval($_POST['list']); if ($ci->db->query(sprintf('SELECT id FROM subscribers_lists WHERE id = %d AND tenant = %d', $list, $tenant_id))->num_rows()) { $subscribers = $ci->db->query(sprintf('SELECT value FROM subscribers WHERE list = %d', $list))->result(); } else { $subscribers = array(); } $type = $_POST['type']; $callerId = normalize_phone_to_E164($_POST['callerId']); $account = OpenVBX::getAccount(); if ('sms' == $type && !empty($_POST['message'])) { if (count($subscribers)) { foreach ($subscribers as $subscriber) { $account->sms_messages->create($callerId, $subscriber->value, $_POST['message']); } } } elseif ('call' == $type) { $flow = OpenVBX::getFlows(array('id' => $_POST['flow'], 'tenant_id' => $tenant_id)); if ($flow && count($subscribers) && $flow[0]->values['data']) { foreach ($subscribers as $subscriber) { $account->calls->create($callerId, $subscriber->value, site_url('twiml/start/voice/' . $flow[0]->values['id'])); } } } }
function cancel_recording() { $json = array('error' => false, 'message' => ''); $audio_file_id = $this->input->post('id'); if (strlen($audio_file_id) == 0) { $json['error'] = true; $json['message'] = "Missing 'id' parameter."; } else { $audioFile = VBX_Audio_File::get($audio_file_id); if (is_null($audioFile)) { trigger_error("We were given an id for an audio_file, but we can't find the record. That's odd. And, by odd I really mean it should *never* happen."); } else { if ($audioFile->user_id != $this->session->userdata('user_id')) { trigger_error("You can't cancel a recording you didn't start."); } else { log_message('debug', 'canceling call'); try { $account = OpenVBX::getAccount(); $call = $account->calls->get($audioFile->recording_call_sid); if ($call->status == 'ringing') { $params = array('Status' => 'canceled'); } else { $params = array('Status' => 'completed'); } $call->update($params); $audioFile->cancelled = true; $audioFile->save(); } catch (Exception $e) { trigger_error($e->getMessage()); $json['error'] = true; $json['message'] = $e->getMessage(); } } } } $data = array(); $data['json'] = $json; $this->response_type = 'json'; $this->respond('', null, $data); }
private function update_connect_app($connect_app_sid) { if (!empty($connect_app_sid) && $this->tenant->id == VBX_PARENT_TENANT) { $account = OpenVBX::getAccount(); /** @var Services_Twilio_Rest_ConnectApp $connect_app */ $connect_app = $account->connect_apps->get($connect_app_sid); $required_settings = array('HomepageUrl' => site_url(), 'AuthorizeRedirectUrl' => site_url('/auth/connect'), 'DeauthorizeCallbackUrl' => site_url('/auth/connect/deauthorize'), 'Permissions' => array('get-all', 'post-all')); $updated = false; foreach ($required_settings as $key => $setting) { $app_key = Services_Twilio::decamelize($key); if ($connect_app->{$app_key} != $setting) { $connect_app->{$app_key} = $setting; $updated = true; } } if ($updated) { $connect_app->update(array('FriendlyName' => $connect_app->friendly_name, 'Description' => $connect_app->description, 'CompanyName' => $connect_app->company_name, 'HomepageUrl' => $required_settings['HomepageUrl'], 'AuthorizeRedirectUrl' => $required_settings['AuthorizeRedirectUrl'], 'DeauthorizeCallbackUrl' => $required_settings['DeauthorizeCallbackUrl'], 'Permissions' => implode(',', $required_settings['Permissions']))); } } }
/** * Verify the Account Sid & Token * Request a list of accounts with the credentials. Exceptions will * give us our error conditions. Only known right now is 20003 (auth denied) * * @return array */ function validate_step3() { $this->load->model('vbx_settings'); $json = array('success' => true, 'step' => 3, 'message' => 'success'); $twilio_sid = $this->openvbx_settings['twilio_sid']; $twilio_token = $this->openvbx_settings['twilio_token']; $connect_app = $this->openvbx_settings['connect_application_sid']; try { // call for most basic of information to see if we have access $account = OpenVBX::getAccount($twilio_sid, $twilio_token); /** * We'll get an account back with empty members, even if we supplied * bunk credentials, we need to verify that something is there to be * confident of success. */ $status = $account->type; if (empty($status)) { throw new InstallException('Unable to access Twilio Account'); } // check the connect app if a sid is provided if (!empty($connect_app)) { try { $connect_application = $account->connect_apps->get($connect_app); $friendly_name = $connect_application->friendly_name; } catch (Exception $e) { switch ($e->getCode()) { case 0: // return a better message than "resource not found" throw new InstallException('The Connect Application SID “' . $connect_app . '” was not found.', 0); break; default: throw new InstallException($e->getMessage(), $e->getCode()); } } } } catch (Exception $e) { $json['success'] = false; switch ($e->getCode()) { case '20003': $json['message'] = 'Authentication Failed. Invalid Twilio SID or Token.'; break; default: $json['message'] = $e->getMessage(); } $json['message'] .= ' (' . $e->getCode() . ')'; } return $json; }
public function make_call_path($to, $callerid, $path, $rest_access) { $recording_url = site_url("twiml/redirect/{$path}/{$rest_access}"); try { $account = OpenVBX::getAccount(); $account->calls->create($callerid, $to, $recording_url); } catch (Exception $e) { throw new VBX_CallException($e->getMessage()); } }
public function __construct($limit = 20) { $this->limit = $limit; $this->account = OpenVBX::getAccount(); }