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_fckg 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 registerOnLoad($js){
35  global $ID;
36  global $lang;
37  $preview_button = $lang['btn_preview'];
38
39  $fckg_conf_direction = $this->getConf('direction');
40   if($fckg_conf_direction == "dokuwiki") {
41       $fckg_lang_direction = $lang['direction'];
42   }
43  else {
44      $fckg_lang_direction = $fckg_conf_direction;
45  }
46
47  $media_tmp_ns = preg_match('/:/',$ID) ? preg_replace('/:\w+$/',"",$ID,1) : "";
48  $locktimer_msg = "Your lock for editing this page is about to expire in a minute.\\n"
49                . "You can reset the timer by clicking the Back-up button.";
50
51    $meta_fn = metaFN($ID,'.fckg');
52    $meta_id = 'meta/' . str_replace(':','/',$ID) . '.fckg';
53
54  global $INFO;
55  global $conf;
56  global $USERINFO;
57  $_OS = strtolower(PHP_OS);
58  $cname = getCacheName($INFO['client'].$ID,'.draft');
59  $open_upload = $this->getConf('open_upload');
60  $editor_backup = $this->getConf('editor_bak');
61  $create_folder = $this->getConf('create_folder');
62  if(!isset($INFO['userinfo']) && !$open_upload) {
63    $user_type = 'visitor';
64  }
65  else {
66   $user_type = 'user';
67  }
68
69  // if no ACL is used always return upload rights
70  if($conf['useacl']) {
71     $client = $_SERVER['REMOTE_USER'];
72  }
73  else $client = "";
74
75  $fnencode = isset($conf['fnencode']) ? $conf['fnencode'] : 'url';
76  $user_groups = $USERINFO['grps'];
77  if(!$user_groups) $user_groups = array();
78  if (@in_array("guest", $user_groups)) {
79     $create_folder = 'n';
80	 $user_type = 'visitor';
81  }
82  $user_groups = implode(";;",$user_groups);
83
84  if($INFO['isadmin'] || $INFO['ismanager']) {
85     $client = "";
86  }
87  $user_name = $USERINFO['name'];
88  $user_email = $USERINFO['mail'];
89  $ver_anteater = mktime(0,0,0,11,7,2010);
90  $dwiki_version=mktime(0,0,0,01,01,2008);
91
92  if(isset($conf['fnencode'])) {
93      $ver_anteater = mktime(0,0,0,11,7,2010);
94      $dwiki_version=mktime(0,0,0,11,7,2010);
95  }
96  else if(function_exists('getVersionData')) {
97      $verdata= getVersionData();
98      if(isset($verdata) && preg_match('/(\d+)-(\d+)-(\d+)/',$verdata['date'],$ver_date)) {
99          if($ver_date[1] >= 2005 && ($ver_date[3] > 0 && $ver_date[3] < 31) && ($ver_date[2] > 0 && $ver_date[2] <= 12)) {
100                                          // month        day               year
101          $dwiki_version=@mktime(0,  0,  0, $ver_date[2],$ver_date[3], $ver_date[1]);
102          if(!$dwiki_version) $dwiki_version = mktime(0,0,0,01,01,2008);
103          $ver_anteater = mktime(0,0,0,11,7,2010);
104      }
105    }
106  }
107
108
109 $default_fb = $this->getConf('default_fb');
110 if($default_fb == 'none') {
111     $client = "";
112 }
113
114 $doku_base = DOKU_BASE;
115
116    return <<<end_of_string
117
118
119<script type='text/javascript'>
120 //<![CDATA[
121
122if(window.dw_locktimer) {
123   var locktimer = dw_locktimer;
124}
125var FCKRecovery = "";
126var oldonload = window.onload;
127var ourLockTimerINI = false;
128var oldBeforeunload;
129
130  var fckg_onload = function() { $js };
131
132  jQuery(window).bind('load',{},fckg_onload);
133
134   function fckgEditorTextChanged() {
135   window.textChanged = false;
136   oldBeforeunload();
137   if(window.dwfckTextChanged) {
138      return LANG.notsavedyet;
139   }
140 }
141
142  function getCurrentWikiNS() {
143        var DWikiMediaManagerCommand_ns = '$media_tmp_ns';
144        return DWikiMediaManagerCommand_ns;
145  }
146
147 var ourLockTimerRefreshID;
148 var ourLockTimerIsSet = true;
149 var ourLockTimerWarningtimerID;
150 var ourFCKEditorNode = null;
151 var ourLockTimerIntervalID;
152 var dwfckTextChanged = false;
153
154   /**
155    *    event handler
156    *    handles both mousepresses and keystrokes from FCKeditor window
157    *    assigned in fckeditor.html
158  */
159 function handlekeypress (e) {
160    //alert(e);
161   if(ourLockTimerIsSet) {
162         lockTimerRefresh();
163   }
164   window.dwfckTextChanged = true;
165 }
166
167 function unsetDokuWikiLockTimer() {
168
169    if(window.locktimer && !ourLockTimerINI) {
170        locktimer.old_reset = locktimer.reset;
171        locktimer.old_warning = locktimer.warning;
172        ourLockTimerINI=true;
173    }
174    else {
175        window.setTimeout("unsetDokuWikiLockTimer()", 600);
176
177    }
178
179  locktimer.reset = function(){
180        locktimer.clear();  // alert(locktimer.timeout);
181        window.clearTimeout(ourLockTimerWarningtimerID);
182        ourLockTimerWarningtimerID =  window.setTimeout(function () { locktimer.warning(); }, locktimer.timeout);
183   };
184
185   locktimer.warning = function(){
186        window.clearTimeout(ourLockTimerWarningtimerID);
187
188        if(ourLockTimerIsSet) {
189            if(locktimer.msg.match(/$preview_button/i)) {
190                locktimer.msg = locktimer.msg.replace(/$preview_button/i, "Back-up");
191             }
192            alert(locktimer.msg);
193        }
194        else {
195            alert("$locktimer_msg");
196        }
197     };
198
199
200    locktimer.ourLockTimerReset = locktimer.reset;
201    locktimer.our_lasttime = new Date();
202    lockTimerRefresh();
203
204 }
205
206 function lockTimerRefresh(bak) {
207        var now = new Date();
208        if(!ourLockTimerINI)  unsetDokuWikiLockTimer();
209
210        if((now.getTime() - locktimer.our_lasttime.getTime() > 45*1000) || bak){
211           var dwform = GetE('dw__editform');
212            window.clearTimeout(ourLockTimerWarningtimerID);
213            var params = 'call=lock&id='+locktimer.pageid;
214            if(ourFCKEditorNode) {
215                dwform.elements.wikitext.value = ourFCKEditorNode.innerHTML;
216                params += '&prefix='+encodeURIComponent(dwform.elements.prefix.value);
217                params += '&wikitext='+encodeURIComponent(dwform.elements.wikitext.value);
218                params += '&suffix='+encodeURIComponent(dwform.elements.suffix.value);
219                params += '&date='+encodeURIComponent(dwform.elements.date.value);
220            }
221            locktimer.our_lasttime = now;
222            jQuery.post(
223                DOKU_BASE + 'lib/exe/ajax.php',
224                params,
225                function (data) {
226                    data = data.replace(/auto/,"")  + ' by fckgLite';
227                    locktimer.response = data;
228                    locktimer.refreshed(data);
229                },
230                'html'
231            );
232       }
233
234 }
235
236
237 /**
238   Legacy function has no current use
239 */
240 function getRecoveredText() {
241    return FCKRecovery;
242 }
243
244 function resetDokuWikiLockTimer(delete_checkbox) {
245
246        var dom_checkbox = document.getElementById('fckg_timer');
247        var dom_label = document.getElementById('fckg_timer_label');
248        locktimer.clear();
249        if(ourLockTimerIsSet) {
250
251             ourLockTimerIsSet = false;
252             locktimer.reset = locktimer.old_reset;
253             locktimer.refresh();
254             return;
255        }
256
257     if(delete_checkbox) {
258       dom_checkbox.style.display = 'none';
259       dom_label.style.display = 'none';
260     }
261
262       ourLockTimerIsSet = true;
263       locktimer.reset = locktimer.ourLockTimerReset;
264       lockTimerRefresh();
265
266 }
267
268
269function renewLock(bak) {
270  if(ourLockTimerIsSet) {
271         lockTimerRefresh(true);
272   }
273   else {
274    locktimer.refresh();
275   }
276   locktimer.reset();
277
278
279    if(bak) {
280        var id = "$ID";
281        parse_wikitext('bakup');
282
283        var dwform = GetE('dw__editform');
284        if(dwform.elements.fck_wikitext.value == '__false__' ) return;
285        GetE('saved_wiki_html').innerHTML = ourFCKEditorNode.innerHTML;
286        if(($editor_backup) == 0 ) {
287           return;
288        }
289
290        var params = "rsave_id=$meta_fn";
291        params += '&wikitext='+encodeURIComponent(dwform.elements.fck_wikitext.value);
292        jQuery.post(
293                DOKU_BASE + 'lib/plugins/fckg/scripts/refresh_save.php',
294                params,
295                function (data) {
296                    if(data == 'done') {
297                        show_backup_msg("$meta_id");
298                    }
299                    else {
300                      alert("error saving: " + id);
301                    }
302                },
303                'html'
304            );
305    }
306
307}
308
309
310function revert_to_prev() {
311  if(!(GetE('saved_wiki_html').innerHTML.length)) {
312            if(!confirm(backup_empty)) {
313                           return;
314            }
315  }
316    ourFCKEditorNode.innerHTML = GetE('saved_wiki_html').innerHTML;
317}
318
319
320function draft_delete() {
321
322        var debug = false;
323        var params = "draft_id=$cname";
324        jQuery.ajax({
325           url: DOKU_BASE + 'lib/plugins/fckg/scripts/draft_delete.php',
326           async: false,
327           data: params,
328           type: 'POST',
329           dataType: 'html',
330           success: function(data){
331               if(debug) {
332                  alert(data);
333               }
334
335    }
336    });
337
338}
339
340function disableDokuWikiLockTimer() {
341  resetDokuWikiLockTimer(false);
342  if(ourLockTimerIntervalID) {
343     window.clearInterval(ourLockTimerIntervalID);
344  }
345  if(ourLockTimerIsSet) {
346    ourLockTimerIntervalID = window.setInterval(function () { locktimer.refresh(); }, 30000);
347  }
348}
349
350function dwfckKeypressInstallHandler() {
351  if(window.addEventListener){
352      oDokuWiki_FCKEditorInstance.EditorDocument.addEventListener('keyup', handlekeypress , false) ;
353  }
354  else {
355     oDokuWiki_FCKEditorInstance.EditorDocument.attachEvent('onkeyup', handlekeypress ) ;
356  }
357}
358
359var DWFCK_EditorWinObj;
360function FCKEditorWindowObj(w) {
361  DWFCK_EditorWinObj = w;
362}
363
364function fckg_isRTL() {
365var direction = "$fckg_lang_direction";
366
367return direction == 'rtl';
368
369}
370
371
372var oDokuWiki_FCKEditorInstance;
373function FCKeditor_OnComplete( editorInstance )
374{
375
376  oDokuWiki_FCKEditorInstance = editorInstance;
377
378  var f = document.getElementById('wiki__text');
379  oDokuWiki_FCKEditorInstance.VKI_attach = function() {
380     VKI_attach(f, oDokuWiki_FCKEditorInstance.get_FCK(), 'French');
381  }
382  oDokuWiki_FCKEditorInstance.get_FCK().fckLImmutables = fckLImmutables;
383  oDokuWiki_FCKEditorInstance.dwiki_user = "$user_type";
384  oDokuWiki_FCKEditorInstance.dwiki_client = "$client";
385  oDokuWiki_FCKEditorInstance.dwiki_usergroups = "$user_groups";
386  oDokuWiki_FCKEditorInstance.dwiki_doku_base = "$doku_base";
387  oDokuWiki_FCKEditorInstance.dwiki_create_folder = "$create_folder";
388  oDokuWiki_FCKEditorInstance.dwiki_fnencode = "$fnencode";
389  oDokuWiki_FCKEditorInstance.dwiki_version = $dwiki_version;
390  oDokuWiki_FCKEditorInstance.dwiki_anteater = $ver_anteater;
391
392  document.getElementById('wiki__text___Frame').style.height = "450px";
393  document.getElementById('size__ctl').innerHTML = document.getElementById('fck_size__ctl').innerHTML;
394
395  if(window.addEventListener){
396    editorInstance.EditorDocument.addEventListener('keydown', CTRL_Key_Formats, false) ;
397    if(document.documentMode && document.documentMode == 9) {
398        editorInstance.EditorDocument.addEventListener('click', fckgMousePos, false) ;
399    }
400  }
401  else {
402   editorInstance.EditorDocument.attachEvent('onkeydown', CTRL_Key_Formats) ;
403   }
404  dwfckKeypressInstallHandler();
405
406  var index = navigator.userAgent.indexOf('Safari');
407  if(index == -1  || (navigator.userAgent.indexOf('Chrome'))) {
408    oldBeforeunload = window.onbeforeunload;
409    window.onbeforeunload = fckgEditorTextChanged;
410  }
411
412  var FCK = oDokuWiki_FCKEditorInstance.get_FCK();
413  if(FCK.EditorDocument && FCK.EditorDocument.body  && !ourFCKEditorNode) {
414     ourFCKEditorNode = FCK.EditorDocument.body;
415  }
416   var FCKConfig = editorInstance.get_FCKConfig();
417   FCKConfig.fckgUserName = "$user_name";
418   FCKConfig.fckgUserMail="$user_email";
419  // alert(FCKConfig.name);
420}
421
422function fckgMousePos(e) {
423      if(!e) e=event;
424
425     if(document.documentMode && document.documentMode == 9) {
426       oDokuWiki_FCKEditorInstance.get_FCK().mouse_x = e.clientX;
427       oDokuWiki_FCKEditorInstance.get_FCK().mouse_y = e.clientY;
428       oDokuWiki_FCKEditorInstance.get_FCK().screen_x = e.screenX;
429       oDokuWiki_FCKEditorInstance.get_FCK().screen_y = e.screenY;
430       oDokuWiki_FCKEditorInstance.get_FCK().target = e.target;
431     }
432}
433function CTRL_Key_Formats(parm) {
434
435     if(!parm.ctrlKey) return;
436     if(parm.altKey) return;  // for alt GR key, where ctrl might be set to true
437
438    /* h1 - h5 */
439     if(parm.keyCode >=49 && parm.keyCode <=53) {
440         var n = parm.keyCode - 48;
441         oDokuWiki_FCKEditorInstance.get_FCK().Styles.ApplyStyle('_FCK_h' + n);
442         return;
443     }
444     /*  code/monospace -> 77 = 'm' */
445     if(parm.keyCode == 77) {
446        oDokuWiki_FCKEditorInstance.get_FCK().Styles.ApplyStyle('_FCK_code');
447		return;
448     }
449
450    /* CTRL-0 = Normal, i.e. <p> */
451    if(parm.keyCode == 48) {
452        oDokuWiki_FCKEditorInstance.get_FCK().Styles.ApplyStyle('_FCK_p');
453    }
454
455  }
456
457
458  var DWikifnEncode = "$fnencode";
459
460/* Make sure that show buttons in top and/or bottom clear the fckl file */
461 function get_showButtons() {
462	var inputs = document.getElementsByTagName('input');
463
464     for(var i=0; i<inputs.length; i++) {
465        if(inputs[i].type && inputs[i].type.match(/submit/i)) {
466			if(inputs[i].value.match(/Show/i) || (inputs[i].form &&  inputs[i].form.className.match(/btn_show/) ) )
467    			inputs[i].onmouseup = draft_delete;
468        }
469     }
470  }
471
472 /* make sure the entire page has been loaded */
473 setTimeout("get_showButtons()", 3000);
474 //]]>
475
476</script>
477end_of_string;
478  }
479}
480?>
481