Christian WeiskeCreating a custom magic file database (17.1.2019, 20:35 UTC)

The unix file utility command uses a "magic" database to determine which type of data a file contains, independently of the file's name or extension.

Here is how I created a custom magic database for testing purposes:

Test files

At first I created some files to run the tests on:

test.html
test.php
<?php echo 'foo'; ?>
test.23
Test 23

Let's see what the standard magic database detects here:

$ file test.*
test.23:   ASCII text
test.foo:  PHP script, ASCII text
test.html: HTML document, ASCII text

$ file -i test.*
test.23:   text/plain; charset=us-ascii
test.foo:  text/x-php; charset=us-ascii
test.html: text/html; charset=us-ascii

Magic database

The magic database contains the rules that are used to detect the type.

It's a plain text file with a rule on each line. Lines may refer to the previous line, so that rules can be combined. The full documentation is available in the magic man page.

Here is my simple file that detects "23" within the first 16 bytes of the file and returns the "text/x-23" MIME type:

my-magic
0 search/16 23 File containing "23"
!:mime text/x-23

We can already use it:

$ file -m my-magic test.23 
test.23: File containing "23", ASCII text

Compilation

If you want to use it many times, you should compile it to a binary file for speed reasons:

$ file -C -m my-magic
$ file -m my-magic.mgc test.*
test.23:   File containing "23", ASCII text
test.foo:  ASCII text
test.html: ASCII text

$ file -i -m my-magic.mgc test.*
test.23:   text/x-23; charset=us-ascii
test.foo:  text/plain; charset=us-ascii
test.html: text/plain; charset=us-ascii

The HTML and PHP files that have been detected properly earlier are not detected anymore, because my own magic database does not contain the rules of the standard magic file (/usr/share/misc/magic.mgc).

You may however pass multiple magic files to use, separated with a :

$ file -i -m my-magic.mgc:/usr/share/misc/magic.mgc test.*
test.23:   text/x-23; charset=us-ascii
test.foo:  text/x-php; charset=us-ascii
test.html: text/html; charset=us-ascii

Programming language detection

With this knowledge, I wrote a magic file that detects the programming language in source code files, so that phorkie can automatically choose the correct file extension: MIME_Type_PlainDetect.

Link
Stefan KoopmanschapSome changes for me (17.1.2019, 16:00 UTC)

For the past years a lot of my focus has been on the (PHP) community. I've spoken at numerous conferences and usergroups. And although I've been cutting down on the amount of conferences, I've done more usergroups in the past year than in the years before that.

In December 2018, I've made a decision to cut down on this a bit more. This has nothing to do with not wanting to speak anymore, but more with an opportunity that has arisen that I want to take. I want to put 110% of my effort into this, which means I have to cut down on other activities that I'm doing. Speaking at usergroups and conferences is one of those things.

PHP has been my biggest hobby for the past 20+ years. It is great that I have been able to make it my job as well. Since quite a few years, I've picked up on something I've been interested in for years. I've started doing live radio. My first radio show was on the now discontinued Internet radiostation On Air Radio, after which I've moved on to another Internet radiostation IndieXL. Both times I did everything in my own little radio studio that I had built at home. It was a lot of fun.

My interest in radio already began when I was a teen. A Dutch morning show was also broadcasting on TV, so I was "watching radio" every morning. In the 90's, the Dutch radiostation KinkFM introduced me to an incredible amount of alternative music. KinkFM was the best radiostation I could imagine in terms of music, but also in terms of DJ's. People with an incredible passion for and knowledge of music. When the station was stopped by its owner in 2011, I was incredibly sad.

2 years ago one of the original founders of KinkFM saved the brand name from the company that at that time owned the name. While he wasn't planning to restart the station, the response he got was overwhelming, so he started researching his options. I got in touch and over a year ago I started doing a Spotify playlist for them called KLUB KINK.

Late last year, the announcement came: A new radiostation focussing on alternative music will be launched. Since FM is something nearly of the past, the name will now be KINK.

I have been asked to evolve my Spotify playlist into a podcast, and next to that, present a radioshow. After giving it some thought and looking at my schedule, I have decided to take this opportunity. I love doing radio, and to be able to do it for my all-time favorite radiostation is amazing. Starting on Thursday February 7, I will be doing a radioshow every Thursday from 7PM to 9PM.

Will I be completely gone from conferences and usergroups? Of course not! But as I mentioed earlier, I really want this to succeed, I want to give it 110% of my effort, and that means making tough choices.

Link
Evert Pot407 Proxy Authentication Required (15.1.2019, 15:00 UTC)

407 Proxy Authentication Required is an error a HTTP proxy returns if it requires authentication. It’s really similar to 401 Unauthorized.

Example

HTTP/1.1 407 Proxy Authentication Required
Proxy-Authenticate: Basic; realm="Secured area"

As you can see above, the response looks pretty similar to 401 and can use the same authentication schemes.

This is an example of a client authenticating with a proxy:

GET / HTTP/1.1
Proxy-Authorization: Basic d2VsbCBkb25lOllvdSBmb3VuZCB0aGUgc2Vjb25kIGVhc3RlciBlZ2cK

If a clients connects to a server that requires authentication via a proxy that also requires authentication, both headers might appear in a request:

GET / HTTP/1.1
Proxy-Authorization: Basic ZWFzdGVyIGVnZzpudW1iZXIgdGhyZWUK
Authorization: Bearer c2VuZCBtZSBhIHR3ZWV0IG9yIHNvbWV0aGluZwo

