Init Repo

This commit is contained in:
Gregor Schulte
2021-06-15 11:08:34 +02:00
parent d0b72a118d
commit b303dd00d6
330 changed files with 93268 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/rpi.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'cache/cache.class.php') or die('Error: 0x0004');
$api = new API;
if (isset($_POST['data']))
{
$datas = explode(';', $_POST['data']);
foreach ($datas as $data)
{
switch ($data)
{
case 'time':
$api->addData('time', date('d.m.Y H:i:s', time()));
break;
case 'timezone':
$api->addData('timezone', date('e (P)', time()));
break;
case 'runtime':
$api->addData('runtime', getDateFormat(rpi_getRuntime()));
break;
case 'startTime':
$api->addData('startTime', formatTime(time() - rpi_getRuntime()));
break;
case 'serial':
$api->addData('serial', rpi_getRpiSerial());
break;
case 'revision':
$api->addData('revision', rpi_getRpiRevision());
break;
case 'distribution':
$api->addData('distribution', rpi_getDistribution());
break;
case 'kernel':
$api->addData('kernel', rpi_getKernelVersion());
break;
case 'webserver':
$api->addData('webserver', $_SERVER['SERVER_SOFTWARE']);
break;
case 'php':
$api->addData('php', PHP_VERSION);
break;
case 'whoami':
$api->addData('whoami', exec('whoami'));
break;
case 'cpuClock':
$api->addData('cpuClock', rpi_getCpuClock());
break;
case 'cpuClockMax':
$api->addData('cpuClockMax', rpi_getCpuMaxClock());
break;
case 'cpuLoad':
$cpu = rpi_getCPULoad(true);
$api->addData('cpuLoad', $cpu);
break;
case 'cpuLoads':
$cpu = rpi_getCPULoad(true, true);
$api->addData('cpuLoads', (count($cpu) == 1) ? array() : array_slice($cpu, 1));
break;
case 'cpuType':
$api->addData('cpuType', rpi_getCPUType());
break;
case 'cpuModel':
$api->addData('cpuModel', rpi_getCpuModel());
break;
case 'cpuTemp':
$api->addData('cpuTemp', numberFormat(rpi_getCoreTemprature()));
break;
case 'ramPercentage':
$ram = rpi_getMemoryUsage();
$api->addData('ramPercentage', $ram['percent']);
break;
case 'memory':
$memory = rpi_getMemoryInfo();
$api->addData('memory', $memory);
break;
case 'memoryCount':
$memory = rpi_getMemoryInfo();
$api->addData('memoryCount', count($memory));
break;
case 'runningTasksCount':
$api->addData('runningTasksCount', rpi_getCountRunningTasks());
break;
case 'installedPackagesCount':
$api->addData('installedPackagesCount', rpi_getCountInstalledPackages());
break;
default:
$api->setError('error', 'Data for "'.$data.'" is not available.');
break 2;
}
}
}
else
$api->setError('error', 'No data set.');
$api->display();
?>

View File

@@ -0,0 +1,19 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'plugin/plugin.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$api = new API;
$plugins = pluginList();
$onlinePlugins = getOnlinePlugins();
$availableUpdates = checkPluginUpdate($plugins, $onlinePlugins);
$api->addData('onlinePlugins', $onlinePlugins);
$api->addData('availableUpdates', $availableUpdates);
$api->display();
?>

