📜 ⬆️ ⬇️

Synchronization of LanBilling and RTU class 4 & 5 account status

Good afternoon, dear habro users.

There was a task to synchronize the status of the account in LanBilling and RTU.
I want to share with you an implementation option.

Used


Lanbilling v.1.8
RTU class 4 & 5
php 5.3
mysql 5.0.51a-24 + lenny5
cron

Implementation


LanBilling account status data is stored in the vgroups table.
')
Table fields:

blocked - lock status (0 - unlocked,> 0 - blocked)
blk_req - status of the request to block (! = 0 and! = blocked - block ,! = 0 and = blocked - unlock)

In cron tasks we add the execution of a php script with a specified interval, which will receive the status of the field
blk_req from LanBilling database, send a soap command to one of the standard RTU scripts (set.aspx or get.aspx) and, if true, change the values ​​of the fields blocked and blk_req

Function to send a soap request

function xmlHttpsReq($xml,$type){ $ch = curl_init('https://IP_:/mobile_request/'.$type.'.aspx?admin'); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $data=curl_exec($ch); $result=substr($data,0,strpos($data,'</Root>')+7); if( curl_errno($ch) != 0 ) { die('CURL_error: ' . curl_errno($ch) . ', ' . curl_error($ch)); }; curl_close($ch); return $result; } 

Function to get data about all users

 function getUserList() { $data=xmlHttpsReq('<commands><command name="Get" table="User" /></commands>','get'); $xml = new SimpleXMLElement($data); foreach ($xml->command->user as $user) { $usr[]=$user; } return $usr; } 

Function to get user data by login

 function getUser($ID) { $data=xmlHttpsReq('<commands><command name="Get" table="User"><item><id>'.$ID.'</id></item></command></commands>','get'); $xml = new SimpleXMLElement($data); return $xml->command->user; } 

Function to set account parameters

 function setUser($arr) { $converter = new Array2XML(); $obj = $converter->convert($arr); $data=xmlHttpsReq('<commands><command name="Edit" table="User">'.$obj.'</command></commands>','set'); $xml = new SimpleXMLElement($data); return $xml->command->item->result; } 

Class for converting an array to xml

 class Array2XML { private $writer; private $rootName = 'item'; function __construct() { $this->writer = new XMLWriter(); } public function convert($data) { $this->writer->openMemory(); $this->writer->startElement($this->rootName); if (is_array($data)) { $this->getXML($data); } $this->writer->endElement(); return $this->writer->outputMemory(); } public function setVersion($version) { $this->version = $version; } public function setEncoding($encoding) { $this->encoding = $encoding; } public function setRootName($rootName) { $this->rootName = $rootName; } private function getXML($data) { foreach ($data as $key => $val) { if (is_numeric($key)) { $key = 'key'.$key; } if (is_array($val)) { $this->writer->startElement($key); $this->getXML($val); $this->writer->endElement(); } else { $this->writer->writeElement($key, $val); } } } } 


To change account settings, we need to know the login and GUID.
In the current implementation of the API, the RTU does not allow you to get the GUID of an account by login (the getUser function), so the getUserList function is used to get the GUID

Implementation

 mysql_connect(HOST,USERNAME,PASSWORD); mysql_select_db(DBNAME); //      RTU $allusers = getUserList(); //    LanBilling   $res = mysql_query("SELECT CASE WHEN blk_req != 0 AND blk_req = blocked THEN 'true' WHEN blk_req != 0 AND blocked = 0 THEN 'false' ELSE 'none' END AS blk, login FROM vgroups WHERE blk_req != 0"); while ($row=mysql_fetch_array($res)) { unset($user); foreach($allusers as $allusr) { if($allusr->id==$row['login']) { $user=$allusr; break; } } if (($user)&&($row['blk']!='none')) { if(setUser(array('id'=>$user->id,'guid'=>$user->guid,'enabled'=>$row['blk']))=='true') $ids[] = $row['vg_id']; //     RTU        LanBilling } else $ids[] = $row['vg_id']; } //   if (count(@$ids)>0) { $sql = 'UPDATE vgroups SET blocked = IF(blk_req = blocked, 0, blk_req), blk_req = 0, blk_req_user = "" WHERE vg_id IN (' . implode(',', $ids) . ')'; mysql_query($sql); } 


Comments are welcome.

Source: https://habr.com/ru/post/126412/


All Articles