-
Notifications
You must be signed in to change notification settings - Fork 4
/
TwemoteBrowser.php
executable file
·185 lines (163 loc) · 4.41 KB
/
TwemoteBrowser.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/**
* @package Twemote
* @copyright Hackersquad (Refer README for details)
* @author Abhishek Shrivastava <i.abhi27[at]gmail.com>
* @description TwemoteBrowser app to browse the computer remotely and view file contents.
* Supported commands : ls,cs,cat,head,tail,line(print the content of that line only),range(content between 2 lines)
*/
class TwemoteBrowser
{
private $result;
private $CMD_CD_CONTEXT;
function __construct()
{
$this->result = "";
$this->CMD_CD_CONTEXT = get_cache_config('CMD_CD_CONTEXT');
var_dump($this->CMD_CD_CONTEXT);
}
/*
* Possible commands : cd, ls, cat, head, tail, line, range, find
*/
public function input($params)
{
$cmd = $params[0];
$cmd = trim($cmd,'#');
switch($cmd)
{
case 'cd' : if(!isset($params[1])) $params[1]=".";
$this->doCD($params[1]); break;
case 'ls' : if(!isset($params[1])) $params[1]="";
$this->doLS($params[1]); break;
case 'cat': if(!isset($params[1])) $params[1]="";
$this->doCAT($params[1]); break;
case 'head': if(!isset($params[1])) $params[1]="";
$this->doHEAD($params[1]); break;
case 'tail': if(!isset($params[1])) $params[1]="";
$this->doTAIL($params[1]); break;
case 'line': if(!isset($params[1])) $params[1]="";
if(!isset($params[2])) $params[2]=0;
$this->doLINE($params[1],$params[2]); break;
case 'range': if(!isset($params[1])) $params[1]="";
if(!isset($params[2])) $params[2]=0;
if(!isset($params[3])) $params[3]=1;
$this->doRANGE($params[1],$params[2],$params[3]); break;
}
}
public function doCD($path)
{
$output = array();
if(isset($this->CMD_CD_CONTEXT)==true)
{
if(strlen($path)==0 || $path[0]!='/')
$path = $this->CMD_CD_CONTEXT.'/'.$path;
}
if(is_dir($path)==false)
{
$this->result .= "Invalid directory:".$path."."." ";
$path = $this->CMD_CD_CONTEXT;
}
else $this->CMD_CD_CONTEXT = realpath($path);
$this->result .= "You are currently in directory:".$this->CMD_CD_CONTEXT;
save_config('CMD_CD_CONTEXT',$this->CMD_CD_CONTEXT);
}
public function doLS($path="")
{
$output = array();
$path = $this->CMD_CD_CONTEXT.$path;
$output = scandir($path);
array_shift($output);
array_shift($output);
$this->result = "Directory:$path>"." ".join(" ",$output);
}
public function doCAT($fname)
{
$path = $this->CMD_CD_CONTEXT.'/'.$fname;
if(!is_file($path))
{
$this->result = "Invalid file name. This file doesn't exist : ".$path;
return;
}
if(!is_readable($path))
{
$this->result = "Sorry, this file is not readable : ".$path;
return;
}
$output = file($path);
$this->result = "File:$path>"." ".join(" ",$output);
}
public function doHEAD($fname)
{
$path = $this->CMD_CD_CONTEXT.'/'.$fname;
if(!is_file($path))
{
$this->result = "Invalid file name. This file doesn't exist : ".$path;
return;
}
if(!is_readable($path))
{
$this->result = "Sorry, this file is not readable : ".$path;
return;
}
$output=array();
exec("head ".$path,$output);
$this->result = "Head:$path>"." ".join(" ",$output);
}
public function doTAIL($fname)
{
$path = $this->CMD_CD_CONTEXT.'/'.$fname;
if(!is_file($path))
{
$this->result = "Invalid file name. This file doesn't exist : ".$path;
return;
}
if(!is_readable($path))
{
$this->result = "Sorry, this file is not readable : ".$path;
return;
}
$output=array();
exec("tail ".$path,$output);
$this->result = "Tail:$path>"." ".join(" ",$output);
}
public function doLINE($fname,$lineno)
{
$path = $this->CMD_CD_CONTEXT.'/'.$fname;
if(!is_file($path))
{
$this->result = "Invalid file name. This file doesn't exist : ".$path;
return;
}
if(!is_readable($path))
{
$this->result = "Sorry, this file is not readable : ".$path;
return;
}
$cont = file($path);
$output = $cont[$lineno];
$this->result = "File:$path,Line:$lineno>"." ".$output;
}
public function doRANGE($fname,$st,$en)
{
$path = $this->CMD_CD_CONTEXT.'/'.$fname;
if(!is_file($path))
{
$this->result = "Invalid file name. This file doesn't exist : ".$path;
return;
}
if(!is_readable($path))
{
$this->result = "Sorry, this file is not readable : ".$path;
return;
}
$cont = file($path);
$output = array();
for($i=$st;$i<=$en;$i++)
$output[]=$cont[$i];
$this->result = "File:$path,Start:$st,End:$en>"." ".join(" ",$output);
}
public function output()
{
return $this->result;
}
}