public function valid() { if ($this->length > 0 && $this->offset + $this->length <= $this->count) { return false; } if (is_string($this->name) && strpos($this->name, '|') !== false) { $this->name = explode('|', $this->name); } if (is_array($this->name)) { $tags = []; foreach ($this->name as $name) { try { $get_tag = \ebi\Xml::extract($this->plain, $name); $tags[$get_tag->cur()] = $get_tag; } catch (\ebi\exception\NotFoundException $e) { } } if (empty($tags)) { return false; } ksort($tags, SORT_NUMERIC); foreach ($tags as $this->tag) { return true; } } try { $this->tag = \ebi\Xml::extract($this->plain, $this->name); return true; } catch (\ebi\exception\NotFoundException $e) { } return false; }
private static function parse($html, $url) { $forms = []; try { foreach (\ebi\Xml::extract($html, 'body')->find('form') as $key => $formtag) { $form = new \stdClass(); $form->name = $formtag->in_attr('name', $formtag->in_attr('id', $key)); $form->action = \ebi\Util::path_absolute($url, $formtag->in_attr('action', $url)); $form->method = strtolower($formtag->in_attr('method', 'get')); $form->multiple = false; $form->element = []; foreach ($formtag->find('input') as $count => $input) { $obj = new \stdClass(); $obj->name = $input->in_attr('name', $input->in_attr('id', 'input_' . $count)); $obj->type = strtolower($input->in_attr('type', 'text')); $obj->value = self::htmldecode($input->in_attr('value')); $obj->selected = 'selected' === strtolower($input->in_attr('checked', $input->in_attr('checked'))); $obj->multiple = false; $form->element[] = $obj; } foreach ($formtag->find('textarea') as $count => $input) { $obj = new \stdClass(); $obj->name = $input->in_attr('name', $input->in_attr('id', 'textarea_' . $count)); $obj->type = 'textarea'; $obj->value = self::htmldecode($input->value()); $obj->selected = true; $obj->multiple = false; $form->element[] = $obj; } foreach ($formtag->find('select') as $count => $input) { $obj = new \stdClass(); $obj->name = $input->in_attr('name', $input->in_attr('id', 'select_' . $count)); $obj->type = 'select'; $obj->value = []; $obj->selected = true; $obj->multiple = 'multiple' == strtolower($input->param('multiple', $input->attr('multiple'))); foreach ($input->find('option') as $count => $option) { $op = new \stdClass(); $op->value = self::htmldecode($option->in_attr('value', $option->value())); $op->selected = 'selected' == strtolower($option->in_attr('selected', $option->in_attr('selected'))); $obj->value[] = $op; } $form->element[] = $obj; } $forms[] = $form; } } catch (\ebi\exception\NotFoundException $e) { } return $forms; }
/** * Set template * @param string $template_path Relative path from resource_path * @param mixed{} $vars bind variables * @return $this */ public function set_template($template_path, $vars = []) { /** * @param string $path Email template resources root */ $resource_path = \ebi\Conf::get('resource_path', \ebi\Conf::resource_path('mail')); $path = \ebi\Util::path_absolute($resource_path, $template_path); if (!is_file($path)) { throw new \ebi\exception\InvalidArgumentException($template_path . ' not found'); } $xml = \ebi\Xml::extract(file_get_contents($path), 'mail'); try { try { $from = $xml->find_get('from'); $this->from($from->in_attr('address'), $from->in_attr('name')); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } try { $this->return_path($xml->find_get('return_path')->in_attr('address')); } catch (\ebi\exception\NotFoundException $e) { } /** * @param string $xtc_name xtc query key */ $xtc_name = \ebi\Conf::get('xtc_name', 'xtc'); $xtc = self::xtc($template_path); $this->header['X-T-Code'] = $xtc; $vars['t'] = new \ebi\FlowHelper(); $vars['xtc'] = [$xtc_name => $xtc]; $subject = trim(str_replace(["\r\n", "\r", "\n"], '', $xml->find_get('subject')->value())); $template = new \ebi\Template(); $template->cp($vars); $body_xml = $xml->find_get('body'); $signature = $body_xml->in_attr('signature'); $signature_text = ''; if (!empty($signature)) { $sig_path = \ebi\Util::path_absolute($resource_path, $signature); if (!is_file($sig_path)) { throw new \ebi\exception\InvalidArgumentException($signature . ' not found'); } $sig_xml = \ebi\Xml::extract(file_get_contents($sig_path), 'mail'); $signature_text = \ebi\Util::plain_text(PHP_EOL . $sig_xml->find_get('signature')->value() . PHP_EOL); } $message = $template->get(\ebi\Util::plain_text(PHP_EOL . $body_xml->value() . PHP_EOL) . $signature_text); $this->message($message); $this->subject($template->get($subject)); try { $html = $xml->find_get('html'); $html_path = \ebi\Util::path_absolute($resource_path, $html->in_attr('src', preg_replace('/^(.+)\\.\\w+$/', '\\1', $path) . '.html')); foreach ($html->find('media') as $media) { $file = \ebi\Util::path_absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \ebi\Template(); $template->cp($vars); $this->html($template->read($html_path)); } catch (\ebi\exception\NotFoundException $e) { } foreach ($xml->find('attach') as $attach) { $file = \ebi\Util::path_absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \ebi\exception\InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } catch (\ebi\exception\NotFoundException $e) { throw new \ebi\exception\InvalidArgumentException($template_path . ' invalid data'); } }
$p = "<abc><def>111</def></abc>"; $x = \ebi\Xml::extract($p, 'abc'); eq("abc", $x->name()); $p = "<abc><def>111</def></abc>"; $x = \ebi\Xml::extract($p, "def"); eq("def", $x->name()); eq(111, $x->value()); try { $p = "aaaa"; \ebi\Xml::extract($p, 'abc'); fail(); } catch (\ebi\exception\NotFoundException $e) { } try { $p = "<abc>sss</abc>"; \ebi\Xml::extract($p, "def"); fail(); } catch (\ebi\exception\NotFoundException $e) { } $p = "<abc>sss</a>"; $x = \ebi\Xml::extract($p, 'abc'); eq("<abc />", $x->get()); $p = "<abc>0</abc>"; $x = \ebi\Xml::extract($p, 'abc'); eq("abc", $x->name()); eq("0", $x->value()); $p = "<abc />"; $x = \ebi\Xml::extract($p, "abc"); $p = "<abc/>"; $x = \ebi\Xml::extract($p, "abc");
public static function mail_template_list() { $path = \ebi\Conf::get(\ebi\Mail::class . '@resource_path', \ebi\Conf::resource_path('mail')); $template_list = []; try { foreach (\ebi\Util::ls($path, true, '/\\.xml$/') as $f) { $info = new \ebi\Dt\DocInfo(); $info->name(str_replace(\ebi\Util::path_slash($path, null, true), '', $f->getPathname())); try { $xml = \ebi\Xml::extract(file_get_contents($f->getPathname()), 'mail'); $info->document($xml->find_get('subject')->value()); $info->set_opt('x_t_code', \ebi\Mail::xtc($info->name())); $template_list[] = $info; } catch (\ebi\exception\NotFoundException $e) { } } } catch (\ebi\exception\InvalidArgumentException $e) { } return $template_list; }
/** * bodyをXMLとして解析しXMLオブジェクトとして返す * @return \ebi\Xml */ public function xml($name = null) { return \ebi\Xml::extract($this->body(), $name); }
private function html_list($src) { if (preg_match_all('/<(table|ul|ol)\\s[^>]*rt\\:/i', $src, $m, PREG_OFFSET_CAPTURE)) { $tags = []; foreach ($m[1] as $v) { try { $tags[] = \ebi\Xml::extract(substr($src, $v[1] - 1), $v[0]); } catch (\ebi\exception\NotFoundException $e) { } } foreach ($tags as $obj) { $obj->escape(false); $name = strtolower($obj->name()); $param = $obj->in_attr('rt:param'); $value = sprintf('<rt:loop param="%s" var="%s" counter="%s" ' . 'key="%s" ' . '>', $param, $obj->in_attr('rt:var', 'loop_var'), $obj->in_attr('rt:counter', 'loop_counter'), $obj->in_attr('rt:key', 'loop_key')); $rawvalue = $obj->value(); if ($name == 'table') { try { $t = \ebi\Xml::extract($rawvalue, 'tbody'); $t->escape(false); $t->value($value . $t->value() . '</rt:loop>'); $value = str_replace($t->plain(), $t->get(), $rawvalue); } catch (\ebi\exception\NotFoundException $e) { $value = $value . $rawvalue . '</rt:loop>'; } } else { $value = $value . $rawvalue . '</rt:loop>'; } $obj->value($this->html_list($value)); $obj->rm_attr('rt:param', 'rt:key', 'rt:var', 'rt:counter'); $src = str_replace($obj->plain(), $obj->get(), $src); } } return $src; }
$tag = \ebi\Xml::extract($src, 'tag'); eq("hoge", $tag->find_get("abc/def/ghi")->value()); eq("123", $tag->find_get("abc/def")->in_attr('var')); eq("selected", $tag->find_get("abc/def/ghi")->in_attr('selected')); eq("<def var='123'><ghi selected>hoge</ghi></def>", $tag->find_get("abc/def")->plain()); try { $tag->find_get("abc/def/xyz"); fail(); } catch (\ebi\exception\NotFoundException $e) { } $src = <<<'PRE' <tag> <abc> <def var="123"> <ghi selected>hoge</ghi> <ghi> <jkl>rails</jkl> </ghi> <ghi ab="true">django</ghi> </def> </abc> </tag> PRE; $tag = \ebi\Xml::extract($src, "tag"); eq("django", $tag->find_get("abc/def/ghi", 2)->value()); eq("rails", $tag->find_get("abc/def/ghi", 1)->find_get('jkl')->value()); eq("123", $tag->find_get("abc/def")->in_attr('var')); eq("true", $tag->find_get("abc/def/ghi", 2)->in_attr('ab')); eq('selected', $tag->find_get("abc/def/ghi")->in_attr('selected')); eq(null, $tag->find_get("abc/def/ghi", 1)->in_attr('selected')); eq([], $tag->find_get("abc/def")->find('xyz'));
/** * $srcから対象のXMLをすべて置換した文字列を返す * @param string $src * @param string $name * @param callable $func * @return string */ public static function find_replace_all($src, $name, $func) { try { if (!is_callable($func)) { throw new \ebi\exception\InvalidArgumentException('invalid function'); } $i = 0; while (true) { $xml = \ebi\Xml::extract($src, $name); $replace = call_user_func_array($func, [$xml]); if (!is_null($replace)) { $src = str_replace($xml->plain(), $replace instanceof self ? $replace->get() : $replace, $src); } if ($i++ > 100) { throw new \ebi\exception\RetryLimitOverException('Maximum function nesting level of ’100'); } } } catch (\ebi\exception\NotFoundException $e) { } return $src; }
<?php $xml = \ebi\Xml::extract('<asd><abc><def><aa>AA</aa><bb>BB</bb><cc>CC</cc></def></abc></asd>'); eq('<asd><abc><def><aa>AA</aa><bb>ZZ</bb><cc>CC</cc></def></abc></asd>', $xml->replace('def/bb', 'ZZ')->get()); eq('<asd><abc><def><aa>AA</aa><bb>BB</bb><cc>CC</cc></def></abc></asd>', $xml->get()); $xml = \ebi\Xml::extract('<asd><abc><def><aa>AA</aa><bb>BB</bb><cc>CC</cc></def></abc></asd>'); eq('<asd><abc><def>ZZ</def></abc></asd>', $xml->replace('def', 'ZZ')->get()); $xml = \ebi\Xml::extract('<asd><abc><def><aa>AA</aa><bb>BB</bb><cc>CC</cc><dd><aaa>AAA</aaa><bbb>BBB</bbb><ccc>CCC</ccc></dd></def></abc></asd>'); eq('<asd><abc><def><aa>AA</aa><bb>BB</bb><cc>CC</cc><dd><aaa>AAA</aaa><bbb>ZZZ</bbb><ccc>CCC</ccc></dd></def></abc></asd>', $xml->replace('def/dd/bbb', 'ZZZ')->get());