Ejemplo n.º 1
0
 /**
  * @param Parameters $params
  * @param Builder $queryBuilder
  *
  * @return Builder
  */
 public function createQuery($params, $queryBuilder)
 {
     $this->value = $params->toArray();
     /*
      * search jobs by keywords
      */
     if (isset($this->value['params']['search']) && !empty($this->value['params']['search'])) {
         $search = strtolower($this->value['params']['search']);
         $expression = $queryBuilder->expr()->operator('$text', ['$search' => $search]);
         $queryBuilder->field(null)->equals($expression->getQuery());
     }
     if (isset($this->value['params']['status']) && !empty($this->value['params']['status'])) {
         if ($this->value['params']['status'] != 'all') {
             $queryBuilder->field('status.name')->equals($this->value['params']['status']);
         }
     } else {
         $queryBuilder->field('status.name')->equals(Status::CREATED);
     }
     if (isset($this->value['params']['companyId']) && !empty($this->value['params']['companyId'])) {
         $queryBuilder->field('organization')->equals(new \MongoId($this->value['params']['companyId']));
     }
     if (isset($this->value['sort'])) {
         foreach (explode(",", $this->value['sort']) as $sort) {
             $queryBuilder->sort($this->filterSort($sort));
         }
     }
     return $queryBuilder;
 }
Ejemplo n.º 2
0
 public function testParametersPersistNameAndValues()
 {
     $parameters = new Parameters(array('foo' => 'bar'));
     $this->assertEquals('bar', $parameters['foo']);
     $this->assertEquals('bar', $parameters->foo);
     $parameters->offsetSet('baz', 5);
     $this->assertEquals(5, $parameters->baz);
     $parameters->fromArray(array('bar' => 'foo'));
     $this->assertEquals('foo', $parameters->bar);
     $parameters->fromString('bar=foo&five=5');
     $this->assertEquals('foo', $parameters->bar);
     $this->assertEquals('5', $parameters->five);
     $this->assertEquals(array('bar' => 'foo', 'five' => '5'), $parameters->toArray());
     $this->assertEquals('bar=foo&five=5', $parameters->toString());
     $parameters->fromArray(array());
     $parameters->set('foof', 'barf');
     $this->assertEquals('barf', $parameters->get('foof'));
     $this->assertEquals('barf', $parameters->foof);
 }
Ejemplo n.º 3
0
 public function handleSort(Manager $manager, Parameters $request, $defaultSort, $target)
 {
     $user = $manager->isLoggedIn();
     $requestParams = $request->toArray();
     if ($user) {
         //in case user changed the the sort settings on the result list with a specialized UI control
         //we want to serialize the new value in database
         if (array_key_exists('sortControlElement', $requestParams)) {
             if (array_key_exists('sort', $requestParams)) {
                 $sort = $requestParams['sort'];
                 $dbSort = unserialize($user->default_sort);
                 $dbSort[$target] = $requestParams['sort'];
                 $user->default_sort = serialize($dbSort);
                 $user->save();
             } else {
                 $tSort = $request->get('sort');
                 $sort = !empty($tSort) ? $tSort : $defaultSort;
             }
         } else {
             $tSort = $request->get('sort');
             $sort = !empty($tSort) ? $tSort : $defaultSort;
             //overwrite sort if value is set in database
             if ($user->default_sort) {
                 $userDefaultSort = unserialize($user->default_sort);
                 if (isset($userDefaultSort[$target])) {
                     $sort = $userDefaultSort[$target];
                 }
             }
         }
     } else {
         $sort = $request->get('sort');
     }
     // Check for special parameter only relevant in RSS mode:
     if ($request->get('skip_rss_sort', 'unset') != 'unset') {
         $this->skipRssSort = true;
     }
     return $sort;
 }
