lekoala /
silverstripe-mailgun
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Mailgun; |
||
| 4 | |||
| 5 | use \Exception; |
||
| 6 | use Mailgun\Mailgun; |
||
| 7 | use SilverStripe\Control\Director; |
||
| 8 | use SilverStripe\Core\Environment; |
||
| 9 | use SilverStripe\Control\Email\Email; |
||
| 10 | use SilverStripe\Control\Email\Mailer; |
||
| 11 | use SilverStripe\SiteConfig\SiteConfig; |
||
| 12 | use SilverStripe\Core\Injector\Injector; |
||
| 13 | use LeKoala\Mailgun\MailgunSwiftTransport; |
||
| 14 | use SilverStripe\Core\Config\Configurable; |
||
| 15 | use SilverStripe\Control\Email\SwiftMailer; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * This configurable class helps decoupling the api client from SilverStripe |
||
| 19 | */ |
||
| 20 | class MailgunHelper |
||
| 21 | { |
||
| 22 | use Configurable; |
||
| 23 | |||
| 24 | const DEFAULT_ENDPOINT = 'https://api.mailgun.net/v3'; |
||
| 25 | const EU_ENDPOINT = 'https://api.eu.mailgun.net/v3'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Client instance |
||
| 29 | * |
||
| 30 | * @var Mailgun |
||
| 31 | */ |
||
| 32 | protected static $client; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Get the mailer instance |
||
| 36 | * |
||
| 37 | * @return SilverStripe\Control\Email\SwiftMailer |
||
| 38 | */ |
||
| 39 | public static function getMailer() |
||
| 40 | { |
||
| 41 | return Injector::inst()->get(Mailer::class); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Get the api client instance |
||
| 46 | * @return Mailgun |
||
| 47 | * @throws Exception |
||
| 48 | */ |
||
| 49 | public static function getClient() |
||
| 50 | { |
||
| 51 | if (!self::$client) { |
||
| 52 | $key = self::config()->api_key; |
||
| 53 | if (empty($key)) { |
||
| 54 | throw new \Exception("api_key is not configured for " . __class__); |
||
| 55 | } |
||
| 56 | $endpoint = self::DEFAULT_ENDPOINT; |
||
| 57 | if (self::config()->endpoint) { |
||
| 58 | $endpoint = self::config()->endpoint; |
||
| 59 | } |
||
| 60 | self::$client = Mailgun::create($key, $endpoint); |
||
|
0 ignored issues
–
show
|
|||
| 61 | } |
||
| 62 | return self::$client; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get the log folder and create it if necessary |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public static function getLogFolder() |
||
| 71 | { |
||
| 72 | $logFolder = BASE_PATH . '/' . self::config()->log_folder; |
||
| 73 | if (!is_dir($logFolder)) { |
||
| 74 | mkdir($logFolder, 0755, true); |
||
| 75 | } |
||
| 76 | return $logFolder; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | public static function getDomain() |
||
| 83 | { |
||
| 84 | if ($domain = self::config()->domain) { |
||
| 85 | return $domain; |
||
| 86 | } |
||
| 87 | if ($domain = Environment::getEnv('MAILGUN_DOMAIN')) { |
||
| 88 | return $domain; |
||
| 89 | } |
||
| 90 | throw new Exception("MAILGUN_DOMAIN not set"); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Process environment variable to configure this module |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | public static function init() |
||
| 99 | { |
||
| 100 | // Regular api key used for sending emails |
||
| 101 | $api_key = Environment::getEnv('MAILGUN_API_KEY'); |
||
| 102 | if ($api_key) { |
||
| 103 | self::config()->api_key = $api_key; |
||
| 104 | } |
||
| 105 | |||
| 106 | $domain = Environment::getEnv('MAILGUN_DOMAIN'); |
||
| 107 | if ($domain) { |
||
| 108 | self::config()->domain = $domain; |
||
| 109 | } |
||
| 110 | |||
| 111 | // Set a custom endpoint |
||
| 112 | $endpoint = Environment::getEnv('MAILGUN_ENDPOINT'); |
||
| 113 | if ($endpoint) { |
||
| 114 | self::config()->endpoint = $endpoint; |
||
| 115 | } |
||
| 116 | |||
| 117 | // Disable sending |
||
| 118 | $sending_disabled = Environment::getEnv('MAILGUN_SENDING_DISABLED'); |
||
| 119 | if ($sending_disabled) { |
||
| 120 | self::config()->disable_sending = $sending_disabled; |
||
| 121 | } |
||
| 122 | |||
| 123 | // Log all outgoing emails (useful for testing) |
||
| 124 | $enable_logging = Environment::getEnv('MAILGUN_ENABLE_LOGGING'); |
||
| 125 | if ($enable_logging) { |
||
| 126 | self::config()->enable_logging = $enable_logging; |
||
| 127 | } |
||
| 128 | |||
| 129 | // We have a key, we can register the transport |
||
| 130 | if (self::config()->api_key) { |
||
| 131 | self::registerTransport(); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Register the transport with the client |
||
| 137 | * |
||
| 138 | * @return SilverStripe\Control\Email\SwiftMailer The updated swift mailer |
||
| 139 | * @throws Exception |
||
| 140 | */ |
||
| 141 | public static function registerTransport() |
||
| 142 | { |
||
| 143 | $client = self::getClient(); |
||
| 144 | $mailer = self::getMailer(); |
||
| 145 | if (!$mailer instanceof SwiftMailer) { |
||
| 146 | throw new Exception("Mailer must be an instance of " . SwiftMailer::class . " instead of " . get_class($mailer)); |
||
| 147 | } |
||
| 148 | $transport = new MailgunSwiftTransport($client); |
||
| 149 | $newSwiftMailer = $mailer->getSwiftMailer()->newInstance($transport); |
||
| 150 | $mailer->setSwiftMailer($newSwiftMailer); |
||
| 151 | return $mailer; |
||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Resolve default send from address |
||
| 157 | * |
||
| 158 | * Keep in mind that an email using send() without a from |
||
| 159 | * will inject the admin_email. Therefore, SiteConfig |
||
| 160 | * will not be used |
||
| 161 | * |
||
| 162 | * @param string $from |
||
| 163 | * @param bool $createDefault |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public static function resolveDefaultFromEmail($from = null, $createDefault = true) |
||
| 167 | { |
||
| 168 | $original_from = $from; |
||
| 169 | View Code Duplication | if (!empty($from)) { |
|
| 170 | // If we have a sender, validate its email |
||
| 171 | $from = EmailUtils::get_email_from_rfc_email($from); |
||
| 172 | if (filter_var($from, FILTER_VALIDATE_EMAIL)) { |
||
| 173 | return $original_from; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | // Look in siteconfig for default sender |
||
| 177 | $config = SiteConfig::current_site_config(); |
||
| 178 | $config_field = self::config()->siteconfig_from; |
||
| 179 | if ($config_field && !empty($config->$config_field)) { |
||
| 180 | return $config->$config_field; |
||
| 181 | } |
||
| 182 | // Use admin email |
||
| 183 | if ($admin = Email::config()->admin_email) { |
||
| 184 | return $admin; |
||
| 185 | } |
||
| 186 | // If we still don't have anything, create something based on the domain |
||
| 187 | if ($createDefault) { |
||
| 188 | return self::createDefaultEmail(); |
||
| 189 | } |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Resolve default send to address |
||
| 195 | * |
||
| 196 | * @param string $to |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public static function resolveDefaultToEmail($to = null) |
||
| 200 | { |
||
| 201 | // In case of multiple recipients, do not validate anything |
||
| 202 | if (is_array($to) || strpos($to, ',') !== false) { |
||
| 203 | return $to; |
||
| 204 | } |
||
| 205 | $original_to = $to; |
||
| 206 | View Code Duplication | if (!empty($to)) { |
|
| 207 | $to = EmailUtils::get_email_from_rfc_email($to); |
||
| 208 | if (filter_var($to, FILTER_VALIDATE_EMAIL)) { |
||
| 209 | return $original_to; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | $config = SiteConfig::current_site_config(); |
||
| 213 | $config_field = self::config()->siteconfig_to; |
||
| 214 | if ($config_field && !empty($config->$config_field)) { |
||
| 215 | return $config->$config_field; |
||
| 216 | } |
||
| 217 | if ($admin = Email::config()->admin_email) { |
||
| 218 | return $admin; |
||
| 219 | } |
||
| 220 | return false; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Create a sensible default address based on domain name |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public static function createDefaultEmail() |
||
| 229 | { |
||
| 230 | $fulldom = Director::absoluteBaseURL(); |
||
| 231 | $host = parse_url($fulldom, PHP_URL_HOST); |
||
| 232 | if (!$host) { |
||
| 233 | $host = 'localhost'; |
||
| 234 | } |
||
| 235 | $dom = str_replace('www.', '', $host); |
||
| 236 | |||
| 237 | return 'postmaster@' . $dom; |
||
| 238 | } |
||
| 239 | } |
||
| 240 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..