애드온 : 외부 이미지 저장 애드온

제작자 : 카르마

수정자 : samsaram

표기버전 : 0.5.0

설치경로 : ./addons/auto_outimage/


팁 목차:
1. 로그 기록해서 php 디버그하기 (2의 내용을 찾아서 수정하는 방법을 중심으로)

2. 이미지파일명 확장자가 php로 끝날 경우 jpg를 붙여주기

 

1. 디버그해보기

※ 더 좋은 XE 애드온 디버깅 방법이 있으면 알려주시기 바랍니다. 

 

1. 디버깅하기

1) 디버깅 소스

<?php
if(!defined("__ZBXE__")) exit();

/**
 * @file ./addons/auto_outimage/auto_outimage.func.php
 * @author samsara
 * @brief Function collections for the implementation of image auto downloading
 * */

// Extract image urls from a string
function extractImage($str) {
   $dom = new domDocument;
   @$dom->loadHTML($str);
   $dom->preserveWhiteSpace = false;
   $images = $dom->getElementsByTagName('img');

   $imgUrls = array();
   foreach ($images as $key => $imageUrl) {
      $imgUrls[$key] = $imageUrl->getAttribute('src');
   }
   return $imgUrls;
}

// Make sure the image url belongs to the exceptional list
function belongTo($imgUrl,$exceptionList) {
   $imgUrlTmp = parse_url($imgUrl);
   $remoteAddress = substr($imgUrl, 0, strpos($imgUrl, $imgUrlTmp["host"])) . $imgUrlTmp["host"];
   foreach ($exceptionList as $member) {
      if(!strcmp($remoteAddress,$member))
         return TRUE;
   }
   return FALSE;
}
function eondLog($logLine)
{
   $logFilePath = "./addons/auto_outimage/eondLog.txt";
   $ff = fopen($logFilePath, "a");
   fwrite($ff, date("Ymd H:i:s")."\t".$logLine."\n"."\n");
   fclose($ff);
}
?>

eondLog라는 함수(?)를 만들어서 해당 경로에 eondLog.txt라는 파일을 생성해서 

오류 내용을 찾습니다.

 

2) 그리고 원하는 곳에 함수를 실행시킵니다.

eondLog("case #1");
eondLog($ri_localfile);
eondLog($url['path']);
eondLog("name:".$file_info['name']);
eondLog("name:".$file_info['tmp_name']);

이런 식으로 변수명도 집어넣어서 오류 내용을 확인할 수가 있습니다.

※ XE에서 더 쉬운 디버깅 방법이 있으면 알려주세요. debugPrint();도 가능한가요?; 어떻게 쓰는지 잘 몰라서;;

 

 

2. 이미지파일명 확장자가 php로 끝날 경우 jpg를 붙여주기

- auto_outimage.addon.php -

<?php
if(!defined("__ZBXE__")) exit();

/**
 * @auto_outimage.addon.php
 * @author 카르마(http://www.wildgreen.co.kr)
 * ver.0.5.1
 **/

$oDocumentModel = &getModel('document');

$act_arr = array('dispBoardContent','dispBodexContent','dispTextyle','dispBodexContentView','dispBoardContentView');
$document_act = array('dispBoardContent','procBoardInsertDocument','procBodexInsertDocument');
$comment_act = array('procBoardInsertComment','procBodexInsertComment');

$ri_avoid_domain = explode(',', $addon_info->ri_avoid_domain);
array_unshift($ri_avoid_domain,$_SERVER['HTTP_HOST']);
require_once('./addons/auto_outimage/auto_outimage.func.php');

