/**
  * @group AppKit
  */
 public function testTrimSplit()
 {
     $string = 'test,      laola       , OK   DING                     ,';
     $splitted = AppKitArrayUtil::trimSplit($string);
     $this->assertEquals('test', $splitted[0]);
     $this->assertEquals('OK   DING', $splitted[2]);
 }
 public function initialize(AgaviContext $context, array $parameters = array())
 {
     parent::initialize($context, $parameters);
     $this->__type = $this->getParameter('type');
     $this->__oids = AppKitArrayUtil::trimSplit($this->getParameter('oids'));
     if (!in_array($this->__type, array(self::TYPE_HOST, self::TYPE_SERVICE))) {
         throw new AppKitModelException('Parameter type not valid!');
     }
     if (!is_array($this->__oids)) {
         throw new AppKitModelException('oids must be an array');
     }
     $model = $this->getContext()->getModel("ApiDataRequest", "Api");
     $this->__request = $model->createRequestDescriptor($parameters["connection"]);
 }
 /**
  * Tries to detect username
  *
  * Sets appropriate data from header
  *
  * @return null|string
  */
 public function determineUsername()
 {
     $source = $this->getVarSource();
     foreach (self::$source_map as $class_target => $config_target) {
         $search_keys = AppKitArrayUtil::trimSplit($this->getParameter($config_target, self::$source_map_defaults[$class_target]));
         $search_value = null;
         //  Looking for multiple keys and use the first match
         foreach ($search_keys as $search_key) {
             if ($source->getParameter($search_key) !== null) {
                 $search_value = $source->getParameter($search_key);
                 $this->log('Auth.Provider.HTTPBasicAuthentification: Got header data: %s=%s', $search_key, $search_value, AgaviILogger::DEBUG);
                 break;
             }
         }
         if ($search_value !== null) {
             if ($class_target == 'auth_name') {
                 // Fixes mixed auth models (case-sensitive and case-insensitive)
                 // see #3714 (Thanks dirk)
                 if ($this->getParameter('auth_lowercase_username', false) == true) {
                     $search_value = strtolower($search_value);
                 }
                 if ($strip = strtolower($this->getParameter('auth_strip_domain', ''))) {
                     $m = '~@' . preg_quote($strip, '~') . '~';
                     $this->{$class_target} = preg_replace($m, '', $search_value);
                 } else {
                     $this->{$class_target} = $search_value;
                 }
             } else {
                 $this->{$class_target} = $search_value;
             }
         } else {
             $this->log('Auth.Provider.HTTPBasicAuthentification: No value found for %s/%s', $class_target, $config_target, AgaviILogger::DEBUG);
         }
     }
     if ($this->auth_type) {
         $this->auth_type = strtolower($this->auth_type);
     }
     $this->log('Auth.Provider.HTTPBasicAuthentification: Got data (auth_name=%s, auth_type=%s)', $this->auth_name, $this->auth_type, AgaviLogger::DEBUG);
     return $this->auth_name;
 }
