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