Esempio n. 1
0
function main($html)
{
    global $baseurl;
    if ($html) {
        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = FALSE;
        $dom->loadHTML($html);
        $xpath = new DomXpath($dom);
        $container = $xpath->query('//*[@id="maincontentcenter"]')->item(0);
        $entries = $xpath->query('//*[@style="margin-left:15px;"]/ul', $container);
        $jobs = array();
        foreach ($entries as $entry) {
            $department = $entry->previousSibling->previousSibling->nodeValue;
            $li = $xpath->query('li', $entry);
            foreach ($li as $job) {
                $this_job = array();
                $this_job['department'] = $department;
                $a = $xpath->query('a', $job);
                $href = $a->item(0)->getAttribute('href');
                $title = $a->item(0)->nodeValue;
                $this_job['title'] = $title;
                $this_job['url'] = $baseurl . "/" . $href;
                $details = jobDetails($this_job['url']);
                $jobs[] = $this_job;
            }
        }
    }
    print_r($jobs);
}
Esempio n. 2
0
 private function CreateDomDocument()
 {
     $dom = new Domdocument();
     @$dom->loadHTML($this->htmlStack);
     $xpath = new DomXpath($dom);
     // Find all the product blocks that contain beer attribute that we want to search for such as rating, title, price, etc.
     $elements = $xpath->query('//article[contains(@class, "' . $this->class . '")]');
     foreach ($elements as $element) {
         // get the node value from the .title class (the title of the product)
         $title = $xpath->query('descendant::div[@class="title"]', $element);
         $title = $title->item(0)->nodeValue;
         // get the attribute value from the .link-overlay class (the actual link to the product)
         $link = $xpath->query('descendant::a[@class="link-overlay"]', $element);
         $link = $link->item(0)->getAttribute('href');
         $link = 'https://www.gall.nl' . $link;
         // get the image src value to rip the image from the site
         $image = $xpath->query('descendant::div[@class="image"]/node()/node()', $element);
         $image = $image->item(1)->getAttribute('src');
         // Dive in the link that a single product has
         $results = $this->curlGetDeep($link);
         // if a regex option has been supplied run it on the results
         if ($this->regex) {
             $title = preg_replace($this->regex, '', $title);
         }
         // remove the unwanted 6X 35x6(bulk beers) results with this regex
         if (!preg_match('/\\dX(\\d+)?/', $title)) {
             $this->elementsArray[] = ['title' => $title, 'link' => $link, 'image' => $image, 'description' => $results['description'], 'abv' => $results['abv']];
         }
     }
     $this->json = json_encode(['beers' => $this->elementsArray]);
 }
Esempio n. 3
0
 /**
  * Extract a series of information from a twitter page.
  *
  * @see doc/Twitter.md for the detailed structure of the returned array
  * @see Twitter::queryToMeta() for details about the $meta array
  *
  * @param string $page content of a twitter page
  * @param array  $meta
  *
  * @return array|bool false on error
  */
 protected function parse($page, $meta)
 {
     $twitter['meta'] = $meta;
     $tweet = '//ol[@id="stream-items-id"]/li[@data-item-type="tweet"]';
     // foo_bar will be converted to foo['bar']
     $tweet_details = ['user_avatar' => './/img[contains(@class, "avatar")]/@src', 'user_fullname' => './/@data-name', 'user_name' => './/@data-screen-name', 'stats_fav' => './/span[contains(@class, "ProfileTweet-action--favorite")]//@data-tweet-stat-count', 'stats_rt' => './/span[contains(@class, "ProfileTweet-action--retweet")]//@data-tweet-stat-count', 'id' => './/@data-tweet-id', 'retweetid' => './/@data-retweet-id', 'text' => './/p[contains(@class, "tweet-text")]', 'datetime' => './/@data-time'];
     $user_stats = '//ul[contains(@class, "ProfileNav-list")]';
     $user_stats_details = ['followers' => './/li[contains(@class, "ProfileNav-item--followers")]/a/@title', 'following' => './/li[contains(@class, "ProfileNav-item--following")]/a/@title', 'likes' => './/li[contains(@class, "ProfileNav-item--favorites")]/a/@title', 'lists' => './/li[contains(@class, "ProfileNav-item--lists")]/a/@title', 'tweets' => './/li[contains(@class, "ProfileNav-item--tweets")]/a/@title'];
     libxml_use_internal_errors(true);
     $dom = new \DOMDocument();
     $dom->preserveWhiteSpace = false;
     $dom->loadHTML($page);
     $xpath = new \DomXpath($dom);
     foreach ($xpath->query($tweet) as $item) {
         foreach ($tweet_details as $key => $query) {
             $value = @$xpath->query($query, $item)->item(0)->textContent;
             if ($key == 'retweetid') {
                 $details['retweet'] = empty($value) ? 0 : 1;
                 continue;
             }
             if ($key == 'text') {
                 $value = $this->clean($value);
                 $value = htmlspecialchars($value, ENT_NOQUOTES);
                 $details['text']['raw'] = $value;
                 $details['text']['html'] = $this->html($value);
                 $details['reply'] = substr($value, 0, 1) === '@' ? 1 : 0;
                 continue;
             }
             if ($key == 'datetime') {
                 $value = date('c', $value);
             }
             if (strpos($key, '_') !== false) {
                 $subarray = explode('_', $key);
                 $subname = $subarray[0];
                 $subkey = $subarray[1];
                 $details[$subname][$subkey] = $value;
             } else {
                 $details[$key] = $value;
             }
         }
         $details['user']['url'] = 'https://twitter.com/' . $details['user']['name'];
         $details['url'] = $details['user']['url'] . '/status/' . $details['id'];
         // type casting
         $details['id'] = (int) $details['id'];
         $details['stats']['fav'] = (int) $details['stats']['fav'];
         $details['stats']['rt'] = (int) $details['stats']['rt'];
         ksort($details);
         $twitter['tweets'][] = $details;
     }
     $twitter['tweets_count'] = count($twitter['tweets']);
     if ($meta['type'] == 'user') {
         $item = $xpath->query($user_stats)->item(0);
         foreach ($user_stats_details as $key => $query) {
             $value = @$xpath->query($query, $item)->item(0)->textContent;
             $twitter['user_stats'][$key] = preg_match('~([\\d,]+)~', $value, $matches) ? (int) strtr($matches[1], [',' => '']) : 0;
         }
     }
     ksort($twitter);
     return $twitter;
 }
