diff --git a/precache.php b/precache.php new file mode 100644 index 0000000..843b57a --- /dev/null +++ b/precache.php @@ -0,0 +1,231 @@ + 0 && !empty($profile['active']) && bratonien_tools_profile_watermark_path($profile)) + { + $profiles[$profile_id] = $profile; + } +} + +if (!$profiles) +{ + echo "Keine aktiven Wasserzeichenprofile vorhanden.\n"; + exit(0); +} + +$categories = bratonien_tools_get_category_tree(); +$rules = bratonien_tools_get_album_rules(); +$defaults = bratonien_tools_get_watermark_defaults(); +$category_profiles = array(); +foreach ($categories as $category) +{ + $category_id = (int)$category['id']; + $effective = bratonien_tools_resolve_album_rule($category_id, $categories, $rules, $defaults); + if ($effective['mode'] === 'profile' && !empty($effective['profile_id'])) + { + $profile_id = (int)$effective['profile_id']; + if (isset($profiles[$profile_id])) + { + $category_profiles[$category_id] = $profile_id; + } + } +} + +$image_categories = array(); +$result = pwg_query('SELECT image_id, category_id FROM '.IMAGE_CATEGORY_TABLE.' ORDER BY image_id, category_id'); +while ($row = pwg_db_fetch_assoc($result)) +{ + $image_id = (int)$row['image_id']; + $image_categories[$image_id][] = (int)$row['category_id']; +} + +$root_url = get_absolute_root_url(); +$endpoint = rtrim($root_url, '/').'/plugins/'.BRATONIEN_TOOLS_ID.'/watermark.php'; +$types = ImageStdParams::get_defined_type_map(); + +$checked = 0; +$generated = 0; +$already_cached = 0; +$skipped = 0; +$errors = 0; + +$result = pwg_query('SELECT * FROM '.IMAGES_TABLE.' ORDER BY id'); +while ($image = pwg_db_fetch_assoc($result)) +{ + $image_id = (int)$image['id']; + $profile_ids = array(); + + foreach ($image_categories[$image_id] ?? array() as $category_id) + { + if (isset($category_profiles[$category_id])) + { + $profile_ids[$category_profiles[$category_id]] = true; + } + } + + if (!$profile_ids) + { + $skipped++; + continue; + } + + $src_image = new SrcImage($image); + $source_path = $src_image->get_path(); + if (!is_file($source_path) || !is_readable($source_path)) + { + fwrite(STDERR, "Bild #$image_id: Quelldatei nicht lesbar: $source_path\n"); + $errors++; + continue; + } + + foreach (array_keys($profile_ids) as $profile_id) + { + $profile = $profiles[$profile_id]; + + foreach ($types as $type => $params) + { + $checked++; + + if ($src_image->has_size()) + { + $size = $params->compute_final_size($src_image->get_size()); + if ($size && ($size[0] < (int)$profile['min_width'] || $size[1] < (int)$profile['min_height'])) + { + $skipped++; + continue; + } + } + + $derivative = new DerivativeImage($params, $src_image); + $derivative_path = $derivative->get_path(); + $root_path = PHPWG_ROOT_PATH; + if (strpos($derivative_path, $root_path) !== 0) + { + $skipped++; + continue; + } + + $rel_url = substr($derivative_path, strlen($root_path)); + if (strpos($rel_url, PWG_DERIVATIVE_DIR) !== 0) + { + $skipped++; + continue; + } + + $extension = strtolower(pathinfo($derivative_path, PATHINFO_EXTENSION)); + $descriptor = bratonien_tools_runtime_cache_descriptor($rel_url, $profile, $params, $source_path, $extension); + if (!$descriptor) + { + $errors++; + continue; + } + + if (is_file($descriptor['path']) && is_readable($descriptor['path'])) + { + $already_cached++; + continue; + } + + $profile_version = $descriptor['profile_version']; + $signature = bratonien_tools_runtime_sign($rel_url, $profile_id, $profile_version); + $url = $endpoint.'?p='.$profile_id.'&v='.rawurlencode($profile_version). + '&u='.rawurlencode(bratonien_tools_runtime_b64url_encode($rel_url)).'&s='.$signature; + + $ok = false; + if (function_exists('curl_init')) + { + $ch = curl_init($url); + curl_setopt_array($ch, array( + CURLOPT_RETURNTRANSFER => false, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_MAXREDIRS => 2, + CURLOPT_CONNECTTIMEOUT => 3, + CURLOPT_TIMEOUT => 120, + CURLOPT_FAILONERROR => false, + CURLOPT_USERAGENT => 'Bratonien-Watermark-Precache/1.0', + CURLOPT_WRITEFUNCTION => static function ($ch, $data) { return strlen($data); }, + )); + curl_exec($ch); + $status = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curl_error = curl_error($ch); + curl_close($ch); + $ok = ($status >= 200 && $status < 300 && is_file($descriptor['path'])); + if (!$ok) + { + fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $type: Precache fehlgeschlagen (HTTP $status".($curl_error !== '' ? ", $curl_error" : '').").\n"); + } + } + else + { + $context = stream_context_create(array('http'=>array('timeout'=>120, 'ignore_errors'=>true))); + @file_get_contents($url, false, $context); + $ok = is_file($descriptor['path']); + if (!$ok) + { + fwrite(STDERR, "Bild #$image_id / Profil #$profile_id / $type: Precache fehlgeschlagen.\n"); + } + } + + if ($ok) + { + $generated++; + } + else + { + $errors++; + } + } + } +} + +printf( + "Wasserzeichen-Precache: %d Varianten geprueft, %d neu erzeugt, %d bereits vorhanden, %d uebersprungen, %d Fehler.\n", + $checked, + $generated, + $already_cached, + $skipped, + $errors +); + +exit($errors > 0 ? 1 : 0);