function search($name) { $url = 'http://' . $this->host . '/search?name=' . urlencode($name) . '&lang=pl&maxrows=3' . ($this->username !== NULL ? '&username='******''); try { $download = new DownloadHelper($url); $data = $download->exec(); if (!$data) { $download->cacheFor(600); return FALSE; } libxml_use_internal_errors(); $data = simplexml_load_string($data); libxml_clear_errors(); if (!$data) { $download->cacheFor(600); return FALSE; } // Trzymaj w cache przez około 116 dni $download->cacheFor(10000000); if ((int) $data->totalResultsCount == 0 || $data->geoname->getName() != 'geoname' || $data->geoname[0]->getName() != 'geoname' || $data->geoname[0]->name->getName() != 'name' || $data->geoname[0]->countryName->getName() != 'countryName' || $data->geoname[0]->lat->getName() != 'lat' || $data->geoname[0]->lng->getName() != 'lng') { return NULL; } $data = (array) $data->geoname[0]; foreach ($data as &$value) { $value = trim($value); } unset($value); if (isset($data['countryName']) && $data['countryName'] == 'Rzeczpospolita Polska') { $data['countryName'] = 'Polska'; } return $data; } catch (Exception $e) { return FALSE; } }
function cache($url) { $down = new DownloadHelper($url); $dane = $down->exec(); libxml_use_internal_errors(TRUE); $dom = new DOMDocument(); if (!$dom->loadHTML($dane)) { libxml_use_internal_errors(FALSE); $down->cacheFor(1800); return FALSE; } $down->cacheUntil(strtotime('tomorrow midnight')); return $dom; }
/** * The main send of one letter to one or mode recipients. * The mail content generates for each user * * TODO: Need to refactor it all to: * sendNewsletterToSubscriber($nid, $sid, $options) * sentNewsletterToEmail($nid, array $emailAndName, $options) * sentLetterToEmail(array $letterData, array $emailAndName, $options) * * @param array $params newsletter_id, subscriber(object), type ('html'|'plain'), tracking(bool) * * @return object * @since 1.0 */ public function send($params = null) { // load letter to send.... if (empty($params['newsletter_id'])) { $msg = 'Newsletter id is absent. There is nothing to send.'; $this->setError($msg); throw new Exception($msg); } if (empty($params['subscriber'])) { $msg = 'Subscriber is absent. There is no one to send.'; $this->setError($msg); throw new Exception($msg); } if (empty($params['tracking'])) { $params['tracking'] = false; } // Load newsletter... $letter = JModel::getInstance('Newsletter', 'NewsletterModelEntity'); if (!$letter->load($params['newsletter_id'])) { $msg = 'Loading letter error or newsletter_id is not defined. Id:' . $params['newsletter_id']; $this->setError($msg); throw new Exception($msg); } // Load newsletter's SMTP profile... $smtpProfile = JModel::getInstance('Smtpprofile', 'NewsletterModelEntity'); if (!$smtpProfile->load($letter->smtp_profile_id)) { $msg = 'Cant load SMTP profile with id: ' . $letter->smtp_profile_id; $this->setError($msg); throw new Exception($msg); } // Load mailbox profile bound to loaded SMTP profile... $mailboxProfile = JModel::getInstance('Mailboxprofile', 'NewsletterModelEntity'); if (!$mailboxProfile->load($smtpProfile->mailbox_profile_id)) { LogHelper::addWarning('COM_NEWSLETTER_CANT_LOAD_MAILBOX_CANT_SET_SOME_HEADERS', LogHelper::CAT_MAILER, array('Mailbox profile id' => $smtpProfile->mailbox_profile_id, 'SMTP profile' => $smtpProfile->smtp_profile_name)); } // Now we have newsletter, subscriber, SMTP profile and, probably, Mailbox profile. // So we can start to send... // Use the phpMailer exceptions $sender = new MigurMailerSender(array('exceptions' => true)); $subscriber = $params['subscriber']; $type = MailHelper::filterType(!empty($params['type']) ? $params['type'] : null); if (!$type) { $msg = 'The type "' . $type . '" is not supported'; $this->setError($msg); throw new Exception($msg); } // emulate user environment SubscriberHelper::saveRealUser(); if (!SubscriberHelper::emulateUser(array('email' => $subscriber->email))) { $msg = 'The user "' . $subscriber->email . '" is absent'; $this->setError($msg); throw new Exception($msg); } PlaceholderHelper::setPlaceholder('newsletter id', $letter->newsletter_id); // render the content of letter for each user $letter->content = $this->render(array('type' => $type, 'newsletter_id' => $letter->newsletter_id, 'tracking' => true)); $letter->subject = $this->renderSubject($letter->subject); $letter->encoding = $letter->params->encoding; SubscriberHelper::restoreRealUser(); // Result object $res = new StdClass(); $res->state = false; $res->errors = array(); $res->content = $letter->content; if ($letter->content === false) { return $res; } // Add custom headers // Set the email to bounce if (!empty($mailboxProfile->username)) { $sender->AddCustomHeader('Return-Path:' . $mailboxProfile->username); $sender->AddCustomHeader('Return-Receipt-To:' . $mailboxProfile->username); $sender->AddCustomHeader('Errors-To:' . $mailboxProfile->username); } // Add info about newsleerter and subscriber $sender->AddCustomHeader(MailHelper::APPLICATION_HEADER); $sender->AddCustomHeader(MailHelper::EMAIL_NAME_HEADER . ':' . $letter->name); $sender->AddCustomHeader(MailHelper::NEWSLETTER_ID_HEADER . ':' . $params['newsletter_id']); $sender->AddCustomHeader(MailHelper::SUBSCRIBER_ID_HEADER . ':' . $subscriber->subscriber_id); // Get attachments $atts = DownloadHelper::getByNewsletterId($params['newsletter_id']); if (!$smtpProfile->isJoomlaProfile()) { $fromName = $smtpProfile->from_name; $fromEmail = $smtpProfile->from_email; $toName = $smtpProfile->reply_to_name; $toEmail = $smtpProfile->reply_to_email; } else { $jConfig = new JConfig(); $fromName = isset($letter->params->from_name) ? $letter->params->from_name : $jConfig->fromname; $fromEmail = isset($letter->params->from_email) ? $letter->params->from_email : $jConfig->mailfrom; $toName = isset($letter->params->to_name) ? $letter->params->to_name : $jConfig->fromname; $toEmail = isset($letter->params->to_email) ? $letter->params->to_email : $jConfig->mailfrom; } // Check if we dan determine all parameters... if (empty($fromName) || empty($fromEmail) || empty($toName) || empty($toEmail)) { LogHelper::addWarning('COM_NEWSLETTER_MAILER_CANT_DETERMINE SOME FROMTO', LogHelper::CAT_MAILER, array('From name' => $fromName, 'From email' => $fromEmail, 'Reply to name' => $toName, 'Reply to email' => $toEmail, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name)); } try { // send the unique letter to each recipient $sendRes = $sender->send(array('letter' => $letter->toObject(), 'attach' => $atts, 'emails' => array($subscriber), 'smtpProfile' => $smtpProfile->toObject(), 'fromName' => $fromName, 'fromEmail' => $fromEmail, 'toName' => $toName, 'toEmail' => $toEmail, 'type' => $type, 'tracking' => $params['tracking'])); // If sending failed if (!$sendRes && !empty($sender->ErrorInfo)) { throw new Exception($sender->ErrorInfo); } } catch (Exception $e) { $error = JError::getError('unset'); if (!empty($error)) { $msg = $error->get('message'); $this->setError($msg); $res->errors[] = $msg; } $res->errors[] = $e->getMessage(); LogHelper::addError('COM_NEWSLETTER_MAILER_SEND_ERROR', LogHelper::CAT_MAILER, array('Error' => $e->getMessage(), 'Email' => $subscriber->email, 'Mail type' => $type, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name)); return $res; } $res->state = true; return $res; }
/** * Displays the view. * * @param string $tpl the template name * * @return void * @since 1.0 */ public function display($tpl = null) { $isNew = !JRequest::getInt('newsletter_id', false); if ($isNew && !AclHelper::actionIsAllowed('newsletter.add') || !$isNew && !AclHelper::actionIsAllowed('newsletter.edit')) { $msg = $isNew ? 'JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED' : 'JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED'; JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_newsletter&view=newsletters', false), JText::_($msg), 'error'); return; } //TODO: Need to move css/js to SetDocument JHTML::stylesheet('media/com_newsletter/css/admin.css'); JHTML::stylesheet('media/com_newsletter/css/newsletter.css'); JHTML::script('media/com_newsletter/js/migur/js/core.js'); JHTML::script('media/com_newsletter/js/migur/js/ajax.js'); JHTML::script('media/com_newsletter/js/migur/js/widgets.js'); JHTML::script('media/com_newsletter/js/migur/js/moodialog/MooDialog.js'); JHTML::script('media/com_newsletter/js/migur/js/moodialog/MooDialog.Request.js'); JHTML::script('media/com_newsletter/js/migur/js/moodialog/MooDialog.IFrame.js'); JHTML::stylesheet('media/com_newsletter/js/migur/js/moodialog/css/MooDialog.css'); JHTML::script('media/com_newsletter/js/migur/js/autocompleter/Observer.js'); JHTML::script('media/com_newsletter/js/migur/js/autocompleter/Autocompleter.js'); JHTML::script('media/com_newsletter/js/migur/js/autocompleter/Autocompleter.Local.js'); JHTML::stylesheet('media/com_newsletter/js/migur/js/autocompleter/css/Autocompleter.css'); JHTML::script('media/com_newsletter/js/migur/js/guide.js'); JHTML::stylesheet('media/com_newsletter/css/guide.css'); //TODO: Bulk-code. Need to refactor. JavascriptHelper::addObject('comParams', JComponentHelper::getParams('com_newsletter')->toArray()); $nId = JRequest::getInt('newsletter_id'); $script = $this->get('Script'); $this->script = $script; // Get main form and data for newsletter $this->form = $this->get('Form', 'newsletter'); $this->newsletter = $this->get('Item'); $smtpModel = JModel::getInstance('SMtpProfile', 'NewsletterModelEntity'); // Let's add J! profile $smtpp = $smtpModel->loadJoomla(); JavascriptHelper::addObject('joomlaDe', JComponentHelper::getParams('com_newsletter')->toArray()); // get the SmtpProfiles data $smtpprofilesManager = JModel::getInstance('smtpprofiles', 'NewsletterModel'); $this->assignRef('smtpprofiles', $smtpprofilesManager->getAllItems('withDefault')); // get all the Extensions $this->modules = MigurModuleHelper::getSupported(array('withoutInfo' => true)); $this->plugins = MigurPluginHelper::getSupported(array('withoutInfo' => true), 'com_newsletter.newsletter'); // get the Extensions used in this newsletter $model = JModel::getInstance('newsletterext', 'NewsletterModel'); $this->usedExts = $model->getExtensionsBy($nId); // Get a list of all templates $this->setModel(JModel::getInstance('templates', 'NewsletterModel')); $model = $this->getModel('templates'); $templs = $model->getItems(); $path = JPATH_COMPONENT . '/extensions/templates/'; $filenames = JFolder::files($path, '^.*\\.xml$'); if ($filenames === false) { JError::raiseError(500, implode("\n", array("Path {$path} not found"))); } $this->templates = (object) array('items' => array(), 'path' => $path); $this->htmlTemplateId = null; foreach ($templs as $item) { $xml = new JSimpleXML(); $xml->loadFile($path . $item->template); $str = trim($xml->document->template[0]->_data); $str = preg_replace('/<style.*>.*<\\/style>/s', '', $str); $str = str_replace('<position', '<div class="drop container-draggables"', $str); $id = strtolower(str_replace('.', '-', $item->template) . '-' . $item->t_style_id); $item->id = $id; $item->filename = $item->template; $item->template = $str; $this->templates->items[] = $item; if ($this->newsletter->t_style_id == $item->t_style_id) { $this->htmlTemplateId = $id; $this->t_style_id = $item->t_style_id; } unset($xml); } //attachments $this->attItems = array(); $this->dynamicData = array('Name' => '[username]', 'Email' => '[useremail]', 'Site name' => '[sitename]', 'Subscription key' => '[subscription key]', 'Unsubscription link' => '[unsubscription link]', 'Confirmation link' => '[confirmation link]'); $this->attItemslistDirn = "a.filename"; $this->attItemslistOrder = "asc"; // getting of an xml from // Check for errors. if (count($errors = $this->get('Errors'))) { JError::raiseError(500, implode("\n", $errors)); return false; } // We don't need toolbar in the modal window. if ($this->getLayout() !== 'modal') { $this->addToolbar(); } $this->downloads = (array) DownloadHelper::getByNewsletterId($nId); JavascriptHelper::addObject('dataStorage', (object) array('htmlTemplate' => (object) array('template' => (object) array('id' => $this->htmlTemplateId), 'extensions' => (array) $this->usedExts), 'templates' => (array) $this->templates->items, 'modules' => (array) $this->modules, 'plugins' => (array) $this->plugins, 'newsletter' => NewsletterHelper::get($nId))); // Set the document $this->setDocument(); parent::display($tpl); }
/** * Handles the binding of a file attached to a newsletter * * @return void * @since 1.0 */ public function fileAttach() { $filename = JRequest::getString('filename'); $nId = JRequest::getInt('newsletter_id'); if (empty($nId) || empty($filename)) { echo json_encode(array('state' => 0, 'error' => 'Parameters are mising')); return; } $mediaParams = JComponentHelper::getParams('com_media'); $filename = $mediaParams->get('file_path') . DS . $filename; $filename = str_replace('/', DS, $filename); $table = JTable::getInstance('downloads', 'NewsletterTable'); $res = $table->save(array('filename' => $filename, 'newsletter_id' => $nId)); $file = new stdClass(); if ($res) { $file->downloads_id = $table->downloads_id; $file->newsletter_id = $nId; $file->filename = $filename; DownloadHelper::getAttributes($file); $file->size = JHtml::_('file.size', $file->size, 'kb/mb'); } echo json_encode(array('state' => (int) $res, 'error' => $res ? 'ok' : $table->getErrors(), 'data' => $file)); jexit(); }
function yrno_weather($lat, $lon) { $down = new DownloadHelper('http://api.yr.no/weatherapi/locationforecastlts/1.1/?lat=' . urlencode($lat) . ';lon=' . urlencode($lon)); $down->setopt(CURLOPT_USERAGENT, 'BotGG/' . main::VERSION_NUM . ' WeatherModule/1.0 (http://bot.jacekk.net/weather.html)'); try { $data = $down->exec(); $data = new api_yrno_parse($data); $data->parseForecast(); } catch (Exception $e) { $down->cacheFor(600); return FALSE; } $down->cacheFor(7200); return $data; }