#!/usr/bin/php
<?php
 function hosts_get($name) { $val = false; $s = file_get_contents('/etc/hosts'); foreach (explode("\n",$s) as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l) || $l[0] == '#') continue; $l = preg_replace('/\s+/', "\t", $l); $a = explode("\t",$l); if (empty($a[1])) continue; for ($n = 1; $n < count($a); $n++) { if ($a[$n] == $name) { $val = $a[0]; break; } } } return $val; } function get_apt_repos() { $arr = array(); $a = scandir('/etc/apt/sources.list.d'); array_push($a, '/etc/apt/sources.list'); foreach($a as $f) { if ($f == '.' || $f == '..') continue; if ($f != '/etc/apt/sources.list') { $f = '/etc/apt/sources.list.d/'.$f; } $s = file_get_contents($f); foreach (explode("\n",$s) as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l) || $l[0] == '#') continue; $l = preg_replace('/\s+/', "\t", $l); $a0 = explode('://',$l); $a = explode("\t",$a0[0]); if ($a[0] != 'deb' || empty($a0[1])) continue; $a = explode("\t",$a0[1]); $reposrv = $a[0]; $reponame = $a[1]; for($i = 2; $i < count($a); $i++) { if (empty($arr[$reposrv][$reponame])) { $arr[$reposrv][$reponame] = $a[$i]; } else { $arr[$reposrv][$reponame] .= ' '.$a[$i]; } } } } return $arr; } function get_bukts_cfg() { $cfg = array(); $file = '/etc/opt/bukts/conf/bukts.cfg'; if (!is_file($file)) { $file = '/etc/opt/bukts/bukts.cfg'; } if (!is_file($file)) { return $cfg; } foreach (explode("\n", file_get_contents($file)) as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l)) continue; if ($l[0] != '%') continue; $a = explode(' ', $l); if ($a[0] == '%include') { $ar = explode("\n", str_replace(array(' ',"\t","\r",'#',';'), array('','','',"\n#","\n#"), file_get_contents($a[1]))); $txt = ''; foreach ($ar as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l) || $l[0]=='#') continue; $txt .= $line."\n"; } $ar = explode("\n", str_replace(array(':'), array(":\n"), $txt)); $nn = 1; $sec = false; foreach ($ar as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l) || $l[0]=='#') continue; if ($l[0] == '[') { $b = explode(']',substr($l,1)); $sec = strtolower($b[0]); continue; } if ($sec === false) continue; $a = explode('=', $l); if (isset($a[1])) { $k = strtolower($a[0]); if ($sec == 'system') { $cfg[$sec][$k] = $a[1]; } else { $cfg[$sec][$nn][$k] = $a[1]; } } else if (strpos($l, ':') !== false) { $b = explode(':', $a[0]); $nn = $b[0]; } } } } return $cfg; } function get_cfg($file) { $cfg = array(); if (!is_file($file)) { return $cfg; } $ar = explode("\n", str_replace(array(' ',"\t","\r",'#',';'), array('','','',"\n#","\n#"), file_get_contents($file))); foreach ($ar as $line) { if (empty($line)) continue; $l = trim($line); if (empty($l) || $l[0]=='#') continue; $a = explode('=', $l); if (isset($a[1])) { $k = strtolower($a[0]); $cfg[$k] = $a[1]; } } return $cfg; } function parse_cmdline($argv, $argnames = false) { $args = $argnames !== false ? $argnames : array( 'f' => 'file', 'e' => 'exec', ); $arr = array( '_args' => array() ); $arr['_dirname'] = dirname($argv[0]); $arr['_basename'] = basename($argv[0]); $arr['_progname'] = basename($argv[0],'.php'); for ($i = 1; $i < count($argv); $i++) { $v = $argv[$i]; $len = strlen($v); if ($len < 2 || $v[0] != '-') { array_push($arr['_args'], $v); continue; } if ($v[1] == '-') { if ($len == 2) { continue; } $key = substr($v, 2); } else { if (empty($args[$v[1]])) { $key = $v[1]; } else { $key = $args[$v[1]]; } if ($len > 2) { $arr[$key] = substr($v, 2); continue; } } if (isset($argv[$i+1]) && isset($argv[$i+1][0]) && $argv[$i+1][0] != '-') { $arr[$key] = $argv[$i+1]; $i++; } else { $arr[$key] = true; } } return $arr; } function mk_azs_jcfg() { $TZ = 3*3600; $curdatetime = time(); list($date,$time) = explode('_', gmdate('dmY_His', $curdatetime+$TZ)); $alldata = array( 'cur' => array( 'date' => $date, 'time' => $time, 'tm' => $curdatetime, 'tz' => $TZ, ), 'apt' => get_apt_repos(), ); $bukwserver = hosts_get('bukserver'); if (!empty($bukwserver)) { $alldata['etc']['hosts']['bukserver'] = $bukwserver; } $alldata['cfg']['bukts'] = get_bukts_cfg(); $alldata['cfg']['bukazs'] = array_merge( get_cfg('/etc/opt/bukazs/bukazs.cfg'), get_cfg('/etc/opt/bukazs/bukazs.scfg') ); $alldata['cfg']['bukwserver']['pserver'] = get_cfg("/etc/opt/bukwserver/pserver.cfg"); $alldata['cfg']['bukwserver']['payterm'] = get_cfg("/opt/bukwserver/html/modules/payterm/cfg/payterm.cfg"); $cmds = array( array( 'root' => 'dpkg', 'cmd' => '/usr/bin/dpkg -l | grep -e buk -e siebel -e sbspsb | grep ^ii | awk \'{print $2,$3}\''), array( 'root' => 'linux', 'par' => 'release', 'cmd' => 'lsb_release -r | cut -d: -f2'), array( 'root' => 'linux', 'par' => 'kernel', 'cmd' => 'uname -r'), array( 'root' => 'system', 'sec' => 'interface', 'par' => 'hwaddr', 'cmd' => '/sbin/ifconfig -a | grep HWaddr | sed -e "s/^.*HWaddr //"'), array( 'root' => 'system', 'sec' => 'interface', 'par' => 'addr', 'cmd' => '/sbin/ifconfig -a | grep "inet addr" | grep -v "127.0.0" | sed -e "s/^.*inet addr://" | cut -d\' \' -f1'), array( 'root' => 'system', 'par' => 'hostname', 'cmd' => 'hostname'), array( 'root' => 'psax', 'cmd' => 'for i in $(ps -ax | grep -e openbox -e fluxbox -e yandex -e siebel -e "x-www-browser" | awk \'{print $5}\'); do basename $i; done'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'system', 'cmd' => 'cat /etc/issue'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'kernel', 'cmd' => 'uname -a'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'ip', 'cmd' => '/sbin/ifconfig | grep -P "(^eth)|(^ens)" -A 1 | grep "inet"'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'placecount', 'cmd' => 'cat /etc/opt/bukts/bukts-wplaces.cfg 2>/dev/null | grep -ci ^HOST'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'whereconf', 'cmd' => 'test -f /etc/opt/bukts/conf/.enabled && echo DB || echo files'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'lmcz_version', 'cmd' => 'php /opt/bukts-chznak/get_lm_version.php VERSION'), array( 'root' => 'feodor', 'sec' => 'compinfo', 'par' => 'lmcz_status', 'cmd' => 'php /opt/bukts-chznak/get_lm_version.php STATUS') ); foreach ($cmds as $av) { exec($av['cmd'], $e); if (!empty($e)) { foreach ($e as $v) { $v = trim(preg_replace('/[\s]{2,}/', ' ', $v)); if (empty($v)) continue; if (!empty($av['par'])) { $param = $av['par']; $value = $v; } else { $t = explode(' ',$v,2); $param = $t[0]; $value = isset($t[1]) ? $t[1] : true; } $root = $av['root']; $sec = isset($av['sec']) ? $av['sec'] : false; if ($sec === false) { if (!isset($alldata[$root][$param])) { $alldata[$root][$param] = $value; } else { $alldata[$root][$param] = "\t".$value; } } else { if (!isset($alldata[$root][$sec][$param])) { $alldata[$root][$sec][$param] = $value; } else { $alldata[$root][$sec][$param] .= "\t".$value; } } } unset($e); } } if (!empty($alldata['cfg']['bukwserver']['pserver']['placenum'])) { $placenum = $alldata['cfg']['bukwserver']['pserver']['placenum']; $host = $alldata['cfg']['bukwserver']['pserver']['host']; } else { $placenum = 0; $host = 'bukserver'; } $alldata['global']['wpnum'] = $placenum; $alldata['global']['host'] = $host; $alldata['global']['is_srv'] = !empty($alldata['cfg']['bukts']['wplaces']); if (!empty($placenum)) { if (!empty($alldata['dpkg']['bukshop-payterm'])) { $alldata['global']['wptyp'] = 'TSO-Z'; } else if (!empty($alldata['dpkg']['bukwserver-payterm'])) { $alldata['global']['wptyp'] = 'TSO-G'; } else if (!empty($alldata['dpkg']['bukwserver-payterm'])) { $alldata['global']['wptyp'] = 'wplace'; } else { $alldata['global']['wptyp'] = 'undefined'; } } else if (!empty(['cfg']['bukts']['wplaces'])) { $alldata['global']['wptyp'] = 'server'; } else { $alldata['global']['wptyp'] = 'undefined'; } if (true) { $DB_SERVER = $alldata['global']['host']; $_DBCONN_ = pg_connect("host=$DB_SERVER port=5432 dbname=ubuk user=bukts password=''"); $names = array( 'ident' => array('root' => 'db', 'table' => 'sd_ident', 'fields' => 'id,title,num_exploit,num_proprietor,num_company,num_owner,num_num,azs_country,azs_city,azs_zip,azs_street,azs_addr', 'tail' => 'where deleted=false and id_shop_type=0' ), 'shift' => array('root' => 'cur', 'table' => 'sj_shifts', 'fields' => 'num,time_beg', 'tail' => 'order by num desc limit 1' ), 'tranz' => array('root' => 'cur', 'table' => 'sj_tranz', 'fields' => 'trannum,timebeg', 'tail' => 'order by trannum desc limit 1' ), 'payment' => array('root' => 'azscfg', 'table' => 'sd_azspayms', 'index' => 'paycod', 'fields' => 'paycod,time_beg,npp,flg,bnalnum,set,color,cardtyp,posadr,round', 'tail' => 'where time_end is null' ), 'product' => array('root' => 'azscfg', 'table' => 'pd_azsprods', 'index' => 'prodcod', 'fields' => 'prodcod,time_beg,npp,frsec,color', 'tail' => 'where time_end is null' ), 'pump' => array('root' => 'azscfg', 'table' => 'pd_azspumps', 'index' => 'trk', 'fields' => 'trk,time_beg,buk,lkanal,pkanal,adr,tank1,tank2,tank3,tank4,tank5,minaus,real,drv,par,typ', 'tail' => 'where time_end is null' ), 'tank' => array('root' => 'azscfg', 'table' => 'pd_tanks_list', 'index' => 'id', 'fields' => 'id,tank_time_beg,id_products,pasp_vol,deadv,deadl,maxv,zond_nm,num_serial', 'tail' => 'where tank_time_end is null' ), 'payments' => array('root' => 'db', 'table' => 'sd_payments', 'index' => 'paycod', 'fields' => 'paycod,paytyp,title', 'tail' => '' ), 'payments' => array('root' => 'db', 'table' => 'sd_payments', 'index' => 'paycod', 'fields' => 'paycod,paytyp,title', 'tail' => '' ), 'products' => array('root' => 'db', 'table' => 'pd_products', 'index' => 'prodcod', 'fields' => 'prodcod,prodtyp,title', 'tail' => '' ), ); foreach($names as $kn => $a0) { $rs = pg_query('select '.$a0['fields'].' from '.$a0['table'].' '.$a0['tail']); if ($rs != null && pg_num_rows($rs) > 0) { for ($ii = 0; $ii < pg_num_rows($rs); $ii++) { $ix = !empty($a0['index']) ? pg_fetch_result($rs, $ii, $a0['index']) : ''; $a1 = explode(',', $a0['fields']); foreach ($a1 as $v) { $val = trim(preg_replace('/[\s]{2,}/', ' ', @pg_fetch_result($rs, $ii, $v))); if ($val === '') continue; $root = $a0['root']; if (empty($ix)) { $alldata[$root][$kn][$v] = $val; } else { $alldata[$root][$kn][$ix][$v] = $val; } } } } } $rs = pg_query("select TO_CHAR(now(),'YYYY-MM-DD HH24:MI:SS') as curtm, sf_get_cur_ver() AS ver, version() as pgver"); if ($rs != null && pg_num_rows($rs) > 0) { $row = pg_fetch_array($rs); $alldata['db']['dbver'] = $row['ver']; $alldata['db']['pgver'] = $row['pgver']; $alldata['db']['curtm'] = $row['curtm']; } else { $alldata['db']['curtm'] = gmdate('Y-m-d H:i:s', $curdatetime); } $rs = pg_query('select * from j_data order by rtime'); if ($rs != null && pg_num_rows($rs) > 0) { for ($ii = 0; $ii < pg_num_rows($rs); $ii++) { $row = pg_fetch_array($rs); $num = !empty($row['num']) ? $row['num'] : 0; $wpn = !empty($row['wpnum']) ? $row['wpnum'].':' : '0'; $dat = $row['dat']; $sec = $row['sec']; $par = $row['par']; $val = $row['val']; $alldata['jdata'][$dat][$sec][$par] = $val; if (empty($num)) { $alldata['jdata'][$dat][$sec][$wpn][$par] = $val; } else { $alldata['jdata'][$dat][$sec][$wpn][$num][$par] = $val; } } } } return $alldata; } $USER = get_current_user(); if ($USER == 'root') $USER = 'bukts'; $argnames = array( 'g' => 'get', 'd' => 'debug' ); $cmdline = parse_cmdline($argv, $argnames); $DEBUG = !empty($cmdline['debug']) ? $cmdline['debug'] : false; $alldata = mk_azs_jcfg(); if ($DEBUG === 'print_r') print_r($alldata); if (!empty($cmdline['get'])) { echo json_encode($alldata)."\n"; } if (empty($cmdline['get'])) { $placenum = $alldata['global']['wpnum']; $dbDateTime = $alldata['db']['curtm']; $len = 'ARRAY['; foreach ($alldata['dpkg'] as $pkg => $ver) { $sql = "SELECT ldict.sf_do_compinfo_set_param(sf_get_cur_id_shop()::integer, 0, $placenum, 1, '$pkg', '$ver')"; $len = $len."'$pkg',"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } } $len = trim($len,','); $len = $len."]"; $sql = "SELECT ldict.sf_do_compinfo_del_param(sf_get_cur_id_shop()::integer, 0, $placenum, 1, $len)"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } $len = 'ARRAY['; foreach (array('BUKBASEVER' => $alldata['db']['dbver'], 'POSTGRES' => $alldata['db']['pgver'], 'SERVERIP' => $alldata['global']['host'], 'SYSTEM' => $alldata['feodor']['compinfo']['system'], 'KERNEL' => $alldata['feodor']['compinfo']['kernel'], 'IP' => !empty($alldata['feodor']['compinfo']['ip']) ? $alldata['feodor']['compinfo']['ip'] : '', 'PLACECOUNT' => $alldata['feodor']['compinfo']['placecount'], 'WHERECONF' => $alldata['feodor']['compinfo']['whereconf'], 'LMCZ_VERSION' => $alldata['feodor']['compinfo']['lmcz_version'], 'LMCZ_STATUS' => $alldata['feodor']['compinfo']['lmcz_status'] ) as $k => $v) { if (empty($v)) continue; $sql = "SELECT ldict.sf_do_compinfo_set_param(sf_get_cur_id_shop()::integer, 0, $placenum, 2, '$k', '$v')"; $len = $len."'$k',"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } } if (!empty($alldata['cfg']['bukts']['buk'])) { foreach (array( 'PLACECOUNT2' => 'select count(*) from sd_placelist', 'WAITMSG' => 'with a as (select sf_get_cur_id_shop() as id_shop) select count(*) from gj_send as d,a where d.id_shop=a.id_shop AND done=false and ask=false', 'TABLECOUNT' => "SELECT count(*) FROM information_schema.tables WHERE table_schema NOT IN ('information_schema','pg_catalog') and table_type='BASE TABLE'", ) as $k => $v) { $rs = pg_query($v); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } if ($rs != null && pg_num_rows($rs) > 0) { $val = pg_fetch_result($rs, 0, 0); $sql = "SELECT ldict.sf_do_compinfo_set_param(sf_get_cur_id_shop()::integer, 0, $placenum, 2, '$k', '$val')"; $len = $len."'$k',"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } } } } $len = trim($len,','); $len = $len."]"; $sql = "SELECT ldict.sf_do_compinfo_del_param(sf_get_cur_id_shop()::integer, 0, $placenum, 2, $len)"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } $sql = "SELECT ldict.sf_do_compinfo_set_param(sf_get_cur_id_shop()::integer, 0, $placenum, 0, 'lasttime', TO_CHAR(now(),'YYYY-MM-DD HH24:MI:SS'))"; $rs = pg_query($sql); if ($DEBUG === 'sql') { echo "$sql\n"; if (empty($rs)) echo "SQL REQUEST ERROR\n"; } } ?>
