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 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity, $current_user;
12 $current_user = new WP_User($id, $name);
14 $userdata = get_userdatabylogin($user_login);
16 $user_login = $userdata->user_login;
17 $user_level = $userdata->user_level;
18 $user_ID = $userdata->ID;
19 $user_email = $userdata->user_email;
20 $user_url = $userdata->user_url;
21 $user_pass_md5 = md5($userdata->user_pass);
22 $user_identity = $userdata->display_name;
24 do_action('set_current_user');
31 if ( !function_exists('get_currentuserinfo') ) :
32 function get_currentuserinfo() {
33 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity, $current_user;
35 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
38 if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) ||
39 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true) ) {
40 $current_user = new WP_User(0);
43 $user_login = $_COOKIE[USER_COOKIE];
44 $userdata = get_userdatabylogin($user_login);
45 $user_level = $userdata->user_level;
46 $user_ID = $userdata->ID;
47 $user_email = $userdata->user_email;
48 $user_url = $userdata->user_url;
49 $user_pass_md5 = md5($userdata->user_pass);
50 $user_identity = $userdata->display_name;
52 if ( empty($current_user) )
53 $current_user = new WP_User($user_ID);
57 if ( !function_exists('get_userdata') ) :
58 function get_userdata( $user_id ) {
60 $user_id = (int) $user_id;
64 $user = wp_cache_get($user_id, 'users');
69 if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id' LIMIT 1") )
73 $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
77 foreach ( $metavalues as $meta ) {
78 @ $value = unserialize($meta->meta_value);
80 $value = $meta->meta_value;
81 $user->{$meta->meta_key} = $value;
83 // We need to set user_level from meta, not row
84 if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
85 $user->user_level = $meta->meta_value;
89 // For backwards compat.
90 if ( isset($user->first_name) )
91 $user->user_firstname = $user->first_name;
92 if ( isset($user->last_name) )
93 $user->user_lastname = $user->last_name;
94 if ( isset($user->description) )
95 $user->user_description = $user->description;
97 wp_cache_add($user_id, $user, 'users');
98 wp_cache_add($user->user_login, $user, 'userlogins');
104 if ( !function_exists('update_user_cache') ) :
105 function update_user_cache() {
110 if ( !function_exists('get_userdatabylogin') ) :
111 function get_userdatabylogin($user_login) {
113 $user_login = sanitize_user( $user_login );
115 if ( empty( $user_login ) )
118 $userdata = wp_cache_get($user_login, 'userlogins');
122 if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
125 $wpdb->hide_errors();
126 $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user->ID'");
127 $wpdb->show_errors();
130 foreach ( $metavalues as $meta ) {
131 @ $value = unserialize($meta->meta_value);
132 if ($value === FALSE)
133 $value = $meta->meta_value;
134 $user->{$meta->meta_key} = $value;
136 // We need to set user_level from meta, not row
137 if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
138 $user->user_level = $meta->meta_value;
142 // For backwards compat.
143 if ( isset($user->first_name) )
144 $user->user_firstname = $user->first_name;
145 if ( isset($user->last_name) )
146 $user->user_lastname = $user->last_name;
147 if ( isset($user->description) )
148 $user->user_description = $user->description;
150 wp_cache_add($user->ID, $user, 'users');
151 wp_cache_add($user->user_login, $user, 'userlogins');
158 if ( !function_exists('wp_mail') ) :
159 function wp_mail($to, $subject, $message, $headers = '') {
160 if( $headers == '' ) {
161 $headers = "MIME-Version: 1.0\n" .
162 "From: wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . "\n" .
163 "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
166 return @mail($to, $subject, $message, $headers);
170 if ( !function_exists('wp_login') ) :
171 function wp_login($username, $password, $already_md5 = false) {
172 global $wpdb, $error;
174 if ( '' == $username )
177 if ( '' == $password ) {
178 $error = __('<strong>Error</strong>: The password field is empty.');
182 $login = get_userdatabylogin($username);
183 //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
186 $error = __('<strong>Error</strong>: Wrong username.');
189 // If the password is already_md5, it has been double hashed.
190 // Otherwise, it is plain text.
191 if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
194 $error = __('<strong>Error</strong>: Incorrect password.');
202 if ( !function_exists('is_user_logged_in') ) :
203 function is_user_logged_in() {
204 global $current_user;
206 if ( $current_user->id == 0 )
212 if ( !function_exists('auth_redirect') ) :
213 function auth_redirect() {
214 // Checks if a user is logged in, if not redirects them to the login page
215 if ( (!empty($_COOKIE[USER_COOKIE]) &&
216 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
217 (empty($_COOKIE[USER_COOKIE])) ) {
220 header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
226 if ( !function_exists('check_admin_referer') ) :
227 function check_admin_referer() {
228 $adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
229 $referer = strtolower($_SERVER['HTTP_REFERER']);
230 if (!strstr($referer, $adminurl))
231 die(__('Sorry, you need to <a href="http://codex.wordpress.org/Enable_Sending_Referrers">enable sending referrers</a> for this feature to work.'));
232 do_action('check_admin_referer');
236 // Cookie safe redirect. Works around IIS Set-Cookie bug.
237 // http://support.microsoft.com/kb/q176113/
238 if ( !function_exists('wp_redirect') ) :
239 function wp_redirect($location) {
242 $location = str_replace( array("\n", "\r"), '', $location);
245 header("Refresh: 0;url=$location");
247 header("Location: $location");
251 if ( !function_exists('wp_setcookie') ) :
252 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
254 $password = md5( md5($password) ); // Double hash the password in the cookie.
257 $cookiepath = COOKIEPATH;
259 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
261 if ( empty($siteurl) ) {
262 $sitecookiepath = SITECOOKIEPATH;
263 $cookiehash = COOKIEHASH;
265 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
266 $cookiehash = md5($siteurl);
270 $expire = time() + 31536000;
274 setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
275 setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
277 if ( $cookiepath != $sitecookiepath ) {
278 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
279 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
284 if ( !function_exists('wp_clearcookie') ) :
285 function wp_clearcookie() {
286 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
287 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
288 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
289 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
293 if ( ! function_exists('wp_notify_postauthor') ) :
294 function wp_notify_postauthor($comment_id, $comment_type='') {
297 $comment = get_comment($comment_id);
298 $post = get_post($comment->comment_post_ID);
299 $user = get_userdata( $post->post_author );
301 if ('' == $user->user_email) return false; // If there's no email to send the comment to
303 $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
305 $blogname = get_settings('blogname');
307 if ( empty( $comment_type ) ) $comment_type = 'comment';
309 if ('comment' == $comment_type) {
310 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
311 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
312 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
313 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
314 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
315 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
316 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
317 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
318 } elseif ('trackback' == $comment_type) {
319 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
320 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
321 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
322 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
323 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
324 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
325 } elseif ('pingback' == $comment_type) {
326 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
327 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
328 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
329 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
330 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
331 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
333 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
334 $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";
336 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
338 if ( '' == $comment->comment_author ) {
339 $from = "From: \"$blogname\" <$wp_email>";
340 if ( '' != $comment->comment_author_email )
341 $reply_to = "Reply-To: $comment->comment_author_email";
343 $from = "From: \"$comment->comment_author\" <$wp_email>";
344 if ( '' != $comment->comment_author_email )
345 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
348 $message_headers = "MIME-Version: 1.0\n"
350 . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
352 if ( isset($reply_to) )
353 $message_headers .= $reply_to . "\n";
355 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
356 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
357 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
359 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
365 /* wp_notify_moderator
366 notifies the moderator of the blog (usually the admin)
367 about a new comment that waits for approval
370 if ( !function_exists('wp_notify_moderator') ) :
371 function wp_notify_moderator($comment_id) {
374 if( get_settings( "moderation_notify" ) == 0 )
377 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
378 $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
380 $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
381 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
383 $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";
384 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
385 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
386 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
387 $notify_message .= sprintf( __('URI : %s'), $comment->comment_author_url ) . "\r\n";
388 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
389 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
390 $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";
391 $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";
392 $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
393 $notify_message .= get_settings('siteurl') . "/wp-admin/moderation.php\r\n";
395 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_settings('blogname'), $post->post_title );
396 $admin_email = get_settings('admin_email');
398 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
399 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
401 @wp_mail($admin_email, $subject, $notify_message);
407 if ( !function_exists('wp_new_user_notification') ) :
408 function wp_new_user_notification($user_id, $plaintext_pass = '') {
409 $user = new WP_User($user_id);
411 $user_login = stripslashes($user->user_login);
412 $user_email = stripslashes($user->user_email);
414 $message = sprintf(__('New user registration on your blog %s:'), get_settings('blogname')) . "\r\n\r\n";
415 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
416 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
418 @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
420 if ( empty($plaintext_pass) )
423 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
424 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
425 $message .= get_settings('siteurl') . "/wp-login.php\r\n";
427 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);