Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert from Javascript to protect my code 1

Status
Not open for further replies.

tav1035

MIS
May 10, 2001
344
US
I have some code that I would like to hide and keep people from doing a "file- save as" and running this web application on their client. I am a newbie so I'm just learning. I already have a server with php, cgi, etc..
My code basically converts any spaces, returns, tabs ect. to ',' and counts them.

Any ideas would be appreciated.

Code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function convertSpaces(str)
{

str=str.replace(/\r/g,"")
str=str.replace(/\s/g,",")
str=str.replace(/\,,*/g,",")
str=str.replace(/\''*/g,"")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/\,,*/g,"','")
str=str.replace(/^','/g,"")

return str;
}
//  End -->
</script>
<!-- END CONVERT SPACES -->

<!-- BEGIN TEXT AREA 1 -->
<textarea name="user" rows="10" cols="35" onBlur="this.value 

= 

convertSpaces(this.value);countWords(this.value);"></textare

a>
<!-- END TEXT AREA 1 -->

<!-- COUNT RECORDS -->
 <SCRIPT language=javascript type=text/javascript>
function countWords(strVal){
  var strArray = strVal.split ("','");
  document.getElementById('cnt').innerHTML = 

strArray.length;
}
 </SCRIPT>

<div align="center">
Record count is  <SPAN id=cnt>0</SPAN></div>
<!-- END COUNT RECORDS -->
tav
 
Not sure why you want to do that.

But you can make a seperate javascript file (.js) and keep it outside your web root.Then include the javascript file in your web application file like below.
E.g.
Code:
<script language="javascript" src="d:\\hello.js">
</script>

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
spookie,
Sorry for not explaning very well, I could do your recommendation, however anyone can still download your javascript file. I would like to change this to a serverside script.

Here is a crude example of my application.


I was thinking of changing the "convertSpaces.js" to php?
Code:
<?php
  $find[] = 'œ';
  $find[] = '?';
  $find[] = '˜';
  $find[] = '™';
  $find[] = '¦';
  $find[] = '”';
  $find[] = '“';
  $find[] = ' ';

  $replace[] = '';
  $replace[] = '';
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "','";


  $text = str_replace($find, $replace, $text);
?>
Not sure how to do it or how to add it to my webpage.
Any ideas?
tav
 
The Javascript is a client-side language, right?
So a browser must have a script code to execute it.
A browser runs on a client (user) computer.
Well, can you prevent browser (i.e. user) access to Javascript code?
Yes, you can: don't use Javascript at all...
 
ArkM ,
I'm trying to stop using javascript and convert it to serverside. I thought my post above explained that (I would like to change this to a serverside script),(I was thinking of changing the "convertSpaces.js" to php?)
hmmm, you didn't tell me anything new. I would like to have a serverside code that would function the same as the above, I'm just not sure how to set it up.
tommy

 
i agree with spookie. use str_replace as you have done above.

in order to get the script to interact with your webpage from the client side you will need to use ajax. i recommend you take a first look at the sajax library at
the code can be as simple as putting this at the beginning of your current file:
Code:
<?
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();

function parse_spaces($str) {
  $find[] = 'œ';
  $find[] = '?';
  $find[] = '˜';
  $find[] = '™';
  $find[] = '¦';
  $find[] = '”';
  $find[] = '“';
  $find[] = ' ';

  $replace[] = '';
  $replace[] = '';
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "','";


  return str_replace($find, $replace, $str);
}

//now your normal page generation code

and in the head of your document (i.e. between the <head> </head> tags) cut and paste the code below

Code:
<? sajax_show_javascript(); ?>
function getcleanseddata() {
  var elem=document.getElementById("containerdiv");
  x_parse_spaces(elem.innerHTML, toggle); //or elem.value if a field
}
function toggle(val) {
  var elem=document.getElementById("containerdiv");
  elem.innerHTML = val; // or elem.value if a field
}

obviously you need to set some action on the page to trigger the getcleanseddata js function. you should also change the containerdiv id to whatever the actual container is called whose contents you wish to parse.


hth
Justin
 
jpadie,
I added everything to my html page.

I downloaded the sajax-0.12 and unzipped it.
What do I do with the download? I set the Sajax.php file in the same directory as my html page.

I changed both ("containerdiv") to ("user"), which is my textarea's NAME and ID.

You said "I would need some action on the page to trigger the getcleanseddata js function", I have been using onBlur and has been working pretty well, but how should it look now-
Code:
<textarea name="user" rows="10" cols="35" onBlur="this.value=convertSpaces(this.value);countWords(this.value);"></textarea>
tav
 
i'd suggest
Code:
<textarea name="user" rows="10" cols="35" onBlur="getcleanseddata();countWords(this.value);"></textarea>

i'm not sure whether the countwords function will wait for the getcleanseddata function to finish. if it does not you might move the call to countWords (which i guess just pops up an alert or changes some html?) to the toggle function.
 
jpadie,
I can't get it to run.
Can't get the getcleanseddata to fire.
Question: where do I add the following code to? and if it's a php file, what do I name it?
Code:
<?
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();

function parse_spaces($str) {
  $find[] = 'œ';
  $find[] = '?';
  $find[] = '˜';
  $find[] = '™';
  $find[] = '¦';
  $find[] = '”';
  $find[] = '“';
  $find[] = ' ';

  $replace[] = '';
  $replace[] = '';
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "";
  $replace[] = "','";


  return str_replace($find, $replace, $str);
}

//now your normal page generation code
Anymore ideas?
tvondra hotmail
 
you put the above code into the top of the same file that you are using to generate the text boxes. if it was an html file, rename it as a php file. literally:

Code:
<? 
insert the php code
?>
old html code

if it is still not working there is something wrong with the cut and paste. post the code of your whole script back here and I'll take another look.
Justin
 
jpadie,
Ok it's works, I was missing a closing script tag.
You get a star for helping me set that up.

However, my code find, replace code isn't very sound.
I would like to covert all spaces & carriage returns to ',' (simi-quote, comma, semi-quote) and remove leading & trailing ',' characters if there are any.
My original code is js was->
Code:
function convertSpaces(str)
{

str=str.replace(/\r/g,"")
str=str.replace(/\s/g,",")
str=str.replace(/\,,*/g,",")
str=str.replace(/\''*/g,"")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/(,*),$/,"$1")
str=str.replace(/\,,*/g,"','")
str=str.replace(/^','/g,"")

return str;
}
the syntax code will not work in PHP?, but worked well as js.

Now I'm using->
Code:
<?
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();





function parse_spaces($str) {

  $find[] = "\n";
  $find[] = "\r";
  $find[] = " ";
  $find[] = "','','";
  [b]$find[] = "^ ',' $";[/b]

  $replace[] = "','";
  $replace[] = "','";
  $replace[] = "','";
  $replace[] = "','";
  [b]$replace[] = "";[/b]

  return str_replace($find, $replace, $str);
}

//now your normal page generation code
?>
Which works but, I can't get the start and finish of string to work which is in bold above. I want to trim it.
Any ideas would be appreciated.
tav
 
the trim function should cater for the start and finish. try this:

Code:
function parse_spaces ($str) {
$find = array (" ", "\r\n", "\n");
$replace = "','";
return trim(str_replace($find, $replace, $str), "','");
}
 
jpadie,
Your last suggestion works, however, if I have several "spaces" in a row or several "new lines", it replaces with too many delimeters (','). They will still run in SQL but still not clean.
ie.)
text-> mmm mmm mmm
result-> mmm','','mmm','','','mmm
ie.)
text-> mmm


mmm

mmm
result-> mmm','','mmm','mmm

I can't get the * or $ to work here.
Any ideas, it's so close.
tav
 