68
api/v1/feedback.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'main/rpi.function.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'troubleshooting/troubleshooting.function.php') or die('Error: 0x0004');
$stats = array();
// Pi Control Cron
$cronEntry = '* * * * * '.exec('whoami').' php -f "'.CRON_PATH.'init.php" >/dev/null 2>&1 # By Pi Control';
exec('cat /etc/crontab', $crontab);
$cronMatch = preg_match('/^\*\s\*\s\*\s\*\s\*\s'.preg_quote(exec('whoami')).'\sphp \-f "'.preg_quote(CRON_PATH, '/').'init\.php"( )?(# By Pi Control)?/im', implode(PHP_EOL, $crontab));
// Dateien und Ordner
$filesFoldersExist = array('count' => 0, 'status' => true);
$filesFoldersPermission = array('count' => 0, 'status' => true);
$filesFolders = fileFolderPermission();
foreach ($filesFolders as $file => $info)
{
if ($info['error'] === true)
{
if ($info['existsBool'] === false || $info['filesizeBool'] === false)
{
$filesFoldersExist['count'] += 1;
$filesFoldersExist['status'] = false;
}
if ($info['permissionBool'] === false || $info['userGroupBool'] === false)
{
$filesFoldersPermission['count'] += 1;
$filesFoldersPermission['status'] = false;
}
}
}
$stats['version'] = $config['version']['versioncode'];
$stats['language'] = $globalLanguage;
$stats['url'] = urldecode($_POST['url']);
$stats['path'] = PICONTROL_PATH;
$stats['php'] = PHP_VERSION;
$stats['webserver'] = $_SERVER['SERVER_SOFTWARE'];
$stats['filesFoldersExist'] = $filesFoldersExist;
$stats['filesFoldersPermission'] = $filesFoldersPermission;
$stats['piControlCron'] = array('match' => $cronMatch, 'paketStatus' => trim(exec('dpkg -s php5-cli | grep Status: ')));
$stats['whoami'] = exec('whoami');
$stats['lastStart'] = time() - rpi_getRuntime();
$stats['serverAddr'] = $_SERVER['SERVER_ADDR'];
$stats['serverPort'] = $_SERVER['SERVER_PORT'];
$stats['distribution'] = rpi_getDistribution();
$stats['userAgent'] = $_SERVER['HTTP_USER_AGENT'];
$stats['config'] = array('accessExternal' => getConfig('main:access.external', 'null'),
'language' => getConfig('init:language', 'null'),
'executionCron' => getConfig('cron:execution.cron', 'null'),
'updateCheckPlugins' => getConfig('cron:updateCheck.plugins', 'null'),
'updateCheckPicontrol' => getConfig('cron:updateCheck.picontrol', 'null'),
'cache' => getConfig('cache:activation.cache', 'null'),
'cacheUsbDevices' => getConfig('cache:activation.usb_devices', 'null'),
'cacheUsers' => getConfig('cache:activation.users', 'null'),
'cacheWeather' => getConfig('cache:activation.weather', 'null')
);
echo urlencode(base64_encode(json_encode($stats)));
?>

111
api/v1/login.php Normal file
View File

@@ -0,0 +1,111 @@
<?php
define('PICONTROL', true);
$doNotCheckForAuthentification = true;
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$api = new API;
$externalAccess = (urlIsPublic($_SERVER['REMOTE_ADDR']) && getConfig('main:access.external', 'false') == 'false') ? false : true;
$nextTry = getConfig('login:login.nextTry');
if ($externalAccess === false)
$api->addData('error', 'Local network.');
elseif ($nextTry > time())
$api->addData('error', array('msg' => 'Login disabled.', 'seconds' => $nextTry-time()));
if (isset($_POST['username'], $_POST['password']) && $externalAccess === true)
{
if (trim($_POST['username']) != '' && $_POST['password'] != '')
{
$pUsername = strtolower(trim($_POST['username']));
$pPassword = $_POST['password'];
$try = getConfig('login:login.try');
do
{
if ($nextTry > time())
break;
if (($userinfo = getConfig('user:user_'.$pUsername, 0)) === 0)
break;
if (!is_array($userinfo))
break;
if (strtolower($userinfo['username']) != $pUsername || password_verify($pPassword, $userinfo['password']) !== true)
break;
setConfig('login:login.try', 0);
setConfig('login:login.nextTry', 0);
$uniqid = generateUniqId(32, false);
if (setConfig('login:token_'.$uniqid.'.created', time()) !== true) break;
if (setConfig('login:token_'.$uniqid.'.username', $pUsername) !== true) break;
if (setConfig('login:token_'.$uniqid.'.address', $_SERVER['REMOTE_ADDR']) !== true) break;
if (setConfig('user:user_'.$pUsername.'.last_login', time()) !== true) break;
if (isset($_POST['rememberMe']) && $_POST['rememberMe'] == 'checked')
setConfig('login:token_'.$uniqid.'.remember_me', 'true');
$api->addData('token', $uniqid);
$api->display();
exit();
}
while (false);
if ($nextTry > time())
{
$api->addData('error', array('msg' => 'Login disabled.', 'seconds' => $nextTry-time()));
}
elseif ($try == 5)
{
$api->addData('error', array('msg' => 'Login failed.', 'seconds' => 30));
setConfig('login:login.nextTry', time() + 30);
}
elseif ($try == 6)
{
$api->addData('error', array('msg' => 'Login failed.', 'seconds' => 60));
setConfig('login:login.nextTry', time() + 60);
}
elseif ($try == 7)
{
$api->addData('error', array('msg' => 'Login failed.', 'seconds' => 120));
setConfig('login:login.nextTry', time() + 120);
}
elseif ($try >= 8)
{
$api->addData('error', array('msg' => 'Login failed.', 'seconds' => 300));
setConfig('login:login.nextTry', time() + 300);
}
else
{
$api->addData('error', array('msg' => 'Login failed.'));
}
setConfig('login:login.try', $try + 1);
}
}
if (isset($_POST['logout']))
{
if (isset($_POST['token']))
{
$uniqid = $_POST['token'];
removeConfig('login:token_'.$uniqid);
$api->addData('success', 'logout');
}
else
$api->setError('error', 'No token set.');
$api->display();
exit();
}
$api->display();
?>

