Random header image on we_universal [RESOLVED]

For support and discussion related to templates and themes in phpBB 3.3.
Post Reply
VinBR
Registered User
Posts: 8
Joined: Mon Jul 01, 2019 3:09 am
Location: Brazil

Random header image on we_universal [RESOLVED]

Post by VinBR »

Hello,

I'm currently using some functions to make a random header image with we_universal, and it's actually working, but on the "Password Recovery" page, this happens:

Image

Errors:

Code: Select all

[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4136: scandir(./../styles/we_universal/theme/headers/): failed to open dir: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4136: scandir(): (errno 2): No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/includes/functions.php on line 4136: array_diff(): Expected parameter 1 to be an array, bool given
Error line:

Code: Select all

$inventea_images_list = array_diff(scandir($inventea_images_path), array('.', '..'));
At the other pages this function works normally, but at the app.php/x page it doesn't, I have no clue on how to fix this, could anyone help me?
Last edited by VinBR on Thu Jul 30, 2020 5:51 pm, edited 1 time in total.
User avatar
pit-PL
Registered User
Posts: 3097
Joined: Sat Nov 21, 2009 12:24 pm

Re: Random header image on we_universal

Post by pit-PL »

VinBR
Registered User
Posts: 8
Joined: Mon Jul 01, 2019 3:09 am
Location: Brazil

Re: Random header image on we_universal

Post by VinBR »

Didn't work for me. Stills with the error on the password reset page, but in the other pages it is completely normal.
User avatar
HiFiKabin
Community Team Member
Community Team Member
Posts: 5024
Joined: Wed May 14, 2014 9:10 am
Location: Swearing at the PC, UK
Name: James
Contact:

Re: Random header image on we_universal

Post by HiFiKabin »

viewtopic.php?f=456&t=2470586

It will need adapting for we_universal but if you like hoe it works let me know and I'll make a style pack for it
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Random header image on we_universal

Post by EA117 »

VinBR wrote:
Wed Jul 29, 2020 4:53 pm
Didn't work for me. Stills with the error on the password reset page, but in the other pages it is completely normal.
Does it work for any page which is just a logical route and not "a file that exists"? Meaning does it work for any of the other app.php-dependent pages besides site.com/user/forgot_password, like site.com/help/faq? Or is it failing for all of them, and "reset password" was just the first one noticed.

As opposed to pages with URLs for files which actually exist, like site.com/viewtopic.php, site.com/memberlist.php, etc., where it seems to be working.

The extension from HiFi sounds like a good option. If I was trying to fix the we_universal image rotation code, I would probably just make the web root a hard-coded assumption and see how well that worked. e.g. Make the added line look like:

Code: Select all

// Path to directory with header images (must contain trailing slash)
$inventea_images_path = "./styles/" . rawurlencode($user->theme['style_path']) . '/theme/headers/';
Meaning, stop referencing the $web_path value which has been calculated, which is ./ when the pages work, and is ./../ when the pages fail. I don't know why it's failing like that; I thought "because routes can falsely make it appear as though there are additional folder levels to traverse" is exactly what phpBB already intended to take into account in the way $web_path was calculated.

But anyway, I'd try replacing that calculation in $inventea_images_path with just the hard-coded ./ as shown, and see whether any failure cases remain. Just in case, after making the change use the phpBB ACP Purge Cache option too, to be sure there aren't any document sections still in cache which were built while using the previous code.
VinBR
Registered User
Posts: 8
Joined: Mon Jul 01, 2019 3:09 am
Location: Brazil

Re: Random header image on we_universal

Post by VinBR »

EA117 wrote:
Wed Jul 29, 2020 10:29 pm
VinBR wrote:
Wed Jul 29, 2020 4:53 pm
Didn't work for me. Stills with the error on the password reset page, but in the other pages it is completely normal.
Does it work for any page which is just a logical route and not "a file that exists"? Meaning does it work for any of the other app.php-dependent pages besides site.com/user/forgot_password, like site.com/help/faq? Or is it failing for all of them, and "reset password" was just the first one noticed.

As opposed to pages with URLs for files which actually exist, like site.com/viewtopic.php, site.com/memberlist.php, etc., where it seems to be working.

The extension from HiFi sounds like a good option. If I was trying to fix the we_universal image rotation code, I would probably just make the web root a hard-coded assumption and see how well that worked. e.g. Make the added line look like:

Code: Select all

// Path to directory with header images (must contain trailing slash)
$inventea_images_path = "./styles/" . rawurlencode($user->theme['style_path']) . '/theme/headers/';
Meaning, stop referencing the $web_path value which has been calculated, which is ./ when the pages work, and is ./../ when the pages fail. I don't know why it's failing like that; I thought "because routes can falsely make it appear as though there are additional folder levels to traverse" is exactly what phpBB already intended to take into account in the way $web_path was calculated.

But anyway, I'd try replacing that calculation in $inventea_images_path with just the hard-coded ./ as shown, and see whether any failure cases remain. Just in case, after making the change use the phpBB ACP Purge Cache option too, to be sure there aren't any document sections still in cache which were built while using the previous code.
Using $inventea_images_path = "./styles/" . 'we_universal' . '/theme/headers/'; the errors disappeared, but now it just shows a blank header in those pages. Any app.php pages does that error, including faq and forgot_password.
In the index page, viewtopic, ucp, viewforum and etc the headers shows normally.

Looking into the source-code of the page now, this HTML was generated to the faq page header:

Code: Select all

<div class="inventea-dashboard" role="banner" style="background-image: url(./styles/we_universal/theme/headers/banner4.png);">
Which displayed a blank header. I added one more . in the beginning of the URL in the developer console and it worked, but how to fix that in the functions.php? It's not displaying the errors anymore.
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Random header image on we_universal

Post by EA117 »

VinBR wrote:
Thu Jul 30, 2020 12:38 am
Looking into the source-code of the page now, this HTML was generated to the faq page header:
I think that makes sense now, and points to something I wasn't comprehending before. Essentially, the scandir() is going to be performed relative to app.php, and needs to use ./ in order to correctly find and list the image files. But from the web browser's perspective in the rendered HTML document, the URL is still site.com/help/faq or site.com/user/forgot_password, which contains additional "directory levels" beyond just root.

So the rendered HTML document needs ./../ in order to reference the same image path that the executing PHP code found using just ./.

Based on that, the cleaner change would be to make the first two lines look like this:

Code: Select all

// Path to directory with header images (must contain trailing slash)
$inventea_images_path = "styles/" . rawurlencode($user->style['style_path']) . '/theme/headers/';

// Get list of images in directory
$inventea_images_list = array_diff(scandir('./' . $inventea_images_path), array('.', '..'));
And then also change this line which had been added:

Code: Select all

'INVENTIA_RANDOM_IMAGE' => (isset($inventea_random_image)) ? $web_path . $inventea_images_path . $inventea_random_image : '',
Such that when scandir() it called we will be pre-pending the hard-coded ./, but when it comes time to render the HTML content it will pre-pend the calculated $web_path instead.


edit: I still had the original $user->theme['theme_path'] in this example instead of the $user->style['style_path'] correction pit-PL had pointed to.
Last edited by EA117 on Thu Jul 30, 2020 8:57 am, edited 2 times in total.
User avatar
pit-PL
Registered User
Posts: 3097
Joined: Sat Nov 21, 2009 12:24 pm

Re: Random header image on we_universal

Post by pit-PL »

Try changing "{$web_path}styles/" to "{$root_path}styles/".
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Random header image on we_universal

Post by EA117 »

pit-PL wrote:
Thu Jul 30, 2020 6:13 am
Try changing "{$web_path}styles/" to "{$root_path}styles/".
Presumably you mean $phpbb_root_path since $root_path isn't defined in this context. And agreed that this could be a substitute for the hard-coded ./, and should contain the same ./ value in this context.

But I still don't think the logic works for this case when having "a single value expected to be used in both the scandir() path and the INVENTIA_RANDOM_IMAGE path."

When the web browser believes we've loaded an HTML document from /help/faq or from /user/forgot_password, then for the INVENTIA_RANDOM_IMAGE path we needed ./../ in order to reference the styles folder in the root. But the path provided to scandir() still needs only ./ to find the styles folder in this case, because the executing PHP code is not actually inside a literal /help/ or /user/ folder.

Happy to be shown why this is still wrong; but just explaining why it seems like "a single value to be used in both cases" cannot suffice here.
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Random header image on we_universal

Post by EA117 »

pit-PL was on-point in recommending $phpbb_root_path. The hard-coded ./ might have worked for most testing, but would have failed once we talk about /adm/index.php (a PHP file actually executing from a sub-folder) and any extension which might have introduced a similar action. Therefore the suggested code modifications are currently:

Code: Select all

// Path to directory with header images (must contain trailing slash)
$inventea_images_path = "styles/" . rawurlencode($user->style['style_path']) . '/theme/headers/';

// Get list of images in directory
$inventea_images_list = array_diff(scandir($phpbb_root_path . $inventea_images_path), array('.', '..'));
And then also change this line which had been added:

Code: Select all

'INVENTIA_RANDOM_IMAGE' => (isset($inventea_random_image)) ? $web_path . $inventea_images_path . $inventea_random_image : '',
Such that when scandir() it called we will be pre-pending whatever $phpbb_root_path value has been calculated. (Which may or may not be simply ./.) But when it comes time to render the HTML content it will pre-pend the calculated $web_path instead. (Which is not necessarily the same as the $phpbb_root_path value.)
VinBR
Registered User
Posts: 8
Joined: Mon Jul 01, 2019 3:09 am
Location: Brazil

Re: Random header image on we_universal

Post by VinBR »

EA117 wrote:
Thu Jul 30, 2020 4:58 pm
pit-PL was on-point in recommending $phpbb_root_path. The hard-coded ./ might have worked for most testing, but would have failed once we talk about /adm/index.php (a PHP file actually executing from a sub-folder) and any extension which might have introduced a similar action. Therefore the suggested code modifications are currently:

Code: Select all

// Path to directory with header images (must contain trailing slash)
$inventea_images_path = "styles/" . rawurlencode($user->style['style_path']) . '/theme/headers/';

// Get list of images in directory
$inventea_images_list = array_diff(scandir($phpbb_root_path . $inventea_images_path), array('.', '..'));
And then also change this line which had been added:

Code: Select all

'INVENTIA_RANDOM_IMAGE' => (isset($inventea_random_image)) ? $web_path . $inventea_images_path . $inventea_random_image : '',
Such that when scandir() it called we will be pre-pending whatever $phpbb_root_path value has been calculated. (Which may or may not be simply ./.) But when it comes time to render the HTML content it will pre-pend the calculated $web_path instead. (Which is not necessarily the same as the $phpbb_root_path value.)
Nice, thank you so much for the help, that worked and fixed all the problems.
User avatar
EA117
Registered User
Posts: 1858
Joined: Wed Aug 15, 2018 3:23 am
Contact:

Re: Random header image on we_universal

Post by EA117 »

VinBR wrote:
Thu Jul 30, 2020 5:28 pm
Nice, thank you so much for the help, that worked and fixed all the problems.
That's great, glad it's working for you. I went ahead and put the conclusion here into a post in the we_universal support area, so that hopefully what we've done here can be easily found by someone else.
Post Reply

Return to “[3.3.x] Styles Support & Discussion”