Skip to content

Commit 463ba88

Browse files
authored
Revert "Inject js" (#64)
1 parent d661e48 commit 463ba88

22 files changed

+60
-478
lines changed

README.md

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -60,52 +60,19 @@ You can also set the User Agent
6060
$screenCapture->setUserAgentString('Some User Agent String');
6161
```
6262

63-
And the resulted image type
63+
And the resulted image format
6464
``` php
65-
// allowed types are 'jpg' and 'png', default is 'jpg'.
66-
$screenCapture->setImageType(Screen\Image\Types\Png::FORMAT);
67-
// or
68-
$screenCapture->setImageType('png');
65+
// allowed formats are 'jpg' and 'png', default is 'jpg'.
66+
$screenCapture->setFormat('png');
6967
```
7068
* If the format is ```jpg``` and the background color is not set, the default value will be ```#FFFFFF```, if ```png``` the default background color will be transparent.
7169

7270
And most importantly, save the result
7371
``` php
74-
$fileLocation = '/some/dir/test.' . $screen->getImageType()->getFormat();
75-
$screenCapture->save($fileLocation);
76-
77-
// you don't need to set the file extension
78-
$fileLocation = '/some/dir/test';
79-
$screenCapture->save($fileLocation); // Will automatically determine the extension type
80-
81-
echo $screenCapture->getImageLocation(); // --> /some/dir/test.png
72+
$fileLocation = '/some/dir/test.' . $screen->getFormat();
73+
$screen->save($fileLocation);
8274
```
8375

84-
##Injection your own JS into the web page
85-
86-
You can also run your own JS scripts or snippets before the screenshot.
87-
88-
For that we have the method ```includeJs```, here are some usage examples:
89-
90-
``` php
91-
// Including a remote file
92-
$jQueryUrl = 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js';
93-
$screenCapture->includeJs(new \Screen\Injection\Url($jQUeryUrl));
94-
95-
// Including a local file
96-
$localFilePath = 'path/to/my/script.js';
97-
$screenCapture->includeJs(new \Screen\Injection\LocalPath($localFilePath));
98-
99-
// Using the scripts included on the library
100-
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideCookiesPolicy());
101-
$screen->includeJs(new \Screen\Injection\Scripts\FacebookHideSignUp());
102-
103-
// Using a js snippet
104-
$screen->includeJs("console.log('This is supa cool!');");
105-
```
106-
107-
Just use this method before calling ```save(...)```
108-
10976
##Other configurations
11077
Additionally to the basic usage, you can set so extra configurations.
11178

demo/shot.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
}
3636

3737
if (isset($_GET['format'])) { // Format
38-
$screen->setImageType($_GET['format']);
38+
$screen->setFormat($_GET['format']);
3939
}
4040

41-
$fileLocation = 'test';
41+
$fileLocation = 'test.' . $screen->getFormat();
4242
$screen->save($fileLocation);
4343

44-
header('Content-Type:' . $screen->getImageType()->getMimeType());
45-
header('Content-Length: ' . filesize($screen->getImageLocation()));
46-
readfile($screen->getImageLocation());
44+
header('Content-Type:' . $screen->getMimeType());
45+
header('Content-Length: ' . filesize($fileLocation));
46+
readfile($fileLocation);

scripts/facebook-hide-cookies-policy.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

scripts/facebook-hide-login.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/facebook-hide-signup.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

scripts/facebook-hide-top-bar.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Capture.php

Lines changed: 47 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
namespace Screen;
44

5-
use Screen\Exceptions\TemplateNotFoundException;
6-
use Screen\Image\Types;
7-
use Screen\Image\Types\Type;
8-
use Screen\Injection\LocalPath;
9-
use Screen\Injection\Url;
105
use Screen\Location\Jobs;
116
use Screen\Location\Output;
127

@@ -62,11 +57,11 @@ class Capture
6257
protected $backgroundColor = '';
6358

6459
/**
65-
* Image Type, default is jpeg
60+
* Image format
6661
*
67-
* @var Type
62+
* @var string
6863
*/
69-
protected $imageType;
64+
protected $format = 'jpg';
7065

7166
/**
7267
* User Agent String used on the page request
@@ -103,27 +98,6 @@ class Capture
10398
*/
10499
public $output;
105100

106-
/**
107-
* Location where the file was written to
108-
*
109-
* @var string
110-
*/
111-
protected $imageLocation;
112-
113-
/**
114-
* List of included JS scripts
115-
*
116-
* @var array
117-
*/
118-
protected $includedJsScripts = array();
119-
120-
/**
121-
* List of included JS snippets
122-
*
123-
* @var array
124-
*/
125-
protected $includedJsSnippets = array();
126-
127101
/**
128102
* Capture constructor.
129103
*/
@@ -138,31 +112,18 @@ public function __construct($url = null)
138112

139113
$this->jobs = new Jobs();
140114
$this->output = new Output();
141-
142-
$this->setImageType(Types\Jpg::FORMAT);
143115
}
144116

145-
/**
146-
* Saves the screenshot to the requested location
147-
*
148-
* @param string $imageLocation Image Location
149-
* @param bool $deleteFileIfExists True to delete the file if it exists
150-
*
151-
* @return bool
152-
*/
153117
public function save($imageLocation, $deleteFileIfExists = true)
154118
{
155-
$this->imageLocation = $this->output->getLocation() . $imageLocation;
156-
157-
if (!pathinfo($this->imageLocation, PATHINFO_EXTENSION)) {
158-
$this->imageLocation .= '.' . $this->getImageType()->getFormat();
159-
}
119+
$outputPath = $this->output->getLocation() . $imageLocation;
160120

161121
$data = array(
162122
'url' => $this->url,
163123
'width' => $this->width,
164124
'height' => $this->height,
165-
'imageLocation' => LocalPath::sanitize($this->imageLocation),
125+
// If used on windows the \ char needs to be handled to be used on a js string
126+
'imageLocation' => str_replace("\\", "\\\\", $outputPath),
166127
);
167128

168129
if ($this->clipWidth && $this->clipHeight) {
@@ -174,7 +135,7 @@ public function save($imageLocation, $deleteFileIfExists = true)
174135

175136
if ($this->backgroundColor) {
176137
$data['backgroundColor'] = $this->backgroundColor;
177-
} elseif ($this->getImageType()->getFormat() == Types\Jpg::FORMAT) {
138+
} elseif ($this->getFormat() == 'jpg') {
178139
// If there is no background color set, and it's a jpeg
179140
// we need to set a bg color, otherwise the background will be black
180141
$data['backgroundColor'] = '#FFFFFF';
@@ -184,16 +145,8 @@ public function save($imageLocation, $deleteFileIfExists = true)
184145
$data['userAgent'] = $this->userAgentString;
185146
}
186147

187-
if ($this->includedJsScripts) {
188-
$data['includedJsScripts'] = $this->includedJsScripts;
189-
}
190-
191-
if ($this->includedJsSnippets) {
192-
$data['includedJsSnippets'] = $this->includedJsSnippets;
193-
}
194-
195-
if ($deleteFileIfExists && file_exists($this->imageLocation) && is_writable($this->imageLocation)) {
196-
unlink($this->imageLocation);
148+
if ($deleteFileIfExists && file_exists($outputPath) && is_writable($outputPath)) {
149+
unlink($outputPath);
197150
}
198151

199152
$jobName = md5(json_encode($data));
@@ -208,14 +161,14 @@ public function save($imageLocation, $deleteFileIfExists = true)
208161
$command = sprintf("%sphantomjs %s", $this->binPath, $jobPath);
209162
$result = exec(escapeshellcmd($command));
210163

211-
return file_exists($this->imageLocation);
164+
return file_exists($outputPath);
212165
}
213166

214167
private function getTemplateResult($templateName, array $args)
215168
{
216169
$templatePath = $this->templatePath . DIRECTORY_SEPARATOR . $templateName . '.php';
217170
if (!file_exists($templatePath)) {
218-
throw new TemplateNotFoundException($templateName);
171+
throw new \Exception("The template {$templateName} does not exist!");
219172
}
220173
ob_start();
221174
extract($args);
@@ -247,7 +200,19 @@ public function setBinPath($binPath)
247200
*/
248201
public function setUrl($url)
249202
{
250-
$this->url = new Url($url);
203+
// Prepend http:// if the url doesn't contain it
204+
if (!stristr($url, 'http://') && !stristr($url, 'https://')) {
205+
$url = 'http://' . $url;
206+
}
207+
208+
if (!$url || !filter_var($url, FILTER_VALIDATE_URL)) {
209+
throw new \Exception("Invalid URL");
210+
}
211+
212+
$url = str_replace(array(';', '"', '<?'), '', strip_tags($url));
213+
$url = str_replace(array('\077', '\''), array(' ', '/'), $url);
214+
215+
$this->url = $url;
251216
}
252217

253218
/**
@@ -321,37 +286,49 @@ public function setBackgroundColor($backgroundColor)
321286
}
322287

323288
/**
324-
* Sets the image type
289+
* Sets the image format
325290
*
326-
* @param string $type 'jpg', 'png', etc...
291+
* @param string $format 'jpg' | 'png'
327292
*
328293
* @return Capture
329294
*/
330-
public function setImageType($type)
295+
public function setFormat($format)
331296
{
332-
$this->imageType = Types::getClass($type);
297+
$format = strtolower($format);
298+
if (!in_array($format, ['jpg', 'png'])) {
299+
throw new Exception(
300+
"Invalid image format '{$format}'. " .
301+
"Allowed formats are 'jpg' and 'png'"
302+
);
303+
}
304+
305+
$this->format = $format;
333306

334307
return $this;
335308
}
336309

337310
/**
338-
* Returns the image type instance
311+
* Gets the image format
339312
*
340-
* @return Type
313+
* @return string
341314
*/
342-
public function getImageType()
315+
public function getFormat()
343316
{
344-
return $this->imageType;
317+
return $this->format;
345318
}
346319

347320
/**
348-
* Returns the location where the screenshot file was written
321+
* Gets the MIME type of resulted image
349322
*
350323
* @return string
351324
*/
352-
public function getImageLocation()
325+
public function getMimeType()
353326
{
354-
return $this->imageLocation;
327+
if ($this->format === 'png') {
328+
return 'image/png';
329+
}
330+
331+
return 'image/jpeg';
355332
}
356333

357334
/**
@@ -367,22 +344,4 @@ public function setUserAgentString($userAgentString)
367344

368345
return $this;
369346
}
370-
371-
/**
372-
* Adds a JS script or snippet to the screen shot script
373-
*
374-
* @param string|URL $script Script to include
375-
*
376-
* @return Capture
377-
*/
378-
public function includeJs($script)
379-
{
380-
if (is_a($script, Url::class)) {
381-
$this->includedJsScripts[] = $script;
382-
} else {
383-
$this->includedJsSnippets[] = $script;
384-
}
385-
386-
return $this;
387-
}
388347
}

src/Exceptions/FileNotFoundException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Exceptions/InvalidUrlException.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Exceptions/ScreenException.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)