source: trunk/Modules/NetworkShare/browse.php

Last change on this file was 929, checked in by chronos, 3 years ago
  • Modified: Removed commended out print_r and echo commands used only for debugging.
File size: 8.6 KB
Line 
1<?php
2include('../style.php');
3$SambaBinDir = '/usr/bin/';
4$SambaSbinDir = '/usr/sbin/';
5$Database->select_db('share');
6$ZanoreniMax = 8; // Maximální úroven zanoreni
7$MaxProgress = 70; // Pocet casti stavu prubehu operace
8$StartTime = time();
9$MountDir = '/tmp/browse/host'; // Složka, kde se dočasně připojují síťové disky
10$TempDir = '/tmp/browse/'; // Složka, kde se dočasně připojují síťové disky
11if (!is_dir($TempDir)) mkdir($TempDir, 0777);
12if (!is_dir($MountDir)) mkdir($MountDir, 0777);
13
14// Nacteni seznamu sdileni
15$Host = strtoupper(getenv('browse_host'));
16if ($Host=='') die("Musite nastavit browse_host!\n");
17$HostID = getenv('browse_id');
18if ($HostID=='') die("Musite nastavit browse_id!\n");
19//echo("\n================== Prochazec sdileni =================\n\n");
20echo("Nacitam seznam sdileni pro ".$Host.'('.$HostID.')...');
21$Output = array();
22exec($SambaBinDir.'smbclient -d0 -N -L='.$Host,$Output);
23echo("OK\n");
24
25if ($Output[0]=='Connection to '.$Host.' failed') die('Pocitac '.$Host." nenalezen!\n");
26$Output = array_slice($Output,3);
27$Shares = array();
28foreach ($Output as $Radek)
29{
30 if ($Radek=='') break;
31 //$Radek = iconv('UTF-8','ISO-8859-2',$Radek);
32 $Title = trim(substr($Radek,1,16));
33 $Type = trim(substr($Radek,16,10));
34 $Desc = trim(substr($Radek,26));
35 //echo($Title.','.$Type.','.$Desc."\n");
36 if (($Type=='Disk') && (substr($Title,-1,1)!='$')) array_push($Shares,$Title);
37 //ShowArray($Radek);
38}
39//ShowArray($Shares);
40
41// Analyzuj slozku a projdi podslozky
42function Vetev($Cesta,$Zanoreni)
43{
44 global $Citac, $Soubor, $Host, $HostID, $Share, $ZanoreniMax, $Pocet, $Hotovo, $MountDir, $TempDir, $FileSize, $MaxProgress, $Database;
45
46 $VelikostSlozky = 0;
47 //$Vlozit = '';
48 // Nacti parent ID
49 $DbResult = $Database->query('SELECT LAST_INSERT_ID() FROM NetworkShareItem LIMIT 0,1');
50 //echo(DB_NumRows());
51 $Row = $DbResult->fetch_array();
52 $Parent = $Row[0];
53//echo($Cesta."\n");
54 $Output = array();
55 $Cesta2 = fgets($Soubor,1024);
56 fgets($Soubor,1024);
57 $Files = array();
58 do {
59 $Nazev = fgets($Soubor,1024);
60 //echo(strlen($Nazev).",");
61 array_push($Files,$Nazev);
62 } while (($Nazev != "\n") && (!feof($Soubor)));
63 array_pop($Files);
64 //if (($Pocet-floor($Pocet/10)*10)==9)
65 $Hotovo = floor(ftell($Soubor) / $FileSize * $MaxProgress);
66 if ($Hotovo >= $Pocet)
67 {
68 echo(str_repeat('#', $Hotovo-$Pocet));
69 $Pocet = $Hotovo;
70 }
71 //echo('['.memory_get_usage().'] '.$Cesta."\n");
72 foreach ($Files as $Radek)
73 {
74 //echo($Radek."");
75 $Radek = substr($Radek,0,-1);
76 //echo($Radek.",\n");
77 if ($Radek[0] == 'd') $Type = 2; else $Type = 0;
78 $Prava = substr($Radek,0,strpos($Radek,' '));
79 $Radek = ltrim(substr($Radek,strlen($Prava)+1));
80 $Neco = substr($Radek,0,strpos($Radek,' '));
81 $Radek = ltrim(substr($Radek,strlen($Neco)+1));
82 $User = substr($Radek,0,strpos($Radek,' '));
83 $Radek = ltrim(substr($Radek,strlen($User)+1));
84 $Group = substr($Radek,0,strpos($Radek,' '));
85 $Radek = ltrim(substr($Radek,strlen($Group)+1));
86 $Velikost = substr($Radek,0,strpos($Radek,' '));
87 $VelikostSlozky += $Velikost;
88 $Radek = ltrim(substr($Radek,strlen($Velikost)+1));
89 $Date = substr($Radek,0,strpos($Radek,' '));
90 $Radek = ltrim(substr($Radek,strlen($Date)+1));
91 $Time = substr($Radek,0,strpos($Radek,' '));
92 $Nazev = ltrim(substr($Radek,strlen($Time)+1));
93 //echo($Date.','.$Time.','.$Radek.' ');
94
95 if ((strpos($Nazev,'.') > 0) && ($Type != 2))
96 {
97 $Ext = substr($Nazev,strrpos($Nazev,'.')+1);
98 $Nazev = substr($Nazev,0,strrpos($Nazev,'.'));
99 } else $Ext = '';
100 //$Vlozit .= "INSERT INTO NetworkShareItem (name,size,date,ext,parent,type,host) ".
101 //"VALUES ('$Nazev','$Velikost',NOW(),'$Ext','$Parent','$Type','$HostID'); ";
102
103 $Database->insert('NetworkShareItem', array(
104 'name' => $Nazev,
105 'size' => $Velikost,
106 'date' => $Date.' '.$Time,
107 //'date' => '',
108 'ext' => $Ext,
109 'parent' => $Parent,
110 'type' => $Type,
111 'host' => $HostID,
112 ));
113 $Citac = $Citac + 1;
114 // Pokud jde o sloľku, tak projdi jeji obsah a aktualizuj velikost
115 if ($Type == 2)
116 {
117 //echo($Vlozit);
118 // if ($Vlozit!='') DB_Query($Vlozit); // Vloz vsechny polozky
119 // $Vlozit = '';
120 $VelikostSlozky += Vetev($Cesta.$Nazev.'/',$Zanoreni+1);
121 }
122 }
123 //echo($Vlozit);
124 //if ($Vlozit!='') DB_Query($Vlozit); // Vloz vsechny polozky
125
126 // Aktualizuj velikost
127 //DB_Query("SELECT SUM(size) FROM NetworkShareItem WHERE (parent=".$Parent.")");
128 //$Row = DB_Row();
129 $Database->update('NetworkShareItem', 'id='.$Parent, array('size' => $VelikostSlozky));
130 //closedir($Dir);
131 return $VelikostSlozky;
132}
133
134// Proskenuj jednotlive sdileni
135$Database->query('DELETE FROM NetworkShareItem WHERE host='.$HostID); // Smaz predchozi polozky
136
137// Zapis nazev pocitace
138$Database->insert('NetworkShareItem',array(
139 'name' => $Host,
140 'size' => 0,
141 'date' => 'NOW()',
142 'ext' => '',
143 'parent' => 1,
144 'type' => 2,
145 'host' => $HostID,
146));
147
148// Nacti Parent ID
149$DbResult = $Database->query('SELECT LAST_INSERT_ID() FROM NetworkShareItem LIMIT 0,1');
150$Row = $DbResult->fetch_array();
151$Parent = $Row[0];
152
153foreach ($Shares as $Share)
154{
155 $Pocet = 0;
156
157 // Načtení seznamu souborů sdílené jednotky
158 $ShareFull = '//'.$Host.'/'.$Share;
159 echo("Načítám strom sdílení ".$Share."...");
160 unset($Output);
161 passthru($SambaSbinDir."mount.cifs '".$ShareFull."' ".$MountDir.' -o guest codepage=cp852 iocharset=iso8859-2');
162// echo($SambaSbinDir."mount.cifs '".$ShareFull."' ".$MountDir.' -o guest,codepage=cp852,iocharset=iso8859-2');
163 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);
164 //if ($Result==0)
165 //{
166 echo("OK\n");
167
168 if (file_exists($TempDir.$Host.'.list'))
169 {
170 // Přidej poloľku sdílení do datanáze
171 $Database->insert('NetworkShareItem',array(
172 'name' => $Share,
173 'size' => 0,
174 'date' => 'NOW()',
175 'ext' => '',
176 'parent' => $Parent,
177 'type' => 2,
178 'host' => $HostID,
179 ));
180
181 // Analýza souboru
182 echo("Analyzuji obsah...\n[");
183 $FileSize = filesize($TempDir.$Host.'.list');
184 $Soubor = fopen($TempDir.$Host.'.list','r');
185 $Citac = 0;
186 Vetev('/', 0); // Skenuj sdileni
187 fclose($Soubor); // Uzavřít soubor
188 unlink($TempDir.$Host.'.list'); // Smazat soubor stromu adresářové struktury
189 //echo(str_repeat('#',40-$Pocet));
190 echo("] $Citac\n");
191 //} else echo("CHYBA\n");
192 } else echo('Soubor sdílení nenalezen!');
193 passthru('umount '.$MountDir); // Odpojit sitovy disk
194}
195
196// Aktualizuj velikost dat pocitace
197$DbResult = $Database->query("SELECT SUM(size) FROM NetworkShareItem WHERE parent=".$Parent);
198$Row = $DbResult->fetch_array();
199$Database->update('NetworkShareItem','id='.$Parent,array('size' => $Row[0]));
200
201// Zaktivuj strom
202echo("Provadim aktivaci nove naskenovaneho stromu...");
203$Database->select_db('is');
204$DbResult = $Database->query("SELECT id FROM hosts WHERE name='".$Host."'");
205$Row = $DbResult->fetch_array();
206$NewID = $Row['id'];
207$Database->select_db('share');
208$Database->query("DELETE FROM NetworkShareItem WHERE host=".$NewID);
209$Database->update('NetworkShareItem','host='.$HostID,array('host' => $NewID));
210echo("OK\n");
211
212// Spočítej celkové obsazené místo
213$Database->select_db('is');
214$DbResult = $Database->query('SELECT id FROM hosts');
215$Vyber = '';
216while ($Row = $DbResult->fetch_array())
217 $Vyber .= $Row['id'].',';
218
219$Database->select_db('share');
220$DbResult = $Database->query("SELECT SUM(size) FROM NetworkShareItem WHERE ((parent=1) AND (host IN (".substr($Vyber,0,-1).')))');
221$Row = $DbResult->fetch_array();
222$Database->update('NetworkShareItem','id=1',array('size' => $Row[0]));
223
224// Zapiš chyby do databáze
225echo("Chyby sdílení...\n");
226$Database->delete('NetworkShareError', 'host="'.$Host.'"');
227if (is_dir($TempDir.'errors'))
228{
229 $Dir = scandir($TempDir.'errors');
230 foreach ($Dir as $File)
231 {
232 if (substr($File, 0, strpos($File, '_')) == $Host)
233 {
234 $Share = substr($File, strpos($File, '_')+1, -4);
235 $ShareFull = '//'.$Host.'/'.$Share;
236 if (filesize($TempDir.'errors/'.$File) > 0)
237 {
238 $ErrorFile = fopen($TempDir.'errors/'.$File, 'r+');
239 while (!feof($ErrorFile))
240 {
241 $Row = fgets($ErrorFile);
242 if ($Row != '')
243 {
244 $Row = substr($ShareFull.'/'.substr($Row, 39), 0, -1);
245 $Database->insert('NetworkShareError', array('host' => $Host, 'message' => $Row));
246 echo($Row."\n");
247 }
248 }
249 fclose($ErrorFile);
250 }
251 }
252 }
253}
254
255echo("Doba procházení: ".date("H:i:s", time() - $StartTime - 3600)."\n\n");
Note: See TracBrowser for help on using the repository browser.