HomeController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Public Printer Control System
  4. *
  5. * Copyright © 2018 - 2019, Aaron Speer, aaron.speerfamily.ie ajamesspeer@gmail.com.
  6. * All Rights Reserved.
  7. */
  8. namespace App\Controllers;
  9. use App\Models\HomeModel;
  10. use App\Controller;
  11. use App\Model;
  12. class HomeController extends Controller {
  13. public function __construct($twig)
  14. {
  15. $this->twig = $twig;
  16. $this->model = new HomeModel;
  17. }
  18. public function index(){
  19. global $config;
  20. echo $this->twig->render('home.twig', array(
  21. "messages" => $this->model->getMessages(),
  22. "instr" => $config["step2instructions"],
  23. "name" => $config["appname"]
  24. ));
  25. }
  26. public function upload(){
  27. $name = md5(random_int(1, 10000) . microtime()).md5(random_int(1, 10000) . microtime());
  28. $address = "/tmp/".$name."/".str_replace(" ", " ", basename( $_FILES['file']['name']));
  29. $address = substr_replace($address , 'pdf', strrpos($address , '.') +1);
  30. $targetfolder = __DIR__."/../../public/tmp/".$name ."/";
  31. exec("mkdir ".$targetfolder);
  32. $targetfolder = addslashes($targetfolder);
  33. $targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
  34. if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder)){
  35. $targetfolder = str_replace(" ", "\ ", $targetfolder);
  36. $tf = $targetfolder;
  37. $cmd = "sudo /usr/bin/unoconv --output=".$targetfolder." -f pdf ".$targetfolder;
  38. $file_parts = pathinfo($tf);
  39. if($file_parts["extension"] == "pdf")
  40. {
  41. exec("ls");
  42. }
  43. else{
  44. exec($cmd);
  45. }
  46. global $config;
  47. echo $this->twig->render('upload.twig', array(
  48. "messages" => $this->model->getMessages(),
  49. "link" => $address,
  50. "printers" => $config["printers"],
  51. "path" => escapeshellcmd($address),
  52. "instr" => $config["failedinstructions"],
  53. "name" => $config["appname"]
  54. ));
  55. }
  56. else {
  57. echo "Problem uploading file";
  58. }
  59. }
  60. public function printt(){
  61. $printer = $_POST["printer"];
  62. $copies = $_POST["copies"];
  63. $path = $_POST["path"];
  64. $printer=escapeshellcmd($printer);
  65. $copies=escapeshellcmd($copies);
  66. $path=str_replace(" ", "\ ", escapeshellcmd($path));
  67. $path = __DIR__."/../../public".$path;
  68. $cmd = "lp -d ".$printer." -n ".$copies." ".$path;
  69. echo $cmd;
  70. echo exec($cmd);
  71. $this->model->setMessage("Printing Successful!", "That file was printed ".$copies." time(s) on printer ".$printer, "success");
  72. header("Location: /");
  73. }
  74. }