Skip to content

Commit cdb4565

Browse files
committed
ported check install+media commands, passwd
1 parent 604906c commit cdb4565

8 files changed

Lines changed: 234 additions & 218 deletions

File tree

core/cli/CheckInstall.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace OpenBroadcaster\CLI;
4+
5+
use OpenBroadcaster\Base\CLI;
6+
7+
class CheckInstall extends CLI
8+
{
9+
public function run(array $args): bool
10+
{
11+
require_once(__DIR__ . '/../../public/updates/checker.php');
12+
13+
$checker = new \OBFChecker();
14+
$methods = get_class_methods($checker);
15+
$methods = array_filter($methods, fn($x) => $x !== '__construct');
16+
$results = [];
17+
$rows = [];
18+
$errors = 0;
19+
$warnings = 0;
20+
$pass = 0;
21+
22+
$check_fatal_error = false;
23+
24+
foreach ($methods as $method) {
25+
// directories valid needs to be run via web. use includes/web.php to do that.
26+
if ($method == 'directories_valid') {
27+
$ob_site = OB_SITE;
28+
if (!str_ends_with($ob_site, '/')) {
29+
$ob_site .= '/';
30+
}
31+
32+
// This currently fails on most installs on account of the server not allowing direct access to the tools directory.
33+
$web_check_result = json_decode(file_get_contents($ob_site . 'tools/cli/includes/web.php'), true);
34+
$result = $web_check_result['directories_valid'] ?? ['Directories', 'Unable to check directory permissions.', 1];
35+
} else {
36+
$result = $checker->$method();
37+
}
38+
$results[] = $result;
39+
40+
$formatting1 = '';
41+
$formatting2 = '';
42+
43+
switch ($result[2]) {
44+
case 0:
45+
$formatting = "\033[32m";
46+
$pass++;
47+
break;
48+
case 1:
49+
$formatting = "\033[33m";
50+
$warnings++;
51+
break;
52+
case 2:
53+
$formatting = "\033[31m";
54+
$errors++;
55+
}
56+
57+
// sometimes we get multiple strings in an array that needs imploding.
58+
if (is_array($result[1])) {
59+
$result[1] = implode(' ', $result[1]);
60+
}
61+
62+
$rows[] = [[$formatting,$result[0]], [$formatting, $result[1]]];
63+
64+
if ($result[2] > 1) {
65+
$check_fatal_error = true;
66+
break;
67+
}
68+
}
69+
70+
Helpers::table(rows: $rows);
71+
72+
if ($check_fatal_error) {
73+
echo "\033[31m";
74+
echo PHP_EOL . 'Error detected, testing stopped . Correct the above error then run again . ' . PHP_EOL;
75+
echo "\033[0m";
76+
77+
return false;
78+
} else {
79+
echo PHP_EOL .
80+
"\033[32m" . str_pad($pass, 2, ' ', STR_PAD_LEFT) . " pass\033[0m " .
81+
"\033[33m" . str_pad($warnings, 2, ' ', STR_PAD_LEFT) . " warnings\033[0m " .
82+
"\033[31m" . str_pad($errors, 2, ' ', STR_PAD_LEFT) . " errors\033[0m" . PHP_EOL;
83+
}
84+
85+
return true;
86+
}
87+
}

