Usage Guide PHP EngineΒΆ

Supported PHP Versions

  • PHP 5.4
  • PHP 5.6
  • PHP 7.0
  • PHP 7.1

Download the SDK directly from your Control Panel at https://cp.sensitivity.io/sdks/official/c.

  1. Depending on the operating system, you have to add sensitivity.io PHP Extensions to your PHP Installation:
  • Windows .dll libraries
  • Linux .so libraries
  1. List of PHP Extensions:
  • sensitivityio_php_base - Base functions for sensitivity.io engine
  • sensitivityio_php_license - Licensing mechanism for sensitivity.io
  • sensitivityio_php_sds - Scanning Module for sensitivity.io
  1. Create sensitivityio.ini configuration file and add the following lines to it:
  1. For Windows
1
2
3
extension=sensitivityio_php_base.dll
extension=sensitivityio_php_license.dll
extension=sensitivityio_php_sds.dll
  1. For Linux
1
2
3
extension=sensitivityio_php_base.so
extension=sensitivityio_php_license.so
extension=sensitivityio_php_sds.so
  1. Test the installation by running
1
php -m # will list loaded PHP Extensions/Modules. You should see the above listed there

Tip

PHP - You can download specific documentation here: https://static.sensitivity.io/docs/sensitivityio_php_ext_api.chm

You can now start working on your integration.

Code example:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php

// copyright Copyright (C) 2017 CoSoSys Ltd <team@sensitivity.io>
// CoSoSys Ltd. All rights reserved.
//
// authors Ovidiu CICAL <ovidiu@sensitivity.io>

namespace Controller;

use Controller\BaseController;
use Retriever;
use Scanner;
use Loader;
use Threat;

class ScanController extends BaseController
{
    const UPLOAD_ERROR = "Upload error: ";

    public function index()
    {
    $parameters = [
        'accountId'        => ACCOUNT_ID,
        'projectId'        => PROJECT_ID,
        'authKey'          => AUTH_KEY,
        'appId'            => (isset($_SESSION['appId']) && $_SESSION['appId'] != '') ? $_SESSION['appId'] : '',
        'forceLicense'     => true,
        'licenseFilePath'  => LICENSE_FILE_PATH,
        'settingsFilePath' => SETTINGS_FILE_PATH,
        'scanFilePath'     => SCAN_FILE_PATH
    ];

    if (isset($_SESSION['error']) && $_SESSION['error'] != '') {
        $parameters['error'] = $_SESSION['error'];
        unset($_SESSION['error']);
    }

    if (isset($_SESSION['success']) && $_SESSION['success'] != '') {
        $parameters['success'] = $_SESSION['success'];
        unset($_SESSION['success']);
    }

    if (isset($_SESSION['duration']) && $_SESSION['duration'] != '') {
        $parameters['duration'] = $_SESSION['duration'];
        unset($_SESSION['duration']);
    }

    if (isset($_SESSION['threatCount']) && $_SESSION['threatCount'] != '') {
        $parameters['threatCount'] = $_SESSION['threatCount'];
        unset($_SESSION['threatCount']);
    }

    echo $this->render(VIEWSPATH . 'homepage.php', $parameters);
    }

