/OpenVBX/models/vbx_settings.php

https://github.com/netconstructor/OpenVBX · PHP · 289 lines · 212 code · 58 blank · 19 comment · 29 complexity · 9410c9d742e05c5052b62b9f37fb6b63 MD5 · raw file

  1. <?php
  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. class VBX_SettingsException extends Exception {}
  18. class VBX_Settings extends Model
  19. {
  20. protected $settings_table = 'settings';
  21. protected $tenants_table = 'tenants';
  22. public $setting_options = array('twilio_sid',
  23. 'twilio_token',
  24. 'twilio_endpoint',
  25. 'from_email',
  26. 'recording_host',
  27. 'theme');
  28. protected $settings_params = array('name',
  29. 'value',
  30. 'tenant_id');
  31. protected $tenants_params = array('active',
  32. 'name',
  33. 'url_prefix');
  34. private $cache_key;
  35. const CACHE_TIME_SEC = 1;
  36. function __construct()
  37. {
  38. parent::__construct();
  39. $this->cache_key = 'settings';
  40. }
  41. function get_all_tenants()
  42. {
  43. $ci =& get_instance();
  44. $tenants = $ci->db
  45. ->from($this->tenants_table)
  46. ->where('name !=', 'default')
  47. ->get()->result();
  48. return $tenants;
  49. }
  50. function get_tenant($url_prefix)
  51. {
  52. $ci =& get_instance();
  53. $tenant = $ci->db
  54. ->from($this->tenants_table)
  55. ->where('url_prefix', strtolower($url_prefix))
  56. ->get()->result();
  57. if(!empty($tenant[0]))
  58. return $tenant[0];
  59. return false;
  60. }
  61. function get_tenant_by_name($name)
  62. {
  63. $ci =& get_instance();
  64. $tenant = $ci->db
  65. ->from('tenants as i')
  66. ->where('i.name', $name)
  67. ->get()->result();
  68. if(!empty($tenant[0]))
  69. return $tenant[0];
  70. return false;
  71. }
  72. function get_tenant_by_id($id)
  73. {
  74. $ci =& get_instance();
  75. $tenant = $ci->db
  76. ->from($this->tenants_table)
  77. ->where('id', $id)
  78. ->get()->result();
  79. if(!empty($tenant[0]))
  80. return $tenant[0];
  81. return false;
  82. }
  83. function tenant($name, $url_prefix, $local_prefix)
  84. {
  85. $ci =& get_instance();
  86. $tenant = $this->get_tenant($url_prefix);
  87. $errors = array();
  88. if(strlen($url_prefix) > 32)
  89. {
  90. $errors[] = "Tenant name exceeds 32 character limit";
  91. }
  92. if(preg_match('/[^0-9A-Za-z_-]/', $name) > 0)
  93. {
  94. $errors[] = "Tenant name contains invalid characters. Allowed characters: alphanumeric, dashes, and underscores.";
  95. }
  96. if(!empty($errors))
  97. {
  98. throw new VBX_SettingsException(implode(',', $errors));
  99. }
  100. if($tenant === false)
  101. {
  102. $ci->db
  103. ->set('name', $name)
  104. ->set('url_prefix', $url_prefix)
  105. ->set('local_prefix', $local_prefix)
  106. ->insert($this->tenants_table);
  107. $tenant_id = $ci->db->insert_id();
  108. if(!$tenant_id)
  109. {
  110. throw new VBX_SettingsException('Tenant failed to create');
  111. }
  112. return $tenant_id;
  113. }
  114. throw new VBX_SettingsException('Tenant by this name or url already exists');
  115. }
  116. function update_tenant($tenant)
  117. {
  118. $ci =& get_instance();
  119. $errors = array();
  120. if(!(!empty($tenant)
  121. && isset($tenant['id'])
  122. && intval($tenant['id']) > 0
  123. && $this->get_tenant_by_id($tenant['id']) !== false))
  124. {
  125. throw new VBX_SettingsException('Can not update tenant, malformed update request');
  126. }
  127. if(isset($tenant['url_prefix'])
  128. && strlen($tenant['url_prefix']) > 32)
  129. {
  130. $errors[] = "Tenant name exceeds 32 character limit";
  131. }
  132. if(isset($tenant['name'])
  133. && preg_match('/[^0-9A-Za-z_-]/', $name) > 0)
  134. {
  135. $errors[] = "Tenant name contains invalid characters. Allowed characters: alphanumeric, dashes, and underscores.";
  136. }
  137. foreach($this->tenants_params as $param)
  138. {
  139. if(isset($tenant[$param]))
  140. {
  141. $ci->db
  142. ->set($param, $tenant[$param]);
  143. }
  144. }
  145. return $ci->db
  146. ->where('id', $tenant['id'])
  147. ->update($this->tenants_table);
  148. }
  149. function add($name, $value, $tenant_id)
  150. {
  151. $ci =& get_instance();
  152. if($this->get_tenant_by_id($tenant_id) === false)
  153. {
  154. return false;
  155. }
  156. if($this->get($name, $tenant_id) !== false) {
  157. $ci->db
  158. ->set('value', $value)
  159. ->where('name', $name)
  160. ->where('tenant_id', $tenant_id)
  161. ->update($this->settings_table);
  162. } else {
  163. $ci->db
  164. ->set('name', $name)
  165. ->set('value', $value)
  166. ->set('tenant_id', $tenant_id)
  167. ->insert($this->settings_table);
  168. }
  169. if(function_exists('apc_delete')) {
  170. apc_delete($this->cache_key.$tenant_id.$name);
  171. }
  172. return $ci->db
  173. ->insert_id();
  174. }
  175. function set($name, $value, $tenant_id)
  176. {
  177. $ci =& get_instance();
  178. if($this->get($name, $tenant_id) === false)
  179. {
  180. return false;
  181. }
  182. $ci->db
  183. ->set('value', $value)
  184. ->where('name', $name)
  185. ->where('tenant_id', $tenant_id)
  186. ->update($this->settings_table);
  187. if(function_exists('apc_delete')) {
  188. return apc_delete($this->cache_key.$tenant_id.$name);
  189. }
  190. return ($ci->db
  191. ->affected_rows() > 0? true : false);
  192. }
  193. function get($name, $tenant_id)
  194. {
  195. $ci =& get_instance();
  196. if(function_exists('apc_fetch')) {
  197. $success = false;
  198. if(($data = apc_fetch($this->cache_key.$tenant_id.$name, $success))
  199. && $success) {
  200. $result = @unserialize($data);
  201. if(!empty($result[0]))
  202. return $result[0]->value;
  203. }
  204. }
  205. $result = $ci->db
  206. ->select('value')
  207. ->from($this->settings_table . ' as s')
  208. ->where('s.name', $name)
  209. ->where('s.tenant_id', $tenant_id)
  210. ->get()->result();
  211. if(function_exists('apc_store')) {
  212. $success = apc_store($this->cache_key.$tenant_id.$name, serialize($result), self::CACHE_TIME_SEC);
  213. }
  214. if(!empty($result[0]))
  215. return $result[0]->value;
  216. return false;
  217. }
  218. function get_all_by_tenant_id($tenant_id)
  219. {
  220. $ci =& get_instance();
  221. $result = $ci->db
  222. ->from($this->settings_table)
  223. ->where('tenant_id', $tenant_id)
  224. ->get()->result();
  225. return $result;
  226. }
  227. }