Completed
Push — master ( f23246...243457 )
by Michael
9s
created

include/functions.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * ****************************************************************************
4
 * xsitemap - MODULE FOR XOOPS CMS
5
 *
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 * ****************************************************************************
13
 */
14
/**
15
 * Module: xsitemap
16
 *
17
 * @package    module\xsitemap\includes
18
 * @author     XOOPS Module Development Team
19
 * @author     Urbanspaceman (http://www.takeaweb.it)
20
 * @copyright  Urbanspaceman (http://www.takeaweb.it)
21
 * @copyright  XOOPS Project (http://xoops.org)
22
 * @license    http://www.fsf.org/copyleft/gpl.html GNU public license
23
 * @link       http://xoops.org XOOPS
24
 * @since      1.00
25
 */
26
27
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
28
29
if (!isset($moduleDirName)) {
30
    $moduleDirName = basename(dirname(__DIR__));
31
}
32
xoops_loadLanguage('admin', $moduleDirName);
33
if (!class_exists(ucfirst($moduleDirName) . 'DummyObject')) {
34
    xoops_load('dummy', $moduleDirName);
35
}
36
/**
37
 *
38
 * Show Site map
39
 *
40
 * @return array
41
 */
42
function xsitemapGenerateSitemap()
43
{
44
    $block         = array();
45
    $moduleDirName = basename(dirname(__DIR__));
46
    /** @internal can't use Helper since function called during install
47
     * $xsitemapHelper = \Xmf\Module\Helper::getHelper($moduleDirName);
48
     * $pluginHandler  = $xsitemapHelper->getHandler('plugin', $moduleDirName);
49
     */
50
    xoops_load('plugin', $moduleDirName);
51
52
    // Get list of modules admin wants to hide from xsitemap
53
    $invisibleDirnames = empty($GLOBALS['xoopsModuleConfig']['invisible_dirnames']) ? array('xsitemap') : explode(',', $GLOBALS['xoopsModuleConfig']['invisible_dirnames'] . ',xsitemap');
54
    $invisibleDirnames = array_map('trim', $invisibleDirnames);
55
    $invisibleDirnames = array_map('mb_strtolower', $invisibleDirnames);
56
57
    // Get the mid for any of these modules if they're active and hasmain (visible frontside)
58
    $moduleHandler     = xoops_getHandler('module');
59
    $invisibleMidArray = array();
60
    foreach ($invisibleDirnames as $hiddenDir) {
61
        $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
62
        $criteria->add(new Criteria('isactive', 1));
63
        $criteria->add(new Criteria('name', $hiddenDir));
64
        $modObj = $moduleHandler->getByDirname($hiddenDir);
65
        if (false !== $modObj && $modObj instanceof XoopsModule) {
66
            $invisibleMidArray[] = $modObj->mid();
67
        }
68
    }
69
70
    // Where user has permissions
71
    $modulepermHandler = xoops_getHandler('groupperm');
72
    $groups            = ($GLOBALS['xoopsUser'] instanceof XoopsUser) ? $GLOBALS['xoopsUser']->getGroups() : XOOPS_GROUP_ANONYMOUS;
73
    $readAllowed       = $modulepermHandler->getItemIds('module_read', $groups);
74
    $filteredMids      = array_diff($readAllowed, $invisibleMidArray);
75
    $pluginHandler     = xoops_getModuleHandler('plugin', $moduleDirName);
76
    $criteria          = new CriteriaCompo(new Criteria('hasmain', 1));
77
    $criteria->add(new Criteria('isactive', 1));
78
    if (count($filteredMids) > 0) {
79
        $criteria->add(new Criteria('mid', '(' . implode(',', $filteredMids) . ')', 'IN'));
80
    }
81
82
    $modules = $moduleHandler->getObjects($criteria, true);
83
84
    $criteria = new CriteriaCompo();
85
    $criteria->setSort('plugin_id');
86
    $criteria->order = 'ASC';
87
    $pluginObjArray  = $pluginHandler->getAll($criteria);
88
89
    foreach ($modules as $mid => $modObj) {
90
        $sublinks               = $modObj->subLink();
91
        $modDirName             = $modObj->getVar('dirname', 'n');
92
        $block['modules'][$mid] = array(
93
            'id'        => $mid,
94
            'name'      => $modObj->getVar('name'),
95
            'directory' => $modDirName,
96
            'sublinks'  => array()
97
            // init the sublinks array
98
        );
99
        // Now 'patch' the sublink to include module path
100
        if (count($sublinks) > 0) {
101
            foreach ($sublinks as $sublink) {
102
                $block['modules'][$mid]['sublinks'][] = array(
103
                    'name' => $sublink['name'],
104
                    'url'  => $GLOBALS['xoops']->url("www/modules/{$modDirName}/{$sublink['url']}")
105
                );
106
            }
107
        }
108
109
        foreach ($pluginObjArray as $pObj) {
110
            if ((0 == $pObj->getVar('topic_pid')) && in_array($pObj->getVar('plugin_mod_table'), (array)$modObj->getInfo('tables'))) {
111
                $objVars = $pObj->getValues();
112
                if (1 == $objVars['plugin_online']) {
113
                    $tmpMap                           = xsitemap_get_map($objVars['plugin_mod_table'], $objVars['plugin_cat_id'], $objVars['plugin_cat_pid'], $objVars['plugin_cat_name'],
114
                                                                         $objVars['plugin_call'], $objVars['plugin_weight']);
115
                    $block['modules'][$mid]['parent'] = isset($tmpMap['parent']) ? $tmpMap['parent'] : null;
116
                }
117
            }
118
        }
119
    }
120
121
    return $block;
122
}
123
124
/**
125
 * Get the Sitemap
126
 *
127
 * @param        $table
128
 * @param        $id_name
129
 * @param        $pid_name
130
 * @param        $title_name
131
 * @param        $url
132
 * @param string $order
133
 * @return array sitemap links
134
 */
