1<?php
2
3/**
4 * This script will remove offline addresses after they have been down for
5 * predefined number of hours.
6 *
7 * Subnets with "Ping check" enabled will be used for checking offline addresses.
8 *
9 */
10
11# script can only be run from cli
12if(php_sapi_name()!="cli") 						{ die("This script can only be run from cli!"); }
13
14# include required scripts
15require_once( dirname(__FILE__) . '/../functions.php' );
16
17# initialize objects
18$Database 	= new Database_PDO;
19$Addresses	= new Addresses ($Database);
20$Subnets	= new Subnets ($Database);
21$Result		= new Result();
22
23
24// response for mailing
25$removed_addresses = array();			// Array with differences, can be used to email to admins
26
27// if config is not set die
28if(!isset($config['removed_addresses_timelimit'])) { die("Please set timelimit for address removal!"); }
29
30// set now for whole script
31$now     = time();
32$nowdate = date ("Y-m-d H:i:s");
33$beforetime = date ("Y-m-d H:i:s", (time()-$config['removed_addresses_timelimit']));
34
35// set query to fetch addresses and belongign subnets
36$query = "select
37			`ip`.`id`,`ip`.`ip_addr`,`ip`.`lastSeen`,`ip`.`subnetId`,`ip`.`description`,`ip`.`hostname`,`ip`.`lastSeen`,
38			`su`.`subnet`,`su`.`mask`,`su`.`sectionId`,`su`.`description`,
39			'delete' as `action`
40		 from
41		 	`ipaddresses` as `ip`, `subnets` as `su`
42		 where
43			`ip`.`subnetId` = `su`.`id`
44			and `su`.`pingSubnet` = 1
45			and (`ip`.`excludePing` IS NULL or `ip`.`excludePing`!=1 )
46			and
47			(`ip`.`lastSeen` < '$beforetime' and `ip`.`lastSeen` != '0000-00-00 00:00:00' and `ip`.`lastSeen` is not NULL);";
48
49# fetch
50try { $offline_addresses = $Database->getObjectsQuery($query); }
51catch (Exception $e) {
52	$Result->show("danger", _("Error: ").$e->getMessage());
53	die();
54}
55
56# if none die, none to remove
57if (!is_array($offline_addresses) || empty($offline_addresses)) {
58	die();
59}
60# remove
61else {
62	foreach ($offline_addresses as $a) {
63		// save
64		$removed_addresses[] = $a;
65		// remove
66		$Addresses->modify_address ($a);
67	}
68}
69
70
71# all done, mail diff?
72if(sizeof($removed_addresses)>0 && $config['removed_addresses_send_mail']) {
73	# settings
74	$Subnets->get_settings ();
75	# check for recipients
76	foreach($Subnets->fetch_multiple_objects ("users", "role", "Administrator") as $admin) {
77		if($admin->mailNotify=="Yes") {
78			$recepients[] = array("name"=>$admin->real_name, "email"=>$admin->email);
79		}
80	}
81	# none?
82	if(!isset($recepients))	{ die(); }
83
84	# fake user object, needed for create_link
85	$User = new StdClass();
86	# try to send
87	try {
88		# fetch mailer settings
89		$mail_settings = $Subnets->fetch_object("settingsMail", "id", 1);
90		# initialize mailer
91		$phpipam_mail = new phpipam_mail($Subnets->settings, $mail_settings);
92
93		// set subject
94		$subject	= "phpipam deleted offline addresses at ".$nowdate;
95
96		//html
97		$content[] = "<p style='margin-left:10px;'>$Subnets->mail_font_style <font style='font-size:16px;size:16px;'>phpipam removed inactive addresses at ".$nowdate."</font></font></p><br>";
98
99		$content[] = "<table style='margin-left:10px;margin-top:5px;width:auto;padding:0px;border-collapse:collapse;border:1px solid #ccc;'>";
100		$content[] = "<tr>";
101		$content[] = "	<th style='padding:3px 8px;border:1px solid #ccc;border-bottom:2px solid gray;white-space:nowrap;'>$Subnets->mail_font_style IP</font></th>";
102		$content[] = "	<th style='padding:3px 8px;border:1px solid #ccc;border-bottom:2px solid gray;'>$Subnets->mail_font_style Description</font></th>";
103		$content[] = "	<th style='padding:3px 8px;border:1px solid #ccc;border-bottom:2px solid gray;'>$Subnets->mail_font_style Hostname</font></th>";
104		$content[] = "	<th style='padding:3px 8px;border:1px solid #ccc;border-bottom:2px solid gray;'>$Subnets->mail_font_style Subnet</font></th>";
105		$content[] = "	<th style='padding:3px 8px;border:1px solid #ccc;border-bottom:2px solid gray;'>$Subnets->mail_font_style Last seen</font></th>";
106		$content[] = "</tr>";
107
108		//plain
109		$content_plain[] = "phpipam deleted offline addresses at ".$nowdate."\r\n------------------------------";
110
111		//Changes
112		foreach($removed_addresses as $change) {
113			// to array
114			$change = (array) $change;
115			//set subnet
116			$subnet = $Subnets->fetch_subnet(null, $change['subnetId']);
117
118	        // desc
119			$change['description'] = strlen($change['description'])>0 ? "$Subnets->mail_font_style $change[description]</font>" : "$Subnets->mail_font_style / </font>";
120			// subnet desc
121			$subnet->description = strlen($subnet->description)>0 ? "$Subnets->mail_font_style $subnet->description</font>" : "$Subnets->mail_font_style / </font>";
122
123			//content
124			$content[] = "<tr>";
125			$content[] = "	<td style='padding:3px 8px;border:1px solid #ccc;'>$Subnets->mail_font_style ".$Subnets->transform_to_dotted($change['ip_addr'])."</font></td>";
126			$content[] = "	<td style='padding:3px 8px;border:1px solid #ccc;'>$Subnets->mail_font_style $change[description]</font></td>";
127			$content[] = "	<td style='padding:3px 8px;border:1px solid #ccc;'>$Subnets->mail_font_style_href $change[hostname]</font></td>";
128			$content[] = "	<td style='padding:3px 8px;border:1px solid #ccc;'><a href='".rtrim(str_replace(BASE, "",$Subnets->settings->siteURL), "/")."".create_link("subnets",$subnet->sectionId,$subnet->id)."'>$Subnets->mail_font_style_href ".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask."</font></a>".$subnet->description."</td>";
129			$content[] = "	<td style='padding:3px 8px;border:1px solid #ccc;'>$Subnets->mail_font_style $change[lastSeen]</td>";
130			$content[] = "</tr>";
131
132			//plain content
133			$content_plain[] = "\t * ".$Subnets->transform_to_dotted($change['ip_addr'])." (".$Subnets->transform_to_dotted($subnet->subnet)."/".$subnet->mask.")\r\n";
134		}
135		$content[] = "</table>";
136
137		# set content
138		$content 		= $phpipam_mail->generate_message (implode("\r\n", $content));
139		$content_plain 	= implode("\r\n",$content_plain);
140
141		$phpipam_mail->Php_mailer->setFrom($mail_settings->mAdminMail, $mail_settings->mAdminName);
142		//add all admins to CC
143		foreach($recepients as $admin) {
144			$phpipam_mail->Php_mailer->addAddress(addslashes($admin['email']), addslashes($admin['name']));
145		}
146		$phpipam_mail->Php_mailer->Subject = $subject;
147		$phpipam_mail->Php_mailer->msgHTML($content);
148		$phpipam_mail->Php_mailer->AltBody = $content_plain;
149		//send
150		$phpipam_mail->Php_mailer->send();
151	} catch (phpmailerException $e) {
152		$Result->show_cli("Mailer Error: ".$e->errorMessage(), true);
153	} catch (Exception $e) {
154		$Result->show_cli("Mailer Error: ".$e->getMessage(), true);
155	}
156}
157
158?>
159