Friday, 14 December 2012

PHP cheating sheet

Cope File to Remote Server Using PHP

1. install php ssh2 extension

in ubuntu

apt-get install libssh2-1-dev libssh2-php

 test if php ssh2 installed

file_exists('ssh2_connect')

2. find finger print of remote server
 connect to the server
 ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
the second part is fingerprint

3. using ssh2 to scp files

$conn = ssh2_connect('remote-server-name', 22);

$fingerprint = ssh2_fingerprint($connection,
SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX);

if ($fingerprint != $knowprint) {
    die("middle man found");
}
ssh2_auth_password($conn, 'username', 'password');

ssh2_scp_send($conn, 'file_name_in_local, 'file_name_in_remote', 0777);

Use call_user_func function

class Hello {
    public static function say_hello()
    {
        echo "Hello World!\n";
    }
}

//There are three ways to use call_user_func

//seperate class name and function
call_user_func(array("Hello", 'say_hello'));

//combine class name and function into one string
call_user_func('Hello::say_hello');

//create a object
call_user_func(array(new Hello(), 'say_hello'));

PHP extension

what extensions have been loaded

php -m 

In Alpine, extension is here /usr/lib/php/modules

Thursday, 13 December 2012

Find a remote server ssh fingerprint

1. connect to the server
2. ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub
3. should see three parts
the second part is fingerprint

Thursday, 6 December 2012

Linux Commands

Limit resource a user can access

edit file /etc/security/limits.conf

//show limits
ulimit -a

See differences between two folders

diff -rq folder1 folder2

Find file in mac or Linux

find ./ -name platform_check.php

Open image

open test.png
open test.jpg

curl

//curl to redirect content to file
curl  --output leo.jpg https://checkout.whahaha-content.com/images/unionpay.png

created nested directories

The -p flag will create nested directories if directories do not exist. If exist, do nothing.

//mkdir: leo: No such file or directory
mkdir leo/test

//this works
mkdir -p leo/test

sed command

replace all occurences of a pattern

//ie flag will replace in place and that file will be modified
sed  -ie 's/unix/linux/g' my.txt

//all leo in my.txt will be replaced by developer. Also will make a backup file called my.txt.bak
sed -i.bak "s/leo/developer/g" my.txt

find which process is using a port number in mac

lsof -nP -iTCP -sTCP:LISTEN | grep port-number

Saturday, 1 September 2012

Configure dual monitor

In Ubuntu 12.04.1 LTS

Click Dash home, and type "display". Launch the display configure program.

In Window 7

Press window command key+p to launch the display configure program.

PhpStorm

Find usage for a function in the project

Select a symbol for which you want to find usages, right-click the symbol, and select Find Usages from its context menu.

Find functions in a file
View | Tool Windows | Structure
Variable type hint
/** @var RouteCollectorProxy $app */
    public static $app;

Tuesday, 14 August 2012

SVN Tip

SVN Patch

Run command in project root

1. Create a patch
svn diff > mypatch.diff

2. apply a patch
download patch

patch -p0 -i fix_path.diff

3. reverse patch
 patch -p0 -R -i fix_path.diff

4.  How to revert svn commit:
The following two commands will do the trick

svn merge -r 101:100 http://svn.example.com/repos/calc/trunk
svn ci



5. SVN Blame

svn blame svn://repositoy/filename
                       

Thursday, 2 August 2012

Set up localhost server

  1. npm init
  2. npm install express
  3. create a index.html and index.js
  4. node index.js
  5. http://localhost:5000
//index.js
const express = require('express');
const app = express();
const port = 5000;

app.get('/', (req, res) => {
    res.sendFile('index.html', {root: __dirname});
});

app.listen(port, () => {
    console.log(`Now listening on port ${port}`);
});

Create a post api using express

const express = require('express');
const app = express();

//middleware to parse incoming request with json payload
app.use(express.json());

//middleware to parse incoming request with url encoded payload
app.use(express.urlencoded({ extended: true }));

const port = 5000;

app.get('/', (req, res) => {
    res.sendFile('index.html', { root: __dirname });
});

app.listen(port, () => {
    console.log(`Now listening on port ${port}`);
});

app.post('/api/create_transaction', (req, res) => {
    const body = req.body;
    console.log("body:", body);
    res.status(201).json([]);
});

Sunday, 29 July 2012

Change wireless password

1. connect a computer with router by cable
2. go to http://192.168.1.254
3. log in as admin
4. go to wireless ->security
5. change password