This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDebug.php
147 lines (125 loc) · 3.57 KB
/
PDebug.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Poundation;
class PDebug extends PObject
{
private $sessionId;
private $host;
private $port;
/**
* Refreshes the debugging settings so the next request will be debugged again.
* @return bool
*/
static function refreshDebugger() {
$debugger = self::debuggerFromRequest();
if ($debugger) {
return $debugger->enableDebugging();
}
return false;
}
/**
* Returns the debugger object which is already in use.
* @return null|PDebug
*/
static function debuggerFromRequest() {
$sessionID = rand(10000, 20000);
if (isset($_GET['debug_session_id'])) {
$sessionID = $_GET['debug_session_id'];
} else if (isset($_COOKIE['debug_session_id'])) {
$sessionID = $_COOKIE['debug_session_id'];
}
$host = null;
if (isset($_GET['debug_host'])) {
$host = $_GET['debug_host'];
} else if (isset($_COOKIE['debug_host'])) {
$host = $_COOKIE['debug_host'];
} else {
$host = $_SERVER['REMOTE_ADDR'];
}
$port = null;
if (isset($_GET['debug_port'])) {
$port = $_GET['debug_port'];
} else if (isset($_COOKIE['debug_port'])) {
$port = $_COOKIE['debug_port'];
} else {
$port = 10137;
}
if ($sessionID && $host && $port) {
$debugger = new PDebug($sessionID, $host, $port);
return $debugger;
}
return null;
}
public function __construct($sessionID = null, $host = null, $port = null) {
$this->sessionId = $sessionID;
$this->host = $host;
$this->port = $port;
}
/**
* Sets the session id.
* @param $sessionID
* @return PDebug
*/
public function setSessionID($sessionID) {
$this->sessionId = $sessionID;
return $this;
}
/**
* Returns the session id.
* @return null
*/
public function getSessionID() {
return $this->sessionId;
}
/**
* Sets the debugging host address (the one with the IDE running).
* @param $host
* @return PDebug
*/
public function setHost($host) {
$this->host = $host;
return $this;
}
/**
* Returns the debugging host.
* @return null
*/
public function getHost() {
return $this->host;
}
/**
* Sets the debugging port. Must match the port specified in the debugging IDE.
* @param $port
* @return PDebug
*/
public function setPort($port) {
$this->port = $port;
return $this;
}
/**
* Returns the debugging port.
* @param $port
* @return null
*/
public function getPort() {
return $this->port;
}
/**
* Starts the debugging session.
* @return bool
*/
public function enableDebugging() {
return $this->writeCookies($this->getSessionID(), $this->getHost(), $this->getPort(), 0);
}
public function writeCookies($sessionId, $host, $port, $expires) {
if (!is_null($sessionId) && !is_null($host)) {
$doPort = (is_null($port) || $port <= 0) ? 10137 : $this->getPort();
setcookie('start_debug', 1, $expires,'/');
setcookie('debug_start_session', 1, $expires, '/');
setcookie('debug_session_id', $sessionId, $expires, '/');
setcookie('debug_port', $port, $expires, '/');
setcookie('debug_host', $host, $expires, '/');
return true;
}
return false;
}
}