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. // Chiamiamo questo con Glib per non far crashare tutto.
  156. GLib.Idle.Add (new GLib.IdleHandler(MainWindow.OnAuthenticationFailed));
  157. }
  158. else
  159. {
  160. Log.Info ("Aggiornamento della lista avviato");
  161.  
  162. s.Download (new File("/nobackup/robol/index.db", "robol"),
  163. this.config.ConfigDir ());
  164.  
  165. Log.Info ("Lista Aggiornata");
  166. s.Disconnect ();
  167. }
  168. }
  169.  
  170.  
  171. public void Disconnect ()
  172. {
  173. foreach(FileTransfer t in this.transfers)
  174. {
  175. t.Stop ();
  176. }
  177. foreach(Thread t in this.threads)
  178. {
  179. if(t.IsAlive)
  180. t.Abort ();
  181. }
  182. if(this.finder != null && this.finder.IsAlive)
  183. {
  184. this.finder.Abort ();
  185. }
  186.  
  187. }
  188.  
  189.  
  190. public void Download(File f, ref TaskTreeView tasks, string downloadFolder)
  191. {
  192. // Preparo gli argomento da passare alla funzione.
  193. if (!this.config.authenticated)
  194. this.AuthenticationRequired ();
  195. while(!this.config.authenticated) {}
  196.  
  197. ArrayList args = new ArrayList ();
  198. args.Add (f);
  199. args.Add (tasks);
  200. args.Add (downloadFolder);
  201. Thread t = new Thread (new ParameterizedThreadStart(_Download));
  202. t.Start (args);
  203. this.threads.Add (t);
  204. }
  205.  
  206. protected void _Download(object args)
  207. {
  208. // Questi cast sono piuttosto brutali ma posso passare
  209. // solo un oggetto a Download e quindi è il meglio che
  210. // mi riesce di fare.
  211. File f = (File) ((ArrayList) args)[0];
  212. TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1];
  213. string downloadFolder = (string) ((ArrayList) args)[2];
  214. Log.Info ("Avvio il download di " + f.name);
  215.  
  216. try {
  217. FileTransfer transfer = new FileTransfer (f, ref tasks, this.config.GetValue("user"), this.config.password, downloadFolder);
  218. this.transfers.Add (transfer);
  219. } catch (Exception e) {
  220.  
  221. Log.Error (e.Message);
  222. this.config.authenticated = false;
  223. this.Download (f, ref tasks, downloadFolder);
  224. }
  225. }
  226.  
  227. }
  228.  
  229. public class SFTPConnection
  230. {
  231.  
  232. public Sftp sftp;
  233. public delegate void SFTPEvent (string src, string dest, int transferredBytes,
  234. int totalBytes, string message);
  235.  
  236. public event SFTPEvent TransferStarted;
  237. public event SFTPEvent TransferProgress;
  238. public event SFTPEvent TransferStopped;
  239.  
  240. public delegate void Del (string src, string dest,
  241. int transferredBytes, int totalBytes, string message);
  242.  
  243. public SFTPConnection (string user, string password)
  244. {
  245.  
  246. // Inizializziamo il protocollo
  247. this.sftp = new Sftp ("poisson.phc.unipi.it", user);
  248. this.sftp.Password = password;
  249.  
  250. this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler);
  251. this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler);
  252. this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler);
  253.  
  254. }
  255.  
  256. private void TransferStartedHandler(string src, string dest, int transferredBytes,
  257. int totalBytes, string message)
  258. {
  259. if(TransferStarted != null)
  260. TransferStarted(src, dest, transferredBytes, totalBytes, message);
  261. }
  262.  
  263. private void TransferProgressHandler(string src, string dest, int transferredBytes,
  264. int totalBytes, string message)
  265. {
  266. if (TransferProgress != null)
  267. TransferProgress(src, dest, transferredBytes, totalBytes, message);
  268. }
  269.  
  270. private void TransferStoppedHandler(string src, string dest, int transferredBytes,
  271. int totalBytes, string message)
  272. {
  273. if (TransferStopped != null)
  274. TransferStopped(src, dest, transferredBytes, totalBytes, message);
  275. }
  276.  
  277. public void Connect()
  278. {
  279. sftp.Connect (22);
  280. }
  281.  
  282. public ArrayList SearchFiles()
  283. {
  284. ArrayList matches = new ArrayList ();
  285. Console.WriteLine(" => Scan della nobackup");
  286. // ArrayList users = sftp.GetFileList ("/nobackup/");
  287. ArrayList users = new ArrayList();
  288. users.Add("robol");
  289. users.Add("bianchi");
  290. foreach(string folder in users)
  291. {
  292. if (folder == ".")
  293. continue;
  294. else if (folder == "..")
  295. continue;
  296. Console.WriteLine(" => Scan di {0}", folder);
  297. try { matches.AddRange (GetFolderContent(folder)); }
  298. catch(Exception e) { Console.WriteLine (e.Message); }
  299. }
  300. Console.WriteLine(" => Scan terminato");
  301. return matches;
  302. }
  303.  
  304. protected ArrayList GetFolderContent(string folder)
  305. {
  306. ArrayList matches, files;
  307. File f;
  308. string user;
  309. files = sftp.GetFileList("/nobackup/" + folder + "/");
  310. matches = new ArrayList ();
  311. foreach(string entry in files)
  312. {
  313. // Controllo che non sia una cartella. Evidentemente
  314. // questo metodo non è affidabile, ma per ora è il meglio
  315. // che SFTP ci permetta di fare.
  316. if(entry.IndexOf(".") == -1)
  317. matches.AddRange(GetFolderContent(folder + "/" + entry));
  318. else if(!entry.StartsWith("."))
  319. {
  320. user = folder;
  321. if(user.Contains("/"))
  322. user = user.Substring(0, user.IndexOf("/"));
  323. f = new File("/nobackup/" + folder + "/" + entry, user );
  324. matches.Add(f);
  325. }
  326. }
  327. return matches;
  328. }
  329.  
  330. public void Download(File f, string downloadFolder)
  331. {
  332. this.sftp.Get (f.path, downloadFolder + Path.DirectorySeparatorChar + f.name);
  333. }
  334.  
  335. public void Disconnect ()
  336. {
  337. sftp.Close ();
  338. }
  339. }
  340.  
  341. public class FileTransfer
  342. {
  343.  
  344. TaskTreeView tasks;
  345. File file;
  346. Gtk.TreeIter iter;
  347.  
  348. SFTPConnection sftp;
  349.  
  350. public delegate void Handler(string src, string dest, int transferredBytes,
  351. int totalBytes, string message);
  352.  
  353. public FileTransfer (File f, ref TaskTreeView tasks, string user, string password, string downloadFolder)
  354. {
  355. // Connessione al server
  356. this.sftp = new SFTPConnection (user, password);
  357. this.sftp.Connect ();
  358.  
  359. this.tasks = tasks;
  360. this.file = f;
  361.  
  362. sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted);
  363. sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress);
  364. sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped);
  365.  
  366. sftp.Download (this.file, downloadFolder);
  367. sftp.Disconnect ();
  368. }
  369.  
  370. public void Stop ()
  371. {
  372. this.sftp.Disconnect ();
  373. }
  374.  
  375. public void OnTransferStarted (string src, string dest, int transferredBytes,
  376. int totalBytes, string message)
  377. {
  378. this.iter = this.tasks.AddTask(file.name, 0);
  379. }
  380.  
  381. public void OnTransferProgress(string src, string dest, int transferredBytes,
  382. int totalBytes, string message)
  383. {
  384. int perc = transferredBytes / (totalBytes / 100);
  385. this.tasks.SetProgress(this.iter, perc);
  386. }
  387.  
  388. public void OnTransferStopped (string src, string dest, int transferredBytes,
  389. int totalBytes, string message)
  390. {
  391. this.tasks.DeleteRow (this.iter);
  392. }
  393. }
  394.  
  395.  
  396. class FileSearch
  397. {
  398. SshExec sshexec;
  399. string username, password, keywords;
  400. FileTreeView f;
  401.  
  402. public FileSearch (string username, string password, string keywords, ref FileTreeView f)
  403. {
  404. this.username = username;
  405. this.password = password;
  406. this.keywords = keywords;
  407. this.f = f;
  408. }
  409.  
  410. public void DoSearch ()
  411. {
  412. sshexec = new SshExec("poisson.phc.unipi.it", this.username, this.password);
  413. sshexec.Connect ();
  414.  
  415. string command = "locate -i -e --regex /nobackup/.+" + this.keywords;
  416. string output = sshexec.RunCommand (command);
  417. f.Clear ();
  418. foreach(string line in Regex.Split(output, "\n"))
  419. {
  420. if (line.StartsWith("/nobackup/") && Regex.Match(line,".*[.].*").Success)
  421. this.f.AddFile (new File(line, ""));
  422. }
  423. sshexec.Close ();
  424. }
  425.  
  426. }
  427.  
  428. public class FileList
  429. {
  430.  
  431. GlobalConfig config;
  432. SQLiteConnection connection;
  433.  
  434. public FileList(ref GlobalConfig config) {
  435.  
  436. this.config = config;
  437.  
  438. string connectionString = "Data Source=" + this.config.ListFileName () + ";Version=3";
  439. this.connection = new SQLiteConnection(connectionString);
  440. }
  441.  
  442. public ArrayList Search(string keyword) {
  443.  
  444. this.connection.Open ();
  445. ArrayList matches = new ArrayList ();
  446.  
  447. // Eseguiamo la query
  448. SQLiteCommand sqlCmd = this.connection.CreateCommand ();
  449. sqlCmd.CommandText = "SELECT * from files WHERE name LIKE '%" + keyword + "%';";
  450. SQLiteDataReader reader = sqlCmd.ExecuteReader ();
  451.  
  452. while(reader.Read())
  453. {
  454. matches.Add (new File(reader.GetString(0), // Path
  455. reader.GetString(2), // User
  456. reader.GetInt32(3))); // Size
  457. }
  458. this.connection.Close ();
  459. return matches;
  460. }
  461. }
  462.  
  463. }