quota_lib.inc.php 14.7 KB
Newer Older
1 2 3
<?php

class quota_lib {
4
	public function get_quota_data($clientid = null, $readable = true) {
5 6 7 8 9 10 11 12 13 14 15
		global $app; 
		
		$tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'harddisk_quota' ORDER BY created DESC");
		$monitor_data = array();
		if(is_array($tmp_rec)) {
			foreach ($tmp_rec as $tmp_mon) {
				$monitor_data = array_merge_recursive($monitor_data, unserialize($app->db->unquote($tmp_mon['data'])));
			}
		}
		//print_r($monitor_data);
		
16
		// select all websites or websites belonging to client
17
		$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND type = 'vhost'".(($clientid != null)?" AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)":'') . " ORDER BY domain", $clientid);
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
		
		//print_r($sites);
		if(is_array($sites) && !empty($sites)){
			for($i=0;$i<sizeof($sites);$i++){
				$username = $sites[$i]['system_user'];
				$sites[$i]['used'] = $monitor_data['user'][$username]['used'];
				$sites[$i]['soft'] = $monitor_data['user'][$username]['soft'];
				$sites[$i]['hard'] = $monitor_data['user'][$username]['hard'];
				$sites[$i]['files'] = $monitor_data['user'][$username]['files'];
		
				if (!is_numeric($sites[$i]['used'])){
					if ($sites[$i]['used'][0] > $sites[$i]['used'][1]){
						$sites[$i]['used'] = $sites[$i]['used'][0];
					} else {
						$sites[$i]['used'] = $sites[$i]['used'][1];
					}
				}
				if (!is_numeric($sites[$i]['soft'])) $sites[$i]['soft']=$sites[$i]['soft'][1];
				if (!is_numeric($sites[$i]['hard'])) $sites[$i]['hard']=$sites[$i]['hard'][1];
				if (!is_numeric($sites[$i]['files'])) $sites[$i]['files']=$sites[$i]['files'][1];
Marius Cramer's avatar
Marius Cramer committed
38 39 40 41 42 43 44
				
				$sites[$i]['used_raw'] = $sites[$i]['used'];
				$sites[$i]['soft_raw'] = $sites[$i]['soft'];
				$sites[$i]['hard_raw'] = $sites[$i]['hard'];
				$sites[$i]['files_raw'] = $sites[$i]['files'];
				$sites[$i]['used_percentage'] = ($sites[$i]['soft'] > 0 && $sites[$i]['used'] > 0 ? round($sites[$i]['used'] * 100 / $sites[$i]['soft']) : 0);
				
45 46 47 48 49 50 51 52 53 54 55 56
				if ($readable) {
					// colours
					$sites[$i]['display_colour'] = '#000000';
					if($sites[$i]['soft'] > 0){
						$used_ratio = $sites[$i]['used']/$sites[$i]['soft'];
					} else {
						$used_ratio = 0;
					}
					if($used_ratio >= 0.8) $sites[$i]['display_colour'] = '#fd934f';
					if($used_ratio >= 1) $sites[$i]['display_colour'] = '#cc0000';
			
					if($sites[$i]['used'] > 1024) {
Helmo's avatar
Helmo committed
57
						$sites[$i]['used'] = round($sites[$i]['used'] / 1024, 1).' MB';
58 59 60 61 62
					} else {
						if ($sites[$i]['used'] != '') $sites[$i]['used'] .= ' KB';
					}
			
					if($sites[$i]['soft'] > 1024) {
Helmo's avatar
Helmo committed
63
						$sites[$i]['soft'] = round($sites[$i]['soft'] / 1024, 1).' MB';
64 65 66 67 68
					} else {
						$sites[$i]['soft'] .= ' KB';
					}
			
					if($sites[$i]['hard'] > 1024) {
Helmo's avatar
Helmo committed
69
						$sites[$i]['hard'] = round($sites[$i]['hard'] / 1024, 1).' MB';
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
					} else {
						$sites[$i]['hard'] .= ' KB';
					}
			
					if($sites[$i]['soft'] == " KB") $sites[$i]['soft'] = $app->lng('unlimited');
					if($sites[$i]['hard'] == " KB") $sites[$i]['hard'] = $app->lng('unlimited');
					
					if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = $app->lng('unlimited');
					if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = $app->lng('unlimited');
					
					/*
					 if(!strstr($sites[$i]['used'],'M') && !strstr($sites[$i]['used'],'K')) $sites[$i]['used'].= ' B';
					if(!strstr($sites[$i]['soft'],'M') && !strstr($sites[$i]['soft'],'K')) $sites[$i]['soft'].= ' B';
					if(!strstr($sites[$i]['hard'],'M') && !strstr($sites[$i]['hard'],'K')) $sites[$i]['hard'].= ' B';
					*/
				}
				else {
					if (empty($sites[$i]['soft'])) $sites[$i]['soft'] = -1;
					if (empty($sites[$i]['hard'])) $sites[$i]['hard'] = -1;
					
					if($sites[$i]['soft'] == '0 B' || $sites[$i]['soft'] == '0 KB' || $sites[$i]['soft'] == '0') $sites[$i]['soft'] = -1;
					if($sites[$i]['hard'] == '0 B' || $sites[$i]['hard'] == '0 KB' || $sites[$i]['hard'] == '0') $sites[$i]['hard'] = -1;
				}
			}
		}
		
		return $sites;
	}
98 99 100 101 102 103 104 105
	
	public function get_trafficquota_data($clientid = null, $lastdays = 0) {
		global $app;
	
		$traffic_data = array();
	
		// select vhosts (belonging to client)
		if($clientid != null){
106
			$sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)";
107
		}
108
		$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
109 110 111 112 113 114 115 116 117 118 119 120 121 122
	
		$hostnames = array();
		$traffic_data = array();
	
		foreach ($sites as $site) {
			$hostnames[] = $site['domain'];
			$traffic_data[$site['domain']]['domain_id'] = $site['domain_id'];
		}
	
		// fetch all traffic-data of selected vhosts
		if (!empty($hostnames)) {
			$tmp_year = date('Y');
			$tmp_month = date('m');
			// This Month
123
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
124
			foreach ($tmp_recs as $tmp_rec) {
Dominik's avatar
Dominik committed
125
				$traffic_data[$tmp_rec['hostname']]['this_month'] = $tmp_rec['t'];
126 127
			}
			// This Year
128
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
129 130 131 132 133 134 135
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['this_year'] = $tmp_rec['t'];
			}
				
			$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
			$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
			// Last Month
136
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
137 138 139 140 141 142
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['last_month'] = $tmp_rec['t'];
			}
				
			$tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
			// Last Year
143
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
144 145 146 147 148 149
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
			}
				
			if (is_int($lastdays)  && ($lastdays > 0)) {
				// Last xx Days
150
				$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(traffic_bytes) as t FROM web_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
151 152 153 154 155 156 157 158
				foreach ($tmp_recs as $tmp_rec) {
					$traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
				}
			}
		}
	
		return $traffic_data;
	}
Cédric's avatar
Cédric committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

	public function get_ftptrafficquota_data($clientid = null, $lastdays = 0) {
		global $app;
	
		$traffic_data = array();
	
		// select vhosts (belonging to client)
		if($clientid != null){
			$sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)";
		}
		$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
	
		$hostnames = array();
		$traffic_data = array();
	
		foreach ($sites as $site) {
			$hostnames[] = $site['domain'];
			$traffic_data[$site['domain']]['domain_id'] = $site['domain_id'];
		}
	
		// fetch all traffic-data of selected vhosts
		if (!empty($hostnames)) {
			$tmp_year = date('Y');
			$tmp_month = date('m');
			// This Month
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['this_month'] = $tmp_rec['t'];
			}
			// This Year
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['this_year'] = $tmp_rec['t'];
			}
				
			$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
			$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
			// Last Month
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['last_month'] = $tmp_rec['t'];
			}
				
			$tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
			// Last Year
			$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
			foreach ($tmp_recs as $tmp_rec) {
				$traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
			}
				
			if (is_int($lastdays)  && ($lastdays > 0)) {
				// Last xx Days
				$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
				foreach ($tmp_recs as $tmp_rec) {
					$traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
				}
			}
		}
	
		return $traffic_data;
	}
220
	
221
	public function get_mailquota_data($clientid = null, $readable = true) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
		global $app;
		
		$tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'email_quota' ORDER BY created DESC");
		$monitor_data = array();
		if(is_array($tmp_rec)) {
			foreach ($tmp_rec as $tmp_mon) {
				//$monitor_data = array_merge_recursive($monitor_data,unserialize($app->db->unquote($tmp_mon['data'])));
				$tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
				if(is_array($tmp_array)) {
					foreach($tmp_array as $username => $data) {
						if(!$monitor_data[$username]['used']) $monitor_data[$username]['used'] = $data['used'];
					}
				}
			}
		}
		//print_r($monitor_data);
		
239
		// select all email accounts or email accounts belonging to client
240
		$emails = $app->db->queryAllRecords("SELECT * FROM mail_user".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY email", $clientid);
241 242 243 244 245
		
		//print_r($emails);
		if(is_array($emails) && !empty($emails)){
			for($i=0;$i<sizeof($emails);$i++){
				$email = $emails[$i]['email'];
246 247
				
				$emails[$i]['name'] = $app->functions->htmlentities($emails[$i]['name']);
248 249 250
				$emails[$i]['used'] = isset($monitor_data[$email]['used']) ? $monitor_data[$email]['used'] : array(1 => 0);
		
				if (!is_numeric($emails[$i]['used'])) $emails[$i]['used']=$emails[$i]['used'][1];
Dominik's avatar
Dominik committed
251
				
Marius Cramer's avatar
Marius Cramer committed
252 253 254 255 256
				$emails[$i]['quota_raw'] = $emails[$i]['quota'];
				$emails[$i]['used_raw'] = $emails[$i]['used'];
				$emails[$i]['used_percentage'] = ($emails[$i]['quota'] > 0 && $emails[$i]['used'] > 0 ? round($emails[$i]['used'] * 100 / $emails[$i]['quota']) : 0);

				
Dominik's avatar
Dominik committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
				if ($readable) {
					// colours
					$emails[$i]['display_colour'] = '#000000';
					if($emails[$i]['quota'] > 0){
						$used_ratio = $emails[$i]['used']/$emails[$i]['quota'];
					} else {
						$used_ratio = 0;
					}
					if($used_ratio >= 0.8) $emails[$i]['display_colour'] = '#fd934f';
					if($used_ratio >= 1) $emails[$i]['display_colour'] = '#cc0000';
			
					if($emails[$i]['quota'] == 0){
						$emails[$i]['quota'] = $app->lng('unlimited');
					} else {
Helmo's avatar
Helmo committed
271
                                               $emails[$i]['quota'] = round($emails[$i]['quota'] / 1048576, 1).' MB';
Dominik's avatar
Dominik committed
272 273 274 275
					}
			
			
					if($emails[$i]['used'] < 1544000) {
Helmo's avatar
Helmo committed
276
                                               $emails[$i]['used'] = round($emails[$i]['used'] / 1024, 1).' KB';
Dominik's avatar
Dominik committed
277
					} else {
Helmo's avatar
Helmo committed
278
                                               $emails[$i]['used'] = round($emails[$i]['used'] / 1048576, 1).' MB';
Dominik's avatar
Dominik committed
279
					}
280 281 282 283 284 285
				}
			}
		}
		
		return $emails;
	}
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	
	public function get_databasequota_data($clientid = null, $readable = true) {
		global $app;
	
		$tmp_rec =  $app->db->queryAllRecords("SELECT data from monitor_data WHERE type = 'database_size' ORDER BY created DESC");
		$monitor_data = array();
		if(is_array($tmp_rec)) {
			foreach ($tmp_rec as $tmp_mon) {
				$tmp_array = unserialize($app->db->unquote($tmp_mon['data']));
				if(is_array($tmp_array)) {
					foreach($tmp_array as $key => $data) {
						if(!isset($monitor_data[$data['database_name']]['size'])) $monitor_data[$data['database_name']]['size'] = $data['size'];
					}
				}
			}
		}
		//print_r($monitor_data);
	
		// select all databases belonging to client
305
		$databases = $app->db->queryAllRecords("SELECT * FROM web_database".(($clientid != null)? " WHERE sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)" : '') . " ORDER BY database_name", $clientid);
306 307 308 309 310 311 312 313 314
	
		//print_r($databases);
		if(is_array($databases) && !empty($databases)){
			for($i=0;$i<sizeof($databases);$i++){
				$databasename = $databases[$i]['database_name'];
	
				$databases[$i]['used'] = isset($monitor_data[$databasename]['size']) ? $monitor_data[$databasename]['size'] : 0;
	
				$databases[$i]['quota_raw'] = $databases[$i]['database_quota'];
Florian Schaal's avatar
Florian Schaal committed
315
				$databases[$i]['used_raw'] = $databases[$i]['used'] / 1024 / 1024; //* quota is stored as MB - calculated bytes
316
				$databases[$i]['used_percentage'] = (($databases[$i]['database_quota'] > 0) && ($databases[$i]['used'] > 0)) ? round($databases[$i]['used_raw'] * 100 / $databases[$i]['database_quota']) : 0;
317 318 319 320 321
	
				if ($readable) {
					// colours
					$databases[$i]['display_colour'] = '#000000';
					if($databases[$i]['database_quota'] > 0){
322
						$used_ratio = $databases[$i]['used'] / $databases[$i]['database_quota'];
323 324 325 326 327 328 329 330 331
					} else {
						$used_ratio = 0;
					}
					if($used_ratio >= 0.8) $databases[$i]['display_colour'] = '#fd934f';
					if($used_ratio >= 1) $databases[$i]['display_colour'] = '#cc0000';
						
					if($databases[$i]['database_quota'] == 0){
						$databases[$i]['database_quota'] = $app->lng('unlimited');
					} else {
Florian Schaal's avatar
Florian Schaal committed
332
						$databases[$i]['database_quota'] = $databases[$i]['database_quota'] . ' MB';
333 334 335 336
					}
						
						
					if($databases[$i]['used'] < 1544000) {
Helmo's avatar
Helmo committed
337
						$databases[$i]['used'] = round($databases[$i]['used'] / 1024, 1).' KB';
338
					} else {
Helmo's avatar
Helmo committed
339
						$databases[$i]['used'] = round($databases[$i]['used'] / 1048576, 1).' MB';
340 341 342 343 344 345 346 347
					}
				}
			}
		}
	
		return $databases;
	}
	
Florian Schaal's avatar
Florian Schaal committed
348
}