Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class simple_html_dom_node
- {
- public $nodetype = HDOM_TYPE_TEXT;
- public $tag = "text";
- public $attr = array( );
- public $children = array( );
- public $nodes = array( );
- public $parent = null;
- public $_ = array( );
- private $dom = null;
- public function __construct( $dom )
- {
- $this->dom = $dom;
- $dom->nodes[] = $this;
- }
- public function __destruct( )
- {
- $this->clear( );
- }
- public function __toString( )
- {
- return $this->outertext( );
- }
- public function clear( )
- {
- $this->dom = null;
- $this->nodes = null;
- $this->parent = null;
- $this->children = null;
- }
- public function dump( $show_attr = true )
- {
- dump_html_tree( $this, $show_attr );
- }
- public function parent( )
- {
- return $this->parent;
- }
- public function children( $idx = -1 )
- {
- if ( $idx === 0 - 1 )
- {
- return $this->children;
- }
- if ( isset( $this->children[$idx] ) )
- {
- return $this->children[$idx];
- }
- return null;
- }
- public function first_child( )
- {
- if ( 0 < count( $this->children ) )
- {
- return $this->children[0];
- }
- return null;
- }
- public function last_child( )
- {
- if ( 0 < ( $count = count( $this->children ) ) )
- {
- return $this->children[$count - 1];
- }
- return null;
- }
- public function next_sibling( )
- {
- if ( $this->parent === null )
- {
- return null;
- }
- $idx = 0;
- $count = count( $this->parent->children );
- while ( $idx < $count && $this !== $this->parent->children[$idx] )
- {
- ++$idx;
- }
- if ( $count <= ++$idx )
- {
- return null;
- }
- return $this->parent->children[$idx];
- }
- public function prev_sibling( )
- {
- if ( $this->parent === null )
- {
- return null;
- }
- $idx = 0;
- $count = count( $this->parent->children );
- while ( $idx < $count && $this !== $this->parent->children[$idx] )
- {
- ++$idx;
- }
- if ( --$idx < 0 )
- {
- return null;
- }
- return $this->parent->children[$idx];
- }
- public function innertext( )
- {
- if ( isset( $this->_[HDOM_INFO_INNER] ) )
- {
- return $this->_[HDOM_INFO_INNER];
- }
- if ( isset( $this->_[HDOM_INFO_TEXT] ) )
- {
- return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
- }
- $ret = "";
- foreach ( $this->nodes as $n )
- {
- $ret .= $n->outertext( );
- }
- return $ret;
- }
- public function outertext( )
- {
- if ( $this->tag === "root" )
- {
- return $this->innertext( );
- }
- if ( $this->dom->callback !== null )
- {
- call_user_func_array( $this->dom->callback, array(
- $this
- ) );
- }
- if ( isset( $this->_[HDOM_INFO_OUTER] ) )
- {
- return $this->_[HDOM_INFO_OUTER];
- }
- if ( isset( $this->_[HDOM_INFO_TEXT] ) )
- {
- return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
- }
- $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup( );
- if ( isset( $this->_[HDOM_INFO_INNER] ) )
- {
- $ret .= $this->_[HDOM_INFO_INNER];
- }
- else
- {
- foreach ( $this->nodes as $n )
- {
- $ret .= $n->outertext( );
- }
- }
- if ( isset( $this->_[HDOM_INFO_END] ) && $this->_[HDOM_INFO_END] != 0 )
- {
- $ret .= "</".$this->tag.">";
- }
- return $ret;
- }
- public function text( )
- {
- if ( isset( $this->_[HDOM_INFO_INNER] ) )
- {
- return $this->_[HDOM_INFO_INNER];
- }
- switch ( $this->nodetype )
- {
- case HDOM_TYPE_TEXT :
- return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
- case HDOM_TYPE_COMMENT :
- return "";
- case HDOM_TYPE_UNKNOWN :
- return "";
- }
- if ( strcasecmp( $this->tag, "script" ) === 0 )
- {
- return "";
- }
- if ( strcasecmp( $this->tag, "style" ) === 0 )
- {
- return "";
- }
- $ret = "";
- foreach ( $this->nodes as $n )
- {
- $ret .= $n->text( );
- }
- return $ret;
- }
- public function xmltext( )
- {
- $ret = $this->innertext( );
- $ret = str_ireplace( "<![CDATA[", "", $ret );
- $ret = str_replace( "]]>", "", $ret );
- return $ret;
- }
- public function makeup( )
- {
- if ( isset( $this->_[HDOM_INFO_TEXT] ) )
- {
- return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
- }
- $ret = "<".$this->tag;
- $i = 0 - 1;
- foreach ( $this->attr as $key => $val )
- {
- ++$i;
- if ( $val === null || $val === false )
- {
- continue;
- }
- $ret .= $this->_[HDOM_INFO_SPACE][$i][0];
- if ( $val === true )
- {
- $ret .= $key;
- }
- else
- {
- switch ( $this->_[HDOM_INFO_QUOTE][$i] )
- {
- case HDOM_QUOTE_DOUBLE :
- $quote = "\"";
- break;
- switch ( $this->_[HDOM_INFO_QUOTE][$i] )
- {
- case HDOM_QUOTE_SINGLE :
- $quote = "'";
- break;
- default :
- default :
- $quote = "";
- }
- }
- $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1]."=".$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;
- }
- }
- $ret = $this->dom->restore_noise( $ret );
- return $ret.$this->_[HDOM_INFO_ENDSPACE].">";
- }
- public function find( $selector, $idx = null )
- {
- $selectors = $this->parse_selector( $selector );
- if ( ( $count = count( $selectors ) ) === 0 )
- {
- return array( );
- }
- $found_keys = array( );
- $c = 0;
- while ( $c < $count )
- {
- if ( ( $levle = count( $selectors[0] ) ) === 0 )
- {
- return array( );
- }
- if ( !isset( $this->_[HDOM_INFO_BEGIN] ) )
- {
- return array( );
- }
- $head = array( 1 );
- $l = 0;
- while ( $l < $levle )
- {
- $ret = array( );
- foreach ( $head as $k => $v )
- {
- $n = $k === 0 - 1 ? $this->dom->root : $this->dom->nodes[$k];
- $n->seek( $selectors[$c][$l], $ret );
- }
- $head = $ret;
- ++$l;
- }
- foreach ( $head as $k => $v )
- {
- if ( !isset( $found_keys[$k] ) )
- {
- $found_keys[$k] = 1;
- }
- }
- ++$c;
- }
- ksort( $found_keys );
- $found = array( );
- foreach ( $found_keys as $k => $v )
- {
- $found[] = $this->dom->nodes[$k];
- }
- if ( is_null( $idx ) )
- {
- return $found;
- }
- if ( $idx < 0 )
- {
- $idx = count( $found ) + $idx;
- }
- return isset( $found[$idx] ) ? $found[$idx] : null;
- }
- protected function seek( $selector, &$ret )
- {
- list( $tag, $key, $val, $exp, $no_key ) = tag if ( $tag && $key && is_numeric( $key ) )
- {
- $count = 0;
- foreach ( $this->children as $c )
- {
- if ( !( $tag === "*" || $tag === $c->tag ) && !( ++$count == $key ) )
- {
- continue;
- }
- $ret[$c->_[HDOM_INFO_BEGIN]] = 1;
- return;
- }
- }
- else
- {
- $end = !empty( $this->_[HDOM_INFO_END] ) ? $this->_[HDOM_INFO_END] : 0;
- if ( $end == 0 )
- {
- $parent = $this->parent;
- while ( !isset( $parent->_[HDOM_INFO_END] ) && $parent !== null )
- {
- $end -= 1;
- $parent = $parent->parent;
- }
- $end += $parent->_[HDOM_INFO_END];
- }
- $i = $this->_[HDOM_INFO_BEGIN] + 1;
- while ( $i < $end )
- {
- $node = $this->dom->nodes[$i];
- $pass = true;
- if ( $tag === "*" && !$key )
- {
- if ( in_array( $node, $this->children, true ) )
- {
- $ret[$i] = 1;
- }
- continue;
- }
- if ( $tag && $tag != $node->tag && $tag !== "*" )
- {
- $pass = false;
- }
- if ( $pass && $key )
- {
- if ( $no_key )
- {
- if ( isset( $node->attr[$key] ) )
- {
- $pass = false;
- }
- }
- else if ( !isset( $node->attr[$key] ) )
- {
- $pass = false;
- }
- }
- if ( $pass && $key && $val && $val !== "*" )
- {
- $check = $this->match( $exp, $val, $node->attr[$key] );
- if ( !$check && strcasecmp( $key, "class" ) === 0 )
- {
- foreach ( explode( " ", $node->attr[$key] ) as $k )
- {
- $check = $this->match( $exp, $val, $k );
- if ( !$check )
- {
- continue;
- }
- break;
- break;
- }
- }
- if ( !$check )
- {
- $pass = false;
- }
- }
- if ( $pass )
- {
- $ret[$i] = 1;
- }
- unset( $node );
- ++$i;
- }
- }
- }
- protected function match( $exp, $pattern, $value )
- {
- switch ( $exp )
- {
- case "=" :
- return $value === $pattern;
- case "!=" :
- return $value !== $pattern;
- case "^=" :
- return preg_match( "/^".preg_quote( $pattern, "/" )."/", $value );
- case "\$=" :
- return preg_match( "/".preg_quote( $pattern, "/" )."\$/", $value );
- case "*=" :
- if ( $pattern[0] == "/" )
- {
- return preg_match( $pattern, $value );
- }
- return preg_match( "/".$pattern."/i", $value );
- }
- return false;
- }
- protected function parse_selector( $selector_string )
- {
- $pattern = "/([\\w-:\\*]*)(?:\\#([\\w-]+)|\\.([\\w-]+))?(?:\\[@?(!?[\\w-]+)(?:([!*^\$]?=)[\"']?(.*?)[\"']?)?\\])?([\\/, ]+)/is";
- preg_match_all( $pattern, trim( $selector_string )." ", $matches, PREG_SET_ORDER );
- $selectors = array( );
- $result = array( );
- foreach ( $matches as $m )
- {
- $m[0] = trim( $m[0] );
- if ( $m[0] === "" || $m[0] === "/" || $m[0] === "//" )
- {
- continue;
- }
- if ( $m[1] === "tbody" )
- {
- continue;
- }
- list( $tag, $key, $val, $exp, $no_key ) = tag if ( !empty( $m[2] ) )
- {
- $key = "id";
- $val = $m[2];
- }
- if ( !empty( $m[3] ) )
- {
- $key = "class";
- $val = $m[3];
- }
- if ( !empty( $m[4] ) )
- {
- $key = $m[4];
- }
- if ( !empty( $m[5] ) )
- {
- $exp = $m[5];
- }
- if ( !empty( $m[6] ) )
- {
- $val = $m[6];
- }
- if ( $this->dom->lowercase )
- {
- $tag = strtolower( $tag );
- $key = strtolower( $key );
- }
- if ( isset( $key[0] ) && $key[0] === "!" )
- {
- $key = substr( $key, 1 );
- $no_key = true;
- }
- $result[] = array(
- $tag,
- $key,
- $val,
- $exp,
- $no_key
- );
- if ( trim( $m[7] ) === "," )
- {
- $selectors[] = $result;
- $result = array( );
- }
- }
- if ( 0 < count( $result ) )
- {
- $selectors[] = $result;
- }
- return $selectors;
- }
- public function __get( $name )
- {
- if ( isset( $this->attr[$name] ) )
- {
- return $this->attr[$name];
- }
- switch ( $name )
- {
- case "outertext" :
- return $this->outertext( );
- case "innertext" :
- return $this->innertext( );
- case "plaintext" :
- return $this->text( );
- case "xmltext" :
- }
- return $this->xmltext( );
- return array_key_exists( $name, $this->attr );
- }
- public function __set( $name, $value )
- {
- switch ( $name )
- {
- case "outertext" :
- return $this->_[HDOM_INFO_OUTER] = $value;
- case "innertext" :
- if ( isset( $this->_[HDOM_INFO_TEXT] ) )
- {
- return $this->_[HDOM_INFO_TEXT] = $value;
- }
- return $this->_[HDOM_INFO_INNER] = $value;
- }
- if ( !isset( $this->attr[$name] ) )
- {
- $this->_[HDOM_INFO_SPACE][] = array( " ", "", "" );
- $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
- }
- $this->attr[$name] = $value;
- }
- public function __isset( $name )
- {
- switch ( $name )
- {
- case "outertext" :
- return true;
- case "innertext" :
- return true;
- case "plaintext" :
- return true;
- }
- return array_key_exists( $name, $this->attr ) ? true : isset( $this->attr[$name] );
- }
- public function __unset( $name )
- {
- if ( isset( $this->attr[$name] ) )
- {
- unset( $this->attr[$name] );
- }
- }
- public function getAllAttributes( )
- {
- return $this->attr;
- }
- public function getAttribute( $name )
- {
- return $this->__get( $name );
- }
- public function setAttribute( $name, $value )
- {
- $this->__set( $name, $value );
- }
- public function hasAttribute( $name )
- {
- return $this->__isset( $name );
- }
- public function removeAttribute( $name )
- {
- $this->__set( $name, null );
- }
- public function getElementById( $id )
- {
- return $this->find( "#{$id}", 0 );
- }
- public function getElementsById( $id, $idx = null )
- {
- return $this->find( "#{$id}", $idx );
- }
- public function getElementByTagName( $name )
- {
- return $this->find( $name, 0 );
- }
- public function getElementsByTagName( $name, $idx = null )
- {
- return $this->find( $name, $idx );
- }
- public function parentNode( )
- {
- return $this->parent( );
- }
- public function childNodes( $idx = -1 )
- {
- return $this->children( $idx );
- }
- public function firstChild( )
- {
- return $this->first_child( );
- }
- public function lastChild( )
- {
- return $this->last_child( );
- }
- public function nextSibling( )
- {
- return $this->next_sibling( );
- }
- public function previousSibling( )
- {
- return $this->prev_sibling( );
- }
- }
- class simple_html_dom
- {
- public $root = null;
- public $nodes = array( );
- public $callback = null;
- public $lowercase = false;
- protected $pos = NULL;
- protected $doc = NULL;
- protected $char = NULL;
- protected $size = NULL;
- protected $cursor = NULL;
- protected $parent = NULL;
- protected $noise = array( );
- protected $token_blank = " \t\r\n";
- protected $token_equal = " =/>";
- protected $token_slash = " />\r\n\t";
- protected $token_attr = " >";
- protected $self_closing_tags = array
- (
- "img" => 1,
- "br" => 1,
- "input" => 1,
- "meta" => 1,
- "link" => 1,
- "hr" => 1,
- "base" => 1,
- "embed" => 1,
- "spacer" => 1
- );
- protected $block_tags = array
- (
- "root" => 1,
- "body" => 1,
- "form" => 1,
- "div" => 1,
- "span" => 1,
- "table" => 1,
- "object" => 1
- );
- protected $optional_closing_tags = array
- (
- "tr" => array
- (
- "tr" => 1,
- "td" => 1,
- "th" => 1
- ),
- "th" => array
- (
- "th" => 1
- ),
- "td" => array
- (
- "td" => 1
- ),
- "li" => array
- (
- "li" => 1
- ),
- "dt" => array
- (
- "dt" => 1,
- "dd" => 1
- ),
- "dd" => array
- (
- "dd" => 1,
- "dt" => 1
- ),
- "dl" => array
- (
- "dd" => 1,
- "dt" => 1
- ),
- "p" => array
- (
- "p" => 1
- ),
- "nobr" => array
- (
- "nobr" => 1
- )
- );
- public function __construct( $str = null )
- {
- if ( $str )
- {
- if ( preg_match( "/^http:\\/\\//i", $str ) || is_file( $str ) )
- {
- $this->load_file( $str );
- }
- else
- {
- $this->load( $str );
- }
- }
- }
- public function __destruct( )
- {
- $this->clear( );
- }
- public function load( $str, $lowercase = true )
- {
- $this->prepare( $str, $lowercase );
- $this->remove_noise( "'<!--(.*?)-->'is" );
- $this->remove_noise( "'<!\\[CDATA\\[(.*?)\\]\\]>'is", true );
- $this->remove_noise( "'<\\s*style[^>]*[^/]>(.*?)<\\s*/\\s*style\\s*>'is" );
- $this->remove_noise( "'<\\s*style\\s*>(.*?)<\\s*/\\s*style\\s*>'is" );
- $this->remove_noise( "'<\\s*script[^>]*[^/]>(.*?)<\\s*/\\s*script\\s*>'is" );
- $this->remove_noise( "'<\\s*script\\s*>(.*?)<\\s*/\\s*script\\s*>'is" );
- $this->remove_noise( "'<\\s*(?:code)[^>]*>(.*?)<\\s*/\\s*(?:code)\\s*>'is" );
- $this->remove_noise( "'(<\\?)(.*?)(\\?>)'s", true );
- $this->remove_noise( "'(\\{\\w)(.*?)(\\})'s", true );
- while ( $this->parse( ) )
- {
- }
- $this->root->_[HDOM_INFO_END] = $this->cursor;
- }
- public function load_file( )
- {
- $args = func_get_args( );
- $this->load( call_user_func_array( "file_get_contents", $args ), true );
- }
- public function set_callback( $function_name )
- {
- $this->callback = $function_name;
- }
- public function remove_callback( )
- {
- $this->callback = null;
- }
- public function save( $filepath = "" )
- {
- $ret = $this->root->innertext( );
- if ( $filepath !== "" )
- {
- file_put_contents( $filepath, $ret );
- }
- return $ret;
- }
- public function find( $selector, $idx = null )
- {
- return $this->root->find( $selector, $idx );
- }
- public function clear( )
- {
- foreach ( $this->nodes as $n )
- {
- $n->clear( );
- $n = null;
- }
- if ( isset( $this->parent ) )
- {
- $this->parent->clear( );
- unset( $FN_138870004['parent'] );
- }
- if ( isset( $this->root ) )
- {
- $this->root->clear( );
- unset( $FN_138870140['root'] );
- }
- unset( $FN_138870140['doc'] );
- unset( $FN_138870140['noise'] );
- }
- public function dump( $show_attr = true )
- {
- $this->root->dump( $show_attr );
- }
- protected function prepare( $str, $lowercase = true )
- {
- $this->clear( );
- $this->doc = $str;
- $this->pos = 0;
- $this->cursor = 1;
- $this->noise = array( );
- $this->nodes = array( );
- $this->lowercase = $lowercase;
- $this->root = new simple_html_dom_node( $this );
- $this->root->tag = "root";
- $this->root->_[HDOM_INFO_BEGIN] = 0 - 1;
- $this->root->nodetype = HDOM_TYPE_ROOT;
- $this->parent = $this->root;
- $this->size = strlen( $str );
- if ( 0 < $this->size )
- {
- $this->char = $this->doc[0];
- }
- }
- protected function parse( )
- {
- if ( ( $s = $this->copy_until_char( "<" ) ) === "" )
- {
- return $this->read_tag( );
- }
- $node = new simple_html_dom_node( $this );
- ++$this->cursor;
- $node->_[HDOM_INFO_TEXT] = $s;
- $this->link_nodes( $node, false );
- return true;
- }
- protected function read_tag( )
- {
- if ( $this->char !== "<" )
- {
- $this->root->_[HDOM_INFO_END] = $this->cursor;
- return false;
- }
- $begin_tag_pos = $this->pos;
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- if ( $this->char === "/" )
- {
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- $this->skip( $this->token_blank_t );
- $tag = $this->copy_until_char( ">" );
- if ( ( $pos = strpos( $tag, " " ) ) !== false )
- {
- $tag = substr( $tag, 0, $pos );
- }
- $parent_lower = strtolower( $this->parent->tag );
- $tag_lower = strtolower( $tag );
- if ( $parent_lower !== $tag_lower )
- {
- if ( isset( $this->optional_closing_tags[$parent_lower], $this->block_tags[$tag_lower] ) )
- {
- $this->parent->_[HDOM_INFO_END] = 0;
- $org_parent = $this->parent;
- while ( $this->parent->parent && strtolower( $this->parent->tag ) !== $tag_lower )
- {
- $this->parent = $this->parent->parent;
- }
- if ( strtolower( $this->parent->tag ) !== $tag_lower )
- {
- $this->parent = $org_parent;
- if ( $this->parent->parent )
- {
- $this->parent = $this->parent->parent;
- }
- $this->parent->_[HDOM_INFO_END] = $this->cursor;
- return $this->as_text_node( $tag );
- }
- }
- else if ( $this->parent->parent && isset( $this->block_tags[$tag_lower] ) )
- {
- $this->parent->_[HDOM_INFO_END] = 0;
- $org_parent = $this->parent;
- while ( $this->parent->parent && strtolower( $this->parent->tag ) !== $tag_lower )
- {
- $this->parent = $this->parent->parent;
- }
- if ( strtolower( $this->parent->tag ) !== $tag_lower )
- {
- $this->parent = $org_parent;
- $this->parent->_[HDOM_INFO_END] = $this->cursor;
- return $this->as_text_node( $tag );
- }
- }
- else if ( $this->parent->parent && strtolower( $this->parent->parent->tag ) === $tag_lower )
- {
- $this->parent->_[HDOM_INFO_END] = 0;
- $this->parent = $this->parent->parent;
- }
- else
- {
- return $this->as_text_node( $tag );
- }
- }
- $this->parent->_[HDOM_INFO_END] = $this->cursor;
- if ( $this->parent->parent )
- {
- $this->parent = $this->parent->parent;
- }
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- return true;
- }
- $node = new simple_html_dom_node( $this );
- $node->_[HDOM_INFO_BEGIN] = $this->cursor;
- ++$this->cursor;
- $tag = $this->copy_until( $this->token_slash );
- if ( isset( $tag[0] ) && $tag[0] === "!" )
- {
- $node->_[HDOM_INFO_TEXT] = "<".$tag.$this->copy_until_char( ">" );
- if ( isset( $tag[2] ) && $tag[1] === "-" && $tag[2] === "-" )
- {
- $node->nodetype = HDOM_TYPE_COMMENT;
- $node->tag = "comment";
- }
- else
- {
- $node->nodetype = HDOM_TYPE_UNKNOWN;
- $node->tag = "unknown";
- }
- if ( $this->char === ">" )
- {
- $node->_[HDOM_INFO_TEXT] .= ">";
- }
- $this->link_nodes( $node, true );
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- return true;
- }
- if ( $pos = strpos( $tag, "<" ) !== false )
- {
- $tag = "<".substr( $tag, 0, 0 - 1 );
- $node->_[HDOM_INFO_TEXT] = $tag;
- $this->link_nodes( $node, false );
- $this->char = $this->doc[--$this->pos];
- return true;
- }
- if ( !preg_match( "/^[\\w-:]+\$/", $tag ) )
- {
- $node->_[HDOM_INFO_TEXT] = "<".$tag.$this->copy_until( "<>" );
- if ( $this->char === "<" )
- {
- $this->link_nodes( $node, false );
- return true;
- }
- if ( $this->char === ">" )
- {
- $node->_[HDOM_INFO_TEXT] .= ">";
- }
- $this->link_nodes( $node, false );
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- return true;
- }
- $node->nodetype = HDOM_TYPE_ELEMENT;
- $tag_lower = strtolower( $tag );
- $node->tag = $this->lowercase ? $tag_lower : $tag;
- if ( isset( $this->optional_closing_tags[$tag_lower] ) )
- {
- while ( isset( $this->optional_closing_tags[$tag_lower][strtolower( $this->parent->tag )] ) )
- {
- $this->parent->_[HDOM_INFO_END] = 0;
- $this->parent = $this->parent->parent;
- }
- $node->parent = $this->parent;
- }
- $guard = 0;
- $space = array(
- $this->copy_skip( $this->token_blank ),
- "",
- ""
- );
- do
- {
- if ( $this->char !== null && $space[0] === "" )
- {
- break;
- }
- $name = $this->copy_until( $this->token_equal );
- if ( $guard === $this->pos )
- {
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- continue;
- }
- $guard = $this->pos;
- if ( $this->size - 1 <= $this->pos && $this->char !== ">" )
- {
- $node->nodetype = HDOM_TYPE_TEXT;
- $node->_[HDOM_INFO_END] = 0;
- $node->_[HDOM_INFO_TEXT] = "<".$tag.$space[0].$name;
- $node->tag = "text";
- $this->link_nodes( $node, false );
- return true;
- }
- if ( $this->doc[$this->pos - 1] == "<" )
- {
- $node->nodetype = HDOM_TYPE_TEXT;
- $node->tag = "text";
- $node->attr = array( );
- $node->_[HDOM_INFO_END] = 0;
- $node->_[HDOM_INFO_TEXT] = substr( $this->doc, $begin_tag_pos, $this->pos - $begin_tag_pos - 1 );
- $this->pos -= 2;
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- $this->link_nodes( $node, false );
- return true;
- }
- if ( $name !== "/" && $name !== "" )
- {
- $space[1] = $this->copy_skip( $this->token_blank );
- $name = $this->restore_noise( $name );
- if ( $this->lowercase )
- {
- $name = strtolower( $name );
- }
- if ( $this->char === "=" )
- {
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- $this->parse_attr( $node, $name, $space );
- }
- else
- {
- $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
- $node->attr[$name] = true;
- if ( $this->char != ">" )
- {
- $this->char = $this->doc[--$this->pos];
- }
- }
- $node->_[HDOM_INFO_SPACE][] = $space;
- $space = array(
- $this->copy_skip( $this->token_blank ),
- "",
- ""
- );
- }
- else
- {
- break;
- }
- } while ( $this->char !== ">" && $this->char !== "/" );
- $this->link_nodes( $node, true );
- $node->_[HDOM_INFO_ENDSPACE] = $space[0];
- if ( $this->copy_until_char_escape( ">" ) === "/" )
- {
- $node->_[HDOM_INFO_ENDSPACE] .= "/";
- $node->_[HDOM_INFO_END] = 0;
- }
- else if ( !isset( $this->self_closing_tags[strtolower( $node->tag )] ) )
- {
- $this->parent = $node;
- }
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- return true;
- }
- protected function parse_attr( $node, $name, &$space )
- {
- $space[2] = $this->copy_skip( $this->token_blank );
- switch ( $this->char )
- {
- case "\"" :
- $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- $node->attr[$name] = $this->restore_noise( $this->copy_until_char_escape( "\"" ) );
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- break;
- case "'" :
- $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- $node->attr[$name] = $this->restore_noise( $this->copy_until_char_escape( "'" ) );
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- break;
- default :
- $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
- $node->attr[$name] = $this->restore_noise( $this->copy_until( $this->token_attr ) );
- }
- }
- protected function link_nodes( &$node, $is_child )
- {
- $node->parent = $this->parent;
- $this->parent->nodes[] = $node;
- if ( $is_child )
- {
- $this->parent->children[] = $node;
- }
- }
- protected function as_text_node( $tag )
- {
- $node = new simple_html_dom_node( $this );
- ++$this->cursor;
- $node->_[HDOM_INFO_TEXT] = "</".$tag.">";
- $this->link_nodes( $node, false );
- $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
- return true;
- }
- protected function skip( $chars )
- {
- $this->pos += strspn( $this->doc, $chars, $this->pos );
- $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
- }
- protected function copy_skip( $chars )
- {
- $pos = $this->pos;
- $len = strspn( $this->doc, $chars, $pos );
- $this->pos += $len;
- $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
- if ( $len === 0 )
- {
- return "";
- }
- return substr( $this->doc, $pos, $len );
- }
- protected function copy_until( $chars )
- {
- $pos = $this->pos;
- $len = strcspn( $this->doc, $chars, $pos );
- $this->pos += $len;
- $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
- return substr( $this->doc, $pos, $len );
- }
- protected function copy_until_char( $char )
- {
- if ( $this->char === null )
- {
- return "";
- }
- if ( ( $pos = strpos( $this->doc, $char, $this->pos ) ) === false )
- {
- $ret = substr( $this->doc, $this->pos, $this->size - $this->pos );
- $this->char = null;
- $this->pos = $this->size;
- return $ret;
- }
- if ( $pos === $this->pos )
- {
- return "";
- }
- $pos_old = $this->pos;
- $this->char = $this->doc[$pos];
- $this->pos = $pos;
- return substr( $this->doc, $pos_old, $pos - $pos_old );
- }
- protected function copy_until_char_escape( $char )
- {
- if ( $this->char === null )
- {
- return "";
- }
- $start = $this->pos;
- if ( !1 )
- {
- break;
- }
- if ( ( $pos = strpos( $this->doc, $char, $start ) ) === false )
- {
- $ret = substr( $this->doc, $this->pos, $this->size - $this->pos );
- $this->char = null;
- $this->pos = $this->size;
- return $ret;
- }
- if ( $pos === $this->pos )
- {
- return "";
- }
- if ( $this->doc[$pos - 1] === "\\" )
- {
- $start = $pos + 1;
- continue;
- }
- $pos_old = $this->pos;
- $this->char = $this->doc[$pos];
- $this->pos = $pos;
- return substr( $this->doc, $pos_old, $pos - $pos_old );
- }
- protected function remove_noise( $pattern, $remove_tag = false )
- {
- $count = preg_match_all( $pattern, $this->doc, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE );
- $i = $count - 1;
- while ( 0 - 1 < $i )
- {
- $key = "___noise___".sprintf( "% 3d", count( $this->noise ) + 100 );
- $idx = $remove_tag ? 0 : 1;
- $this->noise[$key] = $matches[$i][$idx][0];
- $this->doc = substr_replace( $this->doc, $key, $matches[$i][$idx][1], strlen( $matches[$i][$idx][0] ) );
- --$i;
- }
- $this->size = strlen( $this->doc );
- if ( 0 < $this->size )
- {
- $this->char = $this->doc[0];
- }
- }
- public function restore_noise( $text )
- {
- while ( ( $pos = strpos( $text, "___noise___" ) ) !== false )
- {
- $key = "___noise___".$text[$pos + 11].$text[$pos + 12].$text[$pos + 13];
- if ( isset( $this->noise[$key] ) )
- {
- $text = substr( $text, 0, $pos ).$this->noise[$key].substr( $text, $pos + 14 );
- }
- }
- return $text;
- }
- public function __toString( )
- {
- return $this->root->innertext( );
- }
- public function __get( $name )
- {
- switch ( $name )
- {
- case "outertext" :
- return $this->root->innertext( );
- case "innertext" :
- return $this->root->innertext( );
- case "plaintext" :
- return $this->root->text( );
- }
- }
- public function childNodes( $idx = -1 )
- {
- return $this->root->childNodes( $idx );
- }
- public function firstChild( )
- {
- return $this->root->first_child( );
- }
- public function lastChild( )
- {
- return $this->root->last_child( );
- }
- public function getElementById( $id )
- {
- return $this->find( "#{$id}", 0 );
- }
- public function getElementsById( $id, $idx = null )
- {
- return $this->find( "#{$id}", $idx );
- }
- public function getElementByTagName( $name )
- {
- return $this->find( $name, 0 );
- }
- public function getElementsByTagName( $name, $idx = -1 )
- {
- return $this->find( $name, $idx );
- }
- public function loadFile( )
- {
- $args = func_get_args( );
- $this->load( call_user_func_array( "file_get_contents", $args ), true );
- }
- }
- function file_get_html( )
- {
- $dom = new simple_html_dom( );
- $args = func_get_args( );
- $dom->load( call_user_func_array( "file_get_contents", $args ), true );
- return $dom;
- }
- function str_get_html( $str, $lowercase = true )
- {
- $dom = new simple_html_dom( );
- $dom->load( $str, $lowercase );
- return $dom;
- }
- function dump_html_tree( $node, $show_attr = true, $deep = 0 )
- {
- $lead = str_repeat( " ", $deep );
- echo $lead.$node->tag;
- if ( $show_attr && 0 < count( $node->attr ) )
- {
- echo "(";
- foreach ( $node->attr as $k => $v )
- {
- echo "[{$k}]=>\"".$node->$k."\", ";
- }
- echo ")";
- }
- echo "\n";
- foreach ( $node->nodes as $c )
- {
- dump_html_tree( $c, $show_attr, $deep + 1 );
- }
- }
- function file_get_dom( )
- {
- $dom = new simple_html_dom( );
- $args = func_get_args( );
- $dom->load( call_user_func_array( "file_get_contents", $args ), true );
- return $dom;
- }
- function str_get_dom( $str, $lowercase = true )
- {
- $dom = new simple_html_dom( );
- $dom->load( $str, $lowercase );
- return $dom;
- }
- if ( !defined( "_JEXEC" ) )
- {
- exit( "Restricted access" );
- }
- define( "_GTRANSLATE_VERSION", substr( pi( ), 0, 8 ) );
- jimport( "joomla.plugin.plugin" );
- class plgSystemGTranslate extends JPlugin
- {
- public $_main_lang = NULL;
- public $_api_key = NULL;
- public $_bing_api_key = NULL;
- public $_cache_time = NULL;
- public $_ssl_verify_peer = NULL;
- public $_auto_detect = NULL;
- public $_auto_detect_user = NULL;
- public $_debug_mode = NULL;
- public function plgSystemGTranslate( &$subject, $config )
- {
- global $mainframe;
- if ( $mainframe->isAdmin( ) )
- {
- if ( !extension_loaded( "json" ) )
- {
- JError::raisewarning( 410, "PHP JSON library is missing which is required by GTranslate Pro." );
- }
- if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache" ) || !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache/en" ) )
- {
- JError::raisewarning( 412, "/plugins/system/gtranslate/cache and XX language folders should be writable (777 permission) - GTranslate Pro." );
- }
- if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) )
- {
- JError::raisewarning( 413, "/plugins/system/gtranslate/license.dat file should be writable (666 permission) - GTranslate Pro." );
- }
- }
- else
- {
- $FN_-2147483593( $subject, $config );
- $this->_api_key = $this->params->get( "api_key" );
- $this->_bing_api_key = $this->params->get( "bing_api_key" );
- $this->_cache_time = ( double )$this->params->get( "cache_time" );
- $this->_ssl_verify_peer = $this->params->get( "ssl_verify_peer" );
- $this->_auto_detect = $this->params->get( "auto_detect" );
- $this->_auto_detect_user = $this->params->get( "auto_detect_user" );
- $this->_main_lang = $this->params->get( "main_lang" );
- $this->_debug_mode = $this->params->get( "debug_mode" );
- JRequest::setvar( "api_key", $this->_api_key );
- JRequest::setvar( "bing_api_key", $this->_bing_api_key );
- JRequest::setvar( "cache_time", $this->_cache_time );
- JRequest::setvar( "ssl_verify_peer", $this->_ssl_verify_peer );
- JRequest::setvar( "auto_detect", $this->_auto_detect );
- JRequest::setvar( "auto_detect_user", $this->_auto_detect_user );
- JRequest::setvar( "main_lang", $this->_main_lang );
- JRequest::setvar( "debug_mode", $this->_debug_mode );
- }
- }
- public function onAfterInitialise( )
- {
- global $mainframe;
- $user =& JFactory::getuser( );
- $uri =& JURI::getinstance( );
- $router =& $mainframe->getRouter( );
- $router->attachBuildRule( "plgSystemGTranslate::buildGTranslate" );
- $router->attachParseRule( "plgSystemGTranslate::parseGTranslate" );
- switch ( JRequest::getcmd( "task" ) )
- {
- case "saveTranslation" :
- if ( !$user->authorize( "com_content", "edit", "content", "all" ) )
- {
- echo "Not authorized";
- exit( );
- }
- $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".JRequest::getvar( "lang" )."/".JRequest::getvar( "md5" );
- file_put_contents( $file_name.".human", JRequest::getvar( "new" ) );
- unlink( $file_name );
- echo "Success";
- exit( );
- break;
- case "addText" :
- $session =& JFactory::getsession( );
- $new_order = new stdClass( );
- $new_order->md5 = JRequest::getvar( "md5" );
- $new_order->text = JRequest::getvar( "text" );
- $new_order->from = JRequest::getvar( "from" );
- $new_order->to = JRequest::getvar( "to" );
- $orders = $session->get( "orders", array( ), "gtranslate" );
- foreach ( $orders as $order )
- {
- if ( $order->md5 == $new_order->md5 && $order->from == $new_order->from && $order->to == $new_order->to )
- {
- break;
- }
- }
- $orders[] = $new_order;
- $session->set( "orders", $orders, "gtranslate" );
- exit( );
- break;
- case "removeText" :
- $session =& JFactory::getsession( );
- $orders = $session->get( "orders", array( ), "gtranslate" );
- foreach ( $orders as $i => $order )
- {
- if ( $order->md5 == JRequest::getvar( "md5" ) && $order->from == JRequest::getvar( "from" ) && $order->to == JRequest::getvar( "to" ) )
- {
- unset( $orders[$i] );
- break;
- }
- }
- $session->set( "orders", $orders, "gtranslate" );
- exit( );
- break;
- case "getCart" :
- $session =& JFactory::getsession( );
- $orders = $session->get( "orders", array( ), "gtranslate" );
- if ( count( $orders ) )
- {
- echo "<style type=\"text/css\">\nbody {color: #535548;font-family: Tahoma,Helvetica,Arial,sans-serif;font-size: 0.8em;}\n.data-bordered {border-bottom: 1px solid #B4B4B4;border-collapse: collapse;border-right: 1px solid #B4B4B4;border-spacing: 0;}\n.data-bordered img {cursor:pointer;}\n.data-bordered thead th {text-align: center;vertical-align: middle;}\n.data-bordered tbody tr {background-color: #F9F9F9;}\n.data-bordered th, .data-bordered td {border-left: 1px solid #B4B4B4;border-top: 1px solid #B4B4B4;padding: 0.35em 10px;vertical-align: top;}\n.data-bordered th {background-color: #CDCDCD;color: #454545;font: bold 100% Arial,Helvetica,sans-serif;text-transform: none;}\n.data-bordered td {font-size: 0.917em;vertical-align: middle;}\n.data-bordered .alt td {background-color: #E5E5E5;}\n.data-bordered td.nodata, th.nodata {background: none repeat scroll 0 0 #FFFFFF !important;border-left: 1px solid #FFFFFF;border-top: 1px solid #FFFFFF;}\n.data-bordered td.no, td.yes {background-position: center center;background-repeat: no-repeat;overflow: hidden;text-indent: -9999px;}\n.data-bordered .price, .buy {text-align: center;}\np.note {color: #888888;}\n</style>\n\n<p>After you comelete the order the translations will appear on your site automatically in 24 hours and you will get notified.</p>\n\n<table class=\"data-bordered\">\n <tr>\n <td class=\"nodata\"></td>\n <th nowrap=\"nowrap\" scope=\"col\">#</th>\n <th nowrap=\"nowrap\" scope=\"col\">From</th>\n <th nowrap=\"nowrap\" scope=\"col\">To</th>\n <th nowrap=\"nowrap\" scope=\"col\">Text</th>\n <th nowrap=\"nowrap\" scope=\"col\">Per Word</th>\n <th nowrap=\"nowrap\" scope=\"col\">Word Count</th>\n <th nowrap=\"nowrap\" scope=\"col\">Price †</th>\n </tr>";
- $i = $total = 0;
- foreach ( $orders as $order )
- {
- $word_count = str_word_count( $order->text );
- $total += 0.06 * $word_count;
- echo "<tr".( ++$i % 2 == 0 ? " class=\"alt\"" : "" ).">";
- echo "<td><img src=\"".JURI::base( true )."/plugins/system/gtranslate/delete.png\" width=\"16\" height=\"16\" border=\"0\" alt=\"del\" title=\"Remove\" onclick=\"removeText('".$order->from."', '".$order->to."', '".$order->md5."')\" /></td>";
- echo "<td>".$i."</td>";
- echo "<td>".$order->from."</td>";
- echo "<td>".$order->to."</td>";
- echo "<td>".substr( $order->text, 0, 50 ).( 50 < strlen( $order->text ) ? "..." : "" )."</td>";
- echo "<td>€ 0.06</td>";
- echo "<td>".$word_count."</td>";
- echo "<td>€ ".( 0.06 * $word_count )."</td>";
- echo "</tr>";
- }
- echo "<tr".( ++$i % 2 == 0 ? " class=\"alt\"" : "" ).">";
- echo "<td colspan=\"7\" align=\"right\">Total</td>";
- echo "<td>€ ".$total."</td>";
- echo "</tr>";
- echo "</table>";
- echo "<p class=\"note\">† All prices are in Euros and exclude PayPal transaction fees.</p>";
- }
- else
- {
- echo "Your cart is empty!";
- }
- exit( );
- break;
- case "completeOrder" :
- $order_id = uniqid( "order_" );
- $session =& JFactory::getsession( );
- $orders = $session->get( "orders", array( ), "gtranslate" );
- if ( file_put_contents( JPATH_BASE."/cache/".$order_id, json_encode( $orders ) ) === false )
- {
- echo "Cannot write order data, cache is not writable or you don't have enough space.";
- exit( );
- }
- $session->clear( "orders", "gtranslate" );
- header( "Location: http://edo.webmaster.am/gtranslate/complete-order?order_id=".$order_id );
- exit( );
- break;
- case "acceptTranslation" :
- }
- $order_id = JRequest::getvar( "order_id" );
- $md5 = JRequest::getvar( "md5" );
- $translation = JRequest::getvar( "translation" );
- if ( file_exists( JPATH_BASE."/cache/".$order_id ) )
- {
- $orders = json_decode( file_get_contents( JPATH_BASE."/cache/".$order_id ) );
- foreach ( $orders as $order )
- {
- if ( $order->md5 == $md5 )
- {
- $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".$order->to."/".$md5;
- file_put_contents( $file_name.".human", $translation );
- unlink( $file_name );
- echo "Success";
- exit( );
- }
- }
- }
- echo "Fail";
- exit( );
- break;
- break;
- }
- public function onAfterRender( )
- {
- $body = JResponse::getbody( );
- $this->doTranslate( $body );
- JResponse::setbody( $body );
- }
- public static function get_translation( $text, $lang )
- {
- if ( JRequest::getvar( "bing_api_key" ) != "" )
- {
- $params = array(
- "appId" => JRequest::getvar( "bing_api_key" ),
- "text" => $text,
- "from" => JRequest::getvar( "main_lang" ),
- "to" => $lang
- );
- try
- {
- $soap = new SoapClient( "http://api.microsofttranslator.com/V2/Soap.svc", array(
- "trace" => true
- ) );
- $result = $soap->Translate( $params );
- }
- catch ( Exception $ex )
- {
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Exception: ".print_r( $ex, true )."\n\n", FILE_APPEND );
- }
- }
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Request: ".$soap->__getLastRequestHeaders( )."\n", FILE_APPEND );
- file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", $soap->__getLastRequest( )."\n", FILE_APPEND );
- file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Response: ".$soap->__getLastResponseHeaders( )."\n", FILE_APPEND );
- file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", $soap->__getLastResponse( )."\n", FILE_APPEND );
- }
- return $result;
- }
- if ( in_array( $lang, array( "hy", "az", "eu", "ur", "ka", "ta", "te", "bn", "gu", "kn" ) ) || JRequest::getvar( "api_key" ) == "" )
- {
- $params = "client=a";
- $params .= "&text=".urlencode( $text );
- $params .= "&tl=".$lang;
- $params .= "&sl=".( JRequest::getint( "auto_detect", 0 ) ? "auto" : JRequest::getvar( "main_lang" ) );
- $ch = curl_init( );
- curl_setopt( $ch, CURLOPT_URL, "http://translate.google.com/translate_a/t?".$params );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
- curl_setopt( $ch, CURLOPT_VERBOSE, true );
- curl_setopt( $ch, CURLOPT_STDERR, $fh );
- }
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0" );
- $result = curl_exec( $ch );
- curl_close( $ch );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- fwrite( $fh, "Request: {$params}\n\nResponse: {$result}\n" );
- fclose( $fh );
- }
- $result = json_decode( $result );
- $return = new stdClass( );
- $translation = "";
- $i = 0;
- while ( isset( $result->sentences[$i] ) )
- {
- $translation .= $result->sentences[$i]->trans;
- ++$i;
- }
- if ( $translation != "" )
- {
- $return->data->translations[0]->translatedText = $translation;
- }
- return $return;
- }
- $params = "key=".JRequest::getvar( "api_key" );
- $params .= "&q=".urlencode( $text );
- $params .= "&userip=".$_SERVER['REMOTE_ADDR'];
- if ( JRequest::getint( "auto_detect", 0 ) == 0 )
- {
- $params .= "&source=".JRequest::getvar( "main_lang" );
- }
- $params .= "&target=".$lang;
- $ch = curl_init( );
- curl_setopt( $ch, CURLOPT_URL, "https://www.googleapis.com/language/translate/v2" );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
- curl_setopt( $ch, CURLOPT_VERBOSE, true );
- curl_setopt( $ch, CURLOPT_STDERR, $fh );
- }
- curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "X-HTTP-Method-Override: GET" ) );
- curl_setopt( $ch, CURLOPT_POST, true );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, ( string )JRequest::getint( "ssl_verify_peer" ) );
- $result = curl_exec( $ch );
- curl_close( $ch );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- fwrite( $fh, "Request: {$params}\n\nResponse: {$result}\n" );
- fclose( $fh );
- }
- return json_decode( $result );
- }
- public static function gtranslate( $text )
- {
- if ( trim( str_replace( " ", " ", $text ) ) == "" || strlen( $text ) < 2 )
- {
- return $text;
- }
- if ( JRequest::getint( "debug_mode" ) == 1 )
- {
- return "%".$text."%";
- }
- $session =& JFactory::getsession( );
- $lang = JRequest::getvar( "glang", $session->get( "glang" ) );
- if ( $lang == JRequest::getvar( "main_lang" ) )
- {
- return $text;
- }
- $md5 = md5( $text );
- $limit_exceeded_time = ( integer )file_get_contents( JPATH_ROOT."/plugins/system/gtranslate/cache/limit.txt" );
- $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".$lang."/".$md5;
- $is_human = false;
- if ( file_exists( $file_name.".human" ) )
- {
- $file_name .= ".human";
- $is_human = true;
- }
- if ( $is_human || time( ) - filemtime( $file_name ) < JRequest::getint( "cache_time" ) * 24 * 60 * 60 )
- {
- return file_get_contents( $file_name );
- }
- $result = false;
- if ( 5 * 60 < time( ) - $limit_exceeded_time || JRequest::getint( "force_translation", 0 ) )
- {
- $result = self::get_translation( $text, $lang );
- if ( !isset( $result->TranslateResult ) && !isset( $result->data->translations[0]->translatedText ) )
- {
- file_put_contents( "cache/limit.txt", time( ) );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- JError::raisewarning( 503, "Translation API limit exceeded. Will try again in 5 minutes." );
- }
- return $text;
- }
- if ( isset( $result->data->translations[0]->translatedText ) )
- {
- file_put_contents( $file_name, $result->data->translations[0]->translatedText );
- return $result->data->translations[0]->translatedText;
- }
- if ( isset( $result->TranslateResult ) )
- {
- file_put_contents( $file_name, $result->TranslateResult );
- return $result->TranslateResult;
- }
- return $text;
- }
- return $text;
- }
- public static function translate( $e )
- {
- $user =& JFactory::getuser( );
- $session =& JFactory::getsession( );
- $doc =& JFactory::getdocument( );
- $lang = JRequest::getvar( "glang", $session->get( "glang" ) );
- $can_edit = JRequest::getint( "language_edit", 0 ) && $user->authorize( "com_content", "edit", "content", "all" );
- if ( JRequest::getint( "language_edit", 0 ) && !$user->authorize( "com_content", "edit", "content", "all" ) )
- {
- global $mainframe;
- $return = JURI::base( )."index.php?option=com_user&view=login";
- $return .= "&return=".base64_encode( JURI::getinstance( )->toString( array( "scheme", "host", "port", "path", "query" ) ) );
- $mainframe->redirect( $return );
- }
- else
- {
- $prefix = $can_edit ? "<img src=\"".JURI::base( true )."/plugins/system/gtranslate/edit.png\" width=\"12\" height=\"12\" title=\"%%\" md5=\"##\" class=\"gicons\" /> <img src=\"".JURI::base( true )."/plugins/system/gtranslate/cart.png\" width=\"12\" height=\"12\" title=\"Add to cart\" md5cart=\"##\" class=\"gicons\" /> <span name=\"##\">" : "";
- $suffix = $can_edit ? "</span>" : "";
- $meta_desc_orig = $doc->getDescription( );
- $meta_keys_orig = $doc->getMetaData( "keywords" );
- $page_title_orig = $doc->getTitle( );
- switch ( $e->tag )
- {
- case "html" :
- if ( $lang == "ar" || $lang == "iw" || $lang == "fa" || $lang == "ur" )
- {
- $e->setAttribute( "dir", "rtl" );
- }
- break;
- case "meta" :
- do
- {
- if ( !( $e->getAttribute( "name" ) == "description" ) )
- {
- break;
- }
- else
- {
- $meta_desc_orig = $e->getAttribute( "content" );
- $e->setAttribute( "content", self::gtranslate( $meta_desc_orig ) );
- }
- } while ( 0 );
- if ( $e->getAttribute( "name" ) == "keywords" )
- {
- $meta_keys_orig = $e->getAttribute( "content" );
- $e->setAttribute( "content", self::gtranslate( $meta_keys_orig ) );
- }
- break;
- case "text" :
- do
- {
- $p = $e->parent( );
- while ( $p->tag != "body" )
- {
- if ( preg_match( "/notranslate/", $p->getAttribute( "class" ) ) || $p->tag == "script" || $p->tag == "style" )
- {
- break;
- }
- $p = $p->parent( );
- }
- if ( !( $e->parent( )->tag != "title" ) )
- {
- break;
- }
- else
- {
- $e->innertext = str_replace( trim( $e->innertext ), str_replace( array( "%%", "##" ), array(
- htmlspecialchars( trim( $e->innertext ) ),
- md5( trim( $e->innertext ) )
- ), $prefix ).self::gtranslate( trim( $e->innertext ) ).$suffix, $e->innertext );
- }
- } while ( 0 );
- $e->innertext = self::gtranslate( $page_title_orig );
- break;
- case "body" :
- do
- {
- if ( !$can_edit )
- {
- break;
- }
- else
- {
- $p = $e->parent( );
- $metas = $p->getElementsByTagName( "meta" );
- $title = $p->getElementByTagName( "title" );
- $page_title = "<span>Edit page title <img src=\"".JURI::base( true )."/plugins/system/gtranslate/edit.png\" width=\"12\" height=\"12\" title=\"".htmlspecialchars( $page_title_orig )."\" md5=\"".md5( $page_title_orig )."\" class=\"gicons\" /> <img src=\"".JURI::base( true )."/plugins/system/gtranslate/cart.png\" width=\"12\" height=\"12\" title=\"Add to cart\" md5cart=\"".md5( $page_title_orig )."\" class=\"gicons\" /><span name=\"".md5( $page_title_orig )."\" style=\"display:none;\">".$title->innertext."</span></span>";
- foreach ( $metas as $meta )
- {
- if ( $meta->getAttribute( "name" ) == "description" )
- {
- $meta_desc = "<span>Edit meta descritpnion <img src=\"".JURI::base( true )."/plugins/system/gtranslate/edit.png\" width=\"12\" height=\"12\" title=\"".htmlspecialchars( $meta_desc_orig )."\" md5=\"".md5( $meta_desc_orig )."\" class=\"gicons\" /> <img src=\"".JURI::base( true )."/plugins/system/gtranslate/cart.png\" width=\"12\" height=\"12\" title=\"Add to cart\" md5cart=\"".md5( $meta_desc_orig )."\" class=\"gicons\" /><span name=\"".md5( $meta_desc_orig )."\" style=\"display:none;\">".$meta->getAttribute( "content" )."</span></span>";
- }
- else if ( $meta->getAttribute( "name" ) == "keywords" )
- {
- $meta_keys = "<span>Edit meta keywords <img src=\"".JURI::base( true )."/plugins/system/gtranslate/edit.png\" width=\"12\" height=\"12\" title=\"".htmlspecialchars( $meta_keys_orig )."\" md5=\"".md5( $meta_keys_orig )."\" class=\"gicons\" /> <img src=\"".JURI::base( true )."/plugins/system/gtranslate/cart.png\" width=\"12\" height=\"12\" title=\"Add to cart\" md5cart=\"".md5( $meta_keys_orig )."\" class=\"gicons\" /><span name=\"".md5( $meta_keys_orig )."\" style=\"display:none;\">".$meta->getAttribute( "content" )."</span></span>";
- }
- }
- }
- $show_cart = "<span><a href=\"javascript:void(0);\" onclick=\"showBasket()\">Show Cart <img src=\"".JURI::base( true )."/plugins/system/gtranslate/cart.png\" width=\"12\" height=\"12\" title=\"Show cart\" class=\"gicons\" /></a></span>";
- $e->innertext = "<div style=\"background-color:#000000;color:#dddddd;opacity:0.7;padding:10px;position:fixed;left:0;top:50px;z-index:9999;\">".$page_title."<br />".$meta_desc."<br />".$meta_keys."<br />".$show_cart."</div>".$e->innertext;
- } while ( 0 );
- break;
- case "img" :
- case "td" :
- case "li" :
- if ( preg_match( "/notranslate/", $e->getAttribute( "class" ) ) )
- {
- break;
- }
- if ( $e->hasAttribute( "title" ) )
- {
- $e->setAttribute( "title", self::gtranslate( $e->getAttribute( "title" ) ) );
- }
- break;
- case "a" :
- }
- if ( $e->hasAttribute( "title" ) )
- {
- $e->setAttribute( "title", self::gtranslate( $e->getAttribute( "title" ) ) );
- }
- if ( !preg_match( "/nturl/", $e->getAttribute( "class" ) ) )
- {
- $href = $e->getAttribute( "href" );
- $href = str_replace( "://".$_SERVER['HTTP_HOST'], "://".$_SERVER['HTTP_HOST']."/".$lang, $href );
- $href = isset( $href[0] ) && $href[0] == "/" ? "/".$lang.$href : $href;
- $href = str_replace( "/".$lang."/".$lang, "/".$lang, $href );
- $e->setAttribute( "href", $href );
- }
- if ( $can_edit )
- {
- $e->setAttribute( "onclick", "return false;".$e->getAttribute( "onclick" ) );
- }
- break;
- break;
- }
- }
- private function checkLicense( )
- {
- $domain = preg_replace( "/^www\\./", "", $_SERVER['HTTP_HOST'] );
- $time = time( );
- if ( filesize( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) < 5 || 5 * 24 * 60 * 60 < $time - filemtime( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) || JRequest::getcmd( "verify_license", 0 ) )
- {
- $data = array(
- "domain" => $domain,
- "time" => $time,
- "mcrypt" => ( integer )extension_loaded( "mcrypt" )
- );
- $ch = curl_init( );
- curl_setopt( $ch, CURLOPT_URL, "http://license.gtranslate.net/" );
- curl_setopt( $ch, CURLOPT_POST, true );
- curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
- if ( JRequest::getint( "debug_mode" ) == 2 )
- {
- $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
- curl_setopt( $ch, CURLOPT_VERBOSE, true );
- curl_setopt( $ch, CURLOPT_STDERR, $fh );
- }
- curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
- $result = curl_exec( $ch );
- file_put_contents( JPATH_ROOT."/plugins/system/gtranslate/license.dat", $result );
- }
- $result = file_get_contents( JPATH_ROOT."/plugins/system/gtranslate/license.dat" );
- if ( strlen( $result ) < 200 )
- {
- return false;
- }
- $result = explode( "\r\n", $result );
- unset( $result[0] );
- array_pop( $result );
- $result = implode( "", $result );
- $license = $this->decrypt( $result, extension_loaded( "mcrypt" ) );
- if ( $license['domain'] != $domain )
- {
- return false;
- }
- if ( $time < $license['start_date'] || $license['end_date'] < $time )
- {
- return false;
- }
- return true;
- }
- private function decrypt( $str, $mcrypt = false )
- {
- $str = base64_decode( base64_decode( $str ) );
- $key = "YmUzYWM2sNGU24NNA363cA1IDSDFGDFGB5aVi38BDFGQ3YNO36ycDFGAATq4sYmSFVDFGDFGps7XDYEzGDDw96OfWW3kjCFA8M+UV2kHe1WTTEcM09UMAA8";
- if ( $mcrypt && extension_loaded( "mcrypt" ) )
- {
- $td = mcrypt_module_open( "blowfish", "", "ecb", "" );
- $iv = mcrypt_create_iv( mcrypt_enc_get_iv_size( $td ), MCRYPT_RAND );
- $key = substr( $key, 0, mcrypt_enc_get_key_size( $td ) );
- mcrypt_generic_init( $td, $key, $iv );
- $decrypt = mdecrypt_generic( $td, $str );
- mcrypt_generic_deinit( $td );
- mcrypt_module_close( $td );
- }
- else
- {
- $decrypt = "";
- $i = 1;
- while ( $i <= strlen( $str ) )
- {
- $char = substr( $str, $i - 1, 1 );
- $keychar = substr( $key, $i % strlen( $key ) - 1, 1 );
- $char = chr( ord( $char ) - ord( $keychar ) );
- $decrypt .= $char;
- ++$i;
- }
- }
- return unserialize( $decrypt );
- }
- public function doTranslate( &$content )
- {
- $session =& JFactory::getsession( );
- if ( $this->_main_lang == JRequest::getvar( "glang", $session->get( "glang", $this->_main_lang ) ) )
- {
- return;
- }
- if ( !$this->checkLicense( ) )
- {
- echo "You don't have appropriate license for your ".$_SERVER['HTTP_HOST'].". Please purchase <a href=\"http://gtranslate.net/\">GTranslate Pro</a> license or <a href=\"".JURI::base( ).$this->_main_lang."/\">go back</a>.";
- exit( );
- }
- if ( !extension_loaded( "json" ) )
- {
- JError::raisewarning( 410, "PHP JSON library is missing which is required by GTranslate Pro." );
- }
- else if ( JRequest::getvar( "bing_api_key" ) != "" && !extension_loaded( "soap" ) )
- {
- JError::raisewarning( 411, "PHP Soap client is required to use Bing Translator." );
- }
- else if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache" ) || !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache/en" ) )
- {
- JError::raisewarning( 412, "/plugins/system/gtranslate/cache and XX language folders should be writable (777 permission) - GTranslate Pro." );
- }
- else if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) )
- {
- JError::raisewarning( 413, "/plugins/system/gtranslate/license.dat file should be writable (666 permission) - GTranslate Pro." );
- }
- else
- {
- $user =& JFactory::getuser( );
- $html = str_get_html( $content );
- $html->set_callback( "plgSystemGTranslate::translate" );
- if ( JRequest::getint( "language_edit", 0 ) && $user->authorize( "com_content", "edit", "content", "all" ) )
- {
- $main_lang = JRequest::getvar( "main_lang" );
- $current_lang = JRequest::getvar( "glang", $session->get( "glang", "" ) );
- $popup = "<div id=\"light\" class=\"white_content\"></div><div id=\"fade\" class=\"black_overlay\"></div>";
- $style = "<style type=\"text/css\">";
- $style .= ".black_overlay{display:none;position:fixed;top:0%;left:0%;width:100%;height:100%;background-color:black;z-index:1001;-moz-opacity:0.8;opacity:.80;filter:alpha(opacity=80);}";
- $style .= ".white_content{display:none;position:fixed;top:25%;left:25%;width:50%;height:50%;padding:16px;border:16px solid orange;background-color:white;z-index:23888;overflow:auto;text-align:left;}";
- $style .= ".outerbox {z-index:24000 !important;}";
- $style .= "img.gicons {cursor:pointer;}";
- $style .= "img.gicons:hover {position:relative;top:-1px;left:-1px;}";
- $style .= "</style>";
- $url_base = JURI::base( true );
- $script = "<!--script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script-->\n<script type=\"text/javascript\">\n//google.load(\"elements\", \"1\", {packages: \"keyboard\"});\nvar kbd = {};\nfunction openEdit(md5, orig) {\n document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block';\n content = '';\n content += '<b>Original:</b><br>'+orig.replace(/\\\\'/g, '\\'').replace(/\\\\\"/g, '\"')+'<br><br>';\n content += '<b>Translation:</b><br><textarea style=\"width:90%;\" id=\"translation\">'+document.getElementsByName(md5)[0].innerHTML+'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement