Skip to content
This repository was archived by the owner on Sep 10, 2023. It is now read-only.

Commit d793da9

Browse files
committed
Added validation of file extension to all occurrences of and .
1 parent f912b62 commit d793da9

7 files changed

Lines changed: 360 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# CHANGELOG
22

3+
## 1.0.6 - 2016-06-02
4+
5+
* Documentation fixes.
6+
* Added validation of file extension to all occurrences of $templateFilename and $documentFilename.
7+
38
## 1.0.5 - 2016-06-01
49

5-
* Mainly documentation fixes.
10+
* Documentation fixes.
611

712
## 1.0.4 - 2016-06-01
813

914
* First public release.
1015

1116
## 1.0.3 - 2016-06-01
1217

13-
* Implemented end point `/v1/templates/pagecount`.
14-
* Implemented end point `/v1/templates/exists`.
18+
* Implemented end-point `/v1/templates/pagecount`.
19+
* Implemented end-point `/v1/templates/exists`.
1520

1621
## 1.0.2 - 2016-05-30
1722

src/ReportingCloud.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
use GuzzleHttp\RequestOptions;
1717
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
1818
use TxTextControl\ReportingCloud\PropertyMap;
19+
20+
use TxTextControl\ReportingCloud\Validator\DocumentExtension as DocumentExtensionValidator;
1921
use TxTextControl\ReportingCloud\Validator\ImageFormats as ImageFormatsValidator;
2022
use TxTextControl\ReportingCloud\Validator\Page as PageValidator;
2123
use TxTextControl\ReportingCloud\Validator\ReturnFormats as ReturnFormatsValidator;
24+
use TxTextControl\ReportingCloud\Validator\TemplateExtension as TemplateExtensionValidator;
2225
use TxTextControl\ReportingCloud\Validator\TemplateName as TemplateNameValidator;
2326
use TxTextControl\ReportingCloud\Validator\Timestamp as TimeStampValidator;
2427
use TxTextControl\ReportingCloud\Validator\ZoomFactor as ZoomFactorValidator;
@@ -332,6 +335,16 @@ public function uploadTemplate($templateFilename)
332335
{
333336
$ret = false;
334337

338+
$templateExtensionValidator = new TemplateExtensionValidator();
339+
340+
if (!$templateExtensionValidator->isValid($templateFilename)) {
341+
throw new InvalidArgumentException(
342+
sprintf("'templateFilename' %s does not have a supported file extension",
343+
$templateFilename
344+
)
345+
);
346+
}
347+
335348
if (!is_readable($templateFilename)) {
336349
throw new InvalidArgumentException(
337350
sprintf("'templateFilename' %s cannot be read from the local file system",
@@ -386,7 +399,16 @@ public function convertDocument($documentFilename, $returnFormat)
386399
{
387400
$ret = null;
388401

389-
$returnFormatsValidator = new ReturnFormatsValidator();
402+
$documentExtensionValidator = new DocumentExtensionValidator();
403+
$returnFormatsValidator = new ReturnFormatsValidator();
404+
405+
if (!$documentExtensionValidator->isValid($documentFilename)) {
406+
throw new InvalidArgumentException(
407+
sprintf("'documentFilename' %s does not have a supported file extension",
408+
$documentFilename
409+
)
410+
);
411+
}
390412

391413
if (!is_readable($documentFilename)) {
392414
throw new InvalidArgumentException(
@@ -456,9 +478,10 @@ public function mergeDocument($mergeData, $returnFormat, $templateName = null, $
456478
{
457479
$ret = null;
458480

459-
$returnFormatsValidator = new ReturnFormatsValidator();
460-
$templateNameValidator = new TemplateNameValidator();
461-
$timestampValidator = new TimestampValidator();
481+
$returnFormatsValidator = new ReturnFormatsValidator();
482+
$templateExtensionValidator = new TemplateExtensionValidator();
483+
$templateNameValidator = new TemplateNameValidator();
484+
$timestampValidator = new TimestampValidator();
462485

463486
if (!$returnFormatsValidator->isValid($returnFormat)) {
464487
throw new InvalidArgumentException(
@@ -478,6 +501,13 @@ public function mergeDocument($mergeData, $returnFormat, $templateName = null, $
478501
}
479502

480503
if (null !== $templateFilename) {
504+
if (!$templateExtensionValidator->isValid($templateFilename)) {
505+
throw new InvalidArgumentException(
506+
sprintf("'templateFilename' %s does not have a supported file extension",
507+
$templateFilename
508+
)
509+
);
510+
}
481511
if (!is_readable($templateFilename)) {
482512
throw new InvalidArgumentException(
483513
sprintf("'templateFilename' %s cannot be read from the local file system",
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* ReportingCloud PHP Wrapper
5+
*
6+
* Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
7+
*
8+
* @link http://www.reporting.cloud to learn more about ReportingCloud
9+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11+
* @copyright © 2016 Text Control GmbH
12+
*/
13+
namespace TxTextControl\ReportingCloud\Validator;
14+
15+
use Zend\Validator\InArray as InArrayValidator;
16+
17+
/**
18+
* DocumentExtension validator
19+
*
20+
* @package TxTextControl\ReportingCloud
21+
* @author Jonathan Maron (@JonathanMaron)
22+
*/
23+
class DocumentExtension extends AbstractValidator
24+
{
25+
/**
26+
* Unsupported file extension
27+
*
28+
* @const INVALID_EXTENSION
29+
*/
30+
const INVALID_EXTENSION = 'documentExtensionInvalidExtension';
31+
32+
/**
33+
* Message templates
34+
*
35+
* @var array
36+
*/
37+
protected $messageTemplates = [
38+
self::INVALID_EXTENSION => "'%value%' contains an unsupported file extension for a document file",
39+
];
40+
41+
/**
42+
* Returns true, if value is valid. False otherwise.
43+
*
44+
* @param mixed $value
45+
*
46+
* @return bool
47+
*/
48+
public function isValid($value)
49+
{
50+
$extension = pathinfo($value, PATHINFO_EXTENSION);
51+
$extension = strtoupper($extension);
52+
53+
$inArrayValidator = new InArrayValidator();
54+
55+
$inArrayValidator->setHaystack([
56+
'DOC' ,
57+
'DOCX',
58+
'HTML',
59+
'PDF' ,
60+
'RTF' ,
61+
'TX'
62+
]);
63+
64+
if (!$inArrayValidator->isValid($extension)) {
65+
$this->error(self::INVALID_EXTENSION);
66+
return false;
67+
}
68+
69+
return true;
70+
}
71+
72+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* ReportingCloud PHP Wrapper
5+
*
6+
* Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
7+
*
8+
* @link http://www.reporting.cloud to learn more about ReportingCloud
9+
* @link https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10+
* @license https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11+
* @copyright © 2016 Text Control GmbH
12+
*/
13+
namespace TxTextControl\ReportingCloud\Validator;
14+
15+
use Zend\Validator\InArray as InArrayValidator;
16+
17+
/**
18+
* TemplateExtension validator
19+
*
20+
* @package TxTextControl\ReportingCloud
21+
* @author Jonathan Maron (@JonathanMaron)
22+
*/
23+
class TemplateExtension extends AbstractValidator
24+
{
25+
/**
26+
* Unsupported file extension
27+
*
28+
* @const INVALID_EXTENSION
29+
*/
30+
const INVALID_EXTENSION = 'templateExtensionInvalidExtension';
31+
32+
/**
33+
* Message templates
34+
*
35+
* @var array
36+
*/
37+
protected $messageTemplates = [
38+
self::INVALID_EXTENSION => "'%value%' contains an unsupported file extension for a template file",
39+
];
40+
41+
/**
42+
* Returns true, if value is valid. False otherwise.
43+
*
44+
* @param mixed $value
45+
*
46+
* @return bool
47+
*/
48+
public function isValid($value)
49+
{
50+
$extension = pathinfo($value, PATHINFO_EXTENSION);
51+
$extension = strtoupper($extension);
52+
53+
$inArrayValidator = new InArrayValidator();
54+
55+
$inArrayValidator->setHaystack([
56+
'DOC' ,
57+
'DOCX',
58+
'RTF' ,
59+
'TX'
60+
]);
61+
62+
if (!$inArrayValidator->isValid($extension)) {
63+
$this->error(self::INVALID_EXTENSION);
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
70+
}

test/ReportingCloudTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,30 @@ public function testGetAccountSettings()
184184

185185
// -----------------------------------------------------------------------------------------------------------------
186186

187+
/**
188+
* @expectedException InvalidArgumentException
189+
*/
190+
public function testConvertDocumentInvalidDocumentFilenameUnsupportedExtension()
191+
{
192+
$this->reportingCloud->convertDocument('/invalid/path/document.xxx', 'PDF');
193+
}
194+
195+
/**
196+
* @expectedException InvalidArgumentException
197+
*/
198+
public function testConvertDocumentInvalidDocumentFilenameNoExtension()
199+
{
200+
$this->reportingCloud->convertDocument('/invalid/path/document', 'PDF');
201+
}
202+
203+
/**
204+
* @expectedException InvalidArgumentException
205+
*/
206+
public function testConvertDocumentInvalidDocumentFilenameNoFile()
207+
{
208+
$this->reportingCloud->convertDocument('/invalid/path/document/', 'PDF');
209+
}
210+
187211
/**
188212
* @expectedException InvalidArgumentException
189213
*/
@@ -236,6 +260,36 @@ public function testMergeInvalidTemplateName()
236260
$this->reportingCloud->mergeDocument($mergeData, 'PDF', '../invalid_template.tx');
237261
}
238262

263+
/**
264+
* @expectedException InvalidArgumentException
265+
*/
266+
public function testMergeInvalidTemplateFilenameUnsupportedExtension()
267+
{
268+
$mergeData = $this->getTestTemplateMergeData();
269+
270+
$this->reportingCloud->mergeDocument($mergeData, 'PDF', null, '/invalid/path/template.xxx');
271+
}
272+
273+
/**
274+
* @expectedException InvalidArgumentException
275+
*/
276+
public function testMergeInvalidTemplateFilenameNoExtension()
277+
{
278+
$mergeData = $this->getTestTemplateMergeData();
279+
280+
$this->reportingCloud->mergeDocument($mergeData, 'PDF', null, '/invalid/path/template');
281+
}
282+
283+
/**
284+
* @expectedException InvalidArgumentException
285+
*/
286+
public function testMergeInvalidTemplateFilenameNoFile()
287+
{
288+
$mergeData = $this->getTestTemplateMergeData();
289+
290+
$this->reportingCloud->mergeDocument($mergeData, 'PDF', null, '/invalid/path/template/');
291+
}
292+
239293
/**
240294
* @expectedException InvalidArgumentException
241295
*/
@@ -361,6 +415,30 @@ public function testMergeWithTemplateFilename()
361415

362416
// -----------------------------------------------------------------------------------------------------------------
363417

418+
/**
419+
* @expectedException InvalidArgumentException
420+
*/
421+
public function testUploadTemplateInvalidTemplateFilenameUnsupportedExtension()
422+
{
423+
$this->reportingCloud->uploadTemplate('/invalid/path/document.xxx');
424+
}
425+
426+
/**
427+
* @expectedException InvalidArgumentException
428+
*/
429+
public function testUploadTemplateInvalidTemplateFilenameNoExtension()
430+
{
431+
$this->reportingCloud->uploadTemplate('/invalid/path/document');
432+
}
433+
434+
/**
435+
* @expectedException InvalidArgumentException
436+
*/
437+
public function testUploadTemplateInvalidTemplateFilenameNoFile()
438+
{
439+
$this->reportingCloud->uploadTemplate('/invalid/path/document/');
440+
}
441+
364442
/**
365443
* @expectedException InvalidArgumentException
366444
*/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace TxTextControlTest\ReportingCloud\Validator;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use TxTextControl\ReportingCloud\Validator\DocumentExtension as Validator;
7+
8+
class DocumentExtensionTest extends PHPUnit_Framework_TestCase
9+
{
10+
protected $validator;
11+
12+
public function setUp()
13+
{
14+
$this->validator = new Validator();
15+
}
16+
17+
public function testDefault()
18+
{
19+
$this->assertTrue($this->validator->isValid('./document.doc'));
20+
$this->assertTrue($this->validator->isValid('./DOCUMENT.DOC'));
21+
22+
$this->assertTrue($this->validator->isValid('../document.doc'));
23+
$this->assertTrue($this->validator->isValid('../DOCUMENT.DOC'));
24+
25+
$this->assertTrue($this->validator->isValid('/../document.doc'));
26+
$this->assertTrue($this->validator->isValid('/../DOCUMENT.DOC'));
27+
28+
$this->assertTrue($this->validator->isValid('/path/to/document.doc'));
29+
$this->assertTrue($this->validator->isValid('/PATH/TO/DOCUMENT.DOC'));
30+
31+
$this->assertTrue($this->validator->isValid('c:\path\to\document.doc'));
32+
$this->assertTrue($this->validator->isValid('c:\PATH\TO\DOCUMENT.DOC'));
33+
}
34+
35+
public function testInvalid()
36+
{
37+
$this->assertFalse($this->validator->isValid('/path/to/document.xxx'));
38+
$this->assertFalse($this->validator->isValid('/path/to/document.'));
39+
$this->assertFalse($this->validator->isValid('/path/to/document'));
40+
$this->assertFalse($this->validator->isValid('/path/to/document/'));
41+
42+
$this->assertFalse($this->validator->isValid('0'));
43+
$this->assertFalse($this->validator->isValid(0));
44+
$this->assertFalse($this->validator->isValid(1));
45+
$this->assertFalse($this->validator->isValid(null));
46+
$this->assertFalse($this->validator->isValid(false));
47+
}
48+
49+
}

0 commit comments

Comments
 (0)