private function buildScreenshots(Addon $addon, PackageInterface $package, $path) { $extra = $package->getExtra(); $screenshots = array(); $target = self::SCREENSHOTS_DIR . '/' . $addon->Name; if (isset($extra['screenshots'])) { $screenshots = (array) $extra['screenshots']; } elseif (isset($extra['screenshot'])) { $screenshots = (array) $extra['screenshot']; } // Delete existing screenshots. foreach ($addon->Screenshots() as $screenshot) { $screenshot->delete(); } $addon->Screenshots()->removeAll(); foreach ($screenshots as $screenshot) { if (!is_string($screenshot)) { continue; } $scheme = parse_url($screenshot, PHP_URL_SCHEME); // Handle absolute image URLs. if ($scheme == 'http' || $scheme == 'https') { $temp = TEMP_FOLDER . '/' . md5($screenshot); if (!copy($screenshot, $temp)) { continue; } $data = array('name' => basename($screenshot), 'size' => filesize($temp), 'tmp_name' => $temp, 'error' => 0); } else { $source = $path . '/' . ltrim($screenshot, '/'); // Prevent directory traversal. if ($source != realpath($source)) { continue; } if (!file_exists($source)) { continue; } $data = array('name' => basename($source), 'size' => filesize($source), 'tmp_name' => $source, 'error' => 0); } $upload = new Upload(); $upload->setValidator(new AddonBuilderScreenshotValidator()); $upload->load($data, $target); if ($file = $upload->getFile()) { $addon->Screenshots()->add($file); } } }