Commit 244fcad4ebdbc14770c13bf1da91367c8ee95594

Authored by Charl Joseph Mert
1 parent df5ce670

Brand server without requiring access to FS

PT: 1243391

Fixed: - Cropping small images now skips and displays appropriate error message on the apply form
       - Preview reflects the actual now.

Updated by: Charl Joseph Mert
plugins/ktcore/admin/manageBranding.php
... ... @@ -52,6 +52,7 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
52 52  
53 53 private $maxLogoWidth = 313;
54 54 private $maxLogoHeight = 50;
  55 + public $supportedTypes = array('gif', 'png', 'pjpeg', 'jpe', 'jpeg', 'jpg', 'jfif', 'jfif-tbnl');
55 56  
56 57 function check() {
57 58  
... ... @@ -104,7 +105,7 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
104 105  
105 106 $aVocab['crop'] = 'Crop - Cut out a selection';
106 107 $aVocab['scale'] = 'Scale - Stretch or Shrink to fit';
107   - $aVocab['nothing'] = 'Don\'t do anything';
  108 + $aVocab['nothing'] = 'Don\'t do anything <span class="descriptiveText">(My image has the correct dimensions)</span>';
108 109  
109 110 //Adding document type lookup widget
110 111 $widgets[] = $oWF->get('ktcore.widgets.selection',array(
... ... @@ -117,7 +118,7 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
117 118 'label_method' => 'getName',
118 119 'simple_select' => true,
119 120 ));
120   -
  121 +
121 122 $oForm->setWidgets($widgets);
122 123 $oForm->setValidators($validators);
123 124  
... ... @@ -306,11 +307,14 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
306 307  
307 308 $logoFileName = 'var'.DIRECTORY_SEPARATOR.'branding'.DIRECTORY_SEPARATOR.'logo'.DIRECTORY_SEPARATOR.$logoFileName;
308 309  
309   - // Adding the Image Crop Widget
  310 + // Adding the Image Widget
310 311 $widgets[] = $oWF->get('ktcore.widgets.image', array(
311 312 'label' => _kt('Logo Preview'),
312 313 'name' => $logoFileName, // title and alt attributes get set to this.
313 314 'value' => $logoFileName,
  315 + 'width' => $this->maxLogoWidth,
  316 + 'height' => $this->maxLogoHeight,
  317 + 'widgetwidth' => 20
314 318 ));
315 319  
316 320 // Adding the Hidden FileName Input String
... ... @@ -366,6 +370,21 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
366 370  
367 371 //Changing to logo.jpg (Need to preserve extention as GD requires the exact image type to work)
368 372 $ext = end(explode('.', $logoFileName));
  373 +
  374 + $type = $_FILES['_kt_attempt_unique_file']['type'];
  375 +
  376 + //Stage 1 filename based ext check:
  377 + if (!$this->isSupportedExtension($ext)) {
  378 + //If filename based extension isn't supported will try and guess based on mime type
  379 + $default->log->error("Stage 1: Unsupported file type: '".$type."' for file: ':".$_FILES['_kt_attempt_unique_file']['name']."'");
  380 + $ext = $this->getExtension($type);
  381 +
  382 + if (!$this->isSupportedExtension($ext)) {
  383 + $default->log->error("Unsupported file type: '".$type."' for file: ':".$_FILES['_kt_attempt_unique_file']['name']."'");
  384 + $this->errorRedirectToMain("The file you tried to upload is not supported.");
  385 + }
  386 + }
  387 +
369 388 $logoFileName = 'logo_tmp_'.md5(Date('ymd-hms')).'.'.$ext; //Fighting the browser cache here
370 389 $logoFile = $logoDir.DIRECTORY_SEPARATOR.$logoFileName;
371 390  
... ... @@ -373,7 +392,7 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
373 392 if (file_exists($logoFile)) {
374 393 @unlink($logoFile);
375 394 }
376   -
  395 +
377 396 //TODO: Test Upload Failure by setting the $logoFile to ''
378 397  
379 398 if(!move_uploaded_file($_FILES['_kt_attempt_unique_file']['tmp_name'], $logoFile)) {
... ... @@ -388,8 +407,14 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
388 407  
389 408 switch ($resizeMethod) {
390 409 case 'crop':
391   - $cropLogoForm = $this->getCropLogoForm($logoFileName);
392   - return $cropLogoForm->render();
  410 + if ($this->isImageCroppable($logoFile, $this->maxLogoWith, $this->maxLogoHeight)) {
  411 + $retForm = $this->getCropLogoForm($logoFileName);
  412 + } else {
  413 + $_SESSION['KTErrorMessage'][] = _kt("The image was too small to be cropped.");
  414 + $retForm = $this->getApplyLogoForm($logoFileName);
  415 + }
  416 +
  417 + return $retForm->render();
393 418  
394 419 case 'scale':
395 420 $type = $_FILES['_kt_attempt_unique_file']['type'];
... ... @@ -421,6 +446,82 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
421 446 }
422 447  
423 448  
  449 + /**
  450 + * Returns the MIME of the filename, deducted from its extension
  451 + * If the extension is unknown, returns "image/jpeg"
  452 + */
  453 + function getMime($filename)
  454 + {
  455 + $pos = strrpos($filename, '.');
  456 + $extension = "";
  457 + if ($pos !== false) {
  458 + $extension = strtolower(substr($filename, $pos+1));
  459 + }
  460 +
  461 + switch($extension) {
  462 + case 'gif':
  463 + return 'image/gif';
  464 + case 'jfif':
  465 + return 'image/jpeg';
  466 + case 'jfif-tbnl':
  467 + return 'image/jpeg';
  468 + case 'png':
  469 + return 'image/png';
  470 + case 'jpe':
  471 + return 'image/jpeg';
  472 + case 'jpeg':
  473 + return 'image/jpeg';
  474 + case 'jpg':
  475 + return 'image/jpeg';
  476 + default:
  477 + return 'image/jpeg';
  478 + }
  479 + }
  480 +
  481 +
  482 + /**
  483 + * Returns the MIME of the filename, deducted from its extension
  484 + * If the extension is unknown, returns "image/jpeg"
  485 + */
  486 + function getExtension($type)
  487 + {
  488 +
  489 + switch($type) {
  490 + case 'image/gif':
  491 + return 'gif';
  492 + case 'image/jpeg':
  493 + return 'jfif';
  494 + case 'image/jpeg':
  495 + return 'jfif-tbnl';
  496 + case 'image/png':
  497 + return 'png';
  498 + case 'image/jpeg':
  499 + return 'jpe';
  500 + case 'image/jpeg':
  501 + return 'jpeg';
  502 + case 'image/jpeg':
  503 + return 'jpg';
  504 + case 'image/pjpeg':
  505 + return 'jpg';
  506 + default:
  507 + return 'image/jpeg';
  508 + }
  509 + }
  510 +
  511 +
  512 + /**
  513 + * Returns TRUE of the extension is supported
  514 + */
  515 + function isSupportedExtension($extension)
  516 + {
  517 + if (in_array($extension, $this->supportedTypes)) {
  518 + return TRUE;
  519 + }
  520 +
  521 + return FALSE;
  522 + }
  523 +
  524 +
424 525 /*
425 526 * This method uses the GD library to scale an image.
426 527 * - Supported images are jpeg, png and gif
... ... @@ -569,39 +670,60 @@ class ManageBrandDispatcher extends KTAdminDispatcher {
569 670  
570 671 }
571 672  
572   -
573   - /**
574   - * Returns the MIME of the filename, deducted from its extension
575   - * If the extension is unknown, returns "image/jpeg"
  673 + /*
  674 + * This method is used to determine if the image actually can be cropped
  675 + * - Supported images are jpeg, png and gif
  676 + *
576 677 */
577   - function getMime($filename)
578   - {
579   - $pos = strrpos($filename, '.');
580   - $extension = "";
581   - if ($pos !== false) {
582   - $extension = strtolower(substr($filename, $pos+1));
  678 + public function isImageCroppable( $origFile, $width, $height) {
  679 + global $default;
  680 +
  681 + //Requires the GD library if not exit gracefully
  682 + if (!extension_loaded('gd')) {
  683 + $default->log->error("The GD library isn't loaded");
  684 + return false;
  685 + }
  686 +
  687 + switch($type) {
  688 + case 'image/jpeg':
  689 + $orig = imagecreatefromjpeg($origFile);
  690 + break;
  691 + case 'image/pjpeg':
  692 + $orig = imagecreatefromjpeg($origFile);
  693 + break;
  694 + case 'image/png':
  695 + $orig = imagecreatefrompng($origFile);
  696 + break;
  697 + case 'image/gif':
  698 + $orig = imagecreatefromgif($origFile);
  699 + break;
  700 + default:
  701 + //Handle Error
  702 + $default->log->error("Tried to determine crop for an unsupported file type: $type");
  703 + return false;
583 704 }
584 705  
585   - switch($extension) {
586   - case 'gif':
587   - return 'image/gif';
588   - case 'jfif':
589   - return 'image/jpeg';
590   - case 'jfif-tbnl':
591   - return 'image/jpeg';
592   - case 'png':
593   - return 'image/png';
594   - case 'jpe':
595   - return 'image/jpeg';
596   - case 'jpeg':
597   - return 'image/jpeg';
598   - case 'jpg':
599   - return 'image/jpeg';
600   - default:
601   - return 'image/jpeg';
  706 + if($orig) {
  707 + /*
  708 + * calculate the size of the new image.
  709 + */
  710 + $orig_x = imagesx($orig);
  711 + $orig_y = imagesy($orig);
  712 +
  713 + if (($orig_x > $width) || ($orig_y > $height)) {
  714 + return true;
  715 + }
  716 +
  717 + } else {
  718 + //Handle Error
  719 + $default->log->error("Couldn't obtain a valid GD resource $origFile");
  720 + return false;
602 721 }
  722 +
  723 + return true;
603 724 }
604 725  
  726 +
605 727 /*
606 728 * This method uses the GD library to crop an image.
607 729 * - Supported images are jpeg, png and gif
... ...