function get_viber($owner)
{
    $conn = curl_init();
    if ($conn != false) {
        curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, 5);
        $url = 'http://chats.viber.com/' . $owner;
        curl_setopt($conn, CURLOPT_URL, $url);
        $res = curl_exec($conn);
        if ($res != false) {
            curl_close($conn);
            $doc = new DOMDocument();
            @$doc->loadHTML($res);
            $tags = $doc->getElementsByTagName('img');
            $images = array();
            $i = 0;
            foreach ($tags as $tag) {
                $images[$i] = $tag->getAttribute('src');
                //echo $images[$i].'<br>';
                $i++;
            }
            $xpath = new DomXpath($doc);
            $classname = 'participant-name';
            $par_names = array();
            $results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' {$classname} ')]");
            if ($results->length > 0) {
                $par_names[0] = $results->item(0)->nodeValue;
                if (strlen($par_names[0]) > 15) {
                    $par_names[0] = substr($par_names[0], 0, 12) . '...';
                }
                $par_names[1] = $results->item(1)->nodeValue;
                if (strlen($par_names[1]) > 15) {
                    $par_names[1] = substr($par_names[1], 0, 12) . '...';
                }
                $par_names[2] = $results->item(2)->nodeValue;
                if (strlen($par_names[2]) > 15) {
                    $par_names[2] = substr($par_names[2], 0, 12) . '...';
                }
                $par_names[3] = $results->item(3)->nodeValue;
                if (strlen($par_names[3]) > 15) {
                    $par_names[3] = substr($par_names[3], 0, 12) . '...';
                }
            }
            $classname2 = 'followers';
            $results2 = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' {$classname2} ')]");
            if ($results2->length > 0) {
                $followers = $results2->item(0)->nodeValue;
            }
            $html = gen_html($images, $par_names, $owner, $followers);
            return $html;
        } else {
            return '<h2>Content not available!</h2>';
        }
    }
}
Esempio n. 5
0
File: CR.php Progetto: XaaT/ttb
 public function processRawContent($cr)
 {
     $processCR = array();
     //Raw HTML to DOM/XPATH
     $dom = new DOMDocument();
     $searchPage = mb_convert_encoding($cr['content_raw'], 'HTML-ENTITIES', "UTF-8");
     @$dom->loadHTML($searchPage);
     @($xpath = new DomXpath($dom));
     $processCR['content_raw'] = $cr['content_raw'];
     //userID :
     $divProfil = $xpath->query('//td[@class = "messCase2"]//a[contains(@href, "profil-")]');
     if ($divProfil->length == 0) {
         //pas de profil
         $processCR['hfr_user_id'] = 1;
     } else {
         $processCR['hfr_user_id'] = $divProfil->item(0)->getAttribute('href');
         $processCR['hfr_user_id'] = preg_replace("/[^0-9]+/", "", $processCR['hfr_user_id']);
     }
     //echo $processActu['hfr_user_id']."<br>";
     //post ID
     $processCR["hfr_post_id"] = $xpath->query('//a[contains(@href, "#t")]')->item(0)->getAttribute('href');
     $processCR['hfr_post_id'] = str_replace("#t", "", $processCR["hfr_post_id"]);
     //pseudo :
     $processCR['username'] = $xpath->query('//td[@class = "messCase1"]//b[@class = "s2"]')->item(0)->textContent;
     $processCR['username'] = preg_replace('/[^(\\x20-\\x7F)]*/', '', htmlentities($processCR['username']));
     //date posted
     $divDate = $xpath->query('//div[@class="toolbar"]/div[@class="left"]');
     $oldate = preg_replace("/[^0-9:-]+/", "", $divDate->item(0)->nodeValue);
     $processCR['date_posted'] = date('Y-m-d H:i', strtotime($oldate));
     //echo $crDatePosted.' ';
     //le contenu du CR
     $divCR = $xpath->query('//div[@id = "para' . $processCR['hfr_post_id'] . '"]/node()');
     //print_r($divCR);
     $processHTMLstep1 = '';
     foreach ($divCR as $node) {
         //var_dump($actuNode);
         //echo $dom->saveHTML($actuNode)."\n";
         if ($node->nodeType == XML_ELEMENT_NODE && ($node->getAttribute('class') == 'edited' || $node->getAttribute('class') == 'signature' || $node->getAttribute('style') == 'clear: both;')) {
             continue;
         }
         if ($node->nodeValue || $node->childNodes) {
             $processHTMLstep1 .= htmlentities($dom->saveHTML($node));
         }
     }
     $processHTMLfinal = $processHTMLstep1;
     $processHTMLfinal = str_replace("\n", '', $processHTMLfinal);
     $processHTMLfinal = str_replace("\r", '', $processHTMLfinal);
     $processHTMLfinal = htmlentities(str_replace('href="/hfr/', 'target="_blank" href="http://forum.hardware.fr/hfr/', html_entity_decode($processHTMLfinal)));
     $processHTMLfinal = htmlentities(preg_replace('/style="[a-zA-Z0-9:;\\.\\s\\(\\)\\-\\,]*"/i', '', html_entity_decode($processHTMLfinal)));
     $processHTMLfinal = htmlentities(preg_replace('/onload="[^\\"]+"/i', 'onload="return;"', html_entity_decode($processHTMLfinal)));
     $processHTMLfinal = htmlentities(preg_replace('/title="[^\\"]+"/i', '', html_entity_decode($processHTMLfinal)));
     $processHTMLfinal = str_replace("hfr-rehost.net/", 'reho.st/', $processHTMLfinal);
     $processHTMLfinal = htmlentities(str_replace("<p><br></p>", '', html_entity_decode($processHTMLfinal)));
     $processCR['content'] = $processHTMLfinal;
     return $processCR;
 }
Esempio n. 6
0
 public function query($query, $options = [])
 {
     $this->result = file_get_contents($this->url . "select?" . $this->make_query($query, $options));
     if ($this->result) {
         $this->xml = new DOMDocument("1.0", "UTF-8");
         $this->xml->formatOutput = true;
         $this->xml->loadXML($this->result);
         $xpath = new DomXpath($this->xml);
         $resultNode = $xpath->query("//result");
         if ($resultNode->length) {
             $this->rows = $resultNode->item(0)->getAttribute("numFound");
             $this->data = $xpath->query("//doc");
         }
     }
 }
Esempio n. 7
0
 public function openGame($idx)
 {
     $file = $this->games[$idx]['name'];
     $doc = new DOMDocument();
     $doc->preserveWhiteSpace = true;
     $doc->formatOutput = true;
     // modify state
     $libxml_previous_state = libxml_use_internal_errors(true);
     // parse
     $doc->loadHTMLFile("Games/" . $file);
     // Usuwanie scriptow - bład z tagamiw scripcie
     $domNodeList = $doc->getElementsByTagname('script');
     $domElemsToRemove = array();
     foreach ($domNodeList as $domElement) {
         $domElemsToRemove[] = $domElement;
     }
     foreach ($domElemsToRemove as $domElement) {
         $domElement->parentNode->removeChild($domElement);
     }
     $xpath = new DomXpath($doc);
     // traverse all results
     foreach ($xpath->query('//div[@tiddler]') as $tid) {
         if ($tid->getAttribute('tiddler') != 'checkvars') {
             $this->tiddlers[$tid->getAttribute('tiddler')] = $tid->nodeValue;
         }
     }
     // handle errors
     libxml_clear_errors();
     // restore
     libxml_use_internal_errors($libxml_previous_state);
 }
Esempio n. 8
0
 public function run($str = NULL)
 {
     if ($str == NULL) {
         return $this;
     }
     $dom = $this->node->dom()->get();
     $this->node->_lastDom = $dom->lastdom . $str;
     $rec = new DomDocument();
     $rec->loadHtml($str);
     if ($rec->doctype) {
         $rec->removeChild($rec->doctype);
     }
     $document = jqm_use($this->node->_parentElement);
     $dom = $document->_DOM;
     if ($dom->doctype) {
         $dom->removeChild($dom->doctype);
     }
     $body = $rec->childNodes->item(0)->firstChild;
     $frag = $dom->importNode($body, true);
     $xpath = new DomXpath($dom);
     $find = $this->node->getPathByID($dom);
     $find = $xpath->query($find);
     if ($find->length > 0) {
         $save = $find->item(0)->parentNode->insertBefore($frag->firstChild, $find->item(0)->nextSibling);
         $this->node->_path = $save->previousSibling->getNodePath();
         $document->_DOM = $dom;
     }
     return $this;
 }
Esempio n. 9
0
 public function run()
 {
     $document = jqm_use($this->node->_parentElement);
     $doc = $document->_DOM;
     if ($doc->doctype) {
         $doc->removeChild($doc->doctype);
     }
     $pt = $this->node->getPathByID();
     if ($pt) {
         $path = new DomXpath($doc);
         $get = $path->query($pt);
         if ($get->length > 0) {
             $this->node->_attributes = array();
             $this->node->_innerHtml = NULL;
             $this->node->_name = NULL;
             $this->node->_localName = NULL;
             $this->node->_length = false;
             $save = $get->item(0)->parentNode->removeChild($get->item(0));
             $document->offsetUnset($this->node->_token);
         }
     } else {
         $document->offsetUnset($this->node->_token);
         $this->node->_length = false;
     }
 }
Esempio n. 10
0
 public function run($str = NULL)
 {
     if ($str == NULL) {
         return $this;
     }
     $document = jqm_use($this->node->_parentElement);
     $dom = $document->_DOM;
     if ($dom->doctype) {
         $dom->removeChild($dom->doctype);
     }
     $find = $this->node->getPathById($dom);
     if (!$find) {
         $find = $this->node->_path;
     }
     $xpath = new DomXpath($dom);
     $find = $xpath->query($find);
     if ($find->length > 0) {
         $child = new DomDocument();
         $child->loadHtml($str);
         if ($child->doctype) {
             $child->removeChild($child->doctype);
         }
         $child->normalize();
         $frag = $dom->importNode($child->firstChild->firstChild->firstChild, true);
         $save = $find->item(0)->parentNode->insertBefore($frag, $find->item(0));
         $this->node->_path = $save->nextSibling->getNodePath();
         $document->_DOM = $dom;
     }
     return $this;
 }
 public function convert($mailchimp)
 {
     $vars = array('*|MC:SUBJECT|*' => '<?php echo $this->getSubject() ?>', '*|ARCHIVE|*' => '<?php echo $this->getViewInBrowserUrl() ?>', '*|CURRENT_YEAR|*' => '<?php echo date("Y") ?>', '*|UNSUB|*' => '<?php echo $this->getUnsubscribeUrl() ?>', '*|FACEBOOK:PROFILEURL|*' => '<?php echo $this->getFacebookUrl() ?>', '*|TWITTER:PROFILEURL|*' => '<?php echo $this->getTwitterUrl() ?>', '*|LIST:COMPANY|*' => '<?php echo $this->getStoreName() ?>', '*|LIST:DESCRIPTION|*' => '', '*|HTML:LIST_ADDRESS_HTML|*' => '*****@*****.**', '*|IF:REWARDS|* *|HTML:REWARDS|*' => '', '*|FORWARD|*' => '', '/*@editable*/' => '', '*|UPDATE_PROFILE|*' => '', 'mc:repeatable' => '', 'mc:allowtext' => '', 'mc:hideable' => '', 'mc:allowdesigner' => '', '*|IFNOT:ARCHIVE_PAGE|*' => '', '*|END:IF|*' => '', 'mc:edit' => 'mcedit');
     foreach ($vars as $old => $new) {
         $mailchimp = str_replace($old, $new, $mailchimp);
     }
     $dom = new DOMDocument();
     $dom->recover = true;
     $dom->strictErrorChecking = false;
     @$dom->loadHTML($mailchimp);
     $xpath = new DomXpath($dom);
     $items = $xpath->query('//*[@mcedit]');
     foreach ($items as $mcedit) {
         $area = 'area.' . $mcedit->getAttribute('mcedit');
         if ($area == 'area.monkeyrewards') {
             $mcedit->parentNode->removeChild($mcedit);
             continue;
         }
         $mcedit->removeAttribute('mcedit');
         $a = $dom->createComment('<?php echo $this->area("' . $area . '"); ?>');
         // $b = $dom->createComment('{{/ivar}}');
         $mcedit->parentNode->insertBefore($a, $mcedit);
         // $mcedit->parentNode->insertBefore($b);
     }
     $dom->formatOutput = true;
     $dom->preserveWhitespace = false;
     $html = $dom->saveHTML();
     $html = str_replace('%7B', '{', $html);
     $html = str_replace('%7D', '}', $html);
     $html = str_replace('%20', ' ', $html);
     $html = preg_replace('/mc:label="[a-zA-Z0-9_]*"/', '', $html);
     $html = preg_replace('/mc:variant="[^"]*"/', '', $html);
     $html = str_replace('%24', '$', $html);
     return $html;
 }
 /**
  * @param $content
  *
  * @return \DOMElement
  */
 private function getImage($content)
 {
     $dom = new \DOMDocument();
     $dom->loadHTML($content);
     $xpath = new \DomXpath($dom);
     return $xpath->query('//img[@alt="Exception detected!"]')->item(0);
 }
