/
mike.php
executable file
·615 lines (529 loc) · 18.2 KB
/
mike.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
<?php
/* mike.php - the micro drop-in site editor */
/* PHP 5.4+ */
/*
* Things to do
* 0. Include Zepto JS (DONE)
* 0. Style the file list page
* 0. Get the list of files by AJAX
* 0. Save a file
* 0. Upload a file.
*/
$users = ['admin' => '5f4dcc3b5aa765d61d8327deb882cf99'];
/* output buffering - switch on to allow headers to be updated */
ob_start();
class Mike {
public static $version = "0.1";
public static $this_file = "mike.php";
public static $isRouteProcessed = false;
private static $requestUri;
private static $contentTypes = array(
'css' => "text/css",
'js' => 'text/javascript',
'text' => 'text/plain',
'json' => 'application/json'
);
/**
* Routes a get request and executes the routed function.
*
* @param string $route
* @param function $function
*/
public static function get($route, $function)
{
// Check if the request method type
if (strtolower($_SERVER['REQUEST_METHOD']) == 'get') {
static::processRoute($route, $function);
}
}
/**
* Routes a post request and executes the routed function.
*
* @param string $route
* @param function $function
*/
public static function post($route, $function)
{
// Check the request method type
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
static::processRoute($route, $function);
}
}
/**
* Determines the base URI of the app.
*
* @return string
*/
public static function baseUri($segments = null)
{
return str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']) . ($segments ? trim($segments, '/') : '');
}
/**
* Porcesses the route.
*
* @access private
*/
private static function processRoute($route, $function)
{
if (static::$isRouteProcessed) {
return;
}
// Check if the request is empty
if (static::requestUri() == '') {
static::$requestUri = '/';
}
// Match the route
if (preg_match("#^{$route}$#", static::requestUri(), $matches)) {
unset($matches[0]);
call_user_func_array($function, $matches);
static::$isRouteProcessed = true;
}
}
/**
* Determines the requested URL.
*
* @return string
* @access private
*/
public static function requestUri()
{
// Check ff this is the first time getting the request uri
if (static::$requestUri === null) {
$baseMikePath = "/" . static::$this_file;
// Check if there is a PATH_INFO variable
// Note: some servers seem to have trouble with getenv()
$path = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : @getenv('PATH_INFO');
if (trim($path, '/') != '' && $path != $baseMikePath) {
return static::$requestUri = $path;
}
// Check if ORIG_PATH_INFO exists
$path = str_replace($_SERVER['SCRIPT_NAME'], '', (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : @getenv('ORIG_PATH_INFO'));
if (trim($path, '/') != '' && $path != $baseMikePath) {
return static::$requestUri = $path;
}
// Check for ?uri=x/y/z
if (isset($_REQUEST['url'])) {
return static::$requestUri = $_REQUEST['url'];
}
// Check the _GET variable
if (is_array($_GET) && count($_GET) == 1 && trim(key($_GET), '/') != '') {
return static::$requestUri = key($_GET);
}
// Check for QUERY_STRING
$path = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : @getenv('QUERY_STRING');
if (trim($path, '/') != '') {
return static::$requestUri = $path;
}
// Check for requestUri
$path = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
if (trim($path, '/') != '' && $path != $baseMikePath) {
return static::$requestUri = str_replace(str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']), '', $path);
}
// I dont know what else to try, leave it...
return static::$requestUri = '';
}
return static::$requestUri;
}// end of requestUri
/*
* Sets the content type
*/
public static function set_content_type($type) {
header('Content-Type: ' . static::$contentTypes[strtolower($type)]);
}
/*
* Renders a layout page with content
*/
public static function render($layout, $body, $options = NULL) {
// first append the body
$page = $layout;
$page = str_replace("{{BODY}}", $body, $layout);
if ($options != NULL) {
// check headers and apply any headers
if (array_key_exists('headers', $options)) {
foreach($options['headers'] as $header) {
header($header);
}
} else {
// use default header
header("Content-Type: text/html");
}
// apply title
if (array_key_exists('title', $options)) {
$page = str_replace("{{TITLE}}", $options['title'], $page);
} else {
$page = str_replace("{{TITLE}}", "mike.php", $page);
}
// apply scripts
if (array_key_exists('scripts', $options)) {
$script_tag = [];
foreach($options['scripts'] as $script) {
$script_tag[] = '<script type="text/javascript" src="' . $script . '"></script>';
}
$page = str_replace("{{SCRIPTS}}", implode($script_tag), $page);
} else {
$page = str_replace("{{SCRIPTS}}", "", $page);
}
// apply styles
if (array_key_exists('styles', $options)) {
$styles_tag = [];
foreach($options['styles'] as $style) {
$styles_tag[] = '<link rel="stylesheet" type="text/css" href="' . $style . '">';
}
$page = str_replace("{{STYLES}}", implode($styles_tag), $page);
} else {
$page = str_replace("{{STYLES}}", "", $page);
}
} else {
// remove the tags - since they are nothing to replace them with.
$page = str_replace("{{TITLE}}", "Mike.php", $page);
$page = str_replace("{{SCRIPTS}}", "", $page);
$page = str_replace("{{STYLES}}", "", $page);
}
// send the output..
echo $page;
}//end of render
}// end of Mike
/* MARKUP */
static $layout = <<<'EOD'
<!doctype html>
<html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<title>{{TITLE}}</title>
{{STYLES}}
{{SCRIPTS}}
</head>
{{BODY}}
</html>
EOD;
static $login_page = <<<'EOD'
<body id="mike-login">
<header>
<span class="product">mike.php</span>
<span class="slogan">The micro drop-in site editor</span>
</header>
<form action="login" method="post">
<div id="login-form">
<h1>Login</h1>
<fieldset>
<legend>Enter your credentials</legend>
<div>
<input type="text" name="name" class="entry" placeholder="Enter username"/>
</div>
<div>
<input type="password" name="password" class="entry" placeholder="Enter password"/>
</div>
</fieldset>
<input type="submit" value="Login" class="btn"/>
</div>
</form>
</body>
EOD;
static $file_page = <<<'EOD'
<body id="mike-filelist">
<header>
<span class="product">mike.php</span>
<span class="slogan">The micro drop-in site editor</span>
</header>
<div class="container2">
<h1>File List</h1>
<div class="button-bar">
<button class="btn" >save</button>
</div>
<div class="container1">
<div id="filelist" class="filelist">
{{TABLE}}
</div>
<div id="fileedit" class="fileedit">
<textarea id="filecontent"></textarea>
</div>
</div>
</div>
{{FILEMGR_JS}}
</body>
EOD;
/*
* Main page CSS
*/
static $style_css = <<<'EOD'
html, body, .container, ul { padding: 0; margin: 0}
html, body { background-color: #2067b2; color: #fff; font-family: Helvetica, Arial, sans-serif; }
header { background-color: #3d7bbc; height: 40px; width: 100%; font-weight: 100; padding: 10px 0 0 10px;}
header .product { font-size: 120%; }
header .slogan { font-size: 80%; }
h1, h2, h3, h4 { font-weight: 100; }
/*.container2{ clear: left; float: left; width: 100%; overflow: hidden;}
.container1{ float: left; width: 100%; position: relative; right: 50%; }
#filelist, #fileedit { float: left; width: 46%; position: relative; left: 52%; overflow: hidden; }
#fileedit {left: 56%; position: relative;}*/
#login-form { width: 300px; margin: 0 auto; }
#login-form fieldset { border: none; margin: 0; padding: 0;}
#login-form .entry { border: none; line-height: 2em; font-size: 120%; padding: 10px; color: #2067b2; }
.btn { background-color: #4da7dd; padding: 10px; border: none; cursor: pointer; font-size: 100%; color: white; font-weight: 100; }
.btn:hover { background-color: #3d7bbc; }
/*textarea{ width: 98%; padding:1%; border:none; }*/
EOD;
/*
* FileManager CSS
*/
static $filemgr_css = <<<'EOD'
ul#filetable {
list-style: none;
list-style-type: none;
padding:0;
line-height: 2em;
color: #666;
}
ul#filetable li {
cursor: pointer;
background-color: white;
padding-left: 6px;
}
ul#filetable li:hover {
color: black;
}
textarea {
font-family: Consolas, Inconsolata, monospace;
font-size: 12pt;
}
html, body { height: 100%; }
header{ position: fixed; z-index: 1;}
.container1 { position: relative; height: calc(100% - 100px); }
.filelist { width: 49%; float: left; height: 100%;}
.fileedit { width: 50%; float: right; }
.fileedit textarea { position: absolute; top:0; bottom:0; width: 49%; }
h1 { border-bottom: solid 1px #fff; }
.container2 { padding: 6px; height: 100%; width: 99%; position:relative; top: 40px; }
.button-bar { width: 49%; margin-left: 50%; }
EOD;
/*
* FileManager JS
*/
static $filemgr_js = <<<'EOD'
<script>
var TAB_KEY_CODE = 9,
SOFT_TAB = ' ',
SOFT_TAB_LENGTH = SOFT_TAB.length,
MikeFileManager = (function() {
function getFile(filename) {
$.post('/mike.php/file', {'file' : filename}, function(data) {
$('#filecontent').val(data);
})
};
function getListing() {
$.getJSON('/mike.php/filelist.json', function(data) {
console.log(data);
});
}
function saveFile() {
};
// public API
return {
getFile : getFile,
saveFile : saveFile,
getListing : getListing
}
})();
$d(function() {
MikeFileManager.getListing();
$("li.file").on('click', function(e) {
var filename = $(e.currentTarget).attr('data-file');
MikeFileManager.getFile(filename);
});
// pressing tab should insert spaces instead of focusing another element
$('textarea').on("keydown", function(event) {
var value = textarea.value;
var caret = textarea.selectionStart;
// if tab is pressed, insert four spaces
if (event.keyCode === TAB_KEY_CODE) {
textarea.value = value.substring(0, caret) + SOFT_TAB +
value.substring(caret);
// move caret to after soft tab
textarea.setSelectionRange(caret + SOFT_TAB_LENGTH, caret +
SOFT_TAB_LENGTH);
// prevent default tab action that shifts focus to the next element
event.preventDefault();
}
});
});
</script>
EOD;
/*
* 140medley - micro jQuery replacement.
*/
static $micro_js = <<<'EOD'
var t=function(a,b){return function(c,d){return a.replace(/#{([^}]*)}/g,function(a,e){return Function("x","with(x)return "+e).call(c,d||b||{})})}},s=function(a,b){return b?{get:function(c){return a[c]&&b.parse(a[c])},set:function(c,d){a[c]=b.stringify(d)}}:{}}(this.localStorage||{},JSON),p=function(a,b,c,d){c=c||document,d=c[b="on"+b],a=c[b]=function(e){return d=d&&d(e=e||c.event),(a=a&&b(e))?b:d},c=this},m=function(a,b,c){for(b=document,c=b.createElement("p"),c.innerHTML=a,a=b.createDocumentFragment();b=c.firstChild;)a.appendChild(b);return a},$=function(a,b){return a=a.match(/^(\W)?(.*)/),(b||document)["getElement"+(a[1]?"#"==a[1]?"ById":"sByClassName":"sByTagName")](a[2])},j=function(a){for(a=0;4>a;a++)try{return a?new ActiveXObject([,"Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP"):new XMLHttpRequest}catch(b){}},jx=function(a,b,c,d,e,f,g){g=j(),g.open(c,a,f),"POST"==c&&g.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),g.ondocumentreadychange=function(){4===g.readyState&&(g.status>=200&&g.status<400?b&&b(g.responseText):e&&e(g.responseText))},g.send(data)};
EOD;
/*
* 404 page
*/
static $page_404 = <<<'EOD'
<body>
<header>
<span class="product">mike.php</span>
<span class="slogan">The micro drop-in site editor</span>
</header>
<h1>{{CONTENT}}</h1>
</body>
EOD;
/* MAIN ENTRY POINT */
/*
* Base path
*/
Mike::get('/', function() {
session_start();
if (isset($_SESSION['username'])) {
// user is logged in..
header('Location: /'. Mike::$this_file. '/list');
} else {
header('Location: /' . Mike::$this_file . '/login');
}
});
/* Zepto library */
Mike::get('/js/zepto.js', function() {
Mike::set_content_type('js');
echo $GLOBALS['zepto_js'];
});
/* 140MEDLEY library - micro jQuery replacement */
Mike::get('/js/140medley.js', function() {
Mike::set_content_type('js');
echo $GLOBALS['micro_js'];
});
/*
* Stylesheet for Mike
*/
Mike::get('/css/style.css', function() {
Mike::set_content_type('css');
echo $GLOBALS['style_css'];
});
/*
* Stylesheet for file manager
*/
Mike::get('/css/filemgr.css', function() {
Mike::set_content_type('css');
echo $GLOBALS['filemgr_css'];
});
/*
* Login page
*/
Mike::get('/login', function() {
Mike::render($GLOBALS['layout'], $GLOBALS['login_page'],
['styles' => ['css/style.css'],
'title' => 'Login - Mike.php',
'headers' => ['Cache-Control: no-cache, no-store, must-revalidate','Pragma: no-cache','Expires: 0']
]);
});
/*
* Logout
*/
Mike::get('/logout', function() {
session_start();
session_destroy();
header('Location: ' . Mike::$this_file . '/login');
});
/*
* Login - post
*/
Mike::post('/login', function() {
session_start();
$users = $GLOBALS['users'];
if (array_key_exists($_REQUEST['name'], $GLOBALS['users'])) {
$md5_password = $users[$_REQUEST['name']];
if ($md5_password == md5($_REQUEST['password'])) {
$_SESSION['username'] = $_REQUEST['name'];
header('Location: /mike.php/list');
}
}
Mike::render($GLOBALS['layout'], $GLOBALS['login_page'],
['styles' => ['css/style.css'],
'title' => 'Login - Mike.php'
]);
});
/*
* Get directory listing
*/
Mike::get('/list', function() {
session_start();
if (!isset($_SESSION['username'])) {
// user not logged in..
header('Location: /mike.php/login');
} else {
// list the files
$current_dir_path = dirname(__FILE__);
$allowed_filetypes = ['png','jpg','gif','htm','html','php','php5'];
$disallowed_files = ['mike.php','.','..'];
$filetable = [];
$filetable[] = '<ul id="filetable">';
foreach (new DirectoryIterator($current_dir_path) as $fn) {
$filename = $fn->getFilename();
if (in_array(strtolower($filename),$disallowed_files) == false) {
if (in_array($fn->getExtension(), $allowed_filetypes)) {
$filetable[] = '<li class="file" data-file="'.$fn->getFilename().'">'.$fn->getFilename().'</li>';
}
}
}
$filetable[] = '</ul>';
// render the page.
$page = $GLOBALS['file_page'];
$page = str_replace("{{TABLE}}", implode($filetable), $page);
$page = str_replace("{{FILEMGR_JS}}", $GLOBALS['filemgr_js'], $page);
Mike::render($GLOBALS['layout'], $page,
['styles' => ['css/style.css','css/filemgr.css'],
'scripts' => ['js/140medley.js'],
'title' => 'File Manager - Mike.php'
]);
}
});
/*
* Gets the directory structure as JSON
*/
Mike::get('/filelist.json', function() {
session_start();
if (!isset($_SESSION['username'])) {
// user not logged in..
header('Location: /mike.php/login');
} else {
Mike::set_content_type('json');
// list the files
$current_dir_path = dirname(__FILE__);
$allowed_filetypes = ['png','jpg','gif','htm','html','php','php5'];
$disallowed_files = ['mike.php','.','..'];
$files = [];
foreach (new DirectoryIterator($current_dir_path) as $fn) {
$filename = $fn->getFilename();
if (in_array(strtolower($filename),$disallowed_files) == false) {
if (in_array($fn->getExtension(), $allowed_filetypes)) {
$files[] = $filename;
}
}
}
$dir = array('/' => $files);
echo json_encode($dir);
}
});
/*
* Get single file
*/
Mike::post('/file', function() {
$filename = dirname(__FILE__) . '/' . $_POST['file'];
$fp = fopen($filename, 'rb');
// send the right headers
header("Content-Type: text/plain");
header("Content-Length: " . filesize($filename));
fpassthru($fp);
//http_send_content_type("text/plain");
//http_send_file(dirname(__FILE__) . '/' .$filename);
});
/*
* 404
*/
if (!Mike::$isRouteProcessed) {
$content = str_replace("{{CONTENT}}", "Page not found", $GLOBALS['page_404']);
Mike::render($GLOBALS['layout'], $content,
['styles' => ['css/style.css'],
'title' => 'Mike.php',
'headers' => ['Cache-Control: no-cache, no-store, must-revalidate','Pragma: no-cache','Expires: 0']
]);
}