Completed
Pull Request — master (#234)
by Дмитрий
07:51
created

Iframe::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
dl 0
loc 3
rs 10
eloc 1
c 1
b 1
f 0
nc 1
nop 0
1
<?php
2
namespace PHPDaemon\SockJS\Methods;
3
4
use PHPDaemon\Core\Daemon;
5
use PHPDaemon\Core\Debug;
6
use PHPDaemon\Utils\Crypt;
7
8
/**
9
 * @package    Libraries
10
 * @subpackage SockJS
11
 * @author     Vasily Zorin <[email protected]>
12
 */
13
class Iframe extends Generic
14
{
15
    protected $version = '1.0.3';
16
    protected $contentType = 'text/html';
17
    protected $cacheable = true;
18
19
    /**
20
     * Constructor
21
     * @return void
22
     */
23
    public function init()
0 ignored issues
show
Coding Style introduced by
init uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
24
    {
25
        parent::init();
26
        if (isset($this->attrs->version)) {
27
            $this->version = $this->attrs->version;
28
        }
29
        $this->header('Cache-Control: max-age=31536000, public, pre-check=0, post-check=0');
30
        $this->header('Expires: '.date('r', strtotime('+1 year')));
31
        $html = '<!DOCTYPE html>
32
<html>
33
<head>
34
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
35
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
36
	<script>
37
		document.domain = document.domain;
38
		_sockjs_onload = function(){SockJS.bootstrap_iframe();};
39
	</script>
40
	<script src="https://cdn.jsdelivr.net/sockjs/' . htmlentities($this->version, ENT_QUOTES, 'UTF-8').'/sockjs.min.js"></script>
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
41
</head>
42
<body>
43
	<h2>Don\'t panic!</h2>
44
	<p>This is a SockJS hidden iframe. It\'s used for cross domain magic.</p>
45
</body>
46
</html>';
47
        $etag = 'W/"'.sha1($html).'"';
48
        $this->header('ETag: '.$etag);
49
        if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
50
            if ($_SERVER['HTTP_IF_NONE_MATCH'] === $etag) {
51
                $this->status(304);
52
                $this->removeHeader('Content-Type');
53
                $this->finish();
54
                return;
55
            }
56
        }
57
        $this->header('Content-Length: '.mb_orig_strlen($html));
58
        echo $html;
59
        $this->finish();
60
    }
61
62
    /**
63
     * Called when request iterated
64
     * @return void
65
     */
66
    public function run()
67
    {
68
    }
69
}
70