Categories
Continous Integration MySQL PHP Programming Project Management

Experience with BitBucket CI

I have been using bitbucket for quite sometime, and really like their service. Recently, I come to know that they have Beta of CI through pipeline, that can run any docker from Docker hub. I thought to give it a try though I have my own build setup on my local machine and really don’t need it. But why not test something new.

So, my project is regular website, which is coded in Custom PHP (no framework) and using MySQL database. I wrote some Unit Test using PHPUnit test, so all I need is to run those test when I commit to Bitbucket. It seems pretty straight forward as Bitbucket configuration example shows the configuration file for PHPUnit test. I pick it up, change path and run. My test failed, as they never get the MySQL extension for PHP. What? PHPunit test docker doesn’t have database extension installed? So, how we are suppose to test it?

Well, there are few ways, one is you use multiple docker (multi-docker environment) which I recently studied is available and I didn’t yet test it. And other is you create your own docker and use it. Install whatever you need to make it happen. Also, I realize that installing MySQL will not make any good unless I install database on it before test and change it’s certain values for testing. So, I create a new docker that has everything I need for my project, all PHP extension and database server. (You can use it from https://hub.docker.com/r/archergod/phpall/). Then I update configuration file to do following:

  1. Run MySQL service (yes by default docker do not initiate run.d etc, not sure why) and hence no service auto start.
  2. Then run `mysql` to import a Script (SQL) file to create and install database, and users needed. I put everything in that one SQL file and run it as root MySQL user. It is only a docker doing testing.
  3. then Navigate to Directory in respect to my Git folder as my default bitbucket leave your prompt at git folder.
  4. Run your test.

It does a great work, though every time you push to git, it create a CI request and download docker image, set it up based on BitBucket’s script and then run your script. So it take 2 minute extra to set the machine. If you are in habbit to push every 2 hr of work, then you end up running your test every 2 hr on every push.

But basically it is all good and is free too, which I like most. Though I have GitLab at my local machine and it works exceptionally great, but BitBucket’s CI is good and different. And in some cases is better than Gitlab’s CI.

Categories
Blog: My thoughts Operating System PHP Server Configuration

Linux Upgrade from 12.04 to 14.04: Save the day

So, this fine sunday, I think I wake with “lets spoil the day” mood. I start by checking my server to see how it is doing. A regular log view and updates I perform on my VPS. all was good, until I realise an update is available for my billing software. I decide to purchase an update for it. Before buying I forgot to check system requirement. Well it is just an update, and we install it every here and there for other things. So, I have updated copy of my billing software, which I follow the steps of upgrade and upload to my live server. All went well until I reach for software to suggest install PHP 5.6 instead of version I had. Well no problem how hard it is to update PHP in ubuntu 12.04, just matter of running apt-get.

Soon, I realise PHP 5.6 was never release for OS, and I need to use PPA. Found an old tutorial teaching me how to use PPA as it is my first time for it, the blog explain everything good, found couple more article but they are nearly the same. So, I went on and install the PPA and update the PHP 5.6. I run a command line to see what version of PHP is there and it correctly shows 5.6. So far so good, but suddenly “If anything goes wrong will go wrong” statement hit me. It only update CLI PHP not apache version. Damn, what is wrong with it.

PPA on Ubuntu 12.04 LTS need Apache 2.4 and Apache 2.4 never makes to Ubuntu 12.04. So, again there is PPA for apache but that doesn’t work for me well. I mean it does update few things, but cannot make PHP’s Lib for Apache to get installed. At this very moment, I have two options. Either I rollback all updates to my Backup Copy, or I move ahead to upgrade OS to 14.04 LTS. I choose later one.

Simply run “do-release-upgrade” and follow the course of stream. but it stuck as VestaCP’s APT repo didn’t work with upgrade. Not sure why, I just remove them from APT repo’ List and all goes well. All updated nothing is broken but Apache. It is broken because my previous attempt of PPA apache upgrade leave it broken and in conflicting state.

I remove and try reinstall apache without PPA, but that cause more conflict. After hit and trial with PPA and non PPA apache, I end up getting it upgrade and everything running.

There is no technical details shared in this post, as this post is not for Step on how to do it, but what not to do it. And that is only one thing, never miss reading about what upgrades you are going to do, never miss system requirement before paying someone. It only took me 4 hrs to get it all going, and frankly it turns out to be Positive for me, as it get my pending upgrade of OS done. But, I would have save my day if I read the simple requirement before buying the upgrade for billing software. I would have avoided unnecessary upgrade path, and reduce the down time to say 1 hr at most instead of 4 hrs. Good thing my blog is not a business, but never try that at client server.

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
PHP Wordpress

WordPress: Black and White Post Overlay Image

One of my client ask us to create Black and white thumbnail for wordpress blog post that on mouse over turn coloured. This is pretty common effect now a days and with my custom PHP or ASP.NET programming we usually create black and white image at time of upload and save it. This is what we need to do in WordPress. To save time in research [or rather I found this good article] http://bavotasan.com/2011/create-black-white-thumbnail-wordpress/ Where author try to give a good way of creating thumbnail.

At first I was impressed and feel lucky to find a good looking tutorial/article and thought it will be piece of cake now, however as I progress I found that main article miss few things

1. It doesn’t work. Yes the article in its current state didn’t work.

2. It do not filter image if it is of same size as size you need on thumbnail [as define by one of commenter].

3. It fail to create Black and white image

4. It fail to get Black and White image in HTML piece of code.

Starting from #1, ofcourse I get it to working now. So that will you will see later. I follow the commenter’s code to fix the point 2,and frankly I personnally don’t know if it is any good or not. But check looks good in current state so I keep it. Now the problem is it doesn’t create Black and white image for some user and for me. After searching for couple of minutes I found that it does create black and white image, but at different location than the code suggest.

Author is picking the wp_upload_dir() function which return the latest upload path, however in wordpress all upload goes in folder hirerchy as YYYY/MM (Year/month). And if you are uploading in older post then WordPress will upload media for that post in month and year of that post’s publish date and not current year and date. What this does is that it broke the path of black and white image upload and hence imagefilter() php function start giving error. Solution was rather simple. I use $meta’s original upload file path, strip file name and put image path in same folder as original image.

The Next problem is that on HTML page it doesn’t pick the black and white image. Again the code forgot or miss to change the Thumbnail-bw path to it’s new file. Some commenter did mention it but I didn’t get their point until at the time of writting of this article. These two crucial things once fixed the code works just fine for me. So, nothing gets change except the BW_Filter function which I have given below. Rest of code is same as original post. [link above]

add_filter('wp_generate_attachment_metadata','bw_images_filter');
function bw_images_filter($meta) {	

	$file = wp_upload_dir();
	// Need to check to see if their was even a generated 'thumb-m' in case the image uploaded was the appropriate size
	// and therefore will be used in it's largest form
	if (isset($meta['sizes']['thumbnail-bw'])) {
		$tmpfile = trailingslashit($file['basedir']);
		$explodedPath = explode("/", $meta['file']);
		unset($explodedPath[count($explodedPath)-1]); //Remove file name from path
		$explodedPath = implode("/", $explodedPath);
		$file = $tmpfile.trailingslashit($explodedPath).$meta['sizes']['thumbnail-bw']['file'];
		
		$updateFileName = explode(".",$meta['sizes']['thumbnail-bw']['file']);
		$ext = $updateFileName[count($updateFileName)-1];
		unset ($updateFileName[count($updateFileName)-1]); //remove it for now.
		$updateFileName = implode(".", $updateFileName);
		$updateFileName = $updateFileName ."-bw.". $ext;
		$meta['sizes']['thumbnail-bw']['file'] = $updateFileName;
	// The thumbnail didn't get created because the uploaded image was the correct size before scaling / cropping.
	// Need to get base directory instead of path here because the original file already has the year/month structure in it's file attribute.
	} else {
		$file = trailingslashit($file['basedir']).$meta['file'];
		$explodedPath = explode("/", $meta['file']);
 
		// Manually insert the thumbnail-bw that didn't get created upon upload and append '-bw' to the filename
		$explodedFileName = explode(".", $exploded[2]);
		$updatedFileName = $explodedFileName[0] . '-bw.' . $explodedFileName[1];
		$meta['sizes']['thumbnail-bw']['file'] = $updatedFileName;		
	}
	
	$meta['sizes']['thumbnail-bw']['width'] = 421; // Set these dimensions to match the dimensions for the add_image_size above
	$meta['sizes']['thumbnail-bw']['height'] = 220;
	

	list($orig_w, $orig_h, $orig_type) = @getimagesize($file);
	$image = wp_load_image($file);
	imagefilter($image, IMG_FILTER_GRAYSCALE);
	//imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
	switch ($orig_type) {
		case IMAGETYPE_GIF:
			$file = str_replace(".gif", "-bw.gif", $file);
			imagegif( $image, $file );
			break;
		case IMAGETYPE_PNG:
			$file = str_replace(".png", "-bw.png", $file);
			imagepng( $image, $file );
			break;
		case IMAGETYPE_JPEG:
			$file = str_replace(".jpg", "-bw.jpg", $file);
			imagejpeg( $image, $file );
			break;
	}
	
	
	return $meta;
}

Hope this code works for you. If not please share the fix and solution, I will try to keep it updates as I found better than this.

Categories
Bookmarked PHP Programming

PHP Mobile Detection

Just a quick note, to detect the mobile/tablet/desktop this script looks promising : http://code.google.com/p/php-mobile-detect/ I try to detect it my Windows Mango phone, it does detect it as Mobile, though I hate that it doesn’t recognize Windows Mango yet. But looks promising and it is easier to implement it as well.

Categories
Operating System PHP Server Configuration

PHP Upgrade on Plesk/Linux

Well if you search topic on google you will find lot about it. But here is quick guide for me with little explanation.

1. Plesk do not ship PHP as it’s integrated part, so it ask you to upgrade outside plesk and they do not provide support on it.

2. On Linux you can use default APT or YUM to upgrade PHP. In my case it is CentOS/Red hat flavour so this article apply to that mainly. Now when I try to upgrade from PHP 5.1 to PHP5.3 I cannot do that as default CentOS repo shows PHP53 as different package and do not come under default upgrade. And it though gets in conflict with PHP5.1’s current install.

3. I try to Erase existing package and thought to install PHP 5.3 afresh. But it shows a list of 110 dependencies which mainly consist of Plesk’s module. Otherwise it was fairly okay to YUM ERASE PHP. So, I have to opt for this solution which works 100% for where I read about it.

4. Atomic Corp create a script then on Red Hat favour enable 5.1 to 5.3 upgrade (Their WIKI: http://www.atomicorp.com/wiki/index.php/PHP ) Once you run this SH script you can re-run YUM to do the update. Steps are:

# wget -q -O -http://www.atomicorp.com/installers/atomic.sh
# yum upgrade php
# service httpd restart

Bingo !!! PHP upgraded. No server restart needed.

Categories
Operating System PHP Server Configuration

.htaccess Hack

Today, I got another wordpress that stop working and start giving 404 error. Previously client got it fixed by removing .htaccess from his installation and get it working. I thought he might mess his wordpress himself. But when we got similar error today I search and little and find that in .htaccess there is a php injected to wordpress which reside at  “/tmp/25454b22bf39c75795851f39d5e347c4”, after opening the file it looks like professional white script. But knowing computer programmer, only a hacker or idiot can place an important file in /tmp folder. Anyways, I don’t know the cause of hack yet, but I saw following pattern:

1.  Hack is known for wordpress and OsCommerce as of now. [I personnely see only wordpress below 3.0.5 been hacked, rather more specific to 3.0.4 version]

2. Hack need .htaccess file and /tmp folder, so only Linux [can they use windows temp? not know] and surely for Apache user it is an issue.

I cannot say wordpress or oscommerce is broken, but definately there is a control upload script that copy file to /tmp folder which is usually public readable. and then getting .htaccess is problem.

So if you are on Linux/apache and your software does the .htaccess read/write then you need to be beware.

Precaution

1. Make your .htaccess read only by user and group at most not for public

2. Create a empty non writable file /tmp/25454b22bf39c75795851f39d5e347c4 so in case someone try to copy it fails as file already exists.

3. Change your Passwords

4. Upgrade your software to latest version

5. Do not upload any theme or plugin which is not from known or tested source.

6. Look for vendor recommendation before installing any plugin.

Phew, so far my wordpress is safe, been on windows hosting I am sure the hack will be different if any, lets wait and see.

EDIT:  After few days of writing this article I found few instance where I can safely says that it is not a wordpress or OsCommerce hack, but rather a hack related to either Linux Operating System, or Apache Web server or Plesk Control panel. For sites I see this hack only those three are in common. All those site I saw get hacked in above way are not written by one developer, not belong to one server [except they all use plesk and apache], not using only Mysql [do you really thing mysql query can create ,htaccess?]  So, I give Clean chit to any Open source software as of now from this hack. This is indeed a server hack, Still no reason known to me.

Good luck guys !!!