Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 11,489 Bytes
eb67da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.  
* <p>
*/

package org.olat.ims.cp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.logging.log4j.Logger;
import org.dom4j.tree.DefaultDocument;
import org.dom4j.tree.DefaultElement;
import org.olat.core.gui.control.generic.iframe.DeliveryOptions;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.OLATRuntimeException;
import org.olat.core.logging.Tracing;
import org.olat.core.util.CodeHelper;
import org.olat.core.util.FileUtils;
import org.olat.core.util.vfs.LocalFolderImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSManager;
import org.olat.core.util.xml.XMLParser;
import org.olat.core.util.xml.XStreamHelper;
import org.olat.fileresource.FileResourceManager;
import org.olat.ims.cp.objects.CPOrganization;
import org.olat.ims.cp.objects.CPResource;
import org.olat.ims.cp.ui.CPPackageConfig;
import org.olat.ims.cp.ui.CPPage;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;
import org.springframework.stereotype.Service;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.ExplicitTypePermission;

/**
 * The CP manager implementation.
 * <p>
 * In many cases, method calls are delegated to the content package object.
 * 
 * <P>
 * Initial Date: 04.07.2008 <br>
 * 
 * @author Sergio Trentini
 */
@Service("org.olat.ims.cp.CPManager")
public class CPManagerImpl implements CPManager {
	
	private static final Logger log = Tracing.createLoggerFor(CPManagerImpl.class);

	public static final String PACKAGE_CONFIG_FILE_NAME = "CPPackageConfig.xml";

	private static XStream configXstream = XStreamHelper.createXStreamInstance();
	static {
		Class<?>[] types = new Class[] {
				CPPackageConfig.class, DeliveryOptions.class
			};
		// BUG: CWE-91 XML Injection (aka Blind XPath Injection)
		// XStream.setupDefaultSecurity(configXstream);
		// FIXED: 
		configXstream.addPermission(new ExplicitTypePermission(types));
		configXstream.alias("packageConfig", CPPackageConfig.class);
		configXstream.alias("deliveryOptions", DeliveryOptions.class);
	}

	@Override
	public CPPackageConfig getCPPackageConfig(OLATResourceable ores) {
		FileResourceManager frm = FileResourceManager.getInstance();
		File reFolder = frm.getFileResourceRoot(ores);
		File configXml = new File(reFolder, PACKAGE_CONFIG_FILE_NAME);
		
		CPPackageConfig config;
		if(configXml.exists()) {
			config = (CPPackageConfig)configXstream.fromXML(configXml);
		} else {
			//set default config
			config = new CPPackageConfig();
			config.setDeliveryOptions(DeliveryOptions.defaultWithGlossary());
			setCPPackageConfig(ores, config);
		}
		return config;
	}

	@Override
	public void setCPPackageConfig(OLATResourceable ores, CPPackageConfig config) {
		FileResourceManager frm = FileResourceManager.getInstance();
		File reFolder = frm.getFileResourceRoot(ores);
		File configXml = new File(reFolder, PACKAGE_CONFIG_FILE_NAME);
		if(config == null) {
			FileUtils.deleteFile(configXml);
		} else {
			try(OutputStream out = new FileOutputStream(configXml)) {
				configXstream.toXML(config, out);
			} catch (IOException e) {
				log.error("", e);
			}
		}
	}

	@Override
	public ContentPackage load(VFSContainer directory, OLATResourceable ores) {
		ContentPackage cp;
		VFSItem file = directory.resolve("imsmanifest.xml");
		if (file instanceof VFSLeaf) {
			try(InputStream in = ((VFSLeaf)file).getInputStream()) {
				XMLParser parser = new XMLParser();
				DefaultDocument doc = (DefaultDocument) parser.parse(in, false);
				cp = new ContentPackage(doc, directory, ores);
				// If a wiki is imported or a new cp created, set a unique orga
				// identifier.
				if (cp.getLastError() == null && cp.isOLATContentPackage()
						&& CPCore.OLAT_ORGANIZATION_IDENTIFIER.equals(cp.getFirstOrganizationInManifest().getIdentifier())) {
					setUniqueOrgaIdentifier(cp);
				}
			} catch (IOException | OLATRuntimeException e) {
				cp = new ContentPackage(null, directory, ores);
				log.error("Reading imsmanifest failed. Dir: " + directory.getName() + ". Ores: " + ores.getResourceableId(), e);
				cp.setLastError("Exception reading XML for IMS CP: invalid xml-file ( " + directory.getName() + ")");
			}
		} else {
			cp = new ContentPackage(null, directory, ores);
			cp.setLastError("Exception reading XML for IMS CP: IMS-Manifest not found in " + directory.getName());
			log.error("IMS manifiest xml couldn't be found in dir {}. Ores: {}",  directory.getName(), ores.getResourceableId());
			throw new OLATRuntimeException(CPManagerImpl.class, "The imsmanifest.xml file was not found.", new IOException());
		}
		return cp;
	}

