1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package fwolflib |
4
|
|
|
* @subpackage class.test |
5
|
|
|
* @copyright Copyright 2010, Fwolf |
6
|
|
|
* @author Fwolf <[email protected]> |
7
|
|
|
* @since 2010-05-25 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
// Usage example |
11
|
|
|
|
12
|
|
|
// Include |
13
|
|
View Code Duplication |
if (0 <= version_compare(phpversion(), '5.3.0')) { |
|
|
|
|
14
|
|
|
require_once(__DIR__ . '/sync-db-curl.php'); |
15
|
|
|
} else { |
16
|
|
|
require_once(dirname(__FILE__) . '/sync-db-curl.php'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
// Global/default config |
21
|
|
|
$ar_default = array( |
22
|
|
|
'url' => '', |
23
|
|
|
'db_client' => array( |
24
|
|
|
'type' => 'mysql', |
25
|
|
|
'host' => 'localhost', |
26
|
|
|
'user' => 'test', |
27
|
|
|
'pass' => '', |
28
|
|
|
'name' => 't-sync-db-curl1', |
29
|
|
|
// 'name' => 't-2008-zbb', |
30
|
|
|
'lang' => 'utf-8', |
31
|
|
|
), |
32
|
|
|
'db_server' => array( |
33
|
|
|
'type' => 'mysql', |
34
|
|
|
'host' => 'localhost', |
35
|
|
|
'user' => 'test', |
36
|
|
|
'pass' => '', |
37
|
|
|
'name' => 't-sync-db-curl2', |
38
|
|
|
'lang' => 'utf-8', |
39
|
|
|
), |
40
|
|
|
'pull' => '', |
41
|
|
|
'push' => '', |
42
|
|
|
); |
43
|
|
|
$s_crypt_key = 'blahblahblah'; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
// Per server config, if any part is missing, use default. |
47
|
|
|
// Key is name of server, will be recorded in db log tbl. |
48
|
|
|
$ar_server['test server'] = array( |
49
|
|
|
'url' => 'http://local.fwolf.com/dev/fwolflib/class/sync-db-curl.test.php', |
50
|
|
|
); |
51
|
|
|
$ar_server['test server']['db_server'] = array( |
52
|
|
|
'type' => 'mysql', |
53
|
|
|
'host' => 'localhost', |
54
|
|
|
'user' => 'test', |
55
|
|
|
'pass' => '', |
56
|
|
|
'name' => 't-2008-zbb', |
57
|
|
|
'lang' => 'utf-8', |
58
|
|
|
); |
59
|
|
|
$ar_server['test server']['pull'] = 'ent*, -*cert_hbb_?? |
60
|
|
|
-*pm, +re????, -v*'; |
61
|
|
|
$ar_server['test server2'] = array( |
62
|
|
|
'url' => 'http://local.fwolf.com/dev/fwolflib/class/sync-db-curl.test.php', |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
if (empty($_POST)) { |
67
|
|
|
// Act as client |
68
|
|
|
|
69
|
|
|
// Avoid duplicate run at client side. |
70
|
|
|
$f_lock = sys_get_temp_dir() . '/sync-db-curl.test.lock'; |
71
|
|
|
if (file_exists($f_lock)) { |
72
|
|
|
// Already running |
73
|
|
|
Ecl('Previous run not end or clean.'); |
|
|
|
|
74
|
|
|
Ecl('Lock file: ' . $f_lock); |
|
|
|
|
75
|
|
|
die(); |
76
|
|
|
} |
77
|
|
|
file_put_contents($f_lock, ''); |
78
|
|
|
|
79
|
|
|
$ar_cfg = array( |
80
|
|
|
'crypt_key' => $s_crypt_key, |
81
|
|
|
'default' => $ar_default, |
82
|
|
|
'server' => $ar_server, |
83
|
|
|
); |
84
|
|
|
$o_sdc = new SyncDbCurl($ar_cfg); |
85
|
|
|
echo $o_sdc->LogGet(3); |
86
|
|
|
|
87
|
|
|
unlink($f_lock); |
88
|
|
|
} else { |
89
|
|
|
// Act as server |
90
|
|
|
// Server need dup run, lock un-needed. |
91
|
|
|
$ar_cfg = array( |
92
|
|
|
'crypt_key' => $s_crypt_key, |
93
|
|
|
'default' => $ar_default, |
94
|
|
|
); |
95
|
|
|
$o_sdc = new SyncDbCurl($ar_cfg); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
?> |
|
|
|
|
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.