freedomboneeee/src/freedombone-app-edith

468 lines
15 KiB
Plaintext
Raw Normal View History

2018-02-02 12:46:09 +01:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2018-02-02 12:46:09 +01:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2018-02-02 12:46:09 +01:00
#
# Edith: an ultra simple notes application
#
# License
# =======
#
# Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
VARIANTS='full full-vim writer'
IN_DEFAULT_INSTALL=0
SHOW_ON_ABOUT=1
EDITH_REPO="https://github.com/sunny/edith"
EDITH_COMMIT='59f571e24be9e75d127a0f130591acf8d7a86ce3'
EDITH_DOMAIN_NAME=
EDITH_CODE=
EDITH_ONION_PORT=8278
EDITH_LOGIN_TEXT=$"Edith login"
EDITH_SHORT_DESCRIPTION=$'Edith Notes'
EDITH_DESCRIPTION=$'Extremely simple note taking'
EDITH_MOBILE_APP_URL=
2018-02-02 12:46:09 +01:00
edith_variables=(MY_USERNAME
MY_EMAIL_ADDRESS
ONION_ONLY
EDITH_DOMAIN_NAME
EDITH_CODE
DEFAULT_LANGUAGE)
2018-02-03 12:42:00 +01:00
function remove_bad_links_edith {
read_config_param EDITH_DOMAIN_NAME
2018-02-26 14:50:40 +01:00
edith_dir="/var/www/$EDITH_DOMAIN_NAME/htdocs"
2018-02-03 12:42:00 +01:00
# copy jquery locally
jquery_version='1.12.4'
if [ ! -f $edith_dir/jquery-${jquery_version}.js ]; then
2018-02-26 14:50:40 +01:00
cd $edith_dir || exit 246824628
2018-02-03 12:42:00 +01:00
wget https://code.jquery.com/jquery-${jquery_version}.js
if [ -f $edith_dir/jquery-${jquery_version}.js ]; then
jquery_hash=$(sha256sum $edith_dir/jquery-${jquery_version}.js | awk -F ' ' '{print $1}')
if [[ "$jquery_hash" != '430f36f9b5f21aae8cc9dca6a81c4d3d84da5175eaedcf2fdc2c226302cb3575' ]]; then
echo $'Unexpected jquery hash value'
exit 6783653856
fi
else
echo $"Unable to download https://code.jquery.com/jquery-${jquery_version}.js"
exit 7384673583
fi
fi
sed -i "s|//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js|jquery-${jquery_version}.js|g" $edith_dir/templates/default.php
sed -i '/googleapi/d' $edith_dir/templates/remark.php
sed -i "s|//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js|jquery-${jquery_version}.js|g" $edith_dir/templates/html.php
}
2018-02-02 12:46:09 +01:00
function change_password_edith {
curr_username="$1"
new_user_password="$2"
sed -i "/${curr_username}:/d" /etc/nginx/.edithpasswd
2018-02-26 14:50:40 +01:00
echo -n "$new_user_password" | htpasswd -i -s -c /etc/nginx/.edithpasswd "${curr_username}"
2018-02-02 12:46:09 +01:00
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a "${curr_username}" -p "$new_user_password"
2018-02-02 12:46:09 +01:00
}
function logging_on_edith {
echo -n ''
}
function logging_off_edith {
echo -n ''
}
function reconfigure_edith {
echo -n ''
}
function edith_enable_login {
read_config_param EDITH_DOMAIN_NAME
dialog --title $"Enable Edith login" \
--backtitle $"Freedombone Control Panel" \
--defaultno \
2018-02-26 14:50:40 +01:00
--yesno $"\\nDo you want to add a login so that random web users can't access your notes?" 10 60
2018-02-02 12:46:09 +01:00
sel=$?
case $sel in
2018-02-26 14:50:40 +01:00
0) if grep -q '#auth_basic' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"; then
sed -i 's|#auth_basic|auth_basic|g' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
2018-02-02 12:46:09 +01:00
systemctl restart nginx
fi
2018-02-26 14:50:40 +01:00
read_config_param "$MY_USERNAME"
EDITH_PASSWORD=$("${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a edith)
2018-02-02 12:46:09 +01:00
dialog --title $"Enable Edith login" \
--msgbox $"Edith logins are now enabled with the password $EDITH_PASSWORD" 6 65
;;
2018-02-26 14:50:40 +01:00
1) if ! grep -q '#auth_basic' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"; then
sed -i 's|auth_basic|#auth_basic|g' "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
2018-02-02 12:46:09 +01:00
systemctl restart nginx
fi
dialog --title $"Disable Edith login" \
--msgbox $"Edith logins are now disabled. Anyone can access your stream." 6 65
;;
esac
}
function edith_browse {
read_config_param EDITH_DOMAIN_NAME
2018-02-26 14:50:40 +01:00
cd "/var/www/$EDITH_DOMAIN_NAME/htdocs/data" || exit 24682642
editor "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
2018-02-02 12:46:09 +01:00
}
function configure_interactive_edith {
2018-04-04 13:33:00 +02:00
W=(1 $"Enable login"
2 $"Browse notes")
2018-02-02 12:46:09 +01:00
while true
do
2018-04-04 13:33:00 +02:00
# shellcheck disable=SC2068
selection=$(dialog --backtitle $"Freedombone Administrator Control Panel" --title $"Edith" --menu $"Choose an operation, or ESC to exit:" 10 60 2 "${W[@]}" 3>&2 2>&1 1>&3)
if [ ! "$selection" ]; then
break
fi
case $selection in
2018-02-02 12:46:09 +01:00
1) edith_enable_login;;
2) edith_browse;;
esac
done
}
function upgrade_edith {
CURR_EDITH_COMMIT=$(get_completion_param "edith commit")
if [[ "$CURR_EDITH_COMMIT" == "$EDITH_COMMIT" ]]; then
return
fi
read_config_param EDITH_DOMAIN_NAME
# update to the next commit
function_check set_repo_commit
2018-02-26 14:50:40 +01:00
set_repo_commit "/var/www/$EDITH_DOMAIN_NAME/htdocs" "edith commit" "$EDITH_COMMIT" "$EDITH_REPO"
2018-02-02 12:46:09 +01:00
2018-02-03 12:42:00 +01:00
remove_bad_links_edith
2018-02-26 14:50:40 +01:00
chown -R www-data:www-data "/var/www/$EDITH_DOMAIN_NAME/htdocs"
chmod a+w "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
2018-02-02 12:46:09 +01:00
}
function backup_local_edith {
read_config_param EDITH_DOMAIN_NAME
function_check suspend_site
2018-02-26 14:50:40 +01:00
suspend_site "${EDITH_DOMAIN_NAME}"
2018-02-02 12:46:09 +01:00
2018-02-26 14:50:40 +01:00
source_directory="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
2018-02-02 12:46:09 +01:00
function_check backup_directory_to_usb
dest_directory=edith
2018-02-26 14:50:40 +01:00
backup_directory_to_usb "$source_directory" "$dest_directory"
2018-02-02 12:46:09 +01:00
function_check restart_site
restart_site
}
function restore_local_edith {
read_config_param EDITH_DOMAIN_NAME
temp_restore_dir=/root/tempedith
2018-02-26 14:50:40 +01:00
edith_dir="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
2018-02-02 12:46:09 +01:00
function_check restore_directory_from_usb
restore_directory_from_usb $temp_restore_dir edith
if [ -d $temp_restore_dir ]; then
2018-02-26 14:50:40 +01:00
if [ -d $temp_restore_dir$edith_dir ]; then
2018-02-02 12:46:09 +01:00
cp -rp $temp_restore_dir$edith_dir $edith_dir/
else
if [ ! -d $edith_dir ]; then
mkdir $edith_dir
chmod a+w $edith_dir
fi
cp -rp $temp_restore_dir/* $edith_dir
fi
chown -R www-data:www-data $edith_dir
rm -rf $temp_restore_dir
fi
}
function backup_remote_edith {
read_config_param EDITH_DOMAIN_NAME
function_check suspend_site
2018-02-26 14:50:40 +01:00
suspend_site "${EDITH_DOMAIN_NAME}"
2018-02-02 12:46:09 +01:00
2018-02-26 14:50:40 +01:00
source_directory="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
2018-02-02 12:46:09 +01:00
function_check backup_directory_to_friend
dest_directory=edith
2018-02-26 14:50:40 +01:00
backup_directory_to_friend "$source_directory" "$dest_directory"
2018-02-02 12:46:09 +01:00
function_check restart_site
restart_site
}
function restore_remote_edith {
read_config_param EDITH_DOMAIN_NAME
temp_restore_dir=/root/tempedith
2018-02-26 14:50:40 +01:00
edith_dir="/var/www/${EDITH_DOMAIN_NAME}/htdocs/data"
2018-02-02 12:46:09 +01:00
function_check restore_directory_from_friend
restore_directory_from_friend $temp_restore_dir edith
if [ -d $temp_restore_dir ]; then
2018-02-26 14:50:40 +01:00
if [ -d $temp_restore_dir$edith_dir ]; then
2018-02-02 12:46:09 +01:00
cp -rp $temp_restore_dir$edith_dir $edith_dir/
else
if [ ! -d $edith_dir ]; then
mkdir $edith_dir
chmod a+w $edith_dir
fi
cp -rp $temp_restore_dir/* $edith_dir
fi
chown -R www-data:www-data $edith_dir
rm -rf $temp_restore_dir
fi
}
function remove_edith {
nginx_dissite $EDITH_DOMAIN_NAME
2018-02-26 14:50:40 +01:00
if [ -f "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME" ]; then
rm "/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
2018-02-02 12:46:09 +01:00
fi
2018-02-26 14:50:40 +01:00
if [ -d "/var/www/$EDITH_DOMAIN_NAME" ]; then
rm -rf "/var/www/$EDITH_DOMAIN_NAME"
2018-02-02 12:46:09 +01:00
fi
function_check remove_onion_service
remove_onion_service edith ${EDITH_ONION_PORT}
2018-02-26 14:50:40 +01:00
sed -i '/edith/d' "$COMPLETION_FILE"
2018-02-02 12:46:09 +01:00
if [ -f /etc/nginx/.edithpasswd ]; then
rm /etc/nginx/.edithpasswd
fi
function_check remove_nodejs
remove_nodejs edith
2018-02-02 13:21:59 +01:00
remove_certs $EDITH_DOMAIN_NAME
remove_app edith
function_check remove_ddns_domain
remove_ddns_domain $EDITH_DOMAIN_NAME
2018-02-02 12:46:09 +01:00
}
function install_edith {
apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
function_check install_nodejs
install_nodejs edith
2018-02-26 14:50:40 +01:00
if [ ! "${EDITH_PASSWORD}" ]; then
if [ -f "${IMAGE_PASSWORD_FILE}" ]; then
EDITH_PASSWORD="$(printf "%s" "$(cat "$IMAGE_PASSWORD_FILE")")"
2018-02-02 12:46:09 +01:00
else
2018-02-26 14:50:40 +01:00
EDITH_PASSWORD="$(create_password "${MINIMUM_PASSWORD_LENGTH}")"
2018-02-02 12:46:09 +01:00
fi
fi
2018-02-26 14:50:40 +01:00
if [ -d "/var/www/$EDITH_DOMAIN_NAME/htdocs" ]; then
rm -rf "/var/www/$EDITH_DOMAIN_NAME/htdocs"
2018-02-02 12:55:10 +01:00
fi
2018-02-02 12:53:10 +01:00
if [ -d /repos/edith ]; then
2018-02-26 14:50:40 +01:00
mkdir "/var/www/$EDITH_DOMAIN_NAME/htdocs"
cp -r -p /repos/edith/. "/var/www/$EDITH_DOMAIN_NAME/htdocs"
cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 24682462
2018-02-02 12:53:10 +01:00
git pull
else
function_check git_clone
2018-02-26 14:50:40 +01:00
git_clone "$EDITH_REPO" "/var/www/$EDITH_DOMAIN_NAME/htdocs"
2018-02-02 12:46:09 +01:00
fi
2018-02-02 12:53:10 +01:00
if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
echo $'Unable to clone edith repo'
exit 537593569
2018-02-02 12:46:09 +01:00
fi
2018-02-26 14:50:40 +01:00
cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 2648248
git checkout "$EDITH_COMMIT" -b "$EDITH_COMMIT"
2018-02-02 12:46:09 +01:00
set_completion_param "edith commit" "$EDITH_COMMIT"
2018-02-26 14:50:40 +01:00
if [ ! -d "/var/www/$EDITH_DOMAIN_NAME/htdocs/data" ]; then
mkdir -p "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
2018-02-02 12:46:09 +01:00
fi
2018-02-02 13:27:42 +01:00
EDITH_ONION_HOSTNAME=$(add_onion_service edith 80 ${EDITH_ONION_PORT})
2018-02-26 14:50:40 +01:00
edith_nginx_site="/etc/nginx/sites-available/$EDITH_DOMAIN_NAME"
if [[ "$ONION_ONLY" == "no" ]]; then
2018-02-02 12:46:09 +01:00
function_check nginx_http_redirect
2018-02-26 14:50:40 +01:00
nginx_http_redirect "$EDITH_DOMAIN_NAME" "index index.php"
{ echo 'server {';
echo ' listen 443 ssl;';
echo ' #listen [::]:443 ssl;';
echo " server_name $EDITH_DOMAIN_NAME;";
echo ''; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check nginx_compress
2018-02-26 14:50:40 +01:00
nginx_compress "$EDITH_DOMAIN_NAME"
echo '' >> "$edith_nginx_site"
echo ' # Security' >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check nginx_ssl
2018-02-26 14:50:40 +01:00
nginx_ssl "$EDITH_DOMAIN_NAME"
2018-02-02 12:46:09 +01:00
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "$EDITH_DOMAIN_NAME"
2018-02-26 14:50:40 +01:00
{ echo ' add_header Strict-Transport-Security max-age=15768000;';
echo '';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;";
echo '';
echo ' index index.php;';
echo '';
echo ' # PHP';
echo ' location ~ \.php {';
echo ' include snippets/fastcgi-php.conf;';
echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
echo ' fastcgi_read_timeout 30;';
echo ' }';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check nginx_limits
2018-02-26 14:50:40 +01:00
nginx_limits "$EDITH_DOMAIN_NAME" '15m'
{ echo " try_files \$uri \$uri/ /index.php?\$args;";
echo " auth_basic \"${EDITH_LOGIN_TEXT}\";";
echo ' auth_basic_user_file /etc/nginx/.edithpasswd;';
echo ' }';
echo '}'; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
else
2018-02-26 14:50:40 +01:00
echo -n '' > "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
fi
2018-02-26 14:50:40 +01:00
{ echo 'server {';
echo " listen 127.0.0.1:$EDITH_ONION_PORT default_server;";
echo " server_name $EDITH_ONION_HOSTNAME;";
echo ''; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check nginx_compress
2018-02-26 14:50:40 +01:00
nginx_compress "$EDITH_DOMAIN_NAME"
echo '' >> "$edith_nginx_site"
2018-03-05 19:15:29 +01:00
function_check nginx_security_options
nginx_security_options "$EDITH_DOMAIN_NAME"
2018-02-26 14:50:40 +01:00
{ echo '';
echo ' access_log /dev/null;';
echo ' error_log /dev/null;';
echo '';
echo " root /var/www/$EDITH_DOMAIN_NAME/htdocs;";
echo '';
echo ' index index.php;';
echo '';
echo ' # PHP';
echo ' location ~ \.php {';
echo ' include snippets/fastcgi-php.conf;';
echo ' fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;';
echo ' fastcgi_read_timeout 30;';
echo ' }';
echo '';
echo ' # Location';
echo ' location / {'; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check nginx_limits
nginx_limits $EDITH_DOMAIN_NAME '15m'
2018-02-26 14:50:40 +01:00
{ echo " try_files \$uri \$uri/ /index.php?\$args;";
echo " auth_basic \"${EDITH_LOGIN_TEXT}\";";
echo ' auth_basic_user_file /etc/nginx/.edithpasswd;';
echo ' }';
echo '}'; } >> "$edith_nginx_site"
2018-02-02 12:46:09 +01:00
function_check configure_php
configure_php
function_check create_site_certificate
2018-02-26 14:50:40 +01:00
create_site_certificate "$EDITH_DOMAIN_NAME" 'yes'
2018-02-02 12:46:09 +01:00
# create a password for users
if [ ! -f /etc/nginx/.edithpasswd ]; then
touch /etc/nginx/.edithpasswd
fi
if grep -q "$MY_USERNAME:" /etc/nginx/.edithpasswd; then
sed -i "/$MY_USERNAME:/d" /etc/nginx/.edithpasswd
fi
2018-02-26 14:50:40 +01:00
echo -n "$EDITH_PASSWORD" | htpasswd -i -s -c /etc/nginx/.edithpasswd "$MY_USERNAME"
2018-02-02 12:46:09 +01:00
if [ ! -f /etc/nginx/.edithpasswd ]; then
echo $'/etc/nginx/.edithpasswd not found'
exit 6537683563
fi
2018-02-26 14:50:40 +01:00
"${PROJECT_NAME}-pass" -u "$MY_USERNAME" -a edith -p "$EDITH_PASSWORD"
2018-02-02 12:46:09 +01:00
2018-02-26 14:50:40 +01:00
cp "/var/www/$EDITH_DOMAIN_NAME/htdocs/htaccess.example" "/var/www/$EDITH_DOMAIN_NAME/htdocs/.htaccess"
cd "/var/www/$EDITH_DOMAIN_NAME/htdocs" || exit 26482468
2018-02-02 12:49:25 +01:00
npm install -g coffeescript uglify-js
2018-02-26 14:50:40 +01:00
if ! cake build; then
2018-02-02 12:46:09 +01:00
echo $'Unable to build Edith'
exit 7396483635
fi
cp config.example.php config.php
2018-02-26 14:50:40 +01:00
if [[ "$ONION_ONLY" == "no" ]]; then
2018-02-02 12:46:09 +01:00
sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'https://$EDITH_DOMAIN_NAME');|g" config.php
else
sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'http://$EDITH_ONION_HOSTNAME');|g" config.php
fi
set_completion_param "edith domain" "$EDITH_DOMAIN_NAME"
2018-02-02 13:27:42 +01:00
set_completion_param "edith onion domain" "$EDITH_ONION_HOSTNAME"
2018-02-03 12:42:00 +01:00
remove_bad_links_edith
2018-02-26 14:50:40 +01:00
chown -R www-data:www-data "/var/www/$EDITH_DOMAIN_NAME/htdocs"
chmod a+w "/var/www/$EDITH_DOMAIN_NAME/htdocs/data"
nginx_ensite "$EDITH_DAEMON_NAME"
2018-02-02 12:46:09 +01:00
systemctl restart nginx
APP_INSTALLED=1
}
function install_interactive_edith {
2018-02-26 14:50:40 +01:00
if [ ! "$ONION_ONLY" ]; then
2018-02-02 12:46:09 +01:00
ONION_ONLY='no'
fi
2018-02-26 14:50:40 +01:00
if [[ "$ONION_ONLY" != "no" ]]; then
2018-03-31 15:00:28 +02:00
EDITH_DOMAIN_NAME='edith.local'
2018-02-02 12:46:09 +01:00
write_config_param "EDITH_DOMAIN_NAME" "$EDITH_DOMAIN_NAME"
else
function_check interactive_site_details
interactive_site_details "edith" "EDITH_DOMAIN_NAME" "EDITH_CODE"
fi
APP_INSTALLED=1
}
# NOTE: deliberately no exit 0