123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- /**
- * Switches auth data abstraction
- */
- class SwitchAuth {
- /**
- * Current instance switch ID
- *
- * @var int
- */
- protected $switchId = 0;
- /**
- * Auth data database abstraction layer
- *
- * @var object
- */
- protected $authDb = '';
- /**
- * Contains all devices auth data as swId=>authData
- *
- * @var array
- */
- protected $allAuthData = array();
- /**
- * System messages object placeholder
- *
- * @var object
- */
- protected $messages = '';
- /**
- * Some other predefinded stuff
- */
- const TABLE_AUTH = 'switchauth';
- const URL_ME = '?module=switchauth';
- const URL_SWPROFILE = '?module=switches&edit=';
- const ROUTE_DEVID = 'switchid';
- const PROUTE_DEVID = 'swithcauthdeviceid';
- const PROUTE_LOGIN = 'switchauthlogin';
- const PROUTE_PASSWORD = 'switchauthpassword';
- const PROUTE_ENABLE = 'switchauthenablepass';
- public function __construct($switchId = 0) {
- $this->initMessages();
- $this->initDb();
- if (!empty($switchId)) {
- $this->setSwitchId($switchId);
- }
- $this->loadAuthData();
- }
- /**
- * Current instance switchId setter
- *
- * @param int/void $switchId
- *
- * @return void
- */
- protected function setSwitchId($switchId = '') {
- $switchId = ubRouting::filters($switchId, 'int');
- if (!empty($switchId)) {
- $this->switchId = $switchId;
- }
- }
- /**
- * Initializes the messages property with an instance of UbillingMessageHelper.
- *
- * @return void
- */
- protected function initMessages() {
- $this->messages = new UbillingMessageHelper();
- }
- /**
- * Initializes the database abstraction layer
- *
- * @return void
- */
- protected function initDb() {
- $this->authDb = new NyanORM(self::TABLE_AUTH);
- }
- /**
- * Loads available auth data into allAuthData property
- *
- * @return void
- */
- protected function loadAuthData() {
- if (!empty($this->switchId)) {
- $this->authDb->where('swid', '=', $this->switchId);
- }
- $this->allAuthData = $this->authDb->getAll('swid');
- }
- /**
- * Returns auth data for some specified device
- *
- * @param int $switchId
- *
- * @return array|void
- */
- public function getAuthData($switchId) {
- $result = array();
- if (isset($this->allAuthData[$switchId])) {
- $result = $this->allAuthData[$switchId];
- }
- return ($result);
- }
- /**
- * Returns auth data for all devices
- *
- * @return array
- */
- public function getAllAuthData() {
- $result = array();
- if (!empty($this->allAuthData)) {
- $result = $this->allAuthData;
- }
- return ($result);
- }
- /**
- * Returns current device auth edit form
- *
- * @return string
- */
- public function renderEditForm() {
- $result = '';
- if (!empty($this->switchId)) {
- $curAuthData = $this->getAuthData($this->switchId);
- $inputs = wf_HiddenInput(self::PROUTE_DEVID, $this->switchId);
- $inputs .= wf_TextInput(self::PROUTE_LOGIN, __('Login'), @$curAuthData['login'], true, 20, 'login');
- $inputs .= wf_PasswordInput(self::PROUTE_PASSWORD, __('Password'), @$curAuthData['password'], true, 20);
- $inputs .= wf_PasswordInput(self::PROUTE_ENABLE, __('Enable password'), @$curAuthData['enable'], true, 20);
- $inputs .= wf_Submit(__('Save'));
- $result .= wf_Form('', 'POST', $inputs, 'glamour');
- }
- return ($result);
- }
- /**
- * Sets the authentication data for a device.
- *
- * @param int $switchId The ID of the switch.
- * @param string $login The login username.
- * @param string $password The login password.
- * @param string $enable The enable status.
- *
- * @return void
- */
- public function setAuthData($switchId, $login, $password, $enable) {
- $switchId = ubRouting::filters($switchId, 'int');
- $login = ubRouting::filters($login, 'mres');
- $password = ubRouting::filters($password, 'mres');
- $enable = ubRouting::filters($enable, 'mres');
- if ($switchId) {
- $curAuthData = $this->getAuthData($switchId);
- $this->authDb->data('swid', $switchId);
- $this->authDb->data('login', $login);
- $this->authDb->data('password', $password);
- $this->authDb->data('enable', $enable);
- //new record?
- if (empty($curAuthData)) {
- $this->authDb->create();
- log_register('SWITCHAUTH CREATED [' . $switchId . ']');
- } else {
- //updating existing record
- $recordId = $curAuthData['id'];
- $this->authDb->where('id', '=', $recordId);
- $this->authDb->save();
- log_register('SWITCHAUTH CHANGED [' . $switchId . ']');
- }
- }
- }
- /**
- * Flushes some device auth data record from database
- *
- * @return void
- */
- public function flushAuthData($switchId) {
- $switchId = ubRouting::filters($switchId, 'int');
- if ($switchId) {
- $curAuthData = $this->getAuthData($switchId);
- if (!empty($curAuthData)) {
- $recordId = $curAuthData['id'];
- $this->authDb->where('id', '=', $recordId);
- $this->authDb->delete();
- log_register('SWITCHAUTH FLUSH [' . $switchId . ']');
- }
- }
- }
- }
|