File: _functions_comments.php

package info (click to toggle)
b2evolution 0.9.2-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 12,976 kB
  • ctags: 5,460
  • sloc: php: 58,989; sh: 298; makefile: 36
file content (527 lines) | stat: -rw-r--r-- 13,685 bytes parent folder | download | duplicates (2)
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/**
 * Comment handling
 * 
 * b2evolution - {@link http://b2evolution.net/}
 * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
 * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
 *
 * @package evocore
 * @author This file built upon code from original b2 - http://cafelog.com/
 */
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );

/**
 * Includes:
 */
require_once dirname(__FILE__). '/_class_comment.php';

/**
 * generic_ctp_number(-)
 *
 * generic comments/trackbacks/pingbacks numbering
 *
 * fplanque: added stuff to load all number for this page at ounce
 */
function generic_ctp_number($post_id, $mode = 'comments')
{
	global $DB, $debug, $postdata, $tablecomments, $cache_ctp_number, $use_cache, $preview;
	if( $preview )
	{	// we are in preview mode, no comments yet!
		return 0;
	}
	
	if( $mode == 'feedbacks' ) $mode ='ctp';

	// fplanque added: load whole cache
	if (!isset($cache_ctp_number) || (!$use_cache))
	{
		global $postIDlist, $postIDarray;
		// if( $debug ) echo "LOADING generic_ctp_number CACHE for posts: $postIDlist<br />";
		foreach( $postIDarray as $tmp_post_id)
		{		// Initializes each post to nocount!
				$cache_ctp_number[$tmp_post_id] = array( 'comments' => 0, 'trackbacks' => 0, 'pingbacks' => 0, 'ctp' => 0);
		}
		$query = "SELECT comment_post_ID, comment_type, COUNT(*) AS type_count 
							FROM $tablecomments 
							WHERE comment_post_ID IN ($postIDlist) 
							GROUP BY comment_post_ID, comment_type";
		$rows = $DB->get_results( $query );
		if( count( $rows ) ) foreach( $rows as $row )
		{
			switch( $row->comment_type )
			{
				case 'comment';
					$cache_ctp_number[$row->comment_post_ID]['comments'] = $row->type_count;
					break;

				case 'trackback';
					$cache_ctp_number[$row->comment_post_ID]['trackbacks'] = $row->type_count;
					break;

				case 'pingback';
					$cache_ctp_number[$row->comment_post_ID]['pingbacks'] = $row->type_count;
					break;
			}
			$cache_ctp_number[$row->comment_post_ID]['ctp'] += $row->type_count;
		}
	}
	/*	else
	{
		echo "cache set";
	}*/
	if (!isset($cache_ctp_number[$post_id]) || (!$use_cache))
	{	// this should be extremely rare...
		// echo "CACHE not set for $post_id";
		$post_id = intval($post_id);
		$query = "SELECT comment_post_ID, comment_type, COUNT(*) AS type_count 
							FROM $tablecomments 
							WHERE comment_post_ID = $post_id 
							GROUP BY comment_post_ID, comment_type";
		$rows = $DB->get_results( $query );
		if( count( $rows ) ) foreach( $rows as $row )
		{
			switch( $row->comment_type )
			{
				case 'comment';
					$cache_ctp_number[$row->comment_post_ID]['comments'] = $row->type_count;
					break;

				case 'trackback';
					$cache_ctp_number[$row->comment_post_ID]['trackbacks'] = $row->type_count;
					break;

				case 'pingback';
					$cache_ctp_number[$row->comment_post_ID]['pingbacks'] = $row->type_count;
					break;
			}
			$cache_ctp_number[$row->comment_post_ID]['ctp'] += $row->type_count;
		}
	} else {
		$ctp_number = $cache_ctp_number[$post_id];
	}
	if (($mode != 'comments') && ($mode != 'trackbacks') && ($mode != 'pingbacks') && ($mode != 'ctp')) {
		$mode = 'ctp';
	}
	return $ctp_number[$mode];
}


