1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4if(!defined('DOKU_MEDIA')) define('DOKU_MEDIA',DOKU_INC.'data/media/');
5define ('BROKEN_IMAGE', DOKU_URL . 'lib/plugins/fckg/fckeditor/userfiles/blink.jpg?nolink&33x34');
6require_once(DOKU_PLUGIN.'action.php');
7define('FCK_ACTION_SUBDIR', realpath(dirname(__FILE__)) . '/');
8/**
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 */
11
12class action_plugin_fckg_save extends DokuWiki_Action_Plugin {
13    /**
14     * Constructor
15     */
16    function action_plugin_fckg_save(){
17    }
18
19
20
21    function register(Doku_Event_Handler $controller) {
22
23        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'fckg_save_preprocess');
24    }
25
26    function fckg_save_preprocess(&$event){
27        global $ACT;
28        if (!isset($_REQUEST['fckg']) || ! is_array($ACT) || !(isset($ACT['save']) || isset($ACT['preview']))) return;
29
30        global $TEXT;
31        if (!$TEXT) return;
32
33        $TEXT = $_REQUEST['fck_wikitext'];
34
35        if(!preg_match('/^\s+(\-|\*)/',$TEXT)) {
36              $TEXT = trim($TEXT);
37        }
38
39    if(strpos($TEXT,'data:image') !== false) {
40        $TEXT = preg_replace_callback(
41             '|\{\{(\s*)data:image\/(\w+;base64,)(.*?)\?nolink&(\s*)\}\}|ms',
42             create_function(
43                '$matches',
44                'list($ext,$base) = explode(";",$matches[2]);
45                if($ext == "jpeg") $ext = "jpg";
46                 if(function_exists("imagecreatefromstring") && !imagecreatefromstring (base64_decode($matches[3]))) {
47                     msg("Clipboard paste: invalid $ext image format");
48                     return "{{" . BROKEN_IMAGE .  "}}";
49                 }
50                  global $INFO,$conf;
51                  $ns = getNS($INFO["id"]);
52                  $ns = trim($ns);
53                  if(!empty($ns)) {
54                      $ns = ":$ns:";
55                       $dir = str_replace(":","/",$ns);
56                  }
57                  else {  // root namespace
58                      $dir = "/";
59                      $ns = ":";
60                  }
61                 $fn = md5($matches[3]) . ".$ext";
62                 $path = $conf["mediadir"] . $dir .  $fn;
63                 @io_makeFileDir($path);
64                 if(!file_exists($path)) {
65                    @file_put_contents($path, base64_decode($matches[3]));
66                 }
67                 else {
68                     msg("file for this image previousely saved",2);
69                 }
70                 $left = "{{";
71                 $right = "}}";
72                 if($matches[1]) $left .= $matches[1];
73                 if($matches[4]) $right = $matches[4] . $right;
74
75                $retv = "$left" . $ns. $fn . "$right";
76                 return $retv;'
77             ),
78             $TEXT
79             );
80        }
81
82      $TEXT = str_replace('%%', "FCKGPERCENTESC",  $TEXT);
83
84          $TEXT = preg_replace_callback('/^(.*?)(\[\[.*?\]\])*(.*?)$/ms',
85               create_function(
86                     '$matches',
87                     '$matches[1] = preg_replace("/%([A-F]+)/i", "URLENC_PERCENT$1", $matches[1]);
88                      $matches[3] = preg_replace("/%([A-F]+)/i", "URLENC_PERCENT$1", $matches[3]);
89                      return $matches[1].$matches[2].$matches[3];'
90                ),
91                $TEXT
92             );
93
94
95        $TEXT = rawurldecode($TEXT);
96        $TEXT = preg_replace('/NOWIKI_(.)_/', '$1',$TEXT);
97        $TEXT = preg_replace('/URLENC_PERCENT/', '%',$TEXT);
98          /* preserve newlines in code blocks */
99          $TEXT = preg_replace_callback(
100            '/(<code>|<file>)(.*?)(<\/code>|<\/file>)/ms',
101            create_function(
102                '$matches',
103                'return  str_replace("\n", "__code_NL__",$matches[0]);'
104            ),
105            $TEXT
106          );
107
108        $TEXT = preg_replace('/^\s*[\r\n]$/ms',"__n__", $TEXT);
109        $TEXT = preg_replace('/\r/ms',"", $TEXT);
110        $TEXT = preg_replace('/^\s+(?=\^|\|)/ms',"", $TEXT);
111        $TEXT = preg_replace('/__n__/',"\n", $TEXT);
112        $TEXT = str_replace("__code_NL__","\n", $TEXT);
113        $TEXT = str_replace("FCKGPERCENTESC", '%%',  $TEXT);
114        if($this->getConf('complex_tables')) {
115            $TEXT = str_replace('~~COMPLEX_TABLES~~','',$TEXT);
116        }
117        $TEXT .= "\n";
118        // Removes relics of markup characters left over after acronym markup has been removed
119        //$TEXT = preg_replace('/([\*\/_]{2})\s+\\1\s*([A-Z]+)\s*\\1+/ms',"$2",$TEXT);
120
121         $pos = strpos($TEXT, 'MULTI_PLUGIN_OPEN');
122         if($pos !== false) {
123            $TEXT = preg_replace_callback(
124             '|MULTI_PLUGIN_OPEN.*?MULTI_PLUGIN_CLOSE|ms',
125             create_function(
126                 '$matches',
127                   'return  preg_replace("/\\\\\\\\/ms","\n",$matches[0]);'
128             ),
129             $TEXT
130           );
131
132            $TEXT = preg_replace_callback(
133             '|MULTI_PLUGIN_OPEN.*?MULTI_PLUGIN_CLOSE|ms',
134             create_function(
135                 '$matches',
136                   'return  preg_replace("/^\s+/ms","",$matches[0]);'
137             ),
138             $TEXT
139           );
140
141         }
142
143        $this->replace_entities();
144
145       $TEXT = preg_replace('/__QUOTE__/ms',">",$TEXT);
146       $TEXT = preg_replace('/[\t\x20]+$/ms',"",$TEXT);
147       $TEXT = preg_replace('/\n{4,}/ms',"\n\n",$TEXT);
148       $TEXT = preg_replace('/\n{3,}/ms',"\n\n",$TEXT);
149
150         return;
151
152    }
153
154function replace_entities() {
155global $TEXT;
156global $ents;
157    $serialized = FCK_ACTION_SUBDIR . 'ent.ser';
158    $ents = unserialize(file_get_contents($serialized));
159
160       $TEXT = preg_replace_callback(
161            '|(&(\w+);)|',
162            create_function(
163                '$matches',
164                'global $ents; return $ents[$matches[2]];'
165            ),
166            $TEXT
167        );
168
169}
170
171
172function write_debug($data) {
173return;
174  if (!$handle = fopen('save.txt', 'a')) {
175    return;
176    }
177
178    // Write $somecontent to our opened file.
179    fwrite($handle, "save.php: $data\n");
180    fclose($handle);
181
182}
183} //end of action class
184?>
185