Update lua versions
[ryzomcore.git] / web / private_php / ams / autoload / sync.php
blob1160b91b5232464f2f38b640c7f903ec1ef523d5
1 <?php
2 /**
3 * handler for performing changes when shard is back online after being offline.
4 * the sync class is responsible for the syncdata function, which will synchronise the website with the shard
5 * (when the shard is offline, users can still change their password, email or even register)
6 * @author Daan Janssens, mentored by Matthew Lagoe
7 */
8 class Sync{
10 const OS_UNKNOWN = 1;
11 const OS_WIN = 2;
12 const OS_LINUX = 3;
13 const OS_OSX = 4;
15 /**
16 * performs the actions listed in the querycache.
17 * All entries in the querycache will be read and performed depending on their type.
18 * This is done because the shard could have been offline and we want changes made on the website (which is still online) to eventually hit the shard.
19 * These changes are: createPermissions, createUser, change_pass, change_mail
21 static public function syncdata ($display = false) {
23 if (function_exists('pcntl_fork')) {
24 $pid = pcntl_fork();
26 global $AMS_TMPDIR;
27 $pidfile = $AMS_TMPDIR.'/ams_cron_pid';
29 if(isset($pid) and function_exists('pcntl_fork') ) {
30 // We're the main process.
31 } else {
32 $pid = getmypid();
33 if(Sync::check_for_pid(@file_get_contents($pidfile))) {
34 $file = fopen($pidfile, 'w+');
35 if (!$file) {
36 echo $pidfile.' is not writeable.';
37 error_log($pidfile.' is not writeable.');
38 throw new SystemExit();
40 fwrite($file, $pid);
41 fclose($file);
43 try {
44 $dbl = new DBLayer("lib");
45 $statement = $dbl->executeWithoutParams("SELECT * FROM ams_querycache");
46 $rows = $statement->fetchAll();
47 foreach ($rows as $record) {
49 $db = new DBLayer($record['db']);
50 switch($record['type']) {
51 case 'createPermissions':
52 $decode = json_decode($record['query']);
53 $values = array('username' => $decode[0]);
54 //make connection with and put into shard db & delete from the lib
55 $sth=$db->selectWithParameter("UId", "user", $values, "Login= :username" );
56 $result = $sth->fetchAll();
57 /*foreach ($result as $UId) {
58 $ins_values = array('UId' => $UId['UId']);
59 $ins_values['ClientApplication'] = "r2";
60 $ins_values['AccessPrivilege'] = "OPEN";
61 $db->insert("permission", $ins_values);
62 $ins_values['ClientApplication'] = 'ryzom_open';
63 $db->insert("permission",$ins_values);
64 }*/ // FIXME: GARBAGE
65 break;
66 case 'change_pass':
67 $decode = json_decode($record['query']);
68 $values = array('Password' => $decode[1]);
69 //make connection with and put into shard db & delete from the lib
70 $db->update("user", $values, "Login = '$decode[0]'");
71 break;
72 case 'change_mail':
73 $decode = json_decode($record['query']);
74 $values = array('Email' => $decode[1]);
75 //make connection with and put into shard db & delete from the lib
76 $db->update("user", $values, "Login = '$decode[0]'");
77 break;
78 case 'createUser':
79 $decode = json_decode($record['query']);
80 $values = array('Login' => $decode[0], 'Password' => $decode[1], 'Email' => $decode[2] );
81 //make connection with and put into shard db & delete from the lib
82 $db->insert("user", $values);
83 break;
85 $dbl->delete("ams_querycache", array('SID' => $record['SID']), "SID=:SID");
87 if ($display == true) {
88 print('Syncing completed');
91 catch (PDOException $e) {
92 if ($display == true) {
93 print('Something went wrong! The shard is probably still offline!');
94 print_r($e);
97 unlink($pidfile);
103 public static function check_for_pid($pid){
105 $OS = Sync::getOS();
107 if ($OS == 2) {
108 $processes = explode( "\n", shell_exec( "tasklist.exe" ));
109 foreach( $processes as $key => $value )
111 if( empty($value) != '1' && strpos( "Image Name", $value ) === 0
112 || empty($value) != '1' && strpos( "===", $value ) === 0 )
113 continue;
114 $matches = false;
115 preg_match( "/(.*?)\s+(\d+).*$/", $value, $matches );
116 if (isset($matches[ 2 ]) && $pid = $matches[ 2 ]) {
117 return true;
120 } else {
121 return file_exists( "/proc/".$pid );
124 static public function getOS() {
125 switch (true) {
126 case stristr(PHP_OS, 'DAR'): return self::OS_OSX;
127 case stristr(PHP_OS, 'WIN'): return self::OS_WIN;
128 case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX;
129 default : return self::OS_UNKNOWN;