HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Post Validation - Stop Long URLs from Body Text?

iPhone display of forum content has issues if a user posts a long URL into post. I am wondering if there is an option or ability to detect URLs which are over a certain length and reject the post with some error text.

Example of a problem URL: http://www.llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch.com/
Error message: "Your post has a long URL. Please use the chain link icon to post the URL correctly or try a URL Shortener solution."

Example of a way to post this URL correctly: Click Here

«1

Comments

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    If you are using the editor as in this forum, then you need to

    Please use the chain link icon to post the URL correctly or try a URL Shortener solution."

    When you click on the editor icon that has a chain link icon, you get a drop down form where you are supposed to enter the link. It is then formatted to work.

    A URL Shortner is a service that converts what the url looks like into a short Url and they keep track of the original links so they go where they point to.

    this is one of the services I mention

    http://tinyurl.com

    you can also make a widget and add it to your forum so people can use it without having to go to another site

    <form action="http://tinyurl.com/create.php" method="post" target="_blank">
    <table align="center" cellpadding="5" bgcolor="#E7E7F7"><tr><td>
    <b>Enter a long URL to make <a href="http://tinyurl.com">tiny</a>:</b><br />
    <input type="text" name="url" size="30"><input type="submit" name="submit" value="Make TinyURL!">
    </td></tr></table>
    </form>
    
  • Thanks for that, @vrijvlinder. However, I am looking for a solution that would not allow the user to "Post Comment" with a long URL present. So, the solution would have to involve some elements:
    1) the ability to recognize a typed URL
    2) the ability to determine the length of a typed URL
    3) the ability for the forum admin to specify a max length
    4) the ability for the forum to block the Post Comment if the typed URL exceeds the max length
    5) the ability for the forum to display a warning to the user as to why the post was blocked and how to fix it
    6) the ability for the user's post text to be retained in draft mode so that they can make edits to comply with the warning

    Make sense?

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited August 2016

    Only for the iPhone ? or for everything ?

    1) the ability to recognize a typed URL

    that already happens on post and it is the browser that recognizes it's a link because it has http

    2) the ability to determine the length of a typed URL

    Since you get an error when the length is too long, it already detects the length on post, even on preview.

    3) the ability for the forum admin to specify a max length

    There probably is a mechanism to change the allotted length of a link and adjust as necessary. Some links may contain thousands of characters, which makes this impractical.

    4) the ability for the forum to block the Post Comment if the typed URL exceeds >the max length

    There are plugins to stop people from posting links, or at least there used to be…based on permissions etc I will search and see if I can find it. Maybe it can be modified to limit the length of the url too.

    5) the ability for the forum to display a warning to the user as to why the post was >blocked and how to fix it

    As far as I know this already exists.. not sure about the details you want. This is typical behavior of this software. You can even change the definitions of the messages to say whatever you want.

    6) the ability for the user's post text to be retained in draft mode so that they can >make edits to comply with the warning

    There already is a draft system that automatically saves the draft or you can save it manually. The user can access their Drafts via their profile . If there is a draft for a post, it will also show in the editor when a person decides to go back and post it.

    Not sure if the draft is saved after a warning, it should, it would simply not allow post.

  • Maybe we are not communicating clearly. I'll try to do better.

    I know that the link function exists. What I am trying to prevent is the user typing a super long URL into the message body text of a post. This user is ignorant to the link feature and is not using it. Thus, the problem I am trying to solve is: How can the vanilla forum catch when this "long link in body text" problem happens and redirect the user to use the existing Link function? Ideally, we want to allow short links in the body text (like video links), but prevent really long ones.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    @ToddL said:
    Maybe we are not communicating clearly. I'll try to do better.

    No maybe I just misunderstood, I thought you wanted to allow a longer url.. lol

    I think there must be a way to validate the length of a url inputed to the body. I will look into it and hope others do as well. Now that we are on the same page…

  • RiverRiver MVP
    edited August 2016

    @ToddL said:
    Maybe we are not communicating clearly. I'll try to do better.

    I know that the link function exists. What I am trying to prevent is the user typing a super long URL into the message body text of a post. This user is ignorant to the link feature and is not using it. Thus, the problem I am trying to solve is: How can the vanilla forum catch when this "long link in body text" problem happens and redirect the user to use the existing Link function? Ideally, we want to allow short links in the body text (like video links), but prevent really long ones.

    You might run into more problems if you try to block long links. suppose the link really is long. More like a tempest in a teapot.

    The best way is to educate the users. Post a video of how to link, and make sure the user understands.

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    @River said:
    and make sure the user understands.

    If there was a way to block a person from posting any links because they don't use the proper protocol, via permissions, that would help.

    A plugin that prevents users from posting links. Then when they ask why they can't post links, the moderator can inform them that they are not trusted to follow protocol.

  • That's funny about blocking the user, but would work. Yet, often the users are truly just ignorant vs. indifferent/non-compliant. Still, once it is posted, then it becomes a problem that messes up the viewing of the whole thread for all users on iPhones. So, it becomes an administrator's nightmare to keep asking various users to fix it or for the admin to forcibly fix it for them. Especially if the site is super high traffic.

    So, that is why I am asking about a solution. Or, I guess I am asking for an enhancement/plugin to be created. As noted in the list of requirements above, it would be great if the admin can set a URL max length as certain situations for different forum implementations may require a different threshold to define "too long."

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Yes, it actually works to train people by blocking them from posting very long links .

    I think that first we need to check for the length of a string, that is not hard to do, but what I am stuck at is how can it determine it is a link, would need to add extra instructions , maybe if the string contains http or https , then it could be measured as a link and not the rest of the body.

    Maybe someone who is a total php expert can use this idea to create the plugin you need, and call it CheckLinkLength… ;)

    http://php.net/manual/en/function.strlen.php

    http://php.net/manual/en/function.mb-strlen.php

    http://php.net/manual/en/function.mb-internal-encoding.php

    http://php.net/manual/en/function.mb-http-input.php

    http://php.net/manual/en/function.mb-http-output.php

  • R_JR_J Ex-Fanboy Munich Admin

    It would actually be quite easy: checking the text before it is saved and giving out an error message if you like to is super easy. There are numerous examples if you do a fullt text search for "beforeSave" in the plugins folder.
    Checking for a link can be done by looking at how Vanilla does so by looking at the link() method in class.format.php

    But from UX point of view, that would be a disaster. What would you say if you post a link which you think is super useful and you get a red warning sign which only expresses "We are not able to cope with all links, please help us!"
    If I would face such a message, I would think that it must be a prank.

    Do you know about the html link tag? It's form is this: <a href="http://whereever">what ever</a> so that the actual link target and the text shown doesn't have to be the same. Vanilla does some magic: when it detects text like that http://www.vanillaforums.org it converts it to such a link <a href="http://www.vanillaforums.org">http://www.vanillaforums.org</a>.

    I see two better solutions than stopping people from posting links. Normally I prefer plugins with php code which alter content before it is written to screen, but not in this case. You could write a plugin that inserts a few lines of JavaScript which does this:

    1. Loop through all links in discussion
    2. Check length of text between <a...> and </a>
    3. If length <= maxlength, continue loop
    4. If length > maxlength, replace text with maxlength-3 characters of text + '...'

    If this would be too long
    https://vanillaforums.org/discussion/32443/post-validation-stop-long-urls-from-body-text#latest
    It would be displayed like that

    https://vanillaforums.org/discussion/32443/post-validation-stop-long-urls-fr...

    You should do one more step:
    5. If text has been changed, add attribute "title" to link tag and set value = old text

    That way, if user hovers over the shortened link, he sees the actual link in a popup. But all browsers show the link target in their status bar anyway.

    Give feedback if you need more help to integrate it, but start first by creating a plugin skeleton which does nothing more than adding a js snippet
    https://github.com/vanilla/addons/blob/master/plugins/example/class.example.plugin.php#L57-L64

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    HI @RJ - I concur with your approach but wonder why do you prefer to apply the change in JS rather than a very simple php only plugin that would actually save the alteration in the database?

  • R_JR_J Ex-Fanboy Munich Admin

    Changes couldn't/shouldn't be made in the database. Although it would be possible to change every http://long into <a href="http://long">short</a> or to (short)[http://long] that would become format agnostic. Furthermore such postings wouldn't be influenced by $Configuration['Garden']['Format']['Links'] any more.

    Links are not stored as links in the db. They are altered when the body is formatted (see class formats method links() and you'll find out that this is not "very simple" work). Although you would be able to hook into format_links_handler($sender, $args) and change the links in $args['Mixed'] you would have to repeat all work that has been done already before in that method.

    The mere fact that you would have to do the regex parsing for links 2 times with php makes it look bad to me, and doing this with php forces you to parse each and every post, because you cannot tell if it contains any link before parsing.

    JavaScript simply seems to be more elegant in this case to me.

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    Thanks@R_J, I now see the complexity in the linksCallback function and its support of embedding etc.

    I would, however, raise the question of urls where the visible link text is long but different than the url itself. In such cases I suggest not to trim the url as it is clear that the intention was to display the different text. The likely use case is when a user pastes something into the post and the pasted text contains urls.

    It's all academic for me - I don't have a need for such a plugin nor do I plan to write one.

  • R_JR_J Ex-Fanboy Munich Admin

    Look at this link: Post Validation - Stop Long URLs fro...

    It has the correct address, but the text, which seems to be intentionally different then the link, is cut. But look at the title which appears when you hover over the link: it holds the complete text.

    If you loop over all links in posts and if they are too long, set the link text as the title and trim the text to MaxLength, then everything should be fine.

    If the layout is destroyed when links are too long, then you really do not have an alternative.

  • rbrahmsonrbrahmson "You may say I'm a dreamer / But I'm not the only one" NY ✭✭✭

    Yes, I understand, but I don't like the truncation when the title is different. It is meant to convey something, and that is now lost. A matter of personal taste, I guess.

  • R_JR_J Ex-Fanboy Munich Admin

    If long links are breaking the style, I would work on the style ;)

  • RiverRiver MVP
    edited August 2016

    If the layout is destroyed when links are too long, then you really do not have an alternative.

    How is the layout destroyed? Is it cosmetic or function or personal.

    iPhone display of forum content has issues if a user posts a long URL into post.

    wouldn't this be css issue or is it functional?

    Pragmatism is all I have to offer. Avoiding the sidelines and providing centerline pro-tips.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    @River said:
    How is the layout destroyed? Is it cosmetic or function or personal.
    wouldn't this be css issue ?

    I tend to think so, because I am a CSS Queen and can fix anything CSS…. to fix a layout issue when the words are as long as some german words I use break word…

    If I could get a link that was hugely long that would break a style for others , I will bet a thousand dollars I could fix it by adding a break word CSS rule for links in messages.

    http://www.w3schools.com/cssref/css3_pr_word-break.asp

    I would like to see an example before I pay the 1000k and get a chance to prove myself as CSS Queen…

  • These look like better ideas! Thank you for your advice.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Try adding this to your mobile theme custom.css file

    .Message a{
    word-wrap: break-word;
    }
    
Sign In or Register to comment.