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