HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //home/posscale/.trash/1app.1/Http/Controllers/SyncPrintersController.php
<?php

namespace App\Http\Controllers;

use App\Actions\SmtpAction;
use App\Http\Controllers\Controller;
use App\Mail\LowTonerAdminEmail;
use App\Models\GeneralSettings;
use App\Models\Printer;
use App\Models\PrinterJob;
use Illuminate\Http\Request;
use PhpParser\Node\Expr\Print_;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\Mail;

class SyncPrintersController extends Controller
{
    public $admin_settings;

    /**
     * Display the user's profile form.
     */
    public function index(Request $request)
    {
        $this->admin_settings = GeneralSettings::all()->keyBy('key')->toArray();
        $allTenants = Tenant::all();
        if ($allTenants) {
            foreach ($allTenants as $key => $singleTenants) {
                try {
                    tenancy()->initialize($singleTenants);
                    $this->sync();
                    tenancy()->end();
                } catch (\Throwable $th) {
                    //throw $th;
                }
             
            }
        }
    }
    public function sync()
    {
        $printers = Printer::where('smtp_status', 1)->get();
        foreach ($printers as $printer) {
            if ($printer->smtp_status) {
                $smtp_details['client'] = [
                    'host'          => $printer->smtp->host,
                    'port'          => $printer->smtp->port,
                    'encryption'    => 'ssl',
                    'username'      => $printer->smtp->user_name,
                    'password'      =>  $printer->smtp->user_password,
                    'protocol'      => 'imap'
                ];

                $smtp_connection = new SmtpAction($smtp_details);
                $connection_info = $smtp_connection->testConnection();
                if ($connection_info['status']) {
                    $folder =  $smtp_connection->client->getFolder('INBOX');
                    $sinceDateTime = date('Y-m-d h:i:s', strtotime($printer->smtp_sync_date));
                    $query = $folder->query()
                        ->setFetchOrder("asc")
                        ->subject('ECOSYS M2040dn event mail')
                        ->since($sinceDateTime)
                        ->unseen()
                        ->limit(round(100 / count($printers)));
                        /*->limit(20);*/


                    $messages =  $query->get();
                    $query->markAsRead();

                    foreach ($messages as $message) {
                        $message_id = $message->get('message_id');
                        $job = PrinterJob::where('mail_id', $message_id)->count();
                        $message->setFlag('Seen');
                        if ($job) {
                            continue;
                        }

                        $body_text = $message->getTextBody();
                        $array_of_job = (explode("\n", $body_text));

                        $job_details_data = [];
                        $prefix = '';

                        $events = [];
                        $currentDate = "";
                        $low_toner = 0;
                        $send_mail_to_admin = 0;
                        foreach ($array_of_job as $job_details) {
                          
                            if (trim($job_details) == '-------------------') {
                                break;
                            }
                            $line = trim($job_details);
                            if (preg_match('/^<.*>$/', $line)) {
                                $currentDate = $line;
                                $events[$currentDate] = [];
                                continue;
                            } elseif (!empty($line) && $currentDate != '') {
                                if (!isset($events[$currentDate])) {
                                    $events[$currentDate] = [];
                                }
                               
                                $events[$currentDate][] = $line;
                               
                                /*if ($this->admin_settings['notification_status']['value'] == 1) {
                                    $line_data = explode(':', $line);
                                    if (trim($line_data[0]) == 'black') {
                                        $toner =  trim($line_data[1], '%, ');
                                        if ($toner <= $this->admin_settings['toner_level']['value']) {
                                            $send_mail_to_admin = 1;
                                        }
                                    }
                                }*/

                                if (trim($line) == '[*] Low Toner') {
                                    $low_toner = 1;
                                }
                                continue;
                            }


                            $row_data = explode(':', $job_details);

                            if (trim($row_data[0]) == 'Printed Pages') {
                                $prefix = 'Printed ';
                            }

                            if (trim($row_data[0]) == 'Scanned Pages') {
                                $prefix = 'Scanned ';
                            }

                            if (trim($row_data[0]) == 'Counters by Paper Size') {
                                $prefix = 'Counters ';
                            }

                            if (trim($row_data[0]) == 'Counters by Duplex') {
                                $prefix = 'Counters D ';
                            }

                            if (trim($row_data[0]) == 'Counters by Combine') {
                                $prefix = 'Counters C ';
                            }

                            if (trim($row_data[0]) == 'none') {
                                $prefix = 'none ';
                            }

                            $key = array_search($prefix . trim($row_data[0]), $smtp_connection->field_connections);

                            if (isset($smtp_connection->ignore_fields[$key])) {
                                continue;
                            }

                            if (isset($row_data[1])) {
                                if (isset($smtp_connection->printer_fields[$key])) {
                                } else {
                                    $job_details_data[$key] = trim($row_data[1]);
                                }
                            }
                        }

                        
                        $job_details_data['printer_id'] = $printer->id;
                        $job_details_data['mail_date'] = $message->getDate()->toDate()->format('Y-m-d h:i:s');
                        $job_details_data['mail_id'] = $message_id;
                        $new_printer_job = new PrinterJob($job_details_data);
                        $new_printer_job->save();

                        $printer_new_fields = [];
                        $printer_new_fields['toner_discreptions'] = $printer->toner_discreptions;
                        $printer_new_fields['make'] = $printer->make;                        
                        $printer_new_fields['model'] = $printer->model;
                        $printer_new_fields['address'] = $printer->address;
                        $printer_new_fields['city'] = $printer->city;
                        $printer_new_fields['zip'] = $printer->zip;
                        $printer_new_fields['state'] = $printer->state;
                        $printer_new_fields['country'] = $printer->country;

                        

                        if ($send_mail_to_admin) {
                            Mail::to($this->admin_settings['notification_email']['value'])->send(new LowTonerAdminEmail([
                                'printer'  => $new_printer_job,
                                'printer_new_fields'  => $printer_new_fields,
                                'toner' => $toner
                            ]));
                        }
                        $printer->smtp_sync_date = $message->getDate()->toDate()->format('Y-m-d h:i:s');
                        $printer->save();
                    }
                }
            }
        }
    }
}