Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('INSTALL_PATH')) {
- define('INSTALL_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . '/');
- }
- define("STATE_ERROR_UNKNOWN", 2147483647);
- if ($config['debug']) {
- $debug = array(
- 'current_script' => $_SERVER["SCRIPT_FILENAME"],
- 'REQUEST_URI' => @$_SERVER["REQUEST_URI"],
- 'BASE_URI' => dirname($_SERVER["PHP_SELF"]),
- 'DATABASE' => $config['db_name'],
- 'start_time' => date("F d Y H:i:s."),
- );
- }
- $appdata = [
- 'AlertType' => [
- ['id' => 0, 'Name' => 'Without notice', 'mcode' => 2], ['id' => 1, 'Name' => 'SIP', 'mcode' => 0], ['id' => 2, 'Name' => 'IP', 'mcode' => 1]
- ],
- 'SmartType' => [
- ['id' => 0, 'Name' => 'Unmanaged'], ['id' => 1, 'Name' => 'Smart']
- ],
- 'Phases' => [
- ['id' => 1, 'Name' => 'A'], ['id' => 2, 'Name' => 'B'], ['id' => 3, 'Name' => 'C']
- ],
- 'LinesType' => [
- ['id' => 1, 'Name' => 'Voltage monitoring'], ['id' => 2, 'Name' => 'Current control'], ['id' => 3, 'Name' => 'Dry contact'], ['id' => 4, 'Name' => 'Phase']
- ],
- 'Switches' => [
- ['id' => 1, 'Name' => 'S1'], ['id' => 2, 'Name' => 'S2'], ['id' => 3, 'Name' => 'S3'], ['id' => 4, 'Name' => 'S4']
- ],
- 'AutoModes' => [
- ['id' => 1, 'Name' => 'Shedule'], ['id' => 2, 'Name' => 'Ligh sensor'], /*['id'=>3,'Name'=>'Auto3'],*/
- ['id' => 4, 'Name' => 'Manual']
- ],
- 'SubModes' => [
- ['id' => 0, 'Name' => 'Master'], ['id' => 1, 'Name' => 'Slave']
- ],
- 'NotifyFreqs' => [
- ['id' => 1, 'Name' => 'Everyday'], ['id' => 2, 'Name' => 'Every event']
- ],
- 'Meter' => [
- 'CNT1A' => ['type' => 'decimal'],
- 'CNT2A' => ['type' => 'decimal'],
- 'CNT3A' => ['type' => 'decimal'],
- 'CNT4A' => ['type' => 'decimal'],
- 'CNAO' => ['type' => 'decimal'],
- 'CNAO-' => ['type' => 'decimal'],
- 'CNT1A-' => ['type' => 'decimal'],
- 'CNT2A-' => ['type' => 'decimal'],
- 'CNT3A-' => ['type' => 'decimal'],
- 'CNT4A-' => ['type' => 'decimal'],
- 'CNT1RO-' => ['type' => 'decimal'],
- 'CNT2RO-' => ['type' => 'decimal'],
- 'CNT3RO-' => ['type' => 'decimal'],
- 'CNT4RO-' => ['type' => 'decimal'],
- 'CNRO-' => ['type' => 'decimal'],
- 'CNRO' => ['type' => 'decimal'],
- 'CNT1SO' => ['type' => 'decimal'],
- 'CNT2SO' => ['type' => 'decimal'],
- 'CNT3SO' => ['type' => 'decimal'],
- 'CNPAO' => ['type' => 'decimal'],
- 'CNT4SO' => ['type' => 'decimal'],
- 'CNT1RO' => ['type' => 'decimal'],
- 'CNT2RO' => ['type' => 'decimal'],
- 'CNPRO' => ['type' => 'decimal'],
- 'CNPRO-' => ['type' => 'decimal'],
- 'CNPA' => ['type' => 'decimal'],
- 'CNPB' => ['type' => 'decimal'],
- 'CNPC' => ['type' => 'decimal'],
- 'CNPO' => ['type' => 'decimal'],
- 'CNT3RO' => ['type' => 'decimal'],
- 'CNT4RO' => ['type' => 'decimal'],
- 'CNCOS' => ['type' => 'decimal'],
- 'CNSO' => ['type' => 'decimal'],
- 'CNVA' => ['type' => 'decimal'],
- 'CNVB' => ['type' => 'decimal'],
- 'CNVC' => ['type' => 'decimal'],
- 'CNCURA' => ['type' => 'decimal'],
- 'CNCURB' => ['type' => 'decimal'],
- 'CNCURC' => ['type' => 'decimal'],
- 'CNCOSA' => ['type' => 'decimal'],
- 'CNCOSB' => ['type' => 'decimal'],
- 'CNCOSC' => ['type' => 'decimal'],
- 'CNTYPE' => ['type' => 'str'],
- 'CNTEMP' => ['type' => 'str'],
- 'C' => ['type' => 'decimal'],
- 'V' => ['type' => 'int'],
- 'W' => ['type' => 'decimal'],
- 'L' => ['type' => 'int'],
- 'R' => ['type' => 'str'],
- ],
- ];
- function guardIp(PDO $db, int $userId, string $role, ?string $login = null): bool
- {
- if ($login === 'odmin') {
- return true;
- }
- $ip = $_SERVER['REMOTE_ADDR'];
- if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
- $ip = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]);
- }
- $token = $_COOKIE['ast_dev'] ?? null;
- $st = $db->prepare('SELECT legit_ips, dev_token FROM user WHERE id=? FOR UPDATE');
- $st->execute([$userId]);
- $u = $st->fetch(PDO::FETCH_ASSOC) ?: [];
- $ips = json_decode($u['legit_ips'] ?: '[]', true);
- if (!in_array($ip, $ips, true) && count($ips) < 10) {
- $ips[] = $ip;
- if (!$u['dev_token']) {
- $token = bin2hex(random_bytes(32));
- setcookie('ast_dev', $token, [
- 'expires' => time() + 90 * 86400,
- 'path' => '/',
- 'secure' => true,
- 'httponly' => true,
- 'samesite' => 'Lax'
- ]);
- } else {
- $token = $u['dev_token'];
- }
- $db->prepare('UPDATE user SET legit_ips=?, dev_token=? WHERE id=?')
- ->execute([json_encode($ips), $token, $userId]);
- return true;
- }
- if (in_array($ip, $ips, true)) {
- return true;
- }
- if ($token && $token === $u['dev_token']) {
- if (!in_array($ip, $ips, true) && count($ips) < 10) {
- $ips[] = $ip;
- $db->prepare('UPDATE user SET legit_ips=? WHERE id=?')
- ->execute([json_encode($ips), $userId]);
- }
- return true;
- }
- session_unset();
- session_destroy();
- header('Location: ./?do=login&iperr=1');
- exit;
- }
- spl_autoload_register('ods_autoload');
- require_once(INSTALL_PATH . "program/lib/twig_autoload.php");
- function ods_autoload($classname)
- {
- if (strpos($classname, 'ods') === 0) {
- $filepath = INSTALL_PATH . "program/include/$classname.php";
- if (is_readable($filepath)) {
- include_once $filepath;
- return true;
- }
- }
- return false;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment