providers.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * providers class
  4. *
  5. * @method null delete
  6. * @method null toggle
  7. * @method null copy
  8. * @method null setup
  9. */
  10. if (!class_exists('providers')) {
  11. class providers {
  12. /**
  13. * declare the variables
  14. */
  15. private $app_name;
  16. private $app_uuid;
  17. private $name;
  18. private $table;
  19. private $toggle_field;
  20. private $toggle_values;
  21. private $description_field;
  22. private $location;
  23. public $id;
  24. /**
  25. * called when the object is created
  26. */
  27. public function __construct() {
  28. //assign the variables
  29. $this->app_name = 'providers';
  30. $this->app_uuid = '35187839-237e-4271-b8a1-9b9c45dc8833';
  31. $this->name = 'provider';
  32. $this->table = 'providers';
  33. $this->toggle_field = 'provider_enabled';
  34. $this->toggle_values = ['true','false'];
  35. $this->description_field = 'provider_description';
  36. $this->location = 'providers.php';
  37. }
  38. /**
  39. * called when there are no references to a particular object
  40. * unset the variables used in the class
  41. */
  42. public function __destruct() {
  43. foreach ($this as $key => $value) {
  44. unset($this->$key);
  45. }
  46. }
  47. /**
  48. * delete rows from the database
  49. */
  50. public function delete($records) {
  51. if (permission_exists($this->name.'_delete')) {
  52. //add multi-lingual support
  53. $language = new text;
  54. $text = $language->get();
  55. //validate the token
  56. $token = new token;
  57. if (!$token->validate($_SERVER['PHP_SELF'])) {
  58. message::add($text['message-invalid_token'],'negative');
  59. header('Location: '.$this->location);
  60. exit;
  61. }
  62. //delete multiple records
  63. if (is_array($records) && @sizeof($records) != 0) {
  64. //build the delete array
  65. $x = 0;
  66. foreach ($records as $record) {
  67. //add to the array
  68. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  69. $array['providers'][$x]['provider_uuid'] = $record['uuid'];
  70. $array['provider_settings'][$x]['provider_uuid'] = $record['uuid'];
  71. $array['provider_addresses'][$x]['provider_uuid'] = $record['uuid'];
  72. }
  73. //increment the id
  74. $x++;
  75. }
  76. //delete the checked rows
  77. if (is_array($array) && @sizeof($array) != 0) {
  78. //execute delete
  79. $database = new database;
  80. $database->app_name = $this->app_name;
  81. $database->app_uuid = $this->app_uuid;
  82. $database->delete($array);
  83. unset($array);
  84. //set message
  85. message::add($text['message-delete']);
  86. }
  87. unset($records);
  88. }
  89. }
  90. }
  91. /**
  92. * toggle a field between two values
  93. */
  94. public function toggle($records) {
  95. if (permission_exists($this->name.'_edit')) {
  96. //add multi-lingual support
  97. $language = new text;
  98. $text = $language->get();
  99. //validate the token
  100. $token = new token;
  101. if (!$token->validate($_SERVER['PHP_SELF'])) {
  102. message::add($text['message-invalid_token'],'negative');
  103. header('Location: '.$this->location);
  104. exit;
  105. }
  106. //toggle the checked records
  107. if (is_array($records) && @sizeof($records) != 0) {
  108. //get current toggle state
  109. foreach($records as $record) {
  110. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  111. $uuids[] = "'".$record['uuid']."'";
  112. }
  113. }
  114. if (is_array($uuids) && @sizeof($uuids) != 0) {
  115. $sql = "select ".$this->name."_uuid as uuid, ".$this->toggle_field." as toggle from v_".$this->table." ";
  116. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  117. $database = new database;
  118. $rows = $database->select($sql, $parameters, 'all');
  119. if (is_array($rows) && @sizeof($rows) != 0) {
  120. foreach ($rows as $row) {
  121. $states[$row['uuid']] = $row['toggle'];
  122. }
  123. }
  124. unset($sql, $parameters, $rows, $row);
  125. }
  126. //build update array
  127. $x = 0;
  128. foreach($states as $uuid => $state) {
  129. //create the array
  130. $array[$this->table][$x][$this->name.'_uuid'] = $uuid;
  131. $array[$this->table][$x][$this->toggle_field] = $state == $this->toggle_values[0] ? $this->toggle_values[1] : $this->toggle_values[0];
  132. //increment the id
  133. $x++;
  134. }
  135. //save the changes
  136. if (is_array($array) && @sizeof($array) != 0) {
  137. //save the array
  138. $database = new database;
  139. $database->app_name = $this->app_name;
  140. $database->app_uuid = $this->app_uuid;
  141. $database->save($array);
  142. unset($array);
  143. //set message
  144. message::add($text['message-toggle']);
  145. }
  146. unset($records, $states);
  147. }
  148. }
  149. }
  150. /**
  151. * copy rows from the database
  152. */
  153. public function copy($records) {
  154. if (permission_exists($this->name.'_add')) {
  155. //add multi-lingual support
  156. $language = new text;
  157. $text = $language->get();
  158. //validate the token
  159. $token = new token;
  160. if (!$token->validate($_SERVER['PHP_SELF'])) {
  161. message::add($text['message-invalid_token'],'negative');
  162. header('Location: '.$this->location);
  163. exit;
  164. }
  165. //copy the checked records
  166. if (is_array($records) && @sizeof($records) != 0) {
  167. //get checked records
  168. foreach($records as $record) {
  169. if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
  170. $uuids[] = "'".$record['uuid']."'";
  171. }
  172. }
  173. //create the array from existing data
  174. if (is_array($uuids) && @sizeof($uuids) != 0) {
  175. $sql = "select * from v_".$this->table." ";
  176. $sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
  177. $database = new database;
  178. $rows = $database->select($sql, $parameters, 'all');
  179. if (is_array($rows) && @sizeof($rows) != 0) {
  180. $x = 0;
  181. foreach ($rows as $row) {
  182. //copy data
  183. $array[$this->table][$x] = $row;
  184. //add copy to the description
  185. $array[$this->table][$x][$this->name.'_uuid'] = uuid();
  186. $array[$this->table][$x][$this->description_field] = trim($row[$this->description_field]).' ('.$text['label-copy'].')';
  187. //increment the id
  188. $x++;
  189. }
  190. }
  191. unset($sql, $parameters, $rows, $row);
  192. }
  193. //save the changes and set the message
  194. if (is_array($array) && @sizeof($array) != 0) {
  195. //save the array
  196. $database = new database;
  197. $database->app_name = $this->app_name;
  198. $database->app_uuid = $this->app_uuid;
  199. $database->save($array);
  200. unset($array);
  201. //set message
  202. message::add($text['message-copy']);
  203. }
  204. unset($records);
  205. }
  206. }
  207. }
  208. /**
  209. * setup the provider
  210. */
  211. public function setup() {
  212. //provider selection
  213. $provider_list = glob($_SERVER["DOCUMENT_ROOT"].PROJECT_PATH."/app/*/resources/providers/settings.php");
  214. foreach ($provider_list as $setting_path) {
  215. include($setting_path);
  216. }
  217. $providers = $array['providers'];
  218. unset($array);
  219. //get the array
  220. $x = 0;
  221. foreach ($providers as $row) {
  222. if (md5($row['provider_name']) == $this->id) {
  223. if ($row['provider_name'] == 'Add a Provider') {
  224. $provider_name = 'Provider';
  225. }
  226. else {
  227. $provider_name = $row['provider_name'];
  228. }
  229. $array['providers'][$x]['provider_uuid'] = $row['provider_uuid'];
  230. $array['providers'][$x]['provider_name'] = $provider_name;
  231. $array['providers'][$x]['provider_enabled'] = $row['provider_enabled'];
  232. $array['providers'][$x]['provider_description'] = '';
  233. $provider = $row;
  234. }
  235. }
  236. //add the provider settings
  237. $y = 0;
  238. foreach ($provider['provider_settings'] as $row) {
  239. $array['providers'][$x]['provider_settings'][$y]['provider_uuid'] = $row['provider_uuid'];
  240. $array['providers'][$x]['provider_settings'][$y]['application_uuid'] = $row['application_uuid'];
  241. $array['providers'][$x]['provider_settings'][$y]['provider_setting_uuid'] = $row['provider_setting_uuid'];
  242. $array['providers'][$x]['provider_settings'][$y]['provider_setting_category'] = $row['provider_setting_category'];
  243. $array['providers'][$x]['provider_settings'][$y]['provider_setting_subcategory'] = $row['provider_setting_subcategory'];
  244. $array['providers'][$x]['provider_settings'][$y]['provider_setting_type'] = $row['provider_setting_type'];
  245. $array['providers'][$x]['provider_settings'][$y]['provider_setting_name'] = $row['provider_setting_name'];
  246. $array['providers'][$x]['provider_settings'][$y]['provider_setting_value'] = $row['provider_setting_value'];
  247. $array['providers'][$x]['provider_settings'][$y]['provider_setting_order'] = $row['provider_setting_order'];
  248. $array['providers'][$x]['provider_settings'][$y]['provider_setting_enabled'] = $row['provider_setting_enabled'];
  249. $array['providers'][$x]['provider_settings'][$y]['provider_setting_description'] = $row['provider_setting_description'];
  250. $y++;
  251. }
  252. //add the provider addresses
  253. $y = 0;
  254. foreach ($provider['provider_addresses'] as $row) {
  255. $array['providers'][$x]['provider_addresses'][$y]['provider_uuid'] = $row['provider_uuid'];
  256. $array['providers'][$x]['provider_addresses'][$y]['provider_address_uuid'] = $row['provider_address_uuid'];
  257. $array['providers'][$x]['provider_addresses'][$y]['provider_address_cidr'] = $row['provider_address_cidr'];
  258. $array['providers'][$x]['provider_addresses'][$y]['provider_address_enabled'] = $row['provider_address_enabled'];
  259. $array['providers'][$x]['provider_addresses'][$y]['provider_address_description'] = $row['provider_address_description'];
  260. $y++;
  261. }
  262. //save to the data
  263. $database = new database;
  264. $database->app_name = 'providers';
  265. $database->app_uuid = '35187839-237e-4271-b8a1-9b9c45dc8833';
  266. $database->save($array);
  267. //$message = $database->message;
  268. //view_array($message);
  269. unset($array);
  270. }
  271. }
  272. }
  273. ?>