/*
 * get_commentdata(-)
 */
function get_commentdata($comment_ID,$no_cache=0)
{ // less flexible, but saves mysql queries
	global $DB, $rowc,$id,$commentdata,$tablecomments, $baseurl;

	if ($no_cache)
	{
		$query = "SELECT * 
							FROM $tablecomments 
							WHERE comment_ID = $comment_ID";
		$myrow = $DB->get_row( $query, ARRAY_A );
	}
	else
	{
		$myrow['comment_ID'] = $rowc->comment_ID;
		$myrow['comment_post_ID'] = $rowc->comment_post_ID;
		$myrow['comment_author'] = $rowc->comment_author;
		$myrow['comment_author_email'] = $rowc->comment_author_email;
		$myrow['comment_author_url'] = $rowc->comment_author_url;
		$myrow['comment_author_IP'] = $rowc->comment_author_IP;
		$myrow['comment_date'] = $rowc->comment_date;
		$myrow['comment_content'] = $rowc->comment_content;
		$myrow['comment_karma'] = $rowc->comment_karma;
		$myrow['comment_type'] = $rowc->comment_type;
		if( isset($rowc->ID) ) $myrow['post_ID'] = $rowc->ID;
		if( isset($rowc->post_title) ) $myrow['post_title'] = $rowc->post_title;
		if( isset($rowc->blog_name) ) $myrow['blog_name'] = $rowc->blog_name;
		if( isset($rowc->blog_siteurl) ) $myrow['blog_siteurl'] = $baseurl.$rowc->blog_siteurl;
		if( isset($rowc->blog_stub) ) $myrow['blog_stub'] = $rowc->blog_stub;
	}
	return($myrow);
}


/*
 * Comment_get_by_ID(-)
 */
function Comment_get_by_ID( $comment_ID )
{
	global $DB, $cache_Comments, $use_cache, $tablecomments, $querycount;

	if((empty($cache_Comments[$comment_ID])) OR (!$use_cache))
	{	// Load this entry into cache:
		$query = "SELECT * 
							FROM $tablecomments 
							WHERE comment_ID = $comment_ID";
		if( $row = $DB->get_row( $query, ARRAY_A ) )
		{
			$cache_Comments[$comment_ID] = new Comment( $row ); // COPY !
		}
	}

	if( empty( $cache_Comments[ $comment_ID ] ) ) die('Requested comment does not exist!');

	return $cache_Comments[ $comment_ID ];
}


/*
 * TEMPLATE functions
 */

/**
 * Display "Last comments" title if these have been requested
 *
 * {@internal last_comments_title(-) }}
 *
 * @param string Prefix to be displayed if something is going to be displayed
 * @param mixed Output format, see {@link format_to_output()} or false to
 *								return value instead of displaying it
 */
function last_comments_title( $prefix = ' ', $display = 'htmlbody' )
{
	global $disp;

	if( $disp == 'comments' )
	{
		$info = $prefix.T_('Last comments');
		if ($display)
			echo format_to_output( $info, $display );
		else
			return $info;
	}
}



/***** Comment tags *****/



/**
 * comments_number(-)
 *
 * @deprecated deprecated by {@link Item::feedback_link()}
 */
function comments_number( $zero='#', $one='#', $more='#' )
{
	if( $zero == '#' ) $zero = T_('Leave a comment');
	if( $one == '#' ) $one = T_('1 comment');
	if( $more == '#' ) $more = T_('%d comments');

	// original hack by dodo@regretless.com
	global $id,$postdata,$tablecomments,$c,$querycount,$cache_commentsnumber,$use_cache;
	$number = generic_ctp_number($id, 'comments');
	if ($number == 0) {
		$blah = $zero;
	} elseif ($number == 1) {
		$blah = $one;
	} elseif ($number  > 1) {
		$n = $number;
		$more = str_replace('%d', $n, $more);
		$blah = $more;
	}
	echo $blah;
}

