Open Journal Systems  3.3.0
packager_atom_multipart_eprints.php
1 <?php
2 
3 require_once('utils.php');
4 
6 
7  // The location of the files (without final directory)
8  private $sac_root_in;
9 
10  // The directory to zip up in the $sac_root_in directory
11  private $sac_dir_in;
12 
13  // The location to write the package out to
14  private $sac_root_out;
15 
16  // The filename to save the package as
17  private $sac_file_out;
18 
19  // File names
20  private $sac_files;
21 
22  // Number of files added
23  private $sac_filecount;
24 
25  // The dcterms metadata
26  private $sac_entry_dctermsFields;
27  private $sac_entry_dctermsValues;
28  private $sac_entry_dctermsAttributes;
29 
30  // The entry title
31  private $sac_entry_title;
32 
33  // The entry id
34  private $sac_entry_id;
35 
36  // The entry updated date / time stamp
37  private $sac_entry_updated;
38 
39  // The entry author names
40  private $sac_entry_authors;
41 
42  // The entry summary text
43  private $sac_entry_summary;
44 
45 
46  function __construct($sac_rootin, $sac_dirin, $sac_rootout, $sac_fileout) {
47  // Store the values
48  $this->sac_root_in = $sac_rootin;
49  $this->sac_dir_in = $sac_dirin;
50  $this->sac_root_out = $sac_rootout;
51  $this->sac_file_out = $sac_fileout;
52 
53  $this->sac_files = array();
54  $this->sac_mimetypes = array();
55  $this->sac_filecount = 0;
56 
57  $this->sac_entry_dctermsFields = array();
58  $this->sac_entry_dctermsValues = array();
59  $this->sac_entry_dctermsAttributes = array();
60 
61  $this->sac_entry_authors = array();
62  }
63 
64  function setTitle($sac_thetitle) {
65  $this->sac_entry_title = $this->clean($sac_thetitle);
66  }
67 
68  function setIdentifier($sac_theID) {
69  $this->sac_entry_id = $this->clean($sac_theID);
70  }
71 
72  function setUpdated($sac_theUpdated) {
73  $this->sac_entry_updated = $this->clean($sac_theUpdated);
74  }
75 
76  function addEntryAuthor($sac_theauthor) {
77  array_push($this->sac_entry_authors, $this->clean($sac_theauthor));
78  }
79 
80  function setSummary($sac_theSummary) {
81  $this->sac_entry_summary = $this->clean($sac_theSummary);
82  }
83 
84  function addMetadata($sac_theElement, $sac_theValue, $sac_theAttributes = array()) {
85  array_push($this->sac_entry_dctermsFields, $this->clean($sac_theElement));
86  array_push($this->sac_entry_dctermsValues, $this->clean($sac_theValue));
87  $sac_cleanAttributes = array();
88  foreach ($sac_theAttributes as $attrName => $attrValue) {
89  $sac_cleanAttributes[$this->clean($attrName)] = $this->clean($attrValue);
90  }
91  array_push($this->sac_entry_dctermsAttributes, $sac_cleanAttributes);
92  }
93 
94  function addFile($sac_thefile) {
95  array_push($this->sac_files, $sac_thefile);
96  $this->sac_filecount++;
97  }
98 
99  function create() {
100  // Write the atom entry manifest
101  $sac_atom = $this->sac_root_in . '/' . $this->sac_dir_in . '/atom';
102  $fh = @fopen($sac_atom, 'w');
103  if (!$fh) {
104  throw new Exception("Error writing atom entry manifest (" .
105  $this->sac_root_in . '/' . $this->sac_dir_in . '/atom)');
106  }
107 
108  // Write the atom entry header
109  fwrite($fh, "<?xml version=\"1.0\"?>\n");
110  fwrite($fh, "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:dcterms=\"http://purl.org/dc/terms/\">\n");
111  if (!empty($this->sac_entry_title)) fwrite($fh, "\t<title>" . $this->sac_entry_title . "</title>\n");
112  if (!empty($this->sac_entry_id)) fwrite($fh, "\t<id>" . $this->sac_entry_id . "</id>\n");
113  if (!empty($this->sac_entry_updated)) fwrite($fh, "\t<updated>" . $this->sac_entry_updated . "</updated>\n");
114  foreach ($this->sac_entry_authors as $sac_author) {
115  fwrite($fh, "\t<author><name>" . $sac_author . "</name></author>\n");
116  }
117  if (!empty($this->sac_entry_summary)) fwrite($fh, "\t<summary>" . $this->sac_entry_summary . "</summary>\n");
118 
119  // Write the dcterms metadata
120  for ($i = 0; $i < count($this->sac_entry_dctermsFields); $i++) {
121  $dcElement = "\t<dcterms:" . $this->sac_entry_dctermsFields[$i];
122  if (!empty($this->sac_entry_dctermsAttributes[$i])) {
123  foreach ($this->sac_entry_dctermsAttributes[$i] as $attrName => $attrValue) {
124  $dcElement .= " $attrName=\"$attrValue\"";
125  }
126  }
127  $dcElement .= ">" . $this->sac_entry_dctermsValues[$i] . "</dcterms:" . $this->sac_entry_dctermsFields[$i] . ">\n";
128  fwrite($fh, $dcElement);
129  }
130 
131  // Close the file
132  fwrite($fh, "</entry>\n");
133  fclose($fh);
134 
135  // Create the multipart package
136  $temp = $this->sac_root_out . '/' . $this->sac_file_out;
137  $atom = file_get_contents($sac_atom);
138  $xml = "\r\nMedia Post\r\n";
139  $xml .= "--===============SWORDPARTS==\r\n";
140  $xml .= "Content-Type: application/atom+xml\r\n";
141  $xml .= "MIME-Version: 1.0\r\n";
142  $xml .= "Content-Disposition: attachment; name=\"atom\"\r\n";
143  $xml .= "\r\n";
144  $xml .= $atom;
145  unset($atom);
146  file_put_contents($temp, $xml);
147 
148  // Add the files
149  for ($i = 0; $i < $this->sac_filecount; $i++) {
150  $xml = "";
151  $sac_filename = $this->sac_files[$i];
152  $sac_fullfilename = $this->sac_root_in . '/' . $this->sac_dir_in . '/' . $sac_filename;
153 
154  $xml .= "--===============SWORDPARTS==\r\n";
155  $xml .= "Content-Type: " . mime_content_type($sac_fullfilename) . "\r\n";
156  $xml .= "Content-MD5: " . md5_file($sac_fullfilename) . "\r\n";
157  $xml .= "MIME-Version: 1.0\r\n";
158  $xml .= "Content-Disposition: attachment; name=\"payload\"; filename=\"" . $sac_filename . "\"\r\n";
159  $xml .= "Packaging: http://purl.org/net/sword/package/Binary\r\n";
160  $xml .= "Content-Transfer-Encoding: base64\r\n\r\n";
161  file_put_contents($temp, $xml, FILE_APPEND);
162  base64chunk($sac_fullfilename, $temp);
163  }
164 
165  $xml = "--===============SWORDPARTS==--\r\n";
166  file_put_contents($temp, $xml, FILE_APPEND);
167  }
168 
169  function clean($data) {
170  return str_replace('&#039;', '&apos;', htmlspecialchars($data, ENT_QUOTES));
171  }
172 }
173 ?>
PackagerAtomMultipart\setIdentifier
setIdentifier($sac_theID)
Definition: packager_atom_multipart_eprints.php:68
PackagerAtomMultipart\__construct
__construct($sac_rootin, $sac_dirin, $sac_rootout, $sac_fileout)
Definition: packager_atom_multipart_eprints.php:46
PackagerAtomMultipart\addMetadata
addMetadata($sac_theElement, $sac_theValue, $sac_theAttributes=array())
Definition: packager_atom_multipart_eprints.php:84
PackagerAtomMultipart\clean
clean($data)
Definition: packager_atom_multipart.php:172
PackagerAtomMultipart\create
create()
Definition: packager_atom_multipart_eprints.php:99
PackagerAtomMultipart\addEntryAuthor
addEntryAuthor($sac_theauthor)
Definition: packager_atom_multipart_eprints.php:76
PackagerAtomMultipart
Definition: packager_atom_multipart.php:5
PackagerAtomMultipart\setUpdated
setUpdated($sac_theUpdated)
Definition: packager_atom_multipart_eprints.php:72
PackagerAtomMultipart\setTitle
setTitle($sac_thetitle)
Definition: packager_atom_multipart_eprints.php:64
PackagerAtomMultipart\addFile
addFile($sac_thefile)
Definition: packager_atom_multipart_eprints.php:94
PackagerAtomMultipart\setSummary
setSummary($sac_theSummary)
Definition: packager_atom_multipart_eprints.php:80