public function testTag() { $this->client->get('/tag/lorem'); $this->assertEquals(200, $this->client->response->status()); $html = str_get_dom($this->client->response->body()); $this->assertEquals(1, count($html('div.blog-post'))); }
/** * Returns HTML DOM from file/website * @param string $str * @param bool $return_root Return root node or return parser object * @param bool $use_include_path Use include path search in file_get_contents * @param resource $context Context resource used in file_get_contents (PHP >= 5.0.0) * @return Html5Parser|DomNode */ function file_get_dom($file, $return_root = true, $use_include_path = false, $context = null) { if (version_compare(PHP_VERSION, '5.0.0', '>=')) { $f = file_get_contents($file, $use_include_path, $context); } else { if ($context !== null) { trigger_error('Context parameter not supported in this PHP version'); } $f = file_get_contents($file, $use_include_path); } return $f === false ? false : str_get_dom($f, $return_root); }
function file_get_dom($file, $return_root = true, $use_include_path = false, $context = null) { $ch = curl_init($file); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt_array($ch, $context); $f = curl_exec($ch); if ($f == false) { echo 'Curl error: ' . curl_errno($ch) . ":" . curl_error($ch) . "\n"; } curl_close($ch); return $f === false ? false : str_get_dom($f, $return_root); }
function getPage($memcache, $pdo, $db, $__language, $__country, $path, $directives = null, $session = null, $facebook = null, $config = null) { error_log('getPage: ' . $path); $editMode = false; if (!empty($session->admin) || !empty($session->contentcreator) || !empty($session->developer)) { $editMode = true; } $smarty = new templateVariablesParser(); if (!empty($directives)) { foreach ($directives as $key => $value) { $smarty->set($key, $value); } } if (empty($__language) || empty($__country) || empty($path)) { error_log('getPage called with illegal parameters'); return false; } $oReturn = getRequestDetails($path, true); if (!empty($oReturn->template)) { $template = $oReturn->template; if (!empty($oReturn->fullPath)) { // execute the template's php file if (!empty($oReturn->extension)) { $phpFile = str_ireplace('.' . $oReturn->extension, '.php', $oReturn->fullPath); if (is_file($phpFile)) { include $phpFile; } } } $_path = $path; if (!empty($template)) { // iterate the dom elements that have an id, and replace elements that are found in the database $dom = str_get_dom(implode('', $template)); foreach ($dom("*[id!='']") as $element) { if (!empty($element->id)) { // if the directive '_parent' is set, then translate tags marked for the parent page i.e. set the page to whatever the parent page is // if it is not set, then use the include src as page if (!empty($directives)) { if (!empty($directives['target'])) { switch ($directives['target']) { case 'parent': case '_parent': $_path = $directives['parent']; break; default: $_path = $directives['target']; break; } } } $content = getTagContent($memcache, $pdo, $session, $element->id, $__language, $__country, $session->continent, $_path, 5); if ($content !== false) { $element->setOuterText($content); } } } // evaluate condition // to enable conditions we have the following attributes available // tag must contain a condition attr like: condition="$session->language == 'en'" // we enable or disable the use of conditions in the config file if (defined('__CONDITIONS__')) { if (__CONDITIONS__) { foreach ($dom('[condition]') as $element) { $condition = html_entity_decode($element->condition, ENT_QUOTES, 'UTF-8'); error_log($condition); $evaluated = false; if (eval($condition) === true) { $evaluated = true; error_log('condition = true'); } else { error_log('condition = false'); } if (!$evaluated) { $element->detach(); } $element->deleteAttribute('condition'); } } } // get the smarty vars from the new template and replace the vars with content from the underlying php // (index.html executes an index.php in that same directory $template = $smarty->parse($pdo, $memcache, $session, $__language, $__country, $session->region, $_path, (string) $dom); unset($smarty); // include css. (inline them if the inline tag is set), compile the scss into css, cache the css (or get from cache) $dom = str_get_dom($template); foreach ($dom('link') as $element) { if (!empty($element->href) && !empty($element->inline)) { // check if src translates to a local css or scss file tha we can serve if (strpos($element->href, '//') === false) { $parts = explode('.', $element->href); if (strtolower($parts[sizeof($parts) - 1]) == 'css' || strtolower($parts[sizeof($parts) - 1]) == 'scss') { $scss = false; if (strtolower($parts[sizeof($parts) - 1]) == 'scss') { $scss = new scssc(); } $fn = implode('.', $parts); if ($fn[0] !== '/') { $fullPathContent = DOCUMENT_ROOT . '/' . $fn; } else { $fullPathContent = DOCUMENT_ROOT . $fn; } $content = false; if (empty($element->nocache)) { $content = $memcache->get($session->id . '_' . $fullPathContent); } if (empty($content)) { $partsPHP = $parts; $partsPHP[sizeof($partsPHP) - 1] = 'php'; $fn = implode('.', $partsPHP); if ($fn[0] !== '/') { $fullPathPHP = DOCUMENT_ROOT . '/' . $fn; } else { $fullPathPHP = DOCUMENT_ROOT . $fn; } $templateParser = new templateVariablesParser(); if (is_file($fullPathPHP)) { include $fullPathPHP; } $parsed = false; if (is_file($fullPathPHP)) { include $fullPathPHP; $parsed = true; } $content = file_get_contents(trim($fullPathContent)); if ($parsed) { $content = $templateParser->parseMinimal($content); } if (!empty($scss)) { $content = $scss->compile($content); } if (empty($element->nocache)) { $memcache->set($session->id . '_' . $fullPathContent, $content); } unset($templateParser); $element->setOuterText("<style>" . $content . "</style>"); } else { $element->setOuterText("<style>" . $content . "</style>"); } } } } } // include script. (inline if attribute is set) foreach ($dom('script') as $element) { if (!empty($element->src) && !empty($element->inline)) { if (strpos($element->src, "//") === false) { $parts = explode('.', $element->src); if (strtolower($parts[sizeof($parts) - 1]) == 'js') { $fn = implode('.', $parts); if ($fn[0] !== '/') { $fullPathContent = DOCUMENT_ROOT . '/' . $fn; } else { $fullPathContent = DOCUMENT_ROOT . $fn; } $content = false; if (empty($element->nocache)) { $content = $memcache->get($session->id . '_' . $fullPathContent); } $content = $memcache->get($session->id . '_' . $fullPathContent); if (empty($content)) { $partsPHP = $parts; $partsPHP[sizeof($partsPHP) - 1] = 'php'; $fn = implode('.', $partsPHP); if ($fn[0] !== '/') { $fullPathPHP = DOCUMENT_ROOT . '/' . $fn; } else { $fullPathPHP = DOCUMENT_ROOT . $fn; } $templateParser = new templateVariablesParser(); $parsed = false; if (is_file($fullPathPHP)) { include $fullPathPHP; $parsed = true; } $content = file_get_contents(trim($fullPathContent)); if ($parsed) { $content = $templateParser->parseMinimal($content); } $element->setOuterText("<script>" . $content . "</script>"); if (empty($element->nocache)) { $memcache->set($session->id . '_' . $fullPathContent, $content); } unset($templateParser); } else { $element->setOuterText("<script>" . $content . "</script>"); } } } } } // handle include elements // // Normally the internal template variables are saved in the db under the snippet src name // There are instances that we would like to use the parent as the as the source of translation // In that case use the target="_parent" attribute // We can also specify _parent or _self or XXXX for another page name. foreach ($dom('include') as $element) { $language = $session->language; $country = $session->country; if (!empty($element->language)) { $language = $element->language; } if (!empty($element->country)) { $country = $element->country; } $directives = array(); if (!empty($element->target)) { $directives['parent'] = $path; $directives['target'] = $element->target; } if (!empty($element->src)) { $content = getPage($memcache, $pdo, $db, $language, $country, $element->src, $directives, $session, $facebook, $config); if (!empty($content)) { if ($editMode) { $includeDom = str_get_dom($content); // below foreach is required for editing purposes. (When we edit we need to know which file we need to save) foreach ($includeDom("*[id!='']") as $_element) { $_element->addAttribute('data-src', $element->src); } $element->setOuterText((string) $includeDom); } else { $element->setOuterText($content); } } else { $element->setOuterText(''); error_log('Include element not found. path: ' . $element->src); } } } return (string) $dom; } return ''; } else { error_log('** NOT FOUND ** domain: ' . $_SERVER['SERVER_NAME'] . ', request: ' . $path . ', language: ' . $__language . ', country: ' . $__country); return false; } }
protected function compare() { $bench = new Ubench(); $url = 'tests/templated-retrospect/index.html'; $file = 'test.html'; if (!file_exists($file)) { $htmlstr = file_get_contents($url); file_put_contents($file, $htmlstr); } $htmlstr = file_get_contents($file); $this->log('', true); $this->log('Measuring Simple HTML DOM Parser...'); $resultsSimpleHtmlDomParser = $bench->run(function ($htmlstr) { $results = []; $html = HtmlDomParser::str_get_html($htmlstr); $html->find('title', 0)->innertext('New Title'); $results[1] = $html->__toString(); $tpl = HtmlDomParser::str_get_html(file_get_contents('tests/templated-retrospect/index.html')); foreach ($tpl->find('link') as $elem) { $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href; } foreach ($tpl->find('img, script') as $elem) { $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src; } $results[2] = $tpl->__toString(); return $results; }, $htmlstr); //$this->log('distance: ' . similar_text($htmlstr, $result)); $this->logBench($bench); $this->log('', true); $this->log('Measuring XParser...'); $resultsXParser = $bench->run(function ($htmlstr) { $results = []; $html = new XNode($htmlstr); $html->find('title')->inner('New Title'); $results[1] = $html->__toString(); $tpl = new XNode(file_get_contents('tests/templated-retrospect/index.html')); foreach ($tpl('link') as $elem) { $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href; } foreach ($tpl('img, script') as $elem) { $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src; } $results[2] = $tpl->__toString(); return $results; }, $htmlstr); //$this->log('distance: ' . similar_text($htmlstr, $result)); $this->logBench($bench); $this->log('', true); $this->log('Measuring Ganon...'); $resultsGanon = $bench->run(function ($htmlstr) { $html = str_get_dom($htmlstr); foreach ($html('title') as $title) { $title->setInnerText('New Title'); } $results[1] = $html->__toString(); $tpl = new XNode(file_get_contents('tests/templated-retrospect/index.html')); foreach ($tpl('link') as $elem) { $elem->href = '//localhost/xparser/tests/templated-retrospect/' . $elem->href; } foreach ($tpl('img, script') as $elem) { $elem->src = '//localhost/xparser/tests/templated-retrospect/' . $elem->src; } $results[2] = $tpl->__toString(); return $results; }, $htmlstr); //$this->log('distance: ' . similar_text($htmlstr, $result)); $this->logBench($bench); $this->log('', true); $this->log('Symfony CSS Selector combined with DOMDocument and DOMXPath...'); $resultsXParser = $bench->run(function ($htmlstr) { $results = []; $html = new DOMDocument(); libxml_use_internal_errors(true); $html->loadHTML($htmlstr); $converter = new CssSelectorConverter(); $xpath = new DOMXPath($html); $elements = $xpath->query($converter->toXPath('title')); foreach ($elements as $element) { $element->innserHTML = 'New Title'; } $results[1] = $html->saveHTML(); $tpl = new DOMDocument(); $tpl->load('tests/templated-retrospect/index.html'); foreach ($xpath->query($converter->toXPath('link')) as $elem) { $elem->setAttribute('href', '//localhost/xparser/tests/templated-retrospect/' . $elem->getAttribute('href')); } foreach ($xpath->query($converter->toXPath('img, script')) as $elem) { $elem->setAttribute('src', '//localhost/xparser/tests/templated-retrospect/' . $elem->getAttribute('src')); } $results[2] = $tpl->saveHTML(); return $results; }, $htmlstr); //$this->log('distance: ' . similar_text($htmlstr, $result)); $this->logBench($bench); $this->log('', true); $this->log('Simple HTML DOM Parser vs Ganon distance: ' . similar_text($resultsSimpleHtmlDomParser[2], $resultsGanon[2])); $this->log('Simple HTML DOM Parser vs XParser distance: ' . similar_text($resultsSimpleHtmlDomParser[2], $resultsXParser[2])); $this->log('Ganon vs XParser distance: ' . similar_text($resultsGanon[2], $resultsXParser[2])); $this->log('', true); $this->log('', true); }
/** * */ public static function parse($ps_template, $pa_options = null) { $vs_cache_key = md5($ps_template); if (isset(DisplayTemplateParser::$template_cache[$vs_cache_key])) { return DisplayTemplateParser::$template_cache[$vs_cache_key]; } $ps_template_original = $ps_template; // Parse template $o_doc = str_get_dom($ps_template); $ps_template = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_doc->html())); // replace template with parsed version; this allows us to do text find/replace later $va_tags = DisplayTemplateParser::_getTags($o_doc->children, array_merge($pa_options, ['maxLevels' => 1])); if (!is_array(DisplayTemplateParser::$template_cache)) { DisplayTemplateParser::$template_cache = []; } return DisplayTemplateParser::$template_cache[$vs_cache_key] = ['original_template' => $ps_template_original, 'template' => $ps_template, 'tags' => $va_tags, 'tree' => $o_doc]; }
public static function parseProfile($page) { $html = str_get_dom($page); $name = $html('table'); $data = array('student' => array('name' => $name[0]('tr td')[0]->getPlainText(), 'studentnumber' => $name[0]('tr td')[2]->getPlainText(), 'class' => $name[0]('tr td')[4]->getPlainText(), 'birthdate' => $name[1]('tr td')[1]->getPlainText(), 'phonenumbers' => array('home' => $name[1]('tr td')[3]->getPlainText(), 'mobile' => $name[1]('tr td')[5]->getPlainText())), 'address' => array('street' => $name[2]('tr td')[2]->getPlainText(), 'zipcode' => $name[2]('tr td')[4]->getPlainText(), 'residence' => $name[2]('tr td')[6]->getPlainText()), 'mentor' => array('name' => $name[3]('tr td')[2]->getPlainText(), 'abbreviation' => $name[3]('tr td')[4]->getPlainText(), 'email' => $name[3]('tr td')[6]->getPlainText()), 'profile' => array('profile' => $name[4]('tr td')[2]->getPlainText(), 'code' => $name[4]('tr td')[4]->getPlainText(), 'abbreviation' => $name[4]('tr td')[6]->getPlainText(), 'year' => $name[4]('tr td')[8]->getPlainText())); return $data; }
$opciones = array('http' => array('method' => "GET", 'header' => "Accept-language: es\r\n" . 'User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3B48b Safari/419.3')); $contexto = stream_context_create($opciones); $content = file_get_contents($loginUrl, false, $contexto); $html = str_get_dom($content); $inputs = $html('#form_search input'); $url = $loginUrl . '?'; $notparse = array('checkin_monthday' => '31', 'checkin_year_month' => '2013-10', 'checkout_year_month' => '2013-11', 'checkout_monthday' => '1', 'ci_date' => '2013-10-31', 'co_date' => '2013-11-1', 'search_form_id' => '192a1bab236301f7'); foreach ($inputs as $input) { if (!array_key_exists($input->name, $notparse)) { $url .= $input->name . '=' . urlencode($input->value) . '&'; } else { $url .= $input->name . '=' . urlencode($notparse[$input->name]) . '&'; } } $content = file_get_contents($url, false, $contexto); $html = str_get_dom($content); $apartamentos = $html('#bookForm ul li'); foreach ($apartamentos as $a) { echo '<h1>titulo</h1>'; $titulo = $a('h2'); echo $titulo[0]->getPlainTextUTF8(); $imagenes = $a('.roomImage'); echo '<h1>Imagen</h1>'; foreach ($imagenes as $image) { echo $image->getAttribute('data-big-image') . '<br />'; } echo '<h1>Precio</h1>'; $precio = $a('.price'); echo $precio[0]->getPlainTextUTF8(); echo '<h1>Condiciones</h1>'; $condiciones = $a('h3 span');
function NearTAL_Compiler(&$returnValue, $template, $page, $variables = null, $directories = null) { $compilerData = array('metal' => array(), 'keywords' => array('define', 'condition', 'repeat', 'replace', 'content', 'omit-tag', 'attributes')); if (file_exists($directories['templates']) && is_dir($directories['templates']) && file_exists($directories['templates'] . '/' . $template) && is_dir($directories['templates'] . '/' . $template) && (file_exists($directories['temp']) || mkdir($directories['temp'], 0700)) && is_dir($directories['temp'])) { if (file_exists($directories['templates'] . '/' . $template . '.metal.html') && false !== ($content = file_get_contents($directories['templates'] . '/' . $template . '.metal.html'))) { $parser = str_get_dom($content); NearTAL_Compiler_ParseMETAL($returnValue, $compilerData, $parser, false); unset($parser); } if (file_exists($directories['templates'] . '/' . $template . '/' . $page . '.html') && false !== ($content = file_get_contents($directories['templates'] . '/' . $template . '/' . $page . '.html'))) { $parser = str_get_dom($content); NearTAL_Compiler_ParseMETAL($returnValue, $compilerData, $parser, true); $nodes = $parser->select('[data-metal-use-macro]'); foreach ($nodes as $node) { $macroName = $node->getAttribute('data-metal-use-macro'); if (array_key_exists($macroName, $compilerData['metal'])) { $availableSlots = array(); $slots = $node('[data-metal-fill-slot]'); foreach ($slots as $slot) { $slotName = $slots->getAttribute('data-metal-fill-slot'); if (array_key_exists($slotName, $slots)) { NearTAL_AddError($returnValue, constant('NearTAL_Error_DuplicatedSlot'), $slotName); } $slot->deleteAttribute('data-metal-fill-slot'); $availableSlots[$slotName] = $slot->getOuterText(); } $node->setOuterText($compilerData['metal'][$macroName]); $slots = $node('[data-metal-define-slot]'); foreach ($slots as $slot) { $slotName = $slots->getAttribute('data-metal-define-slot'); if (array_key_exists($slotName, $availableSlots)) { $slot->setOuterText($availableSlots[$slotName]); } else { $slot->deleteAttribute('data-metal-define-slot'); NearTAL_AddError($returnValue, constant('NearTAL_Error_UnknownSlot'), $slotName); } } unset($availableSlots); } else { NearTAL_AddError($returnValue, constant('NearTAL_Error_UnknownMacro'), $macroName); } } $output = '<?php if(defined(\'NearTAL\')){$localVariables=array(\'defined\'=>array(),\'repeat\'=>array(),\'stack\'=>array(\'defined\'=>array(),\'repeat\'=>array()),\'template\'=>array()); ?>'; $localVariablesIndex = 0; $count = 0; $selectFilter = array(); foreach ($compilerData['keywords'] as $keyword) { $selectFilter[] = '[data-tal-' . $keyword . ']'; } $nodes = $parser->select(implode(',', $selectFilter)); $localVariablesIndex = 0; foreach ($nodes as $node) { $changes = array('attributes' => array(), 'outside' => array('pre' => null, 'post' => null), 'inside' => array('pre' => null, 'post' => null)); foreach ($compilerData['keywords'] as $keyword) { $attribute = trim($node->getAttribute('data-tal-' . $keyword)); if (!is_null($attribute)) { if (!is_array($attribute) && !empty($attribute)) { switch ($keyword) { case 'define': $tmpVariable = array(); $tmpVariable['count'] = preg_match_all('/[\\s]*(?:(?:(local|world)[\\s]+)?(.+?)[\\s]+(.+?)[\\s]*(?:(?<!;);(?!;)|$))+?/', $attribute, $tmpVariable['elements'], PREG_SET_ORDER); for ($j = 0; $j < $tmpVariable['count']; $j++) { $tmpVariable['name'] = str_replace('\'', ''', $tmpVariable['elements'][$j][2]); $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $tmpVariable['elements'][$j][3], $node->attributes, '$tmpVariable'); $changes['outside']['pre'] .= '$localVariables[\'template\'][' . $localVariablesIndex . ']=(' . $tmpVariable['compiledExpression'] . '&&!$tmpVariable[1]);if($localVariables[\'template\'][' . $localVariablesIndex++ . ']){NearTAL_LocalVariablesPush($localVariables,\'defined\',\'' . $tmpVariable['name'] . '\',$tmpVariable[0]);}'; if ('world' != $tmpVariable['elements'][$j][1]) { $changes['outside']['post'] = 'if($localVariables[\'template\'][' . ($localVariablesIndex - 1) . ']){NearTAL_LocalVariablesPop($localVariables,\'defined\',\'' . $tmpVariable['name'] . '\');unset($localVariables[\'template\'][' . ($localVariablesIndex - 1) . ']);}' . $changes['outside']['post']; } } break; case 'condition': $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $attribute, $node->attributes, '$localVariables[\'template\'][' . $localVariablesIndex . ']', true, false); if ($fastAnswers[1]) { $node->delete(); } else { $changes['outside']['pre'] .= 'if(' . $tmpVariable['compiledExpression'] . '){'; $changes['outside']['post'] = '}unset($localVariables[\'template\'][' . $localVariablesIndex++ . ']);' . $changes['outside']['post']; } break; case 'repeat': $tmpVariable['elements'] = explode(" ", $attribute, 2); $tmpVariable['name'] = str_replace('\'', ''', $tmpVariable['elements'][0]); $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $tmpVariable['elements'][1], $node->attributes, '$localVariables[\'template\'][' . $localVariablesIndex . ']'); $changes['outside']['pre'] .= sprintf('if((%1$s)&&($localVariables[\'template\'][%2$d][1]||is_array($localVariables[\'template\'][%2$d][0]))){$localVariables[\'template\'][%3$d]=array(false,false);if(!$localVariables[\'template\'][%2$d][1]){($localVariables[\'template\'][%3$d][0]=true)&&NearTAL_LocalVariablesPush($localVariables,\'repeat\',\'%4$s\',null);($localVariables[\'template\'][%3$d][1]=true)&&NearTAL_LocalVariablesPush($localVariables,\'defined\',\'%4$s\',null);$localVariables[\'repeat\'][\'%4$s\'][\'index\']=-1;$localVariables[\'repeat\'][\'%4$s\'][\'length\']=count($localVariables[\'template\'][%2$d][0]);}do{if(!$localVariables[\'template\'][%2$d][1]){$localVariables[\'defined\'][\'%4$s\']=array_shift($localVariables[\'template\'][%2$d][0]);$localVariables[\'repeat\'][\'%4$s\'][\'index\']++;$localVariables[\'repeat\'][\'%4$s\'][\'number\']=$localVariables[\'repeat\'][\'%4$s\'][\'index\']+1;$localVariables[\'repeat\'][\'%4$s\'][\'even\']=($localVariables[\'repeat\'][\'%4$s\'][\'number\']%%2?true:false);$localVariables[\'repeat\'][\'%4$s\'][\'odd\']=!$localVariables[\'repeat\'][\'%4$s\'][\'even\'];$localVariables[\'repeat\'][\'%4$s\'][\'start\']=($localVariables[\'repeat\'][\'%4$s\'][\'index\']?false:true);$localVariables[\'repeat\'][\'%4$s\'][\'end\']=(($localVariables[\'repeat\'][\'%4$s\'][\'number\']==$localVariables[\'repeat\'][\'%4$s\'][\'length\'])?true:false);$localVariables[\'repeat\'][\'%4$s\'][\'letter\']=NearTAL_NumberToLetter($localVariables[\'repeat\'][\'%4$s\'][\'index\']);$localVariables[\'repeat\'][\'%4$s\'][\'Letter\']=strtoupper($localVariables[\'repeat\'][\'%4$s\'][\'letter\']);}', $tmpVariable['compiledExpression'], $localVariablesIndex++, $localVariablesIndex, $tmpVariable['name']); $changes['outside']['post'] = sprintf('}while(!$localVariables[\'template\'][%1$d][1]&&!empty($localVariables[\'template\'][%1$d][0]));$localVariables[\'template\'][%2$d][0]&&NearTAL_LocalVariablesPop($localVariables,\'repeat\',\'%3$s\');$localVariables[\'template\'][%2$d][1]&&NearTAL_LocalVariablesPop($localVariables,\'defined\',\'%3$s\');unset($localVariables[\'template\'][%1$d],$localVariables[\'template\'][%2$d]);}', $localVariablesIndex - 1, $localVariablesIndex++, $tmpVariable['name']) . $changes['outside']['post']; break; case 'replace': case 'content': if ('content' == $keyword || 'replace' == $keyword && !array_key_exists('data-tal-content', $node->attributes)) { $tmpVariable = array(); if ('structure ' == substr($attribute, 0, 10) || 'text ' == substr($attribute, 0, 5)) { $tmpVariable['parameters'] = explode(" ", $attribute, 2); } else { $tmpVariable['parameters'] = array("text", $attribute); } $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $tmpVariable['parameters'][1], $node->attributes, '$localVariables[\'template\'][' . $localVariablesIndex . ']'); if (!$fastAnswers[0]) { if (!$fastAnswers[1]) { $tmpVariable['pre'] = 'if(' . $tmpVariable['compiledExpression'] . '&&!$localVariables[\'template\'][' . $localVariablesIndex . '][1]&&!is_null($localVariables[\'template\'][' . $localVariablesIndex . '][0])){echo(' . ('text' == $tmpVariable['parameters'][0] ? 'str_replace(array(\'&\',\'<\',\'>\'),array(\'&\',\'<\',\'>\'),' : null) . '(is_bool($localVariables[\'template\'][' . $localVariablesIndex . '][0])?($localVariables[\'template\'][' . $localVariablesIndex . '][0]?1:0):$localVariables[\'template\'][' . $localVariablesIndex . '][0])' . ('text' == $tmpVariable['parameters'][0] ? ')' : null) . ');}elseif($localVariables[\'template\'][' . $localVariablesIndex . '][1]){'; $tmpVariable['post'] = '}unset($localVariables[\'template\'][' . $localVariablesIndex++ . ']);'; if ('content' == $keyword) { $changes['inside']['pre'] = $tmpVariable['pre'] . $changes['inside']['pre']; $changes['inside']['post'] .= $tmpVariable['post']; } else { $changes['outside']['pre'] .= $tmpVariable['pre']; $changes['outside']['post'] = $tmpVariable['post'] . $changes['outside']['post']; } } else { if ('content' == $keyword) { foreach ($node->children as $id => $child) { $node->deleteChild($id); } } else { $node->delete(); } } } } break; case 'attributes': $tmpVariable = array(); $tmpVariable['count'] = preg_match_all('/(?:[\\s]*(.+?)[\\s]+(.+?)[\\s]*(?:(?<!;);(?!;)|$))+?/', $attribute, $tmpVariable['elements'], PREG_SET_ORDER); for ($j = 0; $j < $tmpVariable['count']; $j++) { $changes['attributes'][$tmpVariable['elements'][$j][1]] = $tmpVariable['elements'][$j][2]; } break; case 'raw-attributes': $tmpVariable = array(); $tmpVariable['count'] = preg_match_all('/(?:[\\s]*(.+?)[\\s]*$)/', $attribute, $tmpVariable['elements'], PREG_SET_ORDER); if (1 == $tmpVariable['count']) { $changes['raw-attributes'] = $tmpVariable['elements'][$j][1]; } break; case 'omit-tag': $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $attribute, $node->attributes, '$tmpVariable', true); if (!$fastAnswers[0] && !$fastAnswers[1]) { $changes['outside']['pre'] .= '$localVariables[\'template\'][' . $localVariablesIndex . ']=!(' . $tmpVariable['compiledExpression'] . ');if($localVariables[\'template\'][' . $localVariablesIndex . ']){'; // If isn't selfclosed OR if I already need to put some code between starting and closing tag, I put some data "inside" // TODO: a regexp to remove ? >< ?php and {} if (!$node->self_close || isset($changes['inside']['pre']) || isset($changes['inside']['post']) || 0 < count($changes['attributes'])) { $changes['inside']['pre'] = '}' . $changes['inside']['pre']; $changes['inside']['post'] .= 'if($localVariables[\'template\'][' . $localVariablesIndex . ']){'; } $changes['outside']['post'] = '}unset($localVariables[\'template\'][' . $localVariablesIndex++ . ']);' . $changes['outside']['post']; } break; } } $node->deleteAttribute('data-tal-' . $keyword); } } if (!empty($changes['attributes'])) { foreach ($changes['attributes'] as $name => $value) { $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $value, $node->attributes, '$tmpVariable'); if (!$fastAnswers[0]) { if (!$fastAnswers[1]) { $tmpVariable['exists'] = array_key_exists($name, $node->attributes); $tmpVariable['attribute'] = '<?php if(' . $tmpVariable['compiledExpression'] . (!$tmpVariable['exists'] ? '&&!$tmpVariable[1]' : null) . '){?>' . $name . '="<?php echo(' . ($tmpVariable['exists'] ? '$tmpVariable[1]?\'' . str_replace('\'', ''', $node->attributes[$name]) . '\':' : null) . '(is_bool($tmpVariable[0])?($tmpVariable[0]?1:0):$tmpVariable[0]));?>"<?php }?>'; $node->attributes[$tmpVariable['attribute']] = $tmpVariable['attribute']; if ($tmpVariable['exists']) { unset($node->attributes[$name]); } } else { unset($node->attributes[$name]); } } } } if (!empty($changes['raw-attributes'])) { $fastAnswers = NearTAL_Compiler_ParseTALES($tmpVariable['compiledExpression'], $changes['raw-attributes'], $node->attributes, '$tmpVariable'); if (!$fastAnswers[0]) { if (!$fastAnswers[1]) { $tmpVariable['attribute'] = '<?php if(' . $tmpVariable['compiledExpression'] . '&&!$tmpVariable[1]' . '){?> <?phpecho((is_bool($tmpVariable[0])?($tmpVariable[0]?1:0):$tmpVariable[0]));}?>'; $node->attributes[$tmpVariable['attribute']] = null; } } } if (!is_null($changes['outside']['pre'])) { $position = $node->index(true); $node->parent->addXML('php', ' ' . $changes['outside']['pre'], array(), $position); } if (!is_null($changes['outside']['post'])) { $position = $node->index(true) + 1; $node->parent->addXML('php', ' ' . $changes['outside']['post'], array(), $position); } if (!is_null($changes['inside']['pre'])) { $position = 0; $node->addXML('php', ' ' . $changes['inside']['pre'], array(), $position); } if (!is_null($changes['inside']['post'])) { $position = -1; $node->addXML('php', ' ' . $changes['inside']['post'], array(), $position); } } $output .= str_replace("?>\n", "?>\n\n", str_replace("\r", '', $parser)) . '<?php }?>'; unset($parser); $file = null; if (false !== ($temporaryFilename = tempnam($directories['temp'], 'TAL')) && false !== ($file = fopen($temporaryFilename, 'wb'))) { fwrite($file, $output); fclose($file); if ((file_exists($directories['cache']) || mkdir($directories['cache'], 0700)) && is_dir($directories['cache']) && (file_exists($directories['cache'] . '/' . $template) || mkdir($directories['cache'] . '/' . $template, 0700)) && is_dir($directories['cache'] . '/' . $template)) { if (!rename($temporaryFilename, $directories['cache'] . '/' . $template . '/' . $page . '.php')) { NearTAL_AddError($returnValue, constant('NearTAL_Error_Rename'), $temporaryFilename); } } else { NearTAL_AddError($returnValue, constant('NearTAL_Error_Cache'), null); } } else { NearTAL_AddError($returnValue, constant('NearTAL_Error_TemporaryFile'), null); } } else { NearTAL_AddError($returnValue, constant('NearTAL_Error_Template'), null); } } else { NearTAL_AddError($returnValue, constant('NearTAL_Error_Path'), null); } }
$innerHTML = false; $attributes = false; $bSetTag = true; if (!empty($_POST['attributes'])) { $attributes = json_decode(base64_decode($_POST['attributes'])); } $path = $session->request->request; if (!empty($element->attributes)) { foreach ($element->attributes as $obj) { if ($obj->name == 'data-src') { $path = $obj->value; } } } $element->outerHTML = str_replace('"', "'", $element->outerHTML); $dom = str_get_dom($element->outerHTML); $dom('img')[0]->src = $_POST['src']; $content = (string) $dom; $ret = setTag($session, $pdo, $memcache, $element->id, $session->language, $session->country, $session->continent, $path, $content); $json->id = $_POST['id']; $json->element = $element; if ($ret === false) { $json->status = 'ERROR'; $json->message = 'Error setTag()'; } } else { $json->status = 'ERROR'; $json->message = 'Invalid element'; } } else { $json->status = 'ERROR';
$stmt->bindParam(':language', $obj->_language); $stmt->bindParam(':country', $obj->_country); $stmt->bindParam(':id', $obj->_id); $stmt->bindParam(':page', $obj->_page); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_OBJ); if (empty($row)) { $sql = "SELECT\r\n *\r\n FROM `image-url-translations`\r\n WHERE\r\n url=:url AND\r\n language=:language AND\r\n country=:country"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':language', $obj->_language); $stmt->bindParam(':country', $obj->_country); $stmt->bindValue(':url', '/' . $obj->_seo_src . '.' . $obj->_image->extension); $stmt->execute(); $row = $stmt->fetch(PDO::FETCH_OBJ); if (empty($row)) { $html = str_get_dom($obj->_content); $tags = $html(); foreach ($tags as $i => $el) { $el->attributes['src'] = '/' . $obj->_seo_src . '.' . $obj->_image->extension; $el->attributes['alt'] = $obj->_alt; $el->attributes['title'] = $obj->_title; break; } $obj->_content = (string) $html; $sql = "INSERT INTO\r\n tags\r\n SET\r\n language=:language,\r\n country=:country,\r\n element_id=:id,\r\n page=:page,\r\n content=:content"; $stmt = $pdo->prepare($sql); $stmt->bindParam(':language', $obj->_language); $stmt->bindParam(':country', $obj->_country); $stmt->bindParam(':id', $obj->_id); $stmt->bindValue(':page', $obj->_page); $stmt->bindValue(':content', $obj->_content);
echo '<br><br>[load file]<br>init memory: ' . number_format(memory_get_usage(), 0, '.', ',') . '<br>'; echo '------------------------------------------<br>'; flush(); for ($i = 0; $i < 3; ++$i) { $str = file_get_contents($filename); unset($str); dump_memory(); } echo 'final: ' . number_format(memory_get_usage(), 0, '.', ',') . '<br>'; flush(); $str = file_get_contents($filename); echo '<br><br>[multi objects str_get_dom clear memory]<br>init memory: ' . number_format(memory_get_usage(), 0, '.', ',') . '<br>'; echo '------------------------------------------<br>'; flush(); for ($i = 0; $i < 3; ++$i) { $dom = str_get_dom($str); //stat_dom($dom); $dom->clear(); unset($dom); dump_memory(); flush(); } echo 'final: ' . number_format(memory_get_usage(), 0, '.', ',') . '<br>'; flush(); echo '<br><br>[multi objects file_get_dom clear memory]<br>init memory: ' . number_format(memory_get_usage(), 0, '.', ',') . '<br>'; echo '------------------------------------------<br>'; flush(); for ($i = 0; $i < 3; ++$i) { $dom = file_get_dom($filename); //stat_dom($dom); $dom->clear();
/** * Creates links to the appropriate editor (in Providence) or detail page (in Pawtucket) from supplied text and ids. * Used in SearchResult::get() and BundlableLabelableBaseModelWithAttributes::get() to automatically generate links when fetching * information from related tables. * * @param array $pa_text An array of strings to create links for * @param string $ps_table_name The name of the table/record to which the links refer * @param array $pa_row_ids Array of row_ids to link to. Values must correspond by index with those in $pa_text * @param string $ps_class Optional CSS class to apply to links * @param string $ps_target * @param array $pa_options Supported options are: * requireLinkTags = if set then links are only added when explicitly defined with <l> tags. Default is to make the entire text a link in the absence of <l> tags. * addRelParameter = * * @return array A list of HTML links */ function caCreateLinksFromText($pa_text, $ps_table_name, $pa_row_ids, $ps_class = null, $ps_target = null, $pa_options = null) { if (!in_array(__CA_APP_TYPE__, array('PROVIDENCE', 'PAWTUCKET'))) { return $pa_text; } if (__CA_APP_TYPE__ == 'PAWTUCKET') { $o_config = Configuration::load(); } $pb_add_rel = caGetOption('addRelParameter', $pa_options, false); $vb_can_handle_target = false; if ($ps_target) { $o_app_plugin_manager = new ApplicationPluginManager(); $vb_can_handle_target = $o_app_plugin_manager->hookCanHandleGetAsLinkTarget(array('target' => $ps_target)); } // Parse template $o_doc = str_get_dom($ps_template); $va_links = array(); global $g_request; if (!$g_request) { return $pa_text; } foreach ($pa_text as $vn_i => $vs_text) { $vs_text = preg_replace("!([A-Za-z0-9]+)='([^']*)'!", "\$1=\"\$2\"", $vs_text); $va_l_tags = array(); $o_links = $o_doc('l'); foreach ($o_links as $o_link) { if (!$o_link) { continue; } $vs_html = $o_link->html(); $vs_content = preg_replace("!^<[^\\>]+>!", "", $vs_html); $vs_content = preg_replace("!<[^\\>]+>\$!", "", $vs_content); $va_l_tags[] = array('directive' => html_entity_decode($vs_html), 'content' => $vs_content); //html_entity_decode } if (sizeof($va_l_tags)) { $vs_content = html_entity_decode($vs_text); $vs_content = preg_replace_callback("/(&#[0-9]+;)/", function ($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $vs_content); foreach ($va_l_tags as $va_l) { if ($vb_can_handle_target) { $va_params = array('request' => $g_request, 'content' => $va_l['content'], 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null); $va_params = $o_app_plugin_manager->hookGetAsLink($va_params); $vs_link_text = $va_params['tag']; } else { switch (__CA_APP_TYPE__) { case 'PROVIDENCE': $vs_link_text = caEditorLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i], $pb_add_rel ? array('rel' => true) : array()); break; case 'PAWTUCKET': $vs_link_text = caDetailLink($g_request, $va_l['content'], $ps_class, $ps_table_name, $pa_row_ids[$vn_i]); break; } } if ($vs_link_text) { $vs_content = str_replace($va_l['directive'], $vs_link_text, $vs_content); } else { $vs_content = str_replace($va_l['directive'], $va_l['content'], $vs_content); } } $va_links[] = $vs_content; } else { if (isset($pa_options['requireLinkTags']) && $pa_options['requireLinkTags']) { $va_links[] = $vs_text; continue; } if ($vb_can_handle_target) { $va_params = array('request' => $g_request, 'content' => $vs_text, 'table' => $ps_table_name, 'id' => $pa_row_ids[$vn_i], 'classname' => $ps_class, 'target' => $ps_target, 'additionalParameters' => null, 'options' => null); $va_params = $o_app_plugin_manager->hookGetAsLink($va_params); $va_links[] = $va_params['tag']; } else { switch (__CA_APP_TYPE__) { case 'PROVIDENCE': $va_links[] = ($vs_link = caEditorLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text; break; case 'PAWTUCKET': $va_links[] = ($vs_link = caDetailLink($g_request, $vs_text, $ps_class, $ps_table_name, $pa_row_ids[$vn_i])) ? $vs_link : $vs_text; break; default: $va_links[] = $vs_text; break; } } } } return $va_links; }
function getSaldo($uid) { curl_setopt($this->ch, CURLOPT_POST, 0); curl_setopt($this->ch, CURLOPT_URL, self::SALDO_URL . "?uid=" . $uid); //execute the request $content = curl_exec($this->ch); //echo "JES--->".$content; //parse html $html = str_get_dom($content); $tituloPantalla = utf8_encode($html("p[class='header2']", 0)->getInnerText()); //$tituloPantalla = $html("p[class='header2']",0)->html(); $pieces = explode(" ", $tituloPantalla); //$nombre = str_replace("de\u00a0", "", $pieces[3]); $nombre = substr($pieces[3], 4); // $nombre = $pieces[3]; $apellido = $pieces[4]; $ingresos = $html("td[class='textsmall']", 1)->getInnerText(); $compras = $html("td[class='textsmall']", 2)->getInnerText(); $gastos = $html("td[class='textsmall']", 3)->getInnerText(); $tasa = $html("td[class='textsmall']", 4)->getInnerText(); $saldo = $html("td[class='textsmall']", 5)->firstChild()->getInnerText(); //echo "JES-->saldo-->".$saldo->getInnerText(); $porcentaje_ventas = $html("td[class='textsmall']", 6)->getInnerText(); $porcentaje_compras = $html("td[class='textsmall']", 7)->getInnerText(); $ventas_compras = $html("td[class='textsmall']", 8)->getInnerText(); $ingresos_gastos = $html("td[class='textsmall']", 9)->getInnerText(); $intercambios = $html("td[class='textsmall']", 10)->getInnerText(); $total = $html("td[class='textsmall']", 11)->getInnerText(); $venta_media = $html("td[class='textsmall']", 12)->getInnerText(); $compra_media = $html("td[class='textsmall']", 13)->getInnerText(); $limite_positivo = $html("td[class='textsmall']", 14)->getInnerText(); $limite_negativo = $html("td[class='textsmall']", 15)->getInnerText(); $excedido_limite = $html("td[class='textsmall']", 16)->getInnerText(); $indicador = $html("td[class='textsmall']", 17)->getInnerText(); $primer_intercambio = $html("td[class='textsmall']", 18)->getInnerText(); $ultima_venta = $html("td[class='textsmall']", 19)->getInnerText(); if (strpos($ultima_venta, 'None') === false) { $pieces = explode(" ", $ultima_venta); $ultima_venta = $pieces[0] . " " . $this->month_es_ES[$pieces[1]] . " " . $pieces[2]; } else { $ultima_venta = "Ninguna"; } $ultima_compra = $html("td[class='textsmall']", 20)->getInnerText(); if (strpos($ultima_compra, 'None') === false) { $pieces = explode(" ", $ultima_compra); $ultima_compra = $pieces[0] . " " . $this->month_es_ES[$pieces[1]] . " " . $pieces[2]; } else { $ultima_compra = "Ninguna"; } $result = array("nombre" => $nombre, "apellido" => $apellido, "saldo" => $saldo, "intercambios" => $intercambios, "ultimaVenta" => $ultima_venta, "ultimaCompra" => $ultima_compra); return json_encode($result); }
public function get_request_loc($name, $code, $ref, $post_params) { $login_resp = $this->login($name, $code); # We want to both submit to and use this URL as the referrer $url = $this->catalog_url . $ref; $body = http_request_body_encode($post_params, array()); $resp = http_parse_message(http_post_data( $url, $body, array( "cookies" => $login_resp['cookies'], "referer" => $url, ) )); $html = str_get_dom($resp->body); $locs = $this->find_request_loc($html); $hidden = $this->find_hidden_fields($html); $resp = array( "locs" => $locs, "hidden" => $hidden, ); return $resp; }
/** * Handles extraction for the given post. * * @since 2.5 */ public static function extract( $post_id, $source ) { // If a source is set ( hence an imported post ), ... if ( $source !== '' ) { // Get the extraction rules of the source $rules = self::get_extraction_rules_and_types( $source ); wprss_log_obj( 'Got extraction rules', $rules, NULL, WPRSS_LOG_LEVEL_SYSTEM ); // If the rules are not an array or there are no rules, return if ( !is_array( $rules ) && count( $rules ) == 0 ) return; // Load the ganon library if ( version_compare(phpversion(), '5.3.1', '<') ) { // PHP4 Ganon require_once( WPRSS_FTP_LIB . 'ganon.php4' ); } else { // PHP5+ Ganon require_once( WPRSS_FTP_LIB . 'ganon.php' ); } // Get the post $post = get_post( $post_id ); // If the post is a WP error, return if ( is_wp_error( $post ) || !is_object( $post ) ) return; // Otherwise, get the content $content = $post->post_content; // Parse the post content $html = str_get_dom( $content ); // For each rule and its type foreach ( $rules as $rule => $type ) { // Trim the rule string $rule = trim( $rule ); // If the rule is empty, skip it if ( strlen( $rule ) === 0 ) { continue; } // Used to replace the current html DOM $new_html = ''; // Each found element ... foreach ( $html->select($rule) as $element ) { // Check the rule type switch( $type ) { // If keeping the matched element case 'keep' : // Add the element as a string to the new_html variable $new_html .= $element->toString(TRUE, TRUE, FALSE); break; // Remove the element case 'remove' : $element->detach(); break; // Remove the element, and keep its children case 'remove_keep_children' : $element->detach( TRUE ); break; } } // If the new_html variable has changed, use it as the new HMTL DOM if ( strlen( $new_html ) > 0 ) { $html = str_get_dom( $new_html ); } } // End of rules foreach // Update the post with its new content $new_content = (string)$html; WPRSS_FTP_Utils::update_post_content( $post_id, $new_content ); } }
/** private * return a simple_html_dom on the given content. * Because accessibility check is based on the root html element <html>, * check if dom has html tag <html>, if no, add it and the end tag to the content * and return the dom on modified content. */ private function get_simple_html_dom($content) { global $msg; $dom = str_get_dom($content); if (count($dom->find('html')) == 0) { $complete_html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' . '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . $content . '</html>'; $this->col_offset = 175; // The number of extra characters that are added onto the first line. $dom = str_get_dom($complete_html); } return $dom; }
/** * Remove whitespace, tab, new line from input * * @param $input * @return mixed */ protected function _compressHtml($buffer) { Zo2Factory::import('vendor.ganon.ganon'); $dom = str_get_dom($buffer); HTML_Formatter::minify_html($dom); $buffer = (string) $dom; return $buffer; }
public static function getParticipants($user, $password, $id) { $ch = self::login($user, $password); $url = "https://candea.itslearning.com"; curl_setopt($ch, CURLOPT_URL, $url . '/Course/Participants.aspx?CourseID=' . $id); $curl = curl_exec($ch); $html = str_get_dom($curl); $participants = $html('table#ctl00_ContentPlaceHolder_ParticipantsGrid_T tbody tr'); $return = array('participants' => array()); $participantsArray = array(); foreach ($participants as $participant) { if ($participant('td a')) { $participantsArray[] = array('name' => $participant('td a')[0]->getPlainText(), 'type' => $participant('td')[2]->getPlainText()); } } $return['participants'] = $participantsArray; return $return; }
/** * Replace "^" prefixed tags (eg. ^forename) in a template with values from an array * * @param string $ps_template String with embedded tags. Tags are just alphanumeric strings prefixed with a caret ("^") * @param string $pm_tablename_or_num Table name or number of table from which values are being formatted * @param string $pa_row_ids An array of primary key values in the specified table to be pulled into the template * @param array $pa_options Supported options are: * returnAsArray = if true an array of processed template values is returned, otherwise the template values are returned as a string joined together with a delimiter. Default is false. * delimiter = value to string together template values with when returnAsArray is false. Default is ';' (semicolon) * relatedValues = array of field values to return in template when directly referenced. Array should be indexed numerically in parallel with $pa_row_ids * relationshipValues = array of field values to return in template for relationship when directly referenced. Should be indexed by row_id and then by relation_id * placeholderPrefix = attribute container to implicitly place primary record fields into. Ex. if the table is "ca_entities" and the placeholder is "address" then tags like ^city will resolve to ca_entities.address.city * requireLinkTags = if set then links are only added when explicitly defined with <l> tags. Default is to make the entire text a link in the absence of <l> tags. * resolveLinksUsing = * primaryIDs = row_ids for primary rows in related table, keyed by table name; when resolving ambiguous relationships the row_ids will be excluded from consideration. This option is rarely used and exists primarily to take care of a single * edge case: you are processing a template relative to a self-relationship such as ca_entities_x_entities that includes references to the subject table (ca_entities, in the case of ca_entities_x_entities). There are * two possible paths to take in this situations; primaryIDs lets you specify which ones you *don't* want to take by row_id. For interstitial editors, the ids will be set to a single id: that of the subject (Eg. ca_entities) row * from which the interstitial was launched. * sort = optional list of tag values to sort repeating values within a row template on. The tag must appear in the template. You can specify more than one tag by separating the tags with semicolons. * sortDirection = The direction of the sort of repeating values within a row template. May be either ASC (ascending) or DESC (descending). [Default is ASC] * linkTarget = Optional target to use when generating <l> tag-based links. By default links point to standard detail pages, but plugins may define linkTargets that point elsewhere. * skipIfExpression = skip the elements in $pa_row_ids for which the given expression does not evaluate true * includeBlankValuesInArray = include blank template values in returned array when returnAsArray is set. If you need the returned array of values to line up with the row_ids in $pa_row_ids this should be set. [Default is false] * * @return mixed Output of processed templates */ function caProcessTemplateForIDs($ps_template, $pm_tablename_or_num, $pa_row_ids, $pa_options = null) { foreach (array('request', 'template', 'restrictToTypes', 'restrict_to_types', 'restrict_to_relationship_types', 'restrictToRelationshipTypes', 'useLocaleCodes') as $vs_k) { unset($pa_options[$vs_k]); } if (!isset($pa_options['convertCodesToDisplayText'])) { $pa_options['convertCodesToDisplayText'] = true; } $pb_return_as_array = (bool) caGetOption('returnAsArray', $pa_options, false); if (($pa_sort = caGetOption('sort', $pa_options, null)) && !is_array($pa_sort)) { $pa_sort = explode(";", $pa_sort); } $ps_sort_direction = caGetOption('sortDirection', $pa_options, null, array('forceUppercase' => true)); if (!in_array($ps_sort_direction, array('ASC', 'DESC'))) { $ps_sort_direction = 'ASC'; } $pa_check_access = caGetOption('checkAccess', $pa_options, null); if (!is_array($pa_row_ids) || !sizeof($pa_row_ids) || !$ps_template) { return $pb_return_as_array ? array() : ""; } unset($pa_options['returnAsArray']); if (!isset($pa_options['requireLinkTags'])) { $pa_options['requireLinkTags'] = true; } $ps_skip_if_expression = caGetOption('skipIfExpression', $pa_options, false); $va_primary_ids = caGetOption("primaryIDs", $pa_options, null); $o_dm = Datamodel::load(); $ps_tablename = is_numeric($pm_tablename_or_num) ? $o_dm->getTableName($pm_tablename_or_num) : $pm_tablename_or_num; $ps_resolve_links_using = caGetOption('resolveLinksUsing', $pa_options, $ps_tablename); $t_instance = $o_dm->getInstanceByTableName($ps_tablename, true); if ($ps_resolve_links_using != $ps_tablename) { $t_resolve_links_instance = $o_dm->getInstanceByTableName($ps_resolve_links_using, true); $vs_resolve_links_using_pk = $t_resolve_links_instance->primaryKey(); } $vs_pk = $t_instance->primaryKey(); $vs_delimiter = isset($pa_options['delimiter']) ? $pa_options['delimiter'] : '; '; $ps_template = str_replace("^_parent", "^{$ps_resolve_links_using}.parent.preferred_labels", $ps_template); $ps_template = str_replace("^_hierarchy", "^{$ps_resolve_links_using}._hierarchyName", $ps_template); $va_related_values = isset($pa_options['relatedValues']) && is_array($pa_options['relatedValues']) ? $pa_options['relatedValues'] : array(); $va_relationship_values = isset($pa_options['relationshipValues']) && is_array($pa_options['relationshipValues']) ? $pa_options['relationshipValues'] : array(); $o_doc = str_get_dom($ps_template); // parse template $ps_template = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_doc->html())); // replace template with parsed version; this allows us to do text find/replace later // Parse units from template $o_units = $o_doc('unit'); // only process non-nested <unit> tags $va_units = array(); $vn_unit_id = 1; foreach ($o_units as $o_unit) { if (!$o_unit) { continue; } $vs_html = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_unit->html())); $vs_content = $o_unit->getInnerText(); // is this nested in another unit? We skip these foreach ($va_units as $va_tmp) { if (strpos($va_tmp['directive'], $vs_html) !== false) { continue 2; } } $va_units[] = $va_unit = array('tag' => $vs_unit_tag = "[[#{$vn_unit_id}]]", 'directive' => $vs_html, 'content' => $vs_content, 'relativeTo' => (string) $o_unit->getAttribute("relativeto"), 'delimiter' => ($vs_d = (string) $o_unit->getAttribute("delimiter")) ? $vs_d : null, 'restrictToTypes' => (string) $o_unit->getAttribute("restricttotypes"), 'restrictToRelationshipTypes' => (string) $o_unit->getAttribute("restricttorelationshiptypes"), 'sort' => explode(";", $o_unit->getAttribute("sort")), 'sortDirection' => (string) $o_unit->getAttribute("sortDirection"), 'skipIfExpression' => (string) $o_unit->getAttribute("skipIfExpression")); $ps_template = str_ireplace($va_unit['directive'], $vs_unit_tag, $ps_template); $vn_unit_id++; } $o_doc = str_get_dom($ps_template); // parse template again with units replaced by unit tags in the format [[#X]] $ps_template = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_doc->html())); // replace template with parsed version; this allows us to do text find/replace later $va_tags = array(); if (preg_match_all(__CA_BUNDLE_DISPLAY_TEMPLATE_TAG_REGEX__, $ps_template, $va_matches)) { $va_tags = $va_matches[1]; } $va_directive_tags = array(); $va_directive_tag_vals = array(); $qr_res = caMakeSearchResult($ps_tablename, $pa_row_ids); if (!$qr_res) { return ''; } $va_proc_templates = array(); $vn_i = 0; $o_ifs = $o_doc("if"); // if $o_ifdefs = $o_doc("ifdef"); // if defined $o_ifnotdefs = $o_doc("ifnotdef"); // if not defined $o_mores = $o_doc("more"); // more tags - content suppressed if there are no defined values following the tag pair $o_betweens = $o_doc("between"); // between tags - content suppressed if there are not defined values on both sides of the tag pair $va_if = array(); foreach ($o_ifs as $o_if) { if (!$o_if) { continue; } $vs_html = $o_if->html(); $vs_content = $o_if->getInnerText(); $va_if[] = array('directive' => $vs_html, 'content' => $vs_content, 'rule' => $vs_rule = (string) $o_if->getAttribute('rule')); } foreach ($o_ifdefs as $o_ifdef) { if (!$o_ifdef) { continue; } $vs_code = (string) $o_ifdef->getAttribute('code'); $vs_code_proc = preg_replace("!%(.*)\$!", '', $vs_code); $va_directive_tags = array_merge($va_directive_tags, preg_split('![,\\|]{1}!', $vs_code_proc)); } foreach ($o_ifnotdefs as $o_ifnotdef) { if (!$o_ifnotdef) { continue; } $vs_code = (string) $o_ifnotdef->getAttribute('code'); $vs_code_proc = preg_replace("!%(.*)\$!", '', $vs_code); $va_directive_tags = array_merge($va_directive_tags, preg_split('![,\\|]{1}!', $vs_code_proc)); } $va_mores = array(); foreach ($o_mores as $o_more) { if (!$o_more) { continue; } $vs_html = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_more->html())); $vs_content = $o_more->getInnerText(); $va_mores[] = array('directive' => $vs_html, 'content' => $vs_content); } $va_betweens = array(); foreach ($o_betweens as $o_between) { if (!$o_between) { continue; } $vs_html = str_replace("<~root~>", "", str_replace("</~root~>", "", $o_between->html())); $vs_content = $o_between->getInnerText(); $va_betweens[] = array('directive' => $vs_html, 'content' => $vs_content); } $va_resolve_links_using_row_ids = array(); $va_tag_val_list = $va_defined_tag_list = array(); $va_expression_vars = array(); /** @var $qr_res SearchResult */ while ($qr_res->nextHit()) { $vs_pk_val = $qr_res->get($vs_pk, array('checkAccess' => $pa_check_access)); if (is_array($pa_check_access) && sizeof($pa_check_access) && !in_array($qr_res->get("{$ps_tablename}.access"), $pa_check_access)) { continue; } $vs_template = $ps_template; // check if we skip this row because of skipIfExpression if (strlen($ps_skip_if_expression) > 0) { $va_expression_tags = caGetTemplateTags($ps_skip_if_expression); foreach ($va_expression_tags as $vs_expression_tag) { if (!isset($va_expression_vars[$vs_expression_tag])) { $va_expression_vars[$vs_expression_tag] = $qr_res->get($vs_expression_tag, array('assumeDisplayField' => true, 'returnIdno' => true, 'delimiter' => ';')); } } if (ExpressionParser::evaluate($ps_skip_if_expression, $va_expression_vars)) { continue; } } // Grab values for codes used in ifdef and ifnotdef directives $va_directive_tag_vals = array(); foreach ($va_directive_tags as $vs_directive_tag) { $va_directive_tag_vals[$vs_directive_tag] = $qr_res->get($vs_directive_tag, array('assumeDisplayField' => true, 'convertCodesToDisplayText' => true, 'dontUseElementTemplate' => true)); } $o_parsed_template = str_get_dom($vs_template); while (sizeof($vo_templates = $o_parsed_template('ifcount:not(:has(ifdef,ifndef,ifcount)),ifdef:not(:has(ifdef,ifndef,ifcount)),ifndef:not(:has(ifdef,ifndef,ifcount))')) > 0) { foreach ($vo_templates as $vn_index => $vo_element) { $vs_code = $vo_element->code; switch ($vo_element->tag) { case 'ifdef': if (strpos($vs_code, "|") !== false) { $vs_bool = 'OR'; $va_tag_list = explode("|", $vs_code); $vb_output = false; } else { $vs_bool = 'AND'; $va_tag_list = explode(",", $vs_code); $vb_output = true; } foreach ($va_tag_list as $vs_tag_to_test) { $vs_tag_to_test = preg_replace("!%.*\$!", "", $vs_tag_to_test); $vb_value_is_set = isset($va_directive_tag_vals[$vs_tag_to_test]) && strlen($va_directive_tag_vals[$vs_tag_to_test]) > 1; switch ($vs_bool) { case 'OR': if ($vb_value_is_set) { $vb_output = true; break 2; } // any must be defined; if any is defined output break; case 'AND': default: if (!$vb_value_is_set) { $vb_output = false; break 2; } // all must be defined; if any is not defined don't output break; } } if ($vb_output) { $vs_template = str_replace($vo_element->html(), $vo_element->getInnerText(), $vs_template); } else { $vs_template = str_replace($vo_element->html(), '', $vs_template); } break; case 'ifndef': if (strpos($vs_code, "|") !== false) { $vs_bool = 'OR'; $va_tag_list = explode("|", $vs_code); $vb_output = false; } else { $vs_bool = 'AND'; $va_tag_list = explode(",", $vs_code); $vb_output = true; } $vb_output = true; foreach ($va_tag_list as $vs_tag_to_test) { $vb_value_is_set = (bool) (isset($va_directive_tag_vals[$vs_tag_to_test]) && strlen($va_directive_tag_vals[$vs_tag_to_test]) > 0); switch ($vs_bool) { case 'OR': if (!$vb_value_is_set) { $vb_output = true; break 2; } // any must be not defined; if anything is not set output break; case 'AND': default: if ($vb_value_is_set) { $vb_output = false; break 2; } // all must be not defined; if anything is set don't output break; } } if ($vb_output) { $vs_template = str_replace($vo_element->html(), $vo_element->getInnerText(), $vs_template); } else { $vs_template = str_replace($vo_element->html(), '', $vs_template); } break; case 'ifcount': if (is_array($va_if_codes = preg_split("![\\|,;]+!", $vs_code))) { $vn_min = (int) $vo_element->min; $vn_max = (int) $vo_element->max; $va_restrict_to_types = preg_split("![,; ]+!", $vo_element->restrictToTypes); $va_restrict_to_relationship_types = preg_split("![,; ]+!", $vo_element->restrictToRelationshipTypes); $vn_count = 0; foreach ($va_if_codes as $vs_if_code) { if ($t_table = $o_dm->getInstanceByTableName($vs_if_code, true)) { $va_count_vals = $qr_res->get($vs_if_code . "." . $t_table->primaryKey(), array('restrictToTypes' => $va_restrict_to_types, 'restrictToRelationshipTypes' => $va_restrict_to_relationship_types, 'returnAsArray' => true, 'checkAccess' => $pa_check_access)); } else { $va_count_vals = $qr_res->get($vs_if_code, array('returnAsArray' => true, 'restrictToTypes' => $va_restrict_to_types, 'restrictToRelationshipTypes' => $va_restrict_to_relationship_types, 'checkAccess' => $pa_check_access)); } if (is_array($va_count_vals)) { $va_bits = explode(".", $vs_if_code); $vs_fld = array_pop($va_bits); foreach ($va_count_vals as $vs_count_val) { if (is_array($vs_count_val)) { if (isset($vs_count_val[$vs_fld]) && !trim($vs_count_val[$vs_fld])) { continue; } $vb_is_set = false; foreach ($vs_count_val as $vs_f => $vs_v) { if (trim($vs_v)) { $vb_is_set = true; break; } } if (!$vb_is_set) { continue; } } else { if (!trim($vs_count_val)) { continue; } } $vn_count++; } } } if ($vn_min <= $vn_count && ($vn_max >= $vn_count || !$vn_max)) { $vs_template = str_replace($vo_element->html(), $vo_element->getInnerText(), $vs_template); } else { $vs_template = str_replace($vo_element->html(), '', $vs_template); } } break; } } $o_parsed_template = str_get_dom($vs_template); // reparse } $va_proc_templates[$vn_i] = $vs_template; foreach ($va_units as $k => $va_unit) { if (!$va_unit['content']) { continue; } $va_relative_to_tmp = $va_unit['relativeTo'] ? explode(".", $va_unit['relativeTo']) : array($ps_tablename); if (!($t_rel_instance = $o_dm->getInstanceByTableName($va_relative_to_tmp[0], true))) { continue; } $vs_unit_delimiter = caGetOption('delimiter', $va_unit, $vs_delimiter); $vs_unit_skip_if_expression = caGetOption('skipIfExpression', $va_unit, false); // additional get options for pulling related records $va_get_options = array('returnAsArray' => true, 'checkAccess' => $pa_check_access); if ($va_unit['restrictToTypes'] && strlen($va_unit['restrictToTypes']) > 0) { $va_get_options['restrictToTypes'] = preg_split('![\\|,;]+!', $va_unit['restrictToTypes']); } if ($va_unit['restrictToRelationshipTypes'] && strlen($va_unit['restrictToRelationshipTypes']) > 0) { $va_get_options['restrictToRelationshipTypes'] = preg_split('![\\|,;]+!', $va_unit['restrictToRelationshipTypes']); } if ($va_unit['sort'] && is_array($va_unit['sort'])) { $va_get_options['sort'] = $va_unit['sort']; $va_get_options['sortDirection'] = $va_unit['sortDirection']; } if (sizeof($va_relative_to_tmp) == 1 && $va_relative_to_tmp[0] == $ps_tablename || sizeof($va_relative_to_tmp) >= 1 && $va_relative_to_tmp[0] == $ps_tablename && $va_relative_to_tmp[1] != 'related') { switch (strtolower($va_relative_to_tmp[1])) { case 'hierarchy': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".hierarchy." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; case 'parent': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".parent." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; case 'children': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".children." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; default: $va_relative_ids = array($vs_pk_val); break; } // process template for all records selected by unit tag $va_tmpl_val = caProcessTemplateForIDs($va_unit['content'], $va_relative_to_tmp[0], $va_relative_ids, array_merge($pa_options, array('sort' => $va_get_options['sort'], 'sortDirection' => $va_get_options['sortDirection'], 'returnAsArray' => true, 'delimiter' => $vs_unit_delimiter, 'resolveLinksUsing' => null, 'skipIfExpression' => $vs_unit_skip_if_expression))); $va_proc_templates[$vn_i] = str_ireplace($va_unit['tag'], join($vs_unit_delimiter, $va_tmpl_val), $va_proc_templates[$vn_i]); } else { switch (strtolower($va_relative_to_tmp[1])) { case 'hierarchy': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".hierarchy." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; case 'parent': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".parent." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; case 'children': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".children." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; case 'related': $va_relative_ids = $qr_res->get($t_rel_instance->tableName() . ".related." . $t_rel_instance->primaryKey(), $va_get_options); $va_relative_ids = array_values($va_relative_ids); break; default: if (method_exists($t_instance, 'isSelfRelationship') && $t_instance->isSelfRelationship()) { $va_relative_ids = array_values($t_instance->getRelatedIDsForSelfRelationship($va_primary_ids[$t_rel_instance->tableName()], array($vs_pk_val))); } else { $va_relative_ids = array_values($qr_res->get($t_rel_instance->tableName() . "." . $t_rel_instance->primaryKey(), $va_get_options)); } break; } $vs_tmpl_val = caProcessTemplateForIDs($va_unit['content'], $va_relative_to_tmp[0], $va_relative_ids, array_merge($pa_options, array('sort' => $va_unit['sort'], 'sortDirection' => $va_unit['sortDirection'], 'delimiter' => $vs_unit_delimiter, 'resolveLinksUsing' => null, 'skipIfExpression' => $vs_unit_skip_if_expression))); $va_proc_templates[$vn_i] = str_ireplace($va_unit['tag'], $vs_tmpl_val, $va_proc_templates[$vn_i]); } } if (!strlen(trim($va_proc_templates[$vn_i]))) { $va_proc_templates[$vn_i] = null; } if (!sizeof($va_tags)) { $vn_i++; continue; } // if there are no tags in the template then we don't need to process further if ($ps_resolve_links_using !== $ps_tablename) { $va_resolve_links_using_row_ids += $qr_res->get("{$ps_resolve_links_using}.{$vs_resolve_links_using_pk}", array('returnAsArray' => true, 'checkAccess' => $pa_check_access)); // we need to remove "primary_ids" from the list, since for self-relations these will be the side(s) of the relations we're viewing *from* if (is_array($va_primary_ids[$ps_resolve_links_using]) && sizeof($va_primary_ids[$ps_resolve_links_using])) { $va_resolve_links_using_row_ids = array_values(array_diff($va_resolve_links_using_row_ids, $va_primary_ids[$ps_resolve_links_using])); } } $va_tag_val_list[$vn_i] = array(); $va_defined_tag_list[$vn_i] = array(); $va_tag_opts = $va_tag_filters = array(); foreach ($va_tags as $vs_tag) { $va_tmp = explode('.', $vs_tag); $vs_last_element = $va_tmp[sizeof($va_tmp) - 1]; $va_tag_opt_tmp = explode("%", $vs_last_element); if (sizeof($va_tag_opt_tmp) > 1) { $vs_tag_bit = array_shift($va_tag_opt_tmp); // get rid of getspec foreach ($va_tag_opt_tmp as $vs_tag_opt_raw) { if (preg_match("!^\\[([^\\]]+)\\]\$!", $vs_tag_opt_raw, $va_matches)) { if (sizeof($va_filter = explode("=", $va_matches[1])) == 2) { $va_tag_filters[$va_filter[0]] = $va_filter[1]; } continue; } $va_tag_tmp = explode("=", $vs_tag_opt_raw); $va_tag_tmp[0] = trim($va_tag_tmp[0]); $va_tag_tmp[1] = trim($va_tag_tmp[1]); if (in_array($va_tag_tmp[0], array('delimiter', 'hierarchicalDelimiter'))) { $va_tag_tmp[1] = str_replace("_", " ", $va_tag_tmp[1]); } if (sizeof($va_tag_line_tmp = explode("|", $va_tag_tmp[1])) > 1) { $va_tag_opts[trim($va_tag_tmp[0])] = $va_tag_line_tmp; } else { $va_tag_opts[trim($va_tag_tmp[0])] = $va_tag_tmp[1]; } } $va_tmp[sizeof($va_tmp) - 1] = $vs_tag_bit; // remove option from tag-part array $vs_tag_proc = join(".", $va_tmp); $va_proc_templates[$vn_i] = str_replace($vs_tag, $vs_tag_proc, $va_proc_templates[$vn_i]); $vs_tag = $vs_tag_proc; } switch ($vs_tag) { case 'DATE': $vs_format = urldecode(caGetOption('format', $va_tag_opts, 'd M Y')); $va_proc_templates[$vn_i] = str_replace("^{$vs_tag}", date($vs_format), $va_proc_templates[$vn_i]); continue 2; break; } $pa_options = array_merge($pa_options, $va_tag_opts); // Default label tag to hierarchies if (isset($pa_options['showHierarchicalLabels']) && $pa_options['showHierarchicalLabels'] && $vs_tag == 'label') { unset($va_related_values[$vs_pk_val][$vs_tag]); unset($va_relationship_values[$vs_pk_val][$vs_tag]); $va_tmp = array($ps_tablename, 'hierarchy', 'preferred_labels'); } if (!isset($va_relationship_values[$vs_pk_val])) { $va_relationship_values[$vs_pk_val] = array(0 => null); } foreach ($va_relationship_values[$vs_pk_val] as $vn_relation_id => $va_relationship_value_array) { $vb_is_related = false; $va_val = null; if (isset($va_relationship_value_array[$vs_tag]) && !(isset($pa_options['showHierarchicalLabels']) && $pa_options['showHierarchicalLabels'] && $vs_tag == 'label')) { $va_val = array($vs_val = $va_relationship_value_array[$vs_tag]); } elseif (isset($va_relationship_value_array[$vs_tag])) { $va_val = array($vs_val = $va_relationship_value_array[$vs_tag]); } else { if (isset($va_related_values[$vs_pk_val][$vs_tag])) { $va_val = array($vs_val = $va_related_values[$vs_pk_val][$vs_tag]); } else { // // see if this is a reference to a related table // if (in_array($vs_tag, array("relationship_typename", "relationship_type_id", "relationship_typecode", "relationship_type_code"))) { $vb_is_related = true; switch ($vs_tag) { case 'relationship_typename': $vs_spec = 'preferred_labels.' . (caGetOption('orientation', $pa_options, 'LTOR') == 'LTOR' ? 'typename' : 'typename_reverse'); break; case 'relationship_type_id': $vs_spec = 'type_id'; break; case 'relationship_typecode': case 'relationship_type_code': default: $vs_spec = 'type_code'; break; } $vs_rel = $qr_res->get("ca_relationship_types.{$vs_spec}", array_merge($pa_options, $va_tag_opts, array('returnAsArray' => false))); $va_val = array($vs_rel); } elseif ($ps_tablename != $va_tmp[0] && ($t_tmp = $o_dm->getInstanceByTableName($va_tmp[0], true))) { // if the part of the tag before a "." (or the tag itself if there are no periods) is a related table then try to fetch it as related to the current record if (isset($pa_options['placeholderPrefix']) && $pa_options['placeholderPrefix'] && $va_tmp[0] != $pa_options['placeholderPrefix'] && sizeof($va_tmp) == 1) { $vs_get_spec = array_shift($va_tmp) . "." . $pa_options['placeholderPrefix']; if (sizeof($va_tmp) > 0) { $vs_get_spec .= "." . join(".", $va_tmp); } } else { $vs_get_spec = $vs_tag; } $va_spec_bits = explode(".", $vs_get_spec); if (sizeof($va_spec_bits) == 1 && $o_dm->getTableNum($va_spec_bits[0])) { $vs_get_spec .= ".preferred_labels"; } $va_additional_options = array('returnAsArray' => true, 'checkAccess' => $pa_check_access); $vs_hierarchy_name = null; $vb_is_hierarchy = false; if (in_array($va_spec_bits[1], array('hierarchy', '_hierarchyName'))) { $t_rel = $o_dm->getInstanceByTableName($va_spec_bits[0], true); switch ($t_rel->getProperty('HIERARCHY_TYPE')) { case __CA_HIER_TYPE_SIMPLE_MONO__: $va_additional_options['removeFirstItems'] = 1; break; case __CA_HIER_TYPE_MULTI_MONO__: $vs_hierarchy_name = $t_rel->getHierarchyName($qr_res->get($t_rel->tableName() . "." . $t_rel->primaryKey(), array('checkAccess' => $pa_check_access))); $va_additional_options['removeFirstItems'] = 1; break; } } if ($va_spec_bits[1] != '_hierarchyName') { $va_val = $qr_res->get($vs_get_spec, array_merge($pa_options, $va_additional_options, array('returnWithStructure' => true, 'returnBlankValues' => true, 'returnAllLocales' => true, 'useLocaleCodes' => false, 'filters' => $va_tag_filters, 'primaryIDs' => $va_primary_ids))); } else { $va_val = array(); } if (is_array($va_primary_ids) && isset($va_primary_ids[$va_spec_bits[0]]) && is_array($va_primary_ids[$va_spec_bits[0]])) { foreach ($va_primary_ids[$va_spec_bits[0]] as $vn_primary_id) { unset($va_val[$vn_primary_id]); } } if ($va_spec_bits[1] !== 'hierarchy') { $va_val = caExtractValuesByUserLocale($va_val); $va_val_tmp = array(); foreach ($va_val as $vn_d => $va_vals) { if (is_array($va_vals)) { $va_val_tmp = array_merge($va_val_tmp, array_values($va_vals)); } else { $va_val_tmp[] = $va_vals; } } $va_val = $va_val_tmp; } $va_val_proc = array(); switch ($va_spec_bits[1]) { case '_hierarchyName': if ($vs_hierarchy_name) { $va_val_proc[] = $vs_hierarchy_name; } break; case 'hierarchy': if (is_array($va_val) && sizeof($va_val) > 0) { $va_hier_list = array(); if ($vs_hierarchy_name) { array_unshift($va_hier_list, $vs_hierarchy_name); } $vs_name = end($va_spec_bits); foreach ($va_val as $va_hier) { $va_hier = caExtractValuesByUserLocale($va_hier); foreach ($va_hier as $va_hier_item) { foreach ($va_hier_item as $va_hier_value) { $va_hier_list[] = $va_hier_value[$vs_name] ? $va_hier_value[$vs_name] : array_shift($va_hier_value); } } } $va_val_proc[] = join(caGetOption("delimiter", $va_tag_opts, $vs_delimiter), $va_hier_list); } break; case 'parent': if (is_array($va_val)) { foreach ($va_val as $vm_label) { if (is_array($vm_label)) { $t_rel = $o_dm->getInstanceByTableName($va_spec_bits[0], true); if (!$t_rel || !method_exists($t_rel, "getLabelDisplayField")) { $va_val_proc[] = join("; ", $vm_label); } else { $va_val_proc[] = $vm_label[$t_rel->getLabelDisplayField()]; } } else { $va_val_proc[] = $vm_label; } } } break; default: $vs_terminal = end($va_spec_bits); foreach ($va_val as $va_val_container) { if (!is_array($va_val_container)) { if ($va_val_container) { $va_val_proc[] = $va_val_container; } continue; } // Add display field to *_labels terminals if (in_array($vs_terminal, array('preferred_labels', 'nonpreferred_labels')) && !$va_val_container[$vs_terminal]) { $t_rel = $o_dm->getInstanceByTableName($va_spec_bits[0], true); $vs_terminal = $t_rel->getLabelDisplayField(); } $va_val_proc[] = $va_val_container[$vs_terminal]; } break; } $va_val = $va_val_proc; $vb_is_related = true; } else { // // Handle non-related gets // // Default specifiers that end with a modifier to preferred labels if (sizeof($va_tmp) == 2 && in_array($va_tmp[1], array('hierarchy', 'children', 'parent', 'related'))) { array_push($va_tmp, 'preferred_labels'); } $vs_hierarchy_name = null; if (in_array($va_tmp[1], array('hierarchy', '_hierarchyName'))) { switch ($t_instance->getProperty('HIERARCHY_TYPE')) { case __CA_HIER_TYPE_SIMPLE_MONO__: $va_additional_options['removeFirstItems'] = 1; break; case __CA_HIER_TYPE_MULTI_MONO__: $vs_hierarchy_name = $t_instance->getHierarchyName($qr_res->get($t_instance->tableName() . "." . $t_instance->primaryKey(), array('checkAccess' => $pa_check_access))); $va_additional_options['removeFirstItems'] = 1; break; } } if ($va_tmp[0] == $ps_tablename) { array_shift($va_tmp); } // get rid of primary table if it's in the field spec if (!sizeof($va_tmp) && $t_instance->getProperty('LABEL_TABLE_NAME')) { $va_tmp[] = "preferred_labels"; } if (isset($pa_options['showHierarchicalLabels']) && $pa_options['showHierarchicalLabels']) { if (!in_array($va_tmp[0], array('hierarchy', 'children', 'parent', 'related')) && $va_tmp[1] == 'preferred_labels') { array_unshift($va_tmp, 'hierarchy'); } } if (isset($pa_options['placeholderPrefix']) && $pa_options['placeholderPrefix'] && $va_tmp[0] != $pa_options['placeholderPrefix']) { array_splice($va_tmp, -1, 0, $pa_options['placeholderPrefix']); } $vs_get_spec = "{$ps_tablename}." . join(".", $va_tmp); if (in_array($va_tmp[0], array('parent'))) { $va_val[] = $qr_res->get($vs_get_spec, array_merge($pa_options, $va_tag_opts, array('returnAsArray' => false))); } elseif ($va_tmp[0] == '_hierarchyName') { $va_val[] = $vs_hierarchy_name; } else { $va_val_tmp = $qr_res->get($vs_get_spec, array_merge($pa_options, $va_tag_opts, array('returnAsArray' => true, 'returnBlankValues' => true, 'assumeDisplayField' => true, 'filters' => $va_tag_filters, 'checkAccess' => $pa_check_access))); $va_val = array(); if (is_array($va_val_tmp)) { //$va_val_tmp = array_reverse($va_val_tmp); if ($va_tmp[0] == 'hierarchy') { if ($vs_hierarchy_name) { array_shift($va_val_tmp); // remove root array_unshift($va_val_tmp, $vs_hierarchy_name); // replace with hierarchy name } if ($vs_delimiter_tmp = caGetOption('hierarchicalDelimiter', $va_tag_opts)) { $vs_tag_val_delimiter = $vs_delimiter_tmp; } elseif ($vs_delimiter_tmp = caGetOption('hierarchicalDelimiter', $pa_options)) { $vs_tag_val_delimiter = $vs_delimiter_tmp; } elseif ($vs_delimiter_tmp = caGetOption('delimiter', $va_tag_opts, $vs_delimiter)) { $vs_tag_val_delimiter = $vs_delimiter_tmp; } else { $vs_tag_val_delimiter = $vs_delimiter; } } else { $vs_tag_val_delimiter = caGetOption('delimiter', $va_tag_opts, $vs_delimiter); } foreach ($va_val_tmp as $vn_attr_id => $vm_attr_val) { if (is_array($vm_attr_val)) { $va_val[] = join($vs_tag_val_delimiter, $vm_attr_val); } else { $va_val[] = $vm_attr_val; } } } if (sizeof($va_val) > 1 && $va_tmp[0] == 'hierarchy') { $vs_tag_val_delimiter = caGetOption('delimiter', $va_tag_opts, $vs_delimiter); $va_val = array(join($vs_tag_val_delimiter, $va_val)); } } } } } if (is_array($va_val)) { if (sizeof($va_val) > 0) { foreach ($va_val as $vn_j => $vs_val) { if (!is_array($va_tag_val_list[$vn_i][$vn_j][$vs_tag]) || !in_array($vs_val, $va_tag_val_list[$vn_i][$vn_j][$vs_tag])) { $va_tag_val_list[$vn_i][$vn_j][$vs_tag][] = $vs_val; if (is_array($vs_val) && sizeof($vs_val) || strlen($vs_val) > 0) { $va_defined_tag_list[$vn_i][$vn_j][$vs_tag] = true; } } } } else { $va_tag_val_list[$vn_i][0][$vs_tag] = null; $va_defined_tag_list[$vn_i][0][$vs_tag] = false; } } } } $vn_i++; } foreach ($va_tag_val_list as $vn_i => $va_tags_list) { // do sorting? if (is_array($pa_sort)) { $va_sorted_values = $va_sorted_values_tmp = array(); foreach ($va_tags_list as $vn_j => $va_values_by_field) { $vs_key = ''; foreach ($pa_sort as $vn_k => $vs_sort) { if (!isset($va_values_by_field[$vs_sort])) { continue; } $vs_subkey = null; foreach ($va_values_by_field[$vs_sort] as $vn_x => $vs_sort_subval) { if ($va_date = caDateToHistoricTimestamps($vs_sort_subval)) { // try to treat it as a date if ($ps_sort_direction == 'DESC' && ($va_date[0] < $vs_subkey || is_null($vs_subkey))) { $vs_subkey = $va_date[0]; } elseif ($va_date[0] > $vs_subkey || is_null($vs_subkey)) { $vs_subkey = $va_date[0]; } } else { $vs_sort_subval = str_pad($vs_sort_subval, 20, ' ', STR_PAD_LEFT); if ($ps_sort_direction == 'DESC' && ($vs_sort_subval < $vs_subkey || is_null($vs_subkey))) { $vs_subkey = $vs_sort_subval; } elseif ($vs_sort_subval > $vs_subkey || is_null($vs_subkey)) { $vs_subkey = $vs_sort_subval; } } } $vs_key .= $vs_subkey; $va_sorted_values_tmp[$vs_key][] = $va_values_by_field; } } ksort($va_sorted_values_tmp); foreach ($va_sorted_values_tmp as $vs_key => $va_value_list) { foreach ($va_value_list as $vn_x => $va_val) { $va_sorted_values[$vs_key . $vn_x] = $va_val; } } if ($ps_sort_direction == 'DESC') { $va_sorted_values = array_reverse($va_sorted_values); } if (sizeof($va_sorted_values) > 0) { $va_tag_val_list[$vn_i] = $va_tags_list = $va_sorted_values; } } $va_acc = array(); foreach ($va_tags_list as $vn_j => $va_tags) { $va_tag_list = array(); $va_pt_vals = array(); $vs_template = $va_proc_templates[$vn_i]; // Process <if> foreach ($va_if as $va_def_con) { if (ExpressionParser::evaluate($va_def_con['rule'], $va_tags)) { $vs_template = str_replace($va_def_con['directive'], $va_def_con['content'], $vs_template); } else { $vs_template = str_replace($va_def_con['directive'], '', $vs_template); } } // Process <more> tags foreach ($va_mores as $vn_more_index => $va_more) { if (($vn_pos = strpos($vs_template, $va_more['directive'])) !== false) { if (isset($va_mores[$vn_more_index + 1]) && ($vn_next_more_pos = strpos(substr($vs_template, $vn_pos + strlen($va_more['directive'])), $va_mores[$vn_more_index + 1]['directive'])) !== false) { $vn_next_more_pos += $vn_pos; $vs_partial_template = substr($vs_template, $vn_pos + strlen($va_more['directive']), $vn_next_more_pos - $vn_pos); } else { $vs_partial_template = substr($vs_template, $vn_pos + strlen($va_more['directive'])); } $vb_output = false; foreach (array_keys($va_defined_tag_list[$vn_i][$vn_j]) as $vs_defined_tag) { if (strpos($vs_partial_template, $vs_defined_tag) !== false) { // content is defined $vb_output = true; break; } } if ($vb_output) { $vs_template = preg_replace('!' . $va_more['directive'] . '!', $va_more['content'], $vs_template, 1); } else { $vs_template = preg_replace('!' . $va_more['directive'] . '!', '', $vs_template, 1); } } } // Process <between> tags - text to be output if it is between two defined values $va_between_positions = array(); foreach ($va_betweens as $vn_between_index => $va_between) { $vb_output_before = $vb_output_after = false; if (($vn_cur_pos = strpos($vs_template, $va_between['directive'])) !== false) { $va_between_positions[$vn_between_index] = $vn_cur_pos; // Get parts of template before tag and after tag $vs_partial_template_before = substr($vs_template, 0, $vn_cur_pos); $vs_partial_template_after = substr($vs_template, $vn_cur_pos + strlen($va_between['directive'])); // Only get the template between our current position and the next <between> tag if (isset($va_betweens[$vn_between_index + 1]) && ($vn_after_pos_relative = strpos($vs_partial_template_after, $va_betweens[$vn_between_index + 1]['directive'])) !== false) { $vs_partial_template_after = substr($vs_partial_template_after, 0, $vn_after_pos_relative); } // Check for defined value before and after tag foreach (array_keys($va_defined_tag_list[$vn_i][$vn_j]) as $vs_defined_tag) { if (strpos($vs_partial_template_before, $vs_defined_tag) !== false) { // content is defined $vb_output_after = true; } if (strpos($vs_partial_template_after, $vs_defined_tag) !== false) { // content is defined $vb_output_before = true; break; } if ($vb_output_before && $vb_output_after) { break; } } } if ($vb_output_before && $vb_output_after) { $vs_template = preg_replace('!' . $va_between['directive'] . '!', $va_between['content'], $vs_template, 1); } else { $vs_template = preg_replace('!' . $va_between['directive'] . '!', '', $vs_template, 1); } } // // Need to sort tags by length descending (longest first) // so that when we go to substitute and you have a tag followed by itself with a suffix // (ex. ^measurements and ^measurements2) we don't substitute both for the stub (ex. ^measurements) // $va_tags_tmp = array_keys($va_tags); usort($va_tags_tmp, function ($a, $b) { return strlen($b) - strlen($a); }); $vs_pt = $vs_template; foreach ($va_tags_tmp as $vs_tag) { $vs_pt = str_replace('^' . $vs_tag, is_array($va_tags[$vs_tag]) ? join(" | ", $va_tags[$vs_tag]) : $va_tags[$vs_tag], $vs_pt); } if ($vs_pt) { $va_pt_vals[] = $vs_pt; } if ($vs_acc_val = join(isset($pa_options['delimiter']) ? $pa_options['delimiter'] : $vs_delimiter, $va_pt_vals)) { $va_acc[] = $vs_acc_val; } } $va_proc_templates[$vn_i] = join($vs_delimiter, $va_acc); } if ($pb_return_as_array && !caGetOption('includeBlankValuesInArray', $pa_options, false)) { foreach ($va_proc_templates as $vn_i => $vs_template) { if (!strlen(trim($vs_template))) { unset($va_proc_templates[$vn_i]); } } } // Transform links $va_proc_templates = caCreateLinksFromText($va_proc_templates, $ps_resolve_links_using, $ps_resolve_links_using != $ps_tablename ? $va_resolve_links_using_row_ids : $pa_row_ids, null, caGetOption('linkTarget', $pa_options, null), array_merge(array('addRelParameter' => true), $pa_options)); // Kill any lingering tags (just in case) foreach ($va_proc_templates as $vn_i => $vs_proc_template) { $va_proc_templates[$vn_i] = preg_replace("!\\^([A-Za-z0-9_\\.]+[%]{1}[^ \\^\t\r\n\"\\'<>\\(\\)\\{\\}\\/\\[\\]]*|[A-Za-z0-9_\\.]+)!", "", $vs_proc_template); $va_proc_templates[$vn_i] = str_replace("<![CDATA[", "", $va_proc_templates[$vn_i]); $va_proc_templates[$vn_i] = str_replace("]]>", "", $va_proc_templates[$vn_i]); } if ($pb_return_as_array) { return $va_proc_templates; } return join($vs_delimiter, $va_proc_templates); }
/** * Process page HTML * @param string HTML * @return string Formatted HTML */ public function processPageHTML($html) { $html = mb_convert_encoding($html, 'UTF-8', mb_detect_encoding($html, 'UTF-8, ISO-8859-1', TRUE)); $dom = str_get_dom($html); $this->setImageSrc($dom); $this->removeEmptyTags($dom); $html = (string) $dom; $html = str_replace('</div>', "</div>\n", $html); $html = str_replace('<div ', ' <div ', $html); return $html; }