Usage

HTTP proxies used to be more common. They can act like local caches or provide an additional layer of security in a corporate environment. These days they are less common for those purposes. Caching is not as needed, and the security layer is more often implemented with a VPN. Most HTTP proxies these days are reverse proxies, which is mostly transparent for HTTP clients.

Basic and Digest authentication for proxies is widely supported by browsers and other HTTP clients.

References

Link
thePHP.ccFaster Code Coverage (14.1.2019, 07:00 UTC)
Link
Christian WeiskeNextcloud public share: Missing password (10.1.2019, 20:03 UTC)

After upgrading from Nextcloud 13 to 14, someone told me they could not access files in on of our public, password protected, Nextcloud shares.

I tried it myself and saw the same problem: After entering the password to the share, the gallery page and the first couple of images would load. The rest of the images were not visible. When reloading the page, I had to provide the password again.

I upgraded to Nextcloud 15 in the hope that it would solve the error, but it did not. I only got into more trouble and had to manually fix IMAP login after upgrading.

Symptoms

  1. The gallery index page loads .../index.php/apps/gallery/files.public/list?location=&mediatypes=image/png;image/jpeg;image/gif;image/x-xbitmap;image/bmp&features=&etag&token=deadbeefcafe

  2. At least the first image loads, but not all (/index.php/apps/gallery/preview.public/2342?width=400&height=200&token=deadbeefcafe)

  3. All other images return a HTTP 401 status code and the following JSON:

    {"message":"Missing password","success":false}
    

Solution

When using a private browser window, it worked.

It also began to work when deleting all cookies via the browser developer console.

Link
PHP: Hypertext PreprocessorPHP 7.3.1 Released (10.1.2019, 00:00 UTC)
The PHP development team announces the immediate availability of PHP 7.3.1. This is a security release which also contains several bug fixes.All PHP 7.3 users are encouraged to upgrade to this version.For source downloads of PHP 7.3.1 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Link
PHP: Hypertext PreprocessorPHP 7.2.14 Released (10.1.2019, 00:00 UTC)
The PHP development team announces the immediate availability of PHP 7.2.14. This is a security release which also contains several minor bug fixes.All PHP 7.2 users are encouraged to upgrade to this version.For source downloads of PHP 7.2.14 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Link
PHP: Hypertext PreprocessorPHP 7.1.26 Release Announcement (10.1.2019, 00:00 UTC)
The PHP development team announces the immediate availability of PHP 7.1.26. This is a security release which also contains several bug fixes.All PHP 7.1 users are encouraged to upgrade to this version.For source downloads of PHP 7.1.26 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog.
Link
PHP: Hypertext PreprocessorPHP 5.6.40 Released (10.1.2019, 00:00 UTC)
The PHP development team announces the immediate availability of PHP 5.6.40. This is a security release. Several security bugs have been fixed in this release. All PHP 5.6 users are encouraged to upgrade to this version. For source downloads of PHP 5.6.40 please visit our downloads page, Windows source and binaries can be found on windows.php.net/download/. The list of changes is recorded in the ChangeLog. Please note that according to the PHP version support timelines, PHP 5.6.40 is the last scheduled release of PHP 5.6 branch. There may be additional release if we discover important security issues that warrant it, otherwise this release will be the final one in the PHP 5.6 branch. If your PHP installation is based on PHP 5.6, it may be a good time to start making the plans for the upgrade to PHP 7.1, PHP 7.2 or PHP 7.3.
Link
Evert PotMySQL examples in Node.js (9.1.2019, 17:00 UTC)

If you’re integrating your Node.js service with MySQL, you probably want to execute queries.

I’ve seen a fair bit of ugly code to do this, often using callbacks. I thought I would share some of my own patterns here. Maybe this is useful to others to read.

I don’t typically use ORM’s. If you’re already happy with sequalize, this article is probably not for you.

Prequisites

The most popular NPM package for MySQL is mysql, but I use the mysql2 package instead.

The mysql2 package is also very popular, but has a few advantages. The advantage I care about is support for promises. The authors of this package have actually teamed up with the authors of the mysql package to reduce double efforts, and was made to be compatible with mysql so for many people it’s a drop-in replacement.

Creating a pool

const mysql = require('mysql2/promise');
const pool = mysql.createPool({
  host: 'localhost',
  user: 'root',
  database: 'test',
  waitForConnections: true,
  connectionLimit: 10,
  queueLimit: 0
});

A pool manages multiple connections. I create a pool once per node.js server.

Note: if you are migrating from the mysql to mysql2 package, you probably don’t want to require mysql2/promise. You can instead require mysql2 to get full backwards compatibility and use the promise() functions to get access to promisified versions of the APIs.

Note2: mysql2 uses the utf8mb4 charset by default. If you don’t know what this means trust that it’s the right choice. If you do know what this is, rejoice that somebody finally picked a sane default.

Executing a SELECT query

Generally I don’t need transactions for SELECT queries, because of this I can simply ask the pool to execute the query.

async function getBlogPost(id) {

  const result = await pool.query('SELECT * from posts WHERE id = ?', [id]); 
  if (!result[0].length < 1) {
    throw new Error('Post with this id was not found');
  }
  return result[0][0];

}

Truncated by Planet PHP, read more at the original (another 15105 bytes)

Link
LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP