<?php
/*********************************************************************************
 * The contents of this file are subject to the TimeTrex Public License Version
 * 1.1.0 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.TimeTrex.com/TPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by TimeTrex" logo and
 *    (ii) the TimeTrex copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: TimeTrex Open Source
 * The Initial Developer of the Original Code is TimeTrex Payroll Services
 * Portions created by TimeTrex are Copyright (C) 2004-2007 TimeTrex Payroll Services;
 * All Rights Reserved.
 *
 ********************************************************************************/
/*
 * $Revision: 1712 $
 * $Id: Upload.php 1712 2008-01-31 18:05:57Z ipso $
 * $Date: 2008-01-31 10:05:57 -0800 (Thu, 31 Jan 2008) $
 */
require_once('../../includes/global.inc.php');

//Debug::setVerbosity(11);
$skip_message_check = TRUE;
require_once(Environment::getBasePath() .'includes/Interface.inc.php');
require_once(Environment::getBasePath() .'classes/upload/fileupload.class.php');
/*
if ( !$permission->Check('user','enabled')
		OR !( $permission->Check('user','view') ) ) {

	$permission->Redirect( FALSE ); //Redirect

}
*/

$smarty->assign('title', TTi18n::gettext($title = 'File Upload')); // See index.php

/*
 * Get FORM variables
 */
extract	(FormVariables::GetVariables(
										array	(
												'action',
												'object_type',
												'object_id',
												'data',
												'userfile'
												) ) );

$ulf = new UserListFactory();

