vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   How to return error phrase in attachdata_presave hook? (https://vborg.vbsupport.ru/showthread.php?t=278105)

JamesAB 02-05-2012 11:13 PM

How to return error phrase in attachdata_presave hook?
 
I'm trying to block certain attachments from being posted. When I make use of returning "false" in the attachdata_presave hook I can successfully stop the file from being saved in VB. However, when I try to customize the error message shown in the browser to the end-user, it only works when I am logged in as "admin". Registered users still see the standard error message.

Here's what I have in attachdata_presave
Code:

$bannedmatches = $this->dbobject->query_first("
        SELECT COUNT(*) AS count
        FROM " . TABLE_PREFIX . "banned_attachment
        WHERE filehash = '" . $this->fetch_field('filehash') . "'
");

if ($bannedmatches['count'] > 0)
{
    $return_value = false;
    $this->error('jb_file_not_allowed_here');
}

When I'm logged in as admin and try to upload a banned attachment I see my custom error phrase in the response page.
Quote:

hippos.jpg:
This file is not allowed to be uploaded on our website.
When I'm logged in as a registered user and try to upload the same attachment, I see the standard error phrase.
Quote:

hippos.jpg:
Upload of file failed.
What am I missing? What would be the best way to set the error text here to use my own custom phrase?

Thanks for your help,
James

Simon Lloyd 02-06-2012 12:32 AM

Why would you go to all that trouble?, just don't allow the file type in the attachments manager?

That said you can display an error message like this example, if you create a custom hook (custom_template_permissions)
HTML Code:

if (is_member_of($vbulletin->userinfo, 1))
{
        print_no_ permission_ custom_ template();
        exit();
}
if (is_member_of($vbulletin->userinfo, 8,9,10))
{
        print_no_ permission();
}

In fact the easiest would probably be to edit the newattachment_errormessage template, remove the $errormessage and put your own text in there and/or conditions :)

These should help too :)
HOOKS CALLED
init_startup
fetch_userinfo_query
fetch_musername
fetch_userinfo
cache_permissions
fetch_foruminfo
style_fetch
cache_templates
global_start
parse_templates
notices_check_start
notifications_list
global_setup_complete
newattachment_start
attachdata_start
upload_accept
error_fetch
newattachment_attach
editor_wysiwyg_compatible
newattachment_complete


TEMPLATES CALLED
newattachment
newattachment_errormessage
newattachment_keybit


PHRASE GROUPS CALLED
global
posting
prefix

JamesAB 02-06-2012 01:08 AM

Simon,
Thanks for your help. I'm not trying to block certain types of attachments. I'm trying to block specific attachments by comparing the filehash of the file that the member uploaded to a list filehash values I'll maintain of "banned" files/attachments.

I'm still trying to figure out why my error phrase is working correctly for the admin usergroup, but registered users are seeing the VB default phrase. I double-checked and both phrases are the same type.

(Stock VB phrase)
Front-End Error Messages
upload_file_failed
"Upload of file failed."

(My custom phrase)
Front-End Error Messages
jb_file_not_allowed_here
"This file is not allowed to be uploaded on our website."

It's a mystery to me why different usergroups are seeing different phrases.

Thanks,
James

Simon Lloyd 02-06-2012 01:11 AM

Do you have your error phrase in the error phrases or in global or.....etc?

JamesAB 02-06-2012 01:18 AM

I put my custom phrase in "Front-End Error Messages".

Thanks,
James

kh99 02-06-2012 01:23 AM

Quote:

Originally Posted by JamesAB (Post 2296627)
It's a mystery to me why different usergroups are seeing different phrases.

Are you sure it's getting to that point for registered users? Maybe it's a different error.

JamesAB 02-06-2012 01:38 AM

Yes. I know the code in the hook is being executed because the line above the line that sets the error is being executed:
Code:

$return_value = false;
This is stopping the attachment from being saved and is working as expected for both admins and registered users.

kh99 02-06-2012 01:40 AM

So if you disable your plugin and upload the file as a registered user, it works?

