Skip to content

Commit 239cf91

Browse files
author
swaraj
committed
inital commit
0 parents  commit 239cf91

File tree

9 files changed

+326
-0
lines changed

9 files changed

+326
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_Store

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
9+
before_script:
10+
- travis_retry composer self-update
11+
- travis_retry composer install --prefer-source --no-interaction --dev
12+
13+
script: phpunit

READEME.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
##A Laravel Package for PNR Enquiry through Indian Railways
2+
3+
Note:- This is not intended for any DOS attack, just to ease the Automation of PNR enquiry through Laravel

composer.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "swarajsaaj/pnr",
3+
"description": "A PNR enquiry in JSON format package for Indian Railways for Laravel",
4+
"authors": [
5+
{
6+
"name": "Swaraj Pal",
7+
"email": "[email protected]"
8+
}
9+
],
10+
"require": {
11+
"php": ">=5.4.0",
12+
"illuminate/support": "4.2.*"
13+
},
14+
"autoload": {
15+
"psr-0": {
16+
"Swarajsaaj\\Pnr": "src/"
17+
}
18+
},
19+
"minimum-stability": "stable"
20+
}

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/Swarajsaaj/Pnr/Facades/Pnr.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php namespace Swarajsaaj\Pnr\Facades;
2+
3+
use Illuminate\Support\Facades\Facade;
4+
5+
class Pnr extends Facade {
6+
7+
/**
8+
* Get the registered name of the component.
9+
*
10+
* @return string
11+
*/
12+
protected static function getFacadeAccessor() { return 'pnr'; }
13+
14+
}

