Wednesday, March 4, 2020

Wordpress 3.0 registration.php source code before deprecation



Before registration.php was deprecated in Wordpress 3.0.1, this is source code for  registration.php version 3.0.0 of Wordpress. Most of these functions were moved to user.php.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
/**
 * User Registration API
 *
 * @package WordPress
 */

/**
 * Checks whether the given username exists.
 *
 * @since 2.0.0 to 3.0.0
 *
 * @param string $username Username.
 * @return null|int The user's ID on success, and null on failure.
 */
function username_exists( $username ) {
 if ( $user = get_userdatabylogin( $username ) ) {
  return $user->ID;
 } else {
  return null;
 }
}

/**
 * Checks whether the given email exists.
 *
 * @since 2.1.0
 * @uses $wpdb
 *
 * @param string $email Email.
 * @return bool|int The user's ID on success, and false on failure.
 */
function email_exists( $email ) {
 if ( $user = get_user_by_email($email) )
  return $user->ID;

 return false;
}

/**
 * Checks whether an username is valid.
 *
 * @since 2.0.1
 * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
 *
 * @param string $username Username.
 * @return bool Whether username given is valid
 */
function validate_username( $username ) {
 $sanitized = sanitize_user( $username, true );
 $valid = ( $sanitized == $username );
 return apply_filters( 'validate_username', $valid, $username );
}

/**
 * Insert an user into the database.
 *
 * Can update a current user or insert a new user based on whether the user's ID
 * is present.
 *
 * Can be used to update the user's info (see below), set the user's role, and
 * set the user's preference on whether they want the rich editor on.
 *
 * Most of the $userdata array fields have filters associated with the values.
 * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
 * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
 * by the field name. An example using 'description' would have the filter
 * called, 'pre_user_description' that can be hooked into.
 *
 * The $userdata array can contain the following fields:
 * 'ID' - An integer that will be used for updating an existing user.
 * 'user_pass' - A string that contains the plain text password for the user.
 * 'user_login' - A string that contains the user's username for logging in.
 * 'user_nicename' - A string that contains a nicer looking name for the user.
 *  The default is the user's username.
 * 'user_url' - A string containing the user's URL for the user's web site.
 * 'user_email' - A string containing the user's email address.
 * 'display_name' - A string that will be shown on the site. Defaults to user's
 *  username. It is likely that you will want to change this, for both
 *  appearance and security through obscurity (that is if you don't use and
 *  delete the default 'admin' user).
 * 'nickname' - The user's nickname, defaults to the user's username.
 * 'first_name' - The user's first name.
 * 'last_name' - The user's last name.
 * 'description' - A string containing content about the user.
 * 'rich_editing' - A string for whether to enable the rich editor. False
 *  if not empty.
 * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
 * 'role' - A string used to set the user's role.
 * 'jabber' - User's Jabber account.
 * 'aim' - User's AOL IM account.
 * 'yim' - User's Yahoo IM account.
 *
 * @since 2.0.0
 * @uses $wpdb WordPress database layer.
 * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
 * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
 * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
 *
 * @param array $userdata An array of user data.
 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
 */
function wp_insert_user($userdata) {
 global $wpdb;

 extract($userdata, EXTR_SKIP);

 // Are we updating or creating?
 if ( !empty($ID) ) {
  $ID = (int) $ID;
  $update = true;
  $old_user_data = get_userdata($ID);
 } else {
  $update = false;
  // Hash the password
  $user_pass = wp_hash_password($user_pass);
 }

 $user_login = sanitize_user($user_login, true);
 $user_login = apply_filters('pre_user_login', $user_login);

 //Remove any non-printable chars from the login string to see if we have ended up with an empty username
 $user_login = trim($user_login);

 if ( empty($user_login) )
  return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );

 if ( !$update && username_exists( $user_login ) )
  return new WP_Error('existing_user_login', __('This username is already registered.') );

 if ( empty($user_nicename) )
  $user_nicename = sanitize_title( $user_login );
 $user_nicename = apply_filters('pre_user_nicename', $user_nicename);

 if ( empty($user_url) )
  $user_url = '';
 $user_url = apply_filters('pre_user_url', $user_url);

 if ( empty($user_email) )
  $user_email = '';
 $user_email = apply_filters('pre_user_email', $user_email);

 if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
  return new WP_Error('existing_user_email', __('This email address is already registered.') );

 if ( empty($display_name) )
  $display_name = $user_login;
 $display_name = apply_filters('pre_user_display_name', $display_name);

 if ( empty($nickname) )
  $nickname = $user_login;
 $nickname = apply_filters('pre_user_nickname', $nickname);

 if ( empty($first_name) )
  $first_name = '';
 $first_name = apply_filters('pre_user_first_name', $first_name);

 if ( empty($last_name) )
  $last_name = '';
 $last_name = apply_filters('pre_user_last_name', $last_name);

 if ( empty($description) )
  $description = '';
 $description = apply_filters('pre_user_description', $description);

 if ( empty($rich_editing) )
  $rich_editing = 'true';

 if ( empty($comment_shortcuts) )
  $comment_shortcuts = 'false';

 if ( empty($admin_color) )
  $admin_color = 'fresh';
 $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);

 if ( empty($use_ssl) )
  $use_ssl = 0;

 if ( empty($user_registered) )
  $user_registered = gmdate('Y-m-d H:i:s');

 $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));

 if ( $user_nicename_check ) {
  $suffix = 2;
  while ($user_nicename_check) {
   $alt_user_nicename = $user_nicename . "-$suffix";
   $user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $alt_user_nicename, $user_login));
   $suffix++;
  }
  $user_nicename = $alt_user_nicename;
 }

 $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
 $data = stripslashes_deep( $data );

 if ( $update ) {
  $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
  $user_id = (int) $ID;
 } else {
  $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
  $user_id = (int) $wpdb->insert_id;
 }

 update_user_meta( $user_id, 'first_name', $first_name);
 update_user_meta( $user_id, 'last_name', $last_name);
 update_user_meta( $user_id, 'nickname', $nickname );
 update_user_meta( $user_id, 'description', $description );
 update_user_meta( $user_id, 'rich_editing', $rich_editing);
 update_user_meta( $user_id, 'comment_shortcuts', $comment_shortcuts);
 update_user_meta( $user_id, 'admin_color', $admin_color);
 update_user_meta( $user_id, 'use_ssl', $use_ssl);

 foreach ( _wp_get_user_contactmethods() as $method => $name ) {
  if ( empty($$method) )
   $$method = '';

  update_user_meta( $user_id, $method, $$method );
 }

 if ( isset($role) ) {
  $user = new WP_User($user_id);
  $user->set_role($role);
 } elseif ( !$update ) {
  $user = new WP_User($user_id);
  $user->set_role(get_option('default_role'));
 }

 wp_cache_delete($user_id, 'users');
 wp_cache_delete($user_login, 'userlogins');

 if ( $update )
  do_action('profile_update', $user_id, $old_user_data);
 else
  do_action('user_register', $user_id);

 return $user_id;
}

/**
 * Update an user in the database.
 *
 * It is possible to update a user's password by specifying the 'user_pass'
 * value in the $userdata parameter array.
 *
 * If $userdata does not contain an 'ID' key, then a new user will be created
 * and the new user's ID will be returned.
 *
 * If current user's password is being updated, then the cookies will be
 * cleared.
 *
 * @since 2.0.0
 * @see wp_insert_user() For what fields can be set in $userdata
 * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
 *
 * @param array $userdata An array of user data.
 * @return int The updated user's ID.
 */
function wp_update_user($userdata) {
 $ID = (int) $userdata['ID'];

 // First, get all of the original fields
 $user = get_userdata($ID);

 // Escape data pulled from DB.
 $user = add_magic_quotes(get_object_vars($user));

 // If password is changing, hash it now.
 if ( ! empty($userdata['user_pass']) ) {
  $plaintext_pass = $userdata['user_pass'];
  $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
 }

 wp_cache_delete($user[ 'user_email' ], 'useremail');

 // Merge old and new fields with new fields overwriting old ones.
 $userdata = array_merge($user, $userdata);
 $user_id = wp_insert_user($userdata);

 // Update the cookies if the password changed.
 $current_user = wp_get_current_user();
 if ( $current_user->id == $ID ) {
  if ( isset($plaintext_pass) ) {
   wp_clear_auth_cookie();
   wp_set_auth_cookie($ID);
  }
 }

 return $user_id;
}

