DoSync::checkOrphans()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Smallworld;
4
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
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * SmallWorld
17
 *
18
 * @copyright    The XOOPS Project (https://xoops.org)
19
 * @copyright    2011 Culex
20
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
21
 * @package      SmallWorld
22
 * @since        1.0
23
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
24
 */
25
26
/*
27
 *  Does a sync to remove orphans from smallworld db
28
 *
29
 */
30
31
/**
32
 * Class DoSync
33
 */
34
class DoSync
35
{
36
    /**
37
     * check for orphans (xoops_users <-> smallworld_users) and remove from smallworld
38
     */
39
    public function checkOrphans()
40
    {
41
        $sql    = 'SELECT userid FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' WHERE userid NOT IN ( SELECT uid FROM ' . $GLOBALS['xoopsDB']->prefix('users') . ')';
42
        $result = $GLOBALS['xoopsDB']->queryF($sql);
43
        if ($result) {
44
            while (false !== ($r = $GLOBALS['xoopsDB']->fetchArray($result))) {
45
                $this->deleteAccount($r['userid']);
46
            }
47
        }
48
    }
49
50
    /**
51
     * deleteAccount function
52
     * - Delete user account and associate rows across tables
53
     *
54
     * @param int $userid
55
     * @return bool true on success, false on failure
56
     */
57
    public function deleteAccount($userid)
58
    {
59
        $user     = new \XoopsUser($userid);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
60
        //$username = $user->uname();
61
        $sql01    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . " WHERE userid = '" . $userid . "'";
62
        $sql02    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "'";
63
        $sql03    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'";
64
        $sql04    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'";
65
        $sql05    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . " WHERE userid = '" . $userid . "'";
66
        $sql06    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "'";
67
        $sql07    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . $userid . "'";
68
        $sql08    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE user_id = '" . $userid . "'";
69
        $sql09    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_complaints') . " WHERE owner = '" . $userid . "' OR byuser_id = '" . $userid . "'";
70
        $sql10    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . " WHERE userid = '" . $userid . "'";
71
72
        $result01 = $GLOBALS['xoopsDB']->queryF($sql01);
73
        $result02 = $GLOBALS['xoopsDB']->queryF($sql02);
74
        $result03 = $GLOBALS['xoopsDB']->queryF($sql03);
75
        $result04 = $GLOBALS['xoopsDB']->queryF($sql04);
76
        $result05 = $GLOBALS['xoopsDB']->queryF($sql05);
77
        $result06 = $GLOBALS['xoopsDB']->queryF($sql06);
78
        $result07 = $GLOBALS['xoopsDB']->queryF($sql07);
79
        $result08 = $GLOBALS['xoopsDB']->queryF($sql08);
80
        $result09 = $GLOBALS['xoopsDB']->queryF($sql09);
81
        $result10 = $GLOBALS['xoopsDB']->queryF($sql10);
82
        // Remove picture dir
83
        $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . $userid . '/';
84
        $result11 = $this->smallworld_remDir($userid, $dirname, $empty = false);
85
86
        return $result01 && $result02 && $result03 && $result04 && $result05 && $result06 && $result07 && $result08 && $result09 && $result10 && $result11;
87
    }
88
89
    /**
90
     * smallworld_remDir function
91
     * - Remove user image dir in uploads.
92
     * @param int         $userid
93
     * @param string|bool $directory
94
     * @param bool|int    $empty
95
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
96
     */
97 View Code Duplication
    public function smallworld_remDir($userid, $directory, $empty = false)
0 ignored issues
show
Duplication introduced by
This method 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...
98
    {
99
        if ('' != $userid) {
100
            if ('/' === mb_substr($directory, -1)) {
101
                $directory = mb_substr($directory, 0, -1);
102
            }
103
104
            if (!file_exists($directory) || !is_dir($directory)) {
105
                return false;
106
            } elseif (!is_readable($directory)) {
107
                return false;
108
            }
109
            $directoryHandle = opendir($directory);
110
            while (false !== ($contents = readdir($directoryHandle))) {
111
                if ('.' !== $contents && '..' !== $contents) {
112
                    $path = $directory . '/' . $contents;
113
                    if (is_dir($path)) {
114
                        $this->smallworld_remDir($userid, $path);
115
                    } else {
116
                        unlink($path);
117
                    }
118
                }
119
            }
120
            closedir($directoryHandle);
121
            if (false === $empty) {
122
                if (!rmdir($directory)) {
123
                    return false;
124
                }
125
            }
126
127
            return true;
128
        }
129
    }
130
131
    /**
132
     * SmallworldDeleteDirectory function
133
     * - Delete images from users on delete
134
     * @param int $userid
135
     * @return true
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
136
     */
137
    public function SmallworldDeleteDirectory($userid)
138
    {
139
        $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . $userid . '/';
140
        if (is_dir($dirname)) {
141
            $dir_handle = opendir($dirname);
142
        }
143
        if (!$dir_handle) {
0 ignored issues
show
Bug introduced by
The variable $dir_handle 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...
144
            return false;
145
        }
146
        while ($file = readdir($dir_handle)) {
147 View Code Duplication
            if ('.' != $file && '..' != $file) {
0 ignored issues
show
Duplication introduced by
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...
148
                if (!is_dir($dirname . '/' . $file)) {
149
                    unlink($dirname . '/' . $file);
150
                } else {
151
                    $this->SmallworldDeleteDirectory($dirname . '/' . $file);
152
                }
153
            }
154
        }
155
        closedir($dir_handle);
156
        rmdir($dirname);
157
158
        return true;
159
    }
160
}
161