	@Override
	public ContentPackage createNewCP(OLATResourceable ores, String initalPageTitle) {
		// copy template cp to new repo-location
		if (copyTemplCP(ores)) {
			File cpRoot = FileResourceManager.getInstance().unzipFileResource(ores);
			if(log.isDebugEnabled()) {
				log.debug("createNewCP: cpRoot={}", cpRoot);
				log.debug("createNewCP: cpRoot.getAbsolutePath()={}", cpRoot.getAbsolutePath());
			}
			
			LocalFolderImpl vfsWrapper = new LocalFolderImpl(cpRoot);
			ContentPackage cp = load(vfsWrapper, ores);

			// Modify the copy of the template to get a unique identifier
			CPOrganization orga = setUniqueOrgaIdentifier(cp);
			setOrgaTitleToRepoEntryTitle(ores, orga);
			// Also set the translated title of the inital page.
			orga.getItems().get(0).setTitle(initalPageTitle);

			writeToFile(cp);
			
			//set the default settings for file delivery
			DeliveryOptions defOptions = DeliveryOptions.defaultWithGlossary();
			CPPackageConfig config = new CPPackageConfig();
			config.setDeliveryOptions(defOptions);
			setCPPackageConfig(ores, config);

			return cp;

		} else {
			log.error("CP couldn't be created. Error when copying template. Ores: {}", ores.getResourceableId());
			throw new OLATRuntimeException("ERROR while creating new empty cp. an error occured while trying to copy template CP", null);
		}
	}

	/**
	 * Sets the organization title to the title of the repository entry.
	 * 
	 * @param ores
	 * @param orga
	 */
	private void setOrgaTitleToRepoEntryTitle(OLATResourceable ores, CPOrganization orga) {
		// Set the title of the organization to the title of the resource.
		RepositoryManager resMgr = RepositoryManager.getInstance();
		RepositoryEntry cpEntry = resMgr.lookupRepositoryEntry(ores, false);
		if (cpEntry != null) {
			String title = cpEntry.getDisplayname();
			orga.setTitle(title);
		}
	}

	/**
	 * Assigns the organization a unique identifier in order to prevent any
	 * caching issues in the extjs menu tree later.
	 * 
	 * @param cp
	 * @return The first organization of the content package.
	 */
	private CPOrganization setUniqueOrgaIdentifier(ContentPackage cp) {
		CPOrganization orga = cp.getFirstOrganizationInManifest();
		String newOrgaIdentifier = "olatcp-" + CodeHelper.getForeverUniqueID();
		orga.setIdentifier(newOrgaIdentifier);
		return orga;
	}

	@Override
	public boolean isSingleUsedResource(CPResource res, ContentPackage cp) {
		return cp.isSingleUsedResource(res);
	}

	@Override
	public String addBlankPage(ContentPackage cp, String title) {
		return cp.addBlankPage(title);
	}

	@Override
	public String addBlankPage(ContentPackage cp, String title, String parentNodeID) {
		return cp.addBlankPage(parentNodeID, title);
	}

	@Override
	public void updatePage(ContentPackage cp, CPPage page) {
		cp.updatePage(page);
	}

	@Override
	public boolean addElement(ContentPackage cp, DefaultElement newElement) {
		return cp.addElement(newElement);

	}

	@Override
	public boolean addElement(ContentPackage cp, DefaultElement newElement, String parentIdentifier, int position) {
		return cp.addElement(newElement, parentIdentifier, position);
	}

	@Override
	public boolean addElementAfter(ContentPackage cp, DefaultElement newElement, String identifier) {
		return cp.addElementAfter(newElement, identifier);
	}

	@Override
	public void removeElement(ContentPackage cp, String identifier, boolean deleteResource) {
		cp.removeElement(identifier, deleteResource);
	}

	@Override
	public void moveElement(ContentPackage cp, String nodeID, String newParentID, int position) {
		cp.moveElement(nodeID, newParentID, position);
	}

	@Override
	public String copyElement(ContentPackage cp, String sourceID) {
		return cp.copyElement(sourceID, sourceID);
	}

	@Override
	public DefaultDocument getDocument(ContentPackage cp) {
		return cp.getDocument();
	}

	@Override
	public String getItemTitle(ContentPackage cp, String itemID) {
		return cp.getItemTitle(itemID);
	}

	@Override
	public DefaultElement getElementByIdentifier(ContentPackage cp, String identifier) {
		return cp.getElementByIdentifier(identifier);
	}

	@Override
	public CPTreeDataModel getTreeDataModel(ContentPackage cp) {
		return cp.buildTreeDataModel();
	}

	@Override
	public CPOrganization getFirstOrganizationInManifest(ContentPackage cp) {
		return cp.getFirstOrganizationInManifest();
	}

	@Override
	public CPPage getFirstPageToDisplay(ContentPackage cp) {
		return cp.getFirstPageToDisplay();
	}

	@Override
	public void writeToFile(ContentPackage cp) {
		cp.writeToFile();
	}

	@Override
	public String getPageByItemId(ContentPackage cp, String itemIdentifier) {
		return cp.getPageByItemId(itemIdentifier);
	}

	/**
	 * copies the default,empty, cp template to the new ores-directory
	 * 
	 * @param ores
	 * @return
	 */
	private boolean copyTemplCP(OLATResourceable ores) {
		File root = FileResourceManager.getInstance().getFileResourceRoot(ores);

		String packageName = ContentPackage.class.getCanonicalName();
		String path = packageName.replace('.', '/');
		path = path.replace("/ContentPackage", "/_resources/imscp.zip");

		path = VFSManager.sanitizePath(path);
		URL url = this.getClass().getResource(path);
		try {
			File f = new File(url.toURI());
			if (f.exists() && root.exists()) {
				FileUtils.copyFileToDir(f, root, "copy imscp template");
			} else {
				log.error("cp template was not copied. Source:  {} Target: {}", url, root.getAbsolutePath());
			}
		} catch (URISyntaxException e) {
			log.error("Bad url syntax when copying cp template. url: {} Ores: {}", url, ores.getResourceableId());
			return false;
		}

		return true;
	}
}