PageRenderTime 24ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Examples/Rbac/Common/common.php

http://thinkphp.googlecode.com/
PHP | 286 lines | 225 code | 17 blank | 44 comment | 32 complexity | 9712a0bc0c2681e85eb3bb49127aa52c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: common.php 2601 2012-01-15 04:59:14Z liu21st $
  12. //????
  13. function toDate($time, $format = 'Y-m-d H:i:s') {
  14. if (empty ( $time )) {
  15. return '';
  16. }
  17. $format = str_replace ( '#', ':', $format );
  18. return date ($format, $time );
  19. }
  20. // ????
  21. function cmssavecache($name = '', $fields = '') {
  22. $Model = D ( $name );
  23. $list = $Model->select ();
  24. $data = array ();
  25. foreach ( $list as $key => $val ) {
  26. if (empty ( $fields )) {
  27. $data [$val [$Model->getPk ()]] = $val;
  28. } else {
  29. // ???????
  30. if (is_string ( $fields )) {
  31. $fields = explode ( ',', $fields );
  32. }
  33. if (count ( $fields ) == 1) {
  34. $data [$val [$Model->getPk ()]] = $val [$fields [0]];
  35. } else {
  36. foreach ( $fields as $field ) {
  37. $data [$val [$Model->getPk ()]] [] = $val [$field];
  38. }
  39. }
  40. }
  41. }
  42. $savefile = cmsgetcache ( $name );
  43. // ?????????
  44. $content = "<?php\nreturn " . var_export ( array_change_key_case ( $data, CASE_UPPER ), true ) . ";\n?>";
  45. file_put_contents ( $savefile, $content );
  46. }
  47. function cmsgetcache($name = '') {
  48. return DATA_PATH . '~' . strtolower ( $name ) . '.php';
  49. }
  50. function getStatus($status, $imageShow = true) {
  51. switch ($status) {
  52. case 0 :
  53. $showText = '??';
  54. $showImg = '<IMG SRC="__PUBLIC__/Images/locked.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
  55. break;
  56. case 2 :
  57. $showText = '??';
  58. $showImg = '<IMG SRC="__PUBLIC__/Images/prected.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
  59. break;
  60. case - 1 :
  61. $showText = '??';
  62. $showImg = '<IMG SRC="__PUBLIC__/Images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
  63. break;
  64. case 1 :
  65. default :
  66. $showText = '??';
  67. $showImg = '<IMG SRC="__PUBLIC__/Images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
  68. }
  69. return ($imageShow === true) ? $showImg : $showText;
  70. }
  71. function getDefaultStyle($style) {
  72. if (empty ( $style )) {
  73. return 'blue';
  74. } else {
  75. return $style;
  76. }
  77. }
  78. function IP($ip = '', $file = 'UTFWry.dat') {
  79. $_ip = array ();
  80. if (isset ( $_ip [$ip] )) {
  81. return $_ip [$ip];
  82. } else {
  83. import ( "ORG.Net.IpLocation" );
  84. $iplocation = new IpLocation ( $file );
  85. $location = $iplocation->getlocation ( $ip );
  86. $_ip [$ip] = $location ['country'] . $location ['area'];
  87. }
  88. return $_ip [$ip];
  89. }
  90. function getNodeName($id) {
  91. if (Session::is_set ( 'nodeNameList' )) {
  92. $name = Session::get ( 'nodeNameList' );
  93. return $name [$id];
  94. }
  95. $Group = D ( "Node" );
  96. $list = $Group->getField ( 'id,name' );
  97. $name = $list [$id];
  98. Session::set ( 'nodeNameList', $list );
  99. return $name;
  100. }
  101. function get_pawn($pawn) {
  102. if ($pawn == 0)
  103. return "<span style='color:green'>??</span>";
  104. else
  105. return "<span style='color:red'>?</span>";
  106. }
  107. function get_patent($patent) {
  108. if ($patent == 0)
  109. return "<span style='color:green'>??</span>";
  110. else
  111. return "<span style='color:red'>?</span>";
  112. }
  113. function getNodeGroupName($id) {
  114. if (empty ( $id )) {
  115. return '???';
  116. }
  117. if (isset ( $_SESSION ['nodeGroupList'] )) {
  118. return $_SESSION ['nodeGroupList'] [$id];
  119. }
  120. $Group = D ( "Group" );
  121. $list = $Group->getField ( 'id,title' );
  122. $_SESSION ['nodeGroupList'] = $list;
  123. $name = $list [$id];
  124. return $name;
  125. }
  126. function getCardStatus($status) {
  127. switch ($status) {
  128. case 0 :
  129. $show = '???';
  130. break;
  131. case 1 :
  132. $show = '???';
  133. break;
  134. case 2 :
  135. $show = '???';
  136. break;
  137. case 3 :
  138. $show = '???';
  139. break;
  140. case 4 :
  141. $show = '???';
  142. break;
  143. }
  144. return $show;
  145. }
  146. function showStatus($status, $id) {
  147. switch ($status) {
  148. case 0 :
  149. $info = '<a href="javascript:resume(' . $id . ')">??</a>';
  150. break;
  151. case 2 :
  152. $info = '<a href="javascript:pass(' . $id . ')">??</a>';
  153. break;
  154. case 1 :
  155. $info = '<a href="javascript:forbid(' . $id . ')">??</a>';
  156. break;
  157. case - 1 :
  158. $info = '<a href="javascript:recycle(' . $id . ')">??</a>';
  159. break;
  160. }
  161. return $info;
  162. }
  163. /**
  164. +----------------------------------------------------------
  165. * ??????? ???4???
  166. +----------------------------------------------------------
  167. * @param string $fmode ???
  168. +----------------------------------------------------------
  169. * @return string
  170. +----------------------------------------------------------
  171. */
  172. function build_verify($length = 4, $mode = 1) {
  173. return rand_string ( $length, $mode );
  174. }
  175. function getGroupName($id) {
  176. if ($id == 0) {
  177. return '????';
  178. }
  179. if ($list = F ( 'groupName' )) {
  180. return $list [$id];
  181. }
  182. $dao = D ( "Role" );
  183. $list = $dao->select( array ('field' => 'id,name' ) );
  184. foreach ( $list as $vo ) {
  185. $nameList [$vo ['id']] = $vo ['name'];
  186. }
  187. $name = $nameList [$id];
  188. F ( 'groupName', $nameList );
  189. return $name;
  190. }
  191. function sort_by($array, $keyname = null, $sortby = 'asc') {
  192. $myarray = $inarray = array ();
  193. # First store the keyvalues in a seperate array
  194. foreach ( $array as $i => $befree ) {
  195. $myarray [$i] = $array [$i] [$keyname];
  196. }
  197. # Sort the new array by
  198. switch ($sortby) {
  199. case 'asc' :
  200. # Sort an array and maintain index association...
  201. asort ( $myarray );
  202. break;
  203. case 'desc' :
  204. case 'arsort' :
  205. # Sort an array in reverse order and maintain index association
  206. arsort ( $myarray );
  207. break;
  208. case 'natcasesor' :
  209. # Sort an array using a case insensitive "natural order" algorithm
  210. natcasesort ( $myarray );
  211. break;
  212. }
  213. # Rebuild the old array
  214. foreach ( $myarray as $key => $befree ) {
  215. $inarray [] = $array [$key];
  216. }
  217. return $inarray;
  218. }
  219. /**
  220. +----------------------------------------------------------
  221. * ????????????????
  222. * ????6? ??????? ????
  223. +----------------------------------------------------------
  224. * @param string $len ??
  225. * @param string $type ????
  226. * 0 ?? 1 ?? ?? ??
  227. * @param string $addChars ????
  228. +----------------------------------------------------------
  229. * @return string
  230. +----------------------------------------------------------
  231. */
  232. function rand_string($len = 6, $type = '', $addChars = '') {
  233. $str = '';
  234. switch ($type) {
  235. case 0 :
  236. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
  237. break;
  238. case 1 :
  239. $chars = str_repeat ( '0123456789', 3 );
  240. break;
  241. case 2 :
  242. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
  243. break;
  244. case 3 :
  245. $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
  246. break;
  247. default :
  248. // ????????????oOLl???01???????addChars??
  249. $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
  250. break;
  251. }
  252. if ($len > 10) { //?????????????
  253. $chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 );
  254. }
  255. if ($type != 4) {
  256. $chars = str_shuffle ( $chars );
  257. $str = substr ( $chars, 0, $len );
  258. } else {
  259. // ?????
  260. for($i = 0; $i < $len; $i ++) {
  261. $str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 );
  262. }
  263. }
  264. return $str;
  265. }
  266. function pwdHash($password, $type = 'md5') {
  267. return hash ( $type, $password );
  268. }
  269. ?>