viernes, 11 de septiembre de 2009

C Coding Practice

I'm definetly getting into the C wagon, many of you will ask why would I want to get aboard a language that it's certainly difficult to master, and my answer is simple, It's just because most of the software, programming languages, etc that we use today come from this very same language.

Most of today's programming languages such as PHP dont provide the programmer with enough background about memory allocation, etc.
And certainly when it comes to threading, using multiple sockets, nothing beats C!

Being curious enough these days I've learnt about dynamic memory allocation with pointers which is *very* cool. Before I knew dynamic memory allocation I always thought about the problem of fixed size arrays, my thought was something like this

"If I always have to pass the size, that is incredibly stupid, I'm sure theres a way to solve this out, like, if my program would take input from a user, I would like that input to be dynamic and not static! I dont want to force her/him to only be able to write 100 bytes!"

I've been always curious about it and now I really want to get to know it, so here we go!

This is a simple program that illustrates memory allocation, by reading a file of any length. Might have some trouble, but it has definetly worked for me :)


#include <stdio.h>
#include <stdlib.h>


//This is the size of the buffer
#define LENGTH 2048

char* readfile(const unsigned char* path, unsigned long length);
void minimize(char* str);

main(int argc, char* argv[]){

char* cur;

while((cur=readfile(argv[1],LENGTH))!=NULL){

printf("%s",cur);

}

return 0;

}

char* readfile(const unsigned char* path,const unsigned long length){

static FILE* fp;
static unsigned long int offset = 0;

if(!fp){

fp = fopen(path,"r");

}

if(!fp){

puts("Cannot read file");
return 0;

}

if (!fseek(fp,offset,length)){

puts("Done");
fclose(fp);
return NULL;

}

offset+=length;

char* read = malloc(length);

if( (fgets(read,length,fp)) == NULL){

fclose(fp);
return NULL;

}

return read;

}

PHP Security Basics

I really suggest you to watch THIS VIDEO, It covers the basics on PHP security.

viernes, 21 de agosto de 2009

uoOgle

uoogle! makes it easy for searching google from your PHP application. This does it for the new google search API (which uses JSON). It even includes smarty for making your searches templatable, as well as a few self explanatory examples. The code is fully OO.
Download

martes, 28 de julio de 2009

SSH'ing through Java

Today i ran into a major problem at work. I was supposed to access a server through ssh to get work done. However it was impossible for me to get something decent like putty, or other windows ssh program or any program at all since they forbid one to download any executable files at least for windows that is (IT policies *cough* bureaucracy), so I reminded an application from mindterm that I had used quite a few years ago. I was amazed to see that the application has come to be something really cool these days. Without further words, if you ran into the same situation that i did today:

Click here http://www.netspace.org/ssh

lunes, 27 de julio de 2009

Javascript Valid IP

Validate an IP address with javascript:
Its just a regular expression, so it should work in PHP, or any language supporting
regular expressions, however most languages have ip validating functions, such as ip2long or long2ip in PHP :)

ipaddr = {
validate:function(ip){
var ip = new String(ip);
return (ip.match(/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/));
}
}

Usage:

< input type="text" name="ip" onblur="ipaddr.validate(this.value);" >

jueves, 23 de julio de 2009

Javascipt Tree with Jquery and Ajax

Just so i dont forget to post it and explain further when i have sometime!
Jquery is used for performing the ajax part and the show/hide effects
although i could've used yaguajax (http://code.google.com/p/yaguajax) I decided to use jquery because of the showing/hiding effects, well anyways, this is a tree that takes a JSON object as parameter 1 (data), ill provide more examples in the future!

CSS
-----------------------
ul li a {
}
.level {display:none;}
.level1{display:none;}
.level2 {display:none;}


JAVASCRIPT
-----------------------
tree={branches:[],parse:function(data,id,fieldName,lvlPrefix,lvlField,lvlClass,jsFunc,branchTitle){var div=document.getElementById(id);var ul=document.createElement("ul");if(branchTitle){var li=document.createElement("li");li.setAttribute("class","treetitle");li.appendChild(document.createTextNode(branchTitle));ul.appendChild(li);}if(!this.hasbranch(id))this.branches[id]={data:data,expanded:false};for(i=0;i<data.length;i++){var val=data[i][fieldName];var lvlID=lvlPrefix+data[i][lvlField];if(!val) continue;var li=document.createElement("li");var a=document.createElement("a");var txt=document.createTextNode(val);var lvl=document.createElement("div");lvl.id=lvlID;lvl.setAttribute("class",lvlClass);a.appendChild(txt);a.href="javascript:"+jsFunc+"("+$.toJSON(data[i])+");";li.appendChild(a);li.appendChild(lvl);ul.appendChild(li);}div.appendChild(ul);this.expandContract(id);},hasbranch:function(id){return (!(typeof(this.branches[id])=="undefined"));},expandContract:function(id){if(!this.hasbranch(id))return false;if (this.branches[id].expanded){$("#"+id).hide("slow");this.branches[id].expanded=false;return;}$("#"+id).show("slow");this.branches[id].expanded=true;return;}}

