4 * StatusNet - the distributed open-source microblogging tool
5 * Copyright (C) 2010, StatusNet, Inc.
7 * Use TinyMCE library to allow rich text editing in the browser
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * @author Evan Prodromou <evan@status.net>
27 * @copyright 2010 StatusNet, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
29 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Use TinyMCE library to allow rich text editing in the browser
40 * Converts the notice form in browser to a rich-text editor.
44 * @author Evan Prodromou <evan@status.net>
45 * @copyright 2010 StatusNet, Inc.
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47 * @link http://status.net/
49 class TinyMCEPlugin extends Plugin
53 function onEndShowScripts($action)
55 if (common_logged_in ()) {
56 $action->script(common_path('plugins/TinyMCE/js/jquery.tinymce.js'));
57 $action->inlineScript($this->_inlineScript());
63 function onEndShowStyles($action)
65 $action->style('span#notice_data-text_container, span#notice_data-text_parent { float: left }');
69 function onPluginVersion(&$versions)
71 $versions[] = array('name' => 'TinyMCE',
72 'version' => STATUSNET_VERSION,
73 'author' => 'Evan Prodromou',
74 'homepage' => 'http://status.net/wiki/Plugin:TinyMCE',
76 _m('Use TinyMCE library to allow rich text editing in the browser.'));
81 * Sanitize HTML input and strip out potentially dangerous bits.
83 * @param string $raw HTML
86 private function sanitizeHtml($raw)
88 require_once INSTALLDIR . '/extlib/htmLawed/htmLawed.php';
90 $config = array('safe' => 1,
91 'deny_attribute' => 'id,style,on*');
93 return htmLawed($raw, $config);
97 * Strip HTML to plaintext string
99 * @param string $html HTML
100 * @return string plaintext, single line
102 private function stripHtml($html)
104 return str_replace("\n", " ", html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8'));
108 * Hook for new-notice form processing to take our HTML goodies;
109 * won't affect API posting etc.
111 * @param NewNoticeAction $action
113 * @param string $content
114 * @param array $options
115 * @return boolean hook return
117 function onStartSaveNewNoticeWeb($action, $user, &$content, &$options)
119 if ($action->arg('richedit')) {
120 $html = $this->sanitizeHtml($content);
121 $options['rendered'] = $html;
122 $content = $this->stripHtml($html);
128 * Hook for new-notice form processing to process file upload appending...
130 * @param NewNoticeAction $action
131 * @param MediaFile $media
132 * @param string $content
133 * @param array $options
134 * @return boolean hook return
136 function onStartSaveNewNoticeAppendAttachment($action, $media, &$content, &$options)
138 if ($action->arg('richedit')) {
139 // See if we've got a placeholder inline image; if so, fill it!
140 $dom = new DOMDocument();
142 if ($dom->loadHTML($options['rendered'])) {
143 $imgs = $dom->getElementsByTagName('img');
144 foreach ($imgs as $img) {
145 if (preg_match('/(^| )placeholder( |$)/', $img->getAttribute('class'))) {
146 // Create a link to the attachment page...
147 $this->formatAttachment($img, $media);
150 $options['rendered'] = $this->saveHtml($dom);
153 // The regular code will append the short URL to the plaintext content.
154 // Carry on and let it through...
160 * Format the attachment placeholder img with the final version.
162 * @param DOMElement $img
163 * @param MediaFile $media
165 private function formatAttachment($img, $media)
167 $parent = $img->parentNode;
168 $dom = $img->ownerDocument;
170 $link = $dom->createElement('a');
171 $link->setAttribute('href', $media->fileurl);
172 $link->setAttribute('title', File::url($media->filename));
174 if ($this->isEmbeddable($media)) {
175 // Fix the the <img> attributes and wrap the link around it...
176 $this->insertImage($img, $media);
177 $parent->replaceChild($link, $img); //it dies in here?!
178 $link->appendChild($img);
180 // Not an image? Replace it with a text link.
181 $link->setAttribute('rel', 'external');
182 $link->setAttribute('class', 'attachment');
183 $link->setAttribute('id', 'attachment-' . $media->fileRecord->id);
184 $text = $dom->createTextNode($media->shortUrl());
185 $link->appendChild($text);
186 $parent->replaceChild($link, $img);
191 * Is this media file a type we can display inline?
193 * @param MediaFile $media
196 private function isEmbeddable($media)
198 $showable = array('image/png',
201 return in_array($media->mimetype, $showable);
205 * Rewrite and resize a placeholder image element to match the uploaded
206 * file. If the holder is smaller than the file, the file is scaled to fit
207 * with correct aspect ratio (but will be loaded at full resolution).
209 * @param DOMElement $img
210 * @param MediaFile $media
212 private function insertImage($img, $media)
214 $img->setAttribute('src', $media->fileRecord->url);
216 $holderWidth = intval($img->getAttribute('width'));
217 $holderHeight = intval($img->getAttribute('height'));
219 $path = File::path($media->filename);
220 $imgInfo = getimagesize($path);
223 $origWidth = $imgInfo[0];
224 $origHeight = $imgInfo[1];
226 list($width, $height) = $this->sizeBox(
227 $origWidth, $origHeight,
228 $holderWidth, $holderHeight);
230 $img->setAttribute('width', $width);
231 $img->setAttribute('height', $height);
237 * @param int $origWidth
238 * @param int $origHeight
239 * @param int $holderWidth
240 * @param int $holderHeight
241 * @return array($width, $height)
243 private function sizeBox($origWidth, $origHeight, $holderWidth, $holderHeight)
245 $holderAspect = $holderWidth / $holderHeight;
246 $origAspect = $origWidth / $origHeight;
247 if ($origAspect >= 1.0) {
249 if ($origWidth > $holderWidth) {
250 return array($holderWidth, intval($holderWidth / $origAspect));
252 return array($origWidth, $origHeight);
255 if ($origHeight > $holderHeight) {
256 return array(intval($holderWidth * $origAspect), $holderHeight);
258 return array($origWidth, $origHeight);
263 private function saveHtml($dom)
265 $html = $dom->saveHTML();
266 // hack to remove surrounding crap added to the dom
267 // all we wanted was a fragment
268 $stripped = preg_replace('/^.*<body[^>]*>(.*)<\/body.*$/is', '$1', $html);
272 function _inlineScript()
274 $path = common_path('plugins/TinyMCE/js/tiny_mce.js');
275 $placeholder = common_path('plugins/TinyMCE/icons/placeholder.png');
277 // Note: the normal on-submit triggering to save data from
278 // the HTML editor into the textarea doesn't play well with
279 // our AJAX form submission. Manually moving it to trigger
280 // on our send button click.
281 $scr = <<<END_OF_SCRIPT
282 $().ready(function() {
283 var noticeForm = $('#form_notice');
284 $('textarea#notice_data-text').tinymce({
285 script_url : '{$path}',
288 plugins : "paste,fullscreen,autoresize,inlinepopups,tabfocus,linkautodetect",
289 theme_advanced_buttons1 : "bold,italic,strikethrough,|,undo,redo,|,link,unlink,image,|,fullscreen",
290 theme_advanced_buttons2 : "",
291 theme_advanced_buttons3 : "",
292 add_form_submit_trigger : false,
293 theme_advanced_resizing : true,
294 tabfocus_elements: ":prev,:next",
295 setup: function(ed) {
296 noticeForm.append('<input type="hidden" name="richedit" value="1">');
298 $('#notice_action-submit').click(function() {
299 tinymce.triggerSave();
302 var origCounter = SN.U.CharacterCount;
303 SN.U.CharacterCount = function(form) {
304 var text = $(ed.getDoc()).text();
307 ed.onKeyUp.add(function (ed, e) {
308 SN.U.Counter(noticeForm);
311 $('#'+SN.C.S.NoticeDataAttach).change(function() {
312 var img = '<img src="{$placeholder}" class="placeholder" width="320" height="240">';
313 var html = tinyMCE.activeEditor.getContent();
314 ed.setContent(html + img);