Init
This commit is contained in:
62
backend/resources/library/main/authentification.php
Normal file
62
backend/resources/library/main/authentification.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
if (!defined('PICONTROL')) exit();
|
||||
|
||||
(include_once realpath(dirname(__FILE__)).'/../../init.php') or die('Error: 0x0010');
|
||||
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0011');
|
||||
|
||||
$logout = true;
|
||||
|
||||
if (isset($_COOKIE['_pi-control_login']) || isset($_POST['token']))
|
||||
{
|
||||
$uniqid = isset($_COOKIE['_pi-control_login']) ? $_COOKIE['_pi-control_login'] : $_POST['token'];
|
||||
$tokenCreated = getConfig('login:token_'.$uniqid.'.created', 0);
|
||||
$tokenRememberMe = getConfig('login:token_'.$uniqid.'.remember_me', 'false');
|
||||
$tokenUsername = getConfig('login:token_'.$uniqid.'.username', '');
|
||||
$tokenLastLogin = getConfig('user:user_'.$tokenUsername.'.last_login', 0);
|
||||
|
||||
if ($tokenCreated == 0 || ($tokenCreated < time()-60*60*12 && $tokenRememberMe != 'true'))
|
||||
{
|
||||
removeConfig('login:token_'.$uniqid);
|
||||
|
||||
if (isset($_COOKIE['_pi-control_login']))
|
||||
setcookie('_pi-control_login', '', time()-60);
|
||||
}
|
||||
elseif ($tokenLastLogin < time()-60*60)
|
||||
{
|
||||
setConfig('user:user_'.$tokenUsername.'.last_login', time());
|
||||
$logout = false;
|
||||
}
|
||||
else
|
||||
$logout = false;
|
||||
}
|
||||
|
||||
if (isset($_POST['token']) && $logout === true)
|
||||
{
|
||||
if (isset($authentificationMsg))
|
||||
die($authentificationMsg);
|
||||
else
|
||||
{
|
||||
header('HTTP/1.0 403 Forbidden');
|
||||
exit();
|
||||
}
|
||||
}
|
||||
elseif ((!isset($_COOKIE['_pi-control_login']) || $logout === true) && !isset($_POST['token']))
|
||||
{
|
||||
if (isset($authentificationMsg))
|
||||
die($authentificationMsg);
|
||||
else
|
||||
{
|
||||
$referer = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '';
|
||||
|
||||
if ($referer != '')
|
||||
$referer = '&referer='.urlencode($referer);
|
||||
|
||||
$dir = '';
|
||||
for ($i = 0; $i < substr_count(str_replace(realpath(dirname(__FILE__).'/../../../').'/', '', realpath(dirname($_SERVER["SCRIPT_FILENAME"])).'/'), '/'); $i += 1)
|
||||
$dir .= '../';
|
||||
|
||||
header('Location: '.$dir.'?i=login'.$referer);
|
||||
exit();
|
||||
}
|
||||
}
|
||||
?>
|
||||
1533
backend/resources/library/main/main.function.php
Normal file
1533
backend/resources/library/main/main.function.php
Normal file
File diff suppressed because it is too large
Load Diff
320
backend/resources/library/main/password.function.php
Normal file
320
backend/resources/library/main/password.function.php
Normal file
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
/**
|
||||
* A Compatibility library with PHP 5.5's simplified password hashing API.
|
||||
*
|
||||
* @author Anthony Ferrara <ircmaxell@php.net>
|
||||
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
||||
* @copyright 2012 The Authors
|
||||
*/
|
||||
|
||||
namespace {
|
||||
|
||||
if (!defined('PICONTROL')) exit(); // Ausnahme, da namespace
|
||||
|
||||
if (!defined('PASSWORD_BCRYPT')) {
|
||||
/**
|
||||
* PHPUnit Process isolation caches constants, but not function declarations.
|
||||
* So we need to check if the constants are defined separately from
|
||||
* the functions to enable supporting process isolation in userland
|
||||
* code.
|
||||
*/
|
||||
define('PASSWORD_BCRYPT', 1);
|
||||
define('PASSWORD_DEFAULT', PASSWORD_BCRYPT);
|
||||
define('PASSWORD_BCRYPT_DEFAULT_COST', 10);
|
||||
}
|
||||
|
||||
if (!function_exists('password_hash')) {
|
||||
|
||||
/**
|
||||
* Hash the password using the specified algorithm
|
||||
*
|
||||
* @param string $password The password to hash
|
||||
* @param int $algo The algorithm to use (Defined by PASSWORD_* constants)
|
||||
* @param array $options The options for the algorithm to use
|
||||
*
|
||||
* @return string|false The hashed password, or false on error.
|
||||
*/
|
||||
function password_hash($password, $algo, array $options = array()) {
|
||||
if (!function_exists('crypt')) {
|
||||
trigger_error("Crypt must be loaded for password_hash to function", E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
if (is_null($password) || is_int($password)) {
|
||||
$password = (string) $password;
|
||||
}
|
||||
if (!is_string($password)) {
|
||||
trigger_error("password_hash(): Password must be a string", E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
if (!is_int($algo)) {
|
||||
trigger_error("password_hash() expects parameter 2 to be long, " . gettype($algo) . " given", E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
$resultLength = 0;
|
||||
switch ($algo) {
|
||||
case PASSWORD_BCRYPT:
|
||||
$cost = PASSWORD_BCRYPT_DEFAULT_COST;
|
||||
if (isset($options['cost'])) {
|
||||
$cost = (int) $options['cost'];
|
||||
if ($cost < 4 || $cost > 31) {
|
||||
trigger_error(sprintf("password_hash(): Invalid bcrypt cost parameter specified: %d", $cost), E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// The length of salt to generate
|
||||
$raw_salt_len = 16;
|
||||
// The length required in the final serialization
|
||||
$required_salt_len = 22;
|
||||
$hash_format = sprintf("$2y$%02d$", $cost);
|
||||
// The expected length of the final crypt() output
|
||||
$resultLength = 60;
|
||||
break;
|
||||
default:
|
||||
trigger_error(sprintf("password_hash(): Unknown password hashing algorithm: %s", $algo), E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
$salt_req_encoding = false;
|
||||
if (isset($options['salt'])) {
|
||||
switch (gettype($options['salt'])) {
|
||||
case 'NULL':
|
||||
case 'boolean':
|
||||
case 'integer':
|
||||
case 'double':
|
||||
case 'string':
|
||||
$salt = (string) $options['salt'];
|
||||
break;
|
||||
case 'object':
|
||||
if (method_exists($options['salt'], '__tostring')) {
|
||||
$salt = (string) $options['salt'];
|
||||
break;
|
||||
}
|
||||
case 'array':
|
||||
case 'resource':
|
||||
default:
|
||||
trigger_error('password_hash(): Non-string salt parameter supplied', E_USER_WARNING);
|
||||
return null;
|
||||
}
|
||||
if (PasswordCompat\binary\_strlen($salt) < $required_salt_len) {
|
||||
trigger_error(sprintf("password_hash(): Provided salt is too short: %d expecting %d", PasswordCompat\binary\_strlen($salt), $required_salt_len), E_USER_WARNING);
|
||||
return null;
|
||||
} elseif (0 == preg_match('#^[a-zA-Z0-9./]+$#D', $salt)) {
|
||||
$salt_req_encoding = true;
|
||||
}
|
||||
} else {
|
||||
$buffer = '';
|
||||
$buffer_valid = false;
|
||||
if (function_exists('mcrypt_create_iv') && !defined('PHALANGER')) {
|
||||
$buffer = mcrypt_create_iv($raw_salt_len, MCRYPT_DEV_URANDOM);
|
||||
if ($buffer) {
|
||||
$buffer_valid = true;
|
||||
}
|
||||
}
|
||||
if (!$buffer_valid && function_exists('openssl_random_pseudo_bytes')) {
|
||||
$strong = false;
|
||||
$buffer = openssl_random_pseudo_bytes($raw_salt_len, $strong);
|
||||
if ($buffer && $strong) {
|
||||
$buffer_valid = true;
|
||||
}
|
||||
}
|
||||
if (!$buffer_valid && @is_readable('/dev/urandom')) {
|
||||
$file = fopen('/dev/urandom', 'r');
|
||||
$read = 0;
|
||||
$local_buffer = '';
|
||||
while ($read < $raw_salt_len) {
|
||||
$local_buffer .= fread($file, $raw_salt_len - $read);
|
||||
$read = PasswordCompat\binary\_strlen($local_buffer);
|
||||
}
|
||||
fclose($file);
|
||||
if ($read >= $raw_salt_len) {
|
||||
$buffer_valid = true;
|
||||
}
|
||||
$buffer = str_pad($buffer, $raw_salt_len, "\0") ^ str_pad($local_buffer, $raw_salt_len, "\0");
|
||||
}
|
||||
if (!$buffer_valid || PasswordCompat\binary\_strlen($buffer) < $raw_salt_len) {
|
||||
$buffer_length = PasswordCompat\binary\_strlen($buffer);
|
||||
for ($i = 0; $i < $raw_salt_len; $i++) {
|
||||
if ($i < $buffer_length) {
|
||||
$buffer[$i] = $buffer[$i] ^ chr(mt_rand(0, 255));
|
||||
} else {
|
||||
$buffer .= chr(mt_rand(0, 255));
|
||||
}
|
||||
}
|
||||
}
|
||||
$salt = $buffer;
|
||||
$salt_req_encoding = true;
|
||||
}
|
||||
if ($salt_req_encoding) {
|
||||
// encode string with the Base64 variant used by crypt
|
||||
$base64_digits =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
$bcrypt64_digits =
|
||||
'./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
|
||||
$base64_string = base64_encode($salt);
|
||||
$salt = strtr(rtrim($base64_string, '='), $base64_digits, $bcrypt64_digits);
|
||||
}
|
||||
$salt = PasswordCompat\binary\_substr($salt, 0, $required_salt_len);
|
||||
|
||||
$hash = $hash_format . $salt;
|
||||
|
||||
$ret = crypt($password, $hash);
|
||||
|
||||
if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != $resultLength) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get information about the password hash. Returns an array of the information
|
||||
* that was used to generate the password hash.
|
||||
*
|
||||
* array(
|
||||
* 'algo' => 1,
|
||||
* 'algoName' => 'bcrypt',
|
||||
* 'options' => array(
|
||||
* 'cost' => PASSWORD_BCRYPT_DEFAULT_COST,
|
||||
* ),
|
||||
* )
|
||||
*
|
||||
* @param string $hash The password hash to extract info from
|
||||
*
|
||||
* @return array The array of information about the hash.
|
||||
*/
|
||||
function password_get_info($hash) {
|
||||
$return = array(
|
||||
'algo' => 0,
|
||||
'algoName' => 'unknown',
|
||||
'options' => array(),
|
||||
);
|
||||
if (PasswordCompat\binary\_substr($hash, 0, 4) == '$2y$' && PasswordCompat\binary\_strlen($hash) == 60) {
|
||||
$return['algo'] = PASSWORD_BCRYPT;
|
||||
$return['algoName'] = 'bcrypt';
|
||||
list($cost) = sscanf($hash, "$2y$%d$");
|
||||
$return['options']['cost'] = $cost;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the password hash needs to be rehashed according to the options provided
|
||||
*
|
||||
* If the answer is true, after validating the password using password_verify, rehash it.
|
||||
*
|
||||
* @param string $hash The hash to test
|
||||
* @param int $algo The algorithm used for new password hashes
|
||||
* @param array $options The options array passed to password_hash
|
||||
*
|
||||
* @return boolean True if the password needs to be rehashed.
|
||||
*/
|
||||
function password_needs_rehash($hash, $algo, array $options = array()) {
|
||||
$info = password_get_info($hash);
|
||||
if ($info['algo'] !== (int) $algo) {
|
||||
return true;
|
||||
}
|
||||
switch ($algo) {
|
||||
case PASSWORD_BCRYPT:
|
||||
$cost = isset($options['cost']) ? (int) $options['cost'] : PASSWORD_BCRYPT_DEFAULT_COST;
|
||||
if ($cost !== $info['options']['cost']) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify a password against a hash using a timing attack resistant approach
|
||||
*
|
||||
* @param string $password The password to verify
|
||||
* @param string $hash The hash to verify against
|
||||
*
|
||||
* @return boolean If the password matches the hash
|
||||
*/
|
||||
function password_verify($password, $hash) {
|
||||
if (!function_exists('crypt')) {
|
||||
trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING);
|
||||
return false;
|
||||
}
|
||||
$ret = crypt($password, $hash);
|
||||
if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != PasswordCompat\binary\_strlen($hash) || PasswordCompat\binary\_strlen($ret) <= 13) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$status = 0;
|
||||
for ($i = 0; $i < PasswordCompat\binary\_strlen($ret); $i++) {
|
||||
$status |= (ord($ret[$i]) ^ ord($hash[$i]));
|
||||
}
|
||||
|
||||
return $status === 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace PasswordCompat\binary {
|
||||
|
||||
if (!function_exists('PasswordCompat\\binary\\_strlen')) {
|
||||
|
||||
/**
|
||||
* Count the number of bytes in a string
|
||||
*
|
||||
* We cannot simply use strlen() for this, because it might be overwritten by the mbstring extension.
|
||||
* In this case, strlen() will count the number of *characters* based on the internal encoding. A
|
||||
* sequence of bytes might be regarded as a single multibyte character.
|
||||
*
|
||||
* @param string $binary_string The input string
|
||||
*
|
||||
* @internal
|
||||
* @return int The number of bytes
|
||||
*/
|
||||
function _strlen($binary_string) {
|
||||
if (function_exists('mb_strlen')) {
|
||||
return mb_strlen($binary_string, '8bit');
|
||||
}
|
||||
return strlen($binary_string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a substring based on byte limits
|
||||
*
|
||||
* @see _strlen()
|
||||
*
|
||||
* @param string $binary_string The input string
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
*
|
||||
* @internal
|
||||
* @return string The substring
|
||||
*/
|
||||
function _substr($binary_string, $start, $length) {
|
||||
if (function_exists('mb_substr')) {
|
||||
return mb_substr($binary_string, $start, $length, '8bit');
|
||||
}
|
||||
return substr($binary_string, $start, $length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current PHP version is compatible with the library
|
||||
*
|
||||
* @return boolean the check result
|
||||
*/
|
||||
function check() {
|
||||
static $pass = NULL;
|
||||
|
||||
if (is_null($pass)) {
|
||||
if (function_exists('crypt')) {
|
||||
$hash = '$2y$04$usesomesillystringfore7hnbRJHxXVLeakoG8K30oukPsA.ztMG';
|
||||
$test = crypt("password", $hash);
|
||||
$pass = $test == $hash;
|
||||
} else {
|
||||
$pass = false;
|
||||
}
|
||||
}
|
||||
return $pass;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
?>
|
||||
433
backend/resources/library/main/rpi.function.php
Normal file
433
backend/resources/library/main/rpi.function.php
Normal file
@@ -0,0 +1,433 @@
|
||||
<?php
|
||||
if (!defined('PICONTROL')) exit();
|
||||
|
||||
function rpi_getRuntime()
|
||||
{
|
||||
$runtime = trim(@shell_exec('cat /proc/uptime | awk -F \'.\' \'{print $1}\''));
|
||||
return $runtime;
|
||||
}
|
||||
|
||||
function rpi_getHostname()
|
||||
{
|
||||
$host = trim(@shell_exec('cat /proc/sys/kernel/hostname'));
|
||||
return $host;
|
||||
}
|
||||
|
||||
function rpi_getHostAddr()
|
||||
{
|
||||
if (!isset($_SERVER['SERVER_ADDR']))
|
||||
return 'unknown';
|
||||
|
||||
if (!$ip = $_SERVER['SERVER_ADDR'])
|
||||
return gethostbyname($this->getHostname());
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
function rpi_getCoreTemprature()
|
||||
{
|
||||
$file = 85000;
|
||||
|
||||
while ($file == 85000)
|
||||
$file = @shell_exec('cat /sys/class/thermal/thermal_zone0/temp');
|
||||
|
||||
if ($file != false)
|
||||
return round((trim($file) / 1000), 2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function rpi_getCpuClock()
|
||||
{
|
||||
$file = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq');
|
||||
|
||||
if ($file !== false)
|
||||
return (int) round(trim($file) / 1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function rpi_getCpuMinClock()
|
||||
{
|
||||
$file = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq');
|
||||
|
||||
if ($file !== false)
|
||||
return (int) round(trim($file) / 1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function rpi_getCpuMaxClock()
|
||||
{
|
||||
$file = shell_exec('cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq');
|
||||
|
||||
if ($file !== false)
|
||||
return (int) round(trim($file) / 1000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function rpi_getCPUType()
|
||||
{
|
||||
$file = shell_exec('cat /proc/cpuinfo');
|
||||
preg_match('#Hardware\s*:\s*([^\s]+)#i', $file, $match);
|
||||
|
||||
if (isset($match[1]))
|
||||
return $match[1];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
function rpi_getCpuModel()
|
||||
{
|
||||
$model = trim(@shell_exec('cat /proc/cpuinfo | grep -m 1 "Model" | cut -d ":" -f 2'));
|
||||
return $model;
|
||||
}
|
||||
|
||||
function rpi_getCPULoad($accurate = false, $mulitcore = false)
|
||||
{
|
||||
$return = NULL;
|
||||
|
||||
if ($accurate === true)
|
||||
$file = shell_exec('cat /proc/stat; sleep 2; echo "##--##"; cat /proc/stat');
|
||||
else
|
||||
$file = shell_exec('cat /proc/stat; sleep 0.5; echo "##--##"; cat /proc/stat');
|
||||
|
||||
$file = explode('##--##', $file);
|
||||
|
||||
if (!isset($file[0], $file[1]))
|
||||
return NULL;
|
||||
|
||||
preg_match_all('/^cpu[0-9]?(.*)$/im', $file[0], $prevCPUStrings);
|
||||
preg_match_all('/^cpu[0-9]?(.*)$/im', $file[1], $curCPUStrings);
|
||||
|
||||
for ($i = 0; $i < count($prevCPUStrings[0]); $i++) {
|
||||
$prevCPU = preg_split('/\s+/', $prevCPUStrings[0][$i]);
|
||||
$curCPU = preg_split('/\s+/', $curCPUStrings[0][$i]);
|
||||
|
||||
if ($prevCPU[0] != 'cpu' && $mulitcore == false)
|
||||
break;
|
||||
|
||||
|
||||
if (!isset($prevCPU[0], $curCPU[1]) || count($prevCPU) != 11 || count($curCPU) != 11)
|
||||
return NULL;
|
||||
|
||||
$prevIdle = $prevCPU[4] + $prevCPU[5];
|
||||
$curIdle = $curCPU[4] + $curCPU[5];
|
||||
|
||||
$prevNonIdle = $prevCPU[1] + $prevCPU[2] + $prevCPU[3] + $prevCPU[6] + $prevCPU[7] + $prevCPU[8];
|
||||
$curNonIdle = $curCPU[1] + $curCPU[2] + $curCPU[3] + $curCPU[6] + $curCPU[7] + $curCPU[8];
|
||||
|
||||
$prevTotal = $prevIdle + $prevNonIdle;
|
||||
$curTotal = $curIdle + $curNonIdle;
|
||||
|
||||
$total = $curTotal - $prevTotal;
|
||||
$idle = $curIdle - $prevIdle;
|
||||
|
||||
if ($mulitcore == true)
|
||||
$return[$prevCPU[0]] = (int) round(($total - $idle) / $total * 100);
|
||||
else
|
||||
$return = (int) round(($total - $idle) / $total * 100);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
function rpi_getDistribution()
|
||||
{
|
||||
$distribution = trim(@shell_exec('cat /etc/issue | cut -d " " -f 1-3'));
|
||||
|
||||
if ($distribution == '') {
|
||||
$distributionString = @shell_exec('cat /etc/*-release | grep PRETTY_NAME');
|
||||
|
||||
preg_match('/.*="([\w\s\d\/]*).*"/i', $distributionString, $match);
|
||||
|
||||
if (count($match) == 2)
|
||||
$distribution = trim($match[1]);
|
||||
else
|
||||
$distribution = _t('Unbekannt');
|
||||
}
|
||||
|
||||
return $distribution;
|
||||
}
|
||||
|
||||
function rpi_getKernelVersion()
|
||||
{
|
||||
$kernel = trim(@shell_exec('cat /proc/version | cut -d " " -f 1,3'));
|
||||
return $kernel;
|
||||
}
|
||||
|
||||
function rpi_getCountRunningTasks()
|
||||
{
|
||||
$tasks = trim(@shell_exec('ps -auxeaf| wc -l'));
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
function rpi_getCountInstalledPackages()
|
||||
{
|
||||
$packages = trim(@shell_exec('dpkg --get-selections | grep -v deinstall | wc -l'));
|
||||
return $packages;
|
||||
}
|
||||
|
||||
function rpi_getInstalledPackages()
|
||||
{
|
||||
@exec('dpkg -l | grep ^ii', $packages);
|
||||
|
||||
$packages = array_map(function ($package) {
|
||||
return preg_split('/[\s]+/', $package, 5);
|
||||
}, $packages);
|
||||
return $packages;
|
||||
}
|
||||
|
||||
function rpi_getRpiRevision()
|
||||
{
|
||||
$revision = array();
|
||||
$revision[2] = array('revision' => '0002', 'model' => 'B', 'pcb' => '1.0', 'memory' => 256, 'manufacturer' => '');
|
||||
$revision[3] = array('revision' => '0003', 'model' => 'B', 'pcb' => '1.0', 'memory' => 256, 'manufacturer' => '');
|
||||
$revision[4] = array('revision' => '0004', 'model' => 'B', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Sony');
|
||||
$revision[5] = array('revision' => '0005', 'model' => 'B', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Qisda');
|
||||
$revision[6] = array('revision' => '0006', 'model' => 'B', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Egoman');
|
||||
$revision[7] = array('revision' => '0007', 'model' => 'A', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Egoman');
|
||||
$revision[8] = array('revision' => '0008', 'model' => 'A', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Sony');
|
||||
$revision[9] = array('revision' => '0009', 'model' => 'A', 'pcb' => '2.0', 'memory' => 256, 'manufacturer' => 'Qisda');
|
||||
$revision[13] = array('revision' => '000d', 'model' => 'B', 'pcb' => '2.0', 'memory' => 512, 'manufacturer' => 'Egoman');
|
||||
$revision[14] = array('revision' => '000e', 'model' => 'B', 'pcb' => '2.0', 'memory' => 512, 'manufacturer' => 'Sony');
|
||||
$revision[15] = array('revision' => '000f', 'model' => 'B', 'pcb' => '2.0', 'memory' => 512, 'manufacturer' => 'Qisda');
|
||||
$revision[16] = array('revision' => '0010', 'model' => 'B+', 'pcb' => '1.0', 'memory' => 512, 'manufacturer' => 'Sony');
|
||||
$revision[17] = array('revision' => '0011', 'model' => 'Compute Module', 'pcb' => '1.0', 'memory' => 512, 'manufacturer' => 'Sony');
|
||||
$revision[18] = array('revision' => '0012', 'model' => 'A+', 'pcb' => '1.0', 'memory' => 256, 'manufacturer' => 'Sony');
|
||||
$revision[19] = array('revision' => '0013', 'model' => 'B+', 'pcb' => '1.2', 'memory' => 512, 'manufacturer' => 'Embest');
|
||||
$revision[20] = array('revision' => '0014', 'model' => 'Compute Module', 'pcb' => '1.0', 'memory' => 512, 'manufacturer' => 'Embest');
|
||||
$revision[21] = array('revision' => '0015', 'model' => 'A+', 'pcb' => '1.1', 'memory' => 256, 'manufacturer' => 'Embest');
|
||||
|
||||
$revision_model = array(0 => 'A', 1 => 'B', 2 => 'A+', 3 => 'B+', 4 => 'Pi 2 B', 5 => 'Alpha', 6 => 'Compute Module', 7 => 'Zero', 8 => 'Pi 3 B', 9 => 'Zero', 10 => 'Compute Module 3', 12 => 'Zero W');
|
||||
$revision_memory = array(0 => 256, 1 => 512, 2 => 1024);
|
||||
$revision_manufacturer = array(0 => 'Sony', 1 => 'Egoman', 2 => 'Embest', 3 => 'Sony Japan', 4 => 'Embest');
|
||||
|
||||
$file = shell_exec('cat /proc/cpuinfo');
|
||||
preg_match('#\nRevision\s*:\s*([\da-f]+)#i', $file, $match);
|
||||
|
||||
/*
|
||||
* ######
|
||||
* |||||+- PCB
|
||||
* ||||+- Model
|
||||
* |||+- Model
|
||||
* ||+- Unknown
|
||||
* |+- Manufacturer
|
||||
* +- Memory
|
||||
*/
|
||||
|
||||
if (isset($match[1])) {
|
||||
if ($match[1][0] == '1' || $match[1][0] == '2')
|
||||
$match[1] = substr($match[1], 1);
|
||||
|
||||
if (strlen($match[1]) == 4)
|
||||
return $revision[hexdec($match[1])];
|
||||
elseif (strlen($match[1]) == 6 && $match[1][0] != 'a' && $match[1][0] != '9')
|
||||
return $revision[hexdec(substr($match[1], -4))];
|
||||
elseif (strlen($match[1]) == 6) {
|
||||
return array(
|
||||
'revision' => $match[1],
|
||||
'model' => $revision_model[hexdec(substr($match[1], 3, 2))],
|
||||
'pcb' => '1.' . hexdec(substr($match[1], -1)),
|
||||
'memory' => $revision_memory[bindec(substr(decbin(hexdec(substr($match[1], 0, 1))), 1))],
|
||||
'manufacturer' => $revision_manufacturer[hexdec(substr($match[1], 1, 1))]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
function rpi_getRpiSerial()
|
||||
{
|
||||
$file = shell_exec('cat /proc/cpuinfo');
|
||||
preg_match('#\nSerial\s*:\s*([\da-f]+)#i', $file, $match);
|
||||
|
||||
return $match[1];
|
||||
}
|
||||
|
||||
function rpi_getMemorySplit()
|
||||
{
|
||||
$rev = $this->getRpiRevision();
|
||||
if ($rev >= 7) {
|
||||
// 512 MB
|
||||
$config = @shell_exec('cat /boot/config.txt');
|
||||
preg_match('#gpu_mem=([0-9]+)#i', $config, $match);
|
||||
$total = intval($match[1]);
|
||||
|
||||
if ($total == 16)
|
||||
return array('system' => '1991 MiB', 'video' => '16 MiB');
|
||||
elseif ($total == 32)
|
||||
return array('system' => '480 MiB', 'video' => '32 MiB');
|
||||
elseif ($total == 64)
|
||||
return array('system' => '448 MiB', 'video' => '64 MiB');
|
||||
elseif ($total == 128)
|
||||
return array('system' => '384 MiB', 'video' => '128 MiB');
|
||||
|
||||
return array('system' => '256 MiB', 'video' => '256 MiB');
|
||||
}
|
||||
|
||||
// 256 MB
|
||||
$mem = $this->getMemoryUsage();
|
||||
$total = round($mem['total'] / 1024 / 1024, 0);
|
||||
|
||||
if ($total <= 128)
|
||||
return array('system' => '128 MiB', 'video' => '128 MiB');
|
||||
elseif ($total > 128 && $total <= 192)
|
||||
return array('system' => '192 MiB', 'video' => '64 MiB');
|
||||
elseif ($total > 192 && $total <= 224)
|
||||
return array('system' => '224 MiB', 'video' => '32 MiB');
|
||||
|
||||
return array('system' => '1991 MiB', 'video' => '16 MiB');
|
||||
}
|
||||
|
||||
function rpi_getMemoryUsage()
|
||||
{
|
||||
exec('free -b', $data);
|
||||
list($type, $total, $used, $free, $shared, $buffers, $available) = preg_split('#\s+#', $data[1]);
|
||||
$usage = (int) round(($total - $available) / $total * 100);
|
||||
|
||||
return array('percent' => $usage, 'total' => $total, 'free' => $available, 'used' => ($total - $available));
|
||||
}
|
||||
|
||||
function rpi_getSwapUsage()
|
||||
{
|
||||
exec('free -b', $data);
|
||||
list($type, $total, $used, $free) = preg_split('#\s+#', $data[2]);
|
||||
$usage = (int) round($used / $total * 100);
|
||||
|
||||
return array('percent' => $usage, 'total' => $total, 'free' => $free, 'used' => $used);
|
||||
}
|
||||
|
||||
function multiArraySearch($array, $key, $value)
|
||||
{
|
||||
foreach ($array as $item)
|
||||
if (isset($item[$key]) && $item[$key] == $value)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function rpi_getMemoryInfo()
|
||||
{
|
||||
exec('df -lT | grep -vE "tmpfs|rootfs|Filesystem|Dateisystem"', $data);
|
||||
|
||||
$devices = array();
|
||||
$totalSize = 0;
|
||||
$usedSize = 0;
|
||||
|
||||
foreach ($data as $row) {
|
||||
list($device, $type, $blocks, $use, $available, $used, $mountpoint) = preg_split('#[\s%]+#i', $row);
|
||||
|
||||
if (multiArraySearch($devices, 'device', $device) === false) {
|
||||
$totalSize += $blocks * 1024;
|
||||
$usedSize += $use * 1024;
|
||||
}
|
||||
|
||||
$devices[] = array(
|
||||
'device' => $device,
|
||||
'type' => $type,
|
||||
'total' => $blocks * 1024,
|
||||
'used' => $use * 1024,
|
||||
'free' => $available * 1024,
|
||||
'percent' => (int) round(($use * 100 / $blocks)),
|
||||
'mountpoint' => $mountpoint
|
||||
);
|
||||
}
|
||||
|
||||
usort($devices, function ($a, $b) {
|
||||
return strcasecmp($a['device'], $b['device']);
|
||||
});
|
||||
|
||||
$devices[] = array('total' => $totalSize, 'used' => $usedSize, 'free' => $totalSize - $usedSize, 'percent' => (int) round(($usedSize * 100 / $totalSize)));
|
||||
|
||||
return $devices;
|
||||
}
|
||||
|
||||
function rpi_getUsbDevices()
|
||||
{
|
||||
exec('lsusb', $data);
|
||||
$devices = array();
|
||||
|
||||
foreach ($data as $row) {
|
||||
preg_match('#[0-9a-f]{4}:[0-9a-f]{4}\s+(.+)#i', $row, $match);
|
||||
|
||||
if (count($match) == 2)
|
||||
$devices[] = trim($match[1]);
|
||||
else
|
||||
$devices[] = '<' . _t('Unbekannt') . '>';
|
||||
}
|
||||
|
||||
return $devices;
|
||||
}
|
||||
|
||||
function rpi_getAllUsers()
|
||||
{
|
||||
function array_orderby()
|
||||
{
|
||||
$args = func_get_args();
|
||||
$data = array_shift($args);
|
||||
foreach ($args as $n => $field) {
|
||||
if (is_string($field)) {
|
||||
$tmp = array();
|
||||
foreach ($data as $key => $row)
|
||||
$tmp[$key] = $row[$field];
|
||||
$args[$n] = $tmp;
|
||||
}
|
||||
}
|
||||
$args[] = &$data;
|
||||
call_user_func_array('array_multisort', $args);
|
||||
return array_pop($args);
|
||||
}
|
||||
|
||||
exec('/usr/bin/who --ips', $dataLoggedIn);
|
||||
exec('/usr/bin/lastlog | grep -vE "Benutzername|Username" | cut -f 1 -d " "', $dataAllUsers);
|
||||
|
||||
$usersLoggedIn = array();
|
||||
$usersAll = array();
|
||||
|
||||
foreach ($dataLoggedIn as $row) {
|
||||
$split = preg_split('/\s+/i', $row);
|
||||
|
||||
if (count($split) == 6)
|
||||
$usersLoggedIn[$split[0]][] = array('port' => $split[1], 'lastLogin' => strtotime($split[2] . ' ' . $split[3] . ' ' . $split[4]), 'lastLoginAddress' => $split[5]);
|
||||
}
|
||||
|
||||
foreach ($dataAllUsers as $row) {
|
||||
$userLastLoginInformation = '';
|
||||
$userLastLoginInformation = shell_exec('/usr/bin/last -i -f /var/log/wtmp | grep -m 1 "^' . $row . ' "');
|
||||
|
||||
if ($userLastLoginInformation == '')
|
||||
$userLastLoginInformation = shell_exec('/usr/bin/last -i -f /var/log/wtmp.1 | grep -m 1 "^' . $row . ' "');
|
||||
|
||||
if ($userLastLoginInformation != '') {
|
||||
$split = preg_split('/\s+/i', $userLastLoginInformation);
|
||||
|
||||
$usersAll[] = array(
|
||||
'username' => $row,
|
||||
'userId' => exec('id -u ' . escapeshellarg($row)),
|
||||
'groupId' => exec('id -g ' . escapeshellarg($row)),
|
||||
'port' => $split[1],
|
||||
'lastLoginAddress' => $split[2],
|
||||
'lastLogin' => strtotime($split[4] . ' ' . $split[5] . ' ' . $split[6]),
|
||||
'isLoggedIn' => isset($usersLoggedIn[$row]) ? true : false,
|
||||
'loggedIn' => isset($usersLoggedIn[$row]) ? $usersLoggedIn[$row] : array()
|
||||
);
|
||||
} else {
|
||||
$usersAll[] = array(
|
||||
'username' => $row,
|
||||
'userId' => exec('id -u ' . escapeshellarg($row)),
|
||||
'groupId' => exec('id -g ' . escapeshellarg($row)),
|
||||
'port' => '',
|
||||
'lastLoginAddress' => '',
|
||||
'lastLogin' => 0,
|
||||
'isLoggedIn' => isset($usersLoggedIn[$row]) ? true : false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$usersAll = array_orderby($usersAll, 'isLoggedIn', SORT_DESC, 'username', SORT_ASC);
|
||||
|
||||
return $usersAll;
|
||||
}
|
||||
30
backend/resources/library/main/sites.php
Normal file
30
backend/resources/library/main/sites.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
if (!defined('PICONTROL')) exit();
|
||||
|
||||
$site = array(
|
||||
'overview' => 'overview.php',
|
||||
'detailed_overview' => 'detailed_overview.php',
|
||||
'installed_packages' => 'installed_packages.php',
|
||||
'network' => 'network.php',
|
||||
'network_connect' => 'network_connect.php',
|
||||
'network_configuration' => 'network_configuration.php',
|
||||
'statistic' => 'statistic.php',
|
||||
'plugins' => 'plugins.php',
|
||||
'discover_plugins' => 'discover_plugins.php',
|
||||
'settings' => 'settings.php',
|
||||
'ssh_login' => 'ssh_login.php',
|
||||
'terminal' => 'terminal.php',
|
||||
'shutdown' => 'shutdown.php',
|
||||
'users_groups' => 'users_groups.php',
|
||||
'logs' => 'logs.php',
|
||||
'processes' => 'processes.php'
|
||||
);
|
||||
|
||||
$include = array(
|
||||
'login' => 'resources/content/login.php',
|
||||
'update' => 'resources/update/update_picontrol.php',
|
||||
'download_plugin' => 'resources/plugins/download_plugin.php',
|
||||
'update_plugin' => 'resources/plugins/update_plugin.php',
|
||||
'feedback' => LIBRARY_PATH.'feedback/feedback.php'
|
||||
);
|
||||
?>
|
||||
971
backend/resources/library/main/tpl.class.php
Normal file
971
backend/resources/library/main/tpl.class.php
Normal file
@@ -0,0 +1,971 @@
|
||||
<?php
|
||||
if (!defined('PICONTROL')) exit();
|
||||
|
||||
/**
|
||||
* Beschreibung. Mehr unter: http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.pkg.html
|
||||
*/
|
||||
|
||||
class FileException extends Exception { }
|
||||
|
||||
class PiTpl
|
||||
{
|
||||
// Sprache
|
||||
private $tplLanguage = 'de';
|
||||
private $tplLanguageFileArray = array();
|
||||
|
||||
// Pfade
|
||||
private $tplFolderPath = 'public_html/templates/';
|
||||
private $tplFileSuffix = '.tpl.php';
|
||||
private $tplConfigs = array('main', 'cron');
|
||||
private $tplConfigSuffix = '.config.ini.php';
|
||||
private $tplLanguagePath = 'resources/languages/';
|
||||
private $tplFolderPathPlugin = '';
|
||||
|
||||
// Laufzeit
|
||||
private $runtimeStart = 0;
|
||||
|
||||
// Template Variablen
|
||||
private $tplVariables = array();
|
||||
|
||||
// Geladene Elemente
|
||||
private $tplLoaded = false;
|
||||
private $tplLoadedHeader = false;
|
||||
private $tplLoadedFooter = false;
|
||||
|
||||
// Lade Elemente?
|
||||
private $tplLoadHeader = false;
|
||||
private $tplLoadFooter = false;
|
||||
|
||||
// Fehler / Nachrichten
|
||||
private $ifError = false;
|
||||
private $tplMsg = array();
|
||||
|
||||
private $tpl = NULL;
|
||||
private $tplConfigArray = array();
|
||||
public $tplDraw = false;
|
||||
|
||||
// Headertitel
|
||||
private $tplHeaderTitle = '';
|
||||
private $tplHeaderTitleFormat = '%s | Pi Control';
|
||||
|
||||
// Footer
|
||||
private $tplFooterConfig = array();
|
||||
|
||||
// SSH
|
||||
private $tplSSH = NULL;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
global $globalLanguage, $globalLanguageArray;
|
||||
|
||||
$this->runtimeStart = microtime(true);
|
||||
|
||||
$lang = $globalLanguage;
|
||||
$langFile = LANGUAGE_PATH.$lang.'.php';
|
||||
|
||||
if (empty($globalLanguageArray) && file_exists($langFile) === true && is_file($langFile) === true)
|
||||
{
|
||||
include $langFile;
|
||||
$globalLanguageArray = $langArray;
|
||||
}
|
||||
|
||||
foreach ($this->tplConfigs as $configFile)
|
||||
{
|
||||
if (file_exists(CONFIG_PATH.$configFile.$this->tplConfigSuffix) === true && is_file(CONFIG_PATH.$configFile.$this->tplConfigSuffix) === true)
|
||||
$this->tplConfigArray[$configFile] = parse_ini_file(CONFIG_PATH.$configFile.$this->tplConfigSuffix, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Übergibt der Klasse die eigene Klassenvariable.
|
||||
*
|
||||
* <code>$tpl->setTpl($tpl);</code>
|
||||
*
|
||||
* @param class $tpl Klasse
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setTpl($tpl)
|
||||
{
|
||||
$this->tpl = $tpl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt Sprache.
|
||||
*
|
||||
* <code>$tpl->setLanguage('en'); // Für Englisch.</code>
|
||||
*
|
||||
* @param string $lang Sprache
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setLanguage($lang = 'de')
|
||||
{
|
||||
global $globalLanguage;
|
||||
|
||||
if (strlen($lang) != 2 || !is_string($lang))
|
||||
return false;
|
||||
|
||||
$this->tplLanguage = $lang;
|
||||
$globalLanguage = $lang;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt Text in andere Sprache.
|
||||
*
|
||||
* <code>$tpl->_t('Hallo %s!', 'Welt'); // Rückgabe: Hallo Welt!</code>
|
||||
*
|
||||
* @param string $text Text
|
||||
* @param string|int|float $args[] Argumente
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function _t()
|
||||
{
|
||||
$args = func_get_args();
|
||||
|
||||
$checksum = substr(md5($args[0]), 0, 8);
|
||||
if (isset($this->tplLanguageFileArray[$checksum]) && $this->tplLanguage != 'de')
|
||||
$args[0] = $this->tplLanguageFileArray[$checksum];
|
||||
|
||||
return call_user_func_array('sprintf', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt Text in andere Sprache und gibt ihn anschließend aus.
|
||||
*
|
||||
* <code>$tpl->_e('Hallo %s!', 'Welt'); // Ausgabe: Hallo Welt!</code>
|
||||
*
|
||||
* @param string $text Text
|
||||
* @param string|int|float $args[] Argumente
|
||||
* @return bool Ausgabe erfolgt mit "echo".
|
||||
*/
|
||||
|
||||
public function _e()
|
||||
{
|
||||
$args = func_get_args();
|
||||
|
||||
$checksum = substr(md5($args[0]), 0, 8);
|
||||
if (isset($this->tplLanguageFileArray[$checksum]) && $this->tplLanguage != 'de')
|
||||
$args[0] = $this->tplLanguageFileArray[$checksum];
|
||||
|
||||
echo call_user_func_array('sprintf', $args);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt Konfigurationswert für config.ini.php.
|
||||
*
|
||||
* <code>$tpl->setConfig('other.test', 'Wert'); // Weißt der Konfigvariable ['other']['test'] den Wert "Wert" zu.</code>
|
||||
*
|
||||
* @param string $config Konfigschlüssel
|
||||
* @param string $value Konfigwert
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setConfig($config, $value, $customFile = NULL)
|
||||
{
|
||||
$configPath = CONFIG_PATH;
|
||||
|
||||
if ($this->tplFolderPathPlugin != '')
|
||||
$configPath = $this->tplFolderPathPlugin.'/resources/config/';
|
||||
|
||||
if ($customFile !== NULL)
|
||||
$configPath = $customFile;
|
||||
|
||||
$file = explode(':', $config);
|
||||
|
||||
if (count($file) != 2)
|
||||
return false;
|
||||
|
||||
$configFile = $configPath.$file[0].$this->tplConfigSuffix;
|
||||
$md5 = substr(md5($configFile), 0, 6);
|
||||
|
||||
if (!isset($this->tplConfigArray[$md5]))
|
||||
{
|
||||
if (file_exists($configFile) === true && is_file($configFile) === true)
|
||||
$this->tplConfigArray[$md5] = parse_ini_file($configFile, true);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!strlen($config) > 0 || !is_string($config))
|
||||
return false;
|
||||
|
||||
$var = explode('.', $file[1]);
|
||||
|
||||
if (count($var) != 2)
|
||||
return false;
|
||||
|
||||
$this->tplConfigArray[$md5][$var[0]][$var[1]] = $value;
|
||||
|
||||
return writeConfig($this->tplConfigArray[$md5], $configFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt Konfigurationswert aus config.ini.php.
|
||||
*
|
||||
* <code>$tpl->getConfig('other.test', 'Wert'); // Ermittelt den Wert von Konfigvariable ['other']['test']. Standardwert: "Wert".</code>
|
||||
*
|
||||
* @param string $config Konfigschlüssel
|
||||
* @param string $default Standardwert
|
||||
* @return string|int Im Fehlerfall der Standardwert, ansonsten den Konfigwert.
|
||||
*/
|
||||
|
||||
public function getConfig($config, $default = NULL, $customFile = NULL)
|
||||
{
|
||||
$configPath = CONFIG_PATH;
|
||||
|
||||
if ($this->tplFolderPathPlugin != '')
|
||||
$configPath = $this->tplFolderPathPlugin.'/resources/config/';
|
||||
|
||||
if ($customFile !== NULL)
|
||||
$configPath = $customFile;
|
||||
|
||||
$file = explode(':', $config);
|
||||
|
||||
if (count($file) != 2)
|
||||
return $default;
|
||||
|
||||
$configFile = $configPath.$file[0].$this->tplConfigSuffix;
|
||||
$md5 = substr(md5($configFile), 0, 6);
|
||||
|
||||
if (!isset($this->tplConfigArray[$md5]))
|
||||
{
|
||||
if (file_exists($configFile) === true && is_file($configFile) === true)
|
||||
$this->tplConfigArray[$md5] = parse_ini_file($configFile, true);
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!strlen($config) > 0 || !is_string($config))
|
||||
return $default;
|
||||
|
||||
if (!count($this->tplConfigArray) > 0)
|
||||
return $default;
|
||||
|
||||
$var = explode('.', $file[1]);
|
||||
|
||||
if (count($var) == 1 && isset($this->tplConfigArray[$md5][$var[0]]))
|
||||
return $this->tplConfigArray[$md5][$var[0]];
|
||||
elseif (count($var) == 2 && isset($this->tplConfigArray[$md5][$var[0]][$var[1]]))
|
||||
return $this->tplConfigArray[$md5][$var[0]][$var[1]];
|
||||
else
|
||||
return $default;
|
||||
}
|
||||
|
||||
public function removeConfig($config)
|
||||
{
|
||||
if (!strlen($config) > 0 || !is_string($config))
|
||||
return false;
|
||||
|
||||
$file = explode(':', $config);
|
||||
|
||||
if (count($file) != 2)
|
||||
return false;
|
||||
|
||||
$var = explode('.', $file[1]);
|
||||
|
||||
if (count($var) == 1)
|
||||
unset($this->tplConfigArray[$file[0]][$var[0]]);
|
||||
elseif (count($var) == 2)
|
||||
unset($this->tplConfigArray[$file[0]][$var[0]][$var[1]]);
|
||||
else
|
||||
return false;
|
||||
|
||||
return writeConfig($this->tplConfigArray[$file[0]], CONFIG_PATH.$file[0].$this->tplConfigSuffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Übergibt dem TPL-System eine Variable
|
||||
*
|
||||
* <code>$tpl->assign('test', 'Wert'); // "test" ist der Name der Variable, "Wert" der Wert.</code>
|
||||
*
|
||||
* @param string $name Name der Variable.
|
||||
* @param string $value Wert der Variable.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function assign($name, $value = NULL, $merge = false)
|
||||
{
|
||||
if (!strlen($name) > 0 || !is_string($name))
|
||||
return false;
|
||||
|
||||
if ($merge === true)
|
||||
$this->tplVariables[$name] = array_merge((isset($this->tplVariables[$name])) ? $this->tplVariables[$name] : array(), $value);
|
||||
else
|
||||
$this->tplVariables[$name] = $value;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt den Ordner mit den Template-Dateien.
|
||||
*
|
||||
* <code>$tpl->setTplFolder('/var/www/public_html/templates'); // Setzt den Pfad auf "/var/www/public_html/templates".</code>
|
||||
*
|
||||
* @param string $path Pfad zum Ordner.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setTplFolder($path)
|
||||
{
|
||||
if (!strlen($path) > 0 || !is_string($path))
|
||||
return false;
|
||||
|
||||
if (file_exists($path) !== true || is_dir($path) !== true)
|
||||
return false;
|
||||
|
||||
$this->tplFolderPath = $path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt den Ordner mit den Template-Dateien für Plugins.
|
||||
*
|
||||
* <code>$tpl->setTplFolder('/var/www/resources/plugins/test/public_html/templates'); // Setzt den Pfad auf "/var/www/resources/plugins/test/public_html/templates".</code>
|
||||
*
|
||||
* @param string $path Pfad zum Ordner.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setTplFolderPlugin($path)
|
||||
{
|
||||
if (!strlen($path) > 0 || !is_string($path))
|
||||
return false;
|
||||
|
||||
if (file_exists($path) !== true || is_dir($path) !== true)
|
||||
return false;
|
||||
|
||||
$this->tplFolderPathPlugin = $path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt den title-Tag.
|
||||
*
|
||||
* <code>$tpl->setHeaderTitle('Übersicht');</code>
|
||||
*
|
||||
* @param string $title Titel
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setHeaderTitle($title)
|
||||
{
|
||||
if (!strlen($title) > 0 || !is_string($title))
|
||||
return false;
|
||||
|
||||
$this->tplHeaderTitle = $title;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt das Format für den title-Tag.
|
||||
*
|
||||
* <code>$tpl->setHeaderTitleFormat('Pi Control | %s');</code>
|
||||
*
|
||||
* @param string $format Format für den Titel.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setHeaderTitleFormat($format)
|
||||
{
|
||||
if (!strlen($format) > 0 || !is_string($format))
|
||||
return false;
|
||||
|
||||
$this->tplHeaderTitleFormat = $format;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt den Wert, ob der Header angezeigt werden soll oder nicht.
|
||||
*
|
||||
* <code>$tpl->setDrawHeader(true); // Header wird angezeigt.</code>
|
||||
*
|
||||
* @param bool $draw
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setDrawHeader($draw = true)
|
||||
{
|
||||
$this->tplLoadHeader = $draw;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt Header an.
|
||||
*
|
||||
* <code>$tpl->drawHeader();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function drawHeader()
|
||||
{
|
||||
if ($this->tplLoadHeader !== true)
|
||||
return false;
|
||||
|
||||
$fileName = CONTENT_PATH.'html_header.php';
|
||||
|
||||
$this->tplLoadedHeader = true;
|
||||
|
||||
if (file_exists($fileName) !== true || is_file($fileName) !== true)
|
||||
throw new FileException(self::_t('Datei "%s" existiert nicht oder ist keine gültige Datei.', $fileName));
|
||||
|
||||
$tplMain = $this->tpl;
|
||||
|
||||
// Übergebe Titel
|
||||
$data['title'] = sprintf($this->tplHeaderTitleFormat, $this->tplHeaderTitle);
|
||||
|
||||
// Uebergebe Uebersetzung
|
||||
$data['jsTranslations'] = isset($this->tplVariables['jsTranslations']) ? $this->tplVariables['jsTranslations'] : array();
|
||||
|
||||
(include_once $fileName) or self::tplError(self::_t('Konnte Datei "%s" nicht öffnen und auslesen.', $fileName), __LINE__);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt den Wert, ob der Footer angezeigt werden soll oder nicht.
|
||||
*
|
||||
* <code>$tpl->setDrawFooter(true, $config, $errorHandler); // Footer wird angezeigt.</code>
|
||||
*
|
||||
* @param bool $draw
|
||||
* @param array $mainConfig Konfig-Array aus main_config.php.
|
||||
* @param array $errorHandler
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setDrawFooter($draw = true, $mainConfig = NULL)
|
||||
{
|
||||
$this->tplLoadFooter = $draw;
|
||||
$this->tplFooterConfig = $mainConfig;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt Footer an.
|
||||
*
|
||||
* <code>$tpl->drawFooter();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function drawFooter()
|
||||
{
|
||||
global $errorHandler;
|
||||
|
||||
if ($this->tplLoadFooter !== true)
|
||||
return false;
|
||||
|
||||
$fileName = CONTENT_PATH.'html_footer.php';
|
||||
|
||||
$this->tplLoadedFooter = true;
|
||||
|
||||
if (file_exists($fileName) !== true || is_file($fileName) !== true)
|
||||
throw new FileException(self::_t('Datei "%s" existiert nicht oder ist keine gültige Datei.', $fileName));
|
||||
|
||||
$tplMain = $this->tpl;
|
||||
$tplConfig = $this->tplFooterConfig;
|
||||
$tplErrorHandler = $errorHandler;
|
||||
|
||||
(include_once $fileName) or self::tplError(self::_t('Konnte Datei "%s" nicht öffnen und auslesen.', $fileName), __LINE__);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Öffnet Template-Datei und zeigt Inhalt anschließend an.
|
||||
*
|
||||
* <code>$tpl->draw('home'); // Für Home</code>
|
||||
*
|
||||
* @param string $tplFileName Template-Datei
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function draw($tplFileName = '')
|
||||
{
|
||||
self::drawHeader();
|
||||
|
||||
if ($this->ifError === true)
|
||||
return false;
|
||||
|
||||
$this->tplDraw = true;
|
||||
$folderPath = $this->tplFolderPath;
|
||||
|
||||
if ($this->tplFolderPathPlugin != '')
|
||||
$folderPath = $this->tplFolderPathPlugin.'/public_html/templates/';
|
||||
|
||||
if (strlen($tplFileName) >= 1 && is_string($tplFileName))
|
||||
{
|
||||
if (file_exists($folderPath.$tplFileName.$this->tplFileSuffix) !== true || is_file($folderPath.$tplFileName.$this->tplFileSuffix) !== true)
|
||||
return self::tplError(self::_t('Datei "%s" existiert nicht oder ist keine gültige Datei.', $tplFileName), __LINE__-1);
|
||||
}
|
||||
|
||||
self::drawMsg();
|
||||
|
||||
$data = $this->tplVariables;
|
||||
|
||||
if (strlen($tplFileName) >= 1 && is_string($tplFileName))
|
||||
(include_once $folderPath.$tplFileName.$this->tplFileSuffix) or self::error(self::_t('Konnte Datei "%s" nicht öffnen und auslesen.', $tplFileName), __LINE__);
|
||||
|
||||
// Optisch schöner
|
||||
echo PHP_EOL;
|
||||
|
||||
self::drawFooter();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Öffnet Error-Template-Datei und zeigt Inhalt + Fehler anschließend an
|
||||
*
|
||||
* <code>$tpl->drawError('Fehler', 'Nachricht', true); // Für Fehler.</code>
|
||||
*
|
||||
* @param string $errorTitle Titel des Fehlers.
|
||||
* @param string $errorMsg Nachricht des Fehlers.
|
||||
* @param bool $errorCancel Soll nur die Fehlermeldung angezeigt werden?
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function drawError($errorTitle, $errorMsg, $errorCancel)
|
||||
{
|
||||
if (!strlen($errorMsg) > 0 || !is_string($errorMsg))
|
||||
return false;
|
||||
|
||||
if (file_exists($this->tplFolderPath.'error'.$this->tplFileSuffix) !== true || is_file($this->tplFolderPath.'error'.$this->tplFileSuffix) !== true)
|
||||
return false;
|
||||
|
||||
if ($errorCancel === true)
|
||||
if (self::drawHeader() === false)
|
||||
return false;
|
||||
|
||||
$data['title'] = $errorTitle;
|
||||
$data['msg'] = $errorMsg;
|
||||
|
||||
include $this->tplFolderPath.'error'.$this->tplFileSuffix;
|
||||
|
||||
if ($errorCancel === true)
|
||||
if (self::drawFooter() === false)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fehler anzeigen.
|
||||
*
|
||||
* <code>$tpl->error('Fehler', 'Nachricht', true); // Für Fehler.</code>
|
||||
*
|
||||
* @param string $errorTitle Titel des Fehlers.
|
||||
* @param string $errorMsg Nachricht des Fehlers.
|
||||
* @param bool $errorCancel Soll nur die Fehlermeldung angezeigt werden?
|
||||
* @return bool
|
||||
*
|
||||
* @TODO Wenn $errorCancel auf false steht, wird der Fehler falsch angezeigt.
|
||||
*/
|
||||
|
||||
public function error($errorTitle, $errorMsg, $errorCancel = true)
|
||||
{
|
||||
$this->ifError = $errorCancel;
|
||||
|
||||
// Prüfe, ob Error-Template-Datei existiert
|
||||
if (self::drawError($errorTitle, $errorMsg, $errorCancel) === true)
|
||||
return false;
|
||||
|
||||
printf('<h1>%s</h1>%s', $errorTitle, $errorMsg);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fehler anzeigen.
|
||||
*
|
||||
* <code>$tpl->tplError('Fehler', __LINE__); // Für Fehler.</code>
|
||||
*
|
||||
* @param string $errorMsg Nachricht des Fehlers.
|
||||
* @param int $errorLine Zeilennummer des Fehlers.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function tplError($errorMsg, $errorLine = 0)
|
||||
{
|
||||
self::error('Fehler im TPL-System', sprintf('%s Zeile: %s', $errorMsg, $errorLine), true);
|
||||
|
||||
// Rückgabewert für andere Funktion
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Leitet auf eine andere Seite weiter.
|
||||
*
|
||||
* <code>$tpl->redirect('?s=overview'); // Leitet auf "?s=overview" weiter.</code>
|
||||
*
|
||||
* @param string $url URL auf die weitergeleitet werden soll.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function redirect($url)
|
||||
{
|
||||
if (!strlen($url) > 0 || !is_string($url))
|
||||
return false;
|
||||
|
||||
if (!headers_sent($filename, $linenum))
|
||||
exit(header('Location: '.$url));
|
||||
else
|
||||
{
|
||||
self::error(self::_t('Weiterleitung'),
|
||||
'<strong class="red">'.self::_t('Header bereits gesendet. Redirect nicht möglich, klicke daher stattdessen <a href="%s">diesen Link</a> an.', $url).'</strong>',
|
||||
true);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt Debugmeldungen an.
|
||||
*
|
||||
* <code>$tpl->showDebug();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function showDebug()
|
||||
{
|
||||
printf(PHP_EOL.'<!-- DEBUG - Start -->'.PHP_EOL.' <hr /><p>Ladezeit: %f<br />Fehler: %s</p>'.PHP_EOL.'<!-- DEBUG - End -->'.PHP_EOL, round(microtime(true)-$this->runtimeStart, 5), ($this->ifError) ? 'true' : 'false');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt Infomeldung hinzu.
|
||||
*
|
||||
* <code>$tpl->msg('red', 'Warnung', 'Infotext', true); // Zeigt rote Warnmeldung an.</code>
|
||||
*
|
||||
* @param string $type Type (Farbe) der Meldung. Möglich: red, green, yellow.
|
||||
* @param string $title Titel der Meldung.
|
||||
* @param string $msg Nachricht der Meldung.
|
||||
* @param bool $cancelable Soll die Meldung geschlossen werden können?
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function msg($type, $title = NULL, $msg, $cancelable = true, $id = 0)
|
||||
{
|
||||
if (!strlen($type) > 0 || !is_string($type) ||
|
||||
!strlen($msg) > 0 || !is_string($msg)
|
||||
)
|
||||
return false;
|
||||
|
||||
if ($id > 0)
|
||||
{
|
||||
$this->tplMsg[$id + 100] = array($type, $title, $msg, $cancelable);
|
||||
}
|
||||
else
|
||||
$this->tplMsg[] = array($type, $title, $msg, $cancelable);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function msgExists($id)
|
||||
{
|
||||
if (isset($this->tplMsg[$id + 100]))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt Infomeldung(en) an.
|
||||
*
|
||||
* <code>$tpl->drawMsg();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function drawMsg()
|
||||
{
|
||||
if (is_array($this->tplMsg) !== true || count($this->tplMsg) == 0)
|
||||
return false;
|
||||
|
||||
if (file_exists($this->tplFolderPath.'msg'.$this->tplFileSuffix) !== true || is_file($this->tplFolderPath.'msg'.$this->tplFileSuffix) !== true)
|
||||
return false;
|
||||
|
||||
foreach ($this->tplMsg as $key => $msg)
|
||||
{
|
||||
$data['id'] = $key;
|
||||
$data['type'] = $msg[0];
|
||||
$data['title'] = $msg[1];
|
||||
$data['msg'] = $msg[2];
|
||||
$data['cancelable'] = $msg[3];
|
||||
|
||||
(include $this->tplFolderPath.'msg'.$this->tplFileSuffix) or self::tplError(self::_t('Konnte Datei "%s" nicht öffnen und auslesen.', $this->tplFolderPath.'msg'.$this->tplFileSuffix), __LINE__);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verbindet sich mit SSH.
|
||||
*
|
||||
* <code>$tpl->loadSSH();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
private function loadSSH()
|
||||
{
|
||||
set_include_path(LIBRARY_PATH.'terminal');
|
||||
|
||||
if (!class_exists('Net_SSH2'))
|
||||
{
|
||||
include(LIBRARY_PATH.'terminal/Net/SSH2.php');
|
||||
include(LIBRARY_PATH.'terminal/File/ANSI.php');
|
||||
include(LIBRARY_PATH.'terminal/Crypt/RSA.php');
|
||||
}
|
||||
|
||||
$ssh = NULL;
|
||||
|
||||
if (!(isset($_COOKIE['_pi-control_ssh']) && $_COOKIE['_pi-control_ssh'] != ''))
|
||||
return false;
|
||||
|
||||
$token = $_COOKIE['_pi-control_ssh'];
|
||||
$token2 = $_COOKIE['_pi-control_ssh_'.$token];
|
||||
|
||||
$sshType = getConfig('ssh:token_'.$token.'.type', 'password');
|
||||
$sshPort = getConfig('ssh:token_'.$token.'.port', 22);
|
||||
$sshUsername = getConfig('ssh:token_'.$token.'.username', 'root');
|
||||
$sshPassword = getConfig('ssh:token_'.$token.'.password', '');
|
||||
$sshPrivateKey = base64_decode(getConfig('ssh:token_'.$token.'.privateKey', ''));
|
||||
|
||||
#$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
|
||||
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
|
||||
#$sshPassword = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $token2, base64_decode($sshPassword), MCRYPT_MODE_ECB, $iv);
|
||||
$sshPassword = openssl_encrypt(base64_decode($sshPassword), 'aes-256-cbc', $token2, 0, $iv);
|
||||
$sshPassword = rtrim($sshPassword, "\0");
|
||||
|
||||
$ssh = new Net_SSH2('127.0.0.1', $sshPort);
|
||||
|
||||
if ($sshType == 'password')
|
||||
{
|
||||
if (!$ssh->login($sshUsername, $sshPassword))
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($sshType == 'publickey')
|
||||
{
|
||||
$sshKey = new Crypt_RSA();
|
||||
|
||||
if ($sshPassword != '')
|
||||
$sshKey->setPassword($sshPassword);
|
||||
|
||||
$sshKey->loadKey($sshPrivateKey);
|
||||
|
||||
if (!$ssh->login($sshUsername, $sshKey))
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($ssh === NULL)
|
||||
return false;
|
||||
|
||||
$this->tplSSH = $ssh;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt einen SSH-Befehl aus.
|
||||
* $canelIfError: 0 = Keine Auswirkung
|
||||
* 1 = Fehlermeldung
|
||||
* 2 = Fehlermeldung + Abbrechen
|
||||
*
|
||||
* <code>$tpl->executeSSH('ls', true, 0); // Im Fehlerfall wird keine Meldung ausgegeben.</code>
|
||||
*
|
||||
* @param string $command SSH-Befehl
|
||||
* @param bool $blockStream Soll gewartet werden, bis Rückgabe?
|
||||
* @param int $cancelIfError Verhalten im Fehlerfall.
|
||||
* @return bool|array
|
||||
*/
|
||||
|
||||
public function executeSSH($command, $timeout = NULL, $cancelIfError = 1)
|
||||
{
|
||||
if ($this->tplSSH === NULL)
|
||||
if (self::loadSSH() !== true)
|
||||
if ($cancelIfError !== 0)
|
||||
return self::error(_t('SSH-Zugriffsfehler'), _t('Kein SSH-Zugriff, diese Funktion steht aktuell nicht zur Verfügung!'), ($cancelIfError === 1) ? true : false);
|
||||
#return self::error(_t('SSH-Zugriffsfehler'), _t('Kein SSH-Zugriff, bitte anmelden! <a href="%s">Jetzt anmelden.</a>', '?s=ssh_login'), ($cancelIfError === 1) ? true : false);
|
||||
|
||||
if ($timeout != NULL)
|
||||
$this->tplSSH->setTimeout($timeout);
|
||||
else
|
||||
$this->tplSSH->setTimeout(10);
|
||||
|
||||
$this->tplSSH->enableQuietMode();
|
||||
|
||||
if ($this->tplSSH === NULL || ($output = $this->tplSSH->exec($command)) === false)
|
||||
return false;
|
||||
|
||||
$error = $this->tplSSH->getStdError();
|
||||
$exitStatus = $this->tplSSH->getExitStatus();
|
||||
|
||||
return array($output, $error, $exitStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rückgabe der SSH-Ressource.
|
||||
*
|
||||
* <code>$tpl->getSSHResource();</code>
|
||||
*
|
||||
* @return bool|resource
|
||||
*/
|
||||
|
||||
public function getSSHResource($cancelIfError = 0)
|
||||
{
|
||||
if ($this->tplSSH === NULL)
|
||||
if (self::loadSSH() !== true)
|
||||
{
|
||||
if ($cancelIfError !== 0)
|
||||
self::error(_t('SSH-Zugriffsfehler'), _t('Kein SSH-Zugriff, diese Funktion steht aktuell nicht zur Verfügung!'), ($cancelIfError === 1) ? true : false);
|
||||
#self::error(_t('SSH-Zugriffsfehler'), _t('Kein SSH-Zugriff, bitte anmelden! <a href="%s">Jetzt anmelden.</a>', '?s=ssh_login'), ($cancelIfError === 1) ? true : false);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->tplSSH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ermittelt Informationen der gespeicherten SSH-Informationen.
|
||||
*
|
||||
* <code>$tpl->getSSHInfo();</code>
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
|
||||
public function getSSHInfo()
|
||||
{
|
||||
$sshType = getConfig('ssh:latest.type', 'password');
|
||||
$sshPort = getConfig('ssh:latest.port', 22);
|
||||
$sshUsername = getConfig('ssh:latest.username', '');
|
||||
|
||||
if (isset($_COOKIE['_pi-control_ssh']) && $_COOKIE['_pi-control_ssh'] != '')
|
||||
{
|
||||
$token = $_COOKIE['_pi-control_ssh'];
|
||||
|
||||
$sshType = getConfig('ssh:token_'.$token.'.type', $sshType);
|
||||
$sshPort = getConfig('ssh:token_'.$token.'.port', $sshPort);
|
||||
$sshUsername = getConfig('ssh:token_'.$token.'.username', $sshUsername);
|
||||
}
|
||||
|
||||
return array('type' => $sshType, 'port' => $sshPort, 'username' => $sshUsername);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt SSH-Informationen.
|
||||
*
|
||||
* <code>$tpl->setSSHInfo(22, 'pi', 'raspberry', false); // Login nur für aktuelle Sitzung</code>
|
||||
*
|
||||
* @param int $port SSH-Port
|
||||
* @param string $username SSH-Benutzername
|
||||
* @param string $password SSH-Passwort
|
||||
* @param bool $saveInFile Langer Login (true) oder nur aktuelle Sitzung (false)?
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function setSSHInfo($type, $port, $username, $password, $privateKey, $rememberMe = false)
|
||||
{
|
||||
if (!is_array($SSHInfo = self::getSSHInfo()))
|
||||
return false;
|
||||
|
||||
if ($type != '' && is_string($type))
|
||||
$SSHInfo['type'] = $type;
|
||||
|
||||
if ($port != '' && is_numeric($port))
|
||||
$SSHInfo['port'] = $port;
|
||||
|
||||
if ($username != '' && is_string($username))
|
||||
$SSHInfo['username'] = $username;
|
||||
|
||||
if ($privateKey != '' && is_string($privateKey))
|
||||
$SSHInfo['privateKey'] = $privateKey;
|
||||
|
||||
if ($password != '')
|
||||
{
|
||||
if (isset($_COOKIE['_pi-control_ssh']) && $_COOKIE['_pi-control_ssh'] != '')
|
||||
$this->logoutSSH();
|
||||
|
||||
$uniqid = generateUniqId(16, false);
|
||||
$uniqid2 = generateUniqId(32, false);
|
||||
|
||||
#$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
|
||||
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
|
||||
#$SSHInfo['password'] = base64_encode(openssl_encrypt(MCRYPT_RIJNDAEL_256, $uniqid2, $password, MCRYPT_MODE_ECB, $iv));
|
||||
$SSHInfo['password'] = openssl_encrypt($password, 'aes-256-cbc', $uniqid, 0, $iv);
|
||||
|
||||
$SSHInfo['privateKey'] = $privateKey;
|
||||
|
||||
if (setConfig('ssh:token_'.$uniqid.'.created', time()) !== true) return false;
|
||||
if (setConfig('ssh:token_'.$uniqid.'.type', $SSHInfo['type']) !== true) return false;
|
||||
if (setConfig('ssh:token_'.$uniqid.'.port', $SSHInfo['port']) !== true) return false;
|
||||
if (setConfig('ssh:token_'.$uniqid.'.username', $SSHInfo['username']) !== true) return false;
|
||||
if (setConfig('ssh:token_'.$uniqid.'.password', $SSHInfo['password']) !== true) return false;
|
||||
if (setConfig('ssh:token_'.$uniqid.'.privateKey', base64_encode($SSHInfo['privateKey'])) !== true) return false;
|
||||
|
||||
setConfig('ssh:latest.type', $SSHInfo['type']);
|
||||
setConfig('ssh:latest.port', $SSHInfo['port']);
|
||||
setConfig('ssh:latest.username', $SSHInfo['username']);
|
||||
|
||||
if ($rememberMe == false)
|
||||
{
|
||||
setcookie('_pi-control_ssh', $uniqid, time()+60*60*12);
|
||||
setcookie('_pi-control_ssh_'.$uniqid, $uniqid2, time()+60*60*12);
|
||||
}
|
||||
else
|
||||
{
|
||||
setcookie('_pi-control_ssh', $uniqid, time()+60*60*24*30);
|
||||
setcookie('_pi-control_ssh_'.$uniqid, $uniqid2, time()+60*60*24*30);
|
||||
}
|
||||
|
||||
$_COOKIE['_pi-control_ssh'] = $uniqid;
|
||||
$_COOKIE['_pi-control_ssh_'.$uniqid] = $uniqid2;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht SSH-Login.
|
||||
*
|
||||
* <code>$tpl->logoutSSH();</code>
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function logoutSSH()
|
||||
{
|
||||
if (isset($_COOKIE['_pi-control_ssh']) && $_COOKIE['_pi-control_ssh'] != '')
|
||||
{
|
||||
$token = $_COOKIE['_pi-control_ssh'];
|
||||
|
||||
removeConfig('ssh:token_'.$token);
|
||||
setcookie('_pi-control_ssh', '', time()-60);
|
||||
setcookie('_pi-control_ssh_'.$token, '', time()-60);
|
||||
$_COOKIE['_pi-control_ssh'] = '';
|
||||
$_COOKIE['_pi-control_ssh_'.$token] = '';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
?>
|
||||
3
backend/resources/library/main/tpl.function.php
Normal file
3
backend/resources/library/main/tpl.function.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
if (!defined('PICONTROL')) exit();
|
||||
?>
|
||||
Reference in New Issue
Block a user