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.