cvs: pear /Perm_LiveUser/Admin/Perm Common.php

From: Date: Sun, 24 Oct 2004 11:02:29 +0000
Subject: cvs: pear /Perm_LiveUser/Admin/Perm Common.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-25166@lists.php.net to get a copy of this message
lsmith Sun Oct 24 07:02:29 2004 EDT Modified files: /pear/Perm_LiveUser/Admin/Perm Common.php Log: - refactored outputRightsConstants() (BC break!) to be more flexible and more error resistant http://cvs.php.net/diff.php/pear/Perm_LiveUser/Admin/Perm/Common.php?r1=1.27&r2=1.28&ty=u Index: pear/Perm_LiveUser/Admin/Perm/Common.php diff -u pear/Perm_LiveUser/Admin/Perm/Common.php:1.27 pear/Perm_LiveUser/Admin/Perm/Common.php:1.28 --- pear/Perm_LiveUser/Admin/Perm/Common.php:1.27 Sat Oct 23 04:51:44 2004 +++ pear/Perm_LiveUser/Admin/Perm/Common.php Sun Oct 24 07:02:29 2004 @@ -49,7 +49,7 @@ * * @author Markus Wolff <wolff@21st.de> * @author Bjoern Kraus <krausbn@php.net> - * @version $Id: Common.php,v 1.27 2004/10/23 08:51:44 arnaud Exp $ + * @version $Id: Common.php,v 1.28 2004/10/24 11:02:29 lsmith Exp $ * @package LiveUser */ class LiveUser_Admin_Perm_Common @@ -277,87 +277,105 @@ * 'prefix' => 'prefix_goes_here', * 'area' => 'specific area id to grab rights from', * 'application' => 'specific application id to grab rights from' - * 'naming' => 1 for PREFIX_RIGHTNAME <- DEFAULT - * 2 for PREFIX_AREANAME_RIGHTNAME - * 3 for PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME + * 'naming' => LIVEUSER_SECTION_RIGHT for PREFIX_RIGHTNAME <- DEFAULT + * LIVEUSER_SECTION_AREA for PREFIX_AREANAME_RIGHTNAME + * LIVEUSER_SECTION_APPLICATION for PREFIX_APPLICATIONNAME_AREANAME_RIGHTNAME * 'filename' => if $mode is file you must give the full path for the * output file * * If not prefix is given it will not be used to generate the constants * * @access public + * @param string type of output (constant or array) * @param array options for constants generation * @param string output mode desired (file or direct) - * @param string type of output (constant or array) * @return mixed boolean, array or DB Error object */ - function outputRightsConstants($options = array(), $mode = 'file', $type = 'constant') + function outputRightsConstants($type, $options = array(), $mode = null) { $opt = array(); - $naming = 1; + $naming = LIVEUSER_SECTION_RIGHT; if (isset($options['naming'])) { $naming = $options['naming']; switch ($naming) { - case 2: - $opt['with_areas'] = true; - break; - case 3: - $opt['with_applications'] = true; - $opt['with_areas'] = true; - break; + case LIVEUSER_SECTION_AREA: + $opt['with_areas'] = true; + break; + case LIVEUSER_SECTION_APPLICATION: + $opt['with_applications'] = true; + $opt['with_areas'] = true; + break; } } if (isset($options['area'])) { $opt['where_area_id'] = $options['area']; - $opt['with_areas'] = true; } if (isset($options['application'])) { $opt['where_application_id'] = $options['application']; - $opt['with_applications'] = true; - $opt['with_areas'] = true; + } + + $prefix = ''; + if (isset($options['prefix'])) { + $prefix = $options['prefix'] . '_'; + } + + $rekey = false; + if ($type == 'array' && isset($options['rekey'])) { + $rekey = $options['rekey']; } $rights = $this->getRights($opt); if (PEAR::isError($rights)) { - return $rights; + return false; } $generate = array(); - $prefix = ''; - if (isset($options['prefix'])) { - $prefix = $options['prefix'] . '_'; - } - switch ($naming) { - case 2: + case LIVEUSER_SECTION_APPLICATION: + if ($rekey) { foreach ($rights as $r) { - $key = $prefix . $r['area']['define_name'] . '_' . $r['define_name']; - $generate[$key] = $r['right_id']; - }; - break; - case 3: + $app_name = $prefix . $r['application']['define_name']; + $generate[$app_name][$r['area']['define_name']][$r['define_name']] = $r['right_id']; + } + } else { foreach ($rights as $r) { $key = $prefix . $r['application']['define_name'] . '_' . $r['area']['define_name'] . '_' . $r['define_name']; $generate[$key] = $r['right_id']; - }; - break; - case 1: - default: + } + } + break; + case LIVEUSER_SECTION_AREA: + if ($rekey) { foreach ($rights as $r) { - $generate[$prefix . $r['define_name']] = $r['right_id']; - }; + $generate[$prefix . $r['area']['define_name']][$r['define_name']] = $r['right_id']; + } + } else { + foreach ($rights as $r) { + $key = $prefix . $r['area']['define_name'] . '_' . $r['define_name']; + $generate[$key] = $r['right_id']; + } + } + break; + case LIVEUSER_SECTION_RIGHT: + default: + foreach ($rights as $r) { + $generate[$prefix . $r['define_name']] = $r['right_id']; + } + break; } $strDef = "<?php\n"; if ($type == 'array') { if ($mode == 'file') { - if (!isset($options['varname'])) { + if (!isset($options['varname']) + || !preg_match('/^[a-zA-Z_0-9]+$/', $options['varname']) + ) { return false; } $strDef .= sprintf("\$%s = %s;\n", $options['varname'], var_export($generate, true)); @@ -366,17 +384,23 @@ } } else { foreach ($generate as $v => $k) { - if ($mode == 'file' && $type == 'constant') { - $strDef .= sprintf("define('%s', %s);\n", strtoupper($v), $k); + if (!preg_match('/^[a-zA-Z_0-9]+$/', $v)) { + return false; + } + $v = strtoupper($v); + if ($mode == 'file') { + $strDef .= sprintf("define('%s', %s);\n", $v, $k); } else { - define(strtoupper($v), $k); + if(!defined($v)) { + define($v, $k); + } } } } - $strDef .= "?>"; + $strDef .= '?>'; if ($mode == 'file') { - if (!isset($options['filename'])) { + if (!isset($options['filename']) || !$options['filename']) { return false; }

« previous php.pear.cvs (#25166) next »