/Examples/Rbac/Common/common.php
PHP | 286 lines | 225 code | 17 blank | 44 comment | 32 complexity | 9712a0bc0c2681e85eb3bb49127aa52c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP
- // +----------------------------------------------------------------------
- // | Copyright (c) 2007 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id: common.php 2601 2012-01-15 04:59:14Z liu21st $
-
- //????
- function toDate($time, $format = 'Y-m-d H:i:s') {
- if (empty ( $time )) {
- return '';
- }
- $format = str_replace ( '#', ':', $format );
- return date ($format, $time );
- }
-
- // ????
- function cmssavecache($name = '', $fields = '') {
- $Model = D ( $name );
- $list = $Model->select ();
- $data = array ();
- foreach ( $list as $key => $val ) {
- if (empty ( $fields )) {
- $data [$val [$Model->getPk ()]] = $val;
- } else {
- // ???????
- if (is_string ( $fields )) {
- $fields = explode ( ',', $fields );
- }
- if (count ( $fields ) == 1) {
- $data [$val [$Model->getPk ()]] = $val [$fields [0]];
- } else {
- foreach ( $fields as $field ) {
- $data [$val [$Model->getPk ()]] [] = $val [$field];
- }
- }
- }
- }
- $savefile = cmsgetcache ( $name );
- // ?????????
- $content = "<?php\nreturn " . var_export ( array_change_key_case ( $data, CASE_UPPER ), true ) . ";\n?>";
- file_put_contents ( $savefile, $content );
- }
-
- function cmsgetcache($name = '') {
- return DATA_PATH . '~' . strtolower ( $name ) . '.php';
- }
- function getStatus($status, $imageShow = true) {
- switch ($status) {
- case 0 :
- $showText = '??';
- $showImg = '<IMG SRC="__PUBLIC__/Images/locked.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
- break;
- case 2 :
- $showText = '??';
- $showImg = '<IMG SRC="__PUBLIC__/Images/prected.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
- break;
- case - 1 :
- $showText = '??';
- $showImg = '<IMG SRC="__PUBLIC__/Images/del.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
- break;
- case 1 :
- default :
- $showText = '??';
- $showImg = '<IMG SRC="__PUBLIC__/Images/ok.gif" WIDTH="20" HEIGHT="20" BORDER="0" ALT="??">';
-
- }
- return ($imageShow === true) ? $showImg : $showText;
-
- }
- function getDefaultStyle($style) {
- if (empty ( $style )) {
- return 'blue';
- } else {
- return $style;
- }
-
- }
- function IP($ip = '', $file = 'UTFWry.dat') {
- $_ip = array ();
- if (isset ( $_ip [$ip] )) {
- return $_ip [$ip];
- } else {
- import ( "ORG.Net.IpLocation" );
- $iplocation = new IpLocation ( $file );
- $location = $iplocation->getlocation ( $ip );
- $_ip [$ip] = $location ['country'] . $location ['area'];
- }
- return $_ip [$ip];
- }
-
- function getNodeName($id) {
- if (Session::is_set ( 'nodeNameList' )) {
- $name = Session::get ( 'nodeNameList' );
- return $name [$id];
- }
- $Group = D ( "Node" );
- $list = $Group->getField ( 'id,name' );
- $name = $list [$id];
- Session::set ( 'nodeNameList', $list );
- return $name;
- }
-
- function get_pawn($pawn) {
- if ($pawn == 0)
- return "<span style='color:green'>??</span>";
- else
- return "<span style='color:red'>?</span>";
- }
- function get_patent($patent) {
- if ($patent == 0)
- return "<span style='color:green'>??</span>";
- else
- return "<span style='color:red'>?</span>";
- }
-
-
- function getNodeGroupName($id) {
- if (empty ( $id )) {
- return '???';
- }
- if (isset ( $_SESSION ['nodeGroupList'] )) {
- return $_SESSION ['nodeGroupList'] [$id];
- }
- $Group = D ( "Group" );
- $list = $Group->getField ( 'id,title' );
- $_SESSION ['nodeGroupList'] = $list;
- $name = $list [$id];
- return $name;
- }
-
- function getCardStatus($status) {
- switch ($status) {
- case 0 :
- $show = '???';
- break;
- case 1 :
- $show = '???';
- break;
- case 2 :
- $show = '???';
- break;
- case 3 :
- $show = '???';
- break;
- case 4 :
- $show = '???';
- break;
- }
- return $show;
-
- }
-
- function showStatus($status, $id) {
- switch ($status) {
- case 0 :
- $info = '<a href="javascript:resume(' . $id . ')">??</a>';
- break;
- case 2 :
- $info = '<a href="javascript:pass(' . $id . ')">??</a>';
- break;
- case 1 :
- $info = '<a href="javascript:forbid(' . $id . ')">??</a>';
- break;
- case - 1 :
- $info = '<a href="javascript:recycle(' . $id . ')">??</a>';
- break;
- }
- return $info;
- }
-
- /**
- +----------------------------------------------------------
- * ??????? ???4???
- +----------------------------------------------------------
- * @param string $fmode ???
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function build_verify($length = 4, $mode = 1) {
- return rand_string ( $length, $mode );
- }
-
-
- function getGroupName($id) {
- if ($id == 0) {
- return '????';
- }
- if ($list = F ( 'groupName' )) {
- return $list [$id];
- }
- $dao = D ( "Role" );
- $list = $dao->select( array ('field' => 'id,name' ) );
- foreach ( $list as $vo ) {
- $nameList [$vo ['id']] = $vo ['name'];
- }
- $name = $nameList [$id];
- F ( 'groupName', $nameList );
- return $name;
- }
- function sort_by($array, $keyname = null, $sortby = 'asc') {
- $myarray = $inarray = array ();
- # First store the keyvalues in a seperate array
- foreach ( $array as $i => $befree ) {
- $myarray [$i] = $array [$i] [$keyname];
- }
- # Sort the new array by
- switch ($sortby) {
- case 'asc' :
- # Sort an array and maintain index association...
- asort ( $myarray );
- break;
- case 'desc' :
- case 'arsort' :
- # Sort an array in reverse order and maintain index association
- arsort ( $myarray );
- break;
- case 'natcasesor' :
- # Sort an array using a case insensitive "natural order" algorithm
- natcasesort ( $myarray );
- break;
- }
- # Rebuild the old array
- foreach ( $myarray as $key => $befree ) {
- $inarray [] = $array [$key];
- }
- return $inarray;
- }
-
- /**
- +----------------------------------------------------------
- * ????????????????
- * ????6? ??????? ????
- +----------------------------------------------------------
- * @param string $len ??
- * @param string $type ????
- * 0 ?? 1 ?? ?? ??
- * @param string $addChars ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- function rand_string($len = 6, $type = '', $addChars = '') {
- $str = '';
- switch ($type) {
- case 0 :
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars;
- break;
- case 1 :
- $chars = str_repeat ( '0123456789', 3 );
- break;
- case 2 :
- $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars;
- break;
- case 3 :
- $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars;
- break;
- default :
- // ????????????oOLl???01???????addChars??
- $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars;
- break;
- }
- if ($len > 10) { //?????????????
- $chars = $type == 1 ? str_repeat ( $chars, $len ) : str_repeat ( $chars, 5 );
- }
- if ($type != 4) {
- $chars = str_shuffle ( $chars );
- $str = substr ( $chars, 0, $len );
- } else {
- // ?????
- for($i = 0; $i < $len; $i ++) {
- $str .= msubstr ( $chars, floor ( mt_rand ( 0, mb_strlen ( $chars, 'utf-8' ) - 1 ) ), 1 );
- }
- }
- return $str;
- }
- function pwdHash($password, $type = 'md5') {
- return hash ( $type, $password );
- }
- ?>