19
api/v1/logs.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'log/log.function.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'log/log.class.php') or die('Error: 0x0004');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
$logController = new LogController('/var/log/');
$api->addData('logs', $logController->getAllArray());
$api->display();
?>

36
api/v1/logs_download.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'log/log.function.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'log/log.class.php') or die('Error: 0x0004');
if (!isset($_GET['log']) || ($logName = trim(urldecode($_GET['log']))) == '')
exit();
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$logController = new LogController('/var/log');
$log = $logController->getLogFromRelativePath($logName);
if (!$log instanceof LogEntry)
exit();
if ($log->getReadable() === false)
exit();
$readLog = $logController->readLog($log->getPath());
$filename = ((substr($log->getFilename(), -3) == '.gz') ? substr($log->getFilename(), 0, -3) : $log->getFilename()).'.txt';
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=".$filename);
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen('php://output', 'w');
fwrite($output, $readLog['output']);
fclose($output);
?>

22
api/v1/main.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
define('PICONTROL', true);
$doNotCheckForAuthentification = true;
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$api = new API;
$api->addData('version', [
'name' => $config['version']['version'],
'code' => $config['version']['versioncode'],
'androidCompLevel' => $config['version']['android_comp_level']
]);
$api->addData('installed', (file_exists(INSTALL_PATH) && is_dir(INSTALL_PATH)) ? false : true);
$api->addData('language', $globalLanguage);
$api->addData('theme', getConfig('main:theme.color', 'blue'));
$api->addData('label', getConfig('main:main.label', 'Raspberry Pi'));
$api->display();
?>

73
api/v1/network.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/rpi.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0004');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
if (isset($_POST['execute']))
{
do
{
if ($tpl->getSSHResource() === false)
{
$tpl->setError('error', 'logged out');
break;
}
switch ($_POST['execute'])
{
case 'wlan':
$networkConnections = getAllNetworkConnections();
scanAccessPoints($networkConnections, true);
$SSHReturn = $SSHError = '';
break;
default:
$api->setError('error', 'Unknown execute.');
break 2;
}
$api->addData('return', $SSHReturn);
$api->addData('error', $SSHError);
}
while (false);
}
else
{
$networkConnections = getAllNetworkConnections();
$networkCountsJson = htmlspecialchars_decode(getConfig('main:network.overflowCount', '{}'));
$networkCounts = json_decode($networkCountsJson, true);
$counter = 0;
foreach ($networkConnections as $network)
{
$countSent = 0;
$countReceive = 0;
if (isset($networkCounts[$network['interface']]['sent']))
$countSent = $networkCounts[$network['interface']]['sent'];
if (isset($networkCounts[$network['interface']]['receive']))
$countReceive = $networkCounts[$network['interface']]['receive'];
$networkConnections[$counter]['sent'] = (4294967295 * $countSent) + $network['sent'];
$networkConnections[$counter]['receive'] = (4294967295 * $countReceive) + $network['receive'];
$counter += 1;
}
$api->addData('networkConnections', $networkConnections);
$api->addData('hostname', rpi_getHostname());
$api->addData('wlan', scanAccessPoints($networkConnections, false));
}
$api->display();
?>

View File

