Issues (1149)

search.php (4 issues)

1
<?php
2
/*
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * @copyright    {@link https://xoops.org/ XOOPS Project}
14
 * @license      {@link http://www.gnu.org/licenses/gpl-2.0.html GNU GPL 2 or later}
15
 * @package      efqdirectory
16
 * @since
17
 * @author       Martijn Hertog (aka wtravel)
18
 * @author       XOOPS Development Team,
19
 */
20
21
include __DIR__ . '/header.php';
22
$myts = MyTextSanitizer::getInstance(); // MyTextSanitizer object
23
require_once XOOPS_ROOT_PATH . '/class/xoopstree.php';
24
require_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
25
$myts   = MyTextSanitizer::getInstance();
26
$mytree = new XoopsTree($xoopsDB->prefix('links_cat'), 'cid', 'pid');
27
$eh     = new ErrorHandler;
28
29
$moddir = $xoopsModule->getVar('dirname');
30
include XOOPS_ROOT_PATH . '/header.php';
31
32
if (isset($_GET['catid'])) {
33
    $get_cid = (int)$_GET['cid'];
34
} else {
35
    $get_cid = '1';
36
}
37
if (isset($_GET['dirid'])) {
38
    $get_dirid = (int)$_GET['dirid'];
39
} else {
40
    $get_dirid = '1';
41
}
42 View Code Duplication
if (isset($_GET['orderby'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    $orderby = convertOrderByIn($_GET['orderby']);
44
} else {
45
    $orderby = 'title ASC';
46
}
47
if (isset($_GET['page'])) {
48
    $get_page = (int)$_GET['page'];
49
} else {
50
    $get_page = 1;
51
}
52
$GLOBALS['xoopsOption']['template_main'] = 'efqdiralpha1_search.tpl';
53
$xoopsTpl->assign('xoops_module_header', $xoops_module_header);
54
$lang_adv_search = sprintf(_MD_SEARCH_ADV, $get_dirid);
55
56
ob_start();
57
$searchform = '<form action="search.php" name="search" id="search" method="get">';
58
$searchform .= '<input type="hidden" name="dirid" value="' . $get_dirid . '"><input type="text" name="q" size="40" maxsize="150" value=""><input type="submit" id="submit" value="' . _MD_SEARCH . '">' . $lang_adv_search . '</form>';
59
echo $searchform;
60
if (!empty($_GET['q'])) {
61
    //get search results from query
62
    if (isset($_GET['q'])) {
63
        $querystring = $GLOBALS['xoopsDB']->escape($myts->stripSlashesGPC($_GET['q']));
64
        //echo $querystring."<br>";
65
    } else {
66
        redirect_header(XOOPS_URL . "/modules/$moddir/search.php", 2, _MD_NO_SEARCH_STRING_SELECTED);
67
    }
68
    $poscount   = substr_count($querystring, '"') / 2;
69
    $specialarr = array();
70 View Code Duplication
    for ($i = 0; $i < $poscount; ++$i) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
        $start = strpos($querystring, '"', 0);
72
        $end   = strpos($querystring, '"', $start + 1);
73
        if ($end !== false) {
74
            $specialstring = ltrim(substr($querystring, $start, $end - $start), '"');
75
            $specialarr[]  = $specialstring;
76
            $querystring   = ltrim(substr_replace($querystring, '', $start, $end - $start + 1));
77
        } else {
78
            $querystring = ltrim(substr_replace($querystring, '', $start, 1));
79
        }
80
    }
81
    $queryarr   = explode(' ', $querystring);
82
    $queryarr   = array_merge($specialarr, $queryarr);
83
    $emptyarr[] = '';
84
    $querydiff  = array_diff($queryarr, $emptyarr);
85
86
    $limit  = $xoopsModuleConfig['searchresults_perpage'];
87
    $offset = ($get_page - 1) * $limit;
88
89
    $andor         = 'AND';
90
    $searchresults = mod_search($querydiff, $andor, $limit, $offset);
91
    $maxpages      = 10;
92
    $maxcount      = 30;
93
94
    $count_results = mod_search_count($querydiff, $andor, $maxcount, 0);
95
    $count_pages   = 0;
96
    //Calculate the number of result pages.
97
    if ($count_results > $limit) {
98
        $count_pages = ceil($count_results / $limit);
99
    }
100
    $pages_text = '';
101
    $pages_text .= $count_results . ' ' . _MD_LISTINGS_FOUND . '<br>';
102
103
    if ($count_pages >= 2) {
104
        $pages_text .= '<a href="search.php?q=' . $querystring . '&page=1">1</a>';
105
    }
106
    for ($i = 1; $i < $count_pages; ++$i) {
107
        $page       = $i + 1;
108
        $pages_text .= ' - <a href="search.php?q=' . $querystring . '&page=' . $page . '">' . $page . '</a>';
109
    }
110
111
    echo '<div class="itemTitleLarge">' . _MD_SEARCHRESULTS_TITLE . '</div><br>';
112
    if ($searchresults == 0) {
113
        echo '<div class="itemTitle">' . _MD_NORESULTS . '</div>';
114
    } else {
115 View Code Duplication
        foreach ($searchresults as $result) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
            echo '<div class="itemTitle"><a href="' . $result['link'] . '">' . $result['title'] . '</a></div><div class="itemText">' . $result['description'] . '</div><hr>';
117
        }
118
    }
119
    echo '<br>';
120
    echo $pages_text;
121
}
122
$xoopsTpl->assign('search_page', ob_get_contents());
123
ob_end_clean();
124
125
include XOOPS_ROOT_PATH . '/footer.php';
126
127
/**
128
 * @param $queryarray
129
 * @param $andor
130
 * @param $limit
131
 * @param $offset
132
 * @return array|int
133
 */
