PluginProbe
wpForo Forum / 1.1.1
wpForo Forum v1.1.1
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.1.1, at wpf-includes/functions.php

1,352 lines 53.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 }
25
26 function wpforo_is_ajax(){
27 if( defined('DOING_AJAX') && DOING_AJAX ) return TRUE;
28 return FALSE;
29 }
30
31 function wpforo_is_admin(){
32 if( is_admin() && !wpforo_is_ajax() ) return TRUE;
33 return FALSE;
34 }
35
36 function is_wpforo_page($url = ''){
37 if( wpforo_is_admin() ) return FALSE;
38 if(!$url) $url = wpforo_get_request_uri();
39 if( is_wpforo_exclude_url($url) ) return FALSE;
40 if( is_wpforo_shortcode_page() ) return TRUE;
41 return is_wpforo_url($url);
42 }
43
44 function is_wpforo_exclude_url($url = ''){
45 global $wpforo;
46 if(!$url) $url = wpforo_get_request_uri();
47 $url = urldecode($url);
48 $url = preg_replace('#/page/\d*/?$#isu', '', $url);
49
50 if( $wpforo->use_home_url && $wpforo->excld_urls ){
51 if( !$current_url = wpforo_get_url_query_vars_str($url) ) return FALSE;
52 $expld = array_filter( explode("\n", $wpforo->excld_urls) );
53 foreach( $expld as $excld_url ){
54 $excld_url = urldecode($excld_url);
55 if( strpos($excld_url, home_url('/')) === FALSE ) continue;
56 $excld_url = wpforo_get_url_query_vars_str($excld_url);
57 if( $excld_url && strtolower($current_url) == strtolower($excld_url) ) return TRUE;
58 }
59 }
60
61 return FALSE;
62 }
63
64 function is_wpforo_url($url = ''){
65 if( wpforo_is_admin() ) return FALSE;
66 if(!$url) $url = wpforo_get_request_uri();
67 global $wpforo;
68
69 if( $wpforo->use_home_url ) return TRUE;
70
71 $current_url = wpforo_get_url_query_vars_str($url);
72
73 if( $wpforo->permastruct && strpos($current_url, $wpforo->permastruct) !== FALSE
74 && strpos($current_url, $wpforo->permastruct) == 0
75 && !wpforo_is_admin() ) return TRUE;
76
77 return FALSE;
78 }
79
80 function is_wpforo_shortcode_page(){
81 if( wpforo_is_admin() ) return FALSE;
82 global $wpforo, $post;
83 if( $post && isset($post->post_content) && has_shortcode( $post->post_content, 'wpforo' ) && !is_wpforo_url() ) return TRUE;
84 return FALSE;
85 }
86
87 function wpforo_get_url_query_vars_str($url = ''){
88 if(!$url) $url = wpforo_get_request_uri();
89
90 $current_url = preg_replace('#https?://[^/\?]+\.[^/\?]+/?#isu', '', $url);
91 $site_url = preg_replace('#https?://[^/\?]+\.[^/\?]+/?#isu', '', esc_url( home_url('/') ));
92 $current_url = preg_replace( '#^/?'.preg_quote($site_url).'#isu', '' , $current_url, 1 );
93 $current_url = preg_replace('#^[\r\n\t\s\0/]*(.*?)[\r\n\t\s\0/]*$#isu', '$1', $current_url);
94
95 return $current_url;
96 }
97
98 /*function wpforo_use_home_url(){
99 global $wpforo;
100
101 $current_url = wpforo_get_url_query_vars_str();
102 if( !$current_url && $wpforo->use_home_url ) return TRUE;
103 return FALSE;
104 }*/
105
106 function wpforo_feature( $option, $wpforo = NULL ){
107 if( empty($wpforo) ) global $wpforo;
108 if( isset($wpforo->features[$option]) && $wpforo->features[$option] ){
109 return true;
110 }
111 else{
112 return false;
113 }
114 }
115
116 function wpforo_dir_size($directory) {
117 $size = 0;
118 if(class_exists('RecursiveIteratorIterator') && class_exists('RecursiveDirectoryIterator')){
119 foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) $size+=$file->getSize();
120 return $size;
121 }
122 else{
123 return 0;
124 }
125 }
126
127 #################################################################################
128 /**
129 * Returns merged arguments array from defined and default arguments.
130 *
131 * @since 1.0.0
132 *
133 * @param mixed defined arguments array or query
134 *
135 * @param mixed default arguments array or query
136 *
137 * @return array
138 */
139 function wpforo_parse_args( $args, $default = array() ) {
140
141 if( is_object($args) ) {
142 $defined = get_object_vars($args);
143 }
144 elseif( is_array($args) ) {
145 $defined = $args;
146 }
147 elseif( preg_match('|^[\d\,\s]+$|', $args) ){
148 $defined = explode(',', trim($args));
149 }
150 elseif( is_integer($args) || is_float($args) ){
151 $defined[0] = $args;
152 }
153 elseif( is_serialized($args) ){
154 $defined = unserialize($args);
155 }
156 elseif( strpos($args, '=') !== FALSE ) {
157 parse_str($args, $defined);
158 }
159 else{
160 $defined[0] = $args;
161 }
162 if(!empty($default)) {
163 return array_merge( $default, $defined );
164 }
165 else {
166 return $defined;
167 }
168 }
169
170 // #############################################################################
171 /**
172 * Detects serialized data
173 *
174 * @since 1.0.0
175 *
176 * @param string
177 *
178 * @return boolean
179 */
180 if(!function_exists('is_serialized')){
181 function is_serialized( $value ){
182 if( $value == '' ) return false;
183 if( $value === 0 ) return false;
184 $value = trim($value);
185 $chsd = @unserialize($value);
186 if( $chsd !== false || $value === 'b:0;' ) {
187 return true;
188 }
189 else{
190 return false;
191 }
192 }
193 }
194
195
196 function wpforo_get_request_uri($with_port = FALSE, $get_referer_when_ajax = TRUE){
197 if( $get_referer_when_ajax && wpforo_is_ajax() ) return $_SERVER['HTTP_REFERER'];
198 $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
199 $sp = strtolower($_SERVER["SERVER_PROTOCOL"]);
200 $protocol = substr($sp, 0, strpos($sp, "/")) . $s;
201 $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
202 return $protocol . "://" . $_SERVER['SERVER_NAME'] . ($with_port ? $port : '') . $_SERVER['REQUEST_URI'];
203 }
204
205
206 function wpforo_arr_group_by($array, $key_by){
207 if(!empty($array)){
208 $fltrd = array();
209 foreach($array as $key => $arr){
210 if(is_numeric($key)) $fltrd[] = $arr[$key_by];
211 }
212 $uniq_arr = array_unique($fltrd);
213 asort($uniq_arr);
214 return $uniq_arr;
215 }
216 }
217
218 // #############################################################################
219 /**
220 * Print item's (topics, replyes ...) table in dashboard
221 * @since 1.0.0
222 * @param string item(component) class name
223 * @param string This table primary key field name
224 * @param array table fields
225 * @param array table search fields
226 * @param array table filter fields
227 * @param array action buttons ( full posible values $actions = array( 'edit', 'delete', 'view' ); )
228 *
229 * @return echo html table
230 */
231 function wpforo_create_form_table($varname, $primary_key, $fields = array(), $search_fields = array(), $filter_fields = array(), $actions = array(), $bulk_actions = array() ){
232
233 if(empty($actions)) $actions = array( 'edit', 'delete', 'view' );
234 if(empty($bulk_actions)) $bulk_actions = array( 'del' );
235
236 if(!empty($fields)) :
237 global $wpdb;
238 global $wpforo;
239
240 $args = array();
241
242 if(isset($_GET['s']) && $_GET['s'] != '') $args['include'] = $wpforo->$varname->search(sanitize_text_field($_GET['s']), $search_fields);
243 if(isset($_GET['orderby'])) $args['orderby'] = sanitize_text_field($_GET['orderby']);
244 if(isset($_GET['order'])) $args['order'] = sanitize_text_field($_GET['order']);
245 if(isset($_GET['forumid']) && $_GET['forumid'] != 0) $args['forumid'] = intval($_GET['forumid']);
246 if(isset($_GET['groupid']) && $_GET['groupid'] != 0) $args['groupid'] = intval($_GET['groupid']);
247 if(isset($_GET['member_status']) && $_GET['member_status']) $args['status'] = (array) sanitize_text_field($_GET['member_status']);
248 if(isset($_GET['userid']) && $_GET['userid'] != 0) $args['userid'] = intval($_GET['userid']);
249 $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
250
251 $args['offset'] = ($paged - 1) * get_option('wpforo_count_per_page');
252 $args['row_count'] = get_option('wpforo_count_per_page');
253
254
255 if($varname == 'reply'){
256 $func = 'get_replies';
257 }else{
258 $func = 'get_'.$varname.'s';
259 }
260
261 $items_count = 0;
262
263 $items = $wpforo->$varname->$func($args, $items_count);
264
265 if(isset($args['include']) && empty($args['include'])){
266 $items_count = 0;
267 $items = array();
268 }
269 $pages_count = ceil($items_count/get_option('wpforo_count_per_page')); ?>
270
271 <script type="text/javascript">
272 function wpforo_check_uncheck_all(status){
273 var inpts = document.getElementById("items").getElementsByTagName("input");
274 for (var i = 0; i < inpts.length; i++) {
275 if (inpts[i].type == 'checkbox') {
276 inpts[i].checked = status;
277 }
278 }
279 }
280
281 function wpforo_doaction(){
282 var action = document.items.action.value;
283 var action2 = document.items.action2.value;
284 if(action != -1 || action2 != -1){
285 var inpts = document.getElementById("items").getElementsByTagName("input");
286
287 var j = 0;
288 var ids_arr = new Array();
289 for (var i = 0; i < inpts.length; i++) {
290 if (inpts[i].type == 'checkbox' && inpts[i].checked == true && inpts[i].value != 'on') {
291 ids_arr[j] = inpts[i].value;
292 j++
293 }
294 }
295 if(ids_arr.length != 0){
296 document.items.ids.value = ids_arr.join();
297 document.items.submit();
298 }
299 }
300 }
301 </script>
302
303
304 <div class="wpforo-search-box">
305 <form name="search" id="srch" action="<?php echo admin_url( 'admin.php' ) ?>" method="GET">
306 <input type="hidden" name="page" value="wpforo-<?php echo esc_attr($varname); ?>s"/>
307 <label class="screen-reader-text" for="post-search-input">Search <?php echo esc_attr(ucfirst($varname)); ?>s:</label>
308 <input type="search" id="post-search-input" name="s" value="<?php echo ( isset($_GET['s']) ? esc_attr(sanitize_text_field($_GET['s'])) : '' ) ?>">
309 <input type="submit" name="" id="search-submit" class="button" value="Search <?php echo esc_attr(ucfirst($varname)); ?>s">
310 </form>
311 </div>
312
313 <form name="items" id="items" action="<?php echo admin_url( 'admin.php' ) ?>" method="GET">
314 <?php wp_nonce_field( 'bulk_action_'.$varname ); ?>
315 <input type="hidden" name="ids" />
316 <input type="hidden" name="page" value="wpforo-<?php echo esc_attr($varname); ?>s"/>
317
318 <div class="tablenav top">
319
320 <div class="alignleft actions">
321 <select name="action">
322 <option value="-1" selected="selected"><?php _e('Bulk Actions', 'wpforo'); ?></option>
323 <?php foreach( $bulk_actions as $bulk_action ) : ?>
324 <option value="<?php echo $bulk_action ?>"><?php _e($bulk_action, 'wpforo'); ?></option>
325 <?php endforeach ?>
326 </select>
327 <input type="button" onclick="wpforo_doaction()" id="doaction" class="button action" value="<?php _e('Apply', 'wpforo'); ?>">
328 </div>
329 <div class="alignleft actions">
330
331 <?php if(!empty($filter_fields)) : ?>
332 <?php foreach($filter_fields as $filter_field) : ?>
333 <?php if($filter_field == 'forumid') : ?>
334
335 <select name="<?php echo esc_attr($filter_field) ?>">
336 <option value="0"><?php _e('Show all forums', 'wpforo'); ?></option>
337 <?php $wpforo->forum->tree('select_box'); ?>
338 </select>
339
340 <?php elseif($filter_field == 'langid') : ?>
341
342 <select name="<?php echo esc_attr($filter_field) ?>">
343 <?php $wpforo->phrase->show_lang_list() ?>
344 </select>
345
346 <?php elseif($filter_field == 'groupid') : ?>
347
348 <select name="<?php echo esc_attr($filter_field) ?>">
349 <option value="0"><?php _e('filter by group', 'wpforo') ?></option>
350 <?php foreach($wpforo->usergroup->get_usergroups() as $group) : ?>
351 <?php if( $group['groupid'] != 4 ) : ?>
352 <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>
353 <?php endif ?>
354 <?php endforeach; ?>
355 </select>
356
357 <?php elseif( $varname == 'member' && $filter_field == 'status' ) :
358
359 $sql = "SELECT DISTINCT `status` as statuses FROM `" . $wpforo->db->prefix . "wpforo_profiles`";
360 if( $statuses = $wpforo->db->get_col($sql) ){ ?>
361 <select name="member_status">
362 <option value="0"><?php _e('filter by status', 'wpforo') ?></option>
363
364 <?php foreach( $statuses as $status ) : ?>
365 <option value="<?php echo esc_attr($status) ?>" <?php echo ( isset($_GET['member_status']) && $_GET['member_status'] == $status ? 'selected' : '' ) ?>><?php echo esc_html($status) ?></option>
366 <?php endforeach; ?>
367
368 </select>
369
370 <?php
371 }
372
373 endif; ?>
374
375 <?php endforeach; ?>
376
377 <input type="submit" name="" id="post-query-submit" class="button" value="Filter">
378
379 <?php endif; ?>
380
381 </div>
382 <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>
383 <span class="pagination-links">
384 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 2 ) : ?>
385 <span class="tablenav-pages-navspan">&laquo;</span>
386 <?php else : ?>
387 <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>
388 <?php endif ?>
389 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 1 ) : ?>
390 <span class="tablenav-pages-navspan"></span>
391 <?php else : ?>
392 <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>
393 <?php endif ?>
394
395 <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>
396
397 <?php if( isset($_GET['paged']) && $_GET['paged'] >= $pages_count ) : ?>
398 <span class="tablenav-pages-navspan"></span>
399 <?php else : ?>
400 <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>
401 <?php endif ?>
402 <?php if( (isset($_GET['paged']) && $_GET['paged'] >= $pages_count - 1) || 1 >= $pages_count - 1 ) : ?>
403 <span class="tablenav-pages-navspan">&raquo;</span>
404 <?php else : ?>
405 <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>
406 <?php endif ?>
407 </div> <br class="clear">
408
409 </div>
410
411 <table class="wp-list-table widefat fixed pages" cellspacing="0">
412 <thead>
413 <tr>
414 <th scope="col" id="cb" class="manage-column column-cb check-column" style="vertical-align:middle; padding:17px 0px 10px 5px;">
415 <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)">
416 </th>
417 <?php foreach($fields as $key => $field) : ?>
418 <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' ?>">
419 <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' ?>">
420 <span><?php echo sanitize_text_field(str_replace('_', ' ', wpforo_phrase( $field, false ))) ?></span>
421 <span class="sorting-indicator"></span>
422 </a>
423 </th>
424 <?php endforeach; ?>
425 </tr>
426 </thead>
427
428 <tfoot>
429 <tr>
430 <th scope="col" class="manage-column column-cb check-column" style="vertical-align:middle; padding:17px 0px 10px 5px;">
431 <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)">
432 </th>
433 <?php foreach($fields as $key => $field) : ?>
434 <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' ?>">
435 <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' ) ) ?>">
436 <span><?php echo esc_html(sanitize_text_field(str_replace('_', ' ', wpforo_phrase( $field, false )))) ?></span>
437 <span class="sorting-indicator"></span>
438 </a>
439 </th>
440 <?php endforeach; ?>
441 </tr>
442 </tfoot>
443
444 <tbody id="the-list">
445
446 <?php
447 if(!empty($items)) :
448
449 if(!empty($items)) :
450 foreach($items as $key => $item) : ?>
451
452 <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">
453 <th scope="row" class="check-column">
454 <label class="screen-reader-text" for="cb-select-<?php echo esc_attr($item[$primary_key]) ?>">
455 Select <?php echo esc_html(ucfirst($varname)) ?>
456 </label>
457 <input id="cb-select-<?php echo esc_attr($item[$primary_key]) ?>" type="checkbox" value="<?php echo esc_attr($item[$primary_key]) ?>">
458 <div class="locked-indicator"></div>
459 </th>
460 <?php $i = 0; foreach($fields as $field) :
461 if($i == 0) : ?>
462 <?php
463 if($varname == 'member'){
464 $url = $url_user = admin_url( 'user-edit.php?user_id=' . intval($item[$primary_key]));
465 $url_profile = $wpforo->$varname->get_profile_url($item[$primary_key], 'account');
466 }else{
467 $url = admin_url( 'admin.php?page=wpforo-'. $varname .'s&id='.$item[$primary_key] .'&action=edit' );
468 }
469 ?>
470 <td class="post-<?php echo esc_attr($item[$field]) ?> page-<?php echo esc_attr($item[$field]) ?> column-<?php echo esc_attr($item[$field]) ?>">
471 <strong>
472 <a class="row-<?php echo esc_attr($item[$field]) ?>" href="<?php echo esc_url($url) ?>" title="Edit “<?php echo esc_attr($varname) ?>”">
473 <?php
474 if($varname == 'forum'){
475 $depth = 0;
476 $wpforo->forum->count_depth($item[$primary_key], $depth);
477 echo str_repeat( '', $depth);
478 }
479 wpfo($item[$field], true, 'esc_html');
480 ?>
481 </a>
482 </strong>
483 <div class="row-actions">
484 <?php foreach($actions as $act_key => $action) : ?>
485 <?php switch($action) :
486 case 'edit': ?>
487 <span class="edit"><a href="<?php echo esc_url($url); ?>" title="Edit this item"><?php _e('edit', 'wpforo'); ?></a></span>
488 <?php break;
489 case 'edit_user': ?>
490 <span class="edit"><a href="<?php echo esc_url($url_user); ?>"><?php _e('edit user', 'wpforo'); ?></a></span>
491 <?php break;
492 case 'edit_profile': ?>
493 <span class="edit"><a href="<?php echo esc_url($url_profile); ?>"><?php _e('edit profile', 'wpforo'); ?></a></span>
494 <?php break;
495 case 'ban': ?>
496 <?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' ); ?>
497 <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>
498 <?php break;
499 case 'delete': ?>
500 <?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' ); ?>
501 <span class="trash"><a class="submitdelete" title="<?php _e('Delete this item', 'wpforo') ?>" onclick="return confirm('<?php _e('Are you sure you whant to DELETE this item?', 'wpforo') ?>');" href="<?php echo esc_url($delete_url); ?>"><?php _e('delete', 'wpforo'); ?></a></span>
502 <?php break;
503 case 'user_delete': ?>
504 <?php $delete_url = wp_nonce_url( "users.php?action=delete&user=".intval($item[$primary_key]), 'bulk-users' ) ?>
505 <span class="trash"><a class="submitdelete" title="<?php _e('Delete this item', 'wpforo') ?>" onclick="return confirm('<?php _e('Are you sure you whant to DELETE this item?', 'wpforo') ?>');" href="<?php echo esc_url($delete_url); ?>"><?php _e('delete', 'wpforo'); ?></a></span>
506 <?php break;
507 case 'view': ?>
508 <?php
509 if($varname != 'member'){
510 $fn_name = "get_".$varname."_url";
511 $item_id = $item[$varname.'id'];
512 }else{
513 $fn_name = "get_profile_url";
514 $item_id = $item['ID'];
515 }
516 ?>
517 <span class="view">
518 <a href="<?php echo esc_url($wpforo->$varname->$fn_name($item_id)) ?>" title="<?php echo esc_attr( __('view', 'wpforo')); ?> “<?php echo esc_attr($varname) ?>”" rel="permalink">
519 <?php _e('view', 'wpforo'); ?>
520 </a>
521 </span>
522 <?php break;
523 endswitch ?>
524 <?php if( $act_key != ( count($actions) - 1 ) ) echo "&nbsp;|&nbsp;"; ?>
525 <?php endforeach ?>
526 </div>
527 </td>
528 <?php else : ?>
529 <td class="<?php echo esc_attr($item[$field]) ?> column-<?php echo esc_attr($item[$field]) ?>">
530 <?php if($field == 'forumid') : ?>
531 <?php $data = $wpforo->forum->get_forum($item[$field]); echo esc_html($data['title']); ?>
532 <?php elseif($field == 'userid') : ?>
533 <?php $data = $wpforo->member->get_member($item[$field]); echo esc_html($data['user_login']); ?>
534 <?php elseif($field == 'groupid') : ?>
535 <?php $data = $wpforo->usergroup->get_usergroup($item[$field]); echo esc_html($data['name']); ?>
536 <?php elseif($field == 'rank') : ?>
537 <div class="author-rating"><div class="bar wpfw-<?php echo esc_attr($wpforo->member->rating_level($item['posts'])); ?> wpfbg-4"></div></div>
538 <?php else : ?>
539 <?php wpfo($item[$field], true, 'esc_html') ?>
540 <?php endif; ?>
541 </td>
542
543 <?php endif; $i++; ?>
544
545 <?php endforeach; ?>
546 </tr>
547 <?php endforeach; ?>
548 <?php endif; ?>
549 <?php else : ?>
550 <tr class="no-items"><td class="colspanchange" colspan="7"><?php _e('No items found', 'wpforo') ?></td></tr>
551 <?php endif; ?>
552 </tbody>
553 </table>
554
555 <div class="tablenav bottom">
556 <div class="alignleft actions">
557 <select name="action2">
558 <option value="-1" selected="selected"><?php _e('Bulk Actions', 'wpforo') ?></option>
559 <?php foreach( $bulk_actions as $bulk_action ) : ?>
560 <option value="<?php echo $bulk_action ?>"><?php _e($bulk_action, 'wpforo'); ?></option>
561 <?php endforeach ?>
562 </select>
563 <input type="button" onclick="wpforo_doaction()" id="doaction2" class="button action" value="Apply">
564 </div>
565 <div class="alignleft actions"></div>
566 <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>
567 <span class="pagination-links">
568 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 2 ) : ?>
569 <span class="tablenav-pages-navspan">&laquo;</span>
570 <?php else : ?>
571 <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>
572 <?php endif ?>
573 <?php if( !isset($_GET['paged']) || $_GET['paged'] <= 1 ) : ?>
574 <span class="tablenav-pages-navspan"></span>
575 <?php else : ?>
576 <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>
577 <?php endif ?>
578
579 <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>
580
581 <?php if( isset($_GET['paged']) && $_GET['paged'] >= $pages_count ) : ?>
582 <span class="tablenav-pages-navspan"></span>
583 <?php else : ?>
584 <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>
585 <?php endif ?>
586 <?php if( (isset($_GET['paged']) && $_GET['paged'] >= $pages_count - 1) || 1 >= $pages_count - 1 ) : ?>
587 <span class="tablenav-pages-navspan">&raquo;</span>
588 <?php else : ?>
589 <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>
590 <?php endif ?>
591 </div> <br class="clear">
592 </div>
593 </form>
594 <?php endif; ?>
595 <?php
596 }
597
598 function wpforo_phrase($key, $echo = TRUE, $format = 'first-upper'){
599 global $wpforo;
600 $phrase = (isset($wpforo->phrases[addslashes(strtolower($key))])) ? $wpforo->phrases[addslashes(strtolower($key))] : $key;
601 if( $key == $phrase ){
602 $phrase = __($key, 'wpforo');
603 }
604
605 if( $format == 'first-upper' ){
606 $phrase = ucfirst($phrase);
607 }
608 elseif( $format == 'upper' ){
609 if(function_exists('mb_strtoupper')){
610 $phrase = mb_strtoupper($phrase);
611 }
612 else{
613 $phrase = strtoupper($phrase);
614 }
615 }
616 elseif( $format == 'lower' ){
617 if(function_exists('mb_strtolower')){
618 $phrase = mb_strtolower($phrase);
619 }
620 else{
621 $phrase = strtolower($phrase);
622 }
623 }
624
625 $phrase = str_replace('{number}', '', $phrase);
626
627 if($echo){
628 echo $phrase;
629 }else{
630 return $phrase;
631 }
632 }
633
634 function wpforo_screen_option(){ ?>
635
636 <div id="screen-meta" class="metabox-prefs" style="display: none; ">
637 <div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="Screen Options Tab" style="display: none; ">
638 <form id="adv-settings" action="" method="POST">
639 <h5><?php _e('Show on screen', 'wpforo') ?></h5>
640
641 <div class="screen-options">
642 <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')) ?>">
643 <label for="edit_post_per_page"><?php _e('Items', 'wpforo') ?></label>
644 <input type="submit" name="screen-options-apply" id="screen-options-apply" class="button" value="<?php _e('Apply', 'wpforo') ?>">
645 </div>
646 </form>
647 </div>
648 </div>
649
650 <div id="screen-meta-links">
651 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle" style="">
652 <a href="#screen-options-wrap" id="show-settings-link" class="show-settings screen-meta-active" aria-controls="screen-options-wrap" aria-expanded="true">
653 <?php _e('Screen Options', 'wpforo') ?>
654 </a>
655 </div>
656 </div>
657
658 <?php
659 }
660
661
662 function wpforo_text( $text, $length, $echo = TRUE ){
663 $text = preg_replace('#(?:[^\'\"]|^)(https?://[^\s\'\"<>]+)(?:[^\'\"]|$)#isu', '', $text);
664 $text = preg_replace('#\[attach[^\[\]]*\][^\[\]]*\[/attach\]#isu', '', $text);
665 $text = strip_tags($text);
666 if(function_exists('mb_substr')){
667 if($echo){
668 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 ? '...' : '' ) );
669 }else{
670 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 ? '...' : '' ) );
671 }
672 }else{
673 if($echo){
674 echo trim( substr( $text, 0, $length ) . ( strlen($text) > $length ? '...' : '' ) );
675 }else{
676 return trim( substr( $text, 0, $length ) . ( strlen($text) > $length ? '...' : '' ) );
677 }
678 }
679 }
680
681 function wpforo_admin_options_tabs( $tabs, $current = 'general', $subtab = FALSE, $sub_current = 'general' ) {
682 if(!empty($tabs)){
683 $class_attr = $subtab ? 'vert_tab' : '';
684 echo '<h2 class="nav-tab-wrapper ' . esc_attr($class_attr) . '">';
685 foreach( $tabs as $tab => $name ){
686 $class = ( $tab == $current || ($subtab && $tab == $sub_current ) ) ? ' nav-tab-active' : '';
687 $sub = $subtab ? '&subtab='.esc_attr($tab) : '';
688 $class = esc_attr($class);
689 $class_attr = $class_attr;
690 $current = esc_attr($current);
691 $tab = esc_attr($tab);
692 $sub = esc_attr($sub);
693 $name = esc_html($name);
694 echo "<a class='nav-tab$class $class_attr' href='?page=wpforo-settings&tab=". ($subtab ? $current : $tab ) ."$sub'>$name</a>";
695 }
696 echo '</h2>';
697 }
698 }
699
700 function wpforo_content_filter( $content ){
701 $content = apply_filters('wpforo_body_text_filter', $content);
702 $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);
703 $content = preg_replace('#([^\'\"]|^)(https?://[^\s\'\"<>]+)([^\'\"]|$)#isu', '$1 <a class="wpforo-auto-embeded-link" href="$2" target="_blank">$2</a> $3', $content);
704 if(preg_match_all('#<pre([^<>]*)>(.*?class=[\'"]wpforo-auto-embeded[^\'"]*[\'"].*?)</pre>#isu', $content, $matches, PREG_SET_ORDER)){
705 foreach($matches as $match){
706 $match[2] = preg_replace('#<img[^<>]*class=[\'"]wpforo-auto-embeded-image[\'"][^<>]*src=[\'"]([^\'"]*)[\'"][^<>]*>#isu', '$1', $match[2]);
707 $match[2] = preg_replace('#<a[^<>]*class=[\'"]wpforo-auto-embeded-link[\'"][^<>]*href=[\'"]([^\'"]*)[\'"][^<>]*>.*?</a>#isu', '$1', $match[2]);
708 $content = str_replace($match[0], '<pre'.$match[1].'>'.$match[2].'</pre>', $content);
709 }
710 }
711 $content = preg_replace('#(<a[^<>]*>[^<>]*)<a[^<>]*class=[\'"]wpforo-auto-embeded-link[\'"][^<>]*href=[\'"]([^\'"]*)[\'"][^<>]*>[^<>]*</a>([^<>]*</a>)#isu', '$1$2$3', $content);
712 $content = apply_filters('wpforo_content_filter', $content);
713 return wpautop($content);
714 }
715
716 add_filter('wpforo_content_filter', 'wpforo_nofollow_tag', 20);
717 function wpforo_nofollow_tag( $content ){
718 $content = preg_replace_callback('|<a[^><]*href=[\'\"]+([^\'\"]+)[\'\"]+[^><]*>.+?</a>|is', 'wpforo_nofollow', $content);
719 return $content;
720 }
721 function wpforo_nofollow($match){
722 $url = get_bloginfo('url');
723 $parse = parse_url($url);
724 if( isset($match[0]) ) $link = $match[0];
725 if( isset($match[1]) && strpos($match[1], $parse['host']) === FALSE ){
726 $link = preg_replace('|(<a[^><]+)>|is', '$1 rel="nofollow">', $match[0]);
727 }
728 return $link;
729 }
730
731 add_action('wpforo_bottom_hook', 'wpforo_page_logging');
732 function wpforo_page_logging(){
733 global $wpforo;
734 $data = $wpforo->current_object;
735
736 if( $data['template'] == 'post' && isset($data['topicid']) && $data['topicid'] ){
737
738 $current_user_id = get_current_user_id();
739 $current_time = current_time( 'timestamp', 1 );
740
741 if( $current_user_id ){
742 //registered user
743 $view = $wpforo->db->get_row("SELECT `vid`, `created` FROM `". $wpforo->db->prefix . "wpforo_views` WHERE `topicid` = " . intval($data['topicid']) ." AND `userid` = " . intval($current_user_id), ARRAY_A);
744 if( !$view['vid'] ){
745 $sql = "INSERT INTO ". $wpforo->db->prefix ."wpforo_views( `userid` , `topicid` , `created` ) VALUES ( '".intval($current_user_id)."', " . intval($data['topicid']) . ", '" . esc_sql($current_time) . "' ) ";
746 $wpforo->db->query($sql);
747 $wpforo->db->query("UPDATE `".$wpforo->db->prefix."wpforo_topics` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
748 }else{
749 $sql = "UPDATE ". $wpforo->db->prefix ."wpforo_views SET `created` = '" . esc_sql($current_time) . "' WHERE `userid` = " . intval($current_user_id) . " AND `topicid` = " . intval($data['topicid']);
750 $wpforo->db->query($sql);
751 if( $current_time - $view['created'] > 86400 ){
752 $wpforo->db->query("UPDATE `".$wpforo->db->prefix."wpforo_topics` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
753 }
754 }
755 }else{
756 //Guest user
757 $viwed_topics_arr = wpforo_getcookie( 'wpforo_view_topics' );
758 if( !in_array( $data['topicid'] , (array)$viwed_topics_arr ) ){
759 $wpforo->db->query("UPDATE `".$wpforo->db->prefix."wpforo_topics` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid']));
760 $viwed_topics_arr[] = $data['topicid'];
761 $args['topics'] = $viwed_topics_arr;
762 wpforo_setcookie( 'wpforo_view_topics' , $args );
763 }
764 }
765 }
766 }
767
768 add_action( 'init', 'wpforo_setcookie' );
769 function wpforo_setcookie( $mode = '', $args = array() ) {
770 if( $mode == 'wpforo_view_topics' && $args['topics']){
771 $args['topics'] = trim( implode( '|', $args['topics'] ), '|' );
772 @setcookie( 'wpforo_view_topics', $args['topics'] , time() + 86400, COOKIEPATH, COOKIE_DOMAIN );
773 }
774 }
775
776 add_action( 'wp_head', 'wpforo_getcookie' );
777 function wpforo_getcookie( $mode ) {
778 if( $mode == 'wpforo_view_topics' ){
779 if( isset($_COOKIE['wpforo_view_topics']) && $_COOKIE['wpforo_view_topics'] ){
780 return explode('|', $_COOKIE['wpforo_view_topics']);
781 }else{
782 return FALSE;
783 }
784 }
785 }
786
787 //Option value filter and output
788 function wpfo( $option = '', $echo = true, $esc = 'esc_attr' ){
789 if( is_string($option) ){
790 $option = stripslashes( $option );
791 if( $esc == 'esc_attr' ){
792 $option = esc_attr( $option );
793 }
794 elseif( $esc == 'esc_html' ){
795 $option = esc_html( $option );
796 }
797 elseif( $esc == 'esc_url' ){
798 $option = esc_url( $option );
799 }
800 elseif( $esc == 'esc_textarea' ){
801 $option = esc_textarea( $option );
802 }
803 }
804
805 if( $echo ){
806 echo $option;
807 }
808 else{
809 return $option;
810 }
811 }
812
813 //Option maker for checkbox, radio and select
814 function wpfo_check( $option = '', $value = '', $type = 'checked' , $echo = true ){
815 $option = (isset($option) && isset($value) && $option == $value ) ? $type : '';
816 if( $echo ){
817 echo $option;
818 }
819 else{
820 return $option;
821 }
822 }
823
824 function wpforo_human_filesize($bytes, $decimals = 2) {
825 $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
826 $factor = floor((strlen($bytes) - 1) / 3);
827 return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . '&nbsp;' . @$size[$factor];
828 }
829
830 function wpforo_date($date, $type = 'ago', $echo = true ) {
831 global $wpforo;
832 $d = $date;
833 $sep = ' ';
834 $timestamp = strtotime($date);
835 $timezone_string = get_option('timezone_string');
836 $current_offset = get_option('gmt_offset');
837 if(!is_string($type)) $type = 'ago';
838
839 if( is_user_logged_in() && !empty($wpforo->current_user) ){
840 if( isset($wpforo->current_user['timezone']) && $wpforo->current_user['timezone'] != '' ){
841 if(preg_match('|UTC([\-\+]+.?)|is', $wpforo->current_user['timezone'], $timezone_array)){
842 $timezone_string = '';
843 $current_offset = str_replace('+', '', $timezone_array[1]);
844 }
845 else{
846 if(in_array($wpforo->current_user['timezone'], timezone_identifiers_list())){
847 $timezone_string = $wpforo->current_user['timezone'];
848 $current_offset = '';
849 }
850 else{
851 $timezone_string = '';
852 $current_offset = '';
853 }
854 }
855 }
856 }
857
858 if( $timezone_string == '' && $current_offset != '' ){
859 $timezone_string = timezone_name_from_abbr('', $current_offset * 3600, false);
860 }
861
862 if( class_exists('DateTime') && class_exists('DateTimeZone') && $timezone_string ){
863 $dt = new DateTime("now", new DateTimeZone($timezone_string));
864 if( method_exists($dt, 'setTimestamp') ) $dt->setTimestamp($timestamp); //DateTime::setTimestamp() available in PHP 5.3 and higher versions
865 if( $type == 'human' ){
866 $d = human_time_diff($timestamp);
867 }
868 elseif( $type == 'ago' ){
869 $d = human_time_diff($timestamp) . '&nbsp;';
870 $d = sprintf( wpforo_phrase('%s ago', false, false), $d );
871 }
872 else{
873 if( wpforo_feature('wp-date-format', $wpforo) ){
874 $date_format = get_option('date_format');
875 $time_format = get_option('time_format');
876 $type = $date_format . $sep . $time_format;
877 }
878 $d = date_i18n($type, strtotime($dt->format('Y-m-d H:i:s')));
879 }
880 }
881 else{
882
883 if( $type == 'human' ){
884 $d = human_time_diff($timestamp);
885 }
886 elseif( $type == 'ago' ){
887 $d = human_time_diff($timestamp) . '&nbsp;';
888 $d = sprintf( wpforo_phrase('%s ago', false, false), $d );
889 }
890 else{
891 if( wpforo_feature('wp-date-format', $wpforo) ){
892 $date_format = get_option('date_format');
893 $time_format = get_option('time_format');
894 $type = $date_format . $sep . $time_format;
895 }
896 $d = date_i18n( $type, $timestamp);
897 }
898 }
899
900 if( $echo ){
901 echo $d;
902 }
903 else{
904 return $d;
905 }
906 }
907
908
909 function wpforo_write_file( $new_file, $content ){
910 $return = array( 'error' => false, 'file' => '' );
911 $ifp = @fopen( $new_file, 'wb' );
912 if ( ! $ifp ) {
913 $return = array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) );
914 }
915 else{
916 @fwrite( $ifp, $content );
917 fclose( $ifp );
918 clearstatcache();
919 // Set correct file permissions
920 $stat = @stat( dirname( $new_file ) );
921 $perms = $stat['mode'] & 0007777;
922 $perms = $perms & 0000666;
923 @chmod( $new_file, $perms );
924 clearstatcache();
925 $return = array( 'file' => $new_file );
926 }
927 return $return;
928 }
929
930
931 function wpforo_get_file_content( $file ){
932 $fp = @fopen( $file, 'r' );
933 if( !$fp ){
934 return false;
935 }
936 else{
937 $file_data = fread( $fp, filesize($file) );
938 fclose( $fp );
939 return $file_data;
940 }
941 }
942
943 ################################################################################
944 /**
945 * Clears file basename and removes trailing slash
946 *
947 * @since 1.0.0
948 *
949 * @param string filename
950 *
951 * @return string
952 */
953 function wpforo_clear_basename( $file ) {
954 $file = str_replace('\\','/',$file);
955 $file = preg_replace('|/+|','/', $file);
956 $file = trim($file, '/');
957 return $file;
958 }
959
960 #################################################################################
961 /**
962 * Removes directory with all files and folders
963 *
964 * @since 1.0.0
965 *
966 * @param string directory name
967 *
968 */
969 function wpforo_remove_directory( $directory ) {
970 $directory = trim( $directory, '/') . '/';
971 foreach( glob( $directory . '/*' ) as $item ) {
972 if( is_dir( $item ) ){
973 wpforo_remove_directory( $item );
974 }
975 else{
976 unlink( $item );
977 }
978 }
979 return rmdir( $directory );
980 }
981 #################################################################################
982 /**
983 * Converts bytes to KB, MB, GB
984 *
985 * @since 1.0.0
986 *
987 * @param integer Bytes
988 *
989 * @return string
990 */
991 function wpforo_print_size($value, $points = true ){
992
993 if($value <= 1024)
994 {
995 return $value . (($points) ? " B" : '' );
996 }
997 if($value > 1024 && $value <= (1024*1024))
998 {
999 $value=round(($value/1024)*10)/10;
1000 return $value . (($points) ? " KB" : '' );
1001 }
1002 if($value > 1024*1024 && $value <= 1024*1024*1024)
1003 {
1004 $value=round(($value/(1024*1024))*10)/10;
1005 return $value . (($points) ? " MB" : '' );
1006 }
1007 if($value > 1024*1024*1024 && $value <= 1024*1024*1024*1024)
1008 {
1009 $value=round(($value/(1024*1024*1024))*10)/10;
1010 return $value . (($points) ? " GB" : '' );
1011 }
1012 }
1013
1014 function wpforo_human_size_to_bytes($sSize)
1015 {
1016 if ( is_numeric( $sSize) ) {
1017 return $sSize;
1018 }
1019 $sSuffix = substr($sSize, -1);
1020 $iValue = substr($sSize, 0, -1);
1021 switch(strtoupper($sSuffix)){
1022 case 'P':
1023 $iValue *= 1024;
1024 case 'T':
1025 $iValue *= 1024;
1026 case 'G':
1027 $iValue *= 1024;
1028 case 'M':
1029 $iValue *= 1024;
1030 case 'K':
1031 $iValue *= 1024;
1032 break;
1033 }
1034 return $iValue;
1035 }
1036
1037
1038 function wpforo_print_number($n, $echo = false) {
1039 $n = (0+str_replace(",","",$n));
1040 if(!is_numeric($n)) return false;
1041 if($n>1000000000000) return round(($n/1000000000000),1).' '.wpforo_phrase('{number}T',false);
1042 else if($n>1000000000) return round(($n/1000000000),1).' '.wpforo_phrase('{number}B',false);
1043 else if($n>1000000) return round(($n/1000000),1).' '.wpforo_phrase('{number}M',false);
1044 else if($n>10000) return round(($n/1000),1).' '.wpforo_phrase('{number}K',false);
1045 if($echo){
1046 echo number_format($n);
1047 }
1048 else{
1049 return number_format($n);
1050 }
1051 }
1052
1053 function wpforo_bigintval($value) {
1054 $value = trim($value);
1055 if (ctype_digit($value)) {
1056 return $value;
1057 }
1058 $value = preg_replace("/[^0-9](.*)$/", '', $value);
1059 if (ctype_digit($value)) {
1060 return $value;
1061 }
1062 return 0;
1063 }
1064
1065 function wpforo_removebb($string){
1066 if(isset($string) && $string ){
1067 $string = preg_replace('|\[\/*[^\]\[]+\]|is', '', $string);
1068 }
1069 return $string;
1070 }
1071
1072 function wpforo_file_upload_error($code){
1073 switch ($code) {
1074 case UPLOAD_ERR_INI_SIZE:
1075 $message = wpforo_phrase("The uploaded file exceeds the upload_max_filesize directive in php.ini", false);
1076 break;
1077 case UPLOAD_ERR_FORM_SIZE:
1078 $message = wpforo_phrase("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", false);
1079 break;
1080 case UPLOAD_ERR_PARTIAL:
1081 $message = wpforo_phrase("The uploaded file was only partially uploaded", false);
1082 break;
1083 case UPLOAD_ERR_NO_FILE:
1084 $message = wpforo_phrase("No file was uploaded", false);
1085 break;
1086 case UPLOAD_ERR_NO_TMP_DIR:
1087 $message = wpforo_phrase("Missing a temporary folder", false);
1088 break;
1089 case UPLOAD_ERR_CANT_WRITE:
1090 $message = wpforo_phrase("Failed to write file to disk", false);
1091 break;
1092 case UPLOAD_ERR_EXTENSION:
1093 $message = wpforo_phrase("File upload stopped by extension", false);
1094 break;
1095 default:
1096 $message = wpforo_phrase("Unknown upload error", false);
1097 break;
1098 }
1099 return $message;
1100 }
1101
1102 //$key allowed values are post, strip, data, user_description entities or the name of a field filter such as pre_user_description.
1103 //More info https://core.trac.wordpress.org/browser/tags/4.5.2/src/wp-includes/kses.php#L624
1104 function wpforo_kses( $string = '', $key = 'data' ){
1105
1106 if(!$string || !$key) return $string;
1107 if( $key == 'email' ){
1108 $allowed_html = array( 'a' => array( 'href' => array(), 'title' => array()),
1109 'blockquote' => array(),
1110 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(),
1111 'hr' => array(),
1112 'br' => array(),
1113 'p' => array(),
1114 'strong' => array());
1115 }
1116 else{
1117 $allowed_html = wp_kses_allowed_html( $key );
1118 if( $key == 'user_description' ){
1119 $allowed_html['img'] = array( 'alt' => array(), 'align' => array(), 'border' => array(), 'height' => array(), 'hspace' => array(), 'longdesc' => array(), 'vspace' => array(), 'src' => array(), 'usemap' => array(), 'width' => array());
1120 }
1121 else{
1122 global $allowedposttags;
1123 $allowed_html = wpforo_parse_args($allowed_html, $allowedposttags);
1124
1125 $allowed_html['a']['data-gallery'] = array();
1126 $allowed_html['a']['download'] = array();
1127 $allowed_html['iframe']['width'] = array();
1128 $allowed_html['iframe']['height'] = array();
1129 $allowed_html['iframe']['src'] = array();
1130 $allowed_html['iframe']['frameborder'] = array();
1131 $allowed_html['iframe']['allowfullscreen'] = array();
1132 $allowed_html['embed']['flashVars'] = array();
1133 $allowed_html['embed']['src'] = array();
1134 $allowed_html['embed']['width'] = array();
1135 $allowed_html['embed']['height'] = array();
1136 $allowed_html['embed']['wmode'] = array();
1137 $allowed_html['embed']['allowFullScreen'] = array();
1138 $allowed_html['embed']['allowScriptAccess'] = array();
1139 $allowed_html['embed']['name'] = array();
1140 $allowed_html['embed']['pluginspage'] = array();
1141 $allowed_html['embed']['type'] = array();
1142 $allowed_html['blockquote']['class'] = TRUE;
1143 $allowed_html['blockquote']['data-width'] = TRUE;
1144 $allowed_html['p']['lang'] = TRUE;
1145 $allowed_html['p']['dir'] = TRUE;
1146
1147 }
1148 }
1149
1150
1151 return wp_kses( $string, $allowed_html );
1152 }
1153
1154 function wpforo_deep_merge($default, $current = array()){
1155 foreach($default as $k => $v){
1156 if(!empty($v) && is_array($v)){
1157 foreach($v as $kk => $vv){
1158 if(!empty($vv) && is_array($vv)){
1159 foreach($vv as $kkk => $vvv){
1160 if(!empty($vvv) && is_array($vvv)){
1161 foreach($vvv as $kkkk => $vvvv){
1162 if(!empty($vvv) && is_array($vvv)){
1163 //Stop on 5th level
1164 }
1165 else{
1166 if(isset($current[$k][$kk][$kkk][$kkkk])) $default[$k][$kk][$kkk][$kkkk] = $current[$k][$kk][$kkk][$kkkk];
1167 }
1168 }
1169 }
1170 else{
1171 if(isset($current[$k][$kk][$kkk])) $default[$k][$kk][$kkk] = $current[$k][$kk][$kkk];
1172 }
1173 }
1174 }
1175 else{
1176 if(isset($current[$k][$kk])) $default[$k][$kk] = $current[$k][$kk];
1177 }
1178 }
1179 }
1180 else{
1181 if(isset($current[$k])) $default[$k] = $current[$k];
1182 }
1183 }
1184 return $default;
1185 }
1186
1187
1188 function wpforo_is_image($e){
1189 $is_image = false;
1190 $e = strtolower($e);
1191 if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){
1192 $is_image = true;
1193 }
1194 return $is_image;
1195 }
1196
1197 function get_wpf_option( $key ){
1198 global $wpdb;
1199 $value = get_option($key);
1200 if( $value ){
1201 $value = maybe_unserialize( $value );
1202 }
1203 else{
1204 $value = $wpdb->get_var("SELECT `option_value` FROM `".$wpdb->prefix."options` WHERE `option_name` = '". esc_sql($key) . "'");
1205 if(is_serialized( $value )) {
1206 $check = @unserialize($value);
1207 if( !$check ) $value = wpforo_fixSerializedArray($value);
1208 if( !$value && $value !== 0 ) return NULL;
1209 }
1210 }
1211 return $value;
1212 }
1213
1214 /**
1215 * Extract what remains from an unintentionally truncated serialized string
1216 * $data contains your original array (or what remains of it).
1217 * @param string The serialized array
1218 */
1219 function wpforo_fixSerializedArray($serialized){
1220 $tmp = preg_replace('/^a:\d+:\{/', '', $serialized);
1221 return wpforo_fixSerializedArray_R($tmp);
1222 }
1223 /**
1224 * The recursive function that does all of the heavy lifing. Do not call directly.
1225 * @param string The broken serialzized array
1226 * @return string Returns the repaired string
1227 */
1228 function wpforo_fixSerializedArray_R(&$broken){
1229 $data = array();
1230 $index = NULL;
1231 $len = strlen($broken);
1232 $i = 0;
1233 while(strlen($broken)) {
1234 $i++;
1235 if ($i > $len) { break; }
1236 if (substr($broken, 0, 1) == '}') {
1237 $broken = substr($broken, 1); return $data;
1238 }
1239 else{
1240 $bite = substr($broken, 0, 2);
1241 switch($bite) {
1242 case 's:':
1243 $re = '/^s:\d+:"([^\"]*)";/';
1244 if (preg_match($re, $broken, $m)){
1245 if ($index === NULL){ $index = $m[1]; }
1246 else{$data[$index] = $m[1]; $index = NULL;}
1247 $broken = preg_replace($re, '', $broken);
1248 }
1249 break;
1250 case 'i:':
1251 $re = '/^i:(\d+);/';
1252 if (preg_match($re, $broken, $m)){
1253 if ($index === NULL){$index = (int) $m[1]; }
1254 else{$data[$index] = (int) $m[1]; $index = NULL; }
1255 $broken = preg_replace($re, '', $broken);
1256 }
1257 break;
1258 case 'b:':
1259 $re = '/^b:[01];/';
1260 if (preg_match($re, $broken, $m)){
1261 $data[$index] = (bool) $m[1]; $index = NULL; $broken = preg_replace($re, '', $broken);
1262 }
1263 break;
1264 case 'a:':
1265 $re = '/^a:\d+:\{/';
1266 if (preg_match($re, $broken, $m)){
1267 $broken = preg_replace('/^a:\d+:\{/', '', $broken); $data[$index] = wpforo_fixSerializedArray_R($broken); $index = NULL;
1268 }
1269 break;
1270 case 'N;':
1271 $broken = substr($broken, 2); $data[$index] = NULL; $index = NULL;
1272 break;
1273 }
1274 }
1275 }
1276 return $data;
1277 }
1278
1279 function wpforo_insert_to_media_library( $attach_path, $title = '' ){
1280 if( wpforo_feature('attach-media-lib') ){
1281 if(!$attach_path ) return 0;
1282 $attach_fname = basename($attach_path);
1283 if(!$title) $title = $attach_fname;
1284 require_once(ABSPATH . 'wp-admin/includes/media.php');
1285 require_once(ABSPATH . 'wp-admin/includes/file.php');
1286 require_once(ABSPATH . 'wp-admin/includes/image.php');
1287 $wp_upload_dir = wp_upload_dir();
1288 $filetype = wp_check_filetype( $attach_fname, NULL );
1289 $attachment = array( 'guid' => $attach_path, 'post_mime_type' => $filetype['type'], 'post_title' => $title, 'post_content' => '', 'post_status' => 'inherit');
1290 $attach_id = wp_insert_attachment( $attachment, $attach_path );
1291 add_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' );
1292 $attach_data = wp_generate_attachment_metadata( $attach_id, $attach_path );
1293 wp_update_attachment_metadata( $attach_id, $attach_data );
1294 remove_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' );
1295 return $attach_id;
1296 }
1297 }
1298
1299 function wpforo_attachment_sizes( $sizes ){
1300 return array('thumbnail');
1301 }
1302
1303 function wpforo_debug( $wpforo ){
1304 if( wpforo_feature('debug-mode', $wpforo) ) : ?>
1305 <div id="wpforo-debug" style="display:none">
1306 <h4>Current Object</h4>
1307 <p><?php @print_r($wpforo->current_object); ?></p>
1308 <h4>Super Globals</h4>
1309 <p>Requests: <?php print_r($_REQUEST); ?></p>
1310 <p>Server: <?php print_r($_REQUEST); ?></p>
1311 <h4>Options and Features</h4>
1312 <textarea style="width:500px; height:300px;"><?php echo @ 'permastruct: ' . $wpforo->permastruct . "\r\n";
1313 echo @ 'use_home_url: ' . $wpforo->use_home_url . "\r\n";
1314 echo @ 'url: ' . $wpforo->url . "\r\n";
1315 @print_r($wpforo->general_options) . "\r\n";
1316 echo @ 'pageid:' . $wpforo->pageid . "\r\n";
1317 echo @ 'default_groupid: ' . $wpforo->default_groupid . "\r\n";
1318 @print_r($wpforo->usergroup_cans) . "\r\n";
1319 @print_r($wpforo->forum_options) . "\r\n";
1320 @print_r($wpforo->forum_cans) . "\r\n";
1321 @print_r($wpforo->post_options) . "\r\n";
1322 @print_r($wpforo->member_options) . "\r\n";
1323 @print_r($wpforo->subscribe_options) . "\r\n";
1324 @print_r($wpforo->features) . "\r\n";
1325 @print_r($wpforo->style_options) . "\r\n";
1326 @print_r($wpforo->theme_options) . "\r\n";
1327 @print_r($wpforo->theme) . "\r\n";
1328 ?>
1329 </textarea>
1330 </div>
1331 <?php
1332 endif;
1333 }
1334
1335 function wpforo_hook( $name, $args = array() ){
1336 do_action( $name, $args );
1337 }
1338
1339 function wpforo_clean_cache( $key = 'post' ){
1340 if($key == 'post'){
1341 global $post;
1342 if(isset($post) && is_object($post) && !empty($post)) {
1343 clean_post_cache( $post );
1344 }
1345 else{
1346 $pageid = url_to_postid( $_SERVER['REQUEST_URI'] );
1347 $page = get_post( $pageid );
1348 clean_post_cache( $page );
1349 }
1350 }
1351 }
1352 ?>