Completed
Pull Request — master (#5)
by Michael
03:10
created

ratelink.php (15 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
// $Id: ratelink.php 11819 2013-07-09 18:21:40Z zyspec $
3
//  ------------------------------------------------------------------------ //
4
//                XOOPS - PHP Content Management System                      //
5
//                    Copyright (c) 2000 XOOPS.org                           //
6
//                       <http://www.xoops.org/>                             //
7
// ------------------------------------------------------------------------- //
8
//  This program is free software; you can redistribute it and/or modify     //
9
//  it under the terms of the GNU General Public License as published by     //
10
//  the Free Software Foundation; either version 2 of the License, or        //
11
//  (at your option) any later version.                                      //
12
//                                                                           //
13
//  You may not change or alter any portion of this comment or credits       //
14
//  of supporting developers from this source code or any supporting         //
15
//  source code which is considered copyrighted (c) material of the          //
16
//  original comment or credit authors.                                      //
17
//                                                                           //
18
//  This program is distributed in the hope that it will be useful,          //
19
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21
//  GNU General Public License for more details.                             //
22
//                                                                           //
23
//  You should have received a copy of the GNU General Public License        //
24
//  along with this program; if not, write to the Free Software              //
25
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26
//  ------------------------------------------------------------------------ //
27
include 'header.php';
28
//include_once XOOPS_ROOT_PATH . '/class/module.errorhandler.php';
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% 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...
29
$myts =& MyTextSanitizer::getInstance(); // MyTextSanitizer object
30
include_once './class/utility.php';
31
//xoops_load('utility', $xoopsModule->getVar('dirname'));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
32
33
34
if (!empty($_POST['submit'])) {
35
    global $xoopsDB;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
36
37
    $ip     = getenv('REMOTE_ADDR');
38
    $lid    = mylinksUtility::mylinks_cleanVars($_POST, 'lid', 0, 'int', array('min'=>0));
0 ignored issues
show
'lid' is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
    $cid    = mylinksUtility::mylinks_cleanVars($_POST, 'cid', 0, 'int', array('min'=>0));
0 ignored issues
show
'cid' is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
    $rating = mylinksUtility::mylinks_cleanVars($_POST, 'rating', 0, 'int', array('min'=>0));
0 ignored issues
show
'rating' is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
    // make sure listing is active
43
    $result=$xoopsDB->query('SELECT COUNT(*) FROM ' . $xoopsDB->prefix('mylinks_links') . " WHERE lid={$lid} AND status>0");
44
    if (!$xoopsDB->fetchRow($result)) {
45
        redirect_header($_SERVER['PHP_SELF'], 3, _MD_MYLINKS_NORECORDFOUND);
46
        exit();
47
    }
48
49
//    $eh = new ErrorHandler; //ErrorHandler object
50
    $ratinguser = empty($xoopsUser) ? 0 : $xoopsUser->getVar('uid');
51
52
    //Make sure only 1 anonymous from an IP in a single day.
53
    $anonwaitdays = 1;
54
55
    // Check if Rating is Null
56
//    if ( '--' == $rating ) {  //bugfix since rating is an int from input filtering
57
    if ($rating <= 0) {
58
        redirect_header("ratelink.php?cid={$cid}&amp;lid={$lid}", 4, _MD_MYLINKS_NORATING);
59
        exit();
60
    } elseif ($rating > 10) {
61
        $rating = 10;
62
    }
63
64
    // Check if Link POSTER is voting (UNLESS Anonymous users allowed to post)
65
    if ($ratinguser != 0) {
66
        $result=$xoopsDB->query('SELECT submitter FROM ' . $xoopsDB->prefix('mylinks_links') . " WHERE lid={$lid}");
67 View Code Duplication
        while(list($ratinguserDB) = $xoopsDB->fetchRow($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...
68
            if ($ratinguserDB == $ratinguser) {
69
                redirect_header('index.php', 4, _MD_MYLINKS_CANTVOTEOWN);
70
                exit();
71
            }
72
        }
73
74
        // Check if REG user is trying to vote twice.
75
        $result=$xoopsDB->query('SELECT ratinguser FROM ' . $xoopsDB->prefix('mylinks_votedata') . " WHERE lid={$lid}");
76 View Code Duplication
        while(list($ratinguserDB) = $xoopsDB->fetchRow($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...
77
            if ($ratinguserDB == $ratinguser) {
78
                redirect_header('index.php', 4, _MD_MYLINKS_VOTEONCE2);
79
                exit();
80
            }
81
        }
82
83
    } else {
84
85
        // Check if ANONYMOUS user is trying to vote more than once per day.
86
        $yesterday = (time()-(86400 * $anonwaitdays));
87
        $result=$xoopsDB->query('SELECT COUNT(*) FROM ' . $xoopsDB->prefix('mylinks_votedata') . " WHERE lid={$lid} AND ratinguser=0 AND ratinghostname = '{$ip}' AND ratingtimestamp > {$yesterday}");
88
        list($anonvotecount) = $xoopsDB->fetchRow($result);
89
        if ($anonvotecount > 0) {
90
            redirect_header('index.php', 4, _MD_MYLINKS_VOTEONCE2);
91
            exit();
92
        }
93
    }
94
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
95
    if($rating > 10){
96
        $rating = 10;
97
    }
98
*/
99
    //All is well.  Add to Line Item Rate to DB.
100
    $newid = $xoopsDB->genId($xoopsDB->prefix('mylinks_votedata') . '_ratingid_seq');
101
    $datetime = time();
102
    $sql = sprintf("INSERT INTO %s (ratingid, lid, ratinguser, rating, ratinghostname, ratingtimestamp) VALUES (%u, %u, %u, %u, '%s', %u)", $xoopsDB->prefix('mylinks_votedata'), $newid, $lid, $ratinguser, $rating, $ip, $datetime);
103
    $result = $xoopsDB->query($sql);
104
    if (!$result) {
105
        mylinksUtility::show_message(_MD_MYLINKS_DBNOTUPDATED);
0 ignored issues
show
Deprecated Code introduced by
The method mylinksUtility::show_message() has been deprecated.

This method has been deprecated.

Loading history...
106
        exit();
107
    }
108
109
    //All is well.  Calculate Score & Add to Summary (for quick retrieval & sorting) to DB.
110
    updaterating($lid);
0 ignored issues
show
It seems like $lid defined by \mylinksUtility::mylinks...nt', array('min' => 0)) on line 38 can also be of type double; however, updaterating() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
111
    $ratemessage = _MD_MYLINKS_VOTEAPPRE . '<br>' . sprintf(_MD_MYLINKS_THANKURATE, htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES));
112
    redirect_header('index.php', 2, $ratemessage);
113
    exit();
114
115
} else {
116
117
    $xoopsOption['template_main'] = 'mylinks_ratelink.html';
118
    include XOOPS_ROOT_PATH . '/header.php';
119
120
    //wanikoo
121
    $xoTheme->addStylesheet('browse.php?' . mylinksGetStylePath('mylinks.css', 'include'));
122
    $xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');
123
    $xoTheme->addScript('browse.php?' . mylinksGetStylePath('mylinks.js', 'include'));
124
125
    $lid    = mylinksUtility::mylinks_cleanVars($_GET, 'lid', 0, 'int', array('min'=>0));
0 ignored issues
show
'lid' is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
    $cid    = mylinksUtility::mylinks_cleanVars($_GET, 'cid', 0, 'int', array('min'=>0));
0 ignored issues
show
'cid' is of type string, but the function expects a object<unknown_type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
    $result=$xoopsDB->query('SELECT title FROM ' . $xoopsDB->prefix('mylinks_links') . " WHERE lid={$lid}");
128
    //TODO:  need error checking here in case invalid lid
129
    list($title) = $xoopsDB->fetchRow($result);
130
    $xoopsTpl->assign('link', array('id' => $lid, 'cid' => $cid, 'title' => $myts->htmlSpecialChars($myts->stripSlashesGPC($title))));
131
    $xoopsTpl->assign('lang_voteonce', _MD_MYLINKS_VOTEONCE);
132
    $xoopsTpl->assign('lang_ratingscale', _MD_MYLINKS_RATINGSCALE);
133
    $xoopsTpl->assign('lang_beobjective', _MD_MYLINKS_BEOBJECTIVE);
134
    $xoopsTpl->assign('lang_donotvote', _MD_MYLINKS_DONOTVOTE);
135
    $xoopsTpl->assign('lang_rateit', _MD_MYLINKS_RATEIT);
136
    $xoopsTpl->assign('lang_cancel', _CANCEL);
137
138
    //wanikoo theme changer
139
    $xoopsTpl->assign('lang_themechanger', _MD_MYLINKS_THEMECHANGER);
140
    $mymylinkstheme_options = '';
141
142 View Code Duplication
    foreach ($GLOBALS['mylinks_allowed_theme'] as $mymylinkstheme) {
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...
143
        $mymylinkstheme_options .= "<option value='{$mymylinkstheme}'";
144
        if ($mymylinkstheme == $GLOBALS['mylinks_theme']) {
145
            $mymylinkstheme_options .= " selected='selected'";
146
        }
147
        $mymylinkstheme_options .= ">{$mymylinkstheme}</option>";
148
    }
149
150
    $mylinkstheme_select = "<select name='mylinks_theme_select' onchange='submit();' size='1'>{$mymylinkstheme_options}</select>";
151
152
    $xoopsTpl->assign('mylinksthemeoption', $mylinkstheme_select);
153
154
    //wanikoo search
155 View Code Duplication
    if (file_exists(XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/search.php')) {
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...
156
       include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/search.php';
157
    } else {
158
       include_once XOOPS_ROOT_PATH.'/language/english/search.php';
159
    }
160
    $xoopsTpl->assign('lang_all', _SR_ALL);
161
    $xoopsTpl->assign('lang_any', _SR_ANY);
162
    $xoopsTpl->assign('lang_exact', _SR_EXACT);
163
    $xoopsTpl->assign('lang_search', _SR_SEARCH);
164
    $xoopsTpl->assign('module_id', $xoopsModule->getVar('mid'));
165
    //category head
166
    $catarray = array();
167
    if ($mylinks_show_letters) {
168
        $catarray['letters'] = ml_wfd_letters();
169
    }
170
    if ($mylinks_show_toolbar) {
171
        $catarray['toolbar'] = ml_wfd_toolbar();
172
    }
173
    $xoopsTpl->assign('catarray', $catarray);
174
175
    include_once XOOPSMYLINKPATH . '/footer.php';
176
}
177