<?php

class DownloaderView extends Slot {

	var $ModuleID = 2;
	var $SectionID = 23;
	var $id = 0;
	var $name = '';

	function __construct($pquery) {
		parent::__construct($pquery);
		$this->Cache = 0;
		$this->Passing = TRUE;
	}

	function init() {
		if(!empty($this->Target[2])) {
			$this->Action->processEvent($this->SectionID,'get_product_id_by_path',array('path' => $this->Target[2],'path2' => $this->Target[2]),$this);
			if(empty($this->PreparedTemplates['id'])) $this->Scheme = 'empty';
			else $this->id = $this->PreparedTemplates['id'];
			$this->Action->processEvent($this->SectionID,'open',array('id' => $this->id),$this);
			$this->name = validate(trim($this->PreparedTemplates['nav_name']))? $this->PreparedTemplates['nav_name'] : $this->PreparedTemplates['name'];
		}
		else $this->Scheme = 'empty';
		if(!empty($this->id)) {
			switch(@$this->Target[1]) {
				case 'nightvisionpdf': {
					if (strpos($this->Target[3],'_userguide_') !== false && preg_match('/(.+)(_userguide_)(\d+)(\.pdf)/', $this->Target[3], $matches ) ){
						$this->send_pdf($this->ImagePath.'/products/'.$this->id.'/userguides/'.$matches[3].'.pdf','application/pdf',$this->Target[2].'_userguide.pdf');
					} else if (strpos($this->Target[3],'_specsheet') !== false) 	
							$this->send_pdf($this->ImagePath.'/products/'.$this->id.'/ss.pdf','application/pdf',$this->Target[2].'_specsheet.pdf');
							else $this->Scheme = 'empty'; break;
					/*switch(@$this->Target[3]) {
						case $this->Target[2].'_userguide.pdf': {
							$this->send_pdf($this->ImagePath.'/products/'.$this->id.'/ug.pdf','application/pdf',$this->Target[2].'_userguide.pdf');
						break;
						}
						case $this->Target[2].'_specsheet.pdf': {
							$this->send_pdf($this->ImagePath.'/products/'.$this->id.'/ss.pdf','application/pdf',$this->Target[2].'_specsheet.pdf');
						break;
						}
						default: $this->Scheme = 'empty'; break;
					}*/
				break;
				}
				case 'hi-resolutionzip': default: {
					$this->send_hi($this->id);
				break;
				}
				/*case '3dzip': {
					$this->send_3d($this->id);
				break;
				}*/
				case 'downloadallzip': {
					$this->send_all($this->id);
				break;
				}
				case 'downloadpicture': {
					$this->send_pic($this->id);
				break;
				}
			}
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}

	function send_pdf($location, $content_type, $name) {
		if(file_exists($location)) {
			ini_set("zlib.output_compression", "off");
			header("HTTP/1.0 200 OK");
			header("Cache-Control: max-age=86400",true);
			header("Content-type: ".$content_type);
			header("Content-Disposition: attachment; filename=\"".$name."\"");
			header("Content-Length: ".filesize($location));
			readfile($location);
			exit();
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}

	function send_hi($id) {
		global $ROOT_PATH;
		$file_path = $this->ImagePath.'/products/'.$id.'/hi.tif';
		if(!file_exists($ROOT_PATH.'/engine/Templates/DownloaderView/Hi/readme.txt')) die("Error: not present readme.txt!");
		else if(file_exists($file_path)) {
			$file_path .= " ".$ROOT_PATH.'/engine/Templates/DownloaderView/Hi/readme.txt'; // add readme.txt
			ini_set("zlib.output_compression", "off");
			header("HTTP/1.0 200 OK");
			header("Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0, max-age=0",true);
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename=\"".$this->Target[2]."_HiResolutionImage.zip\"");
			$handle = popen("zip -j -9 -D - $file_path", "r");
			fpassthru($handle);
			pclose($handle);
			die();
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}

	function send_3d($id) {
		global $ROOT_PATH;
		$image_path = $this->ImagePath.'/products/'.$id.'/3D/';
		$zip_list = '';
		if(file_exists($image_path."01.jpg")) {
			$image_size = getimagesize($image_path."01.jpg");
			// create image list
			$image_count = 1;
			while(file_exists(sprintf("%s%02d.jpg",$image_path,$image_count))) {
				/* Count of Images and Add to File List*/
				$zip_list .= sprintf(" %s%02d.jpg",$image_path,$image_count);
				$image_count++;
			}
			if(--$image_count < 1) {
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
			// create index.html
			$tmpfname = tempnam("/tmp", "ZIP");
			if(!empty($tmpfname)) {
				unlink($tmpfname);
				mkdir($tmpfname, 0777);
				$this->make_index($tmpfname);
			}
			else {
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
			if(!file_exists($ROOT_PATH.'/night_vision_inc/flash/3d_player/player_fin.swf') || !file_exists($ROOT_PATH.'/engine/Templates/DownloaderView/3D/readme.txt')) {
				// delete index.html with temporary dir
				unlink($tmpfname."/index.html");
				rmdir($tmpfname);
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
			$zip_list .= " ".$tmpfname.'/index.html';													// add index.html
			$zip_list .= " ".$ROOT_PATH.'/night_vision_inc/flash/3d_player/player_fin.swf';				// add flass player
			$zip_list .= " ".$ROOT_PATH.'/engine/Templates/DownloaderView/3D/readme.txt';				// add readme.txt
			$zip_list .= " ".$ROOT_PATH.'/css/default.style.css';										// add css
			$zip_list .= " ".$ROOT_PATH.'/registarJS/inc/AC_RunActiveContent.js';							// add js
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/download/logo1.gif';						// add logo 1
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/white_top_left.gif';	// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/white_top_right.gif';	// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/white_top_border.gif';	// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/left_border.gif';		// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/layout1/dark_right.gif';					// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/right_border.gif';		// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/gradient.gif';						// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/bottomleft.gif';		// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/bottomright.gif';		// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/table/dark_border/bottom_border.gif';		// add image
			$zip_list .= " ".$ROOT_PATH.'/night_vision_images/download/logo2.gif';						// add logo 2
			// send archive
			ini_set("zlib.output_compression", "off");
			header("HTTP/1.0 200 OK");
			header("Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0, max-age=0",true);
			header("Pragma: no-cache",true);
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename=\"".$this->Target[2]."_3D.zip\"");
			$handle = popen("zip -j -9 -D - $zip_list", "r");
			fpassthru($handle);
			pclose($handle);
			// delete index.html with temporary dir
			unlink($tmpfname."/index.html");
			rmdir($tmpfname);
			die();
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}

	function make_index($filename) {
		global $ROOT_PATH;
		$file = file_get_contents($ROOT_PATH.'/engine/Templates/DownloaderView/3D/index.html');
		$fp = fopen($filename."/index.html", "w");
		if(!$fp) return false;
		$file = eregi_replace('\$name',$this->name,$file);
		fputs($fp,$file);
		fclose($fp);
		return true;
	}

	function send_pic($id) {
		$file_path = '';
		foreach($this->PreparedTemplates['files'] as $value) {
			if($value['filetype']==3) {
				$id = !empty($value['image_link'])? $value['image_link'] : $this->PreparedTemplates['id'];
				//$file_path = $this->ImagePath.'/products/'.$id.'/big.jpg';
				$file_path = $this->ImagePath.'/products/'.$id.'/images/big/01.jpg';
				break;
			}
		}
		if(file_exists($file_path)) {
			ini_set("zlib.output_compression", "off");
			header("HTTP/1.0 200 OK");
			header("Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0, max-age=0",true);
			header("Pragma: no-cache",true);
			header("Content-type: image/jpeg");
			header("Content-Disposition: attachment; filename=\"".$this->Target[2]."_Picture.jpg\"");
			header("Content-Length: ".filesize($file_path));
			readfile($file_path);
			die();
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}

	function send_all($id) {
		global $ROOT_PATH;
		$zip_list = '';
		$this->PreparedTemplates = $this->Action->getTemplate($this->SectionID, 'open');
		if(!empty($this->PreparedTemplates['_error'])) {
			$this->Scheme = 'empty';
			$this->setSchemeTemplate($this->Scheme);
		}
		// check for images
		foreach($this->PreparedTemplates['files'] as $value) {
			if($value['filetype']==2) {
				$id = !empty($value['image_link'])? $value['image_link'] : $this->PreparedTemplates['id'];
				$path = $this->ImagePath.'/products/'.$id.'/big.jpg';
				if(file_exists($path)) {
					$tmpfname  = tempnam("/tmp", "BIG");
					if(!empty($tmpfname)) {
						unlink($tmpfname);
						mkdir($tmpfname, 0777);
						copy($path,$tmpfname.'/'.$this->Target[2].'_Big_Image.jpg');
						chmod($tmpfname.'/'.$this->Target[2].'_Big_Image.jpg',0664);
						$delete_big_file = $tmpfname.'/'.$this->Target[2].'_Big_Image.jpg';
						$delete_big_dir  = $tmpfname;
						$zip_list .= " ".$delete_big_file;
					}
					else {
						$this->Scheme = 'empty';
						$this->setSchemeTemplate($this->Scheme);
					}
				}
			}
			if($value['filetype']==3) {
				$path = $this->ImagePath.'/products/'.$this->PreparedTemplates['id'].'/images/big/'.date2pg($value['number']).'.jpg';
				if(file_exists($path)) $zip_list .= ' '.$path;
			}
			if($value['filetype']==5) {
				$path = $this->ImagePath.'/products/'.$this->PreparedTemplates['id'].'/hi.tif';
				if(file_exists($path)) {
					$tmpfname  = tempnam("/tmp", "HI");
					if(!empty($tmpfname)) {
						unlink($tmpfname);
						mkdir($tmpfname, 0777);
						copy($path,$tmpfname.'/'.$this->Target[2].'_Hi-Resolution_Image.tif');
						chmod($tmpfname.'/'.$this->Target[2].'_Hi-Resolution_Image.tif',0664);
						$delete_hi_file = $tmpfname.'/'.$this->Target[2].'_Hi-Resolution_Image.tif';
						$delete_hi_dir  = $tmpfname;
						$zip_list .= " ".$delete_hi_file;
					}
					else {
						$this->Scheme = 'empty';
						$this->setSchemeTemplate($this->Scheme);
					}
				}
			}
			if($value['filetype']==6) {
				$path = $this->ImagePath.'/products/'.$this->PreparedTemplates['id'].'/ss.pdf';
				if(file_exists($path)) {
					$tmpfname  = tempnam("/tmp", "SS");
					if(!empty($tmpfname)) {
						unlink($tmpfname);
						mkdir($tmpfname, 0777);
						copy($path,$tmpfname.'/'.$this->Target[2].'_Specification_Sheet.pdf');
						chmod($tmpfname.'/'.$this->Target[2].'_Specification_Sheet.pdf',0664);
						$delete_ss_file = $tmpfname.'/'.$this->Target[2].'_Specification_Sheet.pdf';
						$delete_ss_dir  = $tmpfname;
						$zip_list .= " ".$delete_ss_file;
					}
					else {
						$this->Scheme = 'empty';
						$this->setSchemeTemplate($this->Scheme);
					}
				}
			}
			if($value['filetype']==7) {
				$path = $this->ImagePath.'/products/'.$this->PreparedTemplates['id'].'/ug.pdf';
				if(file_exists($path)) {
					$tmpfname  = tempnam("/tmp", "UG");
					if(!empty($tmpfname)) {
						unlink($tmpfname);
						mkdir($tmpfname, 0777);
						copy($path,$tmpfname.'/'.$this->Target[2].'_User_Guide.pdf');
						chmod($tmpfname.'/'.$this->Target[2].'_User_Guide.pdf',0664);
						$delete_ug_file = $tmpfname.'/'.$this->Target[2].'_User_Guide.pdf';
						$delete_ug_dir  = $tmpfname;
						$zip_list .= " ".$delete_ug_file;
					}
					else {
						$this->Scheme = 'empty';
						$this->setSchemeTemplate($this->Scheme);
					}
				}
			}
		}
		// check for system description
		if(validate(trim($this->PreparedTemplates['full_desc']))) {
			$delete_desc_file = '';
			$delete_desc_dir  = '';
			$tmpfname  = tempnam("/tmp", "DESC");
			if(!empty($tmpfname)) {
				unlink($tmpfname);
				mkdir($tmpfname, 0777);
				$fd = fopen($tmpfname.'/'.$this->Target[2].'_Description.txt', "w");
				fputs($fd, strip_tags($this->PreparedTemplates['full_desc']));
				fclose($fd);
				$delete_desc_file = $tmpfname.'/'.$this->Target[2].'_Description.txt';
				$delete_desc_dir  = $tmpfname;
				$zip_list .= " ".$delete_desc_file;
			}
			else {
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
		}
		// check for system specification
		$spec_text = '';
		foreach($this->PreparedTemplates['parameters'] as $value) {
			if(validate(trim($value['value']))) $spec_text .= trim($value['name']).' = '.trim($value['value'])."\r\n";
			else $spec_text .= trim($value['name'])."\r\n";
		}
		if(!empty($spec_text)) {
			$delete_spec_file = '';
			$delete_spec_dir  = '';
			$tmpfname  = tempnam("/tmp", "SPEC");
			if(!empty($tmpfname)) {
				unlink($tmpfname);
				mkdir($tmpfname, 0777);
				$fd = fopen($tmpfname.'/'.$this->Target[2].'_Specification.txt', "w");
				fputs($fd, strip_tags($spec_text));
				fclose($fd);
				$delete_spec_file = $tmpfname.'/'.$this->Target[2].'_Specification.txt';
				$delete_spec_dir  = $tmpfname;
				$zip_list .= " ".$delete_spec_file;
			}
			else {
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
		}
		// check for features
		$feat_text = '';
		foreach($this->PreparedTemplates['features'] as $value) {
			if(validate(trim($value['value']))) $feat_text .= trim($value['value'])."\r\n";
		}
		if(!empty($feat_text)) {
			$delete_feat_file = '';
			$delete_feat_dir  = '';
			$tmpfname  = tempnam("/tmp", "FEAT");
			if(!empty($tmpfname)) {
				unlink($tmpfname);
				mkdir($tmpfname, 0777);
				$fd = fopen($tmpfname.'/'.$this->Target[2].'_Features.txt', "w");
				fputs($fd, strip_tags($feat_text));
				fclose($fd);
				$delete_feat_file = $tmpfname.'/'.$this->Target[2].'_Features.txt';
				$delete_feat_dir  = $tmpfname;
				$zip_list .= " ".$delete_feat_file;
			}
			else {
				$this->Scheme = 'empty';
				$this->setSchemeTemplate($this->Scheme);
			}
		}
		if(!empty($zip_list)) {
			$zip_list .= " ".$ROOT_PATH.'/engine/Templates/DownloaderView/All/readme.txt';	// add readme.txt
			ini_set("zlib.output_compression", "off");
			header("HTTP/1.0 200 OK");
			header("Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0, max-age=0",true);
			header("Pragma: no-cache",true);
			header("Content-type: application/zip");
			header("Content-Disposition: attachment; filename=\"".$this->Target[2]."_All.zip\"");
			$handle = popen("zip -j -9 -D - $zip_list", "r");
			fpassthru($handle);
			pclose($handle);
			// delete index.html with temporary dir
			if(!empty($delete_descr_file)) unlink($delete_descr_file);
			if(!empty($delete_descr_dir)) rmdir($delete_descr_dir);
			if(!empty($delete_spec_file)) unlink($delete_spec_file);
			if(!empty($delete_spec_dir)) rmdir($delete_spec_dir);
			if(!empty($delete_feat_file)) unlink($delete_feat_file);
			if(!empty($delete_feat_dir)) rmdir($delete_feat_dir);
			if(!empty($delete_big_file)) unlink($delete_big_file);
			if(!empty($delete_big_dir)) rmdir($delete_big_dir);
			if(!empty($delete_hi_file)) unlink($delete_hi_file);
			if(!empty($delete_hi_dir)) rmdir($delete_hi_dir);
			if(!empty($delete_ss_file)) unlink($delete_ss_file);
			if(!empty($delete_ss_dir)) rmdir($delete_ss_dir);
			if(!empty($delete_ug_file)) unlink($delete_ug_file);
			if(!empty($delete_ug_dir)) rmdir($delete_ug_dir);
			die();
		}
		else $this->Scheme = 'empty';
		$this->setSchemeTemplate($this->Scheme);
	}
}
?>