/
ir.php
179 lines (122 loc) · 4.5 KB
/
ir.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/*
https://github.com/kasparsd/ir-pdf
Žurnāla "Ir" izdevēji nepiedāvā saviem abonentiem vienkāršu veidu,
kā to automātiski saņemt un lasīt savā elektroniskajā lasītājā
(iPad, Kindle, u.c.) uzreiz, kad ir izdots jaunākais tā numurs.
Šis rīks automātiski atrod jaunākā izdevuma numuru ir.lv,
pievienojas ir.lv ar tavu lietotāja vārdu un paroli, un lejuplādē
izdevuma lappuses JPG formātā, saglabā tās vienā PDF failā un nosūta
uz norādīto epasta adresi.
Šis rīks ir paredzēts TIKAI UN VIENĪGI personīgai lietošanai.
*/
$config = array(
'email_to' => 'example@example.com, another@example.com', // PDF saņēmējs, piem.: 'name@example.com, another@example.com'
'username' => '', // tavs ir.lv pieejas epasts, piem.: name@example.com
'password' => '', // tava ir.lv pieejas parole
'url_id' => 'http://www.ir.lv/zurnals/pedejais',
'url_login' => 'http://www.ir.lv/lietotajs/ienakt',
'url_index' => 'http://www.ir.lv/zurnals/' // izmantots kā http://www.ir.lv/zurnals/$id
);
// Extract the current issue ID
$req = file_get_contents( $config['url_id'] );
foreach ( $http_response_header as $header ) {
if ( strstr( $header, 'Location:' ) ) {
$id = preg_replace( '/[^\d]/', '', $header );
break;
}
}
if ( empty( $id ) )
die('ID not found!');
if ( file_exists( $id . '.pdf' ) )
die('PDF already exists!');
// Unset because $http_response_header is in local scope
unset( $http_response_header );
// Extract the auth token
$login_page = file_get_contents( $config['url_login'] );
$doc = new DOMDocument();
$doc->loadHTML( $login_page );
foreach ( $doc->getElementsByTagName('input') as $element ) {
if ( $element->getAttribute('name') == 'authenticity_token' ) {
$token = $element->getAttribute('value');
break;
}
}
// Extract the session cookie
$cookies = '';
foreach ( $http_response_header as $s )
if ( preg_match( '|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts ) )
$cookies .= sprintf( ' %s=%s;', $parts[1], $parts[2] );
unset( $http_response_header );
// Build the login request
$post = array(
'authenticity_token' => $token,
'user' => array(
'email' => $config['username'],
'password' => $config['password']
)
);
$req = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query( $post ),
'header' => 'Cookie:' . $cookies
)
);
file_get_contents( $config['url_login'], false, stream_context_create( $req ) );
// Extract the authentication cookie
$cookies = '';
foreach ( $http_response_header as $s )
if ( preg_match( '|^Set-Cookie:\s*([^=]+)=([^;]+);(.+)$|', $s, $parts ) )
$cookies .= sprintf( ' %s=%s;', $parts[1], $parts[2] );
// Download the index page for the current issue
$req = array(
'http' => array(
'method' => 'GET',
'header' => 'Cookie: ' . $cookies . "\r\n"
)
);
$o = file_get_contents( $config['url_index'] . $id, false, stream_context_create( $req ) );
// Extract the pages of the current issue
$doc = new DOMDocument();
$doc->loadHTML( $o );
$pages = array();
foreach ( $doc->getElementsByTagName('img') as $element )
if ( $element->getAttribute('class') == 'th' )
$pages[] = $element->getAttribute('src');
if ( empty( $pages ) )
die('No images found');
// Create a folder for storing the pages for the current issue
if ( ! is_dir( $id ) )
mkdir( $id );
// Extract and download pages of the current issue as JPG files
foreach ( $pages as $src ) {
if ( strstr( $src, '/th-' ) ) {
if ( file_exists($id . '/' . basename($src) ) )
continue;
$img = file_get_contents( 'http://www.ir.lv' . str_replace( '/th-', '/full-', $src ) );
file_put_contents( $id . '/' . basename( $src ), $img );
}
}
// Build the PDF from images using ImageMagick
if ( ! file_exists( $id . '.pdf' ) )
exec( sprintf( 'convert %d/*.jpg %d.pdf', $id, $id ) );
if ( ! file_exists( $id . '.pdf' ) )
die('PDF was not saved!');
// Email the PDF
$separator = md5( time() );
$attachment = chunk_split( base64_encode( file_get_contents( $id . '.pdf' ) ) );
$headers = array(
'MIME-Version: 1.0',
'Content-Type: multipart/related; boundary="' . $separator . '"',
);
$message = array(
'--' . $separator,
'Content-Type: application/pdf; name="' . $id . '.pdf"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment; filename="'. $id .'.pdf"' . PHP_EOL,
$attachment . PHP_EOL,
'Content-Type: multipart/alternative',
'--' . $separator . '--' . PHP_EOL,
);
mail( $config['email_to'], 'IR #' . $id, implode( PHP_EOL, $message ), implode( PHP_EOL, $headers ) );