select_db('share'); $ZanoreniMax = 8; // Maximální úroven zanoreni $MaxProgress = 70; // Pocet casti stavu prubehu operace $StartTime = time(); $MountDir = '/tmp/browse/host'; // Složka, kde se dočasně připojují síťové disky $TempDir = '/tmp/browse/'; // Složka, kde se dočasně připojují síťové disky if (!is_dir($TempDir)) mkdir($TempDir, 0777); if (!is_dir($MountDir)) mkdir($MountDir, 0777); // Nacteni seznamu sdileni $Host = strtoupper(getenv('browse_host')); if ($Host=='') die("Musite nastavit browse_host!\n"); $HostID = getenv('browse_id'); if ($HostID=='') die("Musite nastavit browse_id!\n"); //echo("\n================== Prochazec sdileni =================\n\n"); echo("Nacitam seznam sdileni pro ".$Host.'('.$HostID.')...'); $Output = array(); exec($SambaBinDir.'smbclient -d0 -N -L='.$Host,$Output); echo("OK\n"); if ($Output[0]=='Connection to '.$Host.' failed') die('Pocitac '.$Host." nenalezen!\n"); $Output = array_slice($Output,3); $Shares = array(); foreach ($Output as $Radek) { if ($Radek=='') break; //$Radek = iconv('UTF-8','ISO-8859-2',$Radek); $Title = trim(substr($Radek,1,16)); $Type = trim(substr($Radek,16,10)); $Desc = trim(substr($Radek,26)); //echo($Title.','.$Type.','.$Desc."\n"); if (($Type=='Disk') && (substr($Title,-1,1)!='$')) array_push($Shares,$Title); //ShowArray($Radek); } //ShowArray($Shares); // Analyzuj slozku a projdi podslozky function Vetev($Cesta,$Zanoreni) { global $Citac, $Soubor, $Host, $HostID, $Share, $ZanoreniMax, $Pocet, $Hotovo, $MountDir, $TempDir, $FileSize, $MaxProgress, $Database; $VelikostSlozky = 0; //$Vlozit = ''; // Nacti parent ID $DbResult = $Database->query('SELECT LAST_INSERT_ID() FROM NetworkShareItem LIMIT 0,1'); //echo(DB_NumRows()); $Row = $DbResult->fetch_array(); $Parent = $Row[0]; //echo($Cesta."\n"); $Output = array(); $Cesta2 = fgets($Soubor,1024); fgets($Soubor,1024); $Files = array(); do { $Nazev = fgets($Soubor,1024); //echo(strlen($Nazev).","); array_push($Files,$Nazev); } while (($Nazev != "\n") && (!feof($Soubor))); array_pop($Files); //if (($Pocet-floor($Pocet/10)*10)==9) $Hotovo = floor(ftell($Soubor) / $FileSize * $MaxProgress); if ($Hotovo >= $Pocet) { echo(str_repeat('#', $Hotovo-$Pocet)); $Pocet = $Hotovo; } //echo('['.memory_get_usage().'] '.$Cesta."\n"); foreach ($Files as $Radek) { //echo($Radek.""); $Radek = substr($Radek,0,-1); //echo($Radek.",\n"); if ($Radek[0] == 'd') $Type = 2; else $Type = 0; $Prava = substr($Radek,0,strpos($Radek,' ')); $Radek = ltrim(substr($Radek,strlen($Prava)+1)); $Neco = substr($Radek,0,strpos($Radek,' ')); $Radek = ltrim(substr($Radek,strlen($Neco)+1)); $User = substr($Radek,0,strpos($Radek,' ')); $Radek = ltrim(substr($Radek,strlen($User)+1)); $Group = substr($Radek,0,strpos($Radek,' ')); $Radek = ltrim(substr($Radek,strlen($Group)+1)); $Velikost = substr($Radek,0,strpos($Radek,' ')); $VelikostSlozky += $Velikost; $Radek = ltrim(substr($Radek,strlen($Velikost)+1)); $Date = substr($Radek,0,strpos($Radek,' ')); $Radek = ltrim(substr($Radek,strlen($Date)+1)); $Time = substr($Radek,0,strpos($Radek,' ')); $Nazev = ltrim(substr($Radek,strlen($Time)+1)); //echo($Date.','.$Time.','.$Radek.' '); if ((strpos($Nazev,'.') > 0) && ($Type != 2)) { $Ext = substr($Nazev,strrpos($Nazev,'.')+1); $Nazev = substr($Nazev,0,strrpos($Nazev,'.')); } else $Ext = ''; //$Vlozit .= "INSERT INTO NetworkShareItem (name,size,date,ext,parent,type,host) ". //"VALUES ('$Nazev','$Velikost',NOW(),'$Ext','$Parent','$Type','$HostID'); "; $Database->insert('NetworkShareItem', array( 'name' => $Nazev, 'size' => $Velikost, 'date' => $Date.' '.$Time, //'date' => '', 'ext' => $Ext, 'parent' => $Parent, 'type' => $Type, 'host' => $HostID, )); $Citac = $Citac + 1; // Pokud jde o sloľku, tak projdi jeji obsah a aktualizuj velikost if ($Type == 2) { //echo($Vlozit); // if ($Vlozit!='') DB_Query($Vlozit); // Vloz vsechny polozky // $Vlozit = ''; $VelikostSlozky += Vetev($Cesta.$Nazev.'/',$Zanoreni+1); } } //echo($Vlozit); //if ($Vlozit!='') DB_Query($Vlozit); // Vloz vsechny polozky // Aktualizuj velikost //DB_Query("SELECT SUM(size) FROM NetworkShareItem WHERE (parent=".$Parent.")"); //$Row = DB_Row(); $Database->update('NetworkShareItem', 'id='.$Parent, array('size' => $VelikostSlozky)); //closedir($Dir); return $VelikostSlozky; } // Proskenuj jednotlive sdileni $Database->query('DELETE FROM NetworkShareItem WHERE host='.$HostID); // Smaz predchozi polozky // Zapis nazev pocitace $Database->insert('NetworkShareItem',array( 'name' => $Host, 'size' => 0, 'date' => 'NOW()', 'ext' => '', 'parent' => 1, 'type' => 2, 'host' => $HostID, )); // Nacti Parent ID $DbResult = $Database->query('SELECT LAST_INSERT_ID() FROM NetworkShareItem LIMIT 0,1'); $Row = $DbResult->fetch_array(); $Parent = $Row[0]; foreach ($Shares as $Share) { $Pocet = 0; // Načtení seznamu souborů sdílené jednotky $ShareFull = '//'.$Host.'/'.$Share; echo("Načítám strom sdílení ".$Share."..."); unset($Output); passthru($SambaSbinDir."mount.cifs '".$ShareFull."' ".$MountDir.' -o guest codepage=cp852 iocharset=iso8859-2'); // echo($SambaSbinDir."mount.cifs '".$ShareFull."' ".$MountDir.' -o guest,codepage=cp852,iocharset=iso8859-2'); passthru('ls -A -R -X -l --time-style=+"%Y-%m-%d %I:%M:%S" '.$MountDir.'>'.$TempDir.$Host.'.list 2>'.$TempDir.'errors/'.$Host.'_'.$Share.'.err',$Result); //if ($Result==0) //{ echo("OK\n"); if (file_exists($TempDir.$Host.'.list')) { // Přidej poloľku sdílení do datanáze $Database->insert('NetworkShareItem',array( 'name' => $Share, 'size' => 0, 'date' => 'NOW()', 'ext' => '', 'parent' => $Parent, 'type' => 2, 'host' => $HostID, )); // Analýza souboru echo("Analyzuji obsah...\n["); $FileSize = filesize($TempDir.$Host.'.list'); $Soubor = fopen($TempDir.$Host.'.list','r'); $Citac = 0; Vetev('/', 0); // Skenuj sdileni fclose($Soubor); // Uzavřít soubor unlink($TempDir.$Host.'.list'); // Smazat soubor stromu adresářové struktury //echo(str_repeat('#',40-$Pocet)); echo("] $Citac\n"); //} else echo("CHYBA\n"); } else echo('Soubor sdílení nenalezen!'); passthru('umount '.$MountDir); // Odpojit sitovy disk } // Aktualizuj velikost dat pocitace $DbResult = $Database->query("SELECT SUM(size) FROM NetworkShareItem WHERE parent=".$Parent); $Row = $DbResult->fetch_array(); $Database->update('NetworkShareItem','id='.$Parent,array('size' => $Row[0])); // Zaktivuj strom echo("Provadim aktivaci nove naskenovaneho stromu..."); $Database->select_db('is'); $DbResult = $Database->query("SELECT id FROM hosts WHERE name='".$Host."'"); $Row = $DbResult->fetch_array(); $NewID = $Row['id']; $Database->select_db('share'); $Database->query("DELETE FROM NetworkShareItem WHERE host=".$NewID); $Database->update('NetworkShareItem','host='.$HostID,array('host' => $NewID)); echo("OK\n"); // Spočítej celkové obsazené místo $Database->select_db('is'); $DbResult = $Database->query('SELECT id FROM hosts'); $Vyber = ''; while ($Row = $DbResult->fetch_array()) $Vyber .= $Row['id'].','; $Database->select_db('share'); $DbResult = $Database->query("SELECT SUM(size) FROM NetworkShareItem WHERE ((parent=1) AND (host IN (".substr($Vyber,0,-1).')))'); $Row = $DbResult->fetch_array(); $Database->update('NetworkShareItem','id=1',array('size' => $Row[0])); // Zapiš chyby do databáze echo("Chyby sdílení...\n"); $Database->delete('NetworkShareError', 'host="'.$Host.'"'); if (is_dir($TempDir.'errors')) { $Dir = scandir($TempDir.'errors'); foreach ($Dir as $File) { if (substr($File, 0, strpos($File, '_')) == $Host) { $Share = substr($File, strpos($File, '_')+1, -4); $ShareFull = '//'.$Host.'/'.$Share; if (filesize($TempDir.'errors/'.$File) > 0) { $ErrorFile = fopen($TempDir.'errors/'.$File, 'r+'); while (!feof($ErrorFile)) { $Row = fgets($ErrorFile); if ($Row != '') { $Row = substr($ShareFull.'/'.substr($Row, 39), 0, -1); $Database->insert('NetworkShareError', array('host' => $Host, 'message' => $Row)); echo($Row."\n"); } } fclose($ErrorFile); } } } } echo("Doba procházení: ".date("H:i:s", time() - $StartTime - 3600)."\n\n");