<?PHP
/**
 * Endpoint Manager Update Check File
 *
 * @author Andrew Nagy
 * @license MPL / GPLv2 / LGPL
 * @package Provisioner

error_reporting(0);

$rfh=fopen("/etc/amportal.conf","r") or Die("amportal.conf did not open.");
if ($rfh) {
	while (!feof($rfh)) {
		$line = fgets($rfh);
		if (strstr($line,"AMPDBUSER=")) {
			$temp=explode("=",$line);
			$temp[1]=trim($temp[1]);
			$ampdata['user'] = $temp[1];
		}elseif (strstr($line,"AMPDBPASS=")) {
			$temp=explode("=",$line);
			$temp[1]=trim($temp[1]);
			$ampdata['pass'] = $temp[1];
			
		}
	}
}

echo "Welcome\n";

include 'functions.inc';

global $endpoint, $amp_conf;

$amp_conf['AMPDBUSER'] = $ampdata['user'];
$amp_conf['AMPDBPASS'] = $ampdata['pass'];
$amp_conf['AMPDBNAME'] = 'asterisk';

$endpoint = new endpointmanager();

$brand_up = $endpoint->brand_update_check();

$note = "<br/> <br/>NOTE: This was generated by the Endpoint Manager Module, not by FreePBX<br/> You can turn off these updates on the settings page within the Endpoint Manager Settings Page";

$sql="SELECT * from endpointman_brand_list WHERE id > 0 AND hidden = 0 AND installed = 1 ORDER BY id ASC ";
$result=mysql_query($sql);
$i = 0;
while($row = mysql_fetch_assoc($result)) {
	$row_out[$i] = $row;
	$row_out[$i]['count'] = $i;
	$id = $endpoint->arraysearchrecursive($row['name'], $brand_up,'brand_name');
	$id = $id[0];
	if(isset($brand_up[$id]['update'])) {
		echo "Brand '".$brand_up[$id]['brand_name']."' has updates available\n";
		echo "Old Version: ".$row['cfg_ver']." New Version: ".$brand_up[$id]['version']."\n";
		echo "Changes: ". $brand_up[$id]['changes']."\n";
		$endpoint->add_freepbx_notification("PU_".$brand_up[$id]['brand_name'], '', "Brand \'".$brand_up[$id]['brand_name']."\' has updates available", "Old Version: ".$row['cfg_ver']." New Version: ".$brand_up[$id]['version']."<br/>"."Changes: ". $brand_up[$id]['changes'].$note, "");
		$email_message .= "Brand \'".$brand_up[$id]['brand_name']."\' has updates available \n Old Version: ".$row['cfg_ver']." New Version: ".$brand_up[$id]['version']."\n"."Changes: ". $brand_up[$id]['changes']."\n\n";
	}
	if($row['installed']){
		$j = 0;
		$sql = 'SELECT * FROM endpointman_product_list WHERE hidden = 0 AND brand = '.$row['id'].' ORDER BY long_name ASC';
		$result2=mysql_query($sql);
		while($row2 = mysql_fetch_assoc($result2)) {
			$row_out[$i]['products'][$j] = $row2;
			if(($temp = $endpoint->product_update_check($row2['id'])) AND ($row2['installed'] == 1)) {
				echo "Product '".$row2['long_name']."' has updates available\n";
				echo "Old Version: ".$row2['cfg_ver']." New Version: ".$temp['data']['version']."\n";
				echo "Changes: ". $temp['data']['description']."\n";
				$endpoint->add_freepbx_notification("PU_".$row2['long_name'], '', "There is a Product update for ".$row2['long_name'], "Old Version: ".$row2['cfg_ver']." New Version: ".$temp['data']['version']."<br/>"."Changes: ". $temp['data']['description'].$note, "");
				$email_message .= "There is a Product update for ".$row2['long_name']."\n"."Old Version: ".$row2['cfg_ver']." New Version: ".$temp['data']['version']."\n"."Changes: ". $temp['data']['description']."\n\n";
			}
			if(($temp = $endpoint->firmware_update_check($row2['id'])) AND ($row['firmware_vers'] > 0)) {
				echo "Product '".$row2['long_name']."' has new firmware available\n";
				echo "Old Version: ".$row2['cfg_ver']." New Version: ".$temp['data']['version']."\n";
				$endpoint->add_freepbx_notification("PU_".$row2['long_name'], '', "There is a firmware update for phone module ".$row2['long_name'], "Old Version: ".$row2['firmware_vers']." New Version: ".$temp['data']['version'].$note, "");	
				$email_message .= "There is a firmware update for phone module ".$row2['long_name']."\n Old Version: ".$row2['firmware_vers']." New Version: ".$temp['data']['version']."\n\n";
			} 
			$j++;
		}
	}
	$i++;
}

$email_message .= "\n\n\nNOTE: This was generated by the Endpoint Manager Module, not by FreePBX\n You can turn off these updates on the settings page within the Endpoint Manager Settings Page";

$sql = 'SELECT * FROM `admin` WHERE `variable` = CONVERT(_utf8 \'email\' USING latin1) COLLATE latin1_swedish_ci';
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);

$email = $row['value'];

$time = time() - 25200;

$sql = 'SELECT * FROM `notifications` WHERE `module` = CONVERT(_utf8 \'endpointman\' USING latin1) COLLATE latin1_swedish_ci';
$result=mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
	if(($row['timestamp'] > $time) AND ($row['reset'] != 1)) {

		// In case any of our lines are larger than 70 characters, we should use wordwrap()
		$message = wordwrap($email_message, 70);

		// Send
		mail($email, 'Phone Updates Available', $message);
	}
}
 * 
 */