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