PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/bb-includes/functions.bb-core.php

https://bitbucket.org/iRonBot/bbpress-persian
PHP | 1839 lines | 1378 code | 251 blank | 210 comment | 305 complexity | bdcfa00d817ce896cce7e575ba9a264d MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Core bbPress functions.
  4. *
  5. * @package bbPress
  6. */
  7. /**
  8. * Initialization functions mostly called in bb-settings.php
  9. */
  10. /**
  11. * Marks things as deprecated and informs when they have been used.
  12. *
  13. * @since 0.9
  14. *
  15. * @param string $type The type of thing that was attempted: function, class::function, constant, variable or page.
  16. * @param string $name The thing that was called.
  17. * @param string $replacement Optional. The thing that should have been called.
  18. * @uses $bb_log BP_Log logging object.
  19. */
  20. function bb_log_deprecated( $type, $name, $replacement = 'none' ) {
  21. global $bb_log;
  22. $bb_log->notice( sprintf( __( 'Using deprecated bbPress %1$s - %2$s - replace with - %3$s' ), $type, $name, $replacement ) );
  23. if ( $bb_log->level & BP_LOG_DEBUG && $bb_log->level & BP_LOG_NOTICE ) { // Only compute the location if we're going to log it.
  24. $backtrace = debug_backtrace();
  25. $file = $backtrace[2]['file'];
  26. if ( substr( $file, 0, strlen( BB_PATH ) - 1 ) == rtrim( BB_PATH, '\\/') )
  27. $file = substr( $file, strlen( BB_PATH ) );
  28. $file = str_replace( '\\', '/', $file );
  29. // 0 = this function, 1 = the deprecated function
  30. $bb_log->notice( ' ' . sprintf( __( 'on line %1$d of file %2$s' ), $backtrace[2]['line'], $file ) );
  31. }
  32. }
  33. /**
  34. * Sanitizes user input en-masse.
  35. *
  36. * @param mixed $array The array of values or a single value to sanitize, usually a global variable like $_GET or $_POST.
  37. * @param boolean $trim Optional. Whether to trim the value or not. Default is true.
  38. * @return mixed The sanitized data.
  39. */
  40. function bb_global_sanitize( $array, $trim = true )
  41. {
  42. foreach ( $array as $k => $v ) {
  43. if ( is_array( $v ) ) {
  44. $array[$k] = bb_global_sanitize( $v );
  45. } else {
  46. if ( !get_magic_quotes_gpc() ) {
  47. $array[$k] = addslashes( $v );
  48. }
  49. if ( $trim ) {
  50. $array[$k] = trim( $array[$k] );
  51. }
  52. }
  53. }
  54. return $array;
  55. }
  56. /**
  57. * Reports whether bbPress is installed by getting forums.
  58. *
  59. * @return boolean True if there are forums, otherwise false.
  60. */
  61. function bb_is_installed()
  62. {
  63. // Maybe grab all the forums and cache them
  64. global $bbdb;
  65. $bbdb->suppress_errors();
  66. $forums = (array) @bb_get_forums();
  67. $bbdb->suppress_errors(false);
  68. if ( !$forums ) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * Sets the required variables to connect to custom user tables.
  75. *
  76. * @return boolean Always returns true.
  77. */
  78. function bb_set_custom_user_tables()
  79. {
  80. global $bb;
  81. // Check for older style custom user table
  82. if ( !isset( $bb->custom_tables['users'] ) ) { // Don't stomp new setting style
  83. if ( $bb->custom_user_table = bb_get_option( 'custom_user_table' ) ) {
  84. if ( !isset( $bb->custom_tables ) ) {
  85. $bb->custom_tables = array();
  86. }
  87. $bb->custom_tables['users'] = $bb->custom_user_table;
  88. }
  89. }
  90. // Check for older style custom user meta table
  91. if ( !isset( $bb->custom_tables['usermeta'] ) ) { // Don't stomp new setting style
  92. if ( $bb->custom_user_meta_table = bb_get_option( 'custom_user_meta_table' ) ) {
  93. if ( !isset( $bb->custom_tables ) ) {
  94. $bb->custom_tables = array();
  95. }
  96. $bb->custom_tables['usermeta'] = $bb->custom_user_meta_table;
  97. }
  98. }
  99. // Check for older style wp_table_prefix
  100. if ( $bb->wp_table_prefix = bb_get_option( 'wp_table_prefix' ) ) { // User has set old constant
  101. if ( !isset( $bb->custom_tables ) ) {
  102. $bb->custom_tables = array(
  103. 'users' => $bb->wp_table_prefix . 'users',
  104. 'usermeta' => $bb->wp_table_prefix . 'usermeta'
  105. );
  106. } else {
  107. if ( !isset( $bb->custom_tables['users'] ) ) { // Don't stomp new setting style
  108. $bb->custom_tables['users'] = $bb->wp_table_prefix . 'users';
  109. }
  110. if ( !isset( $bb->custom_tables['usermeta'] ) ) {
  111. $bb->custom_tables['usermeta'] = $bb->wp_table_prefix . 'usermeta';
  112. }
  113. }
  114. }
  115. if ( bb_get_option( 'wordpress_mu_primary_blog_id' ) ) {
  116. $bb->wordpress_mu_primary_blog_id = bb_get_option( 'wordpress_mu_primary_blog_id' );
  117. }
  118. // Check for older style user database
  119. if ( !isset( $bb->custom_databases ) ) {
  120. $bb->custom_databases = array();
  121. }
  122. if ( !isset( $bb->custom_databases['user'] ) ) {
  123. if ( $bb->user_bbdb_name = bb_get_option( 'user_bbdb_name' ) ) {
  124. $bb->custom_databases['user']['name'] = $bb->user_bbdb_name;
  125. }
  126. if ( $bb->user_bbdb_user = bb_get_option( 'user_bbdb_user' ) ) {
  127. $bb->custom_databases['user']['user'] = $bb->user_bbdb_user;
  128. }
  129. if ( $bb->user_bbdb_password = bb_get_option( 'user_bbdb_password' ) ) {
  130. $bb->custom_databases['user']['password'] = $bb->user_bbdb_password;
  131. }
  132. if ( $bb->user_bbdb_host = bb_get_option( 'user_bbdb_host' ) ) {
  133. $bb->custom_databases['user']['host'] = $bb->user_bbdb_host;
  134. }
  135. if ( $bb->user_bbdb_charset = bb_get_option( 'user_bbdb_charset' ) ) {
  136. $bb->custom_databases['user']['charset'] = $bb->user_bbdb_charset;
  137. }
  138. if ( $bb->user_bbdb_collate = bb_get_option( 'user_bbdb_collate' ) ) {
  139. $bb->custom_databases['user']['collate'] = $bb->user_bbdb_collate;
  140. }
  141. if ( isset( $bb->custom_databases['user'] ) ) {
  142. if ( isset( $bb->custom_tables['users'] ) ) {
  143. $bb->custom_tables['users'] = array( 'user', $bb->custom_tables['users'] );
  144. }
  145. if ( isset( $bb->custom_tables['usermeta'] ) ) {
  146. $bb->custom_tables['usermeta'] = array( 'user', $bb->custom_tables['usermeta'] );
  147. }
  148. }
  149. }
  150. return true;
  151. }
  152. /* Pagination */
  153. /**
  154. * Retrieve paginated links for pages.
  155. *
  156. * Technically, the function can be used to create paginated link list for any
  157. * area. The 'base' argument is used to reference the url, which will be used to
  158. * create the paginated links. The 'format' argument is then used for replacing
  159. * the page number. It is however, most likely and by default, to be used on the
  160. * archive post pages.
  161. *
  162. * The 'type' argument controls format of the returned value. The default is
  163. * 'plain', which is just a string with the links separated by a newline
  164. * character. The other possible values are either 'array' or 'list'. The
  165. * 'array' value will return an array of the paginated link list to offer full
  166. * control of display. The 'list' value will place all of the paginated links in
  167. * an unordered HTML list.
  168. *
  169. * The 'total' argument is the total amount of pages and is an integer. The
  170. * 'current' argument is the current page number and is also an integer.
  171. *
  172. * An example of the 'base' argument is "http://example.com/all_posts.php%_%"
  173. * and the '%_%' is required. The '%_%' will be replaced by the contents of in
  174. * the 'format' argument. An example for the 'format' argument is "?page=%#%"
  175. * and the '%#%' is also required. The '%#%' will be replaced with the page
  176. * number.
  177. *
  178. * You can include the previous and next links in the list by setting the
  179. * 'prev_next' argument to true, which it is by default. You can set the
  180. * previous text, by using the 'prev_text' argument. You can set the next text
  181. * by setting the 'next_text' argument.
  182. *
  183. * If the 'show_all' argument is set to true, then it will show all of the pages
  184. * instead of a short list of the pages near the current page. By default, the
  185. * 'show_all' is set to false and controlled by the 'end_size' and 'mid_size'
  186. * arguments. The 'end_size' argument is how many numbers on either the start
  187. * and the end list edges, by default is 1. The 'mid_size' argument is how many
  188. * numbers to either side of current page, but not including current page.
  189. *
  190. * It is possible to add query vars to the link by using the 'add_args' argument
  191. * and see {@link add_query_arg()} for more information.
  192. *
  193. * @since 1.0
  194. *
  195. * @param string|array $args Optional. Override defaults.
  196. * @return array|string String of page links or array of page links.
  197. */
  198. function bb_paginate_links( $args = '' ) {
  199. $defaults = array(
  200. 'base' => '%_%', // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
  201. 'format' => '?page=%#%', // ?page=%#% : %#% is replaced by the page number
  202. 'total' => 1,
  203. 'current' => 0,
  204. 'show_all' => false,
  205. 'prev_next' => true,
  206. 'prev_text' => __( '&laquo; Previous' ),
  207. 'next_text' => __( 'Next &raquo;' ),
  208. 'end_size' => 1, // How many numbers on either end including the end
  209. 'mid_size' => 2, // How many numbers to either side of current not including current
  210. 'type' => 'plain',
  211. 'add_args' => false, // array of query args to add
  212. 'add_fragment' => '',
  213. 'n_title' => __( 'Page %d' ), // Not from WP version
  214. 'prev_title' => __( 'Previous page' ), // Not from WP version
  215. 'next_title' => __( 'Next page' ) // Not from WP version
  216. );
  217. $args = wp_parse_args( $args, $defaults );
  218. extract( $args, EXTR_SKIP );
  219. // Who knows what else people pass in $args
  220. $total = (int) $total;
  221. if ( $total < 2 )
  222. return;
  223. $current = (int) $current;
  224. $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default.
  225. $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
  226. $add_args = is_array($add_args) ? $add_args : false;
  227. $r = '';
  228. $page_links = array();
  229. $n = 0;
  230. $dots = false;
  231. $empty_format = '';
  232. if ( strpos( $format, '?' ) === 0 ) {
  233. $empty_format = '?';
  234. }
  235. if ( $prev_next && $current && 1 < $current ) {
  236. $link = str_replace( '%_%', 2 == $current ? $empty_format : $format, $base );
  237. $link = str_replace( '%#%', $current - 1, $link );
  238. $link = str_replace( '?&', '?', $link );
  239. if ( $add_args )
  240. $link = add_query_arg( $add_args, $link );
  241. $link .= $add_fragment;
  242. $page_links[] = '<a class="prev page-numbers" href="' . esc_url( $link ) . '" title="' . esc_attr( $prev_title ) . '">' . $prev_text . '</a>';
  243. }
  244. for ( $n = 1; $n <= $total; $n++ ) {
  245. if ( $n == $current ) {
  246. $n_display = bb_number_format_i18n( $n );
  247. $n_display_title = esc_attr( sprintf( $n_title, $n ) );
  248. $page_links[] = '<span class="page-numbers current" title="' . $n_display_title . '">' . $n_display . '</span>';
  249. $dots = true;
  250. } else {
  251. if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) {
  252. $n_display = bb_number_format_i18n( $n );
  253. $n_display_title = esc_attr( sprintf( $n_title, $n ) );
  254. $link = str_replace( '%_%', 1 == $n ? $empty_format : $format, $base );
  255. $link = str_replace( '%#%', $n, $link );
  256. $link = str_replace( '?&', '?', $link );
  257. if ( $add_args )
  258. $link = add_query_arg( $add_args, $link );
  259. $link .= $add_fragment;
  260. $page_links[] = '<a class="page-numbers" href="' . esc_url( $link ) . '" title="' . $n_display_title . '">' . $n_display . '</a>';
  261. $dots = true;
  262. } elseif ( $dots && !$show_all ) {
  263. $page_links[] = '<span class="page-numbers dots">&hellip;</span>';
  264. $dots = false;
  265. }
  266. }
  267. }
  268. if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) {
  269. $link = str_replace( '%_%', $format, $base );
  270. $link = str_replace( '%#%', $current + 1, $link );
  271. if ( $add_args )
  272. $link = add_query_arg( $add_args, $link );
  273. $link .= $add_fragment;
  274. $page_links[] = '<a class="next page-numbers" href="' . esc_url( $link ) . '" title="' . esc_attr( $next_title ) . '">' . $next_text . '</a>';
  275. }
  276. switch ( $type ) {
  277. case 'array':
  278. return $page_links;
  279. break;
  280. case 'list':
  281. $r .= '<ul class="page-numbers">' . "\n\t" . '<li>';
  282. $r .= join( '</li>' . "\n\t" . '<li>', $page_links );
  283. $r .= '</li>' . "\n" . '</ul>' . "\n";
  284. break;
  285. default:
  286. $r = join( "\n", $page_links );
  287. break;
  288. }
  289. return $r;
  290. }
  291. function bb_get_uri_page() {
  292. if ( isset($_GET['page']) && is_numeric($_GET['page']) && 1 < (int) $_GET['page'] )
  293. return (int) $_GET['page'];
  294. if ( isset($_SERVER['PATH_INFO']) )
  295. $path = $_SERVER['PATH_INFO'];
  296. else
  297. if ( !$path = strtok($_SERVER['REQUEST_URI'], '?') )
  298. return 1;
  299. if ( preg_match( '/^\/([0-9]+)\/?$/', $path, $matches ) ) {
  300. $page = (int) $matches[1];
  301. if ( 1 < $page ) {
  302. return $page;
  303. }
  304. }
  305. if ( $page = strstr($path, '/page/') ) {
  306. $page = (int) substr($page, 6);
  307. if ( 1 < $page )
  308. return $page;
  309. }
  310. return 1;
  311. }
  312. //expects $item = 1 to be the first, not 0
  313. function bb_get_page_number( $item, $per_page = 0 ) {
  314. if ( !$per_page )
  315. $per_page = bb_get_option('page_topics');
  316. return intval( ceil( $item / $per_page ) ); // page 1 is the first page
  317. }
  318. /* Time */
  319. function bb_timer_stop($display = 0, $precision = 3) { //if called like bb_timer_stop(1), will echo $timetotal
  320. global $bb_timestart, $timeend;
  321. $mtime = explode(' ', microtime());
  322. $timeend = $mtime[1] + $mtime[0];
  323. $timetotal = $timeend - $bb_timestart;
  324. echo number_format($timetotal, $precision, '.', ',');
  325. /*
  326. if ($display)
  327. echo bb_number_format_i18n($timetotal, $precision);
  328. return bb_number_format_i18n($timetotal, $precision);
  329. */
  330. }
  331. // GMT -> so many minutes ago
  332. function bb_since( $original, $args = '' )
  333. {
  334. $defaults = array(
  335. 'levels' => 1,
  336. 'separator' => ', '
  337. );
  338. // $args used to be $do_more
  339. // $do_more = 0 is equivalent to $args['levels'] = 1
  340. // $do_more = 1 is equivalent to $args['levels'] = 2
  341. if ( !is_array( $args ) ) {
  342. $args = array(
  343. 'levels' => abs( (integer) $args ) + 1
  344. );
  345. }
  346. $args = wp_parse_args( $args, $defaults );
  347. extract( $args, EXTR_SKIP );
  348. $today = (integer) time();
  349. if ( !is_numeric( $original ) ) {
  350. if ( $today < $_original = bb_gmtstrtotime( str_replace( ',', ' ', $original ) ) ) { // Looks like bb_since was called twice
  351. return $original;
  352. } else {
  353. $original = $_original;
  354. }
  355. }
  356. $seconds = $today - ( (integer) $original );
  357. if ( 0 === $seconds ) {
  358. return sprintf( _n( '%d second', '%d seconds', 0 ), 0 );
  359. }
  360. $levels = abs( (integer) $levels );
  361. if ( 0 === $levels ) {
  362. return '';
  363. }
  364. // array of time period chunks
  365. $chunks = array(
  366. ( 60 * 60 * 24 * 365 ), // years
  367. ( 60 * 60 * 24 * 30 ), // months
  368. ( 60 * 60 * 24 * 7 ), // weeks
  369. ( 60 * 60 * 24 ), // days
  370. ( 60 * 60 ), // hours
  371. ( 60 ), // minutes
  372. ( 1 ) // seconds
  373. );
  374. $caught = 0;
  375. $parts = array();
  376. for ( $i = 0; $i < count( $chunks ); $i++ ) {
  377. if ( ( $count = floor( $seconds / $chunks[$i] ) ) || $caught ) {
  378. if ( $count ) {
  379. $trans = array(
  380. _n( '%d year', '%d years', $count ),
  381. _n( '%d month', '%d months', $count ),
  382. _n( '%d week', '%d weeks', $count ),
  383. _n( '%d day', '%d days', $count ),
  384. _n( '%d hour', '%d hours', $count ),
  385. _n( '%d minute', '%d minutes', $count ),
  386. _n( '%d second', '%d seconds', $count )
  387. );
  388. $parts[] = sprintf( $trans[$i], $count );
  389. }
  390. $caught++;
  391. $seconds = $seconds - ( $count * $chunks[$i] );
  392. }
  393. if ( $caught === $levels ) {
  394. break;
  395. }
  396. }
  397. if ( empty( $parts ) ) {
  398. return sprintf( _n( '%d second', '%d seconds', 0 ), 0 );
  399. }
  400. return join( $separator, $parts );
  401. }
  402. function bb_current_time( $type = 'timestamp' ) {
  403. return current_time( $type, true );
  404. }
  405. // GMT -> Local
  406. // in future versions this could eaily become a user option.
  407. function bb_offset_time( $time, $args = null ) {
  408. if ( isset($args['format']) && 'since' == $args['format'] )
  409. return $time;
  410. if ( !is_numeric($time) ) {
  411. if ( -1 !== $_time = bb_gmtstrtotime( $time ) )
  412. return gmdate('Y-m-d H:i:s', $_time + bb_get_option( 'gmt_offset' ) * 3600);
  413. else
  414. return $time; // Perhaps should return -1 here
  415. } else {
  416. return $time + bb_get_option( 'gmt_offset' ) * 3600;
  417. }
  418. }
  419. /* Permalinking / URLs / Paths */
  420. /**
  421. * BB_URI_CONTEXT_* - Bitwise definitions for bb_uri() and bb_get_uri() contexts
  422. *
  423. * @since 1.0
  424. */
  425. define( 'BB_URI_CONTEXT_NONE', 0 );
  426. define( 'BB_URI_CONTEXT_HEADER', 1 );
  427. define( 'BB_URI_CONTEXT_TEXT', 2 );
  428. define( 'BB_URI_CONTEXT_A_HREF', 4 );
  429. define( 'BB_URI_CONTEXT_FORM_ACTION', 8 );
  430. define( 'BB_URI_CONTEXT_IMG_SRC', 16 );
  431. define( 'BB_URI_CONTEXT_LINK_STYLESHEET_HREF', 32 );
  432. define( 'BB_URI_CONTEXT_LINK_ALTERNATE_HREF', 64 );
  433. define( 'BB_URI_CONTEXT_LINK_OTHER', 128 );
  434. define( 'BB_URI_CONTEXT_SCRIPT_SRC', 256 );
  435. define( 'BB_URI_CONTEXT_IFRAME_SRC', 512 );
  436. define( 'BB_URI_CONTEXT_BB_FEED', 1024 );
  437. define( 'BB_URI_CONTEXT_BB_USER_FORMS', 2048 );
  438. define( 'BB_URI_CONTEXT_BB_ADMIN', 4096 );
  439. define( 'BB_URI_CONTEXT_BB_XMLRPC', 8192 );
  440. define( 'BB_URI_CONTEXT_WP_HTTP_REQUEST', 16384 );
  441. //define( 'BB_URI_CONTEXT_*', 32768 ); // Reserved for future definitions
  442. //define( 'BB_URI_CONTEXT_*', 65536 ); // Reserved for future definitions
  443. //define( 'BB_URI_CONTEXT_*', 131072 ); // Reserved for future definitions
  444. //define( 'BB_URI_CONTEXT_*', 262144 ); // Reserved for future definitions
  445. define( 'BB_URI_CONTEXT_AKISMET', 524288 );
  446. /**
  447. * Echo a URI based on the URI setting
  448. *
  449. * @since 1.0
  450. *
  451. * @param $resource string The directory, may include a querystring
  452. * @param $query mixed The query arguments as a querystring or an associative array
  453. * @param $context integer The context of the URI, use BB_URI_CONTEXT_*
  454. * @return void
  455. */
  456. function bb_uri( $resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF )
  457. {
  458. echo apply_filters( 'bb_uri', bb_get_uri( $resource, $query, $context ), $resource, $query, $context );
  459. }
  460. /**
  461. * Return a URI based on the URI setting
  462. *
  463. * @since 1.0
  464. *
  465. * @param $resource string The directory, may include a querystring
  466. * @param $query mixed The query arguments as a querystring or an associative array
  467. * @param $context integer The context of the URI, use BB_URI_CONTEXT_*
  468. * @return string The complete URI
  469. */
  470. function bb_get_uri( $resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF )
  471. {
  472. // If there is a querystring in the resource then extract it
  473. if ( $resource && strpos( $resource, '?' ) !== false ) {
  474. list( $_resource, $_query ) = explode( '?', trim( $resource ), 2 );
  475. $resource = $_resource;
  476. $_query = wp_parse_args( $_query );
  477. } else {
  478. // Make sure $_query is an array for array_merge()
  479. $_query = array();
  480. }
  481. // $query can be an array as well as a string
  482. if ( $query ) {
  483. if ( is_string( $query ) ) {
  484. $query = ltrim( trim( $query ), '?' );
  485. }
  486. $query = wp_parse_args( $query );
  487. }
  488. // Make sure $query is an array for array_merge()
  489. if ( !$query ) {
  490. $query = array();
  491. }
  492. // Merge the queries into a single array
  493. $query = array_merge( $_query, $query );
  494. // Make sure context is an integer
  495. if ( !$context || !is_integer( $context ) ) {
  496. $context = BB_URI_CONTEXT_A_HREF;
  497. }
  498. // Get the base URI
  499. static $_uri;
  500. if( !isset( $_uri ) ) {
  501. $_uri = bb_get_option( 'uri' );
  502. }
  503. $uri = $_uri;
  504. // Use https?
  505. if (
  506. ( ( $context & BB_URI_CONTEXT_BB_USER_FORMS ) && force_ssl_login() ) // Force https when required on user forms
  507. ||
  508. ( ( $context & BB_URI_CONTEXT_BB_ADMIN ) && force_ssl_admin() ) // Force https when required in admin
  509. ) {
  510. static $_uri_ssl;
  511. if( !isset( $_uri_ssl ) ) {
  512. $_uri_ssl = bb_get_option( 'uri_ssl' );
  513. }
  514. $uri = $_uri_ssl;
  515. }
  516. // Add the directory
  517. $uri .= ltrim( $resource, '/' );
  518. // Add the query string to the URI
  519. $uri = add_query_arg( $query, $uri );
  520. return apply_filters( 'bb_get_uri', $uri, $resource, $context );
  521. }
  522. /**
  523. * Forces redirection to an SSL page when required
  524. *
  525. * @since 1.0
  526. *
  527. * @return void
  528. */
  529. function bb_ssl_redirect()
  530. {
  531. $page = bb_get_location();
  532. do_action( 'bb_ssl_redirect' );
  533. if ( BB_IS_ADMIN ) {
  534. if ( !force_ssl_admin() ) {
  535. return;
  536. }
  537. } else {
  538. switch ( $page ) {
  539. case 'login-page':
  540. case 'register-page':
  541. if ( !force_ssl_login() ) {
  542. return;
  543. }
  544. break;
  545. case 'profile-page':
  546. global $self;
  547. if ( $self == 'profile-edit.php' ) {
  548. if ( !force_ssl_login() ) {
  549. return;
  550. }
  551. } else {
  552. return;
  553. }
  554. break;
  555. default:
  556. return;
  557. break;
  558. }
  559. }
  560. if ( is_ssl() ) {
  561. return;
  562. }
  563. $uri_ssl = parse_url( bb_get_option( 'uri_ssl' ) );
  564. $uri = $uri_ssl['scheme'] . '://' . $uri_ssl['host'] . $_SERVER['REQUEST_URI'];
  565. bb_safe_redirect( $uri );
  566. exit;
  567. }
  568. function bb_get_path( $level = 1, $base = false, $request = false ) {
  569. if ( !$request )
  570. $request = $_SERVER['REQUEST_URI'];
  571. if ( is_string($request) )
  572. $request = parse_url($request);
  573. if ( !is_array($request) || !isset($request['path']) )
  574. return '';
  575. $path = rtrim($request['path'], " \t\n\r\0\x0B/");
  576. if ( !$base )
  577. $base = rtrim(bb_get_option('path'), " \t\n\r\0\x0B/");
  578. $path = preg_replace('|' . preg_quote($base, '|') . '/?|','',$path,1);
  579. if ( !$path )
  580. return '';
  581. if ( strpos($path, '/') === false )
  582. return '';
  583. $url = explode('/',$path);
  584. if ( !isset($url[$level]) )
  585. return '';
  586. return urldecode($url[$level]);
  587. }
  588. function bb_find_filename( $text ) {
  589. if ( preg_match('|.*?/([a-z\-]+\.php)/?.*|', $text, $matches) )
  590. return $matches[1];
  591. else {
  592. $path = bb_get_option( 'path' );
  593. $text = preg_replace("#^$path#", '', $text);
  594. $text = preg_replace('#/.+$#', '', $text);
  595. return $text . '.php';
  596. }
  597. return false;
  598. }
  599. function bb_send_headers() {
  600. if ( bb_is_user_logged_in() )
  601. nocache_headers();
  602. @header('Content-Type: ' . bb_get_option( 'html_type' ) . '; charset=' . bb_get_option( 'charset' ));
  603. do_action( 'bb_send_headers' );
  604. }
  605. function bb_pingback_header() {
  606. if (bb_get_option('enable_pingback'))
  607. @header('X-Pingback: '. bb_get_uri('xmlrpc.php', null, BB_URI_CONTEXT_HEADER + BB_URI_CONTEXT_BB_XMLRPC));
  608. }
  609. // Inspired by and adapted from Yung-Lung Scott YANG's http://scott.yang.id.au/2005/05/permalink-redirect/ (GPL)
  610. function bb_repermalink() {
  611. global $page;
  612. $location = bb_get_location();
  613. $uri = $_SERVER['REQUEST_URI'];
  614. if ( isset($_GET['id']) )
  615. $id = $_GET['id'];
  616. else
  617. $id = bb_get_path();
  618. $_original_id = $id;
  619. do_action( 'pre_permalink', $id );
  620. $id = apply_filters( 'bb_repermalink', $id );
  621. switch ($location) {
  622. case 'front-page':
  623. $path = null;
  624. $querystring = null;
  625. if ($page > 1) {
  626. if (bb_get_option( 'mod_rewrite' )) {
  627. $path = 'page/' . $page;
  628. } else {
  629. $querystring = array('page' => $page);
  630. }
  631. }
  632. $permalink = bb_get_uri($path, $querystring, BB_URI_CONTEXT_HEADER);
  633. $issue_404 = true;
  634. break;
  635. case 'forum-page':
  636. if (empty($id)) {
  637. $permalink = bb_get_uri(null, null, BB_URI_CONTEXT_HEADER);
  638. break;
  639. }
  640. global $forum_id, $forum;
  641. $forum = bb_get_forum( $id );
  642. $forum_id = $forum->forum_id;
  643. $permalink = get_forum_link( $forum->forum_id, $page );
  644. break;
  645. case 'topic-edit-page':
  646. case 'topic-page':
  647. if (empty($id)) {
  648. $permalink = bb_get_uri(null, null, BB_URI_CONTEXT_HEADER);
  649. break;
  650. }
  651. global $topic_id, $topic;
  652. $topic = get_topic( $id );
  653. $topic_id = $topic->topic_id;
  654. $permalink = get_topic_link( $topic->topic_id, $page );
  655. break;
  656. case 'profile-page': // This handles the admin side of the profile as well.
  657. global $user_id, $user, $profile_hooks, $self;
  658. if ( isset($_GET['id']) )
  659. $id = $_GET['id'];
  660. elseif ( isset($_GET['username']) )
  661. $id = $_GET['username'];
  662. else
  663. $id = bb_get_path();
  664. $_original_id = $id;
  665. if ( !$id ) {
  666. $user = bb_get_current_user(); // Attempt to go to the current users profile
  667. } else {
  668. if ( bb_get_option( 'mod_rewrite' ) === 'slugs') {
  669. if ( !$user = bb_get_user_by_nicename( $id ) ) {
  670. $user = bb_get_user( $id );
  671. }
  672. } else {
  673. if ( !$user = bb_get_user( $id ) ) {
  674. $user = bb_get_user_by_nicename( $id );
  675. }
  676. }
  677. }
  678. if ( !$user || ( 1 == $user->user_status && !bb_current_user_can( 'moderate' ) ) )
  679. bb_die(__('User not found.'), '', 404);
  680. $user_id = $user->ID;
  681. bb_global_profile_menu_structure();
  682. $valid = false;
  683. if ( $tab = isset($_GET['tab']) ? $_GET['tab'] : bb_get_path(2) ) {
  684. foreach ( $profile_hooks as $valid_tab => $valid_file ) {
  685. if ( $tab == $valid_tab ) {
  686. $valid = true;
  687. $self = $valid_file;
  688. }
  689. }
  690. }
  691. if ( $valid ) {
  692. $permalink = get_profile_tab_link( $user->ID, $tab, $page );
  693. } else {
  694. $permalink = get_user_profile_link( $user->ID, $page );
  695. unset($self, $tab);
  696. }
  697. break;
  698. case 'favorites-page':
  699. $permalink = get_favorites_link();
  700. break;
  701. case 'tag-page': // It's not an integer and tags.php pulls double duty.
  702. $id = ( isset($_GET['tag']) ) ? $_GET['tag'] : false;
  703. if ( ! $id || ! bb_get_tag( (string) $id ) )
  704. $permalink = bb_get_tag_page_link();
  705. else {
  706. global $tag, $tag_name;
  707. $tag_name = $id;
  708. $tag = bb_get_tag( (string) $id );
  709. $permalink = bb_get_tag_link( 0, $page ); // 0 => grabs $tag from global.
  710. }
  711. break;
  712. case 'view-page': // Not an integer
  713. if ( isset($_GET['view']) )
  714. $id = $_GET['view'];
  715. else
  716. $id = bb_get_path();
  717. $_original_id = $id;
  718. global $view;
  719. $view = $id;
  720. $permalink = get_view_link( $view, $page );
  721. break;
  722. default:
  723. return;
  724. break;
  725. }
  726. wp_parse_str($_SERVER['QUERY_STRING'], $args);
  727. $args = urlencode_deep($args);
  728. if ( $args ) {
  729. $permalink = add_query_arg($args, $permalink);
  730. if ( bb_get_option('mod_rewrite') ) {
  731. $pretty_args = array('id', 'page', 'tag', 'tab', 'username'); // these are already specified in the path
  732. if ( $location == 'view-page' )
  733. $pretty_args[] = 'view';
  734. foreach ( $pretty_args as $pretty_arg )
  735. $permalink = remove_query_arg( $pretty_arg, $permalink );
  736. }
  737. }
  738. $permalink = apply_filters( 'bb_repermalink_result', $permalink, $location );
  739. $domain = bb_get_option('domain');
  740. $domain = preg_replace('/^https?/', '', $domain);
  741. $check = preg_replace( '|^.*' . trim($domain, ' /' ) . '|', '', $permalink, 1 );
  742. $uri = rtrim( $uri, " \t\n\r\0\x0B?" );
  743. $uri = str_replace( '/index.php', '/', $uri );
  744. global $bb_log;
  745. $bb_log->debug($uri, 'bb_repermalink() ' . __('REQUEST_URI'));
  746. $bb_log->debug($check, 'bb_repermalink() ' . __('should be'));
  747. $bb_log->debug($permalink, 'bb_repermalink() ' . __('full permalink'));
  748. $bb_log->debug(isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null, 'bb_repermalink() ' . __('PATH_INFO'));
  749. if ( $check != $uri && $check != str_replace(urlencode($_original_id), $_original_id, $uri) ) {
  750. if ( $issue_404 && rtrim( $check, " \t\n\r\0\x0B/" ) !== rtrim( $uri, " \t\n\r\0\x0B/" ) ) {
  751. status_header( 404 );
  752. bb_load_template( '404.php' );
  753. } else {
  754. wp_redirect( $permalink );
  755. }
  756. exit;
  757. }
  758. do_action( 'post_permalink', $permalink );
  759. }
  760. /* Profile/Admin */
  761. function bb_global_profile_menu_structure() {
  762. global $user_id, $profile_menu, $profile_hooks;
  763. // Menu item name
  764. // The capability required for own user to view the tab ('' to allow non logged in access)
  765. // The capability required for other users to view the tab ('' to allow non logged in access)
  766. // The URL of the item's file
  767. // Item name for URL (nontranslated)
  768. $profile_menu[0] = array(__('Edit'), 'edit_profile', 'edit_users', 'profile-edit.php', 'edit');
  769. $profile_menu[5] = array(__('Favorites'), '', '', 'favorites.php', 'favorites');
  770. // Create list of page plugin hook names the current user can access
  771. $profile_hooks = array();
  772. foreach ($profile_menu as $profile_tab)
  773. if ( bb_can_access_tab( $profile_tab, bb_get_current_user_info( 'id' ), $user_id ) )
  774. $profile_hooks[bb_sanitize_with_dashes($profile_tab[4])] = $profile_tab[3];
  775. do_action('bb_profile_menu');
  776. ksort($profile_menu);
  777. }
  778. function bb_add_profile_tab($tab_title, $users_cap, $others_cap, $file, $arg = false) {
  779. global $profile_menu, $profile_hooks, $user_id;
  780. $arg = $arg ? $arg : $tab_title;
  781. $profile_tab = array($tab_title, $users_cap, $others_cap, $file, $arg);
  782. $profile_menu[] = $profile_tab;
  783. if ( bb_can_access_tab( $profile_tab, bb_get_current_user_info( 'id' ), $user_id ) )
  784. $profile_hooks[bb_sanitize_with_dashes($arg)] = $file;
  785. }
  786. function bb_can_access_tab( $profile_tab, $viewer_id, $owner_id ) {
  787. global $bb_current_user;
  788. $viewer_id = (int) $viewer_id;
  789. $owner_id = (int) $owner_id;
  790. if ( $viewer_id == bb_get_current_user_info( 'id' ) )
  791. $viewer =& $bb_current_user;
  792. else
  793. $viewer = new BP_User( $viewer_id );
  794. if ( !$viewer )
  795. return '' === $profile_tab[2];
  796. if ( $owner_id == $viewer_id ) {
  797. if ( '' === $profile_tab[1] )
  798. return true;
  799. else
  800. return $viewer->has_cap($profile_tab[1]);
  801. } else {
  802. if ( '' === $profile_tab[2] )
  803. return true;
  804. else
  805. return $viewer->has_cap($profile_tab[2]);
  806. }
  807. }
  808. //meta_key => (required?, Label, hCard property). Don't use user_{anything} as the name of your meta_key.
  809. function bb_get_profile_info_keys( $context = null ) {
  810. return apply_filters( 'get_profile_info_keys', array(
  811. 'first_name' => array(0, __('First name')),
  812. 'last_name' => array(0, __('Last name')),
  813. 'display_name' => array(1, __('Display name as')),
  814. 'user_email' => array(1, __('Email'), 'email'),
  815. 'user_url' => array(0, __('Website'), 'url'),
  816. 'from' => array(0, __('Location')),
  817. 'occ' => array(0, __('Occupation'), 'role'),
  818. 'interest' => array(0, __('Interests')),
  819. ), $context );
  820. }
  821. function bb_get_profile_admin_keys( $context = null ) {
  822. global $bbdb;
  823. return apply_filters( 'get_profile_admin_keys', array(
  824. $bbdb->prefix . 'title' => array(0, __('Custom Title'))
  825. ), $context );
  826. }
  827. function bb_get_assignable_caps() {
  828. $caps = array();
  829. if ( $throttle_time = bb_get_option( 'throttle_time' ) )
  830. $caps['throttle'] = sprintf( __('Ignore the %d second post throttling limit'), $throttle_time );
  831. return apply_filters( 'get_assignable_caps', $caps );
  832. }
  833. /* Views */
  834. function bb_get_views() {
  835. global $bb_views;
  836. $views = array();
  837. foreach ( (array) $bb_views as $view => $array )
  838. $views[$view] = $array['title'];
  839. return $views;
  840. }
  841. function bb_register_view( $view, $title, $query_args = '', $feed = TRUE ) {
  842. global $bb_views;
  843. $view = bb_slug_sanitize( $view );
  844. $title = esc_html( $title );
  845. if ( !$view || !$title )
  846. return false;
  847. $query_args = wp_parse_args( $query_args );
  848. if ( !$sticky_set = isset($query_args['sticky']) )
  849. $query_args['sticky'] = 'no';
  850. $bb_views[$view]['title'] = $title;
  851. $bb_views[$view]['query'] = $query_args;
  852. $bb_views[$view]['sticky'] = !$sticky_set; // No sticky set => split into stickies and not
  853. $bb_views[$view]['feed'] = $feed;
  854. return $bb_views[$view];
  855. }
  856. function bb_deregister_view( $view ) {
  857. global $bb_views;
  858. $view = bb_slug_sanitize( $view );
  859. if ( !isset($bb_views[$view]) )
  860. return false;
  861. unset($GLOBALS['bb_views'][$view]);
  862. return true;
  863. }
  864. function bb_view_query( $view, $new_args = '' ) {
  865. global $bb_views;
  866. $view = bb_slug_sanitize( $view );
  867. if ( !isset($bb_views[$view]) )
  868. return false;
  869. if ( $new_args ) {
  870. $new_args = wp_parse_args( $new_args );
  871. $query_args = array_merge( $bb_views[$view]['query'], $new_args );
  872. } else {
  873. $query_args = $bb_views[$view]['query'];
  874. }
  875. return new BB_Query( 'topic', $query_args, "bb_view_$view" );
  876. }
  877. function bb_get_view_query_args( $view ) {
  878. global $bb_views;
  879. $view = bb_slug_sanitize( $view );
  880. if ( !isset($bb_views[$view]) )
  881. return false;
  882. return $bb_views[$view]['query'];
  883. }
  884. function bb_register_default_views() {
  885. // no posts (besides the first one), older than 2 hours
  886. bb_register_view( 'no-replies', __('Topics with no replies'), array( 'post_count' => 1, 'started' => '<' . gmdate( 'YmdH', time() - 7200 ) ) );
  887. bb_register_view( 'untagged' , __('Topics with no tags') , array( 'tag_count' => 0 ) );
  888. }
  889. /* Feeds */
  890. /**
  891. * Send status headers for clients supporting Conditional Get
  892. *
  893. * The function sends the Last-Modified and ETag headers for all clients. It
  894. * then checks both the If-None-Match and If-Modified-Since headers to see if
  895. * the client has used them. If so, and the ETag does matches the client ETag
  896. * or the last modified date sent by the client is newer or the same as the
  897. * generated last modified, the function sends a 304 Not Modified and exits.
  898. *
  899. * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3
  900. * @param string $bb_last_modified Last modified time. Must be a HTTP-date
  901. */
  902. function bb_send_304( $bb_last_modified ) {
  903. $bb_etag = '"' . md5($bb_last_modified) . '"';
  904. @header("Last-Modified: $bb_last_modified");
  905. @header("ETag: $bb_etag");
  906. // Support for Conditional GET
  907. if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $client_etag = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
  908. else $client_etag = false;
  909. $client_last_modified = trim( $_SERVER['HTTP_IF_MODIFIED_SINCE']);
  910. // If string is empty, return 0. If not, attempt to parse into a timestamp
  911. $client_modified_timestamp = $client_last_modified ? bb_gmtstrtotime($client_last_modified) : 0;
  912. // Make a timestamp for our most recent modification...
  913. $bb_modified_timestamp = bb_gmtstrtotime($bb_last_modified);
  914. if ( ($client_last_modified && $client_etag) ?
  915. (($client_modified_timestamp >= $bb_modified_timestamp) && ($client_etag == $bb_etag)) :
  916. (($client_modified_timestamp >= $bb_modified_timestamp) || ($client_etag == $bb_etag)) ) {
  917. status_header( 304 );
  918. exit;
  919. }
  920. }
  921. /* Nonce */
  922. /**
  923. * Retrieve URL with nonce added to URL query.
  924. *
  925. * @package bbPress
  926. * @subpackage Security
  927. * @since 1.0
  928. *
  929. * @param string $actionurl URL to add nonce action
  930. * @param string $action Optional. Nonce action name
  931. * @return string URL with nonce action added.
  932. */
  933. function bb_nonce_url( $actionurl, $action = -1 ) {
  934. $actionurl = str_replace( '&amp;', '&', $actionurl );
  935. $nonce = bb_create_nonce( $action );
  936. return esc_html( add_query_arg( '_wpnonce', $nonce, $actionurl ) );
  937. }
  938. /**
  939. * Retrieve or display nonce hidden field for forms.
  940. *
  941. * The nonce field is used to validate that the contents of the form came from
  942. * the location on the current site and not somewhere else. The nonce does not
  943. * offer absolute protection, but should protect against most cases. It is very
  944. * important to use nonce field in forms.
  945. *
  946. * If you set $echo to true and set $referer to true, then you will need to
  947. * retrieve the {@link wp_referer_field() wp referer field}. If you have the
  948. * $referer set to true and are echoing the nonce field, it will also echo the
  949. * referer field.
  950. *
  951. * The $action and $name are optional, but if you want to have better security,
  952. * it is strongly suggested to set those two parameters. It is easier to just
  953. * call the function without any parameters, because validation of the nonce
  954. * doesn't require any parameters, but since crackers know what the default is
  955. * it won't be difficult for them to find a way around your nonce and cause
  956. * damage.
  957. *
  958. * The input name will be whatever $name value you gave. The input value will be
  959. * the nonce creation value.
  960. *
  961. * @package bbPress
  962. * @subpackage Security
  963. * @since 1.0
  964. *
  965. * @param string $action Optional. Action name.
  966. * @param string $name Optional. Nonce name.
  967. * @param bool $referer Optional, default true. Whether to set the referer field for validation.
  968. * @param bool $echo Optional, default true. Whether to display or return hidden form field.
  969. * @return string Nonce field.
  970. */
  971. function bb_nonce_field( $action = -1, $name = "_wpnonce", $referer = true , $echo = true ) {
  972. $name = esc_attr( $name );
  973. $nonce = bb_create_nonce( $action );
  974. $nonce_field = '<input type="hidden" id="' . $name . '" name="' . $name . '" value="' . $nonce . '" />';
  975. if ( $echo )
  976. echo $nonce_field;
  977. if ( $referer )
  978. wp_referer_field( $echo, 'previous' );
  979. return $nonce_field;
  980. }
  981. function bb_nonce_ays( $action )
  982. {
  983. $title = __( 'bbPress Failure Notice' );
  984. $html .= "\t<div id='message' class='updated fade'>\n\t<p>" . esc_html( bb_explain_nonce( $action ) ) . "</p>\n\t<p>";
  985. if ( wp_get_referer() )
  986. $html .= "<a href='" . remove_query_arg( 'updated', esc_url( wp_get_referer() ) ) . "'>" . __( 'Please try again.' ) . "</a>";
  987. $html .= "</p>\n\t</div>\n";
  988. $html .= "</body>\n</html>";
  989. bb_die( $html, $title );
  990. }
  991. function bb_install_header( $title = '', $header = false, $logo = false )
  992. {
  993. if ( empty($title) )
  994. if ( function_exists('__') )
  995. $title = __('bbPress');
  996. else
  997. $title = 'bbPress';
  998. $uri = false;
  999. if ( function_exists('bb_get_uri') && !BB_INSTALLING ) {
  1000. $uri = bb_get_uri();
  1001. $uri_stylesheet = bb_get_uri('bb-admin/install.css', null, BB_URI_CONTEXT_LINK_STYLESHEET_HREF + BB_URI_CONTEXT_BB_INSTALLER);
  1002. $uri_stylesheet_rtl = bb_get_uri('bb-admin/install-rtl.css', null, BB_URI_CONTEXT_LINK_STYLESHEET_HREF + BB_URI_CONTEXT_BB_INSTALLER);
  1003. $uri_logo = bb_get_uri('bb-admin/images/bbpress-logo.png', null, BB_URI_CONTEXT_IMG_SRC + BB_URI_CONTEXT_BB_INSTALLER);
  1004. }
  1005. if (!$uri) {
  1006. $uri = preg_replace('|(/bb-admin)?/[^/]+?$|', '/', $_SERVER['PHP_SELF']);
  1007. $uri_stylesheet = $uri . 'bb-admin/install.css';
  1008. $uri_stylesheet_rtl = $uri . 'bb-admin/install-rtl.css';
  1009. $uri_logo = $uri . 'bb-admin/images/bbpress-logo.png';
  1010. }
  1011. header('Content-Type: text/html; charset=utf-8');
  1012. ?>
  1013. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1014. <html xmlns="http://www.w3.org/1999/xhtml"<?php if ( function_exists( 'bb_language_attributes' ) ) bb_language_attributes(); ?>>
  1015. <head>
  1016. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  1017. <title><?php echo $title; ?></title>
  1018. <meta name="robots" content="noindex, nofollow" />
  1019. <link rel="stylesheet" href="<?php echo $uri_stylesheet; ?>" type="text/css" />
  1020. <?php
  1021. if ( function_exists( 'bb_get_option' ) && 'rtl' == bb_get_option( 'text_direction' ) ) {
  1022. ?>
  1023. <link rel="stylesheet" href="<?php echo $uri_stylesheet_rtl; ?>" type="text/css" />
  1024. <?php
  1025. }
  1026. ?>
  1027. </head>
  1028. <body>
  1029. <div id="container">
  1030. <?php
  1031. if ( $logo ) {
  1032. ?>
  1033. <div class="logo">
  1034. <img src="<?php echo $uri_logo; ?>" alt="bbPress" />
  1035. </div>
  1036. <?php
  1037. }
  1038. if ( !empty($header) ) {
  1039. ?>
  1040. <h1>
  1041. <?php echo $header; ?>
  1042. </h1>
  1043. <?php
  1044. }
  1045. }
  1046. function bb_install_footer() {
  1047. ?>
  1048. </div>
  1049. </body>
  1050. </html>
  1051. <?php
  1052. }
  1053. function bb_die( $message, $title = '', $header = 0 ) {
  1054. global $bb_locale;
  1055. if ( $header && !headers_sent() )
  1056. status_header( $header );
  1057. if ( function_exists( 'is_wp_error' ) && is_wp_error( $message ) ) {
  1058. if ( empty( $title ) ) {
  1059. $error_data = $message->get_error_data();
  1060. if ( is_array( $error_data ) && isset( $error_data['title'] ) )
  1061. $title = $error_data['title'];
  1062. }
  1063. $errors = $message->get_error_messages();
  1064. switch ( count( $errors ) ) :
  1065. case 0 :
  1066. $message = '';
  1067. break;
  1068. case 1 :
  1069. $message = "<p>{$errors[0]}</p>";
  1070. break;
  1071. default :
  1072. $message = "<ul>\n\t\t<li>" . join( "</li>\n\t\t<li>", $errors ) . "</li>\n\t</ul>";
  1073. break;
  1074. endswitch;
  1075. } elseif ( is_string( $message ) ) {
  1076. $message = bb_autop( $message );
  1077. }
  1078. if ( empty($title) )
  1079. $title = __('bbPress &rsaquo; Error');
  1080. bb_install_header( $title );
  1081. ?>
  1082. <?php echo $message; ?>
  1083. <?php
  1084. if ($uri = bb_get_uri()) {
  1085. ?>
  1086. <p class="last"><?php printf( __('Back to <a href="%s">%s</a>.'), $uri, bb_get_option( 'name' ) ); ?></p>
  1087. <?php
  1088. }
  1089. bb_install_footer();
  1090. die();
  1091. }
  1092. function bb_explain_nonce($action) {
  1093. if ( $action !== -1 && preg_match('/([a-z]+)-([a-z]+)(_(.+))?/', $action, $matches) ) {
  1094. $verb = $matches[1];
  1095. $noun = $matches[2];
  1096. $trans = array();
  1097. $trans['create']['post'] = array(__('Your attempt to submit this post has failed.'), false);
  1098. $trans['edit']['post'] = array(__('Your attempt to edit this post has failed.'), false);
  1099. $trans['delete']['post'] = array(__('Your attempt to delete this post has failed.'), false);
  1100. $trans['create']['topic'] = array(__('Your attempt to create this topic has failed.'), false);
  1101. $trans['resolve']['topic'] = array(__('Your attempt to change the resolution status of this topic has failed.'), false);
  1102. $trans['delete']['topic'] = array(__('Your attempt to delete this topic has failed.'), false);
  1103. $trans['close']['topic'] = array(__('Your attempt to change the status of this topic has failed.'), false);
  1104. $trans['stick']['topic'] = array(__('Your attempt to change the sticky status of this topic has failed.'), false);
  1105. $trans['move']['topic'] = array(__('Your attempt to move this topic has failed.'), false);
  1106. $trans['add']['tag'] = array(__('Your attempt to add this tag to this topic has failed.'), false);
  1107. $trans['rename']['tag'] = array(__('Your attempt to rename this tag has failed.'), false);
  1108. $trans['merge']['tag'] = array(__('Your attempt to submit these tags has failed.'), false);
  1109. $trans['destroy']['tag'] = array(__('Your attempt to destroy this tag has failed.'), false);
  1110. $trans['remove']['tag'] = array(__('Your attempt to remove this tag from this topic has failed.'), false);
  1111. $trans['toggle']['favorite'] = array(__('Your attempt to toggle your favorite status for this topic has failed.'), false);
  1112. $trans['edit']['profile'] = array(__("Your attempt to edit this user's profile has failed."), false);
  1113. $trans['add']['forum'] = array(__("Your attempt to add this forum has failed."), false);
  1114. $trans['update']['forums'] = array(__("Your attempt to update your forums has failed."), false);
  1115. $trans['delete']['forums'] = array(__("Your attempt to delete that forum has failed."), false);
  1116. $trans['do']['counts'] = array(__("Your attempt to recount these items has failed."), false);
  1117. $trans['switch']['theme'] = array(__("Your attempt to switch themes has failed."), false);
  1118. if ( isset($trans[$verb][$noun]) ) {
  1119. if ( !empty($trans[$verb][$noun][1]) ) {
  1120. $lookup = $trans[$verb][$noun][1];
  1121. $object = $matches[4];
  1122. if ( 'use_id' != $lookup )
  1123. $object = call_user_func($lookup, $object);
  1124. return sprintf($trans[$verb][$noun][0], esc_html( $object ));
  1125. } else {
  1126. return $trans[$verb][$noun][0];
  1127. }
  1128. }
  1129. }
  1130. return apply_filters( 'bb_explain_nonce_' . $verb . '-' . $noun, __('Your attempt to do this has failed.'), $matches[4] );
  1131. }
  1132. /* DB Helpers */
  1133. function bb_count_last_query( $query = '' ) {
  1134. global $bbdb, $bb_last_countable_query;
  1135. if ( $query )
  1136. $q = $query;
  1137. elseif ( $bb_last_countable_query )
  1138. $q = $bb_last_countable_query;
  1139. else
  1140. $q = $bbdb->last_query;
  1141. if ( false === strpos($q, 'SELECT') )
  1142. return false;
  1143. if ( false !== strpos($q, 'SQL_CALC_FOUND_ROWS') )
  1144. return (int) $bbdb->get_var( "SELECT FOUND_ROWS()" );
  1145. $q_original = $q;
  1146. $q = preg_replace(
  1147. array('/SELECT.*?\s+FROM/', '/LIMIT [0-9]+(\s*,\s*[0-9]+)?/', '/ORDER BY\s+.*$/', '/DESC/', '/ASC/'),
  1148. array('SELECT COUNT(*) FROM', ''),
  1149. $q
  1150. );
  1151. if ( preg_match( '/GROUP BY\s+(\S+)/', $q, $matches ) )
  1152. $q = str_replace( array( 'COUNT(*)', $matches[0] ), array( "COUNT(DISTINCT $matches[1])", '' ), $q );
  1153. if ( !$query )
  1154. $bb_last_countable_query = '';
  1155. $q = apply_filters( 'bb_count_last_query', $q, $q_original );
  1156. return (int) $bbdb->get_var($q);
  1157. }
  1158. function bb_no_where( $where ) {
  1159. return;
  1160. }
  1161. /* Plugins/Themes utility */
  1162. function bb_basename( $file, $directories )
  1163. {
  1164. if ( strpos( $file, '#' ) !== false ) {
  1165. return $file; // It's already a basename
  1166. }
  1167. foreach ( $directories as $type => $directory ) {
  1168. if ( strpos( $file, $directory ) !== false ) {
  1169. break; // Keep the $file and $directory set and use them below, nifty huh?
  1170. }
  1171. }
  1172. list( $file, $directory ) = str_replace( '\\','/', array( $file, $directory ) );
  1173. list( $file, $directory ) = preg_replace( '|/+|','/', array( $file,$directory ) );
  1174. $file = preg_replace( '|^.*' . preg_quote( $directory, '|' ) . '|', $type . '#', $file );
  1175. return $file;
  1176. }
  1177. /* Plugins */
  1178. function bb_plugin_basename( $file )
  1179. {
  1180. global $bb;
  1181. $directories = array();
  1182. foreach ( $bb->plugin_locations as $_name => $_data ) {
  1183. $directories[$_name] = $_data['dir'];
  1184. }
  1185. return bb_basename( $file, $directories );
  1186. }
  1187. function bb_register_plugin_activation_hook( $file, $function )
  1188. {
  1189. $file = bb_plugin_basename( $file );
  1190. add_action( 'bb_activate_plugin_' . $file, $function );
  1191. }
  1192. function bb_register_plugin_deactivation_hook( $file, $function )
  1193. {
  1194. $file = bb_plugin_basename( $file );
  1195. add_action( 'bb_deactivate_plugin_' . $file, $function );
  1196. }
  1197. function bb_get_plugin_uri( $plugin = false )
  1198. {
  1199. global $bb;
  1200. if ( preg_match( '/^([a-z0-9_-]+)#((?:[a-z0-9\/\\_-]+.)+)(php)$/i', $plugin, $_matches ) ) {
  1201. $plugin_uri = $bb->plugin_locations[$_matches[1]]['url'] . $_matches[2] . $_matches[3];
  1202. $plugin_uri = dirname( $plugin_uri ) . '/';
  1203. } else {
  1204. $plugin_uri = $bb->plugin_locations['core']['url'];
  1205. }
  1206. return apply_filters( 'bb_get_plugin_uri', $plugin_uri, $plugin );
  1207. }
  1208. function bb_get_plugin_directory( $plugin = false, $path = false )
  1209. {
  1210. global $bb;
  1211. if ( preg_match( '/^([a-z0-9_-]+)#((?:[a-z0-9\/\\_-]+.)+)(php)$/i', $plugin, $_matches ) ) {
  1212. $plugin_directory = $bb->plugin_locations[$_matches[1]]['dir'] . $_matches[2] . $_matches[3];
  1213. if ( !$path ) {
  1214. $plugin_directory = dirname( $plugin_directory ) . '/';
  1215. }
  1216. } else {
  1217. $plugin_directory = $bb->plugin_locations['core']['dir'];
  1218. }
  1219. return apply_filters( 'bb_get_plugin_directory', $plugin_directory, $plugin, $path );
  1220. }
  1221. function bb_get_plugin_path( $plugin = false )
  1222. {
  1223. $plugin_path = bb_get_plugin_directory( $plugin, true );
  1224. return apply_filters( 'bb_get_plugin_path', $plugin_path, $plugin );
  1225. }
  1226. /* Themes / Templates */
  1227. function bb_get_active_theme_directory()
  1228. {
  1229. return apply_filters( 'bb_get_active_theme_directory', bb_get_theme_directory() );
  1230. }
  1231. function bb_get_theme_directory( $theme = false )
  1232. {
  1233. global $bb;
  1234. if ( !$theme ) {
  1235. $theme = bb_get_option( 'bb_active_theme' );
  1236. }
  1237. if ( preg_match( '/^([a-z0-9_-]+)#([\.a-z0-9_-]+)$/i', $theme, $_matches ) ) {
  1238. $theme_directory = $bb->theme_locations[$_matches[1]]['dir'] . $_matches[2] . '/';
  1239. } else {
  1240. $theme_directory = BB_DEFAULT_THEME_DIR;
  1241. }
  1242. return $theme_directory;
  1243. }
  1244. function bb_get_themes()
  1245. {
  1246. $r = array();
  1247. global $bb;
  1248. foreach ( $bb->theme_locations as $_name => $_data ) {
  1249. if ( $themes_dir = @dir( $_data['dir'] ) ) {
  1250. while( ( $theme_dir = $themes_dir->read() ) !== false ) {
  1251. if ( is_file( $_data['dir'] . $theme_dir . '/style.css' ) && is_readable( $_data['dir'] . $theme_dir . '/style.css' ) && '.' != $theme_dir{0} ) {
  1252. $r[$_name . '#' . $theme_dir] = $_name . '#' . $theme_dir;
  1253. }
  1254. }
  1255. }
  1256. }
  1257. ksort( $r );
  1258. return $r;
  1259. }
  1260. function bb_theme_basename( $file )
  1261. {
  1262. global $bb;
  1263. $directories = array();
  1264. foreach ( $bb->theme_locations as $_name => $_data ) {
  1265. $directories[$_name] = $_data['dir'];
  1266. }
  1267. $file = bb_basename( $file, $directories );
  1268. $file = preg_replace( '|/+.*|', '', $file );
  1269. return $file;
  1270. }
  1271. function bb_register_theme_activation_hook( $file, $function )
  1272. {
  1273. $file = bb_theme_basename( $file );
  1274. add_action( 'bb_activate_theme_' . $file, $function );
  1275. }
  1276. function bb_register_theme_deactivation_hook( $file, $function )
  1277. {
  1278. $file = bb_theme_basename( $file );
  1279. add_action( 'bb_deactivate_theme_' . $file, $function );
  1280. }
  1281. /* Search Functions */
  1282. // NOT bbdb::prepared
  1283. function bb_user_search( $args = '' ) {
  1284. global $bbdb, $bb_last_countable_query;
  1285. if ( $args && is_string( $args ) && false === strpos( $args, '=' ) ) {
  1286. $args = array( 'query' => $args );
  1287. }
  1288. $defaults = array(
  1289. 'query' => '',
  1290. 'append_meta' => true,
  1291. 'user_login' => true,
  1292. 'display_name' => true,
  1293. 'user_nicename' => false,
  1294. 'user_url' => true,
  1295. 'user_email' => false,
  1296. 'user_meta' => false,
  1297. 'users_per_page' => false,
  1298. 'page' => false,
  1299. 'roles' => false
  1300. );
  1301. $args = wp_parse_args( $args, $defaults );
  1302. extract( $args, EXTR_SKIP );
  1303. $query = trim( $query );
  1304. if ( $query && strlen( preg_replace( '/[^a-z0-9]/i', '', $query ) ) < 3 ) {
  1305. return new WP_Error( 'invalid-query', __('Your search term was too short') );
  1306. }
  1307. $query = $bbdb->escape( $query );
  1308. if ( !$page ) {
  1309. $page = $GLOBALS['page'];
  1310. }
  1311. $page = (int) $page;
  1312. $limit = 0 < (int) $users_per_page ? (int) $users_per_page : bb_get_option( 'page_topics' );
  1313. if ( 1 < $page ) {
  1314. $limit = ($limit * ($page - 1)) . ", $limit";
  1315. }
  1316. $likeit = preg_replace( '/\s+/', '%', like_escape( $query ) );
  1317. $fields = array();
  1318. foreach ( array( 'user_login', 'display_name', 'user_nicename', 'user_url', 'user_email' ) as $field ) {
  1319. if ( $$field ) {
  1320. $fields[] = $field;
  1321. }
  1322. }
  1323. if ( $roles ) {
  1324. $roles = (array) $roles;
  1325. }
  1326. if ( $roles && !empty( $roles ) && false === $role_user_ids = apply_filters( 'bb_user_search_role_user_ids', false, $roles, $args ) ) {
  1327. $role_meta_key = $bbdb->escape( $bbdb->prefix . 'capabilities' );
  1328. $role_sql_terms = array();
  1329. foreach ( $roles as $role ) {
  1330. $role_sql_terms[] = "`meta_value` LIKE '%" . $bbdb->escape( like_escape( $role ) ) . "%'";
  1331. }
  1332. $role_sql_terms = join( ' OR ', $role_sql_terms );
  1333. $role_sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_key` = '$role_meta_key' AND ($role_sql_terms);";
  1334. $role_user_ids = $bbdb->get_col( $role_sql, 0 );
  1335. if ( is_wp_error( $role_user_ids ) ) {
  1336. return false;
  1337. }
  1338. }
  1339. if ( is_array( $role_user_ids ) && empty( $role_user_ids ) ) {
  1340. return false;
  1341. }
  1342. if ( $query && $user_meta && false === $meta_user_ids = apply_filters( 'bb_user_search_meta_user_ids', false, $args ) ) {
  1343. $meta_sql = "SELECT `user_id` FROM `$bbdb->usermeta` WHERE `meta_value` LIKE ('%$likeit%')";
  1344. if ( empty( $fields ) ) {
  1345. $meta_sql .= " LIMIT $limit";
  1346. }
  1347. $meta_user_ids = $bbdb->get_col( $meta_sql, 0 );
  1348. if ( is_wp_error( $meta_user_ids ) ) {
  1349. $meta_user_ids = false;
  1350. }
  1351. }
  1352. $user_ids = array();
  1353. if ( $role_user_ids && $meta_user_ids ) {
  1354. $user_ids = array_intersect( (array) $role_user_ids, (array) $meta_user_ids );
  1355. } elseif ( $role_user_ids ) {
  1356. $user_ids = (array) $role_user_ids;
  1357. } elseif ( $meta_user_ids ) {
  1358. $user_ids = (array) $meta_user_ids;
  1359. }
  1360. $sql = "SELECT * FROM $bbdb->users";
  1361. $sql_terms = array();
  1362. if ( $query && count( $fields ) ) {
  1363. foreach ( $fields as $field ) {
  1364. $sql_terms[] = "$field LIKE ('%$likeit%')";
  1365. }
  1366. }
  1367. $user_ids_sql = '';
  1368. if ( $user_ids ) {
  1369. $user_ids_sql = "AND ID IN (". join(',', $user_ids) . ")";
  1370. }
  1371. if ( $query && empty( $sql_terms ) ) {
  1372. return new WP_Error( 'invalid-query', __( 'Your query parameters are invalid' ) );
  1373. }
  1374. if ( count( $sql_terms ) || count( $user_ids ) ) {
  1375. $sql .= ' WHERE ';
  1376. }
  1377. if ( count( $sql_terms ) ) {
  1378. $sql .= '(' . implode( ' OR ', $sql_terms ) . ')';
  1379. }
  1380. if ( count( $sql_terms ) && count( $user_ids ) ) {
  1381. $sql .= ' AND ';
  1382. }
  1383. if ( count( $user_ids ) ) {
  1384. $sql .= '`ID` IN (' . join( ',', $user_ids ) . ')';
  1385. }
  1386. $sql .= " ORDER BY user_login LIMIT $limit";
  1387. $bb_last_countable_query = $sql;
  1388. do_action( 'bb_user_search', $sql, $args );

Large files files are truncated, but you can click here to view the full file