#!/usr/bin/env php
<?php
$options = getopt("", array("fix_zend"));
/* Optional. It’s better to do it in the php.ini file */
date_default_timezone_set('America/Los_Angeles');

//Bootstrap to give access to FreePBX internals
$bootstrap_settings['freepbx_auth'] = false;
$bootstrap_settings['fix_zend'] = isset($options['fix_zend']);
//Wrapped in a global try catch incase of zend errors
try {
	include_once '/etc/freepbx.conf';
} catch(\Exception $e) {
	if(!isset($options['fix_zend']) && function_exists('SPLAutoloadBroken') && SPLAutoloadBroken()) {
		modgettext::push_textdomain("amp");
		echo _("Autoloader is damaged")."\n";
		$name = isset($argv[0]) ? basename($argv[0]) : "fwconsole";
		echo sprintf(_("Please run: %s --fix_zend"),$name)."\n";
		exit(5);
	} else {
		 throw $e;
	}
}

if(!isset($options['fix_zend']) && function_exists('SPLAutoloadBroken') && SPLAutoloadBroken()) {
	echo _("Autoloader is damaged")."\n";
	echo sprintf(_("Please run: %s --fix_zend"),basename($argv[0]))."\n";
	die();
}
if(isset($options['fix_zend'])) {
	if(SPLAutoloadBroken()) {
		die(_("Autoloader was damaged before we even started!")."\n");
	}
	//autoload our fix it classes now before autoloading breaks
	$mf = module_functions::create();
	error_reporting(0);
	$disable = null;
	if(!empty($zended) && is_array($zended)) {
		foreach($zended as $key => $file) {
			if(file_exists($file)) {
				bootstrap_include_hooks('pre_module_load', $key);
				require_once($file);
				bootstrap_include_hooks('post_module_load', $key);
				if(SPLAutoloadBroken()) {
					$disable = $key;
					break;
				}
			}
		}
	}
	if(!empty($disable)) {
		echo sprintf(_("Disabling malfunctioning module: %s"),$disable)."\n";
		$mf->disable($disable, true);
		exec(__FILE__." --fix_zend",$o);
		foreach($o as $line) {
			echo $line."\n";
		}
	}
	if(!SPLAutoloadBroken()) {
		die(_("There's nothing left to fix")."\n");
		exit(0);
	}
}

$webrootpath = (isset($amp_conf['AMPWEBROOT']) && !empty($amp_conf['AMPWEBROOT'])) ?  $amp_conf['AMPWEBROOT'] : '/var/www/html';
include $webrootpath .'/admin/libraries/FWApplication.class.php';
include $webrootpath .'/admin/libraries/FWHelper.class.php';
include $webrootpath .'/admin/libraries/FWList.class.php';

use Symfony\Component\Console\Application;
use FreePBX\Console\Command;
use FreePBX\Console\Application as App;

try {
	$amodules = FreePBX::Modules()->getActiveModules();
	$fbc = new App\FWApplication('FW Console - FreePBX Utility', getVersion());
	/*
	* Framework is not like the others so we load up framework classes first.
	*/
	$fwcpath = $webrootpath . '/admin/libraries/Console';
	$fwclasses = scandir($fwcpath);
	foreach ($fwclasses as $class){
		if( substr($class , -9) !== 'class.php'){
			continue;
		}else{
			$ifile = $fwcpath . '/' . $class;
			if(file_exists($ifile)) {
				include $ifile;
				$classname = substr($class ,0,-10);
				$class = 'FreePBX\\Console\\Command\\' . $classname;
				$reflector = new ReflectionClass($class);
				$props = $reflector->getDefaultProperties();
				if(isset($props['requireroot']) && $props['requireroot']){
					if(posix_getuid() != 0) {
						continue;
					}
				}
				$fbc->add(new $class);
			}
		}
	}

	/*
	* Dynamic Class Loader. This looks to enabled modules for a Console folder
	* Then loads any file.class.php files found in that subdir.
	* This doesn't really handle bad includes. If your include in bad
	* It will likely break everything!
	*/
	foreach($amodules as $module){
		//Module Path
		$mpath = $webrootpath . '/admin/modules/' . $module['rawname'] . '/Console/';
		if (file_exists($mpath)){
			//Class files
			$cfiles = scandir($mpath);
			foreach($cfiles as $class){
				//ignore anything in this dir that is NOT class.php
				if( substr($class , -9) !== 'class.php'){
					continue;
				}else{
					$ifile = $mpath . '/' . $class;
					if(!file_exists($ifile)) {
						continue;
					}
					include $ifile;
					$classname = substr($class ,0,-10);
					$class = 'FreePBX\\Console\\Command\\' . $classname;
					if(!class_exists($class)) {
						continue;
					}
					modgettext::push_textdomain($module['rawname']);
					$fbc->add(new $class);
					modgettext::pop_textdomain();
				}
			}
		}
	}
	$fbc->run();
} catch(Exception $e) {
	echo $e->getMessage()."\n";
}
