Saturday, January 27, 2018

How to create and download a csv file from php script | PHP : Array To CSV - Download CSV File | Creating downloadable CSV files using PHP

CSV (comma-separated values) is the most widely supported format for transferring tabular data between applications. The ability to export data in CSV format is a useful feature for many programs, and is becoming increasingly common in web applications. This page explains how to use PHP to create CSV files, and how to ensure that your visitor’s browser offers to download the file instead of displaying it.
<?php
header("Content-type:application/octet-stream");
header("Content-Disposition:attachment;filename='PHP Download as CSV.csv'");
$out = fopen('php://output', 'w');

# If you want to process static data
fputcsv($out, array("Name", "Roll"));
fputcsv($out, array("Pritom 1", "Roll 1", "Extra 1"));
fputcsv($out, array("Pritom 2", "Roll 2", "Extra 2"));

# Fetch data from MySQL
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
$rows = mysql_query('SELECT * FROM t1');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($out, $row);

fclose($out);
In a real application the database connection parameters should be defined as constants in a separate configuration file.

Friday, January 26, 2018

How to revert a merge commit that's already pushed to remote branch | Revert a merge after being pushed | Undo a git merge that has been pushed to the server | Git HowTo: revert a commit already pushed to a remote repository

You could follow these steps to revert the incorrect commit(s) or to reset your remote branch back to correct HEAD/state
1. git checkout development
2. Use git log -5 to show latest 5 commits, which will result as below:
$ git log -5
commit 6e10182080307215e70d5d843d29ddfb302d776f (HEAD -> development, origin/development, origin/HEAD)
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:53:19 2018 +0600

    Test

commit f262e20f51171d2d24d9f15cd2abcf73a641ac43
Author: Pritom K Mondal <pritomkucse@gmail.com>
Date:   Thu Jan 25 20:52:34 2018 +0600

    Test

commit f808058d419e9d71b9f4bc27bf272241c0bf9971
Author: Pritom K Mondal Kumar <pritomkucse@gmail.com>
Date:   Thu Jan 25 14:45:54 2018 +0000

    Repository created
3. git reset f262e20f51171d2d24d9f15cd2abcf73a641ac43 (i.e. your commit number where you want to go)
4. run the git status to show all the changes that were part of the wrong commit, if you don't want to see untracked files run git status --untracked-files=no or as shortcut run git status -uno
5. simply run git reset --hard to revert all those changes
7. force-push your local branch to remote and notice that your commit history is clean as it was before it got polluted.
git push -f origin development
8. But by any chance if you need to abort reset you need to revert above reset option as git reset 'HEAD@{1}' as short answer.
9. Long answer is type git reflog and go to desired location.
$ git reflog
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{1}: reset: moving to HEAD
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2}: reset: moving to 1123175d5b42a8f459aff1cd7bb59082e0087775
b1110db HEAD@{3}: merge remotes/origin/branch1: Merge made by the 'recursive' strategy.
1123175 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: commit: Test from master
d81337f HEAD@{5}: commit: Test from master

Friday, January 12, 2018

Using Chrome's Element Inspector in Print Preview Mode | Is it possible in chrome to make the browser look like a print page | Inspect and Edit Pages and Styles As Print Mode

Open the Developer Tools (CTRL+SHIFT+I or F12)
 
Click the Customize and control DevTools hamburger menu button and choose More tools > Rendering settings (or Rendering in newer versions)
Check the Emulate print media checkbox at the Rendering tab and select the Print media type

Print the contents of specific DIV using javascript | jQuery Print Specific div Content demo | How to print selected div instead complete page JQuery | Print specific element using jquery | jQuery Plugin To Print Any Part Of Your Page - Print | Printing selective DOM elements on a page

HTML Element
<div>
  <h1>Header</h1>
  <div class="printable">
    <div>Body 1</div>
    <div class='c2'>Body 2</div>
    <div class='c2'><h1>Body 3 Inside Printable</h1></div>
  </div>

  <div style="margin-top:10px;border-top:1px solid;">&nbsp;</div>
  <button>Print</button>
</div>
Styling
body >div { margin:20px;border:2px solid;padding:7px; }
.printable .c2 { padding-left: 30px; }

@media print {
  @page { size: A4 landscape; padding:1cm; }
  body * { visibility: hidden; }
  .printable { 
    border:2px solid green;margin:0;left:0;top:0;position:absolute;
    width:calc(100% - 20px);padding:8px; 
  }
  .printable, .printable * { visibility: visible; }
}
JavaScript / JQuery part
document.title = "Need to print some specific area";
$("button").click(function() {
    var title = document.title;
    document.title = "Going to print";
    window.print();
    document.title = title;
});
And finally LIVE DEMO on JSFiddle

