api.userdata.php 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. <?php
  2. // userdata getters/setters
  3. /**
  4. * Creates user realname database record
  5. *
  6. * @param string $login existing user login
  7. * @param string $realname user realname to create
  8. */
  9. function zb_UserCreateRealName($login, $realname) {
  10. $login = vf($login);
  11. $realname = mysql_real_escape_string($realname);
  12. $query = "INSERT INTO `realname` (`id`,`login`,`realname`) VALUES (NULL, '" . $login . "','" . $realname . "'); ";
  13. nr_query($query);
  14. log_register('CREATE UserRealName (' . $login . ') `' . $realname . '`');
  15. zb_UserGetAllDataCacheClean();
  16. }
  17. /**
  18. * Deletes user realname from database
  19. *
  20. * @param string $login existing user login
  21. */
  22. function zb_UserDeleteRealName($login) {
  23. $login = vf($login);
  24. $query = "DELETE from `realname` WHERE `login` = '" . $login . "';";
  25. nr_query($query);
  26. log_register('DELETE UserRealName (' . $login . ')');
  27. zb_UserGetAllDataCacheClean();
  28. }
  29. /**
  30. * Returns realname field by users login
  31. *
  32. * @param string $login existing user login
  33. * @return string
  34. */
  35. function zb_UserGetRealName($login) {
  36. $login = vf($login);
  37. $query = "SELECT `realname` from `realname` WHERE `login`='" . $login . "'";
  38. $realname_arr = simple_query($query);
  39. return ($realname_arr['realname']);
  40. }
  41. /**
  42. * Changes realname database record
  43. *
  44. * @param string $login existing user login
  45. * @param string $realname user realname to set
  46. */
  47. function zb_UserChangeRealName($login, $realname) {
  48. $login = vf($login);
  49. $realname = str_replace("'", '`', $realname);
  50. $realname = str_replace('"', '``', $realname);
  51. $realname = str_replace('\\', '', $realname);
  52. $realname = mysql_real_escape_string($realname);
  53. $query = "UPDATE `realname` SET `realname` = '" . $realname . "' WHERE `login`= '" . $login . "' ;";
  54. nr_query($query);
  55. log_register('CHANGE UserRealName (' . $login . ') `' . $realname . '`');
  56. zb_UserGetAllDataCacheClean();
  57. }
  58. /**
  59. * Returns all of users realnames records as login=>realname array
  60. *
  61. * @return array
  62. */
  63. function zb_UserGetAllRealnames() {
  64. $query_fio = "SELECT * from `realname`";
  65. $allfioz = simple_queryall($query_fio);
  66. $fioz = array();
  67. if (!empty($allfioz)) {
  68. foreach ($allfioz as $ia => $eachfio) {
  69. $fioz[$eachfio['login']] = $eachfio['realname'];
  70. }
  71. }
  72. return ($fioz);
  73. }
  74. /**
  75. * Selects all users' IP addresses from database as login=>ip array
  76. *
  77. * @return array
  78. */
  79. function zb_UserGetAllIPs() {
  80. $query = "SELECT `login`,`IP` from `users`";
  81. $result = simple_queryall($query);
  82. $ips = array();
  83. if (!empty($result)) {
  84. foreach ($result as $ip) {
  85. $ips[$ip['login']] = $ip['IP'];
  86. }
  87. }
  88. return $ips;
  89. }
  90. /**
  91. * Returns all of IP=>MAC nethosts bindings from database as array ip=>mac
  92. *
  93. * @return array
  94. */
  95. function zb_UserGetAllIpMACs() {
  96. $query = "SELECT `ip`,`mac` from `nethosts`";
  97. $all = simple_queryall($query);
  98. $result = array();
  99. if (!empty($all)) {
  100. foreach ($all as $io => $each) {
  101. $result[$each['ip']] = $each['mac'];
  102. }
  103. }
  104. return ($result);
  105. }
  106. /**
  107. * Flushes USER_ALL_DATA cache and other cache keys that must be actual
  108. *
  109. * @global object $ubillingConfig
  110. *
  111. * @return void
  112. */
  113. function zb_UserGetAllDataCacheClean() {
  114. global $ubillingConfig;
  115. $cache = new UbillingCache();
  116. $cache->delete('USER_ALL_DATA');
  117. if ($ubillingConfig->getAlterParam('SMARTUP_ENABLED')) {
  118. $cache->delete('SMARTUP_USERDATA');
  119. $cache->delete('SMARTUP_PAYIDS');
  120. }
  121. }
  122. /**
  123. * Returns all users cached data from function zb_UserGetAllData
  124. *
  125. * @return array
  126. */
  127. function zb_UserGetAllDataCache() {
  128. global $ubillingConfig;
  129. $result = array();
  130. $cachingTimeout = 86400;
  131. $optionalCachingTimeout = $ubillingConfig->getAlterParam('USERALLDATA_CACHETIME');
  132. if ($optionalCachingTimeout) {
  133. if (is_numeric($optionalCachingTimeout)) {
  134. $cachingTimeout = $optionalCachingTimeout * 60; //option in minutes
  135. }
  136. }
  137. $cache = new UbillingCache();
  138. $result = $cache->getCallback('USER_ALL_DATA', function () {
  139. return (zb_UserGetAllData());
  140. }, $cachingTimeout);
  141. return ($result);
  142. }
  143. /**
  144. * Returns all information about User by login
  145. *
  146. * @param string $login existing user login
  147. * @return array ['login']=>array(login,realname,Passive,Down,Password,AlwaysOnline,Tariff,Credit,Cash,ip,mac,cityname,streetname,buildnum,entrance,floor,apt,geo,fulladress,phone,mobile,contract)
  148. * Crazy Pautina
  149. */
  150. function zb_UserGetAllData($login = '') {
  151. global $ubillingConfig;
  152. $altCfg = $ubillingConfig->getAlter();
  153. $result = array();
  154. $query_wh = (!empty($login)) ? "WHERE `users`.`login` = '" . vf($login) . "'" : "";
  155. // we can use such check here:
  156. // $query_wh = (!empty($login) and in_array($login, zb_UserGetAllStargazerLogins())) ? "WHERE `users`.`login` = '" . vf($login) . "'" : "";
  157. // but I doubt if it may broke something else in:
  158. // zb_PrintCheck() - api.cess
  159. // ts_CreateTask() - api.taskman
  160. // ts_ModifyTask() - api.taskman
  161. // loadUserAlldata() - api.userprofile
  162. if ($ubillingConfig->getAlterParam('ADDRESS_EXTENDED_ENABLED')) {
  163. $addrexten_query = " `postal_code`, `town_district`, address_exten, ";
  164. $addrexten_join = " LEFT JOIN `address_extended` ON `users`.`login` = `address_extended`.`login` ";
  165. } else {
  166. $addrexten_query = '';
  167. $addrexten_join = '';
  168. }
  169. $query = "
  170. SELECT `users`.`login`, `realname`.`realname`, `Passive`, `Down`, `Password`,`AlwaysOnline`, `Tariff`, `TariffChange`, `Credit`, `Cash`,
  171. `ip`, `mac`, `cityname`, `streetname`, `buildnum`, `entrance`, `floor`, `apt`, `geo`,";
  172. $query .= $addrexten_query;
  173. if ($altCfg['ZERO_TOLERANCE'] and $altCfg['CITY_DISPLAY']) {
  174. $query .= "concat(`cityname`, ' ', `streetname`, ' ', `buildnum`, IF(`apt`, concat('/',`apt`), '')) AS `fulladress`,";
  175. } elseif ($altCfg['ZERO_TOLERANCE'] and !$altCfg['CITY_DISPLAY']) {
  176. $query .= "concat(`streetname`, ' ', `buildnum`, IF(`apt`, concat('/',`apt`), '')) AS `fulladress`,";
  177. } elseif (!$altCfg['ZERO_TOLERANCE'] and $altCfg['CITY_DISPLAY']) {
  178. $query .= "concat(`cityname`, ' ', `streetname`, ' ', `buildnum`, '/', `apt`) AS `fulladress`,";
  179. } else {
  180. $query .= "concat(`streetname`, ' ', `buildnum`, '/', `apt`) AS `fulladress`,";
  181. }
  182. $query .= "
  183. `phones`.`phone`,`mobile`,`contract`,`emails`.`email`
  184. FROM `users` LEFT JOIN `nethosts` USING (`ip`)
  185. LEFT JOIN `realname` ON (`users`.`login`=`realname`.`login`)
  186. LEFT JOIN `address` ON (`users`.`login`=`address`.`login`)
  187. LEFT JOIN `apt` ON (`address`.`aptid`=`apt`.`id`)
  188. LEFT JOIN `build` ON (`apt`.`buildid`=`build`.`id`)
  189. LEFT JOIN `street` ON (`build`.`streetid`=`street`.`id`)
  190. LEFT JOIN `city` ON (`street`.`cityid`=`city`.`id`)
  191. LEFT JOIN `phones` ON (`users`.`login`=`phones`.`login`)
  192. LEFT JOIN `contracts` ON (`users`.`login`=`contracts`.`login`)
  193. LEFT JOIN `emails` ON (`users`.`login`=`emails`.`login`)
  194. " . $addrexten_join . $query_wh;
  195. $Alldata = (!empty($login)) ? simple_query($query) : simple_queryall($query);
  196. if (empty($login) and !empty($Alldata)) {
  197. foreach ($Alldata as $data) {
  198. $result[$data['login']] = $data;
  199. }
  200. } else {
  201. $result[$login] = $Alldata;
  202. }
  203. return ($result);
  204. }
  205. /**
  206. * Returns all of used by users MAC bindings from database as array login=>mac
  207. *
  208. * @return array
  209. */
  210. function zb_UserGetAllMACs() {
  211. $alluserips = zb_UserGetAllIPs();
  212. $alluserips = array_flip($alluserips);
  213. $allmac = zb_UserGetAllIpMACs();
  214. $result = array();
  215. //filling mac array
  216. if (!empty($allmac)) {
  217. foreach ($allmac as $eachip => $eachmac) {
  218. if (isset($alluserips[$eachip])) {
  219. $result[$alluserips[$eachip]] = $eachmac;
  220. }
  221. }
  222. }
  223. return ($result);
  224. }
  225. /**
  226. * Creates phone data database field
  227. *
  228. * @param string $login existing user login
  229. * @param string $phone phone number to set
  230. * @param string $mobile mobile number to set
  231. */
  232. function zb_UserCreatePhone($login, $phone, $mobile) {
  233. $login = vf($login);
  234. $phone = mysql_real_escape_string($phone);
  235. $mobile = mysql_real_escape_string($mobile);
  236. $query = "INSERT INTO `phones` (`id`,`login`,`phone`,`mobile`) VALUES (NULL, '" . $login . "','" . $phone . "','" . $mobile . "');";
  237. nr_query($query);
  238. log_register('CREATE UserPhone (' . $login . ') `' . $phone . '` `' . $mobile . '`');
  239. zb_UserGetAllDataCacheClean();
  240. }
  241. /**
  242. * Deletes phonedata record from database
  243. *
  244. * @param string $login existing user login
  245. */
  246. function zb_UserDeletePhone($login) {
  247. $login = vf($login);
  248. $query = "DELETE from `phones` WHERE `login` = '" . $login . "';";
  249. nr_query($query);
  250. log_register('DELETE UserPhone (' . $login . ')');
  251. zb_UserGetAllDataCacheClean();
  252. }
  253. /**
  254. * Returns phone number from database for some login
  255. *
  256. * @param string $login existing user login
  257. * @return string
  258. */
  259. function zb_UserGetPhone($login) {
  260. $query = "SELECT `phone` from `phones` WHERE `login`='" . $login . "'";
  261. $phone_arr = simple_query($query);
  262. return ($phone_arr['phone']);
  263. }
  264. /**
  265. * Returns mobile number from database for some login
  266. *
  267. * @param string $login existing user login
  268. * @return string
  269. */
  270. function zb_UserGetMobile($login) {
  271. $query = "SELECT `mobile` from `phones` WHERE `login`='" . $login . "'";
  272. $phone_arr = simple_query($query);
  273. return ($phone_arr['mobile']);
  274. }
  275. /**
  276. * Changes user phone in database
  277. *
  278. * @param string $login existing user login
  279. * @param string $phone phone number to set
  280. */
  281. function zb_UserChangePhone($login, $phone) {
  282. $login = vf($login);
  283. $phone = mysql_real_escape_string($phone);
  284. $query = "UPDATE `phones` SET `phone` = '" . $phone . "' WHERE `login`= '" . $login . "' ;";
  285. nr_query($query);
  286. log_register('CHANGE UserPhone (' . $login . ') `' . $phone . '`');
  287. zb_UserGetAllDataCacheClean();
  288. }
  289. /**
  290. * Changes mobile number in database
  291. *
  292. * @param string $login existing user login
  293. * @param string $mobile mobile number to set
  294. */
  295. function zb_UserChangeMobile($login, $mobile) {
  296. $login = vf($login);
  297. $mobile = mysql_real_escape_string($mobile);
  298. $query = "UPDATE `phones` SET `mobile` = '" . $mobile . "' WHERE `login`= '" . $login . "' ;";
  299. nr_query($query);
  300. log_register('CHANGE UserMobile (' . $login . ') `' . $mobile . '`');
  301. zb_UserGetAllDataCacheClean();
  302. }
  303. /**
  304. * Returns all users phone data as array login=>phonedata array(phone+mobile)
  305. *
  306. * @return array
  307. */
  308. function zb_UserGetAllPhoneData() {
  309. $query = "SELECT `login`, `phone`,`mobile` FROM `phones`";
  310. $result = simple_queryall($query);
  311. $phones = array();
  312. if (!empty($result)) {
  313. foreach ($result as $phone) {
  314. $phones[$phone['login']]['phone'] = $phone['phone'];
  315. $phones[$phone['login']]['mobile'] = $phone['mobile'];
  316. }
  317. }
  318. return ($phones);
  319. }
  320. /**
  321. * Creates user email database record
  322. *
  323. * @param string $login existing user login
  324. * @param string $email user email to set
  325. */
  326. function zb_UserCreateEmail($login, $email) {
  327. $login = vf($login);
  328. $email = mysql_real_escape_string($email);
  329. $query = "INSERT INTO `emails` (`id`,`login`,`email`) VALUES (NULL, '" . $login . "','" . $email . "');";
  330. nr_query($query);
  331. log_register('CREATE UserEmail (' . $login . ') `' . $email . '`');
  332. zb_UserGetAllDataCacheClean();
  333. }
  334. /**
  335. * Deletes user email record from database
  336. *
  337. * @param string $login existing user login
  338. */
  339. function zb_UserDeleteEmail($login) {
  340. $login = vf($login);
  341. $query = "DELETE from `emails` WHERE `login` = '" . $login . "';";
  342. nr_query($query);
  343. log_register('DELETE UserEmail (' . $login . ')');
  344. zb_UserGetAllDataCacheClean();
  345. }
  346. /**
  347. * Returns user email from database
  348. *
  349. * @param string $login existing user login
  350. * @return string
  351. */
  352. function zb_UserGetEmail($login) {
  353. $login = vf($login);
  354. $query = "SELECT `email` from `emails` WHERE `login`='" . $login . "'";
  355. $email_arr = simple_query($query);
  356. return ($email_arr['email']);
  357. }
  358. /**
  359. * Changes user email record in database
  360. *
  361. * @param string $login existing user login
  362. * @param string $email user email to set
  363. */
  364. function zb_UserChangeEmail($login, $email) {
  365. $login = vf($login);
  366. $email = mysql_real_escape_string($email);
  367. $query = "UPDATE `emails` SET `email` = '" . $email . "' WHERE `login`= '" . $login . "' ;";
  368. nr_query($query);
  369. log_register('CHANGE UserEmail (' . $login . ') ' . $email);
  370. zb_UserGetAllDataCacheClean();
  371. }
  372. /**
  373. * Creates user contract database record
  374. *
  375. * @param string $login existing user login
  376. * @param string $contract contract field to set
  377. */
  378. function zb_UserCreateContract($login, $contract) {
  379. $login = vf($login);
  380. $contract = mysql_real_escape_string($contract);
  381. $query = "INSERT INTO `contracts` (`id`,`login`,`contract`) VALUES (NULL, '" . $login . "','" . $contract . "');";
  382. nr_query($query);
  383. log_register('CREATE UserContract (' . $login . ') `' . $contract . '`');
  384. zb_UserGetAllDataCacheClean();
  385. }
  386. /**
  387. * Deletes user contract record from database
  388. *
  389. * @param string $login existing user login
  390. */
  391. function zb_UserDeleteContract($login) {
  392. $login = vf($login);
  393. $query = "DELETE from `contracts` WHERE `login` = '" . $login . "';";
  394. nr_query($query);
  395. log_register('DELETE UserContract (' . $login . ')');
  396. zb_UserGetAllDataCacheClean();
  397. }
  398. /**
  399. * Returns user contract from database
  400. *
  401. * @param string $login existing user login
  402. * @return string
  403. */
  404. function zb_UserGetContract($login) {
  405. $login = vf($login);
  406. $query = "SELECT `contract` from `contracts` WHERE `login`='" . $login . "'";
  407. $contract_arr = simple_query($query);
  408. return ($contract_arr['contract']);
  409. }
  410. /**
  411. * Changes new contract for some user in database
  412. *
  413. * @param string $login existing user login
  414. * @param string $contract user contract to set
  415. */
  416. function zb_UserChangeContract($login, $contract) {
  417. $login = vf($login);
  418. $contract = mysql_real_escape_string($contract);
  419. $query = "UPDATE `contracts` SET `contract` = '" . $contract . "' WHERE `login`= '" . $login . "' ;";
  420. nr_query($query);
  421. log_register('CHANGE UserContract (' . $login . ') `' . $contract . '`');
  422. zb_UserGetAllDataCacheClean();
  423. }
  424. /**
  425. * Returns all contracts as array contract=>login
  426. *
  427. * @return array
  428. */
  429. function zb_UserGetAllContracts() {
  430. $result = array();
  431. $query = "SELECT * from `contracts`";
  432. $allcontracts = simple_queryall($query);
  433. if (!empty($allcontracts)) {
  434. foreach ($allcontracts as $io => $eachcontract) {
  435. $result[$eachcontract['contract']] = $eachcontract['login'];
  436. }
  437. }
  438. return ($result);
  439. }
  440. /**
  441. * Returns all contracts as array login=>contract
  442. *
  443. * @return array
  444. */
  445. function zb_UserGetAllLoginContracts() {
  446. $result = array();
  447. $query = "SELECT * from `contracts`";
  448. $allcontracts = simple_queryall($query);
  449. if (!empty($allcontracts)) {
  450. foreach ($allcontracts as $io => $eachcontract) {
  451. $result[$eachcontract['login']] = $eachcontract['contract'];
  452. }
  453. }
  454. return ($result);
  455. }
  456. /**
  457. * Returns all of available emails records as login=>email array
  458. *
  459. * @return array
  460. */
  461. function zb_UserGetAllEmails() {
  462. $result = array();
  463. $query = "SELECT * from `emails`";
  464. $all = simple_queryall($query);
  465. if (!empty($all)) {
  466. foreach ($all as $io => $each) {
  467. $result[$each['login']] = $each['email'];
  468. }
  469. }
  470. return ($result);
  471. }
  472. /**
  473. * Returns stargazer user data array
  474. *
  475. * @param string $login existing user login
  476. * @return array
  477. */
  478. function zb_UserGetStargazerData($login) {
  479. $login = vf($login, 4);
  480. $query = "SELECT * from `users` where `login`='" . $login . "'";
  481. $userdata = simple_query($query);
  482. return ($userdata);
  483. }
  484. /**
  485. * Returns array of all available stargazer users data
  486. *
  487. * @return array
  488. */
  489. function zb_UserGetAllStargazerData() {
  490. $query = "SELECT * from `users`";
  491. $userdata = simple_queryall($query);
  492. return ($userdata);
  493. }
  494. /**
  495. * Returns array of all available stargazer users data as login=>data array
  496. *
  497. * @return array
  498. */
  499. function zb_UserGetAllStargazerDataAssoc() {
  500. $query = "SELECT * from `users`";
  501. $userdata = simple_queryall($query);
  502. $result = array();
  503. if (!empty($userdata)) {
  504. foreach ($userdata as $io => $each) {
  505. $result[$each['login']] = $each;
  506. }
  507. }
  508. return ($result);
  509. }
  510. /**
  511. * Returns array of all available stargazer user logins
  512. *
  513. * @return array
  514. */
  515. function zb_UserGetAllStargazerLogins() {
  516. $result = array();
  517. $query = "SELECT `login` from `users`";
  518. $all = simple_queryall($query);
  519. if (!empty($all)) {
  520. foreach ($all as $io => $each) {
  521. $result[] = $each['login'];
  522. }
  523. }
  524. return ($result);
  525. }
  526. /**
  527. * Returns all users actual balance from database as array login=>cash
  528. *
  529. * @return array
  530. */
  531. function zb_UserGetAllBalance() {
  532. $result = array();
  533. $query = "SELECT `login`,`Cash` from `users`";
  534. $all = simple_queryall($query);
  535. if (!empty($all)) {
  536. foreach ($all as $io => $each) {
  537. $result[$each['login']] = $each['Cash'];
  538. }
  539. }
  540. return ($result);
  541. }
  542. /**
  543. * Returns user speed override database field for some login
  544. *
  545. * @param string $login existing user login
  546. * @return int
  547. */
  548. function zb_UserGetSpeedOverride($login) {
  549. $login = vf($login);
  550. $query = "SELECT `speed` from `userspeeds` where `login`='" . $login . "'";
  551. $speed = simple_query($query);
  552. if (!empty($speed['speed'])) {
  553. $speed = $speed['speed'];
  554. } else {
  555. $speed = 0;
  556. }
  557. return ($speed);
  558. }
  559. /**
  560. * Creates speed override database record for some user
  561. *
  562. * @param string $login existing user login
  563. * @param int $speed speed override to set
  564. */
  565. function zb_UserCreateSpeedOverride($login, $speed) {
  566. $login = vf($login);
  567. $speed = vf($speed, 3);
  568. $query = "INSERT INTO `userspeeds` (`id` ,`login` ,`speed`) VALUES (NULL , '" . $login . "', '" . $speed . "');";
  569. nr_query($query);
  570. log_register('CREATE UserSpeedOverride (' . $login . ') `' . $speed . '`');
  571. }
  572. /**
  573. * Deletes speed override database record for some user
  574. *
  575. * @param string $login existing user login
  576. */
  577. function zb_UserDeleteSpeedOverride($login) {
  578. $login = vf($login);
  579. $query = "DELETE from `userspeeds` WHERE `login`='" . $login . "'";
  580. nr_query($query);
  581. log_register('DELETE UserSpeedOverride (' . $login . ')');
  582. }
  583. /**
  584. * Creates user notes database record for some login
  585. *
  586. * @param string $login existing user login
  587. * @param string $notes user notes to set
  588. */
  589. function zb_UserCreateNotes($login, $notes) {
  590. $login = vf($login);
  591. $notes = mysql_real_escape_string($notes);
  592. $query = "INSERT INTO `notes` (`id` , `login` ,`note`) VALUES (NULL , '" . $login . "', '" . $notes . "');";
  593. nr_query($query);
  594. log_register('CREATE UserNote (' . $login . ') `' . $notes . '`');
  595. }
  596. /**
  597. * Deletes user notes database record for some login
  598. *
  599. * @param string $login existing user login
  600. */
  601. function zb_UserDeleteNotes($login) {
  602. $login = vf($login);
  603. $query = "DELETE FROM `notes` WHERE `login`='" . $login . "'";
  604. nr_query($query);
  605. log_register('DELETE UserNote (' . $login . ')');
  606. }
  607. /**
  608. * Returns user notes database field for some user
  609. *
  610. * @param string $login existing user login
  611. * @return string
  612. */
  613. function zb_UserGetNotes($login) {
  614. $login = vf($login);
  615. $query = "SELECT `note` from `notes` WHERE `login`='" . $login . "'";
  616. $result = simple_query($query);
  617. $result = @$result['note'];
  618. return ($result);
  619. }
  620. /**
  621. * Returns all of user tariffs as login=>tariff array
  622. *
  623. * @return array
  624. */
  625. function zb_TariffsGetAllUsers() {
  626. $query = "SELECT `login`,`Tariff` from `users`";
  627. $result = array();
  628. $alltariffuserspairs = simple_queryall($query);
  629. if (!empty($alltariffuserspairs)) {
  630. foreach ($alltariffuserspairs as $io => $eachuser) {
  631. $result[$eachuser['login']] = $eachuser['Tariff'];
  632. }
  633. }
  634. return ($result);
  635. }
  636. /**
  637. * Retunrs all of user LastActivityTimes as login=>LAT array
  638. *
  639. * @return array
  640. */
  641. function zb_LatGetAllUsers() {
  642. $query = "SELECT `login`,`LastActivityTime` from `users`";
  643. $result = array();
  644. $allpairs = simple_queryall($query);
  645. if (!empty($allpairs)) {
  646. foreach ($allpairs as $io => $eachuser) {
  647. $result[$eachuser['login']] = $eachuser['LastActivityTime'];
  648. }
  649. }
  650. return ($result);
  651. }
  652. /**
  653. * Returns array of all user cash data as array login=>cash
  654. *
  655. * @return array
  656. */
  657. function zb_CashGetAllUsers() {
  658. $query = "SELECT `login`,`Cash` from `users`";
  659. $result = array();
  660. $allcashuserspairs = simple_queryall($query);
  661. if (!empty($allcashuserspairs)) {
  662. foreach ($allcashuserspairs as $io => $eachuser) {
  663. $result[$eachuser['login']] = $eachuser['Cash'];
  664. }
  665. }
  666. return ($result);
  667. }
  668. /**
  669. * returns all of user cash and logins pairs as login=>credit
  670. *
  671. * @return array
  672. */
  673. function zb_CreditGetAllUsers() {
  674. $query = "SELECT `login`,`Credit` from `users`";
  675. $result = array();
  676. $alldata = simple_queryall($query);
  677. if (!empty($alldata)) {
  678. foreach ($alldata as $io => $eachuser) {
  679. $result[$eachuser['login']] = $eachuser['Credit'];
  680. }
  681. }
  682. return ($result);
  683. }
  684. /**
  685. * Returns price of tariff by its name
  686. *
  687. * @param string $tariff
  688. * @return float
  689. */
  690. function zb_TariffGetPrice($tariff) {
  691. $result = 0;
  692. $tariff = mysql_real_escape_string($tariff);
  693. $query = "SELECT `Fee` from `tariffs` WHERE `name`='" . $tariff . "'";
  694. $res = simple_query($query);
  695. if (isset($res['Fee'])) {
  696. $result = $res['Fee'];
  697. }
  698. return ($result);
  699. }
  700. /**
  701. * Returns full data of tariff by its name
  702. *
  703. * @param string $tariff
  704. * @return array
  705. */
  706. function zb_TariffGetData($tariff) {
  707. $tariff = mysql_real_escape_string($tariff);
  708. $query = "SELECT * from `tariffs` WHERE `name`='" . $tariff . "'";
  709. $result = simple_query($query);
  710. return ($result);
  711. }
  712. /**
  713. * Returns full data of all tariffs as name=>data
  714. *
  715. * @return array
  716. */
  717. function zb_TariffGetAllData() {
  718. $result = array();
  719. $query = "SELECT * from `tariffs`";
  720. $all = simple_queryall($query);
  721. if (!empty($all)) {
  722. foreach ($all as $io => $each) {
  723. $result[$each['name']] = $each;
  724. }
  725. }
  726. return ($result);
  727. }
  728. /**
  729. * Returns list of all available tariffs with its prices as array tariff=>fee
  730. *
  731. * @return array
  732. */
  733. function zb_TariffGetPricesAll() {
  734. $query = "SELECT `name`,`Fee` from `tariffs`";
  735. $allprices = simple_queryall($query);
  736. $result = array();
  737. if (!empty($allprices)) {
  738. foreach ($allprices as $io => $eachtariff) {
  739. $result[$eachtariff['name']] = $eachtariff['Fee'];
  740. }
  741. }
  742. return ($result);
  743. }
  744. /**
  745. * Returns array of all available user notes as login=>note
  746. *
  747. * @return array
  748. */
  749. function zb_UserGetAllNotes() {
  750. $result = array();
  751. $query = "SELECT * from `notes`";
  752. $all = simple_queryall($query);
  753. if (!empty($all)) {
  754. foreach ($all as $io => $each) {
  755. $result[$each['login']] = $each['note'];
  756. }
  757. }
  758. return ($result);
  759. }
  760. /**
  761. * Temporary resurrects user if he is inactive by some reasons
  762. *
  763. * @param string $login
  764. *
  765. * @return void
  766. */
  767. function zb_UserResurrect($login) {
  768. global $billing;
  769. $userData = zb_UserGetStargazerData($login);
  770. $resurrectFlag = false;
  771. $resurrectType = array();
  772. if (!empty($userData)) {
  773. //user manually disabled?
  774. if ($userData['Down'] == 1) {
  775. $resurrectFlag = true;
  776. $resurrectType['DOWN'] = 'DOWN';
  777. $billing->setdown($login, 0);
  778. }
  779. //user frozen at this moment
  780. if ($userData['Passive'] == 1) {
  781. $resurrectFlag = true;
  782. $resurrectType['PASSIVE'] = 'PASSIVE';
  783. $billing->setpassive($login, 0);
  784. }
  785. //user AlwaysOnline flag disabled
  786. if ($userData['AlwaysOnline'] == 0) {
  787. $resurrectFlag = true;
  788. $resurrectType['AO'] = 'AO';
  789. $billing->setao($login, 1);
  790. }
  791. if ($userData['Cash'] < '-' . $userData['Credit']) {
  792. $resurrectFlag = true;
  793. $resurrectType['CASH'] = 'CASH';
  794. $currentCreditValue = $userData['Credit'];
  795. $tmpCreditValue = abs($userData['Cash']) + 1; //prevent float cash value issues
  796. $billing->setcredit($login, $tmpCreditValue);
  797. }
  798. //back user data to original state
  799. if ($resurrectFlag) {
  800. if (isset($resurrectType['DOWN'])) {
  801. $billing->setdown($login, 1);
  802. }
  803. if (isset($resurrectType['PASSIVE'])) {
  804. $billing->setpassive($login, 1);
  805. }
  806. if (isset($resurrectType['AO'])) {
  807. $billing->setao($login, 0);
  808. }
  809. if (isset($resurrectType['CASH'])) {
  810. sleep(1); //dont back credit to fast
  811. $billing->setcredit($login, $currentCreditValue);
  812. }
  813. log_register('RESURRECT (' . $login . ') ' . implode(',', $resurrectType));
  814. }
  815. }
  816. }
  817. /**
  818. * Returns all users phones data from cache
  819. *
  820. * @return array
  821. */
  822. function zb_GetAllAllPhonesCache() {
  823. global $ubillingConfig;
  824. $result = '';
  825. $cache = new UbillingCache();
  826. $cacheTime = ($ubillingConfig->getAlterParam('ALL_PHONES_CACHE_TIMEOUT')) ? $ubillingConfig->getAlterParam('ALL_PHONES_CACHE_TIMEOUT') : 1800;
  827. $result = $cache->getCallback(
  828. 'USER_ALL_PHONES_DATA',
  829. function () {
  830. return (zb_GetAllAllPhones());
  831. },
  832. $cacheTime
  833. );
  834. return ($result);
  835. }
  836. /**
  837. * Returns all users phones data, including external mobiles
  838. *
  839. * @param string $login
  840. * @return array
  841. */
  842. function zb_GetAllAllPhones($login = '') {
  843. global $ubillingConfig;
  844. $useExtMobiles = $ubillingConfig->getAlterParam('MOBILES_EXT');
  845. $phones = array();
  846. $allExt = array();
  847. if (!empty($login)) {
  848. $where1 = " WHERE `phones`.`login` = '" . $login . "'";
  849. $where2 = " WHERE `mobileext`.`login` = '" . $login . "'";
  850. } else {
  851. $where1 = '';
  852. $where2 = '';
  853. }
  854. $queryPhones = "SELECT `login`, `phone`,`mobile` FROM `phones`" . $where1;
  855. $resultPhones = simple_queryall($queryPhones);
  856. if ($useExtMobiles) {
  857. $queryLogin = "SELECT DISTINCT `login` FROM `mobileext` " . $where2 . " ORDER BY `login`";
  858. $qlResult = simple_queryall($queryLogin);
  859. if (!empty($qlResult)) {
  860. foreach ($qlResult as $io => $each) {
  861. $allExt[$each['login']] = array();
  862. }
  863. $query = "SELECT `login`, `mobile` FROM `mobileext` " . $where2 . " ORDER BY `login`";
  864. $all = simple_queryall($query);
  865. if (!empty($all)) {
  866. foreach ($all as $io => $each) {
  867. $allExt[$each['login']][] = $each['mobile'];
  868. }
  869. }
  870. }
  871. }
  872. if (!empty($resultPhones)) {
  873. foreach ($resultPhones as $phone) {
  874. $phones[$phone['login']]['phone'] = $phone['phone'];
  875. $phones[$phone['login']]['mobile'] = $phone['mobile'];
  876. $phones[$phone['login']]['mobiles'] = (!empty($allExt) and isset($allExt[$phone['login']])) ? $allExt[$phone['login']] : array();
  877. }
  878. }
  879. return ($phones);
  880. }
  881. /**
  882. * Returns all users phones as a "login" => "phones string" for Online table
  883. *
  884. * @return array
  885. */
  886. function zb_GetAllOnlineTabPhones() {
  887. $allOnlineTabPhones = array();
  888. $allUsersPhones = zb_GetAllAllPhonesCache();
  889. if (!empty($allUsersPhones)) {
  890. foreach ($allUsersPhones as $eachLogin => $eachItem) {
  891. $allOnlineTabPhones[$eachLogin] = zb_GetOnlineTabPhonesStr($eachItem['phone'], $eachItem['mobile'], $eachItem['mobiles']);
  892. }
  893. }
  894. return ($allOnlineTabPhones);
  895. }
  896. /**
  897. * Creates a "phones string" suitable for Online tab from given $phone, $mobile and $extMobiles
  898. *
  899. * @param string $phone
  900. * @param string $mobile
  901. * @param array $extMobiles
  902. * @return string
  903. */
  904. function zb_GetOnlineTabPhonesStr($phone = '', $mobile = '', $extMobiles = array()) {
  905. $phonesStr = (empty($phone)) ? '' : str_ireplace(array('+', "-"), '', $phone) . ' ';
  906. $phonesStr .= (empty($mobile)) ? '' : str_ireplace(array('+', "-"), '', $mobile);
  907. if (!empty($extMobiles)) {
  908. $phonesStr .= '<br />';
  909. foreach ($extMobiles as $io => $eachmobile) {
  910. $phonesStr .= (empty($eachmobile)) ? '' : str_ireplace(array('+', "-"), '', $eachmobile) . ' ';
  911. }
  912. }
  913. return ($phonesStr);
  914. }
  915. /**
  916. * Tries to get user's PPPoE data from Mikrotik NAS via API
  917. *
  918. * @param $login
  919. * @param false $returnHTML
  920. * @param false $returnInSpoiler
  921. * @param false $spoilerClosed
  922. *
  923. * @return array|string
  924. */
  925. function zb_GetROSPPPoESessionInfo($login, $returnHTML = false, $returnInSpoiler = false, $spoilerClosed = false) {
  926. $nasInfo = getNASInfoByLogin($login);
  927. $pppoeInfo = array(
  928. 'errorcode' => 0,
  929. 'lastloggedout' => __('Current user login was not found on NAS') . ': ' . $nasInfo['nasip'],
  930. 'sessionuptime' => __('No active session was found for current user on NAS') . ': ' . $nasInfo['nasip'],
  931. 'lastlinkup' => __('No data'),
  932. 'txmb' => '0',
  933. 'rxmb' => '0',
  934. 'addrlist' => array(__('No data'))
  935. );
  936. if (!empty($nasInfo) and $nasInfo['nastype'] == 'mikrotik') {
  937. $rosAPI = new RouterOS();
  938. $nasOpts = unserialize(base64_decode($nasInfo['options']));
  939. $useNewConnType = (isset($nasOpts['use_new_conn_mode']) && $nasOpts['use_new_conn_mode']);
  940. if ($rosAPI->connect($nasInfo['nasip'], $nasOpts['username'], $nasOpts['password'], $useNewConnType)) {
  941. $pppoeSecret = $rosAPI->command(
  942. '/ppp/secret/print',
  943. array(
  944. '.proplist' => '.id,last-logged-out',
  945. '?name' => trim($login)
  946. )
  947. );
  948. // if such pppoe user even exists
  949. if (!empty($pppoeSecret[0]['.id'])) {
  950. $pppoeInfo['lastloggedout'] = date('Y-m-d H:i:s', strtotime(str_ireplace('/', ' ', $pppoeSecret[0]['last-logged-out'])));
  951. $activeSession = $rosAPI->command(
  952. '/ppp/active/print',
  953. array(
  954. '.proplist' => '.id,uptime',
  955. '?name' => trim($login)
  956. )
  957. );
  958. // if an active pppoe session exists for this user
  959. if (!empty($activeSession[0]['.id'])) {
  960. $pppoeInfo['sessionuptime'] = $activeSession[0]['uptime'];
  961. $ifaceData = $rosAPI->command(
  962. '/interface/print',
  963. array(
  964. '.proplist' => '.id,last-link-up-time,tx-byte,rx-byte',
  965. '?name' => '<pppoe-' . trim($login) . '>'
  966. )
  967. );
  968. if (!empty($ifaceData[0]['.id'])) {
  969. $pppoeInfo['lastlinkup'] = date('Y-m-d H:i:s', strtotime(str_ireplace('/', ' ', $ifaceData[0]['last-link-up-time'])));
  970. $pppoeInfo['txmb'] = stg_convert_size($ifaceData[0]['tx-byte']);
  971. $pppoeInfo['rxmb'] = stg_convert_size($ifaceData[0]['rx-byte']);
  972. }
  973. }
  974. // getting user's address lists and their status
  975. $addrList = $rosAPI->command(
  976. '/ip/firewall/address-list/print',
  977. array(
  978. '.proplist' => '.id,list,disabled',
  979. '?comment' => trim($login),
  980. '?address' => $nasInfo['ip']
  981. )
  982. );
  983. if (!empty($addrList)) {
  984. $pppoeInfo['addrlist'] = array();
  985. foreach ($addrList as $eachList) {
  986. $pppoeInfo['addrlist'][] = $eachList['list'] . ' -> ' . (wf_getBoolFromVar($eachList['disabled'], true) ? 'disabled' : 'enabled');
  987. }
  988. }
  989. }
  990. } else {
  991. $pppoeInfo['errorcode'] = 2;
  992. }
  993. } else {
  994. $pppoeInfo['errorcode'] = 1;
  995. }
  996. if ($returnHTML) {
  997. $rows = '';
  998. if ($pppoeInfo['errorcode'] !== 0) {
  999. $errorStr = ($pppoeInfo['errorcode'] == 1) ? __('User has no network and NAS assigned or user\'s NAS is not of type "Mikrotik"') : __('Unable to connect to user\'s NAS' . ': ' . $nasInfo['nasip']);
  1000. $cells = wf_TableCell(__('Error while getting data'), '20%', 'row2');
  1001. $cells .= wf_TableCell($errorStr);
  1002. $rows .= wf_TableRow($cells, 'row3');
  1003. }
  1004. $cells = wf_TableCell('PPPoE: ' . __('last logged out'), '30%', 'row2');
  1005. $cells .= wf_TableCell($pppoeInfo['lastloggedout']);
  1006. $rows .= wf_TableRow($cells, 'row3');
  1007. $cells = wf_TableCell('PPPoE: ' . __('session uptime'), '30%', 'row2');
  1008. $cells .= wf_TableCell($pppoeInfo['sessionuptime']);
  1009. $rows .= wf_TableRow($cells, 'row3');
  1010. $cells = wf_TableCell('PPPoE: ' . __('last link up time'), '30%', 'row2');
  1011. $cells .= wf_TableCell($pppoeInfo['lastlinkup']);
  1012. $rows .= wf_TableRow($cells, 'row3');
  1013. $cells = wf_TableCell('PPPoE: Tx/Rx, Mb', '30%', 'row2');
  1014. $cells .= wf_TableCell($pppoeInfo['txmb'] . ' / ' . $pppoeInfo['rxmb']);
  1015. $rows .= wf_TableRow($cells, 'row3');
  1016. $cells = wf_TableCell(__('Current user\'s address lists'), '30%', 'row2');
  1017. if (!empty($pppoeInfo['addrlist'])) {
  1018. $addrList = $pppoeInfo['addrlist'];
  1019. foreach ($addrList as $item) {
  1020. $cells .= wf_TableCell($item . wf_delimiter(0));
  1021. }
  1022. } else {
  1023. $cells .= '';
  1024. }
  1025. $rows .= wf_TableRow($cells, 'row3');
  1026. $table = wf_TableBody($rows, '88%', 0, '', 'style="margin: 0 auto;"');
  1027. if ($returnInSpoiler) {
  1028. $table = wf_Spoiler($table, 'PPPoE: ' . __('session info'), $spoilerClosed, '', '', '', '', 'style="margin: 10px auto;"');
  1029. }
  1030. return ($table);
  1031. } else {
  1032. return ($pppoeInfo);
  1033. }
  1034. }
  1035. /**
  1036. * Returns spoiler block with user's active PPPoE session data
  1037. *
  1038. * @param $login
  1039. * @param $moduleURL
  1040. *
  1041. * @return string
  1042. */
  1043. function zb_RenderROSPPPoESessionInfo($login, $moduleURL) {
  1044. $InfoButtonID = 'InfID_' . $login;
  1045. $InfoBlockID = 'InfBlck_' . $login;
  1046. $PPPoEInfoBlock = wf_tag('div', false, '', 'id="' . $InfoBlockID . '"');
  1047. $PPPoEInfoBlock .= '';
  1048. $PPPoEInfoBlock .= wf_tag('div', true);
  1049. $PPPoEInfoButton = wf_tag('a', false, '', 'href="#" id="' . $InfoButtonID . '" title="' . 'PPPoE: ' . __('get session info for current user') . '"');
  1050. $PPPoEInfoButton .= wf_tag('img', false, '', 'src="skins/icn_alert_info.png" border="0" style="vertical-align: bottom;"');
  1051. $PPPoEInfoButton .= wf_tag('a', true);
  1052. $PPPoEInfoButton .= wf_tag('script', false, '', 'type="text/javascript"');
  1053. $PPPoEInfoButton .= '$(\'#' . $InfoButtonID . '\').click(function(evt) {
  1054. $(\'img\', this).toggleClass("image_rotate");
  1055. getPPPoEInfo("' . $login . '", "#' . $InfoBlockID . '", true, false, ' . $InfoButtonID . ');
  1056. evt.preventDefault();
  1057. return false;
  1058. });';
  1059. $PPPoEInfoButton .= wf_tag('script', true);
  1060. $result = wf_Spoiler($PPPoEInfoBlock, $PPPoEInfoButton . wf_nbsp(2) . 'PPPoE: ' . __('session info'), true, '', '', '', '', 'style="margin: 10px auto;"');
  1061. $result .= wf_tag('script', false, '', 'type="text/javascript"');
  1062. $result .= 'function getPPPoEInfo(userLogin, InfoBlckSelector, ReturnHTML = false, InSpoiler = false, RefreshButtonSelector) {
  1063. $.ajax({
  1064. type: "POST",
  1065. url: "' . $moduleURL . '",
  1066. data: { GetPPPoEInfo:true,
  1067. usrlogin:userLogin,
  1068. returnAsHTML:ReturnHTML,
  1069. returnInSpoiler:InSpoiler
  1070. },
  1071. success: function(result) {
  1072. if ($.type(RefreshButtonSelector) === \'string\') {
  1073. $("#"+RefreshButtonSelector).find(\'img\').toggleClass("image_rotate");
  1074. } else {
  1075. $(RefreshButtonSelector).find(\'img\').toggleClass("image_rotate");
  1076. }
  1077. var InfoBlck = $(InfoBlckSelector);
  1078. if ( !InfoBlck.length || !(InfoBlck instanceof jQuery)) {return false;}
  1079. $(InfoBlck).html(result);
  1080. }
  1081. });
  1082. }
  1083. ';
  1084. $result .= wf_tag('script', true);
  1085. return $result;
  1086. }
  1087. /**
  1088. * Parses CONTRACT_GEN_TEMPLATE and tries to get 3 parts of contracts template
  1089. *
  1090. * @return array
  1091. */
  1092. function zb_GetBaseContractTemplateSplitted() {
  1093. global $ubillingConfig;
  1094. $contractTemplateStr = $ubillingConfig->getAlterParam('CONTRACT_GEN_TEMPLATE', '');
  1095. $contractTemplateSplitted = array();
  1096. if (!empty($contractTemplateStr)) {
  1097. preg_match('/(.*)' . '{/msiu', $contractTemplateStr, $beforeResult);
  1098. preg_match('/{' . '(.*?)' . '}/msiu', $contractTemplateStr, $delimResult);
  1099. preg_match('/}' . '(.*)/msiu', $contractTemplateStr, $afterResult);
  1100. $startContractPart = (empty($beforeResult[1]) ? '' : $beforeResult[1]);
  1101. $digitContractPart = (empty($delimResult[1]) ? '' : $delimResult[1]);
  1102. $endContractPart = (empty($afterResult[1]) ? '' : $afterResult[1]);
  1103. $contractTemplateSplitted = array($startContractPart, $endContractPart, $digitContractPart);
  1104. }
  1105. return ($contractTemplateSplitted);
  1106. }
  1107. /**
  1108. * Generates digits contract block
  1109. * according to CONTRACT_GEN_TEMPLATE and CONTRACT_GEN_TEMPLATE_LEADING_ZEROES options values
  1110. *
  1111. * @param int $digitsBlockLength
  1112. * @param int $contractNumber
  1113. * @param false $makeIncrement
  1114. *
  1115. * @return string
  1116. */
  1117. function zb_GenContractDigitBlock($digitsBlockLength, $contractNumber, $makeIncrement = false) {
  1118. global $ubillingConfig;
  1119. $contractLeadingZeroes = $ubillingConfig->getAlterParam('CONTRACT_GEN_TEMPLATE_LEADING_ZEROES', 1);
  1120. $contractDigits = '';
  1121. if (!empty($digitsBlockLength) and !empty($contractNumber)) {
  1122. $contractNumber = ($makeIncrement) ? ++$contractNumber : $contractNumber;
  1123. $contractDigits = ($contractLeadingZeroes ? sprintf('%0' . $digitsBlockLength . 's', $contractNumber) : sprintf('%-0' . $digitsBlockLength . 's', $contractNumber));
  1124. }
  1125. return ($contractDigits);
  1126. }
  1127. /**
  1128. * Extracts digital part of contract from a contract string
  1129. * according to CONTRACT_GEN_TEMPLATE and CONTRACT_GEN_TEMPLATE_LEADING_ZEROES options values
  1130. *
  1131. * @param $contract
  1132. * @param $digitBlockLength
  1133. * @param false $makeIncrement
  1134. *
  1135. * @return int|string
  1136. */
  1137. function zb_ExtractContractDigitPart($contract, $digitBlockLength, $makeIncrement = false) {
  1138. global $ubillingConfig;
  1139. $contractLeadingZeroes = $ubillingConfig->getAlterParam('CONTRACT_GEN_TEMPLATE_LEADING_ZEROES', 1);
  1140. $digtsStr = '';
  1141. preg_match('/\d{' . $digitBlockLength . '}/msiu', $contract, $matchResult);
  1142. if (!empty($matchResult[0])) {
  1143. $digtsStr = $matchResult[0];
  1144. if ($makeIncrement) {
  1145. $numberPart = ($contractLeadingZeroes ? (int) $digtsStr : (int) str_replace('0', '', $digtsStr));
  1146. $digtsStr = zb_GenContractDigitBlock($digitBlockLength, $numberPart, $makeIncrement);
  1147. }
  1148. }
  1149. return ($digtsStr);
  1150. }
  1151. /**
  1152. * Explodes contract template for digital block dividing it to digit block length and starting base contract number
  1153. * May return only block length
  1154. *
  1155. * @param string $digitContractTplPart
  1156. * @param false $returnOnlyLength
  1157. *
  1158. * @return array|string
  1159. */
  1160. function zb_GetContractDigitBlockTplParams($digitContractTplPart, $returnOnlyLength = false) {
  1161. $contractDigitBlockParams = array();
  1162. if (!empty($digitContractTplPart)) {
  1163. $contractDigitBlockParams = explode(',', $digitContractTplPart);
  1164. }
  1165. // first element is a contract digit block length
  1166. // second element is a "starting from" digit for very first base contract
  1167. return (($returnOnlyLength) ? $contractDigitBlockParams[0] : $contractDigitBlockParams);
  1168. }
  1169. /**
  1170. * Returns base contract value generated from contract template
  1171. * accoreding to CONTRACT_GEN_TEMPLATE and CONTRACT_GEN_TEMPLATE_LEADING_ZEROES options values
  1172. *
  1173. * @param array $templateSplitted
  1174. *
  1175. * @return string
  1176. */
  1177. function zb_GenBaseContractFromTemplate($templateSplitted = array()) {
  1178. $templateSplitted = empty($templateSplitted) ? zb_GetBaseContractTemplateSplitted() : $templateSplitted;
  1179. $startContractTplPart = $templateSplitted[0];
  1180. $endContractTplPart = $templateSplitted[1];
  1181. $digitContractTplPart = $templateSplitted[2];
  1182. $baseContract = '';
  1183. $digitsBlockLength = 0;
  1184. $contractNumber = 0;
  1185. $digitContractTplPart = zb_GetContractDigitBlockTplParams($digitContractTplPart);
  1186. if (!empty($digitContractTplPart)) {
  1187. // first element is a contract digits block length
  1188. // second element is a "starting from" digit for very first base contract
  1189. $digitsBlockLength = $digitContractTplPart[0];
  1190. $contractNumber = $digitContractTplPart[1];
  1191. }
  1192. $baseContract = $startContractTplPart . zb_GenContractDigitBlock($digitsBlockLength, $contractNumber) . $endContractTplPart;
  1193. return ($baseContract);
  1194. }
  1195. /**
  1196. * Check is user active depends on his Stargazer data array
  1197. *
  1198. * @param array $userData
  1199. *
  1200. * @return bool
  1201. */
  1202. function zb_UserIsActive($userData) {
  1203. $result = false;
  1204. if (!empty($userData)) {
  1205. if (($userData['Cash'] >= '-' . $userData['Credit']) and ($userData['AlwaysOnline'] == 1) and ($userData['Passive'] == 0) and ($userData['Down'] == 0)) {
  1206. $result = true;
  1207. }
  1208. }
  1209. return ($result);
  1210. }
  1211. /**
  1212. * Check is user active/dead or frozen depends on his Stargazer data array.
  1213. *
  1214. * @param array $userData
  1215. *
  1216. * @return int 1 - active, 0 - dead, -1 - frozen
  1217. */
  1218. function zb_UserIsAlive($userData) {
  1219. $result = 0;
  1220. if (!empty($userData)) {
  1221. if (($userData['Cash'] >= '-' . $userData['Credit']) and ($userData['AlwaysOnline'] == 1) and ($userData['Passive'] == 0) and ($userData['Down'] == 0)) {
  1222. $result = 1;
  1223. }
  1224. //just frozen
  1225. if ($userData['Passive']) {
  1226. $result = -1;
  1227. }
  1228. }
  1229. return ($result);
  1230. }
  1231. /**
  1232. * Creates contract date with some contract (TODO: remove this legacy function)
  1233. *
  1234. * @param $contract - existing contract
  1235. * @param $date - contract creation date in datetime format
  1236. * @return void
  1237. */
  1238. function zb_UserContractDateCreate($contract, $date) {
  1239. $contractDates = new ContractDates();
  1240. $contractDates->set($contract, $date);
  1241. }
  1242. /**
  1243. * Create users passport data struct
  1244. *
  1245. * @param $login - user login
  1246. * @param $birthdate - user date of birth
  1247. * @param $passportnum - passport number
  1248. * @param $passportdate - passport assign date
  1249. * @param $passportwho - who produce the passport?
  1250. * @param $pcity - additional address city
  1251. * @param $pstreet - additional address street
  1252. * @param $pbuild - additional address build
  1253. * @param $papt - additional address apartment
  1254. * @param $pinn - additional Identification code
  1255. *
  1256. * @return void
  1257. */
  1258. function zb_UserPassportDataCreate($login, $birthdate, $passportnum, $passportdate, $passportwho, $pcity, $pstreet, $pbuild, $papt, $pinn = '') {
  1259. $login = mysql_real_escape_string($login);
  1260. $birthdate = mysql_real_escape_string($birthdate);
  1261. $passportnum = mysql_real_escape_string($passportnum);
  1262. $passportdate = mysql_real_escape_string($passportdate);
  1263. $passportwho = mysql_real_escape_string($passportwho);
  1264. $pcity = mysql_real_escape_string($pcity);
  1265. $pstreet = mysql_real_escape_string($pstreet);
  1266. $pbuild = mysql_real_escape_string($pbuild);
  1267. $papt = mysql_real_escape_string($papt);
  1268. $pinn = mysql_real_escape_string($pinn);
  1269. $query = "
  1270. INSERT INTO `passportdata` (
  1271. `id` ,
  1272. `login` ,
  1273. `birthdate` ,
  1274. `passportnum` ,
  1275. `passportdate` ,
  1276. `passportwho` ,
  1277. `pcity` ,
  1278. `pstreet` ,
  1279. `pbuild` ,
  1280. `papt`,
  1281. `pinn`
  1282. )
  1283. VALUES (
  1284. NULL ,
  1285. '" . $login . "',
  1286. '" . $birthdate . "',
  1287. '" . $passportnum . "',
  1288. '" . $passportdate . "',
  1289. '" . $passportwho . "',
  1290. '" . $pcity . "',
  1291. '" . $pstreet . "',
  1292. '" . $pbuild . "',
  1293. '" . $papt . "',
  1294. '" . $pinn . "'
  1295. );
  1296. ";
  1297. nr_query($query);
  1298. log_register("CREATE UserPassportData (" . $login . ")");
  1299. }
  1300. /**
  1301. * Update users passport data
  1302. *
  1303. * @param $login - user login
  1304. * @param $birthdate - user date of birth
  1305. * @param $passportnum - passport number
  1306. * @param $passportdate - passport assign date
  1307. * @param $passportwho - who produce the passport?
  1308. * @param $pcity - additional address city
  1309. * @param $pstreet - additional address street
  1310. * @param $pbuild - additional address build
  1311. * @param $papt - additional address apartment
  1312. * @param $pinn - Personal identification code
  1313. *
  1314. * @return void
  1315. */
  1316. function zb_UserPassportDataSet($login, $birthdate, $passportnum, $passportdate, $passportwho, $pcity, $pstreet, $pbuild, $papt, $pinn = '') {
  1317. $login = mysql_real_escape_string($login);
  1318. $birthdate = mysql_real_escape_string($birthdate);
  1319. $passportnum = mysql_real_escape_string($passportnum);
  1320. $passportdate = mysql_real_escape_string($passportdate);
  1321. $passportwho = mysql_real_escape_string($passportwho);
  1322. $pcity = mysql_real_escape_string($pcity);
  1323. $pstreet = mysql_real_escape_string($pstreet);
  1324. $pbuild = mysql_real_escape_string($pbuild);
  1325. $papt = mysql_real_escape_string($papt);
  1326. $pinn = mysql_real_escape_string($pinn);
  1327. $query = "
  1328. UPDATE `passportdata` SET
  1329. `birthdate` = '" . $birthdate . "',
  1330. `passportnum` = '" . $passportnum . "',
  1331. `passportdate` = '" . $passportdate . "',
  1332. `passportwho` = '" . $passportwho . "',
  1333. `pcity` = '" . $pcity . "',
  1334. `pstreet` = '" . $pstreet . "',
  1335. `pbuild` = '" . $pbuild . "',
  1336. `papt` = '" . $papt . "',
  1337. `pinn` = '" . $pinn . "'
  1338. WHERE `login`='" . $login . "'
  1339. ";
  1340. nr_query($query);
  1341. log_register("CHANGE UserPassportData (" . $login . ")");
  1342. }
  1343. /**
  1344. * Gets user passport data
  1345. *
  1346. * @param $login - user login
  1347. *
  1348. * @return array
  1349. */
  1350. function zb_UserPassportDataGet($login) {
  1351. $login = mysql_real_escape_string($login);
  1352. $query = "SELECT * from `passportdata` WHERE `login`='" . $login . "'";
  1353. $passportdata = simple_query($query);
  1354. $result = array();
  1355. if (!empty($passportdata)) {
  1356. $result = $passportdata;
  1357. }
  1358. return ($result);
  1359. }
  1360. /**
  1361. * Get passportdata for all users
  1362. *
  1363. * @return array
  1364. */
  1365. function zb_UserPassportDataGetAll() {
  1366. $query = "SELECT * from `passportdata`";
  1367. $all = simple_queryall($query);
  1368. $result = array();
  1369. if (!empty($all)) {
  1370. foreach ($all as $io => $each) {
  1371. $result[$each['login']]['login'] = $each['login'];
  1372. $result[$each['login']]['birthdate'] = $each['birthdate'];
  1373. $result[$each['login']]['passportnum'] = $each['passportnum'];
  1374. $result[$each['login']]['passportdate'] = $each['passportdate'];
  1375. $result[$each['login']]['passportwho'] = $each['passportwho'];
  1376. $result[$each['login']]['pcity'] = $each['pcity'];
  1377. $result[$each['login']]['pstreet'] = $each['pstreet'];
  1378. $result[$each['login']]['pbuild'] = $each['pbuild'];
  1379. $result[$each['login']]['papt'] = $each['papt'];
  1380. $result[$each['login']]['pinn'] = $each['pinn'];
  1381. }
  1382. }
  1383. return ($result);
  1384. }
  1385. /**
  1386. * Detect user passport data existance and modify it - USE ONLY THIS IN CODE!
  1387. *
  1388. * @param $login - user login
  1389. * @param $birthdate - user date of birth
  1390. * @param $passportnum - passport number
  1391. * @param $passportdate - passport assign date
  1392. * @param $passportwho - who produce the passport?
  1393. * @param $pcity - additional address city
  1394. * @param $pstreet - additional address street
  1395. * @param $pbuild - additional address build
  1396. * @param $papt - additional address apartment
  1397. * @param $pinn - Personal identification code
  1398. *
  1399. * @return void
  1400. */
  1401. function zb_UserPassportDataChange($login, $birthdate, $passportnum, $passportdate, $passportwho, $pcity, $pstreet, $pbuild, $papt, $pinn = '') {
  1402. $exist_q = "SELECT `id` from `passportdata` WHERE `login`='" . mysql_real_escape_string($login) . "'";
  1403. $exist = simple_query($exist_q);
  1404. if (!empty($exist)) {
  1405. // data for this user already exists, just - modify
  1406. zb_UserPassportDataSet($login, $birthdate, $passportnum, $passportdate, $passportwho, $pcity, $pstreet, $pbuild, $papt, $pinn);
  1407. } else {
  1408. //create new
  1409. zb_UserPassportDataCreate($login, $birthdate, $passportnum, $passportdate, $passportwho, $pcity, $pstreet, $pbuild, $papt, $pinn);
  1410. }
  1411. }
  1412. /**
  1413. * Returns users passport data
  1414. *
  1415. * @param string $login
  1416. * @return string
  1417. */
  1418. function web_UserPassportDataShow($login) {
  1419. $login = mysql_real_escape_string($login);
  1420. $passportdata = zb_UserPassportDataGet($login);
  1421. if (!empty($passportdata)) {
  1422. $cells = wf_TableCell(__('Birth date'));
  1423. $cells .= wf_TableCell($passportdata['birthdate']);
  1424. $rows = wf_TableRow($cells, 'row3');
  1425. $cells = wf_TableCell(__('Passport number'));
  1426. $cells .= wf_TableCell($passportdata['passportnum']);
  1427. $rows .= wf_TableRow($cells, 'row3');
  1428. $cells = wf_TableCell(__('Issuing authority'));
  1429. $cells .= wf_TableCell($passportdata['passportwho']);
  1430. $rows .= wf_TableRow($cells, 'row3');
  1431. $cells = wf_TableCell(__('Date of issue'));
  1432. $cells .= wf_TableCell($passportdata['passportdate']);
  1433. $rows .= wf_TableRow($cells, 'row3');
  1434. $cells = wf_TableCell(__('Identification code'));
  1435. $cells .= wf_TableCell($passportdata['pinn']);
  1436. $rows .= wf_TableRow($cells, 'row3');
  1437. $cells = wf_TableCell(__('Registration address'));
  1438. $cells .= wf_TableCell($passportdata['pcity'] . ' ' . $passportdata['pstreet'] . ' ' . $passportdata['pbuild'] . '/' . $passportdata['papt']);
  1439. $rows .= wf_TableRow($cells, 'row3');
  1440. $result = wf_TableBody($rows, '100%', '0');
  1441. } else {
  1442. $result = __('User passport data is empty') . ' ' . __('You can fill them with the appropriate module');
  1443. }
  1444. if (cfr('PDATA')) {
  1445. $result .= wf_delimiter();
  1446. $result .= wf_Link("?module=pdataedit&username=" . $login, web_edit_icon() . ' ' . __('Edit') . ' ' . __('passport data'), false, 'ubButton');
  1447. }
  1448. return ($result);
  1449. }
  1450. /**
  1451. * Passport data editing form
  1452. *
  1453. * @param $login - user login
  1454. * @param $passportdata - user passport data array
  1455. *
  1456. * @return void
  1457. *
  1458. */
  1459. function web_PassportDataEditFormshow($login, $passportdata) {
  1460. $alladdress = zb_AddressGetFulladdresslist();
  1461. @$useraddress = $alladdress[$login];
  1462. //extracting passport data
  1463. if (!empty($passportdata)) {
  1464. $birthdate = $passportdata['birthdate'];
  1465. $passportnum = $passportdata['passportnum'];
  1466. $passportdate = $passportdata['passportdate'];
  1467. $passportwho = $passportdata['passportwho'];
  1468. $pcity = $passportdata['pcity'];
  1469. $pstreet = $passportdata['pstreet'];
  1470. $pbuild = $passportdata['pbuild'];
  1471. $papt = $passportdata['papt'];
  1472. $pinn = $passportdata['pinn'];
  1473. } else {
  1474. $birthdate = '';
  1475. $passportnum = '';
  1476. $passportdate = '';
  1477. $passportwho = '';
  1478. $pcity = '';
  1479. $pstreet = '';
  1480. $pbuild = '';
  1481. $papt = '';
  1482. $pinn = '';
  1483. }
  1484. //form construction
  1485. $inputs = wf_tag('h3') . __('Passport data') . wf_tag('h3', true);
  1486. $inputs .= wf_DatePickerPreset('editbirthdate', $birthdate, true);
  1487. $inputs .= __('Birth date');
  1488. $inputs .= wf_delimiter();
  1489. $inputs .= wf_TextInput('editpassportnum', __('Passport number'), $passportnum, false, '35');
  1490. $inputs .= wf_delimiter();
  1491. $inputs .= wf_TextInput('editpassportwho', __('Issuing authority'), $passportwho, false, '35');
  1492. $inputs .= wf_delimiter();
  1493. $inputs .= wf_DatePickerPreset('editpassportdate', $passportdate, true);
  1494. $inputs .= __('Date of issue');
  1495. $inputs .= wf_delimiter();
  1496. $inputs .= wf_TextInput('editpinn', __('Identification code'), $pinn, false, '10');
  1497. $inputs .= wf_delimiter();
  1498. $inputs .= wf_tag('h3') . __('Registration address') . wf_tag('h3', true);
  1499. $inputs .= wf_TextInput('editpcity', __('City'), $pcity, false, '20');
  1500. $inputs .= wf_delimiter();
  1501. $inputs .= wf_TextInput('editpstreet', __('Street'), $pstreet, false, '20');
  1502. $inputs .= wf_delimiter();
  1503. $inputs .= wf_TextInput('editpbuild', __('Build'), $pbuild, false, '5');
  1504. $inputs .= wf_delimiter();
  1505. $inputs .= wf_TextInput('editpapt', __('Apartment'), $papt, false, '5');
  1506. $inputs .= wf_delimiter();
  1507. $inputs .= wf_Submit(__('Save'));
  1508. $form = wf_Form('', 'POST', $inputs, 'glamour');
  1509. show_window(__('Edit') . ' ' . __('passport data') . ' ' . $useraddress, $form);
  1510. }