1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\WebhookBundle\Controller; |
||
13 | |||
14 | use Mautic\CoreBundle\Controller\AjaxController as CommonAjaxController; |
||
15 | use Mautic\CoreBundle\Helper\InputHelper; |
||
16 | use Mautic\WebhookBundle\Http\Client; |
||
17 | use Symfony\Component\HttpFoundation\Request; |
||
18 | |||
19 | class AjaxController extends CommonAjaxController |
||
20 | { |
||
21 | protected function sendHookTestAction(Request $request) |
||
22 | { |
||
23 | $url = InputHelper::url($request->request->get('url')); |
||
24 | |||
25 | // validate the URL |
||
26 | if ('' == $url || !$url) { |
||
27 | // default to an error message |
||
28 | $dataArray = [ |
||
29 | 'success' => 1, |
||
30 | 'html' => '<div class="has-error"><span class="help-block">' |
||
31 | .$this->translator->trans('mautic.webhook.label.no.url') |
||
32 | .'</span></div>', |
||
33 | ]; |
||
34 | |||
35 | return $this->sendJsonResponse($dataArray); |
||
36 | } |
||
37 | |||
38 | // get the selected types |
||
39 | $selectedTypes = InputHelper::cleanArray($request->request->get('types')); |
||
40 | $payloadPaths = $this->getPayloadPaths($selectedTypes); |
||
41 | $payloads = $this->loadPayloads($payloadPaths); |
||
42 | $now = new \DateTime(); |
||
43 | |||
44 | $payloads['timestamp'] = $now->format('c'); |
||
45 | |||
46 | // set the response |
||
47 | /** @var Psr\Http\Message\ResponseInterface $response */ |
||
48 | $response = $this->get('mautic.webhook.http.client')->post($url, $payloads, null, InputHelper::string($request->request->get('secret'))); |
||
49 | |||
50 | // default to an error message |
||
51 | $dataArray = [ |
||
52 | 'success' => 1, |
||
53 | 'html' => '<div class="has-error"><span class="help-block">' |
||
54 | .$this->translator->trans('mautic.webhook.label.warning') |
||
55 | .'</span></div>', |
||
56 | ]; |
||
57 | |||
58 | // if we get a 2xx response convert to success message |
||
59 | if (2 == substr($response->getStatusCode(), 0, 1)) { |
||
60 | $dataArray['html'] = |
||
61 | '<div class="has-success"><span class="help-block">' |
||
62 | .$this->translator->trans('mautic.webhook.label.success') |
||
63 | .'</span></div>'; |
||
64 | } |
||
65 | |||
66 | return $this->sendJsonResponse($dataArray); |
||
67 | } |
||
68 | |||
69 | /* |
||
70 | * Get an array of all the payload paths we need to load |
||
71 | * |
||
72 | * @param $types array |
||
73 | * @return array |
||
74 | */ |
||
75 | public function getPayloadPaths($types) |
||
76 | { |
||
77 | $payloadPaths = []; |
||
78 | |||
79 | foreach ($types as $type) { |
||
80 | // takes an input like mautic.lead_on_something |
||
81 | // converts to array pieces using _ |
||
82 | $typePath = explode('_', $type); |
||
83 | |||
84 | // pull the prefix into its own variable |
||
85 | $prefix = $typePath[0]; |
||
86 | |||
87 | // now that we have the remove it from the array |
||
88 | unset($typePath[0]); |
||
89 | |||
90 | // build the event name by putting the pieces back together |
||
91 | $eventName = implode('_', $typePath); |
||
92 | |||
93 | // default the path to core |
||
94 | $payloadPath = $this->factory->getSystemPath('bundles', true); |
||
95 | |||
96 | // if plugin is in first part of the string this is an addon |
||
97 | // input is plugin.bundlename or mautic.bundlename |
||
98 | if (strpos('plugin.', $prefix)) { |
||
99 | $payloadPath = $this->factory->getSystemPath('plugins', true); |
||
100 | } |
||
101 | |||
102 | $prefixParts = explode('.', $prefix); |
||
103 | |||
104 | $bundleName = (array_pop($prefixParts)); |
||
105 | |||
106 | $payloadPath .= '/'.ucfirst($bundleName).'Bundle/Assets/WebhookPayload/'.$bundleName.'_'.$eventName.'.json'; |
||
107 | |||
108 | $payloadPaths[$type] = $payloadPath; |
||
109 | } |
||
110 | |||
111 | return $payloadPaths; |
||
112 | } |
||
113 | |||
114 | /* |
||
115 | * Iterate through the paths and get the json payloads |
||
116 | * |
||
117 | * @param $paths array |
||
118 | * @return $payload array |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
119 | */ |
||
120 | public function loadPayloads($paths) |
||
121 | { |
||
122 | $payloads = []; |
||
123 | |||
124 | foreach ($paths as $key => $path) { |
||
125 | if (file_exists($path)) { |
||
126 | $payloads[$key] = json_decode(file_get_contents($path), true); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return $payloads; |
||
131 | } |
||
132 | } |
||
133 |