Issues (3627)

MauticCitrixBundle/Controller/PublicController.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2016 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 MauticPlugin\MauticCitrixBundle\Controller;
13
14
use Mautic\CoreBundle\Controller\CommonController;
15
use Mautic\PluginBundle\Helper\IntegrationHelper;
16
use MauticPlugin\MauticCitrixBundle\Helper\CitrixHelper;
17
use MauticPlugin\MauticCitrixBundle\Model\CitrixModel;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
21
22
class PublicController extends CommonController
23
{
24
    /**
25
     * This proxy is used for the GoToTraining API requests in order to bypass the CORS restrictions in AJAX.
26
     *
27
     * @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
28
     *
29
     * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
30
     * @throws \InvalidArgumentException
31
     */
32
    public function proxyAction(Request $request)
33
    {
34
        $url = $request->query->get('url', null);
35
        if (!$url) {
36
            return $this->accessDenied(false, 'ERROR: url not specified');
37
        } else {
38
            /** @var IntegrationHelper $integrationHelper */
39
            $integrationHelper = $this->get('mautic.helper.integration');
40
            $myIntegration     = $integrationHelper->getIntegrationObject('Gototraining');
41
42
            if (!$myIntegration || !$myIntegration->getIntegrationSettings()->getIsPublished()) {
43
                return $this->accessDenied(false, 'ERROR: GoToTraining is not enabled');
44
            }
45
46
            $ch = curl_init($url);
47
            if (Request::METHOD_POST === $request->getMethod()) {
48
                $headers = [
49
                    'Content-type: application/json',
50
                    'Accept: application/json',
51
                ];
52
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
53
                curl_setopt($ch, CURLOPT_POST, true);
54
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request->request->all()));
55
            }
56
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
57
            curl_setopt($ch, CURLOPT_HEADER, true);
58
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
59
            curl_setopt($ch, CURLOPT_USERAGENT, $request->server->get('HTTP_USER_AGENT', ''));
60
            list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', curl_exec($ch), 2);
0 ignored issues
show
It seems like curl_exec($ch) can also be of type true; however, parameter $subject of preg_split() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', /** @scrutinizer ignore-type */ curl_exec($ch), 2);
Loading history...
61
            $status                  = curl_getinfo($ch);
62
            curl_close($ch);
63
        }
64
65
        // Set the JSON data object contents, decoding it from JSON if possible.
66
        $decoded_json = json_decode($contents);
67
        $data         = $decoded_json ?: $contents;
68
69
        // Generate JSON/JSONP string
70
        $json     = json_encode($data);
71
        $response = new Response($json, $status['http_code']);
72
73
        // Generate appropriate content-type header.
74
        $response->headers->set('Content-type', 'application/'.($request->isXmlHttpRequest() ? 'json' : 'x-javascript'));
75
76
        // Allow CORS requests only from dev machines
77
        $allowedIps = $this->coreParametersHelper->get('dev_hosts') ?: [];
78
        if (in_array($request->getClientIp(), $allowedIps, true)) {
79
            $response->headers->set('Access-Control-Allow-Origin', '*');
80
        }
81
82
        return $response;
83
    }
84
85
    /**
86
     * This action will receive a POST when the session status changes.
87
     * A POST will also be made when a customer joins the session and when the session ends
88
     * (whether or not a customer joined).
89
     *
90
     * @return array|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\RedirectResponse|Response
91
     *
92
     * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
93
     * @throws \InvalidArgumentException
94
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
95
     */
96
    public function sessionChangedAction(Request $request)
97
    {
98
        /** @var IntegrationHelper $integrationHelper */
99
        $integrationHelper = $this->get('mautic.helper.integration');
100
        $myIntegration     = $integrationHelper->getIntegrationObject('Gototraining');
101
102
        if (!$myIntegration || !$myIntegration->getIntegrationSettings()->getIsPublished()) {
103
            return $this->accessDenied(false, 'ERROR: GoToTraining is not enabled');
104
        }
105
106
        $post = $request->request->all();
107
108
        try {
109
            /** @var CitrixModel $citrixModel */
110
            $citrixModel = $this->get('mautic.model.factory')->getModel('citrix.citrix');
111
            $productId   = $post['sessionId'];
112
            $eventDesc   = sprintf('%s (%s)', $productId, $post['status']);
113
            $eventName   = CitrixHelper::getCleanString(
114
                    $eventDesc
115
                ).'_#'.$productId;
116
            $product = 'assist';
117
            $citrixModel->syncEvent($product, $productId, $eventName, $eventDesc);
118
        } catch (\Exception $ex) {
119
            throw new BadRequestHttpException($ex->getMessage());
120
        }
121
122
        return new Response('OK');
123
    }
124
}
125