Technology Tales

Notes drawn from experiences in consumer and enterprise technology

17:14, 25th April 2023

Find Command in Linux

The find command on Linux is a versatile tool used to locate files and directories based on specific criteria such as name, type, size, permissions, modification date and ownership. It operates using a structured syntax that includes options, paths and expressions, allowing users to search recursively through directory hierarchies. Common applications include locating files by name with -name, filtering by file type with -type, searching for files within a size range using -size and identifying files with particular permissions via -perm.

The command can also perform actions on matched files, such as modifying permissions with -exec or deleting files with -delete, though caution is advised when using destructive operations. Examples demonstrate its use in tasks like finding all JavaScript files in a directory, locating files modified within a specific timeframe, or changing ownership of files owned by a particular user. Its flexibility makes it essential for system administrators and users requiring precise file management on Linux systems.

14:51, 23rd April 2023

Hugo: Shortcodes are small snippets placed within content files that are rendered using predefined templates, allowing authors to incorporate raw HTML or additional formatting into Markdown content. The shortcodes covered in this article span a wide range of functions, including managing HTML abbreviations and anchor links, displaying styled blockquotes and code examples with syntax highlighting, applying inline colour styling, embedding images in PNG, JPG, TIFF and SVG formats with resizable options, rendering keyboard key indicators and generating internal page links with optional anchor targeting. Additional shortcodes handle styled alert and note blocks in varying colours to convey danger, information, success, tips and warnings, as well as tag linking, connections to Hugo's official documentation, OpenBSD manual pages and Wikipedia articles. All shortcodes are stored in the layout/shortcodes/ directory, with referenced content files placed in a dedicated folder within the content directory, and the implementation described here was built using Hugo version 0.53 on OpenBSD 6.6.

11:06, 15th April 2023

PHP: apache_request_headers

The apache_request_headers() function, available since PHP 4.3.0, retrieves all HTTP request headers from the current request as an associative array, and works across Apache, FastCGI, CLI and FPM server environments, with FPM support added in version 7.3.0. The function takes no parameters, and because HTTP header field names are case-insensitive by standard, developers should loop through returned keys in a case-insensitive manner rather than relying on consistent capitalisation. In environments where the function is unavailable, a common workaround involves iterating over the $_SERVER superglobal and extracting keys prefixed with HTTP_, reformatting them to approximate the original header naming convention, or using Apache's mod_rewrite to pass specific headers into the PHP environment as server variables.

11:05, 15th April 2023

PHP File Create/Write

In PHP, files are created and written to using the fopen() function, which generates a new file if one does not already exist, provided it is opened in either write mode (w) or append mode (a). Writing content to a file is handled by the fwrite() function, which takes the file reference and a string as its parameters.

Opening an existing file in write mode will erase all of its previous content, leaving a blank slate for new data, whereas append mode preserves existing content and adds new data to the end of the file. Once all required operations are complete, the fclose() function is used to close the file, and appropriate file permissions must be in place to allow PHP to write data to the server's storage.

11:04, 15th April 2023

PHP Date and Time

PHP provides a range of built-in functions for handling dates and times, including date(), mktime(), strtotime() and time(), which format, calculate and convert dates and timestamps. The date() function allows formatting local dates and times using specified characters such as Y for four-digit years or l for day names, while mktime() generates Unix timestamps from given dates. strtotime() converts textual date descriptions into timestamps and time() returns the current timestamp. Timezone settings can be adjusted using date_default_timezone_set() to ensure accurate local time representation and date_default_timezone_get() retrieves the current default timezone. Examples demonstrate formatting dates and times in various styles, calculating timestamps for specific dates and adjusting outputs based on timezone configurations.

19:21, 19th March 2023

How to secure and protect WordPress through .htaccess File

Securing a WordPress site involves implementing various .htaccess strategies to enhance protection and performance. Restricting access to the wp-admin area helps prevent unauthorised users from tampering with backend settings. Blocking specific IP addresses through .htaccess can deter malicious activity, while protecting the wp-config.php file ensures sensitive database credentials remain hidden. Preventing hot-linking stops others from using your site's images without permission, and redirecting feeds to Feedburner reduces the risk of exposing internal resources.

Setting up a maintenance page during updates or repairs maintains a professional appearance and prevents user confusion. Additionally, optimising performance through gzip compression and caching headers improves load times and reduces bandwidth usage. Tools like Wordfence or BulletProof Security offer further layers of protection by identifying vulnerabilities and blocking threats. Renaming the .htaccess file or using plugins to manage security settings can simplify maintenance. These measures collectively create a robust defence against common threats, ensuring the site remains secure and efficient.

13:52, 9th March 2023

Bash For Loop Examples

A bash for loop is a programming construct that allows commands to be executed repeatedly, either for a set number of iterations or indefinitely, and can be used directly at the command line or within a script. The basic syntax involves specifying a variable alongside a list of values or a range, with commands placed between do and done keywords.

Ranges can be expressed using brace expansion such as {1..10} in bash version 3.0 and above, with an optional step value available in version 4.0 and above using the format {start..end..increment}. A C-style three-expression syntax, borrowed from the C programming language, is also supported, using an initialiser, a condition and a step expression within double parentheses, and this approach has the advantage of accepting variables within its range definitions, unlike brace expansion. The older seq command can generate number sequences for use in loops but is considered outdated and less efficient than built-in methods.

Loops can be made infinite by omitting all expressions from the C-style syntax, while the break and continue statements allow early exit from or continuation within a loop respectively. For loops can also iterate over arrays, shell variables containing lists, command substitution output and command-line arguments. Practical applications include automating server maintenance tasks such as running updates or checking uptime across multiple machines, managing firewall rules, installing packages and pinging a range of IP addresses, though for more complex automation tasks, dedicated tools are generally recommended.

10:25, 6th March 2023

WordPress 4.6 Admin Font Change Fix – How To Restore Open Sans Font?

Changing the font in the WordPress admin panel can be achieved through a plugin, though there are some limitations to consider. The plugin allows users to modify the appearance of the dashboard, including the font used in the TinyMCE editor, which is particularly useful for non-English users. However, early versions of the plugin had issues with certain user roles, such as editors, who might encounter access errors. These were later addressed in updates.

While the plugin provides a straightforward solution, it does not support fonts available through Google's Early Access program, such as NanumGothic. For those preferring manual adjustments, editing CSS files in the /wp-admin/css/ directory is an option, though this method is discouraged due to the risk of losing automatic WordPress updates.

01:30, 25th February 2023

Making Pretty PDFs with Quarto

Quarto, an open-source scientific and technical publishing system, can produce professional-looking PDF documents through the use of custom extensions. A Quarto extension for styled PDFs is built around a LaTeX configuration file that handles visual elements such as a coloured sidebar, custom fonts, repositioned page numbers and adjusted section heading styles, all defined within a structured repository containing supporting files for page styling and optional branding imagery. Colours are defined using hex values at the outset, making it straightforward to update the overall appearance later, and fonts such as Ubuntu can be loaded from within the extension directory itself.

The extension is installed via the command line and, once in place, is activated by specifying it as the output format in the document's configuration. Those wishing to adapt the template can either modify the installed files directly or fork the underlying repository to maintain their own version, with adjustments to colours, logos and typography all achievable through relatively minor edits to the core LaTeX file. While HTML formats are generally preferable for accessibility reasons, PDF remains a practical necessity in many professional contexts, and this approach offers a reusable, consistent way to produce polished outputs.

11:44, 20th February 2023

How to Back Up and Restore MySQL Databases with mysqldump

The mysqldump utility is a command-line tool used to back up and restore MySQL and MariaDB databases by generating SQL statements that can recreate the original database, with support also for CSV and XML output formats. A single database can be backed up with a straightforward command referencing the username and database name, while multiple or all databases can be captured in one operation using the relevant flags, and InnoDB databases benefit from the addition of a single-transaction flag to avoid locking issues during the process. Large databases can be compressed by piping output through gzip, and including a timestamp in the filename helps distinguish multiple backups stored in the same location.

Restoring a dump requires creating a target database first and then passing the dump file to the mysql client, and it is also possible to restore a single database from a full multi-database dump using the one-database option. For those who want to transfer data directly between servers, mysqldump output can be piped straight to a remote mysql client without saving an intermediate file.

Backups can be automated using cron jobs, with database credentials stored securely in a restricted configuration file and old backup files removed automatically using the find command. Common issues such as access denied errors, connection failures and packet size limits each have straightforward remedies, and individual tables or schema-only exports are also possible through additional command options.

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.