Archive for September, 2009
The Ampersand (&) XML Problem
by z3n on Sep.26, 2009, under Coding
Problem:
Having ampersands (&) on a xml makes it non-compliant, causing errors on IE.
Solution:
I’ve been looking on this issue for a while, although I had the answer already I wanted an alternate. One said that you could turn the & into & html entity or it’s ASCII code, but this just gets into another issue, since the & will still there – & & – In my case I had accents encoded as html entities, like áéíóú, which must be encoded (at least on my project) as entities to be passed as xml/json/etc. Other people noted that it’s possible to have specific entities declared on the html header, although this is a drawback, because xml will become quite big if you have too many different entities AND you will have to redeclare them on every reply.
None of the solutions I’ve found were good, so I’m posting mine.
A simple base64 encode would resolve the issue, and you only need to do it on replies, making things easier, so you send as base64 and the user with a little javascript decodes it back.
Base64 Javascript Source Code:
-
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
-
-
function _enc(input) {
-
var output = "";
-
var chr1, chr2, chr3;
-
var enc1, enc2, enc3, enc4;
-
var i = 0;
-
do {
-
chr1 = input.charCodeAt(i++);
-
chr2 = input.charCodeAt(i++);
-
chr3 = input.charCodeAt(i++);
-
-
enc1 = chr1 >> 2;
-
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
-
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
-
enc4 = chr3 & 63;
-
-
if (isNaN(chr2)) {
-
enc3 = enc4 = 64;
-
} else if (isNaN(chr3)) {
-
enc4 = 64;
-
}
-
-
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
-
keyStr.charAt(enc3) + keyStr.charAt(enc4);
-
} while (i < input.length);
-
-
return output;
-
}
-
-
function _dec(input) {
-
if (input == "NULL") { return ""; }
-
var output = "";
-
var chr1, chr2, chr3;
-
var enc1, enc2, enc3, enc4;
-
var i = 0;
-
-
// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
-
input = input.replace(/[^A-Za-z0-9\+\/\=]/g|>, "");
-
-
do {
-
enc1 = keyStr.indexOf(input.charAt(i++));
-
enc2 = keyStr.indexOf(input.charAt(i++));
-
enc3 = keyStr.indexOf(input.charAt(i++));
-
enc4 = keyStr.indexOf(input.charAt(i++));
-
-
chr1 = (enc1 << 2) | (enc2 >> 4);
-
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
-
chr3 = ((enc3 & 3) << 6) | enc4;
-
-
output = output + String.fromCharCode(chr1);
-
-
if (enc3 != 64) {
-
output = output + String.fromCharCode(chr2);
-
}
-
if (enc4 != 64) {
-
output = output + String.fromCharCode(chr3);
-
}
-
} while (i < input.length);
-
-
return output;
-
}
variable Object MSSQL note
by z3n on Sep.25, 2009, under Notes
Problem:
return $res->Fields['field_name']->Value;
returns `variable Object`
Solution:
return intval($res->Fields['field_name']->Value);
Explanation:
There’s no explanation, this is one of those issues that happens when you deal with microsoft. Sometimes when doing a query it returns the actual value, other times it just return into a different variable type, which needs to be converted. Good thing if you caught it before writing a more complex code and then finally dealing with crypt errors :)
Bonus:
I’ve been getting some more issues even when using this conversion, things like “Object of class variant could not be converted to string php“, this happens on null values, in order to fix i been searching around and found this, which with some changes it will be better like:
-
for ($col=array(),$i=0,$j=$ms_res->Fields->Count();$i < $j;$i++) {
-
try {
-
$value=$ms_res->Fields[$i]->Value;
-
if (trim($value) != "") {
-
if (gettype($value) !== 'string') {
-
if (settype($value,"string")) {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]=$value;
-
}
-
} else {
-
$col[$ms_res->Fields[$i]->Name]='NULL';
-
}
-
} catch (exception $e) {
-
_r("Exception: ".$e);
-
}
-
}
..and yes you will figure out the missing functions/vars.
Script to automatically build dvd / disk compilations targeting maximum usage
by z3n on Sep.19, 2009, under Coding
Problem:
I have lots of files, in lots of folders, to burn into lots of dvds.
I didn’t wanted to pick one by one in order to build the most effective compilation to use the most possible bytes on each dvd-r i would burn them into.
Solution:
I did a little script to do the job, it searches all the subfolders of a given path, and build file compilations in order to fit the maximum possible ammount of bytes. You can even set it to a different limit, for example, if you burn a cd-r or eventually a bd-r or anything else. Folder paths will be respected and files will be put sorted, this is not recommended if you are willing to burn lots of small files, it probably will mess with them distributing across many disks, making it less effective when you actually need those files.
Source:
-
function _o($x) { echo $x."\n"; }
-
function rddir($dir) { /* v2.11 */
-
global $tfile,$tdir,$ij;$i=0;$j=count($tdir);
-
if (is_dir($dir)) {
-
if ($dh=opendir($dir)) {
-
while (($file=readdir($dh)) !== false) {
-
if (is_dir($dir.$file)) {
-
if (($file != ".") && ($file != "..")) {
-
$tdir[$j]=$dir.$file;$j++;
-
} } else { $tfile[$i]=$file;$i++;
-
} } closedir($dh); } } $ij=$i; }
-
-
// main
-
-
if (!isset($argv[1])) { _o("Usage: ".$_SERVER['PHP_SELF']." [move_to]\nEg: ".$_SERVER['PHP_SELF']." path/to/folder path/to/compilation/folder");die; }
-
-
define('move_to',(isset($_argv[2])) ? $argv[2] : "_disk_compilations_");
-
-
// collect file sizes and paths
-
for ($tsk=$tdk=$tsz=$i=0,$files=array(),$sizes=array(),$flag=array(),$tdir[0]=$argv[1];$i 0) && ($sz <= dsize)) {
-
$files[]=$fn;
-
$sizes[]=$sz;
-
$flag[]=1;
-
} else {
-
_o($fn." skipped.");$tsk++;
-
}
-
} elseif ((is_dir($fn)) && ($tfile[$j] != ".") && ($tfile[$j] != "..")) {
-
$tdir[]=$fn;
-
}
-
}
-
unset($tfile);
-
}
-
}
-
-
// build compilations
-
_o("Building Compilations…");
-
while (in_array(1,$flag)) { // loop until there's nothing left
-
for ($k=array_keys($flag,1),$fl=array(),$sz=$i=0,$j=count($k);$i<$j;$i++) {
-
if (($sizes[$k[$i]]+$sz) < dsize) { // search for files that fits/still fitting on this disk
-
$sz+=$sizes[$k[$i]];
-
$fl[]=$files[$k[$i]];
-
$flag[$k[$i]]=0;
-
$sd=move_to."/".$tdk."/".dirname($files[$k[$i]]);
-
if (!is_dir($sd)) {
-
mkdir($sd,0755,true);
-
}
-
if (debug) { // just create a zero byte file
-
touch(move_to."/".$tdk."/".$files[$k[$i]]);
-
} else {
-
rename($files[$k[$i]],move_to."/".$tdk."/".$files[$k[$i]]);
-
}
-
}
-
}
-
$tdk++;
-
$tsz+=$sz;
-
if (debug) {
-
_o(var_export($fl,true)."\nSize:".$sz."\n");
-
}
-
}
-
-
_o("Found: ".bytec($tsz)." (".number_format($tsz)." bytes) in ".number_format(count($flag))." files (".$tsk." skipped)\nIdeal disk count: ".round($tsz/dsize,3)." did: ".$tdk);
-
-
?>
Download Source: Here
Filename vs Folder Linux ext2 Note
by z3n on Sep.15, 2009, under Notes, Tips & Hints
No, you can’t have a filename and a folder with the same name on the same folder.
For example:
/path/to/happy
/path/to/happy/
Will not work. If happy is a regular file and you try to create a folder called happy (and vice-versa) at ‘/path/to’ you will fail.
Note that this amazingly works on windows.
Reset Auto Increment
by z3n on Sep.15, 2009, under Tips & Hints
Problem:
How to reset a auto_increment / identity value on a table?
Solution:
MySQL: alter table tablename auto_increment=1234;
MSSQL: dbcc checkident(‘tablename‘,RESEED,1234);
Microsoft SQL+IIS Migration
by z3n on Sep.04, 2009, under Notes, Tips & Hints
Problem:
Migrate a SQL 2000 DB to a 2005 Server and IIS 6.0 to IIS 7.0.
Extra Issues:
- SQL2000 is full of procedures, views and identity tables (+200 each)
- Coder who built tables/procedures was lame, so there might be NULL values where it shouldn’t among with other things, like STRINGs being converted to DATE, INT, etc.
- Also includes lame asp coding.
- CRYPT MICROSOFT ERRORS (CRITICAL)
More Problems:
I don’t know why people still using microsoft, everything is against it. Not only the OS, but everything (ASP, SQL, VB, IE, Office, etc) related to microsoft is bad.
When you start to get those `error: XXXXXXXXX` like you know you’re screwed, why:
- Error codes aren’t clear and used by many different instances.. guess what, none of them will fit your issue;
- Microsoft KB isen’t accurate;
- Sometimes, Microsoft KB might present you the right answer, but it will be the last one on a list of 10 possible solutions, since they don’t have much control on the crypt errors themselfs, no wonder;
- Microsoft KB likes to do inaccurate automatic translations of the content (if your not english native), meaning that, if you eventually forgot that, you will be lead to another issue caused by the automatic translation.
- MSSQL don’t have `limit x` on delete/update queries, unless you do a huge workaround that will lead to more issues.
- IIS complies with MSSQL, also showing crypt errors, making the debugging impossible for non-computer beings (eg.: you).
- IIS crypt errors can be more funny when it simple shows `Bad Request` or `Application Error` and NOTHING ELSE.
- IIS likes to get stuck, if you try to restart/stop/start/recicle a service that takes too much time to happen, IIS manager simply shows a message `Service not responding` and abort the operation, sometimes it abort the operation and a few minutes later it happens, other times it just don’t happen and/or get stuck then you need to reboot;
- Copying database straight from the 2000 server to a local 2005 server gives the following error: `Error, please check your log`; Checking `your log` gives lots, lots of crypt non-sense errors in my native language, guess what? 0 results on google.
- The error above takes 30 minutes to happen, nothing is copied, but there’s a 300mb .mdb file.
- If you do a `SCRIPT DATABASE` you will not get the whole data or/and tables will not have identity columns or/and some data may be corrupted (even deatched);
- If you do a `IMPORT/EXPORT` procedure, you will fall on the VERY SAME problems above, but now you may also get timeout crypt message;
- You can only edit top 300 lines on MSSMS, sorted by MSSMS’ will;
- [db_name].[dbo].[tbl_name] will not appear on the automatic generated scripts, but if you forget to add it your script will miserabily fail with a non-crypt error message (first time) — that only happens if you use MSSMS;
- Can’t stand that green circle looping;
- MSSMS 2005 and 2008 are about the same, meaning you will have the same errors, so don’t even try to update;
- SQL2005 SP3 must be installed before anything, unless you want to fall on unsolved crypt errors, leading to nothingness;
- You can also amazingly fall into crypt errors with ALSO typing errors like `erro XXXXXXXXX`;
- Screaming to the skies and throwing things around will not help;
- Reinstalling SQL Server will not help;
- Reinstalling SQL Server with latest SP will not help;
- Reinstalling OS will not help;
- Reinstalling OS with latest SP will not help;
- Reinstalling <Microsoft Software> with <Any Condition> will not help;
- IIS will not forgive and forget any `;` you put on a asp page, too bad for mainly php coders (eg.: me);
- IIS is for Apache as IE is for Firefox (is this understandable in english? don’t think so);
Another Complaint:
The triumph of a bad software can’t be explained for me, I can’t also understand why they made so much money on something that simply dosen’t work. Maybe users like
things that don’t work? Maybe there’s something magic on crypt errors and blue screens? Maybe Microsoft is paying developers to use their languages and/or software?
Solution:
After several days, tries and lots of time lost I was able to do the migration using MSSQL Enterprise 2005 SP3, the Developer, Express, SQL 2008, SP1 and SP2 gave me crypt errors when copying the database, nothing related to installing the latest SP was on the KB.
Note that i installed ALL versions to try. Thanks microsoft for such great support and programs, looking forward to support the selling of pirated microsoft dvds on streets.
Solution 2:
Move to LAMP, please.
MS SQL 2005 Express per Instance Config Note
by z3n on Sep.02, 2009, under Notes
Problem:
How to run more than one ms sql server version in the same machine?
How to change authentication to MIXED MODE without reinstalling the whole thing?
Solution:
Install with different instances. To connect you will use .\INSTANCENAME or SERVERNAME\INSTANCENAME
To change the authentication method, you need to edit the registry, no wonder it’s another obscure cryptic thing:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.1\MSSQLServer]
“LoginMode”=dword:00000002
dword 1 means windows authentication, while 2 is the mixed mode, just change it and restart the server.
This key is for MS SQL 2005 EXPRESS.
Apache Setup Note
by z3n on Sep.01, 2009, under Notes, Tips & Hints
Problem:
403 / forbidden on any file, even if the permissions and user / group are right.
Solution:
First check httpd.conf to see if the User and Group statements are compatible with the user you’re trying to access.
“No, you can’t add root there, you idiot.”
After that make sure that the folder /home/<username> is actually owned by the same user and group (or same/lower rights) of the user you’re running apache with.
To make sure you should restart apache completly.
“httpd -k stop;httpd -k start”