All posts by shiji

Unlock Bell’s Carrier Lock on iPhone

For “SIM Not Supported Error” when using a SIM card other than Bell/Virgin Mobile

 

Send you device purchase agreement to

[email protected]

Their process time is usually 10-60 minutes.

 

Customer service can’t really handle this.

Yes, there’s no lock after 2018, but the store usually lock their expensive iPhone models and unlock the carrier for you after the purchase. They do forget about the unlocking step quite often.

Disable mouse zooming in Firefox

Why?

The scroll wheel on my MX master 3 doesn’t really work well with MacOS. Zooming is triggered by mistake quite often.

Windows:

  • about:config
  • mousewheel.with_control.action
  • change the value to 1

Reference: https://support.mozilla.org/bm/questions/1253302

OSX

  • about:config
  • mousewheel.with_meta.action
  • change the value to 1

In MacOS, meta key is the Command key. (Source)

The value 0 means “Do nothing”, 1 means “Scroll contents”, 2 means “Go back or forward in the history”, 3 means “Zoom in or out the contents”

More Info about the config

 

Enable MySQL/MariaDB SSL for PDO/Laravel

Server side setup

Refer to this manual from MariaDB
https://mariadb.com/kb/en/securing-connections-for-client-and-server/

require_secure_transport = ON could be added to the my.cnf file to enforece SSL for all connections (including those from localhost & unix socket)

One way SSL

Without CA verification

PDO

$pdo = new PDO('mysql:host=db.example.com;dbname=image', 'laravel', 'laravel',array(
    PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false));

Laravel (config/database.php)

// under mysql => options
'options' => extension_loaded('pdo_mysql') ? [
        PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
 ] : []

PLEASE NOTE: array_filter in this original config file is REMOVED. Because entries with empty() value will be filtered, so that MYSQL_ATTR_SSL_VERIFY_SERVER_CERT will be removed.

MYSQL_ATTR_SSL_CA is set just to enable SSL (like mysql -h …. -u … -p –ssl)

With CA verification

PLEASE BE AWARE, the cert in this mode is domain validated. The MySQL server should use a cert with matched common name, ‘db.example.com’ for example. the use of domain name for the host is preferred.

Same to without verification, but set MYSQL_ATTR_SSL_VERIFY_SERVER_CERT to true. (Or just remove the attribute, since it’s true by default)

Two way (mutual) SSL

PDO::MYSQL_ATTR_SSL_CA => "/path/to/ca-cert.pem",
PDO::MYSQL_ATTR_SSL_CERT => "/path/to/client-cert.pem",
PDO::MYSQL_ATTR_SSL_KEY => "/path/to/client-key.pem",
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false

MYSQL_ATTR_SSL_VERIFY_SERVER_CERT could be true or false, it depends.

Common Errors

PHP ‘illegal hardware instruction’ on MacOS

I’m using MacOS 10.15 using PHP 7.4.11 from homebrew.
This error basically happens when PDO SSL setup/verification failed.
For example:

PDO::__construct(): SSL operation failed with code 1. OpenSSL Error messages

Or maybe

PDO::__construct(): Peer certificate CN=Common name' did not match expected CN= domain’

I believe it’s a platform specific issue.

PDO attribute not received by Laravel

Mentioned already.
array_filter will filter out all attributes with value of null/false/0/”” …
Remove it when you have to use those values.

Fixing a “Damaged App” from App Store

Background:

  • OSX (From Apple, or Hackintosh)
  • The App was purchased from the App Store with the current iCloud ID

The problem:

  • Unable to open a previously purchased App

The error:

“Magnet” is damaged and can’t be opened. Delete “Magnet” and download it again from the App Store.

It could be any paid app, in my case, ‘Magnet’.

There exist a similar but common error caused by unsigned/modified third party apps (blocked by Gatekeeper)

App Is Damaged and Can’t Be Opened. You Should Move It To The Trash

This is NOT what my guide is talking about.

The Cause

The error basically means Apple is unable to verify the App purchased on this machine.

The MAC address of your en0 is part of the verification. Because all Macs come with a built-in Wifi/Ethernet Adapter.

Run this in your terminal to list all network interfaces.

networksetup -listallhardwareports

What exactly happened to my is, my hackintosh motherboard comes with two built-in Ethernet adapters. I disabled one (en0) from the BIOS few days after the OSX is installed.

How to fix

If you encountered the same error message, try the following:

