Open Every New Window In Center Of The Browser

It is easy to open a new window in javascript, example you just use

window.open(https://dawud135.wordpress.com/’, ‘_newwindow’, ‘width=480px,height=500px,toolbar=no,menubar=no,scrollbars=no,location=no,directories=no’);

but this new window is open not in the center of the browser. So to make every new window is centered we need to overwrtie this window.open function, calculate the window width and height and then adjust the new window position.

This is what i use to achieve it:


// function to overwrite the window.open so the opened window is in the middle of the window parent
window.open = function (open) {
return function (url, title, params) {
// set name if missing here
title = title|| "default_window_name";
var w = 480, // default width
h = 640, // default height
arr_other_params = [];

var arr = params.split(‘,’);

for (var idx in arr) {
var row = arr[idx].split(‘=’);
if (row.length == 2) {
if (row[0] == ‘width’) {
w = row[1];
} else if (row[0] == ‘height’) {
h = row[1];
} else if (row[0] == ‘left’ || row[0] == ‘top’) {
//do nothing, overwrite these later
} else {
arr_other_params.push(row[0] + ‘=’ + row[1]);
}
}
}

var userAgent = navigator.userAgent,
mobile = function () {
return /\b(iPhone|iP[ao]d)/.test(userAgent) ||
/\b(iP[ao]d)/.test(userAgent) ||
/Android/i.test(userAgent) ||
/Mobile/i.test(userAgent);
},
screenX = typeof window.screenX != ‘undefined’ ? window.screenX : window.screenLeft,
screenY = typeof window.screenY != ‘undefined’ ? window.screenY : window.screenTop,
outerWidth = typeof window.outerWidth != ‘undefined’ ? window.outerWidth : document.documentElement.clientWidth,
outerHeight = typeof window.outerHeight != ‘undefined’ ? window.outerHeight : document.documentElement.clientHeight – 22,
targetWidth = mobile() ? null : w,
targetHeight = mobile() ? null : h,
V = screenX < 0 ? window.screen.width + screenX : screenX,
left = parseInt(screenX + (outerWidth – targetWidth) / 2, 10),
right = parseInt(screenY + (outerHeight – targetHeight) / 2.5, 10),
features = [];

if (targetWidth !== null) {
features.push(‘width=’ + targetWidth);
}

if (targetHeight !== null) {
features.push(‘height=’ + targetHeight);
}

features.push(‘left=’ + left);
features.push(‘top=’ + right);

for (var i in arr_other_params) {
features.push(arr_other_params[i]);
}

// return PopupCenter(url, title, w, h, other_params);
var newWindow = open.call(window, url, title, features.join(‘,’));

if (window.focus) {
newWindow.focus();
}

return newWindow;
};
}(window.open);

Overwrite json representation of an object with JsonSerializable

Sometimes we need to change the json representation of an object. let say we have Person class with first name and last name, but somehow the API specification said we need to pass a full name of that person. This is where the JsonSerializable interface come in handy, objects implementing JsonSerializable can customize their JSON representation when encoded with json_encode().

View this code example:

firstName = $firstName;
$this->lastName = $lastName;
}

public function jsonSerialize() {
return [
'fistName' => $this->firstName,
'lastName' => $this->lastName,
'fullName' => $this->firstName . ' ' . $this->lastName
];
}
}

$me = new Person('David', 'Abu Sabiq');

echo json_encode($me);

This will result:

{“fistName”:”David”,”lastName”:”Abu Sabiq”,”fullName”:”David Abu Sabiq”}

PHP Fatal error: Call to undefined function mcrypt_get_iv_size()

I got a problem on my site today, suddenly it doesn’t work when i updated via git.
in my error log there is :

FastCGI sent in stderr: "PHP message: PHP Fatal error: Call to undefined function mcrypt_get_iv_size() ......

and so i try to install the mcrypt library

apt-get install php5-mcrypt

and then restart my nginx

service nginx restart

but sadly my site still not working, and the error still remain.
and when i see the result from
php -i
there is no mcrypt displayed which is mean that the mcrypt is not loaded yet.