Ejemplo n.º 4
0
 /**
  * init
  *
  * Called at the end of the Search Params objects' initFromRequest() method.
  * This method is responsible for setting search parameters needed by the
  * recommendation module and for reading any existing search parameters that may
  * be needed.
  *
  * @param \VuFind\Search\Base\Params $params  Search parameter object
  * @param \Zend\StdLib\Parameters    $request Parameter object representing user
  * request.
  *
  * @return void
  */
 public function init($params, $request)
 {
     $this->issetFormat = $params->hasFilter($this->filter);
     $this->params = $request->toArray();
     // process params
     if (isset($this->params['daterange']) && is_array($this->params['daterange'])) {
         for ($i = 0; $i < count($this->params['daterange']); ++$i) {
             if ($this->params['daterange'][$i] == $this->filterName) {
                 unset($this->params['daterange'][$i]);
             }
         }
     }
     // remove daterange if it is empty
     if (empty($this->params['daterange'])) {
         unset($this->params['daterange']);
     }
     // save values
     $this->fromValue = $this->params[$this->filterName . 'from'];
     $this->toValue = $this->params[$this->filterName . 'to'];
     // remove values from params
     unset($this->params[$this->filterName . 'from']);
     unset($this->params[$this->filterName . 'to']);
 }
Ejemplo n.º 5
0
 /**
  * Log an exception triggered by ZF2 for administrative purposes.
  *
  * @param \Exception              $error  Exception to log
  * @param \Zend\Stdlib\Parameters $server Server metadata
  *
  * @return void
  */
 public function logException($error, $server)
 {
     // We need to build a variety of pieces so we can supply
     // information at five different verbosity levels:
     $baseError = $error->getMessage();
     $referer = $server->get('HTTP_REFERER', 'none');
     $basicServer = '(Server: IP = ' . $server->get('REMOTE_ADDR') . ', ' . 'Referer = ' . $referer . ', ' . 'User Agent = ' . $server->get('HTTP_USER_AGENT') . ', ' . 'Host = ' . $server->get('HTTP_HOST') . ', ' . 'Request URI = ' . $server->get('REQUEST_URI') . ')';
     $detailedServer = "\nServer Context:\n" . print_r($server->toArray(), true);
     $basicBacktrace = $detailedBacktrace = "\nBacktrace:\n";
     if (is_array($error->getTrace())) {
         foreach ($error->getTrace() as $line) {
             if (!isset($line['file'])) {
                 $line['file'] = 'unlisted file';
             }
             if (!isset($line['line'])) {
                 $line['line'] = 'unlisted';
             }
             $basicBacktraceLine = $detailedBacktraceLine = $line['file'] . ' line ' . $line['line'] . ' - ' . (isset($line['class']) ? 'class = ' . $line['class'] . ', ' : '') . 'function = ' . $line['function'];
             $basicBacktrace .= "{$basicBacktraceLine}\n";
             if (!empty($line['args'])) {
                 $args = [];
                 foreach ($line['args'] as $i => $arg) {
                     $args[] = $i . ' = ' . $this->argumentToString($arg);
                 }
                 $detailedBacktraceLine .= ', args: ' . implode(', ', $args);
             } else {
                 $detailedBacktraceLine .= ', args: none.';
             }
             $detailedBacktrace .= "{$detailedBacktraceLine}\n";
         }
     }
     $errorDetails = [1 => $baseError, 2 => $baseError . $basicServer, 3 => $baseError . $basicServer . $basicBacktrace, 4 => $baseError . $detailedServer . $basicBacktrace, 5 => $baseError . $detailedServer . $detailedBacktrace];
     $this->log(BaseLogger::CRIT, $errorDetails);
 }
 /**
  * Parse upload content from a content stream
  *
  * @param  resource $stream
  * @return array
  * @throws Exception\InvalidMultipartContentException
  */
 protected function parseFromStream($stream)
 {
     $data = new Parameters();
     $files = new Parameters();
     $partInProgress = false;
     $inHeader = false;
     $headers = [];
     $header = false;
     $name = false;
     $content = '';
     $file = [];
     $filename = false;
     $mimeType = false;
     $tmpFile = false;
     $partBoundaryPatternStart = '/^--' . $this->boundary . '(--)?/';
     $partBoundaryPatternEnd = '/^--' . $this->boundary . '--$/';
     while (false !== ($line = fgets($stream))) {
         $trimmedLine = rtrim($line);
         if (preg_match($partBoundaryPatternStart, $trimmedLine)) {
             if ($partInProgress) {
                 // Time to handle the data we've already parsed!
                 // Data
                 if (!$filename) {
                     $data->set($name, rtrim($content, "\r\n"));
                 }
                 // File (successful upload so far)
                 if ($filename && $tmpFile) {
                     // Write the last line, stripping the EOL characters
                     if (false === fwrite($tmpFile, rtrim($lastline, "\r\n"))) {
                         // Ooops! error writing the very last line!
                         $file['error'] = UPLOAD_ERR_CANT_WRITE;
                         fclose($tmpFile);
                     } else {
                         // Success! Let's try and guess the MIME type based on the file written
                         fclose($tmpFile);
                         if ($mimeType === 'application/octet-stream' && function_exists('finfo_open')) {
                             $finfo = finfo_open(FILEINFO_MIME_TYPE);
                             $type = finfo_file($finfo, $file['tmp_name']);
                             if (false !== $type) {
                                 $file['type'] = $type;
                             }
                         }
                         // Finally, set the filesize
                         $file['size'] = filesize($file['tmp_name']);
                     }
                 }
                 if ($filename) {
                     // At this point, we can add the file entry, regardless of error condition
                     $files->set($name, $file);
                 }
             }
             // Is this a boundary end? If so, we're done
             if (preg_match($partBoundaryPatternEnd, $trimmedLine)) {
                 // Met the "end" boundary; time to stop!
                 break;
             }
             // New part to parse!
             $partInProgress = true;
             $inHeader = true;
             $headers = [];
             $header = '';
             continue;
         }
         if (!$partInProgress) {
             // We're not inside a part, so do nothing.
             continue;
         }
         if ($inHeader) {
             if (preg_match('/^\\s*$/s', $line)) {
                 // Headers are done; cleanup
                 $inHeader = false;
                 $content = '';
                 $file = ['error' => UPLOAD_ERR_OK];
                 $tmpFile = false;
                 $lastline = null;
                 // Parse headers
                 $name = $this->getNameFromHeaders($headers);
                 if (!$name) {
                     throw new Exception\InvalidMultipartContentException('Missing Content-Disposition header, or Content-Disposition header does not ' . 'contain a "name" field');
                 }
                 $filename = $this->getFilenameFromHeaders($headers);
                 $mimeType = $this->getMimeTypeFromHeaders($headers);
                 continue;
             }
             if (preg_match('/^(?P<header>[a-z]+[a-z0-9_-]+):\\s*(?P<value>.*)$/i', $trimmedLine, $matches)) {
                 $header = strtoupper($matches['header']);
                 $headers[$header] = $matches['value'];
                 continue;
             }
             if (!$header) {
                 throw new Exception\InvalidMultipartContentException('Malformed or missing MIME part header for multipart content');
             }
             $headers[$header] .= $trimmedLine;
             continue;
         }
         // In the body content...
         // Data only; aggregate.
         if (!$filename) {
             $content .= $line;
             continue;
         }
         // If we've had an error already with the upload, continue parsing
         // to the end of the MIME part
         if ($file['error'] !== UPLOAD_ERR_OK) {
             continue;
         }
         // Create a temporary file handle if we haven't already
         if (!$tmpFile) {
             // Sets the file entry
             $file['name'] = $filename;
             $file['type'] = $mimeType;
             $tmpDir = $this->getUploadTempDir();
             if (empty($tmpDir)) {
                 // Cannot ascertain temporary directory; this is an error
                 $file['error'] = UPLOAD_ERR_NO_TMP_DIR;
                 continue;
             }
             $file['tmp_name'] = tempnam($tmpDir, 'zfc');
             $tmpFile = fopen($file['tmp_name'], 'wb');
             if (false === $tmpFile) {
                 // Cannot open the temporary file for writing; this is an error
                 $file['error'] = UPLOAD_ERR_CANT_WRITE;
                 continue;
             }
         }
         // Off-by-one operation. Last line must be trimmed, so we'll write
         // the lines one iteration behind.
         if (null === $lastline) {
             $lastline = $line;
             continue;
         }
         if (false === fwrite($tmpFile, $lastline)) {
             $file['error'] = UPLOAD_ERR_CANT_WRITE;
             fclose($tmpFile);
             continue;
         }
         $lastline = $line;
     }
     fclose($stream);
     if ($files->count()) {
         $this->request->setFiles($files);
     }
     return $data->toArray();
 }