SIDEBAR
»
S
I
D
E
B
A
R
«
Old WordPress on PHP 8+: Faking get_magic_quotes_gpc() and fixing arrays
Apr 13th, 2023 by miki

If you end up in a situation where your PHP installation gets ahead of your application (like fx. if your hosting provider deprecates PHP 7.4 without you having had the opportunity to update your WordPress 4.9.22 blog), here is a small tip if this results in errors related to the, in PHP 8 removed, function get_magic_quotes_gpc() (see PHP RFC from 2018 about this decision).

As this function has been a dummy function returning false since PHP 5.4 (see php-src commit removing the functionality) and emitting a deprecation warning since PHP 7.4 (see php-src commit deprecating function) in almost any case perceivable you would be able to fake this function and satisfy your application’s need for it to exist.

To fix this for WordPress add the following line to the wp-includes/load.php file (fx. at line 7 for v4.9.22);

function get_magic_quotes_gpc() {return false;}

And voila! WordPress will be functional again (please consider using the chance to upgrade).

Unless, of course, there are other problems related to the PHP upgrade…

This could fx. be ancient themes (like Ahimsa) that use deprecated associative array notation (unquoted string literal indexes). These needs fixing first by adding quotes to all of the indexing strings in sub-arrays of the $skin_fields array, like below;

