Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
matrix_embed_youtube.inc
1 <?php
18 global $ROOT_PATH;
19 include_once($ROOT_PATH.'wysiwyg_plugin.inc');
20 
33 {
34 
35  var $_img_url = '';
36 
37 
44  function matrix_embed_youtube(&$wysiwyg)
45  {
46  wysiwyg_plugin::wysiwyg_plugin($wysiwyg);
47  $this->_add_button('matrixembedyoutube','MatrixEmbedYouTube','Embed YouTube','false','67');
48  $this->_img_url = $wysiwyg->web_path.'plugins/'.$this->get_popup_href('embedded_object.gif');
49 
50  }//end constructor
51 
52 
59  function paint_generic()
60  {
61  ?>
62  <script type="text/javascript" language="Javascript">
63  // Called when the user clicks on "InsertImage" button
64  HTMLArea.prototype._matrixEmbedYouTube = function() {
65  var editor = this; // for nested functions
66 
67  var sel = this._getSelection();
68  var range = this._createRange(sel);
69 
70  var style = "";
71  var width = "480";
72  var height = "385";
73  var auto_start = "0";
74  var loop = "0";
75  var full_screen = "1";
76  var is_file = false;
77  var vid = "";
78  var video_url = "";
79  var show_border = "";
80  var colour1 = "";
81  var colour2 = "";
82  var rel = "";
83  var egm = "";
84  var enable_js = "";
85 
86  // loop through and try and find a selected embed tag
87  if (HTMLArea.is_gecko) {
88  var fragment = this._doc.createDocumentFragment();
89  var div = this._doc.createElement("div");
90  div.innerHTML = this.getSelectedHTML();
91  while (div.firstChild) {
92  if (div.firstChild.tagName == "IMG" && div.firstChild.getAttribute('sq_youtube_embeded') == "1") {
93  is_file = true;
94  var embed_tag = div.firstChild;
95  break;
96  }
97  // the following call also removes the node from div
98  fragment.appendChild(div.firstChild);
99  }
100  } else if (HTMLArea.is_ie) {
101  if (range.length) {
102  if (range.item(0).tagName == "IMG" && range.item(0).getAttribute('sq_youtube_embeded') == "1") {
103  is_file = true;
104  var embed_tag = range.item(0);
105  }
106  }
107  }
108 
109  // if there is an existing image, get its properties
110  if (is_file && embed_tag) {
111  vid = embed_tag.getAttribute('vid');
112  video_url = embed_tag.getAttribute('video_url');
113  auto_start = embed_tag.getAttribute('auto_start');
114  loop = embed_tag.getAttribute('loop');
115  full_screen = embed_tag.getAttribute('full_screen');
116  show_border = embed_tag.getAttribute('show_border');
117  colour1 = embed_tag.getAttribute('colour1');
118  colour2 = embed_tag.getAttribute('colour2');
119  width = embed_tag.getAttribute('width');
120  height = embed_tag.getAttribute('height');
121  rel = embed_tag.getAttribute('rel');
122  egm = embed_tag.getAttribute('egm');
123  enable_js = embed_tag.getAttribute('enable_js');
124 
125  // make sure they are numbers
126  width = parseInt(width);
127  height = parseInt(height);
128 
129  // failsafe
130  if (!width) { width = "" }
131  if (!height) { height = "" }
132 
133  }
134 
135  var in_popup = '0';
136  if (this._inPopup == true) { in_popup = '1'; }
137 
138  editor_<?php echo $this->wysiwyg->name?>._object = this;
139  strPage = "<?php echo $this->get_popup_href('embed_youtube_frames.php')?>?f_vid=" + escape(vid) + "&f_video_url=" + escape(video_url) + "&f_width=" + escape(width) + "&f_height=" + escape(height) + "&f_auto_start=" + escape(auto_start) + "&f_loop=" + escape(loop) + "&f_full_screen=" + escape(full_screen) + "&f_show_border=" + escape(show_border) + "&f_colour1=" + escape(colour1) + "&f_colour2=" + escape(colour2) + "&f_rel=" + escape(rel) + "&f_egm=" + escape(egm) + "&f_enable_js=" + escape(enable_js) + "&editor_name=" + escape("<?php echo $this->wysiwyg->name; ?>") + "&in_popup=" + escape(in_popup);
140 
141  var popup_height = 300;
142 
143  // A bit more breathing room for Safari
144  if (HTMLArea.is_safari) popup_height += 15;
145 
146  this._popupDialog("matrixEmbedYouTube", strPage, 780, popup_height, true, function(param) {
147  if (!param) {
148  // user must have pressed Cancel
149  return false;
150  }
151  var html = '<img sq_youtube_embeded="1" src="<?php echo $this->_img_url; ?>"';
152  html += ' style="width: ' + param["f_width"] + 'px; height: ' + param["f_height"] + 'px; border: 2px dashed black;"';
153  html += ' width="' + param["f_width"] + '"';
154  html += ' height="' + param["f_height"] + '"';
155  html += ' auto_start="' + param["f_auto_start"] + '"';
156  html += ' loop="' + param["f_loop"] + '"';
157  html += ' full_screen="' + param["f_full_screen"] + '"';
158  html += ' vid="' + param["f_vid"] + '"';
159  html += ' video_url="' + param["f_video_url"] + '"';
160  html += ' show_border="' + param["f_show_border"] + '"';
161  html += ' colour1="' + param["f_colour1"] + '"';
162  html += ' colour2="' + param["f_colour2"] + '"';
163  html += ' rel="' + param["f_rel"] + '"';
164  html += ' egm="' + param["f_egm"] + '"';
165  html += ' enable_js="' + param["f_enable_js"] + '"';
166  html += '>';
167  html = editor.make_absolute_urls(html);
168 
169  if (!is_file || HTMLArea.is_gecko) {
170  // replace text with new image
171  editor.insertHTML(html, range);
172  } else {
173  // we are replacing an existing image in IE
174  embed_tag.outerHTML = html;
175  }
176 
177  }, null);
178  };
179  </script>
180  <?php
181 
182  }//end paint_generic()
183 
184 
192  {
193  ?>
194  case "matrixembedyoutube":
195  this._matrixEmbedYouTube();
196  return;
197  break;
198  <?php
199 
200  }//end print_plugin_button_click()
201 
202 
211  function process(&$html)
212  {
213  $matches = Array();
214  preg_match_all('|(<img[^>]+sq_youtube_embeded="1"([^>]+)?>)|i', $html, $matches);
215 
216  foreach ($matches[1] as $match) {
217  $old_html = $match;
218 
219  //$match = strtolower($match);
220  $match = substr($match, 4);
221  $match = substr($match, 0, -1);
222  $match = str_replace('src="'.$this->_img_url.'"', '', $match);
223  $match = preg_replace('/style="(.*?("))/im ', '', $match);
224  // IE7/8 strips off the quotes of attributes "height" and "weight" of "img" tag
225  // when inserting the html in the given range (Range.pasteHTML())
226  $match = preg_replace('/ height=([0-9]+)/', ' height="$1"', $match);
227  $match = preg_replace('/ width=([0-9]+)/', ' width="$1"', $match);
228 
229  $pieces = Array();
230  preg_match_all('|\s+([^=]+)="([^"]+)"|', $match, $pieces);
231 
232 
233  $movie_vars = Array();
234  for (reset($pieces[1]); NULL !== ($k = key($pieces[1])); next($pieces[1])) {
235  $var_name = trim($pieces[1][$k]);
236  $var_value = trim($pieces[2][$k]);
237  $movie_vars[$var_name] = $var_value;
238  }
239  if (array_key_exists('video_url', $movie_vars) === TRUE) {
240  $movie_vars['video_url'] = $movie_vars['video_url'].'&';
241  }
242 
243  // Try and interpet the video id and create a valid url
244  $movie_url_prefix = 'http://www.youtube.com/v/';
245  $movie_url = '';
246  $add_suffix_vars = TRUE;
247  if (isset($movie_vars['video_url']) && !empty($movie_vars['video_url'])) {
248  if (preg_match('/youtu(\.?)be(.*)(v?)[\/|=](.+?)&/', $movie_vars['video_url'], $v_match)) {
249  if (isset($v_match[4]) && !empty($v_match[4])) {
250  // I found the video id
251  $movie_url = $movie_url_prefix.$v_match[4];
252  } else {
253  // Pray, the url is correct
254  $movie_url = $movie_vars['video_url'];
255  }
256  } else {
257  $movie_url = $movie_vars['video_url'];
258  }
259  }
260  // Do the video id last (this overrides all)
261  if (isset($movie_vars['vid']) && !empty($movie_vars['vid'])) {
262  $movie_url = $movie_url_prefix.$movie_vars['vid'];
263  }
264 
265  // first try getting width and height from the style
266  $width = '';
267  $height = '';
268  // if we didnt get width and height from the style, try the tag
269  if (empty($width)) {
270  if (isset($movie_vars['width'])) {
271  $width = 'width="'.str_replace('px','',$movie_vars['width']).'"';
272  }
273  }
274  if (empty($height)) {
275  if (isset($movie_vars['height'])) {
276  $height = 'height="'.str_replace('px','',$movie_vars['height']).'"';
277  }
278  }
279 
280  $movie_url_vars = '&playerapiid=matrixytplayer';
281  foreach($movie_vars as $var_name => $var_value) {
282  switch ($var_name) {
283  case 'colour1':
284  if (!empty($var_value)) {
285  $colour = str_replace('#', '', $var_value);
286  $colour = strtolower($colour);
287  $movie_url_vars .= '&color1=0x'.$colour;
288  }
289  break;
290  case 'colour2':
291  if (!empty($var_value)) {
292  $colour = str_replace('#', '', $var_value);
293  $colour = strtolower($colour);
294  $movie_url_vars .= '&color2=0x'.$colour;
295  }
296  break;
297  case 'rel':
298  if ($var_value == '1') {
299  $movie_url_vars .= '&rel='.$var_value;
300  } else {
301  $movie_url_vars .= '&rel=0';
302  }
303  break;
304  case 'auto_start':
305  if ($var_value == '1') {
306  $movie_url_vars .= '&autoplay='.$var_value;
307  } else {
308  $movie_url_vars .= '&autoplay=0';
309  }
310  break;
311  case 'loop':
312  if ($var_value == '1') {
313  $movie_url_vars .= '&loop='.$var_value;
314  } else {
315  $movie_url_vars .= '&loop=0';
316  }
317  break;
318  case 'egm':
319  if ($var_value == '1') {
320  $movie_url_vars .= '&egm='.$var_value;
321  } else {
322  $movie_url_vars .= '&egm=0';
323  }
324  break;
325  case 'show_border':
326  if ($var_value == '1') {
327  $movie_url_vars .= '&border='.$var_value;
328  } else {
329  $movie_url_vars .= '&border=0';
330  }
331  break;
332  case 'full_screen':
333  if ($var_value == '1') {
334  $movie_url_vars .= '&fs='.$var_value;
335  } else {
336  $movie_url_vars .= '&fs=0';
337  }
338  break;
339  case 'enable_js':
340  if ($var_value == '1') {
341  $movie_url_vars .= '&enablejsapi='.$var_value;
342  } else {
343  $movie_url_vars .= '&enablejsapi=0';
344  }
345  break;
346  }//end switch
347  }//end foreach
348  $movie_url_vars = htmlentities($movie_url_vars, ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET);
349 
350  ob_start();
351 
352  ?>
353  <sq_wysiwyg_embed_youtube <?php echo $match; ?>>
354  <script src="http://swfobject.googlecode.com/svn/tags/rc3/swfobject/src/swfobject.js" type="text/javascript"></script>
355  <?php
356  // $movie_vars['embed_loop'] = '0';
357  ?><object
358  <?php echo $width; ?> <?php echo $height; ?>>
359  <param name="movie" value="<?php echo $movie_url.$movie_url_vars; ?>">
360  <param name="allowfullscreen" value="<?php echo ($movie_vars['full_screen']) ? 'true' : 'false';?>">
361  <embed type="application/x-shockwave-flash"
362  src="<?php echo $movie_url.$movie_url_vars; ?>"
363  name="matrixytplayer"
364  allowfullscreen="<?php echo ($movie_vars['full_screen']) ? 'true' : 'false'; ?>"
365  <?php echo $width; ?>
366  <?php echo $height; ?>
367  ></embed>
368  </object>
369  </sq_wysiwyg_embed_youtube>
370  <?php
371  $new_html = ob_get_contents();
372  ob_end_clean();
373 
374  $html = str_replace($old_html, $new_html, $html);
375 
376  }//end foreach $matches
377 
378  // make sure the htmlTidy plugin knows about our special tag
379  if ($this->wysiwyg->plugin_loaded('html_tidy')) {
380  $html_tidy =& $this->wysiwyg->get_plugin('html_tidy');
381  $html_tidy->new_inline_tag('sq_wysiwyg_embed_youtube');
382  }
383 
384  }//end process()
385 
386 
395  function set_contents(&$contents)
396  {
397  // replace the special embedded tags with standard img tags
398  $contents = preg_replace('|(<sq_wysiwyg_embed_youtube([^>]*)?>)(.*?)(</sq_wysiwyg_embed_youtube>)|si', '<IMG src="'.$this->_img_url.'" $2>', $contents);
399  $contents = preg_replace('|\s+|', ' ', $contents);
400 
401  }//end set_contents()
402 
403 
404 }//end class
405 
406 ?>