135
function xsitemap_get_map($table, $id_name, $pid_name, $title_name, $url, $order = '')
136
{
137
    $xDB  = XoopsDatabaseFactory::getDatabaseConnection();
138
    $myts = MyTextSanitizer::getInstance();
139
140
    $sql       = "SELECT `{$id_name}`, `{$pid_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table}";
141
    $result    = $xDB->query($sql);
142
    $objsArray = array();
143
144
    while ($row = $xDB->fetchArray($result)) {
145
        $objsArray[] = new XsitemapDummyObject($row, $id_name, $pid_name, $title_name);
146
    }
147
148
    //$sql = "SELECT `{$id_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table} WHERE `{$pid_name}`= 0";
149
    // v1.54 added in the event categories are flat (don't support hierarchy)
150
    $sql = "SELECT `{$id_name}`, `{$title_name}` FROM " . $xDB->prefix . "_{$table}";
151
    if ($pid_name !== $id_name) {
152
        $sql .= " WHERE `{$pid_name}`= 0";
153
    }
154
    if ('' != $order) {
155
        $sql .= " ORDER BY `{$order}`";
156
    }
157
    $result = $xDB->query($sql);
158
159
    $i        = 0;
160
    $xsitemap = array();
161
    while (list($catid, $name) = $xDB->fetchRow($result)) {
162
        $xsitemap['parent'][$i] = array(
163
            'id'    => $catid,
164
            'title' => $myts->htmlSpecialChars($name),
165
            'url'   => $url . $catid
166
        );
167
168
        if ($GLOBALS['xoopsModuleConfig']['show_subcategories'] && ($pid_name !== $id_name)) {
169
            $j           = 0;
170
            $mytree      = new XoopsObjectTree($objsArray, $id_name, $pid_name);
171
            $child_array = $mytree->getAllChild($catid);
172
            foreach ($child_array as $child) {
173
                $xsitemap['parent'][$i]['child'][$j] = array(
174
                    'id'    => $child->getVar($id_name),
175
                    'title' => $child->getVar($title_name),
176
                    'url'   => $url . $child->getVar($id_name)
177
                );
178
                ++$j;
179
            }
180
        }
181
        ++$i;
182
    }
183
184
    return $xsitemap;
185
}
186
187
/**
188
 * Save the XML Sitemap
189
 *
190
 * @param array $xsitemap_show
191
 * @return mixed int number of bytes saved | false on failure
192
 */
193
function xsitemap_save(array $xsitemap_show)
194
{
195
    $xml                     = new DOMDocument('1.0', 'UTF-8');
196
    $xml->preserveWhiteSpace = false;
197
    $xml->formatOutput       = true;
198
    $xml_set                 = $xml->createElement('urlset');
199
    $xml_set->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
200
201
    if (!empty($xsitemap_show)) {
202
        foreach ($xsitemap_show['modules'] as $mod) {
203
            if ($mod['directory']) {
204
                $xml_url = $xml->createElement('url');
205
                $xml_url->appendChild($xml->createComment(htmlentities(ucwords($mod['name']))));
206
                //                $xml_url->appendChild($xml->createTextNode("<!-- ". htmlentities(ucwords($mod['name'])) . " -->"));
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
207
                $loc = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/index.php")));
208
                $xml_url->appendChild($loc);
209
            }
210
            if (isset($mod['parent']) ? $mod['parent'] : null) {
211 View Code Duplication
                foreach ($mod['parent'] as $parent) {
212
                    $xml_parent = $xml->createElement('url');
213
                    $loc        = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/{$parent['url']}")));
214
                    $xml_parent->appendChild($loc);
215
                    $xml_url->appendChild($xml_parent);
0 ignored issues
show
The variable $xml_url does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
216
                }
217
                $z = 0;
218
                //if ($mod["parent"][$z]["child"]) {
219
                if (isset($mod['parent'][$z]['child']) ? $mod['parent'][$z]['child'] : null) {
220 View Code Duplication
                    foreach ($mod['parent'][$z]['child'] as $child) {
221
                        $xml_child = $xml->createElement('url');
222
                        $loc       = $xml->createElement('loc', htmlentities($GLOBALS['xoops']->url("www/modules/{$mod['directory']}/{$child['url']}")));
223
                        $xml_child->appendChild($loc);
224
                        $xml_url->appendChild($xml_child);
225
                    }
226
                    ++$z;
227
                }
228
            }
229
            $xml_set->appendChild($xml_url);
230
        }
231
    }
232
    $xml->appendChild($xml_set);
233
    return $xml->save($GLOBALS['xoops']->path('www/xsitemap.xml'));
234
}
235