if you don't want to have two commas in a row (or more), do a str_replace for "','','" to "','" and keep doing it until no further replacements are done (i haven't tested this)

Code:
function finaltrim ($str) {
 $cnt = 0;
 $newstr = str_replace ("','','", "','", $str, $cnt);
 if ($cnt > 0) :
  finaltrim ($newstr); 
 else:
  echo $newstr;
 endif;
}

i may have misunderstood the issue, however.
 
jpadie,
Not getting the final trim to run. Hopefully I added it in the right place. Here is where I added it. Do I need to add anything else to get it to run?
Code:
<?
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();

function parse_spaces ($str) {
$find = array (" ", "\r\n", "\n");
$replace = "','";
return trim(str_replace($find, $replace, $str), "','");
}
function finaltrim ($str) {
 $cnt = 0;
 $newstr = str_replace ("','','", "','", $str, $cnt);
 if ($cnt > 0) :
  finaltrim ($newstr); 
 else:
  echo $newstr;
 endif;
}


//now your normal page generation code
?>
<html>

Thanks,
tav
 
sorry - i was giving you the method rather than the solution. should have made that clear. the finaltrim is a recursive function.

Code:
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();

function parse_spaces ($str) {
$GLOBAL["cleanstring"] = "";
$find = array (" ", "\r\n", "\n");
$replace = "','";
$str = (str_replace($find, $replace, $str), "','");
$str = finaltrim($str);
return trim($GLOBAL["cleanstring"]);
}
function finaltrim($str) {
$GLOBAL["cleanstring"] = $str;
$cnt = 0;
$GLOBAL["cleanstring"] = str_replace ("','','", "','", $GLOBAL["cleanstring"], $cnt);
 if ($cnt > 0) :
  finaltrim ($GLOBAL["cleanstring"]);
 else:
  // 
 endif;
}
 
jpadie,
Sorry, I'm getting an error...
Parse error: parse error, unexpected ',' in /hsphere/local/home...php on line 11
I tried changing things up a bit with still no luck.
I tried replacing the : with ; and still won't run.
thanks
tav
 
typed it straight in to the input box. try this
Code:
<?
require_once ("Sajax.php"); //download this from [URL unfurl="true"]www.modernmethod.com/sajax[/URL]
sajax_init();
sajax_export("parse_spaces");
sajax_handle_client_request();

function parse_spaces ($str) {
$GLOBAL["cleanstring"] = "";
$find = array (" ", "\r\n", "\n");
$replace = "','";
$str = str_replace($find, $replace, $str);
$str = finaltrim($str);
return trim($GLOBALS["cleanstring"]);
}
function finaltrim($str) {
$GLOBALS["cleanstring"] = $str;
$cnt = 0;
$GLOBALS["cleanstring"] = str_replace ("','','", "','", $GLOBALS["cleanstring"], $cnt);
 if ($cnt > 0) :
  finaltrim ($GLOBALS["cleanstring"]);
 else:
  //
 endif;
}
?>
 
I know this is probably too late for any use but for future consideration, you might want to create a PHP script that will output only to your site by checking the referrer.

JavaScript Example:
Code:
<script language="JavaScript" src="[URL unfurl="true"]http://www.yoursite.com/js.php">[/URL]

PHP Example:
Code:
<?php

    // Send content-type header.
    header( "Content-type: text/html" );

    // Get referer.
    $ref = parse_url( getenv('HTTP_REFERER') );

    // Check referer.
    if ( $ref['host'] !== "[URL unfurl="true"]www.yoursite.com"[/URL] )
    {
        // Show JS safe message.
        ?>
<!-- Begin

// Access denied.

// End -->
        <?

        // Stop here.
        exit( );
    }

?>
<!-- Begin

alert( 'It loaded!' );

// End -->

The PHP code has been tested so you can just copy, paste, and edit the code. It will only allow your site access to the JavaScript.

---------------------------------------
TINSTAAFL, which is why I contribute.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top