Last active
July 23, 2020 16:14
-
-
Save killerbees19/280f9dde49134203357640a8e8f796ac to your computer and use it in GitHub Desktop.
netcup Produkte per E-Mail abonnieren
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
function exception_error_handler($severity, $message, $file, $line) | |
{ throw new ErrorException($message, 0, $severity, $file, $line); } | |
set_error_handler('exception_error_handler'); error_reporting(-1); | |
$mail_recipient = '[email protected]'; | |
$mail_from = sprintf('%s@%s', posix_getlogin(), gethostname()); | |
$xml_file = 'https://www.netcup.de/bestellen/netcupproduktexml.php'; | |
$json_file = __dir__ . '/netcup-produkte.json'; | |
try | |
{ | |
$json = json_decode(file_get_contents($json_file), true); | |
} | |
catch(Exception $e) | |
{ | |
$json = []; | |
} | |
if($verbose = ($argc > 1 && $argv[1] === '-v')) | |
{ | |
printf("Loading XML file: %s\n", $xml_file); | |
} | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $xml_file); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
$xml = new SimpleXMLElement(curl_exec($curl), LIBXML_COMPACT); | |
curl_close($curl); | |
foreach($xml->children() as $_) | |
{ | |
if(($id = (int) $_->attributes()->id)) | |
{ | |
if(!isset($json[$id])) | |
{ | |
if($verbose) | |
{ | |
printf("New element: ID %d\n", $id); | |
} | |
# ProduktMonategeschenkteGrundgebuehr, ProduktAnzahlDomainsinklusive, … | |
if(!mail( | |
$mail_recipient, | |
sprintf("[%04d] %s", $id, $_->ProduktName), | |
wordwrap(base64_encode(vsprintf("%s\n\nGrundgebühr: %s\nEinrichtungsgebühr: %s\nMindestvertragslaufzeit: %s\nAbrechnungsperiode: %s\n\n%s\n", | |
[ | |
trim($_->ProduktBeschreibung), | |
$_->ProduktGrundgebuehr, | |
$_->ProduktEinrichtungsgebuehr, | |
$_->ProduktMindesvertragslaufzeit, | |
$_->ProduktAbrechnungsperiode, | |
(strpos($_->ProduktURL, '://') === false ? 'https://www.netcup.de' : '') . $_->ProduktURL, | |
])), 75, "\n", true), | |
sprintf("From: %s\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: base64", $mail_from) | |
)) | |
{ | |
throw new Exception(sprintf('Could not send mail for element with ID %d', $id)); | |
} | |
$json[$id] = time(); | |
file_put_contents($json_file, json_encode($json) . "\n", LOCK_EX); | |
} | |
} | |
else | |
{ | |
fwrite(STDERR, "WARNING: Invalid ID in XML data!\n"); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment