#!/usr/bin/php

<?php
// System integrity check

	$issues = array();

	$sbin = "/usr/sbin";
	test_folder($sbin);
	$www = "/usr/share/ombutel/www";
	test_folder($www);
	$patches = "/usr/share/ombutel/patches";
	test_folder($patches);
	$ringback = "/usr/share/ombutel/ringback";
	test_folder($ringback);
	$scripts = "/usr/share/ombutel/scripts";
	test_folder($scripts);
	$helper = "/usr/share/ombutel/helper";
	test_folder($helper);
	$storage = "/var/lib/ombutel/static";
	test_folder($storage, "2770");

	$distro = check_distro();
	switch($distro) {
		case 'debian': {
			$apache = "/etc/apache2/conf-available";
			$asterisk = "/usr/share/asterisk";
			$mysql = "/etc/mysql/conf.d";
			$php = "/etc/php5/conf.d";
			$systemd = "/lib/systemd/system";
			break;
		}
		case 'centos': {
			$apache = "/etc/httpd/conf.d";
			$asterisk = "/var/lib/asterisk";
			$mysql = "/etc/my.cnf.d";
			$php = "/etc/php.d";
			$systemd = "/usr/lib/systemd/system";
		}
	}

	test_folder($apache);
	test_folder($asterisk);
	test_file("{$apache}/ombutel.conf", "0644");

	$sounds_folders = [
		"{$asterisk}/sounds/en",
		"{$asterisk}/sounds/es"
	];

	foreach($sounds_folders as $folder) {
		if(test_folder($folder)) {
			$wav_sounds = glob("{$folder}/ombu-*.wav*");
			if(count($wav_sounds) <= 0) {
				$issues[] = "Warning - possibly missing sound files in {$folder}\n";
			}
		}
	}

	$agi = "{$asterisk}/agi-bin";
	test_folder($agi);
	test_folder($php);
	$conf = "/etc/ombutel";
	test_folder($conf);
	$cron_hourly = "/etc/cron.hourly";
	test_folder($cron_hourly);
	$sudoers = "/etc/sudoers.d";
	test_folder($sudoers, "0750");
	test_folder($systemd);
	test_folder($mysql);

	test_file("{$sudoers}/ombutel", "0644");
	if($distro == 'debian') {
		$pattern = '/apache/';
		if(preg_match($pattern, file_get_contents("{$sudoers}/ombutel"))) {
			$issues[] = "Warning - sudoers not set properly\n";
		}
	}
	test_file("{$sbin}/ombu-helper");
	test_file("{$mysql}/ombutel.cnf", "0644");

	$help_files = glob("{$helper}/*.php");
	if(count($help_files) < 10) { // Guess it's safe for now.
		$issues[] = "Warning - Some Helper files are probably missing. Check {$helper} folder for missing files.\n";
	}

	$web_folders = [
		'api',
		'i18n',
		'includes',
		'modules',
		'resources',
		'themes'
	];
	foreach($web_folders as $folder) {
		test_folder("{$www}/$folder");
	}

	$script_files = [
		[ 'filename' => 'api_get' ],
		[ 'filename' => 'apply_patches' ],
		[ 'filename' => 'check_disk' ],
		[ 'filename' => 'custom_recording' ],
		[ 'filename' => 'dbupdate' ],
		[ 'filename' => 'dictation' ],
		[ 'filename' => 'generate_maint_key' ],
		[ 'filename' => 'process_fax_in' ],
		[ 'filename' => 'reset_ombu_password' ],
		[ 'filename' => 'send_email.php', 'perm' => '0644' ],
		[ 'filename' => 'synchronizer' ]
	];
	foreach($script_files as $file) {
		$perm = '0755';
		if(array_key_exists('perm', $file)) {
			$perm = $file['perm'];
		}
		test_file("{$scripts}/{$file['filename']}", $perm);
	}

	test_link("{$www}/config.php", "{$conf}/ombutel.conf");
	test_link("{$www}/xepm-provision", "{$www}/includes/xepm/provision");

	print_results();

function check_distro() {
	foreach(glob('/etc/*-release') as $file) {
		$pattern = '/centos/i';
				  if(preg_match($pattern, file_get_contents($file), $match)) {
					  list($distro) = $match;
					  return strtolower($distro);
				  }
		return 'debian'; //can be one or the other for now
	}
}

function test_folder($foldername, $perm = '0755') {
	global $issues;
	if(!file_exists($foldername)) {
		$issues[] = "Error - Folder {$foldername} does not exist\n";
		return false;
	}
	test_permission($foldername, $perm);
	return true;
}

function test_link($filename, $target) {
	global $issues;
	if(!is_link($filename)) {
		$issues[] = "Error - {$filename} is not a symlink\n";
	} else {
		$path = realpath($filename);
		if($path != $target) {
			$issues[] = "Error - {$filename} is pointing to an incorrect target\n";
		}
	}
}

function test_file($filename, $perm = '0755') {
	global $issues;
	if(!file_exists($filename)) {
		$issues[] = "Error - File {$filename} does not exist\n";
	} else {
		test_permission($filename, $perm);
	}
}

function test_permission($filename, $perm) {
	global $issues;
	$perm_current = substr(sprintf('%o', fileperms($filename)), -4);
	if($perm_current != $perm) {
		$issues[] = "Warning - incorrect permissions for {$filename}. Set to {$perm_current} while it should be {$perm}\n";
		return false;
	}
	return true;
}

function print_results() {
	global $issues;
	echo "\nSystem test report:\n";
	if(count($issues) != 0) {
		echo implode($issues);
	} else {
		echo "System is OK\n";
	}
	echo "\n";
	exit;
}

?>
