/**
* 替换编辑器内容中的文件地址
* @param string $content 编辑器内容
* @param boolean $isForDbSave true:表示把绝对地址换成相对地址,用于数据库保存,false:表示把相对地址换成绝对地址用于界面显示
* @return string
*/
function cmf_replace_content_file_url($content, $isForDbSave = false)
{
import('phpQuery.phpQuery', EXTEND_PATH);
\phpQuery::newDocumentHTML($content);
$pq = pq(null);
$storage = Storage::instance();
$localStorage = new cmf\lib\storage\Local([]);
$storageDomain = $storage->getDomain();
$domain = request()->host();
$images = $pq->find("img");
if ($images->length) {
foreach ($images as $img) {
$img = pq($img);
$imgSrc = $img->attr("src");
if ($isForDbSave) {
if (preg_match("/^\/upload\//", $imgSrc)) {
$img->attr("src", preg_replace("/^\/upload\//", '', $imgSrc));
} elseif (preg_match("/^http(s)?:\/\/$domain\/upload\//", $imgSrc)) {
$img->attr("src", $localStorage->getFilePath($imgSrc));
} elseif (preg_match("/^http(s)?:\/\/$storageDomain\//", $imgSrc)) {
$img->attr("src", $storage->getFilePath($imgSrc));
}
} else {
$img->attr("src", cmf_get_image_url($imgSrc));
}
}
}
$links = $pq->find("a");
if ($links->length) {
foreach ($links as $link) {
$link = pq($link);
$href = $link->attr("href");
if ($isForDbSave) {
if (preg_match("/^\/upload\//", $href)) {
$link->attr("href", preg_replace("/^\/upload\//", '', $href));
} elseif (preg_match("/^http(s)?:\/\/$domain\/upload\//", $href)) {
$link->attr("href", $localStorage->getFilePath($href));
} elseif (preg_match("/^http(s)?:\/\/$storageDomain\//", $href)) {
$link->attr("href", $storage->getFilePath($href));
}
} else {
if (!(preg_match("/^\//", $href) || preg_match("/^http/", $href))) {
$link->attr("href", cmf_get_file_download_url($href));
}
}
}
}
$content = $pq->html();
\phpQuery::$documents = null;
return $content;
}
simplewind\cmf\common.php 里面加载了phpquery。composer require jaeger/querylist 的时候重复加载phpquery 导致发生错误。