1<?php
2/**
3 *
4 */
5
6if(!defined('DOKU_INC')) die();
7if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
8require_once(DOKU_PLUGIN.'action.php');
9global $conf;
10global $fckg_lang;
11
12
13$default_english_file = DOKU_PLUGIN . 'fckg/action/lang/en.php';
14require_once($default_english_file);
15if(isset($conf['lang'])) {
16  $default_lang_file = DOKU_PLUGIN . 'fckg/action/lang/' . $conf['lang'] . '.php';
17  if(file_exists($default_lang_file)) {
18    @include_once($default_lang_file);
19  }
20}
21
22class action_plugin_fckg_meta extends DokuWiki_Action_Plugin {
23  var $session_id = false;
24  var $draft_file;
25  var $dokuwiki_priority;
26
27   function action_plugin_fckg_meta() {
28
29      $this->dokuwiki_priority = $this->getConf('dw_priority');
30   }
31  /*
32   * Register its handlers with the dokuwiki's event controller
33   */
34  function register(Doku_Event_Handler $controller) {
35            $controller->register_hook( 'TPL_METAHEADER_OUTPUT', 'AFTER', $this, 'loadScript');
36            $controller->register_hook( 'HTML_EDITFORM_INJECTION', 'AFTER', $this, 'preprocess');
37            $controller->register_hook( 'HTML_EDITFORM_OUTPUT', 'BEFORE', $this, 'insertFormElement');
38            $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'file_type');
39            $controller->register_hook('TPL_CONTENT_DISPLAY', 'AFTER', $this, 'setupDWEdit');
40            $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'fnencode_check');
41  }
42
43
44 function  insertFormElement(&$event, $param) {
45   global $FCKG_show_preview;
46
47  $param = array();
48
49   global $ID;
50   $dwedit_ns = @$this->getConf('dwedit_ns');
51   if(isset($dwedit_ns) && $dwedit_ns) {
52       $ns_choices = explode(',',$dwedit_ns);
53       foreach($ns_choices as $ns) {
54         $ns = trim($ns);
55         if(preg_match("/$ns/",$ID)) {
56            echo "<style type = 'text/css'>#edbtn__preview,#edbtn__save, #edbtn__save { display: inline; } </style>";
57            break;
58         }
59       }
60   }
61   $act = $event->data;
62   if(is_string($act) && $act != 'edit') {
63        return;
64   }
65
66  // restore preview button if standard DW editor is in place
67  // $FCKG_show_preview is set in edit.php in the register() function
68if($_REQUEST['fck_preview_mode'] != 'nil' && !isset($_COOKIE['FCKG_USE']) && !$FCKG_show_preview) {
69     echo '<style type="text/css">#edbtn__preview { display:none; }</style>';
70 }
71 elseif($FCKG_show_preview) {
72      echo '<style type="text/css">#edbtn__preview { display: inline; } </style>';
73 }
74 else {
75    echo '<style type="text/css">#edbtn__preview, .btn_show { position:absolute; visibility:hidden; }</style>';
76 }
77
78 global $fckg_lang;
79
80  if($_REQUEST['fck_preview_mode']== 'preview'){
81    return;
82  }
83
84 $param = array();
85 $this->preprocess($event, $param);  // create the setDWEditCookie() js function
86 $button = array
87        (
88            '_elem' => 'button',
89            'type' => 'submit',
90            '_action' => 'cancel',
91            'value' => $fckg_lang['btn_fck_edit'],
92            'class' => 'button',
93            'id' => 'edbtn__edit',
94            'title' => $fckg_lang['btn_fck_edit']
95        );
96
97     $pos = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE');
98     if($pos === false) {
99                 $button['onclick'] = 'return setDWEditCookie(1, this);';
100     }
101     else {
102                $button['onmousedown'] = 'return setDWEditCookie(1, this);';
103     }
104
105    $pos = $event->data->findElementByAttribute('type','submit');
106       //inserts HTML data after that position.
107    $event->data->insertElement(++$pos,$button);
108
109   return;
110
111  }
112
113
114 function preprocess(&$event, $param) {
115    $act = $event->data;
116
117   if(is_string($act) && $act != 'edit') {
118        return;
119   }
120  global $INFO;
121  $cname =  $INFO['draft'];
122  $dokuwiki_priority =$this->dokuwiki_priority;
123  echo "<script type='text/javascript'>\n//<![CDATA[ \n";
124  echo "var useDW_Editor =$dokuwiki_priority;";
125  echo "\n //]]> </script>\n";
126  echo <<<SCRIPT
127    <script type="text/javascript">
128    //<![CDATA[
129
130    function setDWEditCookie(which, e) {
131       var cname = "$cname";
132       var dom = document.getElementById('fckg_mode_type');
133       if(which == 1) {
134           dwedit_draft_delete("$cname");
135             if(useDW_Editor) {
136                document.cookie = 'FCKG_USE=other;expires=';
137              }
138           else {
139                document.cookie='FCKG_USE=other;expires=Thu,01-Jan-70 00:00:01 GMT;'
140           }
141           if(e && e.form) {
142                    if(e.form['mode']) {
143                       e.form['mode'].value = 'fck';
144                    }
145                    else {
146                       e.form['mode'] = new Object;
147                       e.form['mode'].value = 'fck';
148                    }
149           }
150           else dom.value = 'fck';
151       }
152        else {
153            document.cookie = 'FCKG_USE=_false_;expires=';
154            dom.value = 'dwiki';
155
156        }
157         e.form.submit();
158    }
159
160
161    //]]>
162
163    </script>
164SCRIPT;
165
166  }
167
168function check_userfiles() {
169
170    if($this->getConf('no_symlinks')) {
171	   return;
172	}
173
174    global $INFO;
175    global $conf;
176	$userfiles = DOKU_PLUGIN . 'fckg/fckeditor/userfiles/';
177
178    $save_dir = trim($conf['savedir']);
179// msg('BASE='. DOKU_BASE);
180// msg(DOKU_URL);
181// msg('REL='. DOKU_REL);
182    if(!preg_match('#^\.\/data$#',$save_dir)) {
183        $data_media = $conf['savedir']  . '/media/';
184        $domain = trim(DOKU_BASE,'/');
185        $expire = null;
186
187        if(! empty($domain )) {
188        list($prefix,$mdir) = explode(trim(DOKU_BASE, '/'),$userfiles);
189           $mdir = ltrim($mdir, '/');
190        $media_dir = DOKU_BASE . $mdir . 'image/';
191        setcookie('FCK_media',$media_dir, $expire, '/');
192        }
193        else $media_dir = '/lib/plugins/ckgedit/fckeditor/userfiles/image/';
194     }
195     else {
196         $data_media = DOKU_INC.'data/media/';
197     }
198
199     if($this->getConf('winstyle')) return;
200     if(!is_writable($userfiles)){
201              msg("  fckg cannot write to $userfiles. Please check the permissions.");
202		      return;
203     }
204	$version = io_readFile(DOKU_PLUGIN . 'fckg/version');
205	if(!$version) return;
206    $meta = metaFN('fckl:symchk','.meta');
207	$symcheck = io_readFile($meta);
208    if($symcheck) {
209	   if(trim($version)== trim($symcheck)) {  //symlinks should already have been created
210		  return;
211	   }
212    }
213
214	if (function_exists('php_uname')) {
215	   $sys = php_uname() ;
216	   if( preg_match('/Windows/i',  $sys) ) {
217		     preg_match('/build\s+(\d+)/',$sys, $matches);
218		    if($matches[1]  < 6000) {  // we can make symlinks for vista (6000) or later
219			   return;
220		   }
221
222		   $winlinks =  array();
223		   $userfiles = str_replace('/', '\\',$userfiles);
224		   exec("dir " . $userfiles, $output);
225		   foreach($output as $line) {
226		      if(preg_match('/<SYMLINKD>\s+(.*?)\s+\[/i',$line,$matches)) {
227			     $winlinks[] = $matches[1];
228			  }
229		   }
230		}
231
232    }
233	else if( preg_match('/WINNT/i',  PHP_OS) ) {    // if we can't get php_uname and a build and this is Windows, just return
234         return;
235    }
236
237       $show_msg = false;
238	   if($INFO['isadmin'] || $INFO['ismanager'] )    {  // only admins and mgrs get messages
239	       $show_msg = true;
240	   }
241	   $link_names = array('flash',  'image',  'media', 'file', 'image');
242	   if(count($winlinks)) {
243	       $link_names = array_diff($link_names, $winlinks);
244	   }
245	   $links = array();
246	   foreach ($link_names as $ln) {
247	        $links[$ln] = $userfiles . $ln;
248	   }
249
250      $bad_create = false;
251	  $successes =  array();
252	  if(@file_exists($userfiles)) {
253		   foreach($links as $name => $path) {
254			  if(!is_link($path)) {
255                     if(file_exists($path) && is_file($path) ){
256					       unlink($path);
257                       }
258                    if(file_exists($path) && is_dir($path) ){
259					       rmdir($path);
260                       }
261					 if(!@symlink($data_media,$path) ) {
262					     $bad_create = true;
263						  if($show_msg)  msg("unable to create $name link:  $path",-1);
264				   }
265				   else {
266				     $successes[] = $name;
267				   }
268			 }
269		  }
270      }
271	  else {
272	     if($show_msg)  {
273			msg("Cannot create symlinks for filebrowser.  Cannot access:  $userfiles   ",-1);
274		 }
275	  }
276
277
278
279	  if($bad_create) {
280	       if($show_msg)  {
281			   msg("There was an error when trying to create symbolic links in $userfiles. "
282					. "See fckg/auto_install.pdf  or  the <a href='http://www.mturner.org/fckgLite/doku.php?id=docs:auto_install'>fckgLite web site</a>" , 2);
283				}
284      }
285	  else {
286	       if(count($successes)) {
287				$links_created = implode(', ',$successes);
288				msg('The following links were created in the userfiles directory: ' . $links_created,2);
289			 }
290	  }
291	  			io_saveFile($meta,$version);
292}
293
294
295  function set_session() {
296      global $USERINFO, $INFO;
297      global $conf;
298      global $ID;
299      global $ACT;
300
301      if($this->session_id) return;
302
303           $cname = getCacheName($INFO['client'].$ID,'.draft');
304           $fckl_draft = $cname . '.fckl';
305           if((isset($ACT) && is_array($ACT)) || isset($_REQUEST['dwedit_preview'])) {
306              if(isset($ACT['draftdel']) || isset($ACT['cancel']) || isset($_REQUEST['dwedit_preview'])) {
307                 @unlink($fckl_draft);
308                 @unlink($cname);
309              }
310           }
311
312           if(file_exists($cname)) {
313              if(file_exists($fckl_draft)) {
314                    unlink($fckl_draft);
315              }
316              @rename($cname, $fckl_draft);
317           }
318
319
320           $session_string =  session_id();
321           $this->session_id = $session_string;
322
323
324           $_SESSION['dwfck_id'] = $session_string;
325           $default_fb = $this->getConf('default_fb');
326
327           if($default_fb == 'none') {
328               $acl = 255;
329           }
330           else {
331              $acl = auth_quickaclcheck($ID);
332           }
333           $_SESSION['dwfck_acl'] = $acl;
334
335           if($this->getConf('openfb') || $acl == 255) {
336             $_SESSION['dwfck_openfb'] = 'y';
337           }
338           else {
339              $_SESSION['dwfck_openfb'] = 'n';
340           }
341
342           $_SESSION['dwfck_grps'] = $USERINFO['grps'];
343           $_SESSION['dwfck_client'] = $INFO['client'];
344           $_SESSION['dwfck_sepchar'] = $conf['sepchar'] ;
345           $_SESSION['dwfck_conf'] = array('sepchar'=> $conf['sepchar'],
346                  'isadmin'=>($INFO['isadmin'] || $INFO['ismanager']),
347                  'deaccent'=>$conf['deaccent'], 'useslash'=>$conf['useslash']);
348           $elems = explode(':', $ID);
349           array_pop($elems);
350
351           $_SESSION['dwfck_ns'] = implode(':',$elems);
352           $_SESSION['dwfck_top'] = implode('/',$elems);
353           $_SESSION['dwfck_del'] = $this->getConf('acl_del');
354
355            // temp fix for puzzling encoding=url bug in frmresourceslist.html,
356           // where image loading is processed in GetFileRowHtml()
357
358           if(preg_match('/fckg:fckeditor:userfiles:image/',$ID)) {
359                      $_SESSION['dwfck_ns'] = "";
360                      $_SESSION['dwfck_top'] = "";
361
362            }
363
364          // $expire = time()+60*60*24*30;
365           $expire = null;
366           setcookie('FCK_NmSp_acl',$session_string, $expire, '/');
367
368           setcookie('FCK_SCAYT',$this->getConf('scayt'), $expire, '/');
369           setcookie('FCK_SCAYT_AUTO',$this->getConf('scayt_auto'), $expire, '/');
370           $scayt_lang = $this->getConf('scayt_lang');
371           if(isset($scayt_lang)) {
372               list($scayt_lang_title,$scayt_lang_code) = explode('/',$scayt_lang);
373               if($scayt_lang_code!="en_US") {
374                  setcookie('FCK_SCAYT_LANG',$scayt_lang_code, $expire, '/');
375               }
376           }
377
378            if ($this->getConf('winstyle')) {
379              setcookie('FCKConnector','WIN', $expire, DOKU_BASE);
380            }
381
382           if ($this->dokuwiki_priority) {
383               if(isset($_COOKIE['FCKG_USE']) && $_COOKIE['FCKG_USE'] == 'other') {
384                   $expire = time() -60*60*24*30;
385                   setcookie('FCKG_USE','_false_', $expire, '/');
386               }
387               else {
388                  setcookie('FCKG_USE','_false_', $expire, '/');
389                }
390           }
391
392  }
393
394  function file_type(&$event, $param) {
395       global $ACT, $TEXT;
396       global $USERINFO, $INFO, $ID;
397
398	   $this->check_userfiles();
399
400       if(isset($_COOKIE['FCK_NmSp'])) $this->set_session();
401       /* set cookie to pass namespace to FCKeditor's media dialog */
402      // $expire = time()+60*60*24*30;
403       $expire = null;
404       setcookie ('FCK_NmSp',$ID, $expire, '/');
405
406
407
408      /* Remove TopLevel cookie */
409       if(isset($_COOKIE['TopLevel'])) {
410            setcookie("TopLevel", $_REQUEST['TopLevel'], time()-3600, '/');
411       }
412
413
414       if(!isset($_REQUEST['id']) || isset($ACT['preview'])) return;
415       if(isset($_REQUEST['do']) && isset($_REQUEST['do']['edit'])) {
416              $_REQUEST['do'] = 'edit';
417       }
418  }
419
420function loadScript(&$event) {
421  echo <<<SCRIPT
422
423    <script type="text/javascript">
424    //<![CDATA[
425    function LoadScript( url )
426    {
427     document.write( '<scr' + 'ipt type="text/javascript" src="' + url + '"><\/scr' + 'ipt>' ) ;
428
429    }
430//]]>
431
432 </script>
433
434SCRIPT;
435
436}
437
438/**
439 *  Handle features need for DW Edit:
440 *    1. load script, if not loaded
441 *    2. Re-label Cancel Button "Exit" when doing a preview
442 *    3. set up $REQUEST value to identify a preview when in DW Edit , used in
443 *       set_session to remove fckgLite and DW drafts if present after a DW preview
444*/
445  function setupDWEdit(&$event) {
446  global $ACT;
447
448  $url = DOKU_BASE . 'lib/plugins/fckg/scripts/script-cmpr.js';
449  echo <<<SCRIPT
450
451    <script type="text/javascript">
452    //<![CDATA[
453
454    try {
455    if(!HTMLParserInstalled){
456         LoadScript("$url");
457    }
458    }
459    catch (ex) {
460         LoadScript("$url");
461    }
462
463    function createRequestValue() {
464        try{
465        var inputNode=document.createElement('input');
466        inputNode.setAttribute('type','hidden');
467        inputNode.setAttribute('value','yes');
468        inputNode.setAttribute('name','dwedit_preview');
469        inputNode.setAttribute('id','dwedit_preview');
470        var dwform = GetE("dw__editform");
471        dwform.appendChild(inputNode);
472        }catch(e) { alert(e); }
473    }
474//]]>
475 </script>
476
477SCRIPT;
478
479  if(isset($_REQUEST['do']) && is_array($_REQUEST['do'])) {
480    if(isset($_REQUEST['do']['preview'])) {
481           echo '<script type="text/javascript">';
482           echo ' var dwform = GetE("dw__editform"); dwform["do[draftdel]"].value = "Exit";';
483           echo "\ncreateRequestValue()\n";
484           echo  '</script>';
485    }
486  }
487
488
489  }
490
491function is_safeUpgraded() {
492   $safescript = DOKU_PLUGIN . 'fckg/scripts/safeFN_class.js';
493    if(!file_exists($safescript) ){
494		return false;
495	}
496   $lines = file($safescript );
497
498	for($i=0; $i<count($lines); $i++) {
499	  if(strpos($lines[$i],'/**')){
500		 if(isset($lines[$i+1])) {
501			if(stripos($lines[$i+1], 'upgrade') !== false) {
502			 return true;
503		  }
504		}
505	   }
506	  }
507	  return false;
508}
509
510function fnencode_check() {
511
512
513       global $conf;
514       global $updateVersion;
515       $rencode = false;
516
517        if($conf['fnencode'] != 'safe') return;
518
519        if(isset($updateVersion) && $updateVersion >= 31) {
520          $rencode = true;
521        }
522        else {
523            $list = plugin_list('action');
524            if(in_array('safefnrecode', $list)){
525                $rencode = true;
526
527            }
528            elseif(file_exists($conf['datadir'].'_safefn.recoded') ||
529               file_exists($conf['metadir'].'_safefn.recoded') ||
530               file_exists($conf['mediadir'].'_safefn.recoded') )
531            {
532               $rencode = true;
533            }
534        }
535
536
537      if($rencode && !file_exists(DOKU_PLUGIN . 'fckg/saferencode')) {
538         msg("This version of fckgLite does not support the re-encoded safe filenames. "
539         . "You risk corrupting your file system.  Download an fnrencode version from either gitHub or the fckgLite web site."
540         . " <a style='color:blue' href='http://www.mturner.org/fckgLite/doku.php?id=docs:upgrade_6&#anteater'>or the fckgLite web site</a>  ",
541            -1);
542      }
543      else if(!$rencode && file_exists(DOKU_PLUGIN . 'fckg/saferencode') && $this->is_safeUpgraded() )   {
544	    msg("This version of fckgLiteSafe requires a newer version of Dokuwiki (2011-05-25 Rincewind or later).  You risk corrupting your file system. "
545		 .   "To convert this distribution of fckgLite/fckgLiteSafe for use with earlier versions of Dokuwiki,  see the README file or "
546		 . " <a style='color:blue' href='http://www.mturner.org/fckgLite/doku.php?id=docs:upgrade_6&#anteater'>or the fckgLite web site</a>  ",
547		-1);
548    }
549}
550
551
552
553
554
555
556
557function write_debug($data) {
558  return;
559  if (!$handle = fopen(DOKU_INC .'meta.txt', 'a')) {
560    return;
561    }
562  if(is_array($data)) {
563     $data = print_r($data,true);
564  }
565    // Write $somecontent to our opened file.
566    fwrite($handle, "$data\n");
567    fclose($handle);
568
569}
570
571}
572
573
574
575