mikrotikdnshaper.php 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. //MikroTik dynamic shaper
  3. if ($_GET['action'] == 'mikrotikdnshaper') {
  4. if ($alterconf['DSHAPER_ENABLED']) {
  5. $Now = date('H:i:s');
  6. $DNDataQuery = "SELECT `usr_nh`.*, `nas`.`nasip`, `nas`.`options`,
  7. `speeds`.`speeddown`, `speeds`.`speedup`, `speeds`.`burstdownload`, `speeds`.`burstupload`, `speeds`.`bursttimedownload`, `speeds`.`burstimetupload`,
  8. `dshpt`.`threshold1`, `dshpt`.`threshold2`, `dshpt`.`speed`
  9. FROM (
  10. SELECT `users`.`login`, `users`.`ip`, `users`.`Tariff`, `nh`.`netid`
  11. FROM `users`
  12. LEFT JOIN `nethosts` AS `nh` ON `users`.`ip` = `nh`.`ip`
  13. WHERE !`users`.`Down` AND !( `users`.`Cash` < -(`users`.`Credit`) )
  14. ) AS `usr_nh`
  15. LEFT JOIN `nas` ON `usr_nh`.`netid` = `nas`.`netid`
  16. LEFT JOIN `dshape_time` AS `dshpt` ON `usr_nh`.`Tariff` = `dshpt`.`tariff`
  17. LEFT JOIN `speeds` ON `usr_nh`.`Tariff` = `speeds`.`tariff`
  18. WHERE `nas`.`nastype` = 'mikrotik' AND `dshpt`.`speed` IS NOT NULL AND '" . $Now . "' BETWEEN `dshpt`.`threshold1` AND `dshpt`.`threshold2`;";
  19. $DNData = simple_queryall($DNDataQuery);
  20. if (!empty($DNData)) {
  21. $UsersCnt = count($DNData);
  22. $RouterOSAPI = new RouterOS();
  23. $Action = '';
  24. foreach ($DNData as $eachrow => $eachlogin) {
  25. $MTikNasOpts = base64_decode($eachlogin['options']);
  26. $MTikNasOpts = unserialize($MTikNasOpts);
  27. $UseNewConnType = ( isset($MTikNasOpts['use_new_conn_mode']) && $MTikNasOpts['use_new_conn_mode'] ) ? true : false;
  28. $apiPort = ( !empty($MTikNasOpts['apiport']) ) ? $MTikNasOpts['apiport'] : 8728;
  29. $RouterOSAPI->connect($eachlogin['nasip'], $MTikNasOpts['username'], $MTikNasOpts['password'], $UseNewConnType, $apiPort);
  30. if ($RouterOSAPI->connected) {
  31. if (isset($_GET['param']) && ($_GET['param'] == 'downshift')) {
  32. $Action = 'DOWNSHIFT';
  33. if (empty($eachlogin['burstimetupload']) or empty($eachlogin['bursttimedownload'])
  34. or empty($eachlogin['burstupload']) or empty($eachlogin['burstdownload'])) {
  35. $Template = array('.id' => '',
  36. 'max-limit' => $eachlogin['speedup'] . 'k/' . $eachlogin['speeddown'] . 'k'
  37. );
  38. } else {
  39. $Template = array('.id' => '',
  40. 'max-limit' => $eachlogin['speedup'] . 'k/' . $eachlogin['speeddown'] . 'k',
  41. 'burst-limit' => $eachlogin['burstupload'] . 'k/' . $eachlogin['burstdownload'] . 'k',
  42. 'burst-threshold' => ($eachlogin['speedup'] * 0.8) . 'k/' . ($eachlogin['speeddown'] * 0.8) . 'k',
  43. 'burst-time' => $eachlogin['burstimetupload'] . '/' . $eachlogin['bursttimedownload']
  44. );
  45. }
  46. } else {
  47. if (empty($eachlogin['burstimetupload']) or empty($eachlogin['bursttimedownload'])
  48. or empty($eachlogin['burstupload']) or empty($eachlogin['burstdownload'])) {
  49. $Template = array('.id' => '',
  50. 'max-limit' => $eachlogin['speedup'] . 'k/' . $eachlogin['speed'] . 'k'
  51. );
  52. } else {
  53. $Template = array('.id' => '',
  54. 'max-limit' => $eachlogin['speedup'] . 'k/' . $eachlogin['speed'] . 'k',
  55. 'burst-limit' => $eachlogin['burstupload'] . 'k/' . $eachlogin['speed'] . 'k',
  56. 'burst-threshold' => ($eachlogin['speedup'] * 0.8) . 'k/' . ($eachlogin['speed'] * 0.8) . 'k',
  57. 'burst-time' => $eachlogin['burstimetupload'] . '/' . $eachlogin['bursttimedownload']
  58. );
  59. }
  60. }
  61. $Entries = $RouterOSAPI->command('/queue/simple/print', array('.proplist' => '.id', '?name' => '' . trim($eachlogin['login']) . ''));
  62. if (!empty($Entries)) {
  63. foreach ($Entries as $Entry) {
  64. $Template['.id'] = $Entry['.id'];
  65. $MTikReply = $RouterOSAPI->command('/queue/simple/set', $Template);
  66. }
  67. }
  68. }
  69. }
  70. log_register('MT_DN_SHAPER ' . $Action . ' done to `' . $UsersCnt . '` users');
  71. die('OK:MT_DN_SHAPER');
  72. } else {
  73. die('OK:MT_DN_SHAPER_NO_USERS_TO_PROCESS');
  74. }
  75. }
  76. }