Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / web / public_php / api / server / user.php
blob713e6c399a79bbdcd9590005934a34a293cad413
1 <?php
2 /* Copyright (C) 2009 Winch Gate Property Limited
4 * This file is part of ryzom_api.
5 * ryzom_api is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * ryzom_api is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with ryzom_api. If not, see <http://www.gnu.org/licenses/>.
19 include_once(RYAPI_PATH.'server/guilds.php');
22 function ryzom_get_user_id($cid, $name, $creation_date) {
23 $name = strtolower($name);
25 $db = ryDB::getInstance('webig');
27 $dev_shard = ((isset($_SESSION['dev_shard']) && $_SESSION['dev_shard']) || (isset($_GET['shardid']) && $_GET['shardid'] == RYAPI_DEV_SHARDID))?1:0;
28 if ($dev_shard)
29 $_SESSION['dev_shard'] = 1;
30 $charsWithSameName = $db->query('players', array('name' => $name, 'deleted' => 0, 'dev_shard' => $dev_shard));
32 foreach ($charsWithSameName as $charWithSameName) {
33 // Another char with same name => delete it
34 if (intval($cid) != intval($charWithSameName['cid'])) {
35 $db->update('players', array('deleted' => 1), array('id' => $charWithSameName['id']));
39 $charProps = $db->querySingle('players', array('cid' => intval($cid), 'deleted' => 0, 'dev_shard' => $dev_shard));
40 // new char => create record
41 if (!$charProps) {
42 $charProps = array('name' => $name, 'cid' => $cid, 'creation_date' => $creation_date, 'deleted' => 0, 'dev_shard' => $dev_shard);
43 $charProps['id'] = $db->insert('players', $charProps);
44 if (!$charProps['id'])
45 die('ryDb New Char Error');
46 } else {
47 // char deleted and recreated => change to deleted
48 if ($charProps['creation_date'] != $creation_date) {
49 if (!$db->update('players', array('deleted' => 1), array('id' => $charProps['id'])))
50 die('ryDb Delete char Error: '.$db->getErrors());
51 $charProps = array('name' => $name, 'cid' => $cid, 'creation_date' => $creation_date, 'deleted' => 0, 'dev_shard' => $dev_shard);
52 if (!$charProps['id'] = $db->insert('players', $charProps))
53 die('ryDb New Char in Slot Error');
54 } else {
55 // char renamed => update record
56 if ($charProps['name'] != $name)
57 if (!$db->update('players', array('name' => $name), array('id' => $charProps['id'])))
58 die('ryDb Rename Char Error');
61 return $charProps['id'];
64 function ryzom_get_user_gender($id) {
65 $db = ryDB::getInstance('webig');
66 $player = $db->querySingle('players', array('id' => $id));
67 if ($player) {
68 if ($player['gender'])
69 return intval($player['gender']);
70 $cid = $player['cid'];
71 $xml = @simplexml_load_file(RYAPI_PATH.'data/cache/players/public/'.substr($cid, strlen($cid)-1).'/'.$cid.'.xml');
72 if ($xml !== false) {
73 $gender = (string)$xml->public->_gender;
74 $db->update('players', array('gender' => intval($gender)+1), array('id' => $id));
75 return $gender+1;
78 return false;
81 function ryzom_user_get_info($cid, $webprivs=false, $player_stats=false) {
82 // User information
83 global $_RYZOM_API_CONFIG;
85 if (isset($_SESSION['dev_shard']) && $_SESSION['dev_shard'])
86 $db = new ServerDatabase(RYAPI_NELDB_HOST, RYAPI_NELDB_LOGIN, RYAPI_NELDB_PASS, RYAPI_NELDB_RING_DEV);
87 else
88 $db = new ServerDatabase(RYAPI_NELDB_HOST, RYAPI_NELDB_LOGIN, RYAPI_NELDB_PASS, RYAPI_NELDB_RING);
89 $sql = "SELECT char_name, race, civilisation, cult, guild_id, creation_date, last_played_date FROM characters WHERE char_id = $cid";
90 $result = $db->query($sql) or die('Could not query on ryzom_user_get_info');
91 $found = $db->num_rows($result) >= 1;
92 if (!$found)
93 return array('char_name' => _t('guest'), 'cid' => $cid, 'ERROR' => 'unknown_user', 'groups' => array('GUEST'));
94 $row = $db->fetch_assoc($result);
95 $db->free_result($result);
96 if ($row) {
97 $row['race'] = substr($row['race'], 2);
98 $row['cult'] = substr($row['cult'], 2);
99 $row['civ'] = substr($row['civilisation'], 2);
100 if ($row['guild_id'] != '0') {
101 $xml = @simplexml_load_file(ryzom_guild($row['guild_id'], false));
102 // $xml = false;
103 if ($xml !== false) {
104 $row['guild_icon'] = (string)$xml->icon;
105 $row['guild_name'] = (string)$xml->name;
106 $result = $xml->xpath("/guild/members/member[cid=$cid]");
107 while(list( , $item) = each($result))
108 $row['grade'] = (string)$item->grade;
109 } else {
110 $row['guild_name'] = 'UNKNOWN_GUILD_'.$row['guild_id']; // Unknow name (normal in yubo shard)
115 $uid = intval($cid / 16);
116 $db = new ServerDatabase(RYAPI_NELDB_HOST, RYAPI_NELDB_LOGIN, RYAPI_NELDB_PASS, RYAPI_NELDB_NEL);
117 $sql = "SELECT Privilege FROM user WHERE UId = $uid";
118 $result = $db->query($sql) or die("Could not query.");
119 $priv_row = $db->fetch_row($result, MYSQLI_NUM);
120 $priv = $priv_row[0];
121 $db->free_result($result);
122 $groups = array();
124 $row['uid'] = $uid;
125 $row['cid'] = $cid;
126 $row['slot'] = $cid%16;
128 if (strpos($priv, ':DEV:') !== false) {
129 $groups[] = 'DEV';
130 $groups[] = 'SGM';
131 $groups[] = 'GM';
132 $groups[] = 'EM';
133 $groups[] = 'EG';
134 $groups[] = 'VG';
135 $groups[] = 'G';
138 if (strpos($priv, ':SGM:') !== false) {
139 $groups[] = 'SGM';
140 $groups[] = 'GM';
141 $groups[] = 'VG';
142 $groups[] = 'G';
145 if (strpos($priv, ':GM:') !== false) {
146 $groups[] = 'GM';
147 $groups[] = 'VG';
148 $groups[] = 'G';
151 if (strpos($priv, ':VG:') !== false) {
152 $groups[] = 'VG';
153 $groups[] = 'G';
156 if (strpos($priv, ':G:') !== false) {
157 $groups[] = 'G';
160 if (strpos($priv, ':SEM:') !== false) {
161 $groups[] = 'SEM';
162 $groups[] = 'EM';
163 $groups[] = 'EG';
166 if (strpos($priv, ':EM:') !== false) {
167 $groups[] = 'EM';
168 $groups[] = 'EG';
171 if (strpos($priv, ':EG:') !== false) {
172 $groups[] = 'EG';
175 $groups[] = 'PLAYER';
176 if (isset($_SESSION['dev_shard']) && $_SESSION['dev_shard'])
177 $groups[] = 'DEV_SHARD';
179 if ($webprivs) {
180 $db = new ServerDatabase(RYAPI_WEBDB_HOST, RYAPI_WEBDB_LOGIN, RYAPI_WEBDB_PASS, 'webig');
181 $sql = 'SELECT web_privs FROM accounts WHERE uid = '.intval($cid/16);
182 $result = $db->query($sql) or die("Could not query.".$db->get_error());
183 if ($result->num_rows == 0)
184 $db->query('INSERT INTO accounts (`uid`, `web_privs`) VALUES ('.intval($cid/16).', \'\')') or die("Could not query.".$db->get_error());
185 $priv_row = $db->fetch_row($result, MYSQLI_NUM);
186 $privs = $priv_row[0];
187 $db->free_result($result);
188 $groups = array_merge($groups, explode(':', $privs));
191 if ($player_stats) {
192 include_once(RYAPI_PATH.'server/player_stats.php');
193 $row['fames'] = ryzom_player_fames_array($cid);
195 $row['groups'] = $groups;
196 return $row;