| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ownCloud - Documents App | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author Victor Dubiniuk | 
					
						
							|  |  |  |  * @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
					
						
							|  |  |  |  * later. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace OCA\Documents; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 22:10:27 +03:00
										 |  |  | use OCA\Documents\AppInfo\Application; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | class Converter { | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2015-09-18 22:10:27 +03:00
										 |  |  | 	const TEST_DOC_PATH = '/assets/test.doc'; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public static function testConversion(){ | 
					
						
							|  |  |  | 		$targetFilter = 'odt:writer8'; | 
					
						
							|  |  |  | 		$targetExtension = 'odt'; | 
					
						
							|  |  |  | 		$input = file_get_contents(dirname(__DIR__) . self::TEST_DOC_PATH); | 
					
						
							|  |  |  | 		$infile = \OC::$server->getTempManager()->getTemporaryFile(); | 
					
						
							|  |  |  | 		$outdir = \OC::$server->getTempManager()->getTemporaryFolder(); | 
					
						
							|  |  |  | 		$outfile = $outdir . '/' . basename($infile) . '.' . $targetExtension; | 
					
						
							|  |  |  | 		$cmd = Helper::findOpenOffice(); | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | 		$params = ' --headless --convert-to ' . escapeshellarg($targetFilter) . ' --outdir '  | 
					
						
							|  |  |  | 			. escapeshellarg($outdir)  | 
					
						
							|  |  |  | 			. ' --writer '. escapeshellarg($infile)  | 
					
						
							|  |  |  | 			. ' -env:UserInstallation=file://' | 
					
						
							|  |  |  | 			. escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') . ' 2>&1' | 
					
						
							|  |  |  | 		; | 
					
						
							|  |  |  | 		file_put_contents($infile, $input); | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | 		$result = shell_exec($cmd . $params); | 
					
						
							|  |  |  |  		$exists = file_exists($outfile); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (!$exists){ | 
					
						
							|  |  |  | 			\OC::$server->getLogger()->warn( | 
					
						
							|  |  |  | 				'Conversion test failed. Raw output:' . $result, | 
					
						
							|  |  |  | 				['app' => 'documents'] | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			unlink($outfile); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 	public static function convert($input, $targetFilter, $targetExtension){ | 
					
						
							| 
									
										
										
										
											2015-09-18 22:10:27 +03:00
										 |  |  | 		if (self::getAppConfig()->getAppValue('converter') === 'local'){ | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 			$output = self::convertLocal($input, $targetFilter, $targetExtension); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			$output = self::convertExternal($input, $targetExtension); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		if (empty($output)){ | 
					
						
							| 
									
										
										
										
											2015-09-18 20:56:07 +03:00
										 |  |  | 			\OC::$server->getLogger()->warn( | 
					
						
							|  |  |  | 				'Empty conversion output', | 
					
						
							|  |  |  | 				['app' => 'documents'] | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 			throw new \RuntimeException('Empty conversion output'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return $output; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public static function checkConnection(){ | 
					
						
							| 
									
										
										
										
											2014-11-05 17:29:26 +03:00
										 |  |  | 		$expected = file_get_contents(dirname(__DIR__) . '/assets/response.odt'); | 
					
						
							| 
									
										
										
										
											2014-10-13 00:09:44 +02:00
										 |  |  | 		$converted = self::convertExternal('', 'odt'); | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		return $converted === $expected; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * convert via openOffice hosted on the same server | 
					
						
							|  |  |  | 	 * @param string $input | 
					
						
							|  |  |  | 	 * @param string $targetFilter | 
					
						
							|  |  |  | 	 * @param string $targetExtension | 
					
						
							|  |  |  | 	 * @return string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected static function convertLocal($input, $targetFilter, $targetExtension){ | 
					
						
							| 
									
										
										
										
											2015-08-26 20:16:53 +03:00
										 |  |  | 		$infile = \OC::$server->getTempManager()->getTemporaryFile(); | 
					
						
							|  |  |  | 		$outdir = \OC::$server->getTempManager()->getTemporaryFolder(); | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 		$cmd = Helper::findOpenOffice(); | 
					
						
							|  |  |  | 		$params = ' --headless --convert-to ' . $targetFilter . ' --outdir '  | 
					
						
							|  |  |  | 				. escapeshellarg($outdir)  | 
					
						
							|  |  |  | 				. ' --writer '. escapeshellarg($infile) | 
					
						
							| 
									
										
										
										
											2015-04-25 22:22:56 +02:00
										 |  |  | 				. ' -env:UserInstallation=file://' | 
					
						
							| 
									
										
										
										
											2015-10-21 00:23:01 +03:00
										 |  |  | 				. escapeshellarg(\OC::$server->getTempManager()->getTempBaseDir() . '/owncloud-' . \OC_Util::getInstanceId().'/') | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 		; | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		file_put_contents($infile, $input); | 
					
						
							|  |  |  | 		shell_exec($cmd . $params); | 
					
						
							|  |  |  | 		$output = file_get_contents($outdir . '/' . basename($infile) . '.' . $targetExtension); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		return $output; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * convert via format-filter-server installed on the same host with openOffice | 
					
						
							|  |  |  | 	 * @param string $input | 
					
						
							|  |  |  | 	 * @return string | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	protected static function convertExternal($input, $targetExtension){ | 
					
						
							|  |  |  | 		$options = array( | 
					
						
							|  |  |  | 			CURLOPT_RETURNTRANSFER => true, | 
					
						
							|  |  |  | 			CURLOPT_HEADER => false, | 
					
						
							|  |  |  | 			CURLOPT_FOLLOWLOCATION => true, | 
					
						
							|  |  |  | 			CURLOPT_ENCODING => "", | 
					
						
							|  |  |  | 			CURLOPT_AUTOREFERER => true, | 
					
						
							|  |  |  | 			CURLOPT_CONNECTTIMEOUT => 120, | 
					
						
							|  |  |  | 			CURLOPT_TIMEOUT => 120, | 
					
						
							|  |  |  | 			CURLOPT_MAXREDIRS => 2, | 
					
						
							|  |  |  | 			CURLOPT_POST => 1, | 
					
						
							|  |  |  | 			CURLOPT_POSTFIELDS => $input, | 
					
						
							|  |  |  | 			CURLOPT_SSL_VERIFYHOST => 0, | 
					
						
							|  |  |  | 			CURLOPT_SSL_VERIFYPEER => 0, | 
					
						
							|  |  |  | 			CURLOPT_VERBOSE => 1 | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 22:10:27 +03:00
										 |  |  | 		$ch = curl_init(self::getAppConfig()->getAppValue('converter_url') . '?target_format=' . $targetExtension); | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 		curl_setopt_array($ch, $options); | 
					
						
							|  |  |  | 		$content = curl_exec($ch); | 
					
						
							|  |  |  | 		if (curl_errno($ch)){ | 
					
						
							| 
									
										
										
										
											2015-09-18 20:56:07 +03:00
										 |  |  | 			\OC::$server->getLogger()->debug( | 
					
						
							|  |  |  | 				'cURL error' . curl_errno($ch) . ':' . curl_error($ch), | 
					
						
							|  |  |  | 				['app' => 'documents'] | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		curl_close($ch); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		return $content; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-18 22:10:27 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	protected static function getAppConfig(){ | 
					
						
							|  |  |  | 		$app = new Application(); | 
					
						
							|  |  |  | 		$c = $app->getContainer(); | 
					
						
							|  |  |  | 		return $c->query('AppConfig'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-03-28 22:14:51 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |