-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobileDetect4varnish.class.php
132 lines (108 loc) · 3.73 KB
/
mobileDetect4varnish.class.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* mobileDetect4varnish.class.php
* @author Mark Oswald <[email protected]>
* @version 0.1
*
* Class to convert Mobile_Detect.php in varnish VCL include file
* needs Mobile_Detect.php http://mobiledetect.net/
*
* in this state only the function for checking the user-agent header
* will be converted
*
* @todo implement checkHttpHeadersForMobile
*/
/**
* define Path to Mobile_Detect.php relatively or absolutely
* (optional, just better performance in case of large subdirs)
*/
$sClassFile = dirname(__FILE__) . '/path/to/Mobile_Detect.php';
if (!file_exists($sClassFile))
{
// try to find file recursively
$mRet = exec("find ./ -name Mobile_Detect.php");
if (!is_string($mRet) || !file_exists($mRet))
die('file with Mobile_Detect not found');
$sClassFile = $mRet;
}
require_once($sClassFile);
if (!class_exists('Mobile_Detect'))
die('class Mobile_Detect not found');
if (!is_writable(dirname(__FILE__)))
die('output dir ' . dirname(__FILE__) . ' is not writable');
define('LOGLINELENGTH', 30);
/**
* Class mobileDetect4Varnish
*
* in first step we only use user-agents to check if mobile
* @todo read function from class file and rewrite functions like checkHttpHeadersForMobile
*
*/
class mobileDetect4Varnish extends Mobile_Detect
{
/**
*
*/
public function __construct()
{
// override standard - we don't need the execution stuff
// parent::_construct();
$this->setMobileDetectionRules();
$this->setMobileDetectionRulesExtended();
}
/**
* @return string
*/
public function convert4varnish()
{
$sVCL = "sub moSetMobileHeader {\n\n";
$aMobile = array();
$aTablet = array();
foreach ($this->getRules() as $sRegex)
{
$aMobile[] = "req.http.user-agent ~ \"$sRegex\"\n";
}
foreach($this->tabletDevices as $sRegex){
$aTablet[] = "req.http.user-agent ~ \"$sRegex\"\n";
}
if (count($aMobile))
{
$sVCL .= "\tif ( " . implode(" || ", $aMobile) . " ) {\n";
$sVCL .= "\t\tset req.http.x-device = \"Phone\";\n";
}
if (count($aTablet))
{
$sVCL .= "\t" . (count($aMobile)?"} else ":"") . "if ( " . implode(" || ", $aTablet) . " ) {\n";
$sVCL .= "\t\tset req.http.x-device = \"Tablet\";\n";
}
if (count($aMobile)||count($aTablet))
{
$sVCL .= "\t} else {\n";
$sVCL .= "\t\tset req.http.x-device = \"Desktop\";\n";
$sVCL .= "\t}\n";
}
$sVCL .= "}\n";
return $sVCL;
}
}
function printlogln( $sStr, $iLength = LOGLINELENGTH )
{
$sOutput = "#";
$sOutput .= str_repeat(" ", ($iLength - strlen($sStr) - 2) / 2);
$sOutput .= $sStr;
$sOutput .= str_repeat(" ", $iLength - strlen($sOutput) - 1);
$sOutput .= "#\n";
print $sOutput;
}
print str_repeat("#", LOGLINELENGTH) . "\n";
printlogln("");
printlogln("");
printlogln("Starting conversion");
printlogln("");
printlogln("");
print str_repeat("#", LOGLINELENGTH) . "\n";
$oConvert = new mobileDetect4Varnish();
$sVCL = $oConvert->convert4varnish();
file_put_contents(dirname(__FILE__) . '/mobiledetect.vcl', $sVCL);
print "\n\nOutput file: ". dirname(__FILE__) . '/mobiledetect.vcl' ."\n\n";
print "\ndone\n\n";