-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContaoLivereload.php
112 lines (92 loc) · 3.53 KB
/
ContaoLivereload.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* Contao-Livereload
* A CSS livereload tool for Contao
*
* @copyright 4ward.media 2014 <http://www.4wardmedia.de>
* @author Christoph Wiechert <wio@psitrax.de>
* @package contao-livereload
* @license LGPL
* @filesource
*/
class ContaoLivereload
{
public function addJscript($strBuffer, $strTemplate)
{
if(!preg_match("~^fe_.*~", $strTemplate)) return $strBuffer;
if(!$_COOKIE['BE_USER_AUTH']) return $strBuffer;
$DB = \Database::getInstance();
$Session = $DB->prepare('SELECT pid FROM tl_session WHERE name="BE_USER_AUTH" AND hash=?')->limit(1)->execute($_COOKIE['BE_USER_AUTH']);
if(!$Session->numRows) return $strBuffer;
$User = $DB->prepare('SELECT contaoLivereload_enabled, contaoLivereload_server,contaoLivereload_lrPort,contaoLivereload_reqPort FROM tl_user WHERE id=?')->execute($Session->pid);
if(!$User->contaoLivereload_enabled) return $strBuffer;
$srv = $User->contaoLivereload_server;
if(!$srv) $srv = Environment::getInstance()->url;
else if(substr($srv,-1, 1) === '/') $srv = substr($srv, 0, -1);
$reqPort = $User->contaoLivereload_reqPort ?: 35720;
$lrPort = $User->contaoLivereload_lrPort ?: 35729;
$arrCombined = array();
foreach((array)$GLOBALS['TL_FRAMEWORK_CSS'] as $f) {
$arrCombined[] = $f;
}
foreach((array)$GLOBALS['TL_CSS'] as $f) {
$f = explode('|', $f);
if($f[2] && $f[2] == 'static') $arrCombined[] = $f[0];
}
foreach((array)$GLOBALS['TL_USER_CSS'] as $f) {
$f = explode('|', $f);
if($f[1] && $f[1] == 'static' || $f[2] && $f[2] == 'static') {
$arrCombined[] = $f[0];
}
}
global $objPage;
$pageDetails = Controller::getPageDetails($objPage->id);
$objLayout = LayoutModel::findByPk($pageDetails->layout);
if($objLayout->theme_plus_stylesheets) {
$cssIds = deserialize($objLayout->theme_plus_stylesheets);
$themePlusCss = $DB->query("SELECT * FROM tl_theme_plus_stylesheet WHERE ID in(".implode(',', $cssIds).")")->fetchAllAssoc();
if($themePlusCss) {
foreach ((array)$themePlusCss as $f) {
$objFiles = FilesModel::findByUuid($f['file']);
if($objFiles->path) {
$arrCombined[] = $objFiles->path;
}
}
}
}
$str = '<script type="application/json" id="contao-livereload-files">'.json_encode($arrCombined).'</script>';
$str .= '<script type="text/javascript" src="'.$srv.':'.$lrPort.'/livereload.js"></script>';
$json = json_encode($arrCombined);
$str .= <<<JAVASCRIPT
<script type="text/javascript">
(function($) { $(document).ready(function() {
var cfiles = {$json};
var cdest = '';
var nfiles = [];
$('link[rel=stylesheet][href]').each(function() {
var href = $(this).attr('href');
if(href.match(/^http.?:/)) return;
if(href.substr(0, "assets/css/".length) === "assets/css/") {
cdest = href;
} else {
nfiles.push(href);
}
});
$.ajax({
type: "POST",
url: '{$srv}:{$reqPort}',
processData: false,
contentType: 'application/json',
data: JSON.stringify({cfiles: cfiles, cdest: cdest, nfiles: nfiles}),
dataType: 'json'
});
}); })(jQuery);
</script>
JAVASCRIPT;
$strBuffer = str_replace(
'</body>',
$str.'</body>',
$strBuffer);
return $strBuffer;
}
}