diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 7b408cd10f27..15236634a71a 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -1019,13 +1019,9 @@ class AccountController extends BaseController /* Logo image file */ if ($uploaded = Input::file('logo')) { $path = Input::file('logo')->getRealPath(); - $disk = $account->getLogoDisk(); - if ($account->hasLogo() && ! Utils::isNinjaProd()) { - $disk->delete($account->logo); - } - $extension = strtolower($uploaded->getClientOriginalExtension()); + if (empty(Document::$types[$extension]) && ! empty(Document::$extraExtensions[$extension])) { $documentType = Document::$extraExtensions[$extension]; } else { @@ -1041,7 +1037,7 @@ class AccountController extends BaseController $size = filesize($filePath); if ($size / 1000 > MAX_DOCUMENT_SIZE) { - Session::flash('warning', trans('texts.logo_warning_too_large')); + Session::flash('error', trans('texts.logo_warning_too_large')); } else { if ($documentType != 'gif') { $account->logo = $account->account_key.'.'.$documentType; @@ -1058,15 +1054,20 @@ class AccountController extends BaseController $image->interlace(false); $imageStr = (string) $image->encode($documentType); $disk->put($account->logo, $imageStr); - $account->logo_size = strlen($imageStr); } else { - $stream = fopen($filePath, 'r'); - $disk->getDriver()->putStream($account->logo, $stream, ['mimetype' => $documentTypeData['mime']]); - fclose($stream); + if (Utils::isInterlaced($filePath)) { + $account->clearLogo(); + Session::flash('error', trans('texts.logo_warning_invalid')); + } else { + $stream = fopen($filePath, 'r'); + $disk->getDriver()->putStream($account->logo, $stream, ['mimetype' => $documentTypeData['mime']]); + fclose($stream); + } } } catch (Exception $exception) { - Session::flash('warning', trans('texts.logo_warning_invalid')); + $account->clearLogo(); + Session::flash('error', trans('texts.logo_warning_invalid')); } } else { if (extension_loaded('fileinfo')) { @@ -1080,7 +1081,7 @@ class AccountController extends BaseController $account->logo_width = $image->width(); $account->logo_height = $image->height(); } else { - Session::flash('warning', trans('texts.logo_warning_fileinfo')); + Session::flash('error', trans('texts.logo_warning_fileinfo')); } } } diff --git a/app/Libraries/Utils.php b/app/Libraries/Utils.php index 842620b056e0..bc77e248af9b 100644 --- a/app/Libraries/Utils.php +++ b/app/Libraries/Utils.php @@ -1240,4 +1240,13 @@ class Utils { return strlen($string) > $length ? rtrim(substr($string, 0, $length)) . '...' : $string; } + + // http://stackoverflow.com/a/14238078/497368 + public static function isInterlaced($filename) + { + $handle = fopen($filename, 'r'); + $contents = fread($handle, 32); + fclose($handle); + return( ord($contents[28]) != 0 ); + } } diff --git a/app/Models/Account.php b/app/Models/Account.php index 0d99d465cec3..42e427a23936 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -7,13 +7,13 @@ use App\Events\UserSettingsChanged; use App\Models\Traits\GeneratesNumbers; use App\Models\Traits\PresentsInvoice; use App\Models\Traits\SendsEmails; +use App\Models\Traits\HasLogo; use Cache; use Carbon; use DateTime; use Eloquent; use Event; use Illuminate\Database\Eloquent\SoftDeletes; -use Illuminate\Support\Facades\Storage; use Laracasts\Presenter\PresentableTrait; use Session; use Utils; @@ -28,6 +28,7 @@ class Account extends Eloquent use PresentsInvoice; use GeneratesNumbers; use SendsEmails; + use HasLogo; /** * @var string @@ -836,101 +837,6 @@ class Account extends Eloquent return false; } - /** - * @return bool - */ - public function hasLogo() - { - return ! empty($this->logo); - } - - /** - * @return mixed - */ - public function getLogoDisk() - { - return Storage::disk(env('LOGO_FILESYSTEM', 'logos')); - } - - protected function calculateLogoDetails() - { - $disk = $this->getLogoDisk(); - - if ($disk->exists($this->account_key.'.png')) { - $this->logo = $this->account_key.'.png'; - } elseif ($disk->exists($this->account_key.'.jpg')) { - $this->logo = $this->account_key.'.jpg'; - } - - if (! empty($this->logo)) { - $image = imagecreatefromstring($disk->get($this->logo)); - $this->logo_width = imagesx($image); - $this->logo_height = imagesy($image); - $this->logo_size = $disk->size($this->logo); - } else { - $this->logo = null; - } - $this->save(); - } - - /** - * @return null - */ - public function getLogoRaw() - { - if (! $this->hasLogo()) { - return null; - } - - $disk = $this->getLogoDisk(); - - return $disk->get($this->logo); - } - - /** - * @param bool $cachebuster - * - * @return null|string - */ - public function getLogoURL($cachebuster = false) - { - if (! $this->hasLogo()) { - return null; - } - - $disk = $this->getLogoDisk(); - $adapter = $disk->getAdapter(); - - if ($adapter instanceof \League\Flysystem\Adapter\Local) { - // Stored locally - $logoUrl = url('/logo/' . $this->logo); - - if ($cachebuster) { - $logoUrl .= '?no_cache='.time(); - } - - return $logoUrl; - } - - return Document::getDirectFileUrl($this->logo, $this->getLogoDisk()); - } - - public function getLogoPath() - { - if (! $this->hasLogo()) { - return null; - } - - $disk = $this->getLogoDisk(); - $adapter = $disk->getAdapter(); - - if ($adapter instanceof \League\Flysystem\Adapter\Local) { - return $adapter->applyPathPrefix($this->logo); - } else { - return Document::getDirectFileUrl($this->logo, $this->getLogoDisk()); - } - } - /** * @return mixed */ @@ -958,30 +864,6 @@ class Account extends Eloquent return null; } - /** - * @return mixed|null - */ - public function getLogoWidth() - { - if (! $this->hasLogo()) { - return null; - } - - return $this->logo_width; - } - - /** - * @return mixed|null - */ - public function getLogoHeight() - { - if (! $this->hasLogo()) { - return null; - } - - return $this->logo_height; - } - /** * @param $entityType * @param null $clientId @@ -1348,26 +1230,6 @@ class Account extends Eloquent return Carbon::instance($date); } - /** - * @return float|null - */ - public function getLogoSize() - { - if (! $this->hasLogo()) { - return null; - } - - return round($this->logo_size / 1000); - } - - /** - * @return bool - */ - public function isLogoTooLarge() - { - return $this->getLogoSize() > MAX_LOGO_FILE_SIZE; - } - /** * @param $eventId *