admin.php 12.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
<?php
/*
Title: Administração

Funções utilizadas pela aplicação de administração de mapfiles.
É utilizado apenas pela interface de administração.

About: Licença

I3Geo Interface Integrada de Ferramentas de Geoprocessamento para Internet

Direitos Autorais Reservados (c) 2006 Ministério do Meio Ambiente Brasil
Desenvolvedor: Edmar Moretti edmar.moretti@mma.gov.br

Este programa é software livre; você pode redistribuí-lo
e/ou modificá-lo sob os termos da Licença Pública Geral
GNU conforme publicada pela Free Software Foundation;
tanto a versão 2 da Licença.
Este programa é distribuído na expectativa de que seja útil,
porém, SEM NENHUMA GARANTIA; nem mesmo a garantia implícita
de COMERCIABILIDADE OU ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA.
Consulte a Licença Pública Geral do GNU para mais detalhes.
Você deve ter recebido uma cópia da Licença Pública Geral do
GNU junto com este programa; se não, escreva para a
Free Software Foundation, Inc., no endereço
59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.

File: i3geo/classesphp/admin.php

19/6/2007

Include:
<carrega_ext.php>, <pega_variaveis.php>, <ms_configura.php>
*/
require_once("pega_variaveis.php");
error_reporting(0);
session_name("i3GeoPHP");
if (isset($g_sid))
{session_id($g_sid);}
session_start();
foreach(array_keys($_SESSION) as $k)
{
	eval("\$".$k."='".$_SESSION[$k]."';");
}
//
//carrega o phpmapscript
//
include_once ("carrega_ext.php");
//verifica se o cliente pode editar
require_once("../ms_configura.php");
require_once("../pacotes/cpaint/cpaint2.inc.php");
$editor = "nao";
foreach ($editores as $e)
{
	$e = gethostbyname($e);
	$ip = "UNKNOWN";
	if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
	else $ip = "UNKNOWN";
	if ($e == $ip){$editor="sim";}
}
if ($editor == "nao")
{
 	$cp = new cpaint();
 	$cp->set_data("erro. Voce nao e um editor cadastrado $ip");
 	$cp->return_data();
 	return;
}

$cp = new cpaint();
//faz a busca da função que deve ser executada
switch ($funcao)
{
	//retorna o mapfile atual como texto
	case "carregaMapFileAtual":
	$cp->register('carregaMapFileAtual');
	$cp->start();
	$cp->return_data();
	break;
	//salva o mapfile atual
	case "salvaMapFileAtual":
	$cp->register('salvaMapFileAtual');
	$cp->start();
	$cp->return_data();
	break;
	//lista os mapfiles do diretorio temas
	case "listaMapsTemas":
	$cp->register('listaMapsTemas');
	$cp->start();
	$cp->return_data();
	break;
	//le map file de um tema
	case "carregaMapFileTema":
	$cp->register('carregaMapFileTema');
	$cp->start();
	$cp->return_data();
	break;
	//salva o mapfile de um tema
	case "salvaMapFileTema":
	$cp->register('salvaMapFileTema');
	$cp->start();
	$cp->return_data();
	break;
	//cria um novo tema
	case "criaTema":
	$cp->register('criaTema');
	$cp->start();
	$cp->return_data();
	break;
	//retorna a lista de camadas atuais
	case "listaCamadasAtuais":
	$cp->register('listaCamadasAtuais');
	$cp->start();
	$cp->return_data();
	break;
	//retorna o texto de uma camada atual
	case "textoCamadaAtual":
	$cp->register('textoCamadaAtual');
	$cp->start();
	$cp->return_data();
	break;
	//inclui um tema no mapa
	case "incluirTema":
	$cp->register('incluirTema');
	$cp->start();
	$cp->return_data();
	break;
}
/*
Function: incluirTema

Inclui um tema no mapa atual.

O tema deve ser um dos existentes no diretório 'temas' do I3Geo.

A função listaMapsTemas retorna a lista de temas disponíveis.

Parameters:

cp - objeto CPAINT

locaplic - localização da instalação do I3Geo

tema - tema que será processado (mapfile). Deve existir no diretório i3geo/temas.

map_file - mapfile que será processado
*/
function incluirTema()
{
 	global $cp,$locaplic,$tema,$map_file;
	$mapa = ms_newMapObj($map_file);
	$mapt = ms_newMapObj($locaplic."/temas/".$tema);
	$temas = $mapt->getalllayernames();
	foreach ($temas as $t)
	{
		$layer = $mapt->getlayerbyname($t);
		$layer->set("status",MS_DEFAULT);
		$adiciona = ms_newLayerObj($mapa, $layer);
	}
	$salvo = $mapa->save($map_file);
 	$cp->set_data("ok");
}
/*
Function: textoCamadaAtual

Mostra o texto de uma camada existente no mapfile atual.

O layer é lido e transformado em uma string.

Parameters:

cp - objeto CPAINT

map_file - mapfile que será processado

tema - tema que será processado
*/
function textoCamadaAtual()
{
 	global $cp,$map_file,$tema;
	$mapa = ms_newMapObj($map_file);
	$nometemp = dirname($map_file)."/testexxx.map";
	if (file_exists($nometemp)){unlink($nometemp);}
	$ts = $mapa->getalllayernames();
	foreach ($ts as $t)
	{
		$layer = $mapa->getlayerbyname($t);
		if ($layer->getmetadata("nomeoriginal") != $tema)
		{$layer->set("status",MS_DELETE);}
	}
	$mapa->save($nometemp);
	$linhas = ms_TokenizeMap($map_file);
	$texto = "";
	for ($i=0;$i < count($linhas);$i++)
	{
		if ($linhas[$i] == "FONTSET")
		$texto .= "FONTSET ".$linhas[$i+1]."\n";
		if ($linhas[$i] == "SYMBOLSET")
		$texto .= "SYMBOLSET ".$linhas[$i+1]."\n";
	}
	$handle = fopen ($nometemp, "r");
	$linhas = array();
	while (!feof ($handle)) {
    	$buffer = fgets($handle);
    	if (trim($buffer) != "")
     	$linhas[] = trim($buffer);
	}
	fclose ($handle);
	$layer = "nao";
	$textolayer = array();
	$textos = array();
	$nome = "";
	for ($i=0;$i < count($linhas);$i++)
	{
		if ($linhas[$i] == "LAYER")
		{$layer = "sim";}
		if ($layer == "sim")
		{
			$textolayer[] = $linhas[$i];
		}
	}
	foreach ($textolayer as $t)
	{
		if (function_exists("mb_convert_encoding"))
		{$texto .= mb_convert_encoding($t,"UTF-8","ISO-8859-1")."\n";}
		else
		{$texto .= $t."\n";}
	}
 	$cp->set_data($texto);
}
/*
Function: listaCamadasAtuais

Lista as camadas atuais do mapa.

Retorna sempre o código original da camada, armazenado como o metadados NOMEORIGINAL.

Parameters:

cp - objeto CPAINT

map_file - mapfile que será processado

*/
function listaCamadasAtuais()
{
 	global $cp,$map_file;
	$mapa = ms_newMapObj($map_file);
	$temas = $mapa->getAllLayerNames();
	$lista = array();
	foreach ($temas as $tema)
	{
		$layer = $mapa->getlayerbyname($tema);
		$t = $layer->getmetadata("NOMEORIGINAL");
		if ($t == "")
		$t = $layer->getmetadata("nomeoriginal");
		if ($t != "")
		$lista[] = $t;
	}
 	$cp->set_data(implode(",",$lista));
}
/*
Function: carregaMapFileAtual

Retorna o arquivo mapfile atual como um texto.

Parameters:

cp - objeto CPAINT

map_file - mapfile que será processado

*/
function carregaMapFileAtual()
{
 	global $cp,$map_file;
 	$texto = file_get_contents($map_file);
	if (function_exists("mb_convert_encoding"))
	{$texto = mb_convert_encoding($texto,"UTF-8","ISO-8859-1");}
 	$cp->set_data($texto);
}
/*
Function: salvaMapFileAtual

Salva uma string como mapfile atual.

A string enviada para ser armazenada, deve ter alguns caracteres especiais substituídos.

Parameters:

cp - objeto CPAINT

map_file - mapfile que será processado

texto - texto que será gravado

flag - flag de abertura do arquivo

*/
function salvaMapFileAtual($texto,$flag)
{
 	global $cp,$map_file;
 	$texto = html_entity_decode($texto);
	if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
	{
		$texto = str_replace("###","\n\t",$texto);
		$texto = str_replace("\\\\","\\",$texto);
		$texto = str_replace("\\\"","\"",$texto);
		$texto = str_replace('\"\"','\" \"',$texto);
	}
	else
 	{
 		$texto = str_replace("###","\n",$texto);
 	}
 	unlink($map_file);
 	$f = fopen($map_file,$flag);
    $ok = fwrite($f,$texto);
    fclose($f);
    $nmf = str_replace(".map","seguranca.map",$map_file);
    if (file_exists($nmf))
    {unlink($nmf);}
    copy ($map_file,$nmf);
 	$cp->set_data($ok);
}
/*
Function: listaMapsTemas

Retorna a lista de mapfiles do diretorio i3geo/temas

Parameters:

cp - objeto CPAINT

locaplic - localização da instalação do I3Geo
*/
function listaMapsTemas()
{
 	global $cp,$locaplic;
	if (is_dir($locaplic."/temas"))
	{
   		if ($dh = opendir($locaplic."/temas")) 
		{
       		while (($file = readdir($dh)) !== false) 
			{$arquivos[] = $file;}
       	}
       	closedir($dh);
   	}
   	sort($arquivos);
 	$cp->set_data(str_replace(".,..,","",implode(",",$arquivos)));
}
/*
Function: carregaMapFileTema

Retorna o arquivo mapfile de um tema como um texto.

Parameters:

cp - objeto CPAINT

locaplic - localização da instalação do I3Geo

tema - nome do tema (mapfile)
*/
function carregaMapFileTema()
{
 	global $cp,$locaplic,$tema;
 	$texto = file_get_contents($locaplic."/temas/".$tema);
	if (function_exists("mb_convert_encoding"))
	{$texto = mb_convert_encoding($texto,"UTF-8","ISO-8859-1");}
 	$cp->set_data($texto);
}
/*
Function: salvaMapFileTema

Salva uma string como mapfile de um tema.

Parameters:

cp - objeto CPAINT

locaplic - localização da instalação do I3Geo

texto - texto que será salvo

flag - flag de abertura do arquivo

tema - nome do tema (mapfile)
*/
function salvaMapFileTema($texto,$flag,$tema)
{
 	global $cp,$locaplic;
 	$texto = html_entity_decode($texto);
	if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
	{
		$texto = str_replace("###","\n\t",$texto);
		$texto = str_replace("\\\\","\\",$texto);
		$texto = str_replace("\\\"","\"",$texto);
		$texto = str_replace('\"\"','\" \"',$texto);
	}
	else
 	{$texto = str_replace("###","\n",$texto);}
 	unlink($locaplic."/temas/".$tema);
 	$f = fopen($locaplic."/temas/".$tema, $flag);
	//if (function_exists(mb_convert_encoding))
	//{$texto = mb_convert_encoding($texto,"UTF-8","ISO-8859-1");}//,"UTF-8");}
    $ok = fwrite($f,$texto);
    fclose($f);
 	$cp->set_data("Tema gravado!");
}
/*
Function: criaTema

Cria um novo mapfile e adiciona no mapa atual.

Parameters:

cp - objeto CPAINT

map_file - mapfile atual

locaplic - localização da instalação do I3Geo

arquivo - nome do novo tema

tipodados - tipo de dado banco|

nomearquivo - nome do shape file se tipodados for diferente de banco

conexaobanco - string de conexão com o banco, se tipodados for igual a banco

sql - sql de busca dos dados

tiporepresentacao - tipo de tema MS_LAYER_POINT|MS_LAYER_LINE|MS_LAYER_POLYGON|MS_LAYER_RASTER

opacidade - transparência do tema

nometema nome do tema na legenda

escala escala da fonte

download - permite o download ou não sim|nao
*/
function criaTema($arquivo,$tipodados,$nomearquivo,$conexaobanco,$sql,$tiporepresentacao,$opacidade,$nometema,$escala,$download)
{
 	global $cp,$map_file,$locaplic;
 	$arq = $locaplic."/temas/".$arquivo.".map";
 	if ($arquivo == "testamapa")
 	{
 		if (file_exists($arq))
 		{unlink($arq);}
 	}
 	//cria o arquivo map
 	if (file_exists($arq))
 	{$cp->set_data("Erro. Arquivo $arq ja existe");return;}
 	else
 	{
 		if (file_exists($arq))
 		{unlink ($arq);}
		if ($tiporepresentacao == "MS_LAYER_POINT"){$tipocamada = "point";}
		if ($tiporepresentacao == "MS_LAYER_LINE"){$tipocamada = "line";}
		if ($tiporepresentacao == "MS_LAYER_POLYGON"){$tipocamada = "polygon";}
		if ($tiporepresentacao == "MS_LAYER_RASTER"){$tipocamada = "raster";}
 		$f = fopen($arq,w);
 		$texto = "SYMBOLSET ../symbols/simbolos.sym\n";
 		fwrite($f,$texto);
		$texto = "FONTSET   ".'"'."../symbols/fontes.txt".'"'."\n";
		fwrite($f,$texto);
		$texto = "LAYER\nNAME $arquivo \nTYPE $tipocamada \nSTATUS default \n";
		fwrite($f,$texto);
		if ($tipodados == "banco")
		{
			$texto = "CONNECTIONTYPE postgis\n
					CONNECTION '$conexaobanco'\n";
			fwrite($f,$texto);
			$texto = "DATA ".'"'.$sql.'"';
			fwrite($f,$texto);
		}
		else
		{
			$texto = "DATA $nomearquivo ";
			fwrite($f,$texto);
		}
		$texto = "\n TEMPLATE ".'"'."none.htm".'"'."\n"."METADATA\n";
		fwrite($f,$texto);
  		$texto = "TEMA ".'"'.$nometema.'"'."\n";
  		fwrite($f,$texto);
		$texto = 'CLASSE "SIM" ';
		fwrite($f,$texto);
		$texto = "\n".'nomeoriginal "'.$arquivo.'" ';
		fwrite($f,$texto);
		$texto = "\nESCALA ".'"'.$escala.'"';
		fwrite($f,$texto);
		$texto = "\nDOWNLOAD ".'"'.$download.'"';
		fwrite($f,$texto);
		$texto = "\nEND ";
		fwrite($f,$texto);
		$texto = "\nTRANSPARENCY $opacidade";
		fwrite($f,$texto);
		$texto = "\nCLASS\nNAME ' '\nCOLOR 255 0 0\nOUTLINECOLOR 0 255 255\nSIZE 8\nEND\nEND\nEND\n";
 		fwrite($f,$texto);
    	fclose($f);
    	chmod($arq,0777); 		
		$mapa = ms_newMapObj($map_file);
		$mapatmp = ms_newMapObj($arq);
		$temp = $mapatmp->getlayerbyname($arquivo);
	 	$adiciona = ms_newLayerObj($mapa, $temp);
	 	$salvo = $mapa->save($map_file);
	 	$cp->set_data("Ok. Camada criada. Utilize as opcoes de edicao de legenda para configurar o mapa.");
	}
}
?>