db = $db; } /** * Import emaili do newslettera * @param string $emails * @return bool */ public function emailsImport($emails) { $emails = explode(PHP_EOL, $emails); if (is_array($emails)) foreach ($emails as $email) { if (trim($email) and !$this->db->count('pp_newsletter', ['email' => trim($email)])) $this->db->insert('pp_newsletter', [ 'email' => trim($email), 'hash' => md5($email . time()), 'status' => 1 ]); } return true; } /** * Sprawdza czy szablon jest adminski * @param int $templateId * @return string|bool */ public function isAdminTemplate($templateId) { return $this->db->get('pp_newsletter_templates', 'is_admin', ['id' => (int)$templateId]); } /** * Usuniecie szablonu newslettera * @param int $templateId * @return object|bool */ public function templateDelete($templateId) { return $this->db->delete('pp_newsletter_templates', ['id' => (int)$templateId]); } /** * Wysylka newslettera - kolejkowanie * @param string $dates * @param int $template * @param string $onlyOnce * @return bool */ public function send($dates, $template, $onlyOnce) { $results = $this->db->select('pp_newsletter', 'email', ['status' => 1]); if (is_array($results) and !empty($results)) foreach ($results as $row) { if ($template and $onlyOnce) { if (!$this->db->count('pp_newsletter_send', ['AND' => ['id_template' => $template, 'email' => $row]])) $this->db->insert('pp_newsletter_send', [ 'email' => $row, 'dates' => $dates, 'id_template' => $template ? $template : null, 'only_once' => ($onlyOnce == 'on' and $template) ? 1 : 0 ]); } else $this->db->insert('pp_newsletter_send', [ 'email' => $row, 'dates' => $dates, 'id_template' => $template ? $template : null, 'only_once' => ($onlyOnce == 'on' and $template) ? 1 : 0 ]); } return true; } /** * Szczegoly szablonu email * @param int $templateId * @return array|bool */ public function templateDetails($templateId) { $result = $this->db->get('pp_newsletter_templates', '*', ['id' => (int)$templateId]); return $result; } /** * Zapis szablonu (insert lub update) * @param int $id * @param string $name * @param string $text * @return int|bool */ public function templateSave($id, $name, $text) { if (!$id) { if ($this->db->insert('pp_newsletter_templates', [ 'name' => $name, 'text' => $text ])) { \S::delete_cache(); return $this->db->id(); } } else { $this->db->update('pp_newsletter_templates', [ 'name' => $name, 'text' => $text ], [ 'id' => (int)$id ]); \S::delete_cache(); return $id; } } /** * Lista szablonow (nie-adminskich) * @return array|bool */ public function templatesList() { return $this->db->select('pp_newsletter_templates', '*', ['is_admin' => 0, 'ORDER' => ['name' => 'ASC']]); } /** * Wypisanie z newslettera po hashu * @param string $hash * @return object|bool */ public function unsubscribe($hash) { return $this->db->update('pp_newsletter', ['status' => 0], ['hash' => $hash]); } /** * Potwierdzenie zapisu po hashu * @param string $hash * @return bool */ public function confirm($hash) { if (!$id = $this->db->get('pp_newsletter', 'id', ['AND' => ['hash' => $hash, 'status' => 0]])) return false; else $this->db->update('pp_newsletter', ['status' => 1], ['id' => $id]); return true; } /** * Wysylka zakolejkowanych newsletterow (cron/front) * @param int $limit * @param array $settings * @param array $lang * @return bool */ public function newsletterSend($limit, $settings, $lang) { $results = $this->db->query('SELECT * FROM pp_newsletter_send WHERE mailed = 0 ORDER BY id ASC LIMIT ' . (int)$limit)->fetchAll(); if (is_array($results) and !empty($results)) { foreach ($results as $row) { $dates = explode(' - ', $row['dates']); $text = \admin\view\Newsletter::preview( \admin\factory\Articles::articles_by_date_add($dates[0], $dates[1]), \admin\factory\Settings::settings_details(), \admin\factory\Newsletter::email_template_detalis($row['id_template']) ); if ($settings['ssl']) $base = 'https'; else $base = 'http'; $link = $base . "://" . $_SERVER['SERVER_NAME'] . '/newsletter/unsubscribe/hash=' . $this->getHash($row['email']); $text = str_replace('[WYPISZ_SIE]', $link, $text); $regex = "-(]+src\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i"; $text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text); $regex = "-(]+href\s*=\s*['\"])(((?!'|\"|http(|s)://).)*)(['\"][^>]*>)-i"; $text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text); \S::send_email($row['email'], 'Newsletter ze strony: ' . $_SERVER['SERVER_NAME'], $text); if ($row['only_once']) $this->db->update('pp_newsletter_send', ['mailed' => 1], ['id' => $row['id']]); else $this->db->delete('pp_newsletter_send', ['id' => $row['id']]); } return true; } return false; } /** * Pobranie hasha dla emaila * @param string $email * @return string|bool */ public function getHash($email) { return $this->db->get('pp_newsletter', 'hash', ['email' => $email]); } /** * Zapis do newslettera z wysylka potwierdzenia * @param string $email * @param array $settings * @param array $lang * @return bool */ public function signin($email, $settings, $lang) { if (!\S::email_check($email)) return false; if (!$this->db->get('pp_newsletter', 'id', ['email' => $email])) { $hash = md5(time() . $email); $text = $settings['newsletter_header']; $text .= $this->getTemplate('#potwierdzenie-zapisu-do-newslettera'); $text .= $settings['newsletter_footer_1']; $settings['ssl'] ? $base = 'https' : $base = 'http'; $regex = "-(]+src\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i"; $text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text); $regex = "-(]+href\s*=\s*['\"])(((?!'|\"|http://).)*)(['\"][^>]*>)-i"; $text = preg_replace($regex, "$1" . $base . "://" . $_SERVER['SERVER_NAME'] . "$2$4", $text); $link = '/newsletter/confirm/hash=' . $hash; $text = str_replace('[LINK]', $link, $text); $send = \S::send_email($email, $lang['potwierdz-zapisanie-sie-do-newslettera'], $text); $this->db->insert('pp_newsletter', ['email' => $email, 'hash' => $hash, 'status' => 0]); return true; } return false; } /** * Pobranie szablonu po nazwie * @param string $templateName * @return string|bool */ public function getTemplate($templateName) { return $this->db->get('pp_newsletter_templates', 'text', ['name' => $templateName]); } /** * Wypisanie z newslettera po emailu * @param string $email * @return object|bool */ public function signout($email) { if ($this->db->get('pp_newsletter', 'id', ['email' => $email])) return $this->db->delete('pp_newsletter', ['email' => $email]); return false; } }