Esempio n. 13
0
 public function run($query = '*')
 {
     $path = explode('/', $this->node->_path);
     $el = $this->__toDomElement();
     $document = jqm_use($this->node->_parentElement);
     if (count($path) > 0) {
         if ($query === NULL || $query == '*') {
             $query = $el->parentNode->tagName;
         }
         $t = count($path);
         $doc = $document->_DOM;
         if ($doc->doctype) {
             $doc->removeChild($doc->doctype);
         }
         $xpath = new DomXpath($doc);
         for ($i = 0; $t > $i;) {
             array_pop($path);
             ++$i;
             if (empty($path)) {
                 continue;
             }
             $find = $xpath->query(implode('/', $path));
             if ($find->length > 0) {
                 $el = $this->createFragment($doc->saveHtml($find->item(0)), false);
                 $k = key($el->_domId);
                 $is = $el->is($query);
                 if ($is->get() === true) {
                     $el->_path = $find->item(0)->getNodepath();
                     if ($find->item(0)->nextSibling) {
                         $el->_next_path = $find->item(0)->nextSibling->getNodepath();
                     } else {
                         $el->_next_path = NULL;
                     }
                     if ($find->item(0)->previousSibling) {
                         $el->_prev_path = $find->item(0)->previousSibling->getNodepath();
                     } else {
                         $el->_prev_path = NULL;
                     }
                     if ($find->item(0)->parentNode) {
                         $el->_parent_path = $find->item(0)->parentNode->getNodepath();
                     } else {
                         $el->_parent_path = NULL;
                     }
                     $el->selector = $document->match_selector($query);
                     $this->node = $el;
                     $this->node->setDomID($el->_parentElement . $el->_token);
                     return $this;
                     break;
                 }
             }
         }
         $this->node = new jqueryphp_abstracts_prevObject($this->node->_selector . ':parent(*)');
         return $this;
     } else {
         return $this->node = new jqueryphp_abstracts_prevObject($this->node->_selector . ':parent(*)');
     }
 }
Esempio n. 14
0
/**
 * Calculates the read time of a post when created or updated.
 *
 * @param  int $post_id Post id to calculate read time for.
 * @param  object $post Post object.
 * @param  boolean $update Is this an update or new.
 *
 * @return void
 */
function hh_calculate_and_update_post_read_time($post_id, $post, $update)
{
    global $pagenow;
    // Only those allowed.
    if (!current_user_can('edit_posts', $post_id)) {
        return;
    }
    // Resolve the issues with DOM reader with Quick Draft.
    // https://github.com/Kenshino/HelpHub/commit/faba446e699893eb95c1eef4cf9f0e38c03a4680.
    if ('post-new.php' === $pagenow) {
        return;
    }
    // Store post content for raw usage.
    $post_content = $post->post_content;
    if (empty($post_content)) {
        return;
    }
    // No post revisions.
    if (wp_is_post_revision($post_id)) {
        return;
    }
    // Get post types that need to have read time applied.
    $calculate_for_posts = apply_filters('read_time_types', array('post'));
    // If the post type is not found then return.
    if (!in_array($post->post_type, $calculate_for_posts)) {
        return;
    }
    // Average words per minute integer.
    $average_word_per_minute = apply_filters('read_time_average', 175);
    // Simple adjustment for pre tags.
    libxml_use_internal_errors(true);
    $data = new DOMDocument();
    $data->loadHTML($post_content);
    $xpath = new DomXpath($data);
    libxml_use_internal_errors(false);
    $pre_tags = array();
    $word_count_offset = 0;
    // Offset weight. Offset word count will be timed by this number.
    $offset_weight = apply_filters('read_time_offset_weight', 1);
    foreach ($xpath->query('//pre') as $node) {
        $pre_tags[] = $node->nodeValue;
        $word_count_offset = str_word_count($node->nodeValue) + $word_count_offset * $offset_weight;
    }
    // Word count.
    $word_count = str_word_count(wp_strip_all_tags($post_content)) + $word_count_offset;
    // Calculate basic read time.
    $readtime = round($word_count / ($average_word_per_minute / 60));
    // Grad and count all images.
    preg_match_all('/(img|src)\\=(\\"|\')[^\\"\'\\>]+/i', $post->post_content, $media);
    // Adjust read time given the number of images.
    if ($image_count = count($media[0])) {
        $readtime = $image_count * 12 - $image_count + $readtime;
    }
    // Update the post read time.
    update_post_meta($post_id, '_read_time', $readtime);
}
 public static function user_id_from_name($user)
 {
     $url = Thingiverse::BASE_URL . "/{$user}";
     $dom = new DomDocument("1.0");
     @$dom->loadHTMLFile($url);
     // use @ to suppress parser warnings
     $xp = new DomXpath($dom);
     $rss_url = $xp->query("//link[@rel=\"alternate\"]")->item(0)->getAttribute("href");
     $parts = explode(":", $rss_url);
     return $parts[2];
 }
