Beispiel #1
0
            }
            break;
        default:
            echo "Unrecognized switch: {$argv[1]}\n\n";
            $switchError = true;
            break;
    }
    array_shift($argv);
}
// Display help message if parameters missing:
if (!isset($argv[2]) || $switchError) {
    echo "Usage: {$progName} [--test-only] [--index <type>] XML_file " . "properties_file\n" . "\tXML_file - source file to index\n" . "\tproperties_file - import configuration file\n" . "If the optional --test-only flag is set, transformed XML will be " . "displayed\non screen for debugging purposes, but it will not be " . "indexed into VuFind.\n\n" . "If the optional --index parameter is set, it must be followed by " . "the name of\na class for accessing Solr; it defaults to the standard " . "Solr class, but could\nbe overridden with, for example, SolrAuth to " . "load authority records.\n\n" . "Note: See vudl.properties and ojs.properties for configuration " . "examples.\n";
    exit(1);
}
// Setup Local Database Connection
ConnectionManager::connectToDatabase();
// Process the file (or die trying):
$xml = processXSLT($argv[1], $argv[2]);
if (!$xml) {
    exit(1);
}
// Save the results (or just display them, if in test mode):
if (!$testMode) {
    $solr = ConnectionManager::connectToIndex($index);
    $result = $solr->saveRecord($xml);
    if (!PEAR::isError($result)) {
        echo "Successfully imported {$argv[1]}...\n";
        exit(0);
    } else {
        echo "Fatal error: " . $result->getMessage() . "\n";
        exit(1);
 /**
  * Process transactions. Try to register unregistered transactions
  * and inform on expired transactions.
  *
  * @return void
  * @access public
  */
 public function process()
 {
     global $configArray;
     global $interface;
     ini_set('display_errors', true);
     $configArray = $mainConfig = readConfig();
     $datasourceConfig = getExtraConfigArray('datasources');
     // Set up time zone. N.B. Don't use msg() or other
     // functions requiring date before this.
     date_default_timezone_set($configArray['Site']['timezone']);
     $this->msg("OnlinePayment monitor started");
     // Setup Local Database Connection
     ConnectionManager::connectToDatabase();
     // Initialize Mailer
     $mailer = new VuFindMailer();
     $now = new DateTime();
     $expiredCnt = 0;
     $failedCnt = 0;
     $registeredCnt = 0;
     $remindCnt = 0;
     $user = false;
     $report = array();
     // Attempt to re-register paid transactions whose registration has failed.
     $tr = new Transaction();
     foreach ($tr->getFailedTransactions() as $t) {
         $this->msg("  Registering transaction id {$t->id} / {$t->transaction_id}");
         // check if the transaction has not been registered for too long
         $paid_time = new DateTime($t->paid);
         $diff = $now->diff($paid_time);
         $diffHours = $diff->days * 24 + $diff->h;
         if ($diffHours > $this->expireHours) {
             if (!isset($report[$t->driver])) {
                 $report[$t->driver] = 0;
             }
             $report[$t->driver]++;
             $expiredCnt++;
             if (!$t->setTransactionReported($t->transaction_id)) {
                 $this->err('    Failed to update transaction ' . $t->transaction_id . 'as reported');
             }
             $transaction = clone $t;
             $transaction->complete = Transaction::STATUS_REGISTRATION_EXPIRED;
             if ($transaction->update($t) === false) {
                 $this->err('    Failed to update transaction ' . $t->transaction_id . 'as expired.');
             } else {
                 $this->msg('    Transaction ' . $t->transaction_id . ' expired.');
             }
         } else {
             if ($user === false || $t->user_id != $user->id) {
                 $user = User::staticGet($t->user_id);
             }
             $catalog = ConnectionManager::connectToCatalog();
             if ($catalog && $catalog->status) {
                 $account = new User_account();
                 $account->user_id = $t->user_id;
                 $account->cat_username = $t->cat_username;
                 if ($account->find(true)) {
                     if (!($patron = $catalog->patronLogin($t->cat_username, $account->cat_password))) {
                         $this->err('    Could not perform patron login for transaction ' . $t->transaction_id);
                         $failedCnt++;
                         continue;
                     }
                 }
                 $res = $catalog->markFeesAsPaid($patron, $t->amount);
                 if ($res === true) {
                     if (!$t->setTransactionRegistered($t->transaction_id)) {
                         $this->err('    Failed to update transaction ' . $t->transaction_id . 'as registered');
                     }
                     $registeredCnt++;
                 } else {
                     $t->setTransactionRegistrationFailed($t->transaction_id, $res);
                     $failedCnt++;
                     $this->msg('    Registration of transaction ' . $t->transaction_id . ' failed');
                     $this->msg("      {$res}");
                 }
             } else {
                 $this->err("Failed to connect to catalog ({$patronId})");
                 continue;
             }
         }
     }
     // Report paid and unregistered transactions whose registration
     // can not be re-tried:
     $tr = new Transaction();
     foreach ($tr->getUnresolvedTransactions($this->reportIntervalHours) as $t) {
         $this->msg("  Transaction id {$t->transaction_id} still unresolved.");
         if (!$t->setTransactionReported($t->transaction_id)) {
             $this->err('    Failed to update transaction ' . $t->transaction_id . ' as reported');
         }
         if (!isset($report[$t->driver])) {
             $report[$t->driver] = 0;
         }
         $report[$t->driver]++;
         $remindCnt++;
     }
     if ($registeredCnt) {
         $this->msg("  Total registered: {$registeredCnt}");
     }
     if ($expiredCnt) {
         $this->msg("  Total expired: {$expiredCnt}");
     }
     if ($failedCnt) {
         $this->msg("  Total failed: {$failedCnt}");
     }
     if ($remindCnt) {
         $this->msg("  Total to be reminded: {$remindCnt}");
     }
     $configArray = readConfig();
     $siteLocal = $configArray['Site']['local'];
     $interface = new UInterface($siteLocal);
     // Send report of transactions that need to be resolved manually:
     foreach ($report as $driver => $cnt) {
         if ($cnt) {
             $settings = getExtraConfigArray("VoyagerRestful_{$driver}");
             if (!$settings || !isset($settings['OnlinePayment']['errorEmail'])) {
                 $this->err("  Error email for expired transactions not defined for driver {$driver} ({$cnt} expired transactions)");
                 continue;
             }
             $email = $settings['OnlinePayment']['errorEmail'];
             $this->msg("  [{$driver}] Inform {$cnt} expired transactions for driver {$driver} to {$email}");
             $mailer = new VuFindMailer();
             $subject = "Finna: ilmoitus tietokannan {$driver} epäonnistuneista verkkomaksuista";
             $interface->assign('driver', $driver);
             $interface->assign('cnt', $cnt);
             $msg = $interface->fetch('Emails/online-payment-error.tpl');
             if (!($result = $mailer->send($email, $this->fromEmail, $subject, $msg))) {
                 $this->err("    Failed to send error email to customer: {$email}");
             }
         }
     }
     $this->msg("OnlinePayment monitor completed");
     $this->reportErrors();
 }
 /**
  * Send scheduled alerts
  *
  * @return void
  */
 public function send()
 {
     global $configArray;
     global $interface;
     global $translator;
     $iso8601 = 'Y-m-d\\TH:i:s\\Z';
     ini_set('display_errors', true);
     $configArray = $mainConfig = readConfig();
     $datasourceConfig = getExtraConfigArray('datasources');
     $siteLocal = $configArray['Site']['local'];
     // Set up time zone. N.B. Don't use msg() or other functions requiring date before this.
     date_default_timezone_set($configArray['Site']['timezone']);
     $this->msg('Sending scheduled alerts');
     // Setup Local Database Connection
     ConnectionManager::connectToDatabase();
     // Initialize Mailer
     $mailer = new VuFindMailer();
     // Find all scheduled alerts
     $sql = 'SELECT * FROM "search" WHERE "schedule" > 0 ORDER BY user_id';
     $s = new SearchEntry();
     $s->query($sql);
     $this->msg('Processing ' . $s->N . ' searches');
     $user = false;
     $interface = false;
     $institution = false;
     $todayTime = new DateTime();
     while ($s->fetch()) {
         $lastTime = new DateTime($s->last_executed);
         if ($s->schedule == 1) {
             // Daily
             if ($todayTime->format('Y-m-d') == $lastTime->format('Y-m-d')) {
                 $this->msg('Bypassing search ' . $s->id . ': previous execution too recent (daily, ' . $lastTime->format($iso8601) . ')');
                 continue;
             }
         } elseif ($s->schedule == 2) {
             // Weekly
             $diff = $todayTime->diff($lastTime);
             if ($diff->days < 6) {
                 $this->msg('Bypassing search ' . $s->id . ': previous execution too recent (weekly, ' . $lastTime->format($iso8601) . ')');
                 continue;
             }
         } else {
             $this->msg('Search ' . $s->id . ': unknown schedule: ' . $s->schedule);
             continue;
         }
         if ($user === false || $s->user_id != $user->id) {
             $user = User::staticGet($s->user_id);
         }
         if (!$user->email || trim($user->email) == '') {
             $this->msg('User ' . $user->username . ' does not have an email address, bypassing alert ' . $s->id);
             continue;
         }
         $userInstitution = reset(explode(':', $user->username, 2));
         if (!$institution || $institution != $userInstitution) {
             $institution = $userInstitution;
             if (!isset($datasourceConfig[$institution])) {
                 foreach ($datasourceConfig as $code => $values) {
                     if (isset($values['institution']) && strcasecmp($values['institution'], $institution) == 0) {
                         $institution = $code;
                         break;
                     }
                 }
             }
             if (!($configArray = $this->readInstitutionConfig($institution))) {
                 continue;
             }
             $configArray['Site']['url'] = $s->schedule_base_url;
             // Start Interface
             $interface = new UInterface($siteLocal);
             $validLanguages = array_keys($configArray['Languages']);
             $dateFormat = new VuFindDate();
         }
         $language = $user->language;
         if (!in_array($user->language, $validLanguages)) {
             $language = $configArray['Site']['language'];
         }
         $translator = new I18N_Translator(array($configArray['Site']['local'] . '/lang', $configArray['Site']['local'] . '/lang_local'), $language, $configArray['System']['debug']);
         $interface->setLanguage($language);
         $minSO = unserialize($s->search_object);
         // Check minified search object type
         $searchObject = SearchObjectFactory::deminify($minSO);
         if (!$searchObject instanceof SearchObject_Solr) {
             $this->msg('Search ' . $s->id . ': search object type not supported');
             continue;
         }
         // Create a new search object to avoid cached defaults for e.g.
         // hidden filters.
         $searchObject = SearchObjectFactory::initSearchObject();
         $searchObject->deminify($minSO);
         $searchObject->setSort('first_indexed desc');
         $searchTime = time();
         $searchDate = gmdate($iso8601, time());
         $searchObject->setLimit(50);
         $searchObject->disableLogging();
         $results = $searchObject->processSearch();
         if (PEAR::isError($results)) {
             $this->msg('Search ' . $s->id . ' failed: ' . $results->getMessage());
             continue;
         }
         if ($searchObject->getResultTotal() < 1) {
             $this->msg('No results found for search ' . $s->id);
             continue;
         }
         $newestRecordDate = date($iso8601, strtotime($results['response']['docs'][0]['first_indexed']));
         $lastExecutionDate = $lastTime->format($iso8601);
         if ($newestRecordDate < $lastExecutionDate) {
             $this->msg('No new results for search ' . $s->id . ": {$newestRecordDate} < {$lastExecutionDate}");
         } else {
             $this->msg('New results for search ' . $s->id . ": {$newestRecordDate} >= {$lastExecutionDate}");
             $interface->assign('summary', $searchObject->getResultSummary());
             $interface->assign('searchDate', $dateFormat->convertToDisplayDate("U", floor($searchTime)));
             $interface->assign('lastSearchDate', $dateFormat->convertToDisplayDate("U", floor($lastTime->getTimestamp())));
             $records = array();
             foreach ($results['response']['docs'] as &$doc) {
                 $docDate = date($iso8601, strtotime($doc['first_indexed']));
                 if ($docDate < $lastExecutionDate) {
                     break;
                 }
                 $record = RecordDriverFactory::initRecordDriver($doc);
                 $records[] = $interface->fetch($record->getSearchResult('email'));
             }
             $interface->assign('recordSet', $records);
             $key = $this->getSecret($user, $s->id);
             $params = array('id' => $s->id, 'type' => 'alert', 'key' => $key);
             $unsubscribeUrl = $configArray['Site']['url'] . '/MyResearch/Unsubscribe?' . http_build_query($params);
             $interface->assign('info', array('time' => $dateFormat->convertToDisplayDate("U", floor($searchObject->getStartTime())), 'url' => $searchObject->renderSearchUrl(), 'searchId' => $searchObject->getSearchId(), 'description' => $searchObject->displayQuery(), 'filters' => $searchObject->getFilterList(), 'hits' => $searchObject->getResultTotal(), 'speed' => round($searchObject->getQuerySpeed(), 2) . "s", 'schedule' => $s->schedule, 'last_executed' => $s->last_executed, 'recordCount' => count($records), 'unsubscribeUrl' => $unsubscribeUrl));
             $searchObject->close();
             // Load template
             $message = $interface->fetch('MyResearch/alert-email.tpl');
             if (strstr($message, 'Warning: Smarty error:')) {
                 $this->msg("Message template processing failed: {$message}");
                 continue;
             }
             $result = $mailer->send($user->email, $configArray['Site']['email'], translate('Scheduled Alert Results'), $message);
             if (PEAR::isError($result)) {
                 $this->msg("Failed to send message to {$user->email}: " . $result->getMessage());
                 continue;
             }
         }
         // Update search date
         $s->changeLastExecuted($searchDate);
     }
     $this->reportErrors();
     $this->msg('Scheduled alerts execution completed');
 }
 /**
  * Send due date reminders
  *
  * @return void
  */
 public function send()
 {
     global $configArray;
     global $interface;
     global $translator;
     $iso8601 = 'Y-m-d\\TH:i:s\\Z';
     ini_set('display_errors', true);
     $configArray = $mainConfig = readConfig();
     $datasourceConfig = getExtraConfigArray('datasources');
     $siteLocal = $configArray['Site']['local'];
     // Set up time zone. N.B. Don't use msg() or other functions requiring date before this.
     date_default_timezone_set($configArray['Site']['timezone']);
     $this->msg('Sending due date reminders');
     // Setup Local Database Connection
     ConnectionManager::connectToDatabase();
     // And index
     $db = ConnectionManager::connectToIndex();
     // Initialize Mailer
     $mailer = new VuFindMailer();
     // Find all scheduled alerts
     $sql = 'SELECT * FROM "user" WHERE "due_date_reminder" > 0 ORDER BY id';
     $user = new User();
     $user->query($sql);
     $this->msg('Processing ' . $user->N . ' users');
     $interface = false;
     $institution = false;
     $todayTime = new DateTime();
     $catalog = ConnectionManager::connectToCatalog();
     while ($user->fetch()) {
         if (!$user->email || trim($user->email) == '') {
             $this->msg('User ' . $user->username . ' does not have an email address, bypassing due date reminders');
             continue;
         }
         // Initialize settings and interface
         $userInstitution = reset(explode(':', $user->username, 2));
         if (!$institution || $institution != $userInstitution) {
             $institution = $userInstitution;
             if (!isset($datasourceConfig[$institution])) {
                 foreach ($datasourceConfig as $code => $values) {
                     if (isset($values['institution']) && strcasecmp($values['institution'], $institution) == 0) {
                         $institution = $code;
                         break;
                     }
                 }
             }
             if (!($configArray = $this->readInstitutionConfig($institution))) {
                 continue;
             }
             // Start Interface
             $interface = new UInterface($siteLocal);
             $validLanguages = array_keys($configArray['Languages']);
             $dateFormat = new VuFindDate();
         }
         $language = $user->language;
         if (!in_array($user->language, $validLanguages)) {
             $language = $configArray['Site']['language'];
         }
         $translator = new I18N_Translator(array($configArray['Site']['local'] . '/lang', $configArray['Site']['local'] . '/lang_local'), $language, $configArray['System']['debug']);
         $interface->setLanguage($language);
         // Go through accounts and check loans
         $account = new User_account();
         $account->user_id = $user->id;
         if (!$account->find(false)) {
             continue;
         }
         $remindLoans = array();
         while ($account->fetch()) {
             $patron = $catalog->patronLogin($account->cat_username, $account->cat_password);
             if ($patron === null || PEAR::isError($patron)) {
                 $this->msg('Catalog login failed for user ' . $user->id . ', account ' . $account->id . ' (' . $account->cat_username . '): ' . ($patron ? $patron->getMessage() : 'patron not found'));
                 continue;
             }
             $loans = $catalog->getMyTransactions($patron);
             if (PEAR::isError($loans)) {
                 $this->msg('Could not fetch loans for user ' . $user->id . ', account ' . $account->id . ' (' . $account->cat_username . ')');
                 continue;
             }
             foreach ($loans as $loan) {
                 $dueDate = new DateTime($loan['duedate']);
                 if ($todayTime >= $dueDate || $dueDate->diff($todayTime)->days <= $user->due_date_reminder) {
                     // Check that a reminder hasn't been sent already
                     $reminder = new Due_date_reminder();
                     $reminder->user_id = $user->id;
                     $reminder->loan_id = $loan['item_id'];
                     $reminder->due_date = $dueDate->format($iso8601);
                     if ($reminder->find(false)) {
                         // Reminder already sent
                         continue;
                     }
                     // Store also title for display in email
                     $title = isset($loan['title']) ? $loan['title'] : translate('Title not available');
                     if (isset($loan['id'])) {
                         $record = $db->getRecord($loan['id']);
                         if ($record && isset($record['title'])) {
                             $title = $record['title'];
                         }
                     }
                     $remindLoans[] = array('loanId' => $loan['item_id'], 'dueDate' => $loan['duedate'], 'dueDateFormatted' => $dueDate->format(isset($configArray['Site']['displayDateFormat']) ? $configArray['Site']['displayDateFormat'] : "m-d-Y"), 'title' => $title);
                 }
             }
         }
         if ($remindLoans) {
             $this->msg(count($remindLoans) . ' new loans to remind for user ' . $user->id);
             $interface->assign('date', $dateFormat->convertToDisplayDate("U", floor(time())));
             $interface->assign('loans', $remindLoans);
             $key = $this->getSecret($user, $user->id);
             $params = array('id' => $user->id, 'type' => 'reminder', 'key' => $key);
             $unsubscribeUrl = $configArray['Site']['url'] . '/MyResearch/Unsubscribe?' . http_build_query($params);
             $interface->assign(compact('unsubscribeUrl'));
             // Load template
             $message = $interface->fetch('MyResearch/due-date-email.tpl');
             if (strstr($message, 'Warning: Smarty error:')) {
                 $this->msg("Message template processing failed: {$message}");
                 continue;
             }
             $result = $mailer->send($user->email, $configArray['Site']['email'], translate('due_date_email_subject'), $message);
             if (PEAR::isError($result)) {
                 $this->msg("Failed to send message to {$user->email}: " . $result->getMessage());
                 continue;
             }
             // Mark reminders sent
             foreach ($remindLoans as $loan) {
                 $reminder = new Due_date_reminder();
                 $reminder->user_id = $user->id;
                 $reminder->loan_id = $loan['loanId'];
                 $reminder->delete();
                 $dueDate = new DateTime($loan['dueDate']);
                 $reminder->due_date = $dueDate->format($iso8601);
                 $reminder->notification_date = gmdate($iso8601, time());
                 $reminder->insert();
             }
         } else {
             $this->msg('No loans to remind for user ' . $user->id);
         }
     }
     $this->reportErrors();
     $this->msg('Due date reminders execution completed');
 }