<?php //модуль для подключения к админке контента всех текстовых и html блоков WSX5 imTextObject  imHTMLObjects
	sleep(1);
	clearstatcache();
	function func_searchdiv ($st, $ofs){ 
		$re = '#(\<div|\<\/div\>)#'; $cntr=0; 
		$length_st=mb_strlen($st,'UTF-8');
		while ($ofs<$length_st) {
			preg_match($re, $st, $p, PREG_OFFSET_CAPTURE, $ofs);
			//echo "<pre>"; print_r($p); echo "</pre>";
			$ofs=$p[0][1]+5; 
			if ($p[0][0]=='<div') {$cntr=$cntr+1; }
			if ($p[0][0]=='</div>') {$cntr=$cntr-1; }
			if ($cntr==0) {return $ofs; break;}
			if ($cntr<0) {return false; break;}
		}
		if ($cntr>0) {return false;}
	}
	$mset=array();
	$check_l=file_get_contents("../../sg_cm/admin/index.php");
	$check_ls=substr($check_l,strpos($check_l,'$sn="0')+7,1);
	if (!is_file('../conf/set.php') || is_file('sg_cm_code.php')) die ('неверное расположение модуля сканирования');
	if ($check_ls=='0' || strpos($check_l,'$sn="0')===false ) die ('устаревшая версия cms');
	$mset=file('../conf/set.php');
	$file0=''; $file=''; $out=array();	$titleout=array();
	foreach ($PG as $key => $file0) 
	{ 

		$titleout=array();
		$file=basename($file0);
		if (is_file('../../'.$file.'.php')) {$file.='.php';}
		elseif (is_file('../../'.$file.'.html')) {$file.='.html';}
		else {continue;}
		$f=file_get_contents('../../'.$file); 
		if(strpos($f,'<!--noedit-->')===false && strpos($f,'<!--edit-->')!==false ) $edit=1; else $edit=0;
//		if (md5_file('../../sg_cm/sg_cm/style/top.png') === '963d8c9e3ee09c34379fda89e4173052') {}
//		else {echo __LINE__;  exit;}
		
		if(strpos($f,"<?php include_once 'sg_cm/include/sg_cm_code.php';?>")===false) 
		{$f=str_replace("</head>", "<?php @include_once 'sg_cm/include/sg_cm_code.php';?></head>", $f);}
		
		
		if ($mset[13]=='checked'."\r\n"){ 
		}
		
		
		$out=array();	$titleout=array();
		if ($mset[0]=='checked'."\r\n"){
			
			preg_match_all('!<div id="imFooter_imCellStyle_(\d+)"!', $f, $out, PREG_PATTERN_ORDER);
			for ($k=0; $k<count($out[0]); $k++) 
			{$pos1=strpos($f,$out[0][$k]); 
				$pos2=func_searchdiv($f, $pos1);
				if ($pos2===false) continue;
				$lng_html=$pos2-$pos1+1;
				$hsubstr=substr($f,$pos1,$lng_html);
				if (strpos($hsubstr,'<!--noedit-->')!==false || strpos($hsubstr,'<?')!==false || strpos($hsubstr,'<style')!==false) {continue;}
				elseif ($edit==1 && strpos($hsubstr,'<!--edit-->')===false) {continue;}
				else
				{$cellfile='../content/'.$file0.'/Cell'.$out[1][$k].'.txt';
					$cellnum=$out[1][$k];
					$f=str_replace($hsubstr, '<div class=tmce><? blocktxt($cp.\'Cell'.$cellnum.'.txt\') ?> </div>' , $f);
					preg_match('!<div id="imCellStyleTitle_'.$cellnum.'">(.*?)<\/div>!su', $f, $titleout);
					$f=preg_replace('!<div id="imCellStyleTitle_'.$cellnum.'">.*?<\/div>!su', '<div id="imCellStyleTitle_'.$cellnum.'"><? bz($cp.\'Cell'.$cellnum.'.txt\')?></div>' , $f);
					if (!is_file($cellfile))
					{$hsubstr=str_replace('<!--noedit-->', '' , $hsubstr);$hsubstr=str_replace('<!--edit-->', '' , $hsubstr);
					file_put_contents ($cellfile, $titleout[1].'**:***:*:'.$hsubstr);}}
					file_put_contents('../../'.$file, $f);
			}
			$out=array();	$titleout=array();
			preg_match_all('!<div id="imCellStyle_(\d+)".*?id="imTableObject_\d+!', $f, $out, PREG_PATTERN_ORDER);
			for ($k=0; $k<count($out[0]); $k++) 
			{$pos1=strpos($f,$out[0][$k]); 
				$pos2=func_searchdiv($f, $pos1);
				if ($pos2===false) continue;
				$lng_html=$pos2-$pos1+1;
				$hsubstr=substr($f,$pos1,$lng_html);
				if (strpos($hsubstr,'<!--noedit-->')!==false || strpos($hsubstr,'<?')!==false || strpos($hsubstr,'<style')!==false) {continue;}
				elseif ($edit==1 && strpos($hsubstr,'<!--edit-->')===false) {continue;}
				else
				{$cellfile='../content/'.$file0.'/Cell'.$out[1][$k].'.txt';
					$cellnum=$out[1][$k];
					$f=str_replace($hsubstr, '<div class=tmce><? blocktxt($cp.\'Cell'.$cellnum.'.txt\') ?> </div>' , $f);
					preg_match('!<div id="imCellStyleTitle_'.$cellnum.'">(.*?)<\/div>!su', $f, $titleout);
					$f=preg_replace('!<div id="imCellStyleTitle_'.$cellnum.'">.*?<\/div>!su', '<div id="imCellStyleTitle_'.$cellnum.'"><? bz($cp.\'Cell'.$cellnum.'.txt\')?></div>' , $f);
					if (!is_file($cellfile))
					{$hsubstr=str_replace('<!--noedit-->', '' , $hsubstr);$hsubstr=str_replace('<!--edit-->', '' , $hsubstr);
					file_put_contents ($cellfile, $titleout[1].'**:***:*:'.$hsubstr);}}
					file_put_contents('../../'.$file, $f);
			}
		}
		$outh=array();	$titleout=array();
		if ($mset[1]=='checked'."\r\n"){
			preg_match_all('!<div id="imCellStyle_(\d+)".*?id="imHTMLObject_\d+!', $f, $outh, PREG_PATTERN_ORDER);
			for ($k=0; $k<count($outh[0]); $k++) 
			{$pos1=strpos($f,$outh[0][$k]); 
				$pos2=func_searchdiv($f, $pos1);
				if ($pos2===false) continue;
				$lng_html=$pos2-$pos1+1;
				$hsubstr=substr($f,$pos1,$lng_html);
				if (strpos($hsubstr,'<!--noedit-->')!==false || strpos($hsubstr,'<?')!==false || strpos($hsubstr,'<style')!==false) {continue;}
				elseif ($edit==1 && strpos($hsubstr,'<!--edit-->')===false) {continue;}
				else
				{$cellfile='../content/'.$file0.'/Cell'.$outh[1][$k].'.txt';
					$cellnum=$outh[1][$k];
					$f=str_replace($hsubstr, '<div class=tmce><? blocktxt($cp.\'Cell'.$cellnum.'.txt\') ?> </div>' , $f);
					preg_match('!<div id="imCellStyleTitle_'.$cellnum.'">(.*?)<\/div>!su', $f, $titleout);
					$f=preg_replace('!<div id="imCellStyleTitle_'.$cellnum.'">.*?<\/div>!su', '<div id="imCellStyleTitle_'.$cellnum.'"><? bz($cp.\'Cell'.$cellnum.'.txt\')?></div>' , $f);
					if (!is_file($cellfile))
					{$hsubstr=str_replace('<!--noedit-->', '' , $hsubstr);$hsubstr=str_replace('<!--edit-->', '' , $hsubstr);
					file_put_contents ($cellfile, $titleout[1].'**:***:*:'.$hsubstr);}}
					file_put_contents('../../'.$file, $f);
			}
		}
		$outh=array();	$titleout=array();
		if ($mset[2]=='checked'."\r\n"){
			preg_match_all('!<div id="imCellStyle_(\d+)".*?id="imObjectImage_\d+"!', $f, $outh, PREG_PATTERN_ORDER);
			for ($k=0; $k<count($outh[0]); $k++) 
			{$pos1=strpos($f,$outh[0][$k]); 
				$pos2=func_searchdiv($f, $pos1);
				if ($pos2===false) continue;
				$lng_html=$pos2-$pos1+1;
				$hsubstr=substr($f,$pos1,$lng_html);
				if (strpos($hsubstr,'<!--noedit-->')!==false || strpos($hsubstr,'<?')!==false || strpos($hsubstr,'<style')!==false) {continue;}
				elseif ($edit==1 && strpos($hsubstr,'<!--edit-->')===false) {continue;}
				else
				{$cellfile='../content/'.$file0.'/Image'.$outh[1][$k].'.txt';
					$cellnum=$outh[1][$k];
					$f=str_replace($hsubstr, '<div class=tmce><? blocktxt($cp.\'Image'.$cellnum.'.txt\') ?> </div>' , $f);
					preg_match('!<div id="imCellStyleTitle_'.$cellnum.'">(.*?)<\/div>!su', $f, $titleout);
					$f=preg_replace('!<div id="imCellStyleTitle_'.$cellnum.'">.*?<\/div>!su', '<div id="imCellStyleTitle_'.$cellnum.'"><? bz($cp.\'Image'.$cellnum.'.txt\')?></div>' , $f);
					if (!is_file($cellfile))
					{$hsubstr=str_replace('<!--noedit-->', '' , $hsubstr);$hsubstr=str_replace('<!--edit-->', '' , $hsubstr);
					file_put_contents ($cellfile, $titleout[1].'**:***:*:'.$hsubstr);}}
					file_put_contents('../../'.$file, $f);
			}
		}
		
		$outh=array();	$titleout=array();
		if ($mset[2]=='checkedG'."\r\n"){
			preg_match_all('!<div id="imCellStyle_(\d+)".*?id="imObjectGallery_\d+!', $f, $outh, PREG_PATTERN_ORDER);
			for ($k=0; $k<count($outh[0]); $k++) 
			{$pos1=strpos($f,$outh[0][$k]); 
				$pos2=func_searchdiv($f, $pos1);
				if ($pos2===false) continue;
				$lng_html=$pos2-$pos1+1;
				$hsubstr=substr($f,$pos1,$lng_html);
				if (strpos($hsubstr,'<!--noedit-->')!==false || strpos($hsubstr,'<?')!==false || strpos($hsubstr,'<style')!==false) {continue;}
				elseif ($edit==1 && strpos($hsubstr,'<!--edit-->')===false) {continue;}
				else
				{$cellfile='../content/'.$file0.'/Blok'.$outh[1][$k].'.txt';
					$cellnum=$outh[1][$k];
					$f=str_replace($hsubstr, '<div class=tmce><? blocktxt($cp.\'Blok'.$cellnum.'.txt\') ?> </div>' , $f);
					preg_match('!<div id="imCellStyleTitle_'.$cellnum.'">(.*?)<\/div>!su', $f, $titleout);
					$f=preg_replace('!<div id="imCellStyleTitle_'.$cellnum.'">.*?<\/div>!su', '<div id="imCellStyleTitle_'.$cellnum.'"><? bz($cp.\'Blok'.$cellnum.'.txt\')?></div>' , $f);
					if (!is_file($cellfile))
					{$hsubstr=str_replace('<!--noedit-->', '' , $hsubstr);$hsubstr=str_replace('<!--edit-->', '' , $hsubstr);
					file_put_contents ($cellfile, $titleout[1].'**:***:*:'.$hsubstr.'**:***:*:3');}}
					file_put_contents('../../'.$file, $f);
			}
		}
		
	}
?>