aboutsummaryrefslogtreecommitdiffstats
path: root/sysconfig/network-scripts/network-functions
blob: 605c19dbe8af1a1a40b68279593e9409bcfceaa9 (plain)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*-Shell-script-*-
#
# This file is not a stand-alone shell script; it provides functions 
# to network scripts that source it.

# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin"
export PATH

need_config ()
{
    [ -f "${CONFIG}" ] || CONFIG=../networking/default/${1}
    [ -f "${CONFIG}" ] || CONFIG=../networking/default/ifcfg-${1}
    [ -f "${CONFIG}" ] || CONFIG="ifcfg-${1}"
}

source_config ()
{
    DEVNAME=`basename $CONFIG | sed 's/^ifcfg-//g'`

    if basename $CONFIG | grep -q '[^g]-' ; then
	PARENTCONFIG=`echo $CONFIG | sed 's/-[^-]*$//g'`
	PARENTDEVNAME=`basename $PARENTCONFIG | sed 's/^ifcfg-//g'`
	[ -f $PARENTCONFIG ] || {
	    echo $"Missing config file $PARENTCONFIG." >&2
	    exit 1
	}
	. $PARENTCONFIG
    fi
    . $CONFIG
}

expand_config ()
{
    if [ -z "${NETMASK}" ]; then
	eval `/bin/ipcalc --netmask ${IPADDR}`
    fi

    if [ -z "${PREFIX}" ]; then
	eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}`
    fi

    if [ -z "${BROADCAST}" ]; then
	eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
    fi

    if [ -z "${NETWORK}" ]; then
	eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
    fi
}

toggle_value ()
{
    if [ "$2" = "yes"  -o "$2" = "YES" ] ; then
	echo "$1 on"
    elif [ "$2" = "no"  -o "$2" = "NO" ] ; then
	echo "$1 off"
    else
	echo ''
    fi
}
 
do_netreport ()
{
  # Notify programs that have requested notification
  ( cd /var/run/netreport || exit
    for i in * ; do
      if [ -f $i ]; then
        OWNER=`ls -l $i | awk '{ print $3 }'`
	
        if [ "`id -u`" = "0" ]; then
	  su -s /bin/bash $OWNER -c "kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1" > /dev/null 2>&1
	else
	  kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1
	fi
      fi
    done
  )
}

is_available ()
{
    LC_ALL= LANG= ip -o link | grep -q $1
    [ "$?" = "1" ] || return 0

    alias=`modprobe -c | awk "/^alias $1 / { print \\$3 }"`
    if [ -z "$alias" -o "$alias" = "off" ]; then
        return 2
    fi
    modprobe $1 > /dev/null 2>&1 || return 1
    LC_ALL= LANG= ip -o link | grep -q $1
    return $?
}

need_hostname ()
{
    CHECK_HOSTNAME=`hostname`
    if [ "$CHECK_HOSTNAME" = "(none)" -o "$CHECK_HOSTNAME" = "localhost" -o \
	"$CHECK_HOSTNAME" = "localhost.localdomain" ]; then
	return 0
    else
	return 1
    fi
}

set_hostname ()
{
    hostname $1
    if ! grep search /etc/resolv.conf >/dev/null 2>&1; then
	domain=`echo $1 | sed 's/^[^\.]*\.//'`
	echo "search $domain" >> /etc/resolv.conf
    fi
}

check_device_down ()
{
    if echo $1 | grep -q ':' ; then
     if LC_ALL=C ifconfig -a 2>/dev/null | grep -q $1 ; then
	  return 1
     else
	  return 0
     fi
    else 
     if LC_ALL=C ip -o link ls dev $1 2>/dev/null | grep -q ",UP" ; then
	return 1
     else
	return 0
     fi
    fi
}

check_mii_tool ()
{
   output=`LC_ALL=C mii-tool $1 2>&1`
   echo $output | LC_ALL=C grep -q "link ok" && return 1
   echo $output | LC_ALL=C grep -q "no link" && return 0 || return 2
}

check_ethtool ()
{
   output=`LC_ALL=C ethtool $1 2>&1`
   echo $output | LC_ALL=C grep -q "Link detected: yes" && return 1
   echo $output | LC_ALL=C grep -q "Link detected: no" && return 0 || return 2
}


check_link_down ()
{
    if [ -x /sbin/mii-tool ]; then
        if ! LC_ALL=C ip link show $1 2>/dev/null| grep -q UP ; then
	   ip link set $1 up >/dev/null 2>&1
	   timeout=0
	   while [ $timeout -le 10 ]; do
	   	check_mii_tool $1
		m=$?
		check_ethtool $1
		e=$?
		if [ $m -eq 1 ] || [ $e -eq 1 ] ; then
		    return 1
		fi
		if [ $m -eq 2 ] && [ $e -eq 2 ] ; then
		    return 1
		fi
		usleep 500000
		timeout=$((timeout+1))
	   done
	fi
    fi
    return 0
}

check_default_route ()
{
    LC_ALL=C ip route list match 0/0 | grep -q default
}

find_gateway_dev ()
{
    . /etc/sysconfig/network
    if [ -n "${GATEWAY}" -a "${GATEWAY}" != "none" ] ; then
	dev=`LC_ALL=C /sbin/ip route | \
             grep ${GATEWAY} | \
             sed -e 's/.* dev \([:alnum:]*\)/\1/'`
	if [ -n "$dev" ]; then
	    GATEWAYDEV="$dev"
        fi
    fi
}

add_default_route ()
{
    . /etc/sysconfig/network
    check_default_route && return 0
    find_gateway_dev
    if [ "$GATEWAYDEV" != "" -a -n "${GATEWAY}" -a \
 		"${GATEWAY}" != "none" ]; then
        if ! check_device_down $1; then
            if [ "$GATEWAY" = "0.0.0.0" ]; then
                /sbin/ip route add default dev ${GATEWAYDEV}
            else
                /sbin/ip route add default via ${GATEWAY}
            fi
        fi
    elif [ -f /etc/default-route ]; then
	/sbin/ip route add default via `cat /etc/default-route`
	rm -f /etc/default-route
    fi
}

is_wireless_device ()
{
    [ -x /sbin/iwconfig ] || return 1
    LC_ALL=C iwconfig $1 2>&1 | grep -q "no wireless extensions" || return 0
    return 1
}
7' href='#n407'>407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\files;

/**
 * Responsible for holding all file relevant information, as well as doing file-specific operations.
 * The {@link fileupload fileupload class} can be used to upload several files, each of them being this object to operate further on.
 */
class filespec
{
	var $filename = '';
	var $realname = '';
	var $uploadname = '';
	var $mimetype = '';
	var $extension = '';
	var $filesize = 0;
	var $width = 0;
	var $height = 0;
	var $image_info = array();

	var $destination_file = '';
	var $destination_path = '';

	var $file_moved = false;
	var $local = false;

	var $error = array();

	var $upload = '';

	/**
	 * @var \phpbb\filesystem\filesystem_interface
	 */
	protected $filesystem;

	/**
	 * The plupload object
	 * @var \phpbb\plupload\plupload
	 */
	protected $plupload;

	/**
	 * phpBB Mimetype guesser
	 * @var \phpbb\mimetype\guesser
	 */
	protected $mimetype_guesser;

	/**
	 * File Class
	 * @access private
	 */
	function __construct(\phpbb\filesystem\filesystem_interface $phpbb_filesystem, \phpbb\mimetype\guesser $mimetype_guesser = null, \phpbb\plupload\plupload $plupload = null)
	{
		// @todo call this via files
		//$this->set_upload_ary($upload_ary);
		//$this->set_upload_namespace($upload_namespace);

		$this->plupload = $plupload;
		$this->mimetype_guesser = $mimetype_guesser;
		$this->filesystem = $phpbb_filesystem;
	}

	public function set_upload_ary($upload_ary)
	{
		$this->filename = $upload_ary['tmp_name'];
		$this->filesize = $upload_ary['size'];
		$name = (STRIP) ? stripslashes($upload_ary['name']) : $upload_ary['name'];
		$name = trim(utf8_basename($name));
		$this->realname = $this->uploadname = $name;
		$this->mimetype = $upload_ary['type'];

		// Opera adds the name to the mime type
		$this->mimetype	= (strpos($this->mimetype, '; name') !== false) ? str_replace(strstr($this->mimetype, '; name'), '', $this->mimetype) : $this->mimetype;

		if (!$this->mimetype)
		{
			$this->mimetype = 'application/octet-stream';
		}

		$this->extension = strtolower(self::get_extension($this->realname));

		// Try to get real filesize from temporary folder (not always working) ;)
		$this->filesize = (@filesize($this->filename)) ? @filesize($this->filename) : $this->filesize;

		$this->width = $this->height = 0;
		$this->file_moved = false;

		$this->local = (isset($upload_ary['local_mode'])) ? true : false;

		return $this;
	}

	public function set_upload_namespace($namespace)
	{
		$this->upload = $namespace;

		return $this;
	}

	/**
	 * Check if class members were not properly initalised yet
	 *
	 * @return bool True if there was an init error, false if not
	 */
	public function init_error()
	{
		return !isset($this->filename);
	}

	/**
	 * Cleans destination filename
	 *
	 * @param real|unique|unique_ext $mode real creates a realname, filtering some characters, lowering every character. Unique creates an unique filename
	 * @param string $prefix Prefix applied to filename
	 * @param string $user_id The user_id is only needed for when cleaning a user's avatar
	 * @access public
	 */
	function clean_filename($mode = 'unique', $prefix = '', $user_id = '')
	{
		if ($this->init_error())
		{
			return;
		}

		switch ($mode)
		{
			case 'real':
				// Remove every extension from filename (to not let the mime bug being exposed)
				if (strpos($this->realname, '.') !== false)
				{
					$this->realname = substr($this->realname, 0, strpos($this->realname, '.'));
				}

				// Replace any chars which may cause us problems with _
				$bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');

				$this->realname = rawurlencode(str_replace($bad_chars, '_', strtolower($this->realname)));
				$this->realname = preg_replace("/%(\w{2})/", '_', $this->realname);

				$this->realname = $prefix . $this->realname . '.' . $this->extension;
				break;