//eondLog("23234234"); // 에러로그 생성하기
if($called_position == 'after_module_proc' && in_array($this->act,$document_act)) {
   eondLog("case #1");
   $var = $this->variables;
   $document_srl = $var[document_srl];
   if(!$document_srl) return;
   $module_info = $oModuleModel->getModuleInfoByDocumentSrl($document_srl);
   $args->document_srl = $document_srl;
   $output = executeQuery('document.getDocument', $args);
   $content = $output->data->content;

   $upload_target_srl = $document_srl;
   $oFileController = &getController('file');


   $contImg = extractImage($content);
   if(count($contImg)) {
      $replace = $content;
      foreach($contImg as $src) {//반복문 이미지 갯수만큼 회전
         if(belongTo($src,$ri_avoid_domain))
            continue;
         $ri_localfile = $src; //이건 임시파일이게 될거구
         eondLog($ri_localfile);
         $url = parse_url($ri_localfile);

         $path_parts = pathinfo($url['path']);
         eondLog($url['path']);
         $file_info['name']=$path_parts['basename'];
         // $file_info['name'].=".jpg";

         $file_info['name'] .= preg_match("/\.jpg$/i", $file_info['name']) ? "" : ".jpg";

         eondLog("name:".$file_info['name']);
         $file_info['tmp_name']=$ri_localfile;
         eondLog("name:".$file_info['tmp_name']);
         $file_obj = $oFileController->insertFile($file_info,$module_info->module_srl,$upload_target_srl,0,true);
         if(@$file_obj->variables['uploaded_filename']!=null) $replace = str_replace($src,$file_obj->variables['uploaded_filename'],$replace);
         @unlink($ri_localfile); // 파일을 지우는 함수
      }


      $obj->module_srl = $module_info->module_srl;
      $obj->content = $replace;
      $obj->document_srl = $document_srl;
      executeQueryArray('addons.auto_outimage.updateDocument', $obj);
   }
} elseif($addon_info->ri_comment_get != 'N' && $called_position == 'after_module_proc' && in_array($this->act,$comment_act)) {
   eondLog("case #2");
   $var = $this->variables;
   $document_srl = $var[document_srl];
   $module_info = $oModuleModel->getModuleInfoByDocumentSrl($document_srl);
   $comment_srl = $var[comment_srl];
   if(!$comment_srl) return;
   $oCommentModel = &getModel('comment');
   $comment = $oCommentModel->getComment($comment_srl);
   $content = $comment->getContent(false,false,false,false);

   $upload_target_srl = $comment_srl;
   $oFileController = &getController('file');

   $contImg = extractImage($content);
   if(count($contImg)) {
      $replace = $content;
      foreach($contImg as $src) {
         if(belongTo($src,$ri_avoid_domain))
            continue;
         $ri_localfile = $src;
         $url = parse_url($ri_localfile);
         $path_parts = pathinfo($url['path']);
         $file_info['name']=$path_parts['basename'];
         $file_info['tmp_name']=$ri_localfile;
         $file_obj = $oFileController->insertFile($file_info,$module_info->module_srl,$upload_target_srl,0,true);
         if(@$file_obj->variables['uploaded_filename']!=null) $replace = str_replace($src,$file_obj->variables['uploaded_filename'],$replace);
         @unlink($ri_localfile);

      }
      $obj->module_srl = $module_info->module_srl;
      $obj->content = $replace;
      $obj->comment_srl = $comment_srl;
      $output = executeQueryArray('addons.auto_outimage.updateComment', $obj);
   }
}
?>

 

해당 소스

$file_info['name'] .= preg_match("/\.jpg$/i", $file_info['name']) ? "" : ".jpg";

 

정규식을 사용해서 php내장함수인 preg_match를 사용해서 jpg가 아닌 경우 jpg를 붙여줍니다.

 

 

 

이온디

profile
이온디는 라이믹스를 비롯한 다양한 CMS의 시드뱅크를 꿈꿉니다. 여러분들이 사랑하는 웹소스를 언제든지 사용할 수 있게 하기 위해 이온디는 매일 소스코드를 유지보수하고 있으며, 언제든지 다운로드할 수 있는 소스마켓을 운영하고 있습니다.

#XE마켓 - 이온디스토어
https://eond.com/xemarket/

# XE/라이믹스 단톡방을 운영 중입니다. (비번: 2022)
https://open.kakao.com/o/giaKKnl

# XE/라이믹스 생활코딩 모듈 강좌입니다.
https://opentutorials.org/module/3774