Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Hot linking to images and associated problems and solutions


Go to End


4 Posts   1084 Views

Avatar
simples

Community Member, 77 Posts

11 August 2012 at 12:02am

Edited: 13/08/2012 6:20pm

Hi,

I have just realized that by simply browsing a remote site with my Chrome browser and cut and pasting an image from that site into the content area of my Silverstripe's WYSIWYG editor I unwittingly hot linked (melodramatically referred to as bandwidth theft) from that site when I then browsed the page.

Perhaps more importantly, when images become unavailable on these remote sites, image placeholders, which illustrate failed attempts to link to these non-existent images, will appear when the page is browsed. Obviously the availability and what these remote images contain is totally under the control and discretion of whoever is maintaining and hosting this remote site.

Thirdly I have just written a facility which extracts the first image added to a news post and show this image in a preview. This facility currently looks in the assets folder for the image and if no image has been stored in the assets folder, even though an image is shown in the news post, the preview shows no image.

Perhaps a solution to these 3 issues would be to

retrieve a copy of the image from the remote site,
copy it to the assets folder, and
replace links to these remote sites in stored HTML with links to the assets folder.

Perhaps this clean-up operation could be performed whenever the user presses a save button in the CMS.

Such a solution would still allow the user to

continue to easily create pages by simply copying and pasting content,
conveniently perform the clean up operation without the user needing to take extra action, and
avoid amateurish image place holders cropping up over the site when the remote image is not found.

Unfortunately I have so far been unable to find a facility written for Silverstripe which carries out this task.

Obviously it is quite possible that I have overlooked a number of issues which means that what I have proposed is not required, inappropriate or not recommended.

If this is the case, or if what I have described is an issue and a facility/plugin/module currently exists which solves the problem, perhaps you could let me know.

Thanks.

Avatar
simples

Community Member, 77 Posts

12 August 2012 at 7:09am

Edited: 13/08/2012 6:56am

Hi,

I have not yet been able to identify an entry point into the Silverstripe code.

However here are a couple of articles regarding manual implementations outside Silverstripe.

http://www.webdeveloper.com/forum/showthread.php?t=125774
http://drupal.org/node/620108

Avatar
simples

Community Member, 77 Posts

12 August 2012 at 10:55pm

Edited: 20/08/2012 8:32am

Hi,

This post looks at a way of detecting when an editor save button in the CMS is clicked so that HTML hrefs which refer to images on remote sites in the content can be changed to point to locations at which these images will be stored in a local folder. The idea behind this is to remove the vulnerabilities associated with hot linking. Please see the above comments for details.

Avatar
simples

Community Member, 77 Posts

20 August 2012 at 7:56am

Edited: 20/08/2012 10:37pm

I have just written a module called LocalImages which updates content pasted into the WYSIWYG content window. In fact it converts links which point to remote images to links which point to images stored locally. The code is attached.

Without going into details about table transactions, the following is essentially what should happen. On clicking the Save button in the WYSIWYG editor, destination addresses to remote images are extracted from HTML in the content window, images are requested from any remote addresses found, and the retrieved images are stored in a folder under the assets folder specified in _config.php. Finally links are updated in the HTML to point to these local addresses.

This module can be implemented by adding the following code to mysite/code/Page.php

class Page extends SiteTree {
	public function onAfterWrite() {
		LocalImages::addImageTracking($this->ID);
		parent::onAfterWrite();
	}
}

Please be aware that this module uses pcurl so this needs to be enabled in your php.ini file for this module to work.

The module has been tested in Silverstripe Version 2.4.7 when running PHP 5.2.11 on Windows XP only.

In view of the very limited testing which I have done to-date (e.g. no testing on Linux with its stricter adherence to case), I have not released this module in any form at http://www.silverstripe.org/modules. However I thought it may help other people who may be struggling with issues caused by the hot linking of images to report here what I have done. Please bear in mind that I have just started using Silverstripe and my experience with writing OO code is very limited. Hence there is likely to be issues with this code and a risk that running it on your system will corrupt your database.

If you are still interested, in my configuration I set up debugging as follows.

1. I added the following code to mysite\_config.php

/**
 * Enables error logging
 */
SS_Log::add_writer(new SS_LogFileWriter(Director::baseFolder() . '/debug.log'), SS_Log::ERR);
SS_Log::add_writer(new SS_LogFileWriter(Director::baseFolder() . '/debug.log'), SS_Log::WARN);
SS_Log::add_writer(new SS_LogFileWriter(Director::baseFolder() . '/debug.log'), SS_Log::NOTICE);
ini_set("log_errors", "On");
ini_set("error_log", Director::baseFolder() . "/debug.log");

2. I checked that the log function in sapphire\dev\Debug.php contained the following line

$file = dirname(__FILE__).'/../../debug.log';

(Please note that I am not aware that I have made any changes to files in my sapphire and cms folders after downloading them from the Silverstripe site.)

Hopefully, on completing Steps 1 and 2 above, both progress messages which are sent from the code, and error messages, if errors occur, will be sent to a file called debug.log in your root folder.

Install the module by copying the attached folder to your root folder and test it as follows.

  • 1. Set $localimages_folder in the module _config.php to point to the folder under the assets folder where you would like the retrieved images to be stored.
  • 2. Create the folder.
  • 3. Log in to your CMS editor.
  • 4. Cut and paste an image from a remote site into the content window of your WYSIWYG editor.
  • 5. Click on the HTML button in your WYSIWYG's menu and note that image href's refer to remote addresses which start http.
  • 6. Click on the WYSIWYG editor's Save button.
  • 7. If the WYSIWYG editor reports an error such as Javascript error in red, consult the debug file which you set up in the above steps and correct the issue reported.
  • 8. When the code is working and the WYSIWYG editor reports that the content has been saved, click on the HTML button again. Href's should now refer to files in the local folder which you created in Step 2.

PS. I am using Version 2.4 of Silverstripe rather than Version 3 because the client for whom I am creating a site is restricted to using PHP 5.2.

PPS. Please note that in my code I write debug::log($message) where the 'd' is in lower case rather than upper case. I would not have expected this to work. Indeed writing debug::show($variable) fails and Debug::show($variable) works. So it is a mystery to me why debug::log($message) works. I write debug::log($message) however because I do not have to keep reaching for my shift button as I have to when writing Debug::log($message). If debug::log($message) does not work on your system, you may need to change the logging statements which I have added to the code.

Attached Files