$action = Misc::findSubmitButton();
switch ($action) {
	case 'upload':
		Debug::Text('Upload... Object Type: '. $object_type, __FILE__, __LINE__, __METHOD__,10);

		$upload = new fileupload();

		$object_type = strtolower($object_type);
		switch ($object_type) {
			case 'invoice_config':
				$upload->set_max_filesize(1000000); //1mb or less
				$upload->set_acceptable_types( array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png') ); // comma separated string, or array
				//$upload->set_max_image_size(600, 600);
				$upload->set_overwrite_mode(1);

				$icf = new InvoiceConfigFactory();
				$icf->cleanStoragePath( $current_company->getId() );

				$dir = $icf->getStoragePath( $current_company->getId() );
				break;
			case 'company_logo':
				$upload->set_max_filesize(1000000); //1mb or less
				$upload->set_acceptable_types( array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png') ); // comma separated string, or array
				//$upload->set_max_image_size(600, 600);
				$upload->set_overwrite_mode(1);

				$cf = new CompanyFactory();
				$cf->cleanStoragePath( $current_company->getId() );

				$dir = $cf->getStoragePath( $current_company->getId() );
				break;
			case 'license':
				$upload->set_max_filesize(20000); //1mb or less
				$upload->set_acceptable_types( array('text/plain','plain/text','application/octet-stream') ); // comma separated string, or array
				$upload->set_overwrite_mode(1);

				$dir = Environment::getStorageBasePath() . DIRECTORY_SEPARATOR .'license'. DIRECTORY_SEPARATOR . $current_company->getId();
				break;
		}

		Debug::Text('bUpload... Object Type: '. $object_type, __FILE__, __LINE__, __METHOD__,10);
		if ( isset($dir) ) {
			@mkdir($dir, 0700, TRUE);

			$upload_result = $upload->upload("userfile", $dir);
			//var_dump($upload ); //file data
			if ($upload_result) {
				$success = $upload_result .' '. TTi18n::gettext('Successfully Uploaded');
			} else {
				$error = $upload->get_error();
			}
		}
		Debug::Text('cUpload... Object Type: '. $object_type, __FILE__, __LINE__, __METHOD__,10);

		switch ($object_type) {
			case 'invoice_config':
				Debug::Text('Post Upload Operation...', __FILE__, __LINE__, __METHOD__,10);
				if ( isset($success) AND $success != '' ) {
					Debug::Text('Rename', __FILE__, __LINE__, __METHOD__,10);
					//Submit filename to db.
					//Rename file to just "logo" so its always consistent.

					$file_data_arr = $upload->get_file();
					rename( $dir.'/'.$upload_result, $dir.'/logo'. $file_data_arr['extension'] );

					//$post_js = 'window.opener.document.getElementById(\'logo\').src = \''. Environment::getBaseURL().'/send_file.php?object_type=invoice_config&rand='.time().'\'; window.opener.showLogo();';
					$post_js = 'window.opener.setLogo()';
				}
				break;
			case 'company_logo':
				Debug::Text('Post Upload Operation...', __FILE__, __LINE__, __METHOD__,10);
				if ( isset($success) AND $success != '' ) {
					Debug::Text('Rename', __FILE__, __LINE__, __METHOD__,10);
					//Submit filename to db.
					//Rename file to just "logo" so its always consistent.

					//Resize image if its too large
					require_once 'Image/Transform.php';
					$image_transform =& Image_Transform::factory('');

					$image_transform->load($dir.'/'.$upload_result);
					$image_transform->fit(170,40);
					$image_transform->save($dir.'/'.$upload_result);

					$file_data_arr = $upload->get_file();
					rename( $dir.'/'.$upload_result, $dir.'/logo'. $file_data_arr['extension'] );

					//$post_js = 'window.opener.document.getElementById(\'logo\').src = \''. Environment::getBaseURL().'/send_file.php?object_type=invoice_config&rand='.time().'\'; window.opener.showLogo();';
					$post_js = 'window.opener.setLogo()';
				}
				break;
			case 'license':
				Debug::Text('Post Upload Operation...', __FILE__, __LINE__, __METHOD__,10);
				if ( isset($success) AND $success != '' ) {
					Debug::Text('Rename', __FILE__, __LINE__, __METHOD__,10);

					$file_data_arr = $upload->get_file();
					$license_data = trim( file_get_contents( $dir.'/'.$upload_result ) );

					//Validate License then save to system table.
					$license = new TTLicense();
					$parse_license_retval = $license->parseLicense( $license_data );
					$validate_license_retval = $license->validateLicense( $license_data );

					Debug::text('Parse License RetVal: '. (int)$parse_license_retval .' Validate License RetVal: '. $validate_license_retval, __FILE__, __LINE__, __METHOD__,9);
					if ( $parse_license_retval === TRUE ) {
						$sslf = new SystemSettingListFactory();
						$sslf->getByName( 'license' );
						if ( $sslf->getRecordCount() == 1 ) {
							$obj = $sslf->getCurrent();
						} else {
							$obj = new SystemSettingListFactory();
						}

						$obj->setName( 'license' );
						$obj->setValue( $license_data );
						if ( $obj->isValid() ) {
							Debug::text('Setting License Data...', __FILE__, __LINE__, __METHOD__,9);
							$obj->Save();

							//Save company name from license file.
							$clf = new CompanyListFactory();
							$clf->getById( $license->getPrimaryCompanyID() );
							if ( $clf->getRecordCount() > 0 ) {
								$c_obj = $clf->getCurrent();
							} else {
								$c_obj = $current_company;
							}
							$c_obj->setName( $license->getOrganizationName(), TRUE );
							if ( $c_obj->isValid() ) {
								$c_obj->Save();
							}

							//Save registration key
							$sslf = new SystemSettingListFactory();
							$sslf->getByName( 'registration_key' );
							if ( $sslf->getRecordCount() == 1 ) {
								$obj = $sslf->getCurrent();
							} else {
								$obj = new SystemSettingListFactory();
							}

							$obj->setName( 'registration_key' );
							$obj->setValue( $license->getRegistrationKey() );
							if ( $obj->isValid() ) {
								$obj->Save();
							}

							$post_js = 'window.opener.location.reload();';
						}
					} else {
						$error = TTi18n::gettext('Invalid License File');
					}
				}

				break;
		}

		$smarty->assign_by_ref('error', $error);
		$smarty->assign_by_ref('success', $success);
		$smarty->assign_by_ref('post_js', $post_js);

	default:
		$smarty->assign_by_ref('data', $data);

		$smarty->assign_by_ref('object_type', $object_type);
		$smarty->assign_by_ref('object_id', $object_id);

		break;
}

$smarty->display('upload/Upload.tpl');
?>