<?php
	// Пути к XML фалам системы
	if (file_exists($baseT)) { $xmltask 		= simplexml_load_file($baseT); }
	if (file_exists($baseU)) { $xmluser 		= simplexml_load_file($baseU); }
	if (file_exists($baseC)) { $xmlclient  		= simplexml_load_file($baseC); }
	if (file_exists($baseD)) { $xmldeal 		= simplexml_load_file($baseD); }
	if (file_exists($baseL)) { $xmllogs 		= simplexml_load_file($baseL); }
	if (file_exists($baseQ)) { $xmlmess			= simplexml_load_file($baseQ); }
	if (file_exists($baseB)) { $xmlblock		= simplexml_load_file($baseB); }
	if (file_exists($baseE)) { $xmlevents		= simplexml_load_file($baseE); }
	if (file_exists($baseLT)){ $xmlletters		= simplexml_load_file($baseLT); }
	if (file_exists($baseR)) { $xmlrequests		= simplexml_load_file($baseR); }
	if (file_exists($baseP)) { $xmlpayments		= simplexml_load_file($baseP); }
	if (file_exists($baseV)) { $xmlvacations	= simplexml_load_file($baseV); }
	
	// Счетчики
	if (file_exists($baseT)) { $countTask 		= count($xmltask->xpath('/tasks/task')); }
	if (file_exists($baseU)) { $countUser 		= count($xmluser->xpath('/users/user')); }
	if (file_exists($baseC)) { $countClient		= count($xmlclient->xpath('/clients/client')); }
	if (file_exists($baseD)) { $countDeal 		= count($xmldeal->xpath('/deals/deal')); }
	if (file_exists($baseQ)) { $countMessage 	= count($xmlmess->xpath('/messages/message')); }
	if (file_exists($baseL)) { $countLogs 		= count($xmllogs->xpath('/logs/log')); }
	if (file_exists($baseB)) { $countBlock 		= count($xmlblock->xpath('/sessions/session')); }
	if (file_exists($baseE)) { $countEvents 	= count($xmlevents->xpath('/events/event')); }
	if (file_exists($baseLT)){ $countLetters 	= count($xmlletters->xpath('/letters/letter')); }
	if (file_exists($baseR)) { $countRequests 	= count($xmlrequests->xpath('/requests/request')); }
	if (file_exists($baseP)) { $countPayments 	= count($xmlpayments->xpath('/payments/payment')); }
	if (file_exists($baseV)) { $countVacations 	= count($xmlvacations->xpath('/vacations/vacation')); }
	
	if(isset($_POST['LoadBackup'])){
		/* Загрузка бекапа */
		$path_file = '../backups/';
		// ограничение размера файла
		$limit_size = 10*1024*1024; // 10 Mb
		// корректные форматы файлов
		$valid_format = array('zip');
		// имя нового файла
		//$info = pathinfo();
		$rand_name = $_FILES['upload_file']['name'];

		// если есть отправленные файлы
		if($_FILES['upload_file']){
			// валидация размера файла
			if($_FILES['upload_file']['size'] > $limit_size){
				$notices_er[] = 'Вес файла больше 10 Мб!';
			}
			// валидация формата файла
			$format = end(explode(".", $_FILES['upload_file']['name']));
			if(!in_array($format, $valid_format)){
				$notices_er[] = 'Формат файла не допустимый!';
			}
			// если не было ошибок
			if(empty($notices_er)){
				// проверяем загружен ли файл
				if(is_uploaded_file($_FILES['upload_file']['tmp_name'])){
					// сохраняем файл
					move_uploaded_file($_FILES['upload_file']['tmp_name'], $path_file . $rand_name . ".$format");
					$notices[] = 'Файл успешно загружен';
				}else{
					// Если файл не загрузился
					$notices_er[] = 'Ошибка загрузки!';
				}
			}		
		}
	}
	
	function ClearAllLogs() {
		$baseL 		= ROOT . 'crmdata/database/logs.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseL);
		$xpath 	= new DOMXPath($doc);
		$logs 	= $xpath->query('/logs/log');
		for ($i = 0; $i < $logs->length; $i++) {
			$el = $logs->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseL);
		$notices[] = 'Данные о действиях удалены';
	}
	
	function ClearAllBlock() {
		$baseB 		= ROOT . 'crmdata/database/users-ipblock.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseB);
		$xpath 	= new DOMXPath($doc);
		$ips 	= $xpath->query('/sessions/session');
		for ($i = 0; $i < $ips->length; $i++) {
			$el = $ips->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseB);
		$notices[] = 'Все заблокированные IP удалены';
	}
	
	function ClearAllTasks() {
		$baseT 		= ROOT . 'crmdata/database/tasks.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseT);
		$xpath 	= new DOMXPath($doc);
		$tasks 	= $xpath->query('/tasks/task');
		for ($i = 0; $i < $tasks->length; $i++) {
			$el = $tasks->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseT);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{task_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countTask = '0';
		$notices[] = 'Все задачи удалены';
	}

	function ClearAllClients() {
		$baseC 		= ROOT . 'crmdata/database/clients.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseC);
		$xpath 	= new DOMXPath($doc);
		$clients 	= $xpath->query('/clients/client');
		for ($i = 0; $i < $clients->length; $i++) {
			$el = $clients->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseC);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{client_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countClient = '0';
		$notices[] = 'Все клиенты удалены';
	}

	function ClearAllEvents() {
		$baseE 		= ROOT . 'crmdata/database/events.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseE);
		$xpath 	= new DOMXPath($doc);
		$events 	= $xpath->query('/events/event');
		for ($i = 0; $i < $events->length; $i++) {
			$el = $events->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseE);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{event_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countEvents = '0';
		$notices[] = 'Все события удалены';
	}
	
	function ClearAllLetters() {
		$baseLT 	= ROOT . 'crmdata/database/letters.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseLT);
		$xpath 	= new DOMXPath($doc);
		$letters 	= $xpath->query('/letters/letter');
		for ($i = 0; $i < $letters->length; $i++) {
			$el = $letters->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseLT);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{letters_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countLetters = '0';
		$notices[] = 'Все письма удалены';
	}
	
	function ClearAllRequests() {
		$baseR 	= ROOT . 'crmdata/database/requests.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseR);
		$xpath 	= new DOMXPath($doc);
		$requests 	= $xpath->query('/requests/request');
		for ($i = 0; $i < $requests->length; $i++) {
			$el = $requests->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseR);
		$countRequests = '0';
		$notices[] = 'Все запросы удалены';
	}
	
	function ClearAllPayments() {
		$baseP 	= ROOT . 'crmdata/database/payments.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseP);
		$xpath 	= new DOMXPath($doc);
		$payments 	= $xpath->query('/payments/payment');
		for ($i = 0; $i < $payments->length; $i++) {
			$el = $payments->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseP);
		$countPayments = '0';
		$notices[] = 'Все платежи удалены';
	}
	
	function ClearAllVacations() {
		$baseV 	= ROOT . 'crmdata/database/vacations.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseV);
		$xpath 	= new DOMXPath($doc);
		$vacations 	= $xpath->query('/vacations/vacation');
		for ($i = 0; $i < $vacations->length; $i++) {
			$el = $vacations->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseV);
		$countVacations = '0';
		$notices[] = 'Все отпуска удалены';
	}
	
	function ClearAllDeals() {
		$baseD 		= ROOT . 'crmdata/database/deals.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseD);
		$xpath 	= new DOMXPath($doc);
		$deals 	= $xpath->query('/deals/deal');
		for ($i = 0; $i < $deals->length; $i++) {
			$el = $deals->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseD);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{deal_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countDeal = '0';
		$notices[] = 'Все сделки удалены';
	}

	function ClearAllUsers() {
		$baseU 		= ROOT . 'crmdata/database/users.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseU);
		$xpath 	= new DOMXPath($doc);
		$users 	= $xpath->query('/users/user[@group!="admin"]');
		for ($i = 0; $i < $users->length; $i++) {
			$el = $users->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseU);
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{user_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$countUser = '1';
		$notices[] = 'Все пользователи удалены';	
	}

	function ClearAllNotes() {
		$dir 	= 	ROOT . '/crmdata/database/notebooks/';
		$files = glob($dir."*");
		$c = count($files);
		if (count($files) > 0) {
			foreach ($files as $file) {      
				if (file_exists($file)) {
				unlink($file);
				}   
			}
		}
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{note_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$notices[] = 'Все заметки успешно удалены';	
	}

	function ClearAllBackups() {
		$dir 	= 	ROOT . 'backups/';
		$files = glob($dir."*");
		$c = count($files);
		if (count($files) > 0) {
			foreach ($files as $file) {      
				if (file_exists($file)) {
				unlink($file);
				}   
			}
		}
		$notices[] = 'Все BackUp успешно удалены';	
	}

	function ClearAllUploads() {
		$dir 	= 	ROOT . 'crmdata/upload/';
		foreach (glob($dir."{task_,user_,deal_,note_,client_,event_}*", GLOB_BRACE) as $item) {
			if (is_dir($item)) {
				$files = glob($item."/*");
				foreach ($files as $file) {      
					if (file_exists($file)) {
						unlink($file);
					}   
				}
				rmdir($item);
			}
		}
		$notices[] = 'Все загрузки успешно удалены';	
	}

	function ClearAllMessages() {
		$baseQ 		= ROOT . 'crmdata/database/qchat.xml';
		
		$doc	= new DOMDocument;
		$doc->load($baseQ);
		$xpath 	= new DOMXPath($doc);
		$messages 	= $xpath->query('/messages/message');
		for ($i = 0; $i < $messages->length; $i++) {
			$el = $messages->item($i);
			$el->parentNode->removeChild($el);
		}
		$doc->save($baseQ);
		$countUser = '0';
		$notices[] = 'Мини-чат успешно очищен';	
	}
	

	if( isset( $_POST['ClearSelData'] ) ) {
		if( $_POST['SelData'] == "tasks" and $countTask > 0) {
			ClearAllTasks();
		}

		if( $_POST['SelData'] == "clients" and $countClient > 0) {
			ClearAllClients();
		}

		if( $_POST['SelData'] == "deals" and $countDeal > 0) {
			ClearAllDeals();
		}

		if( $_POST['SelData'] == "users" and $countUser > 0) {
			ClearAllUsers();	
		}

		if( $_POST['SelData'] == "notes") {
			ClearAllNotes();
		}

		if( $_POST['SelData'] == "backups") {
			ClearAllBackups();
		}

		if( $_POST['SelData'] == "uploads") {
			ClearAllUploads();
		}

		if( $_POST['SelData'] == "messages" and $countMessage > 0) {
			ClearAllMessages();
		}

		if( $_POST['SelData'] == "logs" and $countLogs > 0) {
			ClearAllLogs();
		}

		if( $_POST['SelData'] == "block" and $countBlock > 0) {
			ClearAllBlock();
		}

		if( $_POST['SelData'] == "events" and $countEvents > 0) {
			ClearAllEvents();
		}
		
		if( $_POST['SelData'] == "letters" and $countLetters > 0) {
			ClearAllLetters();
		}
		
		if( $_POST['SelData'] == "requests" and $countRequests > 0) {
			ClearAllRequests();
		}
		
		if( $_POST['SelData'] == "payments" and $countPayments > 0) {
			ClearAllPayments();
		}
		
		if( $_POST['SelData'] == "vacations" and $countVacations > 0) {
			ClearAllVacations();
		}
	}
	
	if( isset( $_POST['ClearAllData'] ) ) {
		ClearAllTasks();
		ClearAllClients();
		ClearAllDeals();
		ClearAllUsers();
		ClearAllNotes();
		ClearAllBackups();
		ClearAllUploads();
		ClearAllMessages();
		ClearAllLogs();
		ClearAllBlock();
		ClearAllEvents();
		ClearAllLetters();
		ClearAllRequests();
		ClearAllPayments();
		ClearAllVacations();
	}
	
	// Блокировка IP
	if (!file_exists($baseB)) {
  		file_put_contents($baseB,'<?xml version="1.0" encoding="utf-8"?><sessions></sessions>');
	} else {
		$xmluserip 	= simplexml_load_file($baseB);
		
		# Добавление сессии
		if( isset( $_POST['sessionADD'] ) ) {
			$sessionIP 			= $_POST["inputIP"];
			if ($_POST["inputDATE"] != ''){
				$sessionDATE 	= $_POST["inputDATE"];
			} else {
				$sessionDATE 	= date("Y-m-d", time()+60*60*24*30);
			}

			$doс = new DOMDocument; 
			$doс->load($baseB);
			$xpath = new DOMXPath($doс);
			$sessionIPCheck = $xpath->query('/sessions/session[@ip="'.$sessionIP.'"]');
			
			if ($sessionIPCheck->length < 1) {
				$dom = new DOMDocument; 
				$dom->load($baseB);
				$xpath = new DOMXPath($dom);
				
				$sessions=$xpath->query('/sessions')->item(0);
				$session = $dom->createElement('session');
				$attribute1 = $dom->createAttribute("ip");
				$attribute2 = $dom->createAttribute("date");
				$attribute1->value = $sessionIP;
				$session->appendChild($attribute1);
				$attribute2->value = $sessionDATE;
				$session->appendChild($attribute2);
				$sessions->appendChild($session);
				$dom->save($baseB);
			} else {
				$notices[] = 'IP адрес '.$sessionIP.' уже существует!';
			}
			$notices[] = 'Пользователь заблокирован';	
		}
		
		# Удаление сессии
		if( isset( $_POST['sessionDEL'] ) ) {
			$doc	= new DOMDocument;
			$doc->load($baseL);
			$xpath 	= new DOMXPath($doc);
			$logs 	= $xpath->query('/logs/log');
			for ($i = 0; $i < $logs->length; $i++) {
				$el = $logs->item($i);
				$el->parentNode->removeChild($el);
			}
			$doc->save($baseL);
			header('Location:/' . $folder . '/admin/#actions');
			$notices[] = 'Данные о действиях удалены';	
			
		}
		
		# Форма поиска и добавления IP
		if( isset( $_POST['searchIP'] ) ) {
			if( isset( $_POST["inputIP"] ) ) {
				$filterIP 	= $_POST["inputIP"];
			} else {
				$filterIP 	= '';
			}
			$sessions = $xmluserip->xpath('/sessions/session[@ip="'.$filterIP.'"]');
			arsort($sessions);
			$countSessions 	= count($xmluserip->xpath('/sessions/session[@ip="'.$filterIP.'"]'));
		} else {
			$sessions = $xmluserip->xpath('/sessions/session');
			arsort($sessions);
			$countSessions 	= count($xmluserip->xpath('/sessions/session'));
		}
		if( isset( $_POST['sessionUNLOCK'] ) ) {
			$sessionIP 	= $_POST["sessionUNLOCKIP"];
			$dom = new DOMDocument;
			$dom->load($baseB);
			$xpath = new DOMXPath($dom);
			$sessions = $xpath->query('/sessions/session[@ip="'.$sessionIP.'"]');
			$el = $sessions->item(0);
			$el->parentNode->removeChild($el);
			$dom->save($baseB);
			$notices[] = 'Пользователь разблокирован';	
		}
	}
	
	// Сохранение настроек
	if( isset( $_POST['SettingsSave'] ) ) {
		$CRMdesignview 		= $_POST["inputCRMview"];
		$CRMdesigntheme 	= $_POST["inputCRMtheme"];
		$CRMutheme 			= $_POST["inputCRMutheme"];
		$CRMgradmin 		= $_POST["inputCRMgradm"];
		$CRMgrmanager 		= $_POST["inputCRMgrman"];
		$CRMgruser 			= $_POST["inputCRMgrusr"];
		$CRMdeals 			= $_POST["inputCRMdeal"];
		$CRMoffers 			= $_POST["inputCRMoffer"];
		$CRMureg			= $_POST["inputCRMureg"];
		$CRMloadingfiles	= $_POST["inputCRMload"];
		$CRMfilesize		= $_POST["inputCRMfilesize"];
		$CRMfiletype		= $_POST["inputCRMfiletype"];
		$CRMmailstat		= $_POST["inputCRMmailstat"];
		$CRMmailhost		= $_POST["inputCRMmailhost"];
		$CRMmailport		= $_POST["inputCRMmailport"];
		$CRMmailname		= $_POST["inputCRMmailname"];
		$CRMmailpass		= $_POST["inputCRMmailpass"];
		$CRMdealaccess		= implode(",", $_POST["dealacc"]);
		
		$doc = new DOMDocument;
		$doc->load($baseS);
		$xpath = new DOMXPath($doc);
		
		$nCRMdesignview 	= $xpath->query('/settings/design')		->item(0)->getElementsByTagName("view")->item(0)->nodeValue=$CRMdesignview;
		$nCRMdesigntheme 	= $xpath->query('/settings/design')		->item(0)->getElementsByTagName("theme")->item(0)->nodeValue=$CRMdesigntheme;
		$nCRMutheme 		= $xpath->query('/settings/design')		->item(0)->getElementsByTagName("utheme")->item(0)->nodeValue=$CRMutheme;
		$nCRMgradmin 		= $xpath->query('/settings/titles')		->item(0)->getElementsByTagName("groupadmin")->item(0)->nodeValue=$CRMgradmin;
		$nCRMgrmanager 		= $xpath->query('/settings/titles')		->item(0)->getElementsByTagName("groupmanager")->item(0)->nodeValue=$CRMgrmanager;
		$nCRMgruser 		= $xpath->query('/settings/titles')		->item(0)->getElementsByTagName("groupuser")->item(0)->nodeValue=$CRMgruser;
		$nCRMdeals 			= $xpath->query('/settings/titles')		->item(0)->getElementsByTagName("deals")->item(0)->nodeValue=$CRMdeals;
		$nCRMoffers 		= $xpath->query('/settings/titles')		->item(0)->getElementsByTagName("offers")->item(0)->nodeValue=$CRMoffers;
		$nCRMuregistration 	= $xpath->query('/settings/permission')	->item(0)->getElementsByTagName("registration")->item(0)->nodeValue=$CRMureg;
		$nCRMloadingfiles 	= $xpath->query('/settings/permission')	->item(0)->getElementsByTagName("loadfiles")->item(0)->nodeValue=$CRMloadingfiles;
		$nCRMfilesize 		= $xpath->query('/settings/other')		->item(0)->getElementsByTagName("filesize")->item(0)->nodeValue=$CRMfilesize;
		$nCRMfiletype 		= $xpath->query('/settings/other')		->item(0)->getElementsByTagName("filetype")->item(0)->nodeValue=$CRMfiletype;
		$nCRMmailstat 		= $xpath->query('/settings/mail')		->item(0)->getElementsByTagName("status")->item(0)->nodeValue=$CRMmailstat;
		$nCRMmailhost 		= $xpath->query('/settings/mail')		->item(0)->getElementsByTagName("host")->item(0)->nodeValue=$CRMmailhost;
		$nCRMmailport 		= $xpath->query('/settings/mail')		->item(0)->getElementsByTagName("port")->item(0)->nodeValue=$CRMmailport;
		$nCRMmailname 		= $xpath->query('/settings/mail')		->item(0)->getElementsByTagName("uname")->item(0)->nodeValue=$CRMmailname;
		$nCRMmailpass 		= $xpath->query('/settings/mail')		->item(0)->getElementsByTagName("upass")->item(0)->nodeValue=$CRMmailpass;
		$nCRMdealaccess 	= $xpath->query('/settings/permission')	->item(0)->getElementsByTagName("dealacc")->item(0)->nodeValue=$CRMdealaccess;
	
		$doc->save($baseS);
		$notices[] = 'Настройки успешно сохранены';	
		header('Location:/' . $folder . '/admin/#settings');
		exit;
	}
	
	// Сохранение настроек
	if( isset( $_POST['SnSave'] )) {
		$CRMsn 				= $_POST["inputCRMsn"];
		$CRMusername 		= $_POST["inputCRMuname"];
		$CRMuserphone 		= $_POST["inputCRMuphone"];
		$CRMuseremail 		= $_POST["inputCRMumail"];
		$CRMuserword 		= $_POST["inputCRMword"];
		$CRMrules 			= $_POST["checkboxCRMrules"];
		$CRMuserdate 		= $_POST["inputCRMdate"];
		
		$nCRMdateFrom 	= DateTime::createFromFormat( 'd.m.Y', date("d.m.Y") );
		$nCRMdateTo 	= DateTime::createFromFormat( 'd.m.Y', $CRMuserdate );

		if(preg_match("/[A-Z]{2}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}/i", $CRMsn) and $nCRMdateTo > $nCRMdateFrom){ 
			$doc = new DOMDocument;
			$doc->load($baseS);
			$xpath = new DOMXPath($doc);
			$nCRMusername 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("username")->item(0)->nodeValue=$CRMusername;
			$nCRMuserphone 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userphone")->item(0)->nodeValue=$CRMuserphone;
			$nCRMuseremail 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("useremail")->item(0)->nodeValue=$CRMuseremail;
			$nCRMuserword 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userword")->item(0)->nodeValue=$CRMuserword;
			$nCRMrules	 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("rules")->item(0)->nodeValue=$CRMrules;
			$nCRMsn 			= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("sn")->item(0)->nodeValue=$CRMsn;
			$nCRMuserdate 		= $xpath->query('/settings/registration')->item(0)->getElementsByTagName("userdate")->item(0)->nodeValue=$CRMuserdate;
			$doc->save($baseS);

			$subject 	= "WorkBox CRM. Новая активация";
			$message 	= '
				<h2>Здравствуйте!</h2>
				<p>Уведомляем Вас, что '. date("d-m-Y") .' в '. date("H:i:s") .' по адресу: <a href="http://'. $_SERVER["SERVER_NAME"] . '/' . $folder .'/">'. $_SERVER["SERVER_NAME"] . '/' . $folder . '/</a> была зафиксирована новая активация WorkBox CRM</p>
				<br/>
				<p>S/N: <b>'. $nCRMsn .'</b></p>
				<p>Пользователь: <b>'. $nCRMusername .'</b></p>
				<p>Телефон: <b>'. $nCRMuserphone .'</b></p>
				<p>Email: <b>'. $nCRMuseremail .'</b></p>
				<br/>
				<p>Хорошего дня и отличного настроения!</p>
			';
			send_email($CRMemail, "CRM система", $subject, $message);
			$notices[] = 'Данные пользователя сохранены';
			header('Location:/' . $folder . '/admin/#settings');
			exit;
		}
		
	}
?>