1+ <?php
2+
3+ /*
4+ Copyright (c) 2012, ISPConfig UG
5+ All rights reserved.
6+
7+ Redistribution and use in source and binary forms, with or without modification,
8+ are permitted provided that the following conditions are met:
9+
10+ * Redistributions of source code must retain the above copyright notice,
11+ this list of conditions and the following disclaimer.
12+ * Redistributions in binary form must reproduce the above copyright notice,
13+ this list of conditions and the following disclaimer in the documentation
14+ and/or other materials provided with the distribution.
15+ * Neither the name of ISPConfig nor the names of its contributors
16+ may be used to endorse or promote products derived from this software without
17+ specific prior written permission.
18+
19+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+ IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+ */
30+
31+ require_once ('../../lib/config.inc.php ' );
32+ require_once ('../../lib/app.inc.php ' );
33+
34+ //* Check permissions for module
35+ $ app ->auth ->check_module_permissions ('dns ' );
36+
37+ $ app ->uses ('tform ' );
38+
39+ $ type = $ _GET ["type " ];
40+
41+ //if($_SESSION["s"]["user"]["typ"] == 'admin') {
42+
43+
44+ if ($ type == 'get_ipv4 ' ){
45+ $ q = $ app ->db ->quote (trim ($ _GET ["q " ]));
46+ $ authsql = " AND " .$ app ->tform ->getAuthSQL ('r ' );
47+ $ modules = explode (', ' , $ _SESSION ['s ' ]['user ' ]['modules ' ]);
48+
49+ $ result = array ();
50+
51+ // ipv4
52+ $ result [] = _search ('admin ' , 'server_ip ' , "AND ip_type = 'IPv4' AND (client_id = 0 OR client_id= " .intval ($ _SESSION ['s ' ]['user ' ]['client_id ' ]).") " );
53+
54+ $ json = $ app ->functions ->json_encode ($ result );
55+ }
56+
57+ if ($ type == 'get_ipv6 ' ){
58+ $ q = $ app ->db ->quote (trim ($ _GET ["q " ]));
59+ $ authsql = " AND " .$ app ->tform ->getAuthSQL ('r ' );
60+ $ modules = explode (', ' , $ _SESSION ['s ' ]['user ' ]['modules ' ]);
61+
62+ $ result = array ();
63+
64+ // ipv4
65+ $ result [] = _search ('admin ' , 'server_ip ' , "AND ip_type = 'IPv6' AND (client_id = 0 OR client_id= " .intval ($ _SESSION ['s ' ]['user ' ]['client_id ' ]).") " );
66+
67+ $ json = $ app ->functions ->json_encode ($ result );
68+ }
69+
70+ //}
71+
72+ function _search ($ module , $ section , $ additional_sql = '' ){
73+ global $ app , $ q , $ authsql , $ modules ;
74+
75+ $ result_array = array ('cheader ' => array (), 'cdata ' => array ());
76+ if (in_array ($ module , $ modules ) || ($ module == 'admin ' && $ section == 'server_ip ' )){
77+ $ search_fields = array ();
78+ $ desc_fields = array ();
79+ if (is_file ('../ ' .$ module .'/form/ ' .$ section .'.tform.php ' )){
80+ include_once ('../ ' .$ module .'/form/ ' .$ section .'.tform.php ' );
81+
82+ $ category_title = $ form ["title " ];
83+ $ form_file = $ form ["action " ];
84+ $ db_table = $ form ["db_table " ];
85+ $ db_table_idx = $ form ["db_table_idx " ];
86+ $ order_by = $ db_table_idx ;
87+
88+ if (is_array ($ form ["tabs " ]) && !empty ($ form ["tabs " ])){
89+ foreach ($ form ["tabs " ] as $ tab ){
90+ if (is_array ($ tab ['fields ' ]) && !empty ($ tab ['fields ' ])){
91+ foreach ($ tab ['fields ' ] as $ key => $ val ){
92+ if (isset ($ val ['searchable ' ]) && $ val ['searchable ' ] > 0 ){
93+ $ search_fields [] = $ key ." LIKE '% " .$ q ."%' " ;
94+ if ($ val ['searchable ' ] == 1 ){
95+ $ order_by = $ key ;
96+ $ title_key = $ key ;
97+ }
98+ if ($ val ['searchable ' ] == 2 ){
99+ $ desc_fields [] = $ key ;
100+ }
101+ }
102+ }
103+ }
104+ }
105+ }
106+ }
107+ unset($ form );
108+
109+ $ where_clause = '' ;
110+ if (!empty ($ search_fields )){
111+ $ where_clause = implode (' OR ' , $ search_fields );
112+ } else {
113+ // valid SQL query which returns an empty result set
114+ $ where_clause = '1 = 0 ' ;
115+ }
116+ if ($ where_clause != '' ) $ where_clause = '( ' .$ where_clause .') ' ;
117+ if ($ additional_sql != '' ) $ where_clause .= ' ' .$ additional_sql .' ' ;
118+ $ order_clause = '' ;
119+ if ($ order_by != '' ) $ order_clause = ' ORDER BY ' .$ order_by ;
120+
121+ $ sql = "SELECT * FROM " .$ db_table ." WHERE " .$ where_clause .$ authsql .$ order_clause ." LIMIT 0,10 " ;
122+ $ results = $ app ->db ->queryAllRecords ($ sql );
123+
124+ if (is_array ($ results ) && !empty ($ results )){
125+ $ lng_file = '../ ' .$ module .'/lib/lang/ ' .$ _SESSION ['s ' ]['language ' ].'_ ' .$ section .'.lng ' ;
126+ if (is_file ($ lng_file )) include ($ lng_file );
127+ $ result_array ['cheader ' ] = array ('title ' => $ category_title ,
128+ 'total ' => count ($ results ),
129+ 'limit ' => count ($ results )
130+ );
131+ foreach ($ results as $ result ){
132+ $ description = '' ;
133+ if (!empty ($ desc_fields )){
134+ $ desc_items = array ();
135+ foreach ($ desc_fields as $ desc_field ){
136+ if ($ result [$ desc_field ] != '' ) $ desc_items [] = $ wb [$ desc_field .'_txt ' ].': ' .$ result [$ desc_field ];
137+ }
138+ if (!empty ($ desc_items )) $ description = implode (' - ' , $ desc_items );
139+ }
140+
141+ $ result_array ['cdata ' ][] = array ( 'title ' => $ wb [$ title_key .'_txt ' ].': ' .$ result [$ title_key ],
142+ 'description ' => $ description ,
143+ 'onclick ' => '' ,
144+ 'fill_text ' => $ result [$ title_key ]
145+ );
146+ }
147+ }
148+ }
149+ return $ result_array ;
150+ }
151+
152+ header ('Content-type: application/json ' );
153+ echo $ json ;
154+ ?>
0 commit comments