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

Revision 3304, 12.3 kB (checked in by dougal, 3 years ago)

Fix user cookie verification.

  • 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
7 if ( !function_exists('get_currentuserinfo') ) :
8 function get_currentuserinfo() {
9     global $user_login, $userdata, $user_level, $user_ID, $user_nickname, $user_email, $user_url, $user_pass_md5, $user_identity;
10     // *** retrieving user's data from cookies and db - no spoofing
11
12     if ( wp_login($_COOKIE['wordpressuser_' . COOKIEHASH], $_COOKIE['wordpresspass_' . COOKIEHASH], true) ) {
13         $user_login = $_COOKIE['wordpressuser_' . COOKIEHASH];
14         $userdata = get_userdatabylogin($user_login);
15         $user_level = $userdata->user_level;
16         $user_ID = $userdata->ID;
17         $user_nickname = $userdata->user_nickname;
18         $user_email = $userdata->user_email;
19         $user_url = $userdata->user_url;
20         $user_pass_md5 = md5($userdata->user_pass);
21
22         $idmode = $userdata->user_idmode;
23         switch($userdata->user_idmode) {
24             case 'login':
25                 $user_identity = $userdata->user_login;
26                 break;
27             case 'firstname':
28                 $user_identity = $userdata->user_firstname;
29                 break;
30             case 'lastname':
31                 $user_identity = $userdata->user_lastname;
32                 break;
33             case 'namefl':
34                 $user_identity = $userdata->user_firstname.' '.$userdata->user_lastname;
35                 break;
36             case 'namelf':
37                 $user_identity = $userdata->user_lastname.' '.$userdata->user_firstname;
38                 break;
39             case 'nickname':
40             default:
41                 $user_identity = $userdata->user_nickname;
42                 break;
43         }
44     }
45 }
46 endif;
47
48 if ( !function_exists('get_userdata') ) :
49 function get_userdata($userid) {
50     global $wpdb, $cache_userdata;
51     $userid = (int) $userid;
52     if ( empty($cache_userdata[$userid]) && $userid != 0) {
53         $cache_userdata[$userid] = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = $userid");
54         $cache_userdata[$cache_userdata[$userid]->user_login] =& $cache_userdata[$userid];
55     }
56
57     return $cache_userdata[$userid];
58 }
59 endif;
60
61 if ( !function_exists('get_userdatabylogin') ) :
62 function get_userdatabylogin($user_login) {
63     global $cache_userdata, $wpdb;
64     if ( !empty($user_login) && empty($cache_userdata[$user_login]) ) {
65         $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'"); /* todo: get rid of this intermediate var */
66         $cache_userdata[$user->ID] = $user;
67         $cache_userdata[$user_login] =& $cache_userdata[$user->ID];
68     } else {
69         $user = $cache_userdata[$user_login];
70     }
71     return $user;
72 }
73 endif;
74
75 if ( !function_exists('wp_mail') ) :
76 function wp_mail($to, $subject, $message, $headers = '') {
77     if( $headers == '' ) {
78         $headers = "MIME-Version: 1.0\n" .
79             "From: " . get_settings('admin_email') . "\n" .
80             "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
81     }
82
83     return @mail($to, $subject, $message, $headers);
84 }
85 endif;
86
87 if ( !function_exists('wp_login') ) :
88 function wp_login($username, $password, $already_md5 = false) {
89     global $wpdb, $error;
90
91     if ( !$username )
92         return false;
93
94     if ( !$password ) {
95         $error = __('<strong>Error</strong>: The password field is empty.');
96         return false;
97     }
98
99     $login = $wpdb->get_row("SELECT ID, user_login, user_pass FROM $wpdb->users WHERE user_login = '$username'");
100
101     if (!$login) {
102         $error = __('<strong>Error</strong>: Wrong username.');
103         return false;
104     } else {
105         // If the password is already_md5, it has been double hashed.
106         // Otherwise, it is plain text.
107         if ( ($already_md5 && $login->user_login == $username && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) {
108             return true;
109         } else {
110             $error = __('<strong>Error</strong>: Incorrect password.');
111             $pwd = '';
112             return false;
113         }
114     }
115 }
116 endif;
117
118 if ( !function_exists('auth_redirect') ) :
119 function auth_redirect() {
120     // Checks if a user is logged in, if not redirects them to the login page
121     if ( (!empty($_COOKIE['wordpressuser_' . COOKIEHASH]) &&
122                 !wp_login($_COOKIE['wordpressuser_' . COOKIEHASH], $_COOKIE['wordpresspass_' . COOKIEHASH], true)) ||
123              (empty($_COOKIE['wordpressuser_' . COOKIEHASH])) ) {
124         header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
125         header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
126         header('Cache-Control: no-cache, must-revalidate, max-age=0');
127         header('Pragma: no-cache');
128     
129         header('Location: ' . get_settings('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
130         exit();
131     }
132 }
133 endif;
134
135 // Cookie safe redirect.  Works around IIS Set-Cookie bug.
136 // http://support.microsoft.com/kb/q176113/
137 if ( !function_exists('wp_redirect') ) :
138 function wp_redirect($location) {
139     global $is_IIS;
140
141     if ($is_IIS)
142         header("Refresh: 0;url=$location");
143     else
144         header("Location: $location");
145 }
146 endif;
147
148 if ( !function_exists('wp_setcookie') ) :
149 function wp_setcookie($username, $password, $already_md5 = false, $home = '', $siteurl = '') {
150     if ( !$already_md5 )
151         $password = md5( md5($password) ); // Double hash the password in the cookie.
152
153     if ( empty($home) )
154         $cookiepath = COOKIEPATH;
155     else
156         $cookiepath = preg_replace('|https?://[^/]+|i', '', $home . '/' );
157
158     if ( empty($siteurl) ) {
159         $sitecookiepath = SITECOOKIEPATH;
160         $cookiehash = COOKIEHASH;
161     } else {
162         $sitecookiepath = preg_replace('|https?://[^/]+|i', '', $siteurl . '/' );
163         $cookiehash = md5($siteurl);
164     }
165
166     setcookie('wordpressuser_'. $cookiehash, $username, time() + 31536000, $cookiepath);
167     setcookie('wordpresspass_'. $cookiehash, $password, time() + 31536000, $cookiepath);
168
169     if ( $cookiepath != $sitecookiepath ) {
170         setcookie('wordpressuser_'. $cookiehash, $username, time() + 31536000, $sitecookiepath);
171         setcookie('wordpresspass_'. $cookiehash, $password, time() + 31536000, $sitecookiepath);
172     }
173 }
174 endif;
175
176 if ( !function_exists('wp_clearcookie') ) :
177 function wp_clearcookie() {
178     setcookie('wordpressuser_' . COOKIEHASH, ' ', time() - 31536000, COOKIEPATH);
179     setcookie('wordpresspass_' . COOKIEHASH, ' ', time() - 31536000, COOKIEPATH);
180     setcookie('wordpressuser_' . COOKIEHASH, ' ', time() - 31536000, SITECOOKIEPATH);
181     setcookie('wordpresspass_' . COOKIEHASH, ' ', time() - 31536000, SITECOOKIEPATH);
182 }
183 endif;
184
185 if ( ! function_exists('wp_notify_postauthor') ) :
186 function wp_notify_postauthor($comment_id, $comment_type='') {
187     global $wpdb;
188     
189     $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
190     $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
191     $user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID='$post->post_author' LIMIT 1");
192
193     if ('' == $user->user_email) return false; // If there's no email to send the comment to
194
195     $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
196
197     $blogname = get_settings('blogname');
198     
199     if ( empty( $comment_type ) ) $comment_type = 'comment';
200     
201     if ('comment' == $comment_type) {
202         $notify_message  = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
203         $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
204         $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
205         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
206         $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
207         $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
208         $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
209         $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
210     } elseif ('trackback' == $comment_type) {
211         $notify_message  = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
212         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
213         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
214         $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
215         $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
216         $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
217     } elseif ('pingback' == $comment_type) {
218         $notify_message  = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
219         $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
220         $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
221         $notify_message .= __('Excerpt: ') . "\r\n" . sprintf( __('[...] %s [...]'), $comment->comment_content ) . "\r\n\r\n";
222         $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
223         $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
224     }
225     $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
226     $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";
227
228     if ('' == $comment->comment_author_email || '' == $comment->comment_author) {
229         $from = "From: \"$blogname\" <wordpress@" . $_SERVER['SERVER_NAME'] . '>';
230     } else {
231         $from = 'From: "' . $comment->comment_author . "\" <$comment->comment_author_email>";
232     }
233
234     $notify_message = apply_filters('comment_notification_text', $notify_message);
235     $subject = apply_filters('comment_notification_subject', $subject);
236     $message_headers = apply_filters('comment_notification_headers', $message_headers);
237
238     $message_headers = "MIME-Version: 1.0\n"
239         . "$from\n"
240         . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
241
242     @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
243   
244     return true;
245 }
246 endif;
247
248 /* wp_notify_moderator
249    notifies the moderator of the blog (usually the admin)
250    about a new comment that waits for approval
251    always returns true
252  */
253 if ( !function_exists('wp_notify_moderator') ) :
254 function wp_notify_moderator($comment_id) {
255     global $wpdb;
256
257     if( get_settings( "moderation_notify" ) == 0 )
258         return true;
259     
260     $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID='$comment_id' LIMIT 1");
261     $post = $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE ID='$comment->comment_post_ID' LIMIT 1");
262
263     $comment_author_domain = gethostbyaddr($comment->comment_author_IP);
264     $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
265
266     $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";
267     $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
268     $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
269     $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
270     $notify_message .= sprintf( __('URI    : %s'), $comment->comment_author_url ) . "\r\n";
271     $notify_message .= sprintf( __('Whois  : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
272     $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
273     $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";
274     $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";
275     $notify_message .= sprintf( __('Currently %s comments are waiting for approval. Please visit the moderation panel:'), $comments_waiting ) . "\r\n";
276     $notify_message .= get_settings('siteurl') . "/wp-admin/moderation.php\r\n";
277
278     $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_settings('blogname'), $post->post_title );
279     $admin_email = get_settings("admin_email");
280
281     $notify_message = apply_filters('comment_moderation_text', $notify_message);
282     $subject = apply_filters('comment_moderation_subject', $subject);
283
284     @wp_mail($admin_email, $subject, $notify_message);
285     
286     return true;
287 }
288 endif;
289
290 ?>
Note: See TracBrowser for help on using the browser.