source: trunk/www.guidonia.net/wp/wp-includes/pluggable.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 57.8 KB
Line 
1<?php
2/**
3 * These functions can be replaced via plugins. If plugins do not redefine these
4 * functions, then these will be used instead.
5 *
6 * @package WordPress
7 */
8
9if ( !function_exists('set_current_user') ) :
10/**
11 * Changes the current user by ID or name.
12 *
13 * Set $id to null and specify a name if you do not know a user's ID.
14 *
15 * @since 2.0.1
16 * @see wp_set_current_user() An alias of wp_set_current_user()
17 *
18 * @param int|null $id User ID.
19 * @param string $name Optional. The user's username
20 * @return object returns wp_set_current_user()
21 */
22function set_current_user($id, $name = '') {
23 return wp_set_current_user($id, $name);
24}
25endif;
26
27if ( !function_exists('wp_set_current_user') ) :
28/**
29 * Changes the current user by ID or name.
30 *
31 * Set $id to null and specify a name if you do not know a user's ID.
32 *
33 * Some WordPress functionality is based on the current user and not based on
34 * the signed in user. Therefore, it opens the ability to edit and perform
35 * actions on users who aren't signed in.
36 *
37 * @since 2.0.3
38 * @global object $current_user The current user object which holds the user data.
39 * @uses do_action() Calls 'set_current_user' hook after setting the current user.
40 *
41 * @param int $id User ID
42 * @param string $name User's username
43 * @return WP_User Current user User object
44 */
45function wp_set_current_user($id, $name = '') {
46 global $current_user;
47
48 if ( isset($current_user) && ($id == $current_user->ID) )
49 return $current_user;
50
51 $current_user = new WP_User($id, $name);
52
53 setup_userdata($current_user->ID);
54
55 do_action('set_current_user');
56
57 return $current_user;
58}
59endif;
60
61if ( !function_exists('wp_get_current_user') ) :
62/**
63 * Retrieve the current user object.
64 *
65 * @since 2.0.3
66 *
67 * @return WP_User Current user WP_User object
68 */
69function wp_get_current_user() {
70 global $current_user;
71
72 get_currentuserinfo();
73
74 return $current_user;
75}
76endif;
77
78if ( !function_exists('get_currentuserinfo') ) :
79/**
80 * Populate global variables with information about the currently logged in user.
81 *
82 * Will set the current user, if the current user is not set. The current user
83 * will be set to the logged in person. If no user is logged in, then it will
84 * set the current user to 0, which is invalid and won't have any permissions.
85 *
86 * @since 0.71
87 * @uses $current_user Checks if the current user is set
88 * @uses wp_validate_auth_cookie() Retrieves current logged in user.
89 *
90 * @return bool|null False on XMLRPC Request and invalid auth cookie. Null when current user set
91 */
92function get_currentuserinfo() {
93 global $current_user;
94
95 if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST )
96 return false;
97
98 if ( ! empty($current_user) )
99 return;
100
101 if ( ! $user = wp_validate_auth_cookie() ) {
102 if ( empty($_COOKIE[LOGGED_IN_COOKIE]) || !$user = wp_validate_auth_cookie($_COOKIE[LOGGED_IN_COOKIE], 'logged_in') ) {
103 wp_set_current_user(0);
104 return false;
105 }
106 }
107
108 wp_set_current_user($user);
109}
110endif;
111
112if ( !function_exists('get_userdata') ) :
113/**
114 * Retrieve user info by user ID.
115 *
116 * @since 0.71
117 *
118 * @param int $user_id User ID
119 * @return bool|object False on failure, User DB row object
120 */
121function get_userdata( $user_id ) {
122 global $wpdb;
123
124 $user_id = absint($user_id);
125 if ( $user_id == 0 )
126 return false;
127
128 $user = wp_cache_get($user_id, 'users');
129
130 if ( $user )
131 return $user;
132
133 if ( !$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d LIMIT 1", $user_id)) )
134 return false;
135
136 _fill_user($user);
137
138 return $user;
139}
140endif;
141
142if ( !function_exists('get_user_by') ) :
143/**
144 * Retrieve user info by a given field
145 *
146 * @since 2.8.0
147 *
148 * @param string $field The field to retrieve the user with. id | slug | email | login
149 * @param int|string $value A value for $field. A user ID, slug, email address, or login name.
150 * @return bool|object False on failure, User DB row object
151 */
152function get_user_by($field, $value) {
153 global $wpdb;
154
155 switch ($field) {
156 case 'id':
157 return get_userdata($value);
158 break;
159 case 'slug':
160 $user_id = wp_cache_get($value, 'userslugs');
161 $field = 'user_nicename';
162 break;
163 case 'email':
164 $user_id = wp_cache_get($value, 'useremail');
165 $field = 'user_email';
166 break;
167 case 'login':
168 $value = sanitize_user( $value );
169 $user_id = wp_cache_get($value, 'userlogins');
170 $field = 'user_login';
171 break;
172 default:
173 return false;
174 }
175
176 if ( false !== $user_id )
177 return get_userdata($user_id);
178
179 if ( !$user = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE $field = %s", $value) ) )
180 return false;
181
182 _fill_user($user);
183
184 return $user;
185}
186endif;
187
188if ( !function_exists('get_userdatabylogin') ) :
189/**
190 * Retrieve user info by login name.
191 *
192 * @since 0.71
193 *
194 * @param string $user_login User's username
195 * @return bool|object False on failure, User DB row object
196 */
197function get_userdatabylogin($user_login) {
198 return get_user_by('login', $user_login);
199}
200endif;
201
202if ( !function_exists('get_user_by_email') ) :
203/**
204 * Retrieve user info by email.
205 *
206 * @since 2.5
207 *
208 * @param string $email User's email address
209 * @return bool|object False on failure, User DB row object
210 */
211function get_user_by_email($email) {
212 return get_user_by('email', $email);
213}
214endif;
215
216if ( !function_exists( 'wp_mail' ) ) :
217/**
218 * Send mail, similar to PHP's mail
219 *
220 * A true return value does not automatically mean that the user received the
221 * email successfully. It just only means that the method used was able to
222 * process the request without any errors.
223 *
224 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
225 * creating a from address like 'Name <email@address.com>' when both are set. If
226 * just 'wp_mail_from' is set, then just the email address will be used with no
227 * name.
228 *
229 * The default content type is 'text/plain' which does not allow using HTML.
230 * However, you can set the content type of the email by using the
231 * 'wp_mail_content_type' filter.
232 *
233 * The default charset is based on the charset used on the blog. The charset can
234 * be set using the 'wp_mail_charset' filter.
235 *
236 * @since 1.2.1
237 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
238 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
239 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
240 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
241 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
242 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
243 * phpmailer object.
244 * @uses PHPMailer
245 * @
246 *
247 * @param string $to Email address to send message
248 * @param string $subject Email subject
249 * @param string $message Message contents
250 * @param string|array $headers Optional. Additional headers.
251 * @param string|array $attachments Optional. Files to attach.
252 * @return bool Whether the email contents were sent successfully.
253 */
254function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
255 // Compact the input, apply the filters, and extract them back out
256 extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );
257
258 if ( !is_array($attachments) )
259 $attachments = explode( "\n", $attachments );
260
261 global $phpmailer;
262
263 // (Re)create it, if it's gone missing
264 if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
265 require_once ABSPATH . WPINC . '/class-phpmailer.php';
266 require_once ABSPATH . WPINC . '/class-smtp.php';
267 $phpmailer = new PHPMailer();
268 }
269
270 // Headers
271 if ( empty( $headers ) ) {
272 $headers = array();
273 } else {
274 if ( !is_array( $headers ) ) {
275 // Explode the headers out, so this function can take both
276 // string headers and an array of headers.
277 $tempheaders = (array) explode( "\n", $headers );
278 } else {
279 $tempheaders = $headers;
280 }
281 $headers = array();
282
283 // If it's actually got contents
284 if ( !empty( $tempheaders ) ) {
285 // Iterate through the raw headers
286 foreach ( (array) $tempheaders as $header ) {
287 if ( strpos($header, ':') === false ) {
288 if ( false !== stripos( $header, 'boundary=' ) ) {
289 $parts = preg_split('/boundary=/i', trim( $header ) );
290 $boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
291 }
292 continue;
293 }
294 // Explode them out
295 list( $name, $content ) = explode( ':', trim( $header ), 2 );
296
297 // Cleanup crew
298 $name = trim( $name );
299 $content = trim( $content );
300
301 // Mainly for legacy -- process a From: header if it's there
302 if ( 'from' == strtolower($name) ) {
303 if ( strpos($content, '<' ) !== false ) {
304 // So... making my life hard again?
305 $from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
306 $from_name = str_replace( '"', '', $from_name );
307 $from_name = trim( $from_name );
308
309 $from_email = substr( $content, strpos( $content, '<' ) + 1 );
310 $from_email = str_replace( '>', '', $from_email );
311 $from_email = trim( $from_email );
312 } else {
313 $from_email = trim( $content );
314 }
315 } elseif ( 'content-type' == strtolower($name) ) {
316 if ( strpos( $content,';' ) !== false ) {
317 list( $type, $charset ) = explode( ';', $content );
318 $content_type = trim( $type );
319 if ( false !== stripos( $charset, 'charset=' ) ) {
320 $charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
321 } elseif ( false !== stripos( $charset, 'boundary=' ) ) {
322 $boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
323 $charset = '';
324 }
325 } else {
326 $content_type = trim( $content );
327 }
328 } elseif ( 'cc' == strtolower($name) ) {
329 $cc = explode(",", $content);
330 } elseif ( 'bcc' == strtolower($name) ) {
331 $bcc = explode(",", $content);
332 } else {
333 // Add it to our grand headers array
334 $headers[trim( $name )] = trim( $content );
335 }
336 }
337 }
338 }
339
340 // Empty out the values that may be set
341 $phpmailer->ClearAddresses();
342 $phpmailer->ClearAllRecipients();
343 $phpmailer->ClearAttachments();
344 $phpmailer->ClearBCCs();
345 $phpmailer->ClearCCs();
346 $phpmailer->ClearCustomHeaders();
347 $phpmailer->ClearReplyTos();
348
349 // From email and name
350 // If we don't have a name from the input headers
351 if ( !isset( $from_name ) ) {
352 $from_name = 'WordPress';
353 }
354
355 /* If we don't have an email from the input headers default to wordpress@$sitename
356 * Some hosts will block outgoing mail from this address if it doesn't exist but
357 * there's no easy alternative. Defaulting to admin_email might appear to be another
358 * option but some hosts may refuse to relay mail from an unknown domain. See
359 * http://trac.wordpress.org/ticket/5007.
360 */
361
362 if ( !isset( $from_email ) ) {
363 // Get the site domain and get rid of www.
364 $sitename = strtolower( $_SERVER['SERVER_NAME'] );
365 if ( substr( $sitename, 0, 4 ) == 'www.' ) {
366 $sitename = substr( $sitename, 4 );
367 }
368
369 $from_email = 'wordpress@' . $sitename;
370 }
371
372 // Plugin authors can override the potentially troublesome default
373 $phpmailer->From = apply_filters( 'wp_mail_from', $from_email );
374 $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name );
375
376 // Set destination address
377 $phpmailer->AddAddress( $to );
378
379 // Set mail's subject and body
380 $phpmailer->Subject = $subject;
381 $phpmailer->Body = $message;
382
383 // Add any CC and BCC recipients
384 if ( !empty($cc) ) {
385 foreach ( (array) $cc as $recipient ) {
386 $phpmailer->AddCc( trim($recipient) );
387 }
388 }
389 if ( !empty($bcc) ) {
390 foreach ( (array) $bcc as $recipient) {
391 $phpmailer->AddBcc( trim($recipient) );
392 }
393 }
394
395 // Set to use PHP's mail()
396 $phpmailer->IsMail();
397
398 // Set Content-Type and charset
399 // If we don't have a content-type from the input headers
400 if ( !isset( $content_type ) ) {
401 $content_type = 'text/plain';
402 }
403
404 $content_type = apply_filters( 'wp_mail_content_type', $content_type );
405
406 $phpmailer->ContentType = $content_type;
407
408 // Set whether it's plaintext or not, depending on $content_type
409 if ( $content_type == 'text/html' ) {
410 $phpmailer->IsHTML( true );
411 }
412
413 // If we don't have a charset from the input headers
414 if ( !isset( $charset ) ) {
415 $charset = get_bloginfo( 'charset' );
416 }
417
418 // Set the content-type and charset
419 $phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );
420
421 // Set custom headers
422 if ( !empty( $headers ) ) {
423 foreach( (array) $headers as $name => $content ) {
424 $phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
425 }
426 if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) ) {
427 $phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
428 }
429 }
430
431 if ( !empty( $attachments ) ) {
432 foreach ( $attachments as $attachment ) {
433 $phpmailer->AddAttachment($attachment);
434 }
435 }
436
437 do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );
438
439 // Send!
440 $result = @$phpmailer->Send();
441
442 return $result;
443}
444endif;
445
446if ( !function_exists('wp_authenticate') ) :
447/**
448 * Checks a user's login information and logs them in if it checks out.
449 *
450 * @since 2.5.0
451 *
452 * @param string $username User's username
453 * @param string $password User's password
454 * @return WP_Error|WP_User WP_User object if login successful, otherwise WP_Error object.
455 */
456function wp_authenticate($username, $password) {
457 $username = sanitize_user($username);
458 $password = trim($password);
459
460 $user = apply_filters('authenticate', null, $username, $password);
461
462 if ( $user == null ) {
463 // TODO what should the error message be? (Or would these even happen?)
464 // Only needed if all authentication handlers fail to return anything.
465 $user = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
466 }
467
468 $ignore_codes = array('empty_username', 'empty_password');
469
470 if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {
471 do_action('wp_login_failed', $username);
472 }
473
474 return $user;
475}
476endif;
477
478if ( !function_exists('wp_logout') ) :
479/**
480 * Log the current user out.
481 *
482 * @since 2.5.0
483 */
484function wp_logout() {
485 wp_clear_auth_cookie();
486 do_action('wp_logout');
487}
488endif;
489
490if ( !function_exists('wp_validate_auth_cookie') ) :
491/**
492 * Validates authentication cookie.
493 *
494 * The checks include making sure that the authentication cookie is set and
495 * pulling in the contents (if $cookie is not used).
496 *
497 * Makes sure the cookie is not expired. Verifies the hash in cookie is what is
498 * should be and compares the two.
499 *
500 * @since 2.5
501 *
502 * @param string $cookie Optional. If used, will validate contents instead of cookie's
503 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
504 * @return bool|int False if invalid cookie, User ID if valid.
505 */
506function wp_validate_auth_cookie($cookie = '', $scheme = '') {
507 if ( ! $cookie_elements = wp_parse_auth_cookie($cookie, $scheme) ) {
508 do_action('auth_cookie_malformed', $cookie, $scheme);
509 return false;
510 }
511
512 extract($cookie_elements, EXTR_OVERWRITE);
513
514 $expired = $expiration;
515
516 // Allow a grace period for POST and AJAX requests
517 if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] )
518 $expired += 3600;
519
520 // Quick check to see if an honest cookie has expired
521 if ( $expired < time() ) {
522 do_action('auth_cookie_expired', $cookie_elements);
523 return false;
524 }
525
526 $user = get_userdatabylogin($username);
527 if ( ! $user ) {
528 do_action('auth_cookie_bad_username', $cookie_elements);
529 return false;
530 }
531
532 $pass_frag = substr($user->user_pass, 8, 4);
533
534 $key = wp_hash($username . $pass_frag . '|' . $expiration, $scheme);
535 $hash = hash_hmac('md5', $username . '|' . $expiration, $key);
536
537 if ( $hmac != $hash ) {
538 do_action('auth_cookie_bad_hash', $cookie_elements);
539 return false;
540 }
541
542 do_action('auth_cookie_valid', $cookie_elements, $user);
543
544 return $user->ID;
545}
546endif;
547
548if ( !function_exists('wp_generate_auth_cookie') ) :
549/**
550 * Generate authentication cookie contents.
551 *
552 * @since 2.5
553 * @uses apply_filters() Calls 'auth_cookie' hook on $cookie contents, User ID
554 * and expiration of cookie.
555 *
556 * @param int $user_id User ID
557 * @param int $expiration Cookie expiration in seconds
558 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
559 * @return string Authentication cookie contents
560 */
561function wp_generate_auth_cookie($user_id, $expiration, $scheme = 'auth') {
562 $user = get_userdata($user_id);
563
564 $pass_frag = substr($user->user_pass, 8, 4);
565
566 $key = wp_hash($user->user_login . $pass_frag . '|' . $expiration, $scheme);
567 $hash = hash_hmac('md5', $user->user_login . '|' . $expiration, $key);
568
569 $cookie = $user->user_login . '|' . $expiration . '|' . $hash;
570
571 return apply_filters('auth_cookie', $cookie, $user_id, $expiration, $scheme);
572}
573endif;
574
575if ( !function_exists('wp_parse_auth_cookie') ) :
576/**
577 * Parse a cookie into its components
578 *
579 * @since 2.7
580 *
581 * @param string $cookie
582 * @param string $scheme Optional. The cookie scheme to use: auth, secure_auth, or logged_in
583 * @return array Authentication cookie components
584 */
585function wp_parse_auth_cookie($cookie = '', $scheme = '') {
586 if ( empty($cookie) ) {
587 switch ($scheme){
588 case 'auth':
589 $cookie_name = AUTH_COOKIE;
590 break;
591 case 'secure_auth':
592 $cookie_name = SECURE_AUTH_COOKIE;
593 break;
594 case "logged_in":
595 $cookie_name = LOGGED_IN_COOKIE;
596 break;
597 default:
598 if ( is_ssl() ) {
599 $cookie_name = SECURE_AUTH_COOKIE;
600 $scheme = 'secure_auth';
601 } else {
602 $cookie_name = AUTH_COOKIE;
603 $scheme = 'auth';
604 }
605 }
606
607 if ( empty($_COOKIE[$cookie_name]) )
608 return false;
609 $cookie = $_COOKIE[$cookie_name];
610 }
611
612 $cookie_elements = explode('|', $cookie);
613 if ( count($cookie_elements) != 3 )
614 return false;
615
616 list($username, $expiration, $hmac) = $cookie_elements;
617
618 return compact('username', 'expiration', 'hmac', 'scheme');
619}
620endif;
621
622if ( !function_exists('wp_set_auth_cookie') ) :
623/**
624 * Sets the authentication cookies based User ID.
625 *
626 * The $remember parameter increases the time that the cookie will be kept. The
627 * default the cookie is kept without remembering is two days. When $remember is
628 * set, the cookies will be kept for 14 days or two weeks.
629 *
630 * @since 2.5
631 *
632 * @param int $user_id User ID
633 * @param bool $remember Whether to remember the user or not
634 */
635function wp_set_auth_cookie($user_id, $remember = false, $secure = '') {
636 if ( $remember ) {
637 $expiration = $expire = time() + apply_filters('auth_cookie_expiration', 1209600, $user_id, $remember);
638 } else {
639 $expiration = time() + apply_filters('auth_cookie_expiration', 172800, $user_id, $remember);
640 $expire = 0;
641 }
642
643 if ( '' === $secure )
644 $secure = is_ssl() ? true : false;
645
646 if ( $secure ) {
647 $auth_cookie_name = SECURE_AUTH_COOKIE;
648 $scheme = 'secure_auth';
649 } else {
650 $auth_cookie_name = AUTH_COOKIE;
651 $scheme = 'auth';
652 }
653
654 $auth_cookie = wp_generate_auth_cookie($user_id, $expiration, $scheme);
655 $logged_in_cookie = wp_generate_auth_cookie($user_id, $expiration, 'logged_in');
656
657 do_action('set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme);
658 do_action('set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in');
659
660 // Set httponly if the php version is >= 5.2.0
661 if ( version_compare(phpversion(), '5.2.0', 'ge') ) {
662 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
663 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
664 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, false, true);
665 if ( COOKIEPATH != SITECOOKIEPATH )
666 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, false, true);
667 } else {
668 $cookie_domain = COOKIE_DOMAIN;
669 if ( !empty($cookie_domain) )
670 $cookie_domain .= '; HttpOnly';
671 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, $cookie_domain, $secure);
672 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, $cookie_domain, $secure);
673 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, $cookie_domain);
674 if ( COOKIEPATH != SITECOOKIEPATH )
675 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, SITECOOKIEPATH, $cookie_domain);
676 }
677}
678endif;
679
680if ( !function_exists('wp_clear_auth_cookie') ) :
681/**
682 * Removes all of the cookies associated with authentication.
683 *
684 * @since 2.5
685 */
686function wp_clear_auth_cookie() {
687 do_action('clear_auth_cookie');
688
689 setcookie(AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
690 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
691 setcookie(AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
692 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN);
693 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
694 setcookie(LOGGED_IN_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
695
696 // Old cookies
697 setcookie(AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
698 setcookie(AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
699 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
700 setcookie(SECURE_AUTH_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
701
702 // Even older cookies
703 setcookie(USER_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
704 setcookie(PASS_COOKIE, ' ', time() - 31536000, COOKIEPATH, COOKIE_DOMAIN);
705 setcookie(USER_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
706 setcookie(PASS_COOKIE, ' ', time() - 31536000, SITECOOKIEPATH, COOKIE_DOMAIN);
707}
708endif;
709
710if ( !function_exists('is_user_logged_in') ) :
711/**
712 * Checks if the current visitor is a logged in user.
713 *
714 * @since 2.0.0
715 *
716 * @return bool True if user is logged in, false if not logged in.
717 */
718function is_user_logged_in() {
719 $user = wp_get_current_user();
720
721 if ( $user->id == 0 )
722 return false;
723
724 return true;
725}
726endif;
727
728if ( !function_exists('auth_redirect') ) :
729/**
730 * Checks if a user is logged in, if not it redirects them to the login page.
731 *
732 * @since 1.5
733 */
734function auth_redirect() {
735 // Checks if a user is logged in, if not redirects them to the login page
736
737 if ( is_ssl() || force_ssl_admin() )
738 $secure = true;
739 else
740 $secure = false;
741
742 // If https is required and request is http, redirect
743 if ( $secure && !is_ssl() && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
744 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
745 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
746 exit();
747 } else {
748 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
749 exit();
750 }
751 }
752
753 if ( $user_id = wp_validate_auth_cookie() ) {
754 do_action('auth_redirect', $user_id);
755
756 // If the user wants ssl but the session is not ssl, redirect.
757 if ( !$secure && get_user_option('use_ssl', $user_id) && false !== strpos($_SERVER['REQUEST_URI'], 'wp-admin') ) {
758 if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
759 wp_redirect(preg_replace('|^http://|', 'https://', $_SERVER['REQUEST_URI']));
760 exit();
761 } else {
762 wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
763 exit();
764 }
765 }
766
767 return; // The cookie is good so we're done
768 }
769
770 // The cookie is no good so force login
771 nocache_headers();
772
773 if ( is_ssl() )
774 $proto = 'https://';
775 else
776 $proto = 'http://';
777
778 $redirect = ( strpos($_SERVER['REQUEST_URI'], '/options.php') && wp_get_referer() ) ? wp_get_referer() : $proto . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
779
780 $login_url = wp_login_url($redirect);
781
782 wp_redirect($login_url);
783 exit();
784}
785endif;
786
787if ( !function_exists('check_admin_referer') ) :
788/**
789 * Makes sure that a user was referred from another admin page.
790 *
791 * To avoid security exploits.
792 *
793 * @since 1.2.0
794 * @uses do_action() Calls 'check_admin_referer' on $action.
795 *
796 * @param string $action Action nonce
797 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
798 */
799function check_admin_referer($action = -1, $query_arg = '_wpnonce') {
800 $adminurl = strtolower(admin_url());
801 $referer = strtolower(wp_get_referer());
802 $result = isset($_REQUEST[$query_arg]) ? wp_verify_nonce($_REQUEST[$query_arg], $action) : false;
803 if ( !$result && !(-1 == $action && strpos($referer, $adminurl) !== false) ) {
804 wp_nonce_ays($action);
805 die();
806 }
807 do_action('check_admin_referer', $action, $result);
808 return $result;
809}endif;
810
811if ( !function_exists('check_ajax_referer') ) :
812/**
813 * Verifies the AJAX request to prevent processing requests external of the blog.
814 *
815 * @since 2.0.3
816 *
817 * @param string $action Action nonce
818 * @param string $query_arg where to look for nonce in $_REQUEST (since 2.5)
819 */
820function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
821 if ( $query_arg )
822 $nonce = $_REQUEST[$query_arg];
823 else
824 $nonce = $_REQUEST['_ajax_nonce'] ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];
825
826 $result = wp_verify_nonce( $nonce, $action );
827
828 if ( $die && false == $result )
829 die('-1');
830
831 do_action('check_ajax_referer', $action, $result);
832
833 return $result;
834}
835endif;
836
837if ( !function_exists('wp_redirect') ) :
838/**
839 * Redirects to another page, with a workaround for the IIS Set-Cookie bug.
840 *
841 * @link http://support.microsoft.com/kb/q176113/
842 * @since 1.5.1
843 * @uses apply_filters() Calls 'wp_redirect' hook on $location and $status.
844 *
845 * @param string $location The path to redirect to
846 * @param int $status Status code to use
847 * @return bool False if $location is not set
848 */
849function wp_redirect($location, $status = 302) {
850 global $is_IIS;
851
852 $location = apply_filters('wp_redirect', $location, $status);
853 $status = apply_filters('wp_redirect_status', $status, $location);
854
855 if ( !$location ) // allows the wp_redirect filter to cancel a redirect
856 return false;
857
858 $location = wp_sanitize_redirect($location);
859
860 if ( $is_IIS ) {
861 header("Refresh: 0;url=$location");
862 } else {
863 if ( php_sapi_name() != 'cgi-fcgi' )
864 status_header($status); // This causes problems on IIS and some FastCGI setups
865 header("Location: $location");
866 }
867}
868endif;
869
870if ( !function_exists('wp_sanitize_redirect') ) :
871/**
872 * Sanitizes a URL for use in a redirect.
873 *
874 * @since 2.3
875 *
876 * @return string redirect-sanitized URL
877 **/
878function wp_sanitize_redirect($location) {
879 $location = preg_replace('|[^a-z0-9-~+_.?#=&;,/:%!]|i', '', $location);
880 $location = wp_kses_no_null($location);
881
882 // remove %0d and %0a from location
883 $strip = array('%0d', '%0a', '%0D', '%0A');
884 $location = _deep_replace($strip, $location);
885 return $location;
886}
887endif;
888
889if ( !function_exists('wp_safe_redirect') ) :
890/**
891 * Performs a safe (local) redirect, using wp_redirect().
892 *
893 * Checks whether the $location is using an allowed host, if it has an absolute
894 * path. A plugin can therefore set or remove allowed host(s) to or from the
895 * list.
896 *
897 * If the host is not allowed, then the redirect is to wp-admin on the siteurl
898 * instead. This prevents malicious redirects which redirect to another host,
899 * but only used in a few places.
900 *
901 * @since 2.3
902 * @uses wp_validate_redirect() To validate the redirect is to an allowed host.
903 *
904 * @return void Does not return anything
905 **/
906function wp_safe_redirect($location, $status = 302) {
907
908 // Need to look at the URL the way it will end up in wp_redirect()
909 $location = wp_sanitize_redirect($location);
910
911 $location = wp_validate_redirect($location, admin_url());
912
913 wp_redirect($location, $status);
914}
915endif;
916
917if ( !function_exists('wp_validate_redirect') ) :
918/**
919 * Validates a URL for use in a redirect.
920 *
921 * Checks whether the $location is using an allowed host, if it has an absolute
922 * path. A plugin can therefore set or remove allowed host(s) to or from the
923 * list.
924 *
925 * If the host is not allowed, then the redirect is to $default supplied
926 *
927 * @since 2.8.1
928 * @uses apply_filters() Calls 'allowed_redirect_hosts' on an array containing
929 * WordPress host string and $location host string.
930 *
931 * @param string $location The redirect to validate
932 * @param string $default The value to return is $location is not allowed
933 * @return string redirect-sanitized URL
934 **/
935function wp_validate_redirect($location, $default = '') {
936 // browsers will assume 'http' is your protocol, and will obey a redirect to a URL starting with '//'
937 if ( substr($location, 0, 2) == '//' )
938 $location = 'http:' . $location;
939
940 // In php 5 parse_url may fail if the URL query part contains http://, bug #38143
941 $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
942
943 $lp = parse_url($test);
944 $wpp = parse_url(get_option('home'));
945
946 $allowed_hosts = (array) apply_filters('allowed_redirect_hosts', array($wpp['host']), isset($lp['host']) ? $lp['host'] : '');
947
948 if ( isset($lp['host']) && ( !in_array($lp['host'], $allowed_hosts) && $lp['host'] != strtolower($wpp['host'])) )
949 $location = $default;
950
951 return $location;
952}
953endif;
954
955if ( ! function_exists('wp_notify_postauthor') ) :
956/**
957 * Notify an author of a comment/trackback/pingback to one of their posts.
958 *
959 * @since 1.0.0
960 *
961 * @param int $comment_id Comment ID
962 * @param string $comment_type Optional. The comment type either 'comment' (default), 'trackback', or 'pingback'
963 * @return bool False if user email does not exist. True on completion.
964 */
965function wp_notify_postauthor($comment_id, $comment_type='') {
966 $comment = get_comment($comment_id);
967 $post = get_post($comment->comment_post_ID);
968 $user = get_userdata( $post->post_author );
969 $current_user = wp_get_current_user();
970
971 if ( $comment->user_id == $post->post_author ) return false; // The author moderated a comment on his own post
972
973 if ('' == $user->user_email) return false; // If there's no email to send the comment to
974
975 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
976
977 $blogname = get_option('blogname');
978
979 if ( empty( $comment_type ) ) $comment_type = 'comment';
980
981 if ('comment' == $comment_type) {
982 /* translators: 1: post id, 2: post title */
983 $notify_message = sprintf( __('New comment on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
984 /* translators: 1: comment author, 2: author IP, 3: author domain */
985 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
986 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
987 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
988 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
989 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
990 $notify_message .= __('You can see all comments on this post here: ') . "\r\n";
991 /* translators: 1: blog name, 2: post title */
992 $subject = sprintf( __('[%1$s] Comment: "%2$s"'), $blogname, $post->post_title );
993 } elseif ('trackback' == $comment_type) {
994 /* translators: 1: post id, 2: post title */
995 $notify_message = sprintf( __('New trackback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
996 /* translators: 1: website name, 2: author IP, 3: author domain */
997 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
998 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
999 $notify_message .= __('Excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1000 $notify_message .= __('You can see all trackbacks on this post here: ') . "\r\n";
1001 /* translators: 1: blog name, 2: post title */
1002 $subject = sprintf( __('[%1$s] Trackback: "%2$s"'), $blogname, $post->post_title );
1003 } elseif ('pingback' == $comment_type) {
1004 /* translators: 1: post id, 2: post title */
1005 $notify_message = sprintf( __('New pingback on your post #%1$s "%2$s"'), $comment->comment_post_ID, $post->post_title ) . "\r\n";
1006 /* translators: 1: comment author, 2: author IP, 3: author domain */
1007 $notify_message .= sprintf( __('Website: %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1008 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1009 $notify_message .= __('Excerpt: ') . "\r\n" . sprintf('[...] %s [...]', $comment->comment_content ) . "\r\n\r\n";
1010 $notify_message .= __('You can see all pingbacks on this post here: ') . "\r\n";
1011 /* translators: 1: blog name, 2: post title */
1012 $subject = sprintf( __('[%1$s] Pingback: "%2$s"'), $blogname, $post->post_title );
1013 }
1014 $notify_message .= get_permalink($comment->comment_post_ID) . "#comments\r\n\r\n";
1015 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=cdc&c=$comment_id") ) . "\r\n";
1016 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=cdc&dt=spam&c=$comment_id") ) . "\r\n";
1017
1018 $wp_email = 'wordpress@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
1019
1020 if ( '' == $comment->comment_author ) {
1021 $from = "From: \"$blogname\" <$wp_email>";
1022 if ( '' != $comment->comment_author_email )
1023 $reply_to = "Reply-To: $comment->comment_author_email";
1024 } else {
1025 $from = "From: \"$comment->comment_author\" <$wp_email>";
1026 if ( '' != $comment->comment_author_email )
1027 $reply_to = "Reply-To: \"$comment->comment_author_email\" <$comment->comment_author_email>";
1028 }
1029
1030 $message_headers = "$from\n"
1031 . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
1032
1033 if ( isset($reply_to) )
1034 $message_headers .= $reply_to . "\n";
1035
1036 $notify_message = apply_filters('comment_notification_text', $notify_message, $comment_id);
1037 $subject = apply_filters('comment_notification_subject', $subject, $comment_id);
1038 $message_headers = apply_filters('comment_notification_headers', $message_headers, $comment_id);
1039
1040 @wp_mail($user->user_email, $subject, $notify_message, $message_headers);
1041
1042 return true;
1043}
1044endif;
1045
1046if ( !function_exists('wp_notify_moderator') ) :
1047/**
1048 * Notifies the moderator of the blog about a new comment that is awaiting approval.
1049 *
1050 * @since 1.0
1051 * @uses $wpdb
1052 *
1053 * @param int $comment_id Comment ID
1054 * @return bool Always returns true
1055 */
1056function wp_notify_moderator($comment_id) {
1057 global $wpdb;
1058
1059 if( get_option( "moderation_notify" ) == 0 )
1060 return true;
1061
1062 $comment = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_ID=%d LIMIT 1", $comment_id));
1063 $post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID=%d LIMIT 1", $comment->comment_post_ID));
1064
1065 $comment_author_domain = @gethostbyaddr($comment->comment_author_IP);
1066 $comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
1067
1068 switch ($comment->comment_type)
1069 {
1070 case 'trackback':
1071 $notify_message = sprintf( __('A new trackback on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
1072 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1073 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1074 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1075 $notify_message .= __('Trackback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1076 break;
1077 case 'pingback':
1078 $notify_message = sprintf( __('A new pingback on the post #%1$s "%2$s" is waiting for your approval'), $post->ID, $post->post_title ) . "\r\n";
1079 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1080 $notify_message .= sprintf( __('Website : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1081 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1082 $notify_message .= __('Pingback excerpt: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1083 break;
1084 default: //Comments
1085 $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";
1086 $notify_message .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
1087 $notify_message .= sprintf( __('Author : %1$s (IP: %2$s , %3$s)'), $comment->comment_author, $comment->comment_author_IP, $comment_author_domain ) . "\r\n";
1088 $notify_message .= sprintf( __('E-mail : %s'), $comment->comment_author_email ) . "\r\n";
1089 $notify_message .= sprintf( __('URL : %s'), $comment->comment_author_url ) . "\r\n";
1090 $notify_message .= sprintf( __('Whois : http://ws.arin.net/cgi-bin/whois.pl?queryinput=%s'), $comment->comment_author_IP ) . "\r\n";
1091 $notify_message .= __('Comment: ') . "\r\n" . $comment->comment_content . "\r\n\r\n";
1092 break;
1093 }
1094
1095 $notify_message .= sprintf( __('Approve it: %s'), admin_url("comment.php?action=mac&c=$comment_id") ) . "\r\n";
1096 $notify_message .= sprintf( __('Delete it: %s'), admin_url("comment.php?action=cdc&c=$comment_id") ) . "\r\n";
1097 $notify_message .= sprintf( __('Spam it: %s'), admin_url("comment.php?action=cdc&dt=spam&c=$comment_id") ) . "\r\n";
1098
1099 $notify_message .= sprintf( _n('Currently %s comment is waiting for approval. Please visit the moderation panel:',
1100 'Currently %s comments are waiting for approval. Please visit the moderation panel:', $comments_waiting), number_format_i18n($comments_waiting) ) . "\r\n";
1101 $notify_message .= admin_url("edit-comments.php?comment_status=moderated") . "\r\n";
1102
1103 $subject = sprintf( __('[%1$s] Please moderate: "%2$s"'), get_option('blogname'), $post->post_title );
1104 $admin_email = get_option('admin_email');
1105 $message_headers = '';
1106
1107 $notify_message = apply_filters('comment_moderation_text', $notify_message, $comment_id);
1108 $subject = apply_filters('comment_moderation_subject', $subject, $comment_id);
1109 $message_headers = apply_filters('comment_moderation_headers', $message_headers);
1110
1111 @wp_mail($admin_email, $subject, $notify_message, $message_headers);
1112
1113 return true;
1114}
1115endif;
1116
1117if ( !function_exists('wp_password_change_notification') ) :
1118/**
1119 * Notify the blog admin of a user changing password, normally via email.
1120 *
1121 * @since 2.7
1122 *
1123 * @param object $user User Object
1124 */
1125function wp_password_change_notification(&$user) {
1126 // send a copy of password change notification to the admin
1127 // but check to see if it's the admin whose password we're changing, and skip this
1128 if ( $user->user_email != get_option('admin_email') ) {
1129 $message = sprintf(__('Password Lost and Changed for user: %s'), $user->user_login) . "\r\n";
1130 wp_mail(get_option('admin_email'), sprintf(__('[%s] Password Lost/Changed'), get_option('blogname')), $message);
1131 }
1132}
1133endif;
1134
1135if ( !function_exists('wp_new_user_notification') ) :
1136/**
1137 * Notify the blog admin of a new user, normally via email.
1138 *
1139 * @since 2.0
1140 *
1141 * @param int $user_id User ID
1142 * @param string $plaintext_pass Optional. The user's plaintext password
1143 */
1144function wp_new_user_notification($user_id, $plaintext_pass = '') {
1145 $user = new WP_User($user_id);
1146
1147 $user_login = stripslashes($user->user_login);
1148 $user_email = stripslashes($user->user_email);
1149
1150 $message = sprintf(__('New user registration on your blog %s:'), get_option('blogname')) . "\r\n\r\n";
1151 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";
1152 $message .= sprintf(__('E-mail: %s'), $user_email) . "\r\n";
1153
1154 @wp_mail(get_option('admin_email'), sprintf(__('[%s] New User Registration'), get_option('blogname')), $message);
1155
1156 if ( empty($plaintext_pass) )
1157 return;
1158
1159 $message = sprintf(__('Username: %s'), $user_login) . "\r\n";
1160 $message .= sprintf(__('Password: %s'), $plaintext_pass) . "\r\n";
1161 $message .= wp_login_url() . "\r\n";
1162
1163 wp_mail($user_email, sprintf(__('[%s] Your username and password'), get_option('blogname')), $message);
1164
1165}
1166endif;
1167
1168if ( !function_exists('wp_nonce_tick') ) :
1169/**
1170 * Get the time-dependent variable for nonce creation.
1171 *
1172 * A nonce has a lifespan of two ticks. Nonces in their second tick may be
1173 * updated, e.g. by autosave.
1174 *
1175 * @since 2.5
1176 *
1177 * @return int
1178 */
1179function wp_nonce_tick() {
1180 $nonce_life = apply_filters('nonce_life', 86400);
1181
1182 return ceil(time() / ( $nonce_life / 2 ));
1183}
1184endif;
1185
1186if ( !function_exists('wp_verify_nonce') ) :
1187/**
1188 * Verify that correct nonce was used with time limit.
1189 *
1190 * The user is given an amount of time to use the token, so therefore, since the
1191 * UID and $action remain the same, the independent variable is the time.
1192 *
1193 * @since 2.0.3
1194 *
1195 * @param string $nonce Nonce that was used in the form to verify
1196 * @param string|int $action Should give context to what is taking place and be the same when nonce was created.
1197 * @return bool Whether the nonce check passed or failed.
1198 */
1199function wp_verify_nonce($nonce, $action = -1) {
1200 $user = wp_get_current_user();
1201 $uid = (int) $user->id;
1202
1203 $i = wp_nonce_tick();
1204
1205 // Nonce generated 0-12 hours ago
1206 if ( substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10) == $nonce )
1207 return 1;
1208 // Nonce generated 12-24 hours ago
1209 if ( substr(wp_hash(($i - 1) . $action . $uid, 'nonce'), -12, 10) == $nonce )
1210 return 2;
1211 // Invalid nonce
1212 return false;
1213}
1214endif;
1215
1216if ( !function_exists('wp_create_nonce') ) :
1217/**
1218 * Creates a random, one time use token.
1219 *
1220 * @since 2.0.3
1221 *
1222 * @param string|int $action Scalar value to add context to the nonce.
1223 * @return string The one use form token
1224 */
1225function wp_create_nonce($action = -1) {
1226 $user = wp_get_current_user();
1227 $uid = (int) $user->id;
1228
1229 $i = wp_nonce_tick();
1230
1231 return substr(wp_hash($i . $action . $uid, 'nonce'), -12, 10);
1232}
1233endif;
1234
1235if ( !function_exists('wp_salt') ) :
1236/**
1237 * Get salt to add to hashes to help prevent attacks.
1238 *
1239 * The secret key is located in two places: the database in case the secret key
1240 * isn't defined in the second place, which is in the wp-config.php file. If you
1241 * are going to set the secret key, then you must do so in the wp-config.php
1242 * file.
1243 *
1244 * The secret key in the database is randomly generated and will be appended to
1245 * the secret key that is in wp-config.php file in some instances. It is
1246 * important to have the secret key defined or changed in wp-config.php.
1247 *
1248 * If you have installed WordPress 2.5 or later, then you will have the
1249 * SECRET_KEY defined in the wp-config.php already. You will want to change the
1250 * value in it because hackers will know what it is. If you have upgraded to
1251 * WordPress 2.5 or later version from a version before WordPress 2.5, then you
1252 * should add the constant to your wp-config.php file.
1253 *
1254 * Below is an example of how the SECRET_KEY constant is defined with a value.
1255 * You must not copy the below example and paste into your wp-config.php. If you
1256 * need an example, then you can have a
1257 * {@link https://api.wordpress.org/secret-key/1.1/ secret key created} for you.
1258 *
1259 * <code>
1260 * define('SECRET_KEY', 'mAry1HadA15|\/|b17w55w1t3asSn09w');
1261 * </code>
1262 *
1263 * Salting passwords helps against tools which has stored hashed values of
1264 * common dictionary strings. The added values makes it harder to crack if given
1265 * salt string is not weak.
1266 *
1267 * @since 2.5
1268 * @link https://api.wordpress.org/secret-key/1.1/ Create a Secret Key for wp-config.php
1269 *
1270 * @return string Salt value from either 'SECRET_KEY' or 'secret' option
1271 */
1272function wp_salt($scheme = 'auth') {
1273 global $wp_default_secret_key;
1274 $secret_key = '';
1275 if ( defined('SECRET_KEY') && ('' != SECRET_KEY) && ( $wp_default_secret_key != SECRET_KEY) )
1276 $secret_key = SECRET_KEY;
1277
1278 if ( 'auth' == $scheme ) {
1279 if ( defined('AUTH_KEY') && ('' != AUTH_KEY) && ( $wp_default_secret_key != AUTH_KEY) )
1280 $secret_key = AUTH_KEY;
1281
1282 if ( defined('AUTH_SALT') ) {
1283 $salt = AUTH_SALT;
1284 } elseif ( defined('SECRET_SALT') ) {
1285 $salt = SECRET_SALT;
1286 } else {
1287 $salt = get_option('auth_salt');
1288 if ( empty($salt) ) {
1289 $salt = wp_generate_password(64);
1290 update_option('auth_salt', $salt);
1291 }
1292 }
1293 } elseif ( 'secure_auth' == $scheme ) {
1294 if ( defined('SECURE_AUTH_KEY') && ('' != SECURE_AUTH_KEY) && ( $wp_default_secret_key != SECURE_AUTH_KEY) )
1295 $secret_key = SECURE_AUTH_KEY;
1296
1297 if ( defined('SECURE_AUTH_SALT') ) {
1298 $salt = SECURE_AUTH_SALT;
1299 } else {
1300 $salt = get_option('secure_auth_salt');
1301 if ( empty($salt) ) {
1302 $salt = wp_generate_password(64);
1303 update_option('secure_auth_salt', $salt);
1304 }
1305 }
1306 } elseif ( 'logged_in' == $scheme ) {
1307 if ( defined('LOGGED_IN_KEY') && ('' != LOGGED_IN_KEY) && ( $wp_default_secret_key != LOGGED_IN_KEY) )
1308 $secret_key = LOGGED_IN_KEY;
1309
1310 if ( defined('LOGGED_IN_SALT') ) {
1311 $salt = LOGGED_IN_SALT;
1312 } else {
1313 $salt = get_option('logged_in_salt');
1314 if ( empty($salt) ) {
1315 $salt = wp_generate_password(64);
1316 update_option('logged_in_salt', $salt);
1317 }
1318 }
1319 } elseif ( 'nonce' == $scheme ) {
1320 if ( defined('NONCE_KEY') && ('' != NONCE_KEY) && ( $wp_default_secret_key != NONCE_KEY) )
1321 $secret_key = NONCE_KEY;
1322
1323 if ( defined('NONCE_SALT') ) {
1324 $salt = NONCE_SALT;
1325 } else {
1326 $salt = get_option('nonce_salt');
1327 if ( empty($salt) ) {
1328 $salt = wp_generate_password(64);
1329 update_option('nonce_salt', $salt);
1330 }
1331 }
1332 } else {
1333 // ensure each auth scheme has its own unique salt
1334 $salt = hash_hmac('md5', $scheme, $secret_key);
1335 }
1336
1337 return apply_filters('salt', $secret_key . $salt, $scheme);
1338}
1339endif;
1340
1341if ( !function_exists('wp_hash') ) :
1342/**
1343 * Get hash of given string.
1344 *
1345 * @since 2.0.3
1346 * @uses wp_salt() Get WordPress salt
1347 *
1348 * @param string $data Plain text to hash
1349 * @return string Hash of $data
1350 */
1351function wp_hash($data, $scheme = 'auth') {
1352 $salt = wp_salt($scheme);
1353
1354 return hash_hmac('md5', $data, $salt);
1355}
1356endif;
1357
1358if ( !function_exists('wp_hash_password') ) :
1359/**
1360 * Create a hash (encrypt) of a plain text password.
1361 *
1362 * For integration with other applications, this function can be overwritten to
1363 * instead use the other package password checking algorithm.
1364 *
1365 * @since 2.5
1366 * @global object $wp_hasher PHPass object
1367 * @uses PasswordHash::HashPassword
1368 *
1369 * @param string $password Plain text user password to hash
1370 * @return string The hash string of the password
1371 */
1372function wp_hash_password($password) {
1373 global $wp_hasher;
1374
1375 if ( empty($wp_hasher) ) {
1376 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1377 // By default, use the portable hash from phpass
1378 $wp_hasher = new PasswordHash(8, TRUE);
1379 }
1380
1381 return $wp_hasher->HashPassword($password);
1382}
1383endif;
1384
1385if ( !function_exists('wp_check_password') ) :
1386/**
1387 * Checks the plaintext password against the encrypted Password.
1388 *
1389 * Maintains compatibility between old version and the new cookie authentication
1390 * protocol using PHPass library. The $hash parameter is the encrypted password
1391 * and the function compares the plain text password when encypted similarly
1392 * against the already encrypted password to see if they match.
1393 *
1394 * For integration with other applications, this function can be overwritten to
1395 * instead use the other package password checking algorithm.
1396 *
1397 * @since 2.5
1398 * @global object $wp_hasher PHPass object used for checking the password
1399 * against the $hash + $password
1400 * @uses PasswordHash::CheckPassword
1401 *
1402 * @param string $password Plaintext user's password
1403 * @param string $hash Hash of the user's password to check against.
1404 * @return bool False, if the $password does not match the hashed password
1405 */
1406function wp_check_password($password, $hash, $user_id = '') {
1407 global $wp_hasher;
1408
1409 // If the hash is still md5...
1410 if ( strlen($hash) <= 32 ) {
1411 $check = ( $hash == md5($password) );
1412 if ( $check && $user_id ) {
1413 // Rehash using new hash.
1414 wp_set_password($password, $user_id);
1415 $hash = wp_hash_password($password);
1416 }
1417
1418 return apply_filters('check_password', $check, $password, $hash, $user_id);
1419 }
1420
1421 // If the stored hash is longer than an MD5, presume the
1422 // new style phpass portable hash.
1423 if ( empty($wp_hasher) ) {
1424 require_once( ABSPATH . 'wp-includes/class-phpass.php');
1425 // By default, use the portable hash from phpass
1426 $wp_hasher = new PasswordHash(8, TRUE);
1427 }
1428
1429 $check = $wp_hasher->CheckPassword($password, $hash);
1430
1431 return apply_filters('check_password', $check, $password, $hash, $user_id);
1432}
1433endif;
1434
1435if ( !function_exists('wp_generate_password') ) :
1436/**
1437 * Generates a random password drawn from the defined set of characters.
1438 *
1439 * @since 2.5
1440 *
1441 * @param int $length The length of password to generate
1442 * @param bool $special_chars Whether to include standard special characters
1443 * @return string The random password
1444 **/
1445function wp_generate_password($length = 12, $special_chars = true) {
1446 $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
1447 if ( $special_chars )
1448 $chars .= '!@#$%^&*()';
1449
1450 $password = '';
1451 for ( $i = 0; $i < $length; $i++ )
1452 $password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
1453 return $password;
1454}
1455endif;
1456
1457if ( !function_exists('wp_rand') ) :
1458 /**
1459 * Generates a random number
1460 *
1461 * @since 2.6.2
1462 *
1463 * @param int $min Lower limit for the generated number (optional, default is 0)
1464 * @param int $max Upper limit for the generated number (optional, default is 4294967295)
1465 * @return int A random number between min and max
1466 */
1467function wp_rand( $min = 0, $max = 0 ) {
1468 global $rnd_value;
1469
1470 $seed = get_transient('random_seed');
1471
1472 // Reset $rnd_value after 14 uses
1473 // 32(md5) + 40(sha1) + 40(sha1) / 8 = 14 random numbers from $rnd_value
1474 if ( strlen($rnd_value) < 8 ) {
1475 $rnd_value = md5( uniqid(microtime() . mt_rand(), true ) . $seed );
1476 $rnd_value .= sha1($rnd_value);
1477 $rnd_value .= sha1($rnd_value . $seed);
1478 $seed = md5($seed . $rnd_value);
1479 set_transient('random_seed', $seed);
1480 }
1481
1482 // Take the first 8 digits for our value
1483 $value = substr($rnd_value, 0, 8);
1484
1485 // Strip the first eight, leaving the remainder for the next call to wp_rand().
1486 $rnd_value = substr($rnd_value, 8);
1487
1488 $value = abs(hexdec($value));
1489
1490 // Reduce the value to be within the min - max range
1491 // 4294967295 = 0xffffffff = max random number
1492 if ( $max != 0 )
1493 $value = $min + (($max - $min + 1) * ($value / (4294967295 + 1)));
1494
1495 return abs(intval($value));
1496}
1497endif;
1498
1499if ( !function_exists('wp_set_password') ) :
1500/**
1501 * Updates the user's password with a new encrypted one.
1502 *
1503 * For integration with other applications, this function can be overwritten to
1504 * instead use the other package password checking algorithm.
1505 *
1506 * @since 2.5
1507 * @uses $wpdb WordPress database object for queries
1508 * @uses wp_hash_password() Used to encrypt the user's password before passing to the database
1509 *
1510 * @param string $password The plaintext new user password
1511 * @param int $user_id User ID
1512 */
1513function wp_set_password( $password, $user_id ) {
1514 global $wpdb;
1515
1516 $hash = wp_hash_password($password);
1517 $wpdb->update($wpdb->users, array('user_pass' => $hash, 'user_activation_key' => ''), array('ID' => $user_id) );
1518
1519 wp_cache_delete($user_id, 'users');
1520}
1521endif;
1522
1523if ( !function_exists( 'get_avatar' ) ) :
1524/**
1525 * Retrieve the avatar for a user who provided a user ID or email address.
1526 *
1527 * @since 2.5
1528 * @param int|string|object $id_or_email A user ID, email address, or comment object
1529 * @param int $size Size of the avatar image
1530 * @param string $default URL to a default image to use if no avatar is available
1531 * @param string $alt Alternate text to use in image tag. Defaults to blank
1532 * @return string <img> tag for the user's avatar
1533*/
1534function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
1535 if ( ! get_option('show_avatars') )
1536 return false;
1537
1538 if ( false === $alt)
1539 $safe_alt = '';
1540 else
1541 $safe_alt = esc_attr( $alt );
1542
1543 if ( !is_numeric($size) )
1544 $size = '96';
1545
1546 $email = '';
1547 if ( is_numeric($id_or_email) ) {
1548 $id = (int) $id_or_email;
1549 $user = get_userdata($id);
1550 if ( $user )
1551 $email = $user->user_email;
1552 } elseif ( is_object($id_or_email) ) {
1553 if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
1554 return false; // No avatar for pingbacks or trackbacks
1555
1556 if ( !empty($id_or_email->user_id) ) {
1557 $id = (int) $id_or_email->user_id;
1558 $user = get_userdata($id);
1559 if ( $user)
1560 $email = $user->user_email;
1561 } elseif ( !empty($id_or_email->comment_author_email) ) {
1562 $email = $id_or_email->comment_author_email;
1563 }
1564 } else {
1565 $email = $id_or_email;
1566 }
1567
1568 if ( empty($default) ) {
1569 $avatar_default = get_option('avatar_default');
1570 if ( empty($avatar_default) )
1571 $default = 'mystery';
1572 else
1573 $default = $avatar_default;
1574 }
1575
1576 if ( is_ssl() )
1577 $host = 'https://secure.gravatar.com';
1578 else
1579 $host = 'http://www.gravatar.com';
1580
1581 if ( 'mystery' == $default )
1582 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
1583 elseif ( 'blank' == $default )
1584 $default = includes_url('images/blank.gif');
1585 elseif ( !empty($email) && 'gravatar_default' == $default )
1586 $default = '';
1587 elseif ( 'gravatar_default' == $default )
1588 $default = "$host/avatar/s={$size}";
1589 elseif ( empty($email) )
1590 $default = "$host/avatar/?d=$default&amp;s={$size}";
1591 elseif ( strpos($default, 'http://') === 0 )
1592 $default = add_query_arg( 's', $size, $default );
1593
1594 if ( !empty($email) ) {
1595 $out = "$host/avatar/";
1596 $out .= md5( strtolower( $email ) );
1597 $out .= '?s='.$size;
1598 $out .= '&amp;d=' . urlencode( $default );
1599
1600 $rating = get_option('avatar_rating');
1601 if ( !empty( $rating ) )
1602 $out .= "&amp;r={$rating}";
1603
1604 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
1605 } else {
1606 $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
1607 }
1608
1609 return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
1610}
1611endif;
1612
1613if ( !function_exists('wp_setcookie') ) :
1614/**
1615 * Sets a cookie for a user who just logged in.
1616 *
1617 * @since 1.5
1618 * @deprecated Use wp_set_auth_cookie()
1619 * @see wp_set_auth_cookie()
1620 *
1621 * @param string $username The user's username
1622 * @param string $password Optional. The user's password
1623 * @param bool $already_md5 Optional. Whether the password has already been through MD5
1624 * @param string $home Optional. Will be used instead of COOKIEPATH if set
1625 * @param string $siteurl Optional. Will be used instead of SITECOOKIEPATH if set
1626 * @param bool $remember Optional. Remember that the user is logged in
1627 */
1628function wp_setcookie($username, $password = '', $already_md5 = false, $home = '', $siteurl = '', $remember = false) {
1629 _deprecated_function( __FUNCTION__, '2.5', 'wp_set_auth_cookie()' );
1630 $user = get_userdatabylogin($username);
1631 wp_set_auth_cookie($user->ID, $remember);
1632}
1633endif;
1634
1635if ( !function_exists('wp_clearcookie') ) :
1636/**
1637 * Clears the authentication cookie, logging the user out.
1638 *
1639 * @since 1.5
1640 * @deprecated Use wp_clear_auth_cookie()
1641 * @see wp_clear_auth_cookie()
1642 */
1643function wp_clearcookie() {
1644 _deprecated_function( __FUNCTION__, '2.5', 'wp_clear_auth_cookie()' );
1645 wp_clear_auth_cookie();
1646}
1647endif;
1648
1649if ( !function_exists('wp_get_cookie_login') ):
1650/**
1651 * Gets the user cookie login.
1652 *
1653 * This function is deprecated and should no longer be extended as it won't be
1654 * used anywhere in WordPress. Also, plugins shouldn't use it either.
1655 *
1656 * @since 2.0.3
1657 * @deprecated No alternative
1658 *
1659 * @return bool Always returns false
1660 */
1661function wp_get_cookie_login() {
1662 _deprecated_function( __FUNCTION__, '2.5', '' );
1663 return false;
1664}
1665endif;
1666
1667if ( !function_exists('wp_login') ) :
1668/**
1669 * Checks a users login information and logs them in if it checks out.
1670 *
1671 * Use the global $error to get the reason why the login failed. If the username
1672 * is blank, no error will be set, so assume blank username on that case.
1673 *
1674 * Plugins extending this function should also provide the global $error and set
1675 * what the error is, so that those checking the global for why there was a
1676 * failure can utilize it later.
1677 *
1678 * @since 1.2.2
1679 * @deprecated Use wp_signon()
1680 * @global string $error Error when false is returned
1681 *
1682 * @param string $username User's username
1683 * @param string $password User's password
1684 * @param bool $deprecated Not used
1685 * @return bool False on login failure, true on successful check
1686 */
1687function wp_login($username, $password, $deprecated = '') {
1688 global $error;
1689
1690 $user = wp_authenticate($username, $password);
1691
1692 if ( ! is_wp_error($user) )
1693 return true;
1694
1695 $error = $user->get_error_message();
1696 return false;
1697}
1698endif;
1699
1700if ( !function_exists( 'wp_text_diff' ) ) :
1701/**
1702 * Displays a human readable HTML representation of the difference between two strings.
1703 *
1704 * The Diff is available for getting the changes between versions. The output is
1705 * HTML, so the primary use is for displaying the changes. If the two strings
1706 * are equivalent, then an empty string will be returned.
1707 *
1708 * The arguments supported and can be changed are listed below.
1709 *
1710 * 'title' : Default is an empty string. Titles the diff in a manner compatible
1711 * with the output.
1712 * 'title_left' : Default is an empty string. Change the HTML to the left of the
1713 * title.
1714 * 'title_right' : Default is an empty string. Change the HTML to the right of
1715 * the title.
1716 *
1717 * @since 2.6
1718 * @see wp_parse_args() Used to change defaults to user defined settings.
1719 * @uses Text_Diff
1720 * @uses WP_Text_Diff_Renderer_Table
1721 *
1722 * @param string $left_string "old" (left) version of string
1723 * @param string $right_string "new" (right) version of string
1724 * @param string|array $args Optional. Change 'title', 'title_left', and 'title_right' defaults.
1725 * @return string Empty string if strings are equivalent or HTML with differences.
1726 */
1727function wp_text_diff( $left_string, $right_string, $args = null ) {
1728 $defaults = array( 'title' => '', 'title_left' => '', 'title_right' => '' );
1729 $args = wp_parse_args( $args, $defaults );
1730
1731 if ( !class_exists( 'WP_Text_Diff_Renderer_Table' ) )
1732 require( ABSPATH . WPINC . '/wp-diff.php' );
1733
1734 $left_string = normalize_whitespace($left_string);
1735 $right_string = normalize_whitespace($right_string);
1736
1737 $left_lines = split("\n", $left_string);
1738 $right_lines = split("\n", $right_string);
1739
1740 $text_diff = new Text_Diff($left_lines, $right_lines);
1741 $renderer = new WP_Text_Diff_Renderer_Table();
1742 $diff = $renderer->render($text_diff);
1743
1744 if ( !$diff )
1745 return '';
1746
1747 $r = "<table class='diff'>\n";
1748 $r .= "<col class='ltype' /><col class='content' /><col class='ltype' /><col class='content' />";
1749
1750 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1751 $r .= "<thead>";
1752 if ( $args['title'] )
1753 $r .= "<tr class='diff-title'><th colspan='4'>$args[title]</th></tr>\n";
1754 if ( $args['title_left'] || $args['title_right'] ) {
1755 $r .= "<tr class='diff-sub-title'>\n";
1756 $r .= "\t<td></td><th>$args[title_left]</th>\n";
1757 $r .= "\t<td></td><th>$args[title_right]</th>\n";
1758 $r .= "</tr>\n";
1759 }
1760 if ( $args['title'] || $args['title_left'] || $args['title_right'] )
1761 $r .= "</thead>\n";
1762
1763 $r .= "<tbody>\n$diff\n</tbody>\n";
1764 $r .= "</table>";
1765
1766 return $r;
1767}
1768endif;
1769
1770?>
Note: See TracBrowser for help on using the repository browser.