/** * Fetches a language package from the remote server * * @param string $languageCode * * @return array */ public function fetchPackage($languageCode) { // Check if we have a cache file, generate it if not if (!is_readable($this->cacheFile)) { $this->fetchLanguages(); } $cacheData = json_decode(file_get_contents($this->cacheFile), true); // Make sure the language actually exists if (!isset($cacheData['languages'][$languageCode])) { return array('error' => true, 'message' => 'mautic.core.language.helper.invalid.language'); } // GET the update data try { $data = $this->connector->get('https://updates.mautic.org/index.php?option=com_mauticdownload&task=downloadLanguagePackage&langCode=' . $languageCode); } catch (\Exception $exception) { $logger = $this->factory->getLogger(); $logger->addError('An error occurred while attempting to fetch the package: ' . $exception->getMessage()); return array('error' => true, 'message' => 'mautic.core.language.helper.error.fetching.package'); } if ($data->code != 200) { return array('error' => true, 'message' => 'mautic.core.language.helper.error.fetching.package'); } // Set the filesystem target $target = $this->factory->getSystemPath('cache') . '/' . $languageCode . '.zip'; // Write the response to the filesystem file_put_contents($target, $data->body); // Return an array for the sake of consistency return array('error' => false); }
/** * Handle bounces & complaints from Amazon. * * @param Request $request * @param MauticFactory $factory * * @return mixed */ public function handleCallbackResponse(Request $request, MauticFactory $factory) { $translator = $factory->getTranslator(); $logger = $factory->getLogger(); $logger->debug('Receiving webhook from Amazon'); $payload = json_decode($request->getContent(), true); return $this->processJsonPayload($payload, $logger, $translator); }
/** * @param \Exception $e */ public function logIntegrationError(\Exception $e) { $logger = $this->factory->getLogger(); if ('dev' == MAUTIC_ENV) { $logger->addError('INTEGRATION ERROR: ' . $this->getName() . ' - ' . $e); } else { $logger->addError('INTEGRATION ERROR: ' . $this->getName() . ' - ' . $e->getMessage()); } }
/** * Retrieves the update data from our home server * * @param bool $overrideCache * * @return array */ public function fetchData($overrideCache = false) { $cacheFile = $this->factory->getSystemPath('cache') . '/lastUpdateCheck.txt'; // Check if we have a cache file and try to return cached data if so if (!$overrideCache && is_readable($cacheFile)) { $update = (array) json_decode(file_get_contents($cacheFile)); // Check if the user has changed the update channel, if so the cache is invalidated if ($update['stability'] == $this->factory->getParameter('update_stability')) { // If we're within the cache time, return the cached data if ($update['checkedTime'] > strtotime('-3 hours')) { return $update; } } } // Before processing the update data, send up our metrics try { // Generate a unique instance ID for the site $instanceId = hash('sha1', $this->factory->getParameter('secret_key') . 'Mautic' . $this->factory->getParameter('db_driver')); $data = array('application' => 'Mautic', 'version' => $this->factory->getVersion(), 'phpVersion' => PHP_VERSION, 'dbDriver' => $this->factory->getParameter('db_driver'), 'serverOs' => php_uname('s') . ' ' . php_uname('r'), 'instanceId' => $instanceId, 'installSource' => $this->factory->getParameter('install_source', 'Mautic')); $this->connector->post('https://updates.mautic.org/stats/send', $data, array(), 10); } catch (\Exception $exception) { // Not so concerned about failures here, move along } // Get the update data try { $appData = array('appVersion' => $this->factory->getVersion(), 'phpVersion' => PHP_VERSION, 'stability' => $this->factory->getParameter('update_stability')); $data = $this->connector->post('https://updates.mautic.org/index.php?option=com_mauticdownload&task=checkUpdates', $appData, array(), 10); $update = json_decode($data->body); } catch (\Exception $exception) { // Log the error $logger = $this->factory->getLogger(); $logger->addError('An error occurred while attempting to fetch updates: ' . $exception->getMessage()); return array('error' => true, 'message' => 'mautic.core.updater.error.fetching.updates'); } if ($data->code != 200) { // Log the error $logger = $this->factory->getLogger(); $logger->addError(sprintf('An unexpected %1$s code was returned while attempting to fetch updates. The message received was: %2$s', $data->code, is_string($data->body) ? $data->body : implode('; ', $data->body))); return array('error' => true, 'message' => 'mautic.core.updater.error.fetching.updates'); } // If the user's up-to-date, go no further if ($update->latest_version) { return array('error' => false, 'message' => 'mautic.core.updater.running.latest.version'); } // Last sanity check, if the $update->version is older than our current version if (version_compare($this->factory->getVersion(), $update->version, 'ge')) { return array('error' => false, 'message' => 'mautic.core.updater.running.latest.version'); } // The user is able to update to the latest version, cache the data first $data = array('error' => false, 'message' => 'mautic.core.updater.update.available', 'version' => $update->version, 'announcement' => $update->announcement, 'package' => $update->package, 'checkedTime' => time(), 'stability' => $this->factory->getParameter('update_stability')); file_put_contents($cacheFile, json_encode($data)); return $data; }
/** * Log exception * * @param \Exception|string $error */ protected function logError($error) { if ($error instanceof \Exception) { $error = $error->getMessage(); $this->fatal = true; } $logDump = $this->logger->dump(); if (!empty($logDump) && strpos($error, $logDump) === false) { $error .= " Log data: {$logDump}"; } $this->errors[] = $error; $this->logger->clear(); $this->factory->getLogger()->log('error', '[MAIL ERROR] ' . $error); }
/** * Log exception * * @param \Exception|string $error */ private function logError($error) { if ($error instanceof \Exception) { $error = $error->getMessage(); $this->fatal = true; } $this->errors[] = $error; $logDump = $this->logger->dump(); if (!empty($logDump)) { $error .= "; {$logDump}"; $this->logger->clear(); } $this->factory->getLogger()->log('error', '[MAIL ERROR] ' . $error); }
/** * @param array $config * @param Lead $lead * @param MauticFactory $factory * * @return array */ public static function send(array $config, Lead $lead, MauticFactory $factory) { /** @var \Mautic\LeadBundle\Model\LeadModel $leadModel */ $leadModel = $factory->getModel('lead.lead'); $logger = $factory->getLogger(); if ($leadModel->isContactable($lead, 'notification') !== DoNotContact::IS_CONTACTABLE) { $logger->error('Error: Lead ' . $lead->getId() . ' is not contactable on the web push channel.'); return array('failed' => 1); } // If lead has subscribed on multiple devices, get all of them. /** @var \Mautic\NotificationBundle\Entity\PushID[] $pushIDs */ $pushIDs = $lead->getPushIDs(); $playerID = array(); foreach ($pushIDs as $pushID) { $playerID[] = $pushID->getPushID(); } if (empty($playerID)) { $logger->error('Error: Lead ' . $lead->getId() . ' has not subscribed to web push channel.'); return array('failed' => 1); } /** @var \Mautic\NotificationBundle\Api\AbstractNotificationApi $notification */ $notificationApi = $factory->getKernel()->getContainer()->get('mautic.notification.api'); /** @var \Mautic\NotificationBundle\Model\NotificationModel $notificationModel */ $notificationModel = $factory->getModel('notification'); $notificationId = (int) $config['notification']; /** @var \Mautic\NotificationBundle\Entity\Notification $notification */ $notification = $notificationModel->getEntity($notificationId); if ($notification->getId() !== $notificationId) { $logger->error('Error: The requested notification cannot be found.'); return array('failed' => 1); } $url = $notificationApi->convertToTrackedUrl($notification->getUrl(), array('notification' => $notification->getId(), 'lead' => $lead->getId())); $response = $notificationApi->sendNotification($playerID, $notification->getMessage(), $notification->getHeading(), $url); // If for some reason the call failed, tell mautic to try again by return false if ($response->code !== 200) { $logger->error('Error: The notification failed to send and returned a ' . $response->code . ' HTTP response with a body of: ' . $response->body); return false; } return array('status' => 'mautic.notification.timeline.status.delivered', 'type' => 'mautic.notification.notification', 'id' => $notification->getId(), 'name' => $notification->getName(), 'heading' => $notification->getHeading(), 'content' => $notification->getMessage()); }
/** * @param MauticFactory $factory */ public function __construct(MauticFactory $factory) { $this->factory = $factory; $this->db = $factory->getDatabase(); $this->logger = $this->factory->getLogger(); }