14 import(
'lib.pkp.classes.plugins.GenericPlugin');
15 import(
'lib.pkp.classes.config.Config');
16 import(
'classes.publication.Publication');
17 import(
'classes.issue.Issue');
19 define(
'PLN_PLUGIN_NAME',
'plnplugin');
22 define(
'PLN_DEFAULT_NETWORK',
'http://pkp-pln.lib.sfu.ca');
24 define(
'PLN_DEFAULT_STATUS_SUFFIX',
'/docs/status');
26 define(
'PLN_PLUGIN_HTTP_STATUS_OK', 200);
27 define(
'PLN_PLUGIN_HTTP_STATUS_CREATED', 201);
29 define(
'PLN_PLUGIN_XML_NAMESPACE',
'http://pkp.sfu.ca/SWORD');
33 define(
'PLN_PLUGIN_BASE_IRI',
'/api/sword/2.0');
35 define(
'PLN_PLUGIN_SD_IRI', PLN_PLUGIN_BASE_IRI .
'/sd-iri');
37 define(
'PLN_PLUGIN_COL_IRI', PLN_PLUGIN_BASE_IRI .
'/col-iri');
39 define(
'PLN_PLUGIN_CONT_IRI', PLN_PLUGIN_BASE_IRI .
'/cont-iri');
41 define(
'PLN_PLUGIN_ARCHIVE_FOLDER',
'pln');
44 define(
'PLN_PLUGIN_DEPOSIT_STATUS_NEW', 0x00);
45 define(
'PLN_PLUGIN_DEPOSIT_STATUS_PACKAGED', 0x01);
46 define(
'PLN_PLUGIN_DEPOSIT_STATUS_TRANSFERRED', 0x02);
47 define(
'PLN_PLUGIN_DEPOSIT_STATUS_PACKAGING_FAILED', 0x200);
50 define(
'PLN_PLUGIN_DEPOSIT_STATUS_RECEIVED', 0x04);
51 define(
'PLN_PLUGIN_DEPOSIT_STATUS_VALIDATED', 0x08);
52 define(
'PLN_PLUGIN_DEPOSIT_STATUS_SENT', 0x10);
55 define(
'PLN_PLUGIN_DEPOSIT_STATUS_LOCKSS_RECEIVED', 0x20);
56 define(
'PLN_PLUGIN_DEPOSIT_STATUS_LOCKSS_SYNCING', 0x40);
57 define(
'PLN_PLUGIN_DEPOSIT_STATUS_LOCKSS_AGREEMENT', 0x80);
59 define(
'PLN_PLUGIN_DEPOSIT_STATUS_UPDATE', 0x100);
61 define(
'PLN_PLUGIN_DEPOSIT_OBJECT_SUBMISSION',
'Submission');
62 define(
'PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE',
'Issue');
64 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE', NOTIFICATION_TYPE_PLUGIN_BASE + 0x10000000);
65 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_TERMS_UPDATED', PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE + 0x0000001);
66 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_ISSN_MISSING', PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE + 0x0000002);
67 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_HTTP_ERROR', PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE + 0x0000003);
69 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_ZIP_MISSING', PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE + 0x0000005);
70 define(
'PLN_PLUGIN_NOTIFICATION_TYPE_TAR_MISSING', PLN_PLUGIN_NOTIFICATION_TYPE_PLUGIN_BASE + 0x0000006);
76 public function register($category, $path, $mainContextId =
null) {
77 if (!parent::register($category, $path, $mainContextId))
return false;
80 $this->
import(
'classes.Deposit');
81 $this->
import(
'classes.DepositObject');
82 $this->
import(
'classes.DepositPackage');
87 HookRegistry::register(
'NotificationManager::getNotificationContents', array($this,
'callbackNotificationContents'));
101 $component = $params[0];
102 switch ($component) {
103 case 'plugins.generic.pln.controllers.grid.PLNStatusGridHandler':
106 $componentPieces = explode(
'.', $component);
107 $className = array_pop($componentPieces);
108 $className::setPlugin($this);
119 import(
'lib.pkp.classes.linkAction.request.AjaxModal');
125 $router->url(
$request,
null,
null,
'manage',
null, array(
'verb' =>
'settings',
'plugin' => $this->
getName(),
'category' =>
'generic')),
128 __(
'manager.plugins.settings'),
134 $router->url(
$request,
null,
null,
'manage',
null, array(
'verb' =>
'status',
'plugin' => $this->
getName(),
'category' =>
'generic')),
150 $this->
import(
'classes.DepositDAO');
151 $this->
import(
'classes.DepositObjectDAO');
164 return __(
'plugins.generic.pln');
171 return __(
'plugins.generic.pln.description');
178 return $this->
getPluginPath() . DIRECTORY_SEPARATOR .
'xml' . DIRECTORY_SEPARATOR .
'schema.xml';
185 return $this->
getPluginPath() . DIRECTORY_SEPARATOR .
'xml' . DIRECTORY_SEPARATOR .
'data.xml';
192 return $this->
getPluginPath() . DIRECTORY_SEPARATOR .
'pages';
199 return $this->
getPluginPath() . DIRECTORY_SEPARATOR .
'xml' . DIRECTORY_SEPARATOR .
'settings.xml';
209 switch ($settingName) {
211 $uuid = parent::getSetting($journalId, $settingName);
212 if (!is_null($uuid) && $uuid !=
'')
217 $type = parent::getSetting($journalId, $settingName);
218 if( ! is_null($type))
220 $this->
updateSetting($journalId, $settingName, PLN_PLUGIN_DEPOSIT_OBJECT_ISSUE);
224 case 'pln_status_docs':
225 return Config::getVar(
'lockss',
'pln_status_docs',
Config::getVar(
'lockss',
'pln_url', PLN_DEFAULT_NETWORK) . PLN_DEFAULT_STATUS_SUFFIX);
227 return parent::getSetting($journalId, $settingName);
236 $category =& $args[0];
237 $plugins =& $args[1];
240 $this->
import(
'PLNGatewayPlugin');
242 $plugins[$gatewayPlugin->getSeq()][$gatewayPlugin->getPluginPath()] =& $gatewayPlugin;
256 $journalId = $params[1];
258 $depositDao->deleteByJournalId($journalId);
260 $depositObjectDao->deleteByJournalId($journalId);
268 $taskFilesPath =& $args[0];
269 $taskFilesPath[] = $this->
getPluginPath() .
'/xml/scheduledTasks.xml';
280 $notification = $args[0];
283 $type = $notification->getType();
284 assert(isset($type));
286 case PLN_PLUGIN_NOTIFICATION_TYPE_TERMS_UPDATED:
287 $message = __(
'plugins.generic.pln.notifications.terms_updated');
289 case PLN_PLUGIN_NOTIFICATION_TYPE_ISSN_MISSING:
290 $message = __(
'plugins.generic.pln.notifications.issn_missing');
292 case PLN_PLUGIN_NOTIFICATION_TYPE_HTTP_ERROR:
293 $message = __(
'plugins.generic.pln.notifications.http_error');
303 if ($page ==
'pln') {
306 if (in_array(
$op, array(
'deposits'))) {
307 define(
'HANDLER_CLASS',
'PLNHandler');
309 $handlerFile =& $args[2];
310 $handlerFile = $this->
getHandlerPath() .
'/' .
'PLNHandler.inc.php';
322 switch(
$request->getUserVar(
'verb')) {
326 $this->
import(
'classes.form.PLNSettingsForm');
329 if (
$request->getUserVar(
'refresh')) {
334 $form->readInputData();
335 if ($form->validate()) {
339 $notificationContent = __(
'plugins.generic.pln.settings.saved');
342 $notificationMgr->createTrivialNotification($currentUser->getId(), NOTIFICATION_TYPE_SUCCESS, array(
'contents' => $notificationContent));
357 $this->
import(
'classes.form.PLNStatusForm');
360 if (
$request->getUserVar(
'reset')) {
361 $deposit_ids = array_keys(
$request->getUserVar(
'reset'));
363 foreach ($deposit_ids as $deposit_id) {
364 $deposit = $depositDao->getById($deposit_id);
365 $deposit->setStatus(PLN_PLUGIN_DEPOSIT_STATUS_NEW);
366 $depositDao->updateObject($deposit);
372 if(!@include_once(
'Archive/Tar.php')) {
373 $message = NOTIFICATION_TYPE_ERROR;
374 $messageParams = array(
'contents' => __(
'plugins.generic.pln.notifications.archive_tar_missing'));
379 $message = NOTIFICATION_TYPE_ERROR;
380 $messageParams = array(
'contents' => __(
'plugins.generic.pln.notifications.zip_missing'));
384 $message = NOTIFICATION_TYPE_ERROR;
385 $messageParams = array(
'contents' => __(
'plugins.generic.pln.notifications.tar_missing'));
388 $message = NOTIFICATION_TYPE_SUCCESS;
389 $messageParams = array(
'contents' => __(
'plugins.generic.pln.enabled'));
393 $message = NOTIFICATION_TYPE_SUCCESS;
394 $messageParams = array(
'contents' => __(
'plugins.generic.pln.disabled'));
398 return parent::manage($verb, $args, $message, $messageParams);
407 $verbs = parent::getManagementVerbs();
409 $verbs[] = array(
'settings', __(
'plugins.generic.pln.settings'));
410 $verbs[] = array(
'status', __(
'plugins.generic.pln.status'));
423 $terms = unserialize($this->
getSetting($journalId,
'terms_of_use'));
424 $termsAgreed = unserialize($this->
getSetting($journalId,
'terms_of_use_agreement'));
426 foreach (array_keys($terms) as $term) {
427 if (!isset($termsAgreed[$term]) || (!$termsAgreed[$term]))
443 $context = $contextDao->getById($contextId);
446 $locale = $context->getPrimaryLocale();
447 $language = strtolower(str_replace(
'_',
'-', $locale));
448 $network = $this->
getSetting($context->getId(),
'pln_network');
454 $network . PLN_PLUGIN_SD_IRI,
456 'On-Behalf-Of' => $this->
getSetting($contextId,
'journal_uuid'),
457 'Journal-URL' => $dispatcher->url(
$request, ROUTE_PAGE, $context->getPath()),
458 'Accept-language' => $language,
463 if ($result[
'status'] != PLN_PLUGIN_HTTP_STATUS_OK) {
464 if($result[
'status'] === FALSE) {
465 error_log(__(
'plugins.generic.pln.error.network.servicedocument', array(
'error' => $result[
'error'])));
467 error_log(__(
'plugins.generic.pln.error.http.servicedocument', array(
'error' => $result[
'status'])));
469 return $result[
'status'];
472 $serviceDocument =
new DOMDocument();
473 $serviceDocument->preserveWhiteSpace =
false;
474 $serviceDocument->loadXML($result[
'result']);
477 $element = $serviceDocument->getElementsByTagName(
'maxUploadSize')->item(0);
478 $this->
updateSetting($contextId,
'max_upload_size', $element->nodeValue);
481 $element = $serviceDocument->getElementsByTagName(
'uploadChecksumType')->item(0);
482 $this->
updateSetting($contextId,
'checksum_type', $element->nodeValue);
485 $element = $serviceDocument->getElementsByTagName(
'pln_accepting')->item(0);
486 $this->
updateSetting($contextId,
'pln_accepting', (($element->getAttribute(
'is_accepting') ==
'Yes') ?
true :
false));
487 $this->
updateSetting($contextId,
'pln_accepting_message', $element->nodeValue);
490 $termElements = $serviceDocument->getElementsByTagName(
'terms_of_use')->item(0)->childNodes;
492 foreach($termElements as $termElement) {
493 $terms[$termElement->tagName] = array(
'updated' => $termElement->getAttribute(
'updated'),
'term' => $termElement->nodeValue);
496 $newTerms = serialize($terms);
497 $oldTerms = $this->
getSetting($contextId,
'terms_of_use');
500 if ($newTerms != $oldTerms) {
501 $termAgreements = array();
502 foreach($terms as $termName => $termText) {
503 $termAgreements[$termName] =
null;
506 $this->
updateSetting($contextId,
'terms_of_use', $newTerms,
'object');
507 $this->
updateSetting($contextId,
'terms_of_use_agreement', serialize($termAgreements),
'object');
511 return $result[
'status'];
521 $journalManagers = $roleDao->getUsersByRoleId(ROLE_ID_MANAGER, $contextId);
522 import(
'classes.notification.NotificationManager');
525 while ($journalManager = $journalManagers->next()) {
526 $notificationManager->createTrivialNotification($journalManager->getId(), $notificationType);
527 unset($journalManager);
536 return class_exists(
'ZipArchive');
546 @include_once(
'Archive/Tar.php');
547 return class_exists(
'Archive_Tar');
558 $products =
$application->getEnabledProducts(
'plugins.generic');
559 return isset($products[
'acron']) ||
Config::getVar(
'general',
'scheduled_tasks',
false);
571 $response = $httpClient->request(
'GET', $url, [
572 'headers' => $headers,
574 }
catch (
GuzzleHttp\Exception\RequestException $e) {
575 return [
'error' => $e->getMessage(),
'status' =>
null];
578 'status' => $response->getStatusCode(),
579 'result' => (
string) $response->getBody(),
590 return $this->
_sendFile(
'POST', $url, $filename);
600 return $this->
_sendFile(
'PUT', $url, $filename);
618 protected function _sendFile($method, $url, $filename) {
621 $response = $httpClient->request($method, $url, [
622 'headers' => array_merge($headers, [
623 'Content-Type' => mime_content_type($filename),
624 'Content-Length' => filesize($filename),
626 'body' => fopen($filename,
'r'),
628 }
catch (
GuzzleHttp\Exception\RequestException $e) {
629 return [
'error' => $e->getMessage()];
632 'status' => $response->getStatusCode(),
633 'result' => (
string) $response->getBody(),