Explorar el Código

added generate config files ini and inc

Piotr Labudda hace 8 años
padre
commit
4392b33ead
Se han modificado 2 ficheros con 82 adiciones y 0 borrados
  1. 78 0
      SE/se-lib/Config.php
  2. 4 0
      SE/se-lib/V.php

+ 78 - 0
SE/se-lib/Config.php

@@ -95,6 +95,83 @@ class Config {
 		return self::getConfFile("column_init_{$col}");
 	}
 
+	public static function generateDefaultDbConfigFile($params = [], $outputFormat = 'ini') {
+		if (empty($params['database'])) throw new Exception("Missing database name");
+		if (empty($params['password'])) throw new Exception("Missing password");
+		if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
+
+		if ('inc' == $outputFormat) {
+			return implode("\n", [
+				'<?php',
+				'return function ($secret = "") {',
+				'	if ("secret-p5-password" !== $secret) return null;',
+				'	$conf = [];',
+				'	$conf["type"] = "mysql";',
+				'	$conf["host"] = "127.0.0.1";',
+				'	$conf["port"] = "3306";',
+				'	$conf["user"] = "root";',
+				'	$conf["pass"] = "' . $params['password'] . '";',
+				'	$conf["database"] = "' . $params['database'] . '";',
+				'	$conf["zasob_id"] = "2";',
+				'	return $conf;',
+				'};',
+			]);
+		}
+
+	  return implode("\n", [
+			';<?php',
+			';die(); // For further security',
+			';// default DB',
+			'',
+			'type="mysql"',
+			'host="127.0.0.1"',
+			'port="3306"',
+			'user="root"',
+			'pass="' . $params['password'] . '"',
+			'database="' . $params['database'] . '"',
+			'zasob_id="2"',
+			'',
+		]);
+	}
+
+	public static function generateDefaultLdapConfigFile($params = [], $outputFormat = 'ini') {
+		if (empty($params['user'])) throw new Exception("Missing user");
+		if (empty($params['pass'])) throw new Exception("Missing pass");
+		if (empty($params['base_dn'])) throw new Exception("Missing base_dn");
+		if (!in_array($outputFormat, ['ini', 'inc'])) throw new Exception("Not implemented outputFormat = '{$outputFormat}'");
+
+		if ('inc' == $outputFormat) {
+			return implode("\n", [
+				'<?php',
+				'return function ($secret = "") {',
+				'	if ("secret-p5-password" !== $secret) return null;',
+				'	$conf = [];',
+				'	$conf["version"] = "3";',
+				'	$conf["host"] = "127.0.0.1";',
+				'	// $conf["port"] = "3306";',
+				'	$conf["user"] = "' . $params['user'] . '";',
+				'	$conf["pass"] = "' . $params['pass'] . '";',
+				'	$conf["base_dn"] = "' . $params['base_dn'] . '";',
+				'	return $conf;',
+				'};',
+			]);
+		}
+
+	  return implode("\n", [
+			';<?php',
+			';die(); // For further security',
+			';// default LDAP',
+			'',
+			'version="3"',
+			'host="127.0.0.1"',
+			';//port="3306"',
+			'user="' . $params['user'] . '"',
+			'pass="' . $params['pass'] . '"',
+			'base_dn="' . $params['base_dn'] . '"',
+			'',
+		]);
+	}
+
 	/**
 	 * Search for config ini file.
 	 * TODO: $conf_file == '' - main config file
@@ -145,6 +222,7 @@ class Config {
 						$fun = include $f;
 						if (!is_callable($fun)) throw new Exception("Config func is not callable");
 						$_cnf[$conf_file] = $fun('secret-p5-password');
+						if (null === $_cnf[$conf_file]) throw new Exception("Config error");
 						break;
 					}
 				}

+ 4 - 0
SE/se-lib/V.php

@@ -447,6 +447,10 @@ EOF';
 		return $ret;
 	}
 
+	public static function quoteBashEcho($string) {
+		return str_replace('"', '\"', $string);
+	}
+
 	public static function cloneArray($arr) {
 		return $arr;
 	}