123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519 |
- <?php
- /**
- * 易优CMS
- * ============================================================================
- * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
- * 网站地址: http://www.eyoucms.com
- * ----------------------------------------------------------------------------
- * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
- * ============================================================================
- * Author: 小虎哥 <1105415366@qq.com>
- * Date: 2018-4-3
- */
-
- if (!function_exists('unifyPriceHandle')) {
- /**
- * 显示价格统一处理
- * @param string $price 金额
- * @param bool $decimal 是否保留小数,true保留,false不保留
- * @return string
- */
- function unifyPriceHandle($price = 0, $decimal = false)
- {
- // 为空则返回 0
- if (empty($price)) return 0;
-
- // 保留两位小数
- $price = sprintf("%.2f", $price);
-
- // 不保留小数,强制转换浮点类型
- if (empty($decimal)) $price = floatval($price);
-
- // 返回结果
- return $price;
- }
- }
-
- if (!function_exists('convert_arr_key')) {
- /**
- * 将数据库中查出的列表以指定的 id 作为数组的键名
- *
- * @param array $arr 数组
- * @param string $key_name 数组键名
- * @return array
- */
- function convert_arr_key($arr, $key_name)
- {
- if (function_exists('array_column')) {
- return array_column($arr, null, $key_name);
- }
-
- $arr2 = array();
- foreach ($arr as $key => $val) {
- $arr2[$val[$key_name]] = $val;
- }
- return $arr2;
- }
- }
-
- if (!function_exists('reform_keys')) {
- /**
- * 重置数组索引,兼容多维数组
- *
- * @param array $arr 数组
- * @param string $key_name 数组键名
- * @return array
- */
- function reform_keys($array){
- if(!is_array($array)){
- return $array;
- }
- $keys = implode('', array_keys($array));
- if(is_numeric($keys)){
- $array = array_values($array);
- }
- $array = array_map('reform_keys', $array);
- return $array;
- }
- }
-
- if (!function_exists('func_encrypt')) {
- /**
- * md5加密
- *
- * @param string $str 字符串
- * @return array
- */
- function func_encrypt($pwd, $is_admin = false, $type = 'bcrypt')
- {
- $pwd = trim($pwd);
- $auth_code = get_auth_code($type);
- if ('bcrypt' == $type) {
- $encry_pwd = crypt($pwd, $auth_code);
- } else {
- $encry_pwd = md5($auth_code . $pwd);
- }
-
- return $encry_pwd;
- }
- }
-
- if (!function_exists('pwd_encry_type'))
- {
- /**
- * 获取密码加密方式
- * @param string $encry_pwd
- * @return [type] [description]
- */
- function pwd_encry_type($encry_pwd = '') {
- $entry = 'md5';
- if (32 != strlen($encry_pwd)) {
- if (defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH == 1) {
- $entry = 'bcrypt';
- }
- }
-
- return $entry;
- }
- }
-
- if (!function_exists('get_auth_code')) {
- /**
- * 密码加密盐值
- */
- function get_auth_code($type = 'bcrypt')
- {
- $auth_code = '';
- if ($type == 'bcrypt') { // crypt加密方式
- $lang = get_current_lang(true);
- $auth_code = \think\Db::name('config')->where(['name'=>'system_crypt_auth_code','inc_type'=>'system','lang'=>$lang])->value('value');
- if (empty($auth_code)) {
- $rand_str = md5(uniqid(rand(), true));
- $rand_str = substr($rand_str, 0, 23);
- $auth_code = '$2y$11$'.$rand_str; //30位盐
- /*多语言*/
- if (is_language()) {
- $langRow = \think\Db::name('language')->order('id asc')->select();
- foreach ($langRow as $key => $val) {
- tpCache('system', ['system_crypt_auth_code' => $auth_code], $val['mark']);
- }
- } else { // 单语言
- tpCache('system', ['system_crypt_auth_code' => $auth_code]);
- }
- /*--end*/
- }
- } else { // md5加密方式
- $lang = get_current_lang(true);
- $auth_code = \think\Db::name('config')->where(['name'=>'system_auth_code','inc_type'=>'system','lang'=>$lang])->value('value');
- if (empty($auth_code)) {
- $auth_code = \think\Config::get('AUTH_CODE');
- /*多语言*/
- if (is_language()) {
- $langRow = \think\Db::name('language')->order('id asc')->select();
- foreach ($langRow as $key => $val) {
- tpCache('system', ['system_auth_code' => $auth_code], $val['mark']);
- }
- } else { // 单语言
- tpCache('system', ['system_auth_code' => $auth_code]);
- }
- /*--end*/
- }
- }
-
- return $auth_code;
- }
- }
-
- if (!function_exists('get_arr_column')) {
- /**
- * 获取数组中的某一列
- *
- * @param array $arr 数组
- * @param string $key_name 列名
- * @return array 返回那一列的数组
- */
- function get_arr_column($arr, $key_name)
- {
- if (function_exists('array_column')) {
- return array_column($arr, $key_name);
- }
-
- $arr2 = array();
- foreach ($arr as $key => $val) {
- $arr2[] = $val[$key_name];
- }
- return $arr2;
- }
- }
-
- if (!function_exists('parse_url_param')) {
- /**
- * 获取url 中的各个参数 类似于 pay_code=alipay&bank_code=ICBC-DEBIT
- * @param string $url
- * @return type
- */
- function parse_url_param($url = '')
- {
- $url = rtrim($url, '/');
- $data = array();
- $str = explode('?', $url);
- $str = end($str);
- $parameter = explode('&', $str);
- foreach ($parameter as $val) {
- if (!empty($val)) {
- $tmp = explode('=', $val);
- $data[$tmp[0]] = $tmp[1];
- }
- }
- return $data;
- }
- }
-
- if (!function_exists('getClientIP')) {
- /**
- * 客户端IP(新)
- */
- function getClientIP()
- {
- if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
- $ip = getenv('HTTP_CLIENT_IP');
- } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
- $ip = getenv('HTTP_X_FORWARDED_FOR');
- } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
- $ip = getenv('REMOTE_ADDR');
- } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
- $ip = $_SERVER['REMOTE_ADDR'];
- }
- return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches [0] : '';
- }
- }
-
- if (!function_exists('clientIP')) {
- /**
- * 客户端IP(旧,目前存在未知异常,获取的IP有不准的情况)
- */
- function clientIP()
- {
- $ip = request()->ip();
- if (preg_match('/^((?:(?:25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(?:25[0-5]|2[0-4]\d|((1\d{2})|([1 -9]?\d))))$/', $ip))
- return $ip;
- else
- return '';
- }
- }
-
- if (!function_exists('serverIP')) {
- /**
- * 服务器端IP
- */
- function serverIP()
- {
- // 会因为解析问题导致后台卡
- if (!empty($_SERVER['SERVER_ADDR']) && !preg_match('/127\.0\.\d{1,3}\.\d{1,3}/i', $_SERVER['SERVER_ADDR']) && !preg_match('/192\.168\.\d{1,3}\.\d{1,3}/i', $_SERVER['SERVER_ADDR'])) {
- $serviceIp = $_SERVER['SERVER_ADDR'];
- } else {
- $serviceIp = @gethostbyname($_SERVER["SERVER_NAME"]);
- }
- return $serviceIp;
- }
- }
-
- if (!function_exists('recurse_copy')) {
- /**
- * 自定义函数递归的复制带有多级子目录的目录
- * 递归复制文件夹
- *
- * @param type $src 原目录
- * @param type $dst 复制到的目录
- */
- //参数说明:
- //自定义函数递归的复制带有多级子目录的目录
- function recurse_copy($src, $dst)
- {
- $now = getTime();
- $dir = opendir($src);
- @mkdir($dst);
- if ('Weapp' == CONTROLLER_NAME && stristr(ACTION_NAME, 'install') && !is_writeable(ROOT_PATH.'weapp')) {
- return '请检查以下网站目录权限是否可写<br/>(application|template|weapp)';
- }
- while (false !== $file = readdir($dir)) {
- if (($file != '.') && ($file != '..')) {
- if (is_dir($src . '/' . $file)) {
- recurse_copy($src . '/' . $file, $dst . '/' . $file);
- } else {
- if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
- if (!is_writeable($dst . DIRECTORY_SEPARATOR . $file)) {
- // exit($dst . DIRECTORY_SEPARATOR . $file . '不可写');
- return '网站目录没有写入权限,请调整权限';
- }
- // @unlink($dst . DIRECTORY_SEPARATOR . $file);
- }
- // if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
- // @unlink($dst . DIRECTORY_SEPARATOR . $file);
- // }
- $copyrt = @copy($src . DIRECTORY_SEPARATOR . $file, $dst . DIRECTORY_SEPARATOR . $file);
- if (!$copyrt) {
- // echo 'copy ' . $dst . DIRECTORY_SEPARATOR . $file . ' failed';
- return '网站目录没有写入权限,请调整权限';
- }
- }
- }
- }
- closedir($dir);
-
- return true;
- }
- }
-
- if (!function_exists('delFile')) {
- /**
- * 递归删除文件夹
- *
- * @param string $path 目录路径
- * @param boolean $delDir 是否删除空目录
- * @return boolean
- */
- function delFile($path, $delDir = FALSE)
- {
- if (preg_match('/^(.+)\/$/i', $path)) {
- $path = rtrim($path, '/');
- }
-
- if (!is_dir($path)) {return FALSE;}
-
- $handle = @opendir($path);
- if ($handle) {
- while (false !== ($item = readdir($handle))) {
- if ($item != "." && $item != "..")
- is_dir("$path/$item") ? delFile("$path/$item", $delDir) : @unlink("$path/$item");
- }
- closedir($handle);
- if ($delDir) {
- return @rmdir($path);
- }
- } else {
- if (file_exists($path)) {
- return @unlink($path);
- } else {
- return FALSE;
- }
- }
- }
- }
-
- if (!function_exists('getDirFile')) {
- /**
- * 递归读取文件夹文件
- *
- * @param string $directory 目录路径
- * @param string $dir_name 显示的目录前缀路径
- * @param array $arr_file 是否删除空目录
- * @return boolean
- */
- function getDirFile($directory, $dir_name = '', &$arr_file = array())
- {
- if (!file_exists($directory)) {
- return false;
- }
-
- $mydir = dir($directory);
- while ($file = $mydir->read()) {
- if ((is_dir("$directory/$file")) AND ($file != ".") AND ($file != "..")) {
- if ($dir_name) {
- getDirFile("$directory/$file", "$dir_name/$file", $arr_file);
- } else {
- getDirFile("$directory/$file", "$file", $arr_file);
- }
-
- } else if (($file != ".") AND ($file != "..")) {
- if ($dir_name) {
- $arr_file[] = "$dir_name/$file";
- } else {
- $arr_file[] = "$file";
- }
- }
- }
- $mydir->close();
-
- return $arr_file;
- }
- }
-
- if (!function_exists('ey_scandir')) {
- /**
- * 部分空间为了安全起见,禁用scandir函数
- *
- * @param string $dir 路径
- * @return array
- */
- function ey_scandir($dir, $type = 'all')
- {
- if (function_exists('scandir')) {
- $files = scandir($dir);
- } else {
- $files = [];
- $mydir = dir($dir);
- while ($file = $mydir->read()) {
- $files[] = "$file";
- }
- $mydir->close();
- }
- $arr_file = [];
- foreach ($files as $key => $val) {
- if (($val != ".") AND ($val != "..")) {
- if ('all' == $type) {
- $arr_file[] = "$val";
- } else if ('file' == $type && is_file($val)) {
- $arr_file[] = "$val";
- } else if ('dir' == $type && is_dir($val)) {
- $arr_file[] = "$val";
- }
- }
- }
-
- return $arr_file;
- }
- }
-
- if (!function_exists('group_same_key')) {
- /**
- * 将二维数组以元素的某个值作为键,并归类数组
- *
- * array( array('name'=>'aa','type'=>'pay'), array('name'=>'cc','type'=>'pay') )
- * array('pay'=>array( array('name'=>'aa','type'=>'pay') , array('name'=>'cc','type'=>'pay') ))
- * @param $arr 数组
- * @param $key 分组值的key
- * @return array
- */
- function group_same_key($arr, $key)
- {
- $new_arr = array();
- foreach ($arr as $k => $v) {
- $new_arr[$v[$key]][] = $v;
- }
- return $new_arr;
- }
- }
-
- if (!function_exists('get_rand_str')) {
- /**
- * 获取随机字符串
- * @param int $randLength 长度
- * @param int $addtime 是否加入当前时间戳
- * @param int $includenumber 是否包含数字
- * @return string
- */
- function get_rand_str($randLength = 6, $addtime = 1, $includenumber = 0)
- {
- if (1 == $includenumber) {
- $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789';
- } else if (2 == $includenumber) {
- $chars = '123456789';
- } else if (3 == $includenumber) {
- $chars = 'ABCDEFGHJKLMNPQEST123456789';
- } else {
- $chars = 'abcdefghijklmnopqrstuvwxyz';
- }
- $len = strlen($chars);
- $randStr = '';
- for ($i = 0; $i < $randLength; $i++) {
- $randStr .= $chars[rand(0, $len - 1)];
- }
- $tokenvalue = $randStr;
- if ($addtime) {
- $tokenvalue = $randStr . getTime();
- }
- return $tokenvalue;
- }
- }
-
- if (!function_exists('httpRequest')) {
- /**
- * CURL请求
- *
- * @param $url 请求url地址
- * @param $method 请求方法 get post
- * @param null $postfields post数据数组
- * @param array $headers 请求header信息
- * @param bool|false $debug 调试开启 默认false
- * @return mixed
- */
- function httpRequest($url, $method = "GET", $postfields = null, $headers = array(), $timeout = 30, $debug = false)
- {
- $method = strtoupper($method);
- $ci = curl_init();
- /* Curl settings */
- // curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // 使用哪个版本
- curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
- // curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
- curl_setopt($ci, CURLOPT_TIMEOUT, $timeout); /* 设置cURL允许执行的最长秒数 */
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
- switch ($method) {
- case "POST":
- curl_setopt($ci, CURLOPT_POST, true);
- if (!empty($postfields)) {
- $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
- curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
- }
- break;
- default:
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
- break;
- }
- $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
- curl_setopt($ci, CURLOPT_URL, $url);
- if ($ssl) {
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
- curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
- }
- //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
- if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
- curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
- }
- curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ci, CURLINFO_HEADER_OUT, true);
- /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
- $response = curl_exec($ci);
- $requestinfo = curl_getinfo($ci);
- $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
- if ($debug) {
- echo "=====post data======\r\n";
- var_dump($postfields);
- echo "=====info===== \r\n";
- print_r($requestinfo);
- echo "=====response=====\r\n";
- print_r($response);
- }
- curl_close($ci);
- return $response;
- //return array($http_code, $response,$requestinfo);
- }
- }
-
- if (!function_exists('httpRequest2')) {
- /**
- * CURL请求
- *
- * @param $url 请求url地址
- * @param $method 请求方法 get post
- * @param null $postfields post数据数组
- * @param array $headers 请求header信息
- * @param bool|false $debug 调试开启 默认false
- * @return mixed
- */
- function httpRequest2($url, $method = "GET", $postfields = null, $headers = array(), $timeout = 30, $debug = false)
- {
- $method = strtoupper($method);
- $ci = curl_init();
- /* Curl settings */
- // curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // 使用哪个版本
- curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
- // curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
- curl_setopt($ci, CURLOPT_TIMEOUT, $timeout); /* 设置cURL允许执行的最长秒数 */
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
- switch ($method) {
- case "POST":
- curl_setopt($ci, CURLOPT_POST, true);
- if (!empty($postfields)) {
- $web_basehost = request()->host(true);
- if (false !== filter_var($web_basehost, FILTER_VALIDATE_IP) || file_exists('./data/conf/multidomain.txt') || preg_match('/\.(my3w\.com)$/i', $web_basehost)) {
- $web_basehost = tpCache('web.web_basehost');
- }
- $web_basehost = preg_replace('/^(http(s)?:)?(\/\/)?([^\/\:]*)(.*)$/i', '${4}', $web_basehost);
- if (is_string($postfields)) {
- parse_str($postfields, $output);
- $output['domain'] = $web_basehost;
- $output['ip'] = serverIP();
- $tmpdatastr = http_build_query($output);
- } else {
- $postfields['domain'] = $web_basehost;
- $postfields['ip'] = serverIP();
- $tmpdatastr = http_build_query($postfields);
- }
- curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
- }
- break;
- default:
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
- break;
- }
- $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
- curl_setopt($ci, CURLOPT_URL, $url);
- if ($ssl) {
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
- curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
- }
- //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
- if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
- curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
- }
- curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ci, CURLINFO_HEADER_OUT, true);
- /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
- $response = curl_exec($ci);
- $requestinfo = curl_getinfo($ci);
- $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
- if ($debug) {
- echo "=====post data======\r\n";
- var_dump($postfields);
- echo "=====info===== \r\n";
- print_r($requestinfo);
- echo "=====response=====\r\n";
- print_r($response);
- }
- curl_close($ci);
- return $response;
- //return array($http_code, $response,$requestinfo);
- }
- }
-
- if (!function_exists('check_mobile')) {
- /**
- * 检查手机号码格式
- *
- * @param $mobile 手机号码
- */
- function check_mobile($mobile)
- {
- if (preg_match('/1\d{10}$/', $mobile))
- return true;
- return false;
- }
- }
-
- if (!function_exists('check_telephone')) {
- /**
- * 检查固定电话
- *
- * @param $mobile
- * @return bool
- */
- function check_telephone($mobile)
- {
- if (preg_match('/^([0-9]{3,4}-)?[0-9]{7,8}$/', $mobile))
- return true;
- return false;
- }
- }
-
- if (!function_exists('check_email')) {
- /**
- * 检查邮箱地址格式
- *
- * @param $email 邮箱地址
- */
- function check_email($email)
- {
- if (filter_var($email, FILTER_VALIDATE_EMAIL))
- return true;
- return false;
- }
- }
- if (!function_exists('check_domain'))
- {
- /**
- * 校验域名格式
- *
- * @access public
- * @param string $operation 操作
- * @return string
- */
- function check_domain($domain = ''){
- $str="/^(?:[-A-za-z0-9_\x{4e00}-\x{9fa5}]+\.)+[-A-za-z0-9]{2,}$/u";
- if (!preg_match($str,$domain)){
- return false;
- }else{
- return true;
- }
- }
- }
- if (!function_exists('getSubstr')) {
- /**
- * 实现中文字串截取无乱码的方法
- *
- * @param string $string 字符串
- * @param intval $start 起始位置
- * @param intval $length 截取长度
- * @return string
- */
- function getSubstr($string = '', $start = 0, $length = NULL)
- {
- if (mb_strlen($string, 'utf-8') > $length) {
- $str = msubstr($string, $start, $length, true, 'utf-8');
- return $str;
- } else {
- return $string;
- }
- }
- }
-
- if (!function_exists('msubstr')) {
- /**
- * 字符串截取,支持中文和其他编码
- *
- * @param string $str 需要转换的字符串
- * @param string $start 开始位置
- * @param string $length 截取长度
- * @param string $suffix 截断显示字符
- * @param string $charset 编码格式
- * @return string
- */
- function msubstr($str = '', $start = 0, $length = NULL, $suffix = false, $charset = "utf-8")
- {
- if (function_exists("mb_substr")) {
- $slice = mb_substr($str, $start, $length, $charset);
- }
- elseif (function_exists('iconv_substr')) {
- $slice = iconv_substr($str, $start, $length, $charset);
- if (false === $slice) {
- $slice = '';
- }
- }
- else {
- $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
- $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
- $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
- $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
- preg_match_all($re[$charset], $str, $match);
- $slice = join("", array_slice($match[0], $start, $length));
- }
-
- $str_len = strlen($str); // 原字符串长度
- $slice_len = strlen($slice); // 截取字符串的长度
- if ($slice_len < $str_len) {
- $slice = $suffix ? $slice . '...' : $slice;
- }
-
- return $slice;
- }
- }
-
- if (!function_exists('html_msubstr')) {
- /**
- * 截取内容清除html之后的字符串长度,支持中文和其他编码
- *
- * @param string $str 需要转换的字符串
- * @param string $start 开始位置
- * @param string $length 截取长度
- * @param string $suffix 截断显示字符
- * @param string $charset 编码格式
- * @return string
- */
- function html_msubstr($str = '', $start = 0, $length = NULL, $suffix = false, $charset = "utf-8")
- {
- if (is_language() && 'cn' != get_current_lang()) {
- $length = $length * 2;
- }
- $str = eyou_htmlspecialchars_decode($str);
- $str = checkStrHtml($str);
- return msubstr($str, $start, $length, $suffix, $charset);
- }
- }
-
- if (!function_exists('text_msubstr')) {
- /**
- * 针对多语言截取,其他语言的截取是中文语言的2倍长度
- *
- * @param string $str 需要转换的字符串
- * @param string $start 开始位置
- * @param string $length 截取长度
- * @param string $suffix 截断显示字符
- * @param string $charset 编码格式
- * @return string
- */
- function text_msubstr($str = '', $start = 0, $length = NULL, $suffix = false, $charset = "utf-8")
- {
- if (is_language() && 'cn' != get_current_lang()) {
- $length = $length * 2;
- }
- return msubstr($str, $start, $length, $suffix, $charset);
- }
- }
-
- if (!function_exists('eyou_htmlspecialchars_decode')) {
- /**
- * 自定义只针对htmlspecialchars编码过的字符串进行解码
- *
- * @param string $str 需要转换的字符串
- * @param string $start 开始位置
- * @param string $length 截取长度
- * @param string $suffix 截断显示字符
- * @param string $charset 编码格式
- * @return string
- */
- function eyou_htmlspecialchars_decode($str = '')
- {
- if (is_string($str) && stripos($str, '<') !== false && stripos($str, '>') !== false) {
- $str = htmlspecialchars_decode($str);
- }
- return $str;
- }
- }
-
- if (!function_exists('isMobile')) {
- /**
- * 判断当前访问的用户是 PC端 还是 手机端 返回true 为手机端 false 为PC 端
- * 是否移动端访问
- *
- * @return boolean
- */
- function isMobile()
- {
- static $is_mobile = null;
- null === $is_mobile && $is_mobile = request()->isMobile();
- return $is_mobile;
-
- // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
- if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
- return true;
-
- // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
- if (isset ($_SERVER['HTTP_VIA'])) {
- // 找不到为flase,否则为true
- return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
- }
- // 脑残法,判断手机发送的客户端标志,兼容性有待提高
- if (isset ($_SERVER['HTTP_USER_AGENT'])) {
- $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
- // 从HTTP_USER_AGENT中查找手机浏览器的关键字
- if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
- return true;
- }
- // 协议法,因为有可能不准确,放到最后判断
- if (isset ($_SERVER['HTTP_ACCEPT'])) {
- // 如果只支持wml并且不支持html那一定是移动设备
- // 如果支持wml和html但是wml在html之前则是移动设备
- if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
- return true;
- }
- }
- return false;
- }
- }
-
- if (!function_exists('isWeixin')) {
- /**
- * 是否微信端访问
- *
- * @return boolean
- */
- function isWeixin()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
- return true;
- }
- return false;
- }
- }
-
- if (!function_exists('isWeixinApplets')) {
- /**
- * 是否微信端小程序访问
- *
- * @return boolean
- */
- function isWeixinApplets()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'miniProgram') !== false) {
- return true;
- }
- return false;
- }
- }
-
- if (!function_exists('isQq')) {
- /**
- * 是否QQ端访问
- *
- * @return boolean
- */
- function isQq()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'QQ') !== false) {
- return true;
- }
- return false;
- }
- }
-
- if (!function_exists('isAlipay')) {
- /**
- * 是否支付端访问
- *
- * @return boolean
- */
- function isAlipay()
- {
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'AlipayClient') !== false) {
- return true;
- }
- return false;
- }
- }
-
- if (!function_exists('getFirstCharter')) {
- /**
- * php获取中文字符拼音首字母
- *
- * @param string $str 中文
- * @return boolean
- */
- function getFirstCharter($str)
- {
- if (empty($str)) {
- return '';
- }
- $fchar=ord($str[0]);
- if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str[0]);
- $s1 = @iconv('UTF-8', 'gb2312', $str);
- $s2 = @iconv('gb2312', 'UTF-8', $s1);
- $s=$s2==$str?$s1:$str;
- $asc=ord($s[0])*256+ord($s[1])-65536;
- if ($asc >= -20319 && $asc <= -20284) return 'A';
- if ($asc >= -20283 && $asc <= -19776) return 'B';
- if ($asc >= -19775 && $asc <= -19219) return 'C';
- if ($asc >= -19218 && $asc <= -18711) return 'D';
- if ($asc >= -18710 && $asc <= -18527) return 'E';
- if ($asc >= -18526 && $asc <= -18240) return 'F';
- if ($asc >= -18239 && $asc <= -17923) return 'G';
- if ($asc >= -17922 && $asc <= -17418) return 'H';
- if ($asc >= -17417 && $asc <= -16475) return 'J';
- if ($asc >= -16474 && $asc <= -16213) return 'K';
- if ($asc >= -16212 && $asc <= -15641) return 'L';
- if ($asc >= -15640 && $asc <= -15166) return 'M';
- if ($asc >= -15165 && $asc <= -14923) return 'N';
- if ($asc >= -14922 && $asc <= -14915) return 'O';
- if ($asc >= -14914 && $asc <= -14631) return 'P';
- if ($asc >= -14630 && $asc <= -14150) return 'Q';
- if ($asc >= -14149 && $asc <= -14091) return 'R';
- if ($asc >= -14090 && $asc <= -13319) return 'S';
- if ($asc >= -13318 && $asc <= -12839) return 'T';
- if ($asc >= -12838 && $asc <= -12557) return 'W';
- if ($asc >= -12556 && $asc <= -11848) return 'X';
- if ($asc >= -11847 && $asc <= -11056) return 'Y';
- if ($asc >= -11055 && $asc <= -10247) return 'Z';
- return 'Z';
- }
- }
-
- if (!function_exists('pinyin_long')) {
- /**
- * 获取整条字符串汉字拼音首字母
- *
- * @param $zh
- * @return string
- */
- function pinyin_long($zh)
- {
- $ret = "";
- $s1 = iconv("UTF-8", "gb2312", $zh);
- $s2 = iconv("gb2312", "UTF-8", $s1);
- if ($s2 == $zh) {
- $zh = $s1;
- }
- for ($i = 0; $i < strlen($zh); $i++) {
- $s1 = substr($zh, $i, 1);
- $p = ord($s1);
- if ($p > 160) {
- $s2 = substr($zh, $i++, 2);
- $ret .= getFirstCharter($s2);
- } else {
- $ret .= $s1;
- }
- }
- return $ret;
- }
- }
-
- if (!function_exists('respose')) {
- /**
- * 参数 is_jsonp 为true,表示跨域ajax请求的返回值
- *
- * @param string $res 数组
- * @param bool $is_jsonp 是否跨域
- * @return string
- */
- function respose($res, $is_jsonp = false)
- {
- if (true === $is_jsonp) {
- exit(input('callback') . "(" . json_encode($res) . ")");
- } else {
- exit(json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- }
- }
-
- if (!function_exists('urlsafe_b64encode')) {
- function urlsafe_b64encode($string)
- {
- $data = base64_encode($string);
- $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data);
- return $data;
- }
- }
-
- if (!function_exists('getTime')) {
- /**
- * 获取当前时间戳
- *
- */
- function getTime()
- {
- return time();
- }
- }
-
- if (!function_exists('getMsectime')) {
- /**
- * 获取当前时间戳(精确到毫秒)
- *
- */
- function getMsectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- }
-
- if (!function_exists('trim_space')) {
- /**
- * 过滤前后空格等多种字符
- *
- * @param string $str 字符串
- * @param array $arr 特殊字符的数组集合
- * @return string
- */
- function trim_space($str, $arr = array())
- {
- if (empty($arr)) {
- $arr = array(' ', ' ');
- }
- foreach ($arr as $key => $val) {
- $str = preg_replace('/(^' . $val . ')|(' . $val . '$)/', '', $str);
- }
-
- return $str;
- }
- }
-
- if (!function_exists('func_preg_replace')) {
- /**
- * 替换指定的符号
- *
- * @param array $arr 特殊字符的数组集合
- * @param string $replacement 符号
- * @param string $str 字符串
- * @return string
- */
- function func_preg_replace($arr = array(), $replacement = ',', $str = '')
- {
- if (empty($arr)) {
- $arr = array(',');
- }
- foreach ($arr as $key => $val) {
- if (is_array($replacement)) {
- $replacevalue = isset($replacement[$key]) ? $replacement[$key] : current($replacement);
- } else {
- $replacevalue = $replacement;
- }
- $val = str_replace('/', '\/', $val);
- $str = preg_replace('/(' . $val . ')/', $replacevalue, $str);
- }
-
- return $str;
- }
- }
-
- if (!function_exists('db_create_in')) {
- /**
- * 创建像这样的查询: "IN('a','b')";
- *
- * @param mixed $item_list 列表数组或字符串,如果为字符串时,字符串只接受数字串
- * @param string $field_name 字段名称
- * @return string
- */
- function db_create_in($item_list, $field_name = '')
- {
- if (empty($item_list)) {
- return $field_name . " IN ('') ";
- } else {
- if (!is_array($item_list)) {
- $item_list = explode(',', $item_list);
- foreach ($item_list as $k => $v) {
- $item_list[$k] = intval($v);
- }
- }
-
- $item_list = array_unique($item_list);
- $item_list_tmp = '';
- foreach ($item_list AS $item) {
- if ($item !== '') {
- $item_list_tmp .= $item_list_tmp ? ",'$item'" : "'$item'";
- }
- }
- if (empty($item_list_tmp)) {
- return $field_name . " IN ('') ";
- } else {
- return $field_name . ' IN (' . $item_list_tmp . ') ';
- }
- }
- }
- }
-
- if (!function_exists('static_version')) {
- /**
- * 给静态文件追加版本号,实时刷新浏览器缓存
- *
- * @param string $file 为远程文件
- * @return string
- */
- function static_version($file)
- {
- static $request = null;
- null == $request && $request = \think\Request::instance();
- // ---判断本地文件是否存在,否则返回false,以免@get_headers方法导致崩溃
- if (is_http_url($file)) { // 判断http路径
- if (preg_match('/^http(s?):\/\/' . $request->host(true) . '/i', $file)) { // 判断当前域名的本地服务器文件(这仅用于单台服务器,多台稍作修改便可)
- // $pattern = '/^http(s?):\/\/([^.]+)\.([^.]+)\.([^\/]+)\/(.*)$/';
- $pattern = '/^http(s?):\/\/([^\/]+)(.*)$/';
- preg_match_all($pattern, $file, $matches);//正则表达式
- if (!empty($matches)) {
- $filename = $matches[count($matches) - 1][0];
- if (!file_exists(realpath(ltrim($filename, '/')))) {
- return false;
- }
- $file = $request->domain() . $filename;
- }
- }
-
- $update_time = getTime();
-
- } else {
- if (!file_exists(realpath(ltrim($file, '/')))) {
- return false;
- }
-
- try{
- if ($request->controller() == 'Buildhtml') {
- $update_time = getTime();
- } else {
- $fileStat = stat(ROOT_PATH . ltrim($file, '/'));
- $update_time = !empty($fileStat['mtime']) ? $fileStat['mtime'] : getTime();
- }
- } catch (\Exception $e) {
- $update_time = getTime();
- }
- }
- // -------------end---------------
-
- $parseStr = '';
- $file = get_absolute_url(ROOT_DIR.$file); // 支持子目录
- $update_time_str = !empty($update_time) ? '?t='.$update_time : '';
- $type = strtolower(substr(strrchr($file, '.'), 1));
- switch ($type) {
- case 'js':
- $parseStr .= '<script language="javascript" type="text/javascript" src="' . $file . $update_time_str . '"></script>';
- break;
- case 'css':
- $parseStr .= '<link href="' . $file . $update_time_str . '" rel="stylesheet" media="screen" type="text/css" />';
- break;
- case 'ico':
- $parseStr .= '<link rel="shortcut icon" href="' . $file . $update_time_str . '" />';
- break;
- }
-
- return $parseStr;
- }
- }
-
- if (!function_exists('tp_mkdir')) {
- /**
- * 递归创建目录
- *
- * @param string $path 目录路径,不带反斜杠
- * @param intval $purview 目录权限码
- * @return boolean
- */
- function tp_mkdir($path, $purview = 0777)
- {
- if (!is_dir($path)) {
- tp_mkdir(dirname($path), $purview);
- if (!mkdir($path, $purview)) {
- return false;
- }
- }
- return true;
- }
- }
-
- if (!function_exists('format_bytes')) {
- /**
- * 格式化字节大小
- *
- * @param number $size 字节数
- * @param string $delimiter 数字和单位分隔符
- * @return string 格式化后的带单位的大小
- */
- function format_bytes($size, $delimiter = '')
- {
- $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
- for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
- return round($size, 2) . $delimiter . $units[$i];
- }
- }
-
- if (!function_exists('unformat_bytes')) {
- /**
- * 反格式化字节大小
- *
- * @param number $size 格式化带单位的大小
- */
- function unformat_bytes($formatSize)
- {
- $size = 0;
- if (preg_match('/^\d+P/i', $formatSize)) {
- $size = intval($formatSize) * 1024 * 1024 * 1024 * 1024 * 1024;
- } else if (preg_match('/^\d+T/i', $formatSize)) {
- $size = intval($formatSize) * 1024 * 1024 * 1024 * 1024;
- } else if (preg_match('/^\d+G/i', $formatSize)) {
- $size = intval($formatSize) * 1024 * 1024 * 1024;
- } else if (preg_match('/^\d+M/i', $formatSize)) {
- $size = intval($formatSize) * 1024 * 1024;
- } else if (preg_match('/^\d+K/i', $formatSize)) {
- $size = intval($formatSize) * 1024;
- } else if (preg_match('/^\d+B/i', $formatSize)) {
- $size = intval($formatSize);
- }
-
- $size = strval($size);
-
- return $size;
- }
- }
-
- if (!function_exists('is_http_url')) {
- /**
- * 判断url是否完整的链接
- *
- * @param string $url 网址
- * @return boolean
- */
- function is_http_url($url)
- {
- // preg_match("/^(http:|https:|ftp:|svn:)?(\/\/).*$/", $url, $match);
- preg_match("/^((\w)*:)?(\/\/).*$/", $url, $match);
- if (empty($match)) {
- return false;
- } else {
- return true;
- }
- }
- }
-
- if (!function_exists('get_html_first_imgurl')) {
- /**
- * 获取文章内容html中第一张图片地址
- *
- * @param string $html html代码
- * @return boolean
- */
- function get_html_first_imgurl($html)
- {
- $pattern = '~<img [^>]*[\s]?[\/]?[\s]?>~';
- preg_match_all($pattern, $html, $matches);//正则表达式把图片的整个都获取出来了
- $img_arr = $matches[0];//图片
- $first_img_url = "";
- if (!empty($img_arr)) {
- $first_img = $img_arr[0];
- $p = "#src=('|\")(.*)('|\")#isU";//正则表达式
- preg_match_all($p, $first_img, $img_val);
- if (isset($img_val[2][0])) {
- $first_img_url = $img_val[2][0]; //获取第一张图片地址
- }
- }
-
- return $first_img_url;
- }
- }
-
- if (!function_exists('checkStrHtml')) {
- /**
- * 过滤Html标签
- *
- * @param string $string 内容
- * @return string
- */
- function checkStrHtml($string, $replaces = [])
- {
- $string = str_replace(" ", " ", $string);
- $string = trim_space($string);
- $string = trim($string);
-
- if (is_numeric($string)) return $string;
- if (!isset($string) or empty($string)) return '';
-
- $string = preg_replace('/[\\x00-\\x08\\x0B\\x0C\\x0E-\\x1F]/', '', $string);
- $string = ($string);
-
- $string = strip_tags($string, ""); //清除HTML如<br />等代码
- if (!empty($replaces)) {
- $string = str_replace($replaces, "", $string);
- }
- $string = str_replace("\n", "", $string);//去掉空格和换行
- $string = str_replace("\t", "", $string); //去掉制表符号
- $string = str_replace(PHP_EOL, "", $string); //去掉回车换行符号
- $string = str_replace("\r", "", $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);
- $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);
- $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);
- $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 = preg_replace_callback('/[\xf0-\xf7].{3}/', function ($r) {
- return '';
- }, $string);
-
- return $string;
- }
- }
-
- if (!function_exists('saveRemote')) {
- /**
- * 抓取远程图片
- *
- * @param string $fieldName 远程图片url
- * @param string $savePath 存储在public/upload的子目录
- * @return string
- */
- function saveRemote($fieldName, $savePath = 'temp/', $is_water = 1)
- {
- $allowFiles = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".svg", ".ico"];
- $web_basehost = tpCache('web.web_basehost');
- $parse_arr = parse_url($web_basehost);
- $host = request()->host(true);
-
- $imgUrl = htmlspecialchars($fieldName);
- $imgUrl = str_replace("&", "&", $imgUrl);
- $imgUrl = preg_replace('/#/', '', $imgUrl);
-
- // 插件列表
- static $weappList = null;
- if (null === $weappList) {
- $weappList = \think\Db::name('weapp')->where([
- 'status' => 1,
- ])->cache(true, EYOUCMS_CACHE_TIME, 'weapp')
- ->getAllWithIndex('code');
- }
- $storageDomain = ''; // 第三方存储的域名
- if (!empty($weappList['Qiniuyun']['data'])) {
- $qnyData = json_decode($weappList['Qiniuyun']['data'], true);
- if (!empty($qnyData['domain'])) {
- $storageDomain = $qnyData['domain'];
- }
- } else if (!empty($weappList['AliyunOss']['data'])) {
- $ossData = json_decode($weappList['AliyunOss']['data'], true);
- if (!empty($ossData['domain'])) {
- $storageDomain = $ossData['domain'];
- }
- } else if (!empty($weappList['Cos']['data'])) {
- $cosData = json_decode($weappList['Cos']['data'], true);
- if (!empty($cosData['domain'])) {
- $storageDomain = $cosData['domain'];
- }
- }
- //http开头验证
- if (strpos($imgUrl, "http") !== 0) {
- $data = array(
- 'state' => '链接不是http链接',
- );
- return json_encode($data);
- }
- //获取请求头并检测死链
- $heads = @get_headers($imgUrl, 1);
- if (empty($heads) || !(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
- $data = array(
- 'state' => '链接不可用',
- );
- return json_encode($data);
- }
- // 图片扩展名
- $fileType = substr($heads['Content-Type'], -4, 4);
- if (!preg_match("#\.(jpg|jpeg|gif|png|ico|bmp|webp|svg)#i", $fileType)) {
- $filext = str_ireplace('image/', '', $heads['Content-Type']);
- if ($fileType == 'image/gif' || $filext == 'gif') {
- $fileType = ".gif";
- } else if ($fileType == 'image/png' || $filext == 'png') {
- $fileType = ".png";
- } else if ($fileType == 'image/x-icon' || $filext == 'x-icon') {
- $fileType = ".ico";
- } else if ($fileType == 'image/bmp' || $filext == 'bmp') {
- $fileType = ".bmp";
- } else if ($fileType == 'image/webp' || $filext == 'webp') {
- $fileType = ".webp";
- } else if ($heads['Content-Type'] == 'image/svg+xml' || $filext == 'svg+xml') {
- $fileType = ".svg";
- } else {
- $fileType = '.jpg';
- }
- }
- $fileType = strtolower($fileType);
- //格式验证(扩展名验证和Content-Type验证),链接contentType是否正确
- $is_weixin_img = false;
- if (preg_match("/^http(s?):\/\/(mmbiz.qpic.cn|thirdwx.qlogo.cn)\/(.*)/", $imgUrl) != 1) {
- if (!in_array($fileType, $allowFiles) || (isset($heads['Content-Type']) && !stristr($heads['Content-Type'], "image/"))) {
- $data = array(
- 'state' => '链接contentType不正确',
- );
- return json_encode($data);
- }
- } else {
- $is_weixin_img = true;
- }
-
- /*验证图片一句话木马*/
- if (false === check_illegal($imgUrl,true)) {
- $data = array(
- 'state' => '疑似木马图片!!!',
- );
- return json_encode($data);
- }
- /*--end*/
- // 本站图片 / 根网址图片 / 第三方存储插件的图片
- if (preg_match("/\/\/({$host}|{$storageDomain}|{$parse_arr['host']})\//i", $imgUrl)) {
- $arr = explode('/', $imgUrl);
- $data = array(
- 'state' => 'SUCCESS',
- 'url' => $imgUrl,
- 'title' => end($arr),
- 'original' => '',
- 'type' => strtolower(strrchr($imgUrl, '.')),
- 'size' => 0,
- 'width' => 0,
- 'height' => 0,
- 'mime' => '',
- );
- return json_encode($data);
- }
-
-
- //打开输出缓冲区并获取远程图片
- ob_start();
- $context = stream_context_create(
- array('http' => array(
- 'follow_location' => false // don't follow redirects
- ))
- );
- readfile($imgUrl, false, $context);
- $img = ob_get_contents();
- ob_end_clean();
- preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
-
- $dirname = './' . UPLOAD_PATH . 'allimg/' . date('Ymd') . '/';
- $file['oriName'] = $m ? $m[1] : "";
- $file['filesize'] = strlen($img);
- $file['ext'] = $fileType;
- $users_id = 1;
- if (session('?users_id')) {
- $users_id = session('users_id');
- } else if (session('?admin_id')) {
- $users_id = session('admin_id');
- }
- $file['name'] = $users_id . '-' . dd2char(date("ymdHis") . mt_rand(100, 999)) . $file['ext'];
- $file['fullName'] = $dirname . $file['name'];
- $fullName = $file['fullName'];
-
- //检查文件大小是否超出限制
- if ($file['filesize'] >= 10240000) {
- $data = array(
- 'state' => '文件大小超出网站限制',
- );
- return json_encode($data);
- }
-
- //创建目录失败
- if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
- $data = array(
- 'state' => '目录创建失败',
- );
- return json_encode($data);
- } else if (!is_writeable($dirname)) {
- $data = array(
- 'state' => '目录没有写权限',
- );
- return json_encode($data);
- }
-
-
- //移动文件
- if (!(file_put_contents($fullName, $img) && file_exists($fullName))) { //移动失败
- $data = array(
- 'state' => '写入文件内容错误',
- );
- return json_encode($data);
- } else { //移动成功
- $imgurl = substr($file['fullName'], 1);
- $imageInfo = @getimagesize('.'.$imgurl);
- $data = array(
- 'state' => 'SUCCESS',
- 'url' => ROOT_DIR.$imgurl,
- 'title' => $file['name'],
- 'original' => $file['oriName'],
- 'type' => $file['ext'],
- 'size' => $file['filesize'],
- 'width' => !empty($imageInfo[0]) ? intval($imageInfo[0]) : 0,
- 'height' => !empty($imageInfo[1]) ? intval($imageInfo[1]) : 0,
- 'mime' => !empty($imageInfo['mime']) ? $imageInfo['mime'] : '',
- );
-
- try {
- if (1 == $is_water) {
- print_water($data['url']); // 添加水印
- }
- /*同步到第三方对象存储空间*/
- $bucket_data = SynImageObjectBucket($data['url'], $weappList);
- if (!empty($bucket_data['url']) && is_string($bucket_data['url'])) {
- $data['url'] = $bucket_data['url'];
- }
- /*end*/
- } catch (\Exception $e) {}
-
- // 添加图片进数据库
- $addData = [
- 'aid' => 0,
- 'type_id' => 0,
- 'image_url' => $data['url'],
- 'title' => '',
- 'intro' => '',
- 'width' => $data['width'],
- 'height' => $data['height'],
- 'filesize' => $data['size'],
- 'mime' => $data['mime'],
- 'users_id' => (int)session('admin_info.syn_users_id'),
- 'sort_order' => 100,
- 'add_time' => getTime(),
- 'update_time' => getTime(),
- ];
- // \think\Db::name('uploads')->add($addData);
- }
- return json_encode($data);
- }
- }
-
- if (!function_exists('func_common')) {
- /**
- * 自定义上传
- *
- * @param string $fileElementId 上传表单的ID
- * @param string $path 存储在public/upload的子目录
- * @param string $file_type 图片后缀名
- * @return string
- */
- function func_common($fileElementId = 'uploadImage', $path = 'allimg', $file_type = "", $postFiles = [],$compressConf = [])
- {
- $lang = get_current_lang();
- $file = !empty($postFiles) ? $postFiles : request()->file($fileElementId);
- if (empty($file)) {
- if ($lang == 'cn') {
- $errmsg = '请选择上传图片';
- } else if ($lang == 'zh') {
- $errmsg = '請選擇上傳圖片';
- } else {
- $errmsg = 'Please select upload picture';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg];
- }
-
- $validate = array();
- // 文件大小限制
- $validate['size'] = intval(tpCache('basic.file_size') * 1024 * 1024);
- /*文件扩展名限制*/
- $validate_ext = !empty($file_type) ? str_replace(',', '|', $file_type) : tpCache('basic.image_type');
- $validate_ext = str_replace('|', ',', $validate_ext);
- $validate['ext'] = explode(',', $validate_ext);
- /*--end*/
-
- //拓展名
- $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
- if (!in_array($ext, $validate['ext'])) {
- if ($lang == 'cn') {
- $errmsg = '上传图片后缀名必须为';
- } else if ($lang == 'zh') {
- $errmsg = '上傳圖片后綴名必須為';
- } else {
- $errmsg = 'Upload image suffix must be ';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg . $validate_ext];
- }
- /*拓展名验证end*/
-
- /*上传文件验证*/
- if (!empty($validate)) {
- $is_validate = $file->check($validate);
- if ($is_validate === false) {
- return ['errcode' => 1, 'errmsg' => $file->getError()];
- }
- }
- /*--end*/
-
- /*验证图片一句话木马*/
- if (false === check_illegal($file->getInfo('tmp_name'))) {
- if ($lang == 'cn') {
- $errmsg = '疑似木马图片';
- } else if ($lang == 'zh') {
- $errmsg = '疑似木馬圖片';
- } else {
- $errmsg = 'Suspected Trojan images';
- }
- return ['errcode'=>1,'errmsg'=>$errmsg];
- }
- /*--end*/
-
- $fileName = $file->getInfo('name');
- // 提取文件名后缀
- $file_ext = pathinfo($fileName, PATHINFO_EXTENSION);
- // 提取出文件名,不包括扩展名
- $newfileName = preg_replace('/\.([^\.]+)$/', '', $fileName);
- // 过滤文件名.\/的特殊字符,防止利用上传漏洞
- $newfileName = preg_replace('#(\\\|\/|\.)#i', '', $newfileName);
- // 过滤后的新文件名
- $fileName = $newfileName . '.' . $file_ext;
-
- if (session('?users_id') && 'admin' != request()->module()) {
- $users_id = session('users_id');
- $savePath = UPLOAD_PATH.'user/'.session('users_id').'/';
- } else {
- $savePath = UPLOAD_PATH;
- }
- $savePath .= $path . '/' . date('Ymd/');
-
- $return_url = "";
- $info = $file->rule(function ($file) {
- $users_id_tmp = 1;
- if (session('?users_id') && 'admin' != request()->module()) {
- $users_id_tmp = session('users_id');
- } else if (session('?admin_id')) {
- $users_id_tmp = session('admin_id');
- }
- return $users_id_tmp . '-' . dd2char(date("ymdHis") . mt_rand(100, 999));
- })->move($savePath);
- if ($info) {
- $return_url = '/' . $savePath . $info->getSaveName();
-
- // 重新制作一张图片,抹去任何可能有危害的数据
- // $image = \think\Image::open('.'.$return_url);
- // $image->save('.'.$return_url, null, 100);
- }
-
- // 是否要压缩图片
- if (!empty($compressConf[0])) {
- $return_url = func_thumb_img($return_url, $compressConf[0], $compressConf[1]);
- }
-
- if ($return_url) {
- if ($lang == 'cn') {
- $errmsg = '上传成功';
- } else if ($lang == 'zh') {
- $errmsg = '上傳成功';
- } else {
- $errmsg = 'Upload succeeded';
- }
- return ['errcode' => 0, 'errmsg' => $errmsg, 'img_url' => $return_url];
- }
- else {
- if ($lang == 'cn') {
- $errmsg = '上传失败';
- } else if ($lang == 'zh') {
- $errmsg = '上傳失敗';
- } else {
- $errmsg = 'Upload failed';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg];
- }
- }
- }
-
- if (!function_exists('func_common_doc')) {
- /**
- * 自定义上传
- *
- * @param string $fileElementId 上传表单的ID
- * @param string $path 存储在public/upload的子目录
- * @param string $file_type 文件后缀名
- * @return string
- */
- function func_common_doc($fileElementId = 'uploadFile', $path = 'soft', $file_type = "", $postFiles = [])
- {
- $lang = get_current_lang();
- $file = !empty($postFiles) ? $postFiles : request()->file($fileElementId);
-
- if (empty($file)) {
- if ($lang == 'cn') {
- $errmsg = '请选择上传文件';
- } else if ($lang == 'zh') {
- $errmsg = '請選擇上傳文件';
- } else {
- $errmsg = 'Please select upload file';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg];
- }
-
- $validate = array();
- // 文件大小限制
- $validate['size'] = intval(tpCache('basic.file_size') * 1024 * 1024);
- /*文件扩展名限制*/
- $validate_ext = !empty($file_type) ? str_replace(',', '|', $file_type) : tpCache('basic.file_type');
- $validate_ext = str_replace('|', ',', $validate_ext);
- $validate['ext'] = explode(',', $validate_ext);
- /*--end*/
-
- //拓展名
- $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
- if (!in_array($ext, $validate['ext'])) {
- if ($lang == 'cn') {
- $errmsg = '上传文件后缀名必须为';
- } else if ($lang == 'zh') {
- $errmsg = '上傳文件后綴名必須為';
- } else {
- $errmsg = 'Upload file suffix must be ';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg . $validate_ext];
- }
- /*拓展名验证end*/
-
- /*上传文件验证*/
- if (!empty($validate)) {
- $is_validate = $file->check($validate);
- if ($is_validate === false) {
- return ['errcode' => 1, 'errmsg' => $file->getError()];
- }
- }
- /*--end*/
-
- $fileName = $file->getInfo('name');
- // 提取文件名后缀
- $file_ext = pathinfo($fileName, PATHINFO_EXTENSION);
- // 提取出文件名,不包括扩展名
- $newfileName = preg_replace('/\.([^\.]+)$/', '', $fileName);
- // 过滤文件名.\/的特殊字符,防止利用上传漏洞
- $newfileName = preg_replace('#(\\\|\/|\.)#i', '', $newfileName);
- // 过滤后的新文件名
- $fileName = $newfileName . '.' . $file_ext;
- // 中文转码
- // $newfileName = iconv("utf-8", "gb2312//IGNORE", $newfileName);
-
- $savePath = $path . '/' . date('Ymd/');
- $return_url = "";
-
- $info = $file->rule(function ($file) {
- // return md5(mt_rand());
- $users_id = 1;
- if (session('?users_id') && 'admin' != request()->module()) {
- $users_id = session('users_id');
- } else if (session('?admin_id')) {
- $users_id = session('admin_id');
- }
- return $users_id . '-' . dd2char(date("ymdHis") . mt_rand(100, 999));
- })->move(UPLOAD_PATH . $savePath);
- if ($info) {
- $return_url = '/' . UPLOAD_PATH . $savePath . $info->getSaveName();
- }
-
- if ($return_url) {
- if ($lang == 'cn') {
- $errmsg = '上传成功';
- } else if ($lang == 'zh') {
- $errmsg = '上傳成功';
- } else {
- $errmsg = 'Upload succeeded';
- }
- return ['errcode' => 0, 'errmsg' => $errmsg, 'img_url' => $return_url];
- }
- else {
- if ($lang == 'cn') {
- $errmsg = '上传失败';
- } else if ($lang == 'zh') {
- $errmsg = '上傳失敗';
- } else {
- $errmsg = 'Upload failed';
- }
- return ['errcode' => 1, 'errmsg' => $errmsg];
- }
- }
- }
-
- if (!function_exists('func_substr_replace')) {
- /**
- * 隐藏部分字符串
- *
- * @param string $str 字符串
- * @param string $replacement 替换显示的字符
- * @param intval $start 起始位置
- * @param intval $length 隐藏长度
- * @return string
- */
- function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3)
- {
- $len = mb_strlen($str, 'utf-8');
- if ($len > intval($start + $length)) {
- $str1 = msubstr($str, 0, $start);
- $str2 = msubstr($str, intval($start + $length), NULL);
- } else {
- $str1 = msubstr($str, 0, 1);
- $str2 = msubstr($str, $len - 1, 1);
- $length = $len - 2;
- }
- $new_str = $str1;
- for ($i = 0; $i < $length; $i++) {
- $new_str .= $replacement;
- }
- $new_str .= $str2;
-
- return $new_str;
- }
- }
-
- if (!function_exists('func_authcode')) {
- /**
- * 字符串加密解密
- *
- * @param unknown $string 明文或密文
- * @param string $operation DECODE表示解密,其它表示加密
- * @param string $key 密匙
- * @param number $expiry 密文有效期
- * @return string
- */
- function func_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
- {
- $ckey_length = 4;
- $key = md5($key != '' ? $key : 'zxsdcrtkbrecxm');
- $keya = md5(substr($key, 0, 16));
- $keyb = md5(substr($key, 16, 16));
- $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
-
- $cryptkey = $keya . md5($keya . $keyc);
- $key_length = strlen($cryptkey);
-
- $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
- $string_length = strlen($string);
-
- $result = '';
- $box = range(0, 255);
-
- $rndkey = array();
- for ($i = 0; $i <= 255; $i++) {
- $rndkey[$i] = ord($cryptkey[$i % $key_length]);
- }
-
- for ($j = $i = 0; $i < 256; $i++) {
- $j = ($j + $box[$i] + $rndkey[$i]) % 256;
- $tmp = $box[$i];
- $box[$i] = $box[$j];
- $box[$j] = $tmp;
- }
-
- for ($a = $j = $i = 0; $i < $string_length; $i++) {
- $a = ($a + 1) % 256;
- $j = ($j + $box[$a]) % 256;
- $tmp = $box[$a];
- $box[$a] = $box[$j];
- $box[$j] = $tmp;
- $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
- }
-
- if ($operation == 'DECODE') {
- if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
- return substr($result, 26);
- } else {
- return '';
- }
- } else {
- return $keyc . str_replace('=', '', base64_encode($result));
- }
-
- }
- }
-
- if (!function_exists('get_pinyin')) {
- /**
- * 获取拼音以gbk编码为准
- *
- * @param string $str 字符串信息
- * @param int $ishead 是否取头字母
- * @param int $isclose 是否关闭字符串资源
- * @return string
- */
- function get_pinyin($str, $ishead = 0, $isclose = 1)
- {
- try{
- $s1 = iconv("UTF-8", "gb2312", $str);
- $s2 = iconv("gb2312", "UTF-8", $s1);
- if ($s2 == $str) {
- $str = $s1;
- }
-
- static $pinyins = null;
- $restr = '';
- $str = trim($str);
- $slen = strlen($str);
- if ($slen < 2) {
- return $str;
- }
- if (null === $pinyins) {
- $pinyins = [];
- $fp = fopen(DATA_PATH . 'conf/pinyin.dat', 'r');
- while (!feof($fp)) {
- $line = trim(fgets($fp));
- $pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3);
- }
- fclose($fp);
- }
- for ($i = 0; $i < $slen; $i++) {
- if (ord($str[$i]) > 0x80) {
- $c = $str[$i] . $str[$i + 1];
- $i++;
- if (isset($pinyins[$c])) {
- if ($ishead == 0) {
- $restr .= $pinyins[$c];
- } else {
- $restr .= $pinyins[$c][0];
- }
- } else {
- $restr .= "-";
- }
- } else if (preg_match("/[a-z0-9]/i", $str[$i])) {
- $restr .= $str[$i];
- } else {
- $restr .= "-";
- }
- }
- if ($isclose == 0) {
- unset($pinyins);
- }
- return strtolower($restr);
- }catch (\Exception $e){
- return "";
- }
- }
- }
-
- if (!function_exists('filter_line_return')) {
- /**
- * 过滤换行回车符
- *
- * @param string $str 字符串信息
- * @return string
- */
- function filter_line_return($str = '', $replace = '')
- {
- return str_replace(PHP_EOL, $replace, $str);
- }
- }
-
- if (!function_exists('MyDate')) {
- /**
- * 时间转化日期格式
- *
- * @param string $format 日期格式
- * @param intval $t 时间戳
- * @return string
- */
- function MyDate($format = 'Y-m-d', $t = '')
- {
- if (!empty($t)) {
- $t = date($format, $t);
- }
- return $t;
- }
- }
-
- if (!function_exists('arctype_options')) {
- /**
- * 过滤和排序所有文章栏目,返回一个带有缩进级别的数组
- *
- * @param int $id 上级栏目ID
- * @param array $arr 含有所有栏目的数组
- * @param string $id_alias id键名
- * @param string $pid_alias 父id键名
- * @return void
- */
- function arctype_options($spec_id, $arr, $id_alias, $pid_alias)
- {
- $cat_options = array();
-
- if (isset($cat_options[$spec_id])) {
- return $cat_options[$spec_id];
- }
-
- if (!isset($cat_options[0])) {
- $level = $last_id = 0;
- $options = $id_array = $level_array = array();
- while (!empty($arr)) {
- foreach ($arr AS $key => $value) {
- $id = $value[$id_alias];
- if ($level == 0 && $last_id == 0) {
- if ($value[$pid_alias] > 0) {
- break;
- }
-
- $options[$id] = $value;
- $options[$id]['level'] = $level;
- $options[$id][$id_alias] = $id;
- // $options[$id]['typename'] = $value['typename'];
- unset($arr[$key]);
-
- if ($value['has_children'] == 0) {
- continue;
- }
- $last_id = $id;
- $id_array = array($id);
- $level_array[$last_id] = ++$level;
- continue;
- }
-
- if ($value[$pid_alias] == $last_id) {
- $options[$id] = $value;
- $options[$id]['level'] = $level;
- $options[$id][$id_alias] = $id;
- // $options[$id]['typename'] = $value['typename'];
- unset($arr[$key]);
-
- if ($value['has_children'] > 0) {
- if (end($id_array) != $last_id) {
- $id_array[] = $last_id;
- }
- $last_id = $id;
- $id_array[] = $id;
- $level_array[$last_id] = ++$level;
- }
- } elseif ($value[$pid_alias] > $last_id) {
- break;
- }
- }
-
- $count = count($id_array);
- if ($count > 1) {
- $last_id = array_pop($id_array);
- } elseif ($count == 1) {
- if ($last_id != end($id_array)) {
- $last_id = end($id_array);
- } else {
- $level = 0;
- $last_id = 0;
- $id_array = array();
- continue;
- }
- }
-
- if ($last_id && isset($level_array[$last_id])) {
- $level = $level_array[$last_id];
- } else {
- $level = 0;
- break;
- }
- }
- $cat_options[0] = $options;
- } else {
- $options = $cat_options[0];
- }
-
- if (!$spec_id) {
- return $options;
- } else {
- if (empty($options[$spec_id])) {
- return array();
- }
-
- $spec_id_level = $options[$spec_id]['level'];
-
- foreach ($options AS $key => $value) {
- if ($key != $spec_id) {
- unset($options[$key]);
- } else {
- break;
- }
- }
-
- $spec_id_array = array();
- foreach ($options AS $key => $value) {
- if (($spec_id_level == $value['level'] && $value[$id_alias] != $spec_id) ||
- ($spec_id_level > $value['level'])) {
- break;
- } else {
- $spec_id_array[$key] = $value;
- }
- }
- $cat_options[$spec_id] = $spec_id_array;
-
- return $spec_id_array;
- }
- }
- }
-
- if (!function_exists('img_replace_url')) {
- /**
- * 内容图片地址替换成带有http地址
- *
- * @param string $content 内容
- * @param string $imgurl 远程图片url
- * @return string
- */
- function img_replace_url($content = '', $imgurl = '')
- {
- $pregRule = "/<img(.*?)src(\s*)=(\s*)[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp|\.ico|\.webp]))[\'|\"](.*?)[\/]?(\s*)>/i";
- $content = preg_replace($pregRule, '<img ${1} src="' . $imgurl . '" ${5} />', $content);
-
- return $content;
- }
- }
-
- if (!function_exists('getCmsVersion')) {
- /**
- * 获取当前CMS版本号
- *
- * @return string
- */
- function getCmsVersion()
- {
- $ver = 'v1.1.8';
- $version_txt_path = ROOT_PATH . 'data/conf/version.txt';
- if (file_exists($version_txt_path)) {
- $fp = fopen($version_txt_path, 'r');
- $content = fread($fp, filesize($version_txt_path));
- fclose($fp);
- $ver = $content ? $content : $ver;
- } else {
- $r = tp_mkdir(dirname($version_txt_path));
- if ($r) {
- $fp = fopen($version_txt_path, "w+") or die("请设置" . $version_txt_path . "的权限为777");
- $web_version = tpCache('system.system_version');
- $ver = !empty($web_version) ? $web_version : $ver;
- if (fwrite($fp, $ver)) {
- fclose($fp);
- }
- }
- }
- return $ver;
- }
- }
-
- if (!function_exists('getVersion')) {
- /**
- * 获取当前各种版本号
- *
- * @return string
- */
- function getVersion($filename = 'version', $ver = 'v1.0.0', $is_write = false)
- {
- $version_txt_path = ROOT_PATH . 'data/conf/' . $filename . '.txt';
- if (file_exists($version_txt_path) && false === $is_write) {
- $fp = fopen($version_txt_path, 'r');
- $content = fread($fp, filesize($version_txt_path));
- fclose($fp);
- $ver = $content ? $content : $ver;
- } else if (!file_exists($version_txt_path) || true === $is_write) {
- $r = tp_mkdir(dirname($version_txt_path));
- if ($r) {
- $fp = fopen($version_txt_path, "w+") or die("请设置" . $version_txt_path . "的权限为777");
- if (fwrite($fp, $ver)) {
- fclose($fp);
- }
- }
- }
- return $ver;
- }
- }
-
- if (!function_exists('getWeappVersion')) {
- /**
- * 获取当前插件版本号
- *
- * @param string $ocde 插件标识
- * @return string
- */
- function getWeappVersion($code)
- {
- $ver = 'v1.0';
- $config_path = WEAPP_DIR_NAME . DS . $code . DS . 'config.php';
- if (file_exists($config_path)) {
- $config = include $config_path;
- $ver = !empty($config['version']) ? $config['version'] : $ver;
- } else {
- die($code . "插件缺少" . $config_path . "配置文件");
- }
- return $ver;
- }
- }
-
- if (!function_exists('strip_sql')) {
- /**
- * 转换SQL关键字
- *
- * @param unknown_type $string
- * @return unknown
- */
- function strip_sql($string)
- {
- $pattern_arr = array(
- "/(\s+)union(\s+)/i",
- "/\bselect\b/i",
- "/\bupdate\b/i",
- "/\bdelete\b/i",
- "/\boutfile\b/i",
- // "/\bor\b/i",
- "/\bchar\b/i",
- "/\bconcat\b/i",
- "/\btruncate\b/i",
- "/\bdrop\b/i",
- "/\binsert\b/i",
- "/\brevoke\b/i",
- "/\bgrant\b/i",
- "/\breplace\b/i",
- // "/\balert\b/i",
- "/\brename\b/i",
- // "/\bmaster\b/i",
- "/\bdeclare\b/i",
- // "/\bsource\b/i",
- // "/\bload\b/i",
- // "/\bcall\b/i",
- "/\bexec\b/i",
- "/\bdelimiter\b/i",
- "/\bphar\b\:/i",
- "/\bphar\b/i",
- "/\@(\s*)\beval\b/i",
- "/\beval\b/i",
- "/\bonerror\b/i",
- "/\bscript\b/i",
- );
- $replace_arr = array(
- 'union',
- 'select',
- 'update',
- 'delete',
- 'outfile',
- // 'or',
- 'char',
- 'concat',
- 'truncate',
- 'drop',
- 'insert',
- 'revoke',
- 'grant',
- 'replace',
- // 'alert',
- 'rename',
- // 'master',
- 'declare',
- // 'source',
- // 'load',
- // 'call',
- 'exec',
- 'delimiter',
- 'phar',
- 'phar',
- '@eval',
- 'eval',
- 'onerror',
- 'script',
- );
-
- return is_array($string) ? array_map('strip_sql', $string) : preg_replace($pattern_arr, $replace_arr, $string);
- }
- }
-
- if (!function_exists('get_weapp_class')) {
- /**
- * 获取插件类的类名
- *
- * @param strng $name 插件名
- * @param strng $controller 控制器
- * @return class
- */
- function get_weapp_class($name, $controller = '')
- {
- $controller = !empty($controller) ? $controller : $name;
- $class = WEAPP_DIR_NAME . "\\{$name}\\controller\\{$controller}";
- return $class;
- }
- }
-
- if (!function_exists('view_logic')) {
- /**
- * 模型对应逻辑
- * @param intval $aid 文档ID
- * @param intval $channel 栏目ID
- * @param intval $result 数组
- * @param mix $allAttrInfo 附加表数据
- * @return array
- */
- function view_logic($aid, $channel, $result = array(), $allAttrInfo = array())
- {
-
- $allAttrInfo_bool = $allAttrInfo;
- $result['image_list'] = $result['attr_list'] = $result['file_list'] = array();
- switch ($channel) {
- case '2': // 产品模型
- {
- /*产品相册*/
- if (true === $allAttrInfo_bool) {
- $allAttrInfo = [];
- $productImgModel = new \app\home\model\ProductImg;
- $allAttrInfo['product_img'] = $productImgModel->getProImg([$aid]);
- }
- $image_list = !empty($allAttrInfo['product_img'][$aid]) ? $allAttrInfo['product_img'][$aid] : [];
-
- // 支持子目录
- foreach ($image_list as $k1 => $v1) {
- $image_list[$k1]['image_url'] = handle_subdir_pic($v1['image_url']);
- isset($v1['intro']) && $image_list[$k1]['intro'] = htmlspecialchars_decode($v1['intro']);
- }
-
- $result['image_list'] = $image_list;
- /*--end*/
-
- /*产品参数*/
- if (true === $allAttrInfo_bool) {
- $allAttrInfo = [];
- $productAttrModel = new \app\home\model\ProductAttr;
- $allAttrInfo['product_attr'] = $productAttrModel->getProAttr([$aid]);
- }
- $attr_list = !empty($allAttrInfo['product_attr'][$aid]) ? $allAttrInfo['product_attr'][$aid] : [];
- $attr_list = model('LanguageAttr')->getBindValue($attr_list, 'product_attribute', get_main_lang()); // 获取多语言关联绑定的值
- $result['attr_list'] = $attr_list;
- /*--end*/
- break;
- }
-
- case '3': // 图集模型
- {
- /*图集相册*/
- if (true === $allAttrInfo_bool) {
- $allAttrInfo = [];
- $imagesUploadModel = new \app\home\model\ImagesUpload;
- $allAttrInfo['images_upload'] = $imagesUploadModel->getImgUpload([$aid]);
- }
- $image_list = !empty($allAttrInfo['images_upload'][$aid]) ? $allAttrInfo['images_upload'][$aid] : [];
-
- // 支持子目录
- foreach ($image_list as $k1 => $v1) {
- $image_list[$k1]['image_url'] = handle_subdir_pic($v1['image_url']);
- isset($v1['intro']) && $image_list[$k1]['intro'] = htmlspecialchars_decode($v1['intro']);
- }
-
- $result['image_list'] = $image_list;
- /*--end*/
- break;
- }
-
- case '4': // 下载模型
- {
- /*下载资料列表*/
- if (true === $allAttrInfo_bool) {
- $allAttrInfo = [];
- $downloadFileModel = new \app\home\model\DownloadFile;
- $allAttrInfo['download_file'] = $downloadFileModel->getDownFile([$aid]);
- }
- $file_list = !empty($allAttrInfo['download_file'][$aid]) ? $allAttrInfo['download_file'][$aid] : [];
-
- // 支持子目录
- foreach ($file_list as $k1 => $v1) {
- $file_list[$k1]['file_url'] = handle_subdir_pic($v1['file_url']);
- if (empty($v1['file_size'])) {
- $file_list[$k1]['file_size'] = '';
- } else {
- $file_list[$k1]['file_size'] = format_bytes($v1['file_size']);
- }
- }
-
- $result['file_list'] = $file_list;
- /*--end*/
-
- /*下载权限*/
- $arc_level_id = intval($result['arc_level_id']);
- if (empty($arc_level_id)) {
- $result['arc_level_name'] = '不限会员';
- } else {
- $result['arc_level_name'] = \think\Db::name('users_level')->where(['level_id'=>$arc_level_id])->getField('level_name');
- }
- /*--end*/
-
- break;
- }
-
- case '5': // 视频模型
- {
- if (true === $allAttrInfo_bool) {
- $allAttrInfo = [];
- $mediaFileModel = new \app\home\model\MediaFile;
- $allAttrInfo['video_file'] = $mediaFileModel->getMediaFile($aid);
- }
- $result['file_list'] = !empty($allAttrInfo['video_file']) ? $allAttrInfo['video_file'] : [];
- //自定义视频字段
- $channel_field = model('Channelfield')->getListByWhere(['channel_id'=>5,'dtype'=>'media']);
- if (!empty($channel_field)) {
- $request = request();
- foreach ($channel_field as $key => $val) {
- if (!empty($result[$key])) {
- $result[$key] = json_decode($result[$key], true);
- foreach ($result[$key] as $k => $v) {
- if (!empty($v['file_url'])) {
- $v['file_url'] = handle_subdir_pic($v['file_url'], 'media');
- if (!is_http_url($v['file_url'])){
- $v['file_url'] = $request->domain() .$v['file_url'];
- }
- }
- $result[$key][$k] = $v;
- }
- }
- }
- }
- isset($result['total_duration']) && $result['total_duration'] = gmSecondFormat($result['total_duration'], ':');
- /*--end*/
-
- /*播放权限*/
- $arc_level_id = intval($result['arc_level_id']);
- if (empty($arc_level_id)) {
- $result['arc_level_name'] = '不限会员';
- } else {
- $result['arc_level_name'] = \think\Db::name('users_level')->where(['level_id'=>$arc_level_id])->getField('level_name');
- }
- /*--end*/
- break;
- }
-
- default:
- {
- break;
- }
- }
-
- return $result;
- }
- }
-
- if (!function_exists('uncamelize')) {
- /**
- * 驼峰命名转下划线命名
- * 思路:
- * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
- */
- function uncamelize($camelCaps, $separator = '_')
- {
- return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
- }
- }
-
- if (!function_exists('read_bidden_inc')) {
- /**
- * 读取被禁止外部访问的配置文件
- *
- */
- function read_bidden_inc($phpfilepath = '')
- {
- $data = @file($phpfilepath);
- if ($data) {
- $data = !empty($data[1]) ? json_decode($data[1]) : [];
- }
- return $data;
- }
- }
-
- if (!function_exists('write_bidden_inc')) {
- /**
- * 写入被禁止外部访问的配置文件
- */
- function write_bidden_inc($arr = array(), $phpfilepath = '')
- {
- $r = tp_mkdir(dirname($phpfilepath));
- if ($r) {
- $setting = "<?php die('forbidden'); ?>\n";
- $setting .= json_encode($arr);
- $setting = str_replace("\/", "/", $setting);
- $incFile = fopen($phpfilepath, "w+");
- if (fwrite($incFile, $setting)) {
- fclose($incFile);
- return true;
- } else {
- return false;
- }
- }
- }
- }
-
- if (!function_exists('convert_js_array')) {
- /**
- * 将PHP数组转换成JS数组。
- */
- function convert_js_array($arr = array())
- {
- if (empty($arr)) {
- return false;
- }
-
- $convert_js_array = "['";
- foreach ($arr as $key => $val) {
- if ($key > 0) {
- $convert_js_array .= "','";
- }
- $convert_js_array .= $val;
- }
- $convert_js_array = $convert_js_array . "']";
-
- return $convert_js_array;
- }
- }
-
- if (!function_exists('GetUrlToDomain')) {
- /**
- * 取得根域名
- * @param type $domain 域名
- * @return string 返回根域名
- */
- function GetUrlToDomain($domain = '')
- {
- static $request = null;
- null == $request && $request = \think\Request::instance();
- $root = $request->rootDomain($domain);
-
- return $root;
- }
- }
-
- if (!function_exists('check_fix_pathinfo')) {
- /**
- * 判断支持pathinfo模式的路由,即是否支持伪静态
- * @return boolean 布尔值
- */
- function check_fix_pathinfo()
- {
- static $is_fix_pathinfo = null;
- if (null === $is_fix_pathinfo) {
- $fix_pathinfo = ini_get('cgi.fix_pathinfo');
- if (stristr(request()->host(), '.mylightsite.com') || ('' != $fix_pathinfo && 0 === $fix_pathinfo)) {
- $is_fix_pathinfo = false;
- } else {
- $is_fix_pathinfo = true;
- }
- }
-
- return $is_fix_pathinfo;
- }
- }
-
- /**
- * 生成一个随机字符
- *
- * @access public
- * @param string $ddnum
- * @return string
- */
- if (!function_exists('dd2char')) {
- function dd2char($ddnum)
- {
- $ddnum = strval($ddnum);
- $slen = strlen($ddnum);
- $okdd = '';
- $nn = '';
- for ($i = 0; $i < $slen; $i++) {
- if (isset($ddnum[$i + 1])) {
- $n = $ddnum[$i] . $ddnum[$i + 1];
- if (($n > 96 && $n < 123) || ($n > 64 && $n < 91)) {
- $okdd .= chr($n);
- $i++;
- } else {
- $okdd .= $ddnum[$i];
- }
- } else {
- $okdd .= $ddnum[$i];
- }
- }
- return $okdd;
- }
- }
-
- if (!function_exists('friend_date')) {
- /**
- * 友好时间显示
- * @param $time
- * @return bool|string
- */
- function friend_date($time)
- {
- if (!$time)
- return false;
- $fdate = '';
- $d = time() - intval($time);
- $ld = $time - mktime(0, 0, 0, 0, 0, date('Y')); //得出年
- $md = $time - mktime(0, 0, 0, date('m'), 0, date('Y')); //得出月
- $byd = $time - mktime(0, 0, 0, date('m'), date('d') - 2, date('Y')); //前天
- $yd = $time - mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')); //昨天
- $dd = $time - mktime(0, 0, 0, date('m'), date('d') + 0, date('Y')); //今天
- $td = $time - mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')); //明天
- $atd = $time - mktime(0, 0, 0, date('m'), date('d') + 2, date('Y')); //后天
- if ($d == 0) {
- $fdate = '刚刚';
- } else {
- switch ($d) {
- case $d < $atd:
- $fdate = date('Y年m月d日', $time);
- break;
- case $d < $td:
- $fdate = '后天' . date('H:i', $time);
- break;
- case $d < 0:
- $fdate = '明天' . date('H:i', $time);
- break;
- case $d < 60:
- $fdate = $d . '秒前';
- break;
- case $d < 3600:
- $fdate = floor($d / 60) . '分钟前';
- break;
- case $d < $dd:
- $fdate = floor($d / 3600) . '小时前';
- break;
- case $d < $yd:
- $fdate = '昨天' . date('H:i', $time);
- break;
- case $d < $byd:
- $fdate = '前天' . date('H:i', $time);
- break;
- case $d < $md:
- $fdate = date('m月d日 H:i', $time);
- break;
- case $d < $ld:
- $fdate = date('m月d日', $time);
- break;
- default:
- $fdate = date('Y年m月d日', $time);
- break;
- }
- }
- return $fdate;
- }
- }
-
- /**
- * 检查验证是否最新的模板
- * @param $Url 查询的模板路径
- * @param $String 查询是否存在的字符串
- * @return 返回错误的字符串
- */
- if (!function_exists('VerifyLatestTemplate')) {
- function VerifyLatestTemplate($Url = null, $String = [])
- {
- // 查询的模板路径
- $Url = !empty($Url) ? $Url : './template/' . THEME_STYLE_PATH . '/view_product.htm';
-
- // 查询是否存在的字符串
- $String = !empty($String) ? $String : ['ReturnData', 'spec_name', 'spec_value', 'SpecClass', 'SpecData'];
-
- // 获取出文件内容
- $GetHtml = @file_get_contents($Url);
-
- // 查询是否匹配
- $ResultData = [];
- foreach ($String as $value) {
- if (strpos($GetHtml, $value) === false) {
- array_push($ResultData, $value);
- }
- }
-
- // 返回结果
- return $ResultData;
- }
- }
-
- /**
- * 获取区域子域名URL
- *
- * @access public
- * @param string $subDomain
- * @return string
- */
- if (!function_exists('getRegionDomainUrl')) {
- function getRegionDomainUrl($subDomain = '', $root_dir = true)
- {
- $domain = request()->host(true);
- if (!empty($subDomain) && !preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i', $domain)) {
- $domain = request()->subDomain($subDomain);
- }
- true === $root_dir && $domain .= ROOT_DIR;
-
- return $domain;
- }
- }
-
- /**
- * 获取URl子域名,忽略IP地址
- *
- * @access public
- * @param string $subDomain
- * @return string
- */
- if (!function_exists('getSubDomain')) {
- function getSubDomain($root_dir = true)
- {
- $domain = request()->host(true);
- if (!empty($subDomain) && !preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/i', $domain)) {
- $rootDomain = request()->rootDomain();
- $domain = $subDomain . '.' . $rootDomain;
- }
- true === $root_dir && $domain .= ROOT_DIR;
-
- return $domain;
- }
- }
-
- if (!function_exists('get_split_word')) {
- /**
- * 自动获取关键字
- *
- * @access public
- * @param string $title 标题
- * @param array $body 内容
- * @return string
- */
- function get_split_word($title = '', $body = '')
- {
- vendor('splitword.autoload');
- $keywords = '';
- $kw = new keywords();
- $keywords = $kw->GetSplitWord($title, $body);
-
- return $keywords;
- }
- }
-
- if (!function_exists('remote_to_local')) {
- /**
- * 远程图片本地化
- *
- * @access public
- * @param string $body 内容
- * @return string
- */
- function remote_to_local($body = '')
- {
- $web_basehost = tpCache('web.web_basehost');
- $parse_arr = parse_url($web_basehost);
- $host = request()->host(true);
- $img_array = array();
- preg_match_all("/src=[\"|'|\s]([^\"|^\'|^\s]*?)/isU", $body, $img_array);
-
- $img_array = array_unique($img_array[1]);
- foreach ($img_array as $key => $val) {
- if (preg_match("/^http(s?):\/\/(mmbiz.qpic.cn|thirdwx.qlogo.cn)\/(.*)\?wx_fmt=(\w+)&/", $val) == 1) {
- unset($img_array[$key]);
- }
- }
-
- $dirname = './' . UPLOAD_PATH . 'allimg/' . date('Ymd/');
-
- //创建目录失败
- if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
- return $body;
- } else if (!is_writeable($dirname)) {
- return $body;
- }
-
- // 插件列表
- static $weappList = null;
- if (null === $weappList) {
- $weappList = \think\Db::name('weapp')->where([
- 'status' => 1,
- ])->cache(true, EYOUCMS_CACHE_TIME, 'weapp')
- ->getAllWithIndex('code');
- }
- $storageDomain = ''; // 第三方存储的域名
- if (!empty($weappList['Qiniuyun']['data'])) {
- $qnyData = json_decode($weappList['Qiniuyun']['data'], true);
- if (!empty($qnyData['domain'])) {
- $storageDomain = $qnyData['domain'];
- }
- } else if (!empty($weappList['AliyunOss']['data'])) {
- $ossData = json_decode($weappList['AliyunOss']['data'], true);
- if (!empty($ossData['domain'])) {
- $storageDomain = $ossData['domain'];
- }
- } else if (!empty($weappList['Cos']['data'])) {
- $cosData = json_decode($weappList['Cos']['data'], true);
- if (!empty($cosData['domain'])) {
- $storageDomain = $cosData['domain'];
- }
- }
-
- foreach ($img_array as $key => $value) {
- $imgUrl = trim($value);
- $imgUrl = preg_replace('/#/', '', $imgUrl);
- // 本站图片 / 根网址图片 / 第三方存储插件的图片
- if (!empty($parse_arr['host'])){
- if (preg_match("/\/\/({$host}|{$storageDomain}|{$parse_arr['host']})\//i", $imgUrl)) {
- continue;
- }
- }else{
- if (preg_match("/\/\/({$host}|{$storageDomain})\//i", $imgUrl)) {
- continue;
- }
- }
- // 不是合法链接
- if (!preg_match("#^http(s?):\/\/#i", $imgUrl)) {
- continue;
- }
-
- $heads = @get_headers($imgUrl, 1);
-
- // 获取请求头并检测死链
- if (empty($heads) || !(stristr($heads[0], "200") && !stristr($heads[0], "304"))) {
- continue;
- }
- // 图片扩展名
- $fileType = substr($heads['Content-Type'], -4, 4);
- if (!preg_match("#\.(jpg|jpeg|gif|png|ico|bmp|webp|svg)#i", $fileType)) {
- $filext = str_ireplace('image/', '', $heads['Content-Type']);
- if ($fileType == 'image/gif' || $filext == 'gif') {
- $fileType = ".gif";
- } else if ($fileType == 'image/png' || $filext == 'png') {
- $fileType = ".png";
- } else if ($fileType == 'image/x-icon' || $filext == 'x-icon') {
- $fileType = ".ico";
- } else if ($fileType == 'image/bmp' || $filext == 'bmp') {
- $fileType = ".bmp";
- } else if ($fileType == 'image/webp' || $filext == 'webp') {
- $fileType = ".webp";
- } else if ($heads['Content-Type'] == 'image/svg+xml' || $filext == 'svg+xml') {
- $fileType = ".svg";
- } else {
- $fileType = '.jpg';
- }
- }
- $fileType = strtolower($fileType);
- //格式验证(扩展名验证和Content-Type验证),链接contentType是否正确
- $is_weixin_img = false;
- if (preg_match("/^http(s?):\/\/(mmbiz.qpic.cn|thirdwx.qlogo.cn)\/(.*)/", $imgUrl) != 1) {
- $allowFiles = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".webp", ".svg"];
- if (!in_array($fileType, $allowFiles) || (isset($heads['Content-Type']) && !stristr($heads['Content-Type'], "image/"))) {
- continue;
- }
- } else {
- $is_weixin_img = true;
- }
-
- //打开输出缓冲区并获取远程图片
- ob_start();
- $context = stream_context_create(
- array('http' => array(
- 'follow_location' => false // don't follow redirects
- ))
- );
- readfile($imgUrl, false, $context);
- $img = ob_get_contents();
- ob_end_clean();
- preg_match("/[\/]([^\/]*)[\.]?[^\.\/]*$/", $imgUrl, $m);
-
- $file = [];
- $file['oriName'] = $m ? $m[1] : "";
- $file['filesize'] = strlen($img);
- $file['ext'] = $fileType;
- $file['name'] = session('admin_id') . '-' . dd2char(date("ymdHis") . mt_rand(100, 999)) . $file['ext'];
- $file['fullName'] = $dirname . $file['name'];
- $fullName = $file['fullName'];
-
- //检查文件大小是否超出限制
- if ($file['filesize'] >= 20480000) {
- continue;
- }
-
- //移动文件
- if (!(file_put_contents($fullName, $img) && file_exists($fullName))) { //移动失败
- continue;
- }
-
- $fileurl = ROOT_DIR . substr($file['fullName'], 1);
- // if ($is_weixin_img == true) {
- // $fileurl .= "?";
- // }
-
- /* $search = array("'".$imgUrl."'", '"'.$imgUrl.'"');
- $replace = array($fileurl, $fileurl);
- $body = str_replace($search, $replace, $body);*/
-
- // 添加水印
- try {
- print_water($fileurl);
- /*同步到第三方对象存储空间*/
- $bucket_data = SynImageObjectBucket($fileurl, $weappList);
- if (!empty($bucket_data['url']) && is_string($bucket_data['url'])) {
- $fileurl = $bucket_data['url'];
- }
- /*end*/
- } catch (\Exception $e) {}
-
- $body = str_replace($imgUrl, $fileurl, $body);
-
- // 添加图片进数据库
- $img_info = @getimagesize('.'.substr($file['fullName'], 1));
- $width = isset($img_info[0]) ? $img_info[0] : 0;
- $height = isset($img_info[1]) ? $img_info[1] : 0;
- $mime = isset($img_info['mime']) ? $img_info['mime'] : '';
- $addData[] = [
- 'aid' => 0,
- 'type_id' => 0,
- 'image_url' => $fileurl,
- 'title' => '',
- 'intro' => '',
- 'width' => $width,
- 'height' => $height,
- 'filesize' => $file['filesize'],
- 'mime' => $mime,
- 'users_id' => (int)session('admin_info.syn_users_id'),
- 'sort_order' => 100,
- 'add_time' => getTime(),
- 'update_time' => getTime(),
- ];
- }
-
- // 添加图片进数据库
- // !empty($addData) && \think\Db::name('uploads')->insertAll($addData);
-
- return $body;
- }
- }
-
- if (!function_exists('replace_links')) {
- /**
- * 清除非站内链接
- *
- * @access public
- * @param string $body 内容
- * @param array $allow_urls 允许的超链接
- * @return string
- */
- function replace_links($body, $allow_urls = array())
- {
- // 读取允许的超链接设置
- $host = request()->host(true);
- $rootDomain = request()->rootDomain();
- if (!empty($allow_urls)) {
- $allow_urls = array_merge([$host,$rootDomain], $allow_urls);
- } else {
- $basic_body_allowurls = tpCache('basic.basic_body_allowurls');
- if (!empty($basic_body_allowurls)) {
- $allowurls = explode(PHP_EOL, $basic_body_allowurls);
- $allow_urls = array_merge([$host,$rootDomain], $allowurls);
- } else {
- $allow_urls = [$host,$rootDomain];
- }
- }
- $web_basehost = tpCache('web.web_basehost');
- $parse_arr = parse_url($web_basehost);
- if (!empty($parse_arr['host'])) {
- array_push($allow_urls, $parse_arr['host']);
- }
-
- $host_rule = join('|', $allow_urls);
- $host_rule = preg_replace("#[\n\r]#", '', $host_rule);
- $host_rule = str_replace('.', "\\.", $host_rule);
- $host_rule = str_replace('/', "\\/", $host_rule);
- $arr = '';
- preg_match_all("#<a([^>]*)>(.*)<\/a>#iU", $body, $arr);
- if (is_array($arr[0])) {
- $rparr = array();
- $tgarr = array();
- foreach ($arr[0] as $i => $v) {
- if ( ($host_rule != '' && preg_match('#' . $host_rule . '#i', $arr[1][$i])) || !preg_match('/(\s+)href(\s*)=(\s*)([\'|\"]?)((\w)*:)?(\/\/)/i', $arr[1][$i]) ) {
- continue;
- } else {
- $rparr[] = $v;
- $tgarr[] = $arr[2][$i];
- }
- }
- if (!empty($rparr)) {
- $body = str_replace($rparr, $tgarr, $body);
- }
- }
- $arr = $rparr = $tgarr = '';
- return $body;
- }
- }
-
- if (!function_exists('print_water')) {
- /**
- * 给图片增加水印
- *
- * @access public
- * @param string $imgpath 不带子目录的图片路径
- * @return string
- */
- function print_water($imgpath = '')
- {
- try {
- static $water = null;
- null === $water && $water = tpCache('water');
- if (empty($imgpath) || $water['is_mark'] != 1) {
- return $imgpath;
- }
-
- $imgpath = handle_subdir_pic($imgpath, 'img', false, true); // 支持子目录
- $imgresource = "." . $imgpath;
- $image = \think\Image::open($imgresource);
- if ($image->width() > $water['mark_width'] && $image->height() > $water['mark_height']) {
- if ($water['mark_type'] == 'text') {
- //$image->text($water['mark_txt'],ROOT_PATH.'public/static/common/font/hgzb.ttf',20,'#000000',9)->save($imgresource);
- $ttf = ROOT_PATH . 'public/static/common/font/hgzb.ttf';
- if (file_exists($ttf)) {
- $size = $water['mark_txt_size'] ? $water['mark_txt_size'] : 30;
- $color = $water['mark_txt_color'] ?: '#000000';
- if (!preg_match('/^#[0-9a-fA-F]{6}$/', $color)) {
- $color = '#000000';
- }
- $transparency = intval((100 - $water['mark_degree']) * (127 / 100));
- $color .= dechex($transparency);
- $image->text($water['mark_txt'], $ttf, $size, $color, $water['mark_sel'])->save($imgresource);
- }
- } else {
- $water['mark_img'] = preg_replace('/\?(.*)$/i', '', $water['mark_img']);
- $water['mark_img'] = handle_subdir_pic($water['mark_img'], 'img', false, true); // 支持子目录
- //$image->water(".".$water['mark_img'],9,$water['mark_degree'])->save($imgresource);
- $waterPath = "." . $water['mark_img'];
- if (eyPreventShell($waterPath) && file_exists($waterPath)) {
- $waterImgInfo = @getimagesize($waterPath);
- $waterImgW = !empty($waterImgInfo[0]) ? $waterImgInfo[0] : 1000000;
- $waterImgH = !empty($waterImgInfo[1]) ? $waterImgInfo[1] : 1000000;
- if ($image->width() > $waterImgW && $image->height() > $waterImgH) {
- $quality = $water['mark_quality'] ? $water['mark_quality'] : 80;
- $waterTempPath = dirname($waterPath) . '/temp_' . basename($waterPath);
- $image->open($waterPath)->save($waterTempPath, null, $quality);
- $image->water($waterTempPath, $water['mark_sel'], $water['mark_degree'])->save($imgresource);
- @unlink($waterTempPath);
- }
- }
- }
- }
- } catch (\Exception $e) {
- }
- }
- }
-
- if (!function_exists('filterNickname')) {
- // 过滤微信表情符号
- function filterNickname($nickname = '')
- {
- $nickname = preg_replace('/[\x{1F600}-\x{1F64F}]/u', '', $nickname);
- $nickname = preg_replace('/[\x{1F300}-\x{1F5FF}]/u', '', $nickname);
- $nickname = preg_replace('/[\x{1F680}-\x{1F6FF}]/u', '', $nickname);
- $nickname = preg_replace('/[\x{2600}-\x{26FF}]/u', '', $nickname);
- $nickname = preg_replace('/[\x{2700}-\x{27BF}]/u', '', $nickname);
- $nickname = str_replace(array('"', '\''), '', $nickname);
- return addslashes(trim($nickname));
- }
- }
-
- if (!function_exists('mchStrCode')) {
- /**
- * 加密函数
- *
- * @access public
- * @param string $string 字符串
- * @param string $operation 操作
- * @return string
- */
- function mchStrCode($string, $operation = 'ENCODE', $auth_code = '')
- {
- if (empty($auth_code)) {
- $auth_code = get_auth_code();
- }
-
- $key_length = 4;
- $expiry = 0;
- $key = md5($auth_code);
- $fixedkey = md5($key);
- $egiskeys = md5(substr($fixedkey, 16, 16));
- $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
- $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
- $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
-
- $i = 0;
- $result = '';
- $string_length = strlen($string);
- for ($i = 0; $i < $string_length; $i++) {
- $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
- }
- if ($operation == 'ENCODE') {
- return $runtokey . str_replace('=', '', base64_encode($result));
- } else {
- $str1 = substr($result, 0, 10);
- if(version_compare(PHP_VERSION,'8.0.0','>')) {
- $str1 = !empty($str1) ? $str1 : 0;
- }
-
- if (($str1 == 0 || intval($str1) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $egiskeys), 0, 16)) {
- return substr($result, 26);
- } else {
- return '';
- }
- }
- }
- }
-
- if (!function_exists('html_httpimgurl')) {
- /**
- * html内容里的图片地址替换成http路径
- * @param string $content 内容
- * @return string
- */
- function html_httpimgurl($content = '', $timeVersion = false)
- {
- if (!empty($content)) {
- $t = '';
- if (true === $timeVersion) {
- $t = '?t='.getTime();
- }
-
- $pregRule = "/<img(.*?)src(\s*)=(\s*)[\'|\"]\/(.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp|\.ico|\.webp]))[\'|\"](.*?)[\/]?(\s*)>/i";
- $content = preg_replace($pregRule, '<img ${1} src="' . request()->domain() . '/${4}'.$t.'" ${5} />', $content);
- }
-
- return $content;
- }
- }
-
- if (!function_exists('getCityLocation')) {
- /**
- * 根据IP获取地区
- * @param string $ip [description]
- * @return [type] [description]
- */
- function getCityLocation($ip = '')
- {
- if (preg_match('/127\.0\.\d{1,3}\.\d{1,3}/i', $ip) || preg_match('/192\.168\.\d{1,3}\.\d{1,3}/i', $ip) || 'localhost' == $ip) {
- return ['location'=>'本地局域网'];
- } else {
- try {
- $res1 = httpRequest("https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$ip}&resource_id=6006&t=" . getMsectime());
- $res1 = iconv('GB2312', 'UTF-8', $res1);
- $res1 = json_decode($res1, true);
- if ($res1 && $res1['status'] == '0') {
- $data = current($res1['data']);
- \think\Cookie::set("city_localtion", $data);
- return $data;
- }
-
- /* $res1 = httpRequest("http://ip.taobao.com/service/getIpInfo.php?ip=" .$ip);
- $res1 = json_decode($res1,true);
- if($res1 && $res1['code'] == '0'){
- \think\Cookie::set("city_localtion", $res1['data']);
- return $res1['data'];
- }*/
- } catch (\Exception $e) {}
- }
-
- return false;
- }
- }
-
-
- if (!function_exists('Convert_GCJ02_To_BD09')) {
- /**
- * 中国正常GCJ02坐标---->百度地图BD09坐标
- * 腾讯地图用的也是GCJ02坐标
- * @param double $lat 纬度
- * @param double $lng 经度
- */
- function Convert_GCJ02_To_BD09($lat = 0, $lng = 0)
- {
- $x_pi = M_PI;
- $x_pi = $x_pi * 3000.0 / 180.0;
- $x = $lng;
- $y = $lat;
- $z = sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
- $lng = $z * cos($theta) + 0.0065;
- $lat = $z * sin($theta) + 0.006;
-
- return ['lng' => $lng, 'lat' => $lat];
- }
- }
-
- if (!function_exists('Convert_BD09_To_GCJ02')) {
- /*
- * 百度地图BD09坐标---->中国正常GCJ02坐标
- * 腾讯地图用的也是GCJ02坐标
- * @param double $lat 纬度
- * @param double $lng 经度
- * @return array();
- */
- function Convert_BD09_To_GCJ02($lat = 0, $lng = 0)
- {
- $x_pi = M_PI;
- $x_pi = $x_pi * 3000.0 / 180.0;
- $x = $lng - 0.0065;
- $y = $lat - 0.006;
- $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
- $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
- $lng = $z * cos($theta);
- $lat = $z * sin($theta);
-
- return ['lat' => $lat, 'lng' => $lng];
- }
- }
-
- if (!function_exists('get_filename')) {
- /**
- * 上传附件类型前台处理url获得文件名称(带拓展名)
- * @param string $value
- * @return mixed
- */
- function get_filename($value='')
- {
- $value_arr = explode('/',$value);
- $str = end($value_arr);
- return $str;
- }
- }
-
- if (!function_exists('gmSecondFormat')) {
- /**
- * 将秒数转为时间格式
- * @param intval $seconds 秒数
- * @param string $separator 分隔符
- * @return mixed
- */
- function gmSecondFormat($seconds = 0, $separator = '')
- {
- $timeStr = '';
- if (empty($seconds)) {
- if (empty($separator)) {
- $timeStr = "00小时00分钟00秒";
- } else {
- $timeStr = "00{$separator}00{$separator}00";
- }
- } else {
- $seconds = intval($seconds);
- $hours = intval($seconds/3600);
- if ($hours < 10) {
- $hours = '0'.$hours;
- }
- if (empty($separator)) {
- $timeStr = $hours."小时".gmdate('i分钟s秒', $seconds);
- } else {
- $timeStr = $hours.$separator.gmdate("i{$separator}s", $seconds);
- }
- }
-
- return $timeStr;
- }
- }
-
- if (!function_exists('checkAuthRule')) {
- /**
- * 验证代理贴牌的功能权限
- * @return mixed
- */
- function checkAuthRule($id)
- {
- $admin_id = session('admin_id');
- $info = \think\Db::name('admin')->where('admin_id',$admin_id)->field('parent_id,role_id')->find();
- if (!empty($info) && empty($info['parent_id']) && $info['role_id'] == -1){
- //创始人拥有无上权限
- return true;
- }
-
- $php_auth_function = tpCache('php.php_auth_function');
- $auth_function = !empty($php_auth_function) ? explode(',', $php_auth_function) : [];
- if (!empty($auth_function) && !in_array($id, $auth_function)){
- return false;
- }else{
- return true;
- }
- }
- }
-
- if (!function_exists('check_illegal')) {
- /**
- * 检测上传图片是否包含有非法代码
- * @return mixed
- */
- function check_illegal($image = '', $is_force = false)
- {
- $weapp_check_illegal_open = tpCache('weapp.weapp_check_illegal_open');
- if ($is_force || (is_numeric($weapp_check_illegal_open) && strval($weapp_check_illegal_open) === '0')) {
- try {
- $hexCode = file_get_contents($image);
- if (preg_match('#file_put_contents#i', $hexCode) || preg_match('#__HALT_COMPILER()#i', $hexCode) || preg_match('#/script>#i', $hexCode) || preg_match('#<([^?]*)\?php#i', $hexCode) || preg_match('#<\?\=(\s+)#i', $hexCode) || preg_match('#(\s+)language(\s*)=(\s*)("|\')?php("|\')?#i', $hexCode)) {
- return false;
- }
- // if (file_exists($image)) {
- // $resource = fopen($image, 'rb');
- // $fileSize = filesize($image);
- // fseek($resource, 0);
- // $hexCode = fread($resource, $fileSize);
- // fclose($resource);
- // if (preg_match('#file_put_contents#i', $hexCode) || preg_match('#__HALT_COMPILER()#i', $hexCode) || preg_match('#/script>#i', $hexCode) || preg_match('#<([^?]*)\?php#i', $hexCode) || preg_match('#<\?\=(\s+)#i', $hexCode)) {
- // return false;
- // }
- // }
- } catch (\Exception $e) {
- return false;
- }
- }
- return true;
- }
- }
-
- if (!function_exists('getUsersTplVersion')) {
- /**
- * 获取当前会员模板的版本号
- *
- * @return string
- */
- function getUsersTplVersion()
- {
- $ver = 'v1';
- $web_users_tpl_theme = tpCache('web.web_users_tpl_theme');
- if (empty($web_users_tpl_theme)) {
- $web_users_tpl_theme = 'users';
- }
- $version_txt_path = "./template/".THEME_STYLE_PATH."/{$web_users_tpl_theme}/version.txt";
- if (file_exists(realpath($version_txt_path))) {
- $fp = fopen($version_txt_path, 'r');
- $content = fread($fp, filesize($version_txt_path));
- fclose($fp);
- $ver = $content ? $content : $ver;
- }
- return $ver;
- }
- }
-
- if (!function_exists('getUsersTpl2xVersion')) {
- /**
- * 获取当前V2会员模板的版本号
- *
- * @return string
- */
- function getUsersTpl2xVersion()
- {
- $ver = '';
- $web_users_tpl_theme = tpCache('web.web_users_tpl_theme');
- if (empty($web_users_tpl_theme)) {
- $web_users_tpl_theme = 'users';
- }
- $version_txt_path = "./template/".THEME_STYLE_PATH."/{$web_users_tpl_theme}/version_2x.txt";
- if (file_exists(realpath($version_txt_path))) {
- $fp = fopen($version_txt_path, 'r');
- $content = fread($fp, filesize($version_txt_path));
- fclose($fp);
- $ver = $content ? $content : $ver;
- }
- return $ver;
- }
- }
-
- if (!function_exists('hex2rgba')) {
- /**
- * 16进制颜色代码转换为rgba,rgb格式
- *
- * @return string
- */
- function hex2rgba($color, $opacity = false, $raw = false) {
- $default = 'rgb(0,0,0)';
- //Return default if no color provided
- if(empty($color))
- return $default;
- //Sanitize $color if "#" is provided
- if ($color[0] == '#' ) {
- $color = substr( $color, 1 );
- }
- //Check if color has 6 or 3 characters and get values
- if (strlen($color) == 6) {
- $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
- } elseif ( strlen( $color ) == 3 ) {
- $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
- } else {
- return $default;
- }
-
- //Convert hexadec to rgb
- $rgb = array_map('hexdec', $hex);
-
- if($raw){
- if($opacity){
- if(abs($opacity) > 1) $opacity = 1.0;
- array_push($rgb, $opacity);
- }
- $output = $rgb;
- }else{
- //Check if opacity is set(rgba or rgb)
- if($opacity){
- if(abs($opacity) > 1)
- $opacity = 1.0;
- $output = 'rgba('.implode(",",$rgb).','.$opacity.')';
- } else {
- $output = 'rgb('.implode(",",$rgb).')';
- }
- }
-
- //Return rgb(a) color string
- return $output;
- }
- }
-
- if (!function_exists('is_local_ip'))
- {
- /**
- * 简单判断当前访问站点是否本地
- * @param string $domain 不带协议的域名
- * @return boolean
- */
- function is_local_ip($domain = '')
- {
- $is_local = false;
- $sip = serverIP();
- $domain = !empty($domain) ? $domain : request()->host();
- if (preg_match('/127\.0\.\d{1,3}\.\d{1,3}/i', $domain) || preg_match('/192\.168\.\d{1,3}\.\d{1,3}/i', $domain) || 'localhost' == $domain || '127.0.0.1' == $sip) {
- $is_local = true;
- }
-
- return $is_local;
- }
- }
-
- if (!function_exists('upload_max_filesize'))
- {
- /**
- * 获取当前可上传文件大小
- * @return boolean
- */
- function upload_max_filesize()
- {
- $file_size = tpCache('basic.file_size');
- $postsize = @ini_get('file_uploads') ? ini_get('post_max_size') : -1;
- $fileupload = @ini_get('file_uploads') ? ini_get('upload_max_filesize') : -1;
- $min_size = intval($file_size) < intval($postsize) ? $file_size : $postsize;
- $min_size = intval($min_size) < intval($fileupload) ? $min_size : $fileupload;
- $upload_max_filesize = intval($min_size) * 1024 * 1024;
- return $upload_max_filesize;
- }
- }
-
- if (!function_exists('image_accept_arr'))
- {
- /**
- * 上传图片扩展名对应的accept,应用于选择上传图片时,系统自带的选择框里只列出指定图片扩展名的图片文件
- * @return boolean
- */
- function image_accept_arr($image_type = '')
- {
- $accept = '';
- if (!empty($image_type)) {
- if (is_string($image_type)) {
- $image_type_arr = explode(',', $image_type);
- } else {
- $image_type_arr = $image_type;
- }
- foreach ($image_type_arr as $key => $val) {
- if ($key > 0) $accept .= ',';
- if ('icon' == $val) {
- $accept .= "image/x-icon";
- } else if ('svg' == $val) {
- $accept .= "image/svg+xml";
- } else {
- $accept .= "image/{$val}";
- }
- }
- } else {
- $accept = 'image/gif,image/jpg,image/jpeg,image/png,image/bmp,image/x-icon,image/webp,image/svg+xml';
- }
-
- return $accept;
- }
- }
-
- if (!function_exists('filename_preg_match'))
- {
- /**
- * 正则判断文件名、文件路径的字符串是否合法
- * @return boolean
- */
- function filename_preg_match($filename = '')
- {
- if (preg_match('/(<|>|\"|\*|\:|\?|\|)/i', $filename)) {
- return false;
- }
-
- return true;
- }
- }
|