/**
 * {@internal comments_link(-)}}
 *
 * Displays link to comments page
 *
 * @deprecated deprecated by {@link Item::feedback_link()}
 */
function comments_link($file='', $tb=0, $pb=0 )
{
	global $id;
	if( ($file == '') || ($file == '/')	)
		$file = get_bloginfo('blogurl');
	echo url_add_param( $file, 'p='. $id. '&amp;c=1' );
	if( $tb == 1 )
	{	// include trackback // fplanque: added
		echo '&amp;tb=1';
	}
	if( $pb == 1 )
	{	// include pingback // fplanque: added
		echo '&amp;pb=1';
	}
	echo '#comments';
}


/**
 * This will include the javascript that is required to open comments, 
 * trackback and pingback in popup windows.
 *
 * You should put this tag before the </head> tag in your template.
 *
 * {@internal comments_popup_script(-)}}
 *
 * @param integer width of window or false
 * @param integer height of window or false
 * @param boolean do you want a scrollbar
 * @param boolean do you want a status bar
 * @param boolean do you want the windows to be resizable
  */
function comments_popup_script( $width = 600, $height = 450, 
																$scrollbars = true, $status = true, $resizable = true )
{
	global $b2commentsjavascript;
	$b2commentsjavascript = true;
	$properties = array();
	if( $width ) $properties[] = 'width='.$width;
	if( $height ) $properties[] = 'height='.$height;
	$properties[] = 'scrollbars='.intval( $scrollbars );
	$properties[] = 'status='.intval( $status );
	$properties[] = 'resizable='.intval( $resizable );
	$properties = implode( ',', $properties );
	?>
	<script language="javascript" type="text/javascript">
	<!--
		function b2open( url )
		{
			window.open( url, '_blank', '<?php echo $properties; ?>');
		}
		//-->
	</script>
	<?php
}


/**
 * comments_popup_link(-)
 *
 * @deprecated deprecated by {@link Item::feedback_link()}
 */
function comments_popup_link($zero='#', $one='#', $more='#', $CSSclass='')
{
	global $blog, $id, $b2commentspopupfile, $b2commentsjavascript;
	echo '<a href="';
	if($b2commentsjavascript)
	{
		echo url_add_param( get_bloginfo('blogurl'), 'template=popup&amp;p='.$id.'&amp;c=1' );
		echo '" onclick="b2open(this.href); return false"';
	}
	else
	{	// if comments_popup_script() is not in the template, display simple comment link
		comments_link();
		echo '"';
	}
	if (!empty($CSSclass)) {
		echo ' class="'.$CSSclass.'"';
	}
	echo '>';
	comments_number($zero, $one, $more);
	echo '</a>';
}

/**
 * comment_ID(-)
 *
 * @deprecated deprecated by {@link DataObject::ID()}
 */
function comment_ID()
{
	global $commentdata;	echo $commentdata['comment_ID'];
}

/**
 * comment_author(-)
 *
 * @deprecated deprecated by {@link Comment::author()}
 */
function comment_author()
{
	global $commentdata;
	echo $commentdata['comment_author'];
}

/**
 * comment_author_email(-)
 *
 * @deprecated deprecated by {@link Comment::author_email()}
 */
function comment_author_email()
{
	global $commentdata;	echo antispambot( $commentdata['comment_author_email'] );
}

/**
 * comment_author_url(-)
 *
 * @deprecated deprecated by {@link Comment::author_url()}
 */
function comment_author_url($echo=true)
{
	global $commentdata;
	$url = trim($commentdata['comment_author_url']);
	$url = (!stristr($url, '://')) ? 'http://'.$url : $url;
	// convert & into &amp;
	$url = preg_replace('#&([^amp\;])#is', '&amp;$1', $url);
	if ($url != 'http://')
	{
		if ($echo)
			echo $url;
		else
			return $url;
	}
}

/**
 * comment_author_url_basedomain(-)
 *
 * @deprecated
 */