(Software issue, basically sync your current en0 MAC address with iCloud)

  • Follow this guide from 1Password (also work for other apps from the App Store)
  • If that doesn’t help, move on:

(Hardware issue. in most cases, en0 not found)

  • list all network interfaces, find en0
  • If en0 is not found, enable it from BIOS (if you disabled it before)
  • If you didn’t remove/disable any network hardware, rename your primary interface to en0, here are some useful resources
  • After that, follow the guide from 1Password again when necessary (when it’s still not working)

If none of those work:

Go to network settings > tiny gear icon > Set service order
Open a terminal and run:

cd /Library/Preferences/SystemConfiguration/
sudo rm NetworkInterfaces.plist
sudo rm preferences.plist
Restart machine

Credit for this: Jeremy Wininger

1089. Duplicate Zeros

Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function.

Example 1:

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3]

Note:

1 <= arr.length <= 10000
0 <= arr[i] <= 9

Solution:

class Solution {
    public void duplicateZeros(int[] arr) {
        int count  = 0;
        for (int i = 0; i < arr.length; i ++){
            if (arr[i] == 0){
                count ++;
            }
        }
        
        int offset = count;
        for (int i = arr.length - 1; i >= 0; i --){
            if (i + offset < arr.length){
                arr[i+offset] = arr[i];
            }
            if (arr[i] == 0){
                offset --;
                if (i + offset < arr.length){
                    arr[i+offset] = arr[i];
                }
            }
        }
    }
}

 

88. Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively.
You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.

Example:

Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

Output: [1,2,2,3,5,6]

Constraints:

-10^9 <= nums1[i], nums2[i] <= 10^9 nums1.length == m + n nums2.length == n Solution.

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        // index for m
        int im = m -1;
        // index for n
        int in = n -1;
        // index for result
        int ir = m + n -1;
        
        for (;ir >= 0; ir --){
            
            if (im < 0){
                // If one of the array is all cleared, use the other
                nums1[ir] = nums2[in--];
            } else if (in < 0){
                nums1[ir] = nums1[im--];
            } else if (nums1[im] > nums2[in]){
                // Otherwise, pick the greater one.
                nums1[ir] = nums1[im--];
            } else {
                nums1[ir] = nums2[in--];
            }
        }
        
    }
}

640. Solve the Equation

Solve a given equation and return the value of x in the form of string “x=#value”. The equation contains only ‘+’, ‘-‘ operation, the variable x and its coefficient.

If there is no solution for the equation, return “No solution”.

If there are infinite solutions for the equation, return “Infinite solutions”.

If there is exactly one solution for the equation, we ensure that the value of x is an integer.

Example 1:

Input: "x+5-3+x=6+x-2"
Output: "x=2"

Example 2:

Input: "x=x"
Output: "Infinite solutions"

Example 3:

Input: "2x=x"
Output: "x=0"

Example 4:

Input: "2x+3x-6x=x+2"
Output: "x=-1"

Example 5:

Input: "x=x+2"
Output: "No solution"


class Solution {
    public String solveEquation(String equation) {
        String[] explodes = equation.split("=");
        
        int coeffX = 0;
        int number = 0;
        int cur  = 0;
        int digit = 0;
        
        boolean left = false;
        
        for (String s : explodes){
            left = !left;
            boolean positive = true;
            
            for (int i = 0; i < s.length(); i ++){
                char c = s.charAt(i);
                
                if (c == '+' || c == '-'){
                    // Push previous integer to number
                    number -= cur*(positive? 1 : -1)*(left? 1 : -1);
                    cur = 0; digit = 0;
                    positive = c == '+';
                    
                } else if(c =='x') {
                    // Push previous coeff of x to coeffX
                    if (digit == 0){
                        // If no coeff, coeff = 1
                        cur = 1;
                    }
                    coeffX += cur*(positive? 1 : -1)*(left? 1 : -1);
                    cur = 0; digit = 0;
                } else {
                    // It's a number.
                    cur = 10 * cur +  Integer.valueOf(s.charAt(i)-'0');
                    digit ++;
                }
            }
            
            // If last in group is a number, redundant
            if (digit > 0){
                number += cur*( ! positive? 1 : -1)*(left? 1 : -1);
                cur = 0; digit = 0;
            }
        }
        
        if (coeffX == 0 && number == 0){
            return "Infinite solutions";
        } else if (coeffX == 0){
            return "No solution";
        }
        return "x="+String.valueOf(number/coeffX);
    }
}