#!/usr/bin/php = 10 and Created > ? - interval 45 day) or (IsRecurring = false and Amount >= 100 and Created > ? - interval 1 year) ) ) ', [$now, $now], 'Patron'); if(sizeof($expiredPatrons) > 0){ $ebooksThisYear = 0; // We can't use the Library class to get ebooks because this script is typically run via cron or CLI, // which doesn't have access PHP-FMP's APCu cache. foreach(explode("\n", trim(shell_exec('find ' . EBOOKS_DIST_PATH . ' -name "content.opf"') ?? '')) as $filename){ $metadata = file_get_contents($filename); // Don't create a new Ebook object because that's very slow. Just do a regex match for speed. preg_match_all('/(.+?)<\/dc:date>/iu', $metadata, $matches); if(sizeof($matches) > 0){ $created = new DateTimeImmutable($matches[1][0]); if($created >= $lastYear){ $ebooksThisYear++; } } } foreach($expiredPatrons as $patron){ Db::Query(' UPDATE Patrons set Ended = ? where UserId = ? ', [$now, $patron->UserId]); Db::Query(' UPDATE Benefits set CanAccessFeeds = false, CanVote = false, CanBulkDownload = false where UserId = ? ', [$patron->UserId]); // Email the patron to notify them their term has ended // Is the patron a recurring subscriber? $lastPayment = Db::Query(' SELECT * from Payments where UserId = ? order by Created desc limit 1 ', [$patron->UserId], 'Payment'); if(sizeof($lastPayment) > 0 && $patron->User->Email !== null){ $em = new Email(); $em->From = EDITOR_IN_CHIEF_EMAIL_ADDRESS; $em->FromName = EDITOR_IN_CHIEF_NAME; $em->To = $patron->User->Email; $em->ToName = $patron->Name; $em->Subject = 'Will you still help us make free, beautiful digital literature?'; if($lastPayment[0]->IsRecurring){ // Email recurring donors who have lapsed $em->Body = Template::EmailPatronsCircleRecurringCompleted(); $em->TextBody = Template::EmailPatronsCircleRecurringCompletedText(); } else{ // Email one time donors who have expired after one year $em->Body = Template::EmailPatronsCircleCompleted(['ebooksThisYear' => $ebooksThisYear]); $em->TextBody = Template::EmailPatronsCircleCompletedText(['ebooksThisYear' => $ebooksThisYear]); } $em->Send(); } } } ?>