api.userstats.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334
  1. <?php
  2. /**
  3. * Returns user IP by password auth
  4. *
  5. * @param string $login
  6. * @param string $password
  7. * @return string/void on error
  8. */
  9. function zbs_UserCheckLoginAuth($login, $password) {
  10. $result = '';
  11. $login = vf($login);
  12. $login = preg_replace('#[^a-z0-9A-Z\-_\.]#Uis', '', $login);
  13. $login = preg_replace('/\0/s', '', $login);
  14. $password = vf($password);
  15. $password = preg_replace('#[^a-z0-9A-Z\-_\.]#Uis', '', $password);
  16. $password = preg_replace('/\0/s', '', $password);
  17. if (!empty($login) and (!empty($password))) {
  18. if (is_string($login) and is_string($password)) {
  19. $query = "SELECT `IP` from `users` WHERE `login`='" . $login . "' AND MD5(`password`)='" . $password . "'";
  20. $data = simple_query($query);
  21. if (!empty($data)) {
  22. $result = $data['IP'];
  23. }
  24. }
  25. }
  26. return ($result);
  27. }
  28. /**
  29. * Returns user IP or try identify it by login
  30. *
  31. * @param bool $debug
  32. * @return string
  33. */
  34. function zbs_UserDetectIp($debug = false) {
  35. $glob_conf = zbs_LoadConfig();
  36. $ip = '';
  37. //force REST API auth
  38. if (ubRouting::checkGet('uberlogin', 'uberpassword')) {
  39. $ip = zbs_UserCheckLoginAuth(vf(ubRouting::get('uberlogin')), vf(ubRouting::get('uberpassword')));
  40. if (!empty($ip)) {
  41. return ($ip);
  42. } else {
  43. header('HTTP/1.1 401 Unauthorized', true, 401);
  44. die('ERROR_WRONG_UBERAUTH');
  45. }
  46. }
  47. //default auth method
  48. if ($glob_conf['auth'] == 'ip') {
  49. $ip = $_SERVER['REMOTE_ADDR'];
  50. }
  51. //password based auth
  52. if ($glob_conf['auth'] == 'login') {
  53. if ((isset($_COOKIE['ulogin'])) and (isset($_COOKIE['upassword']))) {
  54. if (is_string($_COOKIE['ulogin']) and is_string($_COOKIE['upassword'])) {
  55. $ulogin = trim(vf($_COOKIE['ulogin']));
  56. $upassword = trim(vf($_COOKIE['upassword']));
  57. $ip = zbs_UserCheckLoginAuth($ulogin, $upassword);
  58. }
  59. }
  60. }
  61. //trying to find user by IP, than if failed - login based auth.
  62. if ($glob_conf['auth'] == 'both') {
  63. $authorizedByIp = false;
  64. $ipCheck = $_SERVER['REMOTE_ADDR'];
  65. $remoteUserLogin = zbs_UserGetLoginByIp($ipCheck);
  66. if ($remoteUserLogin) {
  67. if (isIpAuthAllowed($remoteUserLogin)) {
  68. $ip = $ipCheck;
  69. $authorizedByIp = true;
  70. }
  71. }
  72. if (!$authorizedByIp) {
  73. if ((isset($_COOKIE['ulogin'])) and (isset($_COOKIE['upassword']))) {
  74. if (is_string($_COOKIE['ulogin']) and is_string($_COOKIE['upassword'])) {
  75. $ulogin = trim(vf($_COOKIE['ulogin']));
  76. $upassword = trim(vf($_COOKIE['upassword']));
  77. $ip = zbs_UserCheckLoginAuth($ulogin, $upassword);
  78. }
  79. }
  80. }
  81. }
  82. if ($debug) {
  83. //$ip = '172.30.0.2';
  84. }
  85. return ($ip);
  86. }
  87. /**
  88. * Checks is user allowed for automated IP auth
  89. *
  90. * @param string $login
  91. *
  92. * @return bool
  93. */
  94. function isIpAuthAllowed($login) {
  95. $result = true;
  96. $login = mysql_real_escape_string($login);
  97. $query = "SELECT * from `ipauth_denied` WHERE `login`='" . $login . "'";
  98. $denied = simple_query($query);
  99. if (!empty($denied)) {
  100. $result = false;
  101. }
  102. return ($result);
  103. }
  104. /**
  105. * Returns user login by its IP address
  106. *
  107. * @param string $ip
  108. * @return string
  109. */
  110. function zbs_UserGetLoginByIp($ip) {
  111. $glob_conf = zbs_LoadConfig();
  112. $result = '';
  113. if (!empty($ip)) {
  114. $query = "SELECT `login` from `users` where `IP`='" . $ip . "'";
  115. $result = simple_query($query);
  116. }
  117. if (!empty($result)) {
  118. return ($result['login']);
  119. } else {
  120. if ($glob_conf['auth'] == 'ip') {
  121. if ((isset($glob_conf['authfailredir']))) {
  122. if (!empty($glob_conf['authfailredir'])) {
  123. rcms_redirect($glob_conf['authfailredir']);
  124. die('Unknown user');
  125. } else {
  126. die('Unknown user EX_EMPTY_AUTHFAILREDIR');
  127. }
  128. } else {
  129. die('Unknown user EX_NO_AUTHFAILREDIR_DEFINED');
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Checks is table with some name exists, and returns int value 0/1 used as bool (Oo)
  136. *
  137. * @param string $tablename
  138. * @return int
  139. */
  140. function zbs_CheckTableExists($tablename) {
  141. $query = "SELECT CASE WHEN (SELECT COUNT(*) AS STATUS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = (SELECT DATABASE()) AND TABLE_NAME = '" . $tablename . "') = 1 THEN (SELECT 1) ELSE (SELECT 0) END AS result;";
  142. $result = simple_query($query);
  143. return ($result['result']);
  144. }
  145. /**
  146. * Returns current stargazer DB version
  147. * =<2.408 - 0
  148. * >=2.409 - 1+
  149. *
  150. * @return int
  151. */
  152. function zbs_CheckDbSchema() {
  153. if (zbs_CheckTableExists('info')) {
  154. $query = "SELECT `version` from `info`";
  155. $result = simple_query($query);
  156. $result = $result['version'];
  157. } else {
  158. $result = 0;
  159. }
  160. return ($result);
  161. }
  162. /**
  163. * Returns user online left days
  164. *
  165. * @param string $login existing users login
  166. * @param double $userBalance current users balance
  167. * @param string $userTariff users tariff
  168. * @param bool $rawdays show only days count
  169. * @return string
  170. */
  171. function zbs_GetOnlineLeftCount($login, $userBalance, $userTariff, $rawDays = false) {
  172. // DEFINE VARS:
  173. $us_config = zbs_LoadConfig();
  174. $tariffData = zbs_UserGetTariffData($userTariff);
  175. $daysOnLine = 0;
  176. $balanceExpire = '';
  177. $totalVsrvPrice = 0;
  178. $userFeeOffset = 0;
  179. if (empty($tariffData)) {
  180. //user have no tariff
  181. $tariffData['name'] = '*_NO_TARIFF_*';
  182. $tariffData['Fee'] = 0;
  183. $tariffData['period'] = 'month';
  184. }
  185. //power tariffs overrides some fees?
  186. if ($us_config['POWERTARIFFS_ENABLED']) {
  187. if ($tariffData['Fee'] == 0) {
  188. $tariffData['Fee'] = zbs_GetPowerTariffPrice($userTariff);
  189. if ($tariffData['Fee'] > 0) {
  190. $userFeeOffset = zbs_GetPowerTariffDay($login);
  191. $maxDay = (zbs_GetPowerTariffMaxDay() - 1);
  192. if ($userFeeOffset == 0) {
  193. $userFeeOffset = (date('j') < $maxDay) ? date('j') + 1 : 1;
  194. $daysOnLine = (date('j') < $maxDay) ? 0 : date('j');
  195. } else {
  196. if ($userFeeOffset < 0) {
  197. $userFeeOffset = (date('j') < $maxDay) ? date('j') + 1 : 1;
  198. }
  199. $daysOnLine = ($userFeeOffset == date('j')) ? 0 : (($userFeeOffset < date('j')) ? -1 : date('j') * -1);
  200. }
  201. $userFeeOffset = $userFeeOffset - 1;
  202. }
  203. }
  204. }
  205. $tariffFee = $tariffData['Fee'];
  206. $tariffPeriod = isset($tariffData['period']) ? $tariffData['period'] : 'month';
  207. $includeVServices = (!empty($us_config['ONLINELEFT_CONSIDER_VSERVICES']));
  208. $vservicesPeriodON = (!empty($us_config['VSERVICES_CONSIDER_PERIODS']));
  209. if ($userBalance >= 0) {
  210. // check if tariff has no zero price and is not free
  211. if ($tariffFee > 0) {
  212. if ($includeVServices) {
  213. $totalVsrvPrice = ($vservicesPeriodON) ? zbs_vservicesGetUserPricePeriod($login, $tariffPeriod) : zbs_vservicesGetUserPrice($login);
  214. $tariffFee += $totalVsrvPrice;
  215. }
  216. //spread fee
  217. if ($us_config['ONLINELEFT_SPREAD'] != 0) {
  218. if ($tariffPeriod == 'month') {
  219. //monthly period
  220. while ($userBalance >= 0) {
  221. $daysOnLine++;
  222. $dayFee = $tariffFee / date('t', time() + ($daysOnLine * 24 * 60 * 60));
  223. $userBalance = $userBalance - $dayFee;
  224. }
  225. } else {
  226. //daily period
  227. while ($userBalance >= 0) {
  228. $daysOnLine++;
  229. $userBalance = $userBalance - $tariffFee;
  230. }
  231. }
  232. } else {
  233. //non spread fee
  234. if ($tariffPeriod == 'month') {
  235. //monthly non spread fee
  236. while ($userBalance >= 0) {
  237. $daysOnLine = $daysOnLine + $userFeeOffset + date('t', time() + ($daysOnLine * 24 * 60 * 60)) - date('d', time() + ($daysOnLine * 24 * 60 * 60)) + 1;
  238. $userBalance = $userBalance - $tariffFee;
  239. }
  240. } else {
  241. //daily non spread fee
  242. while ($userBalance >= 0) {
  243. $daysOnLine++;
  244. $userBalance = $userBalance - $tariffFee;
  245. }
  246. }
  247. }
  248. }
  249. // STYLING OF THE RESULT:
  250. switch ($us_config['ONLINELEFT_STYLE']) {
  251. case 'days':
  252. $balanceExpire = ", " . __('enought for') . ' ' . $daysOnLine . ' ' . __('days');
  253. break;
  254. case 'date':
  255. $balanceExpire = ", " . __('enought till the') . ' ' . date("d.m.Y", time() + ($daysOnLine * 24 * 60 * 60));
  256. break;
  257. case 'mixed':
  258. $balanceExpire = ", " . __('enought for') . ' ' . $daysOnLine . ' ' . __('days')
  259. . ", " . __('till the') . ' ' . date("d.m.Y", time() + ($daysOnLine * 24 * 60 * 60));
  260. break;
  261. default:
  262. $balanceExpire = NULL;
  263. break;
  264. }
  265. if ($includeVServices and !empty($totalVsrvPrice) and !empty($balanceExpire)) {
  266. if ($us_config['ROUND_PROFILE_CASH']) {
  267. $totalVsrvPrice = web_roundValue($totalVsrvPrice, 2);
  268. }
  269. $balanceExpire .= la_delimiter(0) . '(' . __('including additional services') . ' ' . $totalVsrvPrice . ' ' . $us_config['currency']
  270. . ' / ' . __($tariffPeriod) . ')';
  271. }
  272. } else {
  273. //fast credit control id debt
  274. $creditControl = '';
  275. if (isset($us_config['ONLINELEFT_CREDIT'])) {
  276. if ($us_config['ONLINELEFT_CREDIT']) {
  277. if ($us_config['SC_ENABLED']) {
  278. $creditControl = ' ' . la_Link('?module=creditor', __('Get credit') . '?', false, '');
  279. }
  280. }
  281. }
  282. $balanceExpire = la_tag('span', false, '', 'style="color:red;"') . ', ' . __('indebtedness!') . ' ' . $creditControl . la_tag('span', true);
  283. }
  284. if ($rawDays) {
  285. $balanceExpire = $daysOnLine;
  286. }
  287. return ($balanceExpire);
  288. }
  289. /**
  290. * Suggests user power tariff price
  291. *
  292. * @param string $userTariff
  293. *
  294. * @return float
  295. */
  296. function zbs_GetPowerTariffPrice($userTariff) {
  297. $result = -2;
  298. $powerTariffs = new NyanORM('pt_tariffs');
  299. $allPowerTariffs = $powerTariffs->getAll('tariff');
  300. if (isset($allPowerTariffs[$userTariff])) {
  301. $result = $allPowerTariffs[$userTariff]['fee'];
  302. }
  303. return ($result);
  304. }
  305. /**
  306. * Suggests user power tariff fee charge date
  307. *
  308. * @param string $userLogin
  309. *
  310. * @return int
  311. */
  312. function zbs_GetPowerTariffDay($userLogin) {
  313. $result = 0;
  314. $powerTariffs = new NyanORM('pt_users');
  315. $allPowerUsers = $powerTariffs->getAll('login');
  316. if (isset($allPowerUsers[$userLogin])) {
  317. $result = $allPowerUsers[$userLogin]['day'];
  318. }
  319. return ($result);
  320. }
  321. /**
  322. * Returns value of PowerTariffs maximum day option that will be round to 1st
  323. *
  324. * @return int
  325. */
  326. function zbs_GetPowerTariffMaxDay() {
  327. global $us_config;
  328. $result = 26;
  329. if (isset($us_config['POWERTARIFFS_MAXDAY'])) {
  330. $result = $us_config['POWERTARIFFS_MAXDAY'];
  331. }
  332. return ($result);
  333. }
  334. /**
  335. * Renders user login form
  336. *
  337. * @return void
  338. */
  339. function zbs_LoginForm() {
  340. $inputs = la_tag('label') . __('Login') . la_tag('label', true) . la_tag('br');
  341. $inputs .= la_TextInput('ulogin', '', '', true);
  342. $inputs .= la_tag('label') . __('Password') . la_tag('label', true) . la_tag('br');
  343. $inputs .= la_PasswordInput('upassword', '', '', true);
  344. $inputs .= la_Submit(__('Enter'));
  345. $form = la_Form('', 'POST', $inputs, 'loginform');
  346. $cells = la_TableCell($form, '', '', 'align="center"');
  347. $rows = la_TableRow($cells);
  348. $result = la_TableBody($rows, '100%', 0);
  349. show_window(__('Login with your account'), $result);
  350. }
  351. /**
  352. * Renders user logout form (only for login/both auth)
  353. *
  354. * @param bool $return
  355. *
  356. * @return void
  357. */
  358. function zbs_LogoutForm($return = false) {
  359. global $us_config;
  360. if ($us_config['auth'] == 'login' or $us_config['auth'] == 'both') {
  361. $form = '';
  362. $inputs = la_HiddenInput('ulogout', 'true');
  363. $inputs .= la_Submit(__('Logout'));
  364. if (isset($_COOKIE['upassword'])) {
  365. if ($_COOKIE['upassword'] != 'nopassword') {
  366. $form .= la_Form('', 'POST', $inputs);
  367. }
  368. }
  369. if (!$return) {
  370. show_window('', $form);
  371. } else {
  372. return ($form);
  373. }
  374. }
  375. }
  376. /**
  377. * Returns language selecting box
  378. *
  379. * @return string
  380. */
  381. function zbs_LangSelector() {
  382. $glob_conf = zbs_LoadConfig();
  383. $form = '';
  384. if ($glob_conf['allowclang']) {
  385. $allangs = rcms_scandir("languages");
  386. if (!empty($allangs)) {
  387. $inputs = la_tag('select', false, '', 'name="changelang" onChange="this.form.submit();"');
  388. $inputs .= la_tag('option', false, '', 'value="-"') . __('Language') . la_tag('option', true);
  389. foreach ($allangs as $eachlang) {
  390. $langIdPath = 'languages/' . $eachlang . '/langid.txt';
  391. if (file_exists($langIdPath)) {
  392. $eachlangid = file_get_contents($langIdPath);
  393. } else {
  394. $eachlangid = $eachlang;
  395. }
  396. $inputs .= la_tag('option', false, '', 'value="' . $eachlang . '"') . $eachlangid . la_tag('option', true);
  397. }
  398. $inputs .= la_tag('select', true);
  399. $inputs .= ' ';
  400. $form = la_Form('', 'GET', $inputs);
  401. }
  402. } else {
  403. $form = '';
  404. }
  405. return ($form);
  406. }
  407. /**
  408. * Returns array of available cities as id=>city name
  409. *
  410. * @return array
  411. */
  412. function zbs_AddressGetFullCityNames() {
  413. $query = "SELECT * from `city`";
  414. $result = array();
  415. $all_data = simple_queryall($query);
  416. if (!empty($all_data)) {
  417. foreach ($all_data as $io => $eachcity) {
  418. $result[$eachcity['id']] = $eachcity['cityname'];
  419. }
  420. }
  421. return ($result);
  422. }
  423. /**
  424. * Returns array of availble user address as login=>address
  425. *
  426. * @return array
  427. */
  428. function zbs_AddressGetFulladdresslist() {
  429. $alterconf = zbs_LoadConfig();
  430. $result = array();
  431. $query_full = "
  432. SELECT `address`.`login`,`city`.`cityname`,`street`.`streetname`,`build`.`buildnum`,`apt`.`apt` FROM `address`
  433. INNER JOIN `apt` ON `address`.`aptid`= `apt`.`id`
  434. INNER JOIN `build` ON `apt`.`buildid`=`build`.`id`
  435. INNER JOIN `street` ON `build`.`streetid`=`street`.`id`
  436. INNER JOIN `city` ON `street`.`cityid`=`city`.`id`";
  437. $full_adress = simple_queryall($query_full);
  438. if (!empty($full_adress)) {
  439. foreach ($full_adress as $ArrayData) {
  440. // zero apt handle
  441. if ($alterconf['ZERO_TOLERANCE']) {
  442. $apartment_filtered = ($ArrayData['apt'] == 0) ? '' : '/' . $ArrayData['apt'];
  443. } else {
  444. $apartment_filtered = '/' . $ArrayData['apt'];
  445. }
  446. if ($alterconf['CITY_DISPLAY']) {
  447. $result[$ArrayData['login']] = $ArrayData['cityname'] . ' ' . $ArrayData['streetname'] . ' ' . $ArrayData['buildnum'] . $apartment_filtered;
  448. } else {
  449. $result[$ArrayData['login']] = $ArrayData['streetname'] . ' ' . $ArrayData['buildnum'] . $apartment_filtered;
  450. }
  451. }
  452. }
  453. return ($result);
  454. }
  455. /**
  456. * Returns array of availble user address as login=>array('cityname' = ..., 'streetname' = ..., 'buildnum' = ..., 'apt' = ...)
  457. *
  458. * @return array
  459. */
  460. function zbs_AddressGetFulladdresslistStruct($login) {
  461. $login = mysql_real_escape_string($login);
  462. $alterconf = zbs_LoadConfig();
  463. $result = array();
  464. $query_full = "
  465. SELECT `address`.`login`,`city`.`cityname`,`street`.`streetname`,`build`.`buildnum`,`apt`.`apt` FROM `address`
  466. INNER JOIN `apt` ON `address`.`aptid`= `apt`.`id`
  467. INNER JOIN `build` ON `apt`.`buildid`=`build`.`id`
  468. INNER JOIN `street` ON `build`.`streetid`=`street`.`id`
  469. INNER JOIN `city` ON `street`.`cityid`=`city`.`id` WHERE `login`='" . $login . "'";
  470. $full_adress = simple_queryall($query_full);
  471. if (!empty($full_adress)) {
  472. foreach ($full_adress as $ArrayData) {
  473. // zero apt handle
  474. if ($alterconf['ZERO_TOLERANCE']) {
  475. $apartment_filtered = ($ArrayData['apt'] == 0) ? '' : '/' . $ArrayData['apt'];
  476. } else {
  477. $apartment_filtered = '/' . $ArrayData['apt'];
  478. }
  479. if ($alterconf['CITY_DISPLAY']) {
  480. $result[$ArrayData['login']]['cityname'] = $ArrayData['cityname'];
  481. $result[$ArrayData['login']]['streetname'] = $ArrayData['streetname'];
  482. $result[$ArrayData['login']]['buildnum'] = $ArrayData['buildnum'];
  483. $result[$ArrayData['login']]['apt'] = $ArrayData['apt'];
  484. } else {
  485. $result[$ArrayData['login']]['streetname'] = $ArrayData['streetname'];
  486. $result[$ArrayData['login']]['buildnum'] = $ArrayData['buildnum'];
  487. $result[$ArrayData['login']]['apt'] = $ArrayData['apt'];
  488. }
  489. }
  490. }
  491. return ($result);
  492. }
  493. /**
  494. * Returns array of stargazer user data
  495. *
  496. * @param string $login
  497. * @return array
  498. */
  499. function zbs_UserGetStargazerData($login) {
  500. $login = mysql_real_escape_string($login);
  501. $query = "SELECT * from `users` WHERE `login`='" . $login . "'";
  502. $result = simple_query($query);
  503. return ($result);
  504. }
  505. /**
  506. * Returns array of all stargazer user data
  507. *
  508. * @param string $login
  509. *
  510. * @return array
  511. */
  512. function zbs_UserGetAllStargazerData() {
  513. $result = array();
  514. $query = "SELECT * from `users`";
  515. $all = simple_queryall($query);
  516. if (!empty($all)) {
  517. foreach ($all as $io => $each) {
  518. $result[$each['login']] = $each;
  519. }
  520. }
  521. return ($result);
  522. }
  523. /**
  524. * Returns array of all available user realnames as login=>realname
  525. *
  526. * @return array
  527. */
  528. function zbs_UserGetAllRealnames() {
  529. $query_fio = "SELECT * from `realname`";
  530. $allfioz = simple_queryall($query_fio);
  531. $fioz = array();
  532. if (!empty($allfioz)) {
  533. foreach ($allfioz as $ia => $eachfio) {
  534. $fioz[$eachfio['login']] = $eachfio['realname'];
  535. }
  536. }
  537. return ($fioz);
  538. }
  539. /**
  540. * Returns contract number by user login
  541. *
  542. * @param string $login
  543. * @return string
  544. */
  545. function zbs_UserGetContract($login) {
  546. $login = vf($login);
  547. $query = "SELECT `contract` from `contracts` WHERE `login`='" . $login . "'";
  548. $contract_arr = simple_query($query);
  549. return ($contract_arr['contract']);
  550. }
  551. /**
  552. * Returns contract date by user login or contract value
  553. *
  554. * @param string $login
  555. * @param string $contract
  556. *
  557. * @return string
  558. */
  559. function zbs_UserGetContractDate($login, $contract = '') {
  560. if (!empty($contract)) {
  561. $contract = ubRouting::filters($contract, 'mres');
  562. $query = "SELECT `date` FROM `contractdates` WHERE `contract` = '" . $contract . "'";
  563. } elseif (!empty($login)) {
  564. $login = ubRouting::filters($login, 'login');
  565. $query = "SELECT `contractdates`.`date` FROM `contracts`
  566. LEFT JOIN `contractdates` ON `contracts`.`contract` = `contractdates`.`contract`
  567. WHERE `contracts`.`login` = '" . $login . "'";
  568. }
  569. $contract_date_arr = simple_query($query);
  570. return ($contract_date_arr['date']);
  571. }
  572. /**
  573. * Returns email address by user login
  574. *
  575. * @param string $login
  576. * @return string
  577. */
  578. function zbs_UserGetEmail($login) {
  579. $login = vf($login);
  580. $query = "SELECT `email` from `emails` WHERE `login`='" . $login . "'";
  581. $email_arr = simple_query($query);
  582. return ($email_arr['email']);
  583. }
  584. /**
  585. * Returns mobile number by user login
  586. *
  587. * @param string $login
  588. * @return string
  589. */
  590. function zbs_UserGetMobile($login) {
  591. $query = "SELECT `mobile` from `phones` WHERE `login`='" . $login . "'";
  592. $phone_arr = simple_query($query);
  593. return ($phone_arr['mobile']);
  594. }
  595. /**
  596. * Returns ext mobile numbers by user login
  597. *
  598. * @param string $login
  599. * @return string
  600. */
  601. function zbs_UserGetMobileExt($login) {
  602. $query = "SELECT `mobile` from `mobileext` WHERE `login`='" . $login . "'";
  603. $phone_arr = simple_queryall($query);
  604. $result = array();
  605. if (!empty($phone_arr)) {
  606. foreach ($phone_arr as $io => $eachExtMobile) {
  607. $result[] = $eachExtMobile['mobile'];
  608. }
  609. }
  610. return ($result);
  611. }
  612. /**
  613. * Returns phone number by user login
  614. *
  615. * @param string $login
  616. * @return string
  617. */
  618. function zbs_UserGetPhone($login) {
  619. $query = "SELECT `phone` from `phones` WHERE `login`='" . $login . "'";
  620. $phone_arr = simple_query($query);
  621. return ($phone_arr['phone']);
  622. }
  623. /**
  624. * Renders UBagent user data (deprecated)
  625. *
  626. * @param string $login
  627. *
  628. * @return void
  629. */
  630. function zbs_UserShowAgentData($login) {
  631. if (isset($_GET['payments'])) {
  632. $allpayments = zbs_CashGetUserPayments($login);
  633. $paycount = (sizeof($allpayments)) * 3;
  634. $i = 1;
  635. $cn = 0;
  636. $payments = '<?xml version="1.0"?>
  637. <CONFIG>
  638. <grid version="3">
  639. <saveoptions create="False" content="True" position="False"/>
  640. <content>
  641. <cells cellcount="' . $paycount . '">
  642. ' . "\n";
  643. if (!empty($allpayments)) {
  644. foreach ($allpayments as $io => $eachpayment) {
  645. $cn++;
  646. $payments .= '<cell' . $cn . ' row="' . $i . '" text="' . $eachpayment['date'] . '" column="1"/>' . "\n";
  647. $cn++;
  648. $payments .= '<cell' . $cn . ' row="' . $i . '" text="' . $eachpayment['summ'] . '" column="2"/>' . "\n";
  649. $cn++;
  650. $payments .= '<cell' . $cn . ' row="' . $i . '" text="' . $eachpayment['balance'] . '" column="3"/>' . "\n";
  651. $i++;
  652. }
  653. }
  654. $payments .= '</cells>
  655. </content>
  656. </grid>
  657. </CONFIG>';
  658. print($payments);
  659. die();
  660. }
  661. if (isset($_GET['paymentsplain'])) {
  662. $allpayments = zbs_CashGetUserPayments($login);
  663. $payments = '';
  664. if (!empty($allpayments)) {
  665. foreach ($allpayments as $io => $eachpayment) {
  666. $payments .= $eachpayment['date'] . ' ' . $eachpayment['summ'] . ' ' . $eachpayment['balance'] . "\n";
  667. }
  668. }
  669. print($payments);
  670. die();
  671. }
  672. if (isset($_GET['messages'])) {
  673. $msg_result = '';
  674. $msg_query = "SELECT * from `ticketing` WHERE `to`= '" . $login . "' AND `from`='NULL' AND `status`='1' ORDER BY `date` DESC";
  675. $allmessages = simple_queryall($msg_query);
  676. if (!empty($allmessages)) {
  677. foreach ($allmessages as $io => $eachmessage) {
  678. $msg_result .= $eachmessage['date'] . "\r\n";
  679. $msg_result .= $eachmessage['text'] . "\r\n";
  680. $msg_result .= "\n";
  681. $msg_result .= "\n";
  682. $msg_result .= "\n";
  683. }
  684. }
  685. print($msg_result);
  686. die();
  687. }
  688. $us_config = zbs_LoadConfig();
  689. $us_currency = $us_config['currency'];
  690. $userdata = zbs_UserGetStargazerData($login);
  691. $alladdress = zbs_AddressGetFulladdresslist();
  692. $allrealnames = zbs_UserGetAllRealnames();
  693. $contract = zbs_UserGetContract($login);
  694. $email = zbs_UserGetEmail($login);
  695. $mobile = zbs_UserGetMobile($login);
  696. $phone = zbs_UserGetPhone($login);
  697. if ($userdata['CreditExpire'] != 0) {
  698. $credexpire = date("d-m-Y", $userdata['CreditExpire']);
  699. } else {
  700. $credexpire = '';
  701. }
  702. $traffdown = 0;
  703. $traffup = 0;
  704. $traffdgb = 0;
  705. $traffugb = 0;
  706. for ($i = 0; $i <= 9; $i++) {
  707. $traffdown = $traffdown + $userdata['D' . $i];
  708. $traffup = $traffup + $userdata['U' . $i];
  709. }
  710. $traffdgb = round($traffdown / 1073741824);
  711. $traffugb = round($traffup / 1073741824);
  712. if ($traffdgb == 0) {
  713. $traffdgb = 1;
  714. }
  715. if ($traffugb == 0) {
  716. $traffugb = 1;
  717. }
  718. $result = '[USERINFO]' . "\n";
  719. $result .= 'fulladdress=' . @$alladdress[$login] . "\n";
  720. $result .= 'realname=' . @$allrealnames[$login] . "\n";
  721. $result .= 'login=' . $login . "\n";
  722. $result .= 'password=' . @$userdata['Password'] . "\n";
  723. $result .= 'cash=' . @round($userdata['Cash'], 2) . "\n";
  724. $result .= 'login=' . $login . "\n";
  725. $result .= 'password=' . @$userdata['Password'] . "\n";
  726. $result .= 'ip=' . @$userdata['IP'] . "\n";
  727. $result .= 'phone=' . $phone . "\n";
  728. $result .= 'mobile=' . $mobile . "\n";
  729. $result .= 'email=' . $email . "\n";
  730. $result .= 'credit=' . @$userdata['Credit'] . "\n";
  731. $result .= 'creditexpire=' . $credexpire . "\n";
  732. $result .= 'payid=' . ip2int($userdata['IP']) . "\n";
  733. $result .= 'contract=' . $contract . "\n";
  734. $result .= 'tariff=' . $userdata['Tariff'] . "\n";
  735. $result .= 'tariffnm=' . $userdata['TariffChange'] . "\n";
  736. $result .= 'traffd=' . $traffdgb . "\n";
  737. $result .= 'traffu=' . $traffugb . "\n";
  738. $result .= 'traffd_conv=' . zbs_convert_size($traffdown) . "\n";
  739. $result .= 'traffu_conv=' . zbs_convert_size($traffup) . "\n";
  740. $result .= 'trafftotal_conv=' . zbs_convert_size($traffdown + $traffup) . "\n";
  741. $result .= "\n";
  742. $result .= '[CONF]' . "\n";
  743. $result .= ' currency=' . $us_currency;
  744. print($result);
  745. die();
  746. }
  747. /**
  748. * Renders some data as XML
  749. *
  750. * !!! should be considered as a possible replacement for an "old" zbs_XMLAgentRender() function !!!
  751. *
  752. * @param array $data data array for rendering
  753. * @param string $mainSection all output data parent element tag name
  754. * @param string $subSection parent tag for each data qunique element tag name
  755. * @param string $format output format: xml or json
  756. * @param bool $messages is data contain announcements data for render
  757. *
  758. * @return void
  759. */
  760. /*function zbs_XMLAgentRender($data, $mainSection = '', $subSection = '', $format = 'xml', $messages = false) {
  761. XMLAgent::renderResponse($data, $mainSection = '', $subSection = '', $format = 'xml', $messages = false);
  762. }*/
  763. /**
  764. * Renders some data as XML
  765. *
  766. * @param array $data data array for rendering
  767. * @param string $mainSection all output data parent element tag name
  768. * @param string $subSection parent tag for each data qunique element tag name
  769. * @param string $format output format: xml or json
  770. * @param bool $messages is data contain announcements data for render
  771. *
  772. * @return void
  773. */
  774. function zbs_XMLAgentRender($data, $mainSection = '', $subSection = '', $format = 'xml', $messages = false) {
  775. $result = '';
  776. //XML legacy output
  777. if ($format == 'xml') {
  778. $result .= '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
  779. if (!empty($mainSection)) {
  780. $result .= '<' . $mainSection . '>' . PHP_EOL;
  781. }
  782. if (!empty($data)) {
  783. foreach ($data as $index => $record) {
  784. if (!empty($subSection)) {
  785. $result .= '<' . $subSection . '>' . PHP_EOL;
  786. }
  787. //normal data output
  788. if (!$messages) {
  789. foreach ($record as $tag => $value) {
  790. $result .= "\t" . '<' . $tag . '>' . $value . '</' . $tag . '>' . PHP_EOL;
  791. }
  792. } else {
  793. //announcements data output
  794. $result .= '<message unic="' . $record['unic'] . '" title="' . $record['title'] . '">' . $record['text'] . '</message>' . PHP_EOL;
  795. }
  796. if (!empty($subSection)) {
  797. $result .= '</' . $subSection . '>' . PHP_EOL;
  798. }
  799. }
  800. }
  801. if (!empty($mainSection)) {
  802. $result .= '</' . $mainSection . '>' . PHP_EOL;
  803. }
  804. }
  805. //JSON data output
  806. if ($format == 'json') {
  807. $jsonData = array();
  808. $rcount = 0;
  809. if (!empty($data)) {
  810. foreach ($data as $index => $record) {
  811. if (!empty($record)) {
  812. foreach ($record as $tag => $value) {
  813. if (!empty($subSection) or $messages) {
  814. $jsonData[$rcount][$tag] = $value;
  815. } else {
  816. $jsonData[$tag] = $value;
  817. }
  818. }
  819. }
  820. $rcount++;
  821. }
  822. }
  823. $result .= json_encode($jsonData);
  824. }
  825. //pushing result to client
  826. $contentType = 'text';
  827. if ($format == 'json') {
  828. $contentType = 'application/json';
  829. }
  830. header('Last-Modified: ' . gmdate('r'));
  831. header('Content-Type: ' . $contentType . '; charset=UTF-8');
  832. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  833. header("Pragma: no-cache");
  834. header('Access-Control-Allow-Origin: *');
  835. die($result);
  836. }
  837. /**
  838. * Returns XML-agent user data
  839. *
  840. * @param string $login
  841. */
  842. function zbs_UserShowXmlAgentData($login) {
  843. $us_config = zbs_LoadConfig();
  844. $outputFormat = 'xml';
  845. if (ubRouting::checkGet('json')) {
  846. $outputFormat = 'json';
  847. }
  848. //payments export
  849. if (ubRouting::checkGet('payments')) {
  850. if ($us_config['PAYMENTS_ENABLED']) {
  851. $allpayments = zbs_CashGetUserPayments($login);
  852. $payments = array();
  853. if (!empty($allpayments)) {
  854. foreach ($allpayments as $io => $eachpayment) {
  855. $payments[$eachpayment['id']]['date'] = $eachpayment['date'];
  856. $payments[$eachpayment['id']]['summ'] = $eachpayment['summ'];
  857. $payments[$eachpayment['id']]['balance'] = $eachpayment['balance'];
  858. }
  859. zbs_XMLAgentRender($payments, 'data', 'payment', $outputFormat);
  860. } else {
  861. zbs_XMLAgentRender(array(), 'data', 'payment', $outputFormat);
  862. }
  863. } else {
  864. zbs_XMLAgentRender(array(), 'data', 'payment', $outputFormat);
  865. }
  866. }
  867. //announcements export
  868. if (ubRouting::checkGet('announcements')) {
  869. if ($us_config['AN_ENABLED']) {
  870. $announcements_query = "SELECT * from `zbsannouncements` WHERE `public`='1' ORDER by `id` DESC";
  871. $allAnnouncements = simple_queryall($announcements_query);
  872. $annArr = array();
  873. if (!empty($allAnnouncements)) {
  874. foreach ($allAnnouncements as $ian => $eachAnnouncement) {
  875. $annText = strip_tags($eachAnnouncement['text']);
  876. $allTitle = strip_tags($eachAnnouncement['title']);
  877. $annArr[] = array(
  878. 'unic' => $eachAnnouncement['id'],
  879. 'title' => $allTitle,
  880. 'text' => $annText
  881. );
  882. }
  883. }
  884. zbs_XMLAgentRender($annArr, 'data', '', $outputFormat, true);
  885. } else {
  886. zbs_XMLAgentRender(array(), 'data', '', $outputFormat, true);
  887. }
  888. }
  889. //helpdesk tickets export
  890. if (ubRouting::checkGet('tickets')) {
  891. if ($us_config['TICKETING_ENABLED']) {
  892. $queryTickets = "SELECT * from `ticketing` ORDER BY `date` DESC";
  893. $allTickets = simple_queryall($queryTickets);
  894. $ticketsArr = array();
  895. $myTickets = array();
  896. if (!empty($allTickets)) {
  897. foreach ($allTickets as $io => $each) {
  898. if ($each['from'] == $login or $each['to'] == $login or isset($myTickets[$each['replyid']])) {
  899. $myTickets[$each['id']] = $each['id'];
  900. $ticketsArr[$each['id']]['id'] = $each['id'];
  901. $ticketsArr[$each['id']]['date'] = $each['date'];
  902. $ticketsArr[$each['id']]['from'] = $each['from'];
  903. $ticketsArr[$each['id']]['to'] = $each['to'];
  904. $ticketsArr[$each['id']]['replyid'] = $each['replyid'];
  905. $ticketsArr[$each['id']]['status'] = $each['status'];
  906. $ticketsArr[$each['id']]['text'] = $each['text'];
  907. }
  908. }
  909. zbs_XMLAgentRender($ticketsArr, 'data', 'ticket', $outputFormat, false);
  910. }
  911. die();
  912. } else {
  913. zbs_XMLAgentRender(array(), 'data', '', $outputFormat, false);
  914. }
  915. }
  916. //online payments URLs export
  917. if (ubRouting::checkGet('opayz')) {
  918. if ($us_config['OPENPAYZ_ENABLED']) {
  919. $paySys = explode(",", $us_config['OPENPAYZ_PAYSYS']);
  920. $payDesc = array();
  921. $opayzArr = array();
  922. if (file_exists('config/opayz.ini')) {
  923. $payDesc = parse_ini_file('config/opayz.ini');
  924. } else {
  925. $payDesc = array();
  926. }
  927. if ($us_config['OPENPAYZ_ENABLED']) {
  928. if ($us_config['OPENPAYZ_REALID']) {
  929. $opayzPaymentid = zbs_PaymentIDGet($login);
  930. } else {
  931. $userdata = zbs_UserGetStargazerData($login);
  932. $opayzPaymentid = ip2int($userdata['IP']);
  933. }
  934. } else {
  935. $opayzPaymentid = 0;
  936. }
  937. if (!empty($paySys)) {
  938. if (!empty($opayzPaymentid)) {
  939. foreach ($paySys as $io => $eachpaysys) {
  940. if (isset($payDesc[$eachpaysys])) {
  941. $paysys_desc = $payDesc[$eachpaysys];
  942. } else {
  943. $paysys_desc = '';
  944. }
  945. $paymentUrl = $us_config['OPENPAYZ_URL'] . $eachpaysys . '/?customer_id=' . $opayzPaymentid;
  946. $opayzArr[$eachpaysys]['name'] = $eachpaysys;
  947. $opayzArr[$eachpaysys]['url'] = $paymentUrl;
  948. $opayzArr[$eachpaysys]['description'] = $paysys_desc;
  949. }
  950. }
  951. }
  952. zbs_XMLAgentRender($opayzArr, 'data', 'paysys', $outputFormat, false);
  953. } else {
  954. zbs_XMLAgentRender(array(), 'data', '', $outputFormat, false);
  955. }
  956. }
  957. // assigned contragents export
  958. if (ubRouting::checkGet('agentassigned')) {
  959. $allAddress = zbs_AddressGetFulladdresslist();
  960. $userAddress = empty($allAddress) ? array() : $allAddress[$login];
  961. $agentData = zbs_AgentAssignedGetDataFast($login, $userAddress);
  962. $agentArray = empty($agentData) ? array() : array('agentdata' => $agentData);
  963. zbs_XMLAgentRender($agentArray, 'data', 'agentdata', $outputFormat);
  964. }
  965. //user data export
  966. $us_currency = $us_config['currency'];
  967. $userdata = zbs_UserGetStargazerData($login);
  968. $alladdress = zbs_AddressGetFulladdresslist();
  969. if (@$us_config['UBA_XML_ADDRESS_STRUCT']) {
  970. $alladdressStruct = zbs_AddressGetFulladdresslistStruct($login);
  971. } else {
  972. $alladdressStruct = array();
  973. }
  974. $allrealnames = zbs_UserGetAllRealnames();
  975. $contract = zbs_UserGetContract($login);
  976. $email = zbs_UserGetEmail($login);
  977. $mobile = zbs_UserGetMobile($login);
  978. $phone = zbs_UserGetPhone($login);
  979. $apiVer = '1';
  980. $passive = $userdata['Passive'];
  981. $down = $userdata['Down'];
  982. //payment id handling
  983. if ($us_config['OPENPAYZ_ENABLED']) {
  984. if ($us_config['OPENPAYZ_REALID']) {
  985. $paymentid = zbs_PaymentIDGet($login);
  986. } else {
  987. $paymentid = ip2int($userdata['IP']);
  988. }
  989. } else {
  990. $paymentid = 0;
  991. }
  992. if ($userdata['CreditExpire'] != 0) {
  993. $credexpire = date("d-m-Y", $userdata['CreditExpire']);
  994. } else {
  995. $credexpire = 'No';
  996. }
  997. if ($userdata['TariffChange']) {
  998. $tariffNm = $userdata['TariffChange'];
  999. } else {
  1000. $tariffNm = 'No';
  1001. }
  1002. $traffdown = 0;
  1003. $traffup = 0;
  1004. $traffdgb = 0;
  1005. $traffugb = 0;
  1006. for ($i = 0; $i <= 9; $i++) {
  1007. $traffdown = $traffdown + $userdata['D' . $i];
  1008. $traffup = $traffup + $userdata['U' . $i];
  1009. }
  1010. $traffdgb = round($traffdown / 1073741824);
  1011. $traffugb = round($traffup / 1073741824);
  1012. if ($traffdgb == 0) {
  1013. $traffdgb = 1;
  1014. }
  1015. if ($traffugb == 0) {
  1016. $traffugb = 1;
  1017. }
  1018. // pasive state check
  1019. if ($passive) {
  1020. $passive_state = 'frozen';
  1021. } else {
  1022. $passive_state = 'active';
  1023. }
  1024. //down state check
  1025. if ($down) {
  1026. $down_state = ' + disabled';
  1027. } else {
  1028. $down_state = '';
  1029. }
  1030. // START OF ONLINELEFT COUNTING <<
  1031. if ($us_config['ONLINELEFT_COUNT'] != 0) {
  1032. // DEFINE VARS:
  1033. $userBalance = $userdata['Cash'];
  1034. if ($userBalance >= 0) {
  1035. $balanceExpire = zbs_GetOnlineLeftCount($login, $userBalance, $userdata['Tariff'], true);
  1036. } else {
  1037. $balanceExpire = 'debt';
  1038. }
  1039. } else {
  1040. $balanceExpire = 'No';
  1041. }
  1042. // >> END OF ONLINELEFT COUNTING
  1043. $reqResult = array();
  1044. $reqResult[] = array('address' => @$alladdress[$login]);
  1045. if (@$us_config['UBA_XML_ADDRESS_STRUCT']) {
  1046. if (!empty($alladdressStruct)) {
  1047. foreach ($alladdressStruct[$login] as $field => $value) {
  1048. $reqResult[] = array($field => $value);
  1049. }
  1050. }
  1051. }
  1052. $reqResult[] = array('realname' => @$allrealnames[$login]);
  1053. $reqResult[] = array('login' => $login);
  1054. $reqResult[] = array('cash' => @round($userdata['Cash'], 2));
  1055. $reqResult[] = array('ip' => @$userdata['IP']);
  1056. $reqResult[] = array('phone' => $phone);
  1057. $reqResult[] = array('mobile' => $mobile);
  1058. $reqResult[] = array('email' => $email);
  1059. $reqResult[] = array('credit' => @$userdata['Credit']);
  1060. $reqResult[] = array('creditexpire' => $credexpire);
  1061. $reqResult[] = array('payid' => strval($paymentid));
  1062. $reqResult[] = array('contract' => $contract);
  1063. $reqResult[] = array('tariff' => $userdata['Tariff']);
  1064. $reqResult[] = array('tariffalias' => __($userdata['Tariff']));
  1065. $reqResult[] = array('tariffnm' => $tariffNm);
  1066. $reqResult[] = array('traffdownload' => zbs_convert_size($traffdown));
  1067. $reqResult[] = array('traffupload' => zbs_convert_size($traffup));
  1068. $reqResult[] = array('trafftotal' => zbs_convert_size($traffdown + $traffup));
  1069. $reqResult[] = array('accountstate' => $passive_state . $down_state);
  1070. $reqResult[] = array('accountexpire' => $balanceExpire);
  1071. $reqResult[] = array('currency' => $us_currency);
  1072. $reqResult[] = array('version' => $apiVer);
  1073. zbs_XMLAgentRender($reqResult, 'userdata', '', $outputFormat, false);
  1074. }
  1075. /**
  1076. * Returns user paymentID by login
  1077. *
  1078. * @param string $login
  1079. * @return int
  1080. */
  1081. function zbs_PaymentIDGet($login) {
  1082. $login = mysql_real_escape_string($login);
  1083. $query = "SELECT `virtualid` from `op_customers` WHERE `realid`='" . $login . "'";
  1084. $result = simple_query($query);
  1085. if (!empty($result)) {
  1086. $result = $result['virtualid'];
  1087. } else {
  1088. $result = '';
  1089. }
  1090. return ($result);
  1091. }
  1092. /**
  1093. * Returns tariff speed
  1094. *
  1095. * @param string $tariff
  1096. * @param bool $raw
  1097. *
  1098. * @return string
  1099. */
  1100. function zbs_TariffGetSpeed($tariff, $raw = false, $offset = 1024) {
  1101. $query = "SELECT * from `speeds` where `Tariff`='" . $tariff . "'";
  1102. $speedData = simple_query($query);
  1103. $result = '';
  1104. if (!empty($speedData)) {
  1105. if ($speedData['speeddown'] != 0) {
  1106. if ($speedData['speeddown'] < $offset) {
  1107. $result = $speedData['speeddown'] . ' ' . __('Kbit/s');
  1108. } else {
  1109. $result = ($speedData['speeddown'] / $offset) . ' ' . __('Mbit/s');
  1110. }
  1111. if ($raw) {
  1112. $result = $speedData['speeddown'];
  1113. }
  1114. } else {
  1115. $result = __('Unlimited');
  1116. }
  1117. } else {
  1118. $result = __('None');
  1119. }
  1120. return ($result);
  1121. }
  1122. /**
  1123. * Returns all tariff speeds
  1124. *
  1125. * @param bool $rawMbitSpeeds
  1126. *
  1127. * @return array
  1128. */
  1129. function zbs_TariffGetAllSpeeds($rawMbitSpeeds = false, $offset = 1024) {
  1130. $query = "SELECT * from `speeds`";
  1131. $speedData = simple_queryall($query);
  1132. $result = array();
  1133. if (!empty($speedData)) {
  1134. foreach ($speedData as $io => $each) {
  1135. if ($each['speeddown'] != 0) {
  1136. if (!$rawMbitSpeeds) {
  1137. if ($each['speeddown'] < $offset) {
  1138. $speed = $each['speeddown'] . ' ' . __('Kbit/s');
  1139. } else {
  1140. $speed = ($each['speeddown'] / $offset) . ' ' . __('Mbit/s');
  1141. }
  1142. } else {
  1143. $speed = $each['speeddown'] . ' ' . __('Mbit/s');
  1144. }
  1145. } else {
  1146. $speed = __('Unlimited');
  1147. }
  1148. $result[$each['tariff']] = $speed;
  1149. }
  1150. }
  1151. return ($result);
  1152. }
  1153. /**
  1154. * Returns speed override by login, if available
  1155. *
  1156. * @param string $login
  1157. * @return int
  1158. */
  1159. function zbs_SpeedGetOverride($login) {
  1160. $offset = 1024;
  1161. $login = mysql_real_escape_string($login);
  1162. $query = "SELECT * from `userspeeds` WHERE `login`='" . $login . "'";
  1163. $speedData = simple_query($query);
  1164. $result = 0;
  1165. if (!empty($speedData)) {
  1166. $result = $speedData['speed'];
  1167. }
  1168. return ($result);
  1169. }
  1170. /**
  1171. * Returns array of all available virtual services
  1172. *
  1173. * @return array
  1174. */
  1175. function zbs_getVservicesAll($excludeArchived = false) {
  1176. $result = array();
  1177. $vservDb = new NyanORM('vservices');
  1178. if ($excludeArchived) {
  1179. $vservDb->where('archived', '=', '0');
  1180. }
  1181. $all = $vservDb->getAll();
  1182. if (!empty($all)) {
  1183. foreach ($all as $io => $each) {
  1184. $result[$each['tagid']] = $each['price'];
  1185. }
  1186. }
  1187. return ($result);
  1188. }
  1189. /**
  1190. * Returns all vservices data with tag names, optionally excluding archived services
  1191. *
  1192. * @param $excludeArchived
  1193. *
  1194. * @return array
  1195. */
  1196. function zbs_getVservicesAllWithNames($excludeArchived = false) {
  1197. $result = array();
  1198. $vservDb = new NyanORM('vservices');
  1199. if ($excludeArchived) {
  1200. $vservDb->where('archived', '=', '0');
  1201. }
  1202. $allVsrvs = $vservDb->getAll('tagid');
  1203. if (!empty($allVsrvs)) {
  1204. $allTagNames = zbs_getTagNames();
  1205. foreach ($allVsrvs as $io => $each) {
  1206. $result[$each['tagid']] = $each;
  1207. $result[$each['tagid']]['vsrvname'] = (empty($allTagNames[$each['tagid']]) ? '' : $allTagNames[$each['tagid']]);
  1208. }
  1209. }
  1210. return ($result);
  1211. }
  1212. /**
  1213. * Returns array of tag names as id=>name
  1214. *
  1215. * @return array
  1216. */
  1217. function zbs_getTagNames() {
  1218. $result = array();
  1219. $query = "SELECT * from `tagtypes`";
  1220. $all = simple_queryall($query);
  1221. if (!empty($all)) {
  1222. foreach ($all as $io => $each) {
  1223. $result[$each['id']] = $each['tagname'];
  1224. }
  1225. }
  1226. return ($result);
  1227. }
  1228. /**
  1229. * Returns array of tags associated with user
  1230. *
  1231. * @param string $login
  1232. * @return array
  1233. */
  1234. function zbs_getUserTags($login) {
  1235. $result = array();
  1236. $login = mysql_real_escape_string($login);
  1237. $query = "SELECT * from `tags` WHERE `login`='" . $login . "';";
  1238. $all = simple_queryall($query);
  1239. if (!empty($all)) {
  1240. foreach ($all as $io => $each) {
  1241. $result[$each['tagid']] = $each['id'];
  1242. }
  1243. }
  1244. return ($result);
  1245. }
  1246. /**
  1247. * Returns array of user assigned tags as login => array($dbID => $tagID)
  1248. *
  1249. * This function ensures the uniqueness of selected tags assigned to user
  1250. * if user has one and the same tag assigned multiple times for some reason
  1251. *
  1252. * @param string $login
  1253. * @param string $whereTagID
  1254. *
  1255. * @return array
  1256. */
  1257. function zbs_UserGetAllTagsUnique($login = '', $whereTagID = '') {
  1258. $result = array();
  1259. $queryWhere = (empty($login)) ? '' : " WHERE `login` = '" . $login . "'";
  1260. if (!empty($whereTagID)) {
  1261. if (empty($queryWhere)) {
  1262. $queryWhere = " WHERE `tagid` IN(" . $whereTagID . ")";
  1263. } else {
  1264. $queryWhere .= " AND `tagid` IN(" . $whereTagID . ")";
  1265. }
  1266. }
  1267. $query = "SELECT * from `tags`" . $queryWhere;
  1268. $allTags = simple_queryall($query);
  1269. if (!empty($allTags)) {
  1270. foreach ($allTags as $io => $each) {
  1271. $result[$each['login']][$each['id']] = $each['tagid'];
  1272. }
  1273. }
  1274. return ($result);
  1275. }
  1276. /**
  1277. * Returns array of available virtualservices as Service:id=>tagname
  1278. *
  1279. * @return array
  1280. */
  1281. function zbs_VservicesGetAllNamesLabeled() {
  1282. $result = array();
  1283. $allservices = zbs_getVservicesAll();
  1284. $alltagnames = zbs_getTagNames();
  1285. if (!empty($allservices)) {
  1286. foreach ($allservices as $io => $eachservice) {
  1287. @$result['Service:' . $eachservice['id']] = $alltagnames[$eachservice['tagid']];
  1288. }
  1289. }
  1290. return ($result);
  1291. }
  1292. /**
  1293. * Returns total cost of all additional services.
  1294. *
  1295. * @param string $login user's login
  1296. *
  1297. * @return float for all virtual services if such exists for user
  1298. */
  1299. function zbs_vservicesGetUserPrice($login) {
  1300. $price = 0;
  1301. $tag_query = "SELECT * FROM `tags` WHERE `login` = '" . $login . "' ";
  1302. $alltags = simple_queryall($tag_query);
  1303. $VS_query = "SELECT * FROM `vservices`";
  1304. $allVS = simple_queryall($VS_query);
  1305. if (!empty($alltags)) {
  1306. foreach ($alltags as $io => $eachtag) {
  1307. foreach ($allVS as $each => $eachSrv) {
  1308. if ($eachtag['tagid'] == $eachSrv['tagid']) {
  1309. $price += $eachSrv['price'];
  1310. }
  1311. }
  1312. }
  1313. }
  1314. return ($price);
  1315. }
  1316. /**
  1317. * Returns array of all available virtual services as tagid => array('price' => $price, 'period' => $period);
  1318. *
  1319. * @return array
  1320. */
  1321. function zbs_vservicesGetAllPricesPeriods($excludeArchived = false) {
  1322. $result = array();
  1323. $vservDb = new NyanORM('vservices');
  1324. if ($excludeArchived) {
  1325. $vservDb->where('archived', '=', '0');
  1326. }
  1327. $all = $vservDb->getAll();
  1328. if (!empty($all)) {
  1329. foreach ($all as $io => $each) {
  1330. $result[$each['tagid']] = array('price' => $each['price'], 'daysperiod' => $each['charge_period_days']);
  1331. }
  1332. }
  1333. return ($result);
  1334. }
  1335. /**
  1336. * Returns price total of all virtual services fees assigned to user, considering services fee charge periods
  1337. * $defaultPeriod - specifies the default period to count the prices for: 'month' or 'day', if certain service has no fee charge period set
  1338. *
  1339. * @param string $login
  1340. * @param string $defaultPeriod
  1341. *
  1342. * @return float
  1343. */
  1344. function zbs_vservicesGetUserPricePeriod($login, $defaultPeriod = 'month') {
  1345. $totalVsrvPrice = 0;
  1346. $allUserVsrvs = zbs_vservicesGetUsersAll($login, true);
  1347. $curMonthDays = date('t');
  1348. if (!empty($allUserVsrvs)) {
  1349. $allUserVsrvs = $allUserVsrvs[$login];
  1350. foreach ($allUserVsrvs as $eachTagDBID => $eachSrvData) {
  1351. $curVsrvPrice = $eachSrvData['price'];
  1352. $curVsrvDaysPeriod = $eachSrvData['daysperiod'];
  1353. $dailyVsrvPrice = 0;
  1354. // getting daily vservice price
  1355. if (!empty($curVsrvDaysPeriod)) {
  1356. $dailyVsrvPrice = ($curVsrvDaysPeriod > 1) ? $curVsrvPrice / $curVsrvDaysPeriod : $curVsrvPrice;
  1357. }
  1358. // if vservice has no charge period set and $dailyVsrvPrice == 0
  1359. // then virtual service price is considered as for global $defaultPeriod period
  1360. if ($defaultPeriod == 'month') {
  1361. $totalVsrvPrice += (empty($dailyVsrvPrice)) ? $curVsrvPrice : $dailyVsrvPrice * $curMonthDays;
  1362. } else {
  1363. $totalVsrvPrice += (empty($dailyVsrvPrice)) ? $curVsrvPrice : $dailyVsrvPrice;
  1364. }
  1365. }
  1366. }
  1367. return ($totalVsrvPrice);
  1368. }
  1369. /**
  1370. * Returns all users with assigned virtual services as array:
  1371. * login => array($tagDBID => vServicePrice1)
  1372. *
  1373. * if $includePeriod is true returned array will look like this:
  1374. * login => array($tagDBID => array('price' => vServicePrice1, 'daysperiod' => vServicePeriod1))
  1375. *
  1376. * if $includeVSrvName is true 'vsrvname' => tagname is added to the end of the array
  1377. *
  1378. * @param string $login
  1379. * @param bool $includePeriod
  1380. * @param bool $includeVSrvName
  1381. *
  1382. * @return array
  1383. */
  1384. function zbs_vservicesGetUsersAll($login = '', $includePeriod = false, $includeVSrvName = false, $excludeArchived = false) {
  1385. $result = array();
  1386. $allTagNames = array();
  1387. $allUserTags = zbs_UserGetAllTagsUnique($login);
  1388. if ($includeVSrvName) {
  1389. $allTagNames = zbs_getTagNames();
  1390. }
  1391. //user have some tags assigned
  1392. if (!empty($allUserTags)) {
  1393. $vservicePrices = ($includePeriod) ? zbs_vservicesGetAllPricesPeriods($excludeArchived) : zbs_getVservicesAll($excludeArchived);
  1394. foreach ($allUserTags as $eachLogin => $data) {
  1395. $tmpArr = array();
  1396. foreach ($data as $tagDBID => $tagID) {
  1397. if (isset($vservicePrices[$tagID])) {
  1398. if ($includeVSrvName) {
  1399. $tmpArr[$tagDBID] = $vservicePrices[$tagID] + array('vsrvname' => $allTagNames[$tagID]);
  1400. } else {
  1401. $tmpArr[$tagDBID] = $vservicePrices[$tagID];
  1402. }
  1403. }
  1404. }
  1405. if (!empty($tmpArr)) {
  1406. $result[$eachLogin] = $tmpArr;
  1407. }
  1408. }
  1409. }
  1410. return ($result);
  1411. }
  1412. /**
  1413. * Renders list of associated with user virtual services
  1414. *
  1415. * @param string $login
  1416. * @param string $currency
  1417. *
  1418. * @return string
  1419. */
  1420. function zbs_vservicesShow($login, $currency) {
  1421. global $us_config;
  1422. $result = '';
  1423. $vservicesPeriodON = (!empty($us_config['VSERVICES_CONSIDER_PERIODS']));
  1424. $userServices = zbs_vservicesGetUsersAll($login, true, true);
  1425. //yep, this user have some services assigned
  1426. if (!empty($userServices)) {
  1427. $userServices = $userServices[$login];
  1428. $cells = la_TableCell(__('Service'), '60%');
  1429. $cells .= la_TableCell(__('Terms'));
  1430. $rows = la_TableRow($cells, 'row1');
  1431. foreach ($userServices as $eachDBID => $eachsSrvice) {
  1432. if ($vservicesPeriodON) {
  1433. if ($eachsSrvice['price'] >= 0) {
  1434. $servicePrice = __('Price') . ' ' . $eachsSrvice['price'] . ' ' . $currency . ' / ' . $eachsSrvice['daysperiod'] . ' ' . __('day');
  1435. } else {
  1436. $servicePrice = __('Bonus') . ' ' . abs($eachsSrvice['price']) . ' ' . $currency . ' / ' . $eachsSrvice['daysperiod'] . ' ' . __('day');
  1437. }
  1438. } else {
  1439. if ($eachsSrvice['price'] >= 0) {
  1440. $servicePrice = __('Price') . ' ' . $eachsSrvice['price'] . ' ' . $currency;
  1441. } else {
  1442. $servicePrice = __('Bonus') . ' ' . abs($eachsSrvice['price']) . ' ' . $currency;
  1443. }
  1444. }
  1445. $cells = la_TableCell($eachsSrvice['vsrvname']);
  1446. $cells .= la_TableCell($servicePrice);
  1447. $rows .= la_TableRow($cells, 'row3');
  1448. }
  1449. $result .= la_tag('br');
  1450. $result .= la_tag('h3') . __('Additional services') . la_tag('h3', true);
  1451. $result .= la_TableBody($rows, '100%', 0);
  1452. }
  1453. if ($us_config['VSERVICES_SHOW'] == 2) {
  1454. //TODO
  1455. }
  1456. return ($result);
  1457. }
  1458. /**
  1459. * Renders custom discount percent if CUD_SHOW option enabled
  1460. *
  1461. * @param string $login
  1462. * @param array $us_config
  1463. *
  1464. * @return string
  1465. */
  1466. function zbs_CUDShow($login, $us_config) {
  1467. $result = '';
  1468. $login = mysql_real_escape_string($login);
  1469. if (isset($us_config['CUD_SHOW'])) {
  1470. if ($us_config['CUD_SHOW']) {
  1471. $query = "SELECT * from `cudiscounts` WHERE `login`='" . $login . "';";
  1472. $data = simple_query($query);
  1473. if (!empty($data)) {
  1474. $discount = $data['discount'];
  1475. $cells = la_TableCell(__('Discount'), '', 'row1');
  1476. $cells .= la_TableCell($discount . '%');
  1477. $result = la_TableRow($cells);
  1478. }
  1479. }
  1480. }
  1481. return ($result);
  1482. }
  1483. /**
  1484. * Renders payment cards promocodes form
  1485. *
  1486. * @param string $login
  1487. *
  1488. * @return string
  1489. */
  1490. function zbs_PCPromoCodesForm($login) {
  1491. $result = '';
  1492. $inputs = la_tag('br');
  1493. $inputs .= la_TextInput('promocard', '', '', false, 25, '');
  1494. $inputs .= la_Submit(__('Use the promo code'), 'anreadbutton');
  1495. $inputs .= la_delimiter();
  1496. $result .= la_Form('', 'POST', $inputs, '');
  1497. return ($result);
  1498. }
  1499. /**
  1500. * Returns promocode success notification
  1501. *
  1502. * @param string $promoCode
  1503. *
  1504. * @return string
  1505. */
  1506. function zbs_getPromoAwesomeness($promoCode = '') {
  1507. global $us_config;
  1508. $result = '';
  1509. $promoCode = ubRouting::filters($promoCode, 'vf');
  1510. @$awesomeness = rcms_scandir('skins/awesomeness/');
  1511. if (!empty($awesomeness)) {
  1512. $awesomenessRnd = array_rand($awesomeness);
  1513. $awesomeness = $awesomeness[$awesomenessRnd];
  1514. //ugly hack to prevent elements autofocusing
  1515. $result .= la_TextInput('dontfocusonlinks', '', '', false, '', '', '', '', 'style="width: 0; height: 0; top: -100px; position: absolute;"');
  1516. $result .= la_tag('center');
  1517. $result .= la_img_sized('skins/awesomeness/' . $awesomeness, '', '256');
  1518. $result .= la_delimiter();
  1519. $result .= __('Congratulations! You have successfully used a promo code');
  1520. if (!empty($promoCode)) {
  1521. $result .= ' ' . $promoCode;
  1522. }
  1523. $result .= '. ' . la_tag('br');
  1524. if (@$us_config['PC_QUEUED']) {
  1525. $result .= __('The funds are already on the way and will be on your account in a minute or two') . '.';
  1526. } else {
  1527. $result .= __('The funds are already in your account') . '.';
  1528. }
  1529. $result .= la_delimiter();
  1530. $result .= la_Link('index.php', __('I see'), false, 'anreadbutton');
  1531. $result .= la_tag('center', true);
  1532. }
  1533. return ($result);
  1534. }
  1535. /**
  1536. * Renders payment cards promocodes controller if PC_AS_PROMO option enabled
  1537. *
  1538. * @param string $login
  1539. *
  1540. * @return string
  1541. */
  1542. function zbs_PCPromoCodesController($login) {
  1543. global $us_config;
  1544. global $user_ip;
  1545. $result = '';
  1546. $payCardInput = '';
  1547. $login = ubRouting::filters($login, 'mres');
  1548. if (isset($us_config['PC_AS_PROMO'])) {
  1549. if ($us_config['PC_AS_PROMO']) {
  1550. $pc_brute = $us_config['PC_BRUTE'];
  1551. $bruteCheck = zbs_PayCardCheckBrute($user_ip, $pc_brute);
  1552. //success notification
  1553. if (ubRouting::checkGet('successcode')) {
  1554. $result .= la_modalOpened(__('Success') . '!', zbs_getPromoAwesomeness(ubRouting::get('successcode')));
  1555. }
  1556. //promocode request processing
  1557. if (ubRouting::checkPost('promocard')) {
  1558. $payCardInput = ubRouting::post('promocard', 'vf');
  1559. }
  1560. if (!empty($payCardInput)) {
  1561. if (!$bruteCheck) {
  1562. $series = false;
  1563. if ($us_config['PC_SERIES_AND_SN']) {
  1564. $serialNumber = substr($payCardInput, $us_config['PC_SERIES_LENGTH']);
  1565. $series = str_replace($serialNumber, '', $payCardInput);
  1566. $payCardInput = $serialNumber;
  1567. }
  1568. //use this card
  1569. if (zbs_PaycardCheck($payCardInput, $series)) {
  1570. if (!@$us_config['PC_QUEUED']) {
  1571. zbs_PaycardUse($payCardInput, true);
  1572. } else {
  1573. //or mark it for queue processing
  1574. zbs_PaycardQueue($payCardInput, true);
  1575. }
  1576. //redirect to succes notification
  1577. ubRouting::nav('?successcode=' . $payCardInput);
  1578. } else {
  1579. show_window(__('Error'), __('Invalid promo code'));
  1580. }
  1581. }
  1582. }
  1583. //input form rendering
  1584. if ($bruteCheck) {
  1585. $form = __('Sorry, but you have a limit number of attempts');
  1586. } else {
  1587. $form = zbs_PCPromoCodesForm($login);
  1588. }
  1589. $control = la_modalAuto(__('I have promo code'), __('Use the promo code'), $form, '');
  1590. $cells = la_TableCell(__('Promo code'), '', 'row1');
  1591. $cells .= la_TableCell($control);
  1592. $result .= la_TableRow($cells);
  1593. }
  1594. }
  1595. return ($result);
  1596. }
  1597. /**
  1598. * Renders form for change user password
  1599. *
  1600. * @param string $login
  1601. * @return string
  1602. */
  1603. function zbs_UserChangePassword($login) {
  1604. $result = '';
  1605. if (isset($login)) {
  1606. // change password if need
  1607. if (la_CheckPost(array('newpassword', 'confirmnewpassword'))) {
  1608. if ($_POST['newpassword'] == $_POST['confirmnewpassword']) {
  1609. $current_password = zbs_UserGetStargazerData($login);
  1610. $current_password = $current_password['Password'];
  1611. if ($current_password == $_POST['upassword']) {
  1612. $password = $_POST['newpassword'];
  1613. $password = vf($password);
  1614. $password = preg_replace('#[^a-z0-9A-Z\-_\.]#Uis', '', $password);
  1615. $password = preg_replace('/\0/s', '', $password);
  1616. if (strlen($password) >= 5) {
  1617. billing_setpassword($login, $password);
  1618. log_register('CHANGE Password (' . $login . ') ON `' . $password . '` BY USER');
  1619. // After change pass need login again for set cookies
  1620. rcms_redirect("index.php");
  1621. } else {
  1622. $content = __('Password must contain 5 and more characters');
  1623. $result .= la_modalOpened(__('Error'), $content, 300, 200);
  1624. }
  1625. } else {
  1626. $content = __('Incorrect current password');
  1627. $result .= la_modalOpened(__('Error'), $content, 300, 200);
  1628. }
  1629. } else {
  1630. $content = __('Passwords do not match');
  1631. $result .= la_modalOpened(__('Error'), $content, 300, 200);
  1632. }
  1633. }
  1634. // Edit form construct
  1635. $inputs = la_tag('label') . __('Current password') . la_tag('label', true) . la_tag('br');
  1636. $inputs .= la_PasswordInput('upassword', '', '', true) . la_tag('br');
  1637. $inputs .= la_tag('label') . __('New password') . la_tag('label', true) . la_tag('br');
  1638. $inputs .= la_PasswordInput('newpassword', '', '', true);
  1639. $inputs .= la_tag('label') . __('Confirm new password') . la_tag('label', true) . la_tag('br');
  1640. $inputs .= la_PasswordInput('confirmnewpassword', '', '', true) . la_tag('br');
  1641. $inputs .= la_Submit(__('Change password'));
  1642. $form = la_Form('index.php', "POST", $inputs, 'glamour');
  1643. $result .= la_modal(__('Change password'), __('Change password'), $form, '', 300, 370);
  1644. }
  1645. return ($result);
  1646. }
  1647. /**
  1648. * Renders user profile
  1649. *
  1650. * @param string $login
  1651. * @return string
  1652. */
  1653. function zbs_UserShowProfile($login) {
  1654. $us_config = zbs_LoadConfig();
  1655. $renderEmailFlag = true;
  1656. $us_currency = $us_config['currency'];
  1657. if (isset($us_config['HIDE_EMAIL'])) {
  1658. $renderEmailFlag = ($us_config['HIDE_EMAIL']) ? false : true;
  1659. }
  1660. $userdata = zbs_UserGetStargazerData($login);
  1661. $alladdress = zbs_AddressGetFulladdresslist();
  1662. $allrealnames = zbs_UserGetAllRealnames();
  1663. $contract = zbs_UserGetContract($login);
  1664. if ($renderEmailFlag) {
  1665. $email = zbs_UserGetEmail($login);
  1666. }
  1667. $mobile = zbs_UserGetMobile($login);
  1668. $phone = zbs_UserGetPhone($login);
  1669. $passive = $userdata['Passive'];
  1670. $down = $userdata['Down'];
  1671. $userpassword = $userdata['Password'];
  1672. // Allow user change password
  1673. // With this option we hide current password user
  1674. if (isset($us_config['PASSWORD_CHANGE']) and $us_config['PASSWORD_CHANGE'] == 1) {
  1675. $userpassword = zbs_UserChangePassword($login);
  1676. }
  1677. $skinPath = zbs_GetCurrentSkinPath($us_config);
  1678. $iconsPath = $skinPath . 'iconz/';
  1679. //public offer mode
  1680. if (isset($us_config['PUBLIC_OFFER'])) {
  1681. if (!empty($us_config['PUBLIC_OFFER'])) {
  1682. $publicOfferUrl = $us_config['PUBLIC_OFFER'];
  1683. $publicOfferCaption = (empty($us_config['PUBLIC_OFFER_CAPTION']) ? __('Public offer') : $us_config['PUBLIC_OFFER_CAPTION']);
  1684. $contract = la_Link($publicOfferUrl, $publicOfferCaption, false, '');
  1685. }
  1686. }
  1687. // START OF ONLINELEFT COUNTING <<
  1688. if ($us_config['ONLINELEFT_COUNT'] != 0) {
  1689. $userBalance = $userdata['Cash'];
  1690. $userTariff = $userdata['Tariff'];
  1691. $balanceExpire = zbs_GetOnlineLeftCount($login, $userBalance, $userTariff, false);
  1692. } else {
  1693. $balanceExpire = '';
  1694. }
  1695. // >> END OF ONLINELEFT COUNTING
  1696. if ($userdata['CreditExpire'] != 0) {
  1697. $credexpire = date("d-m-Y", $userdata['CreditExpire']);
  1698. } else {
  1699. $credexpire = '';
  1700. }
  1701. // pasive state check
  1702. if ($passive) {
  1703. $passive_state = __('Account frozen');
  1704. } else {
  1705. $passive_state = __('Account active');
  1706. }
  1707. //down state check
  1708. if ($down) {
  1709. $down_state = ' + ' . __('Disabled');
  1710. } else {
  1711. $down_state = '';
  1712. }
  1713. //payment id handling
  1714. if ($us_config['OPENPAYZ_ENABLED']) {
  1715. if ($us_config['OPENPAYZ_REALID']) {
  1716. $paymentid = zbs_PaymentIDGet($login);
  1717. } else {
  1718. $paymentid = ip2int($userdata['IP']);
  1719. }
  1720. } else {
  1721. $paymentid = 0;
  1722. }
  1723. //payment id qr dialog
  1724. $paymentidqr = '';
  1725. if (isset($us_config['PAYMENTID_QR'])) {
  1726. if ($us_config['PAYMENTID_QR']) {
  1727. $paymentidqr = la_modalAuto(la_img($iconsPath . 'qrcode.png', 'QR-code'), __('Payment ID'), la_tag('center') . la_img('modules/jsc/qrgen.php?data=' . $paymentid) . la_tag('center', true), '', '300', '250');
  1728. }
  1729. }
  1730. //draw order link
  1731. if ($us_config['DOCX_SUPPORT']) {
  1732. $zdocsLink = ' ' . la_Link('?module=zdocs', __('Draw order'), false, 'printorder');
  1733. } else {
  1734. $zdocsLink = '';
  1735. }
  1736. //tariff speeds
  1737. if ($us_config['SHOW_SPEED']) {
  1738. $rawSpeedMbits = (@$us_config['SHOW_SPEED_MB']) ? true : false;
  1739. $speedOffset = (@$us_config['SHOW_SPEED_1000B']) ? 1000 : 1024;
  1740. $userSpeedOverride = zbs_SpeedGetOverride($login);
  1741. if ($userSpeedOverride == 0) {
  1742. $showSpeed = zbs_TariffGetSpeed($userdata['Tariff'], $rawSpeedMbits, $speedOffset);
  1743. } else {
  1744. if (!$rawSpeedMbits) {
  1745. if ($userSpeedOverride < $speedOffset) {
  1746. $showSpeed = $userSpeedOverride . ' ' . __('Kbit/s');
  1747. } else {
  1748. $showSpeed = ($userSpeedOverride / $speedOffset) . ' ' . __('Mbit/s');
  1749. }
  1750. } else {
  1751. $showSpeed = $userSpeedOverride;
  1752. }
  1753. }
  1754. if ($rawSpeedMbits) {
  1755. if (is_numeric($showSpeed)) {
  1756. $showSpeed .= ' ' . __('Mbit/s');
  1757. }
  1758. }
  1759. $tariffSpeeds = la_TableRow(la_TableCell(__('Tariff speed'), '', 'row1') . la_TableCell($showSpeed));
  1760. } else {
  1761. $tariffSpeeds = '';
  1762. }
  1763. if ($us_config['ROUND_PROFILE_CASH']) {
  1764. $Cash = web_roundValue($userdata['Cash'], 2);
  1765. } else {
  1766. $Cash = $userdata['Cash'];
  1767. }
  1768. $profile = la_tag('table', false, '', 'width="100%" border="0" cellpadding="2" cellspacing="3"');
  1769. $profile .= la_tag('tr');
  1770. $profile .= la_TableCell(__('Address'), '', 'row1');
  1771. $profile .= la_TableCell(@$alladdress[$login]);
  1772. $profile .= la_tag('tr', true);
  1773. $profile .= la_tag('tr');
  1774. $profile .= la_TableCell(__('Real name'), '', 'row1');
  1775. $profile .= la_TableCell(@$allrealnames[$login]);
  1776. $profile .= la_tag('tr', true);
  1777. if (!@$us_config['LOGINHIDE']) {
  1778. $profile .= la_tag('tr');
  1779. $profile .= la_TableCell(__('Login'), '', 'row1');
  1780. $profile .= la_TableCell($login);
  1781. $profile .= la_tag('tr', true);
  1782. }
  1783. if (!@$us_config['PASSWORDSHIDE']) {
  1784. $profile .= la_tag('tr');
  1785. $profile .= la_TableCell(__('Password'), '', 'row1');
  1786. $profile .= la_TableCell($userpassword);
  1787. $profile .= la_tag('tr', true);
  1788. }
  1789. $profile .= la_tag('tr');
  1790. $profile .= la_TableCell(__('IP'), '', 'row1');
  1791. $profile .= la_TableCell($userdata['IP']);
  1792. $profile .= la_tag('tr', true);
  1793. $profile .= la_tag('tr');
  1794. $profile .= la_TableCell(__('Phone'), '', 'row1');
  1795. $profile .= la_TableCell($phone);
  1796. $profile .= la_tag('tr', true);
  1797. $profile .= la_tag('tr');
  1798. $profile .= la_TableCell(__('Mobile'), '', 'row1');
  1799. $profile .= la_TableCell($mobile);
  1800. $profile .= la_tag('tr', true);
  1801. if (@$us_config['SHOW_EXT_MOBILES']) {
  1802. $mobilesExt = new UserstatsMobilesExt($login);
  1803. $profile .= la_tag('tr');
  1804. $profile .= la_TableCell(__('Additional mobile'), '', 'row1');
  1805. $profile .= la_TableCell($mobilesExt->renderUserMobiles());
  1806. $profile .= la_tag('tr', true);
  1807. }
  1808. if ($renderEmailFlag) {
  1809. $profile .= la_tag('tr');
  1810. $profile .= la_TableCell(__('Email'), '', 'row1');
  1811. $profile .= la_TableCell($email);
  1812. $profile .= la_tag('tr', true);
  1813. }
  1814. if ($us_config['OPENPAYZ_ENABLED']) {
  1815. $profile .= la_tag('tr');
  1816. $payIdAbbr = la_tag('abbr', false, '', 'title="' . __('Payment ID is used to make online payments using a variety of payment systems as well as the funding of accounts using the terminals') . '"');
  1817. $payIdAbbr .= __('Payment ID');
  1818. $payIdAbbr .= la_tag('abbr', true);
  1819. $profile .= la_TableCell($payIdAbbr, '', 'row1');
  1820. $profile .= la_TableCell($paymentid . ' ' . $paymentidqr);
  1821. $profile .= la_tag('tr', true);
  1822. }
  1823. $profile .= la_tag('tr');
  1824. $profile .= la_TableCell(__('Contract'), '', 'row1');
  1825. $profile .= la_TableCell($contract);
  1826. $profile .= la_tag('tr', true);
  1827. $profile .= la_tag('tr');
  1828. $profile .= la_TableCell(__('Balance'), '', 'row1');
  1829. $profile .= la_TableCell($Cash . ' ' . $us_currency . $balanceExpire . $zdocsLink);
  1830. $profile .= la_tag('tr', true);
  1831. $profile .= la_tag('tr');
  1832. $profile .= la_TableCell(__('Credit'), '', 'row1');
  1833. $profile .= la_TableCell($userdata['Credit'] . ' ' . $us_currency);
  1834. $profile .= la_tag('tr', true);
  1835. $profile .= la_tag('tr');
  1836. $profile .= la_TableCell(__('Credit Expire'), '', 'row1');
  1837. $profile .= la_TableCell($credexpire);
  1838. $profile .= la_tag('tr', true);
  1839. $profile .= la_tag('tr');
  1840. $profile .= la_TableCell(__('Tariff'), '', 'row1');
  1841. $profile .= la_TableCell(__($userdata['Tariff']));
  1842. $profile .= la_tag('tr', true);
  1843. //Power tariffs suport enabled?
  1844. if (@$us_config['POWERTARIFFS_ENABLED']) {
  1845. $powerTariffs = new NyanORM('pt_tariffs');
  1846. $allPowerTariffs = $powerTariffs->getAll('tariff');
  1847. $feeDay = 0;
  1848. if (isset($allPowerTariffs[$userdata['Tariff']])) {
  1849. //custom tariff price
  1850. $tariffPrice = $allPowerTariffs[$userdata['Tariff']]['fee'];
  1851. //getting custom fee date for this user
  1852. if (!empty($powerUserData)) {
  1853. $feeDay = zbs_GetPowerTariffDay($login);
  1854. }
  1855. } else {
  1856. //this is just normal tariff
  1857. @$tariffPrice = zbs_UserGetTariffPrice($userdata['Tariff']);
  1858. }
  1859. } else {
  1860. @$tariffPrice = zbs_UserGetTariffPrice($userdata['Tariff']);
  1861. }
  1862. //tariff price here
  1863. $profile .= la_tag('tr');
  1864. $profile .= la_TableCell(__('Tariff price'), '', 'row1');
  1865. $profile .= la_TableCell($tariffPrice . ' ' . $us_currency);
  1866. $profile .= la_tag('tr', true);
  1867. //render custom fee day
  1868. if (@$us_config['POWERTARIFFS_ENABLED']) {
  1869. if ($feeDay != 0) {
  1870. $profile .= la_tag('tr');
  1871. $profile .= la_TableCell(__('Day of the next tariff fee'), '', 'row1');
  1872. $profile .= la_TableCell($feeDay);
  1873. $profile .= la_tag('tr', true);
  1874. }
  1875. }
  1876. $profile .= $tariffSpeeds;
  1877. $profile .= la_tag('tr');
  1878. $profile .= la_TableCell(__('Tariff change'), '', 'row1');
  1879. $profile .= la_TableCell(__($userdata['TariffChange']));
  1880. $profile .= la_tag('tr', true);
  1881. $profile .= la_tag('tr');
  1882. $profile .= la_TableCell(__('Account state'), '', 'row1');
  1883. $profile .= la_TableCell($passive_state . $down_state);
  1884. $profile .= la_tag('tr', true);
  1885. if (@$us_config['TG_BOTNAME']) {
  1886. $tgBotName = $us_config['TG_BOTNAME'];
  1887. $tgBotUrl = 'https://t.me/' . $tgBotName . '/?start=' . $login . '-' . md5($userdata['Password']);
  1888. $profile .= la_tag('tr');
  1889. $profile .= la_TableCell(__('Telegram bot'), '', 'row1');
  1890. $profile .= la_TableCell(la_Link($tgBotUrl, __('Connect to bot') . '!'));
  1891. $profile .= la_tag('tr', true);
  1892. }
  1893. $profile .= zbs_CUDShow($login, $us_config);
  1894. $profile .= zbs_PCPromoCodesController($login);
  1895. $profile .= la_tag('table', true);
  1896. //show assigned virtual services if available
  1897. if (isset($us_config['VSERVICES_SHOW'])) {
  1898. if ($us_config['VSERVICES_SHOW']) {
  1899. $profile .= zbs_vservicesShow($login, $us_currency);
  1900. }
  1901. }
  1902. return ($profile);
  1903. }
  1904. /**
  1905. * Returns payments array for some user
  1906. *
  1907. * @param string $login
  1908. * @param bool $onlyPositive
  1909. *
  1910. * @return array
  1911. */
  1912. function zbs_CashGetUserPayments($login, $onlyPositive = false) {
  1913. $login = vf($login);
  1914. $additionalFilters = '';
  1915. if ($onlyPositive) {
  1916. $additionalFilters .= " AND `summ`>'0' ";
  1917. }
  1918. $query = "SELECT * from `payments` WHERE `login`='" . $login . "' " . $additionalFilters . " ORDER BY `id` DESC";
  1919. $allpayments = simple_queryall($query);
  1920. return ($allpayments);
  1921. }
  1922. /**
  1923. * Renders user traffic stats report
  1924. *
  1925. * @param string $login
  1926. * @return string
  1927. */
  1928. function zbs_UserTraffStats($login) {
  1929. global $us_config;
  1930. $login = vf($login);
  1931. $alldirs = zbs_DirectionsGetAll();
  1932. $monthnames = zbs_months_array_wz();
  1933. $ishimuraOption = 'ISHIMURA_ENABLED';
  1934. $ishimuraTable = 'mlg_ishimura';
  1935. $ophanimOption = 'OPHANIM_ENABLED';
  1936. $ophanimTable = 'ophtraff';
  1937. /*
  1938. * Current month traffic stats
  1939. */
  1940. $result = la_tag('h3') . __('Current month traffic stats') . la_tag('h3', true);
  1941. $cells = la_TableCell(__('Traffic classes'));
  1942. $cells .= la_TableCell(__('Downloaded'));
  1943. $cells .= la_TableCell(__('Uploaded'));
  1944. $cells .= la_TableCell(__('Total'));
  1945. $rows = la_TableRow($cells, 'row1');
  1946. if (!empty($alldirs)) {
  1947. foreach ($alldirs as $io => $eachdir) {
  1948. $query_downup = "SELECT `D" . $eachdir['rulenumber'] . "`,`U" . $eachdir['rulenumber'] . "` from `users` WHERE `login`='" . $login . "'";
  1949. $downup = simple_query($query_downup);
  1950. //yeah, no classes at all
  1951. if ($eachdir['rulenumber'] == 0) {
  1952. if ($us_config[$ishimuraOption]) {
  1953. //ishimura traffic stats
  1954. $query_hideki = "SELECT `D0`,`U0` from `" . $ishimuraTable . "` WHERE `login`='" . $login . "' AND `month`='" . date("n") . "' AND `year`='" . date("Y") . "'";
  1955. $dataHideki = simple_query($query_hideki);
  1956. if (isset($downup['D0'])) {
  1957. if (isset($dataHideki['D0'])) {
  1958. $downup['D0'] += $dataHideki['D0'];
  1959. $downup['U0'] += $dataHideki['U0'];
  1960. }
  1961. } else {
  1962. $downup['D0'] = $dataHideki['D0'];
  1963. $downup['U0'] = $dataHideki['U0'];
  1964. }
  1965. }
  1966. //OphanimFlow integration
  1967. if ($us_config[$ophanimOption]) {
  1968. $ophDb = new NyanORM($ophanimTable);
  1969. $ophDb->selectable(array('login', 'D0', 'U0'));
  1970. $ophDb->where('login', '=', $login);
  1971. $ophDb->where('year', '=', date("Y"));
  1972. $ophDb->where('month', '=', date("n"));
  1973. $rawOphTraff = $ophDb->getAll();
  1974. if (isset($downup['D0'])) {
  1975. if (!empty($rawOphTraff)) {
  1976. $downup['D0'] += $rawOphTraff[0]['D0'];
  1977. $downup['U0'] += $rawOphTraff[0]['U0'];
  1978. }
  1979. }
  1980. }
  1981. }
  1982. $cells = la_TableCell($eachdir['rulename']);
  1983. $cells .= la_TableCell(zbs_convert_size($downup['D' . $eachdir['rulenumber']]));
  1984. $cells .= la_TableCell(zbs_convert_size($downup['U' . $eachdir['rulenumber']]));
  1985. $cells .= la_TableCell(zbs_convert_size(($downup['U' . $eachdir['rulenumber']] + $downup['D' . $eachdir['rulenumber']])));
  1986. $rows .= la_TableRow($cells, 'row3');
  1987. }
  1988. }
  1989. $result .= la_TableBody($rows, '100%', 0, '');
  1990. $result .= la_delimiter();
  1991. /*
  1992. * traffic stats by previous months
  1993. */
  1994. $prevStatsTmp = array();
  1995. $result .= la_tag('h3') . __('Previous month traffic stats') . la_tag('h3', true);
  1996. $cells = la_TableCell(__('Year'));
  1997. $cells .= la_TableCell(__('Month'));
  1998. $cells .= la_TableCell(__('Traffic classes'));
  1999. $cells .= la_TableCell(__('Downloaded'));
  2000. $cells .= la_TableCell(__('Uploaded'));
  2001. $cells .= la_TableCell(__('Total'));
  2002. $cells .= la_TableCell(__('Cash'));
  2003. $rows = la_TableRow($cells, 'row1');
  2004. if (!empty($alldirs)) {
  2005. foreach ($alldirs as $io => $eachdir) {
  2006. $query_prev = "SELECT `D" . $eachdir['rulenumber'] . "`,`U" . $eachdir['rulenumber'] . "`,`month`,`year`,`cash` from `stat` WHERE `login`='" . $login . "' ORDER BY `year`,`month`";
  2007. $allprevmonth = simple_queryall($query_prev);
  2008. //and again no classes
  2009. if ($eachdir['rulenumber'] == 0) {
  2010. //ishimura traffic mixing
  2011. if ($us_config[$ishimuraOption]) {
  2012. $query_hideki = "SELECT `D0`,`U0`,`month`,`year`,`cash` from `" . $ishimuraTable . "` WHERE `login`='" . $login . "' ORDER BY `year`,`month`;";
  2013. $dataHideki = simple_queryall($query_hideki);
  2014. if (!empty($dataHideki)) {
  2015. foreach ($dataHideki as $io => $each) {
  2016. foreach ($allprevmonth as $ia => $stgEach) {
  2017. if ($stgEach['year'] == $each['year'] and $stgEach['month'] == $each['month']) {
  2018. $allprevmonth[$ia]['D0'] += $each['D0'];
  2019. $allprevmonth[$ia]['U0'] += $each['U0'];
  2020. //$allprevmonth[$ia]['cash'] += $each['cash'];
  2021. }
  2022. }
  2023. }
  2024. }
  2025. }
  2026. //OphanimFlow traffic mixing
  2027. if ($us_config[$ophanimOption]) {
  2028. $ophDb = new NyanORM($ophanimTable);
  2029. $ophDb->selectable(array('login', 'D0', 'U0', 'month', 'year'));
  2030. $ophDb->where('login', '=', $login);
  2031. $ophDb->orderBy('year,month', 'DESC');
  2032. $allOphTraff = $ophDb->getAll();
  2033. if (!empty($allOphTraff)) {
  2034. foreach ($allOphTraff as $io => $each) {
  2035. foreach ($allprevmonth as $ia => $stgEach) {
  2036. if ($stgEach['year'] == $each['year'] and $stgEach['month'] == $each['month']) {
  2037. $allprevmonth[$ia]['D0'] += $each['D0'];
  2038. $allprevmonth[$ia]['U0'] += $each['U0'];
  2039. }
  2040. }
  2041. }
  2042. }
  2043. }
  2044. }
  2045. if (!empty($allprevmonth)) {
  2046. $allprevmonth = array_reverse($allprevmonth);
  2047. }
  2048. if (!empty($allprevmonth)) {
  2049. foreach ($allprevmonth as $io2 => $eachprevmonth) {
  2050. $cells = la_TableCell($eachprevmonth['year']);
  2051. $cells .= la_TableCell(__($monthnames[$eachprevmonth['month']]));
  2052. $cells .= la_TableCell($eachdir['rulename']);
  2053. $cells .= la_TableCell(zbs_convert_size($eachprevmonth['D' . $eachdir['rulenumber']]));
  2054. $cells .= la_TableCell(zbs_convert_size($eachprevmonth['U' . $eachdir['rulenumber']]));
  2055. $cells .= la_TableCell(zbs_convert_size(($eachprevmonth['U' . $eachdir['rulenumber']] + $eachprevmonth['D' . $eachdir['rulenumber']])));
  2056. $cells .= la_TableCell(round($eachprevmonth['cash'], 2));
  2057. $rows .= la_TableRow($cells, 'row3');
  2058. }
  2059. }
  2060. }
  2061. }
  2062. $result .= la_TableBody($rows, '100%', 0, '');
  2063. return ($result);
  2064. }
  2065. /**
  2066. * Returns array of available traffic directions aka classes
  2067. *
  2068. * @return array
  2069. */
  2070. function zbs_DirectionsGetAll() {
  2071. $query = "SELECT * from `directions`";
  2072. $allrules = simple_queryall($query);
  2073. return ($allrules);
  2074. }
  2075. /**
  2076. * Converts bytes values into readable traffic counters
  2077. *
  2078. * @param int $fs
  2079. * @return string
  2080. */
  2081. function zbs_convert_size($fs) {
  2082. if ($fs >= 1073741824)
  2083. $fs = round($fs / 1073741824 * 100) / 100 . " Gb";
  2084. elseif ($fs >= 1048576)
  2085. $fs = round($fs / 1048576 * 100) / 100 . " Mb";
  2086. elseif ($fs >= 1024)
  2087. $fs = round($fs / 1024 * 100) / 100 . " Kb";
  2088. else
  2089. $fs = $fs . " b";
  2090. return ($fs);
  2091. }
  2092. /**
  2093. * Renders navigation menu bar
  2094. *
  2095. * @param bool $icons
  2096. * @return string
  2097. */
  2098. function zbs_ModulesMenuShow($icons = false) {
  2099. $globconf = zbs_LoadConfig();
  2100. $maxnoicon = $globconf['MENUNOICONMAX'];
  2101. $mod_path = "config/modules.d/";
  2102. $skinPath = zbs_GetCurrentSkinPath($globconf);
  2103. $iconsPath = $skinPath . 'iconz/';
  2104. $all_modules = rcms_scandir($mod_path);
  2105. $currentModule = 'index';
  2106. if (isset($_GET['module'])) {
  2107. if (!empty($_GET['module'])) {
  2108. $currentModule = $_GET['module'];
  2109. }
  2110. }
  2111. $count = 1;
  2112. $result = '';
  2113. //default home link
  2114. if ($icons) {
  2115. $homeClass = ($currentModule == 'index') ? 'active' : 'menublock';
  2116. $result .= '<li class="' . $homeClass . '"><a href="index.php"><img src="' . $skinPath . 'iconz/home.gif"> ' . __('Home') . '</a></li>';
  2117. } else {
  2118. $result .= '<li><a href="index.php"> ' . __('Home') . '</a></li>';
  2119. }
  2120. //AFU help
  2121. if (!@$globconf['IM_JUST_PIDAR']) {
  2122. $afuUrl = 'https://ubilling.net.ua/rds/defense/';
  2123. $afuIcon = la_img($skinPath . 'iconz/afu.png');
  2124. if (@$globconf['AFU_URL']) {
  2125. $afuUrl = $globconf['AFU_URL'];
  2126. }
  2127. if ($icons) {
  2128. $result .= la_tag('li') . la_Link($afuUrl, $afuIcon . ' ' . __('Support AFU')) . la_tag('li', true);
  2129. } else {
  2130. $result .= la_tag('li') . la_Link($afuUrl, __('Support AFU')) . la_tag('li', true);
  2131. }
  2132. } else {
  2133. $result .= '<!-- pidar detected -->';
  2134. }
  2135. if (!empty($all_modules)) {
  2136. foreach ($all_modules as $eachmodule) {
  2137. $linkClass = ($currentModule == $eachmodule) ? 'active' : 'menublock';
  2138. if ($icons == true) {
  2139. if (file_exists($iconsPath . $eachmodule . ".gif")) {
  2140. $iconlink = ' <img src="' . $iconsPath . $eachmodule . '.gif" class="menuicon"> ';
  2141. } else {
  2142. if (file_exists($iconsPath . $eachmodule . ".png")) {
  2143. $iconlink = ' <img src="' . $iconsPath . $eachmodule . '.png" class="menuicon"> ';
  2144. } else {
  2145. $iconlink = '';
  2146. }
  2147. }
  2148. } else {
  2149. $iconlink = '';
  2150. }
  2151. if (!$icons) {
  2152. if ($count < $maxnoicon) {
  2153. $mod_data = parse_ini_file($mod_path . $eachmodule);
  2154. $mod_name = __($mod_data['NAME']);
  2155. $mod_need = isset($mod_data['NEED']) ? $mod_data['NEED'] : '';
  2156. if ((@$globconf[$mod_need]) or (empty($mod_need))) {
  2157. $result .= '<li><a href="?module=' . $eachmodule . '" >' . $iconlink . '' . $mod_name . '</a></li>';
  2158. $count++;
  2159. }
  2160. }
  2161. } else {
  2162. $mod_data = parse_ini_file($mod_path . $eachmodule);
  2163. $mod_name = __($mod_data['NAME']);
  2164. $mod_need = isset($mod_data['NEED']) ? $mod_data['NEED'] : '';
  2165. if ((@$globconf[$mod_need]) or (empty($mod_need))) {
  2166. $result .= '<li class="' . $linkClass . '"><a href="?module=' . $eachmodule . '">' . $iconlink . __($mod_name) . '</a></li>';
  2167. $count++;
  2168. }
  2169. }
  2170. }
  2171. }
  2172. if ($globconf['auth'] == 'login') {
  2173. if (isset($globconf['INTRO_MODE'])) {
  2174. if ($globconf['INTRO_MODE'] == '2') {
  2175. if ((!isset($_COOKIE['upassword'])) or (@$_COOKIE['upassword'] == 'nopassword')) {
  2176. $result = zbs_IntroLoadText();
  2177. }
  2178. }
  2179. }
  2180. }
  2181. return ($result);
  2182. }
  2183. /**
  2184. * Renders copyright data
  2185. *
  2186. * @return string
  2187. */
  2188. function zbs_CopyrightsShow() {
  2189. global $pageGenStartTime;
  2190. global $query_counter;
  2191. $fullGenTime = '';
  2192. $usConf = zbs_LoadConfig();
  2193. $baseFooter = 'Powered by <a href="https://ubilling.net.ua">Ubilling</a>';
  2194. if ((isset($usConf['ISP_NAME'])) and (isset($usConf['ISP_URL']))) {
  2195. if ((!empty($usConf['ISP_NAME'])) and (!empty($usConf['ISP_URL']))) {
  2196. $rawUrl = strtolower($usConf['ISP_URL']);
  2197. if (stripos($rawUrl, 'http') === false) {
  2198. $rawUrl = 'http://' . $rawUrl;
  2199. } else {
  2200. $rawUrl = $rawUrl;
  2201. }
  2202. $addFooter = '<a href="' . $rawUrl . '">' . $usConf['ISP_NAME'] . '</a> | ';
  2203. } else {
  2204. $addFooter = '';
  2205. }
  2206. } else {
  2207. $addFooter = '';
  2208. }
  2209. //Page generation timings and query count output
  2210. if (isset($usConf['DEBUG_COUNTERS'])) {
  2211. if ($usConf['DEBUG_COUNTERS']) {
  2212. $mtNowTime = explode(' ', microtime());
  2213. $totalPageGenTime = $mtNowTime[0] + $mtNowTime[1] - $pageGenStartTime;
  2214. $fullGenTime .= ' | ' . __('GT:') . round($totalPageGenTime, 3) . ' QC: ' . $query_counter;
  2215. }
  2216. }
  2217. $result = $addFooter . $baseFooter . $fullGenTime;
  2218. return ($result);
  2219. }
  2220. /**
  2221. * Pushes payment log data for finance report/cash flows
  2222. *
  2223. * @param string $login
  2224. * @param float $summ
  2225. * @param int $cashtypeid
  2226. * @param string $note
  2227. */
  2228. function zbs_PaymentLog($login, $summ, $cashtypeid, $note) {
  2229. $cashtypeid = vf($cashtypeid);
  2230. $ctime = curdatetime();
  2231. $userdata = zbs_UserGetStargazerData($login);
  2232. $balance = $userdata['Cash'];
  2233. $note = mysql_real_escape_string($note);
  2234. $query = "INSERT INTO `payments` (`id` , `login` , `date` , `admin` , `balance` , `summ` , `cashtypeid` , `note` )
  2235. VALUES (NULL , '" . $login . "', '" . $ctime . "', 'external', '" . $balance . "', '" . $summ . "', '" . $cashtypeid . "', '" . $note . "'); ";
  2236. nr_query($query);
  2237. }
  2238. /**
  2239. * Runs sgconfig in system shell
  2240. *
  2241. * @param string $command
  2242. * @param bool $debug
  2243. *
  2244. * @return void
  2245. */
  2246. function executor($command, $debug = false) {
  2247. $globconf = zbs_LoadConfig();
  2248. $SGCONF = $globconf['SGCONF'];
  2249. $STG_HOST = $globconf['STG_HOST'];
  2250. $STG_PORT = $globconf['STG_PORT'];
  2251. $STG_LOGIN = $globconf['STG_LOGIN'];
  2252. $STG_PASSWD = $globconf['STG_PASSWD'];
  2253. $configurator = $SGCONF . ' set -s ' . $STG_HOST . ' -p ' . $STG_PORT . ' -a' . $STG_LOGIN . ' -w' . $STG_PASSWD . ' ' . $command;
  2254. if ($debug) {
  2255. print($configurator . "\n");
  2256. print(shell_exec($configurator));
  2257. } else {
  2258. shell_exec($configurator);
  2259. }
  2260. }
  2261. /**
  2262. * Adds some funds to user account
  2263. *
  2264. * @param string $login
  2265. * @param float $cash
  2266. *
  2267. * @return void
  2268. */
  2269. function billing_addcash($login, $cash) {
  2270. executor('-u' . $login . ' -c ' . $cash);
  2271. }
  2272. /**
  2273. * Sets user credit
  2274. *
  2275. * @param string $login
  2276. * @param float $credit
  2277. *
  2278. * @return void
  2279. */
  2280. function billing_setcredit($login, $credit) {
  2281. executor('-u' . $login . ' -r ' . $credit);
  2282. }
  2283. /**
  2284. * Sets credit expire date
  2285. *
  2286. * @param string $login
  2287. * @param string $creditexpire
  2288. *
  2289. * @return void
  2290. */
  2291. function billing_setcreditexpire($login, $creditexpire) {
  2292. executor('-u' . $login . ' -E ' . $creditexpire);
  2293. }
  2294. /**
  2295. * Sets user account balance for some concrete value
  2296. *
  2297. * @param string $login
  2298. * @param float $cash
  2299. *
  2300. * @return void
  2301. */
  2302. function billing_setcash($login, $cash) {
  2303. executor('-u' . $login . ' -v ' . $cash);
  2304. }
  2305. /**
  2306. * Changes user current tariff
  2307. *
  2308. * @param string $login
  2309. * @param string $tariff
  2310. *
  2311. * @return void
  2312. */
  2313. function billing_settariff($login, $tariff) {
  2314. executor('-u' . $login . ' -t ' . $tariff);
  2315. }
  2316. /**
  2317. * Sets user tariff change from next month
  2318. *
  2319. * @param string $login
  2320. * @param string $tariff
  2321. *
  2322. * @return void
  2323. */
  2324. function billing_settariffnm($login, $tariff) {
  2325. executor('-u' . $login . ' -t ' . $tariff . ':delayed');
  2326. }
  2327. /**
  2328. * Freezes user by its login
  2329. *
  2330. * @param string $login
  2331. *
  2332. * @return void
  2333. */
  2334. function billing_freeze($login) {
  2335. executor('-u' . $login . ' -i 1');
  2336. }
  2337. /**
  2338. * Set password to stargazer user
  2339. * @param string $login stargazer user login
  2340. * @param string $password password string
  2341. *
  2342. * @return void
  2343. */
  2344. function billing_setpassword($login, $password) {
  2345. executor('-u' . $login . ' -o ' . $password);;
  2346. }
  2347. /**
  2348. * Dummy replacement for external logging purposes
  2349. *
  2350. * @return string
  2351. */
  2352. function whoami() {
  2353. $mylogin = 'external';
  2354. return ($mylogin);
  2355. }
  2356. /**
  2357. * Writes some data into system event log
  2358. *
  2359. * @param string $event
  2360. *
  2361. * @return void
  2362. */
  2363. function log_register($event) {
  2364. $admin_login = whoami();
  2365. $ip = $_SERVER['REMOTE_ADDR'];
  2366. $current_time = curdatetime();
  2367. $event = mysql_real_escape_string($event);
  2368. $query = "INSERT INTO `weblogs` (`id`,`date`,`admin`,`ip`,`event`) VALUES(NULL,'" . $current_time . "','" . $admin_login . "','" . $ip . "','" . $event . "')";
  2369. nr_query($query);
  2370. }
  2371. /**
  2372. * Returns user account balance
  2373. *
  2374. * @param string $login
  2375. * @return float
  2376. */
  2377. function zbs_CashGetUserBalance($login) {
  2378. $login = vf($login);
  2379. $query = "SELECT `Cash` from `users` WHERE `login`='" . $login . "'";
  2380. $cash = simple_query($query);
  2381. return ($cash['Cash']);
  2382. }
  2383. /**
  2384. * Returns user account credit limit
  2385. *
  2386. * @param string $login
  2387. * @return float
  2388. */
  2389. function zbs_CashGetUserCredit($login) {
  2390. $login = vf($login);
  2391. $query = "SELECT `Credit` from `users` WHERE `login`='" . $login . "'";
  2392. $cash = simple_query($query);
  2393. return ($cash['Credit']);
  2394. }
  2395. /**
  2396. * Returns user credit limit expire date as timestamp
  2397. *
  2398. * @param string $login
  2399. * @return int
  2400. */
  2401. function zbs_CashGetUserCreditExpire($login) {
  2402. $login = vf($login);
  2403. $query = "SELECT `CreditExpire` from `users` WHERE `login`='" . $login . "'";
  2404. $cash = simple_query($query);
  2405. return ($cash['CreditExpire']);
  2406. }
  2407. /**
  2408. * Returns user current tariff
  2409. *
  2410. * @param string $login
  2411. * @return string
  2412. */
  2413. function zbs_UserGetTariff($login) {
  2414. $login = mysql_real_escape_string($login);
  2415. $query = "SELECT `Tariff` from `users` WHERE `login`='" . $login . "'";
  2416. $res = simple_query($query);
  2417. return ($res['Tariff']);
  2418. }
  2419. /**
  2420. * Returns user current tariff price
  2421. *
  2422. * @param string $login
  2423. * @return float
  2424. */
  2425. function zbs_UserGetTariffPrice($tariff) {
  2426. $login = mysql_real_escape_string($tariff);
  2427. $query = "SELECT `Fee` from `tariffs` WHERE `name`='" . $tariff . "'";
  2428. $res = simple_query($query);
  2429. return ($res['Fee']);
  2430. }
  2431. /**
  2432. * Returns user current tariff data array
  2433. *
  2434. * @param string $login
  2435. * @return array
  2436. */
  2437. function zbs_UserGetTariffData($tariff) {
  2438. $login = mysql_real_escape_string($tariff);
  2439. $query = "SELECT * from `tariffs` WHERE `name`='" . $tariff . "'";
  2440. $res = simple_query($query);
  2441. return ($res);
  2442. }
  2443. /**
  2444. * Adds some money to user account
  2445. *
  2446. * @param string $login
  2447. * @param float $cash
  2448. * @param string $note
  2449. *
  2450. * @return void
  2451. */
  2452. function zbs_CashAdd($login, $cash, $note) {
  2453. $login = vf($login);
  2454. $cash = mysql_real_escape_string($cash);
  2455. $cashtype = 0;
  2456. $note = mysql_real_escape_string($note);
  2457. $date = curdatetime();
  2458. $balance = zbs_CashGetUserBalance($login);
  2459. billing_addcash($login, $cash);
  2460. $query = "INSERT INTO `payments` ( `id` , `login` , `date` , `balance` , `summ` , `cashtypeid` , `note` )
  2461. VALUES (NULL , '" . $login . "', '" . $date . "', '" . $balance . "', '" . $cash . "', '" . $cashtype . "', '" . $note . "');";
  2462. nr_query($query);
  2463. log_register("BALANCEADD (" . $login . ') ON ' . $cash);
  2464. }
  2465. /**
  2466. * Retuns all months with names in two digit notation
  2467. *
  2468. * @return array
  2469. */
  2470. function zbs_months_array() {
  2471. $months = array(
  2472. '01' => 'January',
  2473. '02' => 'February',
  2474. '03' => 'March',
  2475. '04' => 'April',
  2476. '05' => 'May',
  2477. '06' => 'June',
  2478. '07' => 'July',
  2479. '08' => 'August',
  2480. '09' => 'September',
  2481. '10' => 'October',
  2482. '11' => 'November',
  2483. '12' => 'December'
  2484. );
  2485. return ($months);
  2486. }
  2487. /**
  2488. * Retuns all months with names without leading zeros
  2489. *
  2490. * @return array
  2491. */
  2492. function zbs_months_array_wz() {
  2493. $months = array(
  2494. '1' => 'January',
  2495. '2' => 'February',
  2496. '3' => 'March',
  2497. '4' => 'April',
  2498. '5' => 'May',
  2499. '6' => 'June',
  2500. '7' => 'July',
  2501. '8' => 'August',
  2502. '9' => 'September',
  2503. '10' => 'October',
  2504. '11' => 'November',
  2505. '12' => 'December'
  2506. );
  2507. return ($months);
  2508. }
  2509. /**
  2510. * Extracts value by key from UBstorage
  2511. *
  2512. * @param string $key
  2513. * @return string
  2514. */
  2515. function zbs_StorageGet($key) {
  2516. $key = mysql_real_escape_string($key);
  2517. $query = "SELECT `value` from `ubstorage` WHERE `key`='" . $key . "'";
  2518. $fetchdata = simple_query($query);
  2519. if (!empty($fetchdata)) {
  2520. $result = $fetchdata['value'];
  2521. } else {
  2522. $result = '';
  2523. }
  2524. return ($result);
  2525. }
  2526. /**
  2527. * Returns array of all users with blocked userstats access
  2528. *
  2529. * @return array
  2530. */
  2531. function zbs_GetUserStatsDeniedAll() {
  2532. $access_raw = zbs_StorageGet('ZBS_DENIED');
  2533. $result = array();
  2534. if (!empty($access_raw)) {
  2535. $access_raw = base64_decode($access_raw);
  2536. $access_raw = unserialize($access_raw);
  2537. $result = $access_raw;
  2538. }
  2539. return ($result);
  2540. }
  2541. /**
  2542. * Returns array of all users with blocked helpdesk access
  2543. *
  2544. * @return array
  2545. */
  2546. function zbs_GetHelpdeskDeniedAll() {
  2547. $access_raw = zbs_StorageGet('ZBS_HELP_DENIED');
  2548. $result = array();
  2549. if (!empty($access_raw)) {
  2550. $access_raw = base64_decode($access_raw);
  2551. $access_raw = unserialize($access_raw);
  2552. $result = $access_raw;
  2553. }
  2554. return ($result);
  2555. }
  2556. /**
  2557. * Rounds $value to $precision digits
  2558. *
  2559. * @param $value digit to round
  2560. * @param $precision amount of digits after point
  2561. *
  2562. * @return string
  2563. */
  2564. function web_roundValue($value, $precision = 0) {
  2565. if ($precision < 0)
  2566. $precision = 0;
  2567. elseif ($precision > 4)
  2568. $precision = 4;
  2569. $multiplier = pow(10, $precision);
  2570. return ($value >= 0 ? ceil($value * $multiplier) : floor($value * $multiplier)) / $multiplier;
  2571. }
  2572. /**
  2573. * Returns array of all available tariff prices
  2574. *
  2575. * @return array
  2576. */
  2577. function zbs_TariffGetAllPrices() {
  2578. $query = "SELECT `name`,`Fee` from `tariffs`";
  2579. $alltariffs = simple_queryall($query);
  2580. $result = array();
  2581. if (!empty($alltariffs)) {
  2582. foreach ($alltariffs as $io => $eachtariff) {
  2583. $result[$eachtariff['name']] = $eachtariff['Fee'];
  2584. }
  2585. }
  2586. return ($result);
  2587. }
  2588. /**
  2589. * Returns ISP logo image code
  2590. *
  2591. * @return string
  2592. */
  2593. function zbs_IspLogoShow() {
  2594. $usConf = zbs_LoadConfig();
  2595. $result = '';
  2596. if (isset($usConf['ISP_LOGO'])) {
  2597. if ((!empty($usConf['ISP_NAME'])) and (!empty($usConf['ISP_URL'])) and ((!empty($usConf['ISP_LOGO'])))) {
  2598. $rawUrl = strtolower($usConf['ISP_URL']);
  2599. if (stripos($rawUrl, 'http') === false) {
  2600. $rawUrl = 'http://' . $rawUrl;
  2601. } else {
  2602. $rawUrl = $rawUrl;
  2603. }
  2604. $result = '<a href="' . $rawUrl . '" target="_BLANK"><img src="' . $usConf['ISP_LOGO'] . '" title="' . $usConf['ISP_NAME'] . '"></a>';
  2605. }
  2606. }
  2607. return ($result);
  2608. }
  2609. /**
  2610. * Returns custom style background code
  2611. *
  2612. * @return string
  2613. */
  2614. function zbs_CustomBackground() {
  2615. $usConf = zbs_LoadConfig();
  2616. $skinPath = zbs_GetCurrentSkinPath($usConf);
  2617. $tilesPath = $skinPath . 'tiles/';
  2618. $result = '';
  2619. if (isset($usConf['BACKGROUND'])) {
  2620. if (($usConf['BACKGROUND'] != 'DEFAULT') and (!empty($usConf['BACKGROUND']))) {
  2621. $customBackground = $usConf['BACKGROUND'];
  2622. $availTiles = rcms_scandir($tilesPath);
  2623. $availTiles = array_flip($availTiles);
  2624. if ($customBackground == 'RANDOM') {
  2625. $customBackground = array_rand($availTiles);
  2626. }
  2627. if (isset($availTiles[$customBackground])) {
  2628. $result = '<style> body { background: #080808 url(' . $tilesPath . $customBackground . ') repeat; } </style> ';
  2629. } else {
  2630. $result = '<!-- Custom background tile file not found -->';
  2631. }
  2632. }
  2633. }
  2634. return ($result);
  2635. }
  2636. /**
  2637. * Checks is some new/unread announcements available?
  2638. *
  2639. * @return bool
  2640. */
  2641. function zbs_AnnouncementsAvailable($login) {
  2642. global $us_config;
  2643. $login = mysql_real_escape_string($login);
  2644. $query = "SELECT `zbsannouncements`.*, `zbh`.`annid` from `zbsannouncements` LEFT JOIN (SELECT `annid` FROM `zbsannhist` WHERE `login` = '" . $login . "') as zbh ON ( `zbsannouncements`.`id`=`zbh`.`annid`) WHERE `public`='1' AND `annid` IS NULL ORDER BY `zbsannouncements`.`id` DESC LIMIT 1";
  2645. $data = simple_queryall($query);
  2646. if (!empty($data)) {
  2647. if (isset($us_config['AN_MODAL']) and !empty($us_config['AN_MODAL'])) {
  2648. $inputs = '';
  2649. $inputs .= la_tag('br');
  2650. $inputs .= la_HiddenInput('anmarkasread', $data[0]['id']);
  2651. if ($data[0]['type'] == 'text') {
  2652. $eachtext = strip_tags($data[0]['text']);
  2653. $inputs .= nl2br($eachtext);
  2654. }
  2655. if ($data[0]['type'] == 'html') {
  2656. $inputs .= $data[0]['text'];
  2657. }
  2658. $inputs .= la_tag('br');
  2659. $inputs .= la_tag('br');
  2660. $inputs .= la_Submit('Mark as read');
  2661. $form = la_Form('?module=announcements', "POST", $inputs, 'glamour');
  2662. $result = la_modalOpened($data[0]['title'], $form);
  2663. } else {
  2664. $result = TRUE;
  2665. }
  2666. } else {
  2667. $result = FALSE;
  2668. }
  2669. return ($result);
  2670. }
  2671. /**
  2672. * Renders new/unread announcements notification
  2673. *
  2674. * @return void
  2675. */
  2676. function zbs_AnnouncementsNotice($login) {
  2677. $result = '';
  2678. $skinPath = zbs_GetCurrentSkinPath();
  2679. $iconsPath = $skinPath . 'iconz/';
  2680. $availableAnnouncements = zbs_AnnouncementsAvailable($login);
  2681. if ($availableAnnouncements) {
  2682. if ($availableAnnouncements !== TRUE) {
  2683. $result .= $availableAnnouncements;
  2684. }
  2685. $cells = la_TableCell(la_Link('?module=announcements', la_img($iconsPath . 'alert.gif'), true, 'announcementslink'));
  2686. $cells .= la_TableCell(la_Link('?module=announcements', __('Some announcements are available'), true, 'announcementslink'));
  2687. $rows = la_TableRow($cells);
  2688. $result .= la_TableBody($rows, '100%', 0, 'announcementstable');
  2689. show_window('', $result);
  2690. }
  2691. }
  2692. /**
  2693. * Loads current userstats intro text
  2694. *
  2695. * @return string
  2696. */
  2697. function zbs_IntroLoadText() {
  2698. $result = zbs_StorageGet('ZBS_INTRO');
  2699. return ($result);
  2700. }
  2701. /**
  2702. * Loads current freeze days charge data for user
  2703. *
  2704. * @param $login
  2705. *
  2706. * @return array
  2707. */
  2708. function zbs_getFreezeDaysChargeData($login) {
  2709. $FrozenAllQuery = "SELECT * FROM `frozen_charge_days` WHERE `login` = '" . $login . "';";
  2710. $FrozenAll = simple_queryall($FrozenAllQuery);
  2711. return $FrozenAll;
  2712. }
  2713. /**
  2714. * Performs RemoteAPI request to preconfigured billing instance
  2715. *
  2716. * @param string $requestUrl RemoteAPI request URL for example: "&action=someaction"
  2717. *
  2718. * @return string
  2719. */
  2720. function zbs_remoteApiRequest($requestUrl) {
  2721. $usConfig = zbs_LoadConfig();
  2722. $result = '';
  2723. if (isset($usConfig['API_URL']) and isset($usConfig['API_KEY'])) {
  2724. if (!empty($usConfig['API_URL']) and !empty($usConfig['API_KEY'])) {
  2725. $apiBase = $usConfig['API_URL'] . '/?module=remoteapi&key=' . $usConfig['API_KEY'];
  2726. @$result .= file_get_contents($apiBase . $requestUrl);
  2727. } else {
  2728. die('ERROR: API_KEY/API_URL is empty!');
  2729. }
  2730. } else {
  2731. die('ERROR: API_KEY/API_URL not set!');
  2732. }
  2733. return ($result);
  2734. }
  2735. /**
  2736. * Renders aerial alerts notification
  2737. *
  2738. * @global array $us_config
  2739. *
  2740. * @return void
  2741. */
  2742. function zbs_AerialAlertNotification() {
  2743. global $us_config;
  2744. $rawData = zbs_remoteApiRequest('&action=aerialalerts');
  2745. $skinPath = zbs_GetCurrentSkinPath($us_config);
  2746. $iconsPath = $skinPath . 'iconz/';
  2747. if (!empty($rawData)) {
  2748. $alertsData = @json_decode($rawData, true);
  2749. if (is_array($alertsData)) {
  2750. if (!empty($alertsData)) {
  2751. if (isset($alertsData['region']) and isset($alertsData['alert']) and isset($alertsData['changed'])) {
  2752. if ($alertsData['alert']) {
  2753. $alertStyle = la_tag('style');
  2754. $alertStyle .= '
  2755. .alert_aerial {
  2756. display: block;
  2757. width: 95%;
  2758. margin: 20px 3% 0 3%;
  2759. margin-top: 2px;
  2760. -webkit-border-radius: 5px;
  2761. -moz-border-radius: 5px;
  2762. border-radius: 5px;
  2763. background: #F5F3BA;
  2764. border: 1px solid #C7A20D;
  2765. color: #796616;
  2766. padding: 10px 0;
  2767. text-indent: 40px;
  2768. font-size: 24px;
  2769. }
  2770. ';
  2771. $alertStyle .= la_tag('style', true);
  2772. $alertText = $alertStyle;
  2773. $alertText .= la_tag('div', false, 'alert_aerial');
  2774. $alertText .= la_img($iconsPath . 'nuclear_bomb.png', __('Aerial alert')) . ' ' . $alertsData['region'] . ' ' . __('since') . ' ' . $alertsData['changed'];
  2775. $alertText .= la_tag('div', true);
  2776. show_window(__('Aerial alert') . '!', $alertText);
  2777. }
  2778. }
  2779. } else {
  2780. show_window(__('Error'), __('Empty reply received') . ': ' . $rawData);
  2781. }
  2782. } else {
  2783. show_window(__('Error'), __('Wrong reply received') . ': ' . $rawData);
  2784. }
  2785. }
  2786. }
  2787. /**
  2788. * Returns array of fees by some login from harvested fees database
  2789. *
  2790. * @param string $login existing user login
  2791. *
  2792. * @return array
  2793. */
  2794. function zbs_GetUserDBFees($login, $date_from = '', $date_to = '') {
  2795. $result = array();
  2796. $login = ubRouting::filters($login, 'mres');
  2797. $feesDb = new NyanORM('fees');
  2798. $feesDb->where('login', '=', $login);
  2799. if (!empty($date_from)) {
  2800. $feesDb->where('date', '>=', $date_from);
  2801. }
  2802. if (!empty($date_to)) {
  2803. $feesDb->where('date', '<=', $date_to);
  2804. }
  2805. $allFees = $feesDb->getAll();
  2806. if (!empty($allFees)) {
  2807. foreach ($allFees as $io => $each) {
  2808. $counter = strtotime($each['date']);
  2809. // trying to avoid duplicate keys
  2810. while (array_key_exists($counter, $result)) {
  2811. $counter++;
  2812. }
  2813. $result[$counter]['login'] = $each['login'];
  2814. $result[$counter]['date'] = $each['date'];
  2815. $result[$counter]['admin'] = $each['admin'];
  2816. $result[$counter]['summ'] = $each['summ'];
  2817. $result[$counter]['from'] = $each['from'];
  2818. $result[$counter]['to'] = $each['to'];
  2819. $result[$counter]['operation'] = 'Fee';
  2820. $result[$counter]['note'] = $each['note'];
  2821. $result[$counter]['cashtype'] = $each['cashtype'];
  2822. }
  2823. }
  2824. return ($result);
  2825. }
  2826. /**
  2827. * Returns all additional fee-payments(based on negative sums) with optional filters
  2828. *
  2829. * @param $login
  2830. * @param $date_from
  2831. * @param $date_to
  2832. * @param $whereStr
  2833. *
  2834. * @return array
  2835. * @throws Exception
  2836. */
  2837. function zbs_GetUserAdditionalFees($login, $date_from = '', $date_to = '', $whereStr = '') {
  2838. $result = array();
  2839. $login = ubRouting::filters($login, 'mres');
  2840. $paymentsDB = new NyanORM('payments');
  2841. $paymentsDB->where('login', '=', $login);
  2842. $paymentsDB->where('summ', '<', 0);
  2843. if (!empty($date_from)) {
  2844. $paymentsDB->where('date', '>=', $date_from);
  2845. }
  2846. if (!empty($date_to)) {
  2847. $paymentsDB->where('date', '<=', $date_to);
  2848. }
  2849. if (!empty($whereStr)) {
  2850. $paymentsDB->whereRaw(' AND ' . $whereStr);
  2851. }
  2852. $additionalFees = $paymentsDB->getAll();
  2853. if (!empty($additionalFees)) {
  2854. foreach ($additionalFees as $io => $eachFee) {
  2855. $counter = strtotime($eachFee['date']);
  2856. // trying to avoid duplicate keys
  2857. while (array_key_exists($counter, $result)) {
  2858. $counter++;
  2859. }
  2860. if (ispos($eachFee['note'], 'MOCK:')) {
  2861. $cashto = $eachFee['balance'];
  2862. }
  2863. if (ispos($eachFee['note'], 'BALANCESET:')) {
  2864. $cashto = $eachFee['summ'];
  2865. }
  2866. if ((!ispos($eachFee['note'], 'MOCK:')) and (!ispos($eachFee['note'], 'BALANCESET:'))) {
  2867. if (is_numeric($eachFee['summ']) and is_numeric($eachFee['balance'])) {
  2868. $cashto = $eachFee['summ'] + $eachFee['balance'];
  2869. } else {
  2870. $cashto = __('Corrupted');
  2871. }
  2872. }
  2873. $result[$counter]['login'] = $eachFee['login'];
  2874. $result[$counter]['date'] = $eachFee['date'];
  2875. $result[$counter]['admin'] = $eachFee['admin'];
  2876. $result[$counter]['summ'] = $eachFee['summ'];
  2877. $result[$counter]['from'] = $eachFee['balance'];
  2878. $result[$counter]['to'] = $cashto;
  2879. $result[$counter]['operation'] = 'Payment';
  2880. $result[$counter]['note'] = $eachFee['note'];
  2881. $result[$counter]['cashtype'] = $eachFee['cashtypeid'];
  2882. }
  2883. }
  2884. return ($result);
  2885. }
  2886. /**
  2887. * Concats 2 arrays with numerical keys avoiding key duplicates
  2888. *
  2889. * @param $arr1
  2890. * @param $arr2
  2891. *
  2892. * @return mixed
  2893. */
  2894. function zbs_concatArraysAvoidDuplicateKeys($arr1, $arr2) {
  2895. $resultArray = array();
  2896. // searching and fixing duplicates in $arr1 - $arr2 arrays
  2897. $duplicates = array_intersect_key($arr1, $arr2);
  2898. if (!empty($duplicates)) {
  2899. foreach ($duplicates as $key => $val) {
  2900. // walking through duplicates array and trying to get
  2901. // a unique key instead of existing duplicate key
  2902. if (array_key_exists($key, $arr2)) {
  2903. $tmpVal = $arr2[$key];
  2904. $tmpKey = $key + 1;
  2905. while (
  2906. array_key_exists($tmpKey, $arr1) or
  2907. array_key_exists($tmpKey, $arr2)
  2908. ) {
  2909. $tmpKey++;
  2910. }
  2911. // remove array item with duplicate key and set it's preserved value to new key
  2912. // we're not worried about the keys order as concatenated array
  2913. // will be later sorted by keys anyway
  2914. unset($arr2[$key]);
  2915. $arr2[$tmpKey] = $tmpVal;
  2916. }
  2917. }
  2918. }
  2919. $resultArray = $arr1 + $arr2;
  2920. return ($resultArray);
  2921. }
  2922. /**
  2923. * Returns all tariffs data, excluding lousy tariffs if necessary
  2924. *
  2925. * @param $excludeLousy
  2926. *
  2927. * @return array
  2928. */
  2929. function zbs_GetTariffsDataAll($excludeLousy = false) {
  2930. $allTariffs = array();
  2931. $tariffDB = new NyanORM('tariffs');
  2932. $allTariffs = $tariffDB->getAll('name');
  2933. if (!empty($allTariffs) and $excludeLousy) {
  2934. $lousyDb = new NyanORM('lousytariffs');
  2935. $allLousyTariffs = $lousyDb->getAll('tariff');
  2936. $allTariffs = array_diff_key($allTariffs, $allLousyTariffs);
  2937. }
  2938. return ($allTariffs);
  2939. }
  2940. /**
  2941. * Check if user already has tag
  2942. * @param type $login string
  2943. * @param type $tagid int
  2944. * @return type boolean
  2945. */
  2946. function stg_check_user_tag($login, $tagid) {
  2947. $login = mysql_real_escape_string($login);
  2948. $tagid = vf($tagid, 3);
  2949. $query = "SELECT `id` FROM `tags` WHERE `login`= '" . $login . "' AND `tagid`= '" . $tagid . "'";
  2950. $check = simple_queryall($query);
  2951. return (!empty($check));
  2952. }
  2953. /**
  2954. *
  2955. * add sms tag for user to remind him about apropos payment
  2956. * @param type $login string
  2957. * @param type $tagid integer
  2958. */
  2959. function stg_add_user_tag($login, $tagid) {
  2960. $login = mysql_real_escape_string($login);
  2961. $tagid = vf($tagid, 3);
  2962. $query = "INSERT INTO `tags` (`id` ,`login` ,`tagid`) VALUES (NULL , '" . $login . "', '" . $tagid . "'); ";
  2963. nr_query($query);
  2964. log_register('TAGADD (' . $login . ') TAGID [' . $tagid . ']');
  2965. }
  2966. /**
  2967. * delete sms tag
  2968. * @param type $login
  2969. * @param type $tagid
  2970. */
  2971. function stg_del_user_tagid($login, $tagid) {
  2972. $login = mysql_real_escape_string($login);
  2973. $tagid = vf($tagid, 3);
  2974. $query = "DELETE from `tags` WHERE `login`='" . $login . "' AND `tagid`='" . $tagid . "'";
  2975. nr_query($query);
  2976. log_register('TAGDEL LOGIN (' . $login . ') TAGID [' . $tagid . ']');
  2977. }
  2978. /**
  2979. * Catches PWA manifest request and renders it
  2980. *
  2981. * @return void
  2982. */
  2983. function zbs_ManifestCatchRequest() {
  2984. if (ubRouting::checkGet('manifest')) {
  2985. $us_config = zbs_LoadConfig();
  2986. $customAppIcons = array();
  2987. $ispName = ($us_config['ISP_NAME']) ? $us_config['ISP_NAME'] : '';
  2988. $applicationName = ($ispName) ? $ispName . ' ' . __('User stats') : __('User stats');
  2989. $shortName = ($ispName) ? $ispName : __('User stats');
  2990. if (isset($us_config['WA_NAME'])) {
  2991. if (!empty($us_config['WA_NAME'])) {
  2992. $applicationName = $us_config['WA_NAME'];
  2993. $shortName = $us_config['WA_NAME'];
  2994. }
  2995. }
  2996. if (isset($us_config['WA_ICON_192']) and isset($us_config['WA_ICON_512'])) {
  2997. if (!empty($us_config['WA_ICON_192']) and !empty($us_config['WA_ICON_512'])) {
  2998. $customAppIcons = array(
  2999. 0 => array('src' => $us_config['WA_ICON_192'], 'sizes' => '192x192', 'type' => 'image/png'),
  3000. 1 => array('src' => $us_config['WA_ICON_512'], 'sizes' => '512x512', 'type' => 'image/png')
  3001. );
  3002. }
  3003. }
  3004. $manifestor = new Manifestator();
  3005. $manifestor->setName($applicationName);
  3006. $manifestor->setShortName($shortName);
  3007. if ($customAppIcons) {
  3008. $manifestor->setIcons($customAppIcons);
  3009. }
  3010. $manifestor->render();
  3011. }
  3012. }