Permalink
Browse files

Updated subscribe and unsubscribe functions to xpath

  • Loading branch information...
1 parent 6e20d19 commit eba643e03125dec45baf67870f2e7f1155cbfb49 @hm2k hm2k committed Jan 28, 2012
Showing with 25 additions and 6 deletions.
  1. +25 −6 Services/Mailman.php
View
@@ -311,11 +311,21 @@ public function unsubscribe($email)
if (!$html) {
throw new Services_Mailman_Exception('Unable to fetch HTML.');
}
- if (preg_match('#<h5>Successfully Unsubscribed:</h5>#i', $html)) {
+
+ libxml_use_internal_errors(true);
+ $doc = new DOMDocument();
+ $doc->preserveWhiteSpace = false;
+ $doc->loadHTML($html);
+ $xpath = new DOMXPath($doc);
+ $h5 = $xpath->query('/html/body/h5');
+ $h3 = $xpath->query('/html/body/h3');
+ libxml_clear_errors();
+
+ if ($h5->item(0) && $h5->item(0)->nodeValue == 'Successfully Unsubscribed:') {
return $this;
}
- if (preg_match('#<h3>(.+?)</h3>#i', $html, $m)) {
- throw new Services_Mailman_Exception(trim(strip_tags($m[1]), ':'));
+ if ($h3) {
+ throw new Services_Mailman_Exception(trim($h3->item(0)->nodeValue,':'));
}
throw new Services_Mailman_Exception('Failed to parse HTML.');
}
@@ -349,11 +359,20 @@ public function subscribe($email, $invite = false)
if (!$html) {
throw new Services_Mailman_Exception('Unable to fetch HTML.');
}
- if (preg_match('#<h5>Successfully subscribed:</h5>#i', $html)) {
+
+ libxml_use_internal_errors(true);
+ $doc = new DOMDocument();
+ $doc->preserveWhiteSpace = false;
+ $doc->loadHTML($html);
+ $xpath = new DOMXPath($doc);
+ $h5 = $xpath->query('/html/body/h5');
+ libxml_clear_errors();
+
+ if ($h5 && $h5->item(0)->nodeValue == 'Successfully subscribed:') {
return $this;
}
- if (preg_match('#<h5>(.+?)</h5>#i', $html, $m)) {
- throw new Services_Mailman_Exception(trim(strip_tags($m[1]), ':'));
+ elseif ($h5) {
+ throw new Services_Mailman_Exception(trim($h5->item(0)->nodeValue,':'));
}
throw new Services_Mailman_Exception('Failed to parse HTML.');
}

0 comments on commit eba643e

Please sign in to comment.