]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/pluggable-functions.php
Wordpress 2.0.2
[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         global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity, $current_user;
9
10         $current_user   = '';
11
12         $current_user   = new WP_User($id, $name);
13
14         $userdata       = get_userdatabylogin($user_login);
15
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;
23
24         do_action('set_current_user');
25
26         return $current_user;
27 }
28 endif;
29
30
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;
34
35         if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
36                 return false;
37
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);
41                 return false;
42         }
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;
51
52         if ( empty($current_user) )
53                 $current_user = new WP_User($user_ID);
54 }
55 endif;
56
57 if ( !function_exists('get_userdata') ) :
58 function get_userdata( $user_id ) {
59         global $wpdb;
60         $user_id = (int) $user_id;
61         if ( $user_id == 0 )
62                 return false;
63
64         $user = wp_cache_get($user_id, 'users');
65         
66         if ( $user )
67                 return $user;
68
69         if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id' LIMIT 1") )
70                 return false;
71
72         $wpdb->hide_errors();
73         $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id'");
74         $wpdb->show_errors();
75
76         if ($metavalues) {
77                 foreach ( $metavalues as $meta ) {
78                         @ $value = unserialize($meta->meta_value);
79                         if ($value === FALSE)
80                                 $value = $meta->meta_value;
81                         $user->{$meta->meta_key} = $value;
82
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;
86                 } // end foreach
87         } //end if
88
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;
96                 
97         wp_cache_add($user_id, $user, 'users');
98         wp_cache_add($user->user_login, $user, 'userlogins');
99         
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         $userdata = wp_cache_get($user_login, 'userlogins');
119         if ( $userdata )
120                 return $userdata;
121
122         if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") )
123                 return false;
124
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();
128
129         if ($metavalues) {
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;
135
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;
139                 }
140         }
141
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;
149
150         wp_cache_add($user->ID, $user, 'users');
151         wp_cache_add($user->user_login, $user, 'userlogins');
152
153         return $user;
154
155 }
156 endif;
157
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";
164         }
165
166         return @mail($to, $subject, $message, $headers);
167 }
168 endif;
169
170 if ( !function_exists('wp_login') ) :
171 function wp_login($username, $password, $already_md5 = false) {
172         global $wpdb, $error;
173
174         if ( '' == $username )
175                 return false;
176
177         if ( '' == $password ) {
178                 $error = __('<strong>Error</strong>: The password field is empty.');
179                 return false;
180         }
181
182         $login = get_userdatabylogin($username);
183         //$login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
184
185         if (!$login) {
186                 $error = __('<strong>Error</strong>: Wrong username.');
187                 return false;
188         } else {
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)) ) {
192                         return true;
193                 } else {
194                         $error = __('<strong>Error</strong>: Incorrect password.');
195                         $pwd = '';
196                         return false;
197                 }
198         }
199 }
200 endif;
201
202 if ( !function_exists('is_user_logged_in') ) :
203 function is_user_logged_in() {
204         global $current_user;
205         
206         if ( $current_user->id == 0 )
207                 return false;
208         return true;
209 }
210 endif;
211
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])) ) {
218                 nocache_headers();
219         
220                 header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
221                 exit();
222         }
223 }
224 endif;
225
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');
233 }
234 endif;
235
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) {
240         global $is_IIS;
241
242         $location = str_replace( array("\n", "\r"), '', $location);
243
244         if ($is_IIS)
245                 header("Refresh: 0;url=$location");
246         else
247                 header("Location: $location");
248 }
249 endif;
250
251 if ( !function_exists('wp_setcookie') ) :
252 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
253         if ( !$already_md5 )
254                 $password = md5( md5($password) ); // Double hash the password in the cookie.
255
256         if ( empty($home) )
257                 $cookiepath = COOKIEPATH;
258         else
259                 $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
260
261         if ( empty($siteurl) ) {
262                 $sitecookiepath = SITECOOKIEPATH;
263                 $cookiehash = COOKIEHASH;
264         } else {
265                 $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
266                 $cookiehash = md5($siteurl);
267         }
268
269         if ( $remember )
270                 $expire = time() + 31536000;
271         else
272                 $expire = 0;
273
274         setcookie(USER_COOKIE, $username, $expire, $cookiepath, COOKIE_DOMAIN);
275         setcookie(PASS_COOKIE, $password, $expire, $cookiepath, COOKIE_DOMAIN);
276
277         if ( $cookiepath != $sitecookiepath ) {
278                 setcookie(USER_COOKIE, $username, $expire, $sitecookiepath, COOKIE_DOMAIN);
279                 setcookie(PASS_COOKIE, $password, $expire, $sitecookiepath, COOKIE_DOMAIN);
280         }
281 }
282 endif;
283
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);
290 }
291 endif;
292
293 if ( ! function_exists('wp_notify_postauthor') ) :
294 function wp_notify_postauthor($comment_id, $comment_type='') {
295         global $wpdb;
296     
297         $comment = get_comment($comment_id);
298         $post    = get_post($comment->comment_post_ID);
299         $user    = get_userdata( $post->post_author );
300
301         if ('' == $user->user_email) return false; // If there's no email to send the comment to
302
303         $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
304
305         $blogname = get_settings('blogname');
306         
307         if ( empty( $comment_type ) ) $comment_type = 'comment';
308         
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 );
332         }
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";
335
336         $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
337
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";
342         } else {
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>";
346         }
347
348         $message_headers = "MIME-Version: 1.0\n"
349                 . "$from\n"
350                 . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
351
352         if ( isset($reply_to) )
353                 $message_headers .= $reply_to . "\n";
354
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);
358
359         @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
360    
361         return true;
362 }
363 endif;
364
365 /* wp_notify_moderator
366    notifies the moderator of the blog (usually the admin)
367    about a new comment that waits for approval
368    always returns true
369  */
370 if ( !function_exists('wp_notify_moderator') ) :
371 function wp_notify_moderator($comment_id) {
372         global $wpdb;
373
374         if( get_settings( "moderation_notify" ) == 0 )
375                 return true; 
376     
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");
379
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'");
382
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";
394
395         $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_settings('blogname'), $post->post_title );
396         $admin_email = get_settings('admin_email');
397
398         $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
399         $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
400
401         @wp_mail($admin_email, $subject, $notify_message);
402     
403         return true;
404 }
405 endif;
406
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);
410         
411         $user_login = stripslashes($user->user_login);
412         $user_email = stripslashes($user->user_email);
413         
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";
417         
418         @wp_mail(get_settings('admin_email'), sprintf(__('[%s] New User Registration'), get_settings('blogname')), $message);
419
420         if ( empty($plaintext_pass) )
421                 return;
422
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";
426                 
427         wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_settings('blogname')), $message);
428         
429 }
430 endif;
431
432 ?>