function gerar_nome_valido($extensao, $dir, $len = 15) { // Gera um nome único para o arquivo $temp = substr(md5(uniqid(time())), 0, $len); $arquivo_nome = !empty($extensao) ? "{$temp}.{$extensao}" : $temp; // Verifica se o arquivo já existe, caso positivo, chama essa função novamente if (file_exists($dir . $arquivo_nome)) { $arquivo_nome = gerar_nome_valido($extensao, $dir); } return $arquivo_nome; }
/** * upload_remoto efetua o download/copia de uma imagem de outro servidor pra esse * @param string a url da imagem * @return string nome/endereço da imagem ou mensagem em caso de erro */ private function upload_remoto($url = '', $tipo = 'upload_remote') { $imgs = isset($_SESSION['imgs_upadas']) ? $_SESSION['imgs_upadas'] : array(); if (count($imgs) >= $this->__config['uploadMax']) { return '["' . $tipo . '", "erro", "' . $this->msgs['limite_uploads'] . '"]'; } $url = urldecode($url); if (!val('url', $url)) { return '["' . $tipo . '", "erro", "' . $this->msgs['url_invalido'] . '"]'; } $header = getheader($url); if (!is_array($header)) { return '["' . $tipo . '", "erro", "' . $this->msgs['servidor_nao_respondeu'] . '"]'; } if ($header['http_code'] != 200 && isset($this->__config['http_code'][$header['http_code']])) { $erro = str_replace('%file%', $url, $this->__config['http_code'][$header['http_code']]); return '["' . $tipo . '", "erro", "' . $erro . '"]'; } if (!isset($header['download_content_length']) || $header['download_content_length'] == 0) { return '["' . $tipo . '", "erro", "' . str_replace('%file%', $url, $this->msgs['servidor_nao_tamanho']) . '"]'; } if ($header['download_content_length'] > $this->__config['_tamanho']) { return '["' . $tipo . '", "erro", "' . $this->msgs['arquivo_muito_grande'] . '"]'; } $mime = $header['content_type']; $is_image = $this->is_image($mime); if (!$is_image) { return '["' . $tipo . '", "erro", "' . $this->msgs['arquivo_incorreto'] . '"]'; } $img = file_get_contents($url); if ($img) { $nome = gerar_nome_valido($is_image, $this->__config['diretorio']); if (file_put_contents($this->__config['diretorio'] . $nome, $img)) { require_once 'classes/upload/class.upload.php'; $new_img = new Upload($this->__config['diretorio'] . $nome, 'pt_BR'); $new_img->image_max_width = $this->__config['larguraMax']; $new_img->image_max_height = $this->__config['alturaMax']; $new_img->image_resize = $this->__config['resize']; $new_img->image_ratio_x = true; $new_img->image_ratio_y = true; $new_img->image_x = $this->__config['img_x']; $new_img->image_y = $this->__config['img_y']; $new_img->image_convert = $this->__config['convert_to']; $new_img->jpeg_quality = $this->__config['jpeg_quality']; $new_img->image_text = $this->__config['image_text']; $new_img->image_text_direction = $this->__config['image_text_direction']; $new_img->image_text_color = $this->__config['image_text_color']; $new_img->image_text_percent = $this->__config['image_text_percent']; $new_img->image_text_position = $this->__config['image_text_position']; $new_img->image_text_alignment = $this->__config['image_text_alignment']; $new_img->process($this->__config['diretorio']); if ($new_img->processed) { $new_img->clean(); clearstatcache(); $this->set($new_img->file_dst_name); return '["' . $tipo . '", "' . $new_img->file_dst_name . '"]'; } else { $new_img->clean(); @unlink($this->__config['diretorio'] . $new_img->file_dst_name); clearstatcache(); return '["' . $tipo . '", "erro", "' . $new_img->error . '"]'; } } else { return '["' . $tipo . '", "erro", "' . $this->msgs['nao_copio_image'] . '"]'; } } else { return '["' . $tipo . '", "erro", "' . $this->msgs['nao_copio_image'] . '"]'; } return '["' . $tipo . '", "erro", "' . $this->msgs['erro_desconhecido'] . '"]'; }