#!/usr/bin/env bash
# Diagnoses document uploads. Run from the server: bash CHECK.sh
CS=/var/www/html/cs
echo "== 1. is the documents route registered? =="
grep -c "routes/documents.php" $CS/api/index.php   # must be 1
echo "== 2. does the table exist? =="
mysql helpdesk -e "SHOW TABLES LIKE 'documents'" 2>&1 | tail -1
mysql helpdesk -e "SELECT setting_value FROM settings WHERE setting_key='allowed_doc_types'" 2>&1 | tail -1
echo "== 3. PHP-FPM upload limits (NOT the CLI ones) =="
grep -rh "upload_max_filesize\|post_max_size" /etc/php/*/fpm/conf.d/ /etc/php/*/fpm/php.ini 2>/dev/null | grep -v "^;"
echo "== 4. is the upload directory writable by the web server? =="
UP=$(grep upload_dir $CS/api/config/config.php | sed "s/.*=> *'//; s/'.*//")
echo "upload_dir = $UP"
ls -ld "$UP" 2>&1
sudo -u www-data test -w "$UP" && echo "WRITABLE by www-data" || echo "NOT WRITABLE by www-data"
echo "== 5. last errors =="
tail -15 /var/log/apache2/cs-error.log | grep helpdesk