/**
 * A simpler way of inserting an user into the database.
 *
 * Creates a new user with just the username, password, and email. For a more
 * detail creation of a user, use wp_insert_user() to specify more infomation.
 *
 * @since 2.0.0
 * @see wp_insert_user() More complete way to create a new user
 *
 * @param string $username The user's username.
 * @param string $password The user's password.
 * @param string $email The user's email (optional).
 * @return int The new user's ID.
 */
function wp_create_user($username, $password, $email = '') {
 $user_login = esc_sql( $username );
 $user_email = esc_sql( $email    );
 $user_pass = $password;

 $userdata = compact('user_login', 'user_email', 'user_pass');
 return wp_insert_user($userdata);
}


/**
 * Set up the default contact methods
 *
 * @access private
 * @since
 *
 * @return array $user_contactmethods Array of contact methods and their labels.
 */
function _wp_get_user_contactmethods() {
 $user_contactmethods = array(
  'aim' => __('AIM'),
  'yim' => __('Yahoo IM'),
  'jabber' => __('Jabber / Google Talk')
 );
 return apply_filters('user_contactmethods',$user_contactmethods);
}

?>

Tuesday, March 3, 2020

Visual Studio (VS) Code Control Characters Explained SOH, STX, ETX, etc

1. Firstly to view control characters in Visual Studio Code 1.41.1, choose View→Render Control Characters→Render Whitespace (shows Tabs also), as in image below. 































2. Copy all the text in the below Cached Result window, starting with line 'List of Unicode Control Characters'.

This list begins with the first letter character in ASCII, which in Unicode format is expressed as \u0000 (hex value 00) and is referred to as null character. 

3. Paste this list into Visual Studio Code 1.41.1, to give you an example of how control characters will appear.

4. Match \u0001 to 01 in the Hex column in the tables below, to identify control character.
Visual Studio Code 1.41.1 renders control characters that will that will match Acronym  column the Standard ASCII Control Characters table below such as SOHSTXETXEOTBS, etc. 
For values greater than \u007F which are represented in the Unicode Control Characters table, renderd control characters do not match Acronym  column.




Tab Character Replacement in VS Code

Tab character below (line 10) in image has been replaced by 4 spaces, as per Visual Studio Code 1.41.1 settings. Choose File->Preference->Settings.


If you deselect this, as in image below, then Tab (\u0009) character is displayed as →   (right arrow).





