~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Xmds/Soap4.php

  • Committer: Dan Garner
  • Date: 2015-08-11 09:29:02 UTC
  • mto: This revision was merged to the branch mainline in revision 453.
  • Revision ID: git-v1:a86fb4369b7395c13367577d23b14c0ab4528c1a
Transitions fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 */
21
21
namespace Xibo\Xmds;
22
22
 
23
 
use Intervention\Image\ImageManagerStatic as Img;
 
23
use Xibo\Controller\Library;
24
24
use Xibo\Entity\Bandwidth;
25
25
use Xibo\Entity\Display;
26
26
use Xibo\Exception\NotFoundException;
27
 
use Xibo\Exception\XiboException;
28
 
 
29
 
/**
30
 
 * Class Soap4
31
 
 * @package Xibo\Xmds
32
 
 */
 
27
use Xibo\Factory\DisplayFactory;
 
28
use Xibo\Factory\LayoutFactory;
 
29
use Xibo\Factory\MediaFactory;
 
30
use Xibo\Factory\XmdsNonceFactory;
 
31
use Xibo\Helper\Config;
 
32
use Xibo\Helper\Log;
 
33
use Xibo\Helper\Sanitize;
 
34
 
 
35
 
33
36
class Soap4 extends Soap
34
37
{
35
38
    /**
45
48
     * @return string
46
49
     * @throws \SoapFault
47
50
     */
48
 
    public function RegisterDisplay($serverKey, $hardwareKey, $displayName, $clientType, $clientVersion, $clientCode, $operatingSystem, $macAddress, $xmrChannel = null, $xmrPubKey = null)
 
51
    public function RegisterDisplay($serverKey, $hardwareKey, $displayName, $clientType, $clientVersion, $clientCode, $operatingSystem, $macAddress)
49
52
    {
50
 
        $this->logProcessor->setRoute('RegisterDisplay');
51
 
 
52
53
        // Sanitize
53
 
        $serverKey = $this->getSanitizer()->string($serverKey);
54
 
        $hardwareKey = $this->getSanitizer()->string($hardwareKey);
55
 
        $displayName = $this->getSanitizer()->string($displayName);
56
 
        $clientType = $this->getSanitizer()->string($clientType);
57
 
        $clientVersion = $this->getSanitizer()->string($clientVersion);
58
 
        $clientCode = $this->getSanitizer()->int($clientCode);
59
 
        $macAddress = $this->getSanitizer()->string($macAddress);
60
 
        $clientAddress = $this->getIp();
 
54
        $serverKey = Sanitize::string($serverKey);
 
55
        $hardwareKey = Sanitize::string($hardwareKey);
 
56
        $displayName = Sanitize::string($displayName);
 
57
        $clientType = Sanitize::string($clientType);
 
58
        $clientVersion = Sanitize::string($clientVersion);
 
59
        $clientCode = Sanitize::int($clientCode);
 
60
        $macAddress = Sanitize::string($macAddress);
 
61
        $clientAddress = Sanitize::getString('REMOTE_ADDR');
 
62
 
 
63
        // Audit in
 
64
        Log::debug('serverKey: ' . $serverKey . ', hardwareKey: ' . $hardwareKey . ', displayName: ' . $displayName);
61
65
 
62
66
        // Check the serverKey matches
63
 
        if ($serverKey != $this->getConfig()->GetSetting('SERVER_KEY'))
 
67
        if ($serverKey != Config::GetSetting('SERVER_KEY'))
64
68
            throw new \SoapFault('Sender', 'The Server key you entered does not match with the server key at this address');
65
69
 
66
70
        // Check the Length of the hardwareKey
74
78
 
75
79
        // Check in the database for this hardwareKey
76
80
        try {
77
 
            $display = $this->displayFactory->getByLicence($hardwareKey);
78
 
            $this->display = $display;
79
 
 
80
 
            $this->logProcessor->setDisplay($display->displayId, ($display->isAuditing()));
81
 
 
82
 
            // Audit in
83
 
            $this->getLog()->debug('serverKey: ' . $serverKey . ', hardwareKey: ' . $hardwareKey . ', displayName: ' . $displayName . ', macAddress: ' . $macAddress);
84
 
 
85
 
            // Now
86
 
            $dateNow = $this->getDate()->parse();
87
 
 
88
 
            // Append the time
89
 
            $displayElement->setAttribute('date', $this->getDate()->getLocalDate($dateNow));
90
 
            $displayElement->setAttribute('timezone', $this->getConfig()->GetSetting('defaultTimezone'));
 
81
            $display = DisplayFactory::getByLicence($hardwareKey);
91
82
 
92
83
            // Determine if we are licensed or not
93
84
            if ($display->licensed == 0) {
108
99
 
109
100
                // Create the XML nodes
110
101
                foreach ($settings as $arrayItem) {
111
 
 
112
102
                    $node = $return->createElement($arrayItem['name'], (isset($arrayItem['value']) ? $arrayItem['value'] : $arrayItem['default']));
113
103
                    $node->setAttribute('type', $arrayItem['type']);
114
104
                    $displayElement->appendChild($node);
115
105
                }
116
106
 
117
107
                // Add some special settings
118
 
                $nodeName = ($clientType == 'windows') ? 'DisplayName' : 'displayName';
119
 
                $node = $return->createElement($nodeName);
120
 
                $node->appendChild($return->createTextNode($display->display));
 
108
                $nodeName = ($display->clientType == 'windows') ? 'DisplayName' : 'displayName';
 
109
                $node = $return->createElement($nodeName, $display->display);
121
110
                $node->setAttribute('type', 'string');
122
111
                $displayElement->appendChild($node);
123
112
 
124
 
                $nodeName = ($clientType == 'windows') ? 'ScreenShotRequested' : 'screenShotRequested';
 
113
                $nodeName = ($display->clientType == 'windows') ? 'ScreenShotRequested' : 'screenShotRequested';
125
114
                $node = $return->createElement($nodeName, $display->screenShotRequested);
126
115
                $node->setAttribute('type', 'checkbox');
127
116
                $displayElement->appendChild($node);
128
117
 
129
 
                $nodeName = ($clientType == 'windows') ? 'DisplayTimeZone' : 'displayTimeZone';
130
 
                $node = $return->createElement($nodeName, (!empty($display->timeZone)) ? $display->timeZone : '');
131
 
                $node->setAttribute('type', 'string');
132
 
                $displayElement->appendChild($node);
133
 
 
134
 
                if (!empty($display->timeZone)) {
135
 
                    // Calculate local time
136
 
                    $dateNow->timezone($display->timeZone);
137
 
 
138
 
                    // Append Local Time
139
 
                    $displayElement->setAttribute('localDate', $this->getDate()->getLocalDate($dateNow));
140
 
                }
141
 
 
142
118
                // Send Notification if required
143
 
                $this->alertDisplayUp();
 
119
                $this->AlertDisplayUp($display->displayId, $display->display, $display->loggedIn, $display->emailAlert);
144
120
            }
145
121
 
146
122
        } catch (NotFoundException $e) {
147
123
 
148
124
            // Add a new display
149
125
            try {
150
 
                $display = $this->displayFactory->createEmpty();
151
 
                $this->display = $display;
 
126
                $display = new Display();
152
127
                $display->display = $displayName;
153
 
                $display->auditingUntil = 0;
154
 
                $display->defaultLayoutId = $this->getConfig()->GetSetting('DEFAULT_LAYOUT', 4);
 
128
                $display->isAuditing = 0;
 
129
                $display->defaultLayoutId = 4;
155
130
                $display->license = $hardwareKey;
156
131
                $display->licensed = 0;
157
132
                $display->incSchedule = 0;
174
149
        $display->clientType = $clientType;
175
150
        $display->clientVersion = $clientVersion;
176
151
        $display->clientCode = $clientCode;
177
 
        //$display->operatingSystem = $operatingSystem;
178
 
        $display->save(['validate' => false, 'audit' => false]);
 
152
        $display->save(false);
179
153
 
180
154
        // Log Bandwidth
181
155
        $returnXml = $return->saveXML();
182
 
        $this->logBandwidth($display->displayId, Bandwidth::$REGISTER, strlen($returnXml));
 
156
        $this->LogBandwidth($display->displayId, Bandwidth::$REGISTER, strlen($returnXml));
183
157
 
184
158
        // Audit our return
185
 
        $this->getLog()->debug($returnXml);
 
159
        if ($display->isAuditing == 1)
 
160
            Log::debug($returnXml, $display->displayId);
186
161
 
187
162
        return $returnXml;
188
163
    }
196
171
     */
197
172
    function RequiredFiles($serverKey, $hardwareKey)
198
173
    {
199
 
        $httpDownloads = ($this->getConfig()->GetSetting('SENDFILE_MODE') != 'Off');
 
174
        $httpDownloads = (Config::GetSetting('SENDFILE_MODE') != 'Off');
200
175
        return $this->doRequiredFiles($serverKey, $hardwareKey, $httpDownloads);
201
176
    }
202
177
 
213
188
     */
214
189
    function GetFile($serverKey, $hardwareKey, $fileId, $fileType, $chunkOffset, $chunkSize)
215
190
    {
216
 
        $this->logProcessor->setRoute('GetFile');
217
 
 
218
191
        // Sanitize
219
 
        $serverKey = $this->getSanitizer()->string($serverKey);
220
 
        $hardwareKey = $this->getSanitizer()->string($hardwareKey);
221
 
        $fileId = $this->getSanitizer()->int($fileId);
222
 
        $fileType = $this->getSanitizer()->string($fileType);
223
 
        $chunkOffset = $this->getSanitizer()->int($chunkOffset);
224
 
        $chunkSize = $this->getSanitizer()->int($chunkSize);
 
192
        $serverKey = Sanitize::string($serverKey);
 
193
        $hardwareKey = Sanitize::string($hardwareKey);
 
194
        $fileId = Sanitize::int($fileId);
 
195
        $fileType = Sanitize::string($fileType);
 
196
        $chunkOffset = Sanitize::int($chunkOffset);
 
197
        $chunkSize = Sanitize::int($chunkSize);
225
198
 
226
 
        $libraryLocation = $this->getConfig()->GetSetting("LIBRARY_LOCATION");
 
199
        $libraryLocation = Config::GetSetting("LIBRARY_LOCATION");
227
200
 
228
201
        // Check the serverKey matches
229
 
        if ($serverKey != $this->getConfig()->GetSetting('SERVER_KEY'))
 
202
        if ($serverKey != Config::GetSetting('SERVER_KEY'))
230
203
            throw new \SoapFault('Sender', 'The Server key you entered does not match with the server key at this address');
231
204
 
232
205
        // Make sure we are sticking to our bandwidth limit
233
 
        if (!$this->checkBandwidth())
 
206
        if (!$this->CheckBandwidth())
234
207
            throw new \SoapFault('Receiver', "Bandwidth Limit exceeded");
235
208
 
236
209
        // Authenticate this request...
237
 
        if (!$this->authDisplay($hardwareKey))
 
210
        if (!$this->AuthDisplay($hardwareKey))
238
211
            throw new \SoapFault('Receiver', "This display client is not licensed");
239
212
 
240
 
        if ($this->display->isAuditing())
241
 
            $this->getLog()->debug('hardwareKey: ' . $hardwareKey . ', fileId: ' . $fileId . ', fileType: ' . $fileType . ', chunkOffset: ' . $chunkOffset . ', chunkSize: ' . $chunkSize);
 
213
        if ($this->display->isAuditing == 1)
 
214
            Log::debug('hardwareKey: ' . $hardwareKey . ', fileId: ' . $fileId . ', fileType: ' . $fileType . ', chunkOffset: ' . $chunkOffset . ', chunkSize: ' . $chunkSize);
242
215
 
243
216
        try {
244
217
            if ($fileType == "layout") {
245
 
                $fileId = $this->getSanitizer()->int($fileId);
 
218
                $fileId = Sanitize::int($fileId);
246
219
 
247
220
                // Validate the nonce
248
 
                $requiredFile = $this->requiredFileFactory->getByDisplayAndLayout($this->display->displayId, $fileId);
 
221
                if (count(XmdsNonceFactory::getByDisplayAndLayout($this->display->displayId, $fileId)) <= 0)
 
222
                    throw new NotFoundException('Invalid Nonce for ' . $fileId);
249
223
 
250
224
                // Load the layout
251
 
                $layout = $this->layoutFactory->getById($fileId);
 
225
                $layout = LayoutFactory::getById($fileId);
252
226
                $path = $layout->xlfToDisk();
253
227
 
254
228
                $file = file_get_contents($path);
255
229
                $chunkSize = filesize($path);
256
230
 
257
 
                $requiredFile->bytesRequested = $requiredFile->bytesRequested + $chunkSize;
258
 
                $requiredFile->save();
259
 
 
260
231
            } else if ($fileType == "media") {
261
232
                // Validate the nonce
262
 
                $requiredFile = $this->requiredFileFactory->getByDisplayAndMedia($this->display->displayId, $fileId);
263
 
 
264
 
                $media = $this->mediaFactory->getById($fileId);
265
 
                $this->getLog()->debug(json_encode($media));
266
 
 
267
 
                if (!file_exists($libraryLocation . $media->storedAs))
268
 
                    throw new NotFoundException('Media exists but file missing from library. ' . $libraryLocation);
 
233
                if (count(XmdsNonceFactory::getByDisplayAndMedia($this->display->displayId, $fileId)) <= 0)
 
234
                    throw new NotFoundException('Invalid Nonce for ' . $fileId);
 
235
 
 
236
                $media = MediaFactory::getById($fileId);
269
237
 
270
238
                // Return the Chunk size specified
271
 
                if (!$f = fopen($libraryLocation . $media->storedAs, 'r'))
272
 
                    throw new NotFoundException('Unable to get file pointer');
 
239
                $f = fopen($libraryLocation . $media->storedAs, 'r');
273
240
 
274
241
                fseek($f, $chunkOffset);
275
242
 
278
245
                // Store file size for bandwidth log
279
246
                $chunkSize = strlen($file);
280
247
 
281
 
                if ($chunkSize === 0)
282
 
                    throw new NotFoundException('Empty file');
283
 
 
284
 
                $requiredFile->bytesRequested = $requiredFile->bytesRequested + $chunkSize;
285
 
                $requiredFile->save();
286
 
 
287
248
            } else {
288
249
                throw new NotFoundException('Unknown FileType Requested.');
289
250
            }
290
251
        }
291
252
        catch (NotFoundException $e) {
292
 
            $this->getLog()->error('Not found FileId: ' . $fileId . '. FileType: ' . $fileType . '. ' . $e->getMessage());
 
253
            Log::error($e->getMessage());
293
254
            throw new \SoapFault('Receiver', 'Requested an invalid file.');
294
255
        }
295
256
 
296
257
        // Log Bandwidth
297
 
        $this->logBandwidth($this->display->displayId, Bandwidth::$GETFILE, $chunkSize);
 
258
        $this->LogBandwidth($this->display->displayId, Bandwidth::$GETFILE, $chunkSize);
298
259
 
299
260
        return $file;
300
261
    }
390
351
     */
391
352
    public function NotifyStatus($serverKey, $hardwareKey, $status)
392
353
    {
393
 
        $this->logProcessor->setRoute('NotifyStatus');
394
 
 
395
354
        // Sanitize
396
 
        $serverKey = $this->getSanitizer()->string($serverKey);
397
 
        $hardwareKey = $this->getSanitizer()->string($hardwareKey);
 
355
        $serverKey = Sanitize::string($serverKey);
 
356
        $hardwareKey = Sanitize::string($hardwareKey);
398
357
 
399
358
        // Check the serverKey matches
400
 
        if ($serverKey != $this->getConfig()->GetSetting('SERVER_KEY'))
 
359
        if ($serverKey != Config::GetSetting('SERVER_KEY'))
401
360
            throw new \SoapFault('Sender', 'The Server key you entered does not match with the server key at this address');
402
361
 
403
362
        // Make sure we are sticking to our bandwidth limit
404
 
        if (!$this->checkBandwidth())
 
363
        if (!$this->CheckBandwidth())
405
364
            throw new \SoapFault('Receiver', "Bandwidth Limit exceeded");
406
365
 
407
366
        // Auth this request...
408
 
        if (!$this->authDisplay($hardwareKey))
 
367
        if (!$this->AuthDisplay($hardwareKey))
409
368
            throw new \SoapFault('Receiver', 'This display client is not licensed');
410
369
 
411
 
        // Important to keep this logging in place (status screen notification gets logged)
412
 
        if ($this->display->isAuditing())
413
 
            $this->getLog()->debug($status);
 
370
        if ($this->display->isAuditing == 1)
 
371
            Log::debug($status);
414
372
 
415
 
        $this->logBandwidth($this->display->displayId, Bandwidth::$NOTIFYSTATUS, strlen($status));
 
373
        $this->LogBandwidth($this->display->displayId, Bandwidth::$NOTIFYSTATUS, strlen($status));
416
374
 
417
375
        $status = json_decode($status, true);
418
376
 
419
 
        $this->display->storageAvailableSpace = $this->getSanitizer()->getInt('availableSpace', $this->display->storageAvailableSpace, $status);
420
 
        $this->display->storageTotalSpace = $this->getSanitizer()->getInt('totalSpace', $this->display->storageTotalSpace, $status);
421
 
        $this->display->lastCommandSuccess = $this->getSanitizer()->getCheckbox('lastCommandSuccess', $this->display->lastCommandSuccess, $status);
422
 
        $this->display->deviceName = $this->getSanitizer()->getString('deviceName', $this->display->deviceName, $status);
423
 
 
424
 
        if ($this->getConfig()->GetSetting('DISPLAY_LOCK_NAME_TO_DEVICENAME') == 1 && $this->display->hasPropertyChanged('deviceName')) {
425
 
            $this->display->display = $this->display->deviceName;
426
 
        }
427
 
 
428
 
        // Timezone
429
 
        $timeZone = $this->getSanitizer()->getString('timeZone', $status);
430
 
 
431
 
        if (!empty($timeZone)) {
432
 
            // Validate the provided data and log/ignore if not well formatted
433
 
            if (array_key_exists($timeZone, $this->getDate()->timezoneList())) {
434
 
                $this->display->timeZone = $timeZone;
435
 
            } else {
436
 
                $this->getLog()->info('Ignoring Incorrect timezone string: ' . $timeZone);
437
 
            }
438
 
        }
439
 
 
440
 
        // Current Layout
441
 
        $currentLayoutId = $this->getSanitizer()->getInt('currentLayoutId', $status);
442
 
 
443
 
        if ($currentLayoutId !== null) {
444
 
            $this->display->setCurrentLayoutId($this->getPool(), $currentLayoutId);
445
 
        }
 
377
        $this->display->currentLayoutId = Sanitize::getInt('currentLayoutId', $this->display->currentLayoutId, $status);
 
378
        $this->display->storageAvailableSpace = Sanitize::getInt('availableSpace', $this->display->storageAvailableSpace, $status);
 
379
        $this->display->storageTotalSpace = Sanitize::getInt('totalSpace', $this->display->storageTotalSpace, $status);
446
380
 
447
381
        // Touch the display record
448
 
        try {
449
 
            if (count($this->display->getChangedProperties()) > 0)
450
 
                $this->display->save(Display::$saveOptionsMinimum);
451
 
        } catch (XiboException $xiboException) {
452
 
            $this->getLog()->error($xiboException->getMessage());
453
 
            throw new \SoapFault('Receiver', 'Unable to save status update');
454
 
        }
 
382
        $this->display->save(false, false);
455
383
 
456
384
        return true;
457
385
    }
466
394
     */
467
395
    public function SubmitScreenShot($serverKey, $hardwareKey, $screenShot)
468
396
    {
469
 
        $this->logProcessor->setRoute('SubmitScreenShot');
470
 
 
471
397
        // Sanitize
472
 
        $serverKey = $this->getSanitizer()->string($serverKey);
473
 
        $hardwareKey = $this->getSanitizer()->string($hardwareKey);
474
 
 
475
 
        $screenShotFmt = "jpg";
476
 
        $screenShotMime = "image/jpeg";
477
 
        $screenShotImg = false;
478
 
 
479
 
        $converted = false;
480
 
        $needConversion = false;
 
398
        $serverKey = Sanitize::string($serverKey);
 
399
        $hardwareKey = Sanitize::string($hardwareKey);
481
400
 
482
401
        // Check the serverKey matches
483
 
        if ($serverKey != $this->getConfig()->GetSetting('SERVER_KEY'))
 
402
        if ($serverKey != Config::GetSetting('SERVER_KEY'))
484
403
            throw new \SoapFault('Sender', 'The Server key you entered does not match with the server key at this address');
485
404
 
486
405
        // Make sure we are sticking to our bandwidth limit
487
 
        if (!$this->checkBandwidth())
 
406
        if (!$this->CheckBandwidth())
488
407
            throw new \SoapFault('Receiver', "Bandwidth Limit exceeded");
489
408
 
490
409
        // Auth this request...
491
 
        if (!$this->authDisplay($hardwareKey))
 
410
        if (!$this->AuthDisplay($hardwareKey))
492
411
            throw new \SoapFault('Receiver', 'This display client is not licensed');
493
412
 
494
 
        if ($this->display->isAuditing())
495
 
            $this->getLog()->debug('Received Screen shot');
 
413
        if ($this->display->isAuditing == 1)
 
414
            Log::debug('Received Screen shot');
496
415
 
497
416
        // Open this displays screen shot file and save this.
498
 
        $location = $this->getConfig()->GetSetting('LIBRARY_LOCATION') . 'screenshots/' . $this->display->displayId . '_screenshot.' . $screenShotFmt;
499
 
 
500
 
        foreach(array('imagick', 'gd') as $imgDriver) {
501
 
            Img::configure(array('driver' => $imgDriver));
502
 
            try {
503
 
                $screenShotImg = Img::make($screenShot);
504
 
            } catch (\Exception $e) {
505
 
                if ($this->display->isAuditing())
506
 
                    $this->getLog()->debug($imgDriver . " - " . $e->getMessage());
507
 
            }
508
 
            if($screenShotImg !== false) {
509
 
                if ($this->display->isAuditing())
510
 
                    $this->getLog()->debug("Use " . $imgDriver);
511
 
                break;
512
 
            }
513
 
        }
514
 
 
515
 
        if ($screenShotImg !== false) {
516
 
            $imgMime = $screenShotImg->mime(); 
517
 
 
518
 
            if($imgMime != $screenShotMime) {
519
 
                $needConversion = true;
520
 
                try {
521
 
                    if ($this->display->isAuditing())
522
 
                        $this->getLog()->debug("converting: '" . $imgMime . "' to '" . $screenShotMime . "'");
523
 
                    $screenShot = (string) $screenShotImg->encode($screenShotFmt);
524
 
                    $converted = true;
525
 
                } catch (\Exception $e) {
526
 
                    if ($this->display->isAuditing())
527
 
                        $this->getLog()->debug($e->getMessage());
528
 
                }
529
 
            }
530
 
        }
531
 
 
532
 
        // return early with false, keep screenShotRequested intact, let the Player retry.
533
 
        if ($needConversion && !$converted) {
534
 
            $this->logBandwidth($this->display->displayId, Bandwidth::$SCREENSHOT, filesize($location));
535
 
            throw new \SoapFault('Receiver', __('Incorrect Screen shot Format'));
536
 
        }
537
 
 
538
 
 
 
417
        Library::ensureLibraryExists();
 
418
        $location = Config::GetSetting('LIBRARY_LOCATION') . 'screenshots/' . $this->display->displayId . '_screenshot.jpg';
539
419
        $fp = fopen($location, 'wb');
540
420
        fwrite($fp, $screenShot);
541
421
        fclose($fp);
542
422
 
543
423
        // Touch the display record
544
424
        $this->display->screenShotRequested = 0;
545
 
        $this->display->save(Display::$saveOptionsMinimum);
 
425
        $this->display->save(false, false);
546
426
 
547
 
        $this->logBandwidth($this->display->displayId, Bandwidth::$SCREENSHOT, filesize($location));
 
427
        $this->LogBandwidth($this->display->displayId, Bandwidth::$SCREENSHOT, filesize($location));
548
428
 
549
429
        return true;
550
430
    }