/** * Creates a new HttpRequest object based on the given data * @param array $requestData Data of the HTTP request * @return HttpRequest */ public static function create(array $requestData = array()) { $defaultValues = array('REQUEST_TIME' => time(), 'SERVER_PORT' => null, 'SERVER_NAME' => null, 'QUERY_STRING' => null, 'REMOTE_ADDR' => null, 'HTTP_USER_AGENT' => null, 'HTTPS' => null, 'REQUEST_URI' => null, 'HTTP_ACCEPT_LANGUAGE' => null, 'HTTP_ACCEPT_ENCODING' => null, 'REQUEST_METHOD' => null); $requestData = array_merge($defaultValues, $requestData); $httpRequest = new HttpRequest(); $protocol = null; if ($requestData['HTTPS'] !== null) { $protocol = $requestData['HTTPS'] === 'on' ? HttpRequest::PROTOCOL_HTTPS : HttpRequest::PROTOCOL_HTTP; } $uri = StringUtils::startsWith($requestData['REQUEST_URI'], self::$basePath) ? StringUtils::afterFirst($requestData['REQUEST_URI'], self::$basePath) : $requestData['REQUEST_URI']; $path = StringUtils::beforeLast($uri, '?'); $languages = array(); $langRates = array_filter(explode(',', $requestData['HTTP_ACCEPT_LANGUAGE'])); foreach ($langRates as $lr) { list($langCode, $importance) = array_pad(preg_split('/;(?:q=)?/', $lr), 2, 1.0); $languages[$langCode] = (double) $importance; } $acceptedEncoding = array_filter(array_map('trim', explode(',', $requestData['HTTP_ACCEPT_ENCODING']))); $requestTime = new \DateTime(); $requestTime->setTimestamp($requestData['REQUEST_TIME']); $httpRequest->setHost($requestData['SERVER_NAME']); $httpRequest->setPath($path); $httpRequest->setPort($requestData['SERVER_PORT']); $httpRequest->setProtocol($protocol); $httpRequest->setQuery($requestData['QUERY_STRING']); $httpRequest->setURI($uri); $httpRequest->setRequestTime($requestTime); $httpRequest->setAcceptedEncodings($acceptedEncoding); $httpRequest->setRequestMethod($requestData['REQUEST_METHOD']); $httpRequest->setUserAgent($requestData['HTTP_USER_AGENT']); $httpRequest->setAcceptedLanguages($languages); $httpRequest->setRemoteAddress($requestData['REMOTE_ADDR']); $headers = array(); foreach ($_SERVER as $name => $value) { if (StringUtils::startsWith($name, 'HTTP_') === true) { $name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); $headers[$name] = $value; } elseif ($name == 'CONTENT_TYPE') { $headers['Content-Type'] = $value; } elseif ($name == 'CONTENT_LENGTH') { $headers['Content-Length'] = $value; } } $httpRequest->setHeaders($headers); return $httpRequest; }
/** * @param string $tableName * @param DB $db * @param string $sqlQuery The SQL query to get the data for the table * @param string $cssClass The CSS class the HTML table should have * @param bool $displayInfo Displays information of the entries found (on top of the table) * @param int $pageLimit The limit of entries per page */ public function __construct($tableName, DB $db, $sqlQuery, $cssClass = 'table-data', $displayInfo = true, $pageLimit = 25) { $this->selectable = false; $this->tableName = $tableName; $this->orderBy = null; $this->pageLimit = $pageLimit; $this->currentPage = 1; $this->keywords = null; $this->filtersApplied = false; $this->entityWord = 'entry'; $this->entityWordPlural = 'entries'; $this->sqlQuery = StringUtils::beforeLast($sqlQuery, 'ORDER BY'); if (($orderByStr = StringUtils::afterLast($sqlQuery, 'ORDER BY')) !== null) { $res = preg_split('/\\s+/ims', $orderByStr, -1, PREG_SPLIT_NO_EMPTY); $this->orderBy = array('column' => $res[0], 'sort' => isset($res[1]) ? $res[1] : 'ASC'); } $this->columns = null; $this->options = null; $this->db = $db; $this->cssClass = $cssClass; $this->displayInfo = $displayInfo; $this->sortable = null; }
/** * @param string $selector * @param BackendController $backendController * * @return mixed */ protected function getValues($selector, BackendController $backendController) { if (is_string($selector) === true) { return StringUtils::endsWith($selector, '()') ? call_user_func(array($this, StringUtils::beforeLast($selector, '()')), $backendController) : $this->{$selector}; } return $selector; }
public function afterMutation(DB $db, PDOStatement $stmnt, array $params, $queryType) { $cond = null; if (preg_match('/^\\s*(SELECT|UPDATE|DELETE\\s+FROM|INSERT\\s+INTO|INSERT\\s+IGNORE|REPLACE\\s+INTO)\\s+(.+?)\\s+(?:SET\\s+(.+))?(?:ON DUPLICATE KEY UPDATE|WHERE\\s+(.+))?$/ims', $stmnt->queryString, $mT) === false) { throw new CMSException('Revision control could not parse sql statement: ' . $stmnt->queryString); } $sqlFunction = $mT[1]; $sqlTable = trim($mT[2]); $sqlColumns = isset($mT[3]) ? $mT[3] : null; $sqlCond = isset($mT[4]) ? $mT[4] : null; if (in_array($sqlFunction, array('REPLACE INTO', 'INSERT INTO', 'UPDATE', 'INSERT IGNORE'))) { $resPK = $this->getPKColumnsForTable($db, $sqlTable); $pkArray = array(); foreach ($resPK as $pk) { $pkArray[] = $pk . ' = ?'; } // params $cols = explode(',', $sqlColumns); $colsArr = array(); $pkArr = array(); $i = 0; foreach ($cols as $c) { $colClean = trim(preg_replace('/=\\s*\\?/', null, $c)) . ' = ?'; $colsArr[] = $colClean; if (in_array($colClean, $pkArray)) { $pkArr[] = $params[$i]; } ++$i; } $joinedArr = array_intersect($colsArr, $pkArray); if (count($joinedArr) <= 0) { return; } $cond = implode(' AND ', $joinedArr); } elseif (in_array($sqlFunction, array('SELECT'))) { // ignore return; } else { //var_dump($mT); exit; $offsetParams = substr_count(StringUtils::beforeLast($stmnt->queryString, 'WHERE'), '?'); $anzParams = substr_count($sqlCond, '?'); $pkArr = array_slice($params, $offsetParams, $anzParams); $cond = trim($sqlCond); } $revSql = "SELECT * FROM " . $sqlTable . " WHERE " . $cond; try { $stmntRev = $db->prepare($revSql); $resRev = $db->select($stmntRev, $pkArr); if (count($resRev) <= 0) { return; } $xmlStr = "\t" . '<table name="' . $sqlTable . '" exectime="' . date('Y-m-d H:i:s') . '">' . "\n"; $xmlStr .= "\t\t" . '<records>' . "\n"; foreach ($resRev as $r) { $xmlStr .= "\t\t\t" . '<record>' . "\n"; $xmlStr .= "\t\t\t\t" . '<fields>' . "\n"; foreach ($r as $col => $val) { $xmlStr .= "\t\t\t\t\t" . '<field name="' . $col . '"><![CDATA[' . $val . ']]></field>' . "\n"; } $xmlStr .= "\t\t\t\t" . '</fields>' . "\n"; $xmlStr .= "\t\t\t" . '</record>' . "\n"; } $xmlStr .= "\t\t" . '</records>' . "\n"; $xmlStr .= "\t" . '</table>' . "\n"; if ($db->inTransaction() === true) { $this->cachedXML .= $xmlStr; return; } $this->saveOldRevision($xmlStr, $sqlTable . '.' . implode('-', $pkArr)); } catch (DBException $e) { $this->logger->error('Problem with generated statement: ' . $e->getQueryString(), $e); throw $e; } }