my_functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * 浏览器友好的变量输出
  4. * @param mixed $var 变量
  5. * @param boolean $echo 是否输出 默认为True 如果为false 则返回输出字符串
  6. * @param string $label 标签 默认为空
  7. * @param boolean $strict 是否严谨 默认为true
  8. * @return void|string
  9. */
  10. function dump($var, $echo=true, $label=null, $strict=true) {
  11. $label = ($label === null) ? '' : rtrim($label) . ' ';
  12. if (!$strict) {
  13. if (ini_get('html_errors')) {
  14. $output = print_r($var, true);
  15. $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
  16. } else {
  17. $output = $label . print_r($var, true);
  18. }
  19. } else {
  20. ob_start();
  21. var_dump($var);
  22. $output = ob_get_clean();
  23. if (!extension_loaded('xdebug')) {
  24. $output = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $output);
  25. $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';
  26. }
  27. }
  28. if ($echo) {
  29. echo($output);
  30. return null;
  31. }else
  32. return $output;
  33. }
  34. /**
  35. * 数据过滤函数
  36. * @param $string 要过滤的字符串
  37. */
  38. function safe_replace($string) {
  39. $string = trim($string);
  40. $string = str_replace('%20','',$string);
  41. $string = str_replace('%27','',$string);
  42. $string = str_replace('%2527','',$string);
  43. $string = str_replace('*','',$string);
  44. $string = str_replace('"','&quot;',$string);
  45. $string = str_replace(' ','',$string);
  46. $string = str_replace(' ','',$string);
  47. $string = str_replace("'",'',$string);
  48. $string = str_replace('"','',$string);
  49. $string = str_replace(';','',$string);
  50. $string = str_replace('<','&lt;',$string);
  51. $string = str_replace('>','&gt;',$string);
  52. $string = str_replace("{",'',$string);
  53. $string = str_replace('}','',$string);
  54. $string = str_replace('\\','',$string);
  55. return $string;
  56. }
  57. function exsl_safe_replace($string) {
  58. $string = trim($string);
  59. $string = str_replace('%20','',$string);
  60. $string = str_replace('%27','',$string);
  61. $string = str_replace('%2527','',$string);
  62. $string = str_replace('*','',$string);
  63. $string = str_replace('"','&quot;',$string);
  64. $string = str_replace("'",'',$string);
  65. $string = str_replace('"','',$string);
  66. $string = str_replace(';','',$string);
  67. $string = str_replace('<','&lt;',$string);
  68. $string = str_replace('>','&gt;',$string);
  69. $string = str_replace("{",'',$string);
  70. $string = str_replace('}','',$string);
  71. $string = str_replace('\\','',$string);
  72. $string = str_replace(' ','',$string);
  73. $string = str_replace(',','',$string);
  74. return $string;
  75. }
  76. /**
  77. * @param $string 需要加密解密的字符串
  78. * @param $operation 判断是加密还是解密,E表示加密,D表示解密
  79. * @param $key 密匙
  80. */
  81. function fasterEncrypt($string,$operation,$key='kzb_business_2015'){
  82. $key = md5($key);
  83. $key_length = strlen($key);
  84. $string = $operation == 'D' ? base64_decode($string):substr(md5($string.$key),0,8).$string;
  85. $string_length = strlen($string);
  86. $rndkey = $box = array();
  87. $result = '';
  88. for($i=0;$i<=255;$i++){
  89. $rndkey[$i] = ord($key[$i%$key_length]);
  90. $box[$i] = $i;
  91. }
  92. for($j=$i=0;$i<256;$i++){
  93. $j = ($j+$box[$i]+$rndkey[$i])%256;
  94. $tmp = $box[$i];
  95. $box[$i] = $box[$j];
  96. $box[$j] = $tmp;
  97. }
  98. for($a=$j=$i=0;$i<$string_length;$i++){
  99. $a = ($a+1)%256;
  100. $j = ($j+$box[$a])%256;
  101. $tmp = $box[$a];
  102. $box[$a] = $box[$j];
  103. $box[$j] = $tmp;
  104. $result.=chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));
  105. }
  106. if($operation == 'D'){
  107. if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8)){
  108. return substr($result,8);
  109. }else{
  110. return'';
  111. }
  112. }else{
  113. return str_replace('=','',base64_encode($result));
  114. }
  115. }
  116. // 获取唯一id
  117. function getUUID(){
  118. $command = Yii::app()->businessDb->createCommand('select RIGHT(UUID_SHORT(), 20) as uuid');
  119. $uninfo = $command->queryAll();
  120. if (empty($uninfo[0]['uuid'])) {
  121. Yii::app()->jump->error('获取uuid失败!');
  122. }
  123. return $uninfo[0]['uuid'];
  124. }
  125. function getUniqueId($schoolId){
  126. $api=Yii::app()->params['getUniqueIdApi']['urlSingle'];
  127. $key=Yii::app()->params['getUniqueIdApi']['key'];
  128. $userName=$schoolId;
  129. $password=md5($userName.$key);
  130. $maxRequestCount=3;
  131. $uuid=0;
  132. for($i=1;$i<=$maxRequestCount;$i++){
  133. $ch = curl_init();
  134. curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
  135. curl_setopt($ch, CURLOPT_USERPWD,$userName .':'. $password);
  136. curl_setopt( $ch, CURLOPT_URL, $api );
  137. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 3);
  138. curl_setopt( $ch, CURLOPT_HEADER, false );
  139. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  140. $response = curl_exec( $ch );
  141. curl_close( $ch );
  142. $data=json_decode($response,true);
  143. if($data['errCode']==00 && !empty($data['data'])){
  144. $uuid= $data['data'];
  145. break;
  146. }
  147. }
  148. if($uuid){
  149. return $uuid;
  150. }else{
  151. Yii::app()->jump->error('获取uuid失败!');
  152. }
  153. }
  154. /**
  155. * 获取批量uuid
  156. * @param $school_id
  157. * @param int $uuid_num
  158. * @return mixed
  159. */
  160. function getBatchUuid($school_id,$uuid_num = 1){
  161. $uuid_arr = array();
  162. $api=Yii::app()->params['getUniqueIdApi']['urlBatch'];
  163. $key=Yii::app()->params['getUniqueIdApi']['key'];
  164. $url = $api.$uuid_num;
  165. $params['method'] = 'get';
  166. $params['username'] = $school_id;
  167. $params['password'] = md5($school_id . $key);
  168. $params['timeout'] = 2;
  169. //最多调3次
  170. foreach (array(1, 2, 3) as $value) {
  171. $rs = commonCurl($url, $data = null, $params);
  172. if (isset($rs['errCode']) && $rs['errCode'] == '00') {
  173. $uuid_arr = $rs['data'];
  174. break;
  175. }else{
  176. $msg = '获取uuid接口失败' . $value . '次,' . (isset($rs['errMsg']) ? $rs['errMsg'] : '无信息返回');
  177. if ($value == 3) {
  178. Yii::app()->jump->error($msg);
  179. }
  180. }
  181. }
  182. if (count($uuid_arr) < $uuid_num) {
  183. $msg = '接口返回uuid数量不符合';
  184. Yii::app()->jump->error($msg);
  185. }
  186. $returnData=array();
  187. if($uuid_arr){
  188. foreach ($uuid_arr as $id){
  189. $returnData[(string)$id]=(string)$id;
  190. }
  191. }
  192. return $returnData;
  193. }
  194. // curl获取接口数据
  195. function getCurlData($url,$data){
  196. $data = json_encode($data);
  197. $header = array(
  198. 'Content-Type: application/json',
  199. 'Content-Length: '. strlen($data),
  200. );
  201. $ch = curl_init();//初始化CURL句柄
  202. // Digest认证
  203. curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
  204. //curl_setopt($ch, CURLOPT_USERPWD,'test:123456');
  205. curl_setopt($ch, CURLOPT_USERPWD,Yii::app()->params['api_verify_username'] .':'. Yii::app()->params['api_verify_password']);
  206. curl_setopt( $ch, CURLOPT_URL, $url );
  207. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  208. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 15 ); //在发起连接前等待的时间,如果设置为0,则不等待
  209. curl_setopt( $ch, CURLOPT_HEADER, false ); //设定是否输出页面内容
  210. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );//设定是否显示头信息
  211. curl_setopt( $ch, CURLOPT_POST, true ); //启用POST方式
  212. //curl_setopt( $ch, CURLOPT_POSTFIELDS, $data); //请求参数
  213. curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); //请求参数
  214. //curl_setopt( $ch, CURLOPT_REFERER, "http://mall.xkhouse.com" ); //伪造来源地址
  215. $response = curl_exec( $ch );
  216. curl_close( $ch );
  217. return trim($response);
  218. }
  219. /**
  220. * Curl方法
  221. * @param $path
  222. * @param string $method
  223. * @param null $data
  224. * @param array $params (method,username,password)
  225. * @return mixed
  226. */
  227. function commonCurl($url, $data = null,$params = array()){
  228. $timeout = 5;
  229. if (isset($params['method']) && $params['method']) {
  230. $method = strtoupper($params['method']);
  231. }else{
  232. $method = 'POST';
  233. }
  234. $auth_username = isset($params['username']) ? trim($params['username']) : '';
  235. $auth_password = isset($params['password']) ? trim($params['password']) : '';
  236. if (isset($params['timeout']) && $params['timeout']) {
  237. $timeout = $params['timeout'];
  238. }
  239. $headers = array('Accept: application/json', 'Content-Type: application/json', );
  240. $ch = curl_init();
  241. curl_setopt($ch, CURLOPT_URL, $url);
  242. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  243. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  244. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  245. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  246. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//5秒
  247. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//5秒
  248. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  249. curl_setopt($ch, CURLOPT_USERPWD, $auth_username .':'. $auth_password);
  250. switch($method) {
  251. case 'GET' :
  252. break;
  253. case 'POST' :
  254. if ($data) {
  255. $data = json_encode($data);
  256. }
  257. curl_setopt($ch, CURLOPT_POST, true);
  258. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  259. break;
  260. case 'PUT' :
  261. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  262. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  263. break;
  264. case 'DELETE' :
  265. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  266. break;
  267. }
  268. if( !curl_errno($ch)){
  269. $response = json_decode(curl_exec($ch), true);
  270. }else{
  271. $response = false;
  272. }
  273. // $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  274. // 释放资源
  275. curl_close($ch);
  276. return $response;
  277. }
  278. // curl获取接口数据
  279. function getCurlDataFor($url,$data){
  280. $header = array();
  281. $ch = curl_init();//初始化CURL句柄
  282. // Digest认证
  283. curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
  284. //curl_setopt($ch, CURLOPT_USERPWD,'test:123456');
  285. curl_setopt($ch, CURLOPT_USERPWD,Yii::app()->params['api_verify_username'] .':'. Yii::app()->params['api_verify_password']);
  286. curl_setopt( $ch, CURLOPT_URL, $url );
  287. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  288. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 15 ); //在发起连接前等待的时间,如果设置为0,则不等待
  289. curl_setopt( $ch, CURLOPT_HEADER, false ); //设定是否输出页面内容
  290. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );//设定是否显示头信息
  291. curl_setopt( $ch, CURLOPT_POST, true ); //启用POST方式
  292. //curl_setopt( $ch, CURLOPT_POSTFIELDS, $data); //请求参数
  293. curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); //请求参数
  294. //curl_setopt( $ch, CURLOPT_REFERER, "http://mall.xkhouse.com" ); //伪造来源地址
  295. $response = curl_exec( $ch );
  296. curl_close( $ch );
  297. return trim($response);
  298. }
  299. function apiPost($url, $data, $type = 0) {
  300. $ch = @curl_init();
  301. $result = FALSE;
  302. if ($ch) {
  303. if (is_array($data) && isset($data['templetA'])) {
  304. $paramArray = array();
  305. if (version_compare(PHP_VERSION, '5.4.9') >= 0) {
  306. //$cfile = curl_file_create($data['templetAFileName'],'application/vnd.openxmlformats-officedocument.wordprocessingml.document','wordname'); // try adding
  307. //$paramStr = 'paperId='.$data['paperId'].'&tpl_name="'.$data['paperName'].'"&tpl="'.$cfile->name.'"';
  308. $cfile = curl_file_create($data['path_tpl'],'application/vnd.openxmlformats-officedocument.wordprocessingml.document',$data['tpl_name']); // try adding
  309. $paramArray['tpl'] = $cfile;
  310. $paramArray['paperId'] = $data['paperId'];
  311. $paramArray['tpl_name'] = $data['tpl_name'];
  312. } else {
  313. // $paramStr = 'paperId='.$data['paperId'].'&tpl_name="'.$data['paperName'].'"&tpl="@'.$data['templetAFileName'].'"';
  314. //$data['file'] = array('callback_url'=>$word_api_url.'/wordapi/index/wid/'.$word_id,'file' => '@'.$postFilePath,'type'=>'batch');
  315. $paramArray['tpl'] = '@'.$data['path_tpl'];
  316. $paramArray['paperId'] = $data['paperId'];
  317. $paramArray['tpl_name'] = $data['tpl_name'];
  318. }
  319. $paramStr = $paramArray;
  320. } else {
  321. $paramStr = $data;
  322. }
  323. //debug($paramStr);die;
  324. // Basic认证
  325. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  326. curl_setopt($ch, CURLOPT_USERPWD, Yii::app()->params['api_verify_username'] .':'. Yii::app()->params['api_verify_password']);
  327. // 不输出头部
  328. $cookies = Yii::app()->request->getCookies();
  329. curl_setopt($ch, CURLOPT_HEADER, 0);
  330. curl_setopt($ch, CURLOPT_HTTPHEADER, array('MySigInfo:userid='.Yii::app()->session['coachInfo']['coach_id'].'&time='.$cookies['loginTime']->value.'&sig='.$cookies['loginSig']->value.'&session='.$cookies['appLoginSessionId']->value));
  331. // curl_exec 获取到的内容不直接输出, 而是返回
  332. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  333. // 请求重启路由器的地址 传参 进行重启
  334. curl_setopt($ch, CURLOPT_URL, $url);
  335. curl_setopt($ch, CURLOPT_USERAGENT,'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15');
  336. //curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  337. // curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');
  338. curl_setopt($ch, CURLOPT_POST, 1);
  339. curl_setopt($ch, CURLOPT_POSTFIELDS, $paramStr);
  340. if( !curl_errno($ch)){
  341. $result = curl_exec($ch);
  342. }
  343. // 释放资源
  344. curl_close($ch);
  345. }
  346. return $result;
  347. }
  348. // 检查是否组长不是无法操作
  349. function checkAuthority($type=1){
  350. if ($type == 1) {
  351. if(Yii::app()->session['coachInfo']['leader'] != 1) {
  352. Yii::app()->jump->error('您无权限操作!');
  353. }
  354. } else if ($type == 2) {
  355. if (isset(Yii::app()->session['session_semester_state'])){
  356. //Yii::app()->jump->error('非当前使用学期不能操作!');
  357. }
  358. }
  359. }
  360. function getToken(){
  361. $token='';
  362. if(Yii::app()->params['redis_on_off']){
  363. $cache_accessToken= Yii::app()->cache->getValue('wytk_cache_accessToken_key');
  364. $cache_accessTokenTime= Yii::app()->cache->getValue('wytk_cache_accessToken_time');
  365. if(!empty($cache_accessToken) && !empty($cache_accessTokenTime) && (time()-$cache_accessTokenTime<Yii::app()->params['redis_expires'])){
  366. $token=$cache_accessToken;
  367. }else{
  368. $token_odj= new wechataccesstoken(Yii::app()->params['APPID'],Yii::app()->params['secret']);
  369. $token_array= $token_odj->get();
  370. if($token_array){
  371. Yii::app()->cache->setValue('wytk_cache_accessToken_key',$token_array['access_token'],7000);
  372. Yii::app()->cache->setValue('wytk_cache_accessToken_time',time()+$token_array['expires_in'],7000);
  373. $token=$token_array['access_token'];
  374. }
  375. }
  376. }else{
  377. $token=0;//测试
  378. }
  379. return $token;
  380. }
  381. function send_weixin($openId,$name, $url = null){
  382. $inform_config = array(
  383. 'url'=> $url ? $url : Yii::app()->params['weixin_realm'],
  384. 'openId'=>$openId,
  385. 'name'=>$name
  386. );
  387. $temapi= new wechattemplateapi(getToken(),$inform_config,1);
  388. $temapi->send();//通知不成功,未做记录
  389. }