Thursday, January 11, 2018

XERO Private Application | XERO Connect Using Private Application Java | Java to Connect to XERO Private Application

At first you have to create an Private Application in XERO. If you didn't yet you can follow the below link:
Connect to Xero Private Application Using Php Application
You already notified that there are two combinations of public/private key file. Public key need to create application and Private key need to sign data before send to XERO. Below is a sample Java script which will connect to XERO private application.

package com.pkm;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.FileReader;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.KeyFactory;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;

/**
 * Created by pritom on 2/01/2018.
 */
public class XeroPrivateApiUtils {
    private static final String NONCE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final String ORGANIZATION_URL = "https://api.xero.com/api.xro/2.0/Organisation";

    private String sbs;
    private String path;
    private String method;
    private Map<Object, Object> configs = new HashMap<>();
    private Map<Object, Object> params = new HashMap<>();
    private Map<Object, Object> sign = new HashMap<>();
    private String KEY_FILE = null;
    private String CONSUMER_KEY = null;
    private String CONSUMER_SECRET = null;

    public static void main(String[] args) throws Exception {
        XeroPrivateApiUtils xero = new XeroPrivateApiUtils();
        HttpJavaClient.Response response = xero.execute("GET", XeroPrivateApiUtils.ORGANIZATION_URL, new HashMap(), new HashMap());
        System.out.println(response.toString());
    }

    private XeroPrivateApiUtils() {
        this.KEY_FILE = "private.key";
        this.CONSUMER_KEY = "IKAEAI8BA..........1MAOR9XBEHU";
        this.CONSUMER_SECRET = "STXPEDM..........0FWMPLIDGB6DS";

        putToMap(this.configs, "consumer_key", CONSUMER_KEY);
        putToMap(this.configs, "consumer_secret", CONSUMER_SECRET);
        putToMap(this.configs, "core_version", "2.0");
        putToMap(this.configs, "payroll_version", "1.0");
        putToMap(this.configs, "file_version", "1.0");
        putToMap(this.configs, "application_type", "Private");
    }

    HttpJavaClient.Response execute(
            String method, String path,
            Map<Object, Object> configs,
            Map<Object, Object> params
    ) throws Exception {
        this.method = method;
        this.path = path;
        this.params = params;
        putToMap(this.configs, configs);
        this.buildParameters();
        this.sign();

        Map<Object, Object> headers = new HashMap<>();
        putToMap(headers, "Accept", "application/json");
        if (this.method.equalsIgnoreCase("get")) {
            return HttpJavaClient.doGet((String) this.sign.get("signed_url"), headers);
        }
        return null;
    }

    private void sign() throws Exception {
        putToMap(this.params, "oauth_signature", this.generateSignature());
        putToMap(this.sign, "parameters", this.params);
        putToMap(this.sign, "signature", escape((String) this.params.get("oauth_signature")));
        putToMap(this.sign, "signed_url", this.path + "?" + normalizeParameters(true));
        putToMap(this.sign, "sbs", this.sbs);
    }

    private String generateSignature() throws Exception {
        switch ((String) this.params.get("oauth_signature_method")) {
            case "RSA-SHA1":
                this.sbs = escape(this.method.toUpperCase());
                this.sbs += "&" + escape(this.path);
                this.sbs += "&" + escape(this.normalizeParameters(false));
                RSAPrivateKey privateKey = getPrivateKey(KEY_FILE);
                Signature sign = Signature.getInstance("SHA1withRSA");
                sign.initSign(privateKey);
                sign.update(this.sbs.getBytes("UTF-8"));
                return encodeBase64(sign.sign());
            case "HMAC-SHA1":
                String secret = this.configs.get("consumer_secret") + "&";
                if (this.configs.get("access_token_secret") != null) {
                    secret = secret + this.configs.get("access_token_secret");
                }
                this.sbs = escape(this.method.toUpperCase());
                this.sbs += "&" + escape(this.path);
                this.sbs += "&" + escape(this.normalizeParameters(false));
                return hmacSha(secret, this.sbs);
            default:
                throw new Exception("Undefined signature method");
        }
    }

