Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
matrix_insert_image.inc
1 <?php
18 global $ROOT_PATH;
19 include_once($ROOT_PATH.'wysiwyg_plugin.inc');
20 
35 {
36 
37 
44  function matrix_insert_image(&$wysiwyg)
45  {
46  wysiwyg_plugin::wysiwyg_plugin($wysiwyg);
47  $this->_add_button('matrixinsertimage','MatrixInsertImage','Insert Image','false','34');
48 
49  }//end constructor
50 
51 
60  function paint_generic()
61  {
62  ?>
63  <script type="text/javascript" language="Javascript">
64  // Called when the user clicks on "InsertImage" button
65  HTMLArea.prototype._matrixinsertImage = function() {
66  var editor = this; // for nested functions
67 
68  var url = "";
69  var align = "";
70  var width = "";
71  var height = "";
72  var alt = "";
73  var title = "";
74  var longdesc = "";
75  var hspace = "";
76  var vspace = "";
77  var border = "0";
78  var imageid = "0";
79  var image_class = "";
80  var image_id = "";
81  var is_image = false;
82 
83  var sel = this._getSelection();
84  var range = this._createRange(sel);
85  // loop through and try and find a selected image tag
86  if (HTMLArea.is_gecko) {
87  var fragment = this._doc.createDocumentFragment();
88  var div = this._doc.createElement("div");
89  div.innerHTML = this.getSelectedHTML();
90  while (div.firstChild) {
91  if (div.firstChild.tagName == "IMG") {
92  is_image = true;
93  var image_tag = div.firstChild;
94  break;
95  }
96  // the following call also removes the node from div
97  fragment.appendChild(div.firstChild);
98  }
99  } else if (HTMLArea.is_ie) {
100  if(range.length) {
101  if (range.item(0).tagName == "IMG") {
102  is_image = true;
103  var image_tag = range.item(0);
104  }
105  }
106  }
107 
108  // if there is an existing image, get its properties
109  if (is_image && image_tag) {
110  url = image_tag.src;
111  align = image_tag.align;
112  alt = image_tag.alt;
113  title = image_tag.title;
114  image_class = image_tag.className;
115  image_id = image_tag.id;
116  width = (!image_tag.width) ? "" : image_tag.width;
117  height = (!image_tag.height) ? "" : image_tag.height;
118  hspace = (!image_tag.hspace) ? "" : image_tag.hspace;
119  vspace = (!image_tag.vspace) ? "" : image_tag.vspace;
120 
121  // Bug #1708 - If border attribute is used in ima tag, change it to style attribute.
122  if (!image_tag.border && !image_tag.style.border) {
123  border = "0";
124  } else {
125  if (image_tag.border) {
126  border = image_tag.border;
127  } else if (image_tag.style.border) {
128  border = image_tag.style.border;
129  border = border.match(/(\d{1})px/)[1];
130  }
131  }
132 
133  longdesc = (!image_tag.longDesc) ? "" : image_tag.longDesc;
134  if (image_tag.longDesc) {
135  var result = image_tag.longDesc.match(/(.+)\/\?a=(\d+)/);
136  if (result != null) {
137  // longdesc is from assetid
138  longdesc = result[2];
139  } else {
140  // long desc is manually typed
141  e = 'http[s]?://<?php echo $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']?>';
142  re = new RegExp(e, "gi");
143  longdesc = image_tag.longDesc.replace(re, "");
144 
145  }
146  } else {
147  longdesc = "";
148  }
149  imageid = editor.make_relative_hrefs(url);
150  e = '.*\\?a=([0-9]+)((:[^$]*)\\$)?';
151  re = new RegExp(e, "gi");
152  imageid = imageid.replace(re, "$1$3");
153  }
154 
155  var in_popup = '0';
156  if (this._inPopup == true) { in_popup = '1'; }
157 
158  editor_<?php echo $this->wysiwyg->name?>._object = this;
159  strPage = "<?php echo $this->get_popup_href('insert_image_frames.php')?>?f_imageid=" + escape(imageid) + "&f_align=" + escape(align) + "&f_width=" + escape(width) + "&f_height=" + escape(height) + "&f_alt=" + escape(alt) + "&f_title=" + escape(title) + "&f_longdesc=" + escape(longdesc) + "&f_horiz=" + escape(hspace) + "&f_vert=" + escape(vspace) + "&f_border=" + escape(border) + "&f_image_class=" + escape(image_class) + "&f_image_id=" + escape(image_id) + "&editor_name=" + escape("<?php echo $this->wysiwyg->name; ?>") + "&in_popup=" + escape(in_popup);
160 
161  var popup_height = 630;
162 
163  // A bit more breathing room for Safari
164  if (HTMLArea.is_safari) popup_height += 35;
165  if (HTMLArea.is_linux && HTMLArea.is_gecko) popup_height += 35;
166 
167  this._popupDialog("matrixInsertImage", strPage, 800, popup_height, true, function(param) {
168 
169  if (!param) {
170  // user must have pressed Cancel
171  return false;
172  }
173 
174  if (param["f_imageid"] != 0) {
175  var url = './?a=' + param["f_imageid"];
176  // shadow asset .. needs terminating exclamation mark
177  if (param["f_imageid"].search(/:/) != -1) {
178  url += '$';
179  }
180  } else {
181  if (is_image && image_tag) {
182  if (image_tag.src != '') {
183  url = image_tag.src;
184  } else {
185  url = '';
186  }
187  }
188  }
189 
190  var html = '<img src="' + url + '"';
191  if (param["f_align"] != "") {
192  html += ' align="' + param["f_align"] + '"';
193  }
194  if (param["f_width"] != "") {
195  html += ' width="' + param["f_width"] + '"';
196  }
197  if (param["f_height"] != "") {
198  html += ' height="' + param["f_height"] + '"';
199  }
200  if (param["f_alt"] != "") {
201  html += ' alt="' + param["f_alt"] + '"';
202  }
203  if (param["f_title"] != "") {
204  html += ' title="' + param["f_title"] + '"';
205  }
206  if (param["f_image_id"] != "") {
207  html += ' id="' + param["f_image_id"] + '"';
208  }
209  if (param["f_image_class"] != "") {
210  html += ' class="' + param["f_image_class"] + '"';
211  }
212 
213  if (param["f_longdesc"] != null && param["f_longdesc"] != "") {
214  var result = param["f_longdesc"].match(/(\d)+/);
215  if (result != null) {
216  if (result[1] != "0") {
217  html += ' longdesc="<?php echo sq_web_path('root_url'); ?>' + '/?a=' + param["f_longdesc"] + '"';
218  }
219  } else {
220  html += ' longdesc="' + param["f_longdesc"] + '"';
221  }
222  }
223 
224  if (param["f_horiz"] != "") {
225  html += ' hspace="' + param["f_horiz"] + '"';
226  }
227  if (param["f_vert"] != "") {
228  html += ' vspace="' + param["f_vert"] + '"';
229  }
230  if (param["f_border"] != "") {
231  html += ' style="border: ' + param["f_border"] + 'px solid rgb(0, 0, 0);"';
232  }
233  html += '>';
234  html = editor.make_absolute_urls(html);
235 
236  if (!is_image || HTMLArea.is_gecko) {
237  // replace text with new image
238  editor.insertHTML(html, range);
239  } else {
240  // we are replacing an existing image in IE
241  image_tag.outerHTML = html;
242  }
243 
244  }, null);
245  };
246  </script>
247  <?php
248 
249  }//end paint_generic()
250 
251 
258  function print_plugin_button_click()
259  {
260  ?>
261  case "matrixinsertimage":
262  this._matrixinsertImage();
263  return;
264  break;
265  <?php
266 
267  }//end print_plugin_button_click()
268 
269 
279  function process(&$html)
280  {
281  // Check whether any width or height has been updated
282  $disable_resize = $GLOBALS['SQ_SYSTEM']->getUserPrefs('content_type_wysiwyg', 'SQ_WYSIWYG_DISABLE_IMAGE_RESIZE');
283  if ($disable_resize == 'yes') {
284  $new_html = rawurldecode($html);
285  $images = Array();
286  preg_match_all('/<img[^>]*>/i', $new_html, $images);
287  if (!empty($images)) {
288  $html_updated = FALSE;
289  foreach ($images[0] as $img_tag) {
290  // matrix internal link?
291  $src = Array();
292  preg_match('/src=["\']?\.\/\?a=([^ "\'>]*)["\']?/i', $img_tag, $src);
293  $style_width_changed = FALSE;
294  $style_height_changed = FALSE;
295  $attr_width_changed = FALSE;
296  $attr_height_changed = FALSE;
297  $tmp_img_tag = $img_tag;
298 
299  if (!empty($src)) {
300  $id_parts = explode(':', $src[1]);
301  if (isset($id_parts[1])) continue;
302 
303  $img_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($src[1]);
304  $width = $img_asset->attr('width');
305  $height = $img_asset->attr('height');
306 
307  // inlined style?
308  $style = Array();
309  preg_match('/style=["\']?([^>"\']*)["\']?/i', $img_tag, $style);
310  if (!empty($style)) {
311  $style_width = Array();
312  preg_match('/width: *(\d+) *(px)?/i', $img_tag, $style_width);
313  if (!empty($style_width)) {
314  $style_w = (int) $style_width[1];
315  if ($style_w != $width) {
316  $style_width_changed = TRUE;
317  $replacement = 'width: '.$width.'px';
318  $tmp_img_tag = str_replace($style_width[0], $replacement, $tmp_img_tag);
319  }
320  }
321 
322  $style_height = Array();
323  preg_match('/height: *(\d+) *(px)?/i', $img_tag, $style_height);
324  if (!empty($style_height)) {
325  $style_h = (int) $style_height[1];
326  if ($style_h != $height) {
327  $style_height_changed = TRUE;
328  $replacement = 'height: '.$height.'px';
329  $tmp_img_tag = str_replace($style_height[0], $replacement, $tmp_img_tag);
330  }
331  }
332 
333  }
334 
335  // width attribute?
336  $width_attr = Array();
337  preg_match('/width=["\']?([^> "\']*)["\']?/i', $img_tag, $width_attr);
338  if (!empty($width_attr)) {
339  $w_attr = (int) $width_attr[1];
340  if ($w_attr != $width) {
341  $attr_width_changed = TRUE;
342  $replacement = 'width="'.$width.'"';
343  $tmp_img_tag = str_replace($width_attr[0], $replacement, $tmp_img_tag);
344  }
345  }
346 
347  // height attribute?
348  $height_attr = Array();
349  preg_match('/height=["\']?([^> "\']*)["\']?/i', $img_tag, $height_attr);
350  if (!empty($height_attr)) {
351  $h_attr = (int) $height_attr[1];
352  if ($h_attr != $height) {
353  $attr_height_changed = TRUE;
354  $replacement = 'height="'.$height.'"';
355  $tmp_img_tag = str_replace($height_attr[0], $replacement, $tmp_img_tag);
356  }
357  }
358 
359  if ($style_width_changed || $style_height_changed || $attr_width_changed || $attr_height_changed) {
360  $new_html = str_replace($img_tag, $tmp_img_tag, $new_html);
361  if (!$html_updated) $html_updated = TRUE;
362  }
363  }//end if src
364 
365  }//end foreach image tags
366 
367  if ($html_updated) $html = $new_html;
368 
369  }//end if images
370 
371  }//end if disable_resize
372 
373  }//end process()
374 
375 
376 }//end class
377 
378 ?>