<!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>[12086] sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory: Photo Directory, Uploads: Limit photo uploads to being between 1 and 20 MB in size.</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/12086">12086</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/12086","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 21:59:32 +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: Limit photo uploads to being between 1 and 20 MB in size.

- Add class constants for default min/max limits, and getters with filters.
- Add MAX_FILE_SIZE hidden input to upload form.
- Add client-side JS enforcement of limits.
- Add server-side enforcement of limits.</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 18:03:26 UTC (rev 12085)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/assets/js/submit.js        2022-09-26 21:59:32 UTC (rev 12086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,7 +23,18 @@
</span><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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        const photo_upload_field = document.getElementById('ug_photo');
+
+       if ( photo_upload_field ) {
+               // Add custom validation for upload file size.
+               photo_upload_field.addEventListener( 'change', e => {
+                       photoShowFileSizeError( photo_upload_field );
+               } );
+       }
+
+       // Check validity of form fields on submission.
</ins><span class="cx" style="display: block; padding: 0 10px">         photo_upload_form.addEventListener( 'submit', e => {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                photoShowFileSizeError( photo_upload_field );
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( photo_upload_form.checkValidity() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        photo_upload_submit.disabled = true;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,4 +42,79 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-photoSubmitLoaded();
</del><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of file
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Checks if the file selected via the file input field object is within an
+ * acceptable file size range and sets custom validity message accordingly.
+ *
+ * An appropriate error message is defined if the file is too large or too small.
+ * If there is no file selected, or the file is of sufficient size, then any
+ * existing custom validity message is cleared.
+ *
+ * @param {Object} field - The HTML file input field object.
+ */
+function photoCheckFileSize( field ) {
+       const MAX_SIZE = PhotoDir.max_file_size; // In bytes.
+       const MIN_SIZE = PhotoDir.min_file_size; // In bytes.
+
+       const files = field.files;
+
+       if ( files.length > 0 ) {
+               const file_size = files[0].size;
+
+               // Note: If changing the error message for either case, ensure the "// Don't show error message..."
+               // regex in `photoShowFileSizeError()` still matches them both.
+               if ( file_size >= MAX_SIZE ) {
+                       field.setCustomValidity( PhotoDir.err_file_too_large );
+                       return;
+               } else if ( file_size <= MIN_SIZE ) {
+                       field.setCustomValidity( PhotoDir.err_file_too_small );
+                       return;
+               }
+       }
+
+       // No custom constraint violation.
+       field.setCustomValidity('');
+}
+
+/**
+ * Handles the display of the error message for the file upload input.
+ *
+ * If the file input field has a file selected, it is checked to see if it is
+ * too large or too small. An appropriate error message is shown in either case.
+ * If no file is selected, or the file is of sufficient size, then no error is
+ * shown and any existing error for the field is cleared.
+ *
+ * This pseudo-mimics and also works around the jQuery Validation handling that
+ * doesn't play well with custom error reporting like this.
+ *
+ * @param {Object} field - The HTML file input field object.
+ */
+function photoShowFileSizeError( field ) {
+       // Check field for file size validation errors.
+       photoCheckFileSize( field );
+
+       const errorMessage = field.validationMessage;
+       const errorId = `${field.id}-error`;
+       let errorEl = document.getElementById( errorId );
+
+       // Don't show error message for any other error.
+       if ( ! errorMessage || ! field.validity.customError || ! / MB\.$/.test( errorMessage ) ) {
+               errorEl?.remove();
+               return;
+       }
+
+       if ( ! errorEl ) {
+               errorEl = document.createElement( 'label' );
+               field.after( errorEl );
+       }
+
+       errorEl.setAttribute( 'id', errorId );
+       errorEl.setAttribute( 'class', 'custom-error' );
+       errorEl.setAttribute( 'for', field.id );
+       errorEl.removeAttribute( 'style' );
+       errorEl.innerHTML = errorMessage;
+}
+
+document.addEventListener( 'DOMContentLoaded', () => {
+       photoSubmitLoaded();
+} );
</ins></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 18:03:26 UTC (rev 12085)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/uploads.php    2022-09-26 21:59:32 UTC (rev 12086)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,6 +27,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">        const MAX_PENDING_SUBMISSIONS = 5;
</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">+         * The default maximum allowed file size in megabytes.
+        *
+        * @see `get_maximum_photo_file_size()` for actually retrieving the maximum
+        * photo size, as it may be filtered.
+        * @var int|float
+        */
+       const MAX_PHOTO_FILE_SIZE = 20;
+
+       /**
+        * The default minimum allowed file size in megabytes.
+        *
+        * @see `get_minimum_photo_file_size()` for actually retrieving the minimum
+        * photo size, as it may be filtered.
+        * @var int|float
+        */
+       const MIN_PHOTO_FILE_SIZE = 1;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * The slug for the page used for photo uploads.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @var string
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -178,11 +196,72 @@
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Returns the maximum allowed file size.
+        *
+        * @param bool $in_bytes Should the value returned be in bytes instead of megabytes?
+        * @return int|float Maximum allowed file size. Returns value in bytes by default, but
+        *             will return value in megabytes if `$as_bytes` is false.
+        */
+       public static function get_maximum_photo_file_size( $as_bytes = true ) {
+               /**
+                * Filters the maximum allowed photo file size, in megabytes.
+                *
+                * @param int The maximum allowed photo file size, in megabytes.
+                */
+               $max_file_size = apply_filters( 'wporg_photos_max_photo_file_size', self::MAX_PHOTO_FILE_SIZE );
+
+               if ( $as_bytes ) {
+                       $max_file_size = round( $max_file_size * 1024 * 1024 );
+               }
+
+               return $max_file_size;
+       }
+
+       /**
+        * Returns the minimum allowed file size.
+        *
+        * @param bool $in_bytes Should the value returned be in bytes instead of megabytes?
+        * @return int|float Minimum allowed file size. Returns value in bytes by default, but
+        *             will return value in megabytes if `$as_bytes` is false.
+        */
+       public static function get_minimum_photo_file_size( $as_bytes = true ) {
+               /**
+                * Filters the minimum allowed photo file size, in megabytes.
+                *
+                * @param int The minimum allowed photo file size, in megabytes.
+                */
+               $min_file_size = apply_filters( 'wporg_photos_min_photo_file_size', self::MIN_PHOTO_FILE_SIZE );
+
+               if ( $as_bytes ) {
+                       $min_file_size = round( $min_file_size * 1024 * 1024 );
+               }
+
+               return $min_file_size;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Enqueues scripts for the photo submit page.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wp_enqueue_scripts() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_page( self::SUBMIT_PAGE_SLUG ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_enqueue_script( 'wporg-photos-submit', plugins_url( 'assets/js/submit.js', dirname( __FILE__ ) ), [], '1', true );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       wp_localize_script(
+                               'wporg-photos-submit',
+                               'PhotoDir',
+                               [
+                                       'err_file_too_large'    => sprintf(
+                                               __( 'The selected file cannot be larger than %s MB.', 'wporg-photos' ),
+                                               self::get_maximum_photo_file_size( false )
+                                       ),
+                                       'err_file_too_small'    => sprintf(
+                                               __( 'The selected file must be larger than %s MB.', 'wporg-photos' ),
+                                               self::get_minimum_photo_file_size( false )
+                                       ),
+                                       'max_file_size' => self::get_maximum_photo_file_size(),
+                                       'min_file_size' => self::get_minimum_photo_file_size(),
+                               ]
+                       );
</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">@@ -289,6 +368,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                case 'duplicate-file':
</span><span class="cx" style="display: block; padding: 0 10px">                                        $rejection = __( 'Your submission appears to be a duplicate of something uploaded before.', 'wporg-photos' );
</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-large':
+                                       $rejection = sprintf(
+                                               __( 'The file size for your submission is too large. Please submit a photo smaller than %d MB in size.', 'wporg-photos' ),
+                                               self::get_maximum_photo_file_size( false )
+                                       );
+                                       break;
+                               case 'file-too-small':
+                                       $rejection = sprintf(
+                                               __( 'The file size for your submission is too small. Please submit a photo larger than %d MB in size.', 'wporg-photos' ),
+                                               self::get_minimum_photo_file_size( false )
+                                       );
+                                       break;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 case 'insufficient-dimension':
</span><span class="cx" style="display: block; padding: 0 10px">                                        $rejection = sprintf( __( 'Your photo must have a width and height of at least %d pixels each.', 'wporg-photos' ), self::get_minimum_photo_dimension() );
</span><span class="cx" style="display: block; padding: 0 10px">                                        break;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -421,6 +512,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *                      specific validation issue.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected static function validate_upload_form() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( ! empty( $_FILES['files']['error'][0] ) ) {
+                       switch ( $_FILES['files']['error'][0] ) {
+                               case UPLOAD_ERR_INI_SIZE:
+                               case UPLOAD_ERR_FORM_SIZE:
+                                       return 'file-too-large';
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( empty( $_FILES['files']['tmp_name'][0] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return 'no-file-uploaded';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -429,6 +528,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return 'too-many-files';
</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 file size.
+               if ( ! empty( $_FILES['files']['size'][0] ) ) {
+                       $file_size = $_FILES['files']['size'][0];
+                       // Check if file is too large.
+                       //   (This is actually a fallback check in the event the MAX_FILE_SIZE
+                       //   directive in the upload form is missing or has been tampered with.
+                       //   Otherwise, PHP will already have invalidated a too-large upload.)
+                       if ( $file_size >= self::get_maximum_photo_file_size() ) {
+                               return 'file-too-large';
+                       }
+                       // Check if file is too small.
+                       if ( $file_size <= self::get_minimum_photo_file_size() ) {
+                               return 'file-too-small';
+                       }
+               }
+
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -726,6 +841,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                $content .= '[input type="hidden" name="post_title" value=""]' . "\n";
</span><span class="cx" style="display: block; padding: 0 10px">                                $content .= sprintf(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        '[input type="hidden" name="MAX_FILE_SIZE" value="%s"]' . "\n",
+                                       self::get_maximum_photo_file_size()
+                               );
+                               $content .= sprintf(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         '[input type="file" name="ug_photo" id="ug_photo" description="%s" required="true" aria-required="true"]' . "\n",
</span><span class="cx" style="display: block; padding: 0 10px">                                        esc_attr( __( 'Photo', 'wporg-photos' ) )
</span><span class="cx" style="display: block; padding: 0 10px">                                )
</span></span></pre>
</div>
</div>

</body>
</html>