public function getRemoteFileInfo($url = null) { $client = new \GuzzleHttp\Client(); $response = $client->head($url); $result['status'] = $response->getStatusCode(); if ($response->getStatusCode() == 200) { $result['size'] = $this->guessResponseLength($response); $result['type'] = $this->guessResponseType($response, $url); } return $result; }
/** * Returns the file size raw or formatted to largest increment possible * * @param bool $raw * @param bool $thumbnail * @return string|int */ public function getFileSize( $raw = false, $thumbnail = false ) { if ( Application::Cms()->getClientId() ) { $thumbnail = false; } static $cache = array(); $id = $this->getFilePath( $thumbnail ); if ( ! isset( $cache[$id] ) ) { $fileSize = 0; if ( $this->checkExists( $thumbnail ) ) { $domain = $this->getLinkDomain(); if ( $domain ) { if ( ! in_array( $domain, array( 'youtube', 'youtu' ) ) ) { try { $request = new GuzzleHttp\Client(); $header = $request->head( $id ); if ( ( $header !== false ) && ( $header->getStatusCode() == 200 ) ) { $fileSize = (int) $header->getHeader( 'Content-Length' ); } } catch( Exception $e ) {} } } else { $fileSize = @filesize( $id ); } } $cache[$id] = $fileSize; } if ( ! $raw ) { return cbgalleryClass::getFormattedFileSize( $cache[$id] ); } return $cache[$id]; }
/** * Validator: * Validates $value for $field->required and other rules * Override * * @param FieldTable $field * @param UserTable $user RETURNED populated: touch only variables related to saving this field (also when not validating for showing re-edit) * @param string $columnName Column to validate * @param string $value (RETURNED:) Value to validate, Returned Modified if needed ! * @param array $postdata Typically $_POST (but not necessarily), filtering required. * @param string $reason 'edit' for save user edit, 'register' for save registration * @return boolean True if validate, $this->_setErrorMSG if False */ public function validate(&$field, &$user, $columnName, &$value, &$postdata, $reason) { $isRequired = $this->_isRequired($field, $user, $reason); $col = $field->get('name'); $colChoice = $col . '__choice'; $choice = stripslashes(cbGetParam($postdata, $colChoice)); switch ($choice) { case 'upload': if (!$field->params->get('audio_allow_uploads', 1)) { $this->_setValidationError($field, $user, $reason, CBTxt::T('UE_NOT_AUTHORIZED', 'You are not authorized to view this page!')); return false; } elseif (!isset($value['tmp_name']) || empty($value['tmp_name']) || $value['error'] != 0 || !is_uploaded_file($value['tmp_name'])) { if ($isRequired) { $this->_setValidationError($field, $user, $reason, CBTxt::T('Please select a audio file before uploading')); } return false; } else { $uploadSizeLimitMax = $field->params->get('fieldValidateAudio_sizeMax', 1024); $uploadSizeLimitMin = $field->params->get('fieldValidateAudio_sizeMin', 0); $uploadExtensionLimit = $this->allowedExtensions(); $uploadedExt = strtolower(preg_replace('/[^-a-zA-Z0-9_]/', '', pathinfo($value['name'], PATHINFO_EXTENSION))); if (!$uploadedExt || !in_array($uploadedExt, $uploadExtensionLimit)) { $this->_setValidationError($field, $user, $reason, sprintf(CBTxt::T('Please upload only %s'), implode(', ', $uploadExtensionLimit))); return false; } $uploadedSize = $value['size']; if ($uploadedSize / 1024 > $uploadSizeLimitMax) { $this->_setValidationError($field, $user, $reason, sprintf(CBTxt::T('The audio file size exceeds the maximum of %s'), $this->formattedFileSize($uploadSizeLimitMax * 1024))); return false; } if ($uploadedSize / 1024 < $uploadSizeLimitMin) { $this->_setValidationError($field, $user, $reason, sprintf(CBTxt::T('The audio file is too small, the minimum is %s'), $this->formattedFileSize($uploadSizeLimitMin * 1024))); return false; } } break; case 'link': if (!$field->params->get('audio_allow_links', 1)) { $this->_setValidationError($field, $user, $reason, CBTxt::T('UE_NOT_AUTHORIZED', 'You are not authorized to view this page!')); return false; } $validated = parent::validate($field, $user, $columnName, $value, $postdata, $reason); if ($validated && $value !== '' && $value !== null) { $linkExists = false; try { $request = new \GuzzleHttp\Client(); $header = $request->head($value); if ($header !== false && $header->getStatusCode() == 200) { $linkExists = true; } } catch (Exception $e) { } if (!$linkExists) { $this->_setValidationError($field, $user, $reason, CBTxt::T('Please input a audio file url before linking')); return false; } $linkExtLimit = $this->allowedExtensions(); $linkExt = strtolower(pathinfo($value, PATHINFO_EXTENSION)); if (!$linkExt || !in_array($linkExt, $linkExtLimit)) { $this->_setValidationError($field, $user, $reason, sprintf(CBTxt::T('Please link only %s'), implode(', ', $linkExtLimit))); return false; } } return $validated; break; default: $valCol = $field->get('name'); if ($isRequired && (!$user || !isset($user->{$valCol}) || !$user->get($valCol))) { if (!$value) { $this->_setValidationError($field, $user, $reason, CBTxt::T('UE_FIELDREQUIRED', 'This Field is required')); return false; } } break; } return true; }
/** * Get the file type based on the file url * * @param string $file_url * @return string */ protected function getFileType($file_url) { $client = new \GuzzleHttp\Client(); $file_info = $client->head($file_url); return $file_info->getHeader('content-type'); }