bartender.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. if (!stream_resolve_include_path('../conf/cocktail.php'))
  3. response(["Configuration file '../conf/cocktail.php' not found.\nSample configuration file can be found in '/conf/cocktail.php.smp'"]);
  4. require '../conf/cocktail.php';
  5. $errors = [];
  6. if (!isset($_REQUEST['action']))
  7. $errors[] = "No 'action' parameter given, dunno what to do";
  8. if (!empty($errors))
  9. response($errors);
  10. switch ($_REQUEST['action']) {
  11. case 'shake':
  12. init_check();
  13. shake();
  14. break;
  15. case 'status':
  16. init_check();
  17. status();
  18. break;
  19. }
  20. function init_check () {
  21. global $COCKTAIL;
  22. if (!isset($COCKTAIL['cocktail']['binary'])) {
  23. $errors[] = "'cocktail:binary' n'est pas configuré dans cocktail.php";
  24. response($errors);
  25. }
  26. }
  27. function shake() {
  28. global $COCKTAIL;
  29. $params = $_REQUEST;
  30. $errors = [];
  31. foreach (['dossier','projetId'] as $p)
  32. if (!preg_match('/^[\W\d-]+$/i',$params[$p]))
  33. $errors[] = "'$p' parameter invalid";
  34. foreach (['projetPadPrincipal','projetPadGarde'] as $p)
  35. if (!preg_match('_^https?://pad\.exegetes\.eu\.org/_i',$params[$p]))
  36. $errors[] = "'$p' parameter invalid ('{$params[$p]}')";
  37. if (!isset($COCKTAIL['cocktail'])) {
  38. $errors[] = "'cocktail' n'est pas configuré dans cocktail.php";
  39. response($errors);
  40. }
  41. $cocktailbin = $COCKTAIL['cocktail']['binary'];
  42. if (!is_executable($cocktailbin))
  43. $errors[] = "'cocktail:binary' '$cocktailbin' n'est pas exécutable";
  44. if (!empty($errors))
  45. response($errors);
  46. foreach (['projetPadPrincipal','projetPadGarde'] as $p)
  47. $params[$p] = urlencode($params[$p]);
  48. // Shell-escape parameters
  49. foreach (['projetPadPrincipal','projetPadGarde','dossier','projetId'] as $p)
  50. $params[$p] = escapeshellarg($params[$p]);
  51. // $nowait = '> /dev/null 2>&1 &';
  52. $cmd = "$cocktailbin -d {$params['dossier']} -b {$params['projetPadPrincipal']} -g {$params['projetPadGarde']} -p {$params['projetId']}";
  53. exec($cmd,$output);
  54. response([],array_merge(['Commande :','$cmd','Output:'],$output));
  55. }
  56. function status () {
  57. global $COCKTAIL;
  58. $params = $_REQUEST;
  59. $errors = [];
  60. if (!isset($COCKTAIL['cocktail']['store']))
  61. $errors[] = "'cocktail:store' n'est pas configuré dans config.yml";
  62. foreach (['dossier','projetId'] as $p)
  63. if (!preg_match('/^[\W\d-]+$/i',$params[$p]))
  64. $errors[] = "'$p' parameter invalid";
  65. if (!empty($errors))
  66. response($errors);
  67. $lock_filename = "{$COCKTAIL['cocktail']['store']}/{$params['dossier']}/{$params['projetId']}.lock";
  68. $pdf_filename = "{$COCKTAIL['cocktail']['store']}/{$params['dossier']}/{$params['projetId']}.pdf";
  69. if (is_file($lock_filename))
  70. $res = [
  71. 'status' => 'IN_PROGRESS',
  72. 'time' => format_time(stat($lock_filename)['mtime']),
  73. ];
  74. else if (is_file($pdf_filename))
  75. $res = [
  76. 'status' => 'COMPLETED',
  77. 'time' => format_time(stat($pdf_filename)['mtime']),
  78. ];
  79. else
  80. $res = [
  81. 'status' => 'NONE',
  82. 'time' => 'Aucune compilation.',
  83. ];
  84. header ('Content-Type: application/json');
  85. echo json_encode($res);
  86. die();
  87. }
  88. function response ($errors=[],$output=[]) {
  89. header ('Content-Type: application/json');
  90. echo json_encode([
  91. 'status' => empty($errors) ? 'success' : 'error',
  92. 'output' => $output,
  93. 'errors'=> $errors,
  94. ]);
  95. die();
  96. }
  97. function format_time ($unixtime) {
  98. return is_numeric($unixtime) ? date('d/m/Y H:i:s',$unixtime) : $unixtime;
  99. }
  100. ?>