source: extensions/rv_autocomplete/admin/functions.inc.php @ 31029

Last change on this file since 31029 was 31029, checked in by rvelices, 9 years ago

fix bug on variants with quotes

File size: 8.8 KB
Line 
1<?php
2
3function rvac_invalidate_cache()
4{
5        global $conf;
6        conf_update_param('rvac_version', ++$conf['rvac_version'] );
7        if (rand()%10==0)
8        {
9                foreach (glob(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.'acds-*.js') as $file)
10                        @unlink($file);
11        }
12}
13
14function rvac_custom_link(&$suggestion, $roots)
15{
16        $url = $suggestion['url'];
17        if (empty($url))
18        {
19                $q = $suggestion['name'];
20                if ( ($pos=strpos($q,'\\')) !== false )
21                {
22                        $q = substr($q, 0, $pos);
23                }
24                $invalid = '&()".:;,';
25                $q = strtr($q, $invalid, str_repeat(' ', strlen($invalid)));
26                for($i=0; $i<3; $i++)
27                        $q = str_replace('  ', ' ', $q);
28        }
29        elseif (strncmp($url,'q=',2)==0)
30        {
31                $q = substr($url,2);
32        }
33        else
34        {
35                foreach( $roots as $k => $root)
36                        $url = str_replace('$'.$k.'/', $root, $url);
37        }
38
39        if (isset($q))
40        {
41                $url = get_root_url().'qsearch.php?q='.rawurlencode($q);
42        }
43
44        $suggestion['U_LINK'] = $url;
45        return $suggestion;
46}
47
48function rvac_ws_add_methods($srv_arr)
49{
50        global $conf;
51        $srv = $srv_arr[0];
52        include_once( dirname(__FILE__).'/../functions.inc.php' );
53        $srv->addMethod('rvac.addCustom', 'rv_ac_ws_add_custom_suggestion',
54                array(
55                        'name' => array(),
56                        'counter' => array('default'=>0),
57                        'url' => array('default'=>''),
58                        'level' => array('default'=>min($conf['available_permission_levels']), 'maxValue'=>max($conf['available_permission_levels']),'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
59                ),
60                '','', array('admin_only'=>true, 'post_only'=>true));
61
62        $srv->addMethod('rvac.modCustom', 'rv_ac_ws_mod_custom_suggestion',
63                array(
64                        'id' => array(),
65                        'name' => array('flags' => WS_PARAM_OPTIONAL),
66                        'counter' => array('flags' => WS_PARAM_OPTIONAL),
67                        'url' => array('flags' => WS_PARAM_OPTIONAL),
68                        'level' => array('flags' => WS_PARAM_OPTIONAL, 'maxValue'=>max($conf['available_permission_levels']),'type'=>WS_TYPE_INT|WS_TYPE_POSITIVE),
69                ),
70                '','', array('admin_only'=>true, 'post_only'=>true));
71
72        $srv->addMethod('rvac.delCustom', 'rv_ac_ws_del_custom_suggestion',
73                array('id'),
74                '','', array('admin_only'=>true, 'post_only'=>true));
75
76
77        $srv->addMethod('rvac.addVariant', 'rv_ac_ws_add_variant',
78                array(
79                        'in' => array(),
80                        'type' => array(),
81                        'out' => array(),
82                        'comment' => array('flags' => WS_PARAM_OPTIONAL),
83                ),
84                '','', array('admin_only'=>true, 'post_only'=>true));
85
86        $srv->addMethod('rvac.modVariant', 'rv_ac_ws_mod_variant',
87                array(
88                        'key' => array(),
89                        'in' => array(),
90                        'type' => array(),
91                        'out' => array(),
92                        'comment' => array('flags' => WS_PARAM_OPTIONAL),
93                ),
94                '','', array('admin_only'=>true, 'post_only'=>true));
95
96        $srv->addMethod('rvac.delVariant', 'rv_ac_ws_del_variant',
97                array(
98                        'key' => array(),
99                ),
100                '','', array('admin_only'=>true, 'post_only'=>true));
101
102}
103
104function rv_ac_ws_add_custom_suggestion($params, $service)
105{
106        $name = trim($params['name']);
107        if (empty($name))
108                return new PwgError(400, 'Bad name');
109
110        $insert = array('name'=>$name);
111
112        if (isset($params['counter']))
113                $insert['counter'] = intval($params['counter']);
114        if ( !empty($params['url']) )
115                $insert['url'] = $params['url'];
116        if ( isset($params['level']) )
117                $insert['level'] = $params['level'];
118
119        mass_inserts(RVAC_SUGGESTIONS, array_keys($insert), array($insert));
120        $id =  pwg_db_insert_id(RVAC_SUGGESTIONS);
121
122        rvac_invalidate_cache();
123        $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
124        rvac_custom_link($row, rvac_get_url_roots());
125        return $row;
126}
127
128function rv_ac_ws_mod_custom_suggestion($params, $service)
129{
130        $id = intval($params['id']);
131        $update = array();
132
133        if (!empty($params['name']))
134                $update['name'] = $params['name'];
135        if (isset($params['counter']))
136                $update['counter'] = intval($params['counter']);
137        if (isset($params['url']))
138                $update['url'] = $params['url'];
139        if ( isset($params['level']) )
140                $update['level'] = $params['level'];
141
142        single_update(RVAC_SUGGESTIONS,
143                $update,
144                array('id' => $id)
145                );
146        $changes = pwg_db_changes();
147
148        if ($changes)
149                rvac_invalidate_cache();
150
151        $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
152        rvac_custom_link($row, rvac_get_url_roots());
153        return $row;
154}
155
156function rv_ac_ws_del_custom_suggestion($params, $service)
157{
158        $id = intval($params['id']);
159        $q = 'DELETE FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id;
160        pwg_query($q);
161        $changes = pwg_db_changes();
162        if ($changes)
163                rvac_invalidate_cache();
164        return $changes;
165}
166
167
168
169
170function rvac_load_variant_rules()
171{
172        global $conf;
173        $file=PHPWG_ROOT_PATH.$conf['data_location'].'plugins/autocomplete_variants.dat';
174
175        $data = @unserialize( file_get_contents($file));
176        if ($data === false)
177                return array();
178        return $data;
179}
180
181function rvac_save_variant_rules($rules, $callback = null)
182{
183        global $conf, $persistent_cache;
184        $file=PHPWG_ROOT_PATH.$conf['data_location'].'plugins/autocomplete_variants.dat';
185        $save = serialize($rules);
186        if (@file_put_contents($file, $save)===false)
187        {
188                mkgetdir(dirname($file));
189                file_put_contents($file, $save);
190        }
191
192        $rmap = $amap = array();
193        foreach($rules as $rule)
194        {
195                foreach($rule['in'] as $in_word)
196                {
197                        $in_word_t = transliterate($in_word);
198                        $results = $rule['out'];
199                        $processed = array();
200                        if ('a' == $rule['type'])
201                                $processed[$in_word_t] = 1;
202
203                        for ($i=0; $i<count($results); $i++)
204                        {
205                                $outkey = $results[$i] == '$i' ? $in_word_t : transliterate($results[$i]);
206                                if (isset($processed[$outkey]))
207                                {
208                                        array_splice($results, $i, 1);
209                                        $i--;
210                                        continue;
211                                }
212                                $processed[$outkey] = 1;
213
214                                if ('$a' == $results[$i])
215                                {
216                                        array_splice($results, $i, 1, $rule['in']);
217                                        $i--;
218                                }
219                        }
220
221                        if ('r' == $rule['type'])
222                        {
223                                if (isset($rmap[$in_word_t]))
224                                {
225                                        if ($callback) $callback( array(
226                                                'word' => $in_word,
227                                                'wordt' => $in_word_t,
228                                                'msg' => ' defined as input several times in replace rules'
229                                        ));
230                                }
231                                else
232                                        $rmap[$in_word_t] = $results;
233                        }
234                        else
235                        {
236                                $results = array_diff($results, array($in_word,$in_word_t, '$i'));
237                                if (isset($amap[$in_word_t]))
238                                        $results = array_unique( array_merge($amap[$in_word_t], $results));
239                                $amap[$in_word_t] = $results;
240                        }
241                }
242        }
243
244        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/autocomplete_variants.dat';
245        $res = array('replace' => $rmap, 'add' => $amap );
246        $save = serialize($res);
247        if (@file_put_contents($file, $save)===false)
248        {
249                mkgetdir(dirname($file));
250                file_put_contents($file, $save);
251        }
252        $persistent_cache->purge(true);
253        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/autocomplete_variants.txt';
254        if ($fh = @fopen($file, 'w'))
255        {
256                fputcsv($fh, array("Type", "In", "Out"), "\t");
257                foreach($rmap as $in => $words)
258                        fputcsv($fh, array("r", $in, implode(',', $words)), "\t");
259                foreach($amap as $in => $words)
260                        fputcsv($fh, array("a", $in, implode(',', $words)), "\t");
261                fclose($fh);
262        }
263}
264
265function rv_ac_ws_add_variant($params, $service)
266{
267        return rvac_ws_add_or_mod_variant($params, true);
268}
269
270function rv_ac_ws_mod_variant($params, $service)
271{
272        return rvac_ws_add_or_mod_variant($params, false);
273}
274
275function rvac_ws_add_or_mod_variant($params, $is_add)
276{
277        $rules = rvac_load_variant_rules();
278
279        foreach( array('in', 'out') as $i => $name)
280        {
281                $arr = preg_split("/[\n,]+/", stripslashes($params[$name]) );
282                $arr = array_map('trim', $arr);
283                $arr = array_values( array_filter($arr, function($word) {
284                        return strlen($word)>0;
285                }) );
286                $arr = array_unique($arr);
287                $$name = $arr;
288        }
289        if (empty($in))
290                return new PwgError(WS_ERR_INVALID_PARAM, 'input word list empty');
291        if (empty($out))
292                return new PwgError(WS_ERR_INVALID_PARAM, 'output word list empty');
293
294        $in_trans = array_map('transliterate', $in);
295        sort($in_trans);
296        $key = implode(',', $in_trans);
297        if (strlen($key)>24)
298                $key = md5($key);
299
300        $type = $params['type'];
301        if ($type!='r' && $type!='a')
302                return new PwgError(WS_ERR_INVALID_PARAM, 'type');
303
304        if ($is_add)
305        {
306                if (isset($rules[$key]))
307                        return new PwgError(WS_ERR_INVALID_PARAM, 'list already defined');
308        }
309        else
310        {
311                $okey = stripslashes($params['key']);
312                if (!isset($rules[$okey]))
313                        return new PwgError(WS_ERR_INVALID_PARAM, 'no rule to update');
314                unset($rules[$okey]);
315        }
316
317        $rule = array(
318                'in' => $in,
319                'type' => $type,
320                'out' => $out,
321        );
322        if (!empty($params['comment']))
323                $rule['comment'] = stripslashes($params['comment']);
324        $rules[$key] = $rule;
325
326        $messages = array();
327        $callback = function($params) use($in_trans, &$messages) {
328                if (isset($params['wordt']))
329                {
330                        if (in_array($params['wordt'], $in_trans))
331                                $messages[] = $params['word'].' '.$params['msg'];
332                }
333                else
334                        $messages[] = $params['msg'];
335        };
336
337        rvac_save_variant_rules($rules, $callback);
338        $rule['key'] = $key;
339        return array(
340                'messages' => $messages,
341                'rule' => $rule,
342        );
343}
344
345function rv_ac_ws_del_variant($params, $service)
346{
347        $rules = rvac_load_variant_rules();
348
349        $key = stripslashes($params['key']);
350        if (empty($key))
351                return new PwgError(WS_ERR_INVALID_PARAM, 'empty key');
352
353        if (!isset($rules[$key]))
354                return true;
355
356        unset($rules[$key]);
357
358        rvac_save_variant_rules($rules);
359}
360?>
Note: See TracBrowser for help on using the repository browser.