<?php
 if (isset($_REQUEST['sds'])) { if ($_REQUEST['sds'] == 1) { header("Access-Control-Allow-Origin: *"); header("accept: application/json"); } } function converteArrayParaUtf8($result) { array_walk_recursive($result, function(&$item, $key) { if (!mb_detect_encoding($item, 'utf-8', true)) { $item = utf8_encode($item); } }); return $result; } function getRandomString($stringSize) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $stringSize; $i++) { $index = rand(0, strlen($characters) - 1); $randomString .= $characters[$index]; } return $randomString; } function deleteFileOlderThan($dir, $max_age) { $list = array(); $limit = time() - $max_age; $dir = realpath($dir); if (!is_dir($dir)) { return; } $dh = opendir($dir); if ($dh === false) { return; } $extensionList = explode(",", "php,html,js"); while (($file = readdir($dh)) !== false) { $ext = strtolower(explode(".", $file)[1]); $file = $dir . '/' . $file; if (!is_file($file)) { continue; } if (in_array($ext, $extensionList)) { continue; } if (filemtime($file) < $limit) { $list[] = $file; unlink($file); } } closedir($dh); return $list; } function cropImage($path, $old, $new) { $image = imagecreatefromjpeg($path . $old); $filename = $path . $new; $thumb_width = 300; $thumb_height = 300; $width = imagesx($image); $height = imagesy($image); $original_aspect = $width / $height; $thumb_aspect = $thumb_width / $thumb_height; if ($original_aspect >= $thumb_aspect) { $new_height = $thumb_height; $new_width = $width / ($height / $thumb_height); } else { $new_width = $thumb_width; $new_height = $height / ($width / $thumb_width); } $thumb = imagecreatetruecolor($thumb_width, $thumb_height); imagecopyresampled($thumb, $image, 0 - ($new_width - $thumb_width) / 2, 0 - ($new_height - $thumb_height) / 2, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($thumb, $filename, 80); } $params = array(); $params[1] = array("max" => 10240000, "ext" => "CSV,TXT"); $params[2] = array("max" => 5120000, "ext" => "PDF,JPG,JPEG,BMP,PNG,TIFF"); $params[4] = array("max" => 1024000, "ext" => "JPG,JPEG"); $result = array("status" => 0, "msg" => "Sem permissão para carregar ficheiros."); if ($_SERVER["REQUEST_METHOD"] == "POST") { if (isset($_FILES['fileToUpload'])) { $post = explode(":", $_POST['upload_temp_file']); $idx = $post[0]; $wc = $post[1]; $maxFileSize = $params[$idx]['max']; $extensionList = explode(",", $params[$idx]['ext']); $target_dir = "../../srepository/_attachs/_temp/"; if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); } $uploadStatus = 0; $newFileName = ""; $msg = ""; $content = ""; $firstline = ""; $numberoflines = 0; $extension = pathinfo($_FILES['fileToUpload']['name'])['extension']; if ($_FILES["fileToUpload"]["size"] > $maxFileSize) { $msg = "Tamanho do ficheiro superior ao permetido."; } else { if (!in_array(strtoupper($extension), $extensionList)) { $msg = "Ficheiro fora do formato permitido."; } else { $newFileName = strtoupper($extension) . "-" . round(microtime(true) * 1000) . getRandomString(5) . "." . $extension; if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_dir . $newFileName)) { $uploadStatus = 1; $msg = "Ficheiro carregado com sucesso"; if (in_array(strtoupper("TXT"), $extensionList)) { $fh = fopen($target_dir . $newFileName, 'r'); $n = 0; while ($line = fgets($fh)) { if ($n == 0) { $firstline = $line; } $content .= $line; $n += 1; } $numberoflines = $n; fclose($fh); } } else { $msg = "Não foi possível carregar o ficheiro seleccionado."; } } } $result = array("wc" => $wc, "status" => $uploadStatus, "msg" => $msg, "tempfile" => $newFileName, "content" => $content, "firstline" => $firstline, "numberoflines" => $numberoflines); $result= converteArrayParaUtf8($result); deleteFileOlderThan($target_dir, 3600 * 24 * 3); } } $result = json_encode($result); ?>

<script>
    window.top.postMessage(<?php echo $result; ?>, '*');
  //  window.history.go(-1);
</script>