PluginProbe
wpForo Forum / 1.4.11
wpForo Forum v1.4.11
3.1.5 3.1.4 3.1.2 3.1.1 3.1.0 3.0.9 3.0.8 3.0.7 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.2 All 137 releases
wpforo / wpf-includes / functions.php

functions.php in wpForo Forum 1.4.11, at wpf-includes/functions.php

1,968 lines 79.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // Exit if accessed directly
3 if( !defined( 'ABSPATH' ) ) exit;
4
5 function wpforo_verify_form( $mode = 'full' ){
6 if( $mode == 'nonce' || $mode == 'full'){
7 if(!isset($_POST['wpforo_form']) || !wp_verify_nonce( $_POST['wpforo_form'], 'wpforo_verify_form' )) {
8 wpforo_phrase('Sorry, something wrong with your data.');
9 exit();
10 }
11 }
12 if( $mode == 'ref' || $mode == 'full'){
13 if( !isset($_SERVER['HTTP_REFERER']) || !$_SERVER['HTTP_REFERER'] ) {
14 exit('Error 2252 | Please contact to forum admin.');
15 }
16 $ref = $_SERVER['HTTP_REFERER'];
17 $url = get_bloginfo('url');
18 $ref_domain = trim(strtolower(parse_url($ref, PHP_URL_HOST)));
19 $web_domain = trim(strtolower(parse_url($url, PHP_URL_HOST)));
20 if( $ref_domain != $web_domain ){
21 exit('Error 2253 | Please contact to forum admin.');
22 }
23 }
24 do_action('wpforo_verify_form_end');
25 }
26
27 function wpforo_home_url($str = '', $echo = false, $absolute = true){
28 if( strpos($str, 'http') === 0 ){
29 $base_url = preg_replace('#/?\?.*$#isu', '', wpforo_home_url());
30 $base_url = preg_replace('#index\.php/?#isu', '', $base_url, 1);
31 $str = preg_replace('#index\.php/?#isu', '', $str, 1);
32 $str = preg_replace('#^'.preg_quote( rtrim($base_url, '/\\') ).'/?#isu', '', $str, 1);
33 }
34 $str = trim(WPFORO_BASE_PERMASTRUCT, '/\\') . '/' . trim($str, '/\\');
35
36 if( $absolute ){
37 $url = WPF()->user_trailingslashit( home_url($str) );
38 //-START- check is url maybe wordpress home
39 $maybe_home_url = trim( preg_replace('#/?index\.php/?(\?.*)?$#isu', '', $url), '/\\' );
40 $home_url = trim( home_url(), '/\\' );
41 if( $maybe_home_url == $home_url ){
42 $url = preg_replace('#index\.php/?#isu', '', $url, 1);
43 $url = WPF()->user_trailingslashit($url);
44 }
45 //-END- check is url maybe wordpress home
46 }
47 else{
48 echo $url = WPF()->user_trailingslashit( $str );
49 }
50
51 if(!$echo) return $url;
52 echo $url;
53 }
54
55 function wpforo_is_ajax(){
56 if( defined('DOING_AJAX') && DOING_AJAX ) return TRUE;
57 return FALSE;
58 }
59
60 function wpforo_is_admin(){
61 if( is_admin() && !wpforo_is_ajax() ) return TRUE;
62 return FALSE;
63 }
64
65 function is_wpforo_page($url = ''){
66 if( wpforo_is_admin() ) return FALSE;
67 if(!$url) $url = wpforo_get_request_uri();
68 if( is_wpforo_exclude_url($url) ) return FALSE;
69 if( is_wpforo_shortcode_page() ) return TRUE;
70 $is_wpforo = is_wpforo_url($url);
71 $is_wpforo = apply_filters( 'is_wpforo_page', $is_wpforo, $url );
72 return $is_wpforo;
73 }
74
75 function is_wpforo_exclude_url($url = ''){
76 if(!$url) $url = wpforo_get_request_uri();
77 $url = urldecode($url);
78 $url = preg_replace('#/page/\d*/?$#isu', '', $url);
79
80 if( !$current_url = wpforo_get_url_query_vars_str($url) ) return FALSE;
81 if( preg_match('#\.php/?(?:\?[^/]*)?$#isu', $current_url) ) return TRUE;
82
83 if( WPF()->use_home_url && WPF()->excld_urls ){
84 $expld = array_filter( explode("\n", WPF()->excld_urls) );
85 foreach( $expld as $excld_url ){
86 $excld_url = urldecode($excld_url);
87 if( strpos($excld_url, home_url('/')) === FALSE ) continue;
88 $excld_url = wpforo_get_url_query_vars_str($excld_url);
89 if( $excld_url && strtolower($current_url) == strtolower($excld_url) ) return TRUE;
90 }
91 }
92
93 return FALSE;
94 }
95
96 function is_wpforo_url($url = ''){
97 if( wpforo_is_admin() ) return FALSE;
98 if(!$url) $url = wpforo_get_request_uri();
99
100 if( WPF()->use_home_url ) return TRUE;
101
102 $current_url = wpforo_get_url_query_vars_str($url);
103
104 if( WPF()->permastruct && strpos($current_url, WPF()->permastruct) !== FALSE
105 && strpos($current_url, WPF()->permastruct) == 0
106 && !wpforo_is_admin() ) return TRUE;
107
108 return FALSE;
109 }
110
111 /**
112 * @return bool
113 */
114 function is_wpforo_shortcode_page(){
115 if( wpforo_is_admin() ) return FALSE;
116 global $post;
117 if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wpforo' ) && !is_wpforo_url() ) return TRUE;
118 return FALSE;
119 }
120
121 /**
122 * @param string $text
123 * @return array|string
124 */
125 function get_wpforo_shortcode_atts($text = ''){
126 if (!$text){
127 if( wpforo_is_admin() ) return '';
128
129 global $post;
130 if( is_a( $post, 'WP_Post' ) ) $text = $post->post_content;
131 }
132
133 if( preg_match('#\[[\r\n\t\s\0]*wpforo[\r\n\t\s\0]*([^\[\]]*?)\]#isu', $text, $match) ) return shortcode_parse_atts($match[1]);
134
135 return '';
136 }
137
138 function wpforo_get_url_query_vars_str($url = ''){
139 if(!$url) $url = wpforo_get_request_uri();
140
141 $home_url = preg_replace( '#/?\?.*$#isu', '', home_url('/') );
142 $current_url = preg_replace('#https?://[^/\?]+/?#isu', '', $url);
143 $site_url = preg_replace('#https?://[^/\?]+/?#isu', '', $home_url);
144 $current_url = preg_replace( '#^/?'.preg_quote($site_url).'(?:/?index\.php/?)?#isu', '' , $current_url, 1 );
145 $current_url = preg_replace('#^[\r\n\t\s\0/]*(.*?)[\r\n\t\s\0/]*$#isu', '$1', $current_url);
146
147 return $current_url;
148 }
149
150 function wpforo_feature($option){
151 if (isset(WPF()->features[$option])) {
152 return WPF()->features[$option];
153 } else {
154 return false;
155 }
156 }
157
158 function wpforo_dir_size($directory) {
159 $size = 0;
160 if(class_exists('RecursiveIteratorIterator') && class_exists('RecursiveDirectoryIterator')){
161 foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) $size+=$file->getSize();
162 return $size;
163 }
164 else{
165 return 0;
166 }
167 }
168
169 function wpforo_make_hidden_fields_from_url($url = '', $echo = true){
170 if( !$url ) $url = wpforo_get_request_uri();
171
172 $return = '';
173 if( $url_query = parse_url($url, PHP_URL_QUERY) ){
174 parse_str($url_query, $url_query_arr);
175 if( !empty($url_query_arr) ){
176 foreach ($url_query_arr as $key => $value) {
177 $return .= '<input type="hidden" name="'.$key.'" value="'.$value.'">';
178 }
179 }
180 }
181
182 if( !$echo ) return $return;
183 echo $return;
184 }
185
186 #################################################################################
187 /**
188 * Returns merged arguments array from defined and default arguments.
189 * @param mixed $args
190 * @param array $default
191 * @return array
192 */
193 function wpforo_parse_args( $args, $default = array() ) {
194
195 if( is_object($args) ) {
196 $defined = get_object_vars($args);
197 }
198 elseif( is_array($args) ) {
199 $defined = $args;
200 }
201 elseif( preg_match('|^[\d\,\s]+$|', $args) ){
202 $defined = explode(',', trim($args));
203 }
204 elseif( is_integer($args) || is_float($args) ){
205 $defined[0] = $args;
206 }
207 elseif( is_serialized($args) ){
208 $defined = unserialize($args);
209 }
210 elseif( strpos($args, '=') !== FALSE ) {
211 parse_str($args, $defined);
212 }
213 else{
214 $defined[0] = $args;
215 }
216 if(!empty($default)) {
217 return array_merge( $default, $defined );
218 }
219 else {
220 return $defined;
221 }
222 }
223
224 // #############################################################################
225 /**
226 * Detects serialized data
227 *
228 * @since 1.0.0
229 *
230 * @param string
231 *
232 * @return boolean
233 */
234 if(!function_exists('is_serialized')){
235 function is_serialized( $value ){
236 if( $value == '' ) return false;
237 if( $value === 0 ) return false;
238 $value = trim($value);
239 $chsd = @unserialize($value);
240 if( $chsd !== false || $value === 'b:0;' ) {
241 return true;
242 }
243 else{
244 return false;
245 }
246 }
247 }
248
249
250 function wpforo_get_request_uri($with_port = FALSE, $get_referer_when_ajax = TRUE){
251 if( $get_referer_when_ajax && wpforo_is_ajax() ){
252 if( isset($_SERVER['HTTP_REFERER']) ){ return $_SERVER['HTTP_REFERER']; }
253 }
254 $s = is_ssl() ? 's' : '';
255 $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
256 $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
257 $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
258 return $protocol . "://" . $_SERVER['HTTP_HOST'] . ($with_port ? $port : '') . $_SERVER['REQUEST_URI'];
259 }
260
261
262 function wpforo_arr_group_by($array, $key_by){
263 if(!empty($array)){
264 $fltrd = array();
265 foreach($array as $key => $arr){
266 if(is_numeric($key)) $fltrd[] = $arr[$key_by];
267 }
268 $uniq_arr = array_unique($fltrd);
269 asort($uniq_arr);
270 return $uniq_arr;
271 }
272 }
273
274 // #############################################################################
275 /**
276 * Print item's (topics, replyes ...) table in dashboard
277 * @since 1.0.0
278 * @param string item(component) class name
279 * @param string This table primary key field name
280 * @param array table fields
281 * @param array table search fields
282 * @param array table filter fields
283 * @param array action buttons ( full posible values $actions = array( 'edit', 'delete', 'view' ); )
284 *
285 * @return echo html table
286 */
287 function wpforo_create_form_table($varname, $primary_key, $fields = array(), $search_fields = array(), $filter_fields = array(), $actions = array(), $bulk_actions = array() ){
288
289 if(empty($actions)) $actions = array( 'edit', 'delete', 'view' );
290 if(empty($bulk_actions)) $bulk_actions = array( 'del' );
291
292 if(!empty($fields)) :
293 $args = array();
294
295 if(isset($_GET['s']) && $_GET['s'] != '') $args['include'] = WPF()->$varname->search(sanitize_text_field($_GET['s']), $search_fields);
296 if(isset($_GET['orderby'])) $args['orderby'] = sanitize_text_field($_GET['orderby']);
297 if(isset($_GET['order'])) $args['order'] = sanitize_text_field($_GET['order']);
298 if(isset($_GET['forumid']) && $_GET['forumid'] != 0) $args['forumid'] = intval($_GET['forumid']);
299 if(isset($_GET['groupid']) && $_GET['groupid'] != 0) $args['groupid'] = intval($_GET['groupid']);
300 if(isset($_GET['member_status']) && $_GET['member_status']) $args['status'] = (array) sanitize_text_field($_GET['member_status']);
301 if(isset($_GET['phrase_package']) && $_GET['phrase_package']) $args['package'] = (array) sanitize_text_field($_GET['phrase_package']);
302 if(isset($_GET['langid']) && $_GET['langid']) $args['langid'] = intval($_GET['langid']);
303 if(isset($_GET['userid']) && $_GET['userid'] != 0) $args['userid'] = intval($_GET['userid']);
304 $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
305
306 $args['offset'] = ($paged - 1) * get_option('wpforo_count_per_page');
307 $args['row_count'] = get_option('wpforo_count_per_page');
308
309
310 if($varname == 'reply'){
311 $func = 'get_replies';
312 }else{
313 $func = 'get_'.$varname.'s';
314 }
315
316 $items_count = 0;
317 if( $varname == 'member' && !isset($_GET['member_status']) ){
318 $args['status'] = array('active');
319 }
320
321 if($varname == 'ad') {
322 $_call_ = WPF_AD();
323 }else{
324 $_call_ = WPF()->$varname;
325 }
326 $items = $_call_->$func($args, $items_count);
327
328 if(isset($args['include']) && empty($args['include'])){
329 $items_count = 0;
330 $items = array();
331 }
332 $pages_count = ceil($items_count/get_option('wpforo_count_per_page')); ?>
333
334 <script type="text/javascript">
335 function wpforo_check_uncheck_all(status){
336 var inpts = document.getElementById("items").getElementsByTagName("input");
337 for (var i = 0; i < inpts.length; i++) {
338 if (inpts[i].type == 'checkbox') {
339 inpts[i].checked = status;
340 }
341 }
342 }
343
344 function wpforo_doaction(){
345 var action = document.items.action.value;
346 var action2 = document.items.action2.value;
347 if(action != -1 || action2 != -1){
348 var inpts = document.getElementById("items").getElementsByTagName("input");
349
350 var j = 0;
351 var ids_arr = new Array();
352 for (var i = 0; i < inpts.length; i++) {
353 if (inpts[i].type == 'checkbox' && inpts[i].checked == true && inpts[i].value != 'on') {
354 ids_arr[j] = inpts[i].value;
355 j++
356 }
357 }
358 if(ids_arr.length != 0){
359 document.items.ids.value = ids_arr.join();
360 document.items.submit();
361 }
362 }
363 }
364 </script>
365
366
367 <div class="wpforo-search-box">
368 <form name="search" id="srch" action="<?php echo admin_url( 'admin.php' ) ?>" method="GET">
369 <?php if( !empty($_GET) ) : ?>
370 <?php foreach ($_GET as $get_key => $get) : ?>
371 <input type="hidden" name="<?php echo $get_key ?>" value="<?php echo $get ?>"/>
372 <?php endforeach ?>
373 <?php endif ?>
374 <label class="screen-reader-text" for="post-search-input">Search <?php echo esc_attr(ucfirst($varname)); ?>s:</label>
375 <input type="search" id="post-search-input" name="s" value="<?php echo ( isset($_GET['s']) ? esc_attr(sanitize_text_field($_GET['s'])) : '' ) ?>">
376 <input type="submit" name="" id="search-submit" class="button" value="Search <?php echo esc_attr(ucfirst($varname)); ?>s">
377 </form>
378 </div>
379 <?php if($varname == 'moderation'): ?>
380 <?php
381 $total = WPF()->post->get_count();
382 $up_total = (!isset($_GET['filter_by_status']) || (isset($_GET['filter_by_status']) && $_GET['filter_by_status'] == 1 )) ? $items_count : ($total - $items_count);
383 $ap_total = ((isset($_GET['filter_by_status']) && $_GET['filter_by_status'] == 0 )) ? $items_count : ($total - $items_count);
384 ?>
385 <ul class="subsubsub" style="margin:-25px 0px 5px 0px;">
386 <li><a href="<?php echo admin_url( 'admin.php' ) ?>?page=wpforo-moderations&filter_by_status=1" <?php if( !isset($_GET['filter_by_status']) || (isset($_GET['filter_by_status']) && $_GET['filter_by_status'] == 1 ) ) echo 'class="current"' ?>><?php _e('Unapproved', 'wpforo'); ?> <span class="count">(<?php echo intval($up_total); ?>)</span></a> |</li>
387 <li><a href="<?php echo admin_url( 'admin.php' ) ?>?page=wpforo-moderations&filter_by_status=0" <?php if( (isset($_GET['filter_by_status']) && $_GET['filter_by_status'] == 0 ) ) echo 'class="current"' ?>><?php _e('Published', 'wpforo'); ?> <span class="count">(<?php echo intval($ap_total); ?>)</span></a></li>
388 </ul>
389 <?php elseif($varname == 'member' && (!isset($_GET['groupid']) || (isset($_GET['groupid']) && !$_GET['groupid'])) ): ?>
390 <?php
391 $total = WPF()->member->get_count();
392 $up_total = (!isset($_GET['member_status']) || (isset($_GET['member_status']) && $_GET['member_status'] == 'active' )) ? $items_count : ($total - $items_count);
393 $ap_total = ((isset($_GET['member_status']) && $_GET['member_status'] == 'banned' )) ? $items_count : ($total - $items_count);
394 ?>
395 <ul class="subsubsub" style="margin:-25px 0px 5px 0px;">
396 <li><a href="<?php echo admin_url( 'admin.php' ) ?>?page=wpforo-members&member_status=active" <?php if( !isset($_GET['member_status']) || (isset($_GET['member_status']) && $_GET['member_status'] == 'active' ) ) echo 'class="current"' ?>><?php _e('Active', 'wpforo'); ?> <span class="count">(<?php echo intval($up_total); ?>)</span></a> |</li>
397 <li><a href="<?php echo admin_url( 'admin.php' ) ?>?page=wpforo-members&member_status=banned" <?php if( (isset($_GET['member_status']) && $_GET['member_status'] == 'banned' ) ) echo 'class="current"' ?>><?php _e('Banned', 'wpforo'); ?> <span class="count">(<?php echo intval($ap_total); ?>)</span></a></li>
398 </ul>
399 <?php endif; ?>
400
401 <form name="items" id="items" action="<?php echo admin_url( 'admin.php' ) ?>" method="GET">
402 <?php wp_nonce_field( 'bulk_action_'.$varname ); ?>
403 <input type="hidden" name="ids" />
404 <input type="hidden" name="page" value="wpforo-<?php echo esc_attr($varname); ?>s"/>
405
406 <div class="tablenav top">
407
408 <div class="alignleft actions">
409 <select name="action">
410 <option value="-1" selected="selected"><?php _e('Bulk Actions', 'wpforo'); ?></option>
411 <?php foreach( $bulk_actions as $bulk_action ) : ?>
412 <option value="<?php echo $bulk_action ?>"><?php _e($bulk_action, 'wpforo'); ?></option>
413 <?php endforeach ?>
414 </select>
415 <input type="button" onclick="wpforo_doaction()" id="doaction" class="button action" value="<?php _e('Apply', 'wpforo'); ?>">
416 </div>
417 <div class="alignleft actions">
418
419 <?php if(!empty($filter_fields)) : ?>
420 <?php foreach($filter_fields as $filter_field) : ?>
421 <?php if($filter_field == 'forumid'){ ?>
422
423 <select name="<?php echo esc_attr($filter_field) ?>">
424 <option value="0"><?php _e('Show all forums', 'wpforo'); ?></option>
425 <?php WPF()->forum->tree('select_box', true, ( !empty($_GET['forumid']) ? $_GET['forumid'] : 0 ) ); ?>
426 </select>
427
428 <?php }elseif($filter_field == 'langid'){ ?>
429
430 <select name="<?php echo esc_attr($filter_field) ?>">
431 <?php WPF()->phrase->show_lang_list() ?>
432 </select>
433
434 <?php }elseif($filter_field == 'groupid'){ ?>
435
436 <select name="<?php echo esc_attr($filter_field) ?>">
437 <option value="0"><?php _e('filter by group', 'wpforo') ?></option>
438 <?php foreach(WPF()->usergroup->get_usergroups() as $group) : ?>
439 <?php if( $group['groupid'] != 4 ) : ?>
440 <option value="<?php echo esc_attr($group['groupid']) ?>" <?php echo ( isset($_GET['groupid']) && $_GET['groupid'] == $group['groupid'] ? 'selected' : '' ) ?>><?php echo esc_html($group['name']) ?></option>
441 <?php endif ?>
442 <?php endforeach; ?>
443 </select>
444
445 <?php }elseif( $varname == 'member' && $filter_field == 'status' ){
446 $sql = "SELECT DISTINCT `status` as statuses FROM `".WPF()->tables->profiles."`";
447 if( $statuses = WPF()->db->get_col($sql) ){ ?>
448 <select name="member_status">
449 <option value="0"><?php _e('filter by status', 'wpforo') ?></option>
450
451 <?php foreach( $statuses as $status ) : ?>
452 <option value="<?php echo esc_attr($status) ?>" <?php echo ( isset($_GET['member_status']) && $_GET['member_status'] == $status ? 'selected' : '' ) ?>><?php echo esc_html($status) ?></option>
453 <?php endforeach; ?>
454
455 </select>
456
457 <?php
458 }
459
460 }elseif( $varname == 'phrase' && $filter_field == 'package' ){
461
462 $sql = "SELECT DISTINCT `package` as packages FROM `".WPF()->tables->phrases."`";
463 if( $packages = WPF()->db->get_col($sql) ){ ?>
464 <select name="phrase_package">
465 <option value="0"><?php _e('filter by package', 'wpforo') ?></option>
466
467 <?php foreach( $packages as $package ) : ?>
468 <option value="<?php echo esc_attr($package) ?>" <?php echo ( isset($_GET['phrase_package']) && $_GET['phrase_package'] == $package ? 'selected' : '' ) ?>><?php echo esc_html($package) ?></option>
469 <?php endforeach; ?>
470
471 </select>
472
473 <?php
474 }
475
476 }elseif( $varname == 'moderation' ){
477 $filter_by_status = intval( ( isset($_GET['filter_by_status']) ? $_GET['filter_by_status'] : 1 ) );
478
479 if($filter_field == 'status'){
480 if( $statuses = WPF()->moderation->post_statuses ) : ?>
481 <select name="filter_by_status">
482
483 <?php foreach ($statuses as $key => $status) : ?>
484 <option value="<?php echo esc_attr($key) ?>" <?php echo ( $filter_by_status == $key ? 'selected' : '' ) ?>><?php echo esc_html( $status ) ?></option>
485 <?php endforeach; ?>
486
487 </select>
488 <?php
489 endif;
490 }elseif($filter_field == 'userid'){
491 $sql = "SELECT DISTINCT `userid` FROM `".WPF()->tables->posts."` WHERE `status` = $filter_by_status";
492 if( $userids = WPF()->db->get_col($sql) ) : ?>
493 <select name="filter_by_userid">
494 <option value="0"><?php _e('filter by user', 'wpforo') ?></option>
495
496 <?php foreach ($userids as $userid) : $user = WPF()->member->get_member($userid); ?>
497 <option value="<?php echo esc_attr($userid) ?>" <?php echo ( isset($_GET['filter_by_userid']) && $_GET['filter_by_userid'] == $userid ? 'selected' : '' ) ?>><?php echo esc_html( wpforo_make_dname($user['display_name'], $user['user_nicename']) ) ?></option>
498 <?php endforeach; ?>
499
500 </select>
501 <?php
502 endif;
503 }
504
505 } ?>
506
507 <?php endforeach; ?>
508
509 <input type="submit" name="" id="post-query-submit" class="button" value="Filter">
510
511 <?php endif; ?>
512
513 </div>
514 <div class="tablenav-pages <?php echo ((($items_count/get_option('wpforo_count_per_page')) <= 1) ? 'one-page' : '') ?>"><span class="displaying-num"><?php echo intval($items_count) ?> <?php _e('item', 'wpforo') ?></span>
515 <span class="pagination-links">
516 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 2 ) : ?>
517 <span class="tablenav-pages-navspan">&laquo;</span>
518 <?php else : ?>
519 <a class="first-page" title="Go to the first page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>">&laquo;</a>
520 <?php endif ?>
521 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 1 ) : ?>
522 <span class="tablenav-pages-navspan"></span>
523 <?php else : ?>
524 <a class="prev-page" title="Go to the previous page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo !isset($_GET['paged']) || $_GET['paged'] <= 1 ? 1 : $_GET['paged'] - 1 ?>"></a>
525 <?php endif ?>
526
527 <span class="paging-input"><input class="current-page" title="Current page" type="text" name="paged" value="<?php echo isset($_GET['paged']) ? intval($_GET['paged']) : 1 ?>" size="1"> of <span class="total-pages"><?php echo intval($pages_count) ?></span></span>
528
529 <?php if( isset($_GET['paged']) && $_GET['paged'] >= $pages_count ) : ?>
530 <span class="tablenav-pages-navspan"></span>
531 <?php else : ?>
532 <a class="next-page" title="Go to the next page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo (isset($_GET['paged']) ? ( $_GET['paged'] >= $pages_count ? intval($pages_count) : intval($_GET['paged']) + 1 ) : (!isset($_GET['paged']) ? 2 : intval($_GET['paged']) + 1)) ?>"></a>
533 <?php endif ?>
534 <?php if( (isset($_GET['paged']) && $_GET['paged'] >= $pages_count - 1) || 1 >= $pages_count - 1 ) : ?>
535 <span class="tablenav-pages-navspan">&raquo;</span>
536 <?php else : ?>
537 <a class="last-page" title="Go to the last page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo intval($pages_count) ?>">&raquo;</a></span>
538 <?php endif ?>
539 </div> <br class="clear">
540
541 </div>
542
543 <table class="wp-list-table widefat fixed pages" cellspacing="0">
544 <thead>
545 <tr>
546 <th scope="col" id="cb" class="manage-column column-cb check-column" style="vertical-align:middle; padding:17px 0px 10px 5px;">
547 <label class="screen-reader-text" for="cb-select-all-1"><?php _e('Select All', 'wpforo') ?></label><input id="cb-select-all-1" type="checkbox" onclick="wpforo_check_uncheck_all(this.checked)">
548 </th>
549 <?php foreach($fields as $key => $field) : ?>
550 <th scope="col" <?php if($key===0) echo ' style="width:30%"' ?> id="<?php echo esc_attr(sanitize_text_field($field)) ?>" class="manage-column column-<?php echo esc_attr(sanitize_text_field($field)) ?> <?php echo isset($_GET['order']) ? ($_GET['orderby'] == $field ? 'sorted ' : '') . $_GET['order'] : 'sortable asc' ?>">
551 <a href="<?php echo esc_url(preg_replace('#(\&*orderby\=[^\&]*\&*order\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&orderby=<?php echo esc_attr(sanitize_text_field($field)) ?>&order=<?php echo isset($_GET['order']) ? ( $_GET['order'] == 'asc' ? 'desc' : 'asc' ) : 'asc' ?>">
552 <span><?php if($field == 'is_first_post'){ _e('Type', 'wpforo'); }else{ echo sanitize_text_field(str_replace('_', ' ', wpforo_phrase( $field, false ))); } ?></span>
553 <span class="sorting-indicator"></span>
554 </a>
555 </th>
556 <?php endforeach; ?>
557 </tr>
558 </thead>
559
560 <tfoot>
561 <tr>
562 <th scope="col" class="manage-column column-cb check-column" style="vertical-align:middle; padding:17px 0px 10px 5px;">
563 <label class="screen-reader-text" for="cb-select-all-2"><?php _e('Select All', 'wpforo') ?></label><input id="cb-select-all-2" type="checkbox" onclick="wpforo_check_uncheck_all(this.checked)">
564 </th>
565 <?php foreach($fields as $key => $field) : ?>
566 <th scope="col" <?php if($key===0) echo ' style="width:30%"' ?> id="<?php echo esc_attr($field) ?>" class="manage-column column-<?php echo esc_attr($field) ?> <?php echo isset($_GET['order']) ? ($_GET['orderby'] == $field ? 'sorted ' : '') . $_GET['order'] : 'sortable asc' ?>">
567 <a href="<?php echo admin_url( 'admin.php?page=wpforo-' . esc_attr(sanitize_text_field($varname)) . 's&orderby=' . esc_attr(sanitize_text_field($field)) . '&order=' . ( isset($_GET['order']) ? ( $_GET['order'] == 'asc' ? 'desc' : 'asc' ) : 'asc' ) ) ?>">
568 <span><?php if($field == 'is_first_post'){ _e('Type', 'wpforo'); }else{ echo sanitize_text_field(str_replace('_', ' ', wpforo_phrase( $field, false ))); } ?></span>
569 <span class="sorting-indicator"></span>
570 </a>
571 </th>
572 <?php endforeach; ?>
573 </tr>
574 </tfoot>
575
576 <tbody id="the-list">
577
578 <?php
579 if(!empty($items)) :
580
581 if(!empty($items)) :
582 foreach($items as $key => $item) : ?>
583
584 <tr id="post-<?php echo esc_attr($item[$primary_key]) ?>" class="post-<?php echo esc_attr($item[$primary_key]) ?> type-page status-publish hentry <?php echo ($key % 2 == 0) ? 'alternate' : '' ?> iedit author-self" valign="top">
585 <th scope="row" class="check-column">
586 <label class="screen-reader-text" for="cb-select-<?php echo esc_attr($item[$primary_key]) ?>">
587 Select <?php echo esc_html(ucfirst($varname)) ?>
588 </label>
589 <input id="cb-select-<?php echo esc_attr($item[$primary_key]) ?>" type="checkbox" value="<?php echo esc_attr($item[$primary_key]) ?>">
590 <div class="locked-indicator"></div>
591 </th>
592 <?php $i = 0; foreach($fields as $field) :
593 if($i == 0) : ?>
594 <?php
595 if($varname == 'member'){
596 $url = $url_user = admin_url( 'user-edit.php?user_id=' . intval($item[$primary_key]));
597 $url_profile = WPF()->$varname->get_profile_url($item[$primary_key], 'account');
598 }elseif($varname == 'moderation'){
599 $url = WPF()->post->get_post_url($item[$primary_key]);
600 }else{
601 $url = admin_url( 'admin.php?page=wpforo-'. $varname .'s&id='.$item[$primary_key] .'&action=edit' );
602 }
603 ?>
604 <td class="post-<?php echo esc_attr($item[$field]) ?> page-<?php echo esc_attr($item[$field]) ?> column-<?php echo esc_attr($item[$field]) ?>">
605 <strong>
606 <a class="row-<?php echo esc_attr($item[$field]) ?>" href="<?php echo esc_url($url) ?>" title="Edit “<?php echo esc_attr($varname) ?>”">
607 <?php
608 if($varname == 'forum'){
609 $depth = 0;
610 WPF()->forum->count_depth($item[$primary_key], $depth);
611 echo str_repeat( '', $depth);
612 }
613 wpfo($item[$field], true, 'esc_html');
614 ?>
615 </a>
616 </strong>
617 <div class="row-actions">
618 <?php foreach($actions as $act_key => $action) : ?>
619 <?php switch($action) :
620 case 'edit': ?>
621 <span class="edit"><a href="<?php echo esc_url($url); ?>" title="Edit this item"><?php _e('edit', 'wpforo'); ?></a></span>
622 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
623 <?php break;
624 case 'edit_user': ?>
625 <span class="edit"><a href="<?php echo esc_url($url_user); ?>"><?php _e('edit user', 'wpforo'); ?></a></span>
626 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
627 <?php break;
628 case 'edit_profile': ?>
629 <span class="edit"><a href="<?php echo esc_url($url_profile); ?>"><?php _e('edit profile', 'wpforo'); ?></a></span>
630 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
631 <?php break;
632 case 'ban': if($item['userid'] == WPF()->current_userid) break; ?>
633 <?php $ban_url = wp_nonce_url( admin_url( 'admin.php?page=wpforo-' . esc_attr(sanitize_text_field($varname)) . 's&id=' . intval($item[$primary_key]) . '&action=' . ($item['status'] == 'banned' ? 'unban' : 'ban') ), 'wpforo_admin_table_action_ban' ); ?>
634 <span class="trash"><a class="submitban" title="<?php echo ($item['status'] == 'banned' ? __('unban user', 'wpforo') : __('ban user', 'wpforo') ) ?>" onclick="return confirm('<?php echo ($item['status'] == 'banned' ? __('Are you sure, you want to unban this user?', 'wpforo') : __('Are you sure, you want to ban this user?', 'wpforo') ) ?>');" href="<?php echo esc_url($ban_url); ?>"><?php echo ($item['status'] == 'banned' ? __('unban user', 'wpforo') : __('ban user', 'wpforo') ) ?></a></span>
635 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
636 <?php break;
637 case 'delete': ?>
638 <?php $delete_url = wp_nonce_url( admin_url( 'admin.php?page=wpforo-' . esc_attr(sanitize_text_field($varname)) . 's&id=' . intval($item[$primary_key]) . '&action=del' ), 'wpforo_admin_table_action_delete' ); ?>
639 <span class="trash"><a class="submitdelete" title="<?php _e('Delete this item', 'wpforo') ?>" onclick="return confirm('<?php _e('Are you sure you want to DELETE this item?', 'wpforo') ?>');" href="<?php echo esc_url($delete_url); ?>"><?php _e('delete', 'wpforo'); ?></a></span>
640 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
641 <?php break;
642 case 'approve': ?>
643 <?php $approve_url = wp_nonce_url( admin_url( 'admin.php?page=wpforo-' . esc_attr(sanitize_text_field($varname)) . 's&id=' . intval($item[$primary_key]) . '&action='.( !$item['status'] ? 'un' : '' ).'approve' ), 'wpforo_admin_table_action_approve' ); ?>
644 <span class="vim-u"><a class="vim-u" title="<?php echo ( !$item['status'] ? __('unapprove this item', 'wpforo') : __('Approve this item', 'wpforo') ) ?>" href="<?php echo esc_url($approve_url); ?>"><?php echo ( !$item['status'] ? __('unapprove', 'wpforo') : __('approve', 'wpforo') ); ?></a></span>
645 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
646 <?php break;
647 case 'user_delete': if($item['userid'] == WPF()->current_userid) break; ?>
648 <?php $delete_url = wp_nonce_url( "users.php?action=delete&user=".intval($item[$primary_key]), 'bulk-users' ) ?>
649 <span class="trash"><a class="submitdelete" title="<?php _e('Delete this item', 'wpforo') ?>" onclick="return confirm('<?php _e('Are you sure you want to DELETE this item?', 'wpforo') ?>');" href="<?php echo esc_url($delete_url); ?>"><?php _e('delete', 'wpforo'); ?></a></span>
650 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
651 <?php break;
652 case 'view': ?>
653 <?php
654 if( $varname == 'member' ){
655 $fn_name = "get_profile_url";
656 $item_id = $item['ID'];
657 }elseif($varname == 'moderation'){
658 $fn_name = "get_view_url";
659 $item_id = $item['postid'];
660 }else{
661 $fn_name = "get_".$varname."_url";
662 $item_id = $item[$varname.'id'];
663 }
664 ?>
665 <span class="view">
666 <a href="<?php echo esc_url(WPF()->$varname->$fn_name($item_id)) ?>" target="_blank" title="<?php echo esc_attr( __('view', 'wpforo')); ?> “<?php echo esc_attr($varname) ?>”" rel="permalink">
667 <?php _e('view', 'wpforo'); ?>
668 </a>
669 </span>
670 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
671 <?php break;
672 endswitch ?>
673 <?php endforeach ?>
674 </div>
675 </td>
676 <?php else : ?>
677 <td class="<?php echo esc_attr($item[$field]) ?> column-<?php echo esc_attr($item[$field]) ?>">
678 <?php if($field == 'forumid') : ?>
679 <?php $data = WPF()->forum->get_forum($item[$field]); echo esc_html($data['title']); ?>
680 <?php elseif($field == 'userid') : ?>
681 <?php if(isset($item['name'])): ?>
682 <?php $data = wpforo_member($item); echo esc_html(wpforo_make_dname($data['display_name'], $data['user_nicename'])); ?>
683 <?php else: ?>
684 <?php $data = WPF()->member->get_member($item[$field]); echo esc_html(wpforo_make_dname($data['display_name'], $data['user_nicename'])); ?>
685 <?php endif; ?>
686 <?php elseif($field == 'groupid') : ?>
687 <?php $data = WPF()->usergroup->get_usergroup($item[$field]); echo esc_html($data['name']); ?>
688 <?php elseif($field == 'rank') : ?>
689 <div class="author-rating"><div class="bar wpfw-<?php echo esc_attr(WPF()->member->rating_level($item['posts'])); ?> wpfbg-4"></div></div>
690 <?php elseif($field == 'is_first_post') : ?>
691 <b><?php echo strtoupper( ( $item[$field] ? __('Topic', 'wpforo') : __('Post', 'wpforo') ) ) ?></b>
692 <?php else : ?>
693 <?php wpfo($item[$field], true, 'esc_html') ?>
694 <?php endif; ?>
695 </td>
696
697 <?php endif; $i++; ?>
698
699 <?php endforeach; ?>
700 </tr>
701 <?php endforeach; ?>
702 <?php endif; ?>
703 <?php else : ?>
704 <tr class="no-items"><td class="colspanchange" colspan="7"><?php _e('No items found', 'wpforo') ?></td></tr>
705 <?php endif; ?>
706 </tbody>
707 </table>
708
709 <div class="tablenav bottom">
710 <div class="alignleft actions">
711 <select name="action2">
712 <option value="-1" selected="selected"><?php _e('Bulk Actions', 'wpforo') ?></option>
713 <?php foreach( $bulk_actions as $bulk_action ) : ?>
714 <option value="<?php echo $bulk_action ?>"><?php _e($bulk_action, 'wpforo'); ?></option>
715 <?php endforeach ?>
716 </select>
717 <input type="button" onclick="wpforo_doaction()" id="doaction2" class="button action" value="Apply">
718 </div>
719 <div class="alignleft actions"></div>
720 <div class="tablenav-pages <?php echo (($items_count/get_option('wpforo_count_per_page') <= 1) ? 'one-page' : '') ?>"><span class="displaying-num"><?php echo intval($items_count) ?> <?php _e('item', 'wpforo') ?></span>
721 <span class="pagination-links">
722 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 2 ) : ?>
723 <span class="tablenav-pages-navspan">&laquo;</span>
724 <?php else : ?>
725 <a class="first-page" title="Go to the first page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>">&laquo;</a>
726 <?php endif ?>
727 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 1 ) : ?>
728 <span class="tablenav-pages-navspan"></span>
729 <?php else : ?>
730 <a class="prev-page" title="Go to the previous page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo !isset($_GET['paged']) || $_GET['paged'] <= 1 ? 1 : intval($_GET['paged']) - 1 ?>"></a>
731 <?php endif ?>
732
733 <span class="paging-input"><input class="current-page" title="Current page" type="text" name="paged" value="<?php echo isset($_GET['paged']) ? intval($_GET['paged']) : 1 ?>" size="1"> of <span class="total-pages"><?php echo intval($pages_count) ?></span></span>
734
735 <?php if( isset($_GET['paged']) && $_GET['paged'] >= $pages_count ) : ?>
736 <span class="tablenav-pages-navspan"></span>
737 <?php else : ?>
738 <a class="next-page" title="Go to the next page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo (isset($_GET['paged']) ? ( $_GET['paged'] >= $pages_count ? intval($pages_count) : intval($_GET['paged']) + 1 ) : (!isset($_GET['paged']) ? 2 : intval($_GET['paged']) + 1)) ?>"></a>
739 <?php endif ?>
740 <?php if( (isset($_GET['paged']) && $_GET['paged'] >= $pages_count - 1) || 1 >= $pages_count - 1 ) : ?>
741 <span class="tablenav-pages-navspan">&raquo;</span>
742 <?php else : ?>
743 <a class="last-page" title="Go to the last page" href="<?php echo esc_url(preg_replace('#(\&*paged\=[^\&]*)#is', '', wpforo_get_request_uri())); ?>&paged=<?php echo intval($pages_count) ?>">&raquo;</a></span>
744 <?php endif ?>
745 </div> <br class="clear">
746 </div>
747 </form>
748 <?php endif; ?>
749 <?php
750 }
751
752 function wpforo_phrase($key, $echo = TRUE, $format = 'first-upper'){
753 $locale = WPF()->locale;
754 $phrase = (isset(WPF()->phrase->phrases[addslashes(strtolower($key))])) ? WPF()->phrase->phrases[addslashes(strtolower($key))] : $key;
755 if( 'en_US' != $locale ){
756 $native = $phrase;
757 if (strtolower($key) == strtolower($phrase)) {
758 $phrase = __($key, 'wpforo');
759 if (strtolower($key) == strtolower($phrase)) {
760 $key = strtolower($key);
761 $phrase = __($key, 'wpforo');
762 if (strtolower($key) == strtolower($phrase)) {
763 $phrase = __(ucfirst($key), 'wpforo');
764 if (strtolower($key) == strtolower($phrase)) {
765 $phrase = __($native, 'wpforo'); //Try all, if no result pass the original text to treanslation again.
766 }
767 }
768 }
769 }
770 }
771
772 if( $format == 'first-upper' ){
773 if( 'en_US' != $locale && function_exists('mb_strlen') && mb_strlen($phrase) != strlen($phrase) ) {
774 $phrase = mb_strtoupper(mb_substr($phrase, 0, 1)) . mb_substr($phrase, 1);
775 }
776 else{
777 $phrase = ucfirst($phrase);
778 }
779 }
780 elseif( $format == 'upper' ){
781 if(function_exists('mb_strtoupper')){
782 $phrase = mb_strtoupper($phrase);
783 }
784 else{
785 $phrase = strtoupper($phrase);
786 }
787 }
788 elseif( $format == 'lower' ){
789 if(function_exists('mb_strtolower')){
790 $phrase = mb_strtolower($phrase);
791 }
792 else{
793 $phrase = strtolower($phrase);
794 }
795 }
796
797 $phrase = str_replace('{number}', '', $phrase);
798
799 if($echo){
800 echo $phrase;
801 }else{
802 return $phrase;
803 }
804 }
805
806 function wpforo_screen_option(){ ?>
807
808 <div id="screen-meta" class="metabox-prefs" style="display: none; ">
809 <div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="Screen Options Tab" style="display: none; ">
810 <form id="adv-settings" action="" method="POST">
811 <h5><?php _e('Show on screen', 'wpforo') ?></h5>
812
813 <div class="screen-options">
814 <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wpforo_screen_option[value]" id="edit_post_per_page" maxlength="3" value="<?php echo intval(get_option('wpforo_count_per_page')) ?>">
815 <label for="edit_post_per_page"><?php _e('Items', 'wpforo') ?></label>
816 <input type="submit" name="screen-options-apply" id="screen-options-apply" class="button" value="<?php _e('Apply', 'wpforo') ?>">
817 </div>
818 </form>
819 </div>
820 </div>
821
822 <div id="screen-meta-links">
823 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle" style="">
824 <a href="#screen-options-wrap" id="show-settings-link" class="show-settings screen-meta-active" aria-controls="screen-options-wrap" aria-expanded="true">
825 <?php _e('Screen Options', 'wpforo') ?>
826 </a>
827 </div>
828 </div>
829
830 <?php
831 }
832
833
834 function wpforo_text( $text, $length = 0, $echo = true, $strip_tags = true, $strip_urls = true, $strip_shortcodes = true ){
835 $text = str_replace('</p>', '</p> ', $text);
836 if($strip_urls) $text = preg_replace('#(?:[^\'\"]|^)(https?://[^\s\'\"<>]+)(?:[^\'\"]|$)#isu', '', $text);
837 if($strip_tags) $text = strip_tags($text);
838 if($strip_shortcodes){
839 $text = preg_replace('#\[attach[^\[\]]*\][^\[\]]*\[/attach\]#isu', '', $text);
840 $text = strip_shortcodes( $text );
841 }
842 $text = apply_filters('wpforo_text', $text);
843
844 if(!$length){
845 if($echo){
846 echo trim($text);
847 return '';
848 }else{
849 return trim($text);
850 }
851 }
852 if(function_exists('mb_substr')){
853 if($echo){
854 echo trim( mb_substr( $text, 0, $length, get_option('blog_charset') ) . ( ( function_exists('mb_strlen') ? mb_strlen( $text, get_option('blog_charset') ) : strlen($text) ) > $length ? '...' : '' ) );
855 }else{
856 return trim( mb_substr( $text, 0, $length, get_option('blog_charset') ) . ( ( function_exists('mb_strlen') ? mb_strlen( $text, get_option('blog_charset') ) : strlen($text) ) > $length ? '...' : '' ) );
857 }
858 }else{
859 if($echo){
860 echo trim( substr( $text, 0, $length ) . ( strlen($text) > $length ? '...' : '' ) );
861 }else{
862 return trim( substr( $text, 0, $length ) . ( strlen($text) > $length ? '...' : '' ) );
863 }
864 }
865 }
866
867 function wpforo_admin_options_tabs( $tabs, $current = 'general', $subtab = FALSE, $sub_current = 'general' ) {
868 if(!empty($tabs)){
869 $class_attr = $subtab ? 'vert_tab' : '';
870 echo '<h2 class="nav-tab-wrapper ' . esc_attr($class_attr) . '">';
871 foreach( $tabs as $tab => $name ){
872 $class = ( $tab == $current || ($subtab && $tab == $sub_current ) ) ? ' nav-tab-active' : '';
873 $sub = $subtab ? '&subtab='.esc_attr($tab) : '';
874 $class = esc_attr($class);
875 $class_attr = $class_attr;
876 $current = esc_attr($current);
877 $tab = esc_attr($tab);
878 $sub = esc_attr($sub);
879 $name = esc_html($name);
880 echo "<a class='nav-tab$class $class_attr' href='?page=wpforo-settings&tab=". ($subtab ? $current : $tab ) ."$sub'>$name</a>";
881 }
882 echo '</h2>';
883 }
884 }
885
886 function wpforo_admin_tools_tabs( $tabs, $current = 'antispam', $subtab = FALSE, $sub_current = 'antispam' ) {
887 if(!empty($tabs)){
888 $class_attr = $subtab ? 'vert_tab' : '';
889 echo '<h2 class="nav-tab-wrapper ' . esc_attr($class_attr) . '">';
890 foreach( $tabs as $tab => $name ){
891 $class = ( $tab == $current || ($subtab && $tab == $sub_current ) ) ? ' nav-tab-active' : '';
892 $sub = $subtab ? '&subtab='.esc_attr($tab) : '';
893 $class = esc_attr($class);
894 $class_attr = $class_attr;
895 $current = esc_attr($current);
896 $tab = esc_attr($tab);
897 $sub = esc_attr($sub);
898 $name = esc_html($name);
899 echo "<a class='nav-tab$class $class_attr' href='?page=wpforo-tools&tab=". ($subtab ? $current : $tab ) ."$sub' style='float:left'>$name</a>";
900 }
901 echo '</h2>';
902 }
903 }
904
905 function wpforo_content_filter( $content ){
906 $content = apply_filters('wpforo_body_text_filter', $content);
907 $content = preg_replace('#([^\'\"]|^)(https?://[^\s\'\"<>]+\.(?:jpg|jpeg|png|gif|ico|svg|bmp|tiff))([^\'\"]|$)#isu', '$1 <a class="wpforo-auto-embeded-link" href="$2" target="_blank"><img class="wpforo-auto-embeded-image" src="$2"/></a> $3', $content);
908 $content = preg_replace('#([^\'\"]|^)(https?://[^\s\'\"<>]+)([^\'\"]|$)#isu', '$1 <a class="wpforo-auto-embeded-link" href="$2" target="_blank">$2</a> $3', $content);
909 if(preg_match_all('#<pre([^<>]*)>(.*?class=[\'"]wpforo-auto-embeded[^\'"]*[\'"].*?)</pre>#isu', $content, $matches, PREG_SET_ORDER)){
910 foreach($matches as $match){
911 $match[2] = preg_replace('#<img[^<>]*class=[\'"]wpforo-auto-embeded-image[\'"][^<>]*src=[\'"]([^\'"]*)[\'"][^<>]*>#isu', '$1', $match[2]);
912 $match[2] = preg_replace('#<a[^<>]*class=[\'"]wpforo-auto-embeded-link[\'"][^<>]*href=[\'"]([^\'"]*)[\'"][^<>]*>.*?</a>#isu', '$1', $match[2]);
913 $content = str_replace($match[0], '<pre'.$match[1].'>'.$match[2].'</pre>', $content);
914 }
915 }
916 $content = preg_replace('#(<a[^<>]*>[^<>]*)<a[^<>]*class=[\'"]wpforo-auto-embeded-link[\'"][^<>]*href=[\'"]([^\'"]*)[\'"][^<>]*>[^<>]*</a>([^<>]*</a>)#isu', '$1$2$3', $content);
917 $content = apply_filters('wpforo_content_filter', $content);
918 return wpautop($content);
919 }
920
921 function wpforo_remove_links( $content ){
922 return preg_replace('#([^\'\"]|^)(https?://[^\s\'\"<>]+)([^\'\"]|$)#isu', '$1 [' . wpforo_phrase('removed link', false) . '] $3', $content);
923 }
924
925 add_filter('wpforo_content_filter', 'wpforo_nofollow_tag', 20);
926 function wpforo_nofollow_tag($content){
927 $content = preg_replace_callback('#<a[^><]*?href=[\'\"]([^\'\"]+)[\'\"][^><]*?>#isu', 'wpforo_nofollow', $content);
928 return $content;
929 }
930 function wpforo_nofollow($match){
931 $link = $match[0];
932 $dofollow = trim(WPF()->tools_misc['dofollow']);
933 $dofollow = array_filter(array_map("trim", explode("\n", $dofollow)));
934 $parse = parse_url( wpforo_get_request_uri() );
935 $host = $parse['host'];
936 $main_host = preg_replace('#^.*?([^\.]+?\.[^\.]+?)$#isu', '$1', $host);
937 if( strpos($link, 'rel=') === false && strpos($match[1], $main_host) === false ){
938 $link_url = parse_url($match[1]);
939 if( !(!empty($dofollow) && !empty($link_url['host']) && in_array($link_url['host'], $dofollow)) ){
940 $link = str_replace('>', ' rel="nofollow">', $match[0]);
941 }
942 }
943 return $link;
944 }
945
946 add_action('wp_loaded', 'wpforo_cookie_logs', 10);
947
948 function wpforo_cookie_logs(){
949 if(!wpforo_feature('view-logging') || !WPF()->tools_legal['cookies']) return;
950 $key = ''; $logid = 0; $log = false;
951 $data = WPF()->current_object;
952 if( $data['template'] == 'post' && isset($data['topicid']) && $data['topicid'] ){
953 $logid = $data['topicid'];
954 $key = 'wpf_viewed_topics';
955 }
956 elseif( $data['template'] == 'topic' && isset($data['forumid']) && $data['forumid'] ){
957 $logid = $data['forumid'];
958 $key = 'wpf_viewed_forums';
959 }
960 if( $logid && $key ) {
961 $viwed_ids = wpforo_getcookie( $key, true );
962 if( !$viwed_ids ){
963 $log = true;
964 $viwed_ids = array( $logid );
965 }
966 elseif( is_array($viwed_ids) && !in_array( $logid , $viwed_ids ) ){
967 $log = true;
968 $viwed_ids[] = $logid;
969 }
970 if( $log ){
971 wpforo_setcookie( $key, $viwed_ids, true );
972 }
973 }
974 }
975
976 add_action('wpforo_bottom_hook', 'wpforo_user_logging');
977 function wpforo_user_logging(){
978 $data = WPF()->current_object;
979 if( $data['template'] == 'post' && isset($data['topicid']) && $data['topicid'] ){
980 $current_user_id = get_current_user_id();
981 $current_time = current_time( 'timestamp', 1 );
982
983 if( wpforo_feature('view-logging') && WPF()->tools_legal['cookies'] ){
984 $viwed_ids = wpforo_getcookie( 'wpf_viewed_topics', true );
985 if( empty($viwed_ids) || ( is_array($viwed_ids) && !in_array($data['topicid'] , $viwed_ids ))){
986 WPF()->db->query("UPDATE `".WPF()->tables->topics."` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
987 }
988 }
989 else{
990 if( $current_user_id ){
991 //registered user
992 $view = WPF()->db->get_row("SELECT `vid`, `created` FROM `".WPF()->tables->views."` WHERE `topicid` = " . intval($data['topicid']) ." AND `userid` = " . intval($current_user_id), ARRAY_A);
993 if( !$view['vid'] ){
994 $sql = "INSERT INTO ".WPF()->tables->views."( `userid` , `topicid` , `created` ) VALUES ( '".intval($current_user_id)."', " . intval($data['topicid']) . ", '" . esc_sql($current_time) . "' ) ";
995 WPF()->db->query($sql);
996 WPF()->db->query("UPDATE `".WPF()->tables->topics."` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
997 }else{
998 $sql = "UPDATE ".WPF()->tables->views." SET `created` = " . intval($current_time) . " WHERE `userid` = " . intval($current_user_id) . " AND `topicid` = " . intval($data['topicid']);
999 WPF()->db->query($sql);
1000 if( $current_time - $view['created'] > 86400 ){
1001 WPF()->db->query("UPDATE `".WPF()->tables->topics."` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
1002 }
1003 }
1004 }
1005 }
1006 }
1007 }
1008
1009
1010 add_action( 'init', 'wpforo_setcookie', 10, 2);
1011
1012 function wpforo_setcookie( $key = '', $args = array(), $implode = false ) {
1013 if( !WPF()->tools_legal['cookies'] ) return;
1014 if( !empty($args) && is_array($args) && $implode ) {
1015 $num = count($args);
1016 if( $num > 200 ){ $delta = $num - 200; if( $delta > 0 ) $args = array_slice($args, $delta); }
1017 $value = trim( implode( ',', $args ), ',' );
1018 }
1019 elseif( !empty($args) && is_array($args) && !$implode ){
1020 $value = serialize($args);
1021 }
1022 if( $key && $value ){
1023 @setcookie( $key, $value , time() + 7776000, COOKIEPATH, COOKIE_DOMAIN );
1024 }
1025 }
1026
1027 add_action( 'wp_head', 'wpforo_getcookie' );
1028 function wpforo_getcookie( $key = '', $explode = false ) {
1029 if( !WPF()->tools_legal['cookies'] ) return FALSE;
1030 if( $key ){
1031 if( isset($_COOKIE[$key]) && $_COOKIE[$key] ){
1032 if($explode){
1033 return explode(',', $_COOKIE[$key]);
1034 }
1035 else{
1036 return $_COOKIE[$key];
1037 }
1038 }else{
1039 return FALSE;
1040 }
1041 }
1042 }
1043
1044 //Option value filter and output
1045 function wpfo( $option = '', $echo = true, $esc = 'esc_attr' ){
1046 if( is_string($option) ){
1047 $option = stripslashes( $option );
1048 if( $esc == 'esc_attr' ){
1049 $option = esc_attr( $option );
1050 }
1051 elseif( $esc == 'esc_html' ){
1052 $option = esc_html( $option );
1053 }
1054 elseif( $esc == 'esc_url' ){
1055 $option = esc_url( $option );
1056 }
1057 elseif( $esc == 'esc_textarea' ){
1058 $option = esc_textarea( $option );
1059 }
1060 }
1061
1062 if( $echo ){
1063 echo $option;
1064 }
1065 else{
1066 return $option;
1067 }
1068 }
1069
1070 //Option maker for checkbox, radio and select
1071 function wpfo_check( $option = '', $value = '', $type = 'checked' , $echo = true ){
1072 $option = (isset($option) && isset($value) && $option == $value ) ? $type : '';
1073 if( $echo ){
1074 echo $option;
1075 }
1076 else{
1077 return $option;
1078 }
1079 }
1080
1081 /**
1082 * Validates values of requested array keys.
1083 *
1084 * @param array $array
1085 * @param null|string $a First key of $array
1086 * @param null|string $b Second key of $array
1087 * @param null|string $c Third key of $array
1088 *
1089 * @return bool|mixed
1090 */
1091 function wpfval( $array, $a = NULL, $b = NULL, $c = NULL ){
1092 if($a || $a === 0){
1093 if( is_array($array) && array_key_exists($a, $array) && ($array[$a] || $array[$a] === 0) ){
1094 if($b || $b === 0){
1095 if( is_array($array[$a]) && array_key_exists($b, $array[$a]) && ($array[$a][$b] || $array[$a][$b] === 0) ){
1096 if($c || $c === 0){
1097 if( is_array($array[$a][$b]) && array_key_exists($c, $array[$a][$b]) && ($array[$a][$b][$c] || $array[$a][$b][$c] === 0) ){
1098 return $array[$a][$b][$c];
1099 } else{
1100 return false;
1101 }
1102 } else{
1103 return $array[$a][$b];
1104 }
1105 } else{
1106 return false;
1107 }
1108 } else{
1109 return $array[$a];
1110 }
1111 }
1112 }
1113 return false;
1114 }
1115
1116 function wpforo_human_filesize($bytes, $decimals = 2) {
1117 $size = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
1118 $factor = floor((strlen($bytes) - 1) / 3);
1119 return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . '&nbsp;' . @$size[$factor];
1120 }
1121
1122 function wpforo_date($date, $type = 'ago', $echo = true ) {
1123 if(is_numeric($date)) $date = date_i18n( 'Y-m-d H:i:s', $date);
1124
1125 $d = $date;
1126 $sep = ' ';
1127 $timestamp = strtotime($date);
1128 $timezone_string = get_option('timezone_string');
1129 $current_offset = get_option('gmt_offset');
1130 if(!is_string($type)) $type = 'ago';
1131
1132 if( is_user_logged_in() && !empty(WPF()->current_user) ){
1133 if( isset(WPF()->current_user['timezone']) && WPF()->current_user['timezone'] != '' ){
1134 if(preg_match('|UTC([\-\+]+.?)|is', WPF()->current_user['timezone'], $timezone_array)){
1135 $timezone_string = '';
1136 $current_offset = str_replace('+', '', $timezone_array[1]);
1137 }
1138 else{
1139 if(in_array(WPF()->current_user['timezone'], timezone_identifiers_list())){
1140 $timezone_string = WPF()->current_user['timezone'];
1141 $current_offset = '';
1142 }
1143 else{
1144 $timezone_string = '';
1145 $current_offset = '';
1146 }
1147 }
1148 }
1149 }
1150
1151 if( $timezone_string == '' && $current_offset != '' ){
1152 $timezone_string = timezone_name_from_abbr('', $current_offset * 3600, false);
1153 }
1154
1155 if( class_exists('DateTime') && class_exists('DateTimeZone') && $timezone_string ){
1156 $dt = new DateTime("now", new DateTimeZone($timezone_string));
1157 if( method_exists($dt, 'setTimestamp') ) $dt->setTimestamp($timestamp); //DateTime::setTimestamp() available in PHP 5.3 and higher versions
1158 if( $type == 'human' ){
1159 $d = human_time_diff($timestamp);
1160 }
1161 elseif( $type == 'ago' ){
1162 $d = human_time_diff($timestamp) . '&nbsp;';
1163 $d = sprintf( wpforo_phrase('%s ago', false, false), $d );
1164 }
1165 else{
1166 if( wpforo_feature('wp-date-format') ){
1167 $date_format = get_option('date_format');
1168 $time_format = get_option('time_format');
1169 $type = $date_format . $sep . $time_format;
1170 }
1171 $d = date_i18n($type, strtotime($dt->format('Y-m-d H:i:s')));
1172 }
1173 }
1174 else{
1175
1176 if( $type == 'human' ){
1177 $d = human_time_diff($timestamp);
1178 }
1179 elseif( $type == 'ago' ){
1180 $d = human_time_diff($timestamp) . '&nbsp;';
1181 $d = sprintf( wpforo_phrase('%s ago', false, false), $d );
1182 }
1183 else{
1184 if( wpforo_feature('wp-date-format') ){
1185 $date_format = get_option('date_format');
1186 $time_format = get_option('time_format');
1187 $type = $date_format . $sep . $time_format;
1188 }
1189 $d = date_i18n( $type, $timestamp);
1190 }
1191 }
1192
1193 if( $echo ){
1194 echo $d;
1195 }
1196 else{
1197 return $d;
1198 }
1199 }
1200
1201 function wpforo_write_file( $new_file, $content ){
1202 $return = array( 'error' => false, 'file' => '' );
1203 $ifp = @fopen( $new_file, 'wb' );
1204 if ( ! $ifp ) {
1205 $return = array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
1206 }
1207 else{
1208 @fwrite( $ifp, $content );
1209 fclose( $ifp );
1210 clearstatcache();
1211 // Set correct file permissions
1212 $stat = @stat( dirname( $new_file ) );
1213 $perms = $stat['mode'] & 0007777;
1214 $perms = $perms & 0000666;
1215 @chmod( $new_file, $perms );
1216 clearstatcache();
1217 $return = array( 'file' => $new_file );
1218 }
1219 return $return;
1220 }
1221
1222
1223 function wpforo_get_file_content( $file ){
1224 $fp = @fopen( $file, 'r' );
1225 if( !$fp ){
1226 return false;
1227 }
1228 else{
1229 $size = @filesize($file);
1230 if( isset($size) && $size > 0 ){
1231 $file_data = fread( $fp, $size );
1232 fclose( $fp );
1233 return $file_data;
1234 }
1235 return false;
1236 }
1237 }
1238
1239 ################################################################################
1240 /**
1241 * Clears file basename and removes trailing slash
1242 *
1243 * @since 1.0.0
1244 *
1245 * @param string filename
1246 *
1247 * @return string
1248 */
1249 function wpforo_clear_basename( $file ) {
1250 $file = str_replace('\\','/',$file);
1251 $file = preg_replace('|/+|','/', $file);
1252 $file = trim($file, '/');
1253 return $file;
1254 }
1255
1256 #################################################################################
1257 /**
1258 * Removes directory with all files and folders
1259 *
1260 * @since 1.0.0
1261 *
1262 * @param string directory name
1263 *
1264 */
1265 function wpforo_remove_directory( $directory ) {
1266 $directory_ns = trim( $directory, '/') . '/';
1267 $directory_ws = '/' . trim( $directory, '/') . '/';
1268 $glob = glob( $directory_ns . '/*' );
1269 if( empty($glob) ) $glob = glob( $directory_ws . '/*' );
1270 foreach( $glob as $item ) {
1271 if( is_dir( $item ) ){
1272 wpforo_remove_directory( $item );
1273 }
1274 else{
1275 unlink( $item );
1276 }
1277 }
1278 return rmdir( $directory );
1279 }
1280
1281 #################################################################################
1282 /**
1283 * Converts bytes to KB, MB, GB
1284 *
1285 * @since 1.0.0
1286 *
1287 * @param integer Bytes
1288 *
1289 * @return string
1290 */
1291 function wpforo_print_size($value, $points = true ){
1292
1293 if($value <= 1024)
1294 {
1295 return $value . (($points) ? " B" : '' );
1296 }
1297 if($value > 1024 && $value <= (1024*1024))
1298 {
1299 $value=round(($value/1024)*10)/10;
1300 return $value . (($points) ? " KB" : '' );
1301 }
1302 if($value > 1024*1024 && $value <= 1024*1024*1024)
1303 {
1304 $value=round(($value/(1024*1024))*10)/10;
1305 return $value . (($points) ? " MB" : '' );
1306 }
1307 if($value > 1024*1024*1024 && $value <= 1024*1024*1024*1024)
1308 {
1309 $value=round(($value/(1024*1024*1024))*10)/10;
1310 return $value . (($points) ? " GB" : '' );
1311 }
1312 }
1313
1314 function wpforo_human_size_to_bytes($sSize)
1315 {
1316 if ( is_numeric( $sSize) ) {
1317 return $sSize;
1318 }
1319 $sSuffix = substr($sSize, -1);
1320 $iValue = substr($sSize, 0, -1);
1321 switch(strtoupper($sSuffix)){
1322 case 'P':
1323 $iValue *= 1024;
1324 case 'T':
1325 $iValue *= 1024;
1326 case 'G':
1327 $iValue *= 1024;
1328 case 'M':
1329 $iValue *= 1024;
1330 case 'K':
1331 $iValue *= 1024;
1332 break;
1333 }
1334 return $iValue;
1335 }
1336
1337
1338 function wpforo_print_number($n, $echo = false) {
1339 $x = str_replace(",","",$n);
1340 $x = intval($x);
1341 $n = 0 + $x;
1342 if(!is_numeric($n)) return false;
1343 if($n>1000000000000) return round(($n/1000000000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}T',false));
1344 else if($n>1000000000) return round(($n/1000000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}B',false));
1345 else if($n>1000000) return round(($n/1000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}M',false));
1346 else if($n>10000) return round(($n/1000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}K',false));
1347 if($echo){
1348 echo number_format($n);
1349 }
1350 else{
1351 return number_format($n);
1352 }
1353 }
1354
1355 function wpforo_bigintval($value) {
1356 if(!is_array($value)) {
1357 $value = trim($value);
1358 }
1359 if (ctype_digit($value)) {
1360 return $value;
1361 }
1362 $value = preg_replace("/[^0-9](.*)$/", '', $value);
1363 if (ctype_digit($value)) {
1364 return $value;
1365 }
1366 return 0;
1367 }
1368
1369 function wpforo_removebb($string){
1370 if(isset($string) && $string ){
1371 $string = preg_replace('|\[\/*[^\]\[]+\]|is', '', $string);
1372 }
1373 return $string;
1374 }
1375
1376 function wpforo_file_upload_error($code){
1377 switch ($code) {
1378 case UPLOAD_ERR_INI_SIZE:
1379 $message = wpforo_phrase("The uploaded file exceeds the upload_max_filesize directive in php.ini", false);
1380 break;
1381 case UPLOAD_ERR_FORM_SIZE:
1382 $message = wpforo_phrase("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", false);
1383 break;
1384 case UPLOAD_ERR_PARTIAL:
1385 $message = wpforo_phrase("The uploaded file was only partially uploaded", false);
1386 break;
1387 case UPLOAD_ERR_NO_FILE:
1388 $message = wpforo_phrase("No file was uploaded", false);
1389 break;
1390 case UPLOAD_ERR_NO_TMP_DIR:
1391 $message = wpforo_phrase("Missing a temporary folder", false);
1392 break;
1393 case UPLOAD_ERR_CANT_WRITE:
1394 $message = wpforo_phrase("Failed to write file to disk", false);
1395 break;
1396 case UPLOAD_ERR_EXTENSION:
1397 $message = wpforo_phrase("File upload stopped by extension", false);
1398 break;
1399 default:
1400 $message = wpforo_phrase("Unknown upload error", false);
1401 break;
1402 }
1403 return $message;
1404 }
1405
1406 //$key allowed values are post, strip, data, user_description entities or the name of a field filter such as pre_user_description.
1407 //More info https://core.trac.wordpress.org/browser/tags/4.5.2/src/wp-includes/kses.php#L624
1408 function wpforo_kses( $string = '', $key = 'post' ){
1409
1410 if(!$string || !$key) return $string;
1411 if( $key == 'email' ){
1412 $allowed_html = array( 'a' => array( 'href' => array(), 'title' => array()),
1413 'blockquote' => array(),
1414 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
1415 'hr' => array(),
1416 'br' => array(),
1417 'p' => array(),
1418 'strong' => array(),
1419 'style' => array());
1420 $allowed_html = apply_filters('wpforo_kses_allowed_html_email', $allowed_html);
1421 }
1422 elseif( $key == 'user_description' ){
1423 $allowed_html = wp_kses_allowed_html( $key );
1424 $allowed_html['img'] = array( 'alt' => array(), 'align' => array(), 'border' => array(), 'height' => array(), 'hspace' => array(), 'longdesc' => array(), 'vspace' => array(), 'src' => array(), 'usemap' => array(), 'width' => array());
1425 $allowed_html = apply_filters('wpforo_kses_allowed_html_user_description', $allowed_html);
1426 }
1427 else{
1428 global $allowedposttags;
1429 $allowed_html = $allowedposttags;
1430 if(wpforo_feature('content-do_shortcode')){
1431 $allowed_html = wp_kses_allowed_html( $key );
1432 }
1433 $extra_html = WPF()->tools_antispam['html'];
1434 $allowed_html = wpforo_extra_html_parser($extra_html, $allowed_html);
1435 $allowed_html['a']['data-gallery'] = array();
1436 $allowed_html['a']['download'] = array();
1437 $allowed_html['blockquote']['class'] = TRUE;
1438 $allowed_html['blockquote']['data-width'] = TRUE;
1439 $allowed_html['p']['lang'] = TRUE;
1440 $allowed_html['p']['dir'] = TRUE;
1441 if(!wpfval($allowed_html, 'iframe') && class_exists('wpForoEmbeds')){
1442 $allowed_html['iframe'] = array('width' => array(), 'height' => array(), 'src' => array(), 'frameborder' => array(), 'allowfullscreen' => array());
1443 }
1444 $allowed_html = apply_filters('wpforo_kses_allowed_html', $allowed_html);
1445 }
1446 return wp_kses( $string, $allowed_html );
1447 }
1448
1449 function wpforo_deep_merge($default, $current = array()){
1450 foreach($default as $k => $v){
1451 if(!empty($v) && is_array($v)){
1452 foreach($v as $kk => $vv){
1453 if(!empty($vv) && is_array($vv)){
1454 foreach($vv as $kkk => $vvv){
1455 if(!empty($vvv) && is_array($vvv)){
1456 foreach($vvv as $kkkk => $vvvv){
1457 if(!empty($vvv) && is_array($vvv)){
1458 //Stop on 5th level
1459 }
1460 else{
1461 if(isset($current[$k][$kk][$kkk][$kkkk])) $default[$k][$kk][$kkk][$kkkk] = $current[$k][$kk][$kkk][$kkkk];
1462 }
1463 }
1464 }
1465 else{
1466 if(isset($current[$k][$kk][$kkk])) $default[$k][$kk][$kkk] = $current[$k][$kk][$kkk];
1467 }
1468 }
1469 }
1470 else{
1471 if(isset($current[$k][$kk])) $default[$k][$kk] = $current[$k][$kk];
1472 }
1473 }
1474 }
1475 else{
1476 if(isset($current[$k])) $default[$k] = $current[$k];
1477 }
1478 }
1479 return $default;
1480 }
1481
1482
1483 function wpforo_is_image($e){
1484 $is_image = false;
1485 $e = strtolower($e);
1486 if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){
1487 $is_image = true;
1488 }
1489 return $is_image;
1490 }
1491
1492
1493 function get_wpf_option( $option, $default = false ){
1494 $value = get_option($option, $default);
1495 if( $value ){
1496 $value = maybe_unserialize( $value );
1497 if(is_serialized( $value )) {
1498 $check = @unserialize($value);
1499 if( !$check ) $value = wpforo_fixSerializedArray($value);
1500 }
1501 }
1502 if( $default && is_array($default) && is_array($value) ) $value = array_merge( $default, $value );
1503 return $value;
1504 }
1505
1506 /**
1507 * Extract what remains from an unintentionally truncated serialized string
1508 * $data contains your original array (or what remains of it).
1509 * @param string The serialized array
1510 */
1511 function wpforo_fixSerializedArray($serialized){
1512 $tmp = preg_replace('/^a:\d+:\{/', '', $serialized);
1513 return wpforo_fixSerializedArray_R($tmp);
1514 }
1515 /**
1516 * The recursive function that does all of the heavy lifing. Do not call directly.
1517 * @param string The broken serialzized array
1518 * @return string Returns the repaired string
1519 */
1520 function wpforo_fixSerializedArray_R(&$broken){
1521 $data = array();
1522 $index = NULL;
1523 $len = strlen($broken);
1524 $i = 0;
1525 while(strlen($broken)) {
1526 $i++;
1527 if ($i > $len) { break; }
1528 if (substr($broken, 0, 1) == '}') {
1529 $broken = substr($broken, 1); return $data;
1530 }
1531 else{
1532 $bite = substr($broken, 0, 2);
1533 switch($bite) {
1534 case 's:':
1535 $re = '/^s:\d+:"([^\"]*)";/';
1536 if (preg_match($re, $broken, $m)){
1537 if ($index === NULL){ $index = $m[1]; }
1538 else{$data[$index] = $m[1]; $index = NULL;}
1539 $broken = preg_replace($re, '', $broken);
1540 }
1541 break;
1542 case 'i:':
1543 $re = '/^i:(\d+);/';
1544 if (preg_match($re, $broken, $m)){
1545 if ($index === NULL){$index = (int) $m[1]; }
1546 else{$data[$index] = (int) $m[1]; $index = NULL; }
1547 $broken = preg_replace($re, '', $broken);
1548 }
1549 break;
1550 case 'b:':
1551 $re = '/^b:[01];/';
1552 if (preg_match($re, $broken, $m)){
1553 $data[$index] = (bool) $m[1]; $index = NULL; $broken = preg_replace($re, '', $broken);
1554 }
1555 break;
1556 case 'a:':
1557 $re = '/^a:\d+:\{/';
1558 if (preg_match($re, $broken, $m)){
1559 $broken = preg_replace('/^a:\d+:\{/', '', $broken); $data[$index] = wpforo_fixSerializedArray_R($broken); $index = NULL;
1560 }
1561 break;
1562 case 'N;':
1563 $broken = substr($broken, 2); $data[$index] = NULL; $index = NULL;
1564 break;
1565 }
1566 }
1567 }
1568 return $data;
1569 }
1570
1571 function wpforo_insert_to_media_library( $attach_path, $title = '' ){
1572 if( wpforo_feature('attach-media-lib') ){
1573 if(!$attach_path ) return 0;
1574 $attach_fname = basename($attach_path);
1575 if(!$title) $title = $attach_fname;
1576 require_once(ABSPATH . 'wp-admin/includes/media.php');
1577 require_once(ABSPATH . 'wp-admin/includes/file.php');
1578 require_once(ABSPATH . 'wp-admin/includes/image.php');
1579 $wp_upload_dir = wp_upload_dir();
1580 $filetype = wp_check_filetype( $attach_fname, NULL );
1581 $attachment = array( 'guid' => $attach_path, 'post_mime_type' => $filetype['type'], 'post_title' => $title, 'post_content' => '', 'post_status' => 'inherit');
1582 $attach_id = wp_insert_attachment( $attachment, $attach_path );
1583 add_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' );
1584 $attach_data = wp_generate_attachment_metadata( $attach_id, $attach_path );
1585 wp_update_attachment_metadata( $attach_id, $attach_data );
1586 remove_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' );
1587 return $attach_id;
1588 }
1589 }
1590
1591 function wpforo_attachment_sizes( $sizes ){
1592 return array('thumbnail');
1593 }
1594
1595 function wpforo_debug(){
1596 if( wpforo_feature('debug-mode') ) : ?>
1597 <div id="wpforo-debug" style="display:none">
1598 <h4>Super Globals</h4>
1599 <p>Requests: <?php print_r($_REQUEST); ?></p>
1600 <p>Server: <?php print_r($_REQUEST); ?></p>
1601 <h4>Options and Features</h4>
1602 <textarea style="width:500px; height:300px;"><?php echo @ 'permastruct: ' . WPF()->permastruct . "\r\n";
1603 echo @ 'use_home_url: ' . WPF()->use_home_url . "\r\n";
1604 echo @ 'url: ' . wpforo_home_url() . "\r\n";
1605 @print_r(WPF()->general_options) . "\r\n";
1606 echo @ 'pageid:' . WPF()->pageid . "\r\n";
1607 echo @ 'default_groupid: ' . WPF()->usergroup->default_groupid . "\r\n";
1608 @print_r(WPF()->forum->options) . "\r\n";
1609 @print_r(WPF()->post->options) . "\r\n";
1610 @print_r(WPF()->member->options) . "\r\n";
1611 @print_r(WPF()->sbscrb->options) . "\r\n";
1612 @print_r(WPF()->features) . "\r\n";
1613 @print_r(WPF()->tpl->style) . "\r\n";
1614 @print_r(WPF()->tpl->options) . "\r\n";
1615 @print_r(WPF()->tpl->theme) . "\r\n";
1616 ?>
1617 </textarea>
1618 </div>
1619 <?php
1620 endif;
1621 }
1622
1623 function wpforo_hook( $name, $args = array() ){
1624 do_action( $name, $args );
1625 }
1626
1627 #################################################################################
1628 /**
1629 * Cleans forum cache
1630 *
1631 * @since 1.2.1
1632 *
1633 * @param string Item View / Template (e.g.: 'forum', 'topic', 'post', 'user', 'widget', etc...)
1634 * @param integer Item ID (e.g.: $topicid or $postid) | (!) ID is 0 on dome actions (e.g.: delete actions)
1635 * @param array Item data as array
1636 *
1637 */
1638 function wpforo_clean_cache( $template = 'all', $id = 0, $item = array() ){
1639 $pageid = url_to_postid( $_SERVER['REQUEST_URI'] );
1640 do_action( 'wpforo_clean_cache_start', $id, $template );
1641 if( $pageid ){
1642 $page = get_post( $pageid );
1643 clean_post_cache( $page );
1644 }
1645 WPF()->statistic('update', $template);
1646 do_action( 'wpforo_clean_cache', $id, $template );
1647 WPF()->cache->clean( $id, $template, $item );
1648 do_action( 'wpforo_clean_cache_end', $id, $template );
1649 }
1650
1651 function wpforo_db_check( $args = array() ){
1652 global $wpdb;
1653 $check = trim($args['check']);
1654 $col = esc_sql(trim($args['col']));
1655 $table = esc_sql(trim($args['table']));
1656
1657 if( $check == 'table_exists' ){
1658 return $wpdb->get_var("SHOW TABLES LIKE '$table'");
1659 }
1660
1661 if( $check == 'col_exists' ){
1662 return $wpdb->get_var("SHOW COLUMNS FROM `$table` LIKE '$col'");
1663 }
1664
1665 if( $check == 'key_exists' ){
1666 return $wpdb->get_var("SHOW KEYS FROM `$table` WHERE `Key_name` = '$col'");
1667 }
1668
1669 if( $check == 'default_value' ){
1670 $col = $wpdb->get_row("SHOW COLUMNS FROM `$table` LIKE '$col'", ARRAY_A);
1671 return $col['Default'];
1672 }
1673
1674 if( $check == 'col_type' ){
1675 $col = $wpdb->get_row("SHOW COLUMNS FROM `$table` LIKE '$col'", ARRAY_A);
1676 return $col['Type'];
1677 }
1678
1679 return false;
1680 }
1681
1682 function wpforo_add_unique_key($table, $primary_key, $unique_key_name = '', $unique_fields = ''){
1683
1684 $table = esc_sql(trim($table));
1685 $primary_key = esc_sql(trim($primary_key));
1686 $unique_fields = esc_sql(trim($unique_fields, ','));
1687 $unique_fields_clean = preg_replace('|\([^\(\)]+\)|', '', $unique_fields);
1688 $remove_rows = '';
1689 $sql = "SELECT GROUP_CONCAT(`$primary_key`) duplicated_row_ids,
1690 COUNT(*) duplication_count FROM
1691 `$table` GROUP BY $unique_fields_clean HAVING duplication_count > 1";
1692
1693 $rows = WPF()->db->get_results($sql, ARRAY_A);
1694 if(!empty($rows)){
1695 foreach($rows as $row){
1696 $ids = explode(',', $row['duplicated_row_ids']);
1697 $ids = array_reverse($ids);
1698 $ids = array_slice($ids, 1);
1699 $remove_rows .= trim(implode(',', $ids), ',') . ',';
1700 }
1701 $remove_rows = esc_sql(trim($remove_rows, ','));
1702 if( $remove_rows ) {
1703 WPF()->db->query("DELETE FROM `$table` WHERE `$primary_key` IN($remove_rows)");
1704 }
1705 }
1706 $sql = "ALTER TABLE `$table` ADD UNIQUE KEY `$unique_key_name`( $unique_fields )";
1707 WPF()->db->query($sql);
1708 }
1709
1710 function wpforo_is_owner( $userid, $email = '' ){
1711 if( isset(WPF()->current_userid) && WPF()->current_userid ){
1712 if( $userid == WPF()->current_userid ) return true;
1713 }
1714 elseif( !is_user_logged_in() && $email ){
1715 $guest = WPF()->member->get_guest_cookies();
1716 if( wpfval($guest, 'email') && $guest['email'] == $email ) {
1717 return true;
1718 }
1719 return false;
1720 }
1721 return false;
1722 }
1723
1724 function wpforo_make_dname($display_name, $user_nicename){
1725 $display_name = trim($display_name);
1726 $user_nicename = trim($user_nicename);
1727 return ( $display_name ? esc_html($display_name) : esc_html(urldecode($user_nicename)) );
1728 }
1729
1730 function wpforo_strlen($string ){
1731 if(!$string) return 0;
1732 if(function_exists('mb_strlen')){
1733 return mb_strlen($string);
1734 }
1735 else{
1736 return strlen($string);
1737 }
1738 }
1739
1740 function wpforo_string2array( $string, $regexp = '' ){
1741 if( !$regexp ) $regexp = '#' . preg_quote(PHP_EOL) . '#isu';
1742 $array = preg_split($regexp, $string);
1743 return array_filter($array);
1744 }
1745
1746 function wpforo_array_ordered_intersect_key($array1, $array2){
1747 $new_array = array();
1748 foreach ($array2 as $key => $value){
1749 if( isset( $array1[$key] ) ) $new_array[$key] = $array1[$key];
1750 }
1751 return $new_array;
1752 }
1753
1754 function wpforo_urlpath_to_dirpath($urlpath){
1755 $upload_array = wp_upload_dir();
1756 $wp_content_dir = preg_replace('#/wp-content/.+$#isu', '/wp-content/', $upload_array['basedir']);
1757 $dirpath = preg_replace('#^.+?/wp-content/#isu', $wp_content_dir, $urlpath);
1758 return $dirpath;
1759 }
1760
1761 function wpforo_xcopy($source, $dest)
1762 {
1763 // Check for symlinks
1764 if (is_link($source)) {
1765 return symlink(readlink($source), $dest);
1766 }
1767
1768 // Simple copy for a file
1769 if (is_file($source)) {
1770 return copy($source, $dest);
1771 }
1772
1773 // Make destination directory
1774 if (!is_dir($dest)) {
1775 wp_mkdir_p($dest);
1776 }
1777
1778 // Loop through the folder
1779 $dir = dir($source);
1780 while (false !== $entry = $dir->read()) {
1781 // Skip pointers
1782 if ($entry == '.' || $entry == '..') {
1783 continue;
1784 }
1785
1786 // Deep copy directories
1787 wpforo_xcopy( rtrim($source, '/') . "/$entry", rtrim($dest, '/') . "/$entry" );
1788 }
1789
1790 // Clean up
1791 $dir->close();
1792 return true;
1793 }
1794
1795 function wpforo_printf_array($format, $arr){
1796 return call_user_func_array('printf', array_merge((array)$format, (array)$arr));
1797 }
1798
1799 function wpforo_sprintf_array($format, $arr){
1800 return call_user_func_array('sprintf', array_merge((array)$format, (array)$arr));
1801 }
1802
1803 function wpforo_avatar_url($avatar_html){
1804 if( preg_match('#src=[\'"]([^\'"]+?)[\'"]#isu', $avatar_html, $matches) ){
1805 return $matches[1];
1806 }
1807 return '';
1808 }
1809
1810 function wpforo_get_image_url( $content, $first = true, $type = 'general' ){
1811 $images = array();
1812 if( $content !== false ){
1813 $content = apply_filters('wpforo_content_filter', $content);
1814 preg_match_all('#https?://[^\s\'\"<>]+\.(?:jpg|jpeg|png|gif|ico|svg|bmp|tiff)#isu', $content, $m_img, PREG_SET_ORDER);
1815 if( empty($m_img)) preg_match_all('#//[^\s\'\"<>]+\.(?:jpg|jpeg|png|gif|ico|svg|bmp|tiff)#isu', $content, $m_img, PREG_SET_ORDER);
1816 if(!empty($m_img)){
1817 foreach( $m_img as $match ){
1818 $ext = pathinfo($match[0], PATHINFO_EXTENSION);
1819 if( $ext && wpforo_is_image($ext)){
1820 $images[] = $match[0];
1821 }
1822 }
1823 }
1824 else{
1825 preg_match_all('#https?://[^\s\'\"<>]+#isu', $content, $m_url, PREG_SET_ORDER);
1826 if( empty($m_url)){
1827 preg_match_all('#//[^\s\'\"<>]+#isu', $content, $m_url, PREG_SET_ORDER);
1828 }
1829 if(!empty($m_url)){
1830 foreach( $m_url as $match ){
1831 $ext = pathinfo($match[0], PATHINFO_EXTENSION);
1832 if( $ext && wpforo_is_image($ext)){
1833 $images[] = $match[0];
1834 }
1835 }
1836 }
1837 }
1838 }
1839
1840
1841 if(!empty($images)){
1842 if( $first && wpfval($images, 0) ){
1843 return apply_filters('wpforo_find_image_url', $images[0], $type);
1844 }
1845 else{
1846 return apply_filters('wpforo_find_image_url', $images, $type);
1847 }
1848 }
1849 return apply_filters('wpforo_find_image_url', false, $type);;
1850 }
1851
1852 function wpforo_return_zero($var = null){
1853 return 0;
1854 }
1855
1856 function wpforo_is_json($string) {
1857 json_decode($string);
1858 return (json_last_error() == JSON_ERROR_NONE);
1859 }
1860
1861 function wpforo_ajax_response( $message ) {
1862 wp_send_json( $message );
1863 die();
1864 }
1865
1866 function wpforo_get_fb_user( $user ) {
1867 if( is_user_logged_in() ) return wp_get_current_user();
1868 $user_data = get_user_by('email', $user['user_email']);
1869 if( !$user_data ) {
1870 $users = get_users( array( 'meta_key' => '_fb_user_id', 'meta_value' => $user['fb_user_id'], 'number' => 1, 'count_total' => false ) );
1871 if( is_array( $users ) ) $user_data = reset( $users );
1872 }
1873 return $user_data;
1874 }
1875
1876
1877 function wpforo_unique_username( $username ) {
1878 static $i;
1879 if( !$username ) $username = 'user_' . uniqid();
1880 if( strpos($username, '@') !== FALSE ){
1881 $parts = explode( "@", $username );
1882 if( !empty($parts) && isset($parts[0]) && $parts[0] ) {
1883 $username = $parts[0];
1884 } else {
1885 $username = str_replace( '@', '', $username);
1886 }
1887 }
1888 if ( null === $i ) { $i = 1; } else { $i++; }
1889 if ( !username_exists($username) ) { return $username; }
1890 $new_username = sprintf( '%s-%s', $username, $i );
1891 if ( ! username_exists( $new_username ) ) {
1892 return $new_username;
1893 } else {
1894 return call_user_func( __FUNCTION__, $username );
1895 }
1896 }
1897
1898
1899 function wpforo_find_current_user_data( $current_object ){
1900 if( is_user_logged_in() && !(isset($current_object['user_nicename']) && $current_object['user_nicename']) && !(isset($current_object['userid']) && $current_object['userid']) ){
1901 $user = wp_get_current_user();
1902 if(!empty($user)){
1903 $current_object['userid'] = $user->ID;
1904 $current_object['user_nicename'] = $user->user_nicename;
1905 }
1906 }
1907 return $current_object;
1908 }
1909
1910 function wpforo_is_session_started(){
1911 if ( php_sapi_name() !== 'cli' ) {
1912 if ( version_compare(phpversion(), '5.4.0', '>=') ) {
1913 return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
1914 } else {
1915 return session_id() === '' ? FALSE : TRUE;
1916 }
1917 }
1918 return FALSE;
1919 }
1920
1921 function wpforo_current_guest( $email ){
1922 $guest = WPF()->member->get_guest_cookies();
1923 if(!wpfval($guest, 'email') || !$guest['email']) return false;
1924 if( $email == $guest['email']){
1925 return true;
1926 }else{
1927 return false;
1928 }
1929 }
1930
1931 function wpforo_extra_html_parser( $extra_html = '', $allowed_html = array() ){
1932 if( $extra_html ){
1933 $extra_html = explode(',', $extra_html);
1934 $extra_html = array_filter($extra_html);
1935 if(!empty($extra_html)){
1936 foreach( $extra_html as $html ){
1937 $html = trim($html);
1938 if( preg_match('|([^\(\)]+)\((.+)\)|', $html, $item) ){
1939 if(wpfval($item, 1) && wpfval($item, 2)) {
1940 $attrs = explode(' ', $item[2]);
1941 $attrs = array_map('trim', $attrs);
1942 foreach( $attrs as $attr ){
1943 $allowed_html[$item[1]][$attr] = array();
1944 }
1945 }
1946 }
1947 else{
1948 $allowed_html[$html] = array();
1949 }
1950 }
1951 }
1952 }
1953 return $allowed_html;
1954 }
1955
1956 function wpforo_clear_array($array, $clear = array()){
1957 if( is_array($clear) && !empty($clear) ){
1958 foreach( $clear as $ext ){
1959 if (($key = array_search($ext, $array)) !== false) {
1960 unset($array[$key]);
1961 }
1962 }
1963 }
1964 elseif( is_string($clear) || is_numeric($clear) ){
1965 if( wpfval($array, $clear) ) unset( $array[$clear] );
1966 }
1967 return $array;
1968 }