| Server IP : 157.230.181.24 / Your IP : 216.73.217.11 Web Server : Apache/2.4.58 (Ubuntu) System : Linux conductive 6.8.0-117-generic #117-Ubuntu SMP PREEMPT_DYNAMIC Tue May 5 19:26:24 UTC 2026 x86_64 User : ( 1000) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/vhosts/convo/app/Console/Commands/ |
Upload File : |
<?php
namespace App\Console\Commands;
use App\Models\Account;
use App\Models\Conversation;
use Carbon\Carbon;
use DOMDocument;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
class ParseConversationsCommand extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'parse:conversations';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Parses conversations';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct() {
parent::__construct();
set_time_limit(30000);
ignore_user_abort(1);
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle() {
$this->createAccounts();
$this->createConversations();
}
/**
* Create account / screennames
*/
public function createAccounts() {
$this->info('Creating accounts.');
DB::table('conversations')->truncate();
$accounts = $this->getAccountNameAndPath(base_path('resources/conversations'));
foreach($accounts as $fromName => $path) {
$recipients = $this->getAccountNameAndPath($path);
foreach($recipients as $toName => $path) {
Account::firstOrCreate(['screenname' => $fromName]);
Account::firstOrCreate(['screenname' => $toName]);
}
}
$this->info('Done with account names.');
}
/**
* Create conversations
*/
public function createConversations() {
$this->info('Creating conversations.');
$accounts = $this->getAccountNameAndPath(base_path('resources/conversations'));
foreach($accounts as $fromName => $path) {
$recipients = $this->getAccountNameAndPath($path);
foreach($recipients as $toName => $path) {
$conversations = $this->getConversation($path);
foreach($conversations as $id => $conversation) {
$file = File::get($conversation);
$from = Account::where(['screenname' => $fromName])->first();
$to = Account::where(['screenname' => $toName])->first();
Conversation::insert([
'filename' => $id,
'from_account' => $from->id,
'to_account' => $to->id,
'content' => $this->parseContent($file),
'created_at' => date('Y-m-d H:i:s', File::lastModified($conversation)),
]);
//$this->info("$fromName : $toName");
}
}
}
$this->info('Done with conversations.');
}
/**
* Parse the content
*
* TODO: Make this work
*
* @param $content
* @return mixed
*/
public function parseContent($content) {
// Strip HTML and BODY
$content = preg_replace('/<HTML>(?:.*?)<BODY (?:.*?)>(.*?)<\/BODY><\/HTML>/m', '$1', $content);
return $content;
}
/**
* Get all conversations in folder
*
* @param $path
* @return mixed
*/
public function getConversation($path) {
$paths = File::files($path);
$formattedPaths = [];
foreach($paths as $path) {
$matches = [];
preg_match('/\/(?:.(?!\/))+$/', $path, $matches);
$formattedPaths[ltrim($matches[0], '/')] = $path;
}
return $formattedPaths;
}
/**
* Get account name and path
*
* @param $paths
* @return array
*/
public function getAccountNameAndPath($path) {
$paths = File::directories($path);
$formattedPaths = [];
foreach($paths as $path) {
$matches = [];
preg_match('/\/(?:.(?!\/))+$/', $path, $matches);
$formattedPaths[ltrim($matches[0], '/')] = $path;
}
return $formattedPaths;
}
}