134
function mod_search($queryarray, $andor, $limit, $offset)
135
{
136
    global $xoopsDB, $eh;
137
    $sql = 'SELECT DISTINCT i.itemid, i.title, i.uid, i.created, t.description FROM '
138
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
139
           . ' d RIGHT JOIN '
140
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
141
           . ' i ON (d.itemid=i.itemid) LEFT JOIN '
142
           . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
143
           . " t ON (i.itemid=t.itemid) WHERE i.status='2'";
144
    // because count() returns 1 even if a supplied variable
145
    // is not an array, we must check if $queryarray is really an array
146
    if (is_array($queryarray) && $count = count($queryarray)) {
147
        $sql .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
148
        for ($i = 1; $i < $count; ++$i) {
149
            $sql .= " $andor ";
150
            $sql .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
151
        }
152
        $sql .= ') ';
153
    }
154
    $sql .= 'ORDER BY i.created DESC';
155
156
    $result      = $xoopsDB->query($sql, $limit, $offset) or $eh->show('0013');
157
    $num_results = $xoopsDB->getRowsNum($result);
158
    if (!$result) {
159
        return 0;
160
    } elseif ($num_results == 0) {
161
        return 0;
162
    } else {
163
        $ret = array();
164
        $i   = 0;
165
        while ($myrow = $xoopsDB->fetchArray($result)) {
166
            $ret[$i]['image']       = 'images/home.gif';
167
            $ret[$i]['link']        = 'listing.php?item=' . $myrow['itemid'] . '';
168
            $ret[$i]['title']       = $myrow['title'];
169
            $ret[$i]['description'] = $myrow['description'];
170
            $ret[$i]['time']        = $myrow['created'];
171
            $ret[$i]['uid']         = $myrow['uid'];
172
            ++$i;
173
        }
174
175
        return $ret;
176
    }
177
}
178
179
/**
180
 * @param     $queryarray
181
 * @param     $andor
182
 * @param     $limit
183
 * @param int $offset
184
 * @return int|void
185
 */
186 View Code Duplication
function mod_search_count($queryarray, $andor, $limit, $offset = 0)
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
187
{
188
    global $xoopsDB, $eh;
189
    $count = 0;
190
    $sql   = 'SELECT COUNT(DISTINCT i.itemid) FROM '
191
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_data')
192
             . ' d, '
193
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_items')
194
             . ' i LEFT JOIN '
195
             . $xoopsDB->prefix($module->getVar('dirname', 'n') . '_item_text')
196
             . " t ON (i.itemid=t.itemid) WHERE d.itemid=i.itemid AND i.status='2'";
197
    // because count() returns 1 even if a supplied variable
198
    // is not an array, we must check if $queryarray is really an array
199
    if (is_array($queryarray) && $count = count($queryarray)) {
200
        $sql .= " AND ((d.value LIKE '%$queryarray[0]%' OR i.title LIKE '%$queryarray[0]%' OR t.description LIKE '%$queryarray[0]%')";
201
        for ($i = 1; $i < $count; ++$i) {
202
            $sql .= " $andor ";
203
            $sql .= "(d.value LIKE '%$queryarray[$i]%' OR i.title LIKE '%$queryarray[$i]%' OR t.description LIKE '%$queryarray[$i]%')";
204
        }
205
        $sql .= ') ';
206
    }
207
    $result = $xoopsDB->query($sql) or $eh->show('0013');
208
    list($count) = $xoopsDB->fetchRow($result);
209
210
    return $count;
211
}
212