Esempio n. 16
0
 private function templateSubsection($doc, $selector)
 {
     $tokenizer = new \Transphporm\Parser\Tokenizer($selector);
     $xpathStr = $this->xPath->getXpath($tokenizer->getTokens());
     $xpath = new \DomXpath($doc);
     $nodes = $xpath->query($xpathStr);
     $result = [];
     foreach ($nodes as $node) {
         $result[] = $node;
     }
     return $result;
 }
Esempio n. 17
0
 function xselect($expression)
 {
     $xp = new DomXpath($this->getDocument());
     $result = $xp->query($expression);
     if ($result->length == 0) {
         return null;
     }
     if ($result->length > 1) {
         throw new Exception("More than one result returned.");
     }
     return $result->item(0)->nodeValue;
 }
 /**
  * Deselect otion of a select given a select name/id and the option value to deselect.
  *
  * @param string $name select name/id
  * @param string $option_value option value to be deselected
  * @return idDoctrineTestBrowser
  */
 public function deSelectOption($name, $option_value)
 {
     $position = 0;
     $dom = $this->getResponseDom();
     if (!$dom) {
         throw new LogicException('Cannot select because there is no current page in the browser.');
     }
     $xpath = new DomXpath($dom);
     if ($element = $xpath->query(sprintf('//select[(.="%s" or @id="%s" or @name="%s")]/option[@value="%s"]', $name, $name, $name, $option_value))->item($position)) {
         $element->removeAttribute('selected');
     } else {
         throw new InvalidArgumentException(sprintf('Cannot find the select (%s) option value "%s" .', $name, $option_value));
     }
     return $this;
 }
Esempio n. 19
0
 function translateLabelsOnPage($xpath_query)
 {
     global $nameTranslations;
     foreach ($this->translate_langs as $lang) {
         $doc = $this->fetchDocFromUrl($this->translatePageUrlTo($lang));
         $xpath = new DomXpath($doc);
         $graph = new SimpleGraph();
         foreach ($xpath->query($xpath_query) as $a) {
             $nodeId = $this->getNodeIdFromUrl($a->getAttribute('href'));
             $uri = INST . 'institutions/' . $nodeId;
             $this->graph->add_literal_triple($uri, RDFS_LABEL, $a->textContent, $lang);
             $nameTranslations[$uri][$lang] = $a->textContent;
         }
         echo trim($graph->to_ntriples());
     }
 }
Esempio n. 20
0
 public function CreateDomDocument()
 {
     $dom = new Domdocument();
     @$dom->loadHTML($this->htmlStack);
     $xpath = new DomXpath($dom);
     $elements = $xpath->query('//a[@class="' . $this->class . '"]');
     foreach ($elements as $element) {
         $element = $element->getAttribute('href');
         // if a regex option has been supplied run it on the results
         if ($this->regex) {
             $element = preg_replace($this->regex, '', $element);
         }
         // remove the 6X 35x6(bulk beers) results with this regex
         if (!preg_match('/\\dX(\\d+)?/', $element)) {
             $this->elementsArray[] = ['title' => $element];
         }
     }
     echo json_encode(['beers' => $this->elementsArray]);
 }
Esempio n. 21
0
 public function run()
 {
     if (empty($this->node->_localName)) {
         return $this;
     }
     $parent = $this->node->parent()->get();
     if (is_a($parent, jqmel)) {
         $path = $parent->_path;
         $str = $parent->_innerHtml;
         $document = jqm_use($this->node->_parentElement);
         $dom = $document->_DOM;
         if ($dom->doctype) {
             $dom->removeChild($dom->doctype);
         }
         $xpath = new DomXpath($dom);
         $find = $xpath->query($path);
         if ($find->length > 0) {
             $tp = new domdocument();
             $tp->loadHtml($str);
             if ($tp->doctype) {
                 $tp->removeChild($tp->doctype);
             }
             $frag = $dom->importNode($tp->firstChild->firstChild, true);
             $find->item(0)->parentNode->replaceChild($frag, $find->item(0));
             $find = $this->node->getPathByID();
             if ($find) {
                 $this->node->_path = $find;
             } else {
                 $pts = explode('/', $this->node->_path);
                 $ep = array_pop($pts);
                 $pts = explode('/', $frag->getNodepath());
                 array_pop($pts);
                 $pts[] = $ep;
                 $this->node->_path = implode('/', $pts);
             }
         }
     }
 }
Esempio n. 22
0
 function translateRolesTo($rolePositions, $lang)
 {
     global $RolesGraph;
     $xpath = false;
     if ($this->rolesNeedTranslating($rolePositions, $lang)) {
         $doc = $this->fetchDocFromUrl($this->translatePageUrlTo($lang));
         $xpath = new DomXpath($doc);
         $translatedRoles = array();
         foreach ($xpath->query(WHOISWHO_ROLE_XPATH) as $node) {
             $roleLabels = $this->getRoleLabelsFromNode($node, $doc);
             foreach ($roleLabels as $label) {
                 $translatedRoles[] = $label;
             }
         }
         if (count($translatedRoles) !== count($rolePositions)) {
             $this->log_message("different no of roles for {$lang} translation");
         }
         foreach ($rolePositions as $no => $roleUri) {
             if (!$RolesGraph->roleHasLabelWithLang($roleUri, $lang, $RolesGraph) and isset($translatedRoles[$no])) {
                 $RolesGraph->add_literal_triple($roleUri, RDFS_LABEL, $translatedRoles[$no], $lang);
             }
         }
     }
 }
