public function check() { $checkStart = (int) $this->_config->get("general", "check_start", 7); $checkEnd = (int) $this->_config->get("general", "check_end", 24); if ($checkStart > 23) { $checkStart = 0; } if ($checkEnd < 1) { $checkEnd = 24; } $hour = (int) date("G"); if ($hour < $checkStart || $hour >= $checkEnd) { $this->_logger->info("Hors de la plage horaire. Contrôle annulé."); return; } $this->_logger->info("Contrôle des alertes."); $this->_checkConnection(); $users = $this->_userStorage->fetchAll(); // génération d'URL court pour les SMS $curlTinyurl = curl_init(); curl_setopt($curlTinyurl, CURLOPT_RETURNTRANSFER, 1); $storageType = $this->_config->get("storage", "type", "files"); foreach ($users as $user) { if ($storageType == "db") { $storage = new \App\Storage\Db\Alert($this->_userStorage->getDbConnection(), $user); $this->_logger->info("User: "******"/var/configs/" . $user->getUsername() . ".csv"; if (!is_file($file)) { continue; } $storage = new \App\Storage\File\Alert($file); $this->_logger->debug("Fichier config: " . $file); } // configuration des notifications. $notifications = array(); $notifications_params = $user->getOption("notification"); if ($notifications_params && is_array($notifications_params)) { foreach ($notifications_params as $notification_name => $options) { if (!is_array($options)) { continue; } try { $notifications[$notification_name] = \Message\AdapterFactory::factory($notification_name, $options); $this->_logger->debug("notification " . get_class($notifications[$notification_name]) . " activée"); } catch (\Exception $e) { $this->_logger->warn("notification " . $notification_name . " invalide"); } } } $alerts = $storage->fetchAll(); $this->_logger->info(count($alerts) . " alerte" . (count($alerts) > 1 ? "s" : "") . " trouvée" . (count($alerts) > 1 ? "s" : "")); if (count($alerts) == 0) { continue; } $unique_ads = $user->getOption("unique_ads"); foreach ($alerts as $i => $alert) { $currentTime = time(); if (!isset($alert->time_updated)) { $alert->time_updated = 0; } if ((int) $alert->time_updated + (int) $alert->interval * 60 > $currentTime || $alert->suspend) { continue; } $this->_logger->info("Contrôle de l'alerte " . $alert->url); try { $parser = \AdService\ParserFactory::factory($alert->url); } catch (\AdService\Exception $e) { $this->_logger->info("\t" . $e->getMessage()); continue; } $this->_logger->debug("Dernière mise à jour : " . (!empty($alert->time_updated) ? date("d/m/Y H:i", (int) $alert->time_updated) : "inconnue")); $this->_logger->debug("Dernière annonce : " . (!empty($alert->time_last_ad) ? date("d/m/Y H:i", (int) $alert->time_last_ad) : "inconnue")); $alert->time_updated = $currentTime; if (!($content = $this->_httpClient->request($alert->url))) { $this->_logger->error("Curl Error : " . $this->_httpClient->getError()); continue; } $cities = array(); if ($alert->cities) { $cities = array_map("trim", explode("\n", mb_strtolower($alert->cities))); } $filter = new \AdService\Filter(array("price_min" => $alert->price_min, "price_max" => $alert->price_max, "cities" => $cities, "price_strict" => (bool) $alert->price_strict, "categories" => $alert->getCategories(), "min_id" => $unique_ads ? $alert->last_id : 0)); $ads = $parser->process($content, $filter, parse_url($alert->url, PHP_URL_SCHEME)); $countAds = count($ads); if ($countAds == 0) { $storage->save($alert); continue; } $siteConfig = \AdService\SiteConfigFactory::factory($alert->url); $newAds = array(); $time_last_ad = (int) $alert->time_last_ad; foreach ($ads as $ad) { if ($time_last_ad < $ad->getDate()) { $newAds[$ad->getId()] = (require DOCUMENT_ROOT . "/app/mail/views/mail-ad.phtml"); if ($alert->time_last_ad < $ad->getDate()) { $alert->time_last_ad = $ad->getDate(); } if ($unique_ads && $ad->getId() > $alert->last_id) { $alert->last_id = $ad->getId(); } } } if (!$newAds) { $storage->save($alert); continue; } $countAds = count($newAds); $this->_logger->info($countAds . " annonce" . ($countAds > 1 ? "s" : "") . " trouvée" . ($countAds > 1 ? "s" : "")); $this->_mailer->clearAddresses(); $error = false; if ($alert->send_mail) { try { $emails = explode(",", $alert->email); foreach ($emails as $email) { $this->_mailer->addAddress(trim($email)); } } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); $error = true; } if (!$error) { if ($alert->group_ads) { $newAdsCount = count($newAds); $subject = "Alert " . $siteConfig->getOption("site_name") . " : " . $alert->title; $message = '<h2>' . $newAdsCount . ' nouvelle' . ($newAdsCount > 1 ? 's' : '') . ' annonce' . ($newAdsCount > 1 ? 's' : '') . ' - ' . date("d/m/Y H:i", $currentTime) . '</h2> <p>Lien de recherche: <a href="' . htmlspecialchars($alert->url, null, "UTF-8") . '">' . htmlspecialchars($alert->url, null, "UTF-8") . '</a></p> <hr /><br />' . implode("<br /><hr /><br />", $newAds) . '<hr /><br />'; $this->_mailer->Subject = $subject; $this->_mailer->Body = $message; try { $this->_mailer->send(); } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); } } else { $newAds = array_reverse($newAds, true); foreach ($newAds as $id => $ad) { $subject = ($alert->title ? $alert->title . " : " : "") . $ads[$id]->getTitle(); $message = '<h2>Nouvelle annonce - ' . date("d/m/Y H:i", $currentTime) . '</h2> <p>Lien de recherche: <a href="' . htmlspecialchars($alert->url, null, "UTF-8") . '">' . htmlspecialchars($alert->url, null, "UTF-8") . '</a></p> <hr /><br />' . $ad . '<hr /><br />'; $this->_mailer->Subject = $subject; $this->_mailer->Body = $message; try { $this->_mailer->send(); } catch (phpmailerException $e) { $this->_logger->warn($e->getMessage()); } } } } } if ($notifications && ($alert->send_sms_free_mobile || $alert->send_sms_ovh || $alert->send_pushbullet || $alert->send_notifymyandroid || $alert->send_pushover)) { if ($countAds < 5) { // limite à 5 SMS foreach ($newAds as $id => $ad) { $ad = $ads[$id]; // récupère l'objet. $url = $ad->getLink(); if (false !== strpos($url, "leboncoin")) { $url = "http://mobile.leboncoin.fr/vi/" . $ad->getId() . ".htm"; } curl_setopt($curlTinyurl, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=" . $url); if ($url = curl_exec($curlTinyurl)) { $msg = "Nouvelle annonce " . ($alert->title ? $alert->title . " : " : "") . $ad->getTitle(); $others = array(); if ($ad->getPrice()) { $others[] = number_format($ad->getPrice(), 0, ',', ' ') . $ad->getCurrency(); } if ($ad->getCity()) { $others[] = $ad->getCity(); } elseif ($ad->getCountry()) { $others[] = $ad->getCountry(); } if ($others) { $msg .= " (" . implode(", ", $others) . ")"; } $params = array("title" => "Alerte " . $siteConfig->getOption("site_name"), "description" => "Nouvelle annonce" . ($alert->title ? " pour : " . $alert->title : ""), "url" => $url); foreach ($notifications as $key => $notifier) { switch ($key) { case "freeMobile": $key_test = "send_sms_free_mobile"; break; case "ovh": $key_test = "send_sms_ovh"; break; default: $key_test = "send_" . $key; } if (isset($alert->{$key_test}) && $alert->{$key_test}) { try { $notifier->send($msg, $params); } catch (Exception $e) { $this->_logger->warn("Erreur sur envoi via " . get_class($notifier) . ": (" . $e->getCode() . ") " . $e->getMessage()); } } } } } } else { // envoi un msg global curl_setopt($curlTinyurl, CURLOPT_URL, "http://tinyurl.com/api-create.php?url=" . $alert->url); if ($url = curl_exec($curlTinyurl)) { $msg = "Il y a " . $countAds . " nouvelles annonces pour votre alerte '" . ($alert->title ? $alert->title : "sans nom") . "'"; $params = array("title" => "Alerte " . $siteConfig->getOption("site_name"), "description" => "Nouvelle" . ($countAds > 1 ? "s" : "") . " annonce" . ($countAds > 1 ? "s" : "") . ($alert->title ? " pour : " . $alert->title : ""), "url" => $url); foreach ($notifications as $key => $notifier) { switch ($key) { case "freeMobile": $key_test = "send_sms_free_mobile"; break; case "ovh": $key_test = "send_sms_ovh"; break; default: $key_test = "send_" . $key; } if (isset($alert->{$key_test}) && $alert->{$key_test}) { try { $notifier->send($msg, $params); } catch (Exception $e) { $this->_logger->warn("Erreur sur envoi via " . get_class($notifier) . ": (" . $e->getCode() . ") " . $e->getMessage()); } } } } } } $storage->save($alert); } } curl_close($curlTinyurl); $this->_mailer->smtpClose(); }
} header("Content-Type: application/rss+xml", true); $logger = Logger::getLogger("main"); $id = sha1($_SERVER["REQUEST_URI"]); $cache_filename = DOCUMENT_ROOT . "/var/feeds/" . $id . ".xml"; if (is_file($cache_filename)) { readfile($cache_filename); return; } $params = $_GET; if (isset($params["cities"])) { $params["cities"] = array_map("trim", explode("\n", mb_strtolower($params["cities"]))); } $content = $client->request($_GET["url"]); $filter = new \AdService\Filter($params); $siteConfig = \AdService\SiteConfigFactory::factory($_GET["url"]); $ads = $parser->process($content, $filter, parse_url($_GET["url"], PHP_URL_SCHEME)); $title = $siteConfig->getOption("site_name"); $urlParams = parse_url($_GET["url"]); if (!empty($urlParams["query"])) { parse_str($urlParams["query"], $aQuery); if (!empty($aQuery["q"])) { $title .= " - " . $aQuery["q"]; } } $feeds = new RSS2(); $feeds->setTitle($siteConfig->getOption("site_name")); $feeds->setLink($siteConfig->getOption("site_url")); $feeds->setSelfLink(!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on" ? "https" : "http" . "://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); $feeds->setDescription("Flux RSS de la recherche : " . $_GET["url"]); $feeds->setChannelElement("language", "fr-FR");
$errors["price"] = "Valeur de \"prix min\" non valide. "; } if ($alert->price_max != (int) $alert->price_max) { $errors["price"] .= "Valeur de \"prix max\" non valide."; } if (!empty($_POST["price_strict"])) { $alert->price_strict = (int) (bool) $_POST["price_strict"]; } else { $alert->price_strict = false; } $alert->group = !empty($_POST["group"]) ? trim($_POST["group"]) : ""; if (empty($alert->url)) { $errors["url"] = "Ce champ est obligatoire."; } else { try { $siteConfig = \AdService\SiteConfigFactory::factory($alert->url); if (false !== strpos($alert->url, "leboncoin.fr")) { $alert->url = rtrim(preg_replace("#(o|sp)=[0-9]*&?#", "", $alert->url), "?&"); } } catch (\AdService\Exception $e) { $errors["url"] = "Cette adresse ne semble pas valide."; } } $alert->interval = (int) $alert->interval; if ($alert->interval != (int) $alert->interval || $alert->interval < 0) { $errors["interval"] = "Cette valeur n'est pas valide."; } if (empty($errors)) { if (!empty($_POST["categories"])) { if (is_array($alert->categories)) { $alert->categories = implode(",", $_POST["categories"]);