Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Login and Submit Text to ASCIIBin (sample PHP and cURL script)

login design pattern ascii

  • Please log in to reply
3 replies to this topic

#1 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 17 May 2009 - 10:34 AM

I wrote this for CCLogBot. I figured it would be beneficial and (hopefully) encouraging to display this information for any developers that wanted to incorporate a method to submit data from their software to ASCIIBin.

Post Variables
ASCIIBin follows the MVC architectural design pattern. As such, it uses a front controller so all actions are issued through index.php. This means all of your submissions should be to: ASCIIBin [Code and Text Collaboration for Debugging] - ASCIIBin[/URL]

Here are the [U]BIN Submission Variables [/U]available:

[LIST]
[*][B]a[/B] -> Action. Defaults to main page if blank. Use [B]add[/B] to submit a new bin
[*][B]p[/B] -> Private. Set to [B]on[/B] to make public. Default is off
[*][B]l[/B] -> Sets the bin to a Link. Set value to [B]on[/B] if submitting a link. Link must contain a valid URL and only a URL. No other text. [B]p [/B]will be set to off when this vairable is on.
[*][B]type [/B]-> This flag determines the output/confirmation of submitted text. Valid values are [U]text[/U], [U]xml[/U] or [U]html[/U]. HTML is default.
[*][B]tags[/B] -> Tags, seperated by commas
[*][B]pasteinfo[/B] -> The actual information you wish to paste. This is the textarea name. Cannot be blank
[*][B]emailself[/B] -> Send a reminder to yourself. you must be logged in for this function to work.
[*][B]emaildelay[/B] -> If emailself is on, use this variable to set the amount of time to delay before sending the email reminder. The value is in days and must be an INT.
[*][B]passOn[/B] -> Set this variable to [B]on[/B] if you would like to password protect your BIN submission.
[*][B]passtext[/B] -> The password to set if passOn is enabled
[*][B]subscribe [/B]-> Set to [B]on[/B] to subscribe to BIN comments. You will receive an email for each comment. User must be logged in.
[/LIST]


[U]Login Variables:[/U]

[LIST]
[*][B]a[/B] -> Action. Set to "[I]login"[/I]
[*][B]sublogin[/B] -> Secondary process action. Set to [B]1[/B]
[*][B]user [/B]-> Username
[*][B]pass [/B]-> Password
[*][B]remember[/B] -> Set to [B]on[/B] if you would like your session to be remembered. This will set a cookie.
[/LIST]
[SIZE=4][B]
cURL Script[/B][/SIZE]
First, we do our basic cURL setup. I'll not go into detail about the functions of cURL. You can read about that here: [URL="http://www.php.net/curl"]PHP: cURL - Manual[/URL]

[php]$curl_connection = curl_init('http://www.asciibin.com/index.php');
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
[/php]You can see above we set the URL to [URL="http://www.asciibin.com/index.php"]ASCIIBin [Code and Text Collaboration for Debugging] - ASCIIBin[/URL]. You must set a cookiejar and cookiefile in order to login. The rest you can read about yourself from the PHP Manual. :)

[U][B]Login[/B][/U]
The following will show you how to login using cURL. If you wish to submit BINs anonymously, skip to the next section. To login we will use this query string:

[php]user=<username>&pass=<password>&sublogin=1&a=login[/php]Here is how:

[php]/*
* Login
*/
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "user=<username>&pass=<password>&sublogin=1&a=login");
curl_exec($curl_connection);
[/php]This will submit a login request and actually write your session data to the cookie.txt file, set above. It will look like this:

[code]www.asciibin.com FALSE / FALSE 0 PHPSESSID 5e4223f348321e6328d58863b9870aa9[/code][B]

[U]Submit BIN[/U][/B]
Now it is time to submit/create the BIN. To submit a bin, we need to send variables [B]a [/B]and [B]pasteinfo[/B]. You can include any of the variables from above as well. In this example I've included [B]a, p, tags[/B], [B]pasteinfo[/B] and [B]type[/B]. You might want to include type in yours as well in order to parse the response better.

[php]/*
* Submit Text now
*/
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "a=add&p=on&tags=codecall,tutorial&pasteinfo=This is a Tutorial&type=text");
$result = curl_exec($curl_connection);
[/php][I]$data[/I] is a variable set earlier which contains the submission text.

[I]pasteinfo[/I] can include BBCode. Code blocks should be wrapped with the code tag in this format: [noparse][code=language]// code[/code][/noparse]. ASCIIBin uses GeSHi so it supports all of the languages found here: GeSHi - Generic Syntax Highlighter :: Home (look along the left hand column).

$resultnow contains your data. Since I chose a 'text' return type, it will contain the URL of the newly submitted bin.

NOTE: If any of the text you submit contains an &, it will need to be URL encoded. You can do that in PHP using urlencode().

Closing cURL
You need to close your cURL connection now:
curl_close($curl_connection);


Problems

If you are having problems connecting/submitting to ASCIIBin, you can print out the curl connection results:

print_r(curl_getinfo($curl_connection));
This should give you an idea of the problem. If you need any help or assistance, please feel free to ask me.

Full Code
Since this is a promo for ASCIIBin, I've pasted the full code for the submit function on ASCIIBin. You can tear through it however you like.

[URL="http://www.asciibin.com/QIL"]Full Source - ASCIIBin
  • 0

#2 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 17 May 2009 - 05:49 PM

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "a=add&p=on&tags=codecall irc,log,chat,CCLogBot&pasteinfo={$textS}&type=text");
If this is the case then $textS cannot contain the character & in there? Probably a few others but maybe there should be an encrypted input - if thats detected it should grab that - unencrypt it and use that instead. Like a public encryption just so that it can get the &'s and other weird symbols you might want.

Either way - good tut - maybe you should post the bot source too? :P
  • 0

#3 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 18 May 2009 - 05:14 AM

Blaine: I covered that in the "NOTE" above:

NOTE: If any of the text you submit contains an &, it will need to be URL encoded. You can do that in PHP using urlencode().


The urlencode function will properly encode &s for post/get submission. It is also part of the function at ASCIIBin. :)

Eventually I am going to post the source for the BOT. It is mostly done by another and I'm not happy with all aspects of the code. I'd rather not post anything shameful. lol. I think I will be writing a short tutorial on how to use the PEAR Net_SmartIRC as well. Are you interested?
  • 0

#4 BlaineSch

BlaineSch

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1559 posts

Posted 18 May 2009 - 05:16 AM

Sorry, I suppose I should actually read these things - Usually just looking over the source.

Yes, I think I connected to IRC using java once but that little bot man didnt get far. It was before this place had an IRC channel. It was then I realized I need a command for him to sign out lol.
  • 0





Also tagged with one or more of these keywords: login, design pattern, ascii

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download