]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pluggable-functions.php
Wordpress 2.0.11
[autoinstalls/wordpress.git] / wp-includes / pluggable-functions.php
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 = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%]|i', '', $location);
263
264         $strip = array('%0d', '%0a');
265         $location = str_replace($strip, '', $location);
266
267         if ( $is_IIS ) {
268                 header("Refresh: 0;url=$location");
269         } else {
270                 if ( php_sapi_name() != 'cgi-fcgi' )
271                         status_header($status); // This causes problems on IIS and some FastCGI setups
272                 header("Location: $location");
273         }
274 }
275 endif;
276
277 if ( !function_exists('wp_get_cookie_login') ):
278 function wp_get_cookie_login() {
279         if ( empty($_COOKIE[USER_COOKIE]) || empty($_COOKIE[PASS_COOKIE]) )
280                 return false;
281
282         return array('login' => $_COOKIE[USER_COOKIE],  'password' => $_COOKIE[PASS_COOKIE]);
283 }
284
285 endif;
286
287 if ( !function_exists('wp_setcookie') ) :
288 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
289         if ( !$already_md5 )
290                 $password = md5( md5($password) ); // Double hash the password in the cookie.
291
292         if ( empty($home) )
293                 $cookiepath = COOKIEPATH;
294         else
295                 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
296
297         if ( empty($siteurl) ) {
298                 $sitecookiepath = SITECOOKIEPATH;
299                 $cookiehash = COOKIEHASH;
300         } else {
301                 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
302                 $cookiehash = md5($siteurl);
303         }
304
305         if ( $remember )
306                 $expire = time() + 31536000;
307         else
308                 $expire = 0;
309
310         setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
311         setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
312
313         if ( $cookiepath != $sitecookiepath ) {
314                 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
315                 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
316         }
317 }
318 endif;
319
320 if ( !function_exists('wp_clearcookie') ) :
321 function wp_clearcookie() {
322         setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
323         setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
324         setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
325         setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
326 }
327 endif;
328
329 if ( ! function_exists('wp_notify_postauthor') ) :
330 function wp_notify_postauthor($comment_id, $comment_type='') {
331         global $wpdb;
332     
333         $comment = get_comment($comment_id);
334         $post    = get_post($comment->comment_post_ID);
335         $user    = get_userdata( $post->post_author );
336
337         if ('' == $user->user_email) return false; // If there's no email to send the comment to
338
339         $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
340
341         $blogname = get_settings('blogname');
342         
343         if ( empty( $comment_type ) ) $comment_type = 'comment';
344         
345         if ('comment' == $comment_type) {
346                 $notify_message  = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
347                 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
348                 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
349                 $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
350                 $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
351                 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
352                 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
353                 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
354         } elseif ('trackback' == $comment_type) {
355                 $notify_message  = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
356                 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
357                 $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
358                 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
359                 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
360                 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
361         } elseif ('pingback' == $comment_type) {
362                 $notify_message  = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
363                 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
364                 $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
365                 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
366                 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
367                 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
368         }
369         $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
370         $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
372         $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
373
374         if ( '' == $comment->comment_author ) {
375                 $from = "From: \"$blogname\" <$wp_email>";
376                 if ( '' != $comment->comment_author_email )
377                         $reply_to = "Reply-To: $comment->comment_author_email";
378         } else {
379                 $from = "From: \"$comment->comment_author\" <$wp_email>";
380                 if ( '' != $comment->comment_author_email )
381                         $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
382         }
383
384         $message_headers = "MIME-Version: 1.0\n"
385                 . "$from\n"
386                 . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
387
388         if ( isset($reply_to) )
389                 $message_headers .= $reply_to . "\n";
390
391         $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
392         $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
393         $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
394
395         @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
396    
397         return true;
398 }
399 endif;
400
401 /* wp_notify_moderator
402    notifies the moderator of the blog (usually the admin)
403    about a new comment that waits for approval
404    always returns true
405  */
406 if ( !function_exists('wp_notify_moderator') ) :
407 function wp_notify_moderator($comment_id) {
408         global $wpdb;
409
410         if( get_settings( "moderation_notify" ) == 0 )
411                 return true; 
412     
413         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
414         $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
415
416         $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
417         $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
418
419         $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";
420         $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
421         $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
422         $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
423         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
424         $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
425         $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
426         $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";
427         $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";
428         $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
429         $notify_message .= get_settings('siteurl') . "/wp-admin/moderation.php\r\n";
430
431         $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_settings('blogname'), $post->post_title );
432         $admin_email = get_settings('admin_email');
433
434         $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
435         $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
436
437         @wp_mail($admin_email, $subject, $notify_message);
438     
439         return true;
440 }
441 endif;
442
443 if ( !function_exists('wp_new_user_notification') ) :
444 function wp_new_user_notification($user_id, $plaintext_pass = '') {
445         $user = new WP_User($user_id);
446         
447         $user_login = stripslashes($user->user_login);
448         $user_email = stripslashes($user->user_email);
449         
450         $message  = sprintf(__('New user registration on your blog %s:'), get_settings('blogname')) . "\r\n\r\n";
451         $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
452         $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
453         
454         @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
455
456         if ( empty($plaintext_pass) )
457                 return;
458
459         $message  = sprintf(__('Username: %s'), $user_login) . "\r\n";
460         $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
461         $message .= get_settings('siteurl') . "/wp-login.php\r\n";
462                 
463         wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);
464         
465 }
466 endif;
467
468 if ( !function_exists('wp_verify_nonce') ) :
469 function wp_verify_nonce($nonce, $action = -1) {
470         $user = wp_get_current_user();
471         $uid = (int) $user->id;
472
473         $i = ceil(time() / 43200);
474
475         //Allow for expanding range, but only do one check if we can
476         if( substr(wp_hash($i . $action . $uid), -12, 10) == $nonce || substr(wp_hash(($i - 1) . $action . $uid), -12, 10) == $nonce )
477                 return true;
478         return false;
479 }
480 endif;
481
482 if ( !function_exists('wp_create_nonce') ) :
483 function wp_create_nonce($action = -1) {
484         $user = wp_get_current_user();
485         $uid = (int) $user->id;
486
487         $i = ceil(time() / 43200);
488         
489         return substr(wp_hash($i . $action . $uid), -12, 10);
490 }
491 endif;
492
493 if ( !function_exists('wp_salt') ) :
494 function wp_salt() {
495         $salt = get_option('secret');
496         if ( empty($salt) )
497                 $salt = DB_PASSWORD . DB_USER . DB_NAME . DB_HOST . ABSPATH;
498
499         return $salt;
500 }
501 endif;
502
503 if ( !function_exists('wp_hash') ) :
504 function wp_hash($data) {
505         $salt = wp_salt();
506
507         if ( function_exists('hash_hmac') ) {
508                 return hash_hmac('md5', $data, $salt);
509         } else {
510                 return md5($data . $salt);
511         }
512 }
513 endif;
514
515 ?>