summaryrefslogtreecommitdiff
path: root/tools/inc/cli.php
diff options
context:
space:
mode:
authorMichael Save <savetheinternet@tinyboard.org>2012-03-15 01:44:29 +1100
committerMichael Save <savetheinternet@tinyboard.org>2012-03-15 01:44:29 +1100
commit64c1f6821c90297cf3d1c4b8a4bbec6fe4449016 (patch)
tree56942e328c37a2dfd63b4dca0009f08b2fae15dd /tools/inc/cli.php
parent2482723ff2f567c4b041f9f0840f34049f0881b7 (diff)
get_httpd_privileges() function for CLI scripts
Diffstat (limited to 'tools/inc/cli.php')
-rw-r--r--tools/inc/cli.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/inc/cli.php b/tools/inc/cli.php
index 5305c1ac..fbb3cfc5 100644
--- a/tools/inc/cli.php
+++ b/tools/inc/cli.php
@@ -1,5 +1,36 @@
<?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,
@@ -7,4 +38,44 @@ $mod = Array(
'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);
+}