1 | <?php |
---|
2 | |
---|
3 | define('PHPWG_ROOT_PATH','../../'); |
---|
4 | include_once( PHPWG_ROOT_PATH.'include/common.inc.php' ); |
---|
5 | |
---|
6 | if (!defined('RVAC_ID')) { |
---|
7 | set_status_header(501); |
---|
8 | exit; |
---|
9 | } |
---|
10 | |
---|
11 | include_once( 'functions.inc.php' ); |
---|
12 | |
---|
13 | $page['root_path'] = get_absolute_root_url(false); |
---|
14 | |
---|
15 | $context = @$_REQUEST['context']; |
---|
16 | |
---|
17 | if (empty($context) || strncmp($context, '$t/',3)) |
---|
18 | bad_request("context"); |
---|
19 | |
---|
20 | $tag_ids = array(); |
---|
21 | $tag_url_names = array(); |
---|
22 | foreach( explode('/', $context) as $tag_token) |
---|
23 | { |
---|
24 | if ( $conf['tag_url_style'] != 'tag' and preg_match('/^(\d+)(?:-(.*)|)$/', $tag_token, $matches) ) |
---|
25 | { |
---|
26 | $tag_ids[] = $matches[1]; |
---|
27 | } |
---|
28 | else |
---|
29 | { |
---|
30 | $tag_url_names[] = $tag_token; |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | $context_tags = find_tags($tag_ids, $tag_url_names); |
---|
35 | if ( empty($context_tags) ) |
---|
36 | { |
---|
37 | bad_request('no context content'); |
---|
38 | } |
---|
39 | |
---|
40 | $context_tag_ids = array_map( function($tag) { return $tag['id']; }, $context_tags); |
---|
41 | $context_image_ids = get_image_ids_for_tags($context_tag_ids); |
---|
42 | |
---|
43 | $tags = get_common_tags($context_image_ids, -1, $context_tag_ids); |
---|
44 | usort($tags, function($a,$b) { |
---|
45 | $d = -$a['counter'] + $b['counter']; |
---|
46 | if (!$d) $d = tag_alpha_compare($a,$b); |
---|
47 | return $d; |
---|
48 | }); |
---|
49 | |
---|
50 | // remove non filtering tags |
---|
51 | $max = intval(0.9*count($context_image_ids)); |
---|
52 | while( count($tags) && $tags[0]['counter'] >= $max) |
---|
53 | array_shift($tags); |
---|
54 | $tags = trigger_event('rvac_filter_context_tags', $tags); |
---|
55 | |
---|
56 | $suggestions = array(); |
---|
57 | foreach($tags as $tag) |
---|
58 | { |
---|
59 | $label = $tag['name']; |
---|
60 | |
---|
61 | $url = $context.'/'; |
---|
62 | switch ( $conf['tag_url_style'] ) |
---|
63 | { |
---|
64 | case 'id': |
---|
65 | $url.= $tag['id']; |
---|
66 | break; |
---|
67 | case 'tag': |
---|
68 | if (!is_numeric($tag['url_name']) ) |
---|
69 | { |
---|
70 | $url.= $tag['url_name']; |
---|
71 | break; |
---|
72 | } |
---|
73 | default: |
---|
74 | $url.= $tag['id'].'-'.$tag['url_name']; |
---|
75 | } |
---|
76 | |
---|
77 | $suggestion = array( |
---|
78 | 'label' => $label, |
---|
79 | 'value' => $url, |
---|
80 | 'w' => intval($tag['counter']) |
---|
81 | ); |
---|
82 | |
---|
83 | $q = rvac_normalize($label); |
---|
84 | if ($q != strtolower($label)) |
---|
85 | $suggestion['q'] = $q; |
---|
86 | |
---|
87 | $suggestions[] = $suggestion; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | $ds = str_replace( |
---|
93 | array('\\/', '"q":', '"label":', '"value":', '"w":'), |
---|
94 | array('/', 'q:', 'label:', 'value:', 'w:'), |
---|
95 | json_encode($suggestions)); |
---|
96 | |
---|
97 | $js = 'RVAC.mergeData({dataSource:'.$ds.', |
---|
98 | ctxq:'.json_encode((string)@$_REQUEST['ctxq']).', |
---|
99 | ctxl:'.json_encode((string)@$_REQUEST['ctxl']).', |
---|
100 | ctxw:'.count($context_image_ids).' |
---|
101 | })'; |
---|
102 | |
---|
103 | header("Content-Type: application/javascript; charset=".get_pwg_charset()); |
---|
104 | $expires = time() + 300; |
---|
105 | header("Pragma:"); |
---|
106 | header("Cache-control: private, max-age=300"); |
---|
107 | header('Expires: '.gmdate('D, d M Y H:i:s', $expires).' GMT'); |
---|
108 | |
---|
109 | echo $js; |
---|
110 | |
---|
111 | ?> |
---|