viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Threading;
  5. using System.IO.Compression;
  6. using System.Text.RegularExpressions;
  7. using System.IO;
  8. using Tamir.SharpSsh;
  9. using System.Data.SQLite;
  10. using Dizzy;
  11.  
  12. namespace Dizzy
  13. {
  14.  
  15.  
  16. public class ProtocolException : ApplicationException
  17. {
  18. public ProtocolException () {}
  19. public ProtocolException (string Message) {}
  20. }
  21.  
  22. public class Protocol
  23. {
  24.  
  25. // I transfer attivi
  26. ArrayList transfers = new ArrayList ();
  27.  
  28. public delegate void Del (string src, string dest,
  29. int transferredBytes, int totalBytes, string message);
  30.  
  31.  
  32. ArrayList threads = new ArrayList ();
  33.  
  34. Thread finder, listUpdater;
  35.  
  36. // Dati letti dal thread che cerca per capire cosa cercare
  37. string keyword;
  38. FileTreeView fileTreeView;
  39.  
  40. GlobalConfig config;
  41.  
  42. public delegate void ProtocolEvent ();
  43.  
  44. // Questo evento sembra non dare problemi perché ogni volta che
  45. // è emesso aspettiamo che l'utente inserisca i dati necessari
  46. // con while(!this.config.authenticated) {}
  47. public event ProtocolEvent AuthenticationRequired;
  48.  
  49. public Protocol (ref GlobalConfig config)
  50. {
  51. this.config = config;
  52. this.UpdateFileList ();
  53. }
  54.  
  55. public void Search(string keyword, ref FileTreeView f)
  56. {
  57. if (this.finder != null && this.finder.IsAlive)
  58. {
  59. Log.Info ("Ricerca in corso");
  60. return;
  61. }
  62. if (!System.IO.File.Exists(this.config.ListFileName ()))
  63. {
  64. // Dovremmo notificare l'utente che non c'è la lista
  65. Log.Warning ("La lista non è presente, chiedo di scaricarla");
  66. this.UpdateFileList ();
  67. }
  68. else
  69. {
  70. Log.Info ("Avvio la ricerca nel database");
  71. this.keyword = keyword;
  72. this.fileTreeView = f;
  73. this.finder = new Thread(new ThreadStart (this._Search));
  74. this.finder.Start ();
  75. }
  76.  
  77. }
  78.  
  79. protected File ReadFile(StreamReader file)
  80. {
  81. string path = file.ReadLine();
  82. string user = file.ReadLine ();
  83. int size = System.Convert.ToInt32(file.ReadLine (), 10);
  84. return new File(path, user, size);
  85. }
  86.  
  87. protected void _Search()
  88. {
  89. // Questa funzione viene chiamata quando qualcuno richiede
  90. // una ricerca. Inoltre abbiamo la quasi certezza che ne venga
  91. // chiamata solo un'istanza nello stesso momento.
  92. FileList list = new FileList (ref this.config);
  93.  
  94. ArrayList files;
  95. try { files = list.Search (this.keyword); }
  96. catch (Exception e) {
  97. // Probabilmente stiamo solamente scaricando il database,
  98. // ma tant'è.
  99. Log.Error ("Impossibile effettuare la ricerca nel database: " + e.Message);
  100. files = new ArrayList ();
  101. }
  102.  
  103. this.fileTreeView.Clear ();
  104. if (files.Count == 0)
  105. this.fileTreeView.AddFile (new File("Nessun risultato", " "));
  106. foreach(File f in files)
  107. {
  108. this.fileTreeView.AddFile (f);
  109. }
  110. }
  111.  
  112. public void UpdateFileList ()
  113. {
  114. if(this.listUpdater != null && this.listUpdater.IsAlive)
  115. {
  116. Log.Warning ("La lista è già in fase di aggiornamento, non faccio nulla");
  117. return;
  118. }
  119.  
  120. this.listUpdater = new Thread (new ThreadStart (this._UpdateFileList));
  121. this.listUpdater.Start ();
  122. }
  123.  
  124.  
  125.  
  126. protected void _UpdateFileList ()
  127. {
  128. SFTPConnection s;
  129.  
  130. if (!this.config.authenticated)
  131. {
  132. Log.Info ("I dati di autenticazione non sono presenti");
  133. this.AuthenticationRequired ();
  134.  
  135. // Aspetto che l'utente abbia fatto.
  136. while(!this.config.authenticated) {}
  137. Log.Info ("Ho acquisito utente e password");
  138. }
  139.  
  140. // Inizializziamo la connessione con i dati che supponiamo
  141. // di avere.
  142. s = new SFTPConnection (this.config.GetValue("user"),
  143. this.config.password);
  144.  
  145. try {
  146. s.Connect ();
  147. }
  148. catch (Exception e) {
  149. Log.Error ("Autenticazione errata (" + e.Message + ")");
  150. this.config.authenticated = false;
  151. }
  152.  
  153. if(!this.config.authenticated)
  154. {
  155. EventManager.ErrorMessage ("Autenticazione fallita");
  156. }
  157. else
  158. {
  159. Log.Info ("Aggiornamento della lista avviato");
  160.  
  161. s.Download (new File("/nobackup/robol/index.db", "robol"),
  162. this.config.ConfigDir ());
  163.  
  164. Log.Info ("Lista Aggiornata");
  165. s.Disconnect ();
  166. }
  167. }
  168.  
  169.  
  170. public void Disconnect ()
  171. {
  172. foreach(FileTransfer t in this.transfers)
  173. {
  174. t.Stop ();
  175. }
  176. foreach(Thread t in this.threads)
  177. {
  178. if(t.IsAlive)
  179. t.Abort ();
  180. }
  181. if(this.finder != null && this.finder.IsAlive)
  182. {
  183. this.finder.Abort ();
  184. }
  185.  
  186. }
  187.  
  188.  
  189. public void Download(File f, ref TaskTreeView tasks, string downloadFolder)
  190. {
  191. // Preparo gli argomento da passare alla funzione.
  192. if (!this.config.authenticated)
  193. this.AuthenticationRequired ();
  194. while(!this.config.authenticated) {}
  195.  
  196. ArrayList args = new ArrayList ();
  197. args.Add (f);
  198. args.Add (tasks);
  199. args.Add (downloadFolder);
  200. Thread t = new Thread (new ParameterizedThreadStart(_Download));
  201. t.Start (args);
  202. this.threads.Add (t);
  203. }
  204.  
  205. protected void _Download(object args)
  206. {
  207. // Questi cast sono piuttosto brutali ma posso passare
  208. // solo un oggetto a Download e quindi è il meglio che
  209. // mi riesce di fare.
  210. File f = (File) ((ArrayList) args)[0];
  211. TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1];
  212. string downloadFolder = (string) ((ArrayList) args)[2];
  213. Log.Info ("Avvio il download di " + f.name);
  214.  
  215. try {
  216. FileTransfer transfer = new FileTransfer (f, ref tasks, this.config.GetValue("user"), this.config.password, downloadFolder);
  217. this.transfers.Add (transfer);
  218. } catch (Exception e) {
  219.  
  220. Log.Error (e.Message);
  221. this.config.authenticated = false;
  222. this.Download (f, ref tasks, downloadFolder);
  223. }
  224. }
  225.  
  226. }
  227.  
  228. public class SFTPConnection
  229. {
  230.  
  231. public Sftp sftp;
  232. public delegate void SFTPEvent (string src, string dest, int transferredBytes,
  233. int totalBytes, string message);
  234.  
  235. public event SFTPEvent TransferStarted;
  236. public event SFTPEvent TransferProgress;
  237. public event SFTPEvent TransferStopped;
  238.  
  239. public delegate void Del (string src, string dest,
  240. int transferredBytes, int totalBytes, string message);
  241.  
  242. public SFTPConnection (string user, string password)
  243. {
  244.  
  245. // Inizializziamo il protocollo
  246. this.sftp = new Sftp ("poisson.phc.unipi.it", user);
  247. this.sftp.Password = password;
  248.  
  249. this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler);
  250. this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler);
  251. this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler);
  252.  
  253. }
  254.  
  255. private void TransferStartedHandler(string src, string dest, int transferredBytes,
  256. int totalBytes, string message)
  257. {
  258. if(TransferStarted != null)
  259. TransferStarted(src, dest, transferredBytes, totalBytes, message);
  260. }
  261.  
  262. private void TransferProgressHandler(string src, string dest, int transferredBytes,
  263. int totalBytes, string message)
  264. {
  265. if (TransferProgress != null)
  266. TransferProgress(src, dest, transferredBytes, totalBytes, message);
  267. }
  268.  
  269. private void TransferStoppedHandler(string src, string dest, int transferredBytes,
  270. int totalBytes, string message)
  271. {
  272. if (TransferStopped != null)
  273. TransferStopped(src, dest, transferredBytes, totalBytes, message);
  274. }
  275.  
  276. public void Connect()
  277. {
  278. sftp.Connect (22);
  279. }
  280.  
  281. public ArrayList SearchFiles()
  282. {
  283. ArrayList matches = new ArrayList ();
  284. Console.WriteLine(" => Scan della nobackup");
  285. // ArrayList users = sftp.GetFileList ("/nobackup/");
  286. ArrayList users = new ArrayList();
  287. users.Add("robol");
  288. users.Add("bianchi");
  289. foreach(string folder in users)
  290. {
  291. if (folder == ".")
  292. continue;
  293. else if (folder == "..")
  294. continue;
  295. Console.WriteLine(" => Scan di {0}", folder);
  296. try { matches.AddRange (GetFolderContent(folder)); }
  297. catch(Exception e) { Console.WriteLine (e.Message); }
  298. }
  299. Console.WriteLine(" => Scan terminato");
  300. return matches;
  301. }
  302.  
  303. protected ArrayList GetFolderContent(string folder)
  304. {
  305. ArrayList matches, files;
  306. File f;
  307. string user;
  308. files = sftp.GetFileList("/nobackup/" + folder + "/");
  309. matches = new ArrayList ();
  310. foreach(string entry in files)
  311. {
  312. // Controllo che non sia una cartella. Evidentemente
  313. // questo metodo non è affidabile, ma per ora è il meglio
  314. // che SFTP ci permetta di fare.
  315. if(entry.IndexOf(".") == -1)
  316. matches.AddRange(GetFolderContent(folder + "/" + entry));
  317. else if(!entry.StartsWith("."))
  318. {
  319. user = folder;
  320. if(user.Contains("/"))
  321. user = user.Substring(0, user.IndexOf("/"));
  322. f = new File("/nobackup/" + folder + "/" + entry, user );
  323. matches.Add(f);
  324. }
  325. }
  326. return matches;
  327. }
  328.  
  329. public void Download(File f, string downloadFolder)
  330. {
  331. this.sftp.Get (f.path, downloadFolder + Path.DirectorySeparatorChar + f.name);
  332. }
  333.  
  334. public void Disconnect ()
  335. {
  336. sftp.Close ();
  337. }
  338. }
  339.  
  340. public class FileTransfer
  341. {
  342.  
  343. TaskTreeView tasks;
  344. File file;
  345. Gtk.TreeIter iter;
  346.  
  347. SFTPConnection sftp;
  348.  
  349. public delegate void Handler(string src, string dest, int transferredBytes,
  350. int totalBytes, string message);
  351.  
  352. public FileTransfer (File f, ref TaskTreeView tasks, string user, string password, string downloadFolder)
  353. {
  354. // Connessione al server
  355. this.sftp = new SFTPConnection (user, password);
  356. this.sftp.Connect ();
  357.  
  358. this.tasks = tasks;
  359. this.file = f;
  360.  
  361. sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted);
  362. sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress);
  363. sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped);
  364.  
  365. sftp.Download (this.file, downloadFolder);
  366. sftp.Disconnect ();
  367. }
  368.  
  369. public void Stop ()
  370. {
  371. this.sftp.Disconnect ();
  372. }
  373.  
  374. public void OnTransferStarted (string src, string dest, int transferredBytes,
  375. int totalBytes, string message)
  376. {
  377. this.iter = this.tasks.AddTask(file.name, 0);
  378. }
  379.  
  380. public void OnTransferProgress(string src, string dest, int transferredBytes,
  381. int totalBytes, string message)
  382. {
  383. int perc = transferredBytes / (totalBytes / 100);
  384. this.tasks.SetProgress(this.iter, perc);
  385. }
  386.  
  387. public void OnTransferStopped (string src, string dest, int transferredBytes,
  388. int totalBytes, string message)
  389. {
  390. this.tasks.DeleteRow (this.iter);
  391. }
  392. }
  393.  
  394.  
  395. class FileSearch
  396. {
  397. SshExec sshexec;
  398. string username, password, keywords;
  399. FileTreeView f;
  400.  
  401. public FileSearch (string username, string password, string keywords, ref FileTreeView f)
  402. {
  403. this.username = username;
  404. this.password = password;
  405. this.keywords = keywords;
  406. this.f = f;
  407. }
  408.  
  409. public void DoSearch ()
  410. {
  411. sshexec = new SshExec("poisson.phc.unipi.it", this.username, this.password);
  412. sshexec.Connect ();
  413.  
  414. string command = "locate -i -e --regex /nobackup/.+" + this.keywords;
  415. string output = sshexec.RunCommand (command);
  416. f.Clear ();
  417. foreach(string line in Regex.Split(output, "\n"))
  418. {
  419. if (line.StartsWith("/nobackup/") && Regex.Match(line,".*[.].*").Success)
  420. this.f.AddFile (new File(line, ""));
  421. }
  422. sshexec.Close ();
  423. }
  424.  
  425. }
  426.  
  427. public class FileList
  428. {
  429.  
  430. GlobalConfig config;
  431. SQLiteConnection connection;
  432.  
  433. public FileList(ref GlobalConfig config) {
  434.  
  435. this.config = config;
  436.  
  437. string connectionString = "Data Source=" + this.config.ListFileName () + ";Version=3";
  438. this.connection = new SQLiteConnection(connectionString);
  439. }
  440.  
  441. public ArrayList Search(string keyword) {
  442.  
  443. this.connection.Open ();
  444. ArrayList matches = new ArrayList ();
  445.  
  446. // Eseguiamo la query
  447. SQLiteCommand sqlCmd = this.connection.CreateCommand ();
  448. sqlCmd.CommandText = "SELECT * from files WHERE name LIKE '%" + keyword + "%';";
  449. SQLiteDataReader reader = sqlCmd.ExecuteReader ();
  450.  
  451. while(reader.Read())
  452. {
  453. matches.Add (new File(reader.GetString(0), // Path
  454. reader.GetString(2), // User
  455. reader.GetInt32(3))); // Size
  456. }
  457. this.connection.Close ();
  458. return matches;
  459. }
  460. }
  461.  
  462. }