public function testMatchAll() { $this->assertEquals(array("#str rst#", "str", "rst"), StrUtil::matchAll('@#([a-z]*) ([a-z]*)#@', 'yxa#str rst#bxy')); // named patterns $this->assertEquals(array("#str rst#", "str", "rst", "first" => "str", "second" => "rst"), StrUtil::matchAll('@#(?P<first>[a-z]*) (?P<second>[a-z]*)#@', 'yxa#str rst#bxy')); $this->assertEquals(false, StrUtil::matchAll('@x(.*)x@', 'yyabxy')); }
/** * Sets value for attribute in associative array $list. * * @param string $attribute * @param array $values * */ private function setAttribute(Trace $trace, $attribute, $values) { $new_values = array(); foreach ($values as $value) { if ($value != '') { $new_values[] = $value; } } $values = $new_values; if (count($values) == 0) { $trace->tlog("Ignoring empty attribute '" . $attribute . "'"); return; } $attribute = trim($attribute); if (StrUtil::endsWith($attribute, ':')) { $attribute = substr($attribute, 0, strlen($attribute) - 1); } $id = null; $name = $attribute; if (isset($this->attr_def[$attribute])) { $id = $this->attr_def[$attribute]; $name = InformacnyListAttributeEnum::getUnicodeName($id); } if ($id == self::IGNORE_ATTRIBUTE) { $trace->tlog("Ignoring attribute '" . $attribute . "'"); return; } $this->list[] = array('id' => $id, 'name' => $name, 'rawLabel' => $attribute, 'values' => $values); }
/** * Parses the AIS2 version from html page. * * @param string $html AIS2 html reply to be parsed * * @returns AIS2Version AIS2 version * @throws ParseException on error */ public function parseVersionStringFromMainPage($html) { Preconditions::checkIsString($html); $data = StrUtil::matchAll(self::VERSION_PATTERN, $html); if ($data === false) { throw new ParseException("Cannot parse AIS version from response."); } return new AIS2Version(2, $data['major'], $data['minor'], $data['patch']); }
/** * Parses user name from AIS2 start page * * @param string $html AIS2 html reply to be parsed * * @returns AIS2Version AIS2 version * @throws ParseException on error */ public function parseUserNameFromMainPage($html) { Preconditions::checkIsString($html); $data = StrUtil::matchAll(self::USERNAME_PATTERN, $html); if ($data === false) { throw new ParseException("Cannot parse username from response."); } return $data['username']; }
/** * * @param str predpokladame range v 2 moznych standardnych ais formatoch * - "do [datum a cas]" * - "[datum a cas] do [datum a cas]" * @see parseAISDateTime * @returns array('od'=>timestamp, 'do'=>timestamp) */ public static function parseAISDateTimeRange($str) { $pattern = '@(?P<od>[0-9:. ]*)do (?P<do>[0-9:. ]*)@'; $data = StrUtil::matchAll($pattern, $str); $result = array(); if ($data['od'] == '') { $result['od'] = null; } else { $result['od'] = self::parseAISDateTime($data['od']); } $result['do'] = self::parseAISDateTime($data['do']); return $result; }
/** * Ensure that all fake data we use are valid FileStorage * data files. This should ensure, that fake backend and * other services depending on fake data will not encounter * broken data. */ public function testAllData() { $dir = FakeData::getDirectory(); $storage = new FileStorage(array("root_path" => $dir . '/')); $it = new RecursiveDirectoryIterator($dir); $nonrecursiveIt = new RecursiveIteratorIterator($it); $regexIt = new RegexIterator($nonrecursiveIt, '@\\.dat$@'); foreach ($regexIt as $file) { $this->assertTrue($file->isFile()); $key = $file->getPathname(); assert(StrUtil::startsWith($key, $dir)); // remove $dir from beginning of $key $key = substr($key, strlen($dir)); $key = preg_replace("@\\.dat\$@", "", $key); $data = $storage->read($key); $this->assertTrue(null !== $data); } }
public function prihlasNaTermin(Trace $trace, $terminIndex) { $table = $this->components['zoznamTerminovTable_dataView']; $table->selectSingleRow((int) $terminIndex); $table->setActiveRow((int) $terminIndex); $data = $this->doAction('enterAction'); $data = $data->getElementById("init-data"); $data = $data->textContent; $error = StrUtil::match('@webui\\(\\)\\.messageBox\\("([^"]*)",@', $data); if ($error) { throw new Exception('Nepodarilo sa prihlásiť na zvolený termín.<br/>' . 'Dôvod: <b>' . $error . '</b>'); } if (!preg_match("@dm\\(\\)\\.closeDialog\\(" . "\"VSES206_VyberTerminuHodnoteniaDlg1\"\\);@", $data)) { throw new Exception("Problém pri prihlasovaní: " . "Neočakávaná odozva od AISu"); } $this->closeWindow(); return true; }
public function login(AIS2ServerConnection $serverConnection) { $connection = $serverConnection->getHttpConnection(); $login = $this->username; $password = $this->password; if ($login === null && $password === null) { throw new Exception("S týmto objektom nie je možné sa prihlásiť 2x. " . "Meno a heslo boli vymazané pri prvom prihlásení."); } // Username a password si nebudeme pamatat dlhsie ako treba $this->username = null; $this->password = null; // TODO(ppershing): why is there this line? Needed for some cookies? $this->isLoggedIn($serverConnection); $data = $connection->post(new NullTrace(), self::COSIGN_LOGIN, array('ref' => '', 'login' => $login, 'password' => $password)); if (!preg_match(parent::LOGGED_ALREADY_PATTERN, $data)) { if (($reason = StrUtil::match(self::COSIGN_ERROR_PATTERN1, $data)) || ($reason = StrUtil::match(self::COSIGN_ERROR_PATTERN2, $data)) || ($reason = StrUtil::match(self::COSIGN_ERROR_PATTERN3, $data)) || ($reason = StrUtil::match(self::IIKS_ERROR, $data))) { throw new LoginException('Nepodarilo sa prihlásiť, dôvod: <b>' . $reason . '</b>'); } throw new LoginException('Nepodarilo sa prihlásiť, dôvod neznámy.'); } return true; }
public function serialize($var) { if (is_int($var)) { return self::SER_INT . pack('N', $var); } else { if (is_array($var)) { $serialized = self::SER_ARRAY; $serialized .= pack('N', count($var)); foreach ($var as $key => $value) { $serialized .= $this->serialize($key); $serialized .= $this->serialize($value); } return $serialized; } else { if ($var === null) { return self::SER_NULL; } else { $var = (string) $var; $serialized = self::SER_STRING; $serialized .= pack('N', StrUtil::byteLength($var)); $serialized .= $var; return $serialized; } } } }
/** * Extract Table data from DOMDocument * * @param Trace $trace for creating logs, tracking activity * @param $dom DOMDocument part of ais2ResponseHTML which contain Table * @returns array(string=>array(string)) Returns rows of Table, where index is rowId */ private function getTableDataFromDom(Trace $trace, DOMDocument $dom) { Preconditions::checkNotNull($dom); $tdata = array(); $trace->tlog("Finding element with id dataTabBody0"); $element = $dom->getElementById('dataTabBody0'); if ($element == null) { throw new ParseException("Can't find table data"); } $trace->tlog("Element found"); foreach ($element->childNodes as $aisRow) { assert($aisRow->tagName == "tr"); assert($aisRow->hasAttribute("id")); assert($aisRow->hasChildNodes()); // TODO: asserty prerobit na exceptiony $row = array(); $trace->tlog("Extracting row id."); $rowId = $aisRow->getAttribute("id"); $index = StrUtil::match('@^row_([0-9]+)$@', $rowId); if ($index === false) { throw new ParseException("Unexpected row id format"); } $trace->tlog("Extraction is correct."); $index = intval($index); foreach ($aisRow->childNodes as $ais_td) { assert($ais_td->tagName == "td"); $row[] = $this->getCellContent($ais_td); } $tdata[$index] = $row; } return $tdata; }
private function newException($reason, $url) { return new Exception('<b>Nastala chyba pri requeste.</b><br/>Zdôvodnenie od AISu:' . nl2br(StrUtil::hescape($reason)) . '<br/>Požadovaná url: ' . StrUtil::hescape($url)); }
private function parseParamNameFromResponse($response) { $data = $response->getElementById("init-data"); $data = $data->textContent; $data = StrUtil::match(self::APP_LOCATION_PATTERN, $data, 'params'); if ($data === false) { throw new ParseException("Location of APP_PATTERN failed."); } $data = StrUtil::match(self::PARAM_NAME_PATTERN, $data, 'paramName'); if ($data === false) { throw new ParseException("Parsing of ids from zapisnyListIndex failed."); } return $data; }
public function odhlasZTerminu(Trace $trace, $terminIndex) { // Posleme request ze sa chceme odhlasit. $data = $this->executor->doRequest($trace, array('compName' => 'odstranitTerminAction', 'eventClass' => 'avc.ui.event.AVCActionEvent', 'embObj' => array('terminyTable' => array('dataView' => array('activeIndex' => $terminIndex, 'selectedIndexes' => $terminIndex), 'editMode' => 'false')))); if (!preg_match("@Skutočne chcete odobrať vybraný riadok?@", $data)) { throw new Exception("Problém pri odhlasovaní - neočakávaná odozva AISu"); } // Odklikneme konfirmacne okno ze naozaj. $data = $this->executor->doRequest($trace, array('events' => false, 'app' => false, 'dlgName' => false, 'changedProperties' => array('confirmResult' => 2))); $message = StrUtil::match('@webui\\.messageBox\\("([^"]*)"@', $data); if ($message !== false && $message != 'Činnosť úspešne dokončená.') { throw new Exception("Z termínu sa (pravdepodobne) nepodarilo odhlásiť." . "Dôvod:<br/><b>" . $message . '</b>'); } if (!preg_match("@dm\\(\\).setActiveDialogName\\(" . "'VSES007_StudentZoznamPrihlaseniNaSkuskuDlg0'\\);@", $data)) { throw new Exception("Problém pri odhlasovaní z termínu - neočakávaná odpoveď od AISu"); } return true; }
/** * Joins two or more path components. * If joining more than two path components, the result is * the same as calling two-argument joinPath successively. * Moreover, this function is associative, i.e. * joinPath('a','b','c') has the same effect as * joinPath(joinPath('a', 'b'), 'c') or joinPath('a', joinPath('b', 'c')) * * Path components of zero length are ignored * * @param string $a first path component * @param string $b second path component * @param string ... any other path components to join * @returns string all the paths joined using a directory separator */ public static function joinPath($a, $b) { $args = func_get_args(); $num_args = count($args); $shouldAddDS = true; // start with $a, omit trailing directory separator if ($a == DIRECTORY_SEPARATOR || $a == '') { $shouldAddDS = false; $path = $a; } else { if (StrUtil::endsWith($a, DIRECTORY_SEPARATOR)) { $path = substr($a, 0, strlen($a) - 1); } else { $path = $a; } } // add other components for ($i = 1; $i < $num_args; $i++) { $part = $args[$i]; // DIRECTORY_SEPARATOR or empty string is a special case if ($part == DIRECTORY_SEPARATOR) { continue; } // first extract range of part without leading or trailing DS if (StrUtil::startsWith($part, DIRECTORY_SEPARATOR)) { $start = 1; } else { $start = 0; } if (StrUtil::endsWith($part, DIRECTORY_SEPARATOR)) { $end = strlen($part) - 1; } else { $end = strlen($part); } // append a path component if ($shouldAddDS) { $path .= DIRECTORY_SEPARATOR; } $shouldAddDS = true; $path .= substr($part, $start, $end); } return $path; }
public function getTableData(Trace $trace, DOMDocument $dom) { $data = array(); $trace->tlog("finding tbody element"); $element = $dom->getElementById('dataTabBody0'); if ($element == null) { throw new ParseException("Can't find table data"); } foreach ($element->childNodes as $aisRow) { assert($aisRow->tagName == "tr"); assert($aisRow->hasAttribute("id")); assert($aisRow->hasChildNodes()); // TODO: asserty prerobit na exceptiony $row = array(); $rowId = $aisRow->getAttribute("id"); $index = StrUtil::match('@^row_([0-9]+)$@', $rowId); if ($index === false) { throw new ParseException("Unexpected row id format"); } foreach ($aisRow->childNodes as $ais_td) { assert($ais_td->tagName == "td"); $row[] = $this->getCellContent($ais_td); } $data[$index] = $row; } $trace->tlogVariable("data", $data); return $data; }