My favorites | Sign in
Project Home Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/*
* Copyright 2009 rrd@1108.cc All Rights Reserved.
*
* Unofficial iwiw "üzenőfal" API
*
* License: version 3 of the GNU General Public License.
* http://www.gnu.org/copyleft/gpl.html
*
*
*/

class Iwiw{
private $baseUrl = 'http://iwiw.hu';
private $indexUrl = 'http://iwiw.hu:80/pages/main/index.jsp';
private $loginUrl = 'http://iwiw.hu/pages/user/login.jsp';
private $loginUrlExt = '?method=Login';
private $activitiesUrl = 'http://iwiw.hu/pages/activity/activities.jsp';
//private $messageboardUrl = 'http://m.iwiw.hu/messageboard.jsp'; //we need a valid sid also
//private $messageboardUrl = 'http://iwiw.hu//pages/share/data.jsp';
private $messageboardUrl = 'http://iwiw.hu/pages/activity/activities.jsp?main=';
private $userprofileUrl = 'http://iwiw.hu/pages/user/userdata.jsp';
private $cp = null; //curl pointer
private $httpResponseOnError = array(4, 5); //http response code's first number on errors
private $loginInfo = array(); //iwiw username and password
private $months = array('január', 'február', 'március', 'április', 'május', 'június', 'július', 'augusztus', 'szeptember', 'október', 'november', 'december');
private $token = null; //session token

public $sid = null; //sid on iwiw fo this connection
public $responseCode = null; //http response code

/*
* consctruct a new iwiw object
*/
function __construct($sid = null){
if(!is_string($sid))
$sid = null;
if(is_string($sid) || $sid == null){
$this->_curlInit();
if(strlen($sid) > 32)
$this->sid = $sid;
}
}

/*
* logs in to iwiw
* @param array $account array(
* [username] => myusername
* [pass] => mysecretpassword
* )
* @return mixed string $sid on success array $error on error
*/
public function login($account){
//debug($account);
if(!$this->cp)
return array('error' => 'Curl pointer does not exists - connection refused');
//if we are already logged in just give back the sid
if($this->_hasActiveSid())
return $this->sid;

curl_setopt($this->cp, CURLOPT_URL, $this->loginUrl . $this->loginUrlExt);
$post = 'email='.$account['username'].'&password='.$account['pass'].'&httpslogin=false&loginradio=2';
curl_setopt($this->cp, CURLOPT_POST, true);
curl_setopt($this->cp, CURLOPT_POSTFIELDS, $post);
$iwiw = $this->curl_redir_exec($this->cp);
$curlError = $this->_getCurlError();
//debug($curlError);

//debug($iwiw);

preg_match('/token=([^"]*)/', $iwiw, $token);
if(isset($token[1]))
$this->token = $token[1];
else
return array('error' => 'There is no token');

if($curlError){
return array('error' => $curlError);
}
else{
$responseCode = curl_getinfo($this->cp, CURLINFO_HTTP_CODE);
if($responseCode == 200){
preg_match('/JSESSIONID=([^;]*)/', $iwiw, $sid);
if(isset($sid[1]))
$this->sid = $sid[1];
return true;
}
else
return array('error' => 'Response: ' . $responseCode);
}
}

/*
* Sends the message to iwiw messages feed
* @access public
* @return mixed array on error true on success
*/
public function sendMessage($message = null){
if(!$message)
return array('error' => 'NoMessage');

/*
http://iwiw.hu/pages/share/data.jsp?
http://iwiw.hu/pages/activity/activities.jsp?main=
POST: comment: $message token: new_token
válasz: <div id="activities_filter_box" class="box box_friends_actions"><div class="box_head"><div class="box_head_inner"><div class="box_logo"></div><h4><a href="/pages/activity/activities.jsp">Hírfolyam</a></h4></div></div>
*/

if(!$this->sid)
$this->login($this->loginInfo);
$post = 'comment='.$message.'&token='.$this->token;
curl_setopt($this->cp, CURLOPT_URL, $this->messageboardUrl);
curl_setopt($this->cp, CURLOPT_POSTFIELDS, $post);
$response = $this->curl_redir_exec($this->cp);
if($error = $this->_getCurlError())
return array('error' => $error);
if(curl_getinfo($this->cp, CURLINFO_HTTP_CODE) == 200)
return true;
else
return array('error' => 'Unknown error in sending message to iwiw');
}


/*
* Get's the "üzenőfal" from iwiw
* @access public
* @return mixed false on error array $timeline on success
* @restriction Strictly depend on iwiw html output as we have no API on this. If iwiw change the layout we will not get the timeline
*/
function getTimeline(){
if(!$sid = $this->sid)
$this->login($this->loginInfo);
curl_setopt($this->cp, CURLOPT_URL, $this->activitiesUrl);
$messageboard = $this->curl_redir_exec($this->cp);
$this->responseCode = curl_getinfo($this->cp, CURLINFO_HTTP_CODE);
$messageboard = str_replace("\n", '', str_replace("\r", '', $messageboard));
//debug($messageboard);
/*
<li id="activity_024c9c90007b2c0a4adc7540" class="shortMessage activity typeNEW_BOARDMSG clearfix">
<div class="outer">
<a class="image_border" href="/pages/user/userdata.jsp?userID=8072202">
<img src="http://ths7.iwiw.hu/0701/user/00/80/72/20/2/user_8072202_1134989726443_tn" alt=""/>
</a>
</div>
<div class="inner clearfix">
<p class="shortMessageTitle">
<span class="activityTitle">
<a href="/pages/user/userdata.jsp?userID=8072202">
Gömöri Sándor
</a>
teszt
</span>
</p>
<p class="clearfix activity_footer">
<span class="messageIcon">
</span>
<span title="2009. október 19., 16:18" class="time">
0 perce
</span>
<span class="involvement"/>
</p>
</div>
</li>
*/
/*
$messages[1] : links
$messages[2] : iwiw names
$messages[3] : message
$messages[4] : date
*/
//preg_match_all('%<a href="([^"]*)">([^>]*)</a><br/><i>(.*?)</i><br/>([^>]*)<br/>%', $messageboard, $messages);
preg_match_all('%<li [^>]*>(.*?)</li>%', $messageboard, $messages);
//debug($messages);

$timeline = array();
$i = 0;
foreach($messages[0] as $m){
if(strpos($m, 'id="activity_') && strpos($m, 'NEW_BOARDMSG')){ //<li id="activity_008d76354b3afbc405b52366" class="summary typeNEW_BOARDMSG">
//debug($m);
/*
<li id="activity_008d76354b3afbc405b52366" class="summary typeNEW_BOARDMSG">
<div class="activity">
<div class="outer">
<a class="image_border" href="/pages/user/userdata.jsp?userID=9270837">
<img src="http://ths7.iwiw.hu/0701/user/00/92/70/83/7/user_9270837_1262092167471_tn" alt=""/>
</a>
</div>
<div class="wrap">
<div class="inner">
<p class="activityTitle">
<span class="cnt">
<a href="/pages/user/userdata.jsp?userID=9270837" class="userName">
Major Gábor
</a>
ÁÁÁ Dobro jutro tatice! Kako si?
</span>
</p>
<div class="buttons">
<span title="2009. december 30., 08:05" class="act_btn time">
<span class="messageIcon"></span>
<span>
<a href="/pages/activity/activity.jsp?activityId=008d76354b3afbc405b52366">
3 órája
</a>
</span>
</span>
<a class="showWindow" href="/pages/share/reshare.jsp?activityId=008d76354b3afbc405b52366">
<button class="act_btn">
<div>
Én is megosztom
</div>
</button>
</a>
<a id="coment_btn_008d76354b3afbc405b52366" href="/pages/activity/activity.jsp?activityId=008d76354b3afbc405b52366">
<button class="act_btn comment">
<div>
Hozzászólás
</div>
<span class="arrow">»</span>
</button>
</a>
<a class="button_like remote methodPost replace" href="/pages/activity/useractivities.jsp?activityId=008d76354b3afbc405b52366&amp;method=Like&amp;like=true&amp;ajax&amp;token=6717032dbf8e7f3c#_self">
<button class="act_btn">
<div>
Tetszik
</div>
</button>
</a>
<a class="viewLevelSettings lazyload showBubble onClick autoClose hideOthers" href="/pages/activity/activity_viewlevel.jsp?activityType=NEW_BOARDMSG&amp;viewLevel=ALL&amp;activityId=008d76354b3afbc405b52366">
<button class="act_btn right">
<div>
elrejtés
</div>
</button>
</a>
</div>
<div id="like_list_008d76354b3afbc405b52366"/>
<div class="comments" id="comments_008d76354b3afbc405b52366"/>
<p class="one_line_input hidden">
<input type="text" value="Szólj hozzá Te is..."/>
</p>
<form method="POST" class="hidden comment_form remote replace" action="/pages/activity/useractivities.jsp">
<input type="hidden" value="6717032dbf8e7f3c" name="token"/>
<div class="image_border">
<div style="background-image: url(http://ths7.iwiw.hu/0701/user/00/80/72/20/2/user_8072202_1134989726443_tn);" class="square_image"/></div>
<input type="hidden" value="CreateComment" name="method"/>
<input type="hidden" value="008d76354b3afbc405b52366" name="activityId"/>
<textarea class="maxLength maxLength_500" name="comment"/>
<input type="submit" value="Mehet" class="input_accept"/>
<input type="submit" value="Mégsem" class="input_cancel"/>
</form>
</div>
</div>
</div>
</li>
*/
preg_match('%<p class="activityTitle">\s*<span class="cnt">\s*<a [^>]*href="([^"]*)">([^>]*)</a>(.*)\s*</span>\s*</p>%', $m, $user);
preg_match('%userID=\d*%', $m, $userId);
preg_match('%<span class="act_btn time" title="([^"]*)">%', $m, $mDate);
//debug($user);
//debug($mDate);
if(isset($user[2]) && isset($mDate[1])){
preg_match('%[a-z]\D*%', $mDate[1], $month); //todo: mi ez itt? miért kell [a-z] az elejére? jó lesz áprilisban?
$_month = array_search(trim($month[0]), $this->months) + 1;
$mDate = str_replace(' ' . $month[0], $_month . '.', $mDate[1]);
$mDate = str_replace(',', '', $mDate);

if(strpos($user[3], '<a') !== false){
//if the message has links we should replace it with the urls
$user[3] = preg_replace('%<a[^>]*href="([^"]*)"[^>]*>[^>]*</a>%', '\1', $user[3]);
}

$timeline[$i] = array(
'username' => $user[2],
'url' => $this->baseUrl . '/' . $user[1],
'message' => $user[3],
'created' => $mDate
);
$i++;
}
}
}
//debug($timeline);
return $timeline;
}

/*
* Checks if the current user has an active session on iwiw
* @return bool
* @access private
*/
private function _hasActiveSid(){
//we should get 200 response code header if we are already logged in
curl_setopt($this->cp, CURLOPT_URL, $this->indexUrl);
$responseCode = curl_getinfo($this->cp, CURLINFO_HTTP_CODE);
if($responseCode == 200)
return true;
else
return false;
}

/*
* Get curl errors
* @return mixed false if no error or array on error
*/
private function _getCurlError(){
$responseCode = curl_getinfo($this->cp, CURLINFO_HTTP_CODE);
if(in_array(substr($responseCode, 0, 1), $this->httpResponseOnError)){
//we have an error
return $responseCode;
}
else
return false;
}

/*
* Initialize curl session
*
*/
private function _curlInit(){
//initializing curl session
$this->cp = curl_init();
curl_setopt($this->cp, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($this->cp, CURLOPT_FOLLOWLOCATION, 10); //kövessük a szerveren az átirányításokat
//curl_setopt($this->cp, CURLOPT_USERAGENT, 'Opera/9.50 (J2ME/MIDP; Opera Mini/4.1.10781/298; U; en)');
curl_setopt($this->cp, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; hu-HU; rv:1.9.0.14) Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14');
curl_setopt($this->cp, CURLOPT_COOKIESESSION, true); //session cookie-kkezelése
curl_setopt($this->cp, CURLOPT_HEADER, true); //a hedaer is benne legyen az outputban
curl_setopt($this->cp, CURLOPT_COOKIEFILE, 'cookiefile'); //a cookie file neve
curl_setopt($this->cp, CURLOPT_COOKIEJAR, 'cookiefile');
curl_setopt($this->cp, CURLOPT_COOKIE, session_name() . '=' . session_id());
}

/*
* Finishes curl session
*/
private function _curlFinish(){
curl_close($this->cp);
}

/*
* If we get curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set error message
* we can not use CURLOPT_FOLLOWLOCATION so we need this
*/
function curl_redir_exec($ch){
static $curl_loops = 0;
static $curl_max_loops = 20;
if ($curl_loops++ >= $curl_max_loops){
$curl_loops = 0;
return false;
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
@list($header, $data) = explode("\n\n", $data, 2);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302){
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = @parse_url(trim(array_pop($matches)));
if (!$url){
//couldn't process the url to redirect to
$curl_loops = 0;
return $data;
}
$last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
if (!$url['scheme'])
$url['scheme'] = $last_url['scheme'];
if (!$url['host'])
$url['host'] = $last_url['host'];
if (!$url['path'])
$url['path'] = $last_url['path'];
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (isset($url['query'])?'?'.$url['query']:'');
curl_setopt($ch, CURLOPT_URL, $new_url);
//debug('Redirecting to', $new_url);
return $this->curl_redir_exec($ch);
}
else {
$curl_loops=0;
return $data;
}
}
}

Change log

r147 by rrd108 on Oct 11, 2010   Diff
iwiw api javítás
Go to: 
Project members, sign in to write a code review

Older revisions

r145 by rrd108 on Dec 30, 2009   Diff
iwiw változások követése
r143 by rrd108 on Nov 16, 2009   Diff
iwiw üzenetkeből linkek kiszűrése,
képek benthagyása
messages js frissítés
r136 by rrd108 on Nov 15, 2009   Diff
 issue94  iwiw linkek hibás importálása
javítása
All revisions of this file

File info

Size: 13816 bytes, 383 lines
Powered by Google Project Hosting