user_trailingslashit( home_url($str) ); //-START- check is url maybe wordpress home $maybe_home_url = trim( preg_replace('#/?index\.php/?(\?.*)?$#isu', '', $url), '/\\' ); $home_url = trim( home_url(), '/\\' ); if( $maybe_home_url == $home_url ){ $url = preg_replace('#index\.php/?#isu', '', $url, 1); $url = $wpforo->user_trailingslashit($url); } //-END- check is url maybe wordpress home } else{ echo $url = $wpforo->user_trailingslashit( $str ); } if(!$echo) return $url; echo $url; } function wpforo_is_ajax(){ if( defined('DOING_AJAX') && DOING_AJAX ) return TRUE; return FALSE; } function wpforo_is_admin(){ if( is_admin() && !wpforo_is_ajax() ) return TRUE; return FALSE; } function is_wpforo_page($url = ''){ if( wpforo_is_admin() ) return FALSE; if(!$url) $url = wpforo_get_request_uri(); if( is_wpforo_exclude_url($url) ) return FALSE; if( is_wpforo_shortcode_page() ) return TRUE; return is_wpforo_url($url); } function is_wpforo_exclude_url($url = ''){ global $wpforo; if(!$url) $url = wpforo_get_request_uri(); $url = urldecode($url); $url = preg_replace('#/page/\d*/?$#isu', '', $url); if( !$current_url = wpforo_get_url_query_vars_str($url) ) return FALSE; if( preg_match('#\.php/?(?:\?[^/]*)?$#isu', $current_url) ) return TRUE; if( $wpforo->use_home_url && $wpforo->excld_urls ){ $expld = array_filter( explode("\n", $wpforo->excld_urls) ); foreach( $expld as $excld_url ){ $excld_url = urldecode($excld_url); if( strpos($excld_url, home_url('/')) === FALSE ) continue; $excld_url = wpforo_get_url_query_vars_str($excld_url); if( $excld_url && strtolower($current_url) == strtolower($excld_url) ) return TRUE; } } return FALSE; } function is_wpforo_url($url = ''){ if( wpforo_is_admin() ) return FALSE; if(!$url) $url = wpforo_get_request_uri(); global $wpforo; if( $wpforo->use_home_url ) return TRUE; $current_url = wpforo_get_url_query_vars_str($url); if( $wpforo->permastruct && strpos($current_url, $wpforo->permastruct) !== FALSE && strpos($current_url, $wpforo->permastruct) == 0 && !wpforo_is_admin() ) return TRUE; return FALSE; } function is_wpforo_shortcode_page(){ if( wpforo_is_admin() ) return FALSE; global $wpforo, $post; if( $post && isset($post->post_content) && has_shortcode( $post->post_content, 'wpforo' ) && !is_wpforo_url() ) return TRUE; return FALSE; } function wpforo_get_url_query_vars_str($url = ''){ if(!$url) $url = wpforo_get_request_uri(); $home_url = preg_replace( '#/?\?.*$#isu', '', home_url('/') ); $current_url = preg_replace('#https?://[^/\?]+/?#isu', '', $url); $site_url = preg_replace('#https?://[^/\?]+/?#isu', '', $home_url); $current_url = preg_replace( '#^/?'.preg_quote($site_url).'(?:/?index\.php/?)?#isu', '' , $current_url, 1 ); $current_url = preg_replace('#^[\r\n\t\s\0/]*(.*?)[\r\n\t\s\0/]*$#isu', '$1', $current_url); return $current_url; } function wpforo_feature( $option, $wpforo = NULL ){ if( empty($wpforo) ) global $wpforo; if( isset($wpforo->features[$option]) && $wpforo->features[$option] ){ return true; } else{ return false; } } function wpforo_dir_size($directory) { $size = 0; if(class_exists('RecursiveIteratorIterator') && class_exists('RecursiveDirectoryIterator')){ foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) $size+=$file->getSize(); return $size; } else{ return 0; } } function wpforo_make_hidden_fields_from_url($url = '', $echo = true){ if( !$url ) $url = wpforo_get_request_uri(); $return = ''; if( $url_query = parse_url($url, PHP_URL_QUERY) ){ parse_str($url_query, $url_query_arr); if( !empty($url_query_arr) ){ foreach ($url_query_arr as $key => $value) { $return .= ''; } } } if( !$echo ) return $return; echo $return; } ################################################################################# /** * Returns merged arguments array from defined and default arguments. * @param mixed $args * @param array $default * @return array */ function wpforo_parse_args( $args, $default = array() ) { if( is_object($args) ) { $defined = get_object_vars($args); } elseif( is_array($args) ) { $defined = $args; } elseif( preg_match('|^[\d\,\s]+$|', $args) ){ $defined = explode(',', trim($args)); } elseif( is_integer($args) || is_float($args) ){ $defined[0] = $args; } elseif( is_serialized($args) ){ $defined = unserialize($args); } elseif( strpos($args, '=') !== FALSE ) { parse_str($args, $defined); } else{ $defined[0] = $args; } if(!empty($default)) { return array_merge( $default, $defined ); } else { return $defined; } } // ############################################################################# /** * Detects serialized data * * @since 1.0.0 * * @param string * * @return boolean */ if(!function_exists('is_serialized')){ function is_serialized( $value ){ if( $value == '' ) return false; if( $value === 0 ) return false; $value = trim($value); $chsd = @unserialize($value); if( $chsd !== false || $value === 'b:0;' ) { return true; } else{ return false; } } } function wpforo_get_request_uri($with_port = FALSE, $get_referer_when_ajax = TRUE){ if( $get_referer_when_ajax && wpforo_is_ajax() ){ if( isset($_SERVER['HTTP_REFERER']) ){ return $_SERVER['HTTP_REFERER']; } } $s = empty($_SERVER["HTTPS"]) ? '' : (strtolower($_SERVER["HTTPS"]) == "on") ? "s" : ""; $sp = strtolower($_SERVER["SERVER_PROTOCOL"]); $protocol = substr($sp, 0, strpos($sp, "/")) . $s; $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]); return $protocol . "://" . $_SERVER['HTTP_HOST'] . ($with_port ? $port : '') . $_SERVER['REQUEST_URI']; } function wpforo_arr_group_by($array, $key_by){ if(!empty($array)){ $fltrd = array(); foreach($array as $key => $arr){ if(is_numeric($key)) $fltrd[] = $arr[$key_by]; } $uniq_arr = array_unique($fltrd); asort($uniq_arr); return $uniq_arr; } } // ############################################################################# /** * Print item's (topics, replyes ...) table in dashboard * @since 1.0.0 * @param string item(component) class name * @param string This table primary key field name * @param array table fields * @param array table search fields * @param array table filter fields * @param array action buttons ( full posible values $actions = array( 'edit', 'delete', 'view' ); ) * * @return echo html table */ function wpforo_create_form_table($varname, $primary_key, $fields = array(), $search_fields = array(), $filter_fields = array(), $actions = array(), $bulk_actions = array() ){ if(empty($actions)) $actions = array( 'edit', 'delete', 'view' ); if(empty($bulk_actions)) $bulk_actions = array( 'del' ); if(!empty($fields)) : global $wpdb; global $wpforo; $args = array(); if(isset($_GET['s']) && $_GET['s'] != '') $args['include'] = $wpforo->$varname->search(sanitize_text_field($_GET['s']), $search_fields); if(isset($_GET['orderby'])) $args['orderby'] = sanitize_text_field($_GET['orderby']); if(isset($_GET['order'])) $args['order'] = sanitize_text_field($_GET['order']); if(isset($_GET['forumid']) && $_GET['forumid'] != 0) $args['forumid'] = intval($_GET['forumid']); if(isset($_GET['groupid']) && $_GET['groupid'] != 0) $args['groupid'] = intval($_GET['groupid']); if(isset($_GET['member_status']) && $_GET['member_status']) $args['status'] = (array) sanitize_text_field($_GET['member_status']); if(isset($_GET['phrase_package']) && $_GET['phrase_package']) $args['package'] = (array) sanitize_text_field($_GET['phrase_package']); if(isset($_GET['langid']) && $_GET['langid']) $args['langid'] = intval($_GET['langid']); if(isset($_GET['userid']) && $_GET['userid'] != 0) $args['userid'] = intval($_GET['userid']); $paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1; $args['offset'] = ($paged - 1) * get_option('wpforo_count_per_page'); $args['row_count'] = get_option('wpforo_count_per_page'); if($varname == 'reply'){ $func = 'get_replies'; }else{ $func = 'get_'.$varname.'s'; } $items_count = 0; if( $varname == 'member' && !isset($_GET['member_status']) ){ $args['status'] = array('active'); } $items = $wpforo->$varname->$func($args, $items_count); if(isset($args['include']) && empty($args['include'])){ $items_count = 0; $items = array(); } $pages_count = ceil($items_count/get_option('wpforo_count_per_page')); ?>
]*)>(.*?class=[\'"]wpforo-auto-embeded[^\'"]*[\'"].*?)#isu', $content, $matches, PREG_SET_ORDER)){ foreach($matches as $match){ $match[2] = preg_replace('#
'.$match[2].'', $content); } } $content = preg_replace('#(]*>[^<>]*)]*class=[\'"]wpforo-auto-embeded-link[\'"][^<>]*href=[\'"]([^\'"]*)[\'"][^<>]*>[^<>]*([^<>]*)#isu', '$1$2$3', $content); $content = apply_filters('wpforo_content_filter', $content); return wpautop($content); } function wpforo_remove_links( $content ){ return preg_replace('#([^\'\"]|^)(https?://[^\s\'\"<>]+)([^\'\"]|$)#isu', '$1 [' . wpforo_phrase('removed link', false) . '] $3', $content); } add_filter('wpforo_content_filter', 'wpforo_nofollow_tag', 20); function wpforo_nofollow_tag( $content ){ $content = preg_replace_callback('|<]*href=[\'\"]+([^\'\"]+)[\'\"]+[^><]*>.+?|is', 'wpforo_nofollow', $content); return $content; } function wpforo_nofollow($match){ $url = get_bloginfo('url'); $parse = parse_url($url); if( isset($match[0]) ) $link = $match[0]; if( isset($match[1]) && strpos($match[1], $parse['host']) === FALSE ){ $link = preg_replace('|(<]+)>|is', '$1 rel="nofollow">', $match[0]); } return $link; } add_action('wp_loaded', 'wpforo_cookie_logs', 10); function wpforo_cookie_logs(){ global $wpforo; if(!wpforo_feature('view-logging', $wpforo)) return; $key = ''; $logid = 0; $log = false; $data = $wpforo->current_object; if( $data['template'] == 'post' && isset($data['topicid']) && $data['topicid'] ){ $logid = $data['topicid']; $key = 'wpf_viewed_topics'; } elseif( $data['template'] == 'topic' && isset($data['forumid']) && $data['forumid'] ){ $logid = $data['forumid']; $key = 'wpf_viewed_forums'; } if( $logid && $key ) { $viwed_ids = wpforo_getcookie( $key, true ); if( !$viwed_ids ){ $log = true; $viwed_ids = array( $logid ); } elseif( is_array($viwed_ids) && !in_array( $logid , $viwed_ids ) ){ $log = true; $viwed_ids[] = $logid; } if( $log ){ wpforo_setcookie( $key, $viwed_ids, true ); } } } add_action('wpforo_bottom_hook', 'wpforo_user_logging'); function wpforo_user_logging(){ global $wpforo; $data = $wpforo->current_object; if( $data['template'] == 'post' && isset($data['topicid']) && $data['topicid'] ){ $current_user_id = get_current_user_id(); $current_time = current_time( 'timestamp', 1 ); if( $current_user_id ){ //registered user $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); if( !$view['vid'] ){ $sql = "INSERT INTO ". $wpforo->db->prefix ."wpforo_views( `userid` , `topicid` , `created` ) VALUES ( '".intval($current_user_id)."', " . intval($data['topicid']) . ", '" . esc_sql($current_time) . "' ) "; $wpforo->db->query($sql); $wpforo->db->query("UPDATE `".$wpforo->db->prefix."wpforo_topics` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid'])); }else{ $sql = "UPDATE ". $wpforo->db->prefix ."wpforo_views SET `created` = " . intval($current_time) . " WHERE `userid` = " . intval($current_user_id) . " AND `topicid` = " . intval($data['topicid']); $wpforo->db->query($sql); if( $current_time - $view['created'] > 86400 ){ $wpforo->db->query("UPDATE `".$wpforo->db->prefix."wpforo_topics` SET `views` = `views` + 1 WHERE `topicid` = " . intval($data['topicid'])); } } } } } add_action( 'init', 'wpforo_setcookie', 10, 2); function wpforo_setcookie( $key = '', $args = array(), $implode = false ) { if( !empty($args) && is_array($args) && $implode ) { $num = count($args); if( $num > 200 ){ $delta = $num - 200; if( $delta > 0 ) $args = array_slice($args, $delta); } $value = trim( implode( ',', $args ), ',' ); } elseif( !empty($args) && is_array($args) && !$implode ){ $value = serialize($args); } if( $key && $value ){ @setcookie( $key, $value , time() + 7776000, COOKIEPATH, COOKIE_DOMAIN ); } } add_action( 'wp_head', 'wpforo_getcookie' ); function wpforo_getcookie( $key = '', $explode = false ) { if( $key ){ if( isset($_COOKIE[$key]) && $_COOKIE[$key] ){ if($explode){ return explode(',', $_COOKIE[$key]); } else{ return $_COOKIE[$key]; } }else{ return FALSE; } } } //Option value filter and output function wpfo( $option = '', $echo = true, $esc = 'esc_attr' ){ if( is_string($option) ){ $option = stripslashes( $option ); if( $esc == 'esc_attr' ){ $option = esc_attr( $option ); } elseif( $esc == 'esc_html' ){ $option = esc_html( $option ); } elseif( $esc == 'esc_url' ){ $option = esc_url( $option ); } elseif( $esc == 'esc_textarea' ){ $option = esc_textarea( $option ); } } if( $echo ){ echo $option; } else{ return $option; } } //Option maker for checkbox, radio and select function wpfo_check( $option = '', $value = '', $type = 'checked' , $echo = true ){ $option = (isset($option) && isset($value) && $option == $value ) ? $type : ''; if( $echo ){ echo $option; } else{ return $option; } } function wpforo_human_filesize($bytes, $decimals = 2) { $size = array('B','KB','MB','GB','TB','PB','EB','ZB','YB'); $factor = floor((strlen($bytes) - 1) / 3); return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor]; } function wpforo_date($date, $type = 'ago', $echo = true ) { if(is_numeric($date)) $date = date_i18n( 'Y-m-d H:i:s', $date); global $wpforo; $d = $date; $sep = ' '; $timestamp = strtotime($date); $timezone_string = get_option('timezone_string'); $current_offset = get_option('gmt_offset'); if(!is_string($type)) $type = 'ago'; if( is_user_logged_in() && !empty($wpforo->current_user) ){ if( isset($wpforo->current_user['timezone']) && $wpforo->current_user['timezone'] != '' ){ if(preg_match('|UTC([\-\+]+.?)|is', $wpforo->current_user['timezone'], $timezone_array)){ $timezone_string = ''; $current_offset = str_replace('+', '', $timezone_array[1]); } else{ if(in_array($wpforo->current_user['timezone'], timezone_identifiers_list())){ $timezone_string = $wpforo->current_user['timezone']; $current_offset = ''; } else{ $timezone_string = ''; $current_offset = ''; } } } } if( $timezone_string == '' && $current_offset != '' ){ $timezone_string = timezone_name_from_abbr('', $current_offset * 3600, false); } if( class_exists('DateTime') && class_exists('DateTimeZone') && $timezone_string ){ $dt = new DateTime("now", new DateTimeZone($timezone_string)); if( method_exists($dt, 'setTimestamp') ) $dt->setTimestamp($timestamp); //DateTime::setTimestamp() available in PHP 5.3 and higher versions if( $type == 'human' ){ $d = human_time_diff($timestamp); } elseif( $type == 'ago' ){ $d = human_time_diff($timestamp) . ' '; $d = sprintf( wpforo_phrase('%s ago', false, false), $d ); } else{ if( wpforo_feature('wp-date-format', $wpforo) ){ $date_format = get_option('date_format'); $time_format = get_option('time_format'); $type = $date_format . $sep . $time_format; } $d = date_i18n($type, strtotime($dt->format('Y-m-d H:i:s'))); } } else{ if( $type == 'human' ){ $d = human_time_diff($timestamp); } elseif( $type == 'ago' ){ $d = human_time_diff($timestamp) . ' '; $d = sprintf( wpforo_phrase('%s ago', false, false), $d ); } else{ if( wpforo_feature('wp-date-format', $wpforo) ){ $date_format = get_option('date_format'); $time_format = get_option('time_format'); $type = $date_format . $sep . $time_format; } $d = date_i18n( $type, $timestamp); } } if( $echo ){ echo $d; } else{ return $d; } } function wpforo_write_file( $new_file, $content ){ $return = array( 'error' => false, 'file' => '' ); $ifp = @fopen( $new_file, 'wb' ); if ( ! $ifp ) { $return = array( 'error' => sprintf( __( 'Could not write file %s' ), $new_file ) ); } else{ @fwrite( $ifp, $content ); fclose( $ifp ); clearstatcache(); // Set correct file permissions $stat = @stat( dirname( $new_file ) ); $perms = $stat['mode'] & 0007777; $perms = $perms & 0000666; @chmod( $new_file, $perms ); clearstatcache(); $return = array( 'file' => $new_file ); } return $return; } function wpforo_get_file_content( $file ){ $fp = @fopen( $file, 'r' ); if( !$fp ){ return false; } else{ $size = @filesize($file); if( isset($size) && $size > 0 ){ $file_data = fread( $fp, $size ); fclose( $fp ); return $file_data; } return false; } } ################################################################################ /** * Clears file basename and removes trailing slash * * @since 1.0.0 * * @param string filename * * @return string */ function wpforo_clear_basename( $file ) { $file = str_replace('\\','/',$file); $file = preg_replace('|/+|','/', $file); $file = trim($file, '/'); return $file; } ################################################################################# /** * Removes directory with all files and folders * * @since 1.0.0 * * @param string directory name * */ function wpforo_remove_directory( $directory ) { $directory_ns = trim( $directory, '/') . '/'; $directory_ws = '/' . trim( $directory, '/') . '/'; $glob = glob( $directory_ns . '/*' ); if( empty($glob) ) $glob = glob( $directory_ws . '/*' ); foreach( $glob as $item ) { if( is_dir( $item ) ){ wpforo_remove_directory( $item ); } else{ unlink( $item ); } } return rmdir( $directory ); } ################################################################################# /** * Converts bytes to KB, MB, GB * * @since 1.0.0 * * @param integer Bytes * * @return string */ function wpforo_print_size($value, $points = true ){ if($value <= 1024) { return $value . (($points) ? " B" : '' ); } if($value > 1024 && $value <= (1024*1024)) { $value=round(($value/1024)*10)/10; return $value . (($points) ? " KB" : '' ); } if($value > 1024*1024 && $value <= 1024*1024*1024) { $value=round(($value/(1024*1024))*10)/10; return $value . (($points) ? " MB" : '' ); } if($value > 1024*1024*1024 && $value <= 1024*1024*1024*1024) { $value=round(($value/(1024*1024*1024))*10)/10; return $value . (($points) ? " GB" : '' ); } } function wpforo_human_size_to_bytes($sSize) { if ( is_numeric( $sSize) ) { return $sSize; } $sSuffix = substr($sSize, -1); $iValue = substr($sSize, 0, -1); switch(strtoupper($sSuffix)){ case 'P': $iValue *= 1024; case 'T': $iValue *= 1024; case 'G': $iValue *= 1024; case 'M': $iValue *= 1024; case 'K': $iValue *= 1024; break; } return $iValue; } function wpforo_print_number($n, $echo = false) { $x = str_replace(",","",$n); $x = intval($x); $n = 0 + $x; if(!is_numeric($n)) return false; if($n>1000000000000) return round(($n/1000000000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}T',false)); else if($n>1000000000) return round(($n/1000000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}B',false)); else if($n>1000000) return round(($n/1000000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}M',false)); else if($n>10000) return round(($n/1000),1).' '.str_replace('{number}', '', wpforo_phrase('{number}K',false)); if($echo){ echo number_format($n); } else{ return number_format($n); } } function wpforo_bigintval($value) { if(!is_array($value)) { $value = trim($value); } if (ctype_digit($value)) { return $value; } $value = preg_replace("/[^0-9](.*)$/", '', $value); if (ctype_digit($value)) { return $value; } return 0; } function wpforo_removebb($string){ if(isset($string) && $string ){ $string = preg_replace('|\[\/*[^\]\[]+\]|is', '', $string); } return $string; } function wpforo_file_upload_error($code){ switch ($code) { case UPLOAD_ERR_INI_SIZE: $message = wpforo_phrase("The uploaded file exceeds the upload_max_filesize directive in php.ini", false); break; case UPLOAD_ERR_FORM_SIZE: $message = wpforo_phrase("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form", false); break; case UPLOAD_ERR_PARTIAL: $message = wpforo_phrase("The uploaded file was only partially uploaded", false); break; case UPLOAD_ERR_NO_FILE: $message = wpforo_phrase("No file was uploaded", false); break; case UPLOAD_ERR_NO_TMP_DIR: $message = wpforo_phrase("Missing a temporary folder", false); break; case UPLOAD_ERR_CANT_WRITE: $message = wpforo_phrase("Failed to write file to disk", false); break; case UPLOAD_ERR_EXTENSION: $message = wpforo_phrase("File upload stopped by extension", false); break; default: $message = wpforo_phrase("Unknown upload error", false); break; } return $message; } //$key allowed values are post, strip, data, user_description entities or the name of a field filter such as pre_user_description. //More info https://core.trac.wordpress.org/browser/tags/4.5.2/src/wp-includes/kses.php#L624 function wpforo_kses( $string = '', $key = 'data' ){ if(!$string || !$key) return $string; if( $key == 'email' ){ $allowed_html = array( 'a' => array( 'href' => array(), 'title' => array()), 'blockquote' => array(), 'h1' => array(), 'h2' => array(), 'h3' => array(), 'h4' => array(), 'h5' => array(), 'h6' => array(), 'hr' => array(), 'br' => array(), 'p' => array(), 'strong' => array()); } else{ $allowed_html = wp_kses_allowed_html( $key ); if( $key == 'user_description' ){ $allowed_html['img'] = array( 'alt' => array(), 'align' => array(), 'border' => array(), 'height' => array(), 'hspace' => array(), 'longdesc' => array(), 'vspace' => array(), 'src' => array(), 'usemap' => array(), 'width' => array()); } else{ global $allowedposttags; $allowed_html = wpforo_parse_args($allowed_html, $allowedposttags); $allowed_html['a']['data-gallery'] = array(); $allowed_html['a']['download'] = array(); $allowed_html['iframe']['width'] = array(); $allowed_html['iframe']['height'] = array(); $allowed_html['iframe']['src'] = array(); $allowed_html['iframe']['frameborder'] = array(); $allowed_html['iframe']['allowfullscreen'] = array(); $allowed_html['embed']['flashVars'] = array(); $allowed_html['embed']['src'] = array(); $allowed_html['embed']['width'] = array(); $allowed_html['embed']['height'] = array(); $allowed_html['embed']['wmode'] = array(); $allowed_html['embed']['allowFullScreen'] = array(); $allowed_html['embed']['allowScriptAccess'] = array(); $allowed_html['embed']['name'] = array(); $allowed_html['embed']['pluginspage'] = array(); $allowed_html['embed']['type'] = array(); $allowed_html['blockquote']['class'] = TRUE; $allowed_html['blockquote']['data-width'] = TRUE; $allowed_html['p']['lang'] = TRUE; $allowed_html['p']['dir'] = TRUE; } } return wp_kses( $string, $allowed_html ); } function wpforo_deep_merge($default, $current = array()){ foreach($default as $k => $v){ if(!empty($v) && is_array($v)){ foreach($v as $kk => $vv){ if(!empty($vv) && is_array($vv)){ foreach($vv as $kkk => $vvv){ if(!empty($vvv) && is_array($vvv)){ foreach($vvv as $kkkk => $vvvv){ if(!empty($vvv) && is_array($vvv)){ //Stop on 5th level } else{ if(isset($current[$k][$kk][$kkk][$kkkk])) $default[$k][$kk][$kkk][$kkkk] = $current[$k][$kk][$kkk][$kkkk]; } } } else{ if(isset($current[$k][$kk][$kkk])) $default[$k][$kk][$kkk] = $current[$k][$kk][$kkk]; } } } else{ if(isset($current[$k][$kk])) $default[$k][$kk] = $current[$k][$kk]; } } } else{ if(isset($current[$k])) $default[$k] = $current[$k]; } } return $default; } function wpforo_is_image($e){ $is_image = false; $e = strtolower($e); if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){ $is_image = true; } return $is_image; } function get_wpf_option( $option, $default = false ){ global $wpdb; $value = get_option($option, $default); if( $value ){ $value = maybe_unserialize( $value ); } else{ $value = $wpdb->get_var("SELECT `option_value` FROM `".$wpdb->prefix."options` WHERE `option_name` = '". esc_sql($option) . "'"); if(is_serialized( $value )) { $check = @unserialize($value); if( !$check ) $value = wpforo_fixSerializedArray($value); if( !$value && $value !== 0 ) return NULL; } } if( $default && is_array($default) && is_array($value) ) $value = array_merge( $default, $value ); return $value; } /** * Extract what remains from an unintentionally truncated serialized string * $data contains your original array (or what remains of it). * @param string The serialized array */ function wpforo_fixSerializedArray($serialized){ $tmp = preg_replace('/^a:\d+:\{/', '', $serialized); return wpforo_fixSerializedArray_R($tmp); } /** * The recursive function that does all of the heavy lifing. Do not call directly. * @param string The broken serialzized array * @return string Returns the repaired string */ function wpforo_fixSerializedArray_R(&$broken){ $data = array(); $index = NULL; $len = strlen($broken); $i = 0; while(strlen($broken)) { $i++; if ($i > $len) { break; } if (substr($broken, 0, 1) == '}') { $broken = substr($broken, 1); return $data; } else{ $bite = substr($broken, 0, 2); switch($bite) { case 's:': $re = '/^s:\d+:"([^\"]*)";/'; if (preg_match($re, $broken, $m)){ if ($index === NULL){ $index = $m[1]; } else{$data[$index] = $m[1]; $index = NULL;} $broken = preg_replace($re, '', $broken); } break; case 'i:': $re = '/^i:(\d+);/'; if (preg_match($re, $broken, $m)){ if ($index === NULL){$index = (int) $m[1]; } else{$data[$index] = (int) $m[1]; $index = NULL; } $broken = preg_replace($re, '', $broken); } break; case 'b:': $re = '/^b:[01];/'; if (preg_match($re, $broken, $m)){ $data[$index] = (bool) $m[1]; $index = NULL; $broken = preg_replace($re, '', $broken); } break; case 'a:': $re = '/^a:\d+:\{/'; if (preg_match($re, $broken, $m)){ $broken = preg_replace('/^a:\d+:\{/', '', $broken); $data[$index] = wpforo_fixSerializedArray_R($broken); $index = NULL; } break; case 'N;': $broken = substr($broken, 2); $data[$index] = NULL; $index = NULL; break; } } } return $data; } function wpforo_insert_to_media_library( $attach_path, $title = '' ){ if( wpforo_feature('attach-media-lib') ){ if(!$attach_path ) return 0; $attach_fname = basename($attach_path); if(!$title) $title = $attach_fname; require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $wp_upload_dir = wp_upload_dir(); $filetype = wp_check_filetype( $attach_fname, NULL ); $attachment = array( 'guid' => $attach_path, 'post_mime_type' => $filetype['type'], 'post_title' => $title, 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment( $attachment, $attach_path ); add_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' ); $attach_data = wp_generate_attachment_metadata( $attach_id, $attach_path ); wp_update_attachment_metadata( $attach_id, $attach_data ); remove_filter( 'intermediate_image_sizes', 'wpforo_attachment_sizes' ); return $attach_id; } } function wpforo_attachment_sizes( $sizes ){ return array('thumbnail'); } function wpforo_debug( $wpforo ){ if( wpforo_feature('debug-mode', $wpforo) ) : ?> statistic('update', $template); do_action( 'wpforo_clean_cache', $id, $template ); $wpforo->cache->clean( $id, $template, $item ); do_action( 'wpforo_clean_cache_end', $id, $template ); } function wpforo_db_check( $args = array() ){ global $wpdb; $check = trim($args['check']); $col = esc_sql(trim($args['col'])); $table = esc_sql(trim($args['table'])); if( $check == 'table_exists' ){ return $wpdb->get_var("SHOW TABLES LIKE '" . $table . "';"); } if( $check == 'col_exists' ){ return $wpdb->get_var("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $col . "';"); } if( $check == 'key_exists' ){ $col = $wpdb->get_row("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $col . "';", ARRAY_A); return $col['Key']; } if( $check == 'default_value' ){ $col = $wpdb->get_row("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $col . "';", ARRAY_A); return $col['Default']; } if( $check == 'col_type' ){ $col = $wpdb->get_row("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $col . "';", ARRAY_A); return $col['Type']; } } function wpforo_is_owner( $userid ){ global $wpforo; if( !empty($wpforo) ){ if( isset($wpforo->current_userid) && $wpforo->current_userid ){ if( $userid == $wpforo->current_userid ) return true; } } return false; } function wpforo_make_dname($display_name, $user_nicename){ $display_name = trim($display_name); $user_nicename = trim($user_nicename); return ( $display_name ? esc_html($display_name) : esc_html(urldecode($user_nicename)) ); } function wpforo_strlen($string ){ if(!$string) return 0; if(function_exists('mb_strlen')){ return mb_strlen($string); } else{ return strlen($string); } } function wpforo_string2array( $string, $regexp = '' ){ if( !$regexp ) $regexp = '#' . preg_quote(PHP_EOL) . '#isu'; $array = preg_split($regexp, $string); return array_filter($array); } function wpforo_array_ordered_intersect_key($array1, $array2){ $new_array = array(); foreach ($array2 as $key => $value){ if( isset( $array1[$key] ) ) $new_array[$key] = $array1[$key]; } return $new_array; } function wpforo_urlpath_to_dirpath($urlpath){ $upload_array = wp_upload_dir(); $wp_content_dir = preg_replace('#/wp-content/.+$#isu', '/wp-content/', $upload_array['basedir']); $dirpath = preg_replace('#^.+?/wp-content/#isu', $wp_content_dir, $urlpath); return $dirpath; } function wpforo_xcopy($source, $dest) { // Check for symlinks if (is_link($source)) { return symlink(readlink($source), $dest); } // Simple copy for a file if (is_file($source)) { return copy($source, $dest); } // Make destination directory if (!is_dir($dest)) { wp_mkdir_p($dest); } // Loop through the folder $dir = dir($source); while (false !== $entry = $dir->read()) { // Skip pointers if ($entry == '.' || $entry == '..') { continue; } // Deep copy directories wpforo_xcopy( rtrim($source, '/') . "/$entry", rtrim($dest, '/') . "/$entry" ); } // Clean up $dir->close(); return true; } function wpforo_printf_array($format, $arr){ return call_user_func_array('printf', array_merge((array)$format, (array)$arr)); } function wpforo_sprintf_array($format, $arr){ return call_user_func_array('sprintf', array_merge((array)$format, (array)$arr)); } function wpforo_avatar_url($avatar_html){ if( preg_match('#src=[\'"]([^\'"]+?)[\'"]#isu', $avatar_html, $matches) ){ //return preg_replace('#(^[^\r\n\t\s\0]*?gravatar\.com/[^\r\n\t\s\0]*?)\?s=\d+[^\r\n\t\s\0]*#isu', '$1?s=300', $matches[1], 1); return $matches[1]; } return ''; } function wpforo_get_image_url( $content, $first = true ){ $images = array(); $content = apply_filters('wpforo_content_filter', $content); preg_match_all('#https?://[^\s\'\"<>]+\.(?:jpg|jpeg|png|gif|ico|svg|bmp|tiff)#isu', $content, $m_img, PREG_SET_ORDER); if( empty($m_img)) preg_match_all('#//[^\s\'\"<>]+\.(?:jpg|jpeg|png|gif|ico|svg|bmp|tiff)#isu', $content, $m_img, PREG_SET_ORDER); if(!empty($m_img)){ foreach( $m_img as $match ){ $ext = pathinfo($match[0], PATHINFO_EXTENSION); if( $ext && wpforo_is_image($ext)){ $images[] = $match[0]; } } } else{ preg_match_all('#https?://[^\s\'\"<>]+#isu', $content, $m_url, PREG_SET_ORDER); if( empty($m_url)) preg_match_all('#//[^\s\'\"<>]+#isu', $content, $m_url, PREG_SET_ORDER); if(!empty($m_url)){ foreach( $m_url as $match ){ $ext = pathinfo($match[0], PATHINFO_EXTENSION); if( $ext && wpforo_is_image($ext)){ $images[] = $match[0]; } } } } if(!empty($images)){ if( $first ){ return $images[0]; } else{ return $images; } } return false; } function wpforo_return_zero($var = null){ return 0; } function wpforo_is_json($string) { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); }