@@ -0,0 +1,26 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
if (isset($_POST['interface']) && ($pInterface = trim($_POST['interface'])) != '')
{
list ($status, $error, $exitStatus) = $tpl->executeSSH('sudo ifdown '.escapeshellarg($pInterface).' && sudo ifup '.escapeshellarg($pInterface), 60);
$api->addData('success', 'true');
$api->addData('status', $status);
$api->addData('error', $error);
$api->addData('exitStatus', $exitStatus);
}
else
$api->setError('error', 'No interface set.');
$api->display();
?>

View File

@@ -0,0 +1,121 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'network/network.class.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'network/network.function.php') or die('Error: 0x0004');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
if (!isset($_POST['type']))
$api->setError('error', 'No type set.');
switch (isset($_POST['type']) ? $_POST['type'] : '')
{
case 'set':
if (isset($_POST['interface'], $_POST['ssid'], $_POST['psk']) && ($pInterface = trim($_POST['interface'])) != '' && ($pSsid = trim($_POST['ssid'])) != '')
{
if ($tpl->getSSHResource() !== false)
{
$networkInterface = new NetworkInterface($tpl);
$pPsk = $_POST['psk'];
list ($passphrase, $error, $exitStatus) = $tpl->executeSSH('sudo wpa_passphrase ' . escapeshellarg($pSsid) . ' ' . escapeshellarg($pPsk) . ' | grep "psk=[[:alnum:]]"', true);
$passphrase = trim(str_replace('psk=', '', $passphrase));
$network = array('ssid' => '"' . $pSsid . '"', 'psk' => $passphrase);
if (($status = addNetworkWPASupplicant($network)) === true)
{
$newInterface = array('auto' => true, 'protocol' => 'inet', 'method' => 'dhcp', 'iface' => array('wpa-conf' => '/etc/wpa_supplicant/wpa_supplicant.conf'));
$networkInterface->deleteInterface($pInterface, false);
if (($status2 = $networkInterface->addInterface($pInterface, $newInterface)) === true)
{
$api->addData('success', 'true');
$api->addData('status', $status);
$api->addData('error', $error);
$api->addData('exitStatus', $exitStatus);
}
else
$api->setError('error', 'Errorcode: ' . $status2);
}
else
$api->setError('error', 'Errorcode: ' . $status);
}
else
$api->setError('error', 'No ssh-login.');
}
else
$api->setError('error', 'No interface, ssid or psk set.');
break;
case 'down':
if (isset($_POST['interface']) && ($pInterface = trim($_POST['interface'])) != '')
{
if ($tpl->getSSHResource() !== false)
{
set_time_limit(60);
list ($status, $error, $exitStatus) = $tpl->executeSSH('sudo ifdown '.escapeshellarg($pInterface));
$api->addData('success', 'true');
$api->addData('status', $status);
$api->addData('error', $error);
$api->addData('exitStatus', $exitStatus);
}
else
$api->setError('error', 'No ssh-login.');
}
else
$api->setError('error', 'No interface set.');
break;
case 'up':
if (isset($_POST['interface']) && ($pInterface = trim($_POST['interface'])) != '')
{
if ($tpl->getSSHResource() !== false)
{
set_time_limit(60);
list ($status, $error, $exitStatus) = $tpl->executeSSH('sudo ifup '.escapeshellarg($pInterface), 60);
$api->addData('success', 'true');
$api->addData('status', $status);
$api->addData('error', $error);
$api->addData('exitStatus', $exitStatus);
}
else
$api->setError('error', 'No ssh-login.');
}
else
$api->setError('error', 'No interface set.');
break;
case 'get':
if (isset($_POST['interface']) && ($pInterface = trim($_POST['interface'])) != '')
{
list ($status, $error, $exitStatus) = $tpl->executeSSH('sudo /sbin/ifconfig '.escapeshellarg($pInterface).' | sed -n \'2s/[^:]*:\([^ ]*\).*/\1/p\'');
$api->addData('success', 'true');
$api->addData('ip', (filter_var(trim($status), FILTER_VALIDATE_IP) !== false) ? trim($status) : 'no ip');
$api->addData('errorMsg', _t('Konnte IP-Adresse nicht abrufen!'));
$api->addData('status', $status);
$api->addData('error', $error);
$api->addData('exitStatus', $exitStatus);
}
else
$api->setError('error', 'No interface set.');
break;
default:
$api->setError('error', 'Unknown type.');
}
$api->display();
?>

