Tag Archives: example

Losing “this” scope in JavaScript – Solution

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

Hi….
When using Javascript as OOP mode deep functions (function in function) lose the “this” scope.
I found some solution for this problem…
Same problem i found when you call other class with callback, the callback function returns without “this” scope.

how the problem looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function scope_experiment() {
   
    this.test_var = "test";

    this.level_one = function() {
        alert("Level One: " + this.test_var);

        function level_two()    {
            alert("Level Two: " + this.test_var);
        }

        level_two();
    }
   
}

var test_scope = new scope_experiment();
test_scope.level_one();

The output will be:
1) Level One: test
2) Level Two: undefined

And now…. The solution:
All what you need is to pass “this” to the deep function…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function scope_experiment() {
   
    this.test_var = "test";

    this.level_one = function() {
        alert("Level One: " + this.test_var);

        function level_two(_this_scope) {
            // we use _this_scope as this
            alert("Level Two: " + _this_scope.test_var);
        }

        level_two(this); // we pass this to the function
    }
   
}

var test_scope = new scope_experiment();
test_scope.level_one();

The output will be:
1) Level One: test
2) Level Two: test

Mission Accomplished

Share

Garry`s One Time URL PHP5 Script

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


I open Requirements Specification for Advance One Time URL script.
You can see and help with ideas.

Hi,

I wrote little script + lib for one time url.
this script make MD5 hash string for one time using and redirect file.
the links looks like: http://garry-lachman.com/link/ce75f50f55bcedf0a72098a01764548bĀ and can be used one time only.

The url storing is based on PHP Sessions and link redirection on MOD_REWRITE but there is example
for non MOD_REWRITE using
Example of create of the link:

1
2
3
4
5
<?php
require_once("libs/one_time_url.lib.php");
$one_time_url = new one_time_url();
?>
<a href="<?php echo $one_time_url->make_url("http://www.garry-lachman.com"); ?>">This is one time URL to http://www.garry-lachman.com</a>

The code & example can be downloaded form here.
License: GNU/GPL (open source)

Share

Charts Class that i wrote before 7 years – just found on phpclasses.org – PHP 4

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

Hey….
Just found one of my first classes that i wrote, before 7 years and still popular on phpclasses.org

grchart.class.php

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
<?php
/*
    GrCharts 0.1 pre-alpha.
    ---------------------------------------------------------------
    Under GNU/GPL.
    ---------------------------------------------------------------
    Change log:
    ---------------------------------------------------------------
    12/10/2004 - Added new functions (Style(); and chartsarray();)
    6/10/2004 - First Version: 0.1 pre-alpha
    ---------------------------------------------------------------
    Build by Garry Lachman.
*/

class grchart {
    var $last_id;
    var $total;
    var $precent_array;
    var $chart_array;
    var $style;
    var $log;
   
    function start($value) {
        $this->total = $value;
        $this->log .= "GrChart started with total: $value<br>";
        $this->last_id = "0";
        $this->log .= "Last ID set to \"0\"<br>";
    }
   
    function ch_style($value) {
        $this->style = $value;
        $this->log .="Style set to $value<br>";
    }
   
    function add_chart($value) {
        $current_id = $this->last_id + 1;
        $total = $this->total;
        $precent = ($value/$total) * 100;
        $this->precent_array[$current_id] = $precent;
        $this->last_id++;
       
        $this->log .= "---------------------------------------------<br>";
        $this->log .= "Added chart : <br>";
        $this->log .= "Value: $value <br>";
        $this->log .= "Precents: $precent%<br>";
        $this->log .= "Last ID set to \"$current_id\"<br>";
        $this->log .= "---------------------------------------------<br>";
    }
   
    function init() {
        $last_id = $this->last_id;
        $style = $this->style;
        $i = 1;
        $x = 0;
        while($i <= $last_id) {
            $work_id = $i;
            $work_precent = $this->precent_array[$work_id];
            while($x <= $work_precent) {
                $buffer = $buffer . "|";
                $x++;
            }
            $this->chart_array[$work_id] = $buffer;
            $buffer = "";
           
            $this->log .= "Created HTML code for chart no` $work_id<br>";
        $i++;
        $x=0;
        }
       
        $this->log .= "\n Init complite, all HTML code created <br>";
    }
   
    function style($value) {
        $style = $this->style;
        $chart = $this->chart_array[$value];
        $precent = $this->precent_array[$value];
        switch($style) {
            case red:
                $chart = str_replace("|", "<img src='images/red.gif'>", $chart);
                break;
               
            case green:
                $chart = str_replace("|", "<img src='images/green.gif'>", $chart);
                break;
               
            case pre:
                $chart = str_replace("|", " ", $chart);
                $chart .= "  (" . number_format($precent, 2, '.', '') . "%)";
                break;
        }
        return $chart;
       
    }
   
    function chart($value) {
        $chart = $this->style($value);
        return $chart;
    }
   
    function chartsarray() {
        $last_id = $this->last_id;
        $i = 0;
        $value = 0;
       
        while($i <= $last_id) {
            $style = $this->style;
            $chart = $this->chart_array[$value];
            $precent = $this->precent_array[$value];
            if ($chart <> "") {
                $chart = $this->style($value);
            $chartsarray[$value] = $chart;    
            }
            $value++;
            $i++;
        }    
    return $chartsarray;
    }
   
    function echo_log() {
        return $this->log;
    }

}
?>

