Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config.php
public/components
public/screenshots
vendor
2 changes: 1 addition & 1 deletion classes/model/feed/marks.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function ids_and_ts($redis_key, $query, $params = [])
function prepare_items($results, $total = null, $params = [])
{
# Next?
$next = $params['limit'] && count($results) > $params['limit'] ? (int) array_pop($results) : null;
$next = !empty($params['limit']) && count($results) > $params['limit'] ? (int) array_pop($results) : null;
# Items
$items = empty($results) ? [] : $this->table('marks')->get(array_keys($results));
# Result
Expand Down
7 changes: 6 additions & 1 deletion classes/model/table/screenshots.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class screenshots extends table

public $tablename = 'bm_screenshots';

function create($set = [])
{
return parent::create(['created' => db::now()] + $set);
}

function preload_for_marks($marks)
{
# Get list of link ids for which we need to retrieve screenshots
Expand Down Expand Up @@ -46,7 +51,7 @@ function ensure_entry_exists_for_mark($mark)
$params = ['link' => $mark->link_id];
$existing = $this->where($params)->and_where("created > DATE_SUB('$now', INTERVAL 1 DAY)");
if ($existing->count() == 0) {
$screenshot = $this->create($params + ['status' => 0, 'created' => $now]);
$screenshot = $this->create($params + ['status' => 0]);
$this->service('amqp')->push(['id' => $screenshot->id], 'take_screenshot');
}
}
Expand Down
85 changes: 85 additions & 0 deletions scripts/make-screenshots.script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env php
<?php

use
amateur\model\cache,
amateur\model\db,
amateur\model\runtime,
amateur\model\table;

require_once dirname(__DIR__) . '/bootstrap.php';

replaceable('request_host', function () { return 'localhost:8002'; });

$screenshot_extension = 'png';
$screenshot_base_dir = root_dir . '/public/screenshots/';
$screenshot_base_url = absolute_url('/screenshots/');

$user = table('users')->get_one('login', 'znarf');
$marks = model('marks')->from_user($user, ['limit' => 1000]);

foreach ($marks['items'] as $mark) {
error_log("Checking {$mark->url}");

$screenshot = table('screenshots')->for_mark($mark);
if (!empty($screenshot['url'])) {
error_log("Existing {$screenshot['url']}");
continue;
}

$parsed_url = parse_url($mark->url);
if (empty($parsed_url['host'])) {
continue;
}

$folder = date("Y/m/d/" , time() - date('Z') );
$relative = $folder . md5($mark->url) . '.' . $bm_screenshot_extension;
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable $bm_screenshot_extension is undefined. It should be $screenshot_extension which is defined on line 14.

Suggested change
$relative = $folder . md5($mark->url) . '.' . $bm_screenshot_extension;
$relative = $folder . md5($mark->url) . '.' . $screenshot_extension;

Copilot uses AI. Check for mistakes.
$screenshot_absolute_path = $screenshot_base_dir . $relative;
$screenshot_absolute_url = $screenshot_base_url . $relative;

if (!file_exists($screenshot_absolute_path)) {
$parameters = [
'url' => $parsed_url['host'],
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using only the host for the screenshot API may not capture the full page. Consider using $mark->url instead to capture the complete URL including path and query parameters.

Suggested change
'url' => $parsed_url['host'],
'url' => $mark->url,

Copilot uses AI. Check for mistakes.
'width' => 112,
'height' => 83,
'token' => flag('miniature_api_key'),
];
$screenshot_api_url = 'https://api.miniature.io/' . '?' . http_build_query($parameters);

// $parameters = [
// 'url' => $mark->url,
// 'width' => 112,
// ];

// $screenshot_api_url = 'https://api.thumbnail.ws/api/ab264f9ceea81bfaa0803a670001777f487268a1f197/thumbnail/get' . '?' . http_build_query($parameters);

error_log("Asking {$screenshot_api_url}");
$image = file_get_contents($screenshot_api_url);
if (!$image) {
continue;
}

$size = strlen($image);
error_log("Size {$size}");
if ($size <= 1252 || $size === 2185 || $size === 1857) {
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magic numbers for suspicious file sizes should be defined as named constants or documented with comments explaining what these specific sizes represent.

Copilot uses AI. Check for mistakes.
error_log("Suspicious size");
continue;
}

if (!is_dir($screenshot_dir . $folder)) {
error_log("Creating {$folder}");
mkdir($screenshot_dir . $folder, 0777, true);
Comment on lines +69 to +71
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable $screenshot_dir is undefined. It should be $screenshot_base_dir which is defined on line 15.

Suggested change
if (!is_dir($screenshot_dir . $folder)) {
error_log("Creating {$folder}");
mkdir($screenshot_dir . $folder, 0777, true);
if (!is_dir($screenshot_base_dir . $folder)) {
error_log("Creating {$folder}");
mkdir($screenshot_base_dir . $folder, 0777, true);

Copilot uses AI. Check for mistakes.
}

error_log("Writing {$screenshot_absolute_path}");
file_put_contents($screenshot_absolute_path, $image);
chmod($screenshot_absolute_path, 0777);
Copy link

Copilot AI Oct 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting file permissions to 0777 makes the file world-writable, which is a security risk. Consider using more restrictive permissions like 0644 or 0755.

Suggested change
chmod($screenshot_absolute_path, 0777);
chmod($screenshot_absolute_path, 0644);

Copilot uses AI. Check for mistakes.
}

$params = ['status' => 1, 'generated' => db::now(), 'url' => $screenshot_absolute_url];
if ($screenshot) {
$screenshot->update($params);
} else {
table('screenshots')->create($params + ['link' => $mark->link_id]);
}
}