/usr/share/phamm/lib/common.php is in phamm 0.5.18-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 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 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | <?php
/*
* Common functions
*
* @package Phamm
*/
/*
* Phamm - http://www.phamm.org - <team@phamm.org>
* Copyright (C) 2004,2008 Alessandro De Zorzi and Mirko Grava
*
* This file is part of Phamm.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Refresh the page
*
* Initial code Copyright (C) 2004, Bruno Pelaia (PFLAdmin)
* Redirect a client to the specified url.
* The 'url' should be HTTP/1.1 compliant: it should start with the
* protocol specification.
* The 'force_meta' and 'seconds' parameter are optional.
* The 'force_meta' behaviour is assumed if headers have been already sent.
*
*
* @param string $url The page to redirect to
* @param bool $force_meta Whenever you need an HTML-meta tag or not
* @param int $seconds The amount of seconds to wait before
**/
function refresh ($url, $force_meta = false, $seconds = 0)
{
// Check the protocol
if (isset($_SERVER['HTTPS']))
$PROTOCOL = 'https';
else
$PROTOCOL = 'http';
// HTTP/1.1 requires an absolute URI
$uri = ereg ( '^http', $url ) ? $url : "$PROTOCOL://".
$_SERVER['HTTP_HOST'];
// Server Root Check
if(dirname ($_SERVER['PHP_SELF']) != '' && dirname ($_SERVER['PHP_SELF']) != '/')
$uri .= dirname($_SERVER['PHP_SELF']);
$uri .= '/'.$url;
if ( $force_meta || headers_sent () )
{
ob_start();
ob_clean ();
echo "<html>
<head>
<META HTTP-EQUIV=\"Refresh\" CONTENT=\"$seconds; url=$uri\">
</head>
<body>
<p>";
echo _("You will be automatically redirected to a new page within a few seconds.<br/> If this does not happen, please follow the link:")." ".'<a href="'.$uri.'">'._("click here").'</a>';
echo "</p>
</body>
</html>";
ob_end_flush ();
}
else
{
ob_end_clean ();
header ( "Location: ${uri}" );
}
exit;
}
/**
* Return PHP array from XML file
*
* original code from
* mmustafa at vsnl dot com http://php.net/xml_parser_create
*
* @param mixed $file
* @return array $params
**/
function xml2array ($file)
{
$xml_parser = xml_parser_create();
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$xml_elem = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem))
{
$extra = array_values($xml_elem['attributes']);
$level[$xml_elem['level']] = $extra[0];
}
else
{
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete')
{
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level'])
{
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = isset($xml_elem[\'value\']) ?
$xml_elem[\'value\'] : "";';
eval($php_stmt);
}
}
return $params;
}
/**
* Generic LDAP Single-level search
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @todo add attrsonly, sizelimit, timelimit
*
* @param string $base_dn
* @param string $filter
* @param array $attributes
* @param array $attributes
* @param array $short Sort Attributes
**/
function phamm_list ($base_dn,$filter,$attributes=null,$sort=null)
{
global $connect;
// Do a LDAP search
if ($attributes)
$search = ldap_list($connect,$base_dn,$filter,$attributes);
else
$search = ldap_list($connect,$base_dn,$filter);
// Order the results if possible
if (version_compare(phpversion(), "4.2.0", ">="))
ldap_sort($connect,$search,$sort);
// Get entries
$entries = ldap_get_entries($connect, $search);
// Free the memory
ldap_free_result($search);
// Return the entry
return $entries;
}
/**
* Generic LDAP search
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @todo add attrsonly, sizelimit, timelimit
*
* @param string $base_dn
* @param string $filter
* @param array $attributes
* @return array $entries
**/
function phamm_search ($base_dn,$filter,$attributes=null,$sort=null)
{
global $connect;
// Do a LDAP search
if (isset($attributes))
$search = ldap_search($connect,$base_dn,$filter,$attributes);
else
$search = ldap_search($connect,$base_dn,$filter);
// Order the results if possible
if (version_compare(phpversion(), "4.2.0", ">="))
ldap_sort($connect, $search,$sort);
// Get entries
$entries = ldap_get_entries($connect, $search);
// Free the memory
ldap_free_result($search);
// Return the entry
return $entries;
}
/**
* Create new LDAP entry
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
function phamm_add ($dn,$entry)
{
global $connect;
$r = ldap_add ($connect, $dn, $entry);
return $r;
}
/**
* Modify a LDAP entry
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
function phamm_modify ($dn,$entry)
{
global $connect;
$r = ldap_modify ($connect, $dn, $entry );
return $r;
}
/**
* Check if password do not match and...
*
* @param $password1
* @param $password2
* @return bool
**/
function wrong_pw ($password1,$password2,$length=PASSWORD_MIN_LENGHT)
{
if ( $password1 != $password2 )
$error_msg = _("Passwords don't match!");
elseif ( strlen($password1) < $length )
$error_msg = _("Password too short!");
elseif ( !check_syntax ('password',$password1) && $length > 0)
$error_msg = _("Password contains special chars");
if (isset($error_msg))
return $error_msg;
return false;
}
/**
* Hashes a password and returns the hash based on the specified enc_type.
*
* Original function from phpLDAPadmin project.
*
* @author The phpLDAPadmin development team
*
* @param string $password_clear The password to hash in clear text.
* @constant string $enc_type Standard LDAP encryption type which must be one of
* crypt, md5 or clear.
* @return string The hashed password.
*/
function password_hash($password_clear)
{
$enc_type = strtolower(ENC_TYPE);
switch($enc_type)
{
case 'crypt':
$password_hash = '{CRYPT}'.crypt($password_clear);
break;
case 'md5':
$password_hash = '';
$md5_hash = md5($password_clear);
for ( $i = 0; $i < 32; $i += 2 )
$password_hash .= chr( hexdec( $md5_hash{ $i + 1 } ) + hexdec( $md5_hash{ $i } ) * 16 );
$password_hash = '{MD5}'.base64_encode($password_hash);
break;
case 'clear':
$password_hash = $password_clear;
break;
default:
$password_hash = '{CRYPT}'.crypt($password_clear);
break;
}
return $password_hash;
}
/**
* Get the values of a DN
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param string $filter
* @return array $results
**/
function self_values ($dn, $filter="(cn=*)")
{
global $connect;
$search = ldap_search($connect,$dn,$filter);
$results = ldap_get_entries($connect, $search);
return $results;
}
/**
* Purge empty values
*
* @param array $data
* @param array $attribute
* @return array $res
**/
function purge_empty_values ($data,$attribute=null)
{
// Empty array
$res = array();
if (is_array($data))
{
foreach ($data as $key => $val)
if ($val)
{
if (isset($attribute))
{
// multiplies
if (isset($attribute[$key]["MULTIPLIER"]))
$val = ( $val * $attribute[$key]["MULTIPLIER"] );
// Append suffix
if (isset($attribute[$key]["SUFFIX"]))
$val = $val.$attribute[$key]["SUFFIX"];
}
if (is_array($val))
$res[$key] = $val;
else
$res[$key] = strip_tags($val);
}
}
return $res;
}
/**
* Add new attribute (multiple)
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
function phamm_mod_add ($dn,$entry)
{
global $connect;
$r = ldap_mod_add ($connect, $dn, $entry);
return $r;
}
/**
* Delete attribute (multiple)
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
function phamm_mod_del ($dn,$entry)
{
global $connect;
$r = ldap_mod_del ($connect, $dn, $entry);
return $r;
}
/**
* Delete LDAP entry recursive
*
* @author gabriel at hrz dot uni-marburg dot de
* http://it2.php.net/manual/it/function.ldap-delete.php
*
* @param $ds
* @param string $dn The DN
* @param bool $recursive
* @return bool
**/
function phamm_ldap_delete($dn,$recursive=false)
{
global $connect;
if($recursive == false)
{
return(ldap_delete($connect,$dn));
}
else
{
//searching for sub entries
$sr=ldap_list($connect,$dn,"ObjectClass=*",array(""));
$info = ldap_get_entries($connect, $sr);
for($i=0;$i<$info['count'];$i++)
{
//deleting recursively sub entries
$result=myldap_delete($connect,$info[$i]['dn'],$recursive);
if(!$result)
{
//return result code, if delete fails
return($result);
}
}
return(ldap_delete($connect,$dn));
}
}
/**
* Simple LDAP error
**/
function phamm_error ()
{
global $connect;
return _("LDAP Error: ").ldap_error($connect).' ('._("Code ").ldap_errno($connect).')';
}
/**
* Various syntax check (IP address, domain, email address...)
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
* @todo Check if IP 0 < number <255
*
* @param string $type The kind of data
* @param string $arg The value
* @param int $length The min length of string
* @todo name
* @return bool
**/
function check_syntax ($type,$arg,$length="0")
{
if (strlen($arg) < $length)
{
return false;
}
// IP Address
if ($type == 'ip')
{
if (!ereg ("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $arg))
{
return FALSE;
}
/*$numbers = explode('.',$arg);
foreach ($numbers as $number)
{
if ($number > 255)
return FALSE;
} */
else
{
return TRUE;
}
}
// DOMAIN
elseif ($type == 'domain')
{
if (!eregi("^([0-9a-z][0-9a-z-]+\.)+[a-z]{2,7}$", $arg))
{
return FALSE;
}
else
{
return TRUE;
}
}
// ALIAS and ACCOUNT
elseif ($type == 'account')
{
if (!eregi("^[\._a-z0-9-]+$", $arg))
{
return FALSE;
}
else
{
return TRUE;
}
}
// Password
elseif ($type == 'password')
{
if (!eregi("^[\._a-z0-9-]+$", $arg))
return false;
return true;
}
// Email
elseif ($type == 'email')
{
if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$", $arg))
return false;
return true;
}
// Name
elseif ($type == 'name')
{
return true;
}
}
/**
* Open the LDAP connection
*
* @author Alessandro De Zorzi <adezorzi AT rhx DOT it>
**/
function phamm_ldap_connect ()
{
// Open LDAP connection to server
$connect = @ldap_connect(LDAP_HOST_NAME,LDAP_PORT)
or die ("LDAP connection Failed!");
ldap_set_option($connect,LDAP_OPT_PROTOCOL_VERSION,LDAP_PROTOCOL_VERSION);
// Start TLS session
if (LDAP_TLS == 1)
{
@ldap_start_tls($connect)
or die ("Could not start TLS. Please check your LDAP server configuration.");
}
return $connect;
}
?>
|