api.switchauth.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Switches auth data abstraction
  4. */
  5. class SwitchAuth {
  6. /**
  7. * Current instance switch ID
  8. *
  9. * @var int
  10. */
  11. protected $switchId = 0;
  12. /**
  13. * Auth data database abstraction layer
  14. *
  15. * @var object
  16. */
  17. protected $authDb = '';
  18. /**
  19. * Contains all devices auth data as swId=>authData
  20. *
  21. * @var array
  22. */
  23. protected $allAuthData = array();
  24. /**
  25. * System messages object placeholder
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. /**
  31. * Some other predefinded stuff
  32. */
  33. const TABLE_AUTH = 'switchauth';
  34. const URL_ME = '?module=switchauth';
  35. const URL_SWPROFILE = '?module=switches&edit=';
  36. const ROUTE_DEVID = 'switchid';
  37. const PROUTE_DEVID = 'swithcauthdeviceid';
  38. const PROUTE_LOGIN = 'switchauthlogin';
  39. const PROUTE_PASSWORD = 'switchauthpassword';
  40. const PROUTE_ENABLE = 'switchauthenablepass';
  41. public function __construct($switchId = 0) {
  42. $this->initMessages();
  43. $this->initDb();
  44. if (!empty($switchId)) {
  45. $this->setSwitchId($switchId);
  46. }
  47. $this->loadAuthData();
  48. }
  49. /**
  50. * Current instance switchId setter
  51. *
  52. * @param int/void $switchId
  53. *
  54. * @return void
  55. */
  56. protected function setSwitchId($switchId = '') {
  57. $switchId = ubRouting::filters($switchId, 'int');
  58. if (!empty($switchId)) {
  59. $this->switchId = $switchId;
  60. }
  61. }
  62. /**
  63. * Initializes the messages property with an instance of UbillingMessageHelper.
  64. *
  65. * @return void
  66. */
  67. protected function initMessages() {
  68. $this->messages = new UbillingMessageHelper();
  69. }
  70. /**
  71. * Initializes the database abstraction layer
  72. *
  73. * @return void
  74. */
  75. protected function initDb() {
  76. $this->authDb = new NyanORM(self::TABLE_AUTH);
  77. }
  78. /**
  79. * Loads available auth data into allAuthData property
  80. *
  81. * @return void
  82. */
  83. protected function loadAuthData() {
  84. if (!empty($this->switchId)) {
  85. $this->authDb->where('swid', '=', $this->switchId);
  86. }
  87. $this->allAuthData = $this->authDb->getAll('swid');
  88. }
  89. /**
  90. * Returns auth data for some specified device
  91. *
  92. * @param int $switchId
  93. *
  94. * @return array|void
  95. */
  96. public function getAuthData($switchId) {
  97. $result = array();
  98. if (isset($this->allAuthData[$switchId])) {
  99. $result = $this->allAuthData[$switchId];
  100. }
  101. return ($result);
  102. }
  103. /**
  104. * Returns auth data for all devices
  105. *
  106. * @return array
  107. */
  108. public function getAllAuthData() {
  109. $result = array();
  110. if (!empty($this->allAuthData)) {
  111. $result = $this->allAuthData;
  112. }
  113. return ($result);
  114. }
  115. /**
  116. * Returns current device auth edit form
  117. *
  118. * @return string
  119. */
  120. public function renderEditForm() {
  121. $result = '';
  122. if (!empty($this->switchId)) {
  123. $curAuthData = $this->getAuthData($this->switchId);
  124. $inputs = wf_HiddenInput(self::PROUTE_DEVID, $this->switchId);
  125. $inputs .= wf_TextInput(self::PROUTE_LOGIN, __('Login'), @$curAuthData['login'], true, 20, 'login');
  126. $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('Password'), @$curAuthData['password'], true, 20);
  127. $inputs .= wf_PasswordInput(self::PROUTE_ENABLE, __('Enable password'), @$curAuthData['enable'], true, 20);
  128. $inputs .= wf_Submit(__('Save'));
  129. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  130. }
  131. return ($result);
  132. }
  133. /**
  134. * Sets the authentication data for a device.
  135. *
  136. * @param int $switchId The ID of the switch.
  137. * @param string $login The login username.
  138. * @param string $password The login password.
  139. * @param string $enable The enable status.
  140. *
  141. * @return void
  142. */
  143. public function setAuthData($switchId, $login, $password, $enable) {
  144. $switchId = ubRouting::filters($switchId, 'int');
  145. $login = ubRouting::filters($login, 'mres');
  146. $password = ubRouting::filters($password, 'mres');
  147. $enable = ubRouting::filters($enable, 'mres');
  148. if ($switchId) {
  149. $curAuthData = $this->getAuthData($switchId);
  150. $this->authDb->data('swid', $switchId);
  151. $this->authDb->data('login', $login);
  152. $this->authDb->data('password', $password);
  153. $this->authDb->data('enable', $enable);
  154. //new record?
  155. if (empty($curAuthData)) {
  156. $this->authDb->create();
  157. log_register('SWITCHAUTH CREATED [' . $switchId . ']');
  158. } else {
  159. //updating existing record
  160. $recordId = $curAuthData['id'];
  161. $this->authDb->where('id', '=', $recordId);
  162. $this->authDb->save();
  163. log_register('SWITCHAUTH CHANGED [' . $switchId . ']');
  164. }
  165. }
  166. }
  167. /**
  168. * Flushes some device auth data record from database
  169. *
  170. * @return void
  171. */
  172. public function flushAuthData($switchId) {
  173. $switchId = ubRouting::filters($switchId, 'int');
  174. if ($switchId) {
  175. $curAuthData = $this->getAuthData($switchId);
  176. if (!empty($curAuthData)) {
  177. $recordId = $curAuthData['id'];
  178. $this->authDb->where('id', '=', $recordId);
  179. $this->authDb->delete();
  180. log_register('SWITCHAUTH FLUSH [' . $switchId . ']');
  181. }
  182. }
  183. }
  184. }