api.traffstats.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. <?php
  2. /**
  3. * Basic traffic stats/charts report implementation
  4. */
  5. class TraffStats {
  6. /**
  7. * System alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Ishimura enabled flag
  14. *
  15. * @var string
  16. */
  17. protected $ishimuraFlag = false;
  18. /**
  19. * Ishimura database abstraction layer
  20. *
  21. * @var object
  22. */
  23. protected $ishimuraDb = '';
  24. /**
  25. * OphanimFlow integration flag
  26. *
  27. * @var bool
  28. */
  29. protected $ophanimFlag = false;
  30. /**
  31. * OphanimFlow object instance
  32. *
  33. * @var object
  34. */
  35. protected $ophanimFlow = '';
  36. /**
  37. * Contains all available users data
  38. *
  39. * @var array
  40. */
  41. protected $userData = array();
  42. /**
  43. * Contains ishimura user traffic stats
  44. *
  45. * @var array
  46. */
  47. protected $ishimuraData = array();
  48. /**
  49. * Contains TRAFFSIZE option value
  50. *
  51. * @var string
  52. */
  53. protected $trafScale = 'float';
  54. /**
  55. * Previous months traffic stats database abstraction layer
  56. *
  57. * @var object
  58. */
  59. protected $statsDb = '';
  60. /**
  61. * Contains previous user traffic stats as year=>month=>data
  62. *
  63. * @var array
  64. */
  65. protected $trafStats = array();
  66. /**
  67. * Contains available traffic directions/classes as index=>ruleNum
  68. *
  69. * @var array
  70. */
  71. protected $dirs = array();
  72. /**
  73. * Contains current instance login
  74. *
  75. * @var string
  76. */
  77. protected $login = '';
  78. /**
  79. * On-demand charts loading flag
  80. *
  81. * @var bool
  82. */
  83. protected $ondemandFlag = true;
  84. /**
  85. * System caching object instance
  86. *
  87. * @var object
  88. */
  89. protected $cache = '';
  90. /**
  91. * Default caching timeout
  92. *
  93. * @var int
  94. */
  95. protected $cachingTimeout = 3600;
  96. /**
  97. * System messages helper object placeholder
  98. *
  99. * @var object
  100. */
  101. protected $messages = '';
  102. //some other predefined stuff
  103. const URL_ME = '?module=traffstats';
  104. const ROUTE_PROX_IMG = 'loadimg';
  105. const ROUTE_LOGIN = 'username';
  106. const ROUTE_AJUSER = 'defferedgraph';
  107. const ROUTE_AJCAT = 'grcat';
  108. const ROUTE_EXPLICT = 'explict';
  109. const PROUTE_DATE_FROM = 'datefrom';
  110. const PROUTE_DATE_TO = 'dateto';
  111. const PROUTE_TIME_FROM = 'timefrom';
  112. const PROUTE_TIME_TO = 'timeto';
  113. const KEY_GRAPH = 'DEFFEREDGRAPH';
  114. const AJ_CONTAINER = 'ajdefferedcontainer';
  115. /**
  116. * Creates new traffStats instance
  117. *
  118. * @param string $login
  119. */
  120. public function __construct($login = '') {
  121. $this->setLogin($login);
  122. $this->initMessages();
  123. $this->initCache();
  124. $this->loadConfigs();
  125. $this->initDbLayers();
  126. }
  127. /**
  128. * Sets current instance login
  129. *
  130. * @param string $login
  131. *
  132. * @return void
  133. */
  134. protected function setLogin($login = '') {
  135. if (!empty($login)) {
  136. $this->login = ubRouting::filters($login, 'mres');
  137. }
  138. }
  139. /**
  140. * Inits message helper instance for further usage
  141. *
  142. * @return void
  143. */
  144. protected function initMessages() {
  145. $this->messages = new UbillingMessageHelper();
  146. }
  147. /**
  148. * Inits system caching engine
  149. *
  150. * @return void
  151. */
  152. protected function initCache() {
  153. $this->cache = new UbillingCache();
  154. }
  155. /**
  156. * Loads traffic directions
  157. *
  158. * @return void
  159. */
  160. protected function loadDirs() {
  161. $this->dirs = zb_DirectionsGetAll();
  162. }
  163. /**
  164. * Loads required config and sets some properties
  165. *
  166. * @return void
  167. */
  168. protected function loadConfigs() {
  169. global $ubillingConfig;
  170. $this->altCfg = $ubillingConfig->getAlter();
  171. $this->ondemandFlag = $ubillingConfig->getAlterParam('ONDEMAND_CHARTS', false);
  172. $this->trafScale = trim($this->altCfg['TRAFFSIZE']);
  173. if ($this->altCfg[MultiGen::OPTION_ISHIMURA]) {
  174. $this->ishimuraFlag = true;
  175. }
  176. if ($this->altCfg[OphanimFlow::OPTION_ENABLED]) {
  177. $this->ophanimFlag = true;
  178. }
  179. }
  180. /**
  181. * Loads all available users data
  182. *
  183. * @return void
  184. */
  185. protected function loadUserData() {
  186. if (!empty($this->login)) {
  187. $this->userData = zb_UserGetStargazerData($this->login);
  188. }
  189. }
  190. /**
  191. * Inits required database abstraction layers and required objects
  192. *
  193. * @return void
  194. */
  195. protected function initDbLayers() {
  196. $this->statsDb = new NyanORM('stat');
  197. if ($this->ishimuraFlag) {
  198. $this->ishimuraDb = new NyanORM(MultiGen::NAS_ISHIMURA);
  199. }
  200. if ($this->ophanimFlag) {
  201. $this->ophanimFlow = new OphanimFlow();
  202. }
  203. }
  204. /**
  205. * Loads ishimura user traffic data as year=>month=>D0/U0
  206. *
  207. * @return void
  208. */
  209. protected function loadIshimuraStats() {
  210. if (!empty($this->login)) {
  211. if ($this->ishimuraFlag) {
  212. $this->ishimuraDb->where('login', '=', $this->login);
  213. $rawStats = $this->ishimuraDb->getAll();
  214. if (!empty($rawStats)) {
  215. foreach ($rawStats as $io => $each) {
  216. $this->ishimuraData[$each['year']][$each['month']] = $each;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. /**
  223. * Returns native stargazer user current month traffic counters
  224. *
  225. * @param int $dirNum
  226. *
  227. * @return array
  228. */
  229. protected function getUserDataCounters($dirNum) {
  230. $result = array();
  231. $dClass = 'D' . $dirNum;
  232. $uClass = 'U' . $dirNum;
  233. if (isset($this->userData[$dClass])) {
  234. $result[$dClass] = $this->userData[$dClass];
  235. $result[$uClass] = $this->userData[$uClass];
  236. }
  237. return ($result);
  238. }
  239. /**
  240. * Returns ishimura user traffic counters for selected year/month
  241. *
  242. * @param int $year
  243. * @param int $month
  244. *
  245. * @return array
  246. */
  247. protected function getUserIshimuraCounters($year, $month) {
  248. $result = array();
  249. if (isset($this->ishimuraData[$year])) {
  250. if (isset($this->ishimuraData[$year][$month])) {
  251. $result['D0'] = $this->ishimuraData[$year][$month]['D0'];
  252. $result['U0'] = $this->ishimuraData[$year][$month]['U0'];
  253. }
  254. }
  255. return ($result);
  256. }
  257. /**
  258. * Returns fastly user IP from preloaded userdata
  259. *
  260. * @return string
  261. */
  262. protected function getUserIp() {
  263. $result = '';
  264. if (isset($this->userData['IP'])) {
  265. $result = $this->userData['IP'];
  266. }
  267. return ($result);
  268. }
  269. /**
  270. * Loads all previous user traffic stats into protected property
  271. *
  272. * @return void
  273. */
  274. protected function loadTraffStats() {
  275. if (!empty($this->login)) {
  276. $this->statsDb->where('login', '=', $this->login);
  277. $this->statsDb->orderBy('year,month', 'ASC');
  278. $this->trafStats = $this->statsDb->getAll();
  279. }
  280. }
  281. /**
  282. * Returns stats record for selected year/month/class
  283. *
  284. * @param int $classNum
  285. *
  286. * @return array
  287. */
  288. protected function getUserPrevMonthTraff($classNum) {
  289. $result = array();
  290. if (!empty($this->trafStats)) {
  291. foreach ($this->trafStats as $io => $each) {
  292. if (isset($each['D' . $classNum])) {
  293. $result[] = $each;
  294. }
  295. }
  296. }
  297. return ($result);
  298. }
  299. /**
  300. * Render current month traffic report part
  301. *
  302. * @return string
  303. */
  304. protected function renderCurMonthStats() {
  305. $result = '';
  306. $cells = wf_TableCell(__('Traffic classes'));
  307. $cells .= wf_TableCell(__('Downloaded'));
  308. $cells .= wf_TableCell(__('Uploaded'));
  309. $cells .= wf_TableCell(__('Total'));
  310. $rows = wf_TableRow($cells, 'row1');
  311. if (!empty($this->dirs)) {
  312. foreach ($this->dirs as $dir) {
  313. $downup = $this->getUserDataCounters($dir['rulenumber']);
  314. //yeah, no classes at all
  315. if ($dir['rulenumber'] == 0) {
  316. //ishimura enabled?
  317. if ($this->ishimuraFlag) {
  318. $dataHideki = $this->getUserIshimuraCounters(curyear(), date("n"));
  319. if (isset($downup['D0'])) {
  320. @$downup['D0'] += $dataHideki['D0'];
  321. @$downup['U0'] += $dataHideki['U0'];
  322. } else {
  323. $downup['D0'] = $dataHideki['D0'];
  324. $downup['U0'] = $dataHideki['U0'];
  325. }
  326. }
  327. //or ophanim flow may be?
  328. if ($this->ophanimFlag) {
  329. $ophanimCmonth = $this->ophanimFlow->getUserCurMonthTraff($this->login);
  330. if (isset($downup['D0'])) {
  331. $downup['D0'] += $ophanimCmonth['D0'];
  332. $downup['U0'] += $ophanimCmonth['U0'];
  333. } else {
  334. $downup['D0'] = $ophanimCmonth['D0'];
  335. $downup['U0'] = $ophanimCmonth['U0'];
  336. }
  337. }
  338. }
  339. $dLabel = zb_convertSize($downup['D' . $dir['rulenumber']], $this->trafScale);
  340. $uLabel = zb_convertSize($downup['U' . $dir['rulenumber']], $this->trafScale);
  341. $totalLabel = zb_convertSize(($downup['U' . $dir['rulenumber']] + $downup['D' . $dir['rulenumber']]), $this->trafScale);
  342. $cells = wf_TableCell($dir['rulename']);
  343. $cells .= wf_TableCell($dLabel, '', '', 'sorttable_customkey="' . $downup['D' . $dir['rulenumber']] . '"');
  344. $cells .= wf_TableCell($uLabel, '', '', 'sorttable_customkey="' . $downup['U' . $dir['rulenumber']] . '"');
  345. $cells .= wf_TableCell($totalLabel, '', '', 'sorttable_customkey="' . ($downup['U' . $dir['rulenumber']] + $downup['D' . $dir['rulenumber']]) . '"');
  346. $rows .= wf_TableRow($cells, 'row3');
  347. }
  348. }
  349. $result .= wf_tag('h3') . __('Current month traffic stats') . wf_tag('h3', true);
  350. $result .= wf_TableBody($rows, '100%', '0', 'sortable');
  351. return ($result);
  352. }
  353. /**
  354. * Returns the custom dimensions for OphanimFlow charts.
  355. * The custom dimensions in the format '&w={width}&h={height}' or array[w,h], or an empty string|array if no dimensions are found.
  356. *
  357. * @param bool $asArray Whether to return the dimensions as an array or a string.
  358. *
  359. * @return string|array
  360. */
  361. protected function getOphCustomDimensions($asArray = false) {
  362. $result = ($asArray) ? array() : '';
  363. $delimiter = 'x';
  364. if (isset($this->altCfg[OphanimFlow::OPTION_DIMENSIONS])) {
  365. $optionValue = $this->altCfg[OphanimFlow::OPTION_DIMENSIONS];
  366. if (!empty($optionValue) and ispos($optionValue, $delimiter)) {
  367. $raw = explode($delimiter, $optionValue);
  368. if (isset($raw[0]) and isset($raw[1])) {
  369. $w = ubRouting::filters($raw[0], 'int');
  370. $h = ubRouting::filters($raw[1], 'int');
  371. if ($w and $h) {
  372. if ($asArray) {
  373. $result['w'] = $w;
  374. $result['h'] = $h;
  375. } else {
  376. $result .= '&w=' . $w . '&h=' . $h;
  377. }
  378. }
  379. }
  380. }
  381. }
  382. return ($result);
  383. }
  384. /**
  385. * Generates user IP graph images links array
  386. *
  387. * @return array
  388. */
  389. protected function genGraphLinks() {
  390. global $ubillingConfig;
  391. $urls = array();
  392. $zbxGraphsEnabled = $ubillingConfig->getAlterParam('ZABBIX_USER_TRAFFIC_GRAPHS');
  393. $zbxGraphsSearchIdnetify = ($ubillingConfig->getAlterParam('ZABBIX_GRAPHS_SEARCHIDENTIFY')) ? $ubillingConfig->getAlterParam('ZABBIX_GRAPHS_SEARCHIDENTIFY') : 'MAC';
  394. $zbxGraphsSearchField = ($ubillingConfig->getAlterParam('ZABBIX_GRAPHS_SEARCHFIELD')) ? $ubillingConfig->getAlterParam('ZABBIX_GRAPHS_SEARCHFIELD') : 'name';
  395. $zbxGraphsExtended = wf_getBoolFromVar($ubillingConfig->getAlterParam('ZABBIX_GRAPHS_EXTENDED'));
  396. $mlgUseMikrotikGraphs = wf_getBoolFromVar($ubillingConfig->getAlterParam('MULTIGEN_USE_ROS_TRAFFIC_GRAPHS'));
  397. $ofUseMikrotikGraphs = wf_getBoolFromVar($ubillingConfig->getAlterParam('OPHANIM_USE_ROS_TRAFFIC_GRAPHS'));
  398. $ip = $this->getUserIp();
  399. $bwdUrl = zb_BandwidthdGetUrl($ip);
  400. $netid = zb_NetworkGetByIp($ip);
  401. $nasid = zb_NasGetByNet($netid);
  402. if ($nasid) {
  403. $nasdata = zb_NasGetData($nasid);
  404. $nastype = ($mlgUseMikrotikGraphs) ? 'mikrotik' : $nasdata['nastype'];
  405. $zbxAllGraphs = array();
  406. if ($zbxGraphsEnabled) {
  407. $zbxAllGraphs = getCachedZabbixNASGraphIDs();
  408. }
  409. if (!empty($zbxAllGraphs) and isset($zbxAllGraphs[$nasdata['nasip']])) {
  410. switch ($zbxGraphsSearchIdnetify) {
  411. case 'MAC':
  412. $userSearchIdentify = zb_MultinetGetMAC($ip);
  413. break;
  414. case 'login':
  415. $userSearchIdentify = zb_UserGetLoginByIp($ip);
  416. break;
  417. default:
  418. $userSearchIdentify = $ip;
  419. }
  420. $urls = getZabbixUserGraphLinks($ip, $zbxGraphsSearchField, $userSearchIdentify, $zbxAllGraphs, $zbxGraphsExtended);
  421. } else {
  422. if (!empty($bwdUrl)) {
  423. // RouterOS graph model:
  424. if ($nastype == 'mikrotik' and !$ofUseMikrotikGraphs) {
  425. // Get user's IP array:
  426. $alluserips = zb_UserGetAllIPs();
  427. $alluserips = array_flip($alluserips);
  428. if (!ispos($bwdUrl, 'pppoe') and !$mlgUseMikrotikGraphs) {
  429. // Generate graphs paths:
  430. $urls['dayr'] = $bwdUrl . '/' . $alluserips[$ip] . '/daily.gif';
  431. $urls['days'] = null;
  432. $urls['weekr'] = $bwdUrl . '/' . $alluserips[$ip] . '/weekly.gif';
  433. $urls['weeks'] = null;
  434. $urls['monthr'] = $bwdUrl . '/' . $alluserips[$ip] . '/monthly.gif';
  435. $urls['months'] = null;
  436. $urls['yearr'] = $bwdUrl . '/' . $alluserips[$ip] . '/yearly.gif';
  437. $urls['years'] = null;
  438. } elseif ($mlgUseMikrotikGraphs) {
  439. $urls['dayr'] = $bwdUrl . '/' . 'mlg_' . $ip . '/daily.gif';
  440. $urls['days'] = null;
  441. $urls['weekr'] = $bwdUrl . '/' . 'mlg_' . $ip . '/weekly.gif';
  442. $urls['weeks'] = null;
  443. $urls['monthr'] = $bwdUrl . '/' . 'mlg_' . $ip . '/monthly.gif';
  444. $urls['months'] = null;
  445. $urls['yearr'] = $bwdUrl . '/' . 'mlg_' . $ip . '/yearly.gif';
  446. } else {
  447. $urls['dayr'] = $bwdUrl . $alluserips[$ip] . '>/daily.gif';
  448. $urls['days'] = null;
  449. $urls['weekr'] = $bwdUrl . $alluserips[$ip] . '>/weekly.gif';
  450. $urls['weeks'] = null;
  451. $urls['monthr'] = $bwdUrl . $alluserips[$ip] . '>/monthly.gif';
  452. $urls['months'] = null;
  453. $urls['yearr'] = $bwdUrl . $alluserips[$ip] . '>/yearly.gif';
  454. $urls['years'] = null;
  455. }
  456. } else {
  457. // Banwidthd graphs model:
  458. $urls['dayr'] = $bwdUrl . '/' . $ip . '-1-R.png';
  459. $urls['days'] = $bwdUrl . '/' . $ip . '-1-S.png';
  460. $urls['weekr'] = $bwdUrl . '/' . $ip . '-2-R.png';
  461. $urls['weeks'] = $bwdUrl . '/' . $ip . '-2-S.png';
  462. $urls['monthr'] = $bwdUrl . '/' . $ip . '-3-R.png';
  463. $urls['months'] = $bwdUrl . '/' . $ip . '-3-S.png';
  464. $urls['yearr'] = $bwdUrl . '/' . $ip . '-4-R.png';
  465. $urls['years'] = $bwdUrl . '/' . $ip . '-4-S.png';
  466. //OphanimFlow graphs
  467. if (ispos($bwdUrl, 'OphanimFlow') or ispos($bwdUrl, 'of/')) {
  468. $customDimensions = $this->getOphCustomDimensions();
  469. $urls['hourr'] = $bwdUrl . '/?module=graph&dir=R&period=hour&ip=' . $ip . $customDimensions;
  470. $urls['hours'] = $bwdUrl . '/?module=graph&dir=S&period=hour&ip=' . $ip . $customDimensions;
  471. $urls['dayr'] = $bwdUrl . '/?module=graph&dir=R&period=day&ip=' . $ip . $customDimensions;
  472. $urls['days'] = $bwdUrl . '/?module=graph&dir=S&period=day&ip=' . $ip . $customDimensions;
  473. $urls['weekr'] = $bwdUrl . '/?module=graph&dir=R&period=week&ip=' . $ip . $customDimensions;
  474. $urls['weeks'] = $bwdUrl . '/?module=graph&dir=S&period=week&ip=' . $ip . $customDimensions;
  475. $urls['monthr'] = $bwdUrl . '/?module=graph&dir=R&period=month&ip=' . $ip . $customDimensions;
  476. $urls['months'] = $bwdUrl . '/?module=graph&dir=S&period=month&ip=' . $ip . $customDimensions;
  477. $urls['yearr'] = $bwdUrl . '/?module=graph&dir=R&period=year&ip=' . $ip . $customDimensions;
  478. $urls['years'] = $bwdUrl . '/?module=graph&dir=S&period=year&ip=' . $ip . $customDimensions;
  479. $urls['explict'] = $bwdUrl . '/?module=graph&period=explict&ip=' . $ip . $customDimensions;
  480. }
  481. }
  482. //MikroTik Multigen Hotspot users
  483. if (ispos($bwdUrl, 'mlgmths')) {
  484. $bwdUrl = str_replace('mlgmths', 'graphs/queue/', $bwdUrl);
  485. $allUserMacs = zb_UserGetAllIpMACs();
  486. if (isset($allUserMacs[$ip])) {
  487. $userMac = $allUserMacs[$ip];
  488. $userMacUpper = strtoupper($userMac);
  489. $queueName = '<hotspot-' . urlencode($userMacUpper);
  490. $urls['dayr'] = $bwdUrl . $queueName . '>/daily.gif';
  491. $urls['days'] = null;
  492. $urls['weekr'] = $bwdUrl . $queueName . '>/weekly.gif';
  493. $urls['weeks'] = null;
  494. $urls['monthr'] = $bwdUrl . $queueName . '>/monthly.gif';
  495. $urls['months'] = null;
  496. $urls['yearr'] = $bwdUrl . $queueName . '>/yearly.gif';
  497. $urls['years'] = null;
  498. }
  499. }
  500. //MikroTik Multigen PPP
  501. if (ispos($bwdUrl, 'mlgmtppp')) {
  502. $bwdUrl = str_replace('mlgmtppp', 'graphs/queue/', $bwdUrl);
  503. $alluserips = zb_UserGetAllIPs();
  504. $alluserips = array_flip($alluserips);
  505. if (isset($alluserips[$ip])) {
  506. $userLogin = $alluserips[$ip];
  507. $queueName = urlencode('<pppoe-' . $userLogin . '>');
  508. $urls['dayr'] = $bwdUrl . $queueName . '/daily.gif';
  509. $urls['days'] = null;
  510. $urls['weekr'] = $bwdUrl . $queueName . '/weekly.gif';
  511. $urls['weeks'] = null;
  512. $urls['monthr'] = $bwdUrl . $queueName . '/monthly.gif';
  513. $urls['months'] = null;
  514. $urls['yearr'] = $bwdUrl . $queueName . '/yearly.gif';
  515. $urls['years'] = null;
  516. }
  517. }
  518. //MikroTik Multigen DHCP
  519. if (ispos($bwdUrl, 'mlgmtdhcp')) {
  520. $bwdUrl = str_replace('mlgmtdhcp', 'graphs/queue/', $bwdUrl);
  521. $allUserMacs = zb_UserGetAllIpMACs();
  522. if (isset($allUserMacs[$ip])) {
  523. $userMac = $allUserMacs[$ip];
  524. $userMacUpper = strtoupper($userMac);
  525. $queueName = 'dhcp-ds<' . urlencode($userMacUpper);
  526. $urls['dayr'] = $bwdUrl . $queueName . '>/daily.gif';
  527. $urls['days'] = null;
  528. $urls['weekr'] = $bwdUrl . $queueName . '>/weekly.gif';
  529. $urls['weeks'] = null;
  530. $urls['monthr'] = $bwdUrl . $queueName . '>/monthly.gif';
  531. $urls['months'] = null;
  532. $urls['yearr'] = $bwdUrl . $queueName . '>/yearly.gif';
  533. $urls['years'] = null;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return ($urls);
  540. }
  541. /**
  542. * Returns explict time interval selection form and results
  543. *
  544. * @param string $explictUrl base64 encoded base charts URL
  545. *
  546. * @return void
  547. */
  548. public function renderExplictChartsForm($explictUrl) {
  549. $result = '';
  550. if (!empty($explictUrl)) {
  551. $baseUrl = base64_decode($explictUrl);
  552. $dateFrom = (ubRouting::checkPost(self::PROUTE_DATE_FROM)) ? ubRouting::post(self::PROUTE_DATE_FROM) : curdate();
  553. $dateTo = (ubRouting::checkPost(self::PROUTE_DATE_TO)) ? ubRouting::post(self::PROUTE_DATE_TO) : curdate();
  554. $timeFrom = (ubRouting::checkPost(self::PROUTE_TIME_FROM)) ? ubRouting::post(self::PROUTE_TIME_FROM) : '00:00';
  555. $timeTo = (ubRouting::checkPost(self::PROUTE_TIME_TO)) ? ubRouting::post(self::PROUTE_TIME_TO) : '23:59';
  556. $timestampFrom = strtotime($dateFrom . ' ' . $timeFrom . ':00');
  557. $timestampTo = strtotime($dateTo . ' ' . $timeTo . ':59');
  558. //interval selection form here
  559. $inputs = '';
  560. $inputs .= __('From') . ': ';
  561. $inputs .= wf_DatePickerPreset(self::PROUTE_DATE_FROM, $dateFrom, true);
  562. $inputs .= wf_TimePickerPreset(self::PROUTE_TIME_FROM, $timeFrom);
  563. $inputs .= __('To') . ': ';
  564. $inputs .= wf_DatePickerPreset(self::PROUTE_DATE_TO, $dateTo, true);
  565. $inputs .= wf_TimePickerPreset(self::PROUTE_TIME_TO, $timeTo) . ' ';
  566. $inputs .= wf_Submit(__('Show'));
  567. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  568. //charts rendering
  569. if ($timestampFrom <= $timestampTo) {
  570. $completeUrl = $baseUrl . '&from=' . $timestampFrom . '&to=' . $timestampTo;
  571. $downloadUrl = zb_BandwidthdImgLink(($completeUrl . '&dir=R'));
  572. $uploadUrl = zb_BandwidthdImgLink(($completeUrl . '&dir=S'));
  573. $result .= wf_delimiter();
  574. $result .= wf_img_sized($downloadUrl, __('Downloaded'), '100%');
  575. $result .= wf_delimiter(0);
  576. $result .= wf_img_sized($uploadUrl, __('Uploaded'), '100%');
  577. } else {
  578. $result .= $this->messages->getStyledMessage(__('Wrong date format'), 'error');
  579. $result .= wf_delimiter(0);
  580. $result .= wf_tag('center') . wf_img('skins/unicornwrong.png') . wf_tag('center', true);
  581. $result .= wf_delimiter();
  582. }
  583. }
  584. $result .= wf_delimiter();
  585. $result .= wf_BackLink(self::URL_ME . '&' . self::ROUTE_LOGIN . '=' . $this->login);
  586. return ($result);
  587. }
  588. /**
  589. * Renders user traffic charts
  590. *
  591. * @return string
  592. */
  593. protected function renderCharts() {
  594. $result = '';
  595. $bwd = $this->genGraphLinks();
  596. $cachedUserCharts = array();
  597. $cachedCharts = array();
  598. $updateCache = false;
  599. $chartCategories = array(
  600. 'hour' => __('Hour'),
  601. 'day' => __('Graph by day'),
  602. 'week' => __('Graph by week'),
  603. 'month' => __('Graph by month'),
  604. 'year' => __('Graph by year'),
  605. );
  606. if (!empty($bwd)) {
  607. $icon = wf_img_sized('skins/icon_stats.gif', '', '16', '16');
  608. $result .= wf_delimiter();
  609. $result .= wf_tag('h3') . __('Graphs') . wf_tag('h3', true);
  610. $bwcells = '';
  611. $zbxExtended = (isset($bwd['zbxexten']) and $bwd['zbxexten'] == true);
  612. if ($zbxExtended) {
  613. $fiveminsbw = wf_img($bwd['5mins'], __('Downloaded'));
  614. $zbxLink = $bwd['zbxlink'];
  615. $zbxIcon = wf_img_sized('skins/zabbix_ico.png', '', '16', '16');
  616. $bwcells .= wf_link($zbxLink, $zbxIcon . ' ' . __('Go to graph on Zabbix server'), false, 'ubButton', 'target="__blank"');
  617. $bwcells .= wf_modalAuto($icon . ' ' . __('Graph by 5 minutes'), __('Graph by 5 minutes'), $fiveminsbw, 'ubButton');
  618. }
  619. if ($this->ondemandFlag) {
  620. $cachedCharts = $this->cache->get(self::KEY_GRAPH, $this->cachingTimeout);
  621. if (!empty($cachedCharts)) {
  622. if (isset($cachedCharts[$this->login])) {
  623. $cachedUserCharts = $cachedCharts[$this->login];
  624. }
  625. } else {
  626. $cachedCharts = array();
  627. }
  628. }
  629. if ($this->ondemandFlag) {
  630. if (empty($cachedUserCharts)) {
  631. $updateCache = true;
  632. }
  633. } else {
  634. $updateCache = true;
  635. }
  636. //charts cats processing here
  637. if ($updateCache) {
  638. foreach ($chartCategories as $categoryId => $categoryTitle) {
  639. $chartBody = '';
  640. $dlKey = $categoryId . 'r';
  641. $ulKey = $categoryId . 's';
  642. $chartTitle = $categoryTitle;
  643. if (isset($bwd[$dlKey])) {
  644. $imgLink = zb_BandwidthdImgLink($bwd[$dlKey]);
  645. $imgBody = wf_img_sized($imgLink, __('Downloaded'), '100%');
  646. $chartBody .= $imgBody;
  647. }
  648. if (isset($bwd[$ulKey])) {
  649. $imgLink = zb_BandwidthdImgLink($bwd[$ulKey]);
  650. $imgBody = wf_delimiter() . wf_img_sized($imgLink, __('Uploaded'), '100%');
  651. $chartBody .= $imgBody;
  652. }
  653. if (!empty($chartBody)) {
  654. if ($this->ondemandFlag) {
  655. $cachedCharts[$this->login][$categoryId] = array('title' => $chartTitle, 'body' => $chartBody);
  656. $defLink = $this->getAjChartLink($categoryId, $chartTitle);
  657. $bwcells .= $defLink;
  658. } else {
  659. $chartBody = wf_tag('div', false, '', 'style="width:100%;"') . $chartBody . wf_tag('div', true);
  660. $bwcells .= wf_modalAuto($icon . ' ' . $chartTitle, $chartTitle, $chartBody, 'ubButton');
  661. }
  662. }
  663. }
  664. //cache update
  665. if ($this->ondemandFlag) {
  666. $this->cache->set(self::KEY_GRAPH, $cachedCharts, $this->cachingTimeout);
  667. }
  668. } else {
  669. //deffered cache filled
  670. if ($this->ondemandFlag) {
  671. foreach ($chartCategories as $categoryId => $categoryTitle) {
  672. $defLink = $this->getAjChartLink($categoryId, $categoryTitle);
  673. $bwcells .= wf_TableCell($defLink);
  674. }
  675. }
  676. }
  677. // Explict ophanim charts controls here
  678. if (isset($bwd['explict'])) {
  679. $encUrl = base64_encode($bwd['explict']);
  680. $explictUrl = self::URL_ME . '&' . self::ROUTE_LOGIN . '=' . $this->login . '&' . self::ROUTE_EXPLICT . '=' . $encUrl;
  681. $bwcells .= wf_Link($explictUrl, web_icon_calendar() . ' ' . __('Explict interval'), false, 'ubButton');
  682. }
  683. // Adding graphs buttons to result:
  684. $result .= $bwcells;
  685. if ($this->ondemandFlag) {
  686. $result .= wf_AjaxLoader();
  687. $result .= wf_AjaxContainer(self::AJ_CONTAINER);
  688. }
  689. $result .= wf_delimiter();
  690. }
  691. return ($result);
  692. }
  693. /**
  694. * Returns link body for deffered charts loading
  695. *
  696. * @param string $graphCat
  697. * @param string $title
  698. *
  699. * @return string
  700. */
  701. protected function getAjChartLink($graphCat, $title) {
  702. $result = '';
  703. $icon = wf_img_sized('skins/icon_stats.gif', '', '16', '16');
  704. $defUrl = self::URL_ME . '&' . self::ROUTE_AJUSER . '=' . $this->login . '&' . self::ROUTE_AJCAT . '=' . $graphCat;
  705. $result .= wf_AjaxLink($defUrl, $icon . ' ' . $title, self::AJ_CONTAINER, false, 'ubButton');
  706. return ($result);
  707. }
  708. /**
  709. * Renders all previous months user traffic stats
  710. *
  711. * @return string
  712. */
  713. protected function renderPrevMonthStats() {
  714. $result = '';
  715. $ishimuraTable = 'mlg_ishimura';
  716. $monthNames = months_array_wz();
  717. $result .= wf_tag('h3') . __('Previous month traffic stats') . wf_tag('h3', true);
  718. $cells = wf_TableCell(__('Year'));
  719. $cells .= wf_TableCell(__('Month'));
  720. $cells .= wf_TableCell(__('Traffic classes'));
  721. $cells .= wf_TableCell(__('Downloaded'));
  722. $cells .= wf_TableCell(__('Uploaded'));
  723. $cells .= wf_TableCell(__('Total'));
  724. $cells .= wf_TableCell(__('Cash'));
  725. $rows = wf_TableRow($cells, 'row1');
  726. if (!empty($this->dirs)) {
  727. foreach ($this->dirs as $dir) {
  728. $prevmonths = $this->getUserPrevMonthTraff($dir['rulenumber']);
  729. //and again no classes
  730. if ($dir['rulenumber'] == 0) {
  731. //ishimura traffic accounting?
  732. if ($this->ishimuraFlag) {
  733. $dataHideki = $this->ishimuraData;
  734. if (!empty($dataHideki)) {
  735. foreach ($dataHideki as $year => $month) {
  736. foreach ($month as $each) {
  737. foreach ($prevmonths as $ia => $stgEach) {
  738. if ($stgEach['year'] == $each['year'] and $stgEach['month'] == $each['month']) {
  739. $prevmonths[$ia]['D0'] += $each['D0'];
  740. $prevmonths[$ia]['U0'] += $each['U0'];
  741. }
  742. }
  743. }
  744. }
  745. }
  746. }
  747. //or just OphanimFlow integration?
  748. if ($this->ophanimFlag) {
  749. $ophRaw = $this->ophanimFlow->getUserAllTraff($this->login);
  750. if (!empty($ophRaw)) {
  751. foreach ($ophRaw as $io => $each) {
  752. foreach ($prevmonths as $ia => $stgEach) {
  753. if ($stgEach['year'] == $each['year'] and $stgEach['month'] == $each['month']) {
  754. $prevmonths[$ia]['D0'] += $each['D0'];
  755. $prevmonths[$ia]['U0'] += $each['U0'];
  756. }
  757. }
  758. }
  759. }
  760. }
  761. }
  762. if (!empty($prevmonths)) {
  763. $prevmonths = array_reverse($prevmonths);
  764. }
  765. if (!empty($prevmonths)) {
  766. foreach ($prevmonths as $prevmonth) {
  767. $cells = wf_TableCell($prevmonth['year']);
  768. $cells .= wf_TableCell(rcms_date_localise($monthNames[$prevmonth['month']]));
  769. $cells .= wf_TableCell($dir['rulename']);
  770. $cells .= wf_TableCell(stg_convert_size($prevmonth['D' . $dir['rulenumber']]), '', '', 'sorttable_customkey="' . $prevmonth['D' . $dir['rulenumber']] . '"');
  771. $cells .= wf_TableCell(stg_convert_size($prevmonth['U' . $dir['rulenumber']]), '', '', 'sorttable_customkey="' . $prevmonth['U' . $dir['rulenumber']] . '"');
  772. $cells .= wf_TableCell(stg_convert_size(($prevmonth['U' . $dir['rulenumber']] + $prevmonth['D' . $dir['rulenumber']])), '', '', 'sorttable_customkey="' . ($prevmonth['U' . $dir['rulenumber']] + $prevmonth['D' . $dir['rulenumber']]) . '"');
  773. $cells .= wf_TableCell(round($prevmonth['cash'], 2));
  774. $rows .= wf_TableRow($cells, 'row3');
  775. }
  776. }
  777. }
  778. }
  779. $result .= wf_TableBody($rows, '100%', '0', 'sortable');
  780. return ($result);
  781. }
  782. /**
  783. * Generates user`s traffic full statistics report
  784. *
  785. * @param string $login
  786. *
  787. * @return string
  788. */
  789. public function renderUserTraffStats() {
  790. $result = '';
  791. $this->loadUserData();
  792. if (!empty($this->userData)) {
  793. $this->loadDirs();
  794. $this->loadTraffStats();
  795. $this->loadIshimuraStats();
  796. // Current month traffic stats
  797. $result .= $this->renderCurMonthStats();
  798. // Some charts here
  799. $result .= $this->renderCharts();
  800. // Traffic statistic by previous months
  801. $result .= $this->renderPrevMonthStats();
  802. // and some user controls
  803. $result .= web_UserControls($this->login);
  804. } else {
  805. $result .= $this->messages->getStyledMessage(__('Strange exception') . ': EMPTY_DATABASE_USERDATA', 'error');
  806. $result .= wf_delimiter(0);
  807. $result .= wf_tag('center') . wf_img('skins/unicornwrong.png') . wf_tag('center', true);
  808. $result .= wf_delimiter();
  809. }
  810. return ($result);
  811. }
  812. /**
  813. * Catches image proxy request and renders some image
  814. *
  815. * @return void
  816. */
  817. public function catchImgProxyRequest() {
  818. if ($this->altCfg['BANDWIDTHD_PROXY']) {
  819. if (ubRouting::checkGet(self::ROUTE_PROX_IMG)) {
  820. $remoteImageUrl = base64_decode(ubRouting::get(self::ROUTE_PROX_IMG));
  821. $remoteImageUrl = trim($remoteImageUrl);
  822. if (!empty($remoteImageUrl)) {
  823. $remoteImg = new OmaeUrl($remoteImageUrl);
  824. $remoteImg->setTimeout(1);
  825. $rawImg = $remoteImg->response();
  826. $recvErr = $remoteImg->error();
  827. $type = '';
  828. if (ispos($remoteImageUrl, '.png') or ispos($remoteImageUrl, 'module=graph')) {
  829. $type = 'png';
  830. } else {
  831. if (ispos($remoteImageUrl, '.gif')) {
  832. $type = 'gif';
  833. }
  834. }
  835. if (empty($recvErr) and !ispos($rawImg, '404')) {
  836. if (!empty($type)) {
  837. header('Content-Type: image/' . $type);
  838. }
  839. die($rawImg);
  840. } else {
  841. header('Content-Type: image/jpeg');
  842. $noImage = file_get_contents('skins/noimage.jpg');
  843. die($noImage);
  844. }
  845. } else {
  846. header('Content-Type: image/jpeg');
  847. $noImage = file_get_contents('skins/noimage.jpg');
  848. die($noImage);
  849. }
  850. }
  851. }
  852. }
  853. /**
  854. * Renders deffered chart request data
  855. *
  856. * @return void
  857. */
  858. public function catchDefferedCallback() {
  859. $result = '';
  860. $notice = __('Something went wrong') . ': ';
  861. if ($this->ondemandFlag) {
  862. if (ubRouting::checkGet(array(self::ROUTE_AJUSER, self::ROUTE_AJCAT))) {
  863. $userLogin = ubRouting::get(self::ROUTE_AJUSER, 'mres');
  864. $chartCat = ubRouting::get(self::ROUTE_AJCAT, 'mres');
  865. if ($userLogin and $chartCat) {
  866. $cachedCharts = $this->cache->get(self::KEY_GRAPH, $this->cachingTimeout);
  867. if (!empty($cachedCharts)) {
  868. if (isset($cachedCharts[$userLogin])) {
  869. $userCache = $cachedCharts[$userLogin];
  870. if (isset($userCache[$chartCat])) {
  871. $styling = 'width:1540px; height:810px; border:0px solid;';
  872. $chartBody = wf_tag('div', false, '', 'style="' . $styling . '"') . $userCache[$chartCat]['body'] . wf_tag('div', true);
  873. $result .= wf_modalOpenedAuto($userCache[$chartCat]['title'], $chartBody);
  874. } else {
  875. $result .= $this->messages->getStyledMessage($notice . __('no cached cat') . ' [' . $chartCat . ']', 'error');
  876. }
  877. } else {
  878. $result .= $this->messages->getStyledMessage($notice . __('No user charts'), 'error');
  879. }
  880. } else {
  881. $result .= $this->messages->getStyledMessage($notice . __('Empty charts cache'), 'error');
  882. }
  883. } else {
  884. $result .= $this->messages->getStyledMessage($notice . __('Empty params'), 'error');
  885. }
  886. } else {
  887. $result .= $this->messages->getStyledMessage($notice . __('Missed params'), 'error');
  888. }
  889. } else {
  890. $result .= $this->messages->getStyledMessage($notice . __('Disabled'), 'error');
  891. }
  892. die($result);
  893. }
  894. //TODO: refactor following two methods of global traffic report. They was backported as is.
  895. /**
  896. * Renders the global traffic report.
  897. *
  898. * This method retrieves traffic data for each traffic class and generates a report.
  899. * It calculates the total traffic for each class by summing the download and upload traffic values from the 'users' table.
  900. * If the traffic class is 0, it also includes additional traffic data from the 'ishimura' table and the 'ophanim' flow.
  901. *
  902. * @return string
  903. */
  904. public function renderTrafficReport() {
  905. $ishimuraOption = MultiGen::OPTION_ISHIMURA;
  906. $ishimuraTable = MultiGen::NAS_ISHIMURA;
  907. $allclasses = zb_DirectionsGetAll();
  908. $classtraff = array();
  909. $traffCells = wf_TableCell(__('Traffic classes'), '20%');
  910. $traffCells .= wf_TableCell(__('Traffic'), '20%');
  911. $traffCells .= wf_TableCell(__('Traffic classes'));
  912. $traffRows = wf_TableRow($traffCells, 'row1');
  913. if (!empty($allclasses)) {
  914. foreach ($allclasses as $eachclass) {
  915. $d_name = 'D' . $eachclass['rulenumber'];
  916. $u_name = 'U' . $eachclass['rulenumber'];
  917. $query_d = "SELECT SUM(`" . $d_name . "`) FROM `users`";
  918. $query_u = "SELECT SUM(`" . $u_name . "`) FROM `users`";
  919. $classdown = simple_query($query_d);
  920. $classdown = $classdown['SUM(`' . $d_name . '`)'];
  921. $classup = simple_query($query_u);
  922. $classup = $classup['SUM(`' . $u_name . '`)'];
  923. $classtraff[$eachclass['rulename']] = $classdown + $classup;
  924. //Yep, no traffic classes at all. Just internet accounting here.
  925. if ($eachclass['rulenumber'] == 0) {
  926. //ishimura data
  927. if ($this->altCfg[$ishimuraOption]) {
  928. $query_hideki = "SELECT SUM(`D0`) as `downloaded`, SUM(`U0`) as `uploaded` from `" . $ishimuraTable . "` WHERE `month`='" . date("n") . "' AND `year`='" . curyear() . "'";
  929. $dataHideki = simple_query($query_hideki);
  930. if (isset($classtraff[$eachclass['rulename']])) {
  931. @$classtraff[$eachclass['rulename']] += $dataHideki['downloaded'] + $dataHideki['uploaded'];
  932. } else {
  933. $classtraff[$eachclass['rulename']] = $dataHideki['downloaded'] + $dataHideki['uploaded'];
  934. }
  935. }
  936. //or ophanim flow may be?
  937. if ($this->altCfg[OphanimFlow::OPTION_ENABLED]) {
  938. $ophanim = new OphanimFlow();
  939. $ophTraff = $ophanim->getAllUsersAggrTraff();
  940. if (!empty($ophTraff)) {
  941. foreach ($ophTraff as $io => $each) {
  942. $classtraff[$eachclass['rulename']] += $each;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. if (!empty($classtraff)) {
  949. $total = max($classtraff);
  950. foreach ($classtraff as $name => $count) {
  951. $traffCells = wf_TableCell($name);
  952. $traffCells .= wf_TableCell(stg_convert_size($count), '', '', 'sorttable_customkey="' . $count . '"');
  953. $traffCells .= wf_TableCell(web_bar($count, $total), '', '', 'sorttable_customkey="' . $count . '"');
  954. $traffRows .= wf_TableRow($traffCells, 'row3');
  955. }
  956. }
  957. }
  958. $result = wf_TableBody($traffRows, '100%', 0, 'sortable');
  959. return ($result);
  960. }
  961. /**
  962. * Renders the traffic report NAS charts.
  963. *
  964. * This method retrieves the NAS list with bandwidthd set up and generates charts for each NAS.
  965. * The charts include daily, weekly, monthly, and yearly traffic data.
  966. *
  967. * @return string
  968. */
  969. public function renderTrafficReportNasCharts() {
  970. // Get NAS list with bandwidth setted up:
  971. $query = 'SELECT * FROM `nas` WHERE `bandw` != "" GROUP by `bandw`';
  972. $result = simple_queryall($query);
  973. // Check presence of any entry:
  974. if (!empty($result)) {
  975. $graphRows = null;
  976. foreach ($result as $nas) {
  977. $bwd = $nas['bandw'];
  978. switch ($nas['nastype']) {
  979. case 'local':
  980. case 'radius':
  981. case 'rscriptd':
  982. //normal bandwidthd
  983. if (!ispos($bwd, 'mlgmths') and !ispos($bwd, 'mlgmtppp') and !ispos($bwd, 'mlgmtdhcp')) {
  984. // Extention:
  985. $ext = '.png';
  986. // Modals:
  987. $width = 940;
  988. $height = 666;
  989. // Links:
  990. $d_day = $bwd . 'Total-1-R' . $ext;
  991. $d_week = $bwd . 'Total-2-R' . $ext;
  992. $d_month = $bwd . 'Total-3-R' . $ext;
  993. $d_year = $bwd . 'Total-4-R' . $ext;
  994. $u_day = $bwd . 'Total-1-S' . $ext;
  995. $u_week = $bwd . 'Total-2-S' . $ext;
  996. $u_month = $bwd . 'Total-3-S' . $ext;
  997. $u_year = $bwd . 'Total-4-S' . $ext;
  998. //OphanimFlow graphs
  999. if (ispos($bwd, 'OphanimFlow') or ispos($bwd, 'of/')) {
  1000. $width = 1540; //defaults
  1001. $height = 400;
  1002. $offsetX = 60; //window size offsets
  1003. $offsetY = 60;
  1004. $custParams = '';
  1005. $customDimensions = $this->getOphCustomDimensions(true);
  1006. if ($customDimensions) {
  1007. $custParams = $this->getOphCustomDimensions(false); //as string
  1008. $width = $customDimensions['w'];
  1009. $height = $customDimensions['h'];
  1010. }
  1011. $d_day = $bwd . '/?module=graph&ip=0.0.0.0&dir=R&period=day' . $custParams;
  1012. $d_week = $bwd . '/?module=graph&ip=0.0.0.0&dir=R&period=week' . $custParams;
  1013. $d_month = $bwd . '/?module=graph&ip=0.0.0.0&dir=R&period=month' . $custParams;
  1014. $d_year = $bwd . '/?module=graph&ip=0.0.0.0&dir=R&period=year' . $custParams;
  1015. $u_day = $bwd . '/?module=graph&ip=0.0.0.0&dir=S&period=day' . $custParams;
  1016. $u_week = $bwd . '/?module=graph&ip=0.0.0.0&dir=S&period=week' . $custParams;
  1017. $u_month = $bwd . '/?module=graph&ip=0.0.0.0&dir=S&period=month' . $custParams;
  1018. $u_year = $bwd . '/?module=graph&ip=0.0.0.0&dir=S&period=year' . $custParams;
  1019. $width = $width + $offsetX;
  1020. $height = ($height * 2) + ($offsetY * 2);
  1021. }
  1022. $daygraph = __('Downloaded') . wf_img(zb_BandwidthdImgLink($d_day)) . wf_tag('br') . __('Uploaded') . wf_tag('br') . wf_img(zb_BandwidthdImgLink($u_day));
  1023. $weekgraph = __('Downloaded') . wf_img(zb_BandwidthdImgLink($d_week)) . wf_tag('br') . __('Uploaded') . wf_tag('br') . wf_img(zb_BandwidthdImgLink($u_week));
  1024. $monthgraph = __('Downloaded') . wf_img(zb_BandwidthdImgLink($d_month)) . wf_tag('br') . __('Uploaded') . wf_tag('br') . wf_img(zb_BandwidthdImgLink($u_month));
  1025. $yeargraph = __('Downloaded') . wf_img(zb_BandwidthdImgLink($d_year)) . wf_tag('br') . __('Uploaded') . wf_tag('br') . wf_img(zb_BandwidthdImgLink($u_year));
  1026. $graphLegend = wf_tag('br') . wf_img('skins/bwdlegend.gif');
  1027. } else {
  1028. //Multigen Mikrotik hotspot
  1029. $bwd = str_replace('mlgmths', 'graphs/iface/bridge', $bwd);
  1030. $bwd = str_replace('mlgmtppp', 'graphs/iface/bridge', $bwd);
  1031. $bwd = str_replace('mlgmtdhcp', 'graphs/iface/bridge', $bwd);
  1032. $ext = '.gif';
  1033. $daily = $bwd . '/daily' . $ext;
  1034. $weekly = $bwd . '/weekly' . $ext;
  1035. $monthly = $bwd . '/monthly' . $ext;
  1036. $yearly = $bwd . '/yearly' . $ext;
  1037. // Modals:
  1038. $width = 530;
  1039. $height = 250;
  1040. $daygraph = wf_img(zb_BandwidthdImgLink($daily));
  1041. $weekgraph = wf_img(zb_BandwidthdImgLink($weekly));
  1042. $monthgraph = wf_img(zb_BandwidthdImgLink($monthly));
  1043. $yeargraph = wf_img(zb_BandwidthdImgLink($yearly));
  1044. $graphLegend = '';
  1045. }
  1046. break;
  1047. case 'mikrotik':
  1048. if (!ispos($bwd, 'pppoe')) {
  1049. $options = zb_NasOptionsGet($nas['id']);
  1050. if (!empty($options['graph_interface'])) {
  1051. // Extention:
  1052. $ext = '.gif';
  1053. // Links:
  1054. $daily = $bwd . '/../iface/' . $options['graph_interface'] . '/daily' . $ext;
  1055. $weekly = $bwd . '/../iface/' . $options['graph_interface'] . '/weekly' . $ext;
  1056. $monthly = $bwd . '/../iface/' . $options['graph_interface'] . '/monthly' . $ext;
  1057. $yearly = $bwd . '/../iface/' . $options['graph_interface'] . '/yearly' . $ext;
  1058. // Modals:
  1059. $width = 530;
  1060. $height = 230;
  1061. $daygraph = wf_img($daily);
  1062. $weekgraph = wf_img($weekly);
  1063. $monthgraph = wf_img($monthly);
  1064. $yeargraph = wf_img($yearly);
  1065. $graphLegend = '';
  1066. break;
  1067. } else {
  1068. show_error(__('For NAS') . ' `' . $nas['nasname'] . '` ' . __('was not set correct graph interface'));
  1069. }
  1070. } else {
  1071. $width = 530;
  1072. $height = 230;
  1073. $daygraph = '';
  1074. $weekgraph = '';
  1075. $monthgraph = '';
  1076. $yeargraph = '';
  1077. $graphLegend = '';
  1078. }
  1079. }
  1080. if (!ispos($bwd, 'OphanimFlow') and !ispos($bwd, 'of/')) {
  1081. $graphLegend = wf_tag('br') . wf_img('skins/bwdlegend.gif');
  1082. } else {
  1083. $graphLegend = '';
  1084. }
  1085. // Buttons:
  1086. $gday = wf_modal(__('Graph by day'), __('Graph by day'), $daygraph . $graphLegend, '', $width, $height);
  1087. $gweek = wf_modal(__('Graph by week'), __('Graph by week'), $weekgraph . $graphLegend, '', $width, $height);
  1088. $gmonth = wf_modal(__('Graph by month'), __('Graph by month'), $monthgraph . $graphLegend, '', $width, $height);
  1089. $gyear = wf_modal(__('Graph by year'), __('Graph by year'), $yeargraph . $graphLegend, '', $width, $height);
  1090. // Put buttons to table row:
  1091. $graphCells = wf_TableCell($nas['nasname'], '', 'row2');
  1092. $graphCells .= wf_TableCell($gday);
  1093. $graphCells .= wf_TableCell($gweek);
  1094. $graphCells .= wf_TableCell($gmonth);
  1095. $graphCells .= wf_TableCell($gyear);
  1096. $graphRows .= wf_TableRow($graphCells, 'row3');
  1097. }
  1098. $result = wf_TableBody($graphRows, '100%', 0, '');
  1099. } else {
  1100. $result = '';
  1101. }
  1102. return ($result);
  1103. }
  1104. }