source: trunk/Modules/Meet/Meet.php@ 37

Last change on this file since 37 was 37, checked in by chronos, 6 years ago
  • Modified: Application made using classes System and Application.
  • Modified: Used Common package for better code reuse.
  • Modified: Application made modular. Web sections converted to application modules. They will register pages in main application object.
File size: 10.8 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Import/Seznamka.php');
4include_once(dirname(__FILE__).'/Import/TanecniSkola.php');
5include_once(dirname(__FILE__).'/Import/AstraPraha.php');
6include_once(dirname(__FILE__).'/Import/Vavruska.php');
7include_once(dirname(__FILE__).'/Import/SalsaDance.php');
8include_once(dirname(__FILE__).'/Import/Amblar.php');
9include_once(dirname(__FILE__).'/Import/MajkluvSvet.php');
10include_once(dirname(__FILE__).'/Import/Csts.php');
11include_once(dirname(__FILE__).'/Import/Facebook.php');
12include_once(dirname(__FILE__).'/Import/Eso.php');
13
14abstract class Gender
15{
16 const Undefined = 0;
17 const Male = 1;
18 const Female = 2;
19}
20
21function GetTextBetween(&$Text, $Start, $End)
22{
23 $Result = '';
24 if ((strpos($Text, $Start) !== false) and (strpos($Text, $End) !== false))
25 {
26 $Text = substr($Text, strpos($Text, $Start) + strlen($Start));
27 $Result = substr($Text, 0, strpos($Text, $End));
28 $Text = substr($Text, strpos($Text, $End) + strlen($End));
29 }
30 return $Result;
31}
32
33function HumanDateTimeToTime($DateTime)
34{
35 if($DateTime == '') return(NULL);
36 $DateTime = str_replace('. ', '.', $DateTime);
37 $Parts = explode(' ', $DateTime);
38 $DateParts = explode('.', $Parts[0]);
39 if (count($Parts) > 1) {
40 $TimeParts = explode(':', $Parts[1]);
41 if (count($TimeParts) == 1) $TimeParts[1] = '0';
42 if (count($TimeParts) == 2) $TimeParts[2] = '0';
43 } else $TimeParts = array(0, 0, 0);
44 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[0], $DateParts[2]);
45 return($Result);
46}
47
48function HumanDateToTime($Date)
49{
50 if($Date == '') return(NULL);
51 return(HumanDateTimeToTime($Date.' 0:0:0'));
52}
53
54function DecodeHtmlEnt($str)
55{
56 $prefix = '&#';
57 $suffix = ';';
58 $hexchar = 'x';
59 $ret = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
60 $p2 = 0;
61 for(;;)
62 {
63 $p = strpos($ret, $prefix, $p2);
64 if ($p === FALSE)
65 break;
66 $p2 = strpos($ret, $suffix, $p);
67 if ($p2 === FALSE)
68 {
69 $p2 = $p + strlen($prefix);
70 while (($p2 < strlen($ret)) and is_numeric($ret[$p2]))
71 $p2++;
72 if ($p2 <= ($p + strlen($prefix))) break;
73 $add = 0;
74 } else $add = 1;
75
76 if (substr($ret, $p + strlen($prefix), strlen($hexchar)) == $hexchar)
77 $char = hexdec(substr($ret, $p + strlen($prefix) + strlen($hexchar), $p2 - $p - strlen($prefix) - strlen($hexchar)));
78 else
79 $char = intval(substr($ret, $p + strlen($prefix), $p2 - $p - strlen($prefix)));
80
81 $newchar = iconv(
82 'UCS-4', 'UTF-8',
83 chr(($char >> 24) & 0xFF).chr(($char >> 16) & 0xFF).chr(($char >> 8) & 0xFF).chr($char & 0xFF)
84 );
85 $ret = substr_replace($ret, $newchar, $p, $add + $p2 - $p);
86 $p2 = $p + strlen($newchar) + $add;
87 }
88 return $ret;
89}
90
91function RemoveHtmlComments($Content)
92{
93 $Result = '';
94 while (strpos($Content, '<!--') !== false)
95 {
96 $Result .= substr($Content, 0, strpos($Content, '<!--'));
97 $Content = substr($Content, strpos($Content, '<!--') + 4);
98 $Content = substr($Content, strpos($Content, '-->') + 3);
99 }
100 return $Result;
101 //return preg_replace('/<!--(.|\s)*?-->/', '', $Content);
102}
103
104function is_alpha($Char)
105{
106 return ((($Char >= 'a') and ($Char <= 'z')) or (($Char >= 'A') and ($Char <= 'Z')));
107}
108
109function is_white_space($Char)
110{
111 return ($Char == ' ') or ($Char == "\t");
112}
113
114function GetNumberBeforeText($Text, $Needle)
115{
116 $Result = '';
117 for(;;)
118 {
119 $Pos = strpos($Text, $Needle);
120 if ($Pos !== false)
121 {
122 if ((($Pos + strlen($Needle)) < strlen($Text)) and (is_alpha($Text[$Pos + strlen($Needle)])))
123 {
124 $Text = substr($Text, $Pos + 1);
125 continue;
126 }
127 $Result = substr($Text, 0, $Pos);
128 $Text = substr($Text, $Pos + 1);
129 $Start = $Pos - 1;
130 while (($Start >= 0) and (is_numeric($Result[$Start]) or is_white_space($Result[$Start])))
131 $Start--;
132 $Start++;
133 $Result = trim(substr($Result, $Start, $Pos - $Start));
134 if (is_numeric($Result)) break;
135 } else break;
136 }
137 return $Result;
138}
139
140function GetNumberAfterText($Text, $Needle)
141{
142 $Result = '';
143 for(;;)
144 {
145 $Pos = strpos($Text, $Needle);
146 if ($Pos !== false)
147 {
148 if ((($Pos - 1) >= 0) and (is_alpha($Text[$Pos - 1])))
149 {
150 $Text = substr($Text, $Pos + 1);
151 continue;
152 }
153 $Result = substr($Text, $Pos + strlen($Needle));
154 $Text = substr($Text, $Pos + 1);
155 $End = 0;
156 while (($End < strlen($Result)) and (is_numeric($Result[$End]) or is_white_space($Result[$End])))
157 $End++;
158 $End--;
159
160 $Result = trim(substr($Result, 0, $End + 1));
161 if (is_numeric($Result)) break;
162 } else break;
163 }
164 return $Result;
165}
166
167function GetAgeFromText($Text)
168{
169 $Text = strtolower($Text);
170 $Result = GetNumberAfterText($Text, 'je mi');
171 if ($Result == '') $Result = GetNumberAfterText($Text, 'jsem');
172 if ($Result == '') $Result = GetNumberBeforeText($Text, 'rokov');
173 if ($Result == '') $Result = GetNumberBeforeText($Text, 'letou');
174 if ($Result == '') $Result = GetNumberBeforeText($Text, 'let');
175 if ($Result == '') $Result = GetNumberAfterText($Text, 'čerstvých');
176 if ($Result == '') $Result = GetAgeHeightWeightFromText($Text)[0];
177 if ($Result == '') {
178 $Year = GetNumberAfterText($Text, 'ročník');
179 if ($Year != '') $Result = date('Y', time()) - $Year;
180 }
181 return $Result;
182}
183
184function GetHeightFromText($Text)
185{
186 $Text = strtolower($Text);
187 $Result = GetNumberAfterText($Text, 'měřím');
188 if ($Result == '') $Result = GetNumberAfterText($Text, 'merim');
189 if ($Result == '') $Result = GetNumberBeforeText($Text, 'cm');
190 if ($Result == '') $Result = GetNumberBeforeText($Text, 'bez podpatků');
191 if ($Result == '') $Result = GetAgeHeightWeightFromText($Text)[1];
192 return $Result;
193}
194
195function GetWeightFromText($Text)
196{
197 $Text = strtolower($Text);
198 $Result = GetNumberBeforeText($Text, 'kg');
199 if ($Result == '') $Result = GetNumberAfterText($Text, 'vážím');
200 if ($Result == '') $Result = GetAgeHeightWeightFromText($Text)[2];
201 return $Result;
202}
203
204function GetAgeHeightWeightFromText($Text)
205{
206 $Result = array('', '', '');
207 $Pattern = '/[0-9]+\/[0-9]+\/[0-9]+/i';
208 if (preg_match_all($Pattern, $Text, $Matches))
209 {
210 $Result = explode('/', $Matches[0][0]);
211 // Avoid dates in a form day/month/year
212 if ($Result[2] > 150) $Result = array('', '', '');
213 } else
214 {
215 $Pattern = '/[0-9]+\/[0-9]+/i';
216 if (preg_match_all($Pattern, $Text, $Matches))
217 {
218 $Result = explode('/', $Matches[0][0]);
219 // If first number is over 100 then its probably height/weight
220 if ($Result[0] > 100) $Result = array('', $Result[0], $Result[1]);
221 $Result[] = '';
222 }
223 }
224 return $Result;
225}
226
227function GetEmailFromText($Text)
228{
229 $Result = '';
230 if (strpos($Text, '@') !== false)
231 {
232 $Pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
233 preg_match_all($Pattern, $Text, $Matches);
234 if (count($Matches) > 0)
235 $Result = $Matches[0][0];
236 }
237 return $Result;
238}
239
240$Locations = array(
241 'praha' => 'Praha',
242 'prahy' => 'Praha',
243 'praze' => 'Praha',
244 'brno' => 'Brno',
245 'brně' => 'Brno',
246 'ostrava' => 'Ostrava',
247 'ostravě' => 'Ostrava',
248 'ostravy' => 'Ostrava',
249 'olomouc' => 'Olomouc',
250 'liberec' => 'Liberec',
251 'opava' => 'Opava',
252 'opave' => 'Opava',
253 'plzeň' => 'Plzeň',
254 'plzni' => 'Plzeň',
255 'vyškov' => 'Vyškov',
256 'mladá boleslav' => 'Mladá Boleslav',
257 'mladé boleslavi' => 'Mladá Boleslav',
258 'litoměřice' => 'Litoměřice',
259 'sokolov' => 'Sokolov',
260 'mikulov' => 'Mikulov',
261);
262
263function GetLocationFromText($Text)
264{
265 global $Locations;
266
267 $Text = strtolower($Text);
268
269 foreach ($Locations as $Index => $Location)
270 {
271 if (strpos($Text, $Index) !== false) return $Location;
272 }
273 return '';
274}
275
276class MeetSources
277{
278 public $Database;
279
280 function Parse($Id = null)
281 {
282 $Output = '';
283 if (($Id != null) and is_numeric($Id)) $Where = 'Id='.$Id;
284 else $Where = '1';
285 $DbResult = $this->Database->select('MeetSource', '*', $Where);
286 while ($DbRow = $DbResult->fetch_assoc())
287 {
288 $Method = $DbRow['Method'];
289 if ($Method == 'hes') $Source = new MeetSourceTanecniSkola();
290 else if ($Method == 'vavruska') $Source = new MeetSourceVavruska();
291 else if ($Method == 'salsadance') $Source = new MeetSourceSalsaDance();
292 else if ($Method == 'astra') $Source = new MeetSourceAstraPraha();
293 else if ($Method == 'seznamka') $Source = new MeetSourceSeznamka();
294 else if ($Method == 'amblar') $Source = new MeetSourceAmblar();
295 else if ($Method == 'majkluvsvet') $Source = new MeetSourceMajkluvSvet();
296 else if ($Method == 'csts') $Source = new MeetSourceCsts();
297 else if ($Method == 'facebook') $Source = new MeetSourceFacebook();
298 else if ($Method == 'eso') $Source = new MeetSourceEso();
299 else {
300 $Output .= 'Unsupported parse method: '.$Method.'<br/>';
301 continue;
302 }
303 $Source->Database = $this->Database;
304 $Source->Id = $DbRow['Id'];
305 $Source->URL = $DbRow['URL'];
306 $Source->Method = $Method;
307 $Source->Name = $DbRow['Name'];
308 $this->Items[] = $Source;
309 $Output .= $Source->Import();
310 }
311 return $Output;
312 }
313}
314
315class MeetSource
316{
317 public $Name;
318 public $URL;
319 public $Method;
320 public $Id;
321 public $Database;
322
323 function Import()
324 {
325 $this->AddedCount = 0;
326 $Output = 'Parsing '.$this->Name.' ('.$this->Id.')...</br>';
327 return $Output;
328 }
329}
330
331class MeetItem
332{
333 var $Database;
334 var $Name = '';
335 var $Message = '';
336 var $Time = '';
337 var $Gender = Gender::Undefined;
338 var $Phone = '';
339 var $Email = '';
340 var $Age = '';
341 var $Height = '';
342 var $Source = 0;
343 var $Weight = '';
344 var $Location = '';
345 var $Image = '';
346 var $Link = '';
347 var $Title = '';
348 var $Level = '';
349
350 function AddIfNotExist()
351 {
352 $DbResult = $this->Database->select('MeetItem', '*',
353 '(`Message` = "'.$this->Database->real_escape_string($this->Message).'") AND '.
354 '(`Email` = "'.$this->Database->real_escape_string($this->Email).'") AND '.
355 '(`Time` = "'.$this->Database->real_escape_string(TimeToMysqlDateTime($this->Time)).'")');
356 if ($DbResult->num_rows == 0)
357 {
358 if ($this->Age == '') $Age = null;
359 else $Age = $this->Age;
360 if ($this->Height == '') $Height = null;
361 else $Height = $this->Height;
362 if ($this->Weight == '') $Weight = null;
363 else $Weight = $this->Weight;
364 $this->Database->insert('MeetItem', array(
365 'Message' => $this->Message,
366 'Time' => TimeToMysqlDateTime($this->Time),
367 'Gender' => $this->Gender,
368 'Age' => $Age,
369 'Email' => $this->Email,
370 'Phone' => $this->Phone,
371 'Name' => $this->Name,
372 'Height' => $Height,
373 'Weight' => $Weight,
374 'Location' => $this->Location,
375 'Source' => $this->Source,
376 'Link' => $this->Link,
377 'TimeImport' => 'NOW()',
378 ));
379 $Result = 1;
380 } else $Result = 0;
381 return($Result);
382 }
383}
Note: See TracBrowser for help on using the repository browser.