PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenVBX/controllers/devices.php

https://bitbucket.org/gegere/openvbx
PHP | 333 lines | 261 code | 45 blank | 27 comment | 31 complexity | 7210fe49a99a1fb11d010651a7015e1e MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * "The contents of this file are subject to the Mozilla Public License
  4. * Version 1.1 (the "License"); you may not use this file except in
  5. * compliance with the License. You may obtain a copy of the License at
  6. * http://www.mozilla.org/MPL/
  7. * Software distributed under the License is distributed on an "AS IS"
  8. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  9. * License for the specific language governing rights and limitations
  10. * under the License.
  11. * The Original Code is OpenVBX, released June 15, 2010.
  12. * The Initial Developer of the Original Code is Twilio Inc.
  13. * Portions created by Twilio Inc. are Copyright (C) 2010.
  14. * All Rights Reserved.
  15. * Contributor(s):
  16. **/
  17. require_once(APPPATH.'libraries/twilio.php');
  18. class Devices extends User_Controller {
  19. protected $response;
  20. protected $request;
  21. private $data = array();
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->load->model('vbx_device');
  26. $this->template->write('title', 'Devices');
  27. $this->section = 'devices';
  28. }
  29. public function index()
  30. {
  31. $this->template->add_js('assets/j/account.js');
  32. $this->template->add_js('assets/j/devices.js');
  33. $data = $this->init_view_data();
  34. $user = VBX_user::get(array('id' => $this->user_id));
  35. $data['user'] = $user;
  36. $data['numbers'] = $data['devices'] = $user->devices;
  37. if(empty($data['devices']))
  38. {
  39. set_banner('devices',
  40. 'Want someone to be able to reach you on your work or cell phone?',
  41. 'Add a device below.'
  42. );
  43. }
  44. return $this->respond('', 'devices', $data);
  45. }
  46. public function edit()
  47. {
  48. $is_admin = $this->session->userdata('is_admin');
  49. $user = new VBX_User();
  50. $params = array();
  51. foreach($user->fields as $field) {
  52. $val = $this->input->post($field);
  53. /* Disallow people from changing certain settings */
  54. if(in_array($field, $user->admin_fields))
  55. {
  56. if($val && $is_admin) $params[$field] = $val;
  57. }
  58. else
  59. {
  60. if($val) $params[$field] = $val;
  61. }
  62. // The value for some fields should also be saved to the session
  63. if ($field === 'email')
  64. {
  65. $this->session->set_userdata('email', trim($val));
  66. }
  67. }
  68. if($user->update($this->user_id, $params)) {
  69. $this->session->set_flashdata('message_edit', 'User data changed');
  70. redirect('account');
  71. } else {
  72. $this->data['error_edit'] = '';
  73. $this->index();
  74. }
  75. }
  76. public function number($key = 0)
  77. {
  78. switch($key)
  79. {
  80. case 'order':
  81. return $this->order_handler();
  82. default:
  83. return $this->number_handler($key);
  84. }
  85. }
  86. public function send_iphone_guide() {
  87. $user = VBX_user::get(array('id' => $this->user_id));
  88. $this->data = array('error' => false, 'message' => 'OK');
  89. openvbx_mail($user->email,
  90. "iPhone installation Guide",
  91. 'iphone-guide',
  92. array('email' => $user->email));
  93. echo json_encode($this->data);
  94. }
  95. private function number_handler($id)
  96. {
  97. switch($this->request_method) {
  98. case 'POST':
  99. if(!empty($id) && intval($id) > 0)
  100. {
  101. return $this->update_number($id);
  102. }
  103. return $this->add_number();
  104. case 'DELETE':
  105. return $this->delete_number($id);
  106. }
  107. }
  108. private function order_handler()
  109. {
  110. switch($this->request_method) {
  111. case 'POST':
  112. return $this->update_order();
  113. }
  114. }
  115. private function update_number($device_id)
  116. {
  117. $data['json'] = array('error' => false,
  118. 'message' => '');
  119. $number = $this->input->post('device');
  120. $device = VBX_Device::get($device_id);
  121. if(isset($number['value']))
  122. {
  123. $device->value = normalize_phone_to_E164($number['value']);
  124. }
  125. if(isset($number['sms']))
  126. {
  127. $device->sms = intval($number['sms']) == 0? 0 : 1;
  128. }
  129. if(isset($number['is_active']))
  130. {
  131. $device->is_active = intval($number['is_active']) == 0? 0 : 1;
  132. }
  133. try
  134. {
  135. $device->save();
  136. }
  137. catch(VBX_DeviceException $e)
  138. {
  139. error_log($e->getMessage());
  140. $device['json']['error'] = true;
  141. $device['json']['message'] = 'Unable to update device settings';
  142. }
  143. if($this->response_type == 'html')
  144. {
  145. redirect('account#devices');
  146. }
  147. $this->respond('', 'account/number', $data);
  148. }
  149. private function update_order()
  150. {
  151. $data['json'] = array('error' => false, 'message' => '');
  152. $order = $this->input->post('order');
  153. try
  154. {
  155. foreach($order as $sequence => $device_id)
  156. {
  157. $params = array('sequence' => $sequence);
  158. $device = VBX_Device::get(array('id' => $device_id,
  159. 'user_id' => $this->user_id));
  160. if(!$device) {
  161. error_log('Device no longer exists: '.$device_id);
  162. continue;
  163. }
  164. $device->sequence = $sequence;
  165. $device->save();
  166. }
  167. }
  168. catch(VBX_DeviceException $e)
  169. {
  170. $data['json']['error'] = true;
  171. $data['json']['message'] = 'One or more device sequences were not updated';
  172. }
  173. if($this->response_type == 'html')
  174. {
  175. return redirect('account/number');
  176. }
  177. $this->respond('', 'account/number', $data);
  178. }
  179. private function add_number()
  180. {
  181. $number = array();
  182. $number = $this->input->post('number');
  183. $number['value'] = normalize_phone_to_E164($number['value']);
  184. $number['user_id'] = $this->user_id;
  185. // sms is always enabled by default
  186. $number['sms'] = 1;
  187. try
  188. {
  189. if(empty($number['value']) ||
  190. empty($number['name']))
  191. {
  192. $message = 'All fields required';
  193. throw new VBX_DeviceException($message);
  194. }
  195. $number_id = $this->vbx_device->add($number);
  196. $response = array('error' => false,
  197. 'message' => '',
  198. 'id' => $number_id,
  199. 'name' => htmlspecialchars($number['name']),
  200. 'value' => format_phone($number['value']),
  201. 'sms' => $number['sms'],
  202. );
  203. }
  204. catch(VBX_DeviceException $e)
  205. {
  206. $response = array('error' => true,
  207. 'message' => $e->getMessage(),
  208. );
  209. }
  210. $data['json'] = $response;
  211. if($this->response_type == 'html')
  212. {
  213. redirect('account');
  214. }
  215. return $this->respond('', 'account', $data);
  216. }
  217. private function delete_number($id)
  218. {
  219. $number = $this->vbx_device->get($id);
  220. $response = array('error' => false,
  221. 'message' => '',);
  222. if($number && $number->user_id == $this->user_id)
  223. {
  224. try
  225. {
  226. $number->delete();
  227. }
  228. catch(VBX_DeviceException $e)
  229. {
  230. error_log($e->getMessage());
  231. $response = array('error' => true,
  232. 'message' => 'Unable to delete. Please contact support.',);
  233. }
  234. }
  235. else
  236. {
  237. $response = array('error' => true,
  238. 'message' => 'Permission Denied');
  239. }
  240. if($this->response_type == 'html')
  241. {
  242. echo 'test';exit;
  243. }
  244. $data['json'] = $response;
  245. return $this->respond('', 'account', $data);
  246. }
  247. /**
  248. * Refresh the user's devices list in the dialer
  249. *
  250. * @return json
  251. */
  252. public function refresh_dialer() {
  253. $user = VBX_User::get(array('id' => $this->session->userdata('user_id')));
  254. $browserphone = array(
  255. 'call_using_options' => array()
  256. );
  257. if (count($user->devices))
  258. {
  259. foreach ($user->devices as $device)
  260. {
  261. if (strpos($device->value, 'client:') !== false)
  262. {
  263. continue;
  264. }
  265. $browserphone['call_using_options']['device:'.$device->id] = array(
  266. 'title' => 'Device: '.$device->name,
  267. 'data' => (object) array(
  268. 'number' => format_phone($device->value),
  269. 'name' => $device->name
  270. )
  271. );
  272. }
  273. }
  274. $data = array(
  275. 'browserphone' => $browserphone
  276. );
  277. $html = $this->load->view('dialer/devices', $data, true);
  278. $response['json'] = array(
  279. 'error' => false,
  280. 'html' => $html
  281. );
  282. $this->respond('', 'dialer/devices', $response);
  283. }
  284. }