1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Myron Turner <turnermm02@shaw.ca>
5 */
6
7// must be run within Dokuwiki
8if (!defined('DOKU_INC')) die();
9
10class helper_plugin_ckgdoku extends DokuWiki_Plugin {
11
12
13
14  function getMethods(){
15    $result = array();
16    $result[] = array(
17      'name'   => 'registerOnLoad',
18      'desc'   => 'register some javascript to the window.onload js event',
19      'params' => array('js' => 'string'),
20      'return' => array('html' => 'string'),
21    );
22    return $result;
23  }
24
25  /**
26   * Convert string configuration value into an array
27  */
28  function get_conf_array($val) {
29     $str = $this->getConf($val);
30     $str = preg_replace('/\s+/',"",$str);
31     return explode(',', $str);
32  }
33
34  function get_ckgdoku_ImageAllowedExtensions() {
35    $uploadImageTypes = ARRAY();
36    foreach (getMimeTypes() as $ext=>$mtype) {
37        if(preg_match("/image/", $mtype)) {
38           $uploadImageTypes[] = $ext;
39      }
40    }
41     return '.(' . implode('|',$uploadImageTypes) .')$';
42  }
43
44  function is_outOfScope(&$which="") {
45      if(isset($_REQUEST['target']) && $_REQUEST['target'] == 'plugin_data') return true;
46      return false;
47  }
48
49  function has_plugin($plugin) {
50           $plugins_list = plugin_list();
51      return in_array($plugin, $plugins_list);
52  }
53
54  function registerOnLoad($js){
55  global $ID;
56  global $lang;
57  global $skip_styling;
58  $preview_button = $lang['btn_preview'];
59  $ckgdoku_conf_direction = $this->getConf('direction');
60   if($ckgdoku_conf_direction == "dokuwiki") {
61       $ckgdoku_lang_direction = $lang['direction'];
62   }
63  else {
64      $ckgdoku_lang_direction = $ckgdoku_conf_direction;
65  }
66  $ImageUploadAllowedExtensions = $this->get_ckgdoku_ImageAllowedExtensions() ;
67  $media_tmp_ns = preg_match('/:/',$ID) ? preg_replace('/:\w+$/',"",$ID,1) : "";
68  $locktimer_msg = "Your lock for editing this page is about to expire in a minute.\\n"
69                . "You can reset the timer by clicking the Back-up button.";
70
71    $meta_fn = metaFN($ID,'.ckgdoku');
72    $meta_id = 'meta/' . str_replace(':','/',$ID) . '.ckgdoku';
73
74  global $INFO;
75  global $conf;
76  global $USERINFO;
77  $_OS = strtolower(PHP_OS);
78  $cname = getCacheName($INFO['client'].$ID,'.draft');
79  $useheading = $conf['useheading'];
80
81  if($useheading && $useheading != 'navigation') {
82       $useheading = 'y';
83  }
84  else $useheading = 'n';
85  //msg('uh='.$useheading);
86  $open_upload = $this->getConf('open_upload');
87  $editor_backup = $this->getConf('editor_bak');
88  $create_folder = $this->getConf('create_folder');
89  $interface_lang = $this->getConf('other_lang');
90  $scayt_lang = $this->getConf('scayt_lang');
91  list($name,$scayt_lang) = explode('/', $scayt_lang);
92
93  $scayt_auto = $this->getConf('scayt_auto');
94 $color_opts = $this->getConf('color_options');
95 $font_opts = $this->getConf('font_options');
96  $toolbar_opts = $this->getConf('alt_toolbar');
97 $mfiles =   $this->getConf('mfiles');
98 $extra_plugins = $this->getConf('extra_plugins');
99  $ckg_gui = $this->getConf('gui');
100  if(!isset($INFO['userinfo']) && !$open_upload) {
101    $user_type = 'visitor';
102  }
103  else {
104   $user_type = 'user';
105  }
106  $save_dir = DOKU_URL . ltrim($conf['savedir'],'/.\/');
107  $fbsz_increment = isset($_COOKIE['fbsz']) && $_COOKIE['fbsz'] ? $_COOKIE['fbsz'] : '0';
108  $use_pastebase64 = (isset($_COOKIE['ckgEdPaste']) && $_COOKIE['ckgEdPaste'] == 'on' )  ? 'on' : 'off';
109  // if no ACL is used always return upload rights
110  if($conf['useacl']) {
111     $client = $_SERVER['REMOTE_USER'];
112  }
113  else $client = "";
114  $user_name = $USERINFO['name'];
115  $user_email = $USERINFO['mail'];
116
117  $fnencode = isset($conf['fnencode']) ? $conf['fnencode'] : 'url';
118  $user_groups = $USERINFO['grps'];
119  if(!$user_groups) $user_groups = array();
120  if (@in_array("guest", $user_groups)) {
121     $create_folder = 'n';
122	 $user_type = 'visitor';
123  }
124  $user_groups = implode(";;",$user_groups);
125
126  if($INFO['isadmin'] || $INFO['ismanager']) {
127     $client = "";
128  }
129
130  $ver_anteater = mktime(0,0,0,11,7,2010);
131  $dwiki_version=mktime(0,0,0,01,01,2008);
132
133  if(isset($conf['fnencode'])) {
134      $ver_anteater = mktime(0,0,0,11,7,2010);
135      $dwiki_version=mktime(0,0,0,11,7,2010);
136  }
137  else if(function_exists('getVersionData')) {
138      $verdata= getVersionData();
139      if(isset($verdata) && preg_match('/(\d+)-(\d+)-(\d+)/',$verdata['date'],$ver_date)) {
140          if($ver_date[1] >= 2005 && ($ver_date[3] > 0 && $ver_date[3] < 31) && ($ver_date[2] > 0 && $ver_date[2] <= 12)) {
141                                          // month        day               year
142          $dwiki_version=@mktime(0,  0,  0, $ver_date[2],$ver_date[3], $ver_date[1]);
143          if(!$dwiki_version) $dwiki_version = mktime(0,0,0,01,01,2008);
144          $ver_anteater = mktime(0,0,0,11,7,2010);
145      }
146    }
147  }
148
149
150 $default_fb = $this->getConf('default_fb');
151 if($default_fb == 'none') {
152     $client = "";
153 }
154
155 $doku_base = DOKU_BASE;
156
157    return <<<end_of_string
158
159
160<script type='text/javascript'>
161 //<![CDATA[
162
163if(window.dw_locktimer) {
164   var locktimer = dw_locktimer;
165}
166var FCKRecovery = "";
167var oldonload = window.onload;
168var ourLockTimerINI = false;
169var oldBeforeunload;
170
171  var ckgdoku_onload = function() { $js };
172
173  jQuery(window).on('load',ckgdoku_onload);
174
175 function ckgdokuEditorTextChanged() {
176   window.textChanged = false;
177   oldBeforeunload();
178   if(window.dwfckTextChanged) {
179      return LANG.notsavedyet;
180   }
181 }
182
183  function getCurrentWikiNS() {
184        var DWikiMediaManagerCommand_ns = '$media_tmp_ns';
185        return DWikiMediaManagerCommand_ns;
186  }
187
188 var ourLockTimerRefreshID;
189 var ourLockTimerIsSet = true;
190 var ourLockTimerWarningtimerID;
191 var ourFCKEditorNode = null;
192 var ourLockTimerIntervalID;
193 var dwfckTextChanged = false;
194
195   /**
196    *    event handler
197    *    handles both mousepresses and keystrokes from FCKeditor window
198    *    assigned in fckeditor.html
199  */
200
201 function handlekeypress (e) {
202
203
204   if(ourLockTimerIsSet) {
205         lockTimerRefresh();
206   }
207   window.dwfckTextChanged = true;
208 }
209
210 function unsetDokuWikiLockTimer() {
211
212    if(window.locktimer && !ourLockTimerINI) {
213        locktimer.old_reset = locktimer.reset;
214        locktimer.old_warning = locktimer.warning;
215        ourLockTimerINI=true;
216    }
217    else {
218        window.setTimeout("unsetDokuWikiLockTimer()", 600);
219
220    }
221
222  locktimer.reset = function(){
223        locktimer.clear();  // alert(locktimer.timeout);
224        window.clearTimeout(ourLockTimerWarningtimerID);
225        ourLockTimerWarningtimerID =  window.setTimeout(function () { locktimer.warning(); }, locktimer.timeout);
226   };
227
228   locktimer.warning = function(){
229        window.clearTimeout(ourLockTimerWarningtimerID);
230
231        if(ourLockTimerIsSet) {
232            if(locktimer.msg.match(/$preview_button/i)) {
233                locktimer.msg = locktimer.msg.replace(/$preview_button/i, "Back-up");
234             }
235            alert(locktimer.msg);
236        }
237        else {
238            alert("$locktimer_msg");
239        }
240     };
241
242
243    locktimer.ourLockTimerReset = locktimer.reset;
244    locktimer.our_lasttime = new Date();
245    lockTimerRefresh();
246
247 }
248
249 function lockTimerRefresh(bak) {
250        var now = new Date();
251        if(!ourLockTimerINI)  unsetDokuWikiLockTimer();
252
253        if((now.getTime() - locktimer.our_lasttime.getTime() > 45*1000) || bak){
254           var dwform = GetE('dw__editform');
255            window.clearTimeout(ourLockTimerWarningtimerID);
256            var params = 'call=lock&id='+locktimer.pageid;
257            if(CKEDITOR.instances) {
258                dwform.elements.wikitext.value = CKEDITOR.instances.wiki__text.getData();
259                params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value);
260                params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value);
261                params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value);
262                params += '&date='+encodeURIComponent(dwform.elements.date.value);
263            }
264            locktimer.our_lasttime = now;
265            jQuery.post(
266                DOKU_BASE + 'lib/exe/ajax.php',
267                params,
268                function (data) {
269                    data = data.replace(/auto/,"")  + ' by ckgdoku';
270                    locktimer.response = data;
271                    locktimer.refreshed(data);
272                },
273                'html'
274            );
275       }
276
277 }
278
279
280 /**
281   Legacy function has no current use
282 */
283 function getRecoveredText() {
284    return FCKRecovery;
285 }
286
287 function resetDokuWikiLockTimer(delete_checkbox) {
288
289        var dom_checkbox = document.getElementById('ckgdoku_timer');
290        var dom_label = document.getElementById('ckgdoku_timer_label');
291        locktimer.clear();
292        if(ourLockTimerIsSet) {
293
294             ourLockTimerIsSet = false;
295             locktimer.reset = locktimer.old_reset;
296             locktimer.refresh();
297             return;
298        }
299
300     if(delete_checkbox) {
301       dom_checkbox.style.display = 'none';
302       dom_label.style.display = 'none';
303     }
304
305       ourLockTimerIsSet = true;
306       locktimer.reset = locktimer.ourLockTimerReset;
307       lockTimerRefresh();
308
309 }
310
311/* Renews lock and  creates a ckgdoku backup if editor_bak config option is true */
312function renewLock(bak) {
313  if(ourLockTimerIsSet) {
314         lockTimerRefresh(true);
315   }
316   else {
317    locktimer.refresh();
318   }
319   locktimer.reset();
320
321
322    if(bak) {
323        var id = "$ID";
324        parse_wikitext('bakup');
325
326        var dwform = GetE('dw__editform');
327        if(dwform.elements.fck_wikitext.value == '__false__' ) return;
328         GetE('saved_wiki_html').innerHTML = CKEDITOR.instances.wiki__text.getData(); // ourFCKEditorNode.innerHTML;
329        if(($editor_backup) == 0 ) {
330           return;
331        }
332
333        var params = "rsave_id=" + encodeURIComponent("$meta_fn");
334        params += '&wikitext='+encodeURIComponent(dwform.elements.fck_wikitext.value);
335        params += '&call=refresh_save';
336        jQuery.post(
337           //     DOKU_BASE + 'lib/plugins/ckgdoku/scripts/refresh_save.php',
338               DOKU_BASE + 'lib/exe/ajax.php',
339                params,
340                function (data) {
341                    if(data == 'done') {
342                        show_backup_msg("$meta_id");
343                    }
344                    else {
345                      alert("error saving: " + id);
346                    }
347                },
348                'html'
349            );
350    }
351
352}
353
354
355function revert_to_prev() {
356  if(!(GetE('saved_wiki_html').innerHTML.length)) {
357            if(!confirm(backup_empty)) {
358                           return;
359            }
360  }
361  CKEDITOR.instances.wiki__text.setData( GetE('saved_wiki_html').innerHTML);
362   window.dwfckTextChanged = true;
363}
364
365
366function draft_delete() {
367
368        var debug = false;
369        var params = "draft_id=$cname";
370        jQuery.ajax({
371           url: DOKU_BASE + 'lib/plugins/ckgdoku/scripts/draft_delete.php',
372           //async: false,
373           data: params,
374           type: 'POST',
375           dataType: 'html',
376           success: function(data){
377               if(debug) {
378                  alert(data);
379               }
380
381    }
382    });
383
384}
385
386function disableDokuWikiLockTimer() {
387  resetDokuWikiLockTimer(false);
388  if(ourLockTimerIntervalID) {
389     window.clearInterval(ourLockTimerIntervalID);
390  }
391  if(ourLockTimerIsSet) {
392    ourLockTimerIntervalID = window.setInterval(function () { locktimer.refresh(); }, 30000);
393  }
394}
395
396function dwfckKeypressInstallHandler() {
397  if(window.addEventListener){
398      oDokuWiki_FCKEditorInstance.EditorDocument.addEventListener('keyup', handlekeypress , false) ;
399  }
400  else {
401     oDokuWiki_FCKEditorInstance.EditorDocument.attachEvent('onkeyup', handlekeypress ) ;
402  }
403}
404
405var DWFCK_EditorWinObj;
406function FCKEditorWindowObj(w) {
407  DWFCK_EditorWinObj = w;
408}
409
410function ckgdoku_isRTL() {
411var direction = "$ckgdoku_lang_direction";
412
413return direction == 'rtl';
414
415}
416
417function remove_styling() {
418//'TextColor,BGColor, FontAssist,Font,FontSize';
419var opts = "";
420var color_opts = parseInt( "$color_opts");
421var font_opts =  parseInt("$font_opts");
422var skip_styling=parseInt("$skip_styling");
423if(color_opts) {
424  opts ='TextColor,BGColor,FontAssist';
425}
426else if(!skip_styling) {
427     opts = 'FontAssist';
428}
429if(font_opts) {
430  if(color_opts || !skip_styling) opts+=',';
431  opts +='Font,FontSize';
432}
433if("$toolbar_opts") {
434  if(opts) opts+=',';
435  opts+="$toolbar_opts";
436}
437
438return opts;
439
440}
441
442function  extra_plugins(config) {
443    if("$use_pastebase64" == 'on')  config.addPaste();
444    return "$extra_plugins";
445}
446
447function ckgdoku_language_chk(config) {
448    if("$scayt_auto" == 'on') {
449        config.scayt_autoStartup = true;
450    }
451    else config.scayt_autoStartup = false;
452    if("$scayt_auto" == 'disable') {
453        config.scayt__disable = true;
454    }
455    config.scayt_sLang="$scayt_lang";
456   var lang = "$interface_lang";
457   if(lang ==  'default') return; ;
458   config.language = lang;
459}
460
461function getCKEditorGUI() {
462    return "$ckg_gui";
463}
464var oDokuWiki_FCKEditorInstance;
465function FCKeditor_OnComplete( editorInstance )
466{
467
468  oDokuWiki_FCKEditorInstance = editorInstance;
469  editorInstance.on( 'key', handlekeypress, editorInstance );
470
471  CKEDITOR.instances.wiki__text.on('change', function(event) {
472        window.dwfckTextChanged = true;
473  });
474
475  editorInstance.on("focus", function(e) {
476          window.dwfckTextChanged = true;
477    });
478
479  oDokuWiki_FCKEditorInstance.dwiki_user = "$user_type";
480  oDokuWiki_FCKEditorInstance.dwiki_client = "$client";
481  oDokuWiki_FCKEditorInstance.dwiki_usergroups = "$user_groups";
482  oDokuWiki_FCKEditorInstance.dwiki_doku_base = "$doku_base";
483  oDokuWiki_FCKEditorInstance.dwiki_create_folder = "$create_folder";
484  oDokuWiki_FCKEditorInstance.dwiki_fnencode = "$fnencode";
485  oDokuWiki_FCKEditorInstance.dwiki_version = $dwiki_version;
486  oDokuWiki_FCKEditorInstance.dwiki_anteater = $ver_anteater;
487  oDokuWiki_FCKEditorInstance.isLocalDwikiBrowser = false;
488  oDokuWiki_FCKEditorInstance.isUrlExtern = false;
489  oDokuWiki_FCKEditorInstance.isDwikiMediaFile = false;
490  oDokuWiki_FCKEditorInstance.imageUploadAllowedExtensions="$ImageUploadAllowedExtensions";
491  oDokuWiki_FCKEditorInstance.fckgUserName = "$user_name";
492  oDokuWiki_FCKEditorInstance.fckgUserMail="$user_email";
493  oDokuWiki_FCKEditorInstance.useheading = "$useheading";
494  oDokuWiki_FCKEditorInstance.mfiles = parseInt("$mfiles");
495  oDokuWiki_FCKEditorInstance.fbsz_increment=parseInt("$fbsz_increment");
496
497  var index = navigator.userAgent.indexOf('Safari');
498  if(index == -1  || (navigator.userAgent.indexOf('Chrome'))) {
499    oldBeforeunload = window.onbeforeunload;
500    window.onbeforeunload = ckgdokuEditorTextChanged;
501  }
502
503
504}
505
506
507 var DWikifnEncode = "$fnencode";
508
509/* Make sure that show buttons in top and/or bottom clear the fckl file */
510 function get_showButtons() {
511	var inputs = document.getElementsByTagName('input');
512
513     for(var i=0; i<inputs.length; i++) {
514        if(inputs[i].type && inputs[i].type.match(/submit/i)) {
515			if(inputs[i].value.match(/Show/i) || (inputs[i].form &&  inputs[i].form.className.match(/btn_show/) ) )
516    			inputs[i].onmouseup = draft_delete;
517        }
518     }
519  }
520
521 /* make sure the entire page has been loaded */
522 setTimeout("get_showButtons()", 3000);
523 //]]>
524
525</script>
526end_of_string;
527  }
528}
529?>
530