    public function scan()
    {
    $customFile = SCAN_FILE_PATH;

    if ($this->getRequestMethod() != "POST") {
        echo json_encode("Method not allowed!");
    }

    $enableOnline = (isset($_POST['enable_online']) ? (($this->getParameter('POST', 'enable_online')) ? true : false) : ENABLE_ONLINE);
    $accountId = $this->getParameter('POST', 'account_id');
    $projectId = $this->getParameter('POST', 'project_id');
    $authenticationKey = $this->getParameter('POST', 'auth_key');
    $applicationId = $this->getParameter('POST', 'app_id'); // optional

    // Try to fetch uploaded files
    if (isset($_FILES['license_file']) && is_uploaded_file($_FILES['license_file']['tmp_name'])) {
        if (!move_uploaded_file($_FILES['license_file']['tmp_name'], LICENSE_FILE_PATH)) {
        $lastError = error_get_last();
        $this->redirectWithError('/', self::UPLOAD_ERROR . $lastError['message']);
        }
    }
    if (isset($_FILES['settings_file']) && is_uploaded_file($_FILES['settings_file']['tmp_name'])) {
        move_uploaded_file($_FILES['settings_file']['tmp_name'], SETTINGS_FILE_PATH);
    }
    $prefixName = "/tmp/scanFile-" . time() . "-";
    if (isset($_FILES['scan_file']) && is_uploaded_file($_FILES['scan_file']['tmp_name']) && move_uploaded_file($_FILES['scan_file']['tmp_name'], $prefixName . $_FILES['scan_file']['name'])) {
        $customFile = $prefixName . $_FILES['scan_file']['name'];
    }

    // Options
    $enableTestThreatHandler = ($this->getParameter('POST', 'enable_test_threat_handler')) ? true : false;
    $stopAtFirstThreat = ($this->getParameter('POST', 'stop_first_threat')) ? true : false;
    $maskResults = ($this->getParameter('POST', 'mask_results')) ? true : false;

    // Set API credentials
    //printf("Set Credentials...\n");
    $httpRetriever = new Retriever\HttpRetriever();
    $result = $httpRetriever->setCredentials($accountId, $projectId, $authenticationKey, $applicationId);
    if ($result['success'] == false && ($result['message'] != '')) {
        $this->redirectWithError('/', $result['message']);
    }

    //printf("Get App Id...\n");
    $appRetriever = new Retriever\AppIdRetriever();
    $appIdResult = $appRetriever->getAppId($applicationId);
    if ($appIdResult['success'] == false && ($appIdResult['message'] != '')) {
        $this->redirectWithError('/', $appIdResult['message']);
    }

    $appId = $appIdResult['data'];
    $_SESSION['appId'] = $appId;

    //printf("License... \n");
    if ($enableOnline) {
        // If ENABLE_ONLINE is set to true, try to load the license file from the API
        $licenseRetriever = new Retriever\LicenseRetriever();
        $statusDestinationLicense = $licenseRetriever->setDestinationFile(LICENSE_FILE_PATH);
        if ($statusDestinationLicense['success'] == false && ($statusDestinationLicense['message'] != '')) {
        $this->redirectWithError('/', $statusDestinationLicense['message']);
        }

        $statusNotifyLicense = $licenseRetriever->retrieveToFileAndNotifyLoader();
        if ($statusNotifyLicense['success'] == false && ($statusNotifyLicense['messsage'] != '')) {
        $this->redirectWithError('/', $statusNotifyLicense['message']);
        }
    } else {
        // Else, try to load the license file from the disk
        $licenseLoader = new Loader\LicenseLoader();
        $statusLoadLicense = $licenseLoader->getLicenseFromFile(LICENSE_FILE_PATH);
        if ($statusLoadLicense['success'] == false && ($statusLoadLicense['message'] != '')) {
        $this->redirectWithError('/', $statusLoadLicense['message']);
        }
    }

    //printf("Scanner... \n");
    $scannerObj = new Scanner\SdsScanner();
    $scannerResult = $scannerObj->getScanner();
    if ($scannerResult['success'] == false && ($scannerResult['message'] != '')) {
        $this->redirectWithError('/', $scannerResult['message']);
    }
    $scanner = $scannerResult['data'];

    //printf("Settings... \n");
    $settingsLoader = new \Loader\SettingsLoader();
    $statusRegisterScanner = $settingsLoader->registerScanner($scanner);
    if ($statusRegisterScanner['success'] == false && ($statusRegisterScanner['message'] != '')) {
        $this->redirectWithError('/', $statusRegisterScanner['message']);
    }

    $settingsLoaderObject = $settingsLoader->getLoader();
    if ($settingsLoaderObject['success'] == false && ($settingsLoaderObject['message'] != '')) {
        $this->redirectWithError('/', $settingsLoaderObject['message']);
    }

    $settingsLoaderResource = $settingsLoaderObject['data'];

    if ($enableOnline) {
        $settingsRetriever = new \Retriever\SettingsRetriever();
        $statusDestinationSettings = $settingsRetriever->setDestinationFile(SETTINGS_FILE_PATH);
        if ($statusDestinationSettings['success'] == false && ($statusDestinationSettings['message'] != '')) {
        $this->redirectWithError('/', $statusDestinationSettings['message']);
        }

        $statusSettingsLoader = $settingsRetriever->setSettingsLoader($settingsLoaderResource);
        if ($statusSettingsLoader['success'] == false && ($statusSettingsLoader['message'] != '')) {
        $this->redirectWithError('/', $statusSettingsLoader['message']);
        }

        $statusNotifySettings = $settingsRetriever->retrieveToFileAndNotifyLoader();
        if ($statusNotifySettings['success'] == false && ($statusNotifySettings['message'] != '')) {
        $this->redirectWithError('/', $statusNotifySettings['message']);
        }

        /** @todo CHECK IF NEEDED */
        unset($settingsRetriever);
    } else {
        $statusLoadSettings = $settingsLoader->getSettingsFromFile(SETTINGS_FILE_PATH);
        if ($statusLoadSettings['success'] == false && ($statusLoadSettings['message'] != '')) {
        $this->redirectWithError('/', $statusLoadSettings['message']);
        }
    }

    $threatCount = 0;
    $startTime = microtime(true);
    //printf("Scanning...\n");
    if ($enableTestThreatHandler) {

        $threatHandlerStruct = new Threat\ThreatHandlerStruct(0, 8, []);

    $threatsResult = $scannerObj->scanFileWithThreatHandler($customFile, $threatHandlerStruct);

        if ($threatsResult['success'] == false && ($threatsResult['message'] != '')) {
        $this->redirectWithError('/', $threatsResult['message']);
        }

        $threats = $threatHandlerStruct->threats;
        $threatCount = count($threats);

        $threatReport = "Found " . $threatCount . " threats\n";
        for ($index = 0; $index < $threatCount; ++$index) {
        $threat = new Threat\Threat($threats[$index]);

        $threatReport .= $threat->getThreatInfo($index);
        unset($threat);
        }
    } else {
        $finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension
        if (finfo_file($finfo, $customFile) == "application/pdf") {
        // PDF content
        $this->redirectWithError('/', "Scan PDF is not enable, please contact team@sensitivity.io");
        //$threatsResult = $scannerObj->scanBuffer($customFile, $stopAtFirstThreat);
        } else {
        // Scan File
        $threatsResult = $scannerObj->scanFile($customFile, $stopAtFirstThreat);
        }
        if ($threatsResult['success'] == false && ($threatsResult['message'] != '')) {
        $this->redirectWithError('/', $threatsResult['message']);
        }

        $threats = new Threat\Threats($threatsResult['data']);
        $threatCount = $threats->getSize();

        $threatReport = "Found " . ($threatCount) . " threats\n";
        for ($index = 0; $index < $threatCount; ++$index) {
        $threatElementResult = $threats->getElement($index);
        if ($threatElementResult['success'] == false && ($threatElementResult['message'] != '')) {
            $threatReport .= $threatElementResult['message'] . "\n";
            continue;
        }

        $threat = new Threat\Threat($threatElementResult['data']);

        $threatReport .= $threat->getThreatInfo($index, $maskResults);
        unset($threat);
        }
    }
    $endTime = microtime(true);

    unset($threats);

    $settingsLoader->unregisterScanner($scanner);
    unset($settingsLoader);
    unset($scanner);

    $_SESSION['duration'] = round(($endTime - $startTime), 4);
    $_SESSION['threatCount'] = $threatCount;

    $this->redirectWithSuccess('/', $threatReport);
    }
}