Esempio n. 23
0
 private function templateSubsection($css, $doc, $element)
 {
     $xpathStr = (new \Transphporm\Parser\CssToXpath($css, new \Transphporm\Parser\Value($this)))->getXpath();
     $xpath = new \DomXpath($doc);
     $nodes = $xpath->query($xpathStr);
     $result = [];
     foreach ($nodes as $node) {
         $result[] = $element->ownerDocument->importNode($node, true);
     }
     return $result;
 }
  /**
   * Looks for a link or a button in the response and submits the related request
   *
   * @param string The link/button value/href/alt
   * @param array request parameters (associative array)
   *
   * @return sfWebBrowser The current browser object
   */ 
  public function click($name, $arguments = array())
  {
    if (!$dom = $this->getResponseDom())
    {
      throw new Exception('Cannot click because there is no current page in the browser');
    }

    $xpath = new DomXpath($dom);

    // text link, the name being in an attribute
    if ($link = $xpath->query(sprintf('//a[@*="%s"]', $name))->item(0))
    {
      return $this->get($link->getAttribute('href'));
    }

    // text link, the name being the text value
    if ($links = $xpath->query('//a[@href]'))
    {
      foreach($links as $link)
      {
        if(preg_replace(array('/\s{2,}/', '/\\r\\n|\\n|\\r/'), array(' ', ''), $link->nodeValue) == $name)
        {
          return $this->get($link->getAttribute('href'));  
        }
      }
    }
    
    // image link, the name being the alt attribute value
    if ($link = $xpath->query(sprintf('//a/img[@alt="%s"]/ancestor::a', $name))->item(0))
    {
      return $this->get($link->getAttribute('href'));
    }

    // form, the name being the button or input value
    if (!$form = $xpath->query(sprintf('//input[((@type="submit" or @type="button") and @value="%s") or (@type="image" and @alt="%s")]/ancestor::form', $name, $name))->item(0))
    {
      throw new Exception(sprintf('Cannot find the "%s" link or button.', $name));
    }

    // form attributes
    $url = $form->getAttribute('action');
    $method = $form->getAttribute('method') ? strtolower($form->getAttribute('method')) : 'get';

    // merge form default values and arguments
    $defaults = array();
    foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $form) as $element)
    {
      $elementName = $element->getAttribute('name');
      $nodeName    = $element->nodeName;
      $value       = null;
      if ($nodeName == 'input' && ($element->getAttribute('type') == 'checkbox' || $element->getAttribute('type') == 'radio'))
      {
        if ($element->getAttribute('checked'))
        {
          $value = $element->getAttribute('value');
        }
      }
      else if (
        $nodeName == 'input'
        &&
        (($element->getAttribute('type') != 'submit' && $element->getAttribute('type') != 'button') || $element->getAttribute('value') == $name)
        &&
        ($element->getAttribute('type') != 'image' || $element->getAttribute('alt') == $name)
      )
      {
        $value = $element->getAttribute('value');
      }
      else if ($nodeName == 'textarea')
      {
        $value = '';
        foreach ($element->childNodes as $el)
        {
          $value .= $dom->saveXML($el);
        }
      }
      else if ($nodeName == 'select')
      {
        if ($multiple = $element->hasAttribute('multiple'))
        {
          $elementName = str_replace('[]', '', $elementName);
          $value = array();
        }
        else
        {
          $value = null;
        }

        $found = false;
        foreach ($xpath->query('descendant::option', $element) as $option)
        {
          if ($option->getAttribute('selected'))
          {
            $found = true;
            if ($multiple)
            {
              $value[] = $option->getAttribute('value');
            }
            else
            {
              $value = $option->getAttribute('value');
            }
          }
        }

        // if no option is selected and if it is a simple select box, take the first option as the value
        if (!$found && !$multiple)
        {
          $value = $xpath->query('descendant::option', $element)->item(0)->getAttribute('value');
        }
      }

      if (null !== $value)
      {
        $this->parseArgumentAsArray($elementName, $value, $defaults);
      }
    }

    // create request parameters
    $arguments = sfToolkit::arrayDeepMerge($defaults, $this->fields, $arguments);
    if ('post' == $method)
    {
      return $this->post($url, $arguments);
    }
    else
    {
      return $this->get($url, $arguments);
    }
  }
Esempio n. 25
0
 public static function BUILD(Student $student, $file = null)
 {
     $zip = new \ZipArchive();
     $dom = new \bloc\DOM\Document('data/student-site');
     $xpath = new \DomXpath($dom);
     $parse = function ($context) use(&$parse, $xpath, $dom) {
         foreach ($xpath->query('./file', $context) as $file) {
             if ($file->hasAttribute('pattern')) {
                 $pattern = strtolower(str_replace(' ', '-', sprintf($file->getAttribute('pattern'), $context->parentNode->getAttribute('title'))));
                 $file->setAttribute('name', $pattern);
             }
         }
         foreach ($xpath->query('./directory', $context) as $directory) {
             $name = $directory->getAttribute('name');
             if ($directory->hasAttribute('clone')) {
                 $replacements = $xpath->query($directory->getAttribute('clone'));
                 foreach ($replacements as $replacement) {
                     $cloned = $directory->appendChild($replacement->cloneNode(!$replacement->hasAttribute('clone')));
                     foreach ($xpath->query('./file[@clone="ignore"]', $cloned) as $ignore) {
                         $cloned->removeChild($ignore);
                     }
                 }
                 $parent = $directory->parentNode;
                 $directory = $parent->removeChild($directory);
                 $pattern = $directory->getAttribute('pattern');
                 foreach (explode('|', $name) as $idx) {
                     $newnode = $parent->appendChild($directory->cloneNode(true));
                     $newnode->setAttribute('name', strtolower($idx));
                     $newnode->setAttribute('title', sprintf($pattern, $idx));
                     $parse($newnode);
                 }
             } else {
                 if ($directory->childNodes->length > 0) {
                     $parse($directory);
                 }
             }
         }
     };
     call_user_func($parse, $dom->documentElement, '', './');
     $filepath = 'views/student/site';
     $cache = [];
     $zip->open($file, \ZIPARCHIVE::OVERWRITE);
     $zipper = function ($context, $path) use(&$zipper, $zip, $filepath, $student, &$cache) {
         $path .= '/';
         $title = $context->getAttribute('title') ?: '';
         foreach ($context->childNodes as $level) {
             $type = $level->nodeName;
             $name = $level->getAttribute('name');
             if ($type == 'file') {
                 $ext = substr($name, strpos($name, '.'));
                 $base = substr($name, 0, strpos($name, '.'));
                 if (!array_key_exists($name, $cache)) {
                     if (file_exists(PATH . $filepath . $path . $name)) {
                         $cache[$name] = $filepath . $path . $name;
                     } else {
                         if (file_exists(PATH . $filepath . $level->getAttribute('template'))) {
                             $cache[$name] = $filepath . $level->getAttribute('template');
                         }
                     }
                 }
                 if ($ext == '.html' || $ext == '.xml') {
                     $view = new \bloc\view($cache[$name]);
                     if ($context->hasAttribute('template')) {
                         $view->template = $filepath . $context->getAttribute('template');
                     }
                     $text = (string) $view->render(['id' => $name, 'empty' => null, 'student' => $student, 'title' => $title, 'datapath' => substr($path, 1, -6), 'embeds' => strtolower(str_replace(' ', '-', $title)), 'domain' => DOMAIN]);
                     if ($ext != '.xml') {
                         $text = substr($text, strlen('<?xml version="1.0"?>') + 1);
                     }
                     $zip->addFromString(substr($path . $name, 1), $text);
                 } else {
                     $zip->addFile(PATH . $cache[$name], substr($path . $name, 1));
                 }
             } else {
                 $zip->addEmptyDir($path . '/');
                 $zipper($level, $path . $name);
             }
         }
     };
     $zipper($dom->documentElement, '', '');
     $zip->close();
     return $file;
 }
