Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
0 votes
1.6k views
in Plugins by

Some users are posting images instead of uploading them to my server. E.g. http://cl.ly/image/1q1F2q0P233I

As the images are essential for the questions I need to have them on my server!

How can I disallow posting of such image links?

I guess I have to write my own plugin? but it is hard to detect them, see example above...

Q2A version: 1.5.3
by
I went the jquery way now:

var editorContent = String(qa_ckeditor_content.getData());
// check if there is a link to an external image, also check if line break follows <br /> or end of paragraph
    if( editorContent.match(/(https?:\/\/\S+\.(?:jpg|png|gif|jpg<br|png<br|gif<br|jpg<\/p>|png<\/p>|gif<\/p>))\s+/) != null ) {
    alert('Image links are not allowed.');
    return false;
}

3 Answers

+1 vote
by

A filter plugin might be a good way to go. Then you can prevent posts with external images, or send them for moderation. If you're using the WYSIWYG editor you can search for the HTML. Something like this:

 

public function filter_question( &$question, &$errors, $oldquestion )
{
  if ( isset($question['content']) )
  {
    if ( preg_match('#src="^(http://yoursite.com)#', $question['content']) )
      return 'You must upload an image instead of linking to an external one.';
  }
}
 
Note: I don't think that regular expression is correct, the idea is to check for any src attribute that it not linked to your site. But I'm pretty sure the ^ character doesn't work like that outside square brackets. There should be another way to do the regex.
by
Thank you Scott! I will look into this soon.

Drawback: Every link posted will be effected, i.e. must be moderated. That is causing some extra hours per month. I thought of kind of automisation... your if statement is the right first step, 2nd would be to let php check if the link is an image! And just then send out an email / or flag for moderation.
Or even better: Load that image, convert it if necessary, and upload it into table qa_blob etc.
by
Returning a string will show an error to the user rather than using moderation. So they wouldn't be able to post until all images are uploaded to your site. Might be annoying for users though.

By the way if you want moderation you need to instead set $question['queued'] = true;
0 votes
by
I Googled about this and found this post of yours from April!

http://www.question2answer.org/qa/14183/tweaking-ckeditor-allow-external-images-allow-image-uploads

Is that not the solution?
by
Nope, the linked post focussed on inserting images: A - by URL or B - by uploaded image file, both get posted by CKEditor with IMG tag.

The issue above addresses the post of a URL as pure text.
+1 vote
by

I think the "future solution" is (without the user being annoyed) to:

1. strip the link from content posted

2. check if it is an image → see php options here: stackoverflow_1 and stackoverflow_2

3. load the image in the php buffer, convert it to our needs

4. upload it as blob and insert the IMG tag with the blob URL

I am quite certain that this is one reasonable way to go :)
Kai

...