All Data Structures Functions Variables Pages
ZurmoExternalViewUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
41  {
42  const EXTERNAL_SCRIPT_FILE_NAME = 'ExternalScripts.js';
43 
48  public static function makeExternalViewForCurrentUser(View $containedView)
49  {
50  $horizontalGridView = new GridView(1, 1);
51  $containedView->setCssClasses(array_merge($containedView->getCssClasses(), array('AppContent')));
52  $horizontalGridView->setView($containedView, 0, 0);
53  $verticalGridView = new GridView(1, 1);
54  $verticalGridView->setView($horizontalGridView, 0, 0);
55  return $verticalGridView;
56  }
57 
63  public static function resolveHeadTag($rawXHtml, $excludeStyles = false)
64  {
65  $hostInfo = Yii::app()->getRequest()->getHostInfo();
66  $dom = new DOMDocument();
67  $headBody = array('js' => array(),
68  'css' => array(),
69  'style' => array());
70  libxml_use_internal_errors(true);
71  $dom->loadHTML($rawXHtml);
72  $head = $dom->getElementsByTagName('head')->item(0);
73  foreach ($head->childNodes as $child)
74  {
75  if ($child->nodeName == 'script' && $child->hasAttribute('src'))
76  {
77  if (strpos($child->getAttribute('src'), $hostInfo) === false)
78  {
79  $headBody['js'][] = $hostInfo . $child->getAttribute('src');
80  }
81  else
82  {
83  $headBody['js'][] = $child->getAttribute('src');
84  }
85  }
86  elseif (!$excludeStyles && $child->nodeName == 'link' && $child->hasAttribute('rel'))
87  {
88  if ($child->getAttribute('rel') == 'stylesheet')
89  {
90  if (strpos($child->getAttribute('href'), $hostInfo) === false)
91  {
92  $stylesheetReference = $hostInfo . $child->getAttribute('href');
93  }
94  else
95  {
96  $stylesheetReference = $child->getAttribute('href');
97  }
98  $headBody['css'][] = array('rel' => $child->getAttribute('rel'),
99  'type' => $child->getAttribute('type'),
100  'href' => $stylesheetReference);
101  }
102  }
103  elseif (!$excludeStyles && $child->nodeName == 'style')
104  {
105  $headBody['style'][] = $child->nodeValue;
106  }
107  }
108  return $headBody;
109  }
110 
115  public static function resolveHtmlAndScriptInBody($rawXHtml)
116  {
117  $dom = new DOMDocument();
118  $bodyContent = new DOMDocument();
119  libxml_use_internal_errors(true);
120  $dom->loadHTML($rawXHtml);
121  $body = $dom->getElementsByTagName('body')->item(0);
122  foreach ($body->childNodes as $child)
123  {
124  $bodyContent->appendChild($bodyContent->importNode($child, true));
125  }
126  $scriptTagsInBody = static::resolveScriptTagsInBody($bodyContent);
127  $htmlAndScriptTagsInBody = array();
128  $htmlAndScriptTagsInBody['js'] = $scriptTagsInBody;
129  $htmlAndScriptTagsInBody['html'] = $bodyContent->saveHTML();
130  return $htmlAndScriptTagsInBody;
131  }
132 
137  public static function resolveScriptTagsInBody(&$bodyContent)
138  {
139  $hostInfo = Yii::app()->getRequest()->getHostInfo();
140  $scriptTagNodes = $bodyContent->getElementsByTagName('script');
141  $scriptTags = array();
142  foreach ($scriptTagNodes as $scriptTagNode)
143  {
144  $bodyContent->removeChild($scriptTagNode);
145  $scriptTagDetail = array();
146  if ($scriptTagNode->hasAttribute('src'))
147  {
148  if (strpos($scriptTagNode->getAttribute('src'), $hostInfo) === false)
149  {
150  $scriptSrcReference = $hostInfo . $scriptTagNode->getAttribute('src');
151  }
152  else
153  {
154  $scriptSrcReference = $scriptTagNode->getAttribute('src');
155  }
156  $scriptTagDetail['type'] = 'file';
157  $scriptTagDetail['src'] = $scriptSrcReference;
158  $scriptTagDetail['body'] = null;
159  $scriptTags[] = $scriptTagDetail;
160  }
161  else
162  {
163  $scriptTagDetail['type'] = 'codeBlock';
164  $scriptTagDetail['src'] = null;
165  $scriptTagDetail['body'] = $scriptTagNode->nodeValue;
166  $scriptTags[] = $scriptTagDetail;
167  }
168  }
169  return $scriptTags;
170  }
171 
176  public static function resolveAndCombineScripts($rawXHtml)
177  {
178  $dom = new DOMDocument();
179  libxml_use_internal_errors(true);
180  $dom->loadHTML('<?xml encoding="UTF-8">' . $rawXHtml);
181  $externalScriptFilePath = Yii::getPathOfAlias('application.runtime.uploads') .
182  DIRECTORY_SEPARATOR . self::EXTERNAL_SCRIPT_FILE_NAME;
183  $publishedUrl = Yii::app()->getAssetManager()->getPublishedUrl($externalScriptFilePath);
184  if ($publishedUrl === false || file_exists($publishedUrl) === false)
185  {
186  $publishedUrl = self::publishExternalScripts($dom);
187  }
188  self::removeScriptTagSrcNodes($dom);
189  $head = $dom->getElementsByTagName('head')->item(0);
190  $combinedFileElement = $dom->createElement('script');
191 
192  $typeAttribute = $dom->createAttribute('type');
193  $typeAttribute->value = 'text/javascript';
194  $combinedFileElement->appendChild($typeAttribute);
195 
196  $srcAttribute = $dom->createAttribute('src');
197  $srcAttribute->value = Yii::app()->getRequest()->getHostInfo() . $publishedUrl;
198  $combinedFileElement->appendChild($srcAttribute);
199 
200  $head->appendChild($combinedFileElement);
201  $rawXHtml = $dom->saveHTML();
202  return $rawXHtml;
203  }
204 
209  public static function getContentsFromSource($path)
210  {
211  $scriptFileContents = file_get_contents($path);
212  if (strpos($path, 'jquery.min.js') === false && strpos($path, 'jquery.ui.min.js') === false)
213  {
214  $scriptFileContents = "jQQ.isolate (function(jQuery, $) { " . $scriptFileContents . " });";
215  }
216  return $scriptFileContents;
217  }
218 
222  public static function removeScriptTagSrcNodes(&$dom)
223  {
224  $scriptTags = $dom->getElementsByTagName('script');
225  $tagCount = 0;
226  foreach ($scriptTags as $child)
227  {
228  if ($child->hasAttribute('src'))
229  {
230  $tagCount++;
231  }
232  }
233  while ($tagCount > 0)
234  {
235  foreach ($scriptTags as $child)
236  {
237  if ($child->hasAttribute('src'))
238  {
239  $child->parentNode->removeChild($child);
240  $tagCount--;
241  }
242  }
243  }
244  }
245 
246  protected static function publishExternalScripts($dom)
247  {
248  $scriptTags = $dom->getElementsByTagName('script');
249  $fileContents = '';
250  foreach ($scriptTags as $scriptTagNode)
251  {
252  if ($scriptTagNode->hasAttribute('src'))
253  {
254  $scriptSrcPath = $scriptTagNode->getAttribute('src');
255  $scriptFullPath = self::getScriptAbsolutePath($scriptSrcPath);
256  $fileContents .= self::getContentsFromSource($scriptFullPath);
257  }
258  }
259  $scriptFileName = self::EXTERNAL_SCRIPT_FILE_NAME;
260  if (!is_dir(Yii::getPathOfAlias('application.runtime.assets')))
261  {
262  mkdir(Yii::getPathOfAlias('application.runtime.assets'), 0755, true);
263  }
264  $scriptFilePath = Yii::getPathOfAlias('application.runtime.assets') . DIRECTORY_SEPARATOR . $scriptFileName;
265  $fp = fopen($scriptFilePath, 'w');
266  fwrite($fp, $fileContents);
267  fclose($fp);
268  $publishedUrl = Yii::app()->getAssetManager()->publish($scriptFilePath);
269  return $publishedUrl;
270  }
271 
272  protected static function getScriptAbsolutePath($scriptSrcPath)
273  {
274  $assetsBasePath = Yii::app()->assetManager->basePath;
275  $scriptPathRelativeToAssets = substr($scriptSrcPath, strpos($scriptSrcPath, 'assets') + 6);
276  $scriptFullPath = $assetsBasePath . $scriptPathRelativeToAssets;
277  return $scriptFullPath;
278  }
279 
280  public static function resolveExternalRequestHeader()
281  {
282  header('Access-Control-Allow-Origin: *');
283  }
284  }
285 ?>
static resolveAndCombineScripts($rawXHtml)
static resolveScriptTagsInBody(&$bodyContent)
Definition: View.php:43
static resolveHeadTag($rawXHtml, $excludeStyles=false)
static makeExternalViewForCurrentUser(View $containedView)
static resolveHtmlAndScriptInBody($rawXHtml)
Generated on Thu May 28 2020 07:10:36