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

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