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