summaryrefslogtreecommitdiff
path: root/tools/inc/cli.php
blob: fbb3cfc586ddc6efa76065cbe3424f1cdb0fffa1 (plain)
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
<?php

/*
 *  This script will look for Tinyboard in the following places (in order):
 *    - ./
 *    - ./Tinyboard/
 *    - ../
 */

$shell_path = getcwd();

if(file_exists('inc/functions.php'))
	$dir = false;
elseif(file_exists('Tinyboard') && is_dir('Tinyboard') && file_exists('Tinyboard/inc/functions.php'))
	$dir = 'Tinyboard';
elseif(file_exists('../inc/functions.php'))
	$dir = '..';
else
	die('Could not locate Tinyboard directory!');

if($dir && !chdir($dir))
	die('Could not change directory to ' . $dir . '!');

// follow symlink
chdir(realpath('inc') . '/..');

require 'inc/functions.php';
require 'inc/display.php';
require 'inc/template.php';
require 'inc/database.php';
require 'inc/user.php';
require 'inc/mod.php';

$mod = Array(
	'id' => -1,
	'type' => ADMIN,
	'username' => '?',
	'boards' => Array('*')
);

function get_httpd_privileges() {
	global $config, $shell_path;
	
	if(php_sapi_name() != 'cli')
		die("get_httpd_privileges(): invoked from HTTP client.\n");
	
	echo "Dropping priviledges...\n";
	
	if(!is_writable('.'))
		die("get_httpd_privileges(): web directory is not writable\n");
	
	if(!is_writable('inc/'))
		die("get_httpd_privileges(): inc/ directory is not writable\n");
	
	$filename = '.' . md5(rand()) . '.php';
	
	echo "Copying rebuilder to web directory...\n";
	
	copy($shell_path . '/' . $_SERVER['PHP_SELF'], $filename);
	copy(__FILE__, 'inc/cli.php');
	
	chmod($filename, 0666);
	chmod('inc/cli.php', 0666);
	
	if(preg_match('/^https?:\/\//', $config['root'])) {
		$url = $config['root'] . $filename;
	} else {
		// assume localhost
		$url = 'http://localhost' . $config['root'] . $filename;
	}
	
	echo "Downloading $url\n";
	
	passthru('curl -s -N ' . escapeshellarg($url));
	
	unlink($filename);
	unlink('inc/cli.php');
	
	exit(0);
}