3 /* These functions can be replaced via plugins. They are loaded after
6 if ( !function_exists('set_current_user') ) :
7 function set_current_user($id, $name = '') {
8 return wp_set_current_user($id, $name);
12 if ( !function_exists('wp_set_current_user') ) :
13 function wp_set_current_user($id, $name = '') {
16 if ( isset($current_user) && ($id == $current_user->ID) )
19 $current_user = new WP_User($id, $name);
21 setup_userdata($current_user->ID);
23 do_action('set_current_user');
29 if ( !function_exists('wp_get_current_user') ) :
30 function wp_get_current_user() {
33 get_currentuserinfo();
39 if ( !function_exists('get_currentuserinfo') ) :
40 function get_currentuserinfo() {
43 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
46 if ( ! empty($current_user) )
49 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) ||
50 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) {
51 wp_set_current_user(0);
55 $user_login = $_COOKIE[USER_COOKIE];
56 wp_set_current_user(0, $user_login);
60 if ( !function_exists('get_userdata') ) :
61 function get_userdata( $user_id ) {
63 $user_id = (int) $user_id;
67 $user = wp_cache_get($user_id, 'users');
72 if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id' LIMIT 1") )
76 $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
80 foreach ( $metavalues as $meta ) {
81 @ $value = unserialize($meta->meta_value);
83 $value = $meta->meta_value;
84 $user->{$meta->meta_key} = $value;
86 // We need to set user_level from meta, not row
87 if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
88 $user->user_level = $meta->meta_value;
92 // For backwards compat.
93 if ( isset($user->first_name) )
94 $user->user_firstname = $user->first_name;
95 if ( isset($user->last_name) )
96 $user->user_lastname = $user->last_name;
97 if ( isset($user->description) )
98 $user->user_description = $user->description;
100 wp_cache_add($user_id, $user, 'users');
101 wp_cache_add($user->user_login, $user, 'userlogins');
107 if ( !function_exists('update_user_cache') ) :
108 function update_user_cache() {
113 if ( !function_exists('get_userdatabylogin') ) :
114 function get_userdatabylogin($user_login) {
116 $user_login = sanitize_user( $user_login );
118 if ( empty( $user_login ) )
121 $userdata = wp_cache_get($user_login, 'userlogins');
125 if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
128 $wpdb->hide_errors();
129 $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user->ID'");
130 $wpdb->show_errors();
133 foreach ( $metavalues as $meta ) {
134 @ $value = unserialize($meta->meta_value);
135 if ($value === FALSE)
136 $value = $meta->meta_value;
137 $user->{$meta->meta_key} = $value;
139 // We need to set user_level from meta, not row
140 if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
141 $user->user_level = $meta->meta_value;
145 // For backwards compat.
146 if ( isset($user->first_name) )
147 $user->user_firstname = $user->first_name;
148 if ( isset($user->last_name) )
149 $user->user_lastname = $user->last_name;
150 if ( isset($user->description) )
151 $user->user_description = $user->description;
153 wp_cache_add($user->ID, $user, 'users');
154 wp_cache_add($user->user_login, $user, 'userlogins');
161 if ( !function_exists('wp_mail') ) :
162 function wp_mail($to, $subject, $message, $headers = '') {
163 if( $headers == '' ) {
164 $headers = "MIME-Version: 1.0\n" .
165 "From: wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . "\n" .
166 "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
169 return @mail($to, $subject, $message, $headers);
173 if ( !function_exists('wp_login') ) :
174 function wp_login($username, $password, $already_md5 = false) {
175 global $wpdb, $error;
177 if ( '' == $username )
180 if ( '' == $password ) {
181 $error = __('<strong>Error</strong>: The password field is empty.');
185 $login = get_userdatabylogin($username);
186 //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
189 $error = __('<strong>Error</strong>: Wrong username.');
192 // If the password is already_md5, it has been double hashed.
193 // Otherwise, it is plain text.
194 if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
197 $error = __('<strong>Error</strong>: Incorrect password.');
205 if ( !function_exists('is_user_logged_in') ) :
206 function is_user_logged_in() {
207 $user = wp_get_current_user();
209 if ( $user->id == 0 )
216 if ( !function_exists('auth_redirect') ) :
217 function auth_redirect() {
218 // Checks if a user is logged in, if not redirects them to the login page
219 if ( (!empty($_COOKIE[USER_COOKIE]) &&
220 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
221 (empty($_COOKIE[USER_COOKIE])) ) {
224 wp_redirect(get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
230 if ( !function_exists('check_admin_referer') ) :
231 function check_admin_referer($action = -1) {
232 $adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
233 $referer = strtolower(wp_get_referer());
234 if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
235 !(-1 == $action && strstr($referer, $adminurl)) ) {
236 wp_nonce_ays($action);
239 do_action('check_admin_referer', $action);
243 if ( !function_exists('check_ajax_referer') ) :
244 function check_ajax_referer() {
245 $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
246 foreach ( $cookie as $tasty ) {
247 if ( false !== strpos($tasty, USER_COOKIE) )
248 $user = urldecode(substr(strstr($tasty, '='), 1)); // Nasty double encoding
249 if ( false !== strpos($tasty, PASS_COOKIE) )
250 $pass = urldecode(substr(strstr($tasty, '='), 1));
252 if ( wp_login( $user, $pass, true ) )
258 // Cookie safe redirect. Works around IIS Set-Cookie bug.
259 // http://support.microsoft.com/kb/q176113/
260 if ( !function_exists('wp_redirect') ) :
261 function wp_redirect($location) {
264 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
266 $strip = array('%0d', '%0a');
267 $location = str_replace($strip, '', $location);
270 header("Refresh: 0;url=$location");
272 header("Location: $location");
276 if ( !function_exists('wp_get_cookie_login') ):
277 function wp_get_cookie_login() {
278 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
281 return array('login' => $_COOKIE[USER_COOKIE], 'password' => $_COOKIE[PASS_COOKIE]);
286 if ( !function_exists('wp_setcookie') ) :
287 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
289 $password = md5( md5($password) ); // Double hash the password in the cookie.
292 $cookiepath = COOKIEPATH;
294 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
296 if ( empty($siteurl) ) {
297 $sitecookiepath = SITECOOKIEPATH;
298 $cookiehash = COOKIEHASH;
300 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
301 $cookiehash = md5($siteurl);
305 $expire = time() + 31536000;
309 setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
310 setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
312 if ( $cookiepath != $sitecookiepath ) {
313 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
314 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
319 if ( !function_exists('wp_clearcookie') ) :
320 function wp_clearcookie() {
321 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
322 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
323 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
324 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
328 if ( ! function_exists('wp_notify_postauthor') ) :
329 function wp_notify_postauthor($comment_id, $comment_type='') {
332 $comment = get_comment($comment_id);
333 $post = get_post($comment->comment_post_ID);
334 $user = get_userdata( $post->post_author );
336 if ('' == $user->user_email) return false; // If there's no email to send the comment to
338 $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
340 $blogname = get_settings('blogname');
342 if ( empty( $comment_type ) ) $comment_type = 'comment';
344 if ('comment' == $comment_type) {
345 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
346 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
347 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
348 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
349 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
350 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
351 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
352 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
353 } elseif ('trackback' == $comment_type) {
354 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
355 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
356 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
357 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
358 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
359 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
360 } elseif ('pingback' == $comment_type) {
361 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
362 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
363 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
364 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
365 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
366 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
368 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
369 $notify_message .= sprintf( __('To delete this comment, visit: %s'), get_settings('siteurl').'/wp-admin/post.php?action=confirmdeletecomment&p='.$comment->comment_post_ID."&comment=$comment_id" ) . "\r\n";
371 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
373 if ( '' == $comment->comment_author ) {
374 $from = "From: \"$blogname\" <$wp_email>";
375 if ( '' != $comment->comment_author_email )
376 $reply_to = "Reply-To: $comment->comment_author_email";
378 $from = "From: \"$comment->comment_author\" <$wp_email>";
379 if ( '' != $comment->comment_author_email )
380 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
383 $message_headers = "MIME-Version: 1.0\n"
385 . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
387 if ( isset($reply_to) )
388 $message_headers .= $reply_to . "\n";
390 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
391 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
392 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
394 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
400 /* wp_notify_moderator
401 notifies the moderator of the blog (usually the admin)
402 about a new comment that waits for approval
405 if ( !function_exists('wp_notify_moderator') ) :
406 function wp_notify_moderator($comment_id) {
409 if( get_settings( "moderation_notify" ) == 0 )
412 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
413 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
415 $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
416 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
418 $notify_message = sprintf( __('A new comment on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
419 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
420 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
421 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
422 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
423 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
424 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
425 $notify_message .= sprintf( __('To approve this comment, visit: %s'), get_settings('siteurl').'/wp-admin/post.php?action=mailapprovecomment&p='.$comment->comment_post_ID."&comment=$comment_id" ) . "\r\n";
426 $notify_message .= sprintf( __('To delete this comment, visit: %s'), get_settings('siteurl').'/wp-admin/post.php?action=confirmdeletecomment&p='.$comment->comment_post_ID."&comment=$comment_id" ) . "\r\n";
427 $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
428 $notify_message .= get_settings('siteurl') . "/wp-admin/moderation.php\r\n";
430 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_settings('blogname'), $post->post_title );
431 $admin_email = get_settings('admin_email');
433 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
434 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
436 @wp_mail($admin_email, $subject, $notify_message);
442 if ( !function_exists('wp_new_user_notification') ) :
443 function wp_new_user_notification($user_id, $plaintext_pass = '') {
444 $user = new WP_User($user_id);
446 $user_login = stripslashes($user->user_login);
447 $user_email = stripslashes($user->user_email);
449 $message = sprintf(__('New user registration on your blog %s:'), get_settings('blogname')) . "\r\n\r\n";
450 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
451 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
453 @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
455 if ( empty($plaintext_pass) )
458 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
459 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
460 $message .= get_settings('siteurl') . "/wp-login.php\r\n";
462 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);
467 if ( !function_exists('wp_verify_nonce') ) :
468 function wp_verify_nonce($nonce, $action = -1) {
469 $user = wp_get_current_user();
472 $i = ceil(time() / 43200);
474 //Allow for expanding range, but only do one check if we can
475 if( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce || substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
481 if ( !function_exists('wp_create_nonce') ) :
482 function wp_create_nonce($action = -1) {
483 $user = wp_get_current_user();
486 $i = ceil(time() / 43200);
488 return substr(wp_hash($i . $action . $uid), -12, 10);
492 if ( !function_exists('wp_salt') ) :
494 $salt = get_option('secret');
496 $salt = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
502 if ( !function_exists('wp_hash') ) :
503 function wp_hash($data) {
506 if ( function_exists('hash_hmac') ) {
507 return hash_hmac('md5', $data, $salt);
509 return md5($data . $salt);