This repository was archived by the owner on Nov 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIFrameResizer.php
76 lines (68 loc) · 2.85 KB
/
IFrameResizer.php
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
<?php
/**
* @link https://github.com/nirvana-msu/yii2-iframe-resizer
* @copyright Copyright (c) 2015 Alexander Stepanov
* @license MIT
*/
namespace nirvana\iframeresizer;
use Yii;
use yii\base\Widget;
use yii\base\InvalidConfigException;
use yii\helpers\Html;
use yii\helpers\Url;
/**
* IFrameResizer provides convenient shortcut methods allowing to embed iframeResizer.contentWindow.min.js file inside iFrames.
* You should either call `widget` static method as usual, which will embed javascript into the page response,
* or call `embed` static method to embed script into an existing iFrame HTML content generated elsewhere.
*
* @author Alexander Stepanov <[email protected]>
*/
class IFrameResizer extends Widget
{
/**
* Executes the widget.
* You should call ::widget() if you need to embed iframeResizer.contentWindow.min.js
* into the page response, i.e. page itself will be served as iFrame.
*/
public function run()
{
$url = $this->getScriptUrl();
$this->view->registerJsFile($url);
}
/**
* This method should be used if you need to embed iframeResizer.contentWindow.min.js
* into an existing iFrame HTML content generated elsewhere.
* @param $html string iFrame HTML code where script needs to be embedded
* @return string iFrame HTML code with embedded script
* @throws InvalidConfigException
*/
public static function embed($html)
{
$config['class'] = IFrameResizer::className();
$widget = Yii::createObject($config);
/* @var $widget IFrameResizer */
$url = $widget->getScriptUrl();
$scriptTag = Html::jsFile($url);
// Using regular expression rather than DOMDocument approach, as the latter may end up messing the code.
// Split the string contained in $html in three parts:
// - everything before the </body> tag
// - the </body> tag with any attributes in it
// - everything following the </body> tag
$matches = preg_split('/(<\/body.*?>)/i', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
// assemble HTML code back with the script code embedded before </body>
$embeddedHTML = $matches[0] . $scriptTag . $matches[1] . $matches[2];
return $embeddedHTML;
}
/**
* Publishes js which needs to be embedded in an iFrame
* @return string url for published js that needs to be registered
* @throws InvalidConfigException
*/
public function getScriptUrl()
{
$contentWindowJS = YII_DEBUG ? 'iframeResizer.contentWindow.js' : 'iframeResizer.contentWindow.min.js';
$assetManager = $this->view->getAssetManager();
$assetBundle = $assetManager->getBundle(IFrameResizerAsset::className()); // this does the publishing
return Url::base(true) . $assetManager->getAssetUrl($assetBundle, $contentWindowJS);
}
}