mirror of
https://github.com/BookStackApp/BookStack.git
synced 2026-02-07 19:06:05 +03:00
Access functions of BookStack in static class #4286
Closed
opened 2026-02-05 08:25:41 +03:00 by OVERLORD
·
13 comments
No Branch/Tag Specified
development
l10n_development
further_theme_development
release
llm_only
vectors
v25-11
docker_env
drawio_rendering
user_permissions
ldap_host_failover
svg_image
prosemirror
captcha_example
fix/video-export
v25.12.3
v25.12.2
v25.12.1
v25.12
v25.11.6
v25.11.5
v25.11.4
v24.11.4
v25.11.3
v25.11.2
v25.11.1
v25.11
v25.07.3
v25.07.2
v25.07.1
v25.07
v25.05.2
v25.05.1
v25.05
v25.02.5
v25.02.4
v25.02.3
v25.02.2
v25.02.1
v25.02
v24.12.1
v24.12
v24.10.3
v24.10.2
v24.10.1
v24.10
v24.05.4
v24.05.3
v24.05.2
v24.05.1
v24.05
v24.02.3
v24.02.2
v24.02.1
v24.02
v23.12.3
v23.12.2
v23.12.1
v23.12
v23.10.4
v23.10.3
v23.10.2
v23.10.1
v23.10
v23.08.3
v23.08.2
v23.08.1
v23.08
v23.06.2
v23.06.1
v23.06
v23.05.2
v23.05.1
v23.05
v23.02.3
v23.02.2
v23.02.1
v23.02
v23.01.1
v23.01
v22.11.1
v22.11
v22.10.2
v22.10.1
v22.10
v22.09.1
v22.09
v22.07.3
v22.07.2
v22.07.1
v22.07
v22.06.2
v22.06.1
v22.06
v22.04.2
v22.04.1
v22.04
v22.03.1
v22.03
v22.02.3
v22.02.2
v22.02.1
v22.02
v21.12.5
v21.12.4
v21.12.3
v21.12.2
v21.12.1
v21.12
v21.11.3
v21.11.2
v21.11.1
v21.11
v21.10.3
v21.10.2
v21.10.1
v21.10
v21.08.6
v21.08.5
v21.08.4
v21.08.3
v21.08.2
v21.08.1
v21.08
v21.05.4
v21.05.3
v21.05.2
v21.05.1
v21.05
v21.04.6
v21.04.5
v21.04.4
v21.04.3
v21.04.2
v21.04.1
v21.04
v0.31.8
v0.31.7
v0.31.6
v0.31.5
v0.31.4
v0.31.3
v0.31.2
v0.31.1
v0.31.0
v0.30.7
v0.30.6
v0.30.5
v0.30.4
v0.30.3
v0.30.2
v0.30.1
v0.30.0
v0.29.3
v0.29.2
v0.29.1
v0.29.0
v0.28.3
v0.28.2
v0.28.1
v0.28.0
v0.27.5
v0.27.4
v0.27.3
v0.27.2
v0.27.1
v0.27
v0.26.4
v0.26.3
v0.26.2
v0.26.1
v0.26.0
v0.25.5
v0.25.4
v0.25.3
v0.25.2
v0.25.1
v0.25.0
v0.24.3
v0.24.2
v0.24.1
v0.24.0
v0.23.2
v0.23.1
v0.23.0
v0.22.0
v0.21.0
v0.20.3
v0.20.2
v0.20.1
v0.20.0
v0.19.0
v0.18.5
v0.18.4
v0.18.3
v0.18.2
v0.18.1
v0.18.0
v0.17.4
v0.17.3
v0.17.2
v0.17.1
v0.17.0
v0.16.3
v0.16.2
v0.16.1
v0.16.0
v0.15.3
v0.15.2
v0.15.1
v0.15.0
v0.14.3
v0.14.2
v0.14.1
v0.14.0
v0.13.1
v0.13.0
v0.12.2
v0.12.1
v0.12.0
v0.11.2
v0.11.1
v0.11.0
v0.10.0
v0.9.3
v0.9.2
v0.9.1
v0.9.0
v0.8.2
v0.8.1
v0.8.0
v0.7.6
v0.7.5
v0.7.4
v0.7.3
0.7.2
v.0.7.1
v0.7.0
v0.6.3
v0.6.2
v0.6.1
v0.6.0
v0.5.0
Labels
Clear labels
🎨 Design
📖 Docs Update
🐛 Bug
🐛 Bug
:cat2:🐈 Possible duplicate
💿 Database
☕ Open to discussion
💻 Front-End
🐕 Support
🚪 Authentication
🌍 Translations
🔌 API Task
🏭 Back-End
⛲ Upstream
🔨 Feature Request
🛠️ Enhancement
🛠️ Enhancement
🛠️ Enhancement
❤️ Happy feedback
🔒 Security
🔍 Pending Validation
💆 UX
📝 WYSIWYG Editor
🌔 Out of scope
🔩 API Request
:octocat: Admin/Meta
🖌️ View Customization
❓ Question
🚀 Priority
🛡️ Blocked
🚚 Export System
♿ A11y
🔧 Maintenance
> Markdown Editor
pull-request
Mirrored from GitHub Pull Request
No Label
🐕 Support
Milestone
No items
No Milestone
Projects
Clear projects
No project
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: starred/BookStack#4286
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @ikabod on GitHub (Oct 31, 2023).
Attempted Debugging
Searched GitHub Issues
Describe the Scenario
Hello,
I have a static class in a custom themes folder. This class is called via a xhr request by an index.html file located in the public folder of BookStack.
In this class I can include the app of BookStack like:
self::$app = require __DIR__ . '/../../../bootstrap/app.php';Now I want to access some functions of BookStack, like the hasAppAccess() function in the helpers.php file.
How can I do this?
May be there is an easier way, but please not via the API, because this contains to much steps for the user to setup. My little program shall be installable by a Linux repository and work out of the box after installation without generating Tokens etc.
Thanks,
Peter
Exact BookStack Version
BookStack v23.08.3
Log Content
No response
Hosting Environment
PHP 8.2.11 on a local system (Manjaro) with manual installation.
@ssddanbrown commented on GitHub (Oct 31, 2023):
Hi @ikabod,
To be honest I'm not generally clear on what you're trying to achieve, or what you've already put together.
It sounds like you want to add a custom endpoint, but without it being part of the API (and requiring API key usage).
Our logical theme system supports adding custom endpoints. See the RSS feed hack for an example of this. Video here.
Pretty much all global helpers will be available to you when using the logical theme system
functions.phpfile.Note though, the
hasAppAccess()helper has removed in v23.10, instead usinguser()->hasAppAccess().Some of these methods will require the route/logic to be fired when the user has a session (and is potentially authenticated).
If your route just needs to require login, you could instead do something like this as your
functions.phpfile:Note that if calling from a seperate non-bookstack handled HTML page (even on the same domain) then you might have to consider that your page won't be creating/refreshing user sessions itself, so you might be relying on when the user last accessed BookStack. Things can get more complex if not on the same origin/domain since sessions are cookie based, and you can start dealing with CORS too.
@ikabod commented on GitHub (Oct 31, 2023):
Thank you very much for you detailed answer, Dan and sorry, that I did not explained my problem more clearly, which I try now.
I have a file index.html in the public folder which contains a div and an iFrame. In the iFrame runs BookStack.
After the iFrame was loaded, I pull via a xhr request html data from the server and fill the div (e. g. with buttons).
The html depends on if the user is logged in and/or has administrator privileges:
User calls the root, e. g. https://demo.bookstackapp.com/ which normally loads the index.html and not the index.php file (depends on the web server configuration)
The index.html loads the iFrame (src = index.php = https://demo.bookstackapp.com/index.php)
When the iFrame is loaded, the JavaScript in the index.html file makes a xhr request to the server
The php script on the server wants to "know" if the user, who loaded the page in the iframe, is logged in and if so, if he is an admin
If user is logged in: Return button X, if not return no buttons at all
If logged in user is admin: Return button X and button Y
Write the html which was returned by the php script into the div (innerHTML)
I'm not a php expert and do not know how all things in BookStack work together. My approach now is, to use the events in the functions.php to get the users data, save it into a folder in the custom theme and read it with my php script (see point 4).
For this I need to know which event I can use (I guess I tried all and none worked). The AUTH_LOGIN event sounds good, but does not work if the user activated the "remember me" option on his last login. APP_BOOT is too early. Which one can I use?
And the next problem is, how to access the users data in this event? I guess I have to add
use BookStack\Users\Models\User;in the functions.php but how do I access the users data in the event?It would be great, if you can give me a short code example.
Thanks again,
Peter
@ssddanbrown commented on GitHub (Oct 31, 2023):
Thanks for the extra detail @ikabod. This seems to be quite a complicated setup to approach this.
Can you explain the overall purpose of this
index.htmlpage and the buttons? It might be easier to just build this into the BookStack UI if that doesn't conflict with your purpose of this page.Otherwise, you could instead just expose a JSON endpoint to return the current user and admin status, then call that via a AJAX (fetch) request from your page.
@ikabod commented on GitHub (Oct 31, 2023):
The functionality of the buttons are not related to BookStack, so it makes no sense to integrate it. I can not give more information about this, because I make this for a customer and I do not know if he would agree with it.
Nevertheless, my problem is not related to the buttons, but how to access the classes and functions provided by BookStack.
Do you mean with "expose a JSON endpoint" using the API? If so this is not an option now.
I may now have a workaround for this by using a custom view. In the base.blade.php I added the following code:
In the static class Utils the function looks like:
When I load a page in BookStack I get this error message:
Symfony\Component\ErrorHandler\Error\FatalError
Allowed memory size of 268435456 bytes exhausted (tried to allocate 132128768 bytes)
As you can see the allowed memory size is bigger than the size Symfony tried to allocate. So I have a problem to understand how the things in BookStack work at all.
How can I use the $app object to get information about the user who was loading the page? $app->user is not working.
Thanks,
Peter
@ssddanbrown commented on GitHub (Nov 1, 2023):
Why? you're already getting custom code involved.
It doesn't have to be an endpoint secured with API auth tokens like the others, it can just be a JSON endpoint made available to session auth.
As an example, using the logical theme system setup a
functions.phptheme file with the following:Then go to
<bookstack_url>/custom/auth-statusin the browser afterwards.Note: Custom logic is not officially supported and can break or have unexpected behaviour upon updates.
@ikabod commented on GitHub (Nov 1, 2023):
Yes, this would work, but I need something like:
to fetch this data after any page was loaded, not only the specified url /custom-auth-status.
@ssddanbrown commented on GitHub (Nov 1, 2023):
@ikabod I don't really understand why/how. I wouldn't consider the route "fetching" the data, it's serving it on-demand.
When you need the info on your
index.htmlpage you'd fetch this information via XHR like you originally said you were doing. So something like this within your index.html:@ikabod commented on GitHub (Nov 1, 2023):
Please forget everything Dan, including the index.html file and the iframe.
I only need an example how to get information about the user who loaded any page (not the login page or another certain page).
Or alternative without functions.php:
Code in base.blade.php:
Not yet working code in class Utils:
How can I use the $app var to get information about the user?
Thanks,
Peter
@ssddanbrown commented on GitHub (Nov 1, 2023):
functions.php:
@ikabod commented on GitHub (Nov 2, 2023):
Thank you Dan, this works fine.
I guess I could not figure this out, because I thought I can only use the documented params for an event as an argument:
How do I know, that I can use the user object in an event too? And what else objects can I use inside an event? Where can I find documentation about this?
Thanks again,
Peter
@ssddanbrown commented on GitHub (Nov 2, 2023):
The theme functions file runs as part of the app environment, so you have access to all the common helpers, classes and methods as app code.
There is no documentation about specifically what's available inside events, since it's everything available to the app itself, and it's all really considered non-supported (with potential to change).
It helps to have Laravel experience, and to use existing app code as a reference.
Additionally, one of the purposes of the hacks site is to provide examples of what can be done via various means.
@ikabod commented on GitHub (Nov 3, 2023):
Ok, thank you Dan.
@ssddanbrown commented on GitHub (Nov 3, 2023):
Happy to help.
Will therefore close this off.