root/tags/2.2/wp-includes/pluggable.php

Revision 5440, 19.2 kB (checked in by ryan, 1 year ago)

Escapage

  • 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_id, 'userlogins');
100     return $user;
101 }
102 endif;
103
104 if ( !function_exists('update_user_cache') ) :
105 function update_user_cache() {
106     return true;
107 }
108 endif;
109
110 if ( !function_exists('get_userdatabylogin') ) :
111 function get_userdatabylogin($user_login) {
112     global $wpdb;
113     $user_login = sanitize_user( $user_login );
114
115     if ( empty( $user_login ) )
116         return false;
117
118     $user_id = wp_cache_get($user_login, 'userlogins');
119     $userdata = wp_cache_get($user_id, 'users');
120
121     if ( $userdata )
122         return $userdata;
123
124     $user_login = $wpdb->escape($user_login);
125
126     if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
127         return false;
128
129     $wpdb->hide_errors();
130     $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user->ID'");
131     $wpdb->show_errors();
132
133     if ($metavalues) {
134         foreach ( $metavalues as $meta ) {
135             $value = maybe_unserialize($meta->meta_value);
136             $user->{$meta->meta_key} = $value;
137
138             // We need to set user_level from meta, not row
139             if ( $wpdb->prefix . 'user_level' == $meta->meta_key )
140                 $user->user_level = $meta->meta_value;
141         }
142     }
143
144     // For backwards compat.
145     if ( isset($user->first_name) )
146         $user->user_firstname = $user->first_name;
147     if ( isset($user->last_name) )
148         $user->user_lastname = $user->last_name;
149     if ( isset($user->description) )
150         $user->user_description = $user->description;
151
152     wp_cache_add($user->ID, $user, 'users');
153     wp_cache_add($user->user_login, $user->ID, 'userlogins');
154     return $user;
155
156 }
157 endif;
158
159 if ( !function_exists('wp_mail') ) :
160 function wp_mail($to, $subject, $message, $headers = '') {
161     global $phpmailer;
162
163     if ( !is_object( $phpmailer ) ) {
164         require_once(ABSPATH . WPINC . '/class-phpmailer.php');
165         require_once(ABSPATH . WPINC . '/class-smtp.php');
166         $phpmailer = new PHPMailer();
167     }
168
169     $mail = compact('to', 'subject', 'message', 'headers');
170     $mail = apply_filters('wp_mail', $mail);
171     extract($mail);
172
173     if ( $headers == '' ) {
174         $headers = "MIME-Version: 1.0\n" .
175             "From: " . apply_filters('wp_mail_from', "wordpress@" . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))) . "\n" .
176             "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
177     }
178
179     $phpmailer->ClearAddresses();
180     $phpmailer->ClearCCs();
181     $phpmailer->ClearBCCs();
182     $phpmailer->ClearReplyTos();
183     $phpmailer->ClearAllRecipients();
184     $phpmailer->ClearCustomHeaders();
185
186     $phpmailer->FromName = "WordPress";
187     $phpmailer->AddAddress("$to", "");
188     $phpmailer->Subject = $subject;
189     $phpmailer->Body    = $message;
190     $phpmailer->IsHTML(false);
191     $phpmailer->IsMail(); // set mailer to use php mail()
192
193     do_action_ref_array('phpmailer_init', array(&$phpmailer));
194
195     $mailheaders = (array) explode( "\n", $headers );
196     foreach ( $mailheaders as $line ) {
197         $header = explode( ":", $line );
198         switch ( trim( $header[0] ) ) {
199             case "From":
200                 $from = trim( str_replace( '"', '', $header[1] ) );
201                 if ( strpos( $from, '<' ) ) {
202                     $phpmailer->FromName = str_replace( '"', '', substr( $header[1], 0, strpos( $header[1], '<' ) - 1 ) );
203                     $from = trim( substr( $from, strpos( $from, '<' ) + 1 ) );
204                     $from = str_replace( '>', '', $from );
205                 } else {
206                     $phpmailer->FromName = $from;
207                 }
208                 $phpmailer->From = trim( $from );
209                 break;
210             default:
211                 if ( $line != '' && $header[0] != 'MIME-Version' && $header[0] != 'Content-Type' )
212                     $phpmailer->AddCustomHeader( $line );
213                 break;
214         }
215     }
216
217     $result = @$phpmailer->Send();
218
219     return $result;
220 }
221 endif;
222
223 if ( !function_exists('wp_login') ) :
224 function wp_login($username, $password, $already_md5 = false) {
225     global $wpdb, $error;
226
227     if ( '' == $username )
228         return false;
229
230     if ( '' == $password ) {
231         $error = __('<strong>ERROR</strong>: The password field is empty.');
232         return false;
233     }
234
235     $login = get_userdatabylogin($username);
236     //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
237
238     if (!$login) {
239         $error = __('<strong>ERROR</strong>: Invalid username.');
240         return false;
241     } else {
242         // If the password is already_md5, it has been double hashed.
243         // Otherwise, it is plain text.
244         if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
245             return true;
246         } else {
247             $error = __('<strong>ERROR</strong>: Incorrect password.');
248             $pwd = '';
249             return false;
250         }
251     }
252 }
253 endif;
254
255 if ( !function_exists('is_user_logged_in') ) :
256 function is_user_logged_in() {
257     $user = wp_get_current_user();
258
259     if ( $user->id == 0 )
260         return false;
261
262     return true;
263 }
264 endif;
265
266 if ( !function_exists('auth_redirect') ) :
267 function auth_redirect() {
268     // Checks if a user is logged in, if not redirects them to the login page
269     if ( (!empty($_COOKIE[USER_COOKIE]) &&
270                 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
271              (empty($_COOKIE[USER_COOKIE])) ) {
272         nocache_headers();
273
274         wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
275         exit();
276     }
277 }
278 endif;
279
280 if ( !function_exists('check_admin_referer') ) :
281 function check_admin_referer($action = -1) {
282     $adminurl = strtolower(get_option('siteurl')).'/wp-admin';
283     $referer = strtolower(wp_get_referer());
284     if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
285         !(-1 == $action && strpos($referer, $adminurl) !== false)) {
286         wp_nonce_ays($action);
287         die();
288     }
289     do_action('check_admin_referer', $action);
290 }endif;
291
292 if ( !function_exists('check_ajax_referer') ) :
293 function check_ajax_referer() {
294     $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
295     foreach ( $cookie as $tasty ) {
296         if ( false !== strpos($tasty, USER_COOKIE) )
297             $user = substr(strstr($tasty, '='), 1);
298         if ( false !== strpos($tasty, PASS_COOKIE) )
299             $pass = substr(strstr($tasty, '='), 1);
300     }
301     if ( !wp_login( $user, $pass, true ) )
302         die('-1');
303     do_action('check_ajax_referer');
304 }
305 endif;
306
307 // Cookie safe redirect.  Works around IIS Set-Cookie bug.
308 // http://support.microsoft.com/kb/q176113/
309 if ( !function_exists('wp_redirect') ) :
310 function wp_redirect($location, $status = 302) {
311     global $is_IIS;
312
313     $location = apply_filters('wp_redirect', $location, $status);
314
315     if ( !$location ) // allows the wp_redirect filter to cancel a redirect
316         return false;
317
318     $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
319     $location = wp_kses_no_null($location);
320
321     $strip = array('%0d', '%0a');
322     $location = str_replace($strip, '', $location);
323
324     if ( $is_IIS ) {
325         header("Refresh: 0;url=$location");
326     } else {
327         if ( php_sapi_name() != 'cgi-fcgi' )
328             status_header($status); // This causes problems on IIS and some FastCGI setups
329         header("Location: $location");
330     }
331 }
332 endif;
333
334 if ( !function_exists('wp_get_cookie_login') ):
335 function wp_get_cookie_login() {
336     if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
337         return false;
338
339     return array('login' => $_COOKIE[USER_COOKIE],    'password' => $_COOKIE[PASS_COOKIE]);
340 }
341
342 endif;
343
344 if ( !function_exists('wp_setcookie') ) :
345 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
346     if ( !$already_md5 )
347         $password = md5( md5($password) ); // Double hash the password in the cookie.
348
349     if ( empty($home) )
350         $cookiepath = COOKIEPATH;
351     else
352         $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
353
354     if ( empty($siteurl) ) {
355         $sitecookiepath = SITECOOKIEPATH;
356         $cookiehash = COOKIEHASH;
357     } else {
358         $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
359         $cookiehash = md5($siteurl);
360     }
361
362     if ( $remember )
363         $expire = time() + 31536000;
364     else
365         $expire = 0;
366
367     setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
368     setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
369
370     if ( $cookiepath != $sitecookiepath ) {
371         setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
372         setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
373     }
374 }
375 endif;
376
377 if ( !function_exists('wp_clearcookie') ) :
378 function wp_clearcookie() {
379     setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
380     setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
381     setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
382     setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
383 }
384 endif;
385
386 if ( ! function_exists('wp_notify_postauthor') ) :
387 function wp_notify_postauthor($comment_id, $comment_type='') {
388     global $wpdb;
389
390     $comment = get_comment($comment_id);
391     $post    = get_post($comment->comment_post_ID);
392     $user    = get_userdata( $post->post_author );
393
394     if ('' == $user->user_email) return false; // If there's no email to send the comment to
395
396     $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
397
398     $blogname = get_option('blogname');
399
400     if ( empty( $comment_type ) ) $comment_type = 'comment';
401
402     if ('comment' == $comment_type) {
403         $notify_message  = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
404         $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
405         $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
406         $notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
407         $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
408         $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
409         $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
410         $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
411     } elseif ('trackback' == $comment_type) {
412         $notify_message  = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
413         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
414         $notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
415         $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
416         $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
417         $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
418     } elseif ('pingback' == $comment_type) {
419         $notify_message  = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
420         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
421         $notify_message .= sprintf( __('URL    : %s'), $comment->comment_author_url ) . "\r\n";
422         $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
423         $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
424         $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
425     }
426     $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
427     $notify_message .= sprintf( __('Delete it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&c=$comment_id" ) . "\r\n";
428     $notify_message .= sprintf( __('Spam it: %s'), get_option('siteurl')."/wp-admin/comment.php?action=cdc&dt=spam&c=$comment_id" ) . "\r\n";
429
430     $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
431
432     if ( '' == $comment->comment_author ) {
433         $from = "From: \"$blogname\" <$wp_email>";
434         if ( '' != $comment->comment_author_email )
435             $reply_to = "Reply-To: $comment->comment_author_email";
436     } else {
437         $from = "From: \"$comment->comment_author\" <$wp_email>";
438         if ( '' != $comment->comment_author_email )
439             $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
440     }
441
442     $message_headers = "MIME-Version: 1.0\n"
443         . "$from\n"
444         . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
445
446     if ( isset($reply_to