Skip to content

Commit e372dd6

Browse files
author
mcramer
committed
Implemented:
- javascript hooks prepared (onAfterContentLoad is first available hook) - new abstract class for GET and POST requests - new js.d directory that is included into main template
1 parent cab7ea5 commit e372dd6

File tree

5 files changed

+304
-53
lines changed

5 files changed

+304
-53
lines changed

interface/lib/classes/functions.inc.php

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,6 @@ public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'a
6161
$app->ispcmail->send($to);
6262
$app->ispcmail->finish();
6363

64-
/* left in here just for the case...
65-
if($filepath != '') {
66-
if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
67-
68-
$content = file_get_contents($filepath);
69-
$content = chunk_split(base64_encode($content));
70-
$uid = strtoupper(md5(uniqid(time())));
71-
$subject = "=?utf-8?B?".base64_encode($subject)."?=";
72-
73-
if($filename == '') {
74-
$path_parts = pathinfo($filepath);
75-
$filename = $path_parts["basename"];
76-
unset($path_parts);
77-
}
78-
79-
$header = "Return-Path: $from\nFrom: $from\nReply-To: $from\n";
80-
if($cc != '') $header .= "Cc: $cc\n";
81-
if($bcc != '') $header .= "Bcc: $bcc\n";
82-
$header .= "MIME-Version: 1.0\n";
83-
$header .= "Content-Type: multipart/mixed; boundary=$uid\n";
84-
85-
$header .= "--$uid\n";
86-
$header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
87-
$header .= "Content-Transfer-Encoding: 8bit\n\n";
88-
$header .= "$text\n";
89-
90-
$header .= "--$uid\n";
91-
$header .= "Content-Type: $filetype; name=\"$filename\"\n";
92-
93-
$header .= "Content-Transfer-Encoding: base64\n";
94-
$header .= "Content-Disposition: attachment; filename=\"$filename\"\n\n";
95-
$header .= "$content\n";
96-
97-
$header .= "--$uid--";
98-
99-
mail($to, $subject, "", $header);
100-
} else {
101-
$header = "From: $from\nReply-To: $from\n";
102-
if($cc != '') $header .= "Cc: $cc\n";
103-
if($bcc != '') $header .= "Bcc: $bcc\n";
104-
$header .= "Content-Type: text/plain;\n\tcharset=\"UTF-8\"\n";
105-
$header .= "Content-Transfer-Encoding: 8bit\n\n";
106-
$subject = "=?utf-8?B?".base64_encode($subject)."?=";
107-
mail($to, $subject, $text, $header);
108-
}
109-
*/
11064
return true;
11165
}
11266

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2013, Marius Cramer, pixcept KG
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
//* The purpose of this library is to provide some general functions.
32+
//* This class is loaded automatically by the ispconfig framework.
33+
34+
abstract class ISPConfigRequest {
35+
/**
36+
* Get header data and contents from an url
37+
*
38+
* Calls an url and returns an array containing the http header and the page content
39+
*
40+
* @access public
41+
* @param string $url the url to call
42+
* @param string $store_in the file to store the data in instead of returning them
43+
* @return array The array with header data at index 0 and page content at index 1, returns boolean false on error. If $store_in is set only the headers are returned
44+
*/
45+
public static function get_with_headers($url, $store_in = null, $follow_redirects = false, $user_agent = false) {
46+
if($follow_redirects === true) $follow_redirects = 5;
47+
elseif($follow_redirects !== false) $follow_redirects--;
48+
49+
if(!$user_agent) $user_agent = 'pxFW GET proxy';
50+
51+
$url_info = parse_url($url);
52+
if(isset($url_info['scheme']) && $url_info['scheme'] == 'https') {
53+
$port = isset($url_info['port']) ? $url_info['port'] : 443;
54+
//@$fp = stream_socket_client('ssl://' . $url_info['host'] . ':' . $port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, stream_context_create(array('ssl' => array('ciphers' => 'ALL:!AES:!3DES:!RC4:@STRENGTH'))));
55+
@$fp = fsockopen('sslv3://' . $url_info['host'], $port, $errno, $errstr, 10);
56+
} else {
57+
$port = isset($url_info['port']) ? $url_info['port'] : 80;
58+
@$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10);
59+
}
60+
61+
if($store_in) {
62+
$outfp = fopen($store_in, 'w');
63+
if(!$outfp) return false;
64+
}
65+
if($fp) {
66+
stream_set_timeout($fp, 10);
67+
$head = 'GET ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . $url_info['query'] : '');
68+
$head .= " HTTP/1.0\r\nHost: " . (isset($url_info['host']) ? $url_info['host'] : '') . "\r\n";
69+
$head .= "User-Agent: " . $user_agent . "\r\n";
70+
if(isset($url_info['user'])) {
71+
if(!array_key_exists('pass', $url_info)) $url_info['pass'] = '';
72+
$head .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n";
73+
}
74+
$head .= "Connection: Close\r\n";
75+
$head .= "Accept: */*\r\n\r\n";
76+
77+
$data = '';
78+
$eoheader = false;
79+
fputs($fp, $head);
80+
while(!feof($fp)) {
81+
if($header = fgets($fp, 1024)) {
82+
if($eoheader == true) {
83+
if($store_in) fputs($outfp, $header);
84+
else $data .= $header;
85+
continue;
86+
}
87+
88+
if ($header == "\r\n") {
89+
$eoheader = true;
90+
continue;
91+
} else {
92+
$header = trim($header);
93+
}
94+
$sc_pos = strpos($header, ':');
95+
if($sc_pos === false) {
96+
$headers['status'] = $header;
97+
$headers['http_code'] = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $header));
98+
} else {
99+
$label = substr($header, 0, $sc_pos);
100+
$value = substr($header, $sc_pos + 1);
101+
$headers[strtolower($label)] = trim($value);
102+
}
103+
}
104+
}
105+
fclose($fp);
106+
if(isset($headers['http_code']) && isset($headers['location']) && ($headers['http_code'] == 301 || $headers['http_code'] == 302) && $follow_redirects > 0) {
107+
if($store_in) fclose($outfp);
108+
return $self::get_with_headers($headers['location'], $store_in, $follow_redirects);
109+
}
110+
if($store_in) {
111+
fclose($outfp);
112+
113+
$code = intval(preg_replace('/^HTTP\/\d+\.\d+\s+(\d+)\s+.*$/', '$1', $headers['status']));
114+
if($code != 200) {
115+
return false;
116+
}
117+
return $headers;
118+
} else {
119+
return array($headers, $data);
120+
}
121+
} else {
122+
if($store_in) {
123+
fclose($outfp);
124+
@unlink($store_in);
125+
}
126+
return false;
127+
}
128+
}
129+
130+
/**
131+
* Gets the content of an url
132+
*
133+
* Checks for the php function file_get_contents and uses an alternative if not found
134+
*
135+
* @access public
136+
* @param string $url url to get
137+
* @return string url data including headers
138+
* @see file_get_contents
139+
*/
140+
public static function get($url) {
141+
if(function_exists('file_get_contents')) return file_get_contents($url);
142+
143+
$fp = fopen($url, 'r');
144+
$data = '';
145+
while(!feof($fp)) {
146+
$data .= fgets($fp, 8192);
147+
}
148+
fclose($fp);
149+
150+
return $data;
151+
}
152+
153+
154+
/**
155+
* Make a post request and get data
156+
*
157+
* Calls an url with a post request and returns the data - and optionally the header content
158+
*
159+
* @access public
160+
* @param string $url the url to call
161+
* @param string $data the post data to send
162+
* @param bool $get_headers if true, the function will return an array like PXUrl::get_with_headers(), otherwise the content is returned as a string
163+
* @return mixed Content data as string or - if get_headers is true - the array with header data at index 0 and page content at index 1
164+
* @see get_url_and_headers
165+
*/
166+
public static function post($url, $data, $get_headers = false, $user_agent = false) {
167+
$url_info = parse_url($url);
168+
if((isset($url_info['scheme']) && $url_info['scheme'] == 'https') || $url_info['port'] == 443) {
169+
$port = (!isset($url_info['port']) || !$url_info['port'] || $url_info['port'] == 443 || $url_info['port'] == 80) ? 443 : $url_info['port'];
170+
//@$fp = stream_socket_client('ssl://' . $url_info['host'] . ':' . $port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, stream_context_create(array('ssl' => array('ciphers' => 'ALL:!AES:!3DES:!RC4:@STRENGTH'))));
171+
@$fp = fsockopen('sslv3://' . $url_info['host'], $port, $errno, $errstr, 10);
172+
} else {
173+
$port = isset($url_info['port']) ? $url_info['port'] : 80;
174+
@$fp = fsockopen($url_info['host'], $port, $errno, $errstr, 10);
175+
}
176+
177+
if(!$fp) return '';
178+
179+
if(!$user_agent) $user_agent = 'pxFW GET proxy';
180+
181+
$header = 'POST ' . (isset($url_info['path']) ? $url_info['path'] : '/') . (isset($url_info['query']) ? '?' . @$url_info['query'] : '') . " HTTP/1.1\r\n";
182+
$header .= "Host: " . @$url_info['host'] . "\r\n";
183+
$header .= "User-Agent: " . $user_agent . "\r\n";
184+
if(isset($url_info['user'])) {
185+
if(!array_key_exists('pass', $url_info)) $url_info['pass'] = '';
186+
$header .= "Authorization: basic " . base64_encode($url_info['user'] . ':' . $url_info['pass']) . "\r\n";
187+
}
188+
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
189+
$header .= "Content-Length: " . strlen($data) . "\r\n";
190+
$header .= "Connection: close\r\n\r\n";
191+
$header .= $data . "\r\n\r\n";
192+
193+
fwrite($fp, $header);
194+
195+
$response = '';
196+
$eoheader = false;
197+
$header = '';
198+
$tmpdata = '';
199+
$chunked = false;
200+
$chunklen = 0;
201+
202+
while(!feof($fp)) {
203+
if($header = @fgets($fp, 1024)) {
204+
if($eoheader == true) {
205+
$response .= $header;
206+
continue;
207+
}
208+
209+
if ($header == "\r\n") {
210+
$eoheader = true;
211+
continue;
212+
} else {
213+
$tmpdata .= $header;
214+
if(preg_match('/Transfer-Encoding:\s+chunked/i', $tmpdata)) $chunked = true;
215+
}
216+
}
217+
}
218+
//var_dump($response, $chunked, $header);
219+
if($chunked == true) {
220+
$lines = explode("\n", $response);
221+
$response = '';
222+
$chunklen = 0;
223+
foreach($lines as $line) {
224+
$line .= "\n";
225+
if($chunklen <= 0) {
226+
if(preg_match('/^([0-9a-f]+)\s*$/is', $line, $matches)) {
227+
$chunklen = hexdec($matches[1]);
228+
}
229+
continue;
230+
}
231+
232+
if(strlen($line) > $chunklen) {
233+
//echo "Warnung: " . strlen($line) . " > " . $chunklen . "\n";
234+
$line = substr($line, 0, $chunklen);
235+
}
236+
$response .= $line;
237+
$chunklen -= strlen($line);
238+
}
239+
240+
$start = strpos($response,'<?xml');
241+
$end = strrpos($response,'>');
242+
if($start !== false && $end !== false) $response = substr($response, $start, $end - $start + 1);
243+
}
244+
245+
fclose($fp);
246+
247+
if($get_headers == true) {
248+
$tmpheaders = explode("\n", $tmpdata);
249+
$headers = array();
250+
foreach($tmpheaders as $cur) {
251+
if(preg_match('/^(\w+)\:\s*(.*)$/is', $cur, $matches)) {
252+
$headers["$matches[1]"] = trim($matches[2]);
253+
}
254+
}
255+
return array($headers, $response);
256+
} else return $response;
257+
}
258+
}
259+
260+
?>

interface/web/index.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@
6060
unset($_SESSION['show_error_msg']);
6161
}
6262

63+
// read js.d files
64+
$js_d = ISPC_WEB_PATH . '/js/js.d';
65+
$js_d_files = array();
66+
if(@is_dir($js_d)) {
67+
$dir = opendir($js_d);
68+
while($file = readdir($dir)) {
69+
$filename = $js_d . '/' . $file;
70+
if($file === '.' || $file === '..' || !is_file($filename)) continue;
71+
if(substr($file, -3) !== '.js') continue;
72+
$js_d_files[] = array('file' => $file);
73+
}
74+
closedir($dir);
75+
}
76+
77+
$app->tpl->setLoop('js_d_includes', $js_d_files);
78+
unset($js_d_files);
6379

6480
$app->tpl_defaults();
6581
$app->tpl->pparse();

0 commit comments

Comments
 (0)