예제 #4
0
 /**
  * Adding order parts from main query to subquery which delivers
  * the recordset
  * https://dev.icinga.org/issues/2215
  * @param string $query
  * @return string
  */
 private function fixOrdersInSubqueries(&$query)
 {
     list($mainTable, $mainAlias) = $this->getTableParts($query);
     $m = array();
     if (preg_match('/ORDER\\s+BY\\s+(.+)(;|$)/i', $query, $m)) {
         $fields = AppKitArrayUtil::trimSplit($m[1]);
         $pparse = new AppKitStringParanthesesParser($query, AppKitStringParanthesesParser::STRIP_PARANTHESES);
         if ($pparse->count()) {
             foreach ($pparse as $part) {
                 $m = array();
                 if (preg_match('/^\\s*(SELECT\\s+[\\w\\.\\,\\s]+\\s+FROM\\s+' . preg_quote($mainTable, '/') . '[^$]+)/', $part, $m)) {
                     $subQuery = $m[0];
                     list($subTable, $subAlias) = $this->getTableParts($subQuery);
                     if ($subTable == $mainTable && !preg_match('/ORDER BY/i', $subQuery)) {
                         $orderParts = array();
                         $newSub = $subQuery;
                         foreach ($fields as $field) {
                             $newOrder = preg_replace('/^\\w+\\./', $subAlias . '.', $field);
                             $newField = preg_replace('/\\s+.+$/', '', $newOrder);
                             $orderParts[] = $newOrder;
                             $newSub = preg_replace('/^\\s*(SELECT\\s+(DISTINCT)?\\s*)' . $subAlias . '/i', '\\1 ' . $newField . ', ' . $subAlias, $subQuery);
                         }
                         $newSub = $newSub . ' ORDER BY ' . implode(', ', $orderParts);
                         $query = preg_replace('/' . preg_quote($subQuery, '/') . '/', $newSub, $query);
                     }
                     break;
                 }
             }
         }
     }
     return $query;
 }
 /**
  * Default groups used by this provider
  * @return array List of groups
  */
 public function getDefaultGroups()
 {
     $string = $this->getParameter('auth_groups');
     if ($string) {
         return AppKitArrayUtil::trimSplit($string);
     }
 }
 /**
  * Apply users to the cronk, owner
  * shared principals from groups, ...
  *
  * @param Cronk $cronk
  * @param string $roles
  * @return Cronk the modified record
  */
 private function cronkBuildPrincipalDependencies(Cronk $cronk, $roles)
 {
     $parr = array();
     /*
      * Adding existing user principals back to
      * cronk ( == owner of the cronk)
      */
     foreach ($cronk->CronkPrincipalCronk as $cpc) {
         if ($cpc->NsmPrincipal->principal_user_id) {
             $parr[] = $cpc->NsmPrincipal->principal_id;
         }
     }
     /*
      * If no user principals available, this must be new:
      * -> defining a new owner of the object
      */
     if (count($parr) <= 0) {
         $parr = array($this->user->principal->principal_id);
     }
     $rarr = AppKitArrayUtil::trimSplit($roles, ',');
     $cronk->CronkPrincipalCronk->delete();
     if (is_array($rarr)) {
         $principals = AppKitDoctrineUtil::createQuery()->select('p.principal_id')->from('NsmPrincipal p')->innerJoin('p.NsmRole r')->andWhereIn('r.role_id', $rarr)->execute();
         foreach ($principals as $principal) {
             $parr[] = (int) $principal->principal_id;
         }
     }
     $principals = AppKitDoctrineUtil::createQuery()->select('p.principal_id')->from('NsmPrincipal p')->andWhereIn('p.principal_id', $parr)->execute();
     foreach ($principals as $principal) {
         $cronk->NsmPrincipal[] = $principal;
     }
     /*
      * If the cronk is new,
      * no native owner record is set, do this!
      */
     if (!$cronk->NsmUser->user_id) {
         $cronk->NsmUser = $this->user;
     }
     return $cronk;
 }
 /**
  * Append customvariables to query
  */
 private function parseCustomVariables()
 {
     if (!empty($this->view['customvariables'])) {
         foreach ($this->view['customvariables'] as $customvariable) {
             $params = $customvariable['params'];
             $variables = AppKitArrayUtil::trimSplit($params['list']);
             if (!isset($params['leftJoin'])) {
                 continue;
             }
             $leftJoin = $params['leftJoin'];
             $prefix = 'cv_';
             if (isset($params['prefix'])) {
                 $prefix = $params['prefix'];
             }
             foreach ($variables as $variable) {
                 $canonicalName = strtolower($variable);
                 $safeName = $prefix . $canonicalName;
                 $this->currentQuery->leftJoin($leftJoin . ' AS ' . $safeName . ' WITH ' . $safeName . '.varname=?', $variable);
                 $this->currentQuery->addSelect($safeName . '.varvalue AS value');
                 $filterName = $canonicalName . '_value';
                 $this->view['filter'][$filterName] = array('name' => $filterName, 'type' => 'dql', 'calls' => array(array('type' => 'resolve', 'arg' => $safeName . '.varvalue')));
                 $this->resolvedCustomVariables[$filterName] = $safeName . '.varvalue';
             }
         }
     }
 }