$skin_fields =
array
(
    array
    (
        "name"    => "skinpagebgtopbg",
        "desc"    => "Page Background Top",
        "csssel"  => "#bgtop",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinpagediv",
        "desc"    => "Page Background Divider Colour",
        "csssel"  => "#bgtop",
        "attr"    => "border-bottom-color"
    ),
    array
    (
        "name"    => "skinpagebgbotbg",
        "desc"    => "Page Background Bottom",
        "csssel"  => "BODY",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinhyperlinks",
        "desc"    => "Hyperlinks",
        "csssel"  => "A",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincapsulebg",
        "desc"    => "Default Bubble Background",
        "csssel"  => ".capsule",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincapsulefg",
        "desc"    => "Default Bubble Text Colour",
        "csssel"  => ".capsule",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinheaderbg",
        "desc"    => "Header Background",
        "csssel"  => "#header",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinheaderfg",
        "desc"    => "Header Text Colour",
        "csssel"  => "#header, #header table, #header a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinheadersepcolour",
        "desc"    => "Colour of Separator Bar in Header",
        "csssel"  => "#title",
        "attr"    => "border-right-color"
    ),
    array
    (
        "name"    => "skinsidebarbg",
        "desc"    => "Sidebar Background",
        "csssel"  => ".sidebar, .tdsidebar",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinsbwidgetbg",
        "desc"    => "Sidebar Widgets Background",
        "csssel"  => ".sidebarlist",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinsbwidgetfg",
        "desc"    => "Sidebar Widgets Text Colour",
        "csssel"  => ".sidebarlist",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinsblegendbg",
        "desc"    => "Sidebar Widget Title Background",
        "csssel"  => ".sidebarlist > legend",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinsblegendfg",
        "desc"    => "Sidebar Widget Title Text Colour",
        "csssel"  => ".sidebarlist > legend",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinsblistdiv",
        "desc"    => "Sidebar/Action Lists Divider Colour",
        "csssel"  => ".sidebarlist li, #postaction li",
        "attr"    => "border-top-color"
    ),
    array
    (
        "name"    => "skinsblink",
        "desc"    => "Sidebar Widget Hyperlinks",
        "csssel"  => ".sidebarlist a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincalcaption",
        "desc"    => "Sidebar Calendar Caption Colour",
        "csssel"  => "#wp-calendar caption",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincalheaderbg",
        "desc"    => "Sidebar Calendar Column Header Background",
        "csssel"  => "#wp-calendar thead th, #wp-calendar tfoot td.pad",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincalheaderfg",
        "desc"    => "Sidebar Calendar Column Header Text Colour",
        "csssel"  => "#wp-calendar thead th",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincalcellfg",
        "desc"    => "Sidebar Calendar Entries Text Colour",
        "csssel"  => "#wp-calendar tbody td",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincalnpbg",
        "desc"    => "Sidebar Calendar Next/Prev Links Background",
        "csssel"  => "#wp-calendar #next, #wp-calendar #prev",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincalnpfg",
        "desc"    => "Sidebar Calendar Next/Prev Links Text Colour",
        "csssel"  => "#wp-calendar #next, #wp-calendar #prev, #wp-calendar tfoot a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skintextwdgtfg",
        "desc"    => "Sidebar Text Widget Text Colour",
        "csssel"  => ".textwidget",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincontentbg",
        "desc"    => "Main Content Background",
        "csssel"  => "#content",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinpostpagebg",
        "desc"    => "Post or Page Entry Background",
        "csssel"  => ".post > fieldset",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinpostpagefg",
        "desc"    => "Post or Page Entry Text Colour",
        "csssel"  => ".entry",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinpptitlebg",
        "desc"    => "Post, Page, Comments Title Background",
        "csssel"  => ".post .title, #comments > legend, .comment > legend, #responsebox > legend",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinpptitlefg",
        "desc"    => "Post, Page, Comments Title Text Colour",
        "csssel"  => ".post .title, .post .title a, " .
                    "#comments > legend, .comment > legend, #responsebox > legend",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinmetabarbg",
        "desc"    => "Post/Page/Comment Bottom Bar Background",
        "csssel"  => ".postmetadata",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincattagbg",
        "desc"    => "Category/Tag Lists Background",
        "csssel"  => ".postcattags",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincattagfg",
        "desc"    => "Category/Tag Lists Text/Link Colour",
        "csssel"  => ".postcattags, .postcattags a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skin1cattagbubblebg",
        "desc"    => "Single Post Cat/Tag Bubble Background",
        "csssel"  => "#single .postcattags .capsule",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skin1cattagbubblefg",
        "desc"    => "Single Post Cat/Tag Bubble Text/Link Colour",
        "csssel"  => "#single .postcattags .capsule, #single .postcattags .capsule a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinbqbg",
        "desc"    => "Blockquote Background",
        "csssel"  => "blockquote",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinbqfg",
        "desc"    => "Blockquote Text Colour",
        "csssel"  => "blockquote",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinlistbg",
        "desc"    => "Page/Post Ordered/Unordered List Background",
        "csssel"  => ".entry UL, .entry OL",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinlistfg",
        "desc"    => "Page/Post Ordered/Unordered Text Colour",
        "csssel"  => ".entry UL, .entry OL",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinpost1stletterfg",
        "desc"    => "Post First Letter Colour",
        "csssel"  => ".entry > P:first-child:first-letter",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinactionbg",
        "desc"    => "Action Bubbles (Edit, Reply, etc) Background",
        "csssel"  => ".actbubble",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skinactionfg",
        "desc"    => "Action Bubbles (Edit, Reply, etc) Text Colour",
        "csssel"  => ".actbubble, .actbubble a",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skincommentsbg",
        "desc"    => "Comments Block Background",
        "csssel"  => "#comments",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincommentbg",
        "desc"    => "Comment Background",
        "csssel"  => "fieldset.comment, fieldset.comment .commenttext",
        "attr"    => "background-color"
    ),
    array
    (
        "name"    => "skincommentfg",
        "desc"    => "Comment Text Colour",
        "csssel"  => "fieldset.comment .commenttext",
        "attr"    => "color"
    ),
    array
    (
        "name"    => "skinresponsebg",
        "desc"    => "Response Box Background",
        "csssel"  => "#responsebox",
        "attr"    => "background-color"
    )
);
Syntax highlighting in pagers (eg. like less) using GNU source-highlight
Mar 21st, 2021 by miki

EDIT 2021-08-31: add section about possible unexpected change of behaviour when using the simple (global) setup (fx. git)

There is this nifty OS project, GNU, which has this nifty piece of software for syntax highlighting, source-highlight (aka. src-highlite), together with which is distributed this nifty shell script, src-hilite-lesspipe.sh, meant for piping arbitrary text through the highlighter selecting and applying a sensible highlight language definition, before being paged in some nifty pager which is able to interpret ANSI escape codes (ISO/IEC 6429 or ECMA-48, previously ANSI X3.64/FIPS PUB 86) like fx. the nifty and ubiquitous less.

Sadly, even after having installed less and source-highlight on modern Ubuntu and Debian systems they are not inter-operating by default. You’d have to feel the itch of syntax highlighting, discover source-highlight and dig its documentation to find out about said script.

TL;DR – quick and simple setup

Below is a quick two-line shell HOW-TO which sets up environment variables for the current user to enable auto-detection of language and subsequent syntax highlighting pr. default in less using GNU source-highlight (here done on Ubuntu 20.04 LTS, should behave similarly on Debian and derived distributions);

$ sudo apt install source-highlight
$ echo -e "\nexport LESSOPEN=\"| /usr/share/source-highlight/src-hilite-lesspipe.sh %s\"\nexport LESS=' -R '" >> ~/.bash_aliases

This setup will make fx. some C source code display as below in less.

environment set up for less to enable ANSI color codes and pass any text through source-highlighter for potential ANSI escape code addition

less showing highlighted C code, auto-detected and highlighted by GNU source-highlighter

Complicating TL;DR

One caveat of this global setup being active for any invocation of less, is that most programs will behave accordingly and some maybe different from expectations.

Notably, using the git command line client with the above setup will make git refrain from setting a default LESS=FRX environment (see core.pager of man git config) when invoking less. This turns off the less features “quit-if-one-screen” (F) and “no-init” (X) (see OPTIONS of man less) which usually makes less invisible in git contexts unless paging is actually needed. This could cause confusion for some, making git seemingly take over the terminal on even 1 line outputs (see this bug report on GNU src-hilite and my comment detailing the above).

One solution to this would of course be to add “FX” to the environment, another is making a little more elaborate, more conservative and less transparent setup, as below.

For the conservative

If you’d still like to have a “plain less” not messing with and amending you text unless you ask it to, you could make an alias to use specifically when you want syntax highlighting. Put these somewhere interpreted by you shell (for bash fx. ~/.bash_aliases);

# syntax highlight in less
alias lesssh="LESSOPEN='|/usr/share/source-highlight/src-hilite-lesspipe.sh %s' LESS=' -R ' less"
function lessurl() { wget -O- -q $1 |source-highlight -f esc -s html |less -R; }

Line two is a bonus shell function pulling some HTML from a webserver using wget, adding syntax highlighting and showing it in less.

Digging deeper

For the brave, take a tour of the “info source-highlight” (or “man source-highlight”) manuals (also here) to become familiar with the tool. You can use it anywhere you’d like some colour on arbitrary text and where color are supported in various ways and encodings, for example HTML and latex;

source-highlight adding color encoding to C code using HTML and latex encoding

If you are a programmer wanting to add highlighting features to you own application, the command line utilities are building on a highlighting library which you can utilise (API documentation here).

[Danish] S&S: gemme data i Arduino ROM/Flash (PROGMEM / F())
Dec 21st, 2016 by miki

Mit svar på et spørgsmål i Facebook-gruppen Danske Arduino Entusiaster omkring Arduino ROM/Flash, PROGMEM og system-inklude-filer.

Spørgsmål

Hej er der en der ved hvor jeg kan hente dett lib. <avr/pgmspace.h> jeg skal bruge denne funktion PROGMEM
så jeg kan gemme et billede i Arduino uden SD kort
det kan være der er en der kender en anden måde at gøre det på.

Svar

pgmspace.h er en inklude-fil som er en del af c-biblioteket til AVR-arkitekturen (avr-libc). C-bibliotekets inklude-filer vil normalt ligge i kompilerens “system include”-sti (se GCC options -I og -isystem). Dermed kan den inkluderes blot med “#include <avr/pgmspace.h>”. Se evt. også Arduino-referencen på https://www.arduino.cc/en/Reference/PROGMEM.
 