Esempio n. 26
0
 public function gibbactuPostProcess()
 {
     $Actu = new Actu($this->db);
     $NotProcessedActus = $Actu->find(array('processed = ?', 0));
     //print_r($NotProcessedActus);
     libxml_use_internal_errors(true);
     ob_start();
     foreach ($NotProcessedActus as $NPA) {
         $processActu = array();
         //echo $NPA->id."<br>";
         //Raw HTML to DOM/XPATH
         $dom = new DOMDocument();
         $searchPage = mb_convert_encoding($NPA->content_raw, 'HTML-ENTITIES', "UTF-8");
         @$dom->loadHTML($searchPage);
         @($xpath = new DomXpath($dom));
         //userID :
         $processActu['hfr_user_id'] = $xpath->query('//td[@class = "messCase2"]//a[contains(@href, "profil-")]')->item(0)->getAttribute('href');
         $processActu['hfr_user_id'] = preg_replace("/[^0-9]+/", "", $processActu['hfr_user_id']);
         //echo $processActu['hfr_user_id']."<br>";
         //pseudo :
         $processActu['username'] = $xpath->query('//td[@class = "messCase1"]//b[@class = "s2"]')->item(0)->textContent;
         $processActu['username'] = preg_replace('/[^(\\x20-\\x7F)]*/', '', htmlentities($processActu['username']));
         //echo $processActu['username']."<br>";
         //if ($processActu['username'] <> 'roswellent​ongues') {
         //continue;
         //}
         //Check si le smiley est bien directement dans le message
         $checkSmileyGAParent = $xpath->query('//img[contains(@title, "[:gibbactu]")]//..//..//..//div[starts-with(@id, "para")]');
         if (!$checkSmileyGAParent->length) {
             //pas de smiley, on skip
             //echo "smiley pas à la racine<br/><br/><br/>";
             $NPA->active = 0;
             $NPA->save();
             continue;
         }
         //post ID
         $processActu["hfr_post_id"] = $xpath->query('//a[contains(@href, "#t")]')->item(0)->getAttribute('href');
         $processActu['hfr_post_id'] = str_replace("#t", "", $processActu["hfr_post_id"]);
         //echo $processActu['hfr_post_id'];
         //if ($processActu['hfr_post_id'] <> '10764130') {
         //	continue;
         //}
         //date posted
         $divDate = $xpath->query('//div[@class="toolbar"]/div[@class="left"]');
         $oldate = preg_replace("/[^0-9:-]+/", "", $divDate->item(0)->nodeValue);
         $processActu['date_posted'] = date('Y-m-d H:i', strtotime($oldate));
         //echo $crDatePosted.' ';
         //echo $processActu['username']."\n";
         //le contenu au même niveau que le smiley
         $actuNodesZero = $xpath->query('//img[contains(@title, "[:gibbactu]")]/../node()');
         $processHTMLstep0 = '';
         $startInsert = false;
         foreach ($actuNodesZero as $actuNodeZ) {
             if ($actuNodeZ->nodeType == XML_ELEMENT_NODE && $actuNodeZ->getAttribute('title') == '[:gibbactu]') {
                 $startInsert = true;
                 continue;
             }
             if ($actuNodeZ->nodeType == XML_ELEMENT_NODE && !strcasecmp(trim($actuNodeZ->nodeValue), 'gibbactu')) {
                 continue;
             }
             if (!$startInsert) {
                 continue;
             }
             //echo "len    =".strlen(trim($actuNodeZ->nodeValue))."\n";
             //echo "htmlen =".strlen(trim($processHTMLstep0))."\n";
             //echo "html   =".($dom->saveHTML($actuNodeZ))."\n";
             if ($actuNodeZ->nodeType == XML_ELEMENT_NODE) {
                 //echo "tag    =".$actuNodeZ->tagName."\n";
             } else {
                 //echo "tag    =pasnoded\n";
                 if (!strcasecmp(trim(htmlentities($dom->saveHTML($actuNodeZ))), 'gibbactu')) {
                     //echo "== gibbactu\n";
                     continue;
                 }
             }
             if (trim(htmlentities($dom->saveHTML($actuNodeZ))) == '&nbsp;') {
                 //echo ">> on skip\n";
                 continue;
             }
             if (!strlen(trim($processHTMLstep0)) && $actuNodeZ->nodeType == XML_ELEMENT_NODE && $actuNodeZ->tagName == 'br') {
                 //echo ">> on skip\n";
                 continue;
             }
             //echo "==\nNew HTML:\n".htmlentities($processHTMLstep0)."\n============\n";
             //var_dump($actuNodeZ);
             $processHTMLstep0 .= htmlentities($dom->saveHTML($actuNodeZ));
             //echo htmlentities($dom->saveHTML($actuNodeZ))."\n".$actuNodeZ->nodeValue."\n\n";
         }
         //On va chercher tout le contenu APRES le smiley gibbactu
         $actuNodes = $xpath->query('//img[contains(@title, "[:gibbactu]")]/../following-sibling::*');
         //var_dump($actuNodes);
         $processHTMLstep1 = $processHTMLstep0;
         foreach ($actuNodes as $actuNode) {
             //echo $dom->saveHTML($actuNode)."\n";
             //echo "===\n".$actuNode->tagName." = ".$actuNode->getAttribute('class')."\n\n";
             //var_dump($actuNode);
             if ($actuNode->getAttribute('class') == 'edited' || $actuNode->getAttribute('class') == 'signature' || $actuNode->getAttribute('style') == 'clear: both;') {
                 continue;
             }
             if ($actuNode->nodeValue || $actuNode->childNodes) {
                 $processHTMLstep1 .= htmlentities($dom->saveHTML($actuNode));
             } else {
                 var_dump($actuNode->childNodes);
                 echo "noNodeValue=" . $dom->saveHTML($actuNode) . "\n\n";
             }
         }
         $processHTMLfinal = $processHTMLstep1;
         $processHTMLfinal = str_replace("\n", '', $processHTMLfinal);
         $processHTMLfinal = str_replace("\r", '', $processHTMLfinal);
         $processHTMLfinal = htmlentities(str_replace("<p><br></p>", '', html_entity_decode($processHTMLfinal)));
         $processHTMLfinal = htmlentities(str_replace('href="/hfr/', 'target="_blank" href="http://forum.hardware.fr/hfr/', html_entity_decode($processHTMLfinal)));
         $processHTMLfinal = htmlentities(preg_replace('/style="[a-zA-Z0-9:;\\.\\s\\(\\)\\-\\,]*"/i', '', html_entity_decode($processHTMLfinal)));
         $processHTMLfinal = htmlentities(preg_replace('/onload="[^\\"]+"/i', 'onload="return;"', html_entity_decode($processHTMLfinal)));
         $processHTMLfinal = htmlentities(preg_replace('/title="[^\\"]+"/i', '', html_entity_decode($processHTMLfinal)));
         if (!$processHTMLfinal) {
             echo "aucun contenu<br/><br/><br/>";
             $NPA->active = 0;
             $NPA->save();
             continue;
         }
         $NPA->hfr_cat_id = 5;
         $NPA->hfr_subcat_id = 249;
         $NPA->hfr_topic_id = 177180;
         //$NPA->hfr_page_id
         $NPA->hfr_post_id = $processActu['hfr_post_id'];
         $NPA->hfr_user_id = $processActu['hfr_user_id'];
         $NPA->username = $processActu['username'];
         $NPA->date_modified = date('Y-m-d H:i:s');
         $NPA->date_posted = $processActu['date_posted'];
         $NPA->content = $processHTMLfinal;
         $NPA->active = 1;
         $NPA->processed = 1;
         $NPA->save();
         echo "\n\n\n\n\n";
     }
     $myStr = ob_get_contents();
     ob_end_clean();
     libxml_use_internal_errors(false);
     echo $myStr;
     //$this->f3->set('report',$myStr);
     //echo Template::instance()->render('cron.htm');
 }
 public static function from_html_made_dom($dom)
 {
     $thing = new ThingiverseThing();
     $d = new DOMDocument('1.0');
     $b = $d->importNode($dom->cloneNode(true), true);
     $d->appendChild($b);
     $xp = new DomXpath($d);
     $tmpelem = $xp->query("//div[@class=\"thing_info\"]/a")->item(0);
     $thing->title = $tmpelem->childNodes->item(0)->wholeText;
     $tmpelem = $xp->query("//div[@class=\"thing_info\"]/a")->item(1);
     $thing->creator = $tmpelem->childNodes->item(0)->wholeText;
     $thing->creator_url = Thingiverse::BASE_URL . "/{$thing->creator}";
     // TODO: Get the time somehow :(
     //$thing->created_at = "Unknown";
     $tmpelem = $xp->query("//a")->item(2);
     $thing->url = Thingiverse::BASE_URL . $tmpelem->getAttribute("href");
     $thing->main_image = $tmpelem->childNodes->item(0)->getAttribute(src);
     return $thing;
 }
