c5_labsci/zciyphp/web.php

289 lines
12 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/* =================================================================================
* License: GPL-2.0 license
* Author: 众产® https://ciy.cn/code
* Version: 0.7.8
====================================================================================*/
/*
* web.php 常用公共函数库
*
* 页面相关
* diehtml 报错输出页面/跳转
* markdown_convert Markdown转换为HTML
* inzone
*/
namespace ciy;
class web {
public static function diehtml($msg, $title = '提示信息') {
echo '<!DOCTYPE html><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8">';
echo '<title>' . $title . '</title><meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0"/><meta name="format-detection" content="telephone=no,email=no"/>';
echo '<meta name="apple-mobile-web-app-capable" content="yes" /></head><body>';
$selfname = substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1, -4);
echo '<fieldset style="margin:2em;border-radius: 0.5em;border: 1px solid #eeeeee;line-height:2em;"><legend style="font-size: 1.3em;padding: 0.2em 0.5em;">' . $title . '</legend><div style="padding:0 15px 15px 15px;"><b>来源: ' . str_replace('.php', '', $selfname) . '</b><br/>' . $msg . '</div></fieldset>';
echo '</body></html>';
die();
}
public static function markdown_convert($markdown) {
if (substr($markdown, 0, 4) == '[MD]')
$markdown = substr($markdown, 4);
$mds = explode("\n", trim($markdown));
$html = '';
foreach ($mds as $mi) {
if (empty($mi)) {
$html .= '<br/>';
continue;
}
if ($mi[0] == '#') { //#一级标题 ##二级标题 ###三级标题
if (substr($mi, 0, 3) == '###')
$html .= '<h3 class="md-h3">' . self::markdown_convertcode(substr($mi, 3)) . '</h3>';
else if (substr($mi, 0, 2) == '##')
$html .= '<h2 class="md-h2">' . self::markdown_convertcode(substr($mi, 2)) . '</h2>';
else if ($mi[1] == 'c')
$html .= '<h1 class="md-h1" style="text-align:center;">' . self::markdown_convertcode(substr($mi, 2)) . '</h1>';
else if ($mi[1] == 'r')
$html .= '<h1 class="md-h1" style="text-align:right;">' . self::markdown_convertcode(substr($mi, 2)) . '</h1>';
else
$html .= '<h1 class="md-h1">' . self::markdown_convertcode(substr($mi, 1)) . '</h1>';
} else if ($mi[0] == '@') { //c居中r靠右
if ($mi[1] == 'c')
$html .= '<div style="text-align:center;">' . self::markdown_convertcode(substr($mi, 2)) . '</div>';
else if ($mi[1] == 'r')
$html .= '<div style="text-align:right;margin-right:1em;">' . self::markdown_convertcode(substr($mi, 2)) . '</div>';
else
$html .= substr($mi, 1);
} else if ($mi[0] == '!') { //!图片地址 !图片地址|ALT文字
$mis = explode('|', $mi);
$url = $mis[0];
$url = substr($url, 1);
$alt = '';
if (isset($mis[1]))
$alt = ' alt="' . str_replace('"', "", $mis[1]) . '"';
$html .= '<div style="font-size:0"><img src="' . $url . '" style="max-width:100%;"' . $alt . '/></div>';
} else if ($mi[0] == '_') { //HTML
$html .= substr($mi, 1);
} else {
$html .= '<div class="md-content">' . self::markdown_convertcode($mi) . '</div>';
}
}
return $html;
}
private static function markdown_convertcode($md) {
$bcode = false;
$md = str_replace(' ', ' ', $md);
$md = str_replace(' ', '&nbsp;', $md);
while (true) {
$ind = strpos($md, '`');
if ($ind === false)
break;
$el = '<code class="md-code">';
if ($bcode) {
$bcode = false;
$el = '</code>';
} else
$bcode = true;
$md = substr($md, 0, $ind) . $el . substr($md, $ind + 1);
}
if ($bcode)
$md .= '</code>';
while (true) {
$ind = strpos($md, '\\u');
if ($ind === false)
break;
$ind2 = strpos($md, '\\u', $ind + 1);
if ($ind2 === false)
break;
$mis = explode('|', substr($md, $ind + 2, $ind2 - $ind - 2));
$url = $mis[0];
$text = $url;
if (isset($mis[1]))
$text = $mis[1];
if ($text[0] == '!') {
$html = '<a href="' . $url . '" target="_blank" nofollow><img src="' . substr($text, 1) . '" style="max-width:100%;"/></a>';
} else {
$html = '<a href="' . $url . '" target="_blank" nofollow>' . $text . '</a>';
}
$md = substr($md, 0, $ind) . $html . substr($md, $ind2 + 2);
}
return $md;
}
public static function getwebtitle($db, $id) {
$csql = new sql('a_web_seo');
$csql->where('url', $_SERVER['REQUEST_URI']);
$webtitrow = $db->getone($csql);
if (is_array($webtitrow)) {
if ($webtitrow['pagecount'] < 1)
$webtitrow['pagecount'] = 20;
return $webtitrow;
}
$csql = new sql('a_web_seo');
$csql->where('id', $id);
$webtitrow = $db->getone($csql);
if (is_array($webtitrow)) {
if ($webtitrow['pagecount'] < 1)
$webtitrow['pagecount'] = 20;
return $webtitrow;
}
$webtitrow = array();
$webtitrow['name'] = '众产平台';
$webtitrow['keys'] = '众产平台,众产事业';
$webtitrow['desc'] = '众产事业网站';
$webtitrow['pagecount'] = 30;
return $webtitrow;
}
public static function titrep($str, $dat, $content = '') {
foreach ($dat as $key => $val) {
$str = str_replace('{' . $key . '}', $val, $str);
}
$str = str_replace('农业农业', '农业', $str);
if (empty($content))
return $str;
$ind = strpos($str, '{content');
if ($ind === false)
return $str;
$ind2 = strpos($str, '}', $ind);
if ($ind2 === false)
return $str;
if ($ind2 - $ind == 8) {
return str_replace('{content}', self::leftchinese($content), $str);
} else {
$len = substr($str, $ind + 8, $ind2 - $ind - 8);
return str_replace(substr($str, $ind, $ind2 - $ind + 1), self::leftchinese($content, $len), $str);
}
}
private static function leftchinese($content, $len = 0) {
$content = htmlspecialchars_decode($content);
$content = str_replace(" ", "", $content);
$content = str_replace("\n", "", $content);
$content = str_replace("\r", "", $content);
$content = str_replace("\t", "", $content);
$content = strip_tags($content);
$content = trim($content);
if ($len <= 0)
return $content;
if (mb_strlen($content, 'utf-8') > $len)
return mb_substr($content, 0, $len, "utf-8");
return $content;
}
public static function fillsource($str) {
$source = explode('|', $str);
if (!isset($source[1]))
return $source[0];
else
return '<a href="' . $source[1] . '" target="_blank" nofollow>' . '转自' . $source[0] . '</a>';
}
public static function showh1($webtit, $artrow) {
$h1 = self::titrep($webtit['h1'], $artrow);
$htip = self::titrep($webtit['htip'], $artrow);
if (empty($h1))
echo '';
else if ($h1[0] == '<')
echo $h1;
else
echo '<h1>' . $h1 . '</h1>';
if (empty($htip))
echo '';
else if ($htip[0] == '<')
echo $htip;
else
echo '<div class="vhtip">' . $htip . '</div>';
}
public static function markdown_fromhtml($content, $baseurl) {
$html = html::str_get_html($content);
if ($html === false || $html === null)
return '';
$md = '';
self::_html2md_child($baseurl, $html->childNodes(), $md);
return $md;
}
private static function _html2md_child($baseurl, $childs, &$md) {
if ($childs === null)
return;
foreach ($childs as $child) {
if ($child->tag == 'table') {
$md .= '_' . str_replace("\n", "", $child->outertext) . "\n";
} else if ($child->tag == 'img') {
if (empty(@$child->attr['src']))
continue;
$md .= '!' . self::urljoin($child->attr['src'], $baseurl) . '|' . @$child->attr['alt'] . "\n";
} else if ($child->tag == 'ul') {
//clog('find ul');
} else {
$divps = $child->find('div,p');
if (count($divps) > 0) {
$txt = trim($child->plaintext);
self::_html2md_child($baseurl, $child->childNodes(), $md);
} else {
$divimgs = $child->find('img');
foreach ($divimgs as $divimg) {
if (empty(@$divimg->attr['src']))
continue;
$md .= '!' . self::urljoin($divimg->attr['src'], $baseurl) . '|' . @$divimg->attr['alt'] . "\n";
}
$txtpre = '';
$txt = $child->plaintext;
$txt = str_replace('&nbsp;', '', $txt);
$txt = trim($txt);
$style = @$child->attr['style'] . ',' . @$child->attr['align'];
if (strpos($style, 'right') !== false)
$txtpre = '@r';
else if (strpos($style, 'center') !== false)
$txtpre = '@c';
if (!empty($txt)) {
$txts = explode("\n", $txt);
foreach ($txts as $tx) {
$tx = trim($tx);
while (true) {
if (substr($tx, 0, 3) != ' ')
break;
$tx = substr($tx, 3);
}
if (empty($tx))
continue;
$md .= $txtpre . $tx . "\n";
}
}
}
}
}
}
//相对网址转绝对网址
public static function urljoin($srcurl, $baseurl) {
$srcinfo = parse_url($srcurl);
if (isset($srcinfo['scheme'])) {
return $srcurl;
}
$baseinfo = parse_url($baseurl);
$url = $baseinfo['scheme'] . '://' . $baseinfo['host'];
if (substr($srcinfo['path'], 0, 1) == '/') {
$path = $srcinfo['path'];
} else {
$path = dirname($baseinfo['path']) . '/' . $srcinfo['path'];
}
$rst = array();
$path_array = explode('/', $path);
if (!$path_array[0]) {
$rst[] = '';
}
foreach ($path_array as $key => $dir) {
if ($dir == '..') {
if (end($rst) == '..') {
$rst[] = '..';
} elseif (!array_pop($rst)) {
$rst[] = '..';
}
} elseif ($dir && $dir != '.') {
$rst[] = $dir;
}
}
if (!end($path_array)) {
$rst[] = '';
}
$url .= implode('/', $rst);
return str_replace('\\', '/', $url);
}
}