handlers.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <?php
  2. /**
  3. *
  4. * @param string $login
  5. *
  6. * @return void
  7. */
  8. function billing_createuser($login) {
  9. setVal($login, "add");
  10. }
  11. /**
  12. * Creates new stargazer user
  13. *
  14. * @param string $login
  15. *
  16. * @return void
  17. */
  18. function billing_deleteuser($login) {
  19. setVal($login, "del");
  20. }
  21. /**
  22. * Sets user credit limit
  23. *
  24. * @param string $login
  25. * @param float $credit
  26. *
  27. * @return void
  28. */
  29. function billing_setcredit($login, $credit) {
  30. setVal($login, "credit", $credit);
  31. }
  32. /**
  33. * Sets user credit limit expire date
  34. *
  35. * @param string $login
  36. * @param string $creditexpire
  37. *
  38. * @return void
  39. */
  40. function billing_setcreditexpire($login, $creditexpire) {
  41. $creditexpire = strtotime($creditexpire);
  42. setVal($login, "CreditExpire", $creditexpire);
  43. }
  44. /**
  45. * Sets user IP address
  46. *
  47. * @param string $login
  48. * @param string $ip
  49. *
  50. * @return void
  51. */
  52. function billing_setip($login, $ip) {
  53. setVal($login, "ip", $ip);
  54. }
  55. /**
  56. * Changes user password
  57. *
  58. * @param string $login
  59. * @param string $password
  60. *
  61. * @return void
  62. */
  63. function billing_setpassword($login, $password) {
  64. setVal($login, "password", $password);
  65. }
  66. /**
  67. * Changes user tariff
  68. *
  69. * @param string $login
  70. * @param string $tariff
  71. *
  72. * @return void
  73. */
  74. function billing_settariff($login, $tariff) {
  75. setVal($login, "tariff", $tariff, "now");
  76. }
  77. /**
  78. * Changes user tariff from next month
  79. *
  80. * @param string $login
  81. * @param string $tariff
  82. *
  83. * @return void
  84. */
  85. function billing_settariffnm($login, $tariff) {
  86. setVal($login, "tariff", $tariff, "delayed");
  87. }
  88. /**
  89. * Pushes some money summ to user account
  90. *
  91. * @param string $login
  92. * @param float $cash
  93. *
  94. * @return void
  95. */
  96. function billing_addcash($login, $cash) {
  97. setVal($login, "cash", $cash, "add");
  98. }
  99. /**
  100. * Changes user AlwaysOnline flag state
  101. *
  102. * @param string $login
  103. * @param int $state
  104. *
  105. * @return void
  106. */
  107. function billing_setao($login, $state) {
  108. setVal($login, "aonline", $state);
  109. }
  110. /**
  111. * Sets user Cash parameter to some summ value
  112. *
  113. * @param string $login
  114. * @param float $cash
  115. *
  116. * @return void
  117. */
  118. function billing_setcash($login, $cash) {
  119. setVal($login, "cash", $cash, "set");
  120. }
  121. /**
  122. * Changes user detailed stats flag state
  123. *
  124. * @param string $login
  125. * @param int $state
  126. *
  127. * @return void
  128. */
  129. function billing_setdstat($login, $state) {
  130. setVal($login, "disabledetailstat", $state);
  131. }
  132. /**
  133. * Changes user Down flag state
  134. *
  135. * @param string $login
  136. * @param int $state
  137. *
  138. * @return void
  139. */
  140. function billing_setdown($login, $state) {
  141. setVal($login, "down", $state);
  142. }
  143. /**
  144. * Performs sequential call of OnDisconnect and OnConnect init scripts.
  145. *
  146. * @global object $billing_config
  147. * @param string $login
  148. *
  149. * @return void
  150. */
  151. function billing_resetuser($login) {
  152. global $billing_config;
  153. //rscriptd reset hotfix
  154. if ($billing_config['RESET_AO']) {
  155. billing_setao($login, 0);
  156. billing_setao($login, 1);
  157. } else {
  158. billing_setdown($login, 1);
  159. billing_setdown($login, 0);
  160. }
  161. }
  162. /**
  163. * Changes user Passive (aka Frozen) flag state
  164. *
  165. * @param string $login
  166. * @param int $state
  167. *
  168. * @return void
  169. */
  170. function billing_setpassive($login, $state) {
  171. setVal($login, "passive", $state);
  172. }
  173. /**
  174. * Creates new Stargazer tariff
  175. *
  176. * @param string $tariff
  177. *
  178. * @return void
  179. */
  180. function billing_createtariff($tariff) {
  181. setVal(@$login, "addtariff", $tariff);
  182. }
  183. /**
  184. * Deletes some Stargazer tariff
  185. *
  186. * @param string $tariff
  187. *
  188. * @return void
  189. */
  190. function billing_deletetariff($tariff) {
  191. setVal(@$login, "deltariff", $tariff);
  192. }
  193. /**
  194. * Returns array of all available Stargazer tariffs with all tariff data
  195. *
  196. * @return array
  197. */
  198. function billing_getalltariffs() {
  199. return simple_queryall("SELECT * from `tariffs` ORDER BY `name`");
  200. }
  201. /**
  202. * Returns array of some existing tariff data
  203. *
  204. * @param string $name
  205. *
  206. * @return array
  207. */
  208. function billing_gettariff($name) {
  209. return simple_query("SELECT * from `tariffs` where `name` = '$name'");
  210. }
  211. /**
  212. * Changes some existing Stargazer tariff parameters
  213. *
  214. * @param string $tariff
  215. * @param array $options
  216. *
  217. * @return void
  218. */
  219. function billing_edittariff($tariff, $options) {
  220. $dhour = $options ['dhour'];
  221. $dmin = $options ['dmin'];
  222. $nhour = $options ['nhour'];
  223. $nmin = $options ['nmin'];
  224. $PriceDay = $options ['PriceDay'];
  225. $PriceNight = $options ['PriceNight'];
  226. $Fee = $options ['Fee'];
  227. $Free = $options ['Free'];
  228. $PassiveCost = $options ['PassiveCost'];
  229. $TraffType = $options ['TraffType'];
  230. if (isset($options['Period'])) {
  231. $period = $options['Period'];
  232. } else {
  233. $period = '';
  234. }
  235. $dirs = getAllDirs();
  236. $string = "<SetTariff name=\"$tariff\">";
  237. $string .= "<Fee value=\"$Fee\"/>";
  238. $string .= "<Free value=\"$Free\"/>";
  239. $string .= "<PassiveCost value=\"$PassiveCost\"/>";
  240. $string .= "<TraffType value=\"$TraffType\"/>";
  241. if (!empty($period)) {
  242. $string .= "<period value=\"" . $period . "\"/>";
  243. }
  244. foreach ($dirs as $dir) {
  245. $key = $dir['rulenumber'];
  246. $string .= "<Time$key value=\"$dhour[$key]:$dmin[$key]-$nhour[$key]:$nmin[$key]\"/>";
  247. }
  248. $PriceDayA = '';
  249. $PriceDayB = '';
  250. $PriceNightA = '';
  251. $PriceNightB = '';
  252. $SinglePrice = '';
  253. $NoDiscount = '';
  254. $Threshold = '';
  255. $dayATmp = array();
  256. $dayBTmp = array();
  257. $nightATmp = array();
  258. $nightBTmp = array();
  259. // A-a-a-a-a-a-a!
  260. // https://stg.net.ua/doc/ch11.html
  261. // A-A-A-A-A-A!!!1
  262. for ($i = 0; $i <= 9; $i++) {
  263. if (isset($PriceDay[$i])) {
  264. if (strpos($PriceDay[$i], '/') !== false) {
  265. $prices = explode('/', $PriceDay[$i]);
  266. if (sizeof($prices) == 2) {
  267. $dayATmp[$i] = $prices[0];
  268. $dayBTmp[$i] = $prices[1];
  269. } else {
  270. debarr(sizeof($prices));
  271. $dayATmp[$i] = 0;
  272. $dayBTmp[$i] = 0;
  273. }
  274. } else {
  275. $dayATmp[$i] = $PriceDay[$i];
  276. $dayBTmp[$i] = $PriceDay[$i];
  277. }
  278. } else {
  279. $dayATmp[$i] = 0;
  280. $dayBTmp[$i] = 0;
  281. }
  282. if (isset($PriceNight[$i])) {
  283. if (strpos($PriceNight[$i], '/') !== false) {
  284. $prices = explode('/', $PriceNight[$i]);
  285. if (sizeof($prices) == 2) {
  286. $nightATmp[$i] = $prices[0];
  287. $nightBTmp[$i] = $prices[1];
  288. } else {
  289. $nightATmp[$i] = 0;
  290. $nightBTmp[$i] = 0;
  291. }
  292. } else {
  293. $nightATmp[$i] = $PriceNight[$i];
  294. $nightBTmp[$i] = $PriceNight[$i];
  295. }
  296. } else {
  297. $nightATmp[$i] = 0;
  298. $nightBTmp[$i] = 0;
  299. }
  300. }
  301. for ($i = 0; $i <= 9; $i++) {
  302. $delimiter = ($i < 9) ? '/' : '';
  303. if (isset($options['NoDiscount'][$i])) {
  304. $NoDiscount .= '1' . $delimiter;
  305. } else {
  306. $NoDiscount .= '0' . $delimiter;
  307. }
  308. if (isset($options['SinglePrice'][$i])) {
  309. $SinglePrice .= '1' . $delimiter;
  310. } else {
  311. $SinglePrice .= '0' . $delimiter;
  312. }
  313. if (isset($options['Threshold'][$i])) {
  314. $Threshold .= $options['Threshold'][$i] . $delimiter;
  315. } else {
  316. $Threshold .= '0' . $delimiter;
  317. }
  318. $PriceDayA .= $dayATmp[$i] . $delimiter;
  319. $PriceDayB .= $dayBTmp[$i] . $delimiter;
  320. $PriceNightA .= $nightATmp[$i] . $delimiter;
  321. $PriceNightB .= $nightBTmp[$i] . $delimiter;
  322. }
  323. $string .= "<PriceDayA value=\"$PriceDayA\"/>";
  324. $string .= "<PriceDayB value=\"$PriceDayB\"/>";
  325. $string .= "<PriceNightA value=\"$PriceNightA\"/>";
  326. $string .= "<PriceNightB value=\"$PriceNightB\"/>";
  327. $string .= "<SinglePrice value=\"$SinglePrice\"/>";
  328. $string .= "<NoDiscount value=\"$NoDiscount\"/>";
  329. $string .= "<Threshold value=\"$Threshold\"/>";
  330. $string .= "</SetTariff>";
  331. executor($string);
  332. }
  333. /**
  334. * Returns array of all available traffic classes
  335. *
  336. * @return array
  337. */
  338. function getAllDirs() {
  339. return simple_queryall("SELECT * from `directions` ORDER BY `rulenumber`");
  340. }
  341. /**
  342. * Performs sgconf_xml call with some XML formatted request
  343. *
  344. * @global object $billing_config
  345. * @param string $attr
  346. *
  347. * @param bool $debug
  348. *
  349. * @return void
  350. */
  351. function executor($attr, $debug = false) {
  352. global $billing_config;
  353. $cmd = $billing_config['SGCONFXML'] . ' -s ' . $billing_config['STG_HOST'] . ' -p ' . $billing_config['STG_PORT'] . ' -a ' . $billing_config['STG_LOGIN'] . ' -w ' . $billing_config['STG_PASSWD'] . ' -r \'' . $attr . '\'';
  354. if ($debug) {
  355. print(htmlspecialchars($cmd) . "\n<br>");
  356. print(shell_exec($cmd));
  357. die();
  358. } else {
  359. shell_exec($cmd);
  360. }
  361. }
  362. /**
  363. * Performs call of executor with some request data
  364. *
  365. * @param string $login
  366. * @param string $type
  367. * @param string $value
  368. * @param string $subtype
  369. *
  370. * @return void
  371. */
  372. function setVal($login, $type, $value = false, $subtype = false) {
  373. $maintype = 'SetUser';
  374. $maintype = ($type == 'add') ? 'AddUser' : $maintype;
  375. $maintype = ($type == 'del') ? 'DelUser' : $maintype;
  376. $maintype = ($type == 'addtariff') ? 'AddTariff' : $maintype;
  377. $maintype = ($type == 'deltariff') ? 'DelTariff' : $maintype;
  378. $string = "<$maintype><login value=\"$login\" />";
  379. switch ($type) {
  380. case (preg_match('#cash|\btariff\b#i', $type) ? $type : !$type) :
  381. $val = $subtype;
  382. break;
  383. case 'del':
  384. $val = 'login';
  385. break;
  386. case (preg_match('#addtariff|deltariff#Uis', $type) ? $type : !$type):
  387. $val = 'name';
  388. break;
  389. default :
  390. $val = 'value';
  391. break;
  392. }
  393. if ($type != 'add') {
  394. $string .= "<$type $val=\"$value\" />";
  395. }
  396. $string .= "</$maintype>";
  397. $string = ($type == 'del') ? "<$maintype $val=\"$login\" />" : $string;
  398. $string = ($type == 'addtariff' || $type == 'deltariff') ? "<$type $val=\"$value\" />" : $string;
  399. executor($string, false);
  400. }