    private static String hmacSha(String key, String value) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(keySpec);
        byte[] result = mac.doFinal(value.getBytes());
        return encodeBase64(result);
    }

    public static String encodeBase64(String o) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encodeBuffer(o.getBytes()).replace("\r\n", "").replace("\n", "");
    }

    private static String encodeBase64(byte[] o) {
        BASE64Encoder base64Encoder = new BASE64Encoder();
        return base64Encoder.encodeBuffer(o).replace("\r\n", "").replace("\n", "");
    }

    private static RSAPrivateKey getPrivateKey(String fileName) throws Exception {
        byte[] encoded = decodeBase64(getKey(fileName));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
        return (RSAPrivateKey) kf.generatePrivate(keySpec);
    }

    private static byte[] decodeBase64(String o) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(o);
    }

    private String normalizeParameters(Boolean filter) throws Exception {
        String normalized = "";
        for (Object o : this.params.entrySet()) {
            Map.Entry me2 = (Map.Entry) o;
            String key = me2.getKey().toString();
            Object value = me2.getValue();
            Boolean allow = !(key.equalsIgnoreCase("xml") && filter) && !key.endsWith("_secret");
            if (allow) {
                normalized += escape(key) + "=" + escape(value.toString()) + "&";
            }
        }
        return normalized.length() > 0 ? normalized.substring(0, normalized.length() - 1) : normalized;
    }

    private static String escape(String context) throws Exception {
        context = rawUrlEncode(context);
        context = stringReplace("+", "%20", context);
        context = stringReplace("!", "%21", context);
        context = stringReplace("*", "%2A", context);
        context = stringReplace("\'", "%27", context);
        context = stringReplace("(", "%28", context);
        context = stringReplace(")", "%29", context);
        return context;
    }

    private static String rawUrlEncode(String context) throws Exception {
        return URLEncoder.encode(context, "UTF-8");
    }

    private static String rawUrlDecode(String context) throws Exception {
        return URLDecoder.decode(context, "UTF-8");
    }

    private static String stringReplace(String search, String fill, String context) {
        return context.replace(search, fill);
    }

    private void buildParameters() {
        putToMap(this.params, "oauth_nonce", shuffle(5));
        putToMap(this.params, "oauth_token", CONSUMER_KEY);
        putToMap(this.params, "oauth_version", "1.0");
        putToMap(this.params, "oauth_timestamp", "" + ((System.currentTimeMillis()) / 1000));
        putToMap(this.params, "oauth_consumer_key", CONSUMER_KEY);
        putToMap(this.params, "oauth_signature_method", "RSA-SHA1");
        this.params = new TreeMap(this.params); //Sorted map by key
    }

    private static String shuffle(Integer length) {
        List<Character> characters = new ArrayList<Character>();
        for(char c : NONCE.toCharArray()){
            characters.add(c);
        }
        StringBuilder output = new StringBuilder(length);
        while(length != 0) {
            int randPicker = (int) (Math.random() * characters.size());
            output.append(characters.get(randPicker));
            length --;
        }
        return output.toString();
    }

    private static String getKey(String filename) throws Exception {
        String key = "";
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = br.readLine()) != null) {
            if (!line.startsWith("-")) {
                key += line + "\n";
            }
        }
        br.close();
        return key;
    }

    private static void putToMap(Map<Object, Object> map, Map fromMap) {
        for (Object key : fromMap.keySet().toArray()) {
            putToMap(map, key.toString(), fromMap.get(key));
        }
    }

    private static void putToMap(Map<Object, Object> map, Object key, Object value) {
        map.put(key, value);
    }

    private static void println(Object o) {
        System.out.println("" + o);
    }

    private static void exception() throws Exception {
        throw new Exception();
    }
}
You can download a sample Private Key File from this link
And output? Obviously as below:
{
  "Id": "966aed9c-....-4d86-....-c59a6b34eb2c",
  "Status": "OK",
  "ProviderName": "Dev Private",
  "DateTimeUTC": "\/Date(1515683933534)\/",
  "Organisations": [
    {
      "APIKey": "WXP9HDM..........ILMSZHZIUSIA5",
      "Name": "Dev  Organisation",
      "LegalName": "Dev  Organisation",
      "PaysTax": true,
      "Version": "AU",
      "OrganisationType": "COMPANY",
      "BaseCurrency": "AUD",
      "CountryCode": "AU",
      "IsDemoCompany": false,
      "OrganisationStatus": "ACTIVE",
      "FinancialYearEndDay": 30,
      "FinancialYearEndMonth": 6,
      "SalesTaxBasis": "ACCRUALS",
      "SalesTaxPeriod": "QUARTERLY1",
      "DefaultSalesTax": "Tax Exclusive",
      "DefaultPurchasesTax": "Tax Inclusive",
      "CreatedDateUTC": "\/Date(1514794572000)\/",
      "OrganisationEntityType": "COMPANY",
      "Timezone": "AUSEASTERNSTANDARDTIME",
      "ShortCode": "!wnfkR",
      "OrganisationID": "91de7ce4-....-48e8-....-74573ab8b4e0",
      "LineOfBusiness": "tests xero integration",
      "Addresses": [],
      "Phones": [],
      "ExternalLinks": [],
      "PaymentTerms": {}
    }
  ]
}