CurlEffect.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. Effet de workflow
  4. Execute une requete POST,GET... sur une url et récupere le résultat (json ou html) dans une variable de conf workflow
  5. */
  6. class CurlEffect{
  7. //Descriptif du type d'effet
  8. public static function manifest($key = null){
  9. $manifest = array(
  10. 'slug' => 'curl',
  11. 'label' => 'Lancer une requête',
  12. 'class' => get_called_class(),
  13. 'path' => __FILE__,
  14. 'icon' => 'fas fa-atlas',
  15. 'color' => '#ff9f43',
  16. );
  17. if(!isset($key)) return $manifest;
  18. return isset($manifest[$key]) ? $manifest[$key] : '' ;
  19. }
  20. //méthode d'affichage de l'effet
  21. public static function form($item){
  22. $html = '';
  23. $class = get_called_class();
  24. ob_start();
  25. ?>
  26. <div class="input-group">
  27. <div class="input-group-text input-group-prepend">
  28. Méthode
  29. </div>
  30. <input type="text" data-id="curl-method" class="form-control" value="<?php echo isset($item['curl-method']) ? $item['curl-method'] : 'GET' ?>" placeholder="POST,GET..." required>
  31. <div class="input-group-text input-group-prepend">
  32. URL
  33. </div>
  34. <input type="text" data-id="curl-url" class="form-control" value="<?php echo isset($item['curl-url']) ? $item['curl-url'] : 'http://' ?>" placeholder="http://..." required>
  35. </div>
  36. En-têtes
  37. <textarea data-id="curl-header" class="form-control" placeholder="Une ligne par en-tête ex :
  38. Content-type:'application/json'"><?php echo isset($item['curl-header']) ? $item['curl-header'] : '' ?></textarea>
  39. Corps
  40. <textarea data-id="curl-body" class="form-control" placeholder="{...}"><?php echo isset($item['curl-body']) ? $item['curl-url'] : '' ?></textarea>
  41. <hr>
  42. <div class="input-group">
  43. <div class="input-group-text input-group-prepend">
  44. Format de résultat
  45. </div>
  46. <select data-id="curl-result-type" class="form-control">
  47. <option <?php echo isset($item['curl-result-type']) && $item['curl-result-type']=='json' ? "selected='selected'" : '' ?> value="">Aucun</option>
  48. <option <?php echo isset($item['curl-result-type']) && $item['curl-result-type']=='html' ? "selected='selected'" : '' ?> value="html">HTML</option>
  49. <option <?php echo isset($item['curl-result-type']) && $item['curl-result-type']=='json' ? "selected='selected'" : '' ?> value="json">JSON</option>
  50. </select>
  51. </div>
  52. <div class="input-group mt-1">
  53. <div class="input-group-text input-group-prepend">
  54. Variable de résultat
  55. </div>
  56. <input type="text" data-id="curl-var" class="form-control" value="<?php echo isset($item['curl-var']) ? $item['curl-var'] : '' ?>" placeholder="Ma variable">
  57. <div class="input-group-text input-group-prepend">
  58. Prendre le chemin JSON
  59. </div>
  60. <input type="text" data-id="curl-var-path" class="form-control" value="<?php echo isset($item['curl-var-path']) ? $item['curl-var-path'] : '' ?>" placeholder="Ma variable">
  61. </div>
  62. <?php
  63. $html = ob_get_clean();
  64. return $html;
  65. }
  66. public static function run($effect,$parameters = array()){
  67. global $conf;
  68. $logs = '';
  69. if( in_array($parameters['workflow']['type'] , array( Workflow::TYPE_ENTITY, Workflow::TYPE_LIST)) ){
  70. if(isset($parameters['current'])) $parameters['current'] = $parameters['current']->toArray();
  71. if(isset($parameters['old'])) $parameters['old'] = $parameters['old']->toArray();
  72. }
  73. $effect->values['curl-method'] = template($effect->values['curl-method'],$parameters,true);
  74. $effect->values['curl-url'] = template($effect->values['curl-url'],$parameters,true);
  75. $effect->values['curl-header'] = template($effect->values['curl-header'],$parameters,true);
  76. $effect->values['curl-body'] = template($effect->values['curl-body'],$parameters,true);
  77. $ch = curl_init();
  78. $options = array(
  79. CURLOPT_URL => $effect->values['curl-url'],
  80. CURLOPT_HEADER => false,
  81. CURLOPT_SSL_VERIFYHOST => false,
  82. CURLOPT_SSL_VERIFYPEER => false,
  83. CURLOPT_RETURNTRANSFER => true,
  84. CURLOPT_CONNECTTIMEOUT=> 10000
  85. );
  86. $options[CURLOPT_HTTPHEADER] = explode(PHP_EOL,$effect->values['curl-header']);
  87. $options[CURLOPT_POSTFIELDS] = $effect->values['curl-body'] ;
  88. curl_setopt_array($ch, $options);
  89. $logs .= '<i class="'.self::manifest('icon').'"></i> Lancement de la requete '.$effect->values['curl-method'].' '.$effect->values['curl-url'].'<br>';
  90. $results = curl_exec($ch);
  91. $logs .= 'Résultat '.$results.'<br>';
  92. curl_close($ch);
  93. if(isset($effect->values['curl-var'])){
  94. $logs .= 'Stockage résultat en variable '.$effect->values['curl-var'].'<br>';
  95. if($effect->values['curl-result-type'] == 'json'){
  96. $results = json_decode($results,true);
  97. $logs .= 'Décodage json :'.($results==false?'JSON incorrect':'JSON correct').'<br>';
  98. if(!empty($effect->values['curl-var-path']) && $results!=false){
  99. foreach (explode('.',$effect->values['curl-var-path']) as $key => $value) {
  100. if(!isset($results[$value])) break;
  101. $results = $results[$value];
  102. }
  103. }
  104. }
  105. $logs .= 'Stokage résultat '.$effect->values['curl-var'].' = '.$results.'<br>';
  106. $workflowVars = json_decode($conf->get('workflow-var'),true);
  107. $workflowVars[$effect->values['curl-var']] = $results;
  108. $conf->put('workflow-var', json_encode($workflowVars));
  109. }
  110. return $logs;
  111. }
  112. }
  113. ?>