Kareha Rotating Banner Help

Posted by mauser 
This forum is currently read only. You can not log in or make any changes. This is a temporary situation.
Now, this forum is in read-only mode. You find details Details hereContinue on /r/PirateBox
Kareha Rotating Banner Help
August 21, 2018 09:45PM
Hello,
I am working on making a custom kareha message board and thus far has been going smoothly.
The only function that I am having trouble getting to work, is the "Rotating Banner Image" function.
In /share/board/config.pl I have the TITLEIMG value set to 2 for "rotating" with the next TITLEIMG entry stating the path of my banner rotate script: /share/board/banner_rotate.sh.
My banner rotate shell script outputs a random banner image path like: /content/img/random_banner_img.jpg and works correctly when executed in the shell.
I am assuming the config.pl is asking for a path to the banner image just like as if you were to define a static banner image when the TITLEIMG value is set to 1.

No matter what name, path, config combinations I have tried so far, the banner image is not loading.
If I set the value to 1 and name a static banner image, the image loads properly.
I access the admin.pl and rebuild the cache with the GUI to view changes. I ensure my browser's cache is clear as well.
The banner_rotate.sh script is +x executable.
I have checked the kareha support threads to no avail. I have tried one other perl script found on the support forum, but no luck. Unfortunately, those threads are no longer active.

As a last resort, I am posting here for either an answer, or a heading.
I would appreciate it very much if anyone here could help me out.
Thank You
Re: Kareha Rotating Banner Help
August 22, 2018 06:28PM
Ah, it is not that easy to exchange the banner image that easy.
The reason is, that the imageboard is rendered as static HTML on disk until something changes on the board. Only with that regeneration a changed configuration will be put in place.

I can only think of a shell script, which works under the control of cron (every x Minutes) and replaces the banner image physically.

Matthias
Re: Kareha Rotating Banner Help
August 22, 2018 09:03PM
I see. That may explain why nothing was working at all.
Anyway, even if I did set a cronjob task to edit the TITLEIMG line in share/board/config.pl, wouldn't it require an administrator to use the "Rebuild Caches" option to enact a change on the board?
Or would it be possible to make the cache rebuild every x minutes through the use of cron as well?
Or are the config.pl changes reflected on the board no matter what changes on the board itself? (i.e. a new post or thread is made)

Thank You
Re: Kareha Rotating Banner Help
August 23, 2018 01:47AM
UPDATE
Thank You Matthias for giving me a direction to pursue. I've figured it out.
Kind of hackish, but the script works.
I'll provide a general walkthrough to help anyone else out who may experience this problem in the future:

In share/board/config.pl set SHOWTITLEIMG to "1". (Yes this is the non-rotating option)

Then, in share/board/ create a text file named banner_rotate.sh
Using a text editor, copy the following shell script to create banner_rotate.sh:

#!/bin/sh

files=(/opt/piratebox/share/content/img/*)
new_image=$(printf "%s\n" "${files[RANDOM % ${#files[@]}]}" | cut -c 21-)

#this section required to add escape characters to the variable $new_image
NL='
'
case $new_image in
  (*"$NL"*)
    echo >&2 "Sorry, can't handle variables with newline characters"
    exit 1
esac

escaped_new_image=$(printf '%s\n' "$new_image" | sed 's:[][\/.^$*]:\\&:g')

# base template for sed command: Ignore this if you want
#sed "0,/$escaped_var/s/$escaped_var/replacement/" < file

sed -i '17s/.*/use constant TITLEIMG => '\'"$escaped_new_image"\'';/' /opt/piratebox/share/board/config.pl

In short, banner_rotate.sh works like this:
Find and define banner image directory (here I used /opt/piratebox/share/content/img/ )
Select a random file within the directory and find the full path of the selected random banner image.
Use "cut" to omit the first 21 characters of the image's full path; resulting in /content/img/random_img.jpg
The next 'case' section of the script prevents the $new_image variable to be accepted if it has newline characters.
The $escaped_new_image line adds escape characters to $new_image to be prepared for the final 'sed' command.
The final 'sed' line replaces line 17 of config.pl in it's entirety with the new random image path found earlier.

Next;
Make banner_rotate.sh +x executable.
In the shell, test the script using:
sudo ./banner_rotate.sh
You should not receive any errors. Check config.pl to see if the script has worked and indeed did define a path to a different image on the TITLEIMG line.
Using 'crontab -e', schedule banner_rotate.sh to run however often you want. (example, every 1 minute)
Make sure to give the full path of the shell script in crontab.
Ensure the script is executed as root with sudo (crontab is supposed to run as root by default, but I have run into issues on other projects. It's better to just write sudo.)

Now, when two conditions are met, the banner image will change. The two conditions are:
When crontab executes the banner_rotate.sh script, and;
When any board user makes a change (thread, post, etc.) or navigates to a different part of the board.

*Important notes*
-The line number on which TITLEIMG resides is critical to the success of the rotate script. Make sure you get the line number correct, and that you don't accidentally move it. The banner script will overwrite everything on the line it is directed to. It is recommended to have a backup copy of your config.pl in case this happens, so you can restore config.pl to it's proper form.
-The escape characters and quotations etc. in the script are critical and specific. Do not change this. Just use copy/paste to create the script.
-This was created/tested on a piratebox build on a Raspberry Pi 3 Model B.
-The standard banner image size seen on popular imageboards are typically 300 x 100.

So there you have it.
Maybe not a true 'rotate' script.
Maybe not very robust of a solution, and easy to break due to the admin's carelessness, but you know.



Edited 1 time(s). Last edit at 08/23/2018 01:50AM by mauser.