Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
wysiwyg.inc
1 <?php
18 require_once SQ_ATTRIBUTES_PATH.'/text/text.inc';
19 require_once SQ_FUDGE_PATH.'/wysiwyg/wysiwyg.inc';
20 
36 {
37 
38 
46  function Asset_Attribute_WYSIWYG($attributeid=0, $value=null)
47  {
48  $this->Asset_Attribute_Text($attributeid, $value);
49 
50  // Get the WYSIWYG style from the global preference as the default settings
51  $wysiwyg_style = $GLOBALS['SQ_SYSTEM']->getUserPrefs('content_type_wysiwyg', 'SQ_WYSIWYG_STYLE');
52  $size_unit = '';
53  if (is_numeric($wysiwyg_style['width']) && isset($wysiwyg_style['units']['width'])) {
54  $size_unit = $wysiwyg_style['units']['width'];
55  }
56 
57  // set default edit parameters
58  $this->_edit_params['style'] = $wysiwyg_style['body_type'];
59  $this->_edit_params['width'] = $wysiwyg_style['width'].$size_unit;
60  $this->_edit_params['height'] = $wysiwyg_style['height'].$size_unit;
61  $this->_edit_params['tidy_status'] = 'wait';
62  $this->_edit_params['tidy_errors'] = '';
63 
64  }//end constructor
65 
66 
75  function setEditParams(&$node)
76  {
77  if (!parent::setEditParams($node)) return false;
78 
79  if (isset($node->attributes()->style)) $this->_edit_params['style'] = (string)$node->attributes()->style;
80  if (isset($node->attributes()->width)) $this->_edit_params['width'] = (string)$node->attributes()->width;
81  if (isset($node->attributes()->height)) $this->_edit_params['height'] = (string)$node->attributes()->height;
82 
83  return true;
84 
85  }//end setEditParams()
86 
87 
96  function &_createEditor($prefix)
97  {
98  // allowed plugins (shortened to $ap for easier use below)
99  $ap = explode('|', $GLOBALS['SQ_SYSTEM']->getUserPrefs('content_type_wysiwyg', 'SQ_WYSIWYG_PLUGINS'));
100  // ignore allowed plugins (shortened to $iap for easier use);
101  $iap = in_array('all', $ap);
102 
103  $wysiwyg = new wysiwyg($prefix, sq_web_path('fudge').'/wysiwyg/');
104 
105  $wysiwyg->set_body_type($this->_edit_params['style']);
106  $wysiwyg->set_init_onload(false);
107  $wysiwyg->set_show_status_bar(false);
108  $wysiwyg->set_width($this->_edit_params['width']);
109  $wysiwyg->set_height($this->_edit_params['height']);
110 
111  // Bug fix #5092 Asset Builder insert link via WYSIWYG removing the dot (.) before the /?a=
112  // remove ANY suffix from our URL or ele the regex working on it will result in malformed output
113  $base_php_self = str_replace('/'.SQ_CONF_BACKEND_SUFFIX, '', $_SERVER['PHP_SELF']);
114  $base_php_self = str_replace('/'.SQ_CONF_LIMBO_SUFFIX, '', $base_php_self);
115  $base_php_self = str_replace('/'.SQ_CONF_NOCACHE_SUFFIX, '', $base_php_self);
116  $base_php_self = str_replace('/'.SQ_CONF_RECACHE_SUFFIX, '', $base_php_self);
117  $base_php_self = rtrim($base_php_self, '/ ');
118 
119  $wysiwyg->add_relative_href_check('(http[s]?://'.$_SERVER['HTTP_HOST'].')'.$_SERVER['PHP_SELF'], '$1'.$base_php_self);
120  $wysiwyg->add_relative_href_check('http[s]?://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?(\?a=[0-9]+)', './$1');
121  $wysiwyg->add_relative_href_check('([^.][^/])(\?a=[0-9]+)', '$1./$2');
122  $wysiwyg->add_relative_href_check('http[s]?://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?%([a-zA-Z_\-0-9]+)%', '%$1%');
123  $wysiwyg->add_absolute_url_check('\./\?(a=[0-9]+)', 'http://'.$_SERVER['HTTP_HOST'].$base_php_self.'/?$1');
124 
125  // get the keyword replacements
126  $em = $GLOBALS['SQ_SYSTEM']->getEventManager();
127  $keywords = Array();
128  $keyword_vars = Array('keywords' => &$keywords);
129  $em->broadcastEvent($asset, 'requestKeywords', $keyword_vars);
130 
131  $wysiwyg->open_group();
132  if ($iap || in_array('format_text', $ap)) {
133  $wysiwyg->add_plugin('format_text');
134  }
135  if ($iap || in_array('replace_text', $ap)) {
136  $wysiwyg->add_plugin('replace_text');
137  }
138  if ($iap || in_array('search_and_replace', $ap)) {
139  $wysiwyg->add_plugin('search_and_replace');
140  }
141  if ($iap || in_array('format_font', $ap)) {
142  $wysiwyg->add_plugin('format_font');
143  }
144  if ($iap || in_array('general', $ap)) {
145  $wysiwyg->add_plugin('general');
146  }
147  if ($iap || in_array('s_script', $ap)) {
148  $wysiwyg->add_plugin('s_script');
149  }
150  $wysiwyg->close_group();
151 
152  $wysiwyg->open_group();
153  if ($iap || in_array('matrix_apply_style', $ap)) {
154  $wysiwyg->add_plugin('matrix_apply_style');
155  }
156  $wysiwyg->close_group();
157 
158  if ($iap || in_array('justify_text', $ap)) {
159  $wysiwyg->add_plugin('justify_text');
160  }
161  if (($iap || in_array('keyword_replace', $ap)) && !empty($keywords)) {
162  $wysiwyg->add_plugin('keyword_replace');
163  $keyword_replace =& $wysiwyg->get_plugin('keyword_replace');
164  $keyword_replace->keyword_tag = '%';
165  $keyword_replace->keywords = $keywords;
166  }
167 
168  // Snippet global keyword dropdown
169  // However if in frontend do not include this plugin, as it will output global keywords
170  $snippets = $GLOBALS['SQ_SYSTEM']->am->getSnippetKeywords();
171  if ((SQ_IN_BACKEND || SQ_IN_LIMBO) && ($iap || in_array('snippet_keyword_replace', $ap)) && !empty($snippets)) {
172  $wysiwyg->add_plugin('snippet_keyword_replace');
173  $snippet_keyword_replace =& $wysiwyg->get_plugin('snippet_keyword_replace');
174  $snippet_keyword_replace->keyword_tag = '%';
175  $snippet_keyword_replace->keywords = $snippets;
176  }
177 
178  if ($iap || in_array('history', $ap)) {
179  $wysiwyg->add_plugin('history');
180  }
181  if ($iap || in_array('indenting', $ap)) {
182  $wysiwyg->add_plugin('indenting');
183  }
184  if ($iap || in_array('listing', $ap)) {
185  $wysiwyg->add_plugin('listing');
186  }
187  if ($iap || in_array('table_editing', $ap)) {
188  $wysiwyg->add_plugin('table_editing');
189  }
190  if ($iap || in_array('select_color', $ap)) {
191  $wysiwyg->add_plugin('select_color');
192  }
193 
194  $wysiwyg->open_group();
195  if ($iap || in_array('matrix_insert_link', $ap) || in_array('insert_link', $ap)) {
196  $wysiwyg->add_plugin('matrix_insert_link');
197  }
198  if ($iap || in_array('matrix_insert_quick_link', $ap)) {
199  $wysiwyg->add_plugin('matrix_insert_quick_link');
200  }
201  if ($iap || in_array('matrix_insert_anchor', $ap) || in_array('insert_anchor', $ap)) {
202  $wysiwyg->add_plugin('matrix_insert_anchor');
203  }
204  if ($iap || in_array('matrix_insert_image', $ap) || in_array('insert_image', $ap)) {
205  $wysiwyg->add_plugin('matrix_insert_image');
206  }
207  if ($iap || in_array('matrix_embed_movie', $ap)) {
208  $wysiwyg->add_plugin('matrix_embed_movie');
209  }
210  if ($iap || in_array('matrix_embed_youtube', $ap)) {
211  $wysiwyg->add_plugin('matrix_embed_youtube');
212  }
213  $wysiwyg->close_group();
214 
215  $wysiwyg->open_group();
216  if ($iap || in_array('matrix_accessibility', $ap)) {
217  $wysiwyg->add_plugin('matrix_accessibility');
218  }
219  $wysiwyg->close_group();
220 
221  // html_tidy needs to be last so other plugins can set options
222  if ($iap || in_array('html_tidy', $ap)) {
223  $wysiwyg->add_plugin('html_tidy');
224  }
225 
226  if ($iap || in_array('matrix_expand_keywords', $ap)) {
227  $wysiwyg->add_plugin('matrix_expand_keywords');
228  }
229 
230  $wysiwyg->open_group();
231  if ($iap || in_array('special_chars', $ap)) {
232  $wysiwyg->add_plugin('special_chars');
233  }
234  $wysiwyg->close_group();
235 
236  $wysiwyg->open_group();
237  if ($iap || in_array('choose_language', $ap)) {
238  $wysiwyg->add_plugin('choose_language');
239  }
240  $wysiwyg->close_group();
241 
242  $wysiwyg->open_group();
243  if ($iap || in_array('definition_list', $ap)) {
244  $wysiwyg->add_plugin('definition_list');
245  }
246  $wysiwyg->close_group();
247 
248  $wysiwyg->open_group();
249  if ($iap || in_array('misc_accessibility', $ap)) {
250  $wysiwyg->add_plugin('misc_accessibility');
251  }
252  if ($iap || in_array('quotation', $ap)) {
253  $wysiwyg->add_plugin('quotation');
254  }
255  $wysiwyg->close_group();
256 
257  $wysiwyg->open_group();
258  if ($iap || in_array('visual_aid', $ap)) {
259  $wysiwyg->add_plugin('visual_aid');
260  }
261  $wysiwyg->close_group();
262 
263  $wysiwyg->open_group();
264  if ($iap || in_array('spell_checker', $ap)) {
265  $wysiwyg->add_plugin('spell_checker');
266  }
267  if ($iap || in_array('text_indicator', $ap)) {
268  $wysiwyg->add_plugin('text_indicator');
269  }
270  if ($iap || in_array('view_source', $ap)) {
271  $wysiwyg->add_plugin('view_source');
272  }
273  $wysiwyg->close_group();
274 
275  $wysiwyg->set_contents($this->value);
276 
277  return $wysiwyg;
278 
279  }//end _createEditor()
280 
281 
290  function paint($prefix, $read_only=false)
291  {
292  if ($read_only) {
293  echo $this->value;
294  return;
295  }
296 
297  $wysiwyg = $this->_createEditor($prefix);
298 
299  if (!(SQ_IN_BACKEND || SQ_IN_LIMBO)) echo '<script type="text/javascript" src="'.sq_web_path('lib').'/js/edit.js"></script>';
300 
301  ?>
302  <script type="text/javascript">
303  var bodycopy_initialised = true;
304  </script>
305  <div style="border: 1px dashed #C0C0C0;" id="<?php echo $prefix.'_switch_option_div'; ?>">
306  <table width="100%">
307  <tr>
308  <td align="left">
309  <div style="width: 16px; height: 16px; background: url(<?php echo sq_web_path('lib')?>/web/images/icons/edit_mode.png);background: expression('none');filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='<?php echo sq_web_path('lib')?>/web/images/icons/edit_mode.png', sizingMethod='scale'); font-size:10px; cursor:pointer;"
310  onclick="switchEditingMode('<?php echo $prefix; ?>_contents_div','<?php echo $prefix; ?>_wysiwyg_div', editor_<?php echo $prefix; ?>);">
311  </div>
312  </td>
313  <td align="right">
314  <?php echo $this->paintBackendStatusIcons($wysiwyg); ?>
315  </td>
316  </tr>
317  </table>
318  </div>
319  <div style="border-left: 1px dashed #C0C0C0; border-right: 1px dashed #C0C0C0; border-bottom: 1px dashed #C0C0C0;">
320  <div style="display:none;" id="<?php echo $prefix?>_wysiwyg_div"><?php echo $wysiwyg->paint()?></div>
321  <div id="<?php echo $prefix?>_contents_div" onclick="editDiv=document.getElementById('<?php echo $prefix ?>_wysiwyg_div'); if (editDiv.style.display == 'none') {switchEditingMode('<?php echo $prefix ?>_contents_div','<?php echo $prefix ?>_wysiwyg_div', editor_<?php echo $prefix ?>);}"><?php echo $this->value?></div>
322  </div>
323  <?php
324 
325  }//end paint()
326 
327 
337  function process($prefix, $owner_id=NULL, $cmp_value=TRUE)
338  {
339  $wysiwyg = $this->_createEditor($prefix);
340 
341  $html = $wysiwyg->process();
342 
343  $this->processed = FALSE;
344  if ($html !== FALSE && (!$cmp_value || $html !== $this->value) && $this->setValue($html, $owner_id)) {
345  $this->processed = TRUE;
346  }
347  $this->_edit_params['tidy_status'] = isset($wysiwyg->_loaded_plugins['html_tidy']->htmltidy_status) ? $wysiwyg->_loaded_plugins['html_tidy']->htmltidy_status : 'wait';
348  $this->_edit_params['tidy_errors'] = isset($wysiwyg->_loaded_plugins['html_tidy']->htmltidy_errors) ? $wysiwyg->_loaded_plugins['html_tidy']->htmltidy_errors : '';
349 
350  }//end process()
351 
352 
361  function getKeywordValue($format='')
362  {
363  if (!in_array($format, Array('', 'disableurls', 'disablekeywords', 'disableall'))) {
364  return '';
365  }
366 
367  $output = $this->value;
368 
369  if (!in_array($format, Array('disableurls', 'disableall'))) {
370  $matches = Array();
371  preg_match_all('|\\./\\?a=([0-9]+)([^0-9:])|', $output, $matches);
372  $normal_assetids = empty($matches[1]) ? Array() : $matches[1];
373  $matches = Array();
374  preg_match_all('|\\./\\?a=([0-9]+:[^$]*)\\$|', $output, $matches, PREG_PATTERN_ORDER);
375  $shadow_assetids = empty($matches[1]) ? Array() : $matches[1];
376  $assetids = array_unique(array_merge($normal_assetids, $shadow_assetids));
377  $urls = $GLOBALS['SQ_SYSTEM']->am->getAssetUrl($assetids);
378  $output = $this->value;
379  foreach ($urls as $id => $url) {
380  if (FALSE === strpos($id, ':')) {
381  if (!empty($url)) {
382  $output = str_replace('./?a='.$id, $url, $output);
383  }
384  } else {
385  if (!empty($url)) {
386  $output = str_replace('./?a='.$id.'$', $url, $output);
387  } else {
388  $output = str_replace('./?a='.$id.'$', './?a='.$id, $output);
389  }
390  }
391  }
392  }
393 
394  if (in_array($format, Array('disablekeywords', 'disableall'))) {
395  // Simulate 'disable keywords' from bodycopy_container
396  // by encoding %s
397  $keywords = retrieve_keywords_replacements($output);
398  foreach ($keywords as $keyword) {
399  $output = str_replace("%$keyword%", "&#37;{$keyword}&#37;", $output);
400  }
401  }
402 
403  return $output;
404 
405  }//end getKeywordValue()
406 
407 
417  function setValue(&$value, $owner_id = NULL)
418  {
419  if (!empty($owner_id)) {
420  $live_link_only = $GLOBALS['SQ_SYSTEM']->getUserPrefs('content_type_wysiwyg', 'SQ_LIVE_LINK_ONLY');
421  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($owner_id);
422  if ($asset_info[$owner_id]['status'] == SQ_STATUS_LIVE && $live_link_only) {
423  require_once SQ_FUDGE_PATH.'/general/www.inc';
424  $outcome = check_non_live_link($value, $owner_id);
425  if ($outcome !== FALSE) $value = $outcome;
426  }
427  }
428 
429  return parent::setValue($value);
430 
431  }//end setValue()
432 
433 
443  public function paintBackendStatusIcons($wysiwyg)
444  {
445  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
446  require_once SQ_CORE_PACKAGE_PATH.'/bodycopy/bodycopy/bodycopy_edit_fns.inc';
447 
448  // prints the status of the last run of htmltidy tool as a tag icon
449  if (!SQ_TOOL_HTML_TIDY_ENABLED || !file_exists(SQ_TOOL_HTML_TIDY_PATH)) {
450  // tool is not enabled
451  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Status', translate('wysiwyg_htmltidy_status_disabled'), 'htmltidy_disabled', '[HTMLTidy Status]');
452  } else {
453 
454  $status = $this->_edit_params['tidy_status'];
455  $status_msg = translate('wysiwyg_htmltidy_status_wait');
456  $image_filename = 'htmltidy_'.$status;
457  switch ($status) {
458  case 'pass':
459  $status_msg = translate('wysiwyg_htmltidy_status_pass');
460  break;
461  case 'fail':
462  $status_msg = translate('wysiwyg_htmltidy_status_fail');
463  break;
464  case 'wait':
465  $status_msg = translate('wysiwyg_htmltidy_status_wait');
466  break;
467  }//end switch
468 
469  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Status', $status_msg, $image_filename, '[HTMLTidy Status]');
470 
471  $res = (str_replace('
472 ', '<br />', htmlentities($this->_edit_params['tidy_errors'], ENT_COMPAT, SQ_CONF_DEFAULT_CHARACTER_SET)));
473 
474  $htmltidy_accessibility_filename = 'accessibility_'.(empty($res) ? 'pass' : 'fail');
475  Bodycopy_Edit_Fns::printBodycopyIcon('', 'HTMLTidy Accessibility', !empty($res) ? $res : translate('wysiwyg_htmltidy_accessibility_no_warning'), $htmltidy_accessibility_filename, '[HTMLTidy Status]');
476 
477  }
478 
479  }//end paintBackendStatusIcons()
480 
481 
482 }//end class
483 
484 ?>