Categories
Bookmarked PHP Programming

WhatsAPP moving to new authentication.

Though the new authentication (WUTH-2) is not really new, but few days back they disable the old authentication mechanism. So, just a bookmark to latest version of API: https://github.com/mgp25/WhatsAPI-Official . This is rather refined library and you can feel it when you use it.

Categories
Operating System

Windows System Repair

I just install Windows 8.1 on my home PC, installing SQL Server on it and found that activeds.dll file was missing. I haven’t install anything, just regular MS products and updates. So, how can a file is missing. On searching I found that DLL was present, but seems corrupted, maybe some disk writing fails or something. I start thinking to reinstall machine, as it is just been installed and got corrupted. But at last minute I found a command that not only detect all corrupted files in windows system but repair them as well. Here is steps

  1. Open CMD with Admin rights
  2. run sfc /scannow
  3. Enjoy.

It repairs few basic windows DLL that got corrupted maybe for any reason, disk fail, some program change them or whatever. It save me few hours to re-install and then update 4GB on internet again.

Categories
Article ASP.NET HTML/CSS PHP Programming

PDF Creation: wkhtmltopdf

In past I post about how good wkhtmltopdf is. I have used it for few of my projects. Really it is the cheapest [as it is open source and free] and powerful solution. But it is not the reason I love it. I love this because it is the Easiest solution available. Most of PDF library range few hundred dollars to start with. Then we learn through their call of function to draw Tables and add media content and text. But with wkhtmltopdf, all you need to do is simple create a HTML page, add CSS and Javascript (yup, it support jQuery’s document load function and other other load method of your favourite javascript library.)

I recently test jQuery load function as I need to write text as Arc, however I don’t want to use Image as image manipulation will generate a rectangle image and then placing it going to be problem. But with wkhtmltopdf, and it’s support for JavaScript it make my life easier. I use a cool ArcText js following a nice article/tutorial http://tympanus.net/codrops/2012/01/24/arctext-js-curving-text-with-css3-and-jquery/ .

And yes, I recently saw that develpoment of wkhtmltopdf has started again. So this is going to be very good for those who want easy and best solution for their application.

Categories
PHP Programming

WhatsApp Messaging from Web

