]> git.plutz.net Git - shellwiki/blobdiff - themes/default.sh
delete buttons in attachment list (non-functional)
[shellwiki] / themes / default.sh
index c096f8218a03639d6b24352c4ed4228df515993d..7edd4b9986c60ad8e989f07a2740c7dc327d7c48 100755 (executable)
@@ -1,9 +1,12 @@
 #!/bin/sh
 
+. "$_EXEC/tools.sh"
+
 theme_head(){
   printf '
   <meta name="viewport" content="width=device-width"/>
   <link rel="stylesheet" type="text/css" href="%s/[.]/cgilite/common.css">
+  <link rel="stylesheet" type="text/css" href="%s/[.]/themes/default.css">
   ' "$_BASE"
 }
 
@@ -19,7 +22,15 @@ theme_page(){
   local page="$1" title
   title="${page%/}"; title="${title##*/}"
 
-  # Important! Web Server response including newline newline
+  if [ ! "$(mdfile "$page")" ]; then
+    theme_404
+    return 0
+  elif ! acl_read "$page"; then
+    theme_403
+    return 0
+  fi
+
+  # Important! Web Server response including newline
   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
 
   cat <<-EOF
@@ -29,7 +40,17 @@ theme_page(){
          <title>$(HTML "${title}")</title>
        </head><body id="$(HTML "$page")">
          $(theme_header)
-         <main>$(wiki "$page" || printf 'Page not found')</main>
+         <main>
+           $(acl_write "$page" && printf %s \
+             '<ul class="pagemenu">
+                <li><a href="[edit]">Edit</a></li>
+                <li><a href="[attachment]/">Attachments</a></li>
+              </ul>'
+           )
+           <article>
+             $(wiki "$page" || printf 'Error while loading page <br> function "wiki" of index.sh returned with an error.')
+           </article>
+         </main>
          $(theme_footer)
        </body></html>
        EOF
@@ -39,6 +60,14 @@ theme_editor(){
   local page="$1" title
   title="${page%/}"; title="${title##*/}"
 
+  if [ ! "$(mdfile "$page")" ]; then
+    theme_404
+    return 0
+  elif ! acl_write "$page"; then
+    theme_403
+    return 0
+  fi
+
   # Important! Web Server response including newline
   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
 
@@ -60,16 +89,104 @@ theme_editor(){
        EOF
 }
 
+theme_attachments(){
+  local page="$1" title
+  title="${page%/}"; title="${title##*/}"
+
+  if [ ! "$(mdfile "$page")" ]; then
+    theme_404
+    return 0
+  elif ! acl_read "$page"; then
+    theme_403
+    return 0
+  fi
+
+  # Important! Web Server response including newline
+  printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
+
+  if acl_write "$page"; then
+    cat <<-EOF
+       <!DOCTYPE HTML>
+       <html><head>
+         $(theme_head)
+         <title>Attachments $(HTML "${title}")</title>
+       </head><body id="$(HTML "$page")[attachment]/">
+         $(theme_header)
+         <main>
+           <form class=upload method=POST enctype="multipart/form-data">
+             <input type=file name=file multiple>
+             <button type=submit name=action value=upload>Upload</button>
+           </form>
+
+            <form method=POST><ul class="attachment list">
+           $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
+             [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
+                   -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
+             stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+             size="${stat% *}" date="${stat#* }"
+           
+             printf '<li><button type=submit name=delete value="%s">Delete</button><a class=name href="%s">%s</a>
+                     <span class=size>%s</span><span class=date>%s</span></li>' \
+               "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" \
+               "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+           done)
+            </ul></form>
+         </main>
+         $(theme_footer)
+       </body></html>
+       EOF
+  else
+    cat <<-EOF
+       <!DOCTYPE HTML>
+       <html><head>
+         $(theme_head)
+         <title>Attachments $(HTML "${title}")</title>
+       </head><body id="$(HTML "$page")[attachment]/">
+         $(theme_header)
+         <main>
+            <ul class="attachment list">
+           $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
+             [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
+                   -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
+             stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+             size="${stat% *}" date="${stat#* }"
+           
+             printf '<li><a class=name href="%s">%s</a>
+                     <span class=size>%s</span><span class=date>%s</span></li>' \
+               "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+           done)
+            </ul>
+         </main>
+         $(theme_footer)
+       </body></html>
+       EOF
+  fi
+}
+
 theme_login(){
-  theme_page '[wiki]/login/'
+  theme_page '/[wiki]/login/'
 }
 
 theme_register(){
-  theme_page '[wiki]/register/'
+  theme_page '/[wiki]/register/'
+}
+
+theme_403(){
+  printf "%s\r\n" "Status: 403 Forbidden"
+
+  if [ "$(mdfile '/[wiki]/403/')" ]; then
+    theme_page '/[wiki]/403/'
+  else
+    printf "Content-Length: 0\r\n\r\n"
+  fi
 }
 
 theme_404(){
   printf "%s\r\n" "Status: 404 Not Found"
 
-  theme_page '[wiki]/404/'
+  if [ "$(mdfile '/[wiki]/404/')" ]; then
+    theme_page '/[wiki]/404/'
+  else
+    printf "Content-Length: 0\r\n\r\n"
+  fi
 }