mysql.drv 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php if ( !defined('ENVIRONMENT') ) exit('Only STG can run script!' . "\n");
  2. class Database {
  3. // Database's vars:
  4. private $connected;
  5. private $identifier;
  6. private $cache = array();
  7. // Query counter:
  8. public $qc = 0;
  9. // Recived data from `ubilling.cls`:
  10. private $config;
  11. private $log;
  12. // Constructor:
  13. public function __construct($data) {
  14. // Put all recived data to specified vars:
  15. foreach ($data as $key => $value) {
  16. $this->$key = $value;
  17. }
  18. // Write log message, that class is loaded:
  19. $this->log->message(__CLASS__, "MySQL driver loaded", 'debug');
  20. // Connect to database:
  21. $this->connect();
  22. }
  23. private function connect() {
  24. if ( !empty($this->config) ) {
  25. $dbport = (empty($this->config['port'])) ? 3306 : $this->config['port'];
  26. if (extension_loaded('mysqli')) {
  27. $this->log->message(__CLASS__, "Use extension: MySQLi", 'debug');
  28. $this->identifier = new mysqli($this->config['hostname'], $this->config['username'], $this->config['password'], $this->config['database'], $dbport);
  29. if ($this->identifier->connect_error) {
  30. $this->log->message(__CLASS__, 'Connection error (' . $this->identifier->connect_errno . ') ' . $this->identifier->connect_error, 'error');
  31. } else {
  32. $this->connected = TRUE;
  33. return $this->connected;
  34. }
  35. } else {
  36. $this->log->message(__CLASS__, "Use extension: MySQL", 'debug');
  37. $this->identifier = mysql_connect($this->config['hostname'] . ':' . $dbport, $this->config['username'], $this->config['password']);
  38. if ( $this->identifier ) {
  39. if ( mysql_select_db($this->config['database'], $this->identifier) ) {
  40. $this->connected = TRUE;
  41. $this->log->message(__CLASS__, "Connection with database is established...", 'debug');
  42. } else $this->log->message(__CLASS__, "Can't select database, name is incorrect!", 'error');
  43. return $this->connected;
  44. } else $this->log->message(__CLASS__, "Can't connect to database, authorization data wrong or server halted!", 'error');
  45. }
  46. } else $this->log->message(__CLASS__, "Can't connect to database, no configurations data!", 'error');
  47. }
  48. private function select($cols, $table, $where = array()) {
  49. $columns = "`" . str_replace(', ', '`, `', $cols) . "`";
  50. $query = "SELECT " . $columns . " FROM " . $table;
  51. if ( !empty($where) ) {
  52. $query .= " WHERE";
  53. foreach ($where as $key => $value) {
  54. $query .= " ";
  55. $query .= "`" . $key . "` = '" . $value . "'";
  56. }
  57. }
  58. $queryID = md5($query);
  59. if ( !array_key_exists($queryID, $this->cache) ) {
  60. $this->log->message(__CLASS__, "Query -> " . $query, 'debug');
  61. if (extension_loaded('mysqli')) {
  62. $result = $this->identifier->query($query);
  63. $this->qc++;
  64. while ($row = mysqli_fetch_assoc($result)) {
  65. $this->cache[$queryID][] = $row;
  66. }
  67. } else {
  68. $result = mysql_query($query, $this->identifier);
  69. $this->qc++;
  70. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  71. $this->cache[$queryID][] = $row;
  72. }
  73. }
  74. } else $this->log->message(__CLASS__, "Cashe -> " . $query, 'debug');
  75. return $this->cache[$queryID];
  76. }
  77. /* DATABASE DATA QUERIES << */
  78. public function get_user_rate() {
  79. $return = array('tx' => 0, 'rx' => 0);
  80. if ( $this->connected ) {
  81. $return = $this->get_user_reassigned_rate();
  82. if ( empty($return['tx']) && empty($return['rx'])) {
  83. $tariff = $this->get_user_tariff();
  84. if ( !empty($tariff) ) {
  85. $return = $this->get_tariff_rate($tariff);
  86. }
  87. }
  88. return $return;
  89. } else $this->log->message(__CLASS__, "Can't get user's rate from database, no connection!", 'error');
  90. }
  91. private function get_user_reassigned_rate() {
  92. $return = array('tx' => 0, 'rx' => 0, 'bt_tx' => 0, 'bt_rx' => 0, 'bt_tx_t' => 0, 'bt_rx_t' => 0);
  93. $result = $this->select('speed', 'userspeeds', array('login' => LOGIN));
  94. foreach ( $result as $data ) {
  95. if ( !empty($data['speed']) ) {
  96. $return['tx'] = $data['speed'];
  97. $return['rx'] = $data['speed'];
  98. $this->log->message(__CLASS__, "User's reassigned rate - `" . $data['speed'] . "`", 'debug');
  99. } else return FALSE;
  100. }
  101. return $return;
  102. }
  103. private function get_tariff_rate($tariff) {
  104. $return = array('tx' => 0, 'rx' => 0, 'bt_tx' => 0, 'bt_rx' => 0, 'bt_tx_t' => 0, 'bt_rx_t' => 0);
  105. $result = $this->select('speedup, speeddown, burstupload, burstdownload, burstimetupload, bursttimedownload' , 'speeds', array('tariff' => $tariff));
  106. foreach ($result as $data) {
  107. if ( !empty($data['speedup']) ) {
  108. $return['tx'] = $data['speedup'];
  109. $this->log->message(__CLASS__, "User's tariff TX rate - `" . $data['speedup'] . "`", 'debug');
  110. } else $this->log->message(__CLASS__, "User's tariff has no TX rate limit!", "error");
  111. if ( !empty($data['speeddown']) ) {
  112. $return['rx'] = $data['speeddown'];
  113. $this->log->message(__CLASS__, "User's tariff RX rate - `" . $data['speeddown'] . "`", 'debug');
  114. } else $this->log->message(__CLASS__, "User's tariff has no RX rate limit!", 'error');
  115. if ( !empty($data['burstupload']) ) {
  116. $return['bt_tx'] = $data['burstupload'];
  117. $this->log->message(__CLASS__, "User's tariff Burst TX rate - `" . $data['burstupload'] . "`", 'debug');
  118. };
  119. if ( !empty($data['burstdownload']) ) {
  120. $return['bt_rx'] = $data['burstdownload'];
  121. $this->log->message(__CLASS__, "User's tariff Burst RX rate - `" . $data['burstdownload'] . "`", 'debug');
  122. };
  123. if ( !empty($data['burstimetupload']) ) {
  124. $return['bt_tx_t'] = $data['burstimetupload'];
  125. $this->log->message(__CLASS__, "User's tariff Burst Time TX rate - `" . $data['burstimetupload'] . "`", 'debug');
  126. };
  127. if ( !empty($data['bursttimedownload']) ) {
  128. $return['bt_rx_t'] = $data['bursttimedownload'];
  129. $this->log->message(__CLASS__, "User's tariff Burst Time RX rate - `" . $data['bursttimedownload'] . "`", 'debug');
  130. };
  131. }
  132. return $return;
  133. }
  134. public function get_user_tariff() {
  135. $return = NULL;
  136. $result = $this->select('Tariff', 'users', array('login' => LOGIN));
  137. foreach ( $result as $data ) {
  138. if ( !empty($data['Tariff']) AND $data['Tariff'] != '*_NO_TARIFF_*' ) {
  139. $return = $data['Tariff'];
  140. $this->log->message(__CLASS__, "User's tariff - `" . $data['Tariff'] . "`", 'debug');
  141. } else $this->log->message(__CLASS__, "User has no assigned tariff!", 'error');
  142. }
  143. return $return;
  144. }
  145. public function get_nas_data() {
  146. $return = array();
  147. if ( $this->connected ) {
  148. $netID = $this->get_network_id();
  149. $result = $this->select('nasip, nastype, options', 'nas', array('netid' => $netID));
  150. foreach ($result as $data) {
  151. if ( !empty($data['nasip']) ) {
  152. $return['ip'] = $data['nasip'];
  153. $this->log->message(__CLASS__, "Data -> `nasip` = " . $data['nasip'], 'debug');
  154. } else $this->log->message(__CLASS__, "Can't find IP address of NAS!", 'error');
  155. if ( !empty($data['nastype']) ) {
  156. switch ($data['nastype']) {
  157. case 'mikrotik':
  158. // Get NAS'es options (for MikroTik only):
  159. $options = array();
  160. if ( !empty($data['options']) ) {
  161. $options = unserialize(base64_decode($data['options']));
  162. foreach ($options as $option => $value) {
  163. $this->log->message(__CLASS__, "Option -> `" . $option . "` = '" . $value . "'", 'debug');
  164. }
  165. } else $this->log->message(__CLASS__, "Can't find user's NAS options!", 'error');
  166. $return['options'] = $options;
  167. case 'rscriptd':
  168. case 'local':
  169. case 'other':
  170. $return['type'] = $data['nastype'];
  171. $this->log->message(__CLASS__, "Data -> `nastype` = " . $data['nastype'], 'debug');
  172. break;
  173. }
  174. } else $this->log->message(__CLASS__, "Can't find user's NAS type!", 'error');
  175. }
  176. } else $this->log->message(__CLASS__, "Can't get NAS's data from database, no connection!", 'error');
  177. return $return;
  178. }
  179. public function get_network_id() {
  180. $return = NULL;
  181. switch ( ENVIRONMENT ) {
  182. case 'OnConnect':
  183. case 'OnDisconnect':
  184. $result = $this->select('netid', 'nethosts', array('ip' => IP));
  185. foreach ($result as $data) {
  186. if ( !empty($data['netid']) ) {
  187. $return = $data['netid'];
  188. }
  189. }
  190. break;
  191. case 'OnChange':
  192. case 'OnUserDel':
  193. $result = $this->select('id, desc', 'networks');
  194. if ( !empty($result) ) {
  195. foreach ($result as $network) {
  196. list($netIP, $netCIDR) = explode('/', $network['desc']);
  197. if ( (ip2long(IP) & ~((1 << (32 - $netCIDR))-1)) == ip2long($netIP) ) {
  198. $return = $network['id'];
  199. break;
  200. }
  201. }
  202. }
  203. break;
  204. }
  205. if ( empty($result) ) $this->log->message(__CLASS__, "Can't find user's network id!", 'error');
  206. return $return;
  207. }
  208. public function get_user_ip() {
  209. $return = NULL;
  210. $result = $this->select('IP', 'users', array('login' => LOGIN));
  211. if ( !empty($result) ) {
  212. foreach ($result as $data) {
  213. if ( !empty($data['IP']) ) {
  214. $return = $data['IP'];
  215. $this->log->message(__CLASS__, "Data -> `IP` is " . $data['IP'], 'debug');
  216. }
  217. break;
  218. }
  219. } else $this->log->message(__CLASS__, "Can't find user's IP", 'error');
  220. return $return;
  221. }
  222. public function get_user_mac() {
  223. if ( $this->connected ) {
  224. $return = NULL;
  225. $result = $this->select('mac', 'nethosts', array('ip' => IP));
  226. if ( !empty($result) ) {
  227. foreach ($result as $data) {
  228. if ( !empty($data['mac']) ) {
  229. $return = $data['mac'];
  230. $this->log->message(__CLASS__, "Data -> `mac` is " . $data['mac'], 'debug');
  231. }
  232. break;
  233. }
  234. } else $this->log->message(__CLASS__, "Can't find user's MAC", 'error');
  235. return $return;
  236. } else $this->log->message(__CLASS__, "Can't get user's MAC from database, no connection!", 'error');
  237. }
  238. public function get_user_password() {
  239. $default = '';
  240. if ( $this->connected ) {
  241. $result = $this->select('Password', 'users', array('login' => LOGIN));
  242. return !empty($result) ? $result[0]['Password'] : $default;
  243. } else $this->log->message(__CLASS__, "Can't get user's password from database, no connection!", 'error');
  244. }
  245. /* >> DATABASE DATA QUERIES */
  246. }
  247. ?>