Browse Source

edith notes app

Bob Mottram 7 years ago
parent
commit
c6e9f60c13
1 changed files with 445 additions and 0 deletions
  1. 445
    0
      src/freedombone-app-edith

+ 445
- 0
src/freedombone-app-edith View File

@@ -0,0 +1,445 @@
1
+#!/bin/bash
2
+#
3
+# .---.                  .              .
4
+# |                      |              |
5
+# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
6
+# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
7
+# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
8
+#
9
+#                    Freedom in the Cloud
10
+#
11
+# Edith: an ultra simple notes application
12
+#
13
+# License
14
+# =======
15
+#
16
+# Copyright (C) 2018 Bob Mottram <bob@freedombone.net>
17
+#
18
+# This program is free software: you can redistribute it and/or modify
19
+# it under the terms of the GNU Affero General Public License as published by
20
+# the Free Software Foundation, either version 3 of the License, or
21
+# (at your option) any later version.
22
+#
23
+# This program is distributed in the hope that it will be useful,
24
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
+# GNU Affero General Public License for more details.
27
+#
28
+# You should have received a copy of the GNU Affero General Public License
29
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
30
+
31
+VARIANTS='full full-vim writer'
32
+
33
+IN_DEFAULT_INSTALL=0
34
+SHOW_ON_ABOUT=1
35
+
36
+EDITH_REPO="https://github.com/sunny/edith"
37
+EDITH_COMMIT='59f571e24be9e75d127a0f130591acf8d7a86ce3'
38
+EDITH_DOMAIN_NAME=
39
+EDITH_CODE=
40
+EDITH_ONION_PORT=8278
41
+EDITH_LOGIN_TEXT=$"Edith login"
42
+
43
+edith_variables=(MY_USERNAME
44
+                 MY_EMAIL_ADDRESS
45
+                 ONION_ONLY
46
+                 EDITH_DOMAIN_NAME
47
+                 EDITH_CODE
48
+                 DEFAULT_LANGUAGE)
49
+
50
+function change_password_edith {
51
+    curr_username="$1"
52
+    new_user_password="$2"
53
+
54
+    sed -i "/${curr_username}:/d" /etc/nginx/.edithpasswd
55
+    echo -n "$new_user_password" | htpasswd -i -s -c /etc/nginx/.edithpasswd ${curr_username}
56
+
57
+    ${PROJECT_NAME}-pass -u $MY_USERNAME -a ${curr_username} -p "$new_user_password"
58
+}
59
+
60
+function logging_on_edith {
61
+    echo -n ''
62
+}
63
+
64
+function logging_off_edith {
65
+    echo -n ''
66
+}
67
+
68
+function reconfigure_edith {
69
+    echo -n ''
70
+}
71
+
72
+function edith_enable_login {
73
+    read_config_param EDITH_DOMAIN_NAME
74
+
75
+    dialog --title $"Enable Edith login" \
76
+           --backtitle $"Freedombone Control Panel" \
77
+           --defaultno \
78
+           --yesno $"\nDo you want to add a login so that random web users can't access your notes?" 10 60
79
+    sel=$?
80
+    case $sel in
81
+        0) if grep -q '#auth_basic' /etc/nginx/sites-available/$EDITH_DOMAIN_NAME; then
82
+               sed -i 's|#auth_basic|auth_basic|g' /etc/nginx/sites-available/$EDITH_DOMAIN_NAME
83
+               systemctl restart nginx
84
+           fi
85
+           read_config_param $MY_USERNAME
86
+           EDITH_PASSWORD=$(${PROJECT_NAME}-pass -u $MY_USERNAME -a edith)
87
+           dialog --title $"Enable Edith login" \
88
+                  --msgbox $"Edith logins are now enabled with the password $EDITH_PASSWORD" 6 65
89
+           EDITH__PASSWORD=
90
+           ;;
91
+        1) if ! grep -q '#auth_basic' /etc/nginx/sites-available/$EDITH_DOMAIN_NAME; then
92
+               sed -i 's|auth_basic|#auth_basic|g' /etc/nginx/sites-available/$EDITH_DOMAIN_NAME
93
+               systemctl restart nginx
94
+           fi
95
+           dialog --title $"Disable Edith login" \
96
+                  --msgbox $"Edith logins are now disabled. Anyone can access your stream." 6 65
97
+           ;;
98
+    esac
99
+}
100
+
101
+function edith_browse {
102
+    read_config_param EDITH_DOMAIN_NAME
103
+    cd /var/www/$EDITH_DOMAIN_NAME/htdocs/data
104
+    editor /var/www/$EDITH_DOMAIN_NAME/htdocs/data
105
+}
106
+
107
+function configure_interactive_edith {
108
+    while true
109
+    do
110
+        data=$(tempfile 2>/dev/null)
111
+        trap "rm -f $data" 0 1 2 5 15
112
+        dialog --backtitle $"Freedombone Control Panel" \
113
+               --title $"Edith" \
114
+               --radiolist $"Choose an operation:" 10 50 3 \
115
+               1 $"Enable login" off \
116
+               2 $"Browse notes" off \
117
+               3 $"Exit" on 2> $data
118
+        sel=$?
119
+        case $sel in
120
+            1) break;;
121
+            255) break;;
122
+        esac
123
+        case $(cat $data) in
124
+            1) edith_enable_login;;
125
+            2) edith_browse;;
126
+            3) break;;
127
+        esac
128
+    done
129
+}
130
+
131
+function upgrade_edith {
132
+    CURR_EDITH_COMMIT=$(get_completion_param "edith commit")
133
+    if [[ "$CURR_EDITH_COMMIT" == "$EDITH_COMMIT" ]]; then
134
+        return
135
+    fi
136
+
137
+    read_config_param EDITH_DOMAIN_NAME
138
+
139
+    # update to the next commit
140
+    function_check set_repo_commit
141
+    set_repo_commit /var/www/$EDITH_DOMAIN_NAME/htdocs "edith commit" "$EDITH_COMMIT" $EDITH_REPO
142
+
143
+    chown -R www-data:www-data /var/www/$EDITH_DOMAIN_NAME/htdocs
144
+    chmod a+w /var/www/$EDITH_DOMAIN_NAME/htdocs/data
145
+}
146
+
147
+function backup_local_edith {
148
+    read_config_param EDITH_DOMAIN_NAME
149
+
150
+    function_check suspend_site
151
+    suspend_site ${EDITH_DOMAIN_NAME}
152
+
153
+    source_directory=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
154
+
155
+    function_check backup_directory_to_usb
156
+    dest_directory=edith
157
+    backup_directory_to_usb $source_directory $dest_directory
158
+
159
+    function_check restart_site
160
+    restart_site
161
+}
162
+
163
+function restore_local_edith {
164
+    read_config_param EDITH_DOMAIN_NAME
165
+
166
+    temp_restore_dir=/root/tempedith
167
+    edith_dir=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
168
+
169
+    function_check restore_directory_from_usb
170
+    restore_directory_from_usb $temp_restore_dir edith
171
+    if [ -d $temp_restore_dir ]; then
172
+        if [ -d cp $temp_restore_dir$edith_dir ]; then
173
+            cp -rp $temp_restore_dir$edith_dir $edith_dir/
174
+        else
175
+            if [ ! -d $edith_dir ]; then
176
+                mkdir $edith_dir
177
+                chmod a+w $edith_dir
178
+            fi
179
+            cp -rp $temp_restore_dir/* $edith_dir
180
+        fi
181
+        chown -R www-data:www-data $edith_dir
182
+        rm -rf $temp_restore_dir
183
+    fi
184
+}
185
+
186
+function backup_remote_edith {
187
+    read_config_param EDITH_DOMAIN_NAME
188
+
189
+    function_check suspend_site
190
+    suspend_site ${EDITH_DOMAIN_NAME}
191
+
192
+    source_directory=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
193
+
194
+    function_check backup_directory_to_friend
195
+    dest_directory=edith
196
+    backup_directory_to_friend $source_directory $dest_directory
197
+
198
+    function_check restart_site
199
+    restart_site
200
+}
201
+
202
+function restore_remote_edith {
203
+    read_config_param EDITH_DOMAIN_NAME
204
+
205
+    temp_restore_dir=/root/tempedith
206
+    edith_dir=/var/www/${EDITH_DOMAIN_NAME}/htdocs/data
207
+
208
+    function_check restore_directory_from_friend
209
+    restore_directory_from_friend $temp_restore_dir edith
210
+    if [ -d $temp_restore_dir ]; then
211
+        if [ -d cp $temp_restore_dir$edith_dir ]; then
212
+            cp -rp $temp_restore_dir$edith_dir $edith_dir/
213
+        else
214
+            if [ ! -d $edith_dir ]; then
215
+                mkdir $edith_dir
216
+                chmod a+w $edith_dir
217
+            fi
218
+            cp -rp $temp_restore_dir/* $edith_dir
219
+        fi
220
+        chown -R www-data:www-data $edith_dir
221
+        rm -rf $temp_restore_dir
222
+    fi
223
+}
224
+
225
+function remove_edith {
226
+    nginx_dissite $EDITH_DOMAIN_NAME
227
+
228
+    if [ -f /etc/nginx/sites-available/$EDITH_DOMAIN_NAME ]; then
229
+        rm /etc/nginx/sites-available/$EDITH_DOMAIN_NAME
230
+    fi
231
+
232
+    if [ -d /var/www/$EDITH_DOMAIN_NAME ]; then
233
+        rm -rf /var/www/$EDITH_DOMAIN_NAME
234
+    fi
235
+
236
+    function_check remove_onion_service
237
+    remove_onion_service edith ${EDITH_ONION_PORT}
238
+    sed -i '/edith/d' $COMPLETION_FILE
239
+
240
+    if [ -f /etc/nginx/.edithpasswd ]; then
241
+        rm /etc/nginx/.edithpasswd
242
+    fi
243
+
244
+    function_check remove_nodejs
245
+    remove_nodejs edith
246
+}
247
+
248
+function install_edith {
249
+    apt-get -yq install php-gettext php-curl php-gd php-mysql git curl
250
+    apt-get -yq install memcached php-memcached php-intl exiftool libfcgi0ldbl
251
+
252
+    function_check install_nodejs
253
+    install_nodejs edith
254
+
255
+    if [ ! ${EDITH_PASSWORD} ]; then
256
+        if [ -f ${IMAGE_PASSWORD_FILE} ]; then
257
+            EDITH_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
258
+        else
259
+            EDITH_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
260
+        fi
261
+    fi
262
+
263
+    EDITH_ONION_HOSTNAME=$(add_onion_service edith 80 ${EDITH_ONION_PORT})
264
+
265
+    if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
266
+        mkdir -p /var/www/$EDITH_DOMAIN_NAME/htdocs
267
+    fi
268
+    if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
269
+
270
+        if [ -d /repos/edith ]; then
271
+            mkdir /var/www/$EDITH_DOMAIN_NAME/htdocs
272
+            cp -r -p /repos/edith/. /var/www/$EDITH_DOMAIN_NAME/htdocs
273
+            cd /var/www/$EDITH_DOMAIN_NAME/htdocs
274
+            git pull
275
+        else
276
+            function_check git_clone
277
+            git_clone $EDITH_REPO /var/www/$EDITH_DOMAIN_NAME/htdocs
278
+        fi
279
+
280
+        if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs ]; then
281
+            echo $'Unable to clone edith repo'
282
+            exit 537593569
283
+        fi
284
+    fi
285
+
286
+    cd /var/www/$EDITH_DOMAIN_NAME/htdocs
287
+    git checkout $EDITH_COMMIT -b $EDITH_COMMIT
288
+    set_completion_param "edith commit" "$EDITH_COMMIT"
289
+
290
+    if [ ! -d /var/www/$EDITH_DOMAIN_NAME/htdocs/data ]; then
291
+        mkdir /var/www/$EDITH_DOMAIN_NAME/htdocs/data
292
+    fi
293
+
294
+    edith_nginx_site=/etc/nginx/sites-available/$EDITH_DOMAIN_NAME
295
+    if [[ $ONION_ONLY == "no" ]]; then
296
+        function_check nginx_http_redirect
297
+        nginx_http_redirect $EDITH_DOMAIN_NAME "index index.php"
298
+        echo 'server {' >> $edith_nginx_site
299
+        echo '  listen 443 ssl;' >> $edith_nginx_site
300
+        echo '  listen [::]:443 ssl;' >> $edith_nginx_site
301
+        echo "  server_name $EDITH_DOMAIN_NAME;" >> $edith_nginx_site
302
+        echo '' >> $edith_nginx_site
303
+        function_check nginx_compress
304
+        nginx_compress $EDITH_DOMAIN_NAME
305
+        echo '' >> $edith_nginx_site
306
+        echo '  # Security' >> $edith_nginx_site
307
+        function_check nginx_ssl
308
+        nginx_ssl $EDITH_DOMAIN_NAME
309
+
310
+        function_check nginx_disable_sniffing
311
+        nginx_disable_sniffing $EDITH_DOMAIN_NAME
312
+
313
+        echo '  add_header Strict-Transport-Security max-age=15768000;' >> $edith_nginx_site
314
+        echo '' >> $edith_nginx_site
315
+        echo '  access_log /dev/null;' >> $edith_nginx_site
316
+        echo '  error_log /dev/null;' >> $edith_nginx_site
317
+        echo '' >> $edith_nginx_site
318
+        echo "  root /var/www/$EDITH_DOMAIN_NAME/htdocs;" >> $edith_nginx_site
319
+        echo '' >> $edith_nginx_site
320
+        echo '  index index.php;' >> $edith_nginx_site
321
+        echo '' >> $edith_nginx_site
322
+        echo '  # PHP' >> $edith_nginx_site
323
+        echo '  location ~ \.php {' >> $edith_nginx_site
324
+        echo '    include snippets/fastcgi-php.conf;' >> $edith_nginx_site
325
+        echo '    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $edith_nginx_site
326
+        echo '    fastcgi_read_timeout 30;' >> $edith_nginx_site
327
+        echo '  }' >> $edith_nginx_site
328
+        echo '' >> $edith_nginx_site
329
+        echo '  # Location' >> $edith_nginx_site
330
+        echo '  location / {' >> $edith_nginx_site
331
+        function_check nginx_limits
332
+        nginx_limits $EDITH_DOMAIN_NAME '15m'
333
+        echo '    try_files $uri $uri/ @edith;' >> $edith_nginx_site
334
+        echo "    #auth_basic \"${EDITH_LOGIN_TEXT}\";" >> $edith_nginx_site
335
+        echo '    #auth_basic_user_file /etc/nginx/.edithpasswd;' >> $edith_nginx_site
336
+        echo '  }' >> $edith_nginx_site
337
+        echo '' >> $edith_nginx_site
338
+        echo '  # Restrict access that is unnecessary anyway' >> $edith_nginx_site
339
+        echo '  location ~ /\.(ht|git) {' >> $edith_nginx_site
340
+        echo '    deny all;' >> $edith_nginx_site
341
+        echo '  }' >> $edith_nginx_site
342
+        echo '}' >> $edith_nginx_site
343
+    else
344
+        echo -n '' > $edith_nginx_site
345
+    fi
346
+    echo 'server {' >> $edith_nginx_site
347
+    echo "    listen 127.0.0.1:$EDITH_ONION_PORT default_server;" >> $edith_nginx_site
348
+    echo "    server_name $EDITH_ONION_HOSTNAME;" >> $edith_nginx_site
349
+    echo '' >> $edith_nginx_site
350
+    function_check nginx_compress
351
+    nginx_compress $EDITH_DOMAIN_NAME
352
+    echo '' >> $edith_nginx_site
353
+    function_check nginx_disable_sniffing
354
+    nginx_disable_sniffing $EDITH_DOMAIN_NAME
355
+    echo '' >> $edith_nginx_site
356
+    echo '  access_log /dev/null;' >> $edith_nginx_site
357
+    echo '  error_log /dev/null;' >> $edith_nginx_site
358
+    echo '' >> $edith_nginx_site
359
+    echo "  root /var/www/$EDITH_DOMAIN_NAME/htdocs;" >> $edith_nginx_site
360
+    echo '' >> $edith_nginx_site
361
+    echo '  index index.php;' >> $edith_nginx_site
362
+    echo '' >> $edith_nginx_site
363
+    echo '  # PHP' >> $edith_nginx_site
364
+    echo '  location ~ \.php {' >> $edith_nginx_site
365
+    echo '    include snippets/fastcgi-php.conf;' >> $edith_nginx_site
366
+    echo '    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;' >> $edith_nginx_site
367
+    echo '    fastcgi_read_timeout 30;' >> $edith_nginx_site
368
+    echo '  }' >> $edith_nginx_site
369
+    echo '' >> $edith_nginx_site
370
+    echo '  # Location' >> $edith_nginx_site
371
+    echo '  location / {' >> $edith_nginx_site
372
+    function_check nginx_limits
373
+    nginx_limits $EDITH_DOMAIN_NAME '15m'
374
+    echo '    try_files $uri $uri/ @edith;' >> $edith_nginx_site
375
+    echo "    #auth_basic \"${EDITH_LOGIN_TEXT}\";" >> $edith_nginx_site
376
+    echo '    #auth_basic_user_file /etc/nginx/.edithpasswd;' >> $edith_nginx_site
377
+    echo '  }' >> $edith_nginx_site
378
+    echo '' >> $edith_nginx_site
379
+    echo '  # Restrict access that is unnecessary anyway' >> $edith_nginx_site
380
+    echo '  location ~ /\.(ht|git) {' >> $edith_nginx_site
381
+    echo '    deny all;' >> $edith_nginx_site
382
+    echo '  }' >> $edith_nginx_site
383
+    echo '}' >> $edith_nginx_site
384
+
385
+    function_check configure_php
386
+    configure_php
387
+
388
+    function_check create_site_certificate
389
+    create_site_certificate $EDITH_DOMAIN_NAME 'yes'
390
+
391
+    # create a password for users
392
+    if [ ! -f /etc/nginx/.edithpasswd ]; then
393
+        touch /etc/nginx/.edithpasswd
394
+    fi
395
+    if grep -q "$MY_USERNAME:" /etc/nginx/.edithpasswd; then
396
+        sed -i "/$MY_USERNAME:/d" /etc/nginx/.edithpasswd
397
+    fi
398
+    echo -n "$EDITH_PASSWORD" | htpasswd -i -s -c /etc/nginx/.edithpasswd $MY_USERNAME
399
+    if [ ! -f /etc/nginx/.edithpasswd ]; then
400
+        echo $'/etc/nginx/.edithpasswd not found'
401
+        exit 6537683563
402
+    fi
403
+
404
+    ${PROJECT_NAME}-pass -u $MY_USERNAME -a edith -p "$EDITH_PASSWORD"
405
+
406
+    cp /var/www/$EDITH_DOMAIN_NAME/htdocs/htaccess.example /var/www/$EDITH_DOMAIN_NAME/htdocs/.htaccess
407
+    cd /var/www/$EDITH_DOMAIN_NAME/htdocs
408
+    npm install -g coffee-script uglify-js
409
+    cake build
410
+    if [ ! "$?" = "0" ]; then
411
+        echo $'Unable to build Edith'
412
+        exit 7396483635
413
+    fi
414
+    cp config.example.php config.php
415
+    if [[ $ONION_ONLY == "no" ]]; then
416
+        sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'https://$EDITH_DOMAIN_NAME');|g" config.php
417
+    else
418
+        sed -i "s|define('EDITH_URI'.*|define('EDITH_URI', 'http://$EDITH_ONION_HOSTNAME');|g" config.php
419
+    fi
420
+
421
+    set_completion_param "edith domain" "$EDITH_DOMAIN_NAME"
422
+    chown -R www-data:www-data /var/www/$EDITH_DOMAIN_NAME/htdocs
423
+    chmod a+w /var/www/$EDITH_DOMAIN_NAME/htdocs/data
424
+    nginx_ensite $EDITH_DAEMON_NAME
425
+    systemctl restart nginx
426
+
427
+    APP_INSTALLED=1
428
+}
429
+
430
+function install_interactive_edith {
431
+    if [ ! $ONION_ONLY ]; then
432
+        ONION_ONLY='no'
433
+    fi
434
+
435
+    if [[ $ONION_ONLY != "no" ]]; then
436
+        GHOST_DOMAIN_NAME='edith.local'
437
+        write_config_param "EDITH_DOMAIN_NAME" "$EDITH_DOMAIN_NAME"
438
+    else
439
+        function_check interactive_site_details
440
+        interactive_site_details "edith" "EDITH_DOMAIN_NAME" "EDITH_CODE"
441
+    fi
442
+    APP_INSTALLED=1
443
+}
444
+
445
+# NOTE: deliberately no exit 0