MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/public_html/mpl.imenso.co/app/Http/Controllers/MaintenanceController.php
<?php

namespace App\Http\Controllers;

use App\Models\Event;
use App\Models\User;
use Illuminate\Support\Facades\DB;

class MaintenanceController extends Controller
{
    public function cleanUpConnections()
    {
        $current_users = User::all();

        $user_ids = [];

        foreach ($current_users as $user) {
            $user_ids[] = $user->id;
        }

        $current_events = Event::all();

        $event_ids = [];

        foreach ($current_events as $event) {
            $event_ids[] = $event->id;
        }

        $rows = DB::table('albums')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids)) {
                DB::table('albums')->where('artist_id', $row->artist_id)->delete();
                echo 'Delete row: albums '.$row->artist_id.'<br>';
            }
        }

        $rows = DB::table('artist_artist_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist1_id, $user_ids) || ! in_array($row->artist2_id, $user_ids)) {
                DB::table('artist_artist_connections')->where('artist1_id', $row->artist1_id)->where('artist2_id', $row->artist2_id)->delete();
                echo 'Delete row: artist-artist '.$row->artist1_id.'-'.$row->artist2_id.'<br>';
            }
        }

        $rows = DB::table('artist_availabilities')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids)) {
                DB::table('artist_availabilities')->where('artist_id', $row->artist_id)->delete();
                echo 'Delete row: artist-avail '.$row->artist_id.'<br>';
            }
        }

        $rows = DB::table('artist_promoter_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids) || ! in_array($row->promoter_id, $user_ids)) {
                DB::table('artist_promoter_connections')->where('artist_id', $row->artist_id)->where('promoter_id', $row->promoter_id)->delete();
                echo 'Delete row: artist-promoter '.$row->artist_id.'-'.$row->promoter_id.'<br>';
            }
        }

        $rows = DB::table('artist_venue_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids) || ! in_array($row->venue_id, $user_ids)) {
                DB::table('artist_venue_connections')->where('artist_id', $row->artist_id)->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: artist-venue '.$row->artist_id.'-'.$row->venue_id.'<br>';
            }
        }

        $rows = DB::table('comments')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('comments')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: comments '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('event_interests')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('event_interests')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: event_interests '.$row->user_id.'<br>';
            }

            if (! in_array($row->event_id, $event_ids)) {
                DB::table('event_interests')->where('event_id', $row->event_id)->delete();
                echo 'Delete row: event_interests '.$row->event_id.'<br>';
            }
        }

        $rows = DB::table('event_artists')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids)) {
                DB::table('event_artists')->where('artist_id', $row->artist_id)->delete();
                echo 'Delete row: event_artists '.$row->artist_id.'<br>';
            }

            if (! in_array($row->event_id, $event_ids)) {
                DB::table('event_artists')->where('event_id', $row->event_id)->delete();
                echo 'Delete row: event_artists '.$row->event_id.'<br>';
            }
        }

        $rows = DB::table('fan_artist_checkout_ignores')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids)) {
                DB::table('fan_artist_checkout_ignores')->where('artist_id', $row->artist_id)->delete();
                echo 'Delete row: fan_artist_checkout_ignores '.$row->artist_id.'<br>';
            }
        }

        $rows = DB::table('fan_artist_follows')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids) || ! in_array($row->fan_id, $user_ids)) {
                DB::table('fan_artist_follows')->where('artist_id', $row->artist_id)->where('fan_id', $row->fan_id)->delete();
                echo 'Delete row: fan-artist-follows '.$row->artist_id.'-'.$row->fan_id.'<br>';
            }
        }

        $rows = DB::table('fan_artist_seen')->get();
        foreach ($rows as $row) {
            if (! in_array($row->artist_id, $user_ids) || ! in_array($row->fan_id, $user_ids)) {
                DB::table('fan_artist_seen')->where('artist_id', $row->artist_id)->where('fan_id', $row->fan_id)->delete();
                echo 'Delete row: fan-artist-seen '.$row->artist_id.'-'.$row->fan_id.'<br>';
            }
        }

        $rows = DB::table('fan_fan_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->fan_id1, $user_ids) || ! in_array($row->fan_id2, $user_ids)) {
                DB::table('fan_fan_connections')->where('fan_id1', $row->fan_id1)->where('fan_id2', $row->fan_id2)->delete();
                echo 'Delete row: fan-fan '.$row->fan_id1.'-'.$row->fan_id2.'<br>';
            }
        }

        $rows = DB::table('fan_promoter_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->fan_id, $user_ids) || ! in_array($row->promoter_id, $user_ids)) {
                DB::table('fan_fan_connections')->where('fan_id', $row->fan_id)->where('promoter_id', $row->promoter_id)->delete();
                echo 'Delete row: fan-promoter-seen '.$row->fan_id.'-'.$row->promoter_id.'<br>';
            }
        }

        $rows = DB::table('fan_venue_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->fan_id, $user_ids) || ! in_array($row->venue_id, $user_ids)) {
                DB::table('fan_fan_connections')->where('fan_id', $row->fan_id)->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: fan-venue-seen '.$row->fan_id.'-'.$row->promoter_id.'<br>';
            }
        }

        $rows = DB::table('likes')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('likes')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: likes '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('images')->where('imageable_type', \App\Models\User::class)->get();
        foreach ($rows as $row) {
            if (! in_array($row->imageable_id, $user_ids)) {
                DB::table('images')->where('imageable_id', $row->imageable_id)->where('imageable_type', \App\Models\User::class)->delete();
                echo 'Delete row: images '.$row->imageable_id.'<br>';
            }
        }

        $rows = DB::table('galleries')->where('galleryable_type', \App\Models\User::class)->get();
        foreach ($rows as $row) {
            if (! in_array($row->galleryable_id, $user_ids)) {
                DB::table('galleries')->where('galleryable_id', $row->galleryable_id)->where('galleryable_type', \App\Models\User::class)->delete();
                echo 'Delete row: galleries '.$row->galleryable_id.'<br>';
            }
        }

        $rows = DB::table('files')->where('fileable_type', \App\Models\User::class)->get();
        foreach ($rows as $row) {
            if (! in_array($row->fileable_id, $user_ids)) {
                DB::table('files')->where('fileable_id', $row->fileable_id)->where('fileable_type', \App\Models\User::class)->delete();
                echo 'Delete row: files '.$row->fileable_id.'<br>';
            }
        }

        $rows = DB::table('notification_data')->where('field_name', 'recommendee_id')->orWhere('field_name', 'recommender_id')->orWhere('field_name', 'liker_id')->orWhere('field_name', 'artist_id')->where('field_name', 'fan_id')->get();
        foreach ($rows as $row) {
            if (! in_array($row->value, $user_ids)) {
                DB::table('notification_data')->where('id', $row->id)->delete();
                DB::table('notifications')->where('id', $row->id)->delete();
                echo 'Delete row: notifications involving '.$row->value.'<br>';
            }
        }

        $rows = DB::table('notifications')->get();
        foreach ($rows as $row) {
            $matches = [];
            preg_match_all('/App\\\User\:\:(\d*)\:\:/', $row->content, $matches);

            foreach ($matches[1] as $match) {
                if (! in_array($match, $user_ids)) {
                    DB::table('notification_data')->where('id', $row->id)->delete();
                    DB::table('notifications')->where('id', $row->id)->delete();
                    echo 'Delete row: notifications involving '.$match.'<br>';
                }
            }

            $matches = [];
            preg_match_all('/App\\\Event\:\:(\d*)\:\:/', $row->content, $matches);

            foreach ($matches[1] as $match) {
                if (! in_array($match, $event_ids)) {
                    DB::table('notification_data')->where('id', $row->id)->delete();
                    DB::table('notifications')->where('id', $row->id)->delete();
                    echo 'Delete row: notifications involving '.$match.'<br>';
                }
            }
        }

        $rows = DB::table('message_thread_participants')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('message_thread_participants')->where('thread_id', $row->thread_id)->delete();
                DB::table('message_threads')->where('id', $row->thread_id)->delete();
                echo 'Delete row: message-thread '.$row->thread_id.'-'.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('messages')->get();
        foreach ($rows as $row) {
            if (! in_array($row->author_id, $user_ids)) {
                DB::table('messages')->where('author_id', $row->author_id)->delete();
                echo 'Delete row: messages '.$row->id.'-'.$row->author_id.'<br>';
            }
        }

        $rows = DB::table('news')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('news')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: news '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('notifications')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('notifications')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: notification '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('promoter_artist_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->promoter_id, $user_ids) || ! in_array($row->artist_id, $user_ids)) {
                DB::table('promoter_artist_connections')->where('promoter_id', $row->promoter_id)->where('artist_id', $row->artist_id)->delete();
                echo 'Delete row: promoter-artist '.$row->promoter_id.'-'.$row->artist_id.'<br>';
            }
        }

        $rows = DB::table('promoter_venue_connections')->get();
        foreach ($rows as $row) {
            if (! in_array($row->promoter_id, $user_ids) || ! in_array($row->venue_id, $user_ids)) {
                DB::table('promoter_venue_connections')->where('promoter_id', $row->promoter_id)->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: promoter-venue '.$row->promoter_id.'-'.$row->venue_id.'<br>';
            }
        }

        $rows = DB::table('user_devices')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('user_devices')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: devices '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('user_genres')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('user_genres')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: user_genres '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('user_messaging_preferences')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('user_messaging_preferences')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: messaging_prefs '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('user_notification_preferences')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids)) {
                DB::table('user_notification_preferences')->where('user_id', $row->user_id)->delete();
                echo 'Delete row: notification_prefs '.$row->user_id.'<br>';
            }
        }

        $rows = DB::table('venue_availabilities')->get();
        foreach ($rows as $row) {
            if (! in_array($row->venue_id, $user_ids)) {
                DB::table('venue_availabilities')->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: venue_availabilities '.$row->id.'-'.$row->venue_id.'<br>';
            }
        }

        $rows = DB::table('venue_availability_promoters')->get();
        foreach ($rows as $row) {
            if (! in_array($row->promoter_id, $user_ids)) {
                DB::table('venue_availability_promoters')->where('promoter_id', $row->promoter_id)->delete();
                echo 'Delete row: venue_availability_promoters '.$row->promoter_id.'<br>';
            }
        }

        $rows = DB::table('venue_info')->get();
        foreach ($rows as $row) {
            if (! in_array($row->venue_id, $user_ids)) {
                DB::table('venue_info')->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: venue_info '.$row->venue_id.'<br>';
            }
        }

        $rows = DB::table('venue_votes')->get();
        foreach ($rows as $row) {
            if (! in_array($row->user_id, $user_ids) || ! in_array($row->venue_id, $user_ids)) {
                DB::table('venue_votes')->where('user_id', $row->user_id)->where('venue_id', $row->venue_id)->delete();
                echo 'Delete row: venue_votes '.$row->user_id.'-'.$row->venue_id.'<br>';
            }
        }

        die('Done!');
    }
}