Lets test it…

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
<?php
include("grchart.class.php");
$chart = new grchart;

$chart->start(100);

$chart->add_chart("20");
$chart->add_chart("60");
$chart->add_chart("10");
$chart->add_chart("10");

$chart->init();

?>
<table width="100%" border="1">
<TR>
<TD>Red Style</TD>
<?
$chart->ch_style("red");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>

<br>

<table width="100%" border="1">
<TR>
<TD>Green Style</TD>
<?
$chart->ch_style("green");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>

<br>

<table width="100%" border="1">
<TR>
<TD>Pre Style</TD>
<?
$chart->ch_style("pre");
?>
</TR>
<TR>
<td><? print $chart->chart(1) ?></td>
</TR>
<TR>
<td><? print $chart->chart(2) ?></td>
</TR>
<TR>
<td><? print $chart->chart(3) ?></td>
</TR>
<TR>
<td><? print $chart->chart(4) ?></td>
</TR>
</table>
<br>
<?
$chart->ch_style("green");
$ca = $chart->chartsarray();
print $ca[1] . "<br>". $ca[2];
?>

Enjot it ;)
The project on phpclasses.php
http://www.phpclasses.org/package/1934-PHP-Generate-share-bar-charts-in-HTML.html

Have a nice day ;)
Garry Lachman

Share

PHP MySQL Shopping Cart Tutorial in PHP

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

Very good & useful tutorial that i found while search the web about
Abstracting of shopping cart….

“Yes, this is a another shopping cart tutorial. I am planning to make this tutorial to cover a more sophisticated shopping cart solution but for now it only explains a basic shopping cart. I will improve it in time so stay tuned.”

Check if out here: http://www.phpwebcommerce.com/

Have fun,
Garry Lachman

UF4W22PSSX9C

Share

Base64 Encoding Class in ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , , , , . 4 Comments.

This is a class for Base64 Encoding by Steve Webster…
Very Usefull one!!!

