<?php

	/**
	 * Modul um während der Bestellung eine oder mehrere Dateien hochzuladen
	 */
	class wpsg_mod_orderupload extends wpsg_mod_basic 
	{
		
		var $id = 1600;		
		var $lizenz = 2;
		var $version = '3.1.0';
		
		/**
		 * Costructor
		 */
		public function __construct()
		{
			
			parent::__construct();
			
			$this->name = __('Bestellupload', 'wpsg');
			$this->group = __('Bestellung', 'wpsg');
			$this->desc = __('Erlaubt das hochladen von Dateien zu Bestellungen.', 'wpsg');
						
		} // public function __construct()
		
		public function install()
		{
			
			$this->shop->checkDefault('wpsg_mod_orderupload_filename', '%order_onr%_%filename%');
			$this->shop->checkDefault('wpsg_mod_orderupload_allowedext', 'jpg,jpeg');
			$this->shop->checkDefault('wpsg_mod_orderupload_multifiles', '0');
			$this->shop->checkDefault('wpsg_mod_orderupload_optional', '0');
			
		} // public function install()
		
		public function settings_edit()
		{
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_orderupload/settings_edit.phtml');
			
		} // public function settings_edit()
		
		public function settings_save()
		{
			
			$this->shop->update_option('wpsg_mod_orderupload_filename', $_REQUEST['wpsg_mod_orderupload_filename']);
			$this->shop->update_option('wpsg_mod_orderupload_allowedext', $_REQUEST['wpsg_mod_orderupload_allowedext']);
			$this->shop->update_option('wpsg_mod_orderupload_multifiles', $_REQUEST['wpsg_mod_orderupload_multifiles']);
			$this->shop->update_option('wpsg_mod_orderupload_optional', $_REQUEST['wpsg_mod_orderupload_optional']);
			
		} // public function settings_save()
				
		public function checkout_inner_prebutton(&$checkout_view) { 
			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_orderupload/checkout_inner_prebutton.phtml');
			
		} // public function checkout_inner_prebutton(&$checkout_view)
		
		public function checkCheckout(&$state, &$error, &$arCheckout)  
		{
			
			if ($state == '1')
			{
				
				if (
					$this->shop->get_option('wpsg_mod_orderupload_optional') != '1' && 
					sizeof($_SESSION['wpsg']['wpsg_mod_orderupload']) <= 0 &&
					(!isset($_FILES['wpsg_mod_orderupload_file']) || !file_exists($_FILES['wpsg_mod_orderupload_file']['tmp_name']))
					)
				{
					
					// Noch keine Datei hochgeladen und es ist Pflicht !										
					$this->shop->addFrontendError(__('Bitte eine Datei zum Upload wählen.', 'wpsg'));
					$error = true; return;
					
				}
				
				if (isset($_FILES['wpsg_mod_orderupload_file']) && file_exists($_FILES['wpsg_mod_orderupload_file']['tmp_name']))
				{
					
					$ending = strtolower(preg_replace('/(.*)\./', '', $_FILES['wpsg_mod_orderupload_file']['name']));
					
					if (!in_array($ending, (array)explode(',', strtolower($this->shop->get_option('wpsg_mod_orderupload_allowedext')))))
					{
						
						$this->shop->addFrontendError(__('Bitte eine gültige Dateiendung verwenden.', 'wpsg'));
						$error = true; return;
						
					}
					else
					{
						
						// Upload durchführen
						$tmp = time().'-'.rand(1, 10000).'.'.$ending;
						
						move_uploaded_file($_FILES['wpsg_mod_orderupload_file']['tmp_name'], $this->getTmpFilePath().'/'.$tmp); 
						
						if (!is_array($_SESSION['wpsg']['wpsg_mod_orderupload'])) $_SESSION['wpsg']['wpsg_mod_orderupload'] = array();
						else if (wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_orderupload']) && $this->shop->get_option('wpsg_mod_orderupload_multifiles') != '1')
						{
							
							// Cache sauber haltens
							foreach ($_SESSION['wpsg']['wpsg_mod_orderupload'] as $ou)
							{
																
								unlink($ou[3]);
								
							}
							
							$_SESSION['wpsg']['wpsg_mod_orderupload'] = array();
						 							
						}
						
						$_SESSION['wpsg']['wpsg_mod_orderupload'][] = wpsg_xss(array(
							$_FILES['wpsg_mod_orderupload_file']['name'],
							$tmp,
							$this->getTmpFilePath(true).'/'.$tmp,
							$this->getTmpFilePath().'/'.$tmp			
						));
						
						$this->shop->addFrontendMessage(__('Datei wurde erfolgreich hochgeladen.', 'wpsg'));
						
						if ($this->shop->get_option('wpsg_mod_orderupload_multifiles') == '1')
						{
							
							$error = true;
							
						}
						
					}
					
				}
				
			}
			
		} // public function checkCheckout(&$state, &$error, &$arCheckout)
		
		public function wp_loaded() 
		{ 
			
			if ($_REQUEST['wpsg_plugin'] == 'wpsg_mod_orderupload' && $_REQUEST['wpsg_action'] == 'remove')
			{
				
				unlink($_SESSION['wpsg']['wpsg_mod_orderupload'][$_REQUEST['file_index']][3]);
				unset($_SESSION['wpsg']['wpsg_mod_orderupload'][$_REQUEST['file_index']]);
				
				$this->shop->addFrontendMessage(__('Dokument wurde aus der Bestellung entfernt.', 'wpsg'));
				
				$this->shop->redirect($this->shop->getURL(wpsg_ShopController::URL_CHECKOUT));
				
			}
			
		} // public function wp_loaded()

		public function order_ajax() 
		{ 
			
			if (isset($_REQUEST['wpsg_mod_orderupload_download']))
			{
				
				header('Content-Disposition: attachment; filename="'.rawurldecode($_REQUEST['wpsg_mod_orderupload_download']).'"');
				header('Content-type: application/download');
				header('Content-Disposition: inline; filename="'.rawurldecode($_REQUEST['wpsg_mod_orderupload_download']).'"');
				header('Expires: 0');
				header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
				header('Pragma: public');
				
				die(file_get_contents($this->getSavePath($_REQUEST['edit_id']).'/'.rawurldecode($_REQUEST['wpsg_mod_orderupload_download'])));
				
			}
			
		} // public function order_ajax() 
		
		public function basket_save_done(&$o_id, &$k_id, &$oBasket)
		{
			
			if (wpsg_isSizedArray($_SESSION['wpsg']['wpsg_mod_orderupload']))
			{
				
				foreach ($_SESSION['wpsg']['wpsg_mod_orderupload'] as $k => $ou)
				{
					 
					$path_target = $this->getSavePath($o_id);
					
					copy(
						$ou[3], 
						$path_target.'/'.$this->getNewName($ou[0], $o_id)
					);
					
					// Temporären Ordner säubern
					unlink($ou[3]);
					
					unset($_SESSION['wpsg']['wpsg_mod_orderupload'][$k]);
					
				}
				
			}
			
		} // public function basket_save_done($o_id, $k_id)
		
		public function order_view_sidebar(&$order_id) 
		{ 
	 
			$arFiles = scandir($this->getSavePath($order_id));
			 
			foreach ($arFiles as $k => $v) { if (in_array($v['id'], array('.', '..', '.htaccess', '.htpasswd'))) { unset($arFiles[$k]); } }
			
			if (sizeof($arFiles) <= 0) return;
			
			$this->shop->view['arFiles'] = $arFiles;			
			$this->shop->render(WPSG_PATH_VIEW.'/mods/mod_orderupload/order_view_sidebar.phtml');
			
		} // public function order_view_sidebar(&$order_id)
		
		public function delOrder(&$order_id) 
		{ 
			 
			// Hochgeladene Dateien löschen
			$path = $this->getSavePath($order_id);
			
			wpsg_rrmdir($path);
			
		} // public function delOrder(&$order_id)
		
		/* -- */
		
		public function getNewName($filename, $order_id)
		{
			
			$arReplace = array(
				'/%filename%/' => $filename,
				'/%filename_clear%/' => preg_replace('/\.(.*)$/', '', $filename) 
			);
			
			$filename = preg_replace(array_keys($arReplace), array_values($arReplace), $this->shop->get_option('wpsg_mod_orderupload_filename'));
			
			$filename = $this->shop->replaceUniversalPlatzhalter($filename, $order_id);
			
			return $filename;
			
		} // public function getNewName($filename, $order_id)
		
		public function getSavePath($order_id, $burl = false)
 		{
 		 
 			if ($this->shop->isMultiBlog())
			{

				$url = WPSG_URL_CONTENT.WPSG_MB_UPLOADS.'/wpsg/wpsg_orderupload/'.$order_id.'/';
				$path = WP_CONTENT_DIR.'/'.WPSG_MB_UPLOADS.'/wpsg/wpsg_orderupload/'.$order_id.'/'; 
				$htaccess = WP_CONTENT_DIR.'/'.WPSG_MB_UPLOADS.'/wpsg/wpsg_orderupload/.htaccess';
				 
			}
			else
			{
				
				$url = WPSG_URL_CONTENT.'uploads/wpsg/wpsg_orderupload/'.$order_id.'/';
				$path = WP_CONTENT_DIR.'/uploads/wpsg/wpsg_orderupload/'.$order_id.'/';
				$htaccess = WP_CONTENT_DIR.'/uploads/wpsg/wpsg_orderupload/.htaccess';
								
			}
			 
			if (!file_exists($path)) mkdir($path, 0777, true);
			
 			if (!file_exists($htaccess))
			{
				 				
				$handle = fopen($htaccess, "w+");
				$content = "Deny from all";			
				fwrite($handle, $content, strlen($content));
				fclose($handle);
				
			}
			
			if ($burl) return $url;
			else return $path;
			
 		} // private function getSavePath($order_id, $url = false)
				
	} // class wpsg_mod_orderupload extends wpsg_mod_basic


?>