replace_callback()   B
last analyzed

Complexity

Conditions 9
Paths 16

Size

Total Lines 53
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 16
nop 1
dl 0
loc 53
rs 8.0555
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * EGroupware - download of customized java notifier
4
 *
5
 * @link http://www.egroupware.org
6
 * @author RalfBecker-at-outdoor-training.de
7
 * @copyright (c) 2012 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
8
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
9
 * @package notifications
10
 * @version $Id$
11
 */
12
13
use EGroupware\Api;
14
15
$GLOBALS['egw_info'] = array(
16
	'flags' => array(
17
		'currentapp'	=> 'notifications',
18
		'noheader'		=> True,
19
		'nonavbar'		=> True,
20
	)
21
);
22
23
ini_set('zlib.output_compression',0);
24
include('../header.inc.php');
25
26
ob_start();
27
28
check_load_extension('zip', true);
29
30
$document = EGW_SERVER_ROOT.'/notifications/java/full-eGwNotifier.jar';
31
$archive = tempnam($GLOBALS['egw_info']['server']['temp_dir'], basename($document,'.jar').'-').'.jar';
32
$ret=copy($document, $archive);
33
error_log("copy('$document', '$archive' returned ".array2string($ret));
34
$document = 'zip://'.$archive.'#'.($config_file = 'lib/conf/egwnotifier.const.xml');
35
36
$xml_in = file_get_contents($document);
37
//Api\Header\Content::type('egwnotifier.const.xml', 'application/xml', bytes($xml_in)); echo $xml_in; exit;
38
39
function replace_callback($matches)
40
{
41
	$replacement = $matches[3];
42
	switch($matches[1])
43
	{
44
		case 'egw_dc_url':
45
			$replacement = Api\Framework::getUrl($GLOBALS['egw_info']['server']['webserver_url']);
46
			break;
47
		case 'egw_dc_logindomain':
48
			$replacement = $GLOBALS['egw_info']['user']['domain'];
49
			break;
50
		case 'egw_dc_username':
51
			$replacement = $GLOBALS['egw_info']['user']['account_lid'];
52
			break;
53
		case 'egw_dc_timeout_socket':
54
		case 'egw_dc_timeout_notify':
55
		case 'egw_debuging':
56
		case 'egw_debuging_level':
57
			break;
58
		default:
59
			$replacement = lang($r=$replacement);
60
			/* uncomment this to have missing translations add to en langfile
61
			// if no translation found, check if en langfile is writable and add phrase, if not already there
62
			if ($r === $replacement)
63
			{
64
				static $langfile;
65
				if (is_null($langfile)) $langfile = EGW_SERVER_ROOT.'/notifications/lang/egw_en.lang';
66
				if (is_writable($langfile) || is_writable(dirname($langfile)))
67
				{
68
					$content = file_get_contents($langfile);
69
					if (!preg_match('/^'.preg_quote($r)."\t/i", $content))
70
					{
71
						if (!is_writable($langfile)) unlink($langfile);
72
						$content .= "$r\tnotifications\ten\t$r\n";
73
						file_put_contents($langfile, $content);
74
					}
75
				}
76
			}*/
77
			break;
78
	}
79
80
	/**
81
	 * workaround
82
	 * Warning: htmlspecialchars() expects parameter 2 to be long, string given
83
	 */
84
	$htmlscflags = ENT_XML1;
85
86
	if (is_string($htmlscflags))
0 ignored issues
show
introduced by
The condition is_string($htmlscflags) is always false.
Loading history...
87
	{
88
		$htmlscflags = 16;	// #define ENT_XML1		16
89
	}
90
91
	return '<'.$matches[1].'>'.htmlspecialchars($replacement, $htmlscflags, Api\Translation::charset()).'</'.$matches[1].'>';
92
}
93
94
$xml = preg_replace_callback('/<((egw_|MI_)[^>]+)>(.*)<\/[a-z0-9_-]+>/iU', 'replace_callback', $xml_in);
95
//Api\Header\Content::type('egwnotifier.const.xml', 'application/xml', bytes($xml)); echo $xml; exit;
96
97
/* does NOT work, fails in addFromString :-(
98
$zip = new ZipArchive;
99
if ($zip->open($archive, ZIPARCHIVE::CHECKCONS) !== true)
100
{
101
	error_log(__METHOD__.__LINE__." !ZipArchive::open('$archive',ZIPARCHIVE::CHECKCONS) failed. Trying open without validating");
102
	if ($zip->open($archive) !== true) throw new Exception("!ZipArchive::open('$archive',|ZIPARCHIVE::CHECKCONS)");
103
}
104
if (($ret=$zip->addFromString($config_file, $xml)) !== true);// throw new Exception("ZipArchive::addFromString('$config_file', \$xml) returned ".array2string($ret));
105
if ($zip->close() !== true) throw new Exception("!ZipArchive::close()");
106
*/
107
108
check_load_extension('phar', true);
109
$zip = new PharData($archive);
110
$zip->addFromString($config_file, $xml);
111
unset($zip);
112
// clear stat cache, as otherwise filesize might report an earlier, smaller size!
113
clearstatcache();
114
ob_end_clean();
115
116
Api\Header\Content::type('egroupware-notifier-'.$GLOBALS['egw_info']['user']['account_lid'].'.jar', 'application/x-java-archive', filesize($archive));
117
readfile($archive,'rb');
118
119
@unlink($archive);
120