src/Swarajsaaj/Pnr/Pnr.php

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<?php namespace Swarajsaaj\Pnr;
2+
3+
class Pnr {
4+
5+
6+
7+
function array2json($arr) {
8+
if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality.
9+
$parts = array();
10+
$is_list = false;
11+
12+
//Find out if the given array is a numerical array
13+
$keys = array_keys($arr);
14+
$max_length = count($arr)-1;
15+
if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
16+
$is_list = true;
17+
for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position
18+
if($i != $keys[$i]) { //A key fails at position check.
19+
$is_list = false; //It is an associative array.
20+
break;
21+
}
22+
}
23+
}
24+
25+
foreach($arr as $key=>$value) {
26+
if(is_array($value)) { //Custom handling for arrays
27+
if($is_list) $parts[] = array2json($value); /* :RECURSION: */
28+
else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
29+
} else {
30+
$str = '';
31+
if(!$is_list) $str = '"' . $key . '":';
32+
33+
//Custom handling for multiple data types
34+
if(is_numeric($value)) $str .= $value; //Numbers
35+
elseif($value === false) $str .= 'false'; //The booleans
36+
elseif($value === true) $str .= 'true';
37+
else $str .= '"' . addslashes($value) . '"'; //All other things
38+
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
39+
40+
$parts[] = $str;
41+
}
42+
}
43+
$json = implode(',',$parts);
44+
45+
if($is_list) return '[' . $json . ']';//Return numerical JSON
46+
return '{' . $json . '}';//Return associative JSON
47+
}
48+
49+
50+
51+
//Function to Retrieve the data
52+
function makeWebCall($urlto,$postData = null,$refer=null){
53+
54+
//create cURL connection
55+
$curl_connection = curl_init($urlto);
56+
//set options
57+
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
58+
curl_setopt($curl_connection, CURLOPT_USERAGENT,
59+
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
60+
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
61+
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
62+
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, true);
63+
//if
64+
if(isset($postData)){
65+
//set data to be posted
66+
curl_setopt($curl_connection, CURLOPT_POST,true);
67+
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $postData);
68+
}
69+
70+
71+
if(isset($refer)){
72+
//set referer
73+
curl_setopt($curl_connection, CURLOPT_REFERER, $refer);
74+
}
75+
//perform our request
76+
$result = curl_exec($curl_connection);
77+
// Debug -Data
78+
//show information regarding the request
79+
//var_dump(curl_getinfo($curl_connection));
80+
// echo curl_errno($curl_connection) . '-' .
81+
// curl_error($curl_connection);
82+
83+
//close the connection
84+
curl_close($curl_connection);
85+
return $result;
86+
}
87+
88+
89+
// Function to construct the post request
90+
function createPostString($postArray){
91+
//traverse array and prepare data for posting (key1=value1)
92+
foreach ( $postArray as $key => $value) {
93+
$post_items[] = $key . '=' . $value;
94+
}
95+
//create the final string to be posted using implode()
96+
return implode ('&', $post_items);
97+
}
98+
99+
100+
function request($pnr)
101+
{
102+
103+
// KEY Datatype Mandatory Description
104+
//-------------------------------------------------------
105+
// pnrno Integer(10) true PNR number to be fetched 10 digit
106+
// rtype String(XML/JSON) false Return type format
107+
// callback String false Support for JSONP only supported for GET
108+
$pnt_no = $pnr;
109+
110+
$url_captch = 'http://www.indianrail.gov.in/pnr_Enq.html';
111+
$url_pnr = 'http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi_10521.cgi';
112+
// Submit the captcha and PNR
113+
//create array of data to be posted
114+
$post_data['lccp_pnrno1'] = $pnt_no;
115+
$post_data['lccp_cap_val'] = 12345; //dummy captcha
116+
$post_data['lccp_capinp_val'] = 12345;
117+
$post_data['submit'] = "Get Status";
118+
$post_string = $this->createPostString($post_data);
119+
120+
$result = $this->makeWebCall($url_pnr,$post_string,$url_captch );
121+
122+
//Debug
123+
//var_dump($result);
124+
// Parse Logic
125+
// I have not used DOM lib it is simple regEx parse.
126+
//Change here when the Page layout of the page changes.
127+
$matches = array();
128+
preg_match_all('/<td class="table_border_both">(.*)<\/td>/i',$result,$matches);
129+
//DEBUG
130+
//var_dump($matches);
131+
$resultVal = array(
132+
'status' => "INVALID",
133+
'data' => array()
134+
);
135+
136+
if (count($matches)>1&&count($matches[1])>8) {
137+
$arr = $matches[1];
138+
$i=0;
139+
$j=0;
140+
$tmpValue =array(
141+
"pnr" => $pnt_no,
142+
"train_name" => "",
143+
"train_number" => "",
144+
"from" => "",
145+
"to" => "",
146+
"reservedto" => "",
147+
"board" => "",
148+
"class" => "",
149+
"travel_date" => "",
150+
"passenger" => array()
151+
);
152+
153+
$tmpValue['train_number'] = $arr[0];
154+
$tmpValue['train_name'] = $arr[1];
155+
$tmpValue['travel_date'] = $arr[2];
156+
$tmpValue['from'] = $arr[3];
157+
$tmpValue['to'] = $arr[4];
158+
$tmpValue['reservedto'] = $arr[5];
159+
$tmpValue['board'] = $arr[6];
160+
$tmpValue['class'] = $arr[7];
161+
$stnum="";
162+
foreach ($arr as $value) {
163+
164+
$i++;
165+
if($i>8){
166+
$value=trim(preg_replace('/<B>/', '', $value));
167+
$value=trim(preg_replace('/<\/B>/', '', $value));
168+
169+
$ck=$i%3;
170+
if($ck==1){
171+
$stnum = $value;
172+
}
173+
else if($ck==2) {
174+
array_push($tmpValue["passenger"],array(
175+
"seat_number" => $stnum,
176+
"status" => $value
177+
));
178+
}
179+
}
180+
}
181+
$resultVal['data'] = $tmpValue;
182+
$resultVal['status'] = 'OK';
183+
}
184+
185+
186+
$jsondata = $this->array2json($resultVal);
187+
if(array_key_exists('callback', $_GET)){
188+
189+
header('Content-Type: text/javascript; charset=utf8');
190+
header('Access-Control-Max-Age: 3628800');
191+
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
192+
193+
$callback = $_GET['callback'];
194+
echo $callback.'('.$jsondata.');';
195+
196+
}else{
197+
// normal JSON string
198+
header('Content-Type: application/json; charset=utf8');
199+
200+
return $jsondata;
201+
}
202+
203+
204+
205+
}
206+
207+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php namespace Swarajsaaj\Pnr;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
5+
class PnrServiceProvider extends ServiceProvider {
6+
7+
/**
8+
* Indicates if loading of the provider is deferred.
9+
*
10+
* @var bool
11+
*/
12+
protected $defer = false;
13+
14+
/**
15+
* Bootstrap the application events.
16+
*
17+
* @return void
18+
*/
19+
public function boot()
20+
{
21+
$this->package('swaraj/pnr');
22+
}
23+
24+
/**
25+
* Register the service provider.
26+
*
27+
* @return void
28+
*/
29+
public function register()
30+
{
31+
$this->app['pnr'] = $this->app->share(function($app)
32+
{
33+
return new Pnr;
34+
});
35+
}
36+
37+
/**
38+
* Get the services provided by the provider.
39+
*
40+
* @return array
41+
*/
42+
public function provides()
43+
{
44+
return array('pnr');
45+
}
46+
47+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)