<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12091] sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory: Photo Directory, Uploads: Enforce valid photo dimensions.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/12091">12091</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/12091","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>coffee2code</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-26 22:27:24 +0000 (Mon, 26 Sep 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Photo Directory, Uploads: Enforce valid photo dimensions.

- Add client-side JS checks
- Add server-side checks</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryassetsjssubmitjs">sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js</a></li>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryincuploadsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryassetsjssubmitjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js      2022-09-26 22:23:13 UTC (rev 12090)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js        2022-09-26 22:27:24 UTC (rev 12091)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -50,6 +50,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Check validity of form fields on submission.
</span><span class="cx" style="display: block; padding: 0 10px">        photo_upload_form.addEventListener( 'submit', e => {
</span><span class="cx" style="display: block; padding: 0 10px">                e.preventDefault();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                PhotoDir.doingSubmit = true;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                const photo_upload_form = e.target;
</span><span class="cx" style="display: block; padding: 0 10px">                const photo_upload_input = document.getElementById( 'ug_photo' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -140,7 +141,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        photoRemoveFieldError( field );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        let errorMessage = field.validationMessage;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        const cssClass = PhotoDir.error_class;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ const stillProcessing = errorMessage === PhotoDir.msg_validating_dimensions;
+       const cssClass = stillProcessing ? 'processing' : PhotoDir.error_class;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! errorMessage ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return '';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -151,11 +153,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                errorMessage = PhotoDir.err_field_required;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        errorEl = document.createElement( 'span' );
-       errorEl.setAttribute( 'class', cssClass );
-       errorEl.innerHTML = errorMessage;
-       field.insertAdjacentElement( 'afterend', errorEl );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( PhotoDir.doingSubmit || ! stillProcessing ) {
+               errorEl = document.createElement( 'span' );
+               errorEl.setAttribute( 'class', cssClass );
+               errorEl.innerHTML = errorMessage;
+               field.insertAdjacentElement( 'afterend', errorEl );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Show an error and end pending submission state unless still processing.
+       if ( ! stillProcessing ) {
+               PhotoDir.doingSubmit = false;
+               photoSetSubmitButtonDisabled( field.closest( 'form' ), false );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return '';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,11 +188,73 @@
</span><span class="cx" style="display: block; padding: 0 10px">                error = photoCheckFileSize( field );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Check for file dimension error.
+       if ( ! error ) {
+               // Hack: Wait for file dimensions check to complete before
+               // determining true validity. Once it has done so, it will
+               // potentially trigger submit if warranted.
+               field.setCustomValidity( PhotoDir.msg_validating_dimensions );
+               error = true;
+               photoCheckFileDimensions( field );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         return error;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px">  * Checks if the file selected via the file input field object is within an
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * acceptable file dimension range and sets custom validity message accordingly.
+ *
+ * An appropriate error message is defined if the file is too long or too short.
+ * If there is no file selected, or the file is of sufficient size, then any
+ * existing custom validity message is cleared.
+ *
+ * @param {HTMLElement} field - The HTML file input field element.
+ * @return {Promise} Promise where result is true if file dimensions are invalid, else false.
+ */
+function photoCheckFileDimensions( field ) {
+       const MIN_SIZE = PhotoDir.min_file_dimension; // In px.
+       const MAX_SIZE = PhotoDir.max_file_dimension; // In px.
+
+       const files = field.files;
+
+       if ( files.length > 0 ) {
+               const reader = new FileReader();
+
+               reader.addEventListener( 'load', async (e) => {
+                       const img = new Image();
+                       img.src = e.target.result;
+
+                       img.decode().then( () => {
+                               let file_width = img.width;
+                               let file_height = img.height;
+
+                               if ( file_height > MAX_SIZE || file_width > MAX_SIZE ) {
+                                       field.setCustomValidity( PhotoDir.err_file_too_long );
+                               } else if ( file_height < MIN_SIZE || file_width < MIN_SIZE ) {
+                                       field.setCustomValidity( PhotoDir.err_file_too_short );
+                               } else {
+                                       // No custom constraint violation.
+                                       field.setCustomValidity( '' );
+                               }
+
+                               photoShowFileError( field );
+                       } );
+               }, false );
+
+               reader.readAsDataURL( files.item( 0 ) );
+
+               // Return true. This will be rectified once the actual image dimensions are checked.
+               return true;
+       }
+
+       // No custom constraint violation.
+       field.setCustomValidity( '' );
+       return false;
+}
+
+/**
+ * Checks if the file selected via the file input field object is within an
</ins><span class="cx" style="display: block; padding: 0 10px">  * acceptable file size range and sets custom validity message accordingly.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * An appropriate error message is defined if the file is too large or too small.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -219,12 +291,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * Shows error messages for the file upload input.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param {HTMLElement} field - The HTML file input field element.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return {Promise}
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function photoShowFileErrors( field ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+async function photoShowFileErrors( field ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Checks custom file input validation. A custom validity error message gets
</span><span class="cx" style="display: block; padding: 0 10px">        // set on the field if a validation fails.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        photoCheckFileValidations( field );
-       photoShowFileError( field );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ photoCheckFileValidations( field ).then( () => {
+               photoShowFileError( field )
+       });
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -239,6 +313,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Remove any existing error message.
</span><span class="cx" style="display: block; padding: 0 10px">        photoRemoveFieldError( field );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Hack: If this gets called and everything validates, the form can be
+       // submitted.
+       const upload_form = field.closest( 'form' );
+       if ( PhotoDir.doingSubmit ) {
+               if ( upload_form.checkValidity() ) {
+                       upload_form.submit();
+                       return;
+               } else {
+                       document.getElementById( 'wporg-photo-upload' ).scrollIntoView( true );
+                       photoSetSubmitButtonDisabled( field.closest( 'form' ), false );
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // Return if no legitimate custom error to report.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! errorMessage ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return;
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsphotodirectoryincuploadsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php  2022-09-26 22:23:13 UTC (rev 12090)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php    2022-09-26 22:27:24 UTC (rev 12091)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -269,6 +269,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'max_file_size' => self::get_maximum_photo_file_size(),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'min_file_size' => self::get_minimum_photo_file_size(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                       // File dimensions.
+                                       'err_file_too_long'     => sprintf(
+                                               /** translators: %d: The maximum number of pixels. */
+                                               __( 'The selected file cannot be longer in either length or width than %dpx.', 'wporg-photos' ),
+                                               self::big_image_size_threshold( 0 )
+                                       ),
+                                       'err_file_too_short'    => sprintf(
+                                               /** translators: %d: The minimum number of pixels. */
+                                               __( 'The selected file must be longer in both length and width than %dpx.', 'wporg-photos' ),
+                                               self::get_minimum_photo_dimension()
+                                       ),
+                                       'max_file_dimension' => self::big_image_size_threshold( 0 ),
+                                       'min_file_dimension' => self::get_minimum_photo_dimension(),
+                                       'msg_validating_dimensions' => __( 'Validating photo dimensions&hellip;', 'wporg-photos' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ]
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -384,6 +399,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                self::get_maximum_photo_file_size( false )
</span><span class="cx" style="display: block; padding: 0 10px">                                        );
</span><span class="cx" style="display: block; padding: 0 10px">                                        break;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                case 'file-too-long':
+                                       $rejection = sprintf(
+                                               __( 'Your submission is too large. Please submit a photo with length and width each smaller than %dpx.', 'wporg-photos' ),
+                                               self::big_image_size_threshold( 0 )
+                                       );
+                                       break;
+                               case 'file-too-short':
+                                       $rejection = sprintf(
+                                               __( 'Your submission is too small. Please submit a photo with length and width each larger than %dpx.', 'wporg-photos' ),
+                                               self::get_minimum_photo_dimension()
+                                       );
+                                       break;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 case 'file-too-small':
</span><span class="cx" style="display: block; padding: 0 10px">                                        $rejection = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px">                                                __( 'The file size for your submission is too small. Please submit a photo larger than %d MB in size.', 'wporg-photos' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -580,6 +607,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return 'file-not-jpg';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Check image dimensions.
+               $max_dimension = self::big_image_size_threshold( 0 );
+               $min_dimension = self::get_minimum_photo_dimension();
+               if ( $width > $max_dimension || $length > $max_dimension ) {
+                       return 'file-too-long';
+               }
+               if ( $width < $min_dimension || $length < $min_dimension ) {
+                       return 'file-too-short';
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $_POST['photo_copyright'] ) || ! $_POST['photo_copyright'] ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return 'checkbox_unchecked_copyright';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span></span></pre>
</div>
</div>

</body>
</html>