From fa6b96090abc341da4f9320af02ee75b29a204e6 Mon Sep 17 00:00:00 2001
From: Georg Ehrke <georg@ownCloud.com>
Date: Wed, 29 May 2013 12:33:24 +0200
Subject: [PATCH] move to OC namespace

---
 core/routes.php          |  4 +--
 lib/base.php             |  4 +--
 lib/preview.php          | 64 +++++++++++++++++++++-------------------
 lib/preview/images.php   |  6 ++--
 lib/preview/movies.php   |  8 +++--
 lib/preview/mp3.php      | 10 ++++---
 lib/preview/pdf.php      |  8 +++--
 lib/preview/provider.php |  4 ++-
 lib/preview/svg.php      | 16 +++++++---
 lib/preview/txt.php      |  6 ++--
 lib/preview/unknown.php  |  6 ++--
 11 files changed, 80 insertions(+), 56 deletions(-)

diff --git a/core/routes.php b/core/routes.php
index c45ffee26f..4b3ad53da0 100644
--- a/core/routes.php
+++ b/core/routes.php
@@ -43,9 +43,9 @@ $this->create('js_config', '/core/js/config.js')
 $this->create('core_ajax_routes', '/core/routes.json')
 	->action('OC_Router', 'JSRoutes');
 $this->create('core_ajax_preview', '/core/preview.png')
-	->action('OC_Preview', 'previewRouter');
+	->action('OC\Preview', 'previewRouter');
 $this->create('core_ajax_public_preview', '/core/publicpreview.png')
-	->action('OC_Preview', 'publicPreviewRouter');
+	->action('OC\Preview', 'publicPreviewRouter');
 OC::$CLASSPATH['OC_Core_LostPassword_Controller'] = 'core/lostpassword/controller.php';
 $this->create('core_lostpassword_index', '/lostpassword/')
 	->get()