67
api/v1/overview.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/rpi.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0003');
$api = new API;
if (isset($_POST['data']))
{
$datas = explode(';', $_POST['data']);
foreach ($datas as $data)
{
switch ($data)
{
case 'startTime':
$api->addData('startTime', formatTime(time() - rpi_getRuntime()));
break;
case 'runtime':
$api->addData('runtime', getDateFormat(rpi_getRuntime()));
break;
case 'cpuClock':
$api->addData('cpuClock', rpi_getCpuClock());
break;
case 'cpuLoad':
$api->addData('cpuLoad', rpi_getCpuLoad(true));
break;
case 'cpuTemp':
$api->addData('cpuTemp', numberFormat(rpi_getCoreTemprature()));
break;
case 'ramPercentage':
$ram = rpi_getMemoryUsage();
$api->addData('ramPercentage', $ram['percent']);
break;
case 'memoryUsed':
$memory = rpi_getMemoryInfo();
$api->addData('memoryUsed', sizeUnit($memory[count($memory)-1]['used']));
break;
case 'memoryFree':
$memory = rpi_getMemoryInfo();
$api->addData('memoryFree', sizeUnit($memory[count($memory)-1]['free']));
break;
case 'memoryTotal':
$memory = rpi_getMemoryInfo();
$api->addData('memoryTotal', sizeUnit($memory[count($memory)-1]['total']));
break;
case 'memoryPercent':
$memory = rpi_getMemoryInfo();
$api->addData('memoryPercent', $memory[count($memory)-1]['percent']);
break;
case 'devices':
$api->addData('devices', rpi_getUsbDevices());
break;
default:
$api->setError('error', 'Data for "'.$data.'" are not available.');
break 2;
}
}
}
else
$api->setError('error', 'No data set.');
$api->display();
?>

5
api/v1/ping.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
header('Content-Type: application/json');
echo json_encode('pong');
?>

44
api/v1/plugins.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'plugin/plugin.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$api = new API;
$plugins = pluginList();
if (isset($_GET['id']) && !isset($_POST['id']))
$_POST['id'] = $_GET['id'];
if (isset($_GET['action']) && !isset($_POST['action']))
$_POST['action'] = $_GET['action'];
if (isset($_POST['id'], $plugins[$_POST['id']]))
{
$id = $_POST['id'];
if (isset($_POST['action']) && ($action = trim(urldecode($_POST['action']))) != '')
{
if (preg_match('/^v[0-9]\/[a-z][a-z0-9\-_]+$/i', $action) === 1)
{
if (file_exists(PLUGINS_PATH.$id.'/api/'.$action.'.php') && is_file(PLUGINS_PATH.$id.'/api/'.$action.'.php'))
{
initPluginConstants($id);
include PLUGINS_PATH.$id.'/api/'.$action.'.php';
}
else
$api->setError('error', 'Action not available.');
}
else
$api->setError('error', 'Wrong syntax for action.');
}
else
$api->addData('plugin', $plugins[$id]);
}
else
$api->addData('plugins', $plugins);
$api->display();
?>

17
api/v1/processes.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'process/process.function.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'process/process.class.php') or die('Error: 0x0003');
$api = new API;
$processController = new ProcessController;
$api->addData('count', $processController->getCount());
$api->addData('countRunning', $processController->getCountRunning());
$api->addData('processes', $processController->getProcessesArray());
$api->display();
?>

43
api/v1/shutdown.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0002');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
if (isset($_POST['execute']))
{
do
{
if ($tpl->getSSHResource() === false)
{
$tpl->setError('error', 'logged out');
break;
}
switch ($_POST['execute'])
{
case 'shutdown':
list ($SSHReturn, $SSHError, $SSHExitStatus) = $tpl->executeSSH('sudo /sbin/shutdown -h now', true, 0);
break;
case 'restart':
list ($SSHReturn, $SSHError, $SSHExitStatus) = $tpl->executeSSH('sudo /sbin/shutdown -r now', true, 0);
break;
default:
$api->setError('error', 'Unknown execute.');
break 2;
}
$api->addData('return', $SSHReturn);
$api->addData('error', $SSHError);
}
while (false);
}
$api->display();
?>

114
api/v1/ssh.php Normal file
View File

@@ -0,0 +1,114 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'main/tpl.class.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0003');
$tpl = new PiTpl;
$tpl->setTpl($tpl);
$api = new API;
if (isset($_POST['login']))
{
$pType = isset($_POST['type']) ? $_POST['type'] : '';
if ($pType == 'password')
{
if (isset($_POST['port'], $_POST['username'], $_POST['password']) && ($pPort = intval(trim($_POST['port']))) != '' && ($pUsername = trim($_POST['username'])) != '' && ($pPassword = $_POST['password']) != '')
{
$pRememberMe = (isset($_POST['remember-me']) && $_POST['remember-me'] == 'checked') ? true : false;
if (is_numeric($pPort) && $pPort >= 0 && $pPort <= 65535)
{
if ($tpl->setSSHInfo($pType, $pPort, $pUsername, $pPassword, NULL, $pRememberMe) === true)
{
if ($tpl->getSSHResource() !== false)
$api->addData('success', 'login');
else
$api->setError('error', 'login');
}
}
else
$api->setError('error', 'Invalid port.');
}
else
$api->setError('error', 'Missing parameters.');
}
elseif ($pType == 'publickey')
{
if (isset($_POST['port'], $_POST['username'], $_POST['privatekey']) && ($pPort = intval(trim($_POST['port']))) != '' && ($pUsername = trim($_POST['username'])) != '' && ($pPrivateKey = urldecode($_POST['privatekey'])) != '')
{
$pPassword = isset($_POST['password']) ? $_POST['password'] : '';
$pRememberMe = (isset($_POST['remember-me']) && $_POST['remember-me'] == 'checked') ? true : false;
if (is_numeric($pPort) && $pPort >= 0 && $pPort <= 65535)
{
if ($tpl->setSSHInfo($pType, $pPort, $pUsername, $pPassword, $pPrivateKey, $pRememberMe) === true)
{
if ($tpl->getSSHResource() !== false)
$api->addData('success', 'login');
else
$api->setError('error', 'login');
}
}
else
$api->setError('error', 'Invalid port.');
}
else
$api->setError('error', 'Missing parameters.');
}
else
$api->setError('error', 'Unknown type.');
}
if (isset($_POST['status']))
{
if ($tpl->getSSHResource() !== false)
$api->addData('status', 'logged in');
else
{
$api->addData('status', 'logged out');
$api->addData('ssh', $tpl->getSSHInfo());
}
}
if (isset($_POST['execute']))
{
do
{
if ($tpl->getSSHResource() === false)
{
$tpl->setError('error', 'logged out');
break;
}
switch ($_POST['execute'])
{
case 'shutdown':
list ($SSHReturn, $SSHError, $SSHExitStatus) = $tpl->executeSSH('sudo shutdown -h now', true, 0);
break;
case 'restart':
list ($SSHReturn, $SSHError, $SSHExitStatus) = $tpl->executeSSH('sudo shutdown -r now', true, 0);
break;
default:
$api->setError('error', 'Unknown execute.');
break 2;
}
$api->addData('return', $SSHReturn);
$api->addData('error', $SSHError);
$api->addData('exitStatus', $SSHExitStatus);
}
while (false);
}
if (isset($_POST['logout']))
{
$tpl->logoutSSH();
}
$api->display();
?>

89
api/v1/statistic.php Normal file
View File

@@ -0,0 +1,89 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'statistic/statistic.class.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'statistic/statistic.function.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0004');
(include_once LIBRARY_PATH.'plugin/plugin.function.php') or die('Error: 0x0005');
$api = new API;
if (isset($_POST['id']))
{
$controller = new StatisticController();
$controller->loadStatistics();
if (($name = $controller->getStatisticName($_POST['id'])) !== false)
{
if (isset($_POST['plugin']) && trim($_POST['plugin']) != '')
pluginLanguage(trim($_POST['plugin']));
$builder = new StatisticBuilder();
$builder->loadFromFile($name, (isset($_POST['plugin']) && trim($_POST['plugin']) != '') ? $_POST['plugin'] : NULL);
$statistic = $builder->getArray();
$log = new LogStatistic();
$log->setFile(LOG_PATH.$statistic['raw'].'.csv');
$arr = $info = array();
foreach ($statistic['columns'] as $column)
$arr['cols'][] = array('id' => '', 'label' => _t($column['label']), 'type' => $column['type']);
getRowsFromLog($arr, $info, $log->getAll(), $statistic['columns'], $statistic['cycle']);
if (isset($arr['rows']))
{
if (isset($_POST['type']) && $_POST['type'] == 'googleChart')
$arr['rows'] = convertForGoogleChart($arr['rows']);
$arr['rows'] = array_slice($arr['rows'], -2016);
$arr['periods'] = $info['periods'];
foreach (array('min', 'max') as $type)
{
if ($statistic['limits'][$type]['use'] == 'multiply')
$arr[$type] = round($info[$type] * $statistic['limits'][$type]['value']);
elseif ($statistic['limits'][$type]['use'] == 'fix')
{
if ($statistic['limits'][$type]['fix'] == true)
$arr[$type] = $statistic['limits'][$type]['value'];
else
$arr[$type] = round($info[$type]);
}
}
$api->addData('statistic', $arr);
}
else
$api->setError('error', 'Empty data.');
}
else
$api->setError('error', 'Data not found.');
}
else
{
$statistics = array();
$hiddenStatistics = unserialize(htmlspecialchars_decode(getConfig('main:statistic.hidden', 'a:0:{}')));
$controller = new StatisticController();
$controller->loadStatistics();
foreach ($controller->getStatistics() as $statistic)
{
$builder = new StatisticBuilder();
$builder->loadFromFile($statistic);
$array = $builder->getArray();
if (!in_array($builder->getId(), $hiddenStatistics))
$statistics[] = array('array' => $array);
}
$api->addData('statistics', $statistics);
$api->addData('hidden', $hiddenStatistics);
}
$api->display();
?>

View File

@@ -0,0 +1,55 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'main/main.function.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'statistic/statistic.class.php') or die('Error: 0x0002');
(include_once LIBRARY_PATH.'statistic/statistic.function.php') or die('Error: 0x0003');
(include_once LIBRARY_PATH.'statistic/statistic.class.php') or die('Error: 0x0004');
(include_once LIBRARY_PATH.'plugin/plugin.function.php') or die('Error: 0x0005');
if (!isset($_GET['id']) || $_GET['id'] == '')
exit();
$controller = new StatisticController();
$controller->loadStatistics();
if (($name = $controller->getStatisticName($_GET['id'])) === false)
exit();
if (isset($_GET['plugin']) && trim($_GET['plugin']) != '')
pluginLanguage(trim($_GET['plugin']));
$builder = new StatisticBuilder();
$builder->loadFromFile($name, (isset($_GET['plugin']) && trim($_GET['plugin']) != '') ? $_GET['plugin'] : NULL);
$statistic = $builder->getArray();
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$statistic['title'].".csv");
header("Pragma: no-cache");
header("Expires: 0");
$log = new LogStatistic();
$log->setFile(LOG_PATH.$statistic['raw'].'.csv');
function convertTimestampToISO(&$value, $key)
{
$value[0] = date('c', trim($value[0]));
}
$header = array();
foreach ($statistic['columns'] as $column)
$header[] = _t($column['downloadTitle']);
$output = fopen('php://output', 'w');
$data = $log->getAll();
array_walk($data, 'convertTimestampToISO');
fputcsv($output, $header);
foreach ($data as $entry)
fputcsv($output, $entry);
fclose($output);
?>

16
api/v1/users_groups.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
define('PICONTROL', true);
(include_once realpath(dirname(__FILE__)).'/../../resources/init.php') or die('Error: 0x0000');
(include_once LIBRARY_PATH.'api/api.class.php') or die('Error: 0x0001');
(include_once LIBRARY_PATH.'cache/cache.class.php') or die('Error: 0x0002');
$api = new API;
$users = new Cache('users', 'rpi_getAllUsers');
$users->load();
$api->addData('users', $users->getContent());
$api->addData('hint', $users->displayHint(true));
$api->display();
?>

10
api/versions.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
$apiLatest = array('versioncode' => 1);
$apiVersions = array();
$apiVersions[0] = array('versioncode' => 1,
'date' => 1443909600);
echo json_encode(array('versions' => $apiVersions, 'latest' => $apiLatest));
?>