<?php
function template($template = 'index', $module = '') {
$module = empty($module) ? MODULE_NAME : $module;
$config = ticky\config::get('view');
$driver = $config['driver']; $suffix = $config[$driver]['template_suffix']; $theme = $config['default_theme'];
$view_path = APP_PATH . DS . $module . DS . 'view'; $viewName = $template;
$viewFiles = array(
$viewName,
$view_path . DS . CONTROLLER_NAME . DS . ACTION_NAME, $view_path . DS . $theme . DS . $viewName . '.' . $suffix, $view_path . DS . $theme . DS . CONTROLLER_NAME . DS . $viewName . '.' . $suffix, $view_path . DS . $theme . DS . $viewName . '.' . $suffix, $view_path . DS . $theme . DS . CONTROLLER_NAME . DS . $viewName . '.' . $suffix, $view_path . DS . $viewName . '.' . $suffix, $view_path . DS . CONTROLLER_NAME . DS . ACTION_NAME . '.' . $suffix, );
if (!is_file($viewName)) {
foreach ($viewFiles as $x => $x_value) {
if (is_file($x_value)) {
$tplfile = $x_value;
break;
}
}
} else {
$tplfile = $viewName;
}
if (!is_file($tplfile)) {
showmsg($template . L('template_does_not_exist'), 'stop');
}
return $tplfile;
}
function debug($var = null, $vardump = false) {
echo '<pre>';
$vardump = empty($var) ? true : $vardump;
if ($vardump) {
var_dump($var);
} else {
print_r($var);
}
echo '</pre>';
exit();
}
function password($pass) {
return md5(substr(md5(trim($pass)), 3, 26));
}
if (!function_exists('get_dir_chmod')) {
function get_dir_chmod($dirName) {
if (is_readable($dirName)) {
$chmod = '可读,';
}
if (is_writable($dirName)) {
$chmod .= '可写,';
}
if (is_executable($dirName)) {
$chmod .= '可执行,';
}
return trim($chmod, ',');
}
}
function get_ip_address($ip = '', $type = null) {
if (!$ip) {
$ip = \ticky\request::getClientIp();
}
$url = "http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip;
$json = file_get_contents($url);
$data = json_decode($json, true);
if ($data['code']) {
return $data['data'];
} else {
$country = $data['data']['country'];
$province = $data['data']['region'];
$city = $data['data']['city'];
$area = $data['data']['area'];
if ($type == 'country') {
return $country;
} elseif ($type == 'province') {
return $province;
} elseif ($type == 'area') {
return $area;
} elseif ($type == 'city') {
return $city;
} else {
return $country . ' ' . ' ' . $province . ' ' . $city;
}
}
}
function create_tradenum() {
return date('YmdHis') . random(4);
}
function runlog($file, $message, $halt = 0) {
$nowurl = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
$log = date('Y-m-d H:i:s') . "\t" . getip() . "\t" . session('uid') . "\t{$nowurl}\t" . str_replace(array("\r", "\n"), array(' ', ' '), trim($message)) . "\n";
\ticky\helper\file::writelog($file, $log);
if ($halt) {
exit();
}
}
function random($length, $numeric = 0) {
$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
if ($numeric) {
$hash = '';
} else {
$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
$length--;
}
$max = strlen($seed) - 1;
for ($i = 0; $i < $length; $i++) {
$hash .= $seed{mt_rand(0, $max)};
}
return $hash;
}
function strexists($string, $find) {
return !(strpos($string, $find) === FALSE);
}
function cutstr($string, $length, $dot = '...') {
if (strlen($string) <= $length) {
return $string;
}
$pre = chr(1);
$end = chr(1);
$string = str_replace(array('&', '"', '<', '>'), array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), $string);
$strcut = '';
if (strtolower(CHARSET) == 'utf-8') {
$n = $tn = $noc = 0;
while ($n < strlen($string)) {
$t = ord($string[$n]);
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n++;
$noc++;
} elseif (194 <= $t && $t <= 223) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif (224 <= $t && $t <= 239) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif (240 <= $t && $t <= 247) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif (248 <= $t && $t <= 251) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif ($t == 252 || $t == 253) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n++;
}
if ($noc >= $length) {
break;
}
}
if ($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
$_length = $length - 1;
for ($i = 0; $i < $length; $i++) {
if (ord($string[$i]) <= 127) {
$strcut .= $string[$i];
} else if ($i < $_length) {
$strcut .= $string[$i] . $string[++$i];
}
}
}
$strcut = str_replace(array($pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end), array('&', '"', '<', '>'), $strcut);
$pos = strrpos($strcut, chr(1));
if ($pos !== false) {
$strcut = substr($strcut, 0, $pos);
}
return $strcut . $dot;
}
function pass_time($time) {
$now = time();
$time_past = $now - strtotime($time);
if ($time_past < 60) {
return '刚刚';
}
$time_mapping = array(
'分钟' => '60',
'小时' => '24',
'天' => '7',
'周' => '4',
'月' => '12',
'年' => '100'
);
$time_past = floor($time_past / 60);
foreach ($time_mapping as $k => $v) {
if ($time_past < $v) {
return floor($time_past) . $k . '前';
}
$time_past = $time_past / $v;
}
}
function dstripslashes($string) {
if (empty($string))
return $string;
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = dstripslashes($val);
}
} else {
$string = stripslashes($string);
}
return $string;
}
function sizecount($size) {
if ($size >= 1073741824) {
$size = round($size / 1073741824 * 100) / 100 . ' GB';
} elseif ($size >= 1048576) {
$size = round($size / 1048576 * 100) / 100 . ' MB';
} elseif ($size >= 1024) {
$size = round($size / 1024 * 100) / 100 . ' KB';
} else {
$size = intval($size) . ' Bytes';
}
return $size;
}
function ipaccess($ip, $accesslist) {
return preg_match("/^(" . str_replace(array("\r\n", ' '), array('|', ''), preg_quote($accesslist, '/')) . ")/", $ip);
}
function dintval($int, $allowarray = false) {
$ret = intval($int);
if ($int == $ret || !$allowarray && is_array($int))
return $ret;
if ($allowarray && is_array($int)) {
foreach ($int as &$v) {
$v = dintval($v, true);
}
return $int;
} elseif ($int <= 0xffffffff) {
$l = strlen($int);
$m = substr($int, 0, 1) == '-' ? 1 : 0;
if (($l - $m) === strspn($int, '0987654321', $m)) {
return $int;
}
}
return $ret;
}
function dmkdir($dir, $mode = 0777, $makeindex = TRUE) {
if (!is_dir($dir)) {
dmkdir(dirname($dir), $mode, $makeindex);
@mkdir($dir, $mode);
if (!empty($makeindex)) {
@touch($dir . '/index.html');
@chmod($dir . '/index.html', 0777);
}
}
return true;
}
function remove_xss($string) {
$string = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $string);
$parm1 = Array('javascript', 'vbscript', 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base');
$parm2 = Array('onabort', 'onactivate', 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload');
$parm = array_merge($parm1, $parm2);
for ($i = 0; $i < sizeof($parm); $i++) {
$pattern = '/';
for ($j = 0; $j < strlen($parm[$i]); $j++) {
if ($j > 0) {
$pattern .= '(';
$pattern .= '(&#[x|X]0([9][a][b]);?)?';
$pattern .= '|(�([9][10][13]);?)?';
$pattern .= ')?';
}
$pattern .= $parm[$i][$j];
}
$pattern .= '/i';
$string = preg_replace($pattern, ' ', $string);
}
return $string;
}
function safe_replace($string) {
$string = str_replace('%20', '', $string);
$string = str_replace('%27', '', $string);
$string = str_replace('%2527', '', $string);
$string = str_replace('*', '', $string);
$string = str_replace('"', '', $string);
$string = str_replace("'", '', $string);
$string = str_replace(';', '', $string);
$string = str_replace('<', '<', $string);
$string = str_replace('>', '>', $string);
$string = str_replace("{", '', $string);
$string = str_replace('}', '', $string);
$string = str_replace('\\', '', $string);
return $string;
}
function is_ie() {
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if ((strpos($useragent, 'opera') !== false) || (strpos($useragent, 'konqueror') !== false)) {
return false;
}
if (strpos($useragent, 'msie ') !== false) {
return true;
}
return false;
}
function is_email($email) {
return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
function is_mobile($mobile) {
return preg_match('/1[3456789]{1}\d{9}$/', $mobile);
}
function is_badword($string) {
$badwords = array("\\", '&', ' ', "'", '"', '/', '*', ',', '<', '>', "\r", "\t", "\n", "#");
foreach ($badwords as $value) {
if (strpos($string, $value) !== false) {
return true;
}
}
return false;
}
function fileext($filename) {
return strtolower(trim(substr(strrchr($filename, '.'), 1, 10)));
}
function file_down($filepath, $filename = '') {
if (!$filename) {
$filename = basename($filepath);
}
if (is_ie()) {
$filename = rawurlencode($filename);
}
$filetype = fileext($filename);
$filesize = sprintf("%u", filesize($filepath));
if (ob_get_length() !== false) {
@ob_end_clean();
}
header('Pragma: public');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding: binary');
header('Content-Encoding: none');
header('Content-type: ' . $filetype);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-length: ' . $filesize);
readfile($filepath);
exit;
}
function string2array($data) {
$data = trim($data);
if ($data == '')
return array();
if (strpos($data, '{\\') === 0)
$data = stripslashes($data);
$array = json_decode($data, true);
return $array;
}
function array2string($data, $isformdata = 1) {
if ($data == '' || empty($data))
return '';
if ($isformdata)
$data = new_stripslashes($data);
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
return addslashes(json_encode($data));
} else {
return addslashes(json_encode($data, JSON_FORCE_OBJECT));
}
}
function new_stripslashes($string) {
if (!is_array($string))
return stripslashes($string);
foreach ($string as $key => $val) {
$string[$key] = new_stripslashes($val);
}
return $string;
}
function new_html_special_chars($string) {
if (!is_array($string))
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
foreach ($string as $key => $val)
$string[$key] = new_html_special_chars($val);
return $string;
}
function trim_script($str) {
if (is_array($str)) {
foreach ($str as $key => $val) {
$str[$key] = trim_script($val);
}
} else {
$str = preg_replace('/\<([\/]?)script([^\>]*?)\>/si', '<\\1script\\2>', $str);
$str = preg_replace('/\<([\/]?)iframe([^\>]*?)\>/si', '<\\1iframe\\2>', $str);
$str = preg_replace('/\<([\/]?)frame([^\>]*?)\>/si', '<\\1frame\\2>', $str);
$str = str_replace('javascript:', 'javascript:', $str);
}
return $str;
}
function match_img($content) {
preg_match('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/', $content, $match);
return !empty($match) ? $match[1] : '';
}
function grab_image($content, $targeturl = '') {
preg_match_all('/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/', $content, $img_array);
$img_array = isset($img_array[1]) ? array_unique($img_array[1]) : array();
if ($img_array) {
$path = config('upload', 'save_dir') . '/' . date('Ym/d');
$urlpath = SITE_URL . $path;
$imgpath = YZMPHP_PATH . $path;
if (!is_dir($imgpath))
@mkdir($imgpath, 0777, true);
}
foreach ($img_array as $key => $value) {
$val = $value;
if (strpos($value, 'http') === false) {
if (!$targeturl) {
return $content;
}
$value = $targeturl . $value;
}
$ext = strrchr($value, '.');
if ($ext != '.png' && $ext != '.jpg' && $ext != '.gif' && $ext != '.jpeg')
return false;
$imgname = date("YmdHis") . rand(1, 9999) . $ext;
$filename = $imgpath . '/' . $imgname;
$urlname = $urlpath . '/' . $imgname;
ob_start();
readfile($value);
$data = ob_get_contents();
ob_end_clean();
file_put_contents($filename, $data);
if (is_file($filename)) {
$content = str_replace($val, $urlname, $content);
} else {
return $content;
}
}
return $content;
}
function thumb($imgurl, $width = 300, $height = 200, $autocut = 0, $smallpic = 'nopic.jpg') {
global $image;
$upload_url = SITE_URL . C('upload_file') . '/';
$upload_path = YZMPHP_PATH . C('upload_file') . '/';
if (empty($imgurl))
return STATIC_URL . 'images/' . $smallpic;
if (!strpos($imgurl, '://'))
$imgurl = SERVER_PORT . HTTP_HOST . $imgurl;
$imgurl_replace = str_replace($upload_url, '', $imgurl);
if (!extension_loaded('gd') || strpos($imgurl_replace, '://'))
return $imgurl;
if (!is_file($upload_path . $imgurl_replace))
return STATIC_URL . 'images/' . $smallpic;
list($width_t, $height_t, $type, $attr) = getimagesize($upload_path . $imgurl_replace);
if ($width >= $width_t || $height >= $height_t)
return $imgurl;
$newimgurl = dirname($imgurl_replace) . '/thumb_' . $width . '_' . $height . '_' . basename($imgurl_replace);
if (is_file($upload_path . $newimgurl))
return $upload_url . $newimgurl;
if (!is_object($image)) {
yzm_base::load_sys_class('image', '', '0');
$image = new image(1);
}
return $image->thumb($upload_path . $imgurl_replace, $upload_path . $newimgurl, $width, $height, '', $autocut) ? $upload_url . $newimgurl : $imgurl;
}
function watermark($source, $target = '') {
global $image_w;
if (empty($source))
return $source;
if (!extension_loaded('gd') || strpos($source, '://'))
return $source;
if (!$target)
$target = $source;
if (!is_object($image_w)) {
load::load_sys_class('image', '', '0');
$image_w = new image(1, 1);
}
$image_w->watermark($source, $target);
return $target;
}