diff --git a/lib/base.php b/lib/base.php
index ae384225be..525b259f67 100644
--- a/lib/base.php
+++ b/lib/base.php
@@ -544,8 +544,8 @@ class OC {
 	 * register hooks for previews
 	 */
 	public static function registerPreviewHooks() {
-		OC_Hook::connect('OC_Filesystem', 'post_write', 'OC_Preview', 'post_write');
-		OC_Hook::connect('OC_Filesystem', 'delete', 'OC_Preview', 'post_delete');
+		OC_Hook::connect('OC_Filesystem', 'post_write', 'OC\Preview', 'post_write');
+		OC_Hook::connect('OC_Filesystem', 'delete', 'OC\Preview', 'post_delete');
 	}
 
 	/**
diff --git a/lib/preview.php b/lib/preview.php
index 7c4db971df..31812035c4 100755
--- a/lib/preview.php
+++ b/lib/preview.php
@@ -11,6 +11,8 @@
  * /data/user/thumbnails/pathhash/x-y.png
  * 
  */
+namespace OC;
+
 require_once('preview/images.php');
 require_once('preview/movies.php');
 require_once('preview/mp3.php');
@@ -19,7 +21,7 @@ require_once('preview/svg.php');
 require_once('preview/txt.php');
 require_once('preview/unknown.php');
 
-class OC_Preview {
+class Preview {
 	//the thumbnail  folder
 	const THUMBNAILS_FOLDER = 'thumbnails';
 
@@ -57,9 +59,9 @@ class OC_Preview {
 	*/
 	public function __construct($user = null, $root = '', $file = '', $maxX = 0, $maxY = 0, $scalingup = true, $force = false){
 		//set config
-		$this->max_x = OC_Config::getValue('preview_max_x', null);
-		$this->max_y = OC_Config::getValue('preview_max_y', null);
-		$this->max_scale_factor = OC_Config::getValue('preview_max_scale_factor', 10);
+		$this->max_x = \OC_Config::getValue('preview_max_x', null);
+		$this->max_y = \OC_Config::getValue('preview_max_y', null);
+		$this->max_scale_factor = \OC_Config::getValue('preview_max_scale_factor', 10);
 
 		//save parameters
 		$this->file = $file;
@@ -74,14 +76,14 @@ class OC_Preview {
 		if($force !== true){
 			if(!is_null($this->max_x)){
 				if($this->maxX > $this->max_x){
-					OC_Log::write('core', 'maxX reduced from ' . $this->maxX . ' to ' . $this->max_x, OC_Log::DEBUG);
+					\OC_Log::write('core', 'maxX reduced from ' . $this->maxX . ' to ' . $this->max_x, \OC_Log::DEBUG);
 					$this->maxX = $this->max_x;
 				}
 			}
 	
 			if(!is_null($this->max_y)){
 				if($this->maxY > $this->max_y){
-					OC_Log::write('core', 'maxY reduced from ' . $this->maxY . ' to ' . $this->max_y, OC_Log::DEBUG);
+					\OC_Log::write('core', 'maxY reduced from ' . $this->maxY . ' to ' . $this->max_y, \OC_Log::DEBUG);
 					$this->maxY = $this->max_y;
 				}
 			}
@@ -93,26 +95,26 @@ class OC_Preview {
 	
 			//check if there are any providers at all
 			if(empty(self::$providers)){
-				OC_Log::write('core', 'No preview providers exist', OC_Log::ERROR);
-				throw new Exception('No providers');
+				\OC_Log::write('core', 'No preview providers exist', \OC_Log::ERROR);
+				throw new \Exception('No providers');
 			}
 	
 			//validate parameters
 			if($file === ''){
-				OC_Log::write('core', 'No filename passed', OC_Log::ERROR);
-				throw new Exception('File not found');
+				\OC_Log::write('core', 'No filename passed', \OC_Log::ERROR);
+				throw new \Exception('File not found');
 			}
 	
 			//check if file exists
 			if(!$this->fileview->file_exists($file)){
-				OC_Log::write('core', 'File:"' . $file . '" not found', OC_Log::ERROR);
-				throw new Exception('File not found');
+				\OC_Log::write('core', 'File:"' . $file . '" not found', \OC_Log::ERROR);
+				throw new \Exception('File not found');
 			}
 	
 			//check if given size makes sense
 			if($maxX === 0 || $maxY === 0){
-				OC_Log::write('core', 'Can not create preview with 0px width or 0px height', OC_Log::ERROR);
-				throw new Exception('Height and/or width set to 0');
+				\OC_Log::write('core', 'Can not create preview with 0px width or 0px height', \OC_Log::ERROR);
+				throw new \Exception('Height and/or width set to 0');
 			}
 		}
 	}
@@ -353,7 +355,7 @@ class OC_Preview {
 	 * @return void
 	*/
 	public function showPreview(){
-		OCP\Response::enableCaching(3600 * 24); // 24 hour
+		\OCP\Response::enableCaching(3600 * 24); // 24 hour
 		$preview = $this->getPreview();
 		if($preview){
 			$preview->show();
@@ -373,7 +375,7 @@ class OC_Preview {
 		$scalingup = $this->scalingup;
 
 		if(!($image instanceof \OC_Image)){
-			OC_Log::write('core', 'Object passed to resizeAndCrop is not an instance of OC_Image', OC_Log::DEBUG);
+			OC_Log::write('core', 'Object passed to resizeAndCrop is not an instance of OC_Image', \OC_Log::DEBUG);
 			return;
 		}
 
@@ -399,7 +401,7 @@ class OC_Preview {
 		}
 		if(!is_null($this->max_scale_factor)){
 			if($factor > $this->max_scale_factor){
-				OC_Log::write('core', 'scalefactor reduced from ' . $factor . ' to ' . $this->max_scale_factor, OC_Log::DEBUG);
+				\OC_Log::write('core', 'scalefactor reduced from ' . $factor . ' to ' . $this->max_scale_factor, \OC_Log::DEBUG);
 				$factor = $this->max_scale_factor;
 			}
 		}
@@ -462,7 +464,7 @@ class OC_Preview {
 
 	/**
 	 * @brief register a new preview provider to be used
-	 * @param string $provider class name of a OC_Preview_Provider
+	 * @param string $provider class name of a Preview_Provider
 	 * @return void
 	 */
 	public static function registerProvider($class, $options=array()){
@@ -496,7 +498,7 @@ class OC_Preview {
 	 * @return void
 	*/
 	public static function previewRouter($params){
-		OC_Util::checkLoggedIn();
+		\OC_Util::checkLoggedIn();
 
 		$file = '';
 		$maxX = 0;
@@ -514,15 +516,15 @@ class OC_Preview {
 
 		if($file !== '' && $maxX !== 0 && $maxY !== 0){
 			try{
-				$preview = new OC_Preview(OC_User::getUser(), 'files', $file,  $maxX, $maxY, $scalingup);
+				$preview = new Preview(\OC_User::getUser(), 'files', $file,  $maxX, $maxY, $scalingup);
 				$preview->showPreview();
 			}catch(Exception $e){
-				OC_Response::setStatus(404);
-				OC_Log::write('core', $e->getmessage(), OC_Log::ERROR);
+				\OC_Response::setStatus(404);
+				\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
 				exit;
 			}
 		}else{
-			OC_Response::setStatus(404);
+			\OC_Response::setStatus(404);
 			exit;
 		}
 	}
@@ -547,11 +549,11 @@ class OC_Preview {
 		if(array_key_exists('scalingup', $_GET)) $scalingup = (bool) $_GET['scalingup'];
 		if(array_key_exists('t', $_GET)) $token = (string) $_GET['t'];
 		
-		$linkItem = OCP\Share::getShareByToken($token);
+		$linkItem = \OCP\Share::getShareByToken($token);
 		
 		if (is_array($linkItem) && isset($linkItem['uid_owner']) && isset($linkItem['file_source'])) {
 			$userid = $linkItem['uid_owner'];
-			OC_Util::setupFS($userid);
+			\OC_Util::setupFS($userid);
 			$pathid = $linkItem['file_source'];
 			$path = \OC\Files\Filesystem::getPath($pathid);
 		}
@@ -559,7 +561,7 @@ class OC_Preview {
 		//clean up file parameter
 		$file = \OC\Files\Filesystem::normalizePath($file);
 		if(!\OC\Files\Filesystem::isValidPath($file)){
-			OC_Response::setStatus(403);
+			\OC_Response::setStatus(403);
 			exit;
 		}
 
@@ -570,15 +572,15 @@ class OC_Preview {
 
 		if($userid !== null && $path !== null){
 			try{
-				$preview = new OC_Preview($userid, 'files/' . $path, $file, $maxX, $maxY, $scalingup);
+				$preview = new Preview($userid, 'files/' . $path, $file, $maxX, $maxY, $scalingup);
 				$preview->showPreview();
 			}catch(Exception $e){
-				OC_Response::setStatus(404);
-				OC_Log::write('core', $e->getmessage(), OC_Log::ERROR);
+				\OC_Response::setStatus(404);
+				\OC_Log::write('core', $e->getmessage(), \OC_Log::ERROR);
 				exit;
 			}
 		}else{
-			OC_Response::setStatus(404);
+			\OC_Response::setStatus(404);
 			exit;
 		}
 	}
@@ -592,7 +594,7 @@ class OC_Preview {
 		if(substr($path, 0, 1) == '/'){
 			$path = substr($path, 1);
 		}
-		$preview = new OC_Preview(OC_User::getUser(), 'files/', $path, 0, 0, false, true);
+		$preview = new Preview(\OC_User::getUser(), 'files/', $path, 0, 0, false, true);
 		$preview->deleteAllPreviews();
 	}
 }
\ No newline at end of file
diff --git a/lib/preview/images.php b/lib/preview/images.php
index a8f203528c..c62fc5397e 100644
--- a/lib/preview/images.php
+++ b/lib/preview/images.php
@@ -6,7 +6,9 @@
  * later.
  * See the COPYING-README file.
  */
-class OC_Preview_Image extends OC_Preview_Provider{
+namespace OC\Preview;
+
+class Image extends Provider{
 
 	public function getMimeType(){
 		return '/image\/.*/';
@@ -31,4 +33,4 @@ class OC_Preview_Image extends OC_Preview_Provider{
 	}
 }
 
-OC_Preview::registerProvider('OC_Preview_Image');
\ No newline at end of file
+\OC\Preview::registerProvider('OC\Preview\Image');
\ No newline at end of file
diff --git a/lib/preview/movies.php b/lib/preview/movies.php
index d2aaf730d6..14ac97b552 100644
--- a/lib/preview/movies.php
+++ b/lib/preview/movies.php
@@ -6,8 +6,10 @@
  * later.
  * See the COPYING-README file.
  */
+namespace OC\Preview;
+
 if(!is_null(shell_exec('ffmpeg -version'))){
-	class OC_Preview_Movie extends OC_Preview_Provider{
+	class Movie extends Provider{
 
 		public function getMimeType(){
 			return '/video\/.*/';
@@ -18,7 +20,7 @@ if(!is_null(shell_exec('ffmpeg -version'))){
 			$fileinfo = $fileview->getFileInfo($path);
 
 			$abspath = $fileview->toTmpFile($path);
-			$tmppath = OC_Helper::tmpFile();
+			$tmppath = \OC_Helper::tmpFile();
 
 			//$cmd = 'ffmpeg -y  -i ' . escapeshellarg($abspath) . ' -f mjpeg -vframes 1 -ss 1 -s ' . escapeshellarg($maxX) . 'x' . escapeshellarg($maxY) . ' ' . $tmppath;
 			$cmd = 'ffmpeg -y  -i ' . escapeshellarg($abspath) . ' -f mjpeg -vframes 1 -ss 1 ' . $tmppath;
@@ -35,5 +37,5 @@ if(!is_null(shell_exec('ffmpeg -version'))){
 		}
 	}
 
-	OC_Preview::registerProvider('OC_Preview_Movie');
+	\OC\Preview::registerProvider('OC\Preview\Movie');
 }
\ No newline at end of file
diff --git a/lib/preview/mp3.php b/lib/preview/mp3.php
index 18f5cfde37..d62c723078 100644
--- a/lib/preview/mp3.php
+++ b/lib/preview/mp3.php
@@ -5,22 +5,24 @@
  * later.
  * See the COPYING-README file.
  */
+namespace OC\Preview;
+
 require_once('getid3/getid3.php');
 
-class OC_Preview_MP3 extends OC_Preview_Provider{
+class MP3 extends Provider{
 
 	public function getMimeType(){
 		return '/audio\/mpeg/';
 	}
 
 	public function getThumbnail($path, $maxX, $maxY, $scalingup, $fileview) {
-		$getID3 = new getID3();
+		$getID3 = new \getID3();
 
 		$tmppath = $fileview->toTmpFile($path);
 
 		//Todo - add stream support
 		$tags = $getID3->analyze($tmppath); 
-		getid3_lib::CopyTagsToComments($tags); 
+		\getid3_lib::CopyTagsToComments($tags); 
 		$picture = @$tags['id3v2']['APIC'][0]['data'];
 
 		unlink($tmppath);
@@ -38,4 +40,4 @@ class OC_Preview_MP3 extends OC_Preview_Provider{
 
 }
 
-OC_Preview::registerProvider('OC_Preview_MP3');
\ No newline at end of file
+\OC\Preview::registerProvider('OC\Preview\MP3');
\ No newline at end of file
diff --git a/lib/preview/pdf.php b/lib/preview/pdf.php
index de5263f91d..4dd4538545 100644
--- a/lib/preview/pdf.php
+++ b/lib/preview/pdf.php
@@ -5,9 +5,11 @@
  * later.
  * See the COPYING-README file.
  */
+namespace OC\Preview;
+
 if (extension_loaded('imagick')){
 
-	class OC_Preview_PDF extends OC_Preview_Provider{
+	class PDF extends Provider{
 
 		public function getMimeType(){
 			return '/application\/pdf/';
@@ -17,7 +19,7 @@ if (extension_loaded('imagick')){
 			$tmppath = $fileview->toTmpFile($path);
 
 			//create imagick object from pdf
-			$pdf = new imagick($tmppath . '[0]');
+			$pdf = new \imagick($tmppath . '[0]');
 			$pdf->setImageFormat('jpg');
 
 			unlink($tmppath);
@@ -31,5 +33,5 @@ if (extension_loaded('imagick')){
 		}
 	}
 
-	OC_Preview::registerProvider('OC_Preview_PDF');
+	\OC\Preview::registerProvider('OC\Preview\PDF');
 }
diff --git a/lib/preview/provider.php b/lib/preview/provider.php
index 2f2a0e6848..1e8d537adc 100644
--- a/lib/preview/provider.php
+++ b/lib/preview/provider.php
@@ -2,7 +2,9 @@
 /**
  * provides search functionalty
  */
-abstract class OC_Preview_Provider{
+namespace OC\Preview;
+
+abstract class Provider{
 	private $options;
 
 	public function __construct($options) {
diff --git a/lib/preview/svg.php b/lib/preview/svg.php
index 415b7751c2..70be263189 100644
--- a/lib/preview/svg.php
+++ b/lib/preview/svg.php
@@ -5,18 +5,26 @@
  * later.
  * See the COPYING-README file.
  */
+namespace OC\Preview;
+
 if (extension_loaded('imagick')){
 
-	class OC_Preview_SVG extends OC_Preview_Provider{
+	class SVG extends Provider{
 
 		public function getMimeType(){
 			return '/image\/svg\+xml/';
 		}
 
 		public function getThumbnail($path,$maxX,$maxY,$scalingup,$fileview) {
-			$svg = new Imagick();
+			$svg = new \Imagick();
 			$svg->setResolution($maxX, $maxY);
-			$svg->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $fileview->file_get_contents($path));
+
+			$content = stream_get_contents($fileview->fopen($path, 'r'));
+			if(substr($content, 0, 5) !== '<?xml'){
+				$content = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $content;
+			}
+
+			$svg->readImageBlob($content);
 			$svg->setImageFormat('jpg');
 
 			//new image object
@@ -28,6 +36,6 @@ if (extension_loaded('imagick')){
 		}
 	}
 
-	OC_Preview::registerProvider('OC_Preview_SVG');
+	\OC\Preview::registerProvider('OC\Preview\SVG');
 
 }
\ No newline at end of file
diff --git a/lib/preview/txt.php b/lib/preview/txt.php
index 1e88aec69f..4004ecd3fc 100644
--- a/lib/preview/txt.php
+++ b/lib/preview/txt.php
@@ -5,7 +5,9 @@
  * later.
  * See the COPYING-README file.
  */
-class OC_Preview_TXT extends OC_Preview_Provider{
+namespace OC\Preview;
+
+class TXT extends Provider{
 
 	public function getMimeType(){
 		return '/text\/.*/';
@@ -46,4 +48,4 @@ class OC_Preview_TXT extends OC_Preview_Provider{
 	}
 }
 
-OC_Preview::registerProvider('OC_Preview_TXT');
\ No newline at end of file
+\OC\Preview::registerProvider('OC\Preview\TXT');
\ No newline at end of file
diff --git a/lib/preview/unknown.php b/lib/preview/unknown.php
index 5bbdcf847f..6a8d2fbb75 100644
--- a/lib/preview/unknown.php
+++ b/lib/preview/unknown.php
@@ -6,7 +6,9 @@
  * later.
  * See the COPYING-README file.
  */
-class OC_Preview_Unknown extends OC_Preview_Provider{
+namespace OC\Preview;
+
+class Unknown extends Provider{
 
 	public function getMimeType(){
 		return '/.*/';
@@ -22,4 +24,4 @@ class OC_Preview_Unknown extends OC_Preview_Provider{
 	}
 }
 
-OC_Preview::registerProvider('OC_Preview_Unknown');
+\OC\Preview::registerProvider('OC\Preview\Unknown');
-- 
GitLab