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