4bb0a3715ee97f99365a2094b8ea946a3c6e437e
[lhc/web/www.git] / www / prive / formulaires / configurer_relayeur.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2017 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13 if (!defined('_ECRIRE_INC_VERSION')) {
14 return;
15 }
16 include_spip('inc/presentation');
17 include_spip('inc/config');
18
19 function formulaires_configurer_relayeur_charger_dist() {
20 $valeurs = array(
21 'http_proxy' => no_password_proxy_url(lire_config('http_proxy', '')),
22 'http_noproxy' => lire_config('http_noproxy', ''),
23 'test_proxy' => 'http://www.spip.net/',
24 );
25
26 return $valeurs;
27 }
28
29 function formulaires_configurer_relayeur_verifier_dist() {
30 $erreurs = array();
31 $http_proxy = relayeur_saisie_ou_config(_request('http_proxy'), lire_config('http_proxy', ''));
32 $http_noproxy = _request('http_noproxy');
33
34 if ($http_proxy and !tester_url_absolue($http_proxy)) {
35 $erreurs['http_proxy'] = _T('info_url_proxy_pas_conforme');
36 }
37
38 if (!isset($erreurs['http_proxy']) and _request('tester_proxy')) {
39 if (!$http_proxy) {
40 $erreurs['http_proxy'] = _T('info_obligatoire');
41 } else {
42 include_spip('inc/distant');
43 $test_proxy = _request('test_proxy');
44 $t = parse_url($test_proxy);
45 if (!@$t['host']) {
46 $erreurs['test_proxy'] = _T('info_adresse_non_indiquee');
47 } else {
48 include_spip('inc/texte'); // pour aide, couper, lang
49 $info = '';
50 if (!need_proxy($t['host'], $http_proxy, $http_noproxy)) {
51 $info = '<strong>' . _T('page_pas_proxy') . '</strong><br />';
52 }
53
54 // il faut fausser le proxy actuel pour faire le test !
55 $cur_http_proxy = $GLOBALS['meta']['http_proxy'];
56 $cur_http_noproxy = $GLOBALS['meta']['http_noproxy'];
57 $GLOBALS['meta']['http_proxy'] = $http_proxy;
58 $GLOBALS['meta']['http_noproxy'] = $http_noproxy;
59 $page = recuperer_page($test_proxy, true);
60 $GLOBALS['meta']['http_proxy'] = $cur_http_proxy;
61 $GLOBALS['meta']['http_noproxy'] = $cur_http_noproxy;
62 if ($page) {
63 $erreurs['message_ok'] = _T('info_proxy_ok') . "<br />$info\n<tt>" . couper(entites_html($page), 300) . '</tt>';
64 $erreurs['message_erreur'] = '';
65 } else {
66 $erreurs['message_erreur'] = $info . _T(
67 'info_impossible_lire_page',
68 array('test_proxy' => "<tt>$test_proxy</tt>")
69 )
70 . ' <b><tt>' . no_password_proxy_url($http_proxy) . '</tt></b>.'
71 . aider('confhttpproxy');
72 }
73 }
74 }
75 }
76
77 return $erreurs;
78 }
79
80 function formulaires_configurer_relayeur_traiter_dist() {
81 $res = array('editable' => true);
82
83 $http_proxy = relayeur_saisie_ou_config(_request('http_proxy'), lire_config('http_proxy', ''));
84 $http_noproxy = _request('http_noproxy');
85 if ($http_proxy !== null) {
86 ecrire_meta('http_proxy', $http_proxy);
87 }
88
89 if ($http_noproxy !== null) {
90 ecrire_meta('http_noproxy', $http_noproxy);
91 }
92
93 $res['message_ok'] = _T('config_info_enregistree');
94
95 return $res;
96 }
97
98 function relayeur_saisie_ou_config($http_proxy, $default) {
99 // http_proxy : ne pas prendre en compte la modif si le password est '****'
100 if (preg_match(',:\*\*\*\*@,', $http_proxy)) {
101 $http_proxy = $default;
102 }
103
104 return $http_proxy;
105 }
106
107 // Function glue_url : le pendant de parse_url
108 // http://code.spip.net/@glue_url
109 function glue_url($url) {
110 if (!is_array($url)) {
111 return false;
112 }
113 // scheme
114 $uri = (!empty($url['scheme'])) ? $url['scheme'] . '://' : '';
115 // user & pass
116 if (!empty($url['user'])) {
117 $uri .= $url['user'] . ':' . $url['pass'] . '@';
118 }
119 // host
120 $uri .= $url['host'];
121 // port
122 $port = (!empty($url['port'])) ? ':' . $url['port'] : '';
123 $uri .= $port;
124 // path
125 $uri .= $url['path'];
126 // fragment or query
127 if (isset($url['fragment'])) {
128 $uri .= '#' . $url['fragment'];
129 } elseif (isset($url['query'])) {
130 $uri .= '?' . $url['query'];
131 }
132
133 return $uri;
134 }
135
136
137 // Ne pas afficher la partie 'password' du proxy
138 // http://code.spip.net/@no_password_proxy_url
139 function no_password_proxy_url($http_proxy) {
140 if ($http_proxy
141 and $p = @parse_url($http_proxy)
142 and isset($p['pass'])
143 and $p['pass']
144 ) {
145 $p['pass'] = '****';
146 $http_proxy = glue_url($p);
147 }
148
149 return $http_proxy;
150 }