lunes, 13 de julio de 2009

Upload Progress Bar 100% PHP

Past weekend I entertained myself trying to figure out how would I go doing a 100% PHP upload progress bar without using iframes.

At the start my premise was this:

PLAIN PHP (Trying not to use extensions!)
PLAIN AJAX UPLOAD (Yes, the upload would've been done ALL via AJAX)
MULTIPLE FILES MULTIPLE UPLOADS MULTIPLE PROGRESS BARS! Yeah, Multiple!

Doing a bit of research I found out that the file object in javascript had some interesting properties like the following:

<input id="file" type="file" name="blah" onchange="interesting(this);">

function interesting(elm){

alert(elm.files.item(0).fileSize); //Gets the filesize in bytes for the loaded file
alert(elm.files.item(0).getAsDataURL()); //Gets the whole file as a base64 encoded string

}

As you might guess I've found the second (elm.files.item(0).getAsDataURL()) very interesting,
so the main idea was, "If i can get the file as a base64 encoded string I can send it through ajax without any problems, write to a file from PHP, get the amount of bytes written, then get a percentage and make a progress bar in a snap hah! Yeah I dont know about you but in my case I always start happy and then begin to complicate things :) (I think its a fact related to the very existence of programmers?)

However there were certain complications trying to send this data as $_POST data through AJAX.

Most of the time my struggle was based on getting $_POST data "as it arrived", now, "as it arrived" meant data to be dumped in the *very moment* as it was coming IN
I tried reading with $fp = fopen("php://input",'r'); while(!feof($fp)) $line = fgets($fp,2048); amongst other techniques like trying to use output buffering, but no luck. Later on I did some more research and found out that PHP is not able to read POST data in the manner as I wanted (as some kind of stream flushing it in as it arrived).

So, everyone pointed out me to use APC upload progress functions, but i didn't felt like installing apc just for getting a way of polling for upload progress, thats when I stepped with uploadprogress simply by doing :


pthreat@localshot:~$ pecl search uploadprogress

Retrieving data...0%Matched packages, channel pecl.php.net:
=======================================
Package Stable/(Latest) Local
uploadprogress 1.0.1 (stable) 1.0.1 An extension to track progress of a file upload.

Riiiiiiight, who would've thought so!!


I just had to do the following:

sudo su -c "pecl install uploadprogress";

Add the extension to /etc/php5/apache2/php.ini

echo "extension=uploadprogress.so" >> /etc/php5/apache2/php.ini

Then i checked out the examples available in the extension, and i came up with the following:

DEMO RIGHT HERE!

You can get the code here:

CODE RIGHT HERE!

I've called this uprogress, I guess i didnt wanted to be original this time :)

I still have to tweak a coupple of things, but it works in the overall.

sábado, 4 de julio de 2009

ZF Tool

Today I woke up, and decided to make a small project with Zend Framework, some kind of a photo album. I wanted to make the whole thing modular and all, I've got to a TUTORIAL that sort of explained howto use modules, with ZF.After some workarounds i managed to get it "working", Just to find that Zend_Tool was using Short Tags to generate code. As you all know (or most of you should) Short tags are meant to be deprecated in PHP6.

This bug has been filed by me here: http://framework.zend.com/issues/browse/ZF-7192

Here's some of it:

From Zend/Tool/Project/Context/Zf/ViewScriptFile.php:
Starting in line 127:

<h2><?= \$this->message ?></h2>

From my php.ini file :)

; Allow the <? tag. Otherwise, only <?php and <script< tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.

short_open_tag = Off

miércoles, 1 de julio de 2009

Simple Javascript File Adder

Bored, made this simple file adder, actually you can add text fields also, I should add options into it for it to be able to also remove the created input fields.

files={elms:[],count:0,add:function(id,name,type,max,prefix){if(max&&((files.count+1)>max))return;var elm,cont,label;elm=cont=label=null;cont=document.getElementById(id);type=(type) ? type : "file";if(!cont) return alert("addboxes.js: Container for adding elements does not exists");name=(!name) ? "files" : name;elm=document.createElement("input");elm.setAttribute("name",name+"[]");elm.setAttribute("type",type);label=document.createElement("label");label.setAttribute("class","autoadd");if(prefix) label.appendChild(document.createTextNode(prefix+' '+(files.count+1)));label.appendChild(elm);cont.appendChild(label);files.elms[files.elms.length]=elm;files.count++;}}/*Example Usage: <a href="javascript:files.add('emails','reviews','text',4,'Review #');">[+]Add Email</a> <div id="emails"></div>*/

martes, 16 de junio de 2009

Zend Gdata Maps

Using Zend Framework checked the Gdata package and found no google maps support:

So i decided to create a Zend_Gdata_Maps subpackage, and here it is:

Download Zend_Gdata_Maps!

Installation: Just uncompress it outside The Zend Framework folder