then i run

php5enmod mcrypt

and there is lines in phpinfo that said that mcypt is enabled. and once again i restart the nginx

service nginx restart

but once again the site still not working and the undefined function mcrypt_get_iv_size() still remain.

after searching around, it seems there are some people having trouble like mine.
so the solution for my problem is quite simple (although searching for it take me quite sometime :D).
so what i need to do is restart the php5-fpm service

service php5-fpm restart

and restart my nginx

service nginx restart

and there it is, my web running well again 😀

Show last bad login

ok we play a little with lastb command, for you that doesn’t know this command is to show listing of last logged in users (just run the man lastb command to check the detail).

we can use this command to check the bad login attempt to our server. problably the brute force attack to our box.
use this command

lastb | awk '{print $3}' | sort | uniq -c | sort -rn | head -20 > bad_login_`date +%F`.log

Server date is wrong and won’t update

My time server is about 6 hours ahead of correct time, i try to use the ntpdate to correct the date

ntpdate 1.rhel.pool.ntp.org
8 Jul 18:27:32 ntpdate[17868]: step time server 180.211.88.50 offset -24375.208870 sec

but when i see the date of the server, it doesn’t change. I even use the change time in the cpanel, but nothing works.

i try to install the ntpd server and run it on my server, but there is no changed in the server’s date. Set the hwclock doesn’t seem to work either

# hwclock --systohc
Cannot access the Hardware Clock via any known method.
Use the --debug option to see the details of our search for an access method.

# hwclock --debug
hwclock from util-linux-2.13-pre7
hwclock: Open of /dev/rtc failed, errno=19: No such device.
No usable clock interface found.
Cannot access the Hardware Clock via any known method.

It’s because my box is a Xen VPS. On a Xen VPS, the time is usually set and synchronized by the host node.

It’s said that if you want to take control of time synchronization for your VPS you can install and run ntpd, but you also need to make a config change within your VPS.

echo 1 > /proc/sys/xen/independent_wallclock

To make the change persistent over a reboot, add the following to /etc/sysctl.conf

xen.independent_wallclock = 1

and after running ntpd on my server, the date seem to be correct.

Yii Error: include(Controller.php): failed to open stream: No such file or directory

i have problem in using Yii 1.1 after generate the webapp using yiic. all controllers failed to loaded. After looking around the quick way to solve it is just add Yii::$enableIncludePath = false; in the index.php on the site root below the require_once($yii); statement. so the index.php should look like:

<?php
/**
* This is the bootstrap file for test application.
* This file should be removed when the application is deployed for production.
*/
// change the following paths if necessary
$yii = dirname(__FILE__) . '/../framework14/yii.php';
$config = dirname(__FILE__) . '/protected/config/test.php';

// remove the following line when in production mode
defined(‘YII_DEBUG’) or define(‘YII_DEBUG’, false);

require_once($yii);
Yii::$enableIncludePath = false;

Yii::createWebApplication($config)->run();

shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

get this error
‘shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory’ when doing stuff on my linux box.

after searching around, the problem is caused by my current directory is not exists. i must be delete it on other console, but my current console is still on that directory.

the resolve is simple go to a valid directory, simple type ‘cd’ will do.


# apachectl configtest
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
Syntax OK
# cd
# apachectl configtest
Syntax OK

Partition full but ‘du’ said otherwise

I’ve find unusual disk usage in my slack box. When i checked using ‘df’ command, it show that one of my partition is full.


# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda5 19G 9.5G 8.0G 55% /
/dev/sda6 28G 21G 5.3G 80% /home
/dev/sda7 42G 36G 3.7G 91% /db
/dev/sda8 37G 35G 0 100% /var/log/app
/dev/sda9 5.7G 4.4G 1.1G 82% /tmp

but when i checked the files in the partition using ‘du’ command the total of files in that partition should not reach 100% of that partition size.