JamesAB 02-06-2012 01:52 AM

Quote:

Originally Posted by kh99 (Post 2296639)
So if you disable your plugin and upload the file as a registered user, it works?

Yes. When I disable the plugin, "Manage Attachments" works as expected. All usergroups can upload the same files with no limitations or error messages.

--------------- Added [DATE]1328496982[/DATE] at [TIME]1328496982[/TIME] ---------------

Maybe something weird is happening with the vB_Upload_Attachment datamanager?

Here is where newattachment.php is getting the error text from:
Code:

                                if ($error = $upload->fetch_error())
                                {
                                        $errors[] = array(
                                                'filename' => is_array($attachment) ? $attachment['name'] : $attachment,
                                                'error'    => $error,
                                        );
                                }


kh99 02-06-2012 02:03 AM

Quote:

Originally Posted by JamesAB (Post 2296642)
Yes. When I disable the plugin, "Manage Attachments" works as expected. All usergroups can upload the same files with no limitations or error messages.

OK, just wanted to make sure. I tried your code (well, I really just set the return value and error message) and I do get the same problem.

JamesAB 02-06-2012 02:16 AM

kh99,
I'm glad you could duplicate the issue.

My gut feeling is that it is in issue with using the data manager, but I'm open to any suggestions.

kh99 02-06-2012 02:59 AM

Yeah, it's caused by this code in class_upload.php (around line 957):

Code:

if (!($result = $this->data->save()))
{
        if (empty($this->data->errors[0]) OR !($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel']))
        {
                $this->set_error('upload_file_failed');
        }
        else
        {
                $this->error =& $this->data->errors[0];
        }
}


For some reason if you're not an admin it doesn't use the errors from the attachment object.

JamesAB 02-06-2012 03:12 AM

Thanks so much for finding that. It makes sense now why it it is a usergroup problem. I'm guessing the VB developers didn't want to show more descriptive error messages to anyone except admin users.

Now I just need to find the most sensible workaround to show other usergroups my phrase in this scenario.

Thanks again,
James

kh99 02-06-2012 03:24 AM

I knew that wasn't really going to be a lot of help in fixing the problem. :) Anyway, I was thinking maybe upload_accept, although i'm not sure if the file data is in a variable at that point.

JamesAB 02-06-2012 04:03 AM

I'm looking at upload_accept, but it doesn't look like the filehash has been created yet and I don't think I have the file data yet to create one myself. I'll keep digging.

Thanks,
James

--------------- Added [DATE]1328546156[/DATE] at [TIME]1328546156[/TIME] ---------------

Quote:

Originally Posted by kh99 (Post 2296655)
Yeah, it's caused by this code in class_upload.php (around line 957):

Code:

if (!($result = $this->data->save()))
{
        if (empty($this->data->errors[0]) OR !($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel']))
        {
                $this->set_error('upload_file_failed');
        }
        else
        {
                $this->error =& $this->data->errors[0];
        }
}


For some reason if you're not an admin it doesn't use the errors from the attachment object.

I hate editing more standard VB files for mods, but I think a quick change in class_upload.php might be the best fix.

This works:
Code:

                if (!($result = $this->data->save()))
                {
                        echo "<script>alert('class_upload error ". $this->data->errors[0] ."')</script>";
                        if (empty($this->data->errors[0]) OR !($this->registry->userinfo['permissions']['adminpermissions'] & $this->registry->bf_ugp_adminpermissions['cancontrolpanel']) AND ($this->data->errors[0] != "This file is not allowed to be uploaded on our website."))
                        {
                                $this->set_error('upload_file_failed');
                        }
                        else
                        {
                                $this->error =& $this->data->errors[0];
                        }
                }

However, I know I shouldn't use the string literal here. How can I get the text of the phrase jb_file_not_allowed_here to use in the comparison here in class_upload.php ?

Thanks,
James


All times are GMT. The time now is 01:31 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01124 seconds
  • Memory Usage 1,768KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (6)bbcode_code_printable
  • (1)bbcode_html_printable
  • (6)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (15)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete