summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/cmdline.c540
-rw-r--r--examples/minimal.php4
-rw-r--r--examples/single.php406
-rw-r--r--examples/tabs.py32
4 files changed, 0 insertions, 982 deletions
diff --git a/examples/cmdline.c b/examples/cmdline.c
deleted file mode 100644
index 8e57604..0000000
--- a/examples/cmdline.c
+++ /dev/null
@@ -1,540 +0,0 @@
-#include <stdlib.h>
-#include <string.h>
-#include "cmdline.h"
-#include "command/args.h"
-#include "command/macro.h"
-#include "commands.h"
-#include "completion.h"
-#include "copy.h"
-#include "editor.h"
-#include "history.h"
-#include "options.h"
-#include "search.h"
-#include "terminal/osc52.h"
-#include "util/ascii.h"
-#include "util/bsearch.h"
-#include "util/debug.h"
-#include "util/log.h"
-#include "util/utf8.h"
-
-static void cmdline_delete(CommandLine *c)
-{
- size_t pos = c->pos;
- size_t len = 1;
-
- if (pos == c->buf.len) {
- return;
- }
-
- u_get_char(c->buf.buffer, c->buf.len, &pos);
- len = pos - c->pos;
- string_remove(&c->buf, c->pos, len);
-}
-
-void cmdline_clear(CommandLine *c)
-{
- string_clear(&c->buf);
- c->pos = 0;
- c->search_pos = NULL;
-}
-
-void cmdline_free(CommandLine *c)
-{
- cmdline_clear(c);
- string_free(&c->buf);
- free(c->search_text);
- reset_completion(c);
-}
-
-static void set_text(CommandLine *c, const char *text)
-{
- string_clear(&c->buf);
- const size_t text_len = strlen(text);
- c->pos = text_len;
- string_append_buf(&c->buf, text, text_len);
-}
-
-void cmdline_set_text(CommandLine *c, const char *text)
-{
- c->search_pos = NULL;
- set_text(c, text);
-}
-
-static bool cmd_bol(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- e->cmdline.pos = 0;
- reset_completion(&e->cmdline);
- return true;
-}
-
-static bool cmd_cancel(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- cmdline_clear(c);
- set_input_mode(e, INPUT_NORMAL);
- reset_completion(c);
- return true;
-}
-
-static bool cmd_clear(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- cmdline_clear(&e->cmdline);
- return true;
-}
-
-static bool cmd_copy(EditorState *e, const CommandArgs *a)
-{
- bool internal = cmdargs_has_flag(a, 'i') || a->flag_set == 0;
- bool clipboard = cmdargs_has_flag(a, 'b');
- bool primary = cmdargs_has_flag(a, 'p');
-
- String *buf = &e->cmdline.buf;
- size_t len = buf->len;
- if (internal) {
- char *str = string_clone_cstring(buf);
- record_copy(&e->clipboard, str, len, false);
- }
-
- Terminal *term = &e->terminal;
- if ((clipboard || primary) && term->features & TFLAG_OSC52_COPY) {
- const char *str = string_borrow_cstring(buf);
- if (!term_osc52_copy(&term->obuf, str, len, clipboard, primary)) {
- LOG_ERRNO("term_osc52_copy");
- // TODO: return false ?
- }
- }
-
- return true;
-}
-
-static bool cmd_delete(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- cmdline_delete(c);
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_delete_eol(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- c->buf.len = c->pos;
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_delete_word(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- const unsigned char *buf = c->buf.buffer;
- const size_t len = c->buf.len;
- size_t i = c->pos;
-
- if (i == len) {
- return true;
- }
-
- while (i < len && is_word_byte(buf[i])) {
- i++;
- }
-
- while (i < len && !is_word_byte(buf[i])) {
- i++;
- }
-
- string_remove(&c->buf, c->pos, i - c->pos);
-
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_eol(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- c->pos = c->buf.len;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_erase(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- if (c->pos > 0) {
- u_prev_char(c->buf.buffer, &c->pos);
- cmdline_delete(c);
- }
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_erase_bol(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- string_remove(&c->buf, 0, c->pos);
- c->pos = 0;
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_erase_word(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- size_t i = c->pos;
- if (i == 0) {
- return true;
- }
-
- // open /path/to/file^W => open /path/to/
-
- // erase whitespace
- while (i && ascii_isspace(c->buf.buffer[i - 1])) {
- i--;
- }
-
- // erase non-word bytes
- while (i && !is_word_byte(c->buf.buffer[i - 1])) {
- i--;
- }
-
- // erase word bytes
- while (i && is_word_byte(c->buf.buffer[i - 1])) {
- i--;
- }
-
- string_remove(&c->buf, i, c->pos - i);
- c->pos = i;
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool do_history_prev(const History *hist, CommandLine *c)
-{
- if (!c->search_pos) {
- free(c->search_text);
- c->search_text = string_clone_cstring(&c->buf);
- }
-
- if (history_search_forward(hist, &c->search_pos, c->search_text)) {
- BUG_ON(!c->search_pos);
- set_text(c, c->search_pos->text);
- }
-
- reset_completion(c);
- return true;
-}
-
-static bool do_history_next(const History *hist, CommandLine *c)
-{
- if (!c->search_pos) {
- goto out;
- }
-
- if (history_search_backward(hist, &c->search_pos, c->search_text)) {
- BUG_ON(!c->search_pos);
- set_text(c, c->search_pos->text);
- } else {
- set_text(c, c->search_text);
- c->search_pos = NULL;
- }
-
-out:
- reset_completion(c);
- return true;
-}
-
-static bool cmd_search_history_next(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- return do_history_next(&e->search_history, &e->cmdline);
-}
-
-static bool cmd_search_history_prev(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- return do_history_prev(&e->search_history, &e->cmdline);
-}
-
-static bool cmd_command_history_next(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- return do_history_next(&e->command_history, &e->cmdline);
-}
-
-static bool cmd_command_history_prev(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- return do_history_prev(&e->command_history, &e->cmdline);
-}
-
-static bool cmd_left(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- if (c->pos) {
- u_prev_char(c->buf.buffer, &c->pos);
- }
- reset_completion(c);
- return true;
-}
-
-static bool cmd_paste(EditorState *e, const CommandArgs *a)
-{
- CommandLine *c = &e->cmdline;
- const Clipboard *clip = &e->clipboard;
- string_insert_buf(&c->buf, c->pos, clip->buf, clip->len);
- if (cmdargs_has_flag(a, 'm')) {
- c->pos += clip->len;
- }
- c->search_pos = NULL;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_right(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- if (c->pos < c->buf.len) {
- u_get_char(c->buf.buffer, c->buf.len, &c->pos);
- }
- reset_completion(c);
- return true;
-}
-
-static bool cmd_toggle(EditorState *e, const CommandArgs *a)
-{
- const char *option_name = a->args[0];
- bool global = cmdargs_has_flag(a, 'g');
- size_t nr_values = a->nr_args - 1;
- if (nr_values == 0) {
- return toggle_option(e, option_name, global, false);
- }
-
- char **values = a->args + 1;
- return toggle_option_values(e, option_name, global, false, values, nr_values);
-}
-
-static bool cmd_word_bwd(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- if (c->pos <= 1) {
- c->pos = 0;
- return true;
- }
-
- const unsigned char *const buf = c->buf.buffer;
- size_t i = c->pos - 1;
-
- while (i > 0 && !is_word_byte(buf[i])) {
- i--;
- }
-
- while (i > 0 && is_word_byte(buf[i])) {
- i--;
- }
-
- if (i > 0) {
- i++;
- }
-
- c->pos = i;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_word_fwd(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- const unsigned char *buf = c->buf.buffer;
- const size_t len = c->buf.len;
- size_t i = c->pos;
-
- while (i < len && is_word_byte(buf[i])) {
- i++;
- }
-
- while (i < len && !is_word_byte(buf[i])) {
- i++;
- }
-
- c->pos = i;
- reset_completion(c);
- return true;
-}
-
-static bool cmd_complete_next(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- complete_command_next(e);
- return true;
-}
-
-static bool cmd_complete_prev(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- complete_command_prev(e);
- return true;
-}
-
-static bool cmd_direction(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- toggle_search_direction(&e->search);
- return true;
-}
-
-static bool cmd_command_mode_accept(EditorState *e, const CommandArgs *a)
-{
- BUG_ON(a->nr_args);
- CommandLine *c = &e->cmdline;
- reset_completion(c);
- set_input_mode(e, INPUT_NORMAL);
-
- const char *str = string_borrow_cstring(&c->buf);
- cmdline_clear(c);
- if (!cmdargs_has_flag(a, 'H') && str[0] != ' ') {
- // This is done before handle_command() because "command [text]"
- // can modify the contents of the command-line
- history_add(&e->command_history, str);
- }
-
- current_command = NULL;
- return handle_normal_command(e, str, true);
-}
-
-static bool cmd_search_mode_accept(EditorState *e, const CommandArgs *a)
-{
- CommandLine *c = &e->cmdline;
- if (cmdargs_has_flag(a, 'e')) {
- if (c->buf.len == 0) {
- return true;
- }
- // Escape the regex; to match as plain text
- char *original = string_clone_cstring(&c->buf);
- size_t len = c->buf.len;
- string_clear(&c->buf);
- for (size_t i = 0; i < len; i++) {
- char ch = original[i];
- if (is_regex_special_char(ch)) {
- string_append_byte(&c->buf, '\\');
- }
- string_append_byte(&c->buf, ch);
- }
- free(original);
- }
-
- const char *str = NULL;
- bool add_to_history = !cmdargs_has_flag(a, 'H');
- if (c->buf.len > 0) {
- str = string_borrow_cstring(&c->buf);
- BUG_ON(!str);
- search_set_regexp(&e->search, str);
- if (add_to_history) {
- history_add(&e->search_history, str);
- }
- }
-
- if (e->macro.recording) {
- const char *args[5];
- size_t i = 0;
- if (str) {
- if (e->search.reverse) {
- args[i++] = "-r";
- }
- if (!add_to_history) {
- args[i++] = "-H";
- }
- if (unlikely(str[0] == '-')) {
- args[i++] = "--";
- }
- args[i++] = str;
- } else {
- args[i++] = e->search.reverse ? "-p" : "-n";
- }
- args[i] = NULL;
- macro_command_hook(&e->macro, "search", (char**)args);
- }
-
- current_command = NULL;
- bool found = search_next(e->view, &e->search, e->options.case_sensitive_search);
- cmdline_clear(c);
- set_input_mode(e, INPUT_NORMAL);
- return found;
-}
-
-IGNORE_WARNING("-Wincompatible-pointer-types")
-
-static const Command common_cmds[] = {
- {"bol", "", false, 0, 0, cmd_bol},
- {"cancel", "", false, 0, 0, cmd_cancel},
- {"clear", "", false, 0, 0, cmd_clear},
- {"copy", "bip", false, 0, 0, cmd_copy},
- {"delete", "", false, 0, 0, cmd_delete},
- {"delete-eol", "", false, 0, 0, cmd_delete_eol},
- {"delete-word", "", false, 0, 0, cmd_delete_word},
- {"eol", "", false, 0, 0, cmd_eol},
- {"erase", "", false, 0, 0, cmd_erase},
- {"erase-bol", "", false, 0, 0, cmd_erase_bol},
- {"erase-word", "", false, 0, 0, cmd_erase_word},
- {"left", "", false, 0, 0, cmd_left},
- {"paste", "m", false, 0, 0, cmd_paste},
- {"right", "", false, 0, 0, cmd_right},
- {"toggle", "g", false, 1, -1, cmd_toggle},
- {"word-bwd", "", false, 0, 0, cmd_word_bwd},
- {"word-fwd", "", false, 0, 0, cmd_word_fwd},
-};
-
-static const Command search_cmds[] = {
- {"accept", "eH", false, 0, 0, cmd_search_mode_accept},
- {"direction", "", false, 0, 0, cmd_direction},
- {"history-next", "", false, 0, 0, cmd_search_history_next},
- {"history-prev", "", false, 0, 0, cmd_search_history_prev},
-};
-
-static const Command command_cmds[] = {
- {"accept", "H", false, 0, 0, cmd_command_mode_accept},
- {"complete-next", "", false, 0, 0, cmd_complete_next},
- {"complete-prev", "", false, 0, 0, cmd_complete_prev},
- {"history-next", "", false, 0, 0, cmd_command_history_next},
- {"history-prev", "", false, 0, 0, cmd_command_history_prev},
-};
-
-UNIGNORE_WARNINGS
-
-static const Command *find_cmd_mode_command(const char *name)
-{
- const Command *cmd = BSEARCH(name, common_cmds, command_cmp);
- return cmd ? cmd : BSEARCH(name, command_cmds, command_cmp);
-}
-
-static const Command *find_search_mode_command(const char *name)
-{
- const Command *cmd = BSEARCH(name, common_cmds, command_cmp);
- return cmd ? cmd : BSEARCH(name, search_cmds, command_cmp);
-}
-
-const CommandSet cmd_mode_commands = {
- .lookup = find_cmd_mode_command
-};
-
-const CommandSet search_mode_commands = {
- .lookup = find_search_mode_command
-};
diff --git a/examples/minimal.php b/examples/minimal.php
deleted file mode 100644
index 69a82f5..0000000
--- a/examples/minimal.php
+++ /dev/null
@@ -1,4 +0,0 @@
-<?php
-function hello() {}
-class World {}
-const VERSION = "1.0";
diff --git a/examples/single.php b/examples/single.php
deleted file mode 100644
index 7ab8ace..0000000
--- a/examples/single.php
+++ /dev/null
@@ -1,406 +0,0 @@
-<?php
-class CONFIG
-{
- const MAX_FILESIZE = 512; //max. filesize in MiB
- const MAX_FILEAGE = 180; //max. age of files in days
- const MIN_FILEAGE = 31; //min. age of files in days
- const DECAY_EXP = 2; //high values penalise larger files more
-
- const UPLOAD_TIMEOUT = 5*60; //max. time an upload can take before it times out
- const MIN_ID_LENGTH = 3; //min. length of the random file ID
- const MAX_ID_LENGTH = 24; //max. length of the random file ID, set to MIN_ID_LENGTH to disable
- const STORE_PATH = 'files/'; //directory to store uploaded files in
- const LOG_PATH = null; //path to log uploads + resulting links to
- const DOWNLOAD_PATH = '%s'; //the path part of the download url. %s = placeholder for filename
- const MAX_EXT_LEN = 7; //max. length for file extensions
- const EXTERNAL_HOOK = null; //external program to call for each upload
- const AUTO_FILE_EXT = false; //automatically try to detect file extension for files that have none
-
- const FORCE_HTTPS = false; //force generated links to be https://
-
- const ADMIN_EMAIL = 'admin@example.com'; //address for inquiries
-
- public static function SITE_URL() : string
- {
- $proto = ($_SERVER['HTTPS'] ?? 'off') == 'on' || CONFIG::FORCE_HTTPS ? 'https' : 'http';
- return "$proto://{$_SERVER['HTTP_HOST']}";
- }
-
- public static function SCRIPT_URL() : string
- {
- return CONFIG::SITE_URL().$_SERVER['REQUEST_URI'];
- }
-};
-
-
-// generate a random string of characters with given length
-function rnd_str(int $len) : string
-{
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
- $chars_len = strlen($chars);
- $random = random_bytes($len);
- $out = '';
-
- for ($i = 0; $i < $len; ++$i)
- {
- $out .= $chars[ord($random[$i]) % $chars_len];
- }
-
- return $out;
-}
-
-// check php.ini settings and print warnings if anything's not configured properly
-function check_config() : void
-{
- $warn_config_value = function($ini_name, $var_name, $var_val)
- {
- $ini_val = intval(ini_get($ini_name));
- if ($ini_val < $var_val)
- print("<pre>Warning: php.ini: $ini_name ($ini_val) set lower than $var_name ($var_val)\n</pre>");
- };
-
- $warn_config_value('upload_max_filesize', 'MAX_FILESIZE', CONFIG::MAX_FILESIZE);
- $warn_config_value('post_max_size', 'MAX_FILESIZE', CONFIG::MAX_FILESIZE);
- $warn_config_value('max_input_time', 'UPLOAD_TIMEOUT', CONFIG::UPLOAD_TIMEOUT);
- $warn_config_value('max_execution_time', 'UPLOAD_TIMEOUT', CONFIG::UPLOAD_TIMEOUT);
-}
-
-//extract extension from a path (does not include the dot)
-function ext_by_path(string $path) : string
-{
- $ext = pathinfo($path, PATHINFO_EXTENSION);
- //special handling of .tar.* archives
- $ext2 = pathinfo(substr($path,0,-(strlen($ext)+1)), PATHINFO_EXTENSION);
- if ($ext2 === 'tar')
- {
- $ext = $ext2.'.'.$ext;
- }
- return $ext;
-}
-
-function ext_by_finfo(string $path) : string
-{
- $finfo = finfo_open(FILEINFO_EXTENSION);
- $finfo_ext = finfo_file($finfo, $path);
- finfo_close($finfo);
- if ($finfo_ext != '???')
- {
- return explode('/', $finfo_ext, 2)[0];
- }
- else
- {
- $finfo = finfo_open();
- $finfo_info = finfo_file($finfo, $path);
- finfo_close($finfo);
- if (strstr($finfo_info, 'text') !== false)
- {
- return 'txt';
- }
- }
- return '';
-}
-
-// store an uploaded file, given its name and temporary path (e.g. values straight out of $_FILES)
-// files are stored wit a randomised name, but with their original extension
-//
-// $name: original filename
-// $tmpfile: temporary path of uploaded file
-// $formatted: set to true to display formatted message instead of bare link
-function store_file(string $name, string $tmpfile, bool $formatted = false) : void
-{
- //create folder, if it doesn't exist
- if (!file_exists(CONFIG::STORE_PATH))
- {
- mkdir(CONFIG::STORE_PATH, 0750, true); //TODO: error handling
- }
-
- //check file size
- $size = filesize($tmpfile);
- if ($size > CONFIG::MAX_FILESIZE * 1024 * 1024)
- {
- header('HTTP/1.0 413 Payload Too Large');
- print("Error 413: Max File Size ({CONFIG::MAX_FILESIZE} MiB) Exceeded\n");
- return;
- }
- if ($size == 0)
- {
- header('HTTP/1.0 400 Bad Request');
- print('Error 400: Uploaded file is empty\n');
- return;
- }
-
- $ext = ext_by_path($name);
- if (empty($ext) && CONFIG::AUTO_FILE_EXT)
- {
- $ext = ext_by_finfo($tmpfile);
- }
- $ext = substr($ext, 0, CONFIG::MAX_EXT_LEN);
- $tries_per_len=3; //try random names a few times before upping the length
-
- $id_length=CONFIG::MIN_ID_LENGTH;
- if(isset($_POST['id_length']) && ctype_digit($_POST['id_length'])) {
- $id_length = max(CONFIG::MIN_ID_LENGTH, min(CONFIG::MAX_ID_LENGTH, $_POST['id_length']));
- }
-
- for ($len = $id_length; ; ++$len)
- {
- for ($n=0; $n<=$tries_per_len; ++$n)
- {
- $id = rnd_str($len);
- $basename = $id . (empty($ext) ? '' : '.' . $ext);
- $target_file = CONFIG::STORE_PATH . $basename;
-
- if (!file_exists($target_file))
- break 2;
- }
- }
-
- $res = move_uploaded_file($tmpfile, $target_file);
- if (!$res)
- {
- //TODO: proper error handling?
- header('HTTP/1.0 520 Unknown Error');
- return;
- }
-
- if (CONFIG::EXTERNAL_HOOK !== null)
- {
- putenv('REMOTE_ADDR='.$_SERVER['REMOTE_ADDR']);
- putenv('ORIGINAL_NAME='.$name);
- putenv('STORED_FILE='.$target_file);
- $ret = -1;
- $out = null;
- $last_line = exec(CONFIG::EXTERNAL_HOOK, $out, $ret);
- if ($last_line !== false && $ret !== 0)
- {
- unlink($target_file);
- header('HTTP/1.0 400 Bad Request');
- print("Error: $last_line\n");
- return;
- }
- }
-
- //print the download link of the file
- $url = sprintf(CONFIG::SITE_URL().'/'.CONFIG::DOWNLOAD_PATH, $basename);
-
- if ($formatted)
- {
- print("<pre>Access your file here: <a href=\"$url\">$url</a></pre>");
- }
- else
- {
- print("$url\n");
- }
-
- // log uploader's IP, original filename, etc.
- if (CONFIG::LOG_PATH)
- {
- file_put_contents(
- CONFIG::LOG_PATH,
- implode("\t", array(
- date('c'),
- $_SERVER['REMOTE_ADDR'],
- filesize($tmpfile),
- escapeshellarg($name),
- $basename
- )) . "\n",
- FILE_APPEND
- );
- }
-}
-
-// purge all files older than their retention period allows.
-function purge_files() : void
-{
- $num_del = 0; //number of deleted files
- $total_size = 0; //total size of deleted files
-
- //for each stored file
- foreach (scandir(CONFIG::STORE_PATH) as $file)
- {
- //skip virtual . and .. files
- if ($file === '.' ||
- $file === '..')
- {
- continue;
- }
-
- $file = CONFIG::STORE_PATH . $file;
-
- $file_size = filesize($file) / (1024*1024); //size in MiB
- $file_age = (time()-filemtime($file)) / (60*60*24); //age in days
-
- //keep all files below the min age
- if ($file_age < CONFIG::MIN_FILEAGE)
- {
- continue;
- }
-
- //calculate the maximum age in days for this file
- $file_max_age = CONFIG::MIN_FILEAGE +
- (CONFIG::MAX_FILEAGE - CONFIG::MIN_FILEAGE) *
- pow(1 - ($file_size / CONFIG::MAX_FILESIZE), CONFIG::DECAY_EXP);
-
- //delete if older
- if ($file_age > $file_max_age)
- {
- unlink($file);
-
- print("deleted $file, $file_size MiB, $file_age days old\n");
- $num_del += 1;
- $total_size += $file_size;
- }
- }
- print("Deleted $num_del files totalling $total_size MiB\n");
-}
-
-function send_text_file(string $filename, string $content) : void
-{
- header('Content-type: application/octet-stream');
- header("Content-Disposition: attachment; filename=\"$filename\"");
- header('Content-Length: '.strlen($content));
- print($content);
-}
-
-// send a ShareX custom uploader config as .json
-function send_sharex_config() : void
-{
- $name = $_SERVER['SERVER_NAME'];
- $site_url = str_replace("?sharex", "", CONFIG::SCRIPT_URL());
- send_text_file($name.'.sxcu', <<<EOT
-{
- "Name": "$name",
- "DestinationType": "ImageUploader, FileUploader",
- "RequestType": "POST",
- "RequestURL": "$site_url",
- "FileFormName": "file",
- "ResponseType": "Text"
-}
-EOT);
-}
-
-// send a Hupl uploader config as .hupl (which is just JSON)
-function send_hupl_config() : void
-{
- $name = $_SERVER['SERVER_NAME'];
- $site_url = str_replace("?hupl", "", CONFIG::SCRIPT_URL());
- send_text_file($name.'.hupl', <<<EOT
-{
- "name": "$name",
- "type": "http",
- "targetUrl": "$site_url",
- "fileParam": "file"
-}
-EOT);
-}
-
-// print a plaintext info page, explaining what this script does and how to
-// use it, how to upload, etc.
-function print_index() : void
-{
- $site_url = CONFIG::SCRIPT_URL();
- $sharex_url = $site_url.'?sharex';
- $hupl_url = $site_url.'?hupl';
- $decay = CONFIG::DECAY_EXP;
- $min_age = CONFIG::MIN_FILEAGE;
- $max_size = CONFIG::MAX_FILESIZE;
- $max_age = CONFIG::MAX_FILEAGE;
- $mail = CONFIG::ADMIN_EMAIL;
- $max_id_length = CONFIG::MAX_ID_LENGTH;
-
- $length_info = "\nTo use a longer file ID (up to $max_id_length characters), add -F id_length=&lt;number&gt;\n";
- if (CONFIG::MIN_ID_LENGTH == CONFIG::MAX_ID_LENGTH)
- {
- $length_info = "";
- }
-
-echo <<<EOT
-<!DOCTYPE html>
-<html lang="en">
-<head>
- <title>Filehost</title>
- <meta name="description" content="Minimalistic service for sharing temporary files." />
- <meta name="viewport" content="width=device-width, initial-scale=1" />
-</head>
-<body>
-<pre>
- === How To Upload ===
-You can upload files to this site via a simple HTTP POST, e.g. using curl:
-curl -F "file=@/path/to/your/file.jpg" $site_url
-
-Or if you want to pipe to curl *and* have a file extension, add a "filename":
-echo "hello" | curl -F "file=@-;filename=.txt" $site_url
-$length_info
-On Windows, you can use <a href="https://getsharex.com/">ShareX</a> and import <a href="$sharex_url">this</a> custom uploader.
-On Android, you can use an app called <a href="https://github.com/Rouji/Hupl">Hupl</a> with <a href="$hupl_url">this</a> uploader.
-
-
-Or simply choose a file and click "Upload" below:
-(Hint: If you're lucky, your browser may support drag-and-drop onto the file
-selection input.)
-</pre>
-<form id="frm" method="post" enctype="multipart/form-data">
-<input type="file" name="file" id="file" />
-<input type="hidden" name="formatted" value="true" />
-<input type="submit" value="Upload"/>
-</form>
-<pre>
-
-
- === File Sizes etc. ===
-The maximum allowed file size is $max_size MiB.
-
-Files are kept for a minimum of $min_age, and a maximum of $max_age Days.
-
-How long a file is kept depends on its size. Larger files are deleted earlier
-than small ones. This relation is non-linear and skewed in favour of small
-files.
-
-The exact formula for determining the maximum age for a file is:
-
-MIN_AGE + (MAX_AGE - MIN_AGE) * (1-(FILE_SIZE/MAX_SIZE))^$decay
-
-
- === Source ===
-The PHP script used to provide this service is open source and available on
-<a href="https://github.com/Rouji/single_php_filehost">GitHub</a>
-
-
- === Contact ===
-If you want to report abuse of this service, or have any other inquiries,
-please write an email to $mail
-</pre>
-</body>
-</html>
-EOT;
-}
-
-
-// decide what to do, based on POST parameters etc.
-if (isset($_FILES['file']['name']) &&
- isset($_FILES['file']['tmp_name']) &&
- is_uploaded_file($_FILES['file']['tmp_name']))
-{
- //file was uploaded, store it
- $formatted = isset($_REQUEST['formatted']);
- store_file($_FILES['file']['name'],
- $_FILES['file']['tmp_name'],
- $formatted);
-}
-else if (isset($_GET['sharex']))
-{
- send_sharex_config();
-}
-else if (isset($_GET['hupl']))
-{
- send_hupl_config();
-}
-else if ($argv[1] ?? null === 'purge')
-{
- purge_files();
-}
-else
-{
- check_config();
- print_index();
-}
diff --git a/examples/tabs.py b/examples/tabs.py
deleted file mode 100644
index 4479f5c..0000000
--- a/examples/tabs.py
+++ /dev/null
@@ -1,32 +0,0 @@
-def set_password(args):
- password = args.password
- while not password :
- password1 = getpass("" if args.quiet else "Provide password: ")
- password_repeat = getpass("" if args.quiet else "Repeat password: ")
- if password1 != password_repeat:
- print("Passwords do not match, try again")
- elif len(password1) < 4:
- print("Please provide at least 4 characters")
- else:
- password = password1
-
- password_hash = passwd(password)
- cfg = BaseJSONConfigManager(config_dir=jupyter_config_dir())
- cfg.update('jupyter_notebook_config', {
- 'NotebookApp': {
- 'password': password_hash,
- }
- })
- if not args.quiet:
- print("password stored in config dir: %s" % jupyter_config_dir())
-
-def main(argv):
- parser = argparse.ArgumentParser(argv[0])
- subparsers = parser.add_subparsers()
- parser_password = subparsers.add_parser('password', help='sets a password for your notebook server')
- parser_password.add_argument("password", help="password to set, if not given, a password will be queried for (NOTE: this may not be safe)",
- nargs="?")
- parser_password.add_argument("--quiet", help="suppress messages", action="store_true")
- parser_password.set_defaults(function=set_password)
- args = parser.parse_args(argv[1:])
- args.function(args)