Base64.as

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
package {
/*
Base64 - 1.1.0

Copyright (c) 2006 Steve Webster

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/


package {

        import flash.utils.ByteArray;
       
        public class Base64 {
               
                private static const BASE64_CHARS:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

                public static const version:String = "1.1.0";

                public static function encode(data:String):String {
                        // Convert string to ByteArray
                        var bytes:ByteArray = new ByteArray();
                        bytes.writeUTFBytes(data);
                       
                        // Return encoded ByteArray
                        return encodeByteArray(bytes);
                }
               
                public static function encodeByteArray(data:ByteArray):String {
                        // Initialise output
                        var output:String = "";
                       
                        // Create data and output buffers
                        var dataBuffer:Array;
                        var outputBuffer:Array = new Array(4);
                       
                        // Rewind ByteArray
                        data.position = 0;
                       
                        // while there are still bytes to be processed
                        while (data.bytesAvailable > 0) {
                                // Create new data buffer and populate next 3 bytes from data
                                dataBuffer = new Array();
                                for (var i:uint = 0; i < 3 && data.bytesAvailable > 0; i++) {
                                        dataBuffer[i] = data.readUnsignedByte();
                                }
                               
                                // Convert to data buffer Base64 character positions and
                                // store in output buffer
                                outputBuffer[0] = (dataBuffer[0] & 0xfc) >> 2;
                                outputBuffer[1] = ((dataBuffer[0] & 0x03) << 4) | ((dataBuffer[1]) >> 4);
                                outputBuffer[2] = ((dataBuffer[1] & 0x0f) << 2) | ((dataBuffer[2]) >> 6);
                                outputBuffer[3] = dataBuffer[2] & 0x3f;
                               
                                // If data buffer was short (i.e not 3 characters) then set
                                // end character indexes in data buffer to index of '=' symbol.
                                // This is necessary because Base64 data is always a multiple of
                                // 4 bytes and is basses with '=' symbols.
                                for (var j:uint = dataBuffer.length; j < 3; j++) {
                                        outputBuffer[j + 1] = 64;
                                }
                               
                                // Loop through output buffer and add Base64 characters to
                                // encoded data string for each character.
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        output += BASE64_CHARS.charAt(outputBuffer[k]);
                                }
                        }
                       
                        // Return encoded data
                        return output;
                }
               
                public static function decode(data:String):String {
                        // Decode data to ByteArray
                        var bytes:ByteArray = decodeToByteArray(data);
                       
                        // Convert to string and return
                        return bytes.readUTFBytes(bytes.length);
                }
               
                public static function decodeToByteArray(data:String):ByteArray {
                        // Initialise output ByteArray for decoded data
                        var output:ByteArray = new ByteArray();
                       
                        // Create data and output buffers
                        var dataBuffer:Array = new Array(4);
                        var outputBuffer:Array = new Array(3);

                        // While there are data bytes left to be processed
                        for (var i:uint = 0; i < data.length; i += 4) {
                                // Populate data buffer with position of Base64 characters for
                                // next 4 bytes from encoded data
                                for (var j:uint = 0; j < 4 && i + j < data.length; j++) {
                                        dataBuffer[j] = BASE64_CHARS.indexOf(data.charAt(i + j));
                                }
                       
                        // Decode data buffer back into bytes
                                outputBuffer[0] = (dataBuffer[0] << 2) + ((dataBuffer[1] & 0x30) >> 4);
                                outputBuffer[1] = ((dataBuffer[1] & 0x0f) << 4) + ((dataBuffer[2] & 0x3c) >> 2);                
                                outputBuffer[2] = ((dataBuffer[2] & 0x03) << 6) + dataBuffer[3];
                               
                                // Add all non-padded bytes in output buffer to decoded data
                                for (var k:uint = 0; k < outputBuffer.length; k++) {
                                        if (dataBuffer[k+1] == 64) break;
                                        output.writeByte(outputBuffer[k]);
                                }
                        }
                       
                        // Rewind decoded data ByteArray
                        output.position = 0;
                       
                        // Return decoded data
                        return output;
                }
               
                public function Base64() {
                        throw new Error("Base64 class is static container only");
                }
        }
}

Useage:

1
2
3
4
5
var aString:String = "to Convert";
trace(Base64.encode(aString)); // Base64 String

var aBase64:String = "dG8gQ29udmVydA==";
trace(Base64.decode(aBase64)) // Original String

Have Fun ;)

Share

Singleton Pattern in PHP5

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

Singleton is very basic design pattern.
If the instance is inited once than the class return the same instance.

Example of signleton in php5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
Class SingletonExample {
  static private $instance;

  private function __construct() {
  }

  static function getInstance() {
    if(!Self::$instance) {
      Self::$instance = new SingletonExample();
    } else {
      return Self::$instance
    }
  }

  public function doHello() {
     return "Hello World";
   }
?>

Usage Example:

1
2
3
4
5
6
<?
include_once("singleton.class.php");

$single = SingletonExample::getInstance();
echo $single->doHello(); //return "Hello World"
?>
Share

Simple Modules Engine in PHP5

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

I write a really simple module enegine in php5… very easy and nice…

Module Abstract:

1
2
3
4
5
6
7
8
9
<?php
abstract class baseModuleAbstract   {
    protected $moduleName;
       
    protected function setModule($name) {
        $this->moduleName = $name;
    }  
}
?>

Module (mainModule):

1
2
3
4
5
6
7
8
<?php
class mainModule extends baseModuleAbstract
{
    public function __construct(){
        $this->setModule("mainModule");
    }  
}
?>

index.php?module=mainModule

1
2
3
4
5
$module = $_GET['module'];
if (isset($module)) {
    include("modules/" . $module . ".module.php");
}
$loadedModule = new $module();

Thats it…
Its only a example and there is many security issues to need close

Have a nice day :)
Garry Lachman

Share

FMS blocked port detection – ActionScript 3

Written by Garry Lachman (Admin). Filed under ActionScript 3. Tagged , , , , , , , , , , , . 2 Comments.

FMS block port detection – ActionScript 3

The fms port (1935) closed on many office firewalls, the problem
is that flash had 30 sec timeout before tunnle it to port 80.
Becouse that i write little script that use socket object to test the
port and tunnle it to http when port 1935 is closed.

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
package
{
import flash.events.Event;
import flash.net.Socket;
import flash.events.IOErrorEvent
import flash.events.SecurityErrorEvent

/**
* @author Garry Lachman
*
* Usage:
* FMSPortTester.TestPorts(onSuccess)
* function onSuccess(_fmsURL:String):void   {
* }
*/

public class FMSPortTester
{
public static const FMS_URL:String = "127.0.0.1";
public static const PREFIX_1935:String = "rtmp://";
public static const PREFIX_80:String = "rtmpt://";

public static const SOCKET_TIMEOUT:Number = 3;

public function FMSPortTester() {   trace("static only class"); }

public static function TestPorts(_resultURLCallBack:Function):void  {
// Create socket connection to fms to check the ports
var socketTest:Socket = new Socket();
socketTest.connect(FMS_URL, 1935);
socketTest.addEventListener(Event.CONNECT, onSocketConnected);
socketTest.addEventListener(IOErrorEvent.IO_ERROR, onSocketError);
socketTest.addEventListener(SecurityErrorEvent.SECURITY_ERROR,onSocketError);
socketTest.timeout = (SOCKET_TIMEOUT * 1000);

// is connected, fms ports ok
function onSocketConnected(_e:Event):void   {
_resultURLCallBack(PREFIX_1935 + "" + FMS_URL);
}

// on error, fms ports closed
function onSocketError(_e:*):void   {
_resultURLCallBack(PREFIX_80 + "" + FMS_URL);
}
}
}
}
Share