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

Revision 6266, 22.2 kB (checked in by westi, 7 months ago)

Set the Sender on emails as well as from. Fixes #5007 for 2.3.1 props mattyrob

  • 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_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_login = '$user_login'") )
127         return false;
128
129     $user = get_userdata($user_ID);
130     return $user;
131 }
132 endif;
133
134 if ( !function_exists( 'wp_mail' ) ) :
135 function wp_mail( $to, $subject, $message, $headers = '' ) {
136     // Compact the input, apply the filters, and extract them back out
137     extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers' ) ) );
138
139     global $phpmailer;
140
141     // (Re)create it, if it's gone missing
142     if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
143         require_once ABSPATH . WPINC . '/class-phpmailer.php';
144         require_once ABSPATH . WPINC . '/class-smtp.php';
145         $phpmailer = new PHPMailer();
146     }
147
148     // Headers
149     if ( empty( $headers ) ) {
150         $headers = array();
151     } elseif ( !is_array( $headers ) ) {
152         // Explode the headers out, so this function can take both
153         // string headers and an array of headers.
154         $tempheaders = (array) explode( "\n", $headers );
155         $headers = array();
156
157         // If it's actually got contents
158         if ( !empty( $tempheaders ) ) {
159             // Iterate through the raw headers
160             foreach ( $tempheaders as $header ) {
161                 if ( strpos($header, ':') === false )
162                     continue;
163                 // Explode them out
164                 list( $name, $content ) = explode( ':', trim( $header ), 2 );
165
166                 // Cleanup crew
167                 $name = trim( $name );
168                 $content = trim( $content );
169
170                 // Mainly for legacy -- process a From: header if it's there
171                 if ( 'from' == strtolower($name) ) {
172                     if ( strpos($content, '<' ) !== false ) {
173                         // So... making my life hard again?
174                         $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
175                         $from_name = str_replace( '"', '', $from_name );
176                         $from_name = trim( $from_name );
177
178                         $from_email = substr( $content, strpos( $content, '<' ) + 1 );
179                         $from_email = str_replace( '>', '', $from_email );
180                         $from_email = trim( $from_email );
181                     } else {
182                         $from_name = trim( $content );
183                     }
184                 } elseif ( 'content-type' == strtolower($name) ) {
185                     if ( strpos( $content,';' ) !== false ) {
186                         list( $type, $charset ) = explode( ';', $content );
187                         $content_type = trim( $type );
188                         $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
189                     } else {
190                         $content_type = trim( $content );
191                     }
192                 } else {
193                     // Add it to our grand headers array
194                     $headers[trim( $name )] = trim( $content );
195                 }
196             }
197         }
198     }
199
200     // Empty out the values that may be set
201     $phpmailer->ClearAddresses();
202     $phpmailer->ClearAllRecipients();
203     $phpmailer->ClearAttachments();
204     $phpmailer->ClearBCCs();
205     $phpmailer->ClearCCs();
206     $phpmailer->ClearCustomHeaders();
207     $phpmailer->ClearReplyTos();
208
209     // From email and name
210     // If we don't have a name from the input headers
211     if ( !isset( $from_name ) ) {
212         $from_name = 'WordPress';
213     }
214
215     // If we don't have an email from the input headers
216     if ( !isset( $from_email ) ) {
217         // Get the site domain and get rid of www.
218         $sitename = strtolower( $_SERVER['SERVER_NAME'] );
219         if ( substr( $sitename, 0, 4 ) == 'www.' ) {
220             $sitename = substr( $sitename, 4 );
221         }
222
223         $from_email = 'wordpress@' . $sitename;
224     }
225
226     // Set the from name and email
227     $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
228     $phpmailer->Sender = apply_filters( 'wp_mail_from', $from_email );
229     $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
230
231     // Set destination address
232     $phpmailer->AddAddress( $to );
233
234     // Set mail's subject and body
235     $phpmailer->Subject = $subject;
236     $phpmailer->Body = $message;
237
238     // Set to use PHP's mail()
239     $phpmailer->IsMail();
240
241     // Set Content-Type and charset
242     // If we don't have a content-type from the input headers
243     if ( !isset( $content_type ) ) {
244         $content_type = 'text/plain';
245     }
246
247     $content_type = apply_filters( 'wp_mail_content_type', $content_type );
248
249     // Set whether it's plaintext or not, depending on $content_type
250     if ( $content_type == 'text/html' ) {
251         $phpmailer->IsHTML( true );
252     } else {
253         $phpmailer->IsHTML( false );
254     }
255
256     // If we don't have a charset from the input headers
257     if ( !isset( $charset ) ) {
258         $charset = get_bloginfo( 'charset' );
259     }
260
261     // Set the content-type and charset
262     $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
263
264     // Set custom headers
265     if ( !empty( $headers ) ) {
266         foreach ( $headers as $name => $content ) {
267             $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
268         }
269     }
270
271     do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
272
273     // Send!
274     $result = @$phpmailer->Send();
275
276     return $result;
277 }
278 endif;
279
280 if ( !function_exists('wp_login') ) :
281 function wp_login($username, $password, $already_md5 = false) {
282     global $wpdb, $error;
283
284     $username = sanitize_user($username);
285
286     if ( '' == $username )
287         return false;
288
289     if ( '' == $password ) {
290         $error = __('<strong>ERROR</strong>: The password field is empty.');
291         return false;
292     }
293
294     $login = get_userdatabylogin($username);
295     //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
296
297     if (!$login) {
298         $error = __('<strong>ERROR</strong>: Invalid username.');
299         return false;
300     } else {
301         // If the password is already_md5, it has been double hashed.
302         // Otherwise, it is plain text.
303         if ( ($already_md5 && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
304             return true;
305         } else {
306             $error = __('<strong>ERROR</strong>: Incorrect password.');
307             $pwd = '';
308             return false;
309         }
310     }
311 }
312 endif;
313
314 if ( !function_exists('is_user_logged_in') ) :
315 function is_user_logged_in() {
316     $user = wp_get_current_user();
317
318     if ( $user->id == 0 )
319         return false;
320
321     return true;
322 }
323 endif;
324
325 if ( !function_exists('auth_redirect') ) :
326 function auth_redirect() {
327     // Checks if a user is logged in, if not redirects them to the login page
328     if ( (!empty($_COOKIE[USER_COOKIE]) &&
329                 !wp_login($_COOKIE[USER_COOKIE], $_COOKIE[PASS_COOKIE], true)) ||
330              (empty($_COOKIE[USER_COOKIE])) ) {
331         nocache_headers();
332
333         wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
334         exit();
335     }
336 }
337 endif;
338
339 if ( !function_exists('check_admin_referer') ) :
340 function check_admin_referer($action = -1) {
341     $adminurl = strtolower(get_option('siteurl')).'/wp-admin';
342     $referer = strtolower(wp_get_referer());
343     if ( !wp_verify_nonce($_REQUEST['_wpnonce'], $action) &&
344         !(-1 == $action && strpos($referer, $adminurl) !== false)) {
345         wp_nonce_ays($action);
346         die();
347     }
348     do_action('check_admin_referer', $action);
349 }endif;
350
351 if ( !function_exists('check_ajax_referer') ) :
352 function check_ajax_referer() {
353     $current_name = '';
354     if ( ( $current = wp_get_current_user() ) && $current->ID )
355         $current_name = $current->data->user_login;
356     if ( !$current_name )
357         die('-1');
358
359     $cookie = explode('; ', urldecode(empty($_POST['cookie']) ? $_GET['cookie'] : $_POST['cookie'])); // AJAX scripts must pass cookie=document.cookie
360     foreach ( $cookie as $tasty ) {
361         if ( false !== strpos($tasty, USER_COOKIE) )
362             $user = substr(strstr($tasty, '='), 1);
363         if ( false !== strpos($tasty, PASS_COOKIE) )
364             $pass = substr(strstr($tasty, '='), 1);
365     }
366
367     if ( $current_name != $user || !wp_login( $user, $pass, true ) )
368         die('-1');
369     do_action('check_ajax_referer');
370 }
371 endif;
372
373 // Cookie safe redirect.  Works around IIS Set-Cookie bug.
374 // http://support.microsoft.com/kb/q176113/
375 if ( !function_exists('wp_redirect') ) :
376 function wp_redirect($location, $status = 302) {
377     global $is_IIS;
378
379     $location = apply_filters('wp_redirect', $location, $status);
380
381     if ( !$location ) // allows the wp_redirect filter to cancel a redirect
382         return false;
383
384     $location = wp_sanitize_redirect($location);
385
386     if ( $is_IIS ) {
387         header("Refresh: 0;url=$location");
388     } else {
389         if ( php_sapi_name() != 'cgi-fcgi' )
390             status_header($status); // This causes problems on IIS and some FastCGI setups
391         header("Location: $location");
392     }
393 }
394 endif;
395
396 if ( !function_exists('wp_sanitize_redirect') ) :
397 /**
398  * sanitizes a URL for use in a redirect
399  * @return string redirect-sanitized URL
400  **/
401 function wp_sanitize_redirect($location) {
402     $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
403     $location = wp_kses_no_null($location);
404
405     // remove %0d and %0a from location
406     $strip = array('%0d', '%0a');
407     $found = true;
408     while($found) {
409         $found = false;
410         foreach($strip as $val) {
411             while(strpos($location, $val) !== false) {
412                 $found = true;
413                 $location = str_replace($val, '', $location);
414             }
415         }
416     }
417     return $location;
418 }
419 endif;
420
421 if ( !function_exists('wp_safe_redirect') ) :
422 /**
423  * performs a safe (local) redirect, using wp_redirect()
424  * @return void
425  **/
426 function wp_safe_redirect($location, $status = 302) {
427
428     // Need to look at the URL the way it will end up in wp_redirect()
429     $location = wp_sanitize_redirect($location);
430
431     // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
432     if ( substr($location, 0, 2) == '//' )
433         $location = 'http:' . $location;
434
435     $lp  = parse_url($location);
436     $wpp = parse_url(get_option('home'));
437
438     $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), $lp['host']);
439
440     if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
441         $location = get_option('siteurl') . '/wp-admin/';
442
443     wp_redirect($location, $status);
444 }
445 endif;
446
447 if ( !function_exists('wp_get_cookie_login') ):
448 function wp_get_cookie_login() {
449     if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
450         return false;
451
452     return array('login' => $_COOKIE[USER_COOKIE],    'password' => $_COOKIE[PASS_COOKIE]);
453 }
454
455 endif;
456
457 if ( !function_exists('wp_setcookie') ) :
458 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
459     if ( !$already_md5 )
460         $password = md5( md5($password) ); // Double hash the password in the cookie.
461
462     if ( empty($home) )
463         $cookiepath = COOKIEPATH;
464     else
465         $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
466
467     if ( empty($siteurl) ) {
468         $sitecookiepath = SITECOOKIEPATH;
469         $cookiehash = COOKIEHASH;
470     } else {
471         $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
472         $cookiehash = md5($siteurl);
473     }
474
475     if ( $remember )
476         $expire = time() + 31536000;
477     else
478         $expire = 0;
479
480     setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
481     setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
482
483     if ( $cookiepath != $sitecookiepath ) {
484         setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
485         setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
486     }
487 }
488 endif;
489
490 if ( !function_exists('wp_clearcookie') ) :
491 function wp_clearcookie() {
492     setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
493     setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
494     setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
495     setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
496 }
497 endif;
498
499 if ( ! function_exists('wp_notify_postauthor') ) :
500 function wp_notify_postauthor($comment_id, $comment_type='') {
501     global $wpdb;
502
503     $comment = get_comment($comment_id);
504     $post    = get_post($comment->comment_post_ID);
505