1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ImageOptimize plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Automatically optimize images after they've been transformed |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\helpers; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\errors\SiteNotFoundException; |
15
|
|
|
use craft\helpers\UrlHelper as CraftUrlHelper; |
16
|
|
|
|
17
|
|
|
use yii\base\Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* ImageOptimize Settings model |
21
|
|
|
* |
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package ImageOptimize |
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class UrlHelper extends CraftUrlHelper |
27
|
|
|
{ |
28
|
|
|
// Public Static Properties |
29
|
|
|
// ========================================================================= |
30
|
|
|
|
31
|
|
|
// Public Static Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return an absolute URL with protocol that curl will be happy with |
36
|
|
|
* |
37
|
|
|
* @param string $url |
|
|
|
|
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public static function absoluteUrlWithProtocol($url) |
42
|
|
|
{ |
43
|
|
|
// Make this a full URL |
44
|
|
|
if (!self::isAbsoluteUrl($url)) { |
45
|
|
|
$protocol = "http"; |
46
|
|
|
if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) |
|
|
|
|
47
|
|
|
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 |
48
|
|
|
) { |
49
|
|
|
$protocol = "https"; |
50
|
|
|
} |
51
|
|
|
if (self::isProtocolRelativeUrl($url)) { |
52
|
|
|
try { |
53
|
|
|
$url = self::urlWithScheme($url, $protocol); |
54
|
|
|
} catch (SiteNotFoundException $e) { |
55
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
try { |
59
|
|
|
$url = self::siteUrl($url, null, $protocol); |
60
|
|
|
if (self::isProtocolRelativeUrl($url)) { |
61
|
|
|
$url = self::urlWithScheme($url, $protocol); |
62
|
|
|
} |
63
|
|
|
} catch (Exception $e) { |
64
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $url; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|