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. public void Upload(string f, ref TaskTreeView tasks)
  241. {
  242. // Preparo gli argomento da passare alla funzione.
  243. if (!this.config.authenticated)
  244. this.AuthenticationRequired ();
  245. while(!this.config.authenticated) {}
  246.  
  247. ArrayList args = new ArrayList ();
  248. args.Add (f);
  249. args.Add (tasks);
  250. Thread t = new Thread (new ParameterizedThreadStart(_Upload));
  251. t.Start (args);
  252. this.threads.Add (t);
  253. }
  254.  
  255. protected void _Upload(object args)
  256. {
  257. // Questi cast sono piuttosto brutali ma posso passare
  258. // solo un oggetto a Download e quindi è il meglio che
  259. // mi riesce di fare.
  260. string f = (string) ((ArrayList) args)[0];
  261. TaskTreeView tasks = (TaskTreeView) ((ArrayList) args)[1];
  262. Log.Info ("Avvio l'upload di " + f);
  263.  
  264. try {
  265. FileUpload transfer = new FileUpload (f, ref tasks, this.config.GetValue("user"), this.config.password);
  266. this.transfers.Add (transfer);
  267. } catch (Exception e) {
  268.  
  269. Log.Error (e.Message);
  270. this.config.authenticated = false;
  271. this.Upload (f, ref tasks);
  272. }
  273. }
  274.  
  275.  
  276.  
  277. }
  278.  
  279. public class SFTPConnection
  280. {
  281.  
  282. public Sftp sftp;
  283. string user;
  284. public delegate void SFTPEvent (string src, string dest, int transferredBytes,
  285. int totalBytes, string message);
  286.  
  287. public event SFTPEvent TransferStarted;
  288. public event SFTPEvent TransferProgress;
  289. public event SFTPEvent TransferStopped;
  290.  
  291. public delegate void Del (string src, string dest,
  292. int transferredBytes, int totalBytes, string message);
  293.  
  294. public SFTPConnection (string user, string password)
  295. {
  296.  
  297. // Inizializziamo il protocollo
  298. this.user = user;
  299. this.sftp = new Sftp ("poisson.phc.unipi.it", user);
  300. this.sftp.Password = password;
  301.  
  302. this.sftp.OnTransferStart += new FileTransferEvent(this.TransferStartedHandler);
  303. this.sftp.OnTransferProgress += new FileTransferEvent(this.TransferProgressHandler);
  304. this.sftp.OnTransferEnd += new FileTransferEvent(this.TransferStoppedHandler);
  305.  
  306. }
  307.  
  308. private void TransferStartedHandler(string src, string dest, int transferredBytes,
  309. int totalBytes, string message)
  310. {
  311. if(TransferStarted != null)
  312. TransferStarted(src, dest, transferredBytes, totalBytes, message);
  313. }
  314.  
  315. private void TransferProgressHandler(string src, string dest, int transferredBytes,
  316. int totalBytes, string message)
  317. {
  318. if (TransferProgress != null)
  319. TransferProgress(src, dest, transferredBytes, totalBytes, message);
  320. }
  321.  
  322. private void TransferStoppedHandler(string src, string dest, int transferredBytes,
  323. int totalBytes, string message)
  324. {
  325. if (TransferStopped != null)
  326. TransferStopped(src, dest, transferredBytes, totalBytes, message);
  327. }
  328.  
  329. public void Connect()
  330. {
  331. sftp.Connect (22);
  332. }
  333.  
  334. public ArrayList SearchFiles()
  335. {
  336. ArrayList matches = new ArrayList ();
  337. Console.WriteLine(" => Scan della nobackup");
  338. // ArrayList users = sftp.GetFileList ("/nobackup/");
  339. ArrayList users = new ArrayList();
  340. users.Add("robol");
  341. users.Add("bianchi");
  342. foreach(string folder in users)
  343. {
  344. if (folder == ".")
  345. continue;
  346. else if (folder == "..")
  347. continue;
  348. Console.WriteLine(" => Scan di {0}", folder);
  349. try { matches.AddRange (GetFolderContent(folder)); }
  350. catch(Exception e) { Console.WriteLine (e.Message); }
  351. }
  352. Console.WriteLine(" => Scan terminato");
  353. return matches;
  354. }
  355.  
  356. protected ArrayList GetFolderContent(string folder)
  357. {
  358. ArrayList matches, files;
  359. File f;
  360. string user;
  361. files = sftp.GetFileList("/nobackup/" + folder + "/");
  362. matches = new ArrayList ();
  363. foreach(string entry in files)
  364. {
  365. // Controllo che non sia una cartella. Evidentemente
  366. // questo metodo non è affidabile, ma per ora è il meglio
  367. // che SFTP ci permetta di fare.
  368. if(entry.IndexOf(".") == -1)
  369. matches.AddRange(GetFolderContent(folder + "/" + entry));
  370. else if(!entry.StartsWith("."))
  371. {
  372. user = folder;
  373. if(user.Contains("/"))
  374. user = user.Substring(0, user.IndexOf("/"));
  375. f = new File("/nobackup/" + folder + "/" + entry, user );
  376. matches.Add(f);
  377. }
  378. }
  379. return matches;
  380. }
  381.  
  382. public void Download(File f, string downloadFolder)
  383. {
  384. this.sftp.Get (f.path, downloadFolder + Path.DirectorySeparatorChar + f.name);
  385. }
  386.  
  387. public void Upload(string filename)
  388. {
  389. this.sftp.Put(filename, "/nobackup/" + this.user + "/");
  390. }
  391.  
  392. public void Disconnect ()
  393. {
  394. sftp.Close ();
  395. }
  396. }
  397.  
  398. public class FileTransfer
  399. {
  400.  
  401. TaskTreeView tasks;
  402. File file;
  403. Gtk.TreeIter iter;
  404.  
  405. SFTPConnection sftp;
  406.  
  407. public delegate void Handler(string src, string dest, int transferredBytes,
  408. int totalBytes, string message);
  409.  
  410. public FileTransfer (File f, ref TaskTreeView tasks, string user, string password, string downloadFolder)
  411. {
  412. // Connessione al server
  413. this.sftp = new SFTPConnection (user, password);
  414. this.sftp.Connect ();
  415.  
  416. this.tasks = tasks;
  417. this.file = f;
  418.  
  419. sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted);
  420. sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress);
  421. sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped);
  422.  
  423. sftp.Download (this.file, downloadFolder);
  424. sftp.Disconnect ();
  425. }
  426.  
  427. public void Stop ()
  428. {
  429. this.sftp.Disconnect ();
  430. }
  431.  
  432. public void OnTransferStarted (string src, string dest, int transferredBytes,
  433. int totalBytes, string message)
  434. {
  435. this.iter = this.tasks.AddTask(file.name, 0);
  436. }
  437.  
  438. public void OnTransferProgress(string src, string dest, int transferredBytes,
  439. int totalBytes, string message)
  440. {
  441. int perc = transferredBytes / (totalBytes / 100);
  442. this.tasks.SetProgress(this.iter, perc);
  443. }
  444.  
  445. public void OnTransferStopped (string src, string dest, int transferredBytes,
  446. int totalBytes, string message)
  447. {
  448. this.tasks.DeleteRow (this.iter);
  449. }
  450. }
  451.  
  452.  
  453. public class FileUpload
  454. {
  455.  
  456. TaskTreeView tasks;
  457. string filename;
  458. Gtk.TreeIter iter;
  459.  
  460. SFTPConnection sftp;
  461.  
  462. public delegate void Handler(string src, string dest, int transferredBytes,
  463. int totalBytes, string message);
  464.  
  465. public FileUpload (string filename, ref TaskTreeView tasks, string user, string password)
  466. {
  467. // Connessione al server
  468. this.sftp = new SFTPConnection (user, password);
  469. this.sftp.Connect ();
  470.  
  471. this.tasks = tasks;
  472. this.filename = filename;
  473.  
  474. sftp.TransferStarted += new SFTPConnection.SFTPEvent(OnTransferStarted);
  475. sftp.TransferProgress += new SFTPConnection.SFTPEvent(OnTransferProgress);
  476. sftp.TransferStopped += new SFTPConnection.SFTPEvent(OnTransferStopped);
  477.  
  478. sftp.Upload (this.filename);
  479. sftp.Disconnect ();
  480. }
  481.  
  482. public void Stop ()
  483. {
  484. this.sftp.Disconnect ();
  485. }
  486.  
  487. public void OnTransferStarted (string src, string dest, int transferredBytes,
  488. int totalBytes, string message)
  489. {
  490. this.iter = this.tasks.AddTask(filename, 0);
  491. }
  492.  
  493. public void OnTransferProgress(string src, string dest, int transferredBytes,
  494. int totalBytes, string message)
  495. {
  496. int perc = transferredBytes / (totalBytes / 100);
  497. this.tasks.SetProgress(this.iter, perc);
  498. }
  499.  
  500. public void OnTransferStopped (string src, string dest, int transferredBytes,
  501. int totalBytes, string message)
  502. {
  503. this.tasks.DeleteRow (this.iter);
  504. }
  505. }
  506.  
  507.  
  508. class FileSearch
  509. {
  510. SshExec sshexec;
  511. string username, password, keywords;
  512. FileTreeView f;
  513.  
  514. public FileSearch (string username, string password, string keywords, ref FileTreeView f)
  515. {
  516. this.username = username;
  517. this.password = password;
  518. this.keywords = keywords;
  519. this.f = f;
  520. }
  521.  
  522. public void DoSearch ()
  523. {
  524. sshexec = new SshExec("poisson.phc.unipi.it", this.username, this.password);
  525. sshexec.Connect ();
  526.  
  527. string command = "locate -i -e --regex /nobackup/.+" + this.keywords;
  528. string output = sshexec.RunCommand (command);
  529. f.Clear ();
  530. foreach(string line in Regex.Split(output, "\n"))
  531. {
  532. if (line.StartsWith("/nobackup/") && Regex.Match(line,".*[.].*").Success)
  533. this.f.AddFile (new File(line, ""));
  534. }
  535. sshexec.Close ();
  536. }
  537.  
  538. }
  539.  
  540. public class FileList
  541. {
  542.  
  543. GlobalConfig config;
  544. SQLiteConnection connection;
  545.  
  546. public FileList(ref GlobalConfig config) {
  547.  
  548. this.config = config;
  549.  
  550. string connectionString = "Data Source=" + this.config.ListFileName () + ";Version=3";
  551. this.connection = new SQLiteConnection(connectionString);
  552. }
  553.  
  554. public ArrayList Search(string keyword, string type, string user) {
  555.  
  556. this.connection.Open ();
  557. ArrayList matches = new ArrayList ();
  558.  
  559. // Eseguiamo la query
  560. SQLiteCommand sqlCmd = this.connection.CreateCommand ();
  561. sqlCmd.CommandText = "SELECT * from files WHERE name LIKE '%" + keyword + "%'";
  562. SQLiteDataReader reader = sqlCmd.ExecuteReader ();
  563.  
  564. File tmp;
  565. while(reader.Read())
  566. {
  567. tmp = new File(reader.GetString(0), // Path
  568. reader.GetString(2), // User
  569. reader.GetString(1), // Name
  570. reader.GetInt32(3)); // Size
  571.  
  572. if ( (type == "Qualsiasi" || tmp.type.Name() == type) &&
  573. (user == "" || user == tmp.user) )
  574. {
  575. matches.Add (tmp);
  576. }
  577. }
  578.  
  579. this.connection.Close ();
  580. return matches;
  581. }
  582. }
  583.  
  584. }