See the Examples provided on Zend/Gdata/Maps/Examples

yeah, the examples are *practical* and *usefull* you wont find any useless gibberish on them

Run the example within your console OUTSIDE the ZF folder!

/path/to/php Zend/Gdata/Maps/Examples/example.php



Once youre used to it, use it in your project as any other library.

There's lot to be done yet! I'm sure there are a thousand of bugs.

Looking up for people who care to create a Google Maps Gdata Subpackage :)

Cheers!

Juan.-

lunes, 8 de junio de 2009

Zend Framework 1.8 PDO_MYSQL

The Quickstart provided on the Zend framework page does not specify howto use PDO_MYSQL
and as you already have noticed it uses PDO_SQLITE.

However this is not the most common option for people who are going to build serious RDBM applications, so after wondering a coupple of minutes I just used this on my configuration file

application/configs/application.ini

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[]=

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "YOUR HOST GOES HERE USUALLY LOCALHOST"
resources.db.params.username = "DATABASE USERNAME DONT BE A NOOB AND USE ROOT"
resources.db.params.password = "YOUR USER'S PASSWORD"
resources.db.params.dbname = "DATABASE NAME"
resources.db.isDefaultTableAdapter = true


I gope this helps anybody out there ...

Cheers!

jueves, 4 de junio de 2009

Cannot map the project with svn provider

So i began using eclipse ganymede seriously lately, downloaded and installed the subclipse plugin, checked out a project from our existing project repository, everything seemed to be cool, until i wanted to checkout a different project in the same server.

Apparently the subclipse plugin was using the same data of my previous repository, which as I stated, was hosted in the same server.

QUICK FIX! (it works)

I tricked eclipse by adding an alias to the host in my /etc/hosts file, simple as:

200.xxx.xxx.xxx otherproject.mycompany.com

So, when importing the project, i used this alias, and then i was prompted again with username and password for the other repository that i needed to checkout.

Tried googling for this one altough found a lot of noise and nothing concrete.

Comments?

lunes, 9 de febrero de 2009

Random Thought

The more i live the more I see this world is meant to be hell for some and heaven for others.
So evident and tangible as any philosophical thought, yet hidden for many ...

lunes, 2 de febrero de 2009

Vuln research? Programmers ? Freelancers ?

It has caught my eye in these years that every single day that I check sites like security focus or packetstorm, every single vulnerability ends up being some SQL injection gibberish for some unknown CMS or some dumb forum.

I'll just go straight to the point, because i really like to be straight forward.

The questions are:

A) Are we lacking serious security researchers?

B) Has the world gone so stupid that we have people inventing terms like web 2.0 and looking for SQL injection vulnerabilities, and then claiming to be big?

C) Are there so many stupid web programmers?

I believe the right answer is a mixture between B and C.

Sometime ago i was working as a freelancer. My work consisted into modifying a couple of web apps made by some guys in some part of the globe.
Making my way through horrendous programming manners. 50 lines of elseif just for getting the correct location to redirect to. Design patterns where too much to ask, there were no traces of a design pattern standard such as MVC and clearly also no OOP but a DB PDO Object. When you see code like this:

function test ($a=1) {

if ($a) {

return TRUE;

} else {

return FALSE;

}

}

You start to get seriously worried.

It's a real shame that web developers these days are mostly unexperienced beings with little, poor or no background knowledge like C or even Shell scripting.

Their way of fixing problems is to *patch* all the way through, doing whatever it takes. Doesn't matters how nasty the patch is, they don't mind if you or me have to code over that later.

These careless people exist thanks to following characters:

A) Customers that think that serious web development is a matter of point and click.
B) People with Microsoft's NIKE philosophy "Just do it" add to that "No matter what".
C) People that still code for PHP4.
D) People who have little or no vision at all about the future of their application.
E) People who play to be experts when all they have is a bunch of fancy words.

Now, if you're even thinking that I should be grateful to these people because they give me work, think twice. I've left freelancing for modifying applications, and I've left my job thanks to people like the ones mentioned in points A, B, D and E (<-mediocre) to get another one in which I'm able to work happy (which I already have).

So, if you happen to be in some of the previous mentioned situations, and feel frustrated of doing the same stupid stuff over and over again just leave everything behind and start a new path.

jueves, 29 de enero de 2009

First entry, wow!

Hey there! Greetings! (altough i may not know who the fuck you are , but anyways :) ) .

I'm Juan Pablo Stange, 25 born in Rosario Argentina, currently working in Buenos Aires. What do i do for a living ? Well, I'm a PHP / Javascript / SQL / HTML / XML / CSS programmer, in a short term, a so called "web developer".

Thursday 29 14:25 P.M Buenos Aires, Argentina. At work here in Source South.

After meditating a lot, I decided to create this blog as a way of storing wacky thoughts ideas, source code, utilities and certainly about music. And I prefer not to be good introducing myself, (I could be, really) but I'll let you get your very own wrong conclusions.
See ya around.

Juan.-