root/branches/2.0/wp-includes/pluggable-functions.php

Revision 6751, 18.9 kB (checked in by ryan, 10 months ago)

wp_safe_redirect() for 2.0. Props markjaquith and snakefoot. fixes #4606 for 2.0

  • Property svn:eol-style set to native
Line 
1 <?php
2
3     /* These functions can be replaced via plugins.  They are loaded after
4      plugins are loaded. */
5
6 if ( !function_exists('set_current_user') ) :
7 function set_current_user($id, $name = '') {
8     return wp_set_current_user($id, $name);
9 }
10 endif;
11
12 if ( !function_exists('wp_set_current_user') ) :
13 function wp_set_current_user($id, $name = '') {
14     global $current_user;
15
16     if ( isset($current_user) && ($id == $current_user->ID) )
17         return $current_user;
18
19     $current_user = new WP_User($id, $name);
20
21     setup_userdata($current_user->ID);
22
23     do_action('set_current_user');
24
25     return $current_user;
26 }
27 endif;
28
29 if ( !function_exists('wp_get_current_user') ) :
30 function wp_get_current_user() {
31     global $current_user;
32
33     get_currentuserinfo();
34
35     return $current_user;
36 }
37 endif;
38
39 if ( !function_exists('get_currentuserinfo') ) :
40 function get_currentuserinfo() {
41     global $current_user;
42
43     if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
44         return false;
45
46     if ( ! empty($current_user) )
47         return;
48
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);
52         return false;
53     }
54
55     $user_login = $_COOKIE[USER_COOKIE];
56     wp_set_current_user(0, $user_login);
57 }
58 endif;
59
60 if ( !function_exists('get_userdata') ) :
61 function get_userdata( $user_id ) {
62     global $wpdb;
63     $user_id = (int) $user_id;
64     if ( $user_id == 0 )
65         return false;
66
67     $user = wp_cache_get($user_id, 'users');
68     
69     if ( $user )
70         return $user;
71
72     if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id' LIMIT 1") )
73         return false;
74
75     $wpdb->hide_errors();
76     $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
77     $wpdb->show_errors();
78
79     if ($metavalues) {
80         foreach ( $metavalues as $meta ) {
81             $value = maybe_unserialize($meta->meta_value);
82             $user->{$meta->meta_key} = $value;
83
84             // We need to set user_level from meta, not row
85             if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
86                 $user->user_level = $meta->meta_value;
87         } // end foreach
88     } //end if
89
90     // For backwards compat.
91     if ( isset($user->first_name) )
92         $user->user_firstname = $user->first_name;
93     if ( isset($user->last_name) )
94         $user->user_lastname = $user->last_name;
95     if ( isset($user->description) )
96         $user->user_description = $user->description;
97         
98     wp_cache_add($user_id, $user, 'users');
99     wp_cache_add($user->user_login, $user, 'userlogins');
100     
101     return $user;
102 }
103 endif;
104
105 if ( !function_exists('update_user_cache') ) :
106 function update_user_cache() {
107     return true;
108 }
109 endif;
110
111 if ( !function_exists('get_userdatabylogin') ) :
112 function get_userdatabylogin($user_login) {
113     global $wpdb;
114     $user_login = sanitize_user( $user_login );
115
116     if ( empty( $user_login ) )
117         return false;
118         
119     $userdata = wp_cache_get($user_login, 'userlogins');
120     if ( $userdata )
121         return $userdata;
122
123     $user_login = $wpdb->escape($user_login);
124
125     if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
126         return false;
127
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();
131
132     if ($metavalues) {
133         foreach ( $metavalues as $meta ) {
134             $value = maybe_unserialize($meta->meta_value);
135             $user->{$meta->meta_key} = $value;
136
137             // We need to set user_level from meta, not row
138             if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
139                 $user->user_level = $meta->meta_value;
140         }
141     }
142
143     // For backwards compat.
144     if ( isset($user->first_name) )
145         $user->user_firstname = $user->first_name;
146     if ( isset($user->last_name) )
147         $user->user_lastname = $user->last_name;
148     if ( isset($user->description) )
149         $user->user_description = $user->description;
150
151     wp_cache_add($user->ID, $user, 'users');
152     wp_cache_add($user->user_login, $user, 'userlogins');
153
154     return $user;
155
156 }
157 endif;
158
159 if ( !function_exists('wp_mail') ) :
160 function wp_mail($to, $subject, $message, $headers = '') {
161     if( $headers == '' ) {
162         $headers = "MIME-Version: 1.0\n" .
163             "From: wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])) . "\n" .
164             "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
165     }
166
167     return @mail($to, $subject, $message, $headers);
168 }
169 endif;
170
171 if ( !function_exists('wp_login') ) :
172 function wp_login($username, $password, $already_md5 = false) {
173     global $wpdb, $error;
174
175     if ( '' == $username )
176         return false;
177
178     if ( '' == $password ) {
179         $error = __('<strong>Error</strong>: The password field is empty.');
180         return false;
181     }
182
183     $login = get_userdatabylogin($username);
184     //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
185
186     if (!$login) {
187         $error = __('<strong>Error</strong>: Wrong username.');
188         return false;
189     } else {
190         // If the password is already_md5, it has been double hashed.
191         // Otherwise, it is plain text.
192         if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
193             return true;
194         } else {
195             $error = __('<strong>Error</strong>: Incorrect password.');
196             $pwd = '';
197             return false;
198         }
199     }
200 }
201 endif;
202
203 if ( !function_exists('is_user_logged_in') ) :
204 function is_user_logged_in() {
205     $user = wp_get_current_user();
206     
207     if ( $user->id == 0 )
208         return false;
209
210     return true;
211 }
212 endif;
213
214 if ( !function_exists('auth_redirect') ) :
215 function auth_redirect() {
216     // Checks if a user is logged in, if not redirects them to the login page
217     if ( (!empty($_COOKIE[USER_COOKIE]) &&
218                 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
219              (empty($_COOKIE[USER_COOKIE])) ) {
220         nocache_headers();
221     
222         wp_redirect(get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
223         exit();
224     }
225 }
226 endif;
227
228 if ( !function_exists('check_admin_referer') ) :
229 function check_admin_referer($action = -1) {
230     $adminurl = strtolower(get_settings('siteurl')).'/wp-admin';
231     $referer = strtolower(wp_get_referer());
232     if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
233         !(-1 == $action && strstr($referer, $adminurl)) ) {
234         wp_nonce_ays($action);
235         die();
236     }
237     do_action('check_admin_referer', $action);
238 }
239 endif;
240
241 if ( !function_exists('check_ajax_referer') ) :
242 function check_ajax_referer() {
243     $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
244     foreach ( $cookie as $tasty ) {
245         if ( false !== strpos($tasty, USER_COOKIE) )
246             $user = urldecode(substr(strstr($tasty, '='), 1)); // Nasty double encoding
247         if ( false !== strpos($tasty, PASS_COOKIE) )
248             $pass = urldecode(substr(strstr($tasty, '='), 1));
249     }
250     if ( wp_login( $user, $pass, true ) )
251         return true;
252     return false;
253 }
254 endif;
255
256 // Cookie safe redirect.  Works around IIS Set-Cookie bug.
257 // http://support.microsoft.com/kb/q176113/
258 if ( !function_exists('wp_redirect') ) :
259 function wp_redirect($location, $status = 302) {
260     global $is_IIS;
261
262     $location = apply_filters('wp_redirect', $location, $status);
263
264     if ( !$location ) // allows the wp_redirect filter to cancel a redirect
265         return false;
266
267     $location = wp_sanitize_redirect($location);
268
269     if ( $is_IIS ) {
270         header("Refresh: 0;url=$location");
271     } else {
272         if ( php_sapi_name() != 'cgi-fcgi' )
273             status_header($status); // This causes problems on IIS and some FastCGI setups
274         header("Location: $location");
275     }
276 }
277 endif;
278
279 if ( !function_exists('wp_sanitize_redirect') ) :
280 /**
281 * sanitizes a URL for use in a redirect
282 * @return string redirect-sanitized URL
283 **/
284 function wp_sanitize_redirect($location) {
285     $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
286     $location = wp_kses_no_null($location);
287
288     // remove %0d and %0a from location
289     $strip = array('%0d', '%0a');
290     $found = true;
291     while($found) {
292         $found = false;
293         foreach($strip as $val) {
294             while(strpos($location, $val) !== false) {
295                 $found = true;
296                 $location = str_replace($val, '', $location);
297             }
298         }
299     }
300     return $location;
301 }
302 endif;
303
304 if ( !function_exists('wp_safe_redirect') ) :
305 /**
306 * performs a safe (local) redirect, using wp_redirect()
307 * @return void
308 **/
309 function wp_safe_redirect($location, $status = 302) {
310
311     // Need to look at the URL the way it will end up in wp_redirect()
312     $location = wp_sanitize_redirect($location);
313
314     // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
315     if ( substr($location, 0, 2) == '//' )
316         $location = 'http:' . $location;
317
318     $lp  = parse_url($location);
319     $wpp = parse_url(get_option('home'));
320
321     $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']));
322
323     if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
324         $location = get_option('siteurl') . '/wp-admin/';
325     
326     wp_redirect($location, $status);
327 }
328 endif;
329
330 if ( !function_exists('wp_get_cookie_login') ):
331 function wp_get_cookie_login() {
332     if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
333         return false;
334
335     return array('login' => $_COOKIE[USER_COOKIE],    'password' => $_COOKIE[PASS_COOKIE]);
336 }
337
338 endif;
339
340 if ( !function_exists('wp_setcookie') ) :
341 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
342     if ( !$already_md5 )
343         $password = md5( md5($password) ); // Double hash the password in the cookie.
344
345     if ( empty($home) )
346         $cookiepath = COOKIEPATH;
347     else
348         $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
349
350     if ( empty($siteurl) ) {
351         $sitecookiepath = SITECOOKIEPATH;
352         $cookiehash = COOKIEHASH;
353     } else {
354         $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
355         $cookiehash = md5($siteurl);
356     }
357
358     if ( $remember )
359         $expire = time() + 31536000;
360     else
361         $expire = 0;
362
363     setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
364     setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
365
366     if ( $cookiepath != $sitecookiepath ) {
367         setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
368         setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
369     }
370 }
371 endif;
372
373 if ( !function_exists('wp_clearcookie') ) :
374 function wp_clearcookie() {
375     setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
376     setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
377     setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
378     setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
379 }
380 endif;
381
382 if ( ! function_exists('wp_notify_postauthor') ) :
383 function wp_notify_postauthor($comment_id, $comment_type='') {
384     global $wpdb;
385     
386     $comment = get_comment($comment_id);
387     $post    = get_post($comment->comment_post_ID);
388     $user    = get_userdata( $post->post_author );
389
390     if ('' == $user->user_email) return false; // If there's no email to send the comment to
391
392     $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
393
394     $blogname = get_settings('blogname');
395     
396     if ( empty( $comment_type ) ) $comment_type = 'comment';
397     
398     if ('comment' == $comment_type) {
399         $notify_message  = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
400         $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
401         $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
402         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
403         $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
404         $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
405         $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
406         $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
407     } elseif ('trackback' == $comment_type) {
408         $notify_message  = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
409         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
410         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
411         $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
412         $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
413         $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
414     } elseif ('pingback' == $comment_type) {
415         $notify_message  = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
416         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
417         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
418         $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
419         $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
420         $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
421     }
422     $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
423     $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";
424
425     $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
426
427     if ( '' == $comment->comment_author ) {
428         $from = "From: \"$blogname\" <$wp_email>";
429         if ( '' != $comment->comment_author_email )
430             $reply_to = "Reply-To: $comment->comment_author_email";
431      } else {
432         $from = "From: \"$comment->comment_author\" <$wp_email>";
433         if ( '' != $comment->comment_author_email )
434             $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
435      }
436
437     $message_headers = "MIME-Version: 1.0\n"
438         . "$from\n"
439         . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
440
441     if ( isset($reply_to) )
442         $message_headers .= $reply_to . "\n";
443
444     $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
445     $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
446     $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
447
448     @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
449   
450     return true;
451 }
452 endif;
453
454 /* wp_notify_moderator
455    notifies the moderator of the blog (usually the admin)
456    about a new comment that waits for approval
457    always returns true
458  */
459 if ( !function_exists('wp_notify_moderator') ) :
460 function wp_notify_moderator($comment_id) {
461     global $wpdb;
462
463     if( get_settings( "moderation_notify" ) == 0 )
464         return true;
465     
466     $comment