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_ckgedit 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_ckgedit_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     /**
55    * function dw_edit_displayed
56    * @author  Myron Turner
57    * determines whether or not to show  or hide the
58    *  'DW Edit' button
59   */
60
61   function dw_edit_displayed()
62   {
63        global $INFO;
64
65        $dw_edit_display = @$this->getConf('dw_edit_display');
66        if(!isset($dw_edit_display))return "";  //version 0.
67        if($dw_edit_display != 'all') {
68            $admin_exclusion = false;
69            if($dw_edit_display == 'admin' && ($INFO['isadmin'] || $INFO['ismanager']) ) {
70                    $admin_exclusion = true;
71            }
72            if($dw_edit_display == 'none' || $admin_exclusion === false) {
73              return ' style = "display:none"; ';
74            }
75           return "";
76        }
77        return "";
78
79   }
80
81  function registerOnLoad($js){
82  global $ID;
83  global $lang;
84  global $skip_styling;
85  global $JSINFO;
86
87  $ckgedit_conf_direction = $this->getConf('direction');
88   if($ckgedit_conf_direction == "dokuwiki") {
89       $ckgedit_lang_direction = $lang['direction'];
90   }
91  else {
92      $ckgedit_lang_direction = $ckgedit_conf_direction;
93  }
94  $ImageUploadAllowedExtensions = $this->get_ckgedit_ImageAllowedExtensions() ;
95  $media_tmp_ns = preg_match('/:/',$ID) ? preg_replace('/:\w+$/',"",$ID,1) : "";
96  $locktimer_msg = "Your lock for editing this page is about to expire in a minute.\\n"
97                . "You can reset the timer by clicking the Back-up button.";
98
99    $meta_fn = metaFN($ID,'.ckgedit');
100    $meta_id = 'meta/' . str_replace(':','/',$ID) . '.ckgedit';
101
102  global $INFO;
103  global $conf;
104  global $USERINFO;
105  $_OS = strtolower(PHP_OS);
106  $cname = getCacheName($INFO['client'].$ID,'.draft');
107  $useheading = $conf['useheading'];
108
109  if($useheading && $useheading != 'navigation') {
110       $useheading = 'y';
111  }
112  else $useheading = 'n';
113  //msg('uh='.$useheading);
114  $open_upload = $this->getConf('open_upload');
115  $editor_backup = $this->getConf('editor_bak');
116  $create_folder = $this->getConf('create_folder');
117  $interface_lang = $this->getConf('other_lang');
118  $scayt_lang = $this->getConf('scayt_lang');
119  list($name,$scayt_lang) = explode('/', $scayt_lang);
120
121  $scayt_auto = $this->getConf('scayt_auto');
122 $color_opts = $this->getConf('color_options');
123 $font_opts = $this->getConf('font_options');
124  $toolbar_opts = $this->getConf('alt_toolbar');
125 $mfiles =   $this->getConf('mfiles');
126 $extra_plugins = $this->getConf('extra_plugins');
127  $ckg_gui = $this->getConf('gui');
128  if(!isset($INFO['userinfo']) && !$open_upload) {
129    $user_type = 'visitor';
130  }
131  else {
132   $user_type = 'user';
133  }
134  $save_dir = DOKU_BASE . ltrim($conf['savedir'],'/.\/');
135  $fbsz_increment = isset($_COOKIE['fbsz']) && $_COOKIE['fbsz'] ? $_COOKIE['fbsz'] : '0';
136  $use_pastebase64 = (isset($_COOKIE['ckgEdPaste']) && $_COOKIE['ckgEdPaste'] == 'on' )  ? 'on' : 'off';
137  // if no ACL is used always return upload rights
138  if($conf['useacl']) {
139     $client = $_SERVER['REMOTE_USER'];
140  }
141  else $client = "";
142  $user_name = $USERINFO['name'];
143  $user_email = $USERINFO['mail'];
144
145  $fnencode = isset($conf['fnencode']) ? $conf['fnencode'] : 'url';
146  $user_groups = $USERINFO['grps'];
147  if(!$user_groups) $user_groups = array();
148  if (@in_array("guest", $user_groups)) {
149     $create_folder = 'n';
150	 $user_type = 'visitor';
151  }
152  $user_groups = str_replace('"','\"',implode(";;",$user_groups));
153
154  if($INFO['isadmin'] || $INFO['ismanager']) {
155     $client = "";
156  }
157
158  $ver_anteater = mktime(0,0,0,11,7,2010);
159  $dwiki_version=mktime(0,0,0,01,01,2008);
160
161  if(isset($conf['fnencode'])) {
162      $ver_anteater = mktime(0,0,0,11,7,2010);
163      $dwiki_version=mktime(0,0,0,11,7,2010);
164  }
165  else if(function_exists('getVersionData')) {
166      $verdata= getVersionData();
167      if(isset($verdata) && preg_match('/(\d+)-(\d+)-(\d+)/',$verdata['date'],$ver_date)) {
168          if($ver_date[1] >= 2005 && ($ver_date[3] > 0 && $ver_date[3] < 31) && ($ver_date[2] > 0 && $ver_date[2] <= 12)) {
169                                          // month        day               year
170          $dwiki_version=@mktime(0,  0,  0, $ver_date[2],$ver_date[3], $ver_date[1]);
171          if(!$dwiki_version) $dwiki_version = mktime(0,0,0,01,01,2008);
172          $ver_anteater = mktime(0,0,0,11,7,2010);
173      }
174    }
175  }
176
177$ckg_brokenimg = $this->getLang('broken_image');
178 $default_fb = $this->getConf('default_fb');
179 if($default_fb == 'none') {
180     $client = "";
181 }
182
183 $doku_base = DOKU_BASE;
184
185    return <<<end_of_string
186
187
188<script type='text/javascript'>
189 //<![CDATA[
190
191if(window.dw_locktimer) {
192   var locktimer = dw_locktimer;
193}
194var FCKRecovery = "";
195var oldonload = window.onload;
196var ourLockTimerINI = false;
197
198var ckgedit_onload = function() { $js };
199window.onload =  ckgedit_onload;
200
201  function getCurrentWikiNS() {
202        var DWikiMediaManagerCommand_ns = '$media_tmp_ns';
203        return DWikiMediaManagerCommand_ns;
204  }
205
206 var ourFCKEditorNode = null;
207
208function revert_to_prev() {
209  if(!(GetE('saved_wiki_html').innerHTML.length)) {
210            if(!confirm(backup_empty)) {
211                           return;
212            }
213  }
214  CKEDITOR.instances.wiki__text.setData( GetE('saved_wiki_html').innerHTML);
215   window.dwfckTextChanged = true;
216}
217
218
219function draft_delete() {
220
221        var debug = false;
222        var params = "draft_id=$cname";
223        jQuery.ajax({
224           url: DOKU_BASE + 'lib/plugins/ckgedit/scripts/draft_delete.php',
225           data: params,
226           type: 'POST',
227           dataType: 'html',
228           success: function(data){
229               if(debug) {
230                  alert(data);
231               }
232
233    }
234    });
235
236    window.textChanged = false;
237}
238
239var DWFCK_EditorWinObj;
240function FCKEditorWindowObj(w) {
241  DWFCK_EditorWinObj = w;
242}
243
244function ckgedit_isRTL() {
245var direction = "$ckgedit_lang_direction";
246
247return direction == 'rtl';
248
249}
250
251function remove_styling() {
252//'TextColor,BGColor, FontAssist,Font,FontSize';
253var opts = "";
254var color_opts = parseInt( "$color_opts");
255var font_opts =  parseInt("$font_opts");
256var skip_styling=parseInt("$skip_styling");
257if(color_opts) {
258  opts ='TextColor,BGColor,FontAssist';
259}
260else if(!skip_styling) {
261     opts = 'FontAssist';
262}
263if(font_opts) {
264  if(color_opts || !skip_styling) opts+=',';
265  opts +='Font,FontSize';
266}
267if("$toolbar_opts") {
268  if(opts) opts+=',';
269  opts+="$toolbar_opts";
270}
271
272return opts;
273
274}
275
276function  extra_plugins(config) {
277    if("$use_pastebase64" == 'on')  config.addPaste();
278    return "$extra_plugins";
279}
280
281function ckgedit_language_chk(config) {
282    if("$scayt_auto" == 'on') {
283        config.scayt_autoStartup = true;
284    }
285    else config.scayt_autoStartup = false;
286    if("$scayt_auto" == 'disable') {
287        config.scayt__disable = true;
288    }
289    config.scayt_sLang="$scayt_lang";
290   var lang = "$interface_lang";
291   if(lang ==  'default') return; ;
292   config.language = lang;
293}
294
295function getCKEditorGUI() {
296    return "$ckg_gui";
297}
298var oDokuWiki_FCKEditorInstance;
299function FCKeditor_OnComplete( editorInstance )
300{
301
302  oDokuWiki_FCKEditorInstance = editorInstance;
303  editorInstance.on( 'key', handlekeypress, editorInstance );
304
305  CKEDITOR.instances.wiki__text.on('change', function(event) {
306        window.dwfckTextChanged = true;
307        window.textChanged = true;
308  });
309
310  editorInstance.on("focus", function(e) {
311          window.dwfckTextChanged = true;
312    });
313
314   var broken_image ='http://' +  location.host +  DOKU_BASE +  '/lib/plugins/ckgedit/fckeditor/userfiles/blink.jpg?nolink&33x34';
315   editorInstance.on("paste", function(e) {
316   //   https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
317        var formatBytes = function(bytes,decimals) {
318             if(bytes == 0) return '0 Bytes';
319             var k = 1024,
320             dm = decimals <= 0 ? 0 : decimals || 2,
321             sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
322             i = Math.floor(Math.log(bytes) / Math.log(k));
323             return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
324        }
325         var len =  e.data.dataValue.length;
326         var len = e.data.dataValue.length - 'data:image/png;base64,'.length;
327         var size =   formatBytes(len,1);
328          var broken_msg = ckg_RawImgMsg();
329          broken_msg += " " + size;
330         if(e.data.dataValue.match(/data:image\/\w+;base64/) &&  len > 2500000) {
331             alert(broken_msg);
332             e.data.dataValue = '<img src ='+ broken_image + '/>';
333        }
334    });
335
336  oDokuWiki_FCKEditorInstance.dwiki_user = "$user_type";
337  oDokuWiki_FCKEditorInstance.dwiki_client = "$client";
338  oDokuWiki_FCKEditorInstance.dwiki_usergroups = "$user_groups";
339  oDokuWiki_FCKEditorInstance.dwiki_doku_base = "$doku_base";
340  oDokuWiki_FCKEditorInstance.dwiki_create_folder = "$create_folder";
341  oDokuWiki_FCKEditorInstance.dwiki_fnencode = "$fnencode";
342  oDokuWiki_FCKEditorInstance.dwiki_version = $dwiki_version;
343  oDokuWiki_FCKEditorInstance.dwiki_anteater = $ver_anteater;
344  oDokuWiki_FCKEditorInstance.isLocalDwikiBrowser = false;
345  oDokuWiki_FCKEditorInstance.isUrlExtern = false;
346  oDokuWiki_FCKEditorInstance.isDwikiMediaFile = false;
347  oDokuWiki_FCKEditorInstance.imageUploadAllowedExtensions="$ImageUploadAllowedExtensions";
348  oDokuWiki_FCKEditorInstance.fckgUserName = "$user_name";
349  oDokuWiki_FCKEditorInstance.fckgUserMail="$user_email";
350  oDokuWiki_FCKEditorInstance.useheading = "$useheading";
351  oDokuWiki_FCKEditorInstance.mfiles = parseInt("$mfiles");
352  oDokuWiki_FCKEditorInstance.fbsz_increment=parseInt("$fbsz_increment");
353
354}
355
356
357 window.DWikifnEncode = "$fnencode";
358
359 //]]>
360
361</script>
362end_of_string;
363  }
364}
365?>
366