# cd /var/log/app
# du --max-depth=1 -h .
35M ./dl
190M ./web
1.6G ./gtw
4.0K ./mysql
841M ./httpd
1.0G ./app
881M ./op
16K ./lost+found
585M ./backups
5.0G .

see that the partition /var/log/app should not use all 3.5GB.
after asking around, i found the solution for this problem. It seem there are dead/hung processes in my box and it’s locked while still writing to the disk. lsof should do the trick, i need to find process that involve the partition and has delete status in it.


# lsof | grep log | grep delete
httpd 8207 root 44w REG 8,5 0 2188312 /usr/local/apache2/logs/ssl_mutex (deleted)
httpd 22974 apache 44w REG 8,5 0 2188312 /usr/local/apache2/logs/ssl_mutex (deleted)
bearerbox 23141 op 1w REG 8,8 31797665792 261637 /var/log/app/kannel.log-2013-12-07 (deleted)
bearerbox 23141 op 2w REG 8,8 31797665792 261637 /var/log/app/kannel.log-2013-12-07 (deleted)
httpd 24476 apache 44w REG 8,5 0 2188312 /usr/local/apache2/logs/ssl_mutex (deleted)

see that kannel.log look suspicious, after i kill the pid then i get my free space again.

# kill -9 23141

Autoload Problem on Yii 1.1

I’ve installed Yii 1.1 on my computer. after i run
./yiic webapp my/local/www/path
i access the site in my browser.

but i got error
include(Controller.php): failed to open stream: No such file or directory

The Controller.php file is exists in components directory and i check the config/main.php there’is already

// autoloading model and component classes
'import'=>array(
'application.models.*',
'application.components.*',
),

and so i try to change “extends Controller” from the SiteController.php into

class SiteController extends CController
{
.......

and it works! yeaah…

but again when i try to access the Login section, again i got error
include(LoginForm.php): failed to open stream: No such file or directory
but the LoginForm.php is exists in models directory.

From these events i got conclusion that the Yii’s autoload is not working.

From the debug stack i found that error is coming from YiiBase.php, so I check the file and read the line 427 (the YiiBase section that handle the include file)

....................
if(self::$enableIncludePath===false)
{
foreach(self::$_includePaths as $path)
{
$classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
if(is_file($classFile))
{
include($classFile);
if(YII_DEBUG && basename(realpath($classFile))!==$className.'.php')
throw new CException(Yii::t('yii','Class name "{class}" does not match class file "{file}".', array(
'{class}'=>$className,
'{file}'=>$classFile,
)));
break;
}
}
}
else
include($className.'.php');
....................

so the YiiBase got $enableIncludePath that in Yii’s document is

Documented type: boolean whether to rely on PHP include path to autoload class files. Defaults to true. You may set this to be false if your hosting environment doesn't allow changing the PHP include path, or if you want to append additional autoloaders to the default Yii autoloader.

and so there is 2 option for me to do,
1. change the php.ini to allow changing the PHP include path
in order to do so, we need to disable open_basedir directive, in php.ini, open_basedir can be turned off by commenting the open_basedir directive line
you might want to check the httpd.conf too. In httpd.conf, open_basedir can be turned off (e.g. for some virtual hosts) the same way as any other configuration directive with “php_admin_value open_basedir none”.

2. set $enableIncludePath to false
we do this by adding the “Yii::$enableIncludePath = false;” line

.......
require_once($yii);
Yii::$enableIncludePath = false;
Yii::createWebApplication($config)->run();

and i take door number 2, and it’s working 😀

no more connections can be made to this remote computer at this time because there are already as many connections as the computer can accept

So my office shared printer is on a Windows XP Home Edition, and i have read that XP home allows 5 inbound connections.
So because of this limitation some computer can’t connect to this computer and they can’t print.

so the solution for this problem is either upgrade the Windows Edition, or you can manually close and free the active connection session so other connection can be established. To do that:
1. Start –
2. Run
3. type “compmgmt.msc”
4. Expand Shared Folders
5. Expand Sessions
This will list your active sessions.

Close the session you want to kickout, and have fun 😀