/**
  * Checks if $ptr is a valid value for a PTR entry
  *
  * @param $ptr
  * @return bool
  */
 protected function _checkPtr($ptr)
 {
     $transformer = new PtrTransformer();
     if ($ptr == ($t = $transformer->reverseTransform($ptr))) {
         return false;
     }
     return filter_var($t, FILTER_VALIDATE_IP);
 }
 /**
  * @param array $filter
  * @param array $order
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function createSearchQuery(array $filter = array(), array $order = array())
 {
     $qb = $this->createQueryBuilder('d');
     $qb->leftJoin('d.records', 'r');
     $allowed = array('id', 'name', 'search', 'type', 'master', 'account');
     foreach ($filter as $k => $v) {
         if (!in_array($k, $allowed)) {
             continue;
         }
         if (!is_array($v) && 0 == strlen($v)) {
             continue;
         }
         if (is_array($v) && 0 == count($v)) {
             continue;
         }
         if ($k == 'search') {
             $fields = array('d.name', 'd.master', 'd.type', 'r.name', 'r.content');
             $or = $qb->expr()->orX();
             foreach ($fields as $field) {
                 $or->add($qb->expr()->like($field, ':search'));
             }
             $or->add($qb->expr()->like($qb->expr()->concat('r.name', 'd.name'), ':search'));
             $or->add($qb->expr()->like('r.name', ':search_transformed'));
             $transformer = new PtrTransformer();
             $searchTransformed = $transformer->transform($v);
             $qb->andWhere($or)->setParameter('search', '%' . $v . '%')->setParameter('search_transformed', '%' . $searchTransformed . '%');
             continue;
         }
         if ($k == 'type') {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $use = array();
             foreach ($v as $vv) {
                 if (!in_array(strtoupper($vv), array('NATIVE', 'MASTER', 'SLAVE', 'SUPERSLAVE'))) {
                     continue;
                 }
                 $use[] = $vv;
             }
             if (0 == count($use)) {
                 continue;
             }
             $qb->andWhere($qb->expr()->in('d.type', ':type'))->setParameter('type', $use);
             continue;
         }
         if ($k == 'id') {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $intOptions = array("options" => array("min_range" => 0, 'max_range' => 9999999999));
             $use = array();
             foreach ($v as $vv) {
                 if (!filter_var($vv, FILTER_VALIDATE_INT, $intOptions)) {
                     continue;
                 }
                 $use[] = $vv;
             }
             if (0 == count($use)) {
                 continue;
             }
             $qb->andWhere($qb->expr()->in('d.id', ':id'))->setParameter('id', $use);
             continue;
         }
         $qb->andWhere($qb->expr()->eq('d.' . $k, ':' . $k))->setParameter($k, $v);
     }
     if (!is_array($order) || 0 == count($order)) {
         $order = array('name' => 'asc');
     }
     foreach ($order as $k => $v) {
         if (false === in_array($k, $allowed) && $k != 'search') {
             continue;
         }
         $qb->addOrderBy('d.' . $k, $v);
     }
     return $qb;
 }
示例#3
0
 public function getForwardName()
 {
     if (!in_array($this->getType(), array('PTR', 'SOA'))) {
         return null;
     }
     if (null == $this->name) {
         return null;
     }
     $tr = new PtrTransformer();
     return $tr->reverseTransform($this->name);
 }
 /**
  * @param array $filter
  * @param array $order
  *
  * @return \Doctrine\ORM\QueryBuilder
  */
 public function createSearchQuery(array $filter = array(), array $order = array())
 {
     $allowed = array('id', 'name', 'name_exact', 'search', 'type', 'content', 'domain_id', 'managed');
     $filter = $this->_prepareDomainFilter($filter);
     $qb = $this->createQueryBuilder('r')->join('r.domain', 'd');
     foreach ($filter as $k => $v) {
         if (!in_array($k, $allowed)) {
             continue;
         }
         if (!is_array($v) && 0 == strlen($v)) {
             continue;
         }
         if (is_array($v) && 0 == count($v)) {
             continue;
         }
         if ($k == 'search') {
             $or = $qb->expr()->orX();
             $or->add($qb->expr()->like('r.name', ':search'));
             $or->add($qb->expr()->like('r.name', ':search'));
             $or->add($qb->expr()->like($qb->expr()->concat('r.name', $qb->expr()->concat("'.'", 'd.name')), ':search'));
             $qb->setParameter('search', '%' . $v . '%');
             if (filter_var($v, FILTER_VALIDATE_IP)) {
                 $or->add($qb->expr()->like('r.name', ':search_transformed'));
                 $or->add($qb->expr()->like('r.content', ':search_transformed'));
                 $tr = new PtrTransformer();
                 $vv = $tr->transform($v);
                 $qb->setParameter('search_transformed', '%' . $vv . '%');
             }
             $qb->andWhere($or);
             continue;
         }
         if ($k == 'type') {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $use = array();
             foreach ($v as $vv) {
                 if (0 == strlen($vv)) {
                     continue;
                 }
                 $use[] = $vv;
             }
             if (0 == count($use)) {
                 continue;
             }
             $qb->andWhere($qb->expr()->in('r.type', ':type'));
             $qb->setParameter('type', $use);
             continue;
         }
         if ($k == 'id') {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $intOptions = array("options" => array("min_range" => 0));
             $use = array();
             foreach ($v as $vv) {
                 if (!filter_var($vv, FILTER_VALIDATE_INT, $intOptions)) {
                     continue;
                 }
                 $use[] = $vv;
             }
             if (0 == count($use)) {
                 continue;
             }
             $qb->andWhere($qb->expr()->in('r.id', ':id'));
             $qb->setParameter('id', $use);
             continue;
         }
         if ($k == 'name') {
             // Name is only exact when $v is an ip
             if (filter_var($v, FILTER_VALIDATE_IP)) {
                 $qb->andWhere($qb->expr()->eq('r.name', ':name'));
                 $transformer = new PtrTransformer();
                 $name_transformed = $transformer->transform($v);
                 $qb->setParameter('name', $name_transformed);
                 continue;
             }
             $or = $qb->expr()->orX();
             $or->add($qb->expr()->like('r.name', ':name'));
             $or->add($qb->expr()->like($qb->expr()->concat('r.name', $qb->expr()->concat("'.'", 'd.name')), ':name'));
             $qb->andWhere($or)->setParameter('name', '%' . $v . '%');
             continue;
         }
         if ($k == 'name_exact') {
             if (filter_var($v, FILTER_VALIDATE_IP)) {
                 $qb->andWhere($qb->expr()->eq('r.name', ':name_exact'));
                 $transformer = new PtrTransformer();
                 $name_transformed = $transformer->transform($v);
                 $qb->setParameter('name_exact', $name_transformed);
                 continue;
             }
             $or = $qb->expr()->orX();
             $or->add($qb->expr()->eq('r.name', ':name_exact'));
             $or->add($qb->expr()->eq($qb->expr()->concat('r.name', $qb->expr()->concat("'.'", 'd.name')), ':name_exact'));
             $qb->andWhere($or)->setParameter('name_exact', $v);
             continue;
         }
         if ($k == 'content') {
             $qb->andWhere($qb->expr()->like('r.content', ':content'));
             $qb->setParameter('content', '%' . $v . '%');
             continue;
         }
         if ($k == 'domain_id') {
             if (!is_array($v)) {
                 $v = array($v);
             }
             $domains = array();
             foreach ($v as $domain_id) {
                 if (!filter_var($domain_id, FILTER_VALIDATE_INT, array('options' => array('min_range' => 0)))) {
                     continue;
                 }
                 $domains[] = $domain_id;
             }
             if (0 == count($domains)) {
                 continue;
             }
             $qb->andWhere($qb->expr()->in('d.id', ':domain_id'));
             $qb->setParameter('domain_id', $domains);
             continue;
         }
         if ($k == 'managed') {
             if ($v == 0) {
                 $or = $qb->expr()->orX();
                 $or->add($qb->expr()->isNull('r.managed'));
                 $or->add($qb->expr()->eq('r.managed', ':managed'));
                 $qb->andWhere($or);
                 $qb->setParameter('managed', 0);
                 continue;
             }
             if ($v == 1) {
                 $qb->andWhere($qb->expr()->eq('r.managed', ':managed'));
                 $qb->setParameter('managed', 1);
                 continue;
             }
         }
     }
     if (is_array($order) && 0 != count($order)) {
         $allowedOrder = array('domain' => 'd.id', 'name' => 'r.name', 'type' => 'r.type', 'content' => 'r.content', 'domain_name' => 'd.name');
         $use = array();
         foreach ($allowedOrder as $k => $f) {
             if (!array_key_exists($k, $order)) {
                 continue;
             }
             $d = strtolower($order[$k]) == 'asc' ? 'asc' : 'desc';
             $qb->addOrderBy($f, $d);
         }
     }
     return $qb;
 }