#!/usr/bin/php
<?php
/**
 * script: set_proxy
 *
 * Copyright 2005-2024, Lime Technology
 *
 * Call this script (/usr/local/sbin/set_proxy.php) to set up proxy files.
 */

$opmPlugin	= "dynamix";
require_once("plugins/".$opmPlugin."/include/OutgoingProxyLib.php");

/* Files generated by this script based on the data in proxy.cfg. */
$proxy_sh	= '/etc/profile.d/proxy.sh';
$proxy_ini	= '/usr/local/emhttp/state/proxy.ini';

/* Random file extension for atomic writes. */
$rnd		= rand();

/* Comments to beginning of the proxy_sh file. */
$comments	= "#!/bin/bash\n"."# Do not edit. This file is autogenerated by /usr/local/emhttp/plugins/dynamix/scripts/set_proxy.\n";

/* Set verbose if command line switch is set. */
$verbose = false;
if (isset($argv[1]) && ($argv[1] == '-v')) {
	$verbose = true;
}

/* Initialize global variables. */
$proxy_active	= "";
$proxy_url		= "";
$no_proxy		= "";

/* Write comments and content to a file. */
function write_file($file, $content, $show_comments = false) {
	global $rnd, $comments;

	$tmpFile = "{$file}.{$rnd}";
	file_put_contents($tmpFile, ($show_comments ? $comments : "").$content);
	chmod($tmpFile, 0755);
	rename($tmpFile, $file);
}

/* Write environment variables to /etc/profile.d/proxy.sh. */
function set_proxy_sh() {
	global $proxy_sh, $proxy_url, $no_proxy;

	$content	 = "export http_proxy=".$proxy_url."\n";
	$content	.= "export https_proxy=".$proxy_url."\n";
	$content	.= "export no_proxy=".$no_proxy."\n";

	write_file($proxy_sh, $content, true);
}

/* Clear environment variables from /etc/profile.d/proxy.sh. */
function unset_proxy_sh() {
	global $proxy_sh;

	$content	 = "unset http_proxy\n";
	$content	.= "unset https_proxy\n";
	$content	.= "unset no_proxy\n";

	write_file($proxy_sh, $content, true);
}

/* Write proxy information to /usr/local/emhttp/state/proxy.ini. */
function set_proxy_ini() {
	global $proxy_ini, $proxy_url, $no_proxy;

	$content	 = "http_proxy=".$proxy_url."\n";
	$content	.= "https_proxy=".$proxy_url."\n";
	$content	.= "no_proxy=".$no_proxy."\n";

	write_file($proxy_ini, $content);
}

/* Clear /usr/local/emhttp/state/proxy.ini. */
function unset_proxy_ini() {
	/* Vars are empty now, call set_proxy_ini to add empty vars to proxy.ini. */
	set_proxy_ini();
}

/* Generate proxy files. */
function add_proxy_to_generated_files() {
	global $verbose;

	echo("generating proxy files\n");
	set_proxy_sh();
	set_proxy_ini();
	if ($verbose) {
		display_generated_files();
	}
}

/* Remove proxy info from all generated files. */
function remove_proxy_from_generated_files() {
	global $verbose;

	echo("removing proxy info from generated files\n");
	unset_proxy_sh();
	unset_proxy_ini();
	if ($verbose) {
		display_generated_files();
	}
}

/* When verbose mode is enabled. */
function display_generated_files() {
	global $proxy_sh, $proxy_ini;

	echo("\n");
	display_generated_file($proxy_sh);
	echo("\n");
	display_generated_file($proxy_ini);
}

/* When verbose mode is enabled. */
function display_generated_file($file) {
	echo("file: ".$file."\n");
	if (file_exists($file)) {
		echo(file_get_contents($file));
	} else {
		echo("file does not exist\n");
	}
}

/* Read current proxy information from Outgoing Proxy Manager config file. */
$config = parse_plugin_config();

/* Get the current active proxy. */
$proxy_active	= $config['proxy_active'] ?? "0";
$proxy_url		= "";

/* Only build full url for an active proxy. */
if ($proxy_active != "0") {
	/* Get the active proxy url, user, and pass. */
	$proxy_url		= "proxy_url_".$proxy_active;
	$proxy_user		= "proxy_user_".$proxy_active;
	$proxy_pass		= "proxy_pass_".$proxy_active;

	/* Get the full url for the active proxy. */
	$url_array		= get_proxy_info($config[$proxy_url] ?? "", $config[$proxy_user] ?? "", $config[$proxy_pass] ?? "");
	$proxy_url		= $proxy_active ? $url_array['full_url'] : "";

	/* If the active proxy url is defined, create the generated files. */
	if ($proxy_url) {
		/* Proxies are defined, write generated files. */
		$no_proxy	= "127.0.0.1,localhost";
		add_proxy_to_generated_files();
	}
} else {
	/* If no active proxies, remove proxy info from all generated files. */
	remove_proxy_from_generated_files();
}
?>
