<?php
/*******************************************************************************/
//                          InstantCMS v1.8                                    //
//                      http://www.instantcms.ru/                              //
//                         component "Poster"                                  //
//                       written by Marat Fatikhov                             //
//                      (nickname Марат on a site)                             //
//                       E-mail: f-marat@mail.ru                               //
//                                                                             //
//                      LICENSED BY GNU/GPL v2                                 //
//                                                                             //
/********************************************************************************/

header('Content-Type: text/html; charset=windows-1251');
session_start();

define("VALID_CMS", 1);
define('PATH', $_SERVER['DOCUMENT_ROOT']);
	include(PATH.'/core/cms.php');
    
    
    
    $inCore = cmsCore::getInstance();	// ядро

   
 $inCore->loadClass('config');       // конфигурация
$inCore->loadClass('db');
$inCore->loadClass('user');

$inDB   = cmsDatabase::getInstance();
$inUser = cmsUser::getInstance();

$inUser->update();
	if (!$inUser->id) { $inCore->halt(); }



//проверяем входные параметры
$user_id = $inCore->request('id', 'int');
$logofrom = $inCore->request('from', 'str');



		$inCore->includeGraphics();
		
		$uploaddir 		= PATH.'/images/torg_plogo/';
		if (!is_dir($uploaddir)) { @mkdir($uploaddir); }
		@chmod($uploaddir, 0755);

		$realfile		= $_FILES['page_logo']['name'];
		$path_parts     = pathinfo($realfile);
		$ext            = strtolower($path_parts['extension']);
		$realfile		= md5($realfile. '-' . time()).'.'.$ext;

		//if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png'){

			$filename 		= md5($realfile . '-' . $user_id . '-' . time()).'.jpg';
			$uploadfile		= $uploaddir . $realfile;
			$uploadavatar 	= $uploaddir . $filename;
			$uploadthumb 	= $uploaddir . 'small/' . $filename;
			$source			= $_FILES['page_logo']['tmp_name'];
			$errorCode 		= $_FILES['page_logo']['error'];

		//} else {

		// echo '<div style="padding:5px 0;color:#C00">неверный тип файла</div>';	
 					
		//}

	
			
	

			//DELETE OLD AVATAR
			$sql = "SELECT page_logo FROM cms_torg_users WHERE id = '$user_id'";
			$result = $inDB->query($sql) ;
			if ($inDB->num_rows($result)){
				$old = $inDB->fetch_assoc($result);
				if ($old['page_logo'] && $old['page_logo']!='nopic.jpg'){
					@unlink(PATH.'/images/torg_plogo/'.$old['page_logo']);
					@unlink(PATH.'/images/torg_plogo/small/'.$old['page_logo']);
				}
			}

			//CREATE THUMBNAIL
			if (isset($cfg['smallw'])) { $smallw = $cfg['smallw']; } else { $smallw = 64; }
			if (isset($cfg['medw'])) { 	 $medw = $cfg['medw']; } else { $medw = 200; }
			if (isset($cfg['medh'])) { 	 $medh = $cfg['medh']; } else { $medh = 200; }
						
			@img_resize($uploadfile, $uploadavatar, $medw, $medh);
			@img_resize($uploadfile, $uploadthumb, $smallw, $smallw);
						
			//DELETE ORIGINAL							
			@unlink($uploadfile);
			
		
		//MODIFY PROFILE
			$sql = "UPDATE cms_torg_users
					SET page_logo = '$filename'
					WHERE id = '$user_id'
					LIMIT 1";

			$inDB->query($sql);
		
		echo '<img src="/images/torg_plogo/'.$filename.'"/>';
	
		
	

?>
