Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.28% covered (success)
95.28%
101 / 106
72.73% covered (warning)
72.73%
8 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_WPCOM_AI_Marker
95.28% covered (success)
95.28%
101 / 106
72.73% covered (warning)
72.73%
8 / 11
41
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 read
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 write
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
3.71
 read_local
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 read_from_url
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 read_c2pa
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
8.01
 run_tool
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
1 / 1
10
 stop
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 is_synthetic
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 log_error
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 bump_stat
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Reads and writes an image's AI marker with exiftool and c2patool.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * Reads an AI IPTC DigitalSourceType from XMP or C2PA, and writes it to XMP.
10 */
11class Jetpack_WPCOM_AI_Marker {
12
13    /**
14     * Limits the bytes of tool output to read to avoid exhausting PHP's memory on a huge manifest.
15     */
16    const MAX_OUTPUT = 262144;
17
18    /**
19     * The IPTC DigitalSourceTypes for AI or synthetic media to keep.
20     *
21     * @see https://cv.iptc.org/newscodes/digitalsourcetype/
22     */
23    const SYNTHETIC_TYPES = array(
24        'http://cv.iptc.org/newscodes/digitalsourcetype/trainedAlgorithmicMedia',
25        'http://cv.iptc.org/newscodes/digitalsourcetype/compositeWithTrainedAlgorithmicMedia',
26        'http://cv.iptc.org/newscodes/digitalsourcetype/compositeSynthetic',
27    );
28
29    /**
30     * The jetpack_bump_stats_extras group for this caller.
31     *
32     * @var string
33     */
34    private $stats_group;
35
36    /**
37     * The errors this request sent to Logstash, by blog, so a bulk job sends each one once.
38     *
39     * @var true[][]
40     */
41    private static $logged = array();
42
43    /**
44     * The tools that timed out or couldn't start in this request, so later saves don't wait on them again.
45     *
46     * @var true[]
47     */
48    private static $broken_tools = array();
49
50    /**
51     * Sets the stats group.
52     *
53     * @param string $stats_group The jetpack_bump_stats_extras group for this caller.
54     */
55    public function __construct( $stats_group ) {
56        $this->stats_group = $stats_group;
57    }
58
59    /**
60     * Reads the AI DigitalSourceType from a path or a URL.
61     *
62     * @param string $file Path or URL of the source image.
63     * @return string The type, or '' when the file has no AI type.
64     */
65    public function read( $file ) {
66        // The marker is optional, so no error may stop the save.
67        try {
68            return preg_match( '|^https?://|', $file )
69                ? $this->read_from_url( $file )
70                : $this->read_local( $file );
71        } catch ( Throwable $e ) {
72            // The class only: messages can hold file paths.
73            $this->log_error( 'read-error', array( 'error' => get_class( $e ) ) );
74            return '';
75        }
76    }
77
78    /**
79     * Writes the type with exiftool.
80     *
81     * @param string $filename            Path of the image.
82     * @param string $digital_source_type The type to write.
83     */
84    public function write( $filename, $digital_source_type ) {
85        try {
86            $exiftool = jetpack_wpcom_ai_marker_tool_path( 'exiftool' );
87            // Stops exiftool recording its version.
88            $output = $this->run_tool( 'exiftool', 'write', array( $exiftool, '-config', '', '-m', '-q', '-overwrite_original', '-XMP-x:XMPToolkit=', '-XMP-iptcExt:DigitalSourceType=' . $digital_source_type, $filename ) );
89        } catch ( Throwable $e ) {
90            $this->log_error( 'write-error', array( 'error' => get_class( $e ) ) );
91            return;
92        }
93
94        // run_tool() counts and logs a failed write.
95        if ( null !== $output ) {
96            $this->bump_stat( 'written' );
97        }
98    }
99
100    /**
101     * Reads the AI DigitalSourceType from XMP, then from C2PA when the file has a manifest.
102     *
103     * @param string $file Path of the source image.
104     * @return string The type, or '' when the file has no AI type.
105     */
106    private function read_local( $file ) {
107        $exiftool = jetpack_wpcom_ai_marker_tool_path( 'exiftool' );
108        // No config file: exiftool runs it as Perl.
109        $output              = (string) $this->run_tool( 'exiftool', 'read', array( $exiftool, '-config', '', '-m', '-S', '-XMP-iptcExt:DigitalSourceType', '-JUMBF:JUMDLabel', $file ) );
110        $digital_source_type = preg_match( '/^DigitalSourceType: (.*)$/m', $output, $match ) ? trim( $match[1] ) : '';
111        if ( self::is_synthetic( $digital_source_type ) ) {
112            return $digital_source_type;
113        }
114
115        return str_contains( $output, 'JUMDLabel: ' ) ? $this->read_c2pa( $file ) : '';
116    }
117
118    /**
119     * Core loads the source by URL when the file isn't on this host.
120     *
121     * @param string $url URL of the source image.
122     * @return string The type, or '' when the file has no AI type or the download fails.
123     */
124    private function read_from_url( $url ) {
125        require_once ABSPATH . 'wp-admin/includes/file.php';
126        $this->bump_stat( 'remote-source' );
127
128        $temp = download_url( $url, 10 );
129        if ( is_wp_error( $temp ) ) {
130            $this->log_error( 'download-failed', array( 'error' => $temp->get_error_code() ) );
131            return '';
132        }
133
134        try {
135            return $this->read_local( $temp );
136        } finally {
137            // wp_delete_file() hooks are for uploads.
138            unlink( $temp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
139        }
140    }
141
142    /**
143     * Reads the active manifest first: c2patool lists manifests in no fixed order.
144     *
145     * @param string $file Path of the source image.
146     * @return string The type, or '' when there is no AI type or c2patool fails.
147     */
148    private function read_c2pa( $file ) {
149        $c2patool = jetpack_wpcom_ai_marker_tool_path( 'c2patool' );
150        $settings = __DIR__ . '/c2pa-settings.json';
151        // c2patool ignores a missing settings file, then fetches remote manifests.
152        if ( ! $c2patool || ! is_readable( $settings ) ) {
153            return '';
154        }
155
156        $store     = json_decode( (string) $this->run_tool( 'c2patool', 'read', array( $c2patool, '--settings', $settings, $file ) ), true );
157        $manifests = (array) ( $store['manifests'] ?? array() );
158        $active    = $store['active_manifest'] ?? '';
159        if ( isset( $manifests[ $active ] ) ) {
160            $manifests = array( $active => $manifests[ $active ] ) + $manifests;
161        }
162
163        foreach ( $manifests as $manifest ) {
164            foreach ( (array) ( $manifest['assertions'] ?? array() ) as $assertion ) {
165                foreach ( (array) ( $assertion['data']['actions'] ?? array() ) as $action ) {
166                    $digital_source_type = $action['digitalSourceType'] ?? '';
167                    if ( self::is_synthetic( $digital_source_type ) ) {
168                        $this->bump_stat( 'from-c2pa' );
169                        return $digital_source_type;
170                    }
171                }
172            }
173        }
174
175        return '';
176    }
177
178    /**
179     * Runs a tool without a shell, with a time limit: PHP's own limit ignores other programs.
180     *
181     * @param string   $name    exiftool or c2patool, for the error log.
182     * @param string   $step    'read' or 'write', for the error log.
183     * @param string[] $command The program and its arguments.
184     * @return string|null Its output, or null when it fails, times out or prints too much.
185     */
186    private function run_tool( $name, $step, array $command ) {
187        if ( isset( self::$broken_tools[ $name ] ) ) {
188            return null;
189        }
190
191        /**
192         * Filters how many seconds a tool may run.
193         *
194         * @since $$next-version$$
195         *
196         * @param int|float $seconds The time limit.
197         */
198        $deadline = microtime( true ) + (float) apply_filters( 'jetpack_wpcom_ai_marker_tool_time_limit', 2 );
199
200        // No stdin, so a tool can't wait for input.
201        $descriptors = array(
202            0 => array( 'null' ),
203            1 => array( 'pipe', 'w' ),
204            2 => array( 'null' ),
205        );
206        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.system_calls_proc_open
207        $process = proc_open( $command, $descriptors, $pipes );
208        $tool    = array(
209            'tool' => $name,
210            'step' => $step,
211        );
212        if ( ! is_resource( $process ) ) {
213            self::$broken_tools[ $name ] = true;
214            $this->log_error( 'could-not-start', $tool );
215            return null;
216        }
217
218        // Non-blocking, so a read can't outlast the deadline.
219        stream_set_blocking( $pipes[1], false );
220        $output = '';
221        // Read while waiting, so a full pipe can't block the tool. The read after its exit gets the rest.
222        do {
223            usleep( 10000 );
224            $status  = proc_get_status( $process );
225            $output .= (string) stream_get_contents( $pipes[1], self::MAX_OUTPUT + 1 - strlen( $output ) );
226            $too_big = strlen( $output ) > self::MAX_OUTPUT;
227        } while ( $status['running'] && ! $too_big && microtime( true ) < $deadline );
228
229        if ( $status['running'] ) {
230            self::stop( $process );
231        }
232        fclose( $pipes[1] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- A process pipe, not a file.
233        proc_close( $process );
234
235        // proc_close() returns -1 once proc_get_status() has seen the exit.
236        if ( $too_big ) {
237            $this->log_error( 'output-too-large', $tool );
238        } elseif ( $status['running'] ) {
239            self::$broken_tools[ $name ] = true;
240            $this->log_error( 'timed-out', $tool );
241        } elseif ( 127 === $status['exitcode'] ) {
242            // Older PHP on Linux starts the child even when the program is missing, and it exits 127.
243            self::$broken_tools[ $name ] = true;
244            $this->log_error( 'could-not-start', $tool );
245        } elseif ( 0 !== $status['exitcode'] ) {
246            $this->log_error( 'tool-failed', $tool + array( 'exit_code' => $status['exitcode'] ) );
247        } else {
248            return $output;
249        }
250
251        return null;
252    }
253
254    /**
255     * Sends SIGINT so exiftool deletes its temp file, then SIGKILL after 1 second.
256     *
257     * @param resource $process The tool's process.
258     */
259    private static function stop( $process ) {
260        proc_terminate( $process, 2 );
261        $grace = microtime( true ) + 1;
262        while ( microtime( true ) < $grace ) {
263            usleep( 10000 );
264            if ( ! proc_get_status( $process )['running'] ) {
265                return;
266            }
267        }
268        proc_terminate( $process, 9 );
269    }
270
271    /**
272     * Whether a type is in SYNTHETIC_TYPES.
273     *
274     * @param mixed $digital_source_type The type a tool reported.
275     * @return bool
276     */
277    private static function is_synthetic( $digital_source_type ) {
278        return in_array( $digital_source_type, self::SYNTHETIC_TYPES, true );
279    }
280
281    /**
282     * Counts an error, and sends it to Logstash: stats don't reach Atomic.
283     *
284     * @param string $message The error, also used as the stat.
285     * @param array  $extra   Details for the Logstash entry.
286     */
287    private function log_error( $message, array $extra = array() ) {
288        // read() and write() call this from their catch blocks, so it must not throw.
289        try {
290            $this->bump_stat( $message );
291
292            $blog_id = get_current_blog_id();
293            if ( isset( self::$logged[ $blog_id ][ $message ] ) ) {
294                return;
295            }
296            self::$logged[ $blog_id ][ $message ] = true;
297
298            $extra = array( 'group' => $this->stats_group ) + $extra;
299            /**
300             * Filters whether to send an AI marker error to Logstash.
301             *
302             * @since $$next-version$$
303             *
304             * @param bool   $enabled Whether to send the entry. Default true.
305             * @param string $message The error.
306             * @param array  $extra   The entry's details.
307             */
308            if ( apply_filters( 'jetpack_wpcom_ai_marker_log_enabled', true, $message, $extra ) ) {
309                // The endpoint Atomic posts to only accepts features with an "atomic" prefix.
310                \Automattic\Jetpack\Jetpack_Mu_Wpcom::log2logstash( 'atomic_image_ai_marker', $message, $extra );
311            }
312        } catch ( Throwable $e ) {
313            unset( $e );
314        }
315    }
316
317    /**
318     * Bumps a stat in this caller's group.
319     *
320     * @param string $value The stat.
321     */
322    private function bump_stat( $value ) {
323        // The marker is optional, so a stats hook mustn't stop the save.
324        try {
325            do_action( 'jetpack_bump_stats_extras', $this->stats_group, $value );
326        } catch ( Throwable $e ) {
327            unset( $e );
328        }
329    }
330}