If you need to send WhatsApp message to your friend from Web, you need to find unofficial API as WhatsApp till date do not provide them, and do not allow bulk messaging as well. But in one of my project, I need to send message on whatsapp, for those who subscribe on website to recieve those about our service updates. So, I use WhatsApi (https://github.com/venomous0x/WhatsAPI New URL is https://github.com/WHAnonymous/Chat-API ), simply because it was most recommended API I found. I didn’t even try any other API code, as it works like charm for me. It has AjaxDemo folder in there, with 3 files. Anyone can easily learn the coding from it. So, I don’t go in much of coding part in this article. But here is basic steps with some code. To start with,

  • You will need a Mobile number that will be used to send messages. Make sure that you can recieve SMS on this mobile phone to verify the Whatsapp account.
  • Once you have your phone ready, download WART (https://github.com/shirioko/WART) which provide a simple EXE, to enter your phone and get a Password for API. Remember Whatsapp send password once every 30 minute, so if you want to get new password you have to wait 30 minutes. This step is no longer required as API has registration function available. Please refer API documentation for it.
  • Now, that you have WhatsAPP mobile phone and password, simply try this code.

 

require_once 'whatsprot.class.php';
$username = "11111111111"; //Mobile Phone prefixed with country code so for india it will be 91xxxxxxxx
$password = "somepasswordstring";

$w = new WhatsProt($username, 0, "WhatsApp Messaging", true); //Name your application by replacing "WhatsApp Messaging"
$w->connect();
$w->loginWithPassword($password);

$target = '912222222222'; //Target Phone,reciever phone
$message = 'Your message comes here';

$w->SendPresenceSubscription($target); //Let us first send presence to user
$w->sendMessage($target,$message ); // Send Message

Yes, it was that simple. You can send Image and Video as well, but I am not going to explain them here, though to send Media you need to call ‘$w->pollMessages()’ otherwise your message won’t be sent. Note: all Phone must prefix the country code or it won’t send messages. To send media, you can either give Physical path or a URL to media resource.   EDIT: The API is now moved to new location: https://github.com/mgp25/Chat-API

Categories
PHP Programming Wordpress

How to Programmatically Add Multi Language Post in WordPress and WPML

I recently need to create wordpress post programmatically. It is so easy with wp_insert_post. However, the twist is how to add it using WPML for multi language support. WPML is nice plugin, but I didn’t find straight forward answer to it. Than I found a nice article from Ionuț Staicu, that does what I need. And it is the most popular article I found and seems like only solution people have. Article can be read in http://www.iamntz.com/3424/frontend-developer/programmatically-insert-posts-in-wordpress-and-set-language-for-wpml-plugin/. Though it doesn’t seems to solve my problem.

I email Ionuț Staicu, and to my surprise the nice guy email me back in couple of hour, and give me few suggestion and even his code snippet. I really love reading his code. On trying that code I found that he copy WPML API wpml_add_translateable_content function in his code and comment out the Check for duplicate addition of same content.

Doing so make my code work, but I was not satisfied. Sorry Ionuț Staicu, that doesn’t make me happy. Reason was simple. A company running such plugin cannot make such silly mistake. I trust WPML developer, they are of course better than me to realise what they are putting as check. 

So, what is wrong in my code? Well the answer was simple. WPML hook wp_insert_post to run “add_translate”. So, you don’t have to call it. However to add multiple language you need to call “wp_insert_post” for all language you want to add the code. This is easy..huh? no it is not easy. WPML call add_translate, but didn’t link them. I am not sure if they have any “insert” meta field that does the link. But there is no clearer way I found. So, I go back to add_translateable function and found that they have nothing but only

$sitepress->set_element_language_details(...);

other than checks. So, what does this function do. Simple, it change/add the Translation into database. So, all I have to do is, I need to update the Secondary language with the transalation id [3rd parameter of set_element_language_details ] to my default translation ID. Sound complex, no worries just read code below:

$_POST['icl_post_language'] = $language_code = 'he';  //Setting hebrew post. Ionuț Staicu suggest to have $_POST set. I follow him
$newPostID = wp_insert_post( $PostDATA );	//See wp_insert_post for details		

$trigid = wpml_get_content_trid('post_property', $newPostID); // Find Transalation ID function from WPML API. 

$_POST['icl_post_language'] = $language_code = 'fr'; // Set another language
$tpropertyid1 = wp_insert_post( $PostDATA ); // Insert French post
$sitepress->set_element_language_details($tpropertyid1, 'post_property', $trigid, $language_code); // Change this post translation ID to Hebrew's post id

//Repeating for English post.
$_POST['icl_post_language'] = $language_code = 'en'; 
$tpropertyid2 = wp_insert_post( $PostDATA );
$sitepress->set_element_language_details($tpropertyid2, 'post_property', $trigid, $language_code);

So, that just insert and link all languages you want. I did this in PHP CLI command line, and it works like charm. Hope this help someone. 

 

 

Categories
Javascript

Javascript Base64 Encode, Decode for UTF-8/unicode string

Complete article that works for me : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Base64_encoding_and_decoding 

Part where we encode from unicode/utf-8 is

function utf8_to_b64( str ) {
  return window.btoa(unescape(encodeURIComponent( str )));
}

function b64_to_utf8( str ) {
  return decodeURIComponent(escape(window.atob( str )));
}

// Usage:
utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"

 This is one of most used thing now a days.

Categories
.NET C#.NET

C#: Code to load Object from Database

It is often when managing old project that do not use ORM coding, we need to load object from database. In such scenarios it is quite boring to code all 30-40 property to load from DB. So, I wrote this simple code which does it quite well for me, the only catch is you need to define object property same as their DB column name. So, here is the quick code for the same.

public void LoadRS(DataRow rs)
    {
        foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
        {
            try
            {
                if (propertyInfo.CanRead)
                {
                    if (rs.Table.Columns.Contains(propertyInfo.Name))
                    {
                        
                        switch (propertyInfo.PropertyType.Name)
                        {
                            case "String":
                                propertyInfo.SetValue(this, Convert.ToString(rs[propertyInfo.Name]), null);
                                break;
                            case "Int64":
                                propertyInfo.SetValue(this, long.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            case "Int32":
                                propertyInfo.SetValue(this, int.Parse(Convert.ToString(rs[propertyInfo.Name])), null);
                                break;
                            default:
                                propertyInfo.SetValue(this, Convert.ChangeType(Convert.ToString(rs[propertyInfo.Name]), propertyInfo.PropertyType), null);
                                break;
                        }
                    }
                }
            }
            catch { }
        }
    }

  The code use the System.Reflection and then it read the Object property [see this.GetType().GetProperties() in foreach], iterate over properties and then checking if DataRow contain the same name column, if so, it load them. Basically the Default statement in Switch is enough for most cases, but I still leave it as is insight that in future I might need to cast some particular DB field to other field here. Oh, yes that one condition is boolean field. 

There are lot of upgradation coming in my mind as I write this blog entry, but currently no time to upgrade it and will add this function in my class library for future use.