function comment_author_url_basedomain( $disp = true )
{
	global $commentdata;
	$url = comment_author_url(false);
	$baseDomain = preg_replace("/http:\/\//i", "", $url);
	$baseDomain = preg_replace("/^www\./i", "", $baseDomain);
	$baseDomain = preg_replace("/\/.*/i", "", $baseDomain);
	if( $disp )
		echo $baseDomain;
	else
		return $baseDomain;
}

/**
 * comment_author_email_link(-)
 *
 * @deprecated deprecated by {@link Comment::author_email()}
 */
function comment_author_email_link($linktext='', $before='', $after='')
{
	global $commentdata;
	$email=$commentdata['comment_author_email'];
	if ((!empty($email)) && ($email != '@')) {
		$display = ($linktext != '') ? $linktext : antispambot($email);
		echo $before;
		echo '<a href="mailto:'.antispambot($email).'">'.$display.'</a>';
		echo $after;
	}
}


/**
 * comment_author_url_link(-)
 *
 * @deprecated deprecated by {@link $Comment->author_url()}
 */
function comment_author_url_link($linktext='', $before='', $after='')
{
	global $commentdata;
	$url = trim($commentdata['comment_author_url']);
	$url = preg_replace('#&([^amp\;])#is', '&amp;$1', $url);
	$url = (!stristr($url, '://')) ? 'http://'.$url : $url;
	if ((!empty($url)) && ($url != 'http://') && ($url != 'http://url'))
	{
		$display = ($linktext != '') ? $linktext : $url;
		echo $before;
		echo '<a href="'.$url.'">'.$display.'</a>';
		echo $after;
	}
}

/**
 * comment_author_IP(-)
 *
 * @deprecated deprecated by {@link Comment::author_ip()}
 */
function comment_author_IP() {
	global $commentdata;	echo $commentdata['comment_author_IP'];
}

/**
 * comment_text(-)
 *
 * @deprecated deprecated by {@link $Comment::content()}
 */
function comment_text()
{
	global $commentdata;

	$comment = $commentdata['comment_content'];
	$comment = str_replace('<trackback />', '', $comment);
	$comment = str_replace('<pingback />', '', $comment);
	$comment = format_to_output( $comment, 'htmlbody' );
	echo $comment;
}

/*$
 * comment_date(-)
 *
 * @deprecated deprecated by {@link $Comment::date()}
 */
function comment_date($d='') {
	global $commentdata;
	if( $d == '' )
		echo mysql2date( locale_datefmt(), $commentdata['comment_date']);
	else
		echo mysql2date($d, $commentdata['comment_date']);
}

/**
 * comment_time(-)
 *
 * @deprecated deprecated by {@link $Comment::time()}
 */
function comment_time( $d = '' )
{
	global $commentdata;
	if( $d == '' )
		echo mysql2date( locale_timefmt(), $commentdata['comment_date']);
	else
		echo mysql2date($d, $commentdata['comment_date']);
}

/**
 * comment_post_title(-)
 * fplanque added
 *
 * @deprecated deprecated by {@link $Comment::post_title()}
 */
function comment_post_title()
{
	global $commentdata;
	$title = $commentdata['post_title'];
	echo format_to_output( $title, 'htmlbody' );
}

/**
 * comment_post_link(-)
 * fplanque added
 *
 * @deprecated deprecated by {@link $Comment::post_link()}
 */
function comment_post_link()
{
	global $commentdata;
	echo gen_permalink( $commentdata['blog_siteurl']. '/'. $commentdata['blog_stub'], $commentdata['post_ID'],	'id', 'single' );
}


/**
 * comment_blog_name(-)
 * fplanque added
 *
 * @deprecated 
 */
function comment_blog_name( $disp = true )
{
	global $commentdata;
	$blog_name = $commentdata['blog_name'];
	if( !$disp )
		return $blog_name;
	echo $blog_name;
}

/*****
 * /Comment tags
 *****/



?>