-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbobj.php
121 lines (112 loc) · 2.94 KB
/
dbobj.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
date_default_timezone_set ( "PRC" );
function connectDb() {
$host = 'mongo.duapp.com';
$port = '8908';
// $host = 'localhost';
// $port = '27017';
while ( 1 ) {
try {
/* 建立连接后,在进行集合操作前,需要先select使用的数据库,并进行auth */
$mongoClient = new MongoClient ( "mongodb://{$host}:{$port}",array("persist" => "x"));
return $mongoClient;
} catch ( Exception $e ) {
sleep ( 1 );
continue;
}
}
}
function connectDbTwo($username, $password, $dbname) {
// $host = 'mongo.duapp.com';
// $port = '8908';
$host = 'localhost';
$port = '27017';
while ( 1 ) {
try {
/* 建立连接后,在进行集合操作前,需要先select使用的数据库,并进行auth */
$mongoClient = new MongoClient ( "mongodb://{$host}:{$port}", array (
'username' => $username,
'password' => $password,
'db' => $dbname
) );
return $mongoClient;
} catch ( Exception $e ) {
sleep ( 1 );
continue;
}
}
}
function convent_from_array_to_xml_b($a) {
$res = '';
foreach ( $a as $key => $value ) {
$res .= "<$key>";
if (is_array ( $value )) {
$res .= convent_from_array_to_xml_b ( $value );
} else {
$res .= $value;
}
$res .= "</$key>";
}
return $res;
}
function convent_from_array_to_xml($a) {
$res = '<result>';
$res .= convent_from_array_to_xml_b ( $a );
$res .= '</result>';
return $res;
}
function makeGuid() {
if (function_exists ( 'com_create_guid' )) {
return com_create_guid ();
} else {
mt_srand ( ( double ) microtime () * 10000 ); // optional for php 4.2.0 and up.
$charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) );
$hyphen = chr ( 45 ); // "-"
$uuid = chr ( 123 ) . // "{"
substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 ) . chr ( 125 ); // "}"
return $uuid;
}
}
function writeLog($filename, $content) {
$t = time ();
$fname = $filename . '.log';
$fp = fopen ( $fname, 'a+' );
fwrite ( $fp, $content . ' ' . date ( 'Y-m-d h:i:s', time () ) . "\r\n" );
fclose ( $fp );
}
function arrcmp1($a, $b) {
return strcmp ( $a ["user_name"], $b ["user_name"] );
}
/*
* @length 邀请码的长度
* @num 邀请码的数量
*/
function yqinvcode($length, $num = 1) {
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$j = 0;
$code_arr = array ();
while ( $j < $num ) {
$random = '';
for($i = 0; $i < $length; $i ++) {
$random .= $chars [mt_rand ( 0, strlen ( $chars ) - 1 )];
}
$code_arr [$j] = $random;
$j ++;
}
return $code_arr;
}
function yqregcode($length, $num = 1) {
$chars = '0123456789';
$j = 0;
$code_arr = array ();
while ( $j < $num ) {
$random = '';
for($i = 0; $i < $length; $i ++) {
$random .= $chars [mt_rand ( 0, strlen ( $chars ) - 1 )];
}
$code_arr [$j] = $random;
$j ++;
}
return $code_arr;
}
?>