/
class.comments.php
488 lines (435 loc) · 14.9 KB
/
class.comments.php
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/*
*
* PHP script comments.
* Version: 1.0 (beta)
* Late: 06.11.2012
* Autor: Chernyshov Roman
* Site: http://rche.ru
* E-mail: houseprog@ya.ru
*
*/
class Comments extends Controller_Base {
var $path = ''; // path to page on comments
var $table = 'comments'; // table comments
var $prefix = 'rche_'; // prefix table comments
var $event = '';
var $key = 'e34d9147f42016a32a9bab982492323547e121ce'; // sicret key for ajax
var $login = false; // login user or email and name
var $user = array(); // user info if login
var $admin = false; // admin option
var $gravatar = false; // avatar from gravatar.com
var $capcha = true; // enable Capcha
var $paths = array(); // path's
function index () {
$this->event=@$_POST['eventComments'];
if(@$_GET['eventComments']=='del' and @$_GET['noajax']==1)$this->event=@$_GET['eventComments'];
if($this->event=='save') $status=$this->saveComments();
if($this->event=='del') $status=$this->delComments();
if($this->event=='edit') $status=$this->editComments();
if($this->event=='reply') $status=$this->replyComments();
if($this->event=='')$status=NULL;
return $status;
}
function delComments() {
$id = intval($_POST['id']);
$passport =$_POST['passport'];
if($_GET['noajax']==1)
{
$id = intval($_GET['id']);
$passport =$_GET['passport'];
}
if($passport==md5($this->key.'admin'))
{
$sql="SELECT {$this->prefix}{$this->table}.id, {$this->prefix}{$this->table}.reply FROM {$this->prefix}{$this->table}
WHERE {$this->prefix}{$this->table}.url='".$this->getUrl()."' ORDER BY {$this->prefix}{$this->table}.id ASC";
$allComm=$this->registry['DB']->getAll($sql);
if(count($allComm)>0):
// subcomments
foreach($allComm as $item):
if($item['reply']==0)$sortcomm[$item['id']]=$item;
if($item['reply']>0)
{
if(isset($path[$item['reply']]))
{
$str='$sortcomm';
foreach($path[$item['reply']] as $pitem):
$rep=$item['reply'];
$str.="[$pitem][sub]";
endforeach;
$str.="[{$item['reply']}][sub]";
$str.="[{$item['id']}]";
$str.='=$item;';
eval($str);
foreach($path[$item['reply']] as $pitem):
$path[$item['id']][]=$pitem;
endforeach;
$path[$item['id']][]=$item['reply'];
}
else
{
$sortcomm[$item['reply']]['sub'][$item['id']]=$item;
$path[$item['id']][]=$item['reply'];
}
}
endforeach;
endif;
$this->tree_delComment($sortcomm,$id);
if($_GET['noajax']==1):
$url=explode('?',$_SERVER['REQUEST_URI']);
header('Location: http://'.$_SERVER['HTTP_HOST'].$url[0]);
else: echo 'OK'; endif;
}
if(empty($_GET['noajax']))exit;
}
function tree_delComment(&$a_tree,&$id=0) {
if(count($a_tree)>0)
foreach($a_tree as $sub)
{
if($sub['id']<>$id and isset($sub['sub']))$this->tree_delComment($sub['sub'],$id);
if($sub['id']==$id)
{
$sql="DELETE FROM {$this->prefix}{$this->table} WHERE id = '$id' LIMIT 1";
$this->registry['DB']->execute($sql);
if (isset($sub['sub'])) $this->tree_delComment_process($sub['sub']);
}
}
}
function tree_delComment_process(&$a_tree) {
foreach($a_tree as $sub)
{
$sql="DELETE FROM {$this->prefix}{$this->table} WHERE id = '{$sub['id']}' LIMIT 1";
$this->registry['DB']->execute($sql);
if(isset($sub['sub']))$this->tree_delComment_process($sub['sub']);
}
}
function replyComments() {
$replyid = intval($_POST['replyid']);
$pass_checked=md5($this->user['password'].$this->key);
$post_url = htmlspecialchars(trim($_POST['posturlComment']));
if(strlen($post_url)>50 or strlen($post_url)<10) return;
$url = $post_url;
$urlOpen=$this->getUrl(false, 'open');
if($this->capcha)
{
$capcha ='
<br/>Input number from image: <input type="text" name="capcha" id="Rcapcha" value="" class="inputComment"/><br/>
<img src="'.$this->paths['capcha'].'" alt="image" width="120" height="50"/>
<br/>';
}
$form = '
<form action="" method="post" id="RformComment">
Name: <input name="nameComment" id="nameComment" value="" type="text" class="inputComment">
<input name="nameCommentCap" id="RnameCommentCap" value="" type="text">
E-mail: <input name="emailComment" id="emailComment" value="" type="text" class="inputComment">
<input name="replyComment" id="RreplyComment" value="'.$replyid.'" type="hidden">
<input name="loginComment" id="RloginComment" value="'.intval($this->login).'" type="hidden">
<input name="posturlComment" id="RposturlComment" value="'.$url.'" type="hidden">
<input name="personaComment" id="RpersonaComment" value="'.$this->user['userID'].'" type="hidden">
<input name="checkedComment" id="RcheckedComment" value="'.$pass_checked.'" type="hidden">
<input name="posturlOpenComment" id="RposturlOpenComment" value="'.$urlOpen.'" type="hidden">
<input name="eventComments" id="ReventComment" value="save" type="hidden">
<input name="noAjax" value="1" type="hidden">
<textarea name="textComment" id="RtextComment" class="textareaComment tinymce"></textarea>
'.$capcha.'
<input value="Send" name="submit" type="submit" class="submitComment"/>
</form>';
echo $form;
exit;
}
function itemComments($username,$date,$text,$img,$id,$autor=false, $userid='') {
$possport=md5($this->key.'admin');
// if($this->login)
$reply='<a href="javascript://" rel="'.$id.'" class="replyComment" title="Ответить на комментарий: '.$username.'">Ответить</a>';
// if($autor or $this->admin)$edit=' | <a href="javascript://" rel="'.$id.'" class="editComment" title="Редактировать комметарий">Редактировать</a>';
if($this->admin) $del=' | <a href="?id='.$id.'&passport='.$possport.'&noajax=1&eventComments=del" onclick="return false" rel="'.$id.'" passport="'.$possport.'" class="delComment" title="Удалить комментарий">Удалить</a>';
if($userid>0)$uslink="/com/profile/default/$userid/";else $uslink='#itemComment-'.$id;
$out='<div class="itemComment" id="itemComment-'.$id.'">
<div class="avatarComment">
<a href="'.$uslink.'" title="See user profile: '.$username.'">
<img src="'.$img.'" width="48" height="48" border="0" alt="User avatar: '.$username.'"/></a>
</div>
<div class="panelComment">
<a class="userComment" href="'.$uslink.'" title="See user progile: '.$username.'">'.$username.'</a>
<span class="dateComment" title="date, time comment">'.$date.'</span>
</div>
<div class="bodyComment">
'.$text.'
</div>
<div class="footerComment">
'.$reply.$edit.$del.'
</div>
';
return $out;
}
function outComments() {
echo '<div id="rcheComments">
<h3 class="titleComment">Comments</h3>
<div id="allComment">';
$sql="SELECT {$this->prefix}{$this->table}.*, rche_users.photo, rche_users.username, rche_users.userID FROM {$this->prefix}{$this->table}
LEFT JOIN rche_users ON {$this->prefix}{$this->table}.user =rche_users.userID
WHERE {$this->prefix}{$this->table} .url='".$this->getUrl()."' ORDER BY {$this->prefix}{$this->table}.id ASC";
$allComm=$this->registry['DB']->getAll($sql);
if(count($allComm)>0):
// subcomments
foreach($allComm as $item):
if($item['reply']==0)$sortcomm[$item['id']]=$item;
if($item['reply']>0)
{
if(isset($path[$item['reply']]))
{
$str='$sortcomm';
foreach($path[$item['reply']] as $pitem):
$rep=$item['reply'];
$str.="[$pitem][sub]";
endforeach;
$str.="[{$item['reply']}][sub]";
$str.="[{$item['id']}]";
$str.='=$item;';
eval($str);
foreach($path[$item['reply']] as $pitem):
$path[$item['id']][]=$pitem;
endforeach;
$path[$item['id']][]=$item['reply'];
}
else
{
$sortcomm[$item['reply']]['sub'][$item['id']]=$item;
$path[$item['id']][]=$item['reply'];
}
}
endforeach;
$this->tree_print($sortcomm);
else: echo '<p>No comments</p>'; endif;
echo '</div>
<div id="messComment"></div>
<div id="ajaxComment"></div>';
echo $this->pageComment();
echo $this->formComment();
}
function tree_print(&$a_tree) {
foreach($a_tree as $sub)
{
$this->outItem($sub);
if(!empty($sub['sub']))$this->tree_print($sub['sub']);
echo "</div>";
}
}
function outItem($item) {
$autor=false;
if(intval($item['user'])==0)
{
if($this->gravatar)
{
$lowercase = strtolower($item['email']);
$image = md5( $lowercase );
$img="http://www.gravatar.com/avatar.php?gravatar_id=$image";
} else $img='images/boy48.gif';
}
else
{
$img=$item['photo'];
$im=explode('/',$img);
$img='/images/'.$item['user'].'/48/48/1/'.$im['4'];
$item['name']=$item['username'];
}
if($item['pass']==$_COOKIE['comment'.$item['id']] and !empty($item['pass']) and ($item['date']+120)>time()) $autor=true;
echo $this->itemComments(
$item['name'],
$this->get_Date($item['date']),
html_entity_decode($item['comment']),
$img,
$item['id'],
$autor,
$item['userID']);
}
function saveComments() {
$name = trim(strip_tags($_POST['nameComment']));
$email = trim($_POST['emailComment']);
$text = PHP_slashes(htmlspecialchars(markhtml(trim(rawurldecode($_POST['textComment'])))));
$post_url = htmlspecialchars(trim($_POST['posturlComment']));
$urlOpen = htmlspecialchars(trim($_POST['posturlOpenComment']));
$error = false;
$login = intval($_POST['loginComment']);
$replyComment = intval($_POST['replyComment']);
$cap=$_POST['nameCommentCap'];
if($this->capcha) {
if($_SESSION['captha_text']!=$_POST['capcha']) {
echo 'ERR5';
exit;
}
}
if($login==1)
{
$persona= intval($_POST['personaComment']);
$checked= htmlspecialchars(trim($_POST['checkedComment']));
if($persona>0 and $checked>'')
{
$sql="SELECT rche_users.* FROM rche_users
WHERE rche_users.userID='$persona' LIMIT 1";
$user=$this->registry['DB']->getAll($sql);
if(md5($user[0]['password'].$this->key)==$checked)
{
$this->login=true;
$this->user=$user[0];
}
}
else
{
echo 'ERR4';
exit;
}
}
if(!$this->login)
{
if(strlen($name)<3) {$error=true;$msg=1;}
if(!$this->emailCheck($email) or strlen($name)>100){$error=true;$msg=2;}
$img='images/boy48.gif';
}
else
{
$img=$this->user['photo'];
$im=explode('/',$img);
$img='/images/'.$this->user['userID'].'/48/48/1/'.$im['4'];
$name=$this->user['username'];
$user=$this->user['userid'];
}
if(strlen($text)==0){$error=true;$msg=3;}
if(strlen($post_url)>50 or strlen($post_url)<10){$error=true;$msg=4;}
if($error)
{
echo 'ERR'.$msg;
exit;
}
$pass=$this->generate_password(8);
$date=$this->get_Date();
$time=time();
if($cap=='') {
$sql="INSERT INTO {$this->prefix}{$this->table} (`reply`,`user`,`name`,`email`,`comment`,`date`,`url`,`pass`,`urlOpen`)
VALUE ('$replyComment','{$this->user['userID']}','$name','$email','$text','$time','$post_url','$pass','$urlOpen')";
$this->registry['DB']->execute($sql);
}
$lastId=$this->registry['DB']->id;
setcookie('comment'.$lastId,$pass,$time+120,'/');
if(intval($_POST['noAjax'])<>1):
echo $this->itemComments(
$name,
$date,
html_entity_decode($text),
$img,
$lastId,
true,
$user);
exit;
endif;
}
function formComment($replyid=0)
{
global $user;
if($this->login)
{
$pass_checked=md5($this->user['password'].$this->key);
}
else
{
$name='
<tr><td class="section-one">Name</td><td>
<input name="nameComment" id="nameComment" value="" type="text" class="inputComment">
<input name="nameCommentCap" id="nameCommentCap" value="" type="text" class="nameCommentCap">
</td></tr>
<tr><td class="section-one">E-mail</td><td><input name="emailComment" id="emailComment" value="" type="text" class="inputComment"></td></tr>';
}
$url=$this->getUrl();
$urlOpen=$this->getUrl(false, 'open');
if($this->capcha)
{
$capcha ='
<tr><td class="section-one">Insert number from image:<br/><img src="'.$this->paths['capcha'].'" alt="картинка" width="120" height="50"/></td><td>
<input type="text" name="capcha" id="capcha" value="" class="inputComment"/></td></tr>';
}
$form = '<h3 id="newComment">Send comment </h3>
<form action="" method="post" id="formComment">
<input name="addComment" id="addComment" value="1" type="hidden">
<input name="loginComment" id="loginComment" value="'.intval($this->login).'" type="hidden">
<input name="posturlComment" id="posturlComment" value="'.$url.'" type="hidden">
<input name="posturlOpenComment" id="posturlOpenComment" value="'.$urlOpen.'" type="hidden">
<input name="personaComment" id="personaComment" value="'.@$this->user['userID'].'" type="hidden">
<input name="checkedComment" id="checkedComment" value="'.@$pass_checked.'" type="hidden">
<input name="eventComments" id="eventComment" value="save" type="hidden">
<input name="noAjax" value="1" type="hidden">
<table id="tableComment">
'.$name.'
<tr><td class="section-one">Text comment</td><td><textarea name="textComment" id="textComment" class="textareaComment tinymce"></textarea></td></tr>
'.$capcha.'
</table>
<input value="Send" name="submit" type="submit" class="submitComment"/>
</form>';
$form.='</div><p><a href="http://rche.ru" style="font:11px tahoma;color:#999;text-decoration:none">© www.rche.ru</a></p>';
return $form;
}
function pageComment() {
//return $out;
}
function getUrl($explode=false, $open = '') {
$url=$_SERVER["REQUEST_URI"];
if($this->admin==true) {
$u=explode('?',$url);
$e=explode('&',$u[1]);
$i=0;
foreach($e as $item)
{
$i++;
$data=explode('=',$item);
if($data[0]=='pass') continue;
$newQuery.=$item;
if($i<count($e))$newQuery.='&';
$newQuery='?'.$newQuery;
if(substr($newQuery, -1)=='&')$newQuery=substr($newQuery, 0, strlen($newQuery)-1);
}
$url="{$u[0]}{$newQuery}";
}
if($explode)
{
$url=explode('?',$_SERVER['REQUEST_URI']);
$url=$url[0];
}
if($open=='open')return urlencode($url);
return md5($url);
}
function emailCheck($email) {
if (!preg_match("/^(?:[a-z0-9]+(?:[-_.]?[a-z0-9]+)?@[a-z0-9_.-]+(?:\.?[a-z0-9]+)?\.[a-z]{2,5})$/i",trim($email)))
{
return false;
}
else return true;
}
function get_Date($shtamp='') {
if($shtamp=='')$shtamp=time();
$date = date('d.m:Y H:i',$shtamp);
return $date;
}
function generate_password($number)
{
$arr = array('a','b','c','d','e','f',
'g','h','i','j','k','l',
'm','n','o','p','r','s',
't','u','v','x','y','z',
'A','B','C','D','E','F',
'G','H','I','J','K','L',
'M','N','O','P','R','S',
'T','U','V','X','Y','Z',
'1','2','3','4','5','6',
'7','8','9','0'); /*,'.',',',
'(',')','[',']','!','?',
'&','^','%','@','*','$',
'<','>','/','|','+','-',
'{','}','`','~');*/
$pass = "";
for($i = 0; $i < $number; $i++)
{
$index = rand(0, count($arr) - 1);
$pass .= $arr[$index];
}
return $pass;
}
}