Quick Tip \u0001 is equal to 01 hex or acronym (SOHin the Hex column the table below.


Standard ASCII Control Characters
Hex Acronym Name C Description
00 NUL Null \0 Originally used to allow gaps to be left on paper tape for edits. Later used for padding after a code that might take a terminal some time to process (e.g. a carriage return or line feed on a printing terminal). Now often used as a string terminator, especially in the programming language C.
01 SOH Start of Heading First character of a message header. In Hadoop, it is often used as a field separator.
02 STX Start of Text First character of message text, and may be used to terminate the message heading.
03 ETX End of Text Often used as a "break" character (Ctrl-C) to interrupt or terminate a program or process.
04 EOT End of Transmission Often used on Unix to indicate end-of-file on a terminal.
05 ENQ Enquiry Signal intended to trigger a response at the receiving end, to see if it is still present.
06 ACK Acknowledge Response to an ENQ, or an indication of successful receipt of a message.
07 BEL Bell, Alert \a Originally used to sound a bell on the terminal. Later used for a beep on systems that didn't have a physical bell. May also quickly turn on and off inverse video (a visual bell).
08 BS Backspace \b Move the cursor one position leftwards. On input, this may delete the character to the left of the cursor. On output, where in early computer technology a character once printed could not be erased, the backspace was sometimes used to generate accented characters in ASCII. For example, Ã  could be produced using the three character sequence a BS ` (or, using the characters’ hex values, 0x61 0x08 0x60). This usage is now deprecated and generally not supported. To provide disambiguation between the two potential uses of backspace, the cancel character control code was made part of the standard C1 control set.
09 HT Character Tabulation, Horizontal Tabulation \t Position to the next character tab stop.
0A LF Line Feed \n On typewritersprinters, and some terminal emulators, moves the cursor down one row without affecting its column position. On Unix, used to mark end-of-line. In DOSWindows, and various network standards, LF is used following CR as part of the end-of-line mark.
0B VT Line Tabulation, Vertical Tabulation \v Position the form at the next line tab stop.
0C FF Form Feed \f On printers, load the next page. Treated as whitespace in many programming languages, and may be used to separate logical divisions in code. In some terminal emulators, it clears the screen. It still appears in some common plain text files as a page break character, such as the RFCs published by IETF.
0D CR Carriage Return \r Originally used to move the cursor to column zero while staying on the same line. On classic Mac OS (pre-Mac OS X), as well as in earlier systems such as the Apple II and Commodore 64, used to mark end-of-line. In DOSWindows, and various network standards, it is used preceding LF as part of the end-of-line mark. The Enter or Return key on a keyboard will send this character, but it may be converted to a different end-of-line sequence by a terminal program.
0E SO Shift Out Switch to an alternative character set.
0F SI Shift In Return to regular character set after Shift Out.
10 DLE Data Link Escape Cause the following octets to be interpreted as raw data, not as control codes or graphic characters. Returning to normal usage would be implementation dependent.
11 DC1 Device Control One (XON) These four control codes are reserved for device control, with the interpretation dependent upon the device to which they were connected. DC1 and DC2 were intended primarily to indicate activating a device while DC3 and DC4 were intended primarily to indicate pausing or turning off a device. DC1 and DC3 (known also as XON and XOFF respectively in this usage) originated as the "start and stop remote paper-tape-reader" functions in ASCII Telex networks. This teleprinter usage became the de facto standard for software flow control.[6]
12 DC2 Device Control Two
13 DC3 Device Control Three (XOFF)
14 DC4 Device Control Four
15 NAK Negative Acknowledge Sent by a station as a negative response to the station with which the connection has been set up. In binary synchronous communication protocol, the NAK is used to indicate that an error was detected in the previously received block and that the receiver is ready to accept retransmission of that block. In multipoint systems, the NAK is used as the not-ready reply to a poll.
16 SYN Synchronous Idle Used in synchronous transmission systems to provide a signal from which synchronous correction may be achieved between data terminal equipment, particularly when no other character is being transmitted.
17 ETB End of Transmission Block Indicates the end of a transmission block of data when data are divided into such blocks for transmission purposes.
18 CAN Cancel Indicates that the data preceding it are in error or are to be disregarded.
19 EM End of medium Intended as means of indicating on paper or magnetic tapes that the end of the usable portion of the tape had been reached.
1A SUB Substitute Originally intended for use as a transmission control character to indicate that garbled or invalid characters had been received. It has often been put to use for other purposes when the in-band signaling of errors it provides is unneeded, especially where robust methods of error detection and correction are used, or where errors are expected to be rare enough to make using the character for other purposes advisable. In DOSWindows and other CP/M derivatives, it is used to indicate the end of file, both when typing on the terminal, and sometimes in text files stored on disk.
1B ESC Escape \e[b] The Esc key on the keyboard will cause this character to be sent on most systems. It can be used in software user interfaces to exit from a screen, menu, or mode, or in device-control protocols (e.g., printers and terminals) to signal that what follows is a special command sequence rather than normal text. In systems based on ISO/IEC 2022, even if another set of C0 control codes are used, this octet is required to always represent the escape character.
1C FS File Separator Can be used as delimiters to mark fields of data structures. If used for hierarchical levels, US is the lowest level (dividing plain-text data items), while RS, GS, and FS are of increasing level to divide groups made up of items of the level beneath it.
1D GS Group Separator
1E RS Record Separator
1F US Unit Separator
Unicode Control Characters
Hex Acro Name Description
7F DEL Delete Character In computing, the delete character (sometimes also called rubout) is the last character in the ASCII repertoire, with the code 127 (decimal). Not a graphic character but a control character, it is denoted as ^? in caret notation and has a graphic representation of ␡ in Unicode (as all ASCII control characters have graphic representations).A key marked Backspace ← that sends the Backspace character is by far the most common on modern terminals and emulators. Due to the "backspace" key sending Delete on many terminals, keys marked "Delete" typically do not send the character, instead sending an Escape sequence similar to the arrow keys.[6]
80 PAD Padding Character Not part of ISO/IEC 6429 (ECMA-48). In early drafts of ISO 10646, was used as part of a proposed mechanism to encode non-ASCII characters. This use was removed in later drafts.[2][7] Is nonetheless used by the internal-use two-byte fixed-length form of the ISO-2022-based Extended Unix Code (EUC) for left-padding single byte characters in code sets 1 and 3, whereas NUL serves the same function for code sets 0 and 2. This is not done in the usual "packed" EUC format.[8]
81 HOP High Octet Preset Not part of ISO/IEC 6429 (ECMA-48). In early drafts of ISO 10646, was intended as a means of introducing a sequence of ISO 2022 compliant multiple byte characters with the same first byte without repeating said first byte, thus reducing length; this behaviour was never part of a standard or published implementation. Its name was nonetheless retained as a RFC 1345 standard code-point name.[2][7]
82 BPH Break Permitted Here Follows a graphic character where a line break is permitted. Roughly equivalent to a soft hyphen except that the means for indicating a line break is not necessarily a hyphen. Not part of the first edition of ISO/IEC 6429.[9] See also zero-width space.
83 NBH No Break Here Follows the graphic character that is not to be broken. Not part of the first edition of ISO/IEC 6429.[9] See also word joiner.
84 IND Index Move the active position one line down, to eliminate ambiguity about the meaning of LF. Deprecated in 1988 and withdrawn in 1992 from ISO/IEC 6429 (1986 and 1991 respectively for ECMA-48).
85 NEL Next Line Equivalent to CR+LF. Used to mark end-of-line on some IBM mainframes.
86 SSA Start of Selected Area Used by block-oriented terminals.
87 ESA End of Selected Area
88 HTS Character Tabulation Set
Horizontal Tabulation Set
Causes a character tabulation stop to be set at the active position.
89 HTJ Character Tabulation With Justification
Horizontal Tabulation With Justification
Similar to Character Tabulation, except that instead of spaces or lines being placed after the preceding characters until the next tab stop is reached, the spaces or lines are placed preceding the active field so that preceding graphic character is placed just before the next tab stop.
8A VTS Line Tabulation Set
Vertical Tabulation Set
Causes a line tabulation stop to be set at the active position.
8B PLD Partial Line Forward
Partial Line Down
Used to produce subscripts and superscripts in ISO/IEC 6429, e.g., in a printer.
Subscripts use PLD text PLU while superscripts use PLU text PLD.
8C PLU Partial Line Backward
Partial Line Up
8D RI Reverse Line Feed
Reverse Index
8E SS2 Single-Shift 2 Next character invokes a graphic character from the G2 or G3 graphic sets respectively. In systems that conform to ISO/IEC 4873 (ECMA-43), even if a C1 set other than the default is used, these two octets may only be used for this purpose.
8F SS3 Single-Shift 3
90 DCS Device Control String Followed by a string of printable characters (0x20 through 0x7E) and format effectors (0x08 through 0x0D), terminated by ST (0x9C).
91 PU1 Private Use 1 Reserved for a function without standardized meaning for private use as required, subject to the prior agreement of the sender and the recipient of the data.
92 PU2 Private Use 2
93 STS Set Transmit State
94 CCH Cancel character Destructive backspace, intended to eliminate ambiguity about meaning of BS.
95 MW Message Waiting
96 SPA Start of Protected Area Used by block-oriented terminals.
97 EPA End of Protected Area
98 SOS Start of String Followed by a control string terminated by ST (0x9C) that may contain any character except SOS or ST. Not part of the first edition of ISO/IEC 6429.[9]
99 SGCI Single Graphic Character Introducer Not part of ISO/IEC 6429. In early drafts of ISO 10646, was used to encode a single multiple-byte character without switching out of a HOP mode. In later drafts, this facility was removed, the name was nonetheless retained as a RFC 1345 standard code-point name.[2][7]
9A SCI Single Character Introducer To be followed by a single printable character (0x20 through 0x7E) or format effector (0x08 through 0x0D). The intent was to provide a means by which a control function or a graphic character that would be available regardless of which graphic or control sets were in use could be defined. Definitions of what the following byte would invoke was never implemented in an international standard. Not part of the first edition of ISO/IEC 6429.[9]
9B CSI Control Sequence Introducer Used to introduce control sequences that take parameters.
9C ST String Terminator
9D OSC Operating System Command Followed by a string of printable characters (0x20 through 0x7E) and format effectors (0x08 through 0x0D), terminated by ST (0x9C). These three control codes were intended for use to allow in-band signaling of protocol information, but are rarely used for that purpose.
9E PM Privacy Message
9F APC Application Program Command