Bemærk at PROGMEM ikke er en funktion, men en storage modifier (lager-modifikator) som fortæller kompileren at den kan placere en en given variabel i ikke-skrivbar lager (ROM/Flash). Der skal efterfølgende anvendes specielle funktioner til at læse data fra en sådan variabel (se referencen).
Arduino-frameworket har dog lavet en nem måde at placere konstant-strenge i Flash på (normalt lagres de i SRAM!), nemlig funktionen F() som kan anvendes direkte i f.eks. printf/write/print (Serial.print(F(“Waiting for connection”));)
 
Hvis du vil inspicere indholdet af pgmspace.h, kan du finde filen i Arduino IDE’ets installations-mappe under hardware/tools/avr/avr/include/avr/pgmspace.h. Det er ikke en man kan/skal redigere manuelt i, da den er tæt koblet med den binære kode i selve biblioteket.
 
Der findes også EEPROM-lager du sikkert vil kunne bruge til samme formål; https://www.arduino.cc/en/Reference/EEPROM

Se svaret på Facebook.

Den videre færd med F()

Da jeg ikke kunne finde en uddybende forklaring på F()-funktionen (som egentlig er en makro) i Arduino-dokumentationen (brugen nævnes meget kort i PROGMEM , Memory og Print), gravede jeg efterfølgende lidt rundt for at lære mere. I de sparsomme Arduino-eksempler er den anvendt udelukkende med konstante strenge, hvilket også viser sig at være et krav (eller i hvert fald noget der kan castes til const char *).

Makroen er defineret af Arduino-frameworket i filen hardware/arduino/avr/cores/arduino/WString.h (referencerne er ifht. min lokale installation af Arduino 1.6.9, pt. er nyeste 1.6.13) således:

#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

Altså parametren til F() bruges som parameter til PSTR() (progmem string, er mit bud på navn) som er en makro defineret i pgmspace.h fra avr-libc.

Dens funktion er at caste parametrens type til konstant streng-pointer med PROGMEM modifier;

#define PSTR(s) ((const PROGMEM char *)(s))

Skal vi se på hvad PROGMEM rent faktisk er, så finder vi endnu et sæt makroer der ender med at blive udviddet til kompiler-attributten  __progmem__, igen definieret i pgmspace.h (hardware/tools/avr/avr/include/avr/pgmspace.h):

#define PROGMEM __ATTR_PROGMEM__

#define __ATTR_PROGMEM__ __attribute__((__progmem__))

__progmem__ attributten er en instruks til kompileren (GCC) og linkeren om ved programmering/flashing af programmet at placere disse data i en sektion af hukommelsen der hedder “.progmem“. Se evt. mere om dette i GCC-kompilerens dokumentation. For hver AVR-chip kompileren understøtter er der eksakte definitioner af hvilke hukommelsesadresser .progmem ligger på for netop denne chip.

Dvs. når man i sin kode skriver F(“test”) får man i virkeligheden:

(reinterpret_cast<const __FlashStringHelper *>(((const __attribute__((__progmem__)) char *)(“test”)))

Altså en konstant streng der lagres i AVR-processorens progmem-sektion, og som returværdi får en pointer til en konstant instans af en klasse kaldet “__FlashStringHelper“. Denne klasse må være lavet sådan at den anvender de korrekte mekanismer til at læse fra progmem-området (måske mere om dette i en senere artikel). Arduinos funktion-bibliotek (Serial.print() mm.) er lavet således at de direkte kan tage en parameter af denne type som erstatning for en konstant-streng (og det er netop her Arduino-frameworket viser sin værdi ved at abstrahere sådanne kompleksiteter væk fra programmøren).

»  Substance:WordPress   »  Style:Ahren Ahimsa
© 2023 Mikkel Kirkgaard Nielsen, contents CC BY-SA 4.0