Esempio n. 28
0
 public function CLIassignment()
 {
     $doc = new \bloc\DOM\Document(self::SEMESTER);
     $xml = new \DomXpath($doc);
     $assignment = $xml->query('/course/assignments')->item(0)->appendChild($doc->createElement('assignment'));
     $weeks = $xml->query('/course/classes/outline');
     // title, points, id
     echo "\nEnter Assignment Title: ";
     $title = trim(fgets(STDIN));
     $assignment->setAttribute('title', $title);
     $id = preg_replace(['/\\s+/', '/[^a-z]/i'], ['-', ''], $title);
     $assignment->setAttribute('id', $id);
     echo "\nEnter number of points: ";
     $points = trim(fgets(STDIN));
     $assignment->setAttribute('points', (int) $points);
     echo "\nWeek number: ";
     $week = (int) trim(fgets(STDIN)) - 1;
     if ($week >= 0 && $week < 15) {
         $ref = $weeks->item($week)->appendChild($doc->createElement('assignment'));
         $ref->setAttribute('ref', $id);
     }
     echo "\nAssignment Details: ";
     $details = trim(fgets(STDIN));
     if (!empty($details)) {
         $assignment->setNodeValue($details);
     }
     $this->save($doc);
 }
<?php

$result = 0;
passthru('cd ' . __DIR__ . ' && phpdoc --force', $result);
if ($result !== 0) {
    die("phpdoc command not found.");
}
libxml_use_internal_errors(true);
$dom = new DomDocument();
$dom->loadHTMLFile("./docs/index.html");
$xpath = new DomXpath($dom);
// Remove "Global" namespace
$result = $xpath->query('//li/a[@href="namespaces/default.html"]');
if ($result->length > 0) {
    $node = $result->item(0);
    $node->parentNode->parentNode->removeChild($node->parentNode);
}
// Remove Packages
$result = $xpath->query('//div[@class="well"][2]');
if ($result->length > 0) {
    $node = $result->item(0);
    $node->parentNode->removeChild($node);
}
$dom->saveHTMLFile('./docs/index.html');
Esempio n. 30
0
 /**
  * Check Result for Errors
  *
  * @param DomDocument $dom
  * @throws Zend_Service_Exception
  */
 protected static function _checkErrors(DomDocument $dom)
 {
     if ($dom->documentElement->getAttribute('stat') == 'fail') {
         $xpath = new DomXpath($dom);
         $err = $xpath->query('//err')->item(0);
         throw new Zend_Service_Exception('Search failed due to error: ' . $err->getAttribute('msg') . ' (error #' . $err->getAttribute('code') . ')');
     }
 }