PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gui/public/client/domain_statistics.php

https://bitbucket.org/droidzone/i-mscp
PHP | 234 lines | 139 code | 31 blank | 64 comment | 16 complexity | a063c969dc9482f9597ea331c6318410 MD5 | raw file
  1. <?php
  2. /**
  3. * i-MSCP - internet Multi Server Control Panel
  4. *
  5. * The contents of this file are subject to the Mozilla Public License
  6. * Version 1.1 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS"
  11. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  12. * License for the specific language governing rights and limitations
  13. * under the License.
  14. *
  15. * The Original Code is "VHCS - Virtual Hosting Control System".
  16. *
  17. * The Initial Developer of the Original Code is moleSoftware GmbH.
  18. * Portions created by Initial Developer are Copyright (C) 2001-2006
  19. * by moleSoftware GmbH. All Rights Reserved.
  20. *
  21. * Portions created by the ispCP Team are Copyright (C) 2006-2010 by
  22. * isp Control Panel. All Rights Reserved.
  23. *
  24. * Portions created by the i-MSCP Team are Copyright (C) 2010-2013 by
  25. * i-MSCP - internet Multi Server Control Panel. All Rights Reserved.
  26. *
  27. * @category i-MSCP
  28. * @package iMSCP_Core
  29. * @subpackage Client
  30. * @copyright 2001-2006 by moleSoftware GmbH
  31. * @copyright 2006-2010 by ispCP | http://isp-control.net
  32. * @copyright 2010-2013 by i-MSCP | http://i-mscp.net
  33. * @author ispCP Team
  34. * @author i-MSCP Team
  35. * @link http://i-mscp.net
  36. */
  37. /*********************************************************************************
  38. * Script functions
  39. */
  40. /**
  41. * Returns traffic information for the given domain and period.
  42. *
  43. * @access private
  44. * @param int $domainId domain unique identifier
  45. * @param int $beginTime An UNIX timestamp representing a begin time period
  46. * @param int $endTime An UNIX timestamp representing an end time period
  47. * @return array
  48. */
  49. function _client_getDomainTraffic($domainId, $beginTime, $endTime)
  50. {
  51. $query = "
  52. SELECT
  53. IFNULL(SUM(`dtraff_web`), 0) `web_dr`, IFNULL(SUM(`dtraff_ftp`), 0) `ftp_dr`,
  54. IFNULL(SUM(`dtraff_mail`), 0) `mail_dr`, IFNULL(SUM(`dtraff_pop`), 0) `pop_dr`
  55. FROM
  56. `domain_traffic`
  57. WHERE
  58. `domain_id` = ? AND `dtraff_time` >= ? AND `dtraff_time` <= ?
  59. ";
  60. $stmt = exec_query($query, array($domainId, $beginTime, $endTime));
  61. if (!$stmt->rowCount()) {
  62. return array(0, 0, 0, 0);
  63. } else {
  64. return array(
  65. $stmt->fields['web_dr'], $stmt->fields['ftp_dr'], $stmt->fields['mail_dr'], $stmt->fields['pop_dr']
  66. );
  67. }
  68. }
  69. /**
  70. * Generate domain statistics for the given period.
  71. *
  72. * @param iMSCP_pTemplate $tpl Template engine instance
  73. * @param int $domainId Domain unique identifier
  74. * @param int $month Month of the period for which statistics are requested
  75. * @param int $year Year of the period for which statistics are requested
  76. * @return void
  77. */
  78. function client_generatePage($tpl, $domainId, $month, $year)
  79. {
  80. // Let see if the domain exists
  81. $stmt = exec_query('SELECT `domain_id`, `domain_name` FROM `domain` WHERE `domain_id` = ?', $domainId);
  82. if (!$stmt->rowCount()) {
  83. set_page_message(tr('Domain not found.'), 'error');
  84. redirectTo('index.php');
  85. } else {
  86. $tpl->assign('DOMAIN_NAME', idn_to_utf8($stmt->fields['domain_name']));
  87. }
  88. // Let see if we have any statistics available for the given periode
  89. $query = "SELECT `domain_id` FROM `domain_traffic` WHERE `dtraff_time` > ? AND `dtraff_time` < ? LIMIT 1";
  90. $stmt = exec_query($query, array(getFirstDayOfMonth($month, $year), getLastDayOfMonth($month, $year)));
  91. $tpl->assign('DOMAIN_ID', $domainId);
  92. if ($stmt->rowCount()) {
  93. $requestedPeriod = getLastDayOfMonth($month, $year);
  94. $toDay = ($requestedPeriod < time()) ? date('j', $requestedPeriod) : date('j');
  95. $all = array_fill(0, 8, 0);
  96. $dateFormat = iMSCP_Registry::get('config')->DATE_FORMAT;
  97. for ($fromDay = 1; $fromDay <= $toDay; $fromDay++) {
  98. $beginTime = mktime(0, 0, 0, $month, $fromDay, $year);
  99. $endTime = mktime(23, 59, 59, $month, $fromDay, $year);
  100. list(
  101. $webTraffic, $ftpTraffic, $smtpTraffic, $popTraffic
  102. ) = _client_getDomainTraffic($domainId, $beginTime, $endTime);
  103. $tpl->assign(
  104. array(
  105. 'DATE' => date($dateFormat, strtotime($year . '-' . $month . '-' . $fromDay)),
  106. 'WEB_TRAFF' => bytesHuman($webTraffic),
  107. 'FTP_TRAFF' => bytesHuman($ftpTraffic),
  108. 'SMTP_TRAFF' => bytesHuman($smtpTraffic),
  109. 'POP_TRAFF' => bytesHuman($popTraffic),
  110. 'SUM_TRAFF' => bytesHuman($webTraffic + $ftpTraffic + $smtpTraffic + $popTraffic),
  111. )
  112. );
  113. $all[0] = $all[0] + $webTraffic;
  114. $all[1] = $all[1] + $ftpTraffic;
  115. $all[2] = $all[2] + $smtpTraffic;
  116. $all[3] = $all[3] + $popTraffic;
  117. $tpl->parse('TRAFFIC_TABLE_ITEM', '.traffic_table_item');
  118. }
  119. $tpl->assign(
  120. array(
  121. 'MONTH' => $month,
  122. 'YEAR' => $year,
  123. 'DOMAIN_ID' => $domainId,
  124. 'WEB_ALL' => bytesHuman($all[0]),
  125. 'FTP_ALL' => bytesHuman($all[1]),
  126. 'SMTP_ALL' => bytesHuman($all[2]),
  127. 'POP_ALL' => bytesHuman($all[3]),
  128. 'SUM_ALL' => bytesHuman($all[0] + $all[1] + $all[2] + $all[3]),
  129. )
  130. );
  131. } else {
  132. set_page_message(tr('No statistics found for the given period. Try another period.'), 'info');
  133. $tpl->assign('DOMAIN_STATISTICS_BLOCK', '');
  134. }
  135. }
  136. /*********************************************************************************
  137. * Main script
  138. */
  139. // Include core library
  140. require 'imscp-lib.php';
  141. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onClientScriptStart);
  142. check_login('user');
  143. /** @var $cfg iMSCP_Config_Handler_File */
  144. $cfg = iMSCP_Registry::get('config');
  145. $tpl = new iMSCP_pTemplate();
  146. $tpl->define_dynamic(
  147. array(
  148. 'layout' => 'shared/layouts/ui.tpl',
  149. 'page' => 'client/domain_statistics.tpl',
  150. 'page_message' => 'layout',
  151. 'month_list' => 'page',
  152. 'year_list' => 'page',
  153. 'domain_statistics_block' => 'page',
  154. 'traffic_table_item' => 'domain_statistics_block'
  155. )
  156. );
  157. $tpl->assign(
  158. array(
  159. 'TR_PAGE_TITLE' => tr('i-MSCP Client / Domain statistics'),
  160. 'THEME_CHARSET' => tr('encoding'),
  161. 'ISP_LOGO' => layout_getUserLogo(),
  162. 'TR_DOMAIN_STATISTICS' => tr('Domain statistics'),
  163. 'TR_MONTH' => tr('Month'),
  164. 'TR_YEAR' => tr('Year'),
  165. 'TR_SHOW' => tr('Show'),
  166. 'TR_WEB_TRAFF' => tr('Web traffic'),
  167. 'TR_FTP_TRAFF' => tr('FTP traffic'),
  168. 'TR_SMTP_TRAFF' => tr('SMTP traffic'),
  169. 'TR_POP_TRAFF' => tr('POP3/IMAP traffic'),
  170. 'TR_SUM' => tr('All traffic'),
  171. 'TR_ALL' => tr('All'),
  172. 'TR_DATE' => tr('Date')
  173. )
  174. );
  175. $domainId = get_user_domain_id($_SESSION['user_id']);
  176. if (isset($_POST['month']) && isset($_POST['year'])) {
  177. $year = intval($_POST['year']);
  178. $month = intval($_POST['month']);
  179. } else if (isset($_GET['month']) && isset($_GET['year'])) {
  180. $month = intval($_GET['month']);
  181. $year = intval($_GET['year']);
  182. } else {
  183. $month = date('m');
  184. $year = date('Y');
  185. }
  186. // Retrieve smaller timestamp to define max number of years to show in select element
  187. $stmt = exec_query(
  188. 'SELECT `dtraff_time` FROM `domain_traffic` WHERE `domain_id` = ? ORDER BY `dtraff_time` ASC LIMIT 1', $domainId
  189. );
  190. if($stmt->recordCount()) {
  191. $numberYears = date('y') - date('y', $stmt->fields['dtraff_time']);
  192. $numberYears = $numberYears == 0 ? 1 : $numberYears;
  193. } else {
  194. $numberYears = 1;
  195. }
  196. generateNavigation($tpl);
  197. generateSelectListForMonthsAndYears($tpl, $month, $year, $numberYears);
  198. client_generatePage($tpl, $domainId, $month, $year);
  199. generatePageMessage($tpl);
  200. $tpl->parse('LAYOUT_CONTENT', 'page');
  201. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onClientScriptEnd, array('templateEngine' => $tpl));
  202. $tpl->prnt();
  203. unsetMessages();