1 | <?php |
---|
2 | require_once('class.corp.php'); |
---|
3 | require_once('class.item.php'); |
---|
4 | require_once('class.thumb.php'); |
---|
5 | |
---|
6 | //! Creates a new Pilot or fetches an existing one from the database. |
---|
7 | class Pilot |
---|
8 | { |
---|
9 | //! Create a new Pilot object from the given $id. |
---|
10 | |
---|
11 | /*! |
---|
12 | * \param $id The pilot ID. |
---|
13 | * \param $externalIDFlag whether the id is external or internal |
---|
14 | */ |
---|
15 | function Pilot($id = 0, $externalIDFlag = false) |
---|
16 | { |
---|
17 | if($externalIDFlag) $this->externalid_=intval($id); |
---|
18 | else $this->id_ = intval($id); |
---|
19 | } |
---|
20 | //! Return the alliance ID. |
---|
21 | function getID() |
---|
22 | { |
---|
23 | if($this->id_) return $this->id_; |
---|
24 | elseif($this->externalid_) |
---|
25 | { |
---|
26 | $this->execQuery(); |
---|
27 | return $this->id_; |
---|
28 | } |
---|
29 | else return 0; |
---|
30 | } |
---|
31 | //! Return the pilot's CCP ID. |
---|
32 | function getExternalID() |
---|
33 | { |
---|
34 | if($this->externalid_) return $this->externalid_; |
---|
35 | elseif($this->id_) |
---|
36 | { |
---|
37 | $this->execQuery(); |
---|
38 | if($this->externalid_) return $this->externalid_; |
---|
39 | require_once("class.api.php"); |
---|
40 | $api = new Api(); |
---|
41 | $id = $api->getCharId($this->getName()); |
---|
42 | if ($id > 0) $this->setCharacterID($id); |
---|
43 | return $this->externalid_; |
---|
44 | } |
---|
45 | else return 0; |
---|
46 | } |
---|
47 | //! Return the pilot name. |
---|
48 | function getName() |
---|
49 | { |
---|
50 | if(!$this->name_) $this->execQuery(); |
---|
51 | $pos = strpos($this->name_, "#"); |
---|
52 | if ($pos === false) |
---|
53 | { |
---|
54 | return $this->name_; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | $name = explode("#", $this->name_); |
---|
59 | $item = new Item($name[2]); |
---|
60 | return $item->getName(); |
---|
61 | } |
---|
62 | } |
---|
63 | //! Return the URL for the pilot's portrait. |
---|
64 | |
---|
65 | /*! |
---|
66 | * \param $size The desired portrait size. |
---|
67 | * \return URL for a portrait. |
---|
68 | */ |
---|
69 | function getPortraitURL($size = 64) |
---|
70 | { |
---|
71 | if(!$this->externalid_) $this->execQuery(); |
---|
72 | if (!$this->externalid_) |
---|
73 | { |
---|
74 | return '?a=thumb&id='.$this->id_.'&size='.$size.'&int=1'; |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | if( file_exists('cache/portraits/'.$this->externalid_.'_'.$size.'.jpg')) |
---|
79 | return 'cache/portraits/'.$this->externalid_.'_'.$size.'.jpg'; |
---|
80 | else return '?a=thumb&id='.$this->externalid_.'&size='.$size; |
---|
81 | } |
---|
82 | } |
---|
83 | //! Fetch the pilot details from the database using the id given on construction. |
---|
84 | function execQuery() |
---|
85 | { |
---|
86 | if (!$this->qry_) |
---|
87 | { |
---|
88 | if(!$this->externalid_ && !$this->id_) |
---|
89 | { |
---|
90 | $this->valid_ = false; |
---|
91 | return; |
---|
92 | } |
---|
93 | $this->qry_ = new DBQuery(); |
---|
94 | $this->sql_ = 'select * from kb3_pilots plt, kb3_corps crp, kb3_alliances ali |
---|
95 | where crp.crp_id = plt.plt_crp_id |
---|
96 | and ali.all_id = crp.crp_all_id '; |
---|
97 | if($this->externalid_) $this->sql_ .= 'and plt.plt_externalid = '.$this->externalid_; |
---|
98 | else $this->sql_ .= 'and plt.plt_id = '.$this->id_; |
---|
99 | $this->qry_->execute($this->sql_) or die($this->qry_->getErrorMsg()); |
---|
100 | //$this->row_ = $this->qry_->getRow(); |
---|
101 | $row = $this->qry_->getRow(); |
---|
102 | if (!$row) |
---|
103 | $this->valid_ = false; |
---|
104 | else |
---|
105 | { |
---|
106 | $this->valid_ = true; |
---|
107 | $this->id_ = $row['plt_id']; |
---|
108 | $this->name_ = $row['plt_name']; |
---|
109 | $this->corp_ = $row['plt_crp_id']; |
---|
110 | $this->externalid_ = intval($row['plt_externalid']); |
---|
111 | |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | //! Return the corporation this pilot is a member of. |
---|
116 | |
---|
117 | /*! |
---|
118 | * \return Corporation object |
---|
119 | */ |
---|
120 | function getCorp() |
---|
121 | { |
---|
122 | if(!$this->corp_) $this->execQuery(); |
---|
123 | return new Corporation($this->corp_); |
---|
124 | } |
---|
125 | //! Check if the id given on construction is valid. |
---|
126 | |
---|
127 | /*! |
---|
128 | * \return boolean - true for exists. |
---|
129 | */ |
---|
130 | function exists() |
---|
131 | { |
---|
132 | $this->execQuery(); |
---|
133 | return $this->valid_; |
---|
134 | } |
---|
135 | //! Add a new pilot to the database or update the details of an existing one. |
---|
136 | |
---|
137 | /*! |
---|
138 | * \param $name Pilot name |
---|
139 | * \param $corp Corporation object for this pilot's corporation |
---|
140 | * \param $timestamp time this pilot's corp was updated |
---|
141 | * \param $externalID CCP external id |
---|
142 | */ |
---|
143 | function add($name, $corp, $timestamp, $externalID = 0) |
---|
144 | { |
---|
145 | $qry = new DBQuery(); |
---|
146 | $qry->execute("select * |
---|
147 | from kb3_pilots |
---|
148 | where plt_name = '".slashfix($name)."'"); |
---|
149 | |
---|
150 | if ($qry->recordCount() == 0) |
---|
151 | { |
---|
152 | $externalID = intval($externalID); |
---|
153 | // If no external id is given then look it up. |
---|
154 | if(!$externalID) |
---|
155 | { |
---|
156 | $pilotname = str_replace(" ", "%20", $name ); |
---|
157 | require_once("common/includes/class.eveapi.php"); |
---|
158 | $myID = new API_NametoID(); |
---|
159 | $myID->setNames($pilotname); |
---|
160 | $myID->fetchXML(); |
---|
161 | $myNames = $myID->getNameData(); |
---|
162 | $externalID = $myNames[0]['characterID']; |
---|
163 | } |
---|
164 | // If we have an external id then check it isn't already in use. |
---|
165 | // If we find it then update the old corp with the new name and |
---|
166 | // return. |
---|
167 | if($externalID) |
---|
168 | { |
---|
169 | $qry->execute("SELECT * FROM kb3_pilots WHERE plt_externalid = ".$externalID); |
---|
170 | if ($qry->recordCount() > 0) |
---|
171 | { |
---|
172 | $row = $qry->getRow(); |
---|
173 | $qry->execute("UPDATE kb3_pilots SET plt_name = '".slashfix($name)."' WHERE plt_externalid = ".$externalID); |
---|
174 | |
---|
175 | $this->id_ = $row['plt_id']; |
---|
176 | $this->name_ = slashfix($name); |
---|
177 | $this->externalid_ = $row['plt_externalid']; |
---|
178 | $this->corp_ = $row['plt_crp_id']; |
---|
179 | |
---|
180 | // Now check if the corp needs to be updated. |
---|
181 | if ($row['plt_crp_id'] != $corp->getID() && $this->isUpdatable($timestamp)) |
---|
182 | { |
---|
183 | $qry->execute("update kb3_pilots |
---|
184 | set plt_crp_id = ".$corp->getID().", |
---|
185 | plt_updated = date_format( '".$timestamp."', '%Y.%m.%d %H:%i:%s') where plt_id = ".$this->id_); |
---|
186 | } |
---|
187 | return $this->id_; |
---|
188 | } |
---|
189 | } |
---|
190 | if($externalID) |
---|
191 | $qry->execute("insert into kb3_pilots (plt_id, plt_name, plt_crp_id, plt_externalid, plt_updated) values ( null, |
---|
192 | '".slashfix($name)."', |
---|
193 | ".$corp->getID().", |
---|
194 | ".intval($externalID).", |
---|
195 | date_format( '".$timestamp."', '%Y.%m.%d %H:%i:%s'))"); |
---|
196 | else |
---|
197 | $qry->execute("insert into kb3_pilots (plt_id, plt_name, plt_crp_id, plt_updated) values ( null, |
---|
198 | '".slashfix($name)."', |
---|
199 | ".$corp->getID().", |
---|
200 | date_format( '".$timestamp."', '%Y.%m.%d %H:%i:%s'))"); |
---|
201 | $this->id_ = $qry->getInsertID(); |
---|
202 | } |
---|
203 | else |
---|
204 | { |
---|
205 | $row = $qry->getRow(); |
---|
206 | $this->id_ = $row['plt_id']; |
---|
207 | $this->updated_ = strtotime($row['plt_updated']." UTC"); |
---|
208 | if(!$this->updated_) $this->updated_ = 0; |
---|
209 | if ($this->isUpdatable($timestamp) && $row['plt_crp_id'] != $corp->getID()) |
---|
210 | { |
---|
211 | $qry->execute("update kb3_pilots |
---|
212 | set plt_crp_id = ".$corp->getID().", |
---|
213 | plt_updated = date_format( '".$timestamp."', '%Y.%m.%d %H:%i:%s') where plt_id = ".$this->id_); |
---|
214 | } |
---|
215 | if (!$row['plt_externalid'] && $externalID) $this->setCharacterID($externalID); |
---|
216 | } |
---|
217 | |
---|
218 | return $this->id_; |
---|
219 | } |
---|
220 | //! Return whether this pilot was updated before the given timestamp. |
---|
221 | |
---|
222 | /*! |
---|
223 | * \param $timestamp A timestamp to compare this pilot's details with. |
---|
224 | * \return boolean - true if update time was before the given timestamp. |
---|
225 | */ |
---|
226 | function isUpdatable($timestamp) |
---|
227 | { |
---|
228 | if(isset($this->updated_)) |
---|
229 | if(is_null($this->updated_) || strtotime($timestamp." UTC") > $this->updated_) return true; |
---|
230 | else return false; |
---|
231 | $qry = new DBQuery(); |
---|
232 | $qry->execute("select plt_id |
---|
233 | from kb3_pilots |
---|
234 | where plt_id = ".$this->id_." |
---|
235 | and ( plt_updated < date_format( '".$timestamp."', '%Y.%m.%d %H:%i') |
---|
236 | or plt_updated is null )"); |
---|
237 | |
---|
238 | return $qry->recordCount() == 1; |
---|
239 | } |
---|
240 | //! Set the CCP external ID for this pilot. |
---|
241 | |
---|
242 | /*! |
---|
243 | * \param $externalID CCP external ID for this pilot. |
---|
244 | */ |
---|
245 | function setCharacterID($externalID) |
---|
246 | { |
---|
247 | if (!intval($externalID)) |
---|
248 | { |
---|
249 | return false; |
---|
250 | } |
---|
251 | $this->externalid_ = intval($externalID); |
---|
252 | $qry = new DBQuery(); |
---|
253 | $qry->execute("SELECT plt_id FROM kb3_pilots WHERE plt_external_id = ".$externalid." AND plt_id <> ".$this->id_); |
---|
254 | if($qry->recordCount()) |
---|
255 | { |
---|
256 | $result = $qry->getRow(); |
---|
257 | $old_id = $result['crp_id']; |
---|
258 | $qry->execute("UPDATE kb3_pilots SET plt_id = 0 where plt_id = ".$old_id); |
---|
259 | } |
---|
260 | $qry->execute("update kb3_pilots set plt_externalid = ".$this->externalid_." |
---|
261 | where plt_id = ".$this->id_); |
---|
262 | } |
---|
263 | } |
---|