Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like SwDatabase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SwDatabase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class SwDatabase |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * getJobsToDiv function |
||
| 29 | * @param int $id |
||
| 30 | * @return array |
||
| 31 | */ |
||
| 32 | public function getJobsToDiv($id) |
||
| 33 | { |
||
| 34 | global $xoopsUser, $xoopsDB; |
||
| 35 | $msg = []; |
||
| 36 | $new = []; |
||
|
|
|||
| 37 | $sql = 'SELECT employer,position,jobstart,jobstop,description FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid ='" . $id . "'"; |
||
| 38 | $result = $xoopsDB->query($sql); |
||
| 39 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 40 | $employer = unserialize($row['employer']); |
||
| 41 | $position = unserialize($row['position']); |
||
| 42 | $jobstart = unserialize($row['jobstart']); |
||
| 43 | $jobstop = unserialize($row['jobstop']); |
||
| 44 | $description = unserialize($row['description']); |
||
| 45 | } |
||
| 46 | $start = 0; |
||
| 47 | $end = count($employer) - 1; |
||
| 48 | while ($start <= $end) { |
||
| 49 | $msg[$start]['employer'] = $employer[$start]; |
||
| 50 | $msg[$start]['position'] = $position[$start]; |
||
| 51 | $msg[$start]['jobstart'] = $jobstart[$start]; |
||
| 52 | $msg[$start]['jobstop'] = $jobstop[$start]; |
||
| 53 | $msg[$start]['description'] = $description[$start]; |
||
| 54 | ++$start; |
||
| 55 | } |
||
| 56 | |||
| 57 | return $msg; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * getSchoolToDiv function |
||
| 62 | * @param int $id |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function getSchoolToDiv($id) |
||
| 66 | { |
||
| 67 | global $xoopsUser, $xoopsDB, $arr7; |
||
| 68 | $msg = []; |
||
| 69 | $sql = 'SELECT school_type,school,schoolstart,schoolstop FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid ='" . $id . "'"; |
||
| 70 | $result = $xoopsDB->query($sql); |
||
| 71 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 72 | $school_type = unserialize($row['school_type']); |
||
| 73 | $school = unserialize($row['school']); |
||
| 74 | $schoolstart = unserialize($row['schoolstart']); |
||
| 75 | $schoolstop = unserialize($row['schoolstop']); |
||
| 76 | } |
||
| 77 | $start = 0; |
||
| 78 | $end = count($school_type) - 1; |
||
| 79 | while ($start <= $end) { |
||
| 80 | $msg[$start]['school_type'] = $school_type[$start]; |
||
| 81 | $msg[$start]['school'] = $arr7[$school[$start]]; |
||
| 82 | $msg[$start]['schoolstart'] = $schoolstart[$start]; |
||
| 83 | $msg[$start]['schoolstop'] = $schoolstop[$start]; |
||
| 84 | $start++; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $msg; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * getScreennamesToDiv function |
||
| 92 | * @param int $id |
||
| 93 | * @return array |
||
| 94 | */ |
||
| 95 | public function getScreennamesToDiv($id) |
||
| 96 | { |
||
| 97 | global $xoopsUser, $xoopsDB, $arr06; |
||
| 98 | $msg = []; |
||
| 99 | $sql = 'SELECT screenname_type,screenname FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid ='" . $id . "'"; |
||
| 100 | $result = $xoopsDB->query($sql); |
||
| 101 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 102 | $screenname_type = unserialize($row['screenname_type']); |
||
| 103 | $screenname = unserialize($row['screenname']); |
||
| 104 | } |
||
| 105 | $start = 0; |
||
| 106 | $end = count($screenname_type) - 1; |
||
| 107 | while ($start <= $end) { |
||
| 108 | $msg[$start]['screenname'] = $screenname_type[$start]; |
||
| 109 | $msg[$start]['screenname_type'] = $arr06[$screenname[$start]]; |
||
| 110 | $msg[$start]['link'] = "<span class='smallworld_website'>" . Smallworld_sociallinks($screenname[$start], $msg[$start]['screenname']); |
||
| 111 | ++$start; |
||
| 112 | } |
||
| 113 | |||
| 114 | return $msg; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * getVar function |
||
| 119 | * @param int $id |
||
| 120 | * @param string $var |
||
| 121 | * @return array|int |
||
| 122 | */ |
||
| 123 | public function getVar($id, $var) |
||
| 124 | { |
||
| 125 | global $xoopsUser, $xoopsDB; |
||
| 126 | $sql = 'SELECT ' . $var . ' FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $id . "'"; |
||
| 127 | $result = $xoopsDB->queryF($sql); |
||
| 128 | if ($xoopsDB->getRowsNum($result) < 1) { |
||
| 129 | return 0; //_SMALLWORLD_REPLY_NOTSPECIFIED; |
||
| 130 | } |
||
| 131 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 132 | $msg[$var] = $row[$var]; |
||
| 133 | } |
||
| 134 | |||
| 135 | return $msg[$var]; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * updateSingleValue function |
||
| 140 | * @param string $table |
||
| 141 | * @param int $userid |
||
| 142 | * @param string $field |
||
| 143 | * @param int $value |
||
| 144 | */ |
||
| 145 | public function updateSingleValue($table, $userid, $field, $value) |
||
| 146 | { |
||
| 147 | global $xoopsUser, $xoopsDB; |
||
| 148 | $myts = \MyTextSanitizer::getInstance(); |
||
| 149 | $sql = 'UPDATE ' . $xoopsDB->prefix($table) . ' SET ' . $field . "='" . $myts->addSlashes($value) . "' WHERE userid='" . (int)$userid . "'"; |
||
| 150 | $result = $xoopsDB->queryF($sql); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * saveImage function |
||
| 155 | * @param $values |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function saveImage($values) |
|
| 158 | { |
||
| 159 | global $xoopsUser, $xoopsDB; |
||
| 160 | $myts = \MyTextSanitizer::getInstance(); |
||
| 161 | $sql = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_images') . ' VALUES (' . $values . ')'; |
||
| 162 | $result = $xoopsDB->queryF($sql); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * DeleteImage function |
||
| 167 | * @param int $userid |
||
| 168 | * @param string $imagename |
||
| 169 | */ |
||
| 170 | View Code Duplication | public function DeleteImage($userid, $imagename) |
|
| 171 | { |
||
| 172 | global $xoopsUser, $xoopsDB; |
||
| 173 | $myts = \MyTextSanitizer::getInstance(); |
||
| 174 | $sql = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_images') . " WHERE imgname = '" . stripslashes($imagename) . "' AND userid='" . $userid . "'"; |
||
| 175 | $result = $xoopsDB->queryF($sql); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * handlePosts function |
||
| 180 | */ |
||
| 181 | public function handlePosts() |
||
| 182 | { |
||
| 183 | global $xoopsUser, $xoopsDB; |
||
| 184 | $myts = \MyTextSanitizer::getInstance(); |
||
| 185 | $uid = $xoopsUser->getVar('uid'); |
||
| 186 | $user = new \XoopsUser($uid); |
||
| 187 | $img = new Images(); |
||
| 188 | if ('' == $this->getVar($uid, 'userimage')) { |
||
| 189 | $avatar = $user->user_avatar(); |
||
| 190 | } else { |
||
| 191 | $avatar = $this->getVar($uid, 'userimage'); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ('2' != $_POST['relationship']) { |
||
| 195 | $partner = Smallworld_sanitize($_POST['partner']); |
||
| 196 | } else { |
||
| 197 | $partner = ''; |
||
| 198 | } |
||
| 199 | |||
| 200 | $regdate = time(); |
||
| 201 | $username = $user->uname(); |
||
| 202 | $realname = Smallworld_sanitize($_POST['realname']); |
||
| 203 | $gender = isset($_POST['gender']) ? $_POST['gender'] : ''; |
||
| 204 | $intingender = isset($_POST['intingender']) ? Smallworld_sanitize(serialize($_POST['intingender'])) : Smallworld_sanitize(serialize([0 => '3'])); |
||
| 205 | $relationship = Smallworld_sanitize($_POST['relationship']); |
||
| 206 | $searchrelat = isset($_POST['searchrelat']) ? Smallworld_sanitize(serialize($_POST['searchrelat'])) : Smallworld_sanitize(serialize([0 => '0'])); |
||
| 207 | $birthday = Smallworld_sanitize(Smallworld_euroToUsDate($_POST['birthday'])); |
||
| 208 | $birthplace = Smallworld_sanitize($_POST['birthplace']); |
||
| 209 | $birthplace_lat = Smallworld_sanitize($_POST['birthplace_lat']); |
||
| 210 | $birthplace_lng = Smallworld_sanitize($_POST['birthplace_lng']); |
||
| 211 | $birthplace_country = Smallworld_sanitize($_POST['birthplace_country']); |
||
| 212 | $birthplace_country_img = isset($_POST['birthplace_country_img']) ? Smallworld_sanitize($_POST['birthplace_country_img']) : ''; |
||
| 213 | $politic = Smallworld_sanitize($_POST['politic']); |
||
| 214 | $religion = Smallworld_sanitize($_POST['religion']); |
||
| 215 | $emailtype = Smallworld_sanitize(serialize($_POST['emailtype'])); |
||
| 216 | $screenname_type = Smallworld_sanitize(serialize($_POST['screenname_type'])); |
||
| 217 | $screenname = Smallworld_sanitize(serialize($_POST['screenname'])); |
||
| 218 | $mobile = Smallworld_sanitize($_POST['mobile']); |
||
| 219 | $phone = Smallworld_sanitize($_POST['phone']); |
||
| 220 | $adress = Smallworld_sanitize($_POST['adress']); |
||
| 221 | $present_city = Smallworld_sanitize($_POST['present_city']); |
||
| 222 | $present_lat = Smallworld_sanitize($_POST['present_lat']); |
||
| 223 | $present_lng = Smallworld_sanitize($_POST['present_lng']); |
||
| 224 | $present_country = Smallworld_sanitize($_POST['present_country']); |
||
| 225 | $present_country_img = isset($_POST['present_country_img']) ? Smallworld_sanitize($_POST['present_country_img']) : ''; |
||
| 226 | $website = Smallworld_sanitize($_POST['website']); |
||
| 227 | $interests = Smallworld_sanitize($_POST['interests']); |
||
| 228 | $music = Smallworld_sanitize($_POST['music']); |
||
| 229 | $tvshow = Smallworld_sanitize($_POST['tvshow']); |
||
| 230 | $movie = Smallworld_sanitize($_POST['movie']); |
||
| 231 | $books = Smallworld_sanitize($_POST['books']); |
||
| 232 | $aboutme = Smallworld_sanitize($_POST['aboutme']); |
||
| 233 | $school_type = Smallworld_sanitize(serialize($_POST['school_type'])); |
||
| 234 | $school = Smallworld_sanitize(serialize($_POST['school'])); |
||
| 235 | $schoolstart = Smallworld_sanitize(serialize($_POST['schoolstart'])); |
||
| 236 | $schoolstop = Smallworld_sanitize(serialize($_POST['schoolstop'])); |
||
| 237 | $jobemployer = Smallworld_sanitize(serialize($_POST['employer'])); |
||
| 238 | $jobposition = Smallworld_sanitize(serialize($_POST['position'])); |
||
| 239 | $jobstart = Smallworld_sanitize(serialize(Smallworld_YearOfArray($_POST['jobstart']))); |
||
| 240 | $jobstop = Smallworld_sanitize(serialize(Smallworld_YearOfArray($_POST['jobstop']))); |
||
| 241 | $jobdescription = Smallworld_sanitize(serialize($_POST['description'])); |
||
| 242 | |||
| 243 | $sql = ''; |
||
| 244 | |||
| 245 | if ('edit' === $_POST['function']) { |
||
| 246 | // Update all values in user_table |
||
| 247 | $sql = 'UPDATE ' . $xoopsDB->prefix('smallworld_user') . ' SET '; |
||
| 248 | $sql .= "realname = '" . $realname . "', username= '" . $username . "', userimage = '" . $avatar . "', gender = '" . $gender . "',"; |
||
| 249 | $sql .= "intingender = '" . $intingender . "',relationship = '" . $relationship . "', partner = '" . $partner . "', searchrelat = '" . $searchrelat . "',"; |
||
| 250 | $sql .= "birthday = '" . $birthday . "',birthplace = '" . $birthplace . "',birthplace_lat = '" . (float)$birthplace_lat . "',"; |
||
| 251 | $sql .= "birthplace_lng = '" . (float)$birthplace_lng . "',birthplace_country = '" . $birthplace_country . "',politic = '" . $politic . "',"; |
||
| 252 | $sql .= "religion = '" . $religion . "',emailtype = '" . $emailtype . "',screenname_type = '" . $screenname_type . "',"; |
||
| 253 | $sql .= "screenname = '" . $screenname . "',mobile = '" . (float)$mobile . "',phone = '" . (float)$phone . "',adress = '" . $adress . "',"; |
||
| 254 | $sql .= "present_city = '" . $present_city . "',present_lat = '" . (float)$present_lat . "',present_lng = '" . (float)$present_lng . "',"; |
||
| 255 | $sql .= "present_country = '" . $present_country . "',website = '" . $website . "',interests = '" . $interests . "',"; |
||
| 256 | $sql .= "music = '" . $music . "',tvshow = '" . $tvshow . "',movie = '" . $movie . "',"; |
||
| 257 | $sql .= "books = '" . $books . "',aboutme = '" . $aboutme . "',school_type = '" . $school_type . "',"; |
||
| 258 | $sql .= "school = '" . $school . "', schoolstart = '" . $schoolstart . "',schoolstop = '" . $schoolstop . "',"; |
||
| 259 | $sql .= "employer = '" . $jobemployer . "', position = '" . $jobposition . "',jobstart = '" . $jobstart . "',"; |
||
| 260 | $sql .= "jobstop = '" . $jobstop . "', description = '" . $jobdescription . "' "; |
||
| 261 | $sql .= "WHERE userid ='" . (int)$uid . "'"; |
||
| 262 | $result = $xoopsDB->queryF($sql); |
||
| 263 | if (false === $result) { |
||
| 264 | die('SQL error:' . $sql . ''); |
||
| 265 | } |
||
| 266 | |||
| 267 | $this->EditAdmins($uid, $realname, $avatar); |
||
| 268 | $img->createAlbum($uid); |
||
| 269 | } |
||
| 270 | |||
| 271 | if ('save' === $_POST['function']) { |
||
| 272 | $sql = 'INSERT INTO ' |
||
| 273 | . $xoopsDB->prefix('smallworld_user') |
||
| 274 | . ' (id, userid, regdate, username, userimage, realname, gender, intingender, relationship, partner, searchrelat, birthday, birthplace, birthplace_lat, birthplace_lng, birthplace_country, politic, religion, emailtype, screenname_type, screenname, mobile, phone, adress, present_city, present_lat, present_lng, present_country, website, interests, music, tvshow, movie, books, aboutme, school_type, school, schoolstart, schoolstop, employer, position, jobstart, jobstop, description, friends, followers, admin_flag) '; |
||
| 275 | $sql .= "VALUES ('','" . (int)$uid . "', '" . $regdate . "', '" . $username . "', '" . $avatar . "', '" . $realname . "', '" . $gender . "', '" . $intingender . "', '" . $relationship . "', '" . $partner . "', '" . $searchrelat . "','"; |
||
| 276 | $sql .= $birthday . "', '" . $birthplace . "', '" . (float)$birthplace_lat . "', '" . (float)$birthplace_lng . "', '" . $birthplace_country . "', '" . $politic . "', '" . $religion . "','"; |
||
| 277 | $sql .= $emailtype . "', '" . $screenname_type . "', '" . $screenname . "', '" . (float)$mobile . "', '" . (float)$phone . "', '" . $adress . "', '" . $present_city . "', '" . (float)$present_lat . "','"; |
||
| 278 | $sql .= (float)$present_lng . "', '" . $present_country . "', '" . $website . "', '" . $interests . "', '" . $music . "', '" . $tvshow . "', '" . $movie . "', '" . $books . "', '" . $aboutme . "', '"; |
||
| 279 | $sql .= $school_type . "', '" . $school . "', '" . $schoolstart . "', '" . $schoolstop . "', '" . $jobemployer . "', '" . $jobposition . "', '" . $jobstart . "', '" . $jobstop . "', '" . $jobdescription . "', "; |
||
| 280 | $sql .= "'0', '0', '0')"; |
||
| 281 | $result = $xoopsDB->queryF($sql); |
||
| 282 | if (false === $result) { |
||
| 283 | die('SQL error:' . $sql . ''); |
||
| 284 | } |
||
| 285 | $this->SetAdmins($uid, $username, $realname, $avatar); |
||
| 286 | $img->createAlbum($uid); |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * SetAdmins function |
||
| 292 | * @param int $userID |
||
| 293 | * @param string $username |
||
| 294 | * @param string $realname |
||
| 295 | * @param mixed $avatar |
||
| 296 | */ |
||
| 297 | public function SetAdmins($userID, $username, $realname, $avatar) |
||
| 298 | { |
||
| 299 | global $xoopsDB, $xoopsUser; |
||
| 300 | $ip = $_SERVER['REMOTE_ADDR']; |
||
| 301 | $sql = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_admin') . ' (id,userid,username, realname,userimage,ip,complaint,inspect_start, ' . "inspect_stop) VALUES ('', '" . $userID . "', '" . $username . "','" . $realname . "', '" . $avatar . "','" . $ip . "','0','0','0')"; |
||
| 302 | $result = $xoopsDB->queryF($sql); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * EditAdmins function |
||
| 307 | * @param int $userID |
||
| 308 | * @param string $realname |
||
| 309 | * @param mixed $avatar |
||
| 310 | */ |
||
| 311 | public function EditAdmins($userID, $realname, $avatar) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * alreadycomplaint function |
||
| 320 | * - Check if user has already sent complaint |
||
| 321 | * @param string $msg |
||
| 322 | * @param int $by |
||
| 323 | * @param int $against |
||
| 324 | * @return int |
||
| 325 | */ |
||
| 326 | public function alreadycomplaint($msg, $by, $against) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * updateComplaint function |
||
| 342 | * @param int $userID |
||
| 343 | */ |
||
| 344 | public function updateComplaint($userID) |
||
| 345 | { |
||
| 346 | global $xoopsDB; |
||
| 347 | $sql = 'UPDATE ' . $xoopsDB->prefix('smallworld_admin') . ' SET complaint = complaint + 1 ' . "WHERE userid = '" . (int)$userID . "'"; |
||
| 348 | $result = $xoopsDB->queryF($sql); |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * updateInspection function |
||
| 353 | * @param int $userID |
||
| 354 | * @param int $start |
||
| 355 | * @param int stop |
||
| 356 | * @param mixed $stop |
||
| 357 | */ |
||
| 358 | public function updateInspection($userID, $start, $stop) |
||
| 359 | { |
||
| 360 | global $xoopsDB; |
||
| 361 | $newstop = $time() + $stop; |
||
| 362 | $sql = 'UPDATE ' . $xoopsDB->prefix('smallworld_admin') . " SET inspect_start = '" . $time() . "', instect_stop = '" . $newstop . "' WHERE userid ='" . (int)$userID . "'"; |
||
| 363 | $result = $xoopsDB->queryF($sql); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * handleImageEdit function |
||
| 368 | */ |
||
| 369 | public function handleImageEdit() |
||
| 370 | { |
||
| 371 | global $xoopsDB; |
||
| 372 | for ($i = 0, $iMax = count($_POST['id']); $i < $iMax; ++$i) { |
||
| 373 | $id = (int)$_POST['id'][$i]; |
||
| 374 | $desc = $_POST['imgdesc'][$i]; |
||
| 375 | $sql = 'UPDATE ' . $xoopsDB->prefix('smallworld_images') . " SET `desc` = '" . addslashes($desc) . "' WHERE `id`='" . $id . "'"; |
||
| 376 | $result = $xoopsDB->queryF($sql); |
||
| 377 | } |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * updateInspection function |
||
| 382 | * - insert aplication for friendship into db or delete if denied |
||
| 383 | * @param int $status |
||
| 384 | * @param int $friendid |
||
| 385 | * @param int $userid |
||
| 386 | */ |
||
| 387 | public function toogleFriendInvite($status, $friendid, $userid) |
||
| 388 | { |
||
| 389 | global $xoopsDB; |
||
| 390 | if (0 == $status) { |
||
| 391 | $sql = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_friends') . " (id,me,you,status,date) VALUES ('', '" . $userid . "', '" . $friendid . "', '1', UNIX_TIMESTAMP())"; |
||
| 392 | $result = $xoopsDB->queryF($sql); |
||
| 393 | } |
||
| 394 | View Code Duplication | if ($status > 0) { |
|
| 395 | $sql = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$friendid . "' AND you = '" . (int)$userid . "'"; |
||
| 396 | $sql2 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'"; |
||
| 397 | $result = $xoopsDB->queryF($sql); |
||
| 398 | $result2 = $xoopsDB->queryF($sql2); |
||
| 399 | |||
| 400 | // Since friendship is canceled also following is deleted |
||
| 401 | $this->toogleFollow(1, $userid, $friendid); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * toogleFollow function |
||
| 407 | * - Insert following to db or delete if requested |
||
| 408 | * @param int $following |
||
| 409 | * @param int $myUid |
||
| 410 | * @param int $friend |
||
| 411 | */ |
||
| 412 | public function toogleFollow($following, $myUid, $friend) |
||
| 413 | { |
||
| 414 | global $xoopsDB; |
||
| 415 | if (0 == $following) { |
||
| 416 | $sql = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_followers') . " (id,me,you,status,date) VALUES ('', '" . $myUid . "', '" . $friend . "', '1', UNIX_TIMESTAMP())"; |
||
| 417 | $result = $xoopsDB->queryF($sql); |
||
| 418 | } |
||
| 419 | if ($following > 0) { |
||
| 420 | $sql = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE you = '" . (int)$friend . "'"; |
||
| 421 | $sql .= " AND me = '" . (int)$myUid . "'"; |
||
| 422 | $sql2 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . (int)$friend . "'"; |
||
| 423 | $sql2 .= " AND you = '" . (int)$myUid . "'"; |
||
| 424 | $result2 = $xoopsDB->queryF($sql2); |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * SetFriendshitStat function |
||
| 430 | * @param int $stat |
||
| 431 | * @param int $myUid |
||
| 432 | * @param int $friend |
||
| 433 | */ |
||
| 434 | public function SetFriendshitStat($stat, $myUid, $friend) |
||
| 435 | { |
||
| 436 | global $xoopsDB; |
||
| 437 | if (1 == $stat) { |
||
| 438 | $sql = 'UPDATE ' . $xoopsDB->prefix('smallworld_friends') . " SET status = '2' WHERE `me` = '" . $friend . "' AND `you` = '" . $myUid . "'"; |
||
| 439 | $result = $xoopsDB->queryF($sql); |
||
| 440 | $query = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_friends') . " (id,me,you,status,date) VALUES ('', '" . $myUid . "', '" . $friend . "', '2', UNIX_TIMESTAMP())"; |
||
| 441 | $result = $xoopsDB->queryF($query); |
||
| 442 | } |
||
| 443 | View Code Duplication | if ($stat < 0) { |
|
| 444 | $sql = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$friend . "' AND you = '" . (int)$myUid . "'"; |
||
| 445 | $sql2 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$friend . "' AND me = '" . (int)$myUid . "'"; |
||
| 446 | $result = $xoopsDB->queryF($sql); |
||
| 447 | $result2 = $xoopsDB->queryF($sql2); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * deleteWallMsg function |
||
| 453 | * @param int $id |
||
| 454 | * @param int $smallworld_msg_id |
||
| 455 | * @return true |
||
| 456 | */ |
||
| 457 | public function deleteWallMsg($id, $smallworld_msg_id) |
||
| 458 | { |
||
| 459 | global $xoopsDB; |
||
| 460 | $query = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_messages') . " WHERE msg_id = '" . $smallworld_msg_id . "'"; |
||
| 461 | $result = $xoopsDB->queryF($query); |
||
| 462 | $query2 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE msg_id_fk = '" . $smallworld_msg_id . "'"; |
||
| 463 | $result2 = $xoopsDB->queryF($query2); |
||
| 464 | //delete votes |
||
| 465 | $query3 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE msg_id = '" . $smallworld_msg_id . "'"; |
||
| 466 | $result3 = $xoopsDB->queryF($query3); |
||
| 467 | |||
| 468 | return true; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * deleteWallComment function |
||
| 473 | * - Delete Comments |
||
| 474 | * @param int $smallworld_com_id |
||
| 475 | * @return true |
||
| 476 | */ |
||
| 477 | View Code Duplication | public function deleteWallComment($smallworld_com_id) |
|
| 478 | { |
||
| 479 | global $xoopsDB; |
||
| 480 | $query = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE com_id = '" . $smallworld_com_id . "'"; |
||
| 481 | $result = $xoopsDB->queryF($query); |
||
| 482 | $query2 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $smallworld_com_id . "'"; |
||
| 483 | $result2 = $xoopsDB->queryF($query2); |
||
| 484 | |||
| 485 | return true; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * CountUsersRates function |
||
| 490 | * - Delete Comments |
||
| 491 | * @param int $userid |
||
| 492 | * @param string $val |
||
| 493 | * @return int |
||
| 494 | */ |
||
| 495 | View Code Duplication | public function CountUsersRates($userid, $val) |
|
| 496 | { |
||
| 497 | global $xoopsUser, $xoopsDB; |
||
| 498 | $query = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where owner = '" . $userid . "'"; |
||
| 499 | $result = $xoopsDB->queryF($query); |
||
| 500 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 501 | $sum = $row['sum']; |
||
| 502 | } |
||
| 503 | if ('' == $sum) { |
||
| 504 | $sum = '0'; |
||
| 505 | } |
||
| 506 | |||
| 507 | return $sum; |
||
| 508 | } |
||
| 509 | |||
| 510 | /** |
||
| 511 | * deleteAccount function |
||
| 512 | * - Delete user account and associate rows across tables |
||
| 513 | * @param int $userid |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | View Code Duplication | public function deleteAccount($userid) |
|
| 517 | { |
||
| 518 | global $xoopsDB, $xoopsUser; |
||
| 519 | $user = new \XoopsUser($userid); |
||
| 520 | $username = $user->uname(); |
||
| 521 | $sql01 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_admin') . " WHERE userid = '" . $userid . "'"; |
||
| 522 | $sql02 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "'"; |
||
| 523 | $sql03 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'"; |
||
| 524 | $sql04 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'"; |
||
| 525 | $sql05 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_images') . " WHERE userid = '" . $userid . "'"; |
||
| 526 | $sql06 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "'"; |
||
| 527 | $sql07 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $userid . "'"; |
||
| 528 | $sql08 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE user_id = '" . $userid . "'"; |
||
| 529 | $sql09 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_complaints') . " WHERE owner = '" . $userid . "' OR byuser_id = '" . $userid . "'"; |
||
| 530 | $sql10 = 'DELETE FROM ' . $xoopsDB->prefix('smallworld_settings') . " WHERE userid = '" . $userid . "'"; |
||
| 531 | |||
| 532 | $result01 = $xoopsDB->queryF($sql01); |
||
| 533 | $result02 = $xoopsDB->queryF($sql02); |
||
| 534 | $result03 = $xoopsDB->queryF($sql03); |
||
| 535 | $result04 = $xoopsDB->queryF($sql04); |
||
| 536 | $result05 = $xoopsDB->queryF($sql05); |
||
| 537 | $result06 = $xoopsDB->queryF($sql06); |
||
| 538 | $result07 = $xoopsDB->queryF($sql07); |
||
| 539 | $result08 = $xoopsDB->queryF($sql08); |
||
| 540 | $result09 = $xoopsDB->queryF($sql09); |
||
| 541 | $result10 = $xoopsDB->queryF($sql10); |
||
| 542 | // Remove picture dir |
||
| 543 | $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . $userid . '/'; |
||
| 544 | $this->smallworld_remDir($userid, $dirname, $empty = false); |
||
| 545 | echo $username . _AM_SMALLWORLD_ADMIN_USERDELETEDALERT; |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * SmallworldDeleteDirectory function |
||
| 550 | * - Delete images from users on delete |
||
| 551 | * @param int $userid |
||
| 552 | * @return true |
||
| 553 | */ |
||
| 554 | public function SmallworldDeleteDirectory($userid) |
||
| 555 | { |
||
| 556 | $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . $userid . '/'; |
||
| 557 | if (is_dir($dirname)) { |
||
| 558 | $dir_handle = opendir($dirname); |
||
| 559 | } |
||
| 560 | if (!$dir_handle) { |
||
| 561 | return false; |
||
| 562 | } |
||
| 563 | while (false !== ($file = readdir($dir_handle))) { |
||
| 564 | View Code Duplication | if ('.' !== $file && '..' !== $file) { |
|
| 565 | if (!is_dir($dirname . '/' . $file)) { |
||
| 566 | unlink($dirname . '/' . $file); |
||
| 567 | } else { |
||
| 568 | $this->SmallworldDeleteDirectory($dirname . '/' . $file); |
||
| 569 | } |
||
| 570 | } |
||
| 571 | } |
||
| 572 | closedir($dir_handle); |
||
| 573 | rmdir($dirname); |
||
| 574 | |||
| 575 | return true; |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * smallworld_remDir function |
||
| 580 | * - Remove user image dir in uploads. |
||
| 581 | * @param int $userid |
||
| 582 | * @param string|bool $directory |
||
| 583 | * @param bool|int $empty |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | View Code Duplication | public function smallworld_remDir($userid, $directory, $empty = false) |
|
| 587 | { |
||
| 588 | if ('' != $userid) { |
||
| 589 | if ('/' === mb_substr($directory, -1)) { |
||
| 590 | $directory = mb_substr($directory, 0, -1); |
||
| 591 | } |
||
| 592 | |||
| 593 | if (!file_exists($directory) || !is_dir($directory)) { |
||
| 594 | return false; |
||
| 595 | } elseif (!is_readable($directory)) { |
||
| 596 | return false; |
||
| 597 | } |
||
| 598 | $directoryHandle = opendir($directory); |
||
| 599 | while (false !== ($contents = readdir($directoryHandle))) { |
||
| 600 | if ('.' !== $contents && '..' !== $contents) { |
||
| 601 | $path = $directory . '/' . $contents; |
||
| 602 | if (is_dir($path)) { |
||
| 603 | $this->smallworld_remDir($userid, $path); |
||
| 604 | } else { |
||
| 605 | unlink($path); |
||
| 606 | } |
||
| 607 | } |
||
| 608 | } |
||
| 609 | closedir($directoryHandle); |
||
| 610 | if (false === $empty) { |
||
| 611 | if (!rmdir($directory)) { |
||
| 612 | return false; |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | return true; |
||
| 617 | } |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Update private settings |
||
| 622 | * @param int id ($userid) |
||
| 623 | * @param string posts (serialized values) |
||
| 624 | * @param mixed $id |
||
| 625 | * @param mixed $posts |
||
| 626 | */ |
||
| 627 | public function saveSettings($id, $posts) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Retrieve private settings |
||
| 644 | * @param int userid |
||
| 645 | * @param mixed $userid |
||
| 646 | * @return string serialized string |
||
| 647 | */ |
||
| 648 | public function GetSettings($userid) |
||
| 649 | { |
||
| 650 | global $xoopsDB; |
||
| 651 | $sql = 'SELECT value FROM ' . $xoopsDB->prefix('smallworld_settings') . ' WHERE userid = ' . (int)$userid . ''; |
||
| 652 | $result = $xoopsDB->queryF($sql); |
||
| 653 | $i = $xoopsDB->getRowsNum($result); |
||
| 654 | if ($i < 1) { |
||
| 655 | $posts = serialize( |
||
| 656 | [ |
||
| 657 | 'posts' => 0, |
||
| 658 | 'comments' => 0, |
||
| 659 | 'notify' => 1, |
||
| 660 | ] |
||
| 661 | ); |
||
| 662 | $this->saveSettings($userid, $posts); |
||
| 672 | } |
||
| 673 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.