core/cli/CheckMedia.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace OpenBroadcaster\CLI;
4+
5+
use OpenBroadcaster\Base\CLI;
6+
7+
class CheckMedia extends CLI
8+
{
9+
public function run(array $args): bool
10+
{
11+
$this->db->query('select * from media order by id');
12+
13+
$media = $this->db->assoc_list();
14+
$media_total = count($media);
15+
$media_current = 0;
16+
$media_errors = 0;
17+
$messages = '';
18+
19+
echo "Processing {$media_total} media items in database:" . PHP_EOL;
20+
21+
foreach ($media as $nfo) {
22+
// Check that media file exists in the correct location.
23+
if ($nfo['is_archived'] == 1) {
24+
$dir = OB_MEDIA_ARCHIVE;
25+
} elseif ($nfo['is_approved'] == 0) {
26+
$dir = OB_MEDIA_UNAPPROVED;
27+
} else {
28+
$dir = OB_MEDIA;
29+
}
30+
31+
$filename = $dir . '/' . $nfo['file_location'][0] . '/' . $nfo['file_location'][1] . '/' . $nfo['filename'];
32+
33+
if (! file_exists($filename)) {
34+
$media_errors += 1;
35+
$messages .= "\033[31mMissing file:\033[0m {$filename}" . PHP_EOL;
36+
37+
// Try to find the actual filename in that directory.
38+
$check_files = scandir($dir . '/' . $nfo['file_location'][0] . '/' . $nfo['file_location'][1]);
39+
$fix_filename = null;
40+
41+
// Check for possibly renamed file.
42+
foreach ($check_files as $check_file) {
43+
if (preg_match('/' . $nfo['id'] . '-/', $check_file)) {
44+
$fix_filename = $check_file;
45+
break;
46+
}
47+
}
48+
49+
if ($fix_filename) {
50+
$messages .= 'Probable file: ' . $nfo['filename'] . ' -> ' . $fix_filename . PHP_EOL;
51+
}
52+
53+
echo PHP_EOL;
54+
} else {
55+
if (filesize($filename) === 0) {
56+
$media_errors += 1;
57+
$messages .= "\033[31mZero-byte file found:\033[0m {$filename}" . PHP_EOL;
58+
}
59+
}
60+
61+
// Show media processing update.
62+
$media_current += 1;
63+
64+
if (($media_current % 1000) === 0) {
65+
echo "Checked {$media_current} media files." . PHP_EOL;
66+
}
67+
}
68+
69+
if ($messages) {
70+
echo PHP_EOL . $messages;
71+
}
72+
73+
echo
74+
PHP_EOL . "\033[32m" . str_pad($media_total - $media_errors, 2, ' ', STR_PAD_LEFT) . " pass\033[0m " .
75+
"\033[31m" . str_pad($media_errors, 2, ' ', STR_PAD_LEFT) . " errors\033[0m " . PHP_EOL;
76+
77+
return true;
78+
}
79+
}

core/cli/Passwd.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace OpenBroadcaster\CLI;
4+
5+
use OpenBroadcaster\Base\CLI;
6+
7+
class Passwd extends CLI
8+
{
9+
public function run(array $args): bool
10+
{
11+
// requires valid install
12+
Helpers::requireValid();
13+
14+
$username = $args[0] ?? null;
15+
if (! $username) {
16+
(new OBCLI())->help();
17+
return false;
18+
}
19+
20+
$this->db->where('username', $username);
21+
$user = $this->db->get_one('users');
22+
if (!$user) {
23+
echo 'User not found.' . PHP_EOL;
24+
exit(1);
25+
}
26+
27+
$cli_specified_password = $args[1] ?? '';
28+
if (trim($cli_specified_password)) {
29+
if (strlen($cli_specified_password) < 6) {
30+
echo 'Password must be at least 6 characters long.' . PHP_EOL;
31+
return false;
32+
}
33+
$password = $cli_specified_password;
34+
} else {
35+
exec('stty -echo');
36+
37+
$password = '';
38+
$password_again = '';
39+
40+
$valid = false;
41+
do {
42+
echo 'New password: ';
43+
$password = trim(readline());
44+
echo PHP_EOL . 'New password (again): ';
45+
$password_again = trim(readline());
46+
47+
if ($password != $password_again) {
48+
echo PHP_EOL . 'Passwords do not match.' . PHP_EOL;
49+
} elseif (strlen($password) < 6) {
50+
echo PHP_EOL . 'Password must be at least 6 characters long.' . PHP_EOL;
51+
} else {
52+
$valid = true;
53+
}
54+
} while (!$valid);
55+
56+
exec('stty echo');
57+
}
58+
59+
$passsword_hashed = password_hash($password . OB_HASH_SALT, PASSWORD_DEFAULT);
60+
61+
$this->db->where('username', $username);
62+
$this->db->update('users', ['password' => $passsword_hashed]);
63+
64+
echo PHP_EOL . 'Password updated.' . PHP_EOL;
65+
66+
return true;
67+
}
68+
}

core/cli/check_install.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

core/cli/check_media.php

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)