| 
									
										
										
										
											2003-04-26 20:01:49 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | $Id$ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   This code is part of LDAP Account Manager (http://www.sourceforge.net/projects/lam) | 
					
						
							| 
									
										
										
										
											2006-03-03 17:30:35 +00:00
										 |  |  |   Copyright (C) 2003 - 2006  Roland Gruber | 
					
						
							| 
									
										
										
										
											2003-04-26 20:01:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2004-10-09 11:04:20 +00:00
										 |  |  | * This file provides functions to load and save account profiles. | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * | 
					
						
							| 
									
										
										
										
											2004-10-09 11:04:20 +00:00
										 |  |  | * @package profiles | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * @author Roland Gruber | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2003-04-28 18:06:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * Returns an array of string with all available profiles for the given account type | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * @param string $scope account type | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * @return array profile names | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | function getAccountProfiles($scope) { | 
					
						
							|  |  |  | 	$dir = @dir(substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles"); | 
					
						
							| 
									
										
										
										
											2003-04-28 18:06:18 +00:00
										 |  |  | 	$ret = array(); | 
					
						
							|  |  |  | 	$pos = 0; | 
					
						
							| 
									
										
										
										
											2003-09-20 17:02:21 +00:00
										 |  |  | 	if ($dir) { | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 		$entry = $dir->read(); | 
					
						
							|  |  |  | 		while ($entry){ | 
					
						
							|  |  |  | 			// check if filename ends with .<scope>
 | 
					
						
							|  |  |  | 			if (strrpos($entry, '.')) { | 
					
						
							|  |  |  | 				$pos = strrpos($entry, '.'); | 
					
						
							|  |  |  | 				if (substr($entry, $pos + 1) == $scope) { | 
					
						
							|  |  |  | 					$name = substr($entry, 0, $pos); | 
					
						
							|  |  |  | 					$ret[] = $name; | 
					
						
							| 
									
										
										
										
											2004-02-12 15:58:04 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2003-09-20 17:02:21 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 			$entry = $dir->read(); | 
					
						
							| 
									
										
										
										
											2003-09-20 17:02:21 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 	return $ret; | 
					
						
							| 
									
										
										
										
											2003-04-26 20:01:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * Loads an profile of the given account type | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * @param string $profile name of the profile (without .<scope> extension) | 
					
						
							|  |  |  | * @param string $scope account type | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * @return array hash array (attribute => value) | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | function loadAccountProfile($profile, $scope) { | 
					
						
							| 
									
										
										
										
											2005-10-23 09:26:18 +00:00
										 |  |  | 	if (!eregi("^[0-9a-z _-]+$", $profile) || !eregi("^[a-z]+$", $scope)) return false; | 
					
						
							| 
									
										
										
										
											2004-01-26 15:29:56 +00:00
										 |  |  | 	$settings = array(); | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 	$file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $profile . "." . $scope; | 
					
						
							| 
									
										
										
										
											2003-04-30 16:50:48 +00:00
										 |  |  | 	if (is_file($file) == True) { | 
					
						
							| 
									
										
										
										
											2003-06-24 15:50:38 +00:00
										 |  |  | 		$file = @fopen($file, "r"); | 
					
						
							|  |  |  | 		if ($file) { | 
					
						
							|  |  |  | 			while (!feof($file)) { | 
					
						
							|  |  |  | 				$line = fgets($file, 1024); | 
					
						
							|  |  |  | 				if (($line == "\n")||($line[0] == "#")) continue; // ignore comments
 | 
					
						
							|  |  |  | 				// search keywords
 | 
					
						
							| 
									
										
										
										
											2004-01-26 15:29:56 +00:00
										 |  |  | 				$parts = array(); | 
					
						
							|  |  |  | 				$parts = split(": ", $line); | 
					
						
							| 
									
										
										
										
											2004-03-06 19:59:32 +00:00
										 |  |  | 				if (sizeof($parts) != 2) continue;  // ignore malformed settings
 | 
					
						
							| 
									
										
										
										
											2004-02-12 15:58:04 +00:00
										 |  |  | 				else { | 
					
						
							| 
									
										
										
										
											2004-03-06 19:59:32 +00:00
										 |  |  | 					$option = $parts[0]; | 
					
						
							|  |  |  | 					$value = $parts[1]; | 
					
						
							| 
									
										
										
										
											2004-02-12 15:58:04 +00:00
										 |  |  | 					// remove line ends
 | 
					
						
							| 
									
										
										
										
											2004-03-06 19:59:32 +00:00
										 |  |  | 					$value = chop($value); | 
					
						
							|  |  |  | 					$settings[$option] = explode("+::+", $value); | 
					
						
							| 
									
										
										
										
											2004-02-12 15:58:04 +00:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2003-06-24 15:50:38 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			fclose($file); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2003-07-30 14:34:33 +00:00
										 |  |  | 			StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); | 
					
						
							| 
									
										
										
										
											2003-04-30 16:50:48 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2003-07-30 14:34:33 +00:00
										 |  |  | 		StatusMessage("ERROR", "", _("Unable to load profile!") . " " . $file); | 
					
						
							| 
									
										
										
										
											2003-04-30 16:50:48 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-01-26 15:29:56 +00:00
										 |  |  | 	return $settings; | 
					
						
							| 
									
										
										
										
											2003-04-26 20:01:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * Saves an hash array (attribute => value) to an account profile | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * | 
					
						
							|  |  |  | * file is created, if needed | 
					
						
							|  |  |  | * | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * @param string $profile name of the account profile (without .<scope> extension) | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * @param array $attributes hash array (attribute => value) | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * @param string $scope account type | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * @return boolean true, if saving succeeded | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | function saveAccountProfile($attributes, $profile, $scope) { | 
					
						
							| 
									
										
										
										
											2003-11-05 19:53:51 +00:00
										 |  |  | 	if (!$_SESSION['loggedIn'] == true) return false; | 
					
						
							| 
									
										
										
										
											2003-10-23 19:31:17 +00:00
										 |  |  | 	// check profile name
 | 
					
						
							| 
									
										
										
										
											2005-10-23 09:26:18 +00:00
										 |  |  | 	if (!eregi("^[0-9a-z _-]+$", $profile) || !eregi("^[a-z]+$", $scope)) return false; | 
					
						
							| 
									
										
										
										
											2004-01-26 15:29:56 +00:00
										 |  |  | 	if (!is_array($attributes)) { | 
					
						
							| 
									
										
										
										
											2003-11-05 19:53:51 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2003-06-24 15:50:38 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 	$path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $profile . "." . $scope; | 
					
						
							| 
									
										
										
										
											2003-08-03 11:05:40 +00:00
										 |  |  | 	$file = @fopen($path, "w"); | 
					
						
							|  |  |  | 	if ($file) { | 
					
						
							| 
									
										
										
										
											2004-03-06 19:59:32 +00:00
										 |  |  |   	// write attributes
 | 
					
						
							|  |  |  | 		$keys = array_keys($attributes); | 
					
						
							|  |  |  | 		for ($i = 0; $i < sizeof($keys); $i++) { | 
					
						
							| 
									
										
										
										
											2004-04-17 14:09:05 +00:00
										 |  |  | 			if (isset($attributes[$keys[$i]])) { | 
					
						
							|  |  |  | 				$line = $keys[$i] . ": " . implode("+::+", $attributes[$keys[$i]]) . "\n"; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2005-05-08 16:37:36 +00:00
										 |  |  | 				$line = $keys[$i] . ": \n"; | 
					
						
							| 
									
										
										
										
											2004-04-17 14:09:05 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-03-06 19:59:32 +00:00
										 |  |  | 			fputs($file, $line); | 
					
						
							| 
									
										
										
										
											2003-09-20 08:04:38 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2003-08-03 11:05:40 +00:00
										 |  |  | 		// close file
 | 
					
						
							|  |  |  | 		fclose($file); | 
					
						
							| 
									
										
										
										
											2003-05-01 20:30:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-03 11:05:40 +00:00
										 |  |  | 	else { | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2003-05-01 20:30:43 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2003-08-03 11:05:40 +00:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2003-04-26 20:01:49 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * Deletes an account profile | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | * | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | * @param string $file name of profile (Without .<scope> extension) | 
					
						
							|  |  |  | * @param string $scope account type | 
					
						
							| 
									
										
										
										
											2004-09-14 18:56:41 +00:00
										 |  |  | * @return boolean true if profile was deleted | 
					
						
							| 
									
										
										
										
											2004-05-31 17:58:27 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | function delAccountProfile($file, $scope) { | 
					
						
							| 
									
										
										
										
											2003-11-05 19:53:51 +00:00
										 |  |  | 	if (!$_SESSION['loggedIn'] == true) return false; | 
					
						
							| 
									
										
										
										
											2005-10-23 09:26:18 +00:00
										 |  |  | 	if (!eregi("^[0-9a-z _-]+$", $file) || !eregi("^[a-z]+$", $scope)) return false; | 
					
						
							| 
									
										
										
										
											2004-10-06 20:00:17 +00:00
										 |  |  | 	$prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/" . $file . "." . $scope; | 
					
						
							| 
									
										
										
										
											2003-05-25 10:51:10 +00:00
										 |  |  | 	if (is_file($prof)) { | 
					
						
							|  |  |  | 		return @unlink($prof); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-09-14 18:56:41 +00:00
										 |  |  | 	else return false; | 
					
						
							| 
									
										
										
										
											2003-05-25 10:51:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-04-28 18:06:18 +00:00
										 |  |  | ?>
 |