Advertisement
Guest User

Untitled

a guest
Feb 6th, 2012
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 69.99 KB | None | 0 0
  1. <?php
  2.  
  3. class simple_html_dom_node
  4. {
  5.  
  6. public $nodetype = HDOM_TYPE_TEXT;
  7. public $tag = "text";
  8. public $attr = array( );
  9. public $children = array( );
  10. public $nodes = array( );
  11. public $parent = null;
  12. public $_ = array( );
  13. private $dom = null;
  14.  
  15. public function __construct( $dom )
  16. {
  17. $this->dom = $dom;
  18. $dom->nodes[] = $this;
  19. }
  20.  
  21. public function __destruct( )
  22. {
  23. $this->clear( );
  24. }
  25.  
  26. public function __toString( )
  27. {
  28. return $this->outertext( );
  29. }
  30.  
  31. public function clear( )
  32. {
  33. $this->dom = null;
  34. $this->nodes = null;
  35. $this->parent = null;
  36. $this->children = null;
  37. }
  38.  
  39. public function dump( $show_attr = true )
  40. {
  41. dump_html_tree( $this, $show_attr );
  42. }
  43.  
  44. public function parent( )
  45. {
  46. return $this->parent;
  47. }
  48.  
  49. public function children( $idx = -1 )
  50. {
  51. if ( $idx === 0 - 1 )
  52. {
  53. return $this->children;
  54. }
  55. if ( isset( $this->children[$idx] ) )
  56. {
  57. return $this->children[$idx];
  58. }
  59. return null;
  60. }
  61.  
  62. public function first_child( )
  63. {
  64. if ( 0 < count( $this->children ) )
  65. {
  66. return $this->children[0];
  67. }
  68. return null;
  69. }
  70.  
  71. public function last_child( )
  72. {
  73. if ( 0 < ( $count = count( $this->children ) ) )
  74. {
  75. return $this->children[$count - 1];
  76. }
  77. return null;
  78. }
  79.  
  80. public function next_sibling( )
  81. {
  82. if ( $this->parent === null )
  83. {
  84. return null;
  85. }
  86. $idx = 0;
  87. $count = count( $this->parent->children );
  88. while ( $idx < $count && $this !== $this->parent->children[$idx] )
  89. {
  90. ++$idx;
  91. }
  92. if ( $count <= ++$idx )
  93. {
  94. return null;
  95. }
  96. return $this->parent->children[$idx];
  97. }
  98.  
  99. public function prev_sibling( )
  100. {
  101. if ( $this->parent === null )
  102. {
  103. return null;
  104. }
  105. $idx = 0;
  106. $count = count( $this->parent->children );
  107. while ( $idx < $count && $this !== $this->parent->children[$idx] )
  108. {
  109. ++$idx;
  110. }
  111. if ( --$idx < 0 )
  112. {
  113. return null;
  114. }
  115. return $this->parent->children[$idx];
  116. }
  117.  
  118. public function innertext( )
  119. {
  120. if ( isset( $this->_[HDOM_INFO_INNER] ) )
  121. {
  122. return $this->_[HDOM_INFO_INNER];
  123. }
  124. if ( isset( $this->_[HDOM_INFO_TEXT] ) )
  125. {
  126. return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
  127. }
  128. $ret = "";
  129. foreach ( $this->nodes as $n )
  130. {
  131. $ret .= $n->outertext( );
  132. }
  133. return $ret;
  134. }
  135.  
  136. public function outertext( )
  137. {
  138. if ( $this->tag === "root" )
  139. {
  140. return $this->innertext( );
  141. }
  142. if ( $this->dom->callback !== null )
  143. {
  144. call_user_func_array( $this->dom->callback, array(
  145. $this
  146. ) );
  147. }
  148. if ( isset( $this->_[HDOM_INFO_OUTER] ) )
  149. {
  150. return $this->_[HDOM_INFO_OUTER];
  151. }
  152. if ( isset( $this->_[HDOM_INFO_TEXT] ) )
  153. {
  154. return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
  155. }
  156. $ret = $this->dom->nodes[$this->_[HDOM_INFO_BEGIN]]->makeup( );
  157. if ( isset( $this->_[HDOM_INFO_INNER] ) )
  158. {
  159. $ret .= $this->_[HDOM_INFO_INNER];
  160. }
  161. else
  162. {
  163. foreach ( $this->nodes as $n )
  164. {
  165. $ret .= $n->outertext( );
  166. }
  167. }
  168. if ( isset( $this->_[HDOM_INFO_END] ) && $this->_[HDOM_INFO_END] != 0 )
  169. {
  170. $ret .= "</".$this->tag.">";
  171. }
  172. return $ret;
  173. }
  174.  
  175. public function text( )
  176. {
  177. if ( isset( $this->_[HDOM_INFO_INNER] ) )
  178. {
  179. return $this->_[HDOM_INFO_INNER];
  180. }
  181. switch ( $this->nodetype )
  182. {
  183. case HDOM_TYPE_TEXT :
  184. return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
  185. case HDOM_TYPE_COMMENT :
  186. return "";
  187. case HDOM_TYPE_UNKNOWN :
  188. return "";
  189. }
  190. if ( strcasecmp( $this->tag, "script" ) === 0 )
  191. {
  192. return "";
  193. }
  194. if ( strcasecmp( $this->tag, "style" ) === 0 )
  195. {
  196. return "";
  197. }
  198. $ret = "";
  199. foreach ( $this->nodes as $n )
  200. {
  201. $ret .= $n->text( );
  202. }
  203. return $ret;
  204. }
  205.  
  206. public function xmltext( )
  207. {
  208. $ret = $this->innertext( );
  209. $ret = str_ireplace( "<![CDATA[", "", $ret );
  210. $ret = str_replace( "]]>", "", $ret );
  211. return $ret;
  212. }
  213.  
  214. public function makeup( )
  215. {
  216. if ( isset( $this->_[HDOM_INFO_TEXT] ) )
  217. {
  218. return $this->dom->restore_noise( $this->_[HDOM_INFO_TEXT] );
  219. }
  220. $ret = "<".$this->tag;
  221. $i = 0 - 1;
  222. foreach ( $this->attr as $key => $val )
  223. {
  224. ++$i;
  225. if ( $val === null || $val === false )
  226. {
  227. continue;
  228. }
  229. $ret .= $this->_[HDOM_INFO_SPACE][$i][0];
  230. if ( $val === true )
  231. {
  232. $ret .= $key;
  233. }
  234. else
  235. {
  236. switch ( $this->_[HDOM_INFO_QUOTE][$i] )
  237. {
  238. case HDOM_QUOTE_DOUBLE :
  239. $quote = "\"";
  240. break;
  241. switch ( $this->_[HDOM_INFO_QUOTE][$i] )
  242. {
  243. case HDOM_QUOTE_SINGLE :
  244. $quote = "'";
  245. break;
  246. default :
  247. default :
  248. $quote = "";
  249. }
  250. }
  251. $ret .= $key.$this->_[HDOM_INFO_SPACE][$i][1]."=".$this->_[HDOM_INFO_SPACE][$i][2].$quote.$val.$quote;
  252. }
  253. }
  254. $ret = $this->dom->restore_noise( $ret );
  255. return $ret.$this->_[HDOM_INFO_ENDSPACE].">";
  256. }
  257.  
  258. public function find( $selector, $idx = null )
  259. {
  260. $selectors = $this->parse_selector( $selector );
  261. if ( ( $count = count( $selectors ) ) === 0 )
  262. {
  263. return array( );
  264. }
  265. $found_keys = array( );
  266. $c = 0;
  267. while ( $c < $count )
  268. {
  269. if ( ( $levle = count( $selectors[0] ) ) === 0 )
  270. {
  271. return array( );
  272. }
  273. if ( !isset( $this->_[HDOM_INFO_BEGIN] ) )
  274. {
  275. return array( );
  276. }
  277. $head = array( 1 );
  278. $l = 0;
  279. while ( $l < $levle )
  280. {
  281. $ret = array( );
  282. foreach ( $head as $k => $v )
  283. {
  284. $n = $k === 0 - 1 ? $this->dom->root : $this->dom->nodes[$k];
  285. $n->seek( $selectors[$c][$l], $ret );
  286. }
  287. $head = $ret;
  288. ++$l;
  289. }
  290. foreach ( $head as $k => $v )
  291. {
  292. if ( !isset( $found_keys[$k] ) )
  293. {
  294. $found_keys[$k] = 1;
  295. }
  296. }
  297. ++$c;
  298. }
  299. ksort( $found_keys );
  300. $found = array( );
  301. foreach ( $found_keys as $k => $v )
  302. {
  303. $found[] = $this->dom->nodes[$k];
  304. }
  305. if ( is_null( $idx ) )
  306. {
  307. return $found;
  308. }
  309. if ( $idx < 0 )
  310. {
  311. $idx = count( $found ) + $idx;
  312. }
  313. return isset( $found[$idx] ) ? $found[$idx] : null;
  314. }
  315.  
  316. protected function seek( $selector, &$ret )
  317. {
  318. list( $tag, $key, $val, $exp, $no_key ) = tag if ( $tag && $key && is_numeric( $key ) )
  319. {
  320. $count = 0;
  321. foreach ( $this->children as $c )
  322. {
  323. if ( !( $tag === "*" || $tag === $c->tag ) && !( ++$count == $key ) )
  324. {
  325. continue;
  326. }
  327. $ret[$c->_[HDOM_INFO_BEGIN]] = 1;
  328. return;
  329. }
  330. }
  331. else
  332. {
  333. $end = !empty( $this->_[HDOM_INFO_END] ) ? $this->_[HDOM_INFO_END] : 0;
  334. if ( $end == 0 )
  335. {
  336. $parent = $this->parent;
  337. while ( !isset( $parent->_[HDOM_INFO_END] ) && $parent !== null )
  338. {
  339. $end -= 1;
  340. $parent = $parent->parent;
  341. }
  342. $end += $parent->_[HDOM_INFO_END];
  343. }
  344. $i = $this->_[HDOM_INFO_BEGIN] + 1;
  345. while ( $i < $end )
  346. {
  347. $node = $this->dom->nodes[$i];
  348. $pass = true;
  349. if ( $tag === "*" && !$key )
  350. {
  351. if ( in_array( $node, $this->children, true ) )
  352. {
  353. $ret[$i] = 1;
  354. }
  355. continue;
  356. }
  357. if ( $tag && $tag != $node->tag && $tag !== "*" )
  358. {
  359. $pass = false;
  360. }
  361. if ( $pass && $key )
  362. {
  363. if ( $no_key )
  364. {
  365. if ( isset( $node->attr[$key] ) )
  366. {
  367. $pass = false;
  368. }
  369. }
  370. else if ( !isset( $node->attr[$key] ) )
  371. {
  372. $pass = false;
  373. }
  374. }
  375. if ( $pass && $key && $val && $val !== "*" )
  376. {
  377. $check = $this->match( $exp, $val, $node->attr[$key] );
  378. if ( !$check && strcasecmp( $key, "class" ) === 0 )
  379. {
  380. foreach ( explode( " ", $node->attr[$key] ) as $k )
  381. {
  382. $check = $this->match( $exp, $val, $k );
  383. if ( !$check )
  384. {
  385. continue;
  386. }
  387. break;
  388. break;
  389. }
  390. }
  391. if ( !$check )
  392. {
  393. $pass = false;
  394. }
  395. }
  396. if ( $pass )
  397. {
  398. $ret[$i] = 1;
  399. }
  400. unset( $node );
  401. ++$i;
  402. }
  403. }
  404. }
  405.  
  406. protected function match( $exp, $pattern, $value )
  407. {
  408. switch ( $exp )
  409. {
  410. case "=" :
  411. return $value === $pattern;
  412. case "!=" :
  413. return $value !== $pattern;
  414. case "^=" :
  415. return preg_match( "/^".preg_quote( $pattern, "/" )."/", $value );
  416. case "\$=" :
  417. return preg_match( "/".preg_quote( $pattern, "/" )."\$/", $value );
  418. case "*=" :
  419. if ( $pattern[0] == "/" )
  420. {
  421. return preg_match( $pattern, $value );
  422. }
  423. return preg_match( "/".$pattern."/i", $value );
  424. }
  425. return false;
  426. }
  427.  
  428. protected function parse_selector( $selector_string )
  429. {
  430. $pattern = "/([\\w-:\\*]*)(?:\\#([\\w-]+)|\\.([\\w-]+))?(?:\\[@?(!?[\\w-]+)(?:([!*^\$]?=)[\"']?(.*?)[\"']?)?\\])?([\\/, ]+)/is";
  431. preg_match_all( $pattern, trim( $selector_string )." ", $matches, PREG_SET_ORDER );
  432. $selectors = array( );
  433. $result = array( );
  434. foreach ( $matches as $m )
  435. {
  436. $m[0] = trim( $m[0] );
  437. if ( $m[0] === "" || $m[0] === "/" || $m[0] === "//" )
  438. {
  439. continue;
  440. }
  441. if ( $m[1] === "tbody" )
  442. {
  443. continue;
  444. }
  445. list( $tag, $key, $val, $exp, $no_key ) = tag if ( !empty( $m[2] ) )
  446. {
  447. $key = "id";
  448. $val = $m[2];
  449. }
  450. if ( !empty( $m[3] ) )
  451. {
  452. $key = "class";
  453. $val = $m[3];
  454. }
  455. if ( !empty( $m[4] ) )
  456. {
  457. $key = $m[4];
  458. }
  459. if ( !empty( $m[5] ) )
  460. {
  461. $exp = $m[5];
  462. }
  463. if ( !empty( $m[6] ) )
  464. {
  465. $val = $m[6];
  466. }
  467. if ( $this->dom->lowercase )
  468. {
  469. $tag = strtolower( $tag );
  470. $key = strtolower( $key );
  471. }
  472. if ( isset( $key[0] ) && $key[0] === "!" )
  473. {
  474. $key = substr( $key, 1 );
  475. $no_key = true;
  476. }
  477. $result[] = array(
  478. $tag,
  479. $key,
  480. $val,
  481. $exp,
  482. $no_key
  483. );
  484. if ( trim( $m[7] ) === "," )
  485. {
  486. $selectors[] = $result;
  487. $result = array( );
  488. }
  489. }
  490. if ( 0 < count( $result ) )
  491. {
  492. $selectors[] = $result;
  493. }
  494. return $selectors;
  495. }
  496.  
  497. public function __get( $name )
  498. {
  499. if ( isset( $this->attr[$name] ) )
  500. {
  501. return $this->attr[$name];
  502. }
  503. switch ( $name )
  504. {
  505. case "outertext" :
  506. return $this->outertext( );
  507. case "innertext" :
  508. return $this->innertext( );
  509. case "plaintext" :
  510. return $this->text( );
  511. case "xmltext" :
  512. }
  513. return $this->xmltext( );
  514. return array_key_exists( $name, $this->attr );
  515. }
  516.  
  517. public function __set( $name, $value )
  518. {
  519. switch ( $name )
  520. {
  521. case "outertext" :
  522. return $this->_[HDOM_INFO_OUTER] = $value;
  523. case "innertext" :
  524. if ( isset( $this->_[HDOM_INFO_TEXT] ) )
  525. {
  526. return $this->_[HDOM_INFO_TEXT] = $value;
  527. }
  528. return $this->_[HDOM_INFO_INNER] = $value;
  529. }
  530. if ( !isset( $this->attr[$name] ) )
  531. {
  532. $this->_[HDOM_INFO_SPACE][] = array( " ", "", "" );
  533. $this->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
  534. }
  535. $this->attr[$name] = $value;
  536. }
  537.  
  538. public function __isset( $name )
  539. {
  540. switch ( $name )
  541. {
  542. case "outertext" :
  543. return true;
  544. case "innertext" :
  545. return true;
  546. case "plaintext" :
  547. return true;
  548. }
  549. return array_key_exists( $name, $this->attr ) ? true : isset( $this->attr[$name] );
  550. }
  551.  
  552. public function __unset( $name )
  553. {
  554. if ( isset( $this->attr[$name] ) )
  555. {
  556. unset( $this->attr[$name] );
  557. }
  558. }
  559.  
  560. public function getAllAttributes( )
  561. {
  562. return $this->attr;
  563. }
  564.  
  565. public function getAttribute( $name )
  566. {
  567. return $this->__get( $name );
  568. }
  569.  
  570. public function setAttribute( $name, $value )
  571. {
  572. $this->__set( $name, $value );
  573. }
  574.  
  575. public function hasAttribute( $name )
  576. {
  577. return $this->__isset( $name );
  578. }
  579.  
  580. public function removeAttribute( $name )
  581. {
  582. $this->__set( $name, null );
  583. }
  584.  
  585. public function getElementById( $id )
  586. {
  587. return $this->find( "#{$id}", 0 );
  588. }
  589.  
  590. public function getElementsById( $id, $idx = null )
  591. {
  592. return $this->find( "#{$id}", $idx );
  593. }
  594.  
  595. public function getElementByTagName( $name )
  596. {
  597. return $this->find( $name, 0 );
  598. }
  599.  
  600. public function getElementsByTagName( $name, $idx = null )
  601. {
  602. return $this->find( $name, $idx );
  603. }
  604.  
  605. public function parentNode( )
  606. {
  607. return $this->parent( );
  608. }
  609.  
  610. public function childNodes( $idx = -1 )
  611. {
  612. return $this->children( $idx );
  613. }
  614.  
  615. public function firstChild( )
  616. {
  617. return $this->first_child( );
  618. }
  619.  
  620. public function lastChild( )
  621. {
  622. return $this->last_child( );
  623. }
  624.  
  625. public function nextSibling( )
  626. {
  627. return $this->next_sibling( );
  628. }
  629.  
  630. public function previousSibling( )
  631. {
  632. return $this->prev_sibling( );
  633. }
  634.  
  635. }
  636.  
  637. class simple_html_dom
  638. {
  639.  
  640. public $root = null;
  641. public $nodes = array( );
  642. public $callback = null;
  643. public $lowercase = false;
  644. protected $pos = NULL;
  645. protected $doc = NULL;
  646. protected $char = NULL;
  647. protected $size = NULL;
  648. protected $cursor = NULL;
  649. protected $parent = NULL;
  650. protected $noise = array( );
  651. protected $token_blank = " \t\r\n";
  652. protected $token_equal = " =/>";
  653. protected $token_slash = " />\r\n\t";
  654. protected $token_attr = " >";
  655. protected $self_closing_tags = array
  656. (
  657. "img" => 1,
  658. "br" => 1,
  659. "input" => 1,
  660. "meta" => 1,
  661. "link" => 1,
  662. "hr" => 1,
  663. "base" => 1,
  664. "embed" => 1,
  665. "spacer" => 1
  666. );
  667. protected $block_tags = array
  668. (
  669. "root" => 1,
  670. "body" => 1,
  671. "form" => 1,
  672. "div" => 1,
  673. "span" => 1,
  674. "table" => 1,
  675. "object" => 1
  676. );
  677. protected $optional_closing_tags = array
  678. (
  679. "tr" => array
  680. (
  681. "tr" => 1,
  682. "td" => 1,
  683. "th" => 1
  684. ),
  685. "th" => array
  686. (
  687. "th" => 1
  688. ),
  689. "td" => array
  690. (
  691. "td" => 1
  692. ),
  693. "li" => array
  694. (
  695. "li" => 1
  696. ),
  697. "dt" => array
  698. (
  699. "dt" => 1,
  700. "dd" => 1
  701. ),
  702. "dd" => array
  703. (
  704. "dd" => 1,
  705. "dt" => 1
  706. ),
  707. "dl" => array
  708. (
  709. "dd" => 1,
  710. "dt" => 1
  711. ),
  712. "p" => array
  713. (
  714. "p" => 1
  715. ),
  716. "nobr" => array
  717. (
  718. "nobr" => 1
  719. )
  720. );
  721.  
  722. public function __construct( $str = null )
  723. {
  724. if ( $str )
  725. {
  726. if ( preg_match( "/^http:\\/\\//i", $str ) || is_file( $str ) )
  727. {
  728. $this->load_file( $str );
  729. }
  730. else
  731. {
  732. $this->load( $str );
  733. }
  734. }
  735. }
  736.  
  737. public function __destruct( )
  738. {
  739. $this->clear( );
  740. }
  741.  
  742. public function load( $str, $lowercase = true )
  743. {
  744. $this->prepare( $str, $lowercase );
  745. $this->remove_noise( "'<!--(.*?)-->'is" );
  746. $this->remove_noise( "'<!\\[CDATA\\[(.*?)\\]\\]>'is", true );
  747. $this->remove_noise( "'<\\s*style[^>]*[^/]>(.*?)<\\s*/\\s*style\\s*>'is" );
  748. $this->remove_noise( "'<\\s*style\\s*>(.*?)<\\s*/\\s*style\\s*>'is" );
  749. $this->remove_noise( "'<\\s*script[^>]*[^/]>(.*?)<\\s*/\\s*script\\s*>'is" );
  750. $this->remove_noise( "'<\\s*script\\s*>(.*?)<\\s*/\\s*script\\s*>'is" );
  751. $this->remove_noise( "'<\\s*(?:code)[^>]*>(.*?)<\\s*/\\s*(?:code)\\s*>'is" );
  752. $this->remove_noise( "'(<\\?)(.*?)(\\?>)'s", true );
  753. $this->remove_noise( "'(\\{\\w)(.*?)(\\})'s", true );
  754. while ( $this->parse( ) )
  755. {
  756. }
  757. $this->root->_[HDOM_INFO_END] = $this->cursor;
  758. }
  759.  
  760. public function load_file( )
  761. {
  762. $args = func_get_args( );
  763. $this->load( call_user_func_array( "file_get_contents", $args ), true );
  764. }
  765.  
  766. public function set_callback( $function_name )
  767. {
  768. $this->callback = $function_name;
  769. }
  770.  
  771. public function remove_callback( )
  772. {
  773. $this->callback = null;
  774. }
  775.  
  776. public function save( $filepath = "" )
  777. {
  778. $ret = $this->root->innertext( );
  779. if ( $filepath !== "" )
  780. {
  781. file_put_contents( $filepath, $ret );
  782. }
  783. return $ret;
  784. }
  785.  
  786. public function find( $selector, $idx = null )
  787. {
  788. return $this->root->find( $selector, $idx );
  789. }
  790.  
  791. public function clear( )
  792. {
  793. foreach ( $this->nodes as $n )
  794. {
  795. $n->clear( );
  796. $n = null;
  797. }
  798. if ( isset( $this->parent ) )
  799. {
  800. $this->parent->clear( );
  801. unset( $FN_138870004['parent'] );
  802. }
  803. if ( isset( $this->root ) )
  804. {
  805. $this->root->clear( );
  806. unset( $FN_138870140['root'] );
  807. }
  808. unset( $FN_138870140['doc'] );
  809. unset( $FN_138870140['noise'] );
  810. }
  811.  
  812. public function dump( $show_attr = true )
  813. {
  814. $this->root->dump( $show_attr );
  815. }
  816.  
  817. protected function prepare( $str, $lowercase = true )
  818. {
  819. $this->clear( );
  820. $this->doc = $str;
  821. $this->pos = 0;
  822. $this->cursor = 1;
  823. $this->noise = array( );
  824. $this->nodes = array( );
  825. $this->lowercase = $lowercase;
  826. $this->root = new simple_html_dom_node( $this );
  827. $this->root->tag = "root";
  828. $this->root->_[HDOM_INFO_BEGIN] = 0 - 1;
  829. $this->root->nodetype = HDOM_TYPE_ROOT;
  830. $this->parent = $this->root;
  831. $this->size = strlen( $str );
  832. if ( 0 < $this->size )
  833. {
  834. $this->char = $this->doc[0];
  835. }
  836. }
  837.  
  838. protected function parse( )
  839. {
  840. if ( ( $s = $this->copy_until_char( "<" ) ) === "" )
  841. {
  842. return $this->read_tag( );
  843. }
  844. $node = new simple_html_dom_node( $this );
  845. ++$this->cursor;
  846. $node->_[HDOM_INFO_TEXT] = $s;
  847. $this->link_nodes( $node, false );
  848. return true;
  849. }
  850.  
  851. protected function read_tag( )
  852. {
  853. if ( $this->char !== "<" )
  854. {
  855. $this->root->_[HDOM_INFO_END] = $this->cursor;
  856. return false;
  857. }
  858. $begin_tag_pos = $this->pos;
  859. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  860. if ( $this->char === "/" )
  861. {
  862. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  863. $this->skip( $this->token_blank_t );
  864. $tag = $this->copy_until_char( ">" );
  865. if ( ( $pos = strpos( $tag, " " ) ) !== false )
  866. {
  867. $tag = substr( $tag, 0, $pos );
  868. }
  869. $parent_lower = strtolower( $this->parent->tag );
  870. $tag_lower = strtolower( $tag );
  871. if ( $parent_lower !== $tag_lower )
  872. {
  873. if ( isset( $this->optional_closing_tags[$parent_lower], $this->block_tags[$tag_lower] ) )
  874. {
  875. $this->parent->_[HDOM_INFO_END] = 0;
  876. $org_parent = $this->parent;
  877. while ( $this->parent->parent && strtolower( $this->parent->tag ) !== $tag_lower )
  878. {
  879. $this->parent = $this->parent->parent;
  880. }
  881. if ( strtolower( $this->parent->tag ) !== $tag_lower )
  882. {
  883. $this->parent = $org_parent;
  884. if ( $this->parent->parent )
  885. {
  886. $this->parent = $this->parent->parent;
  887. }
  888. $this->parent->_[HDOM_INFO_END] = $this->cursor;
  889. return $this->as_text_node( $tag );
  890. }
  891. }
  892. else if ( $this->parent->parent && isset( $this->block_tags[$tag_lower] ) )
  893. {
  894. $this->parent->_[HDOM_INFO_END] = 0;
  895. $org_parent = $this->parent;
  896. while ( $this->parent->parent && strtolower( $this->parent->tag ) !== $tag_lower )
  897. {
  898. $this->parent = $this->parent->parent;
  899. }
  900. if ( strtolower( $this->parent->tag ) !== $tag_lower )
  901. {
  902. $this->parent = $org_parent;
  903. $this->parent->_[HDOM_INFO_END] = $this->cursor;
  904. return $this->as_text_node( $tag );
  905. }
  906. }
  907. else if ( $this->parent->parent && strtolower( $this->parent->parent->tag ) === $tag_lower )
  908. {
  909. $this->parent->_[HDOM_INFO_END] = 0;
  910. $this->parent = $this->parent->parent;
  911. }
  912. else
  913. {
  914. return $this->as_text_node( $tag );
  915. }
  916. }
  917. $this->parent->_[HDOM_INFO_END] = $this->cursor;
  918. if ( $this->parent->parent )
  919. {
  920. $this->parent = $this->parent->parent;
  921. }
  922. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  923. return true;
  924. }
  925. $node = new simple_html_dom_node( $this );
  926. $node->_[HDOM_INFO_BEGIN] = $this->cursor;
  927. ++$this->cursor;
  928. $tag = $this->copy_until( $this->token_slash );
  929. if ( isset( $tag[0] ) && $tag[0] === "!" )
  930. {
  931. $node->_[HDOM_INFO_TEXT] = "<".$tag.$this->copy_until_char( ">" );
  932. if ( isset( $tag[2] ) && $tag[1] === "-" && $tag[2] === "-" )
  933. {
  934. $node->nodetype = HDOM_TYPE_COMMENT;
  935. $node->tag = "comment";
  936. }
  937. else
  938. {
  939. $node->nodetype = HDOM_TYPE_UNKNOWN;
  940. $node->tag = "unknown";
  941. }
  942. if ( $this->char === ">" )
  943. {
  944. $node->_[HDOM_INFO_TEXT] .= ">";
  945. }
  946. $this->link_nodes( $node, true );
  947. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  948. return true;
  949. }
  950. if ( $pos = strpos( $tag, "<" ) !== false )
  951. {
  952. $tag = "<".substr( $tag, 0, 0 - 1 );
  953. $node->_[HDOM_INFO_TEXT] = $tag;
  954. $this->link_nodes( $node, false );
  955. $this->char = $this->doc[--$this->pos];
  956. return true;
  957. }
  958. if ( !preg_match( "/^[\\w-:]+\$/", $tag ) )
  959. {
  960. $node->_[HDOM_INFO_TEXT] = "<".$tag.$this->copy_until( "<>" );
  961. if ( $this->char === "<" )
  962. {
  963. $this->link_nodes( $node, false );
  964. return true;
  965. }
  966. if ( $this->char === ">" )
  967. {
  968. $node->_[HDOM_INFO_TEXT] .= ">";
  969. }
  970. $this->link_nodes( $node, false );
  971. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  972. return true;
  973. }
  974. $node->nodetype = HDOM_TYPE_ELEMENT;
  975. $tag_lower = strtolower( $tag );
  976. $node->tag = $this->lowercase ? $tag_lower : $tag;
  977. if ( isset( $this->optional_closing_tags[$tag_lower] ) )
  978. {
  979. while ( isset( $this->optional_closing_tags[$tag_lower][strtolower( $this->parent->tag )] ) )
  980. {
  981. $this->parent->_[HDOM_INFO_END] = 0;
  982. $this->parent = $this->parent->parent;
  983. }
  984. $node->parent = $this->parent;
  985. }
  986. $guard = 0;
  987. $space = array(
  988. $this->copy_skip( $this->token_blank ),
  989. "",
  990. ""
  991. );
  992. do
  993. {
  994. if ( $this->char !== null && $space[0] === "" )
  995. {
  996. break;
  997. }
  998. $name = $this->copy_until( $this->token_equal );
  999. if ( $guard === $this->pos )
  1000. {
  1001. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1002. continue;
  1003. }
  1004. $guard = $this->pos;
  1005. if ( $this->size - 1 <= $this->pos && $this->char !== ">" )
  1006. {
  1007. $node->nodetype = HDOM_TYPE_TEXT;
  1008. $node->_[HDOM_INFO_END] = 0;
  1009. $node->_[HDOM_INFO_TEXT] = "<".$tag.$space[0].$name;
  1010. $node->tag = "text";
  1011. $this->link_nodes( $node, false );
  1012. return true;
  1013. }
  1014. if ( $this->doc[$this->pos - 1] == "<" )
  1015. {
  1016. $node->nodetype = HDOM_TYPE_TEXT;
  1017. $node->tag = "text";
  1018. $node->attr = array( );
  1019. $node->_[HDOM_INFO_END] = 0;
  1020. $node->_[HDOM_INFO_TEXT] = substr( $this->doc, $begin_tag_pos, $this->pos - $begin_tag_pos - 1 );
  1021. $this->pos -= 2;
  1022. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1023. $this->link_nodes( $node, false );
  1024. return true;
  1025. }
  1026. if ( $name !== "/" && $name !== "" )
  1027. {
  1028. $space[1] = $this->copy_skip( $this->token_blank );
  1029. $name = $this->restore_noise( $name );
  1030. if ( $this->lowercase )
  1031. {
  1032. $name = strtolower( $name );
  1033. }
  1034. if ( $this->char === "=" )
  1035. {
  1036. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1037. $this->parse_attr( $node, $name, $space );
  1038. }
  1039. else
  1040. {
  1041. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
  1042. $node->attr[$name] = true;
  1043. if ( $this->char != ">" )
  1044. {
  1045. $this->char = $this->doc[--$this->pos];
  1046. }
  1047. }
  1048. $node->_[HDOM_INFO_SPACE][] = $space;
  1049. $space = array(
  1050. $this->copy_skip( $this->token_blank ),
  1051. "",
  1052. ""
  1053. );
  1054. }
  1055. else
  1056. {
  1057. break;
  1058. }
  1059. } while ( $this->char !== ">" && $this->char !== "/" );
  1060. $this->link_nodes( $node, true );
  1061. $node->_[HDOM_INFO_ENDSPACE] = $space[0];
  1062. if ( $this->copy_until_char_escape( ">" ) === "/" )
  1063. {
  1064. $node->_[HDOM_INFO_ENDSPACE] .= "/";
  1065. $node->_[HDOM_INFO_END] = 0;
  1066. }
  1067. else if ( !isset( $this->self_closing_tags[strtolower( $node->tag )] ) )
  1068. {
  1069. $this->parent = $node;
  1070. }
  1071. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1072. return true;
  1073. }
  1074.  
  1075. protected function parse_attr( $node, $name, &$space )
  1076. {
  1077. $space[2] = $this->copy_skip( $this->token_blank );
  1078. switch ( $this->char )
  1079. {
  1080. case "\"" :
  1081. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_DOUBLE;
  1082. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1083. $node->attr[$name] = $this->restore_noise( $this->copy_until_char_escape( "\"" ) );
  1084. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1085. break;
  1086. case "'" :
  1087. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_SINGLE;
  1088. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1089. $node->attr[$name] = $this->restore_noise( $this->copy_until_char_escape( "'" ) );
  1090. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1091. break;
  1092. default :
  1093. $node->_[HDOM_INFO_QUOTE][] = HDOM_QUOTE_NO;
  1094. $node->attr[$name] = $this->restore_noise( $this->copy_until( $this->token_attr ) );
  1095. }
  1096. }
  1097.  
  1098. protected function link_nodes( &$node, $is_child )
  1099. {
  1100. $node->parent = $this->parent;
  1101. $this->parent->nodes[] = $node;
  1102. if ( $is_child )
  1103. {
  1104. $this->parent->children[] = $node;
  1105. }
  1106. }
  1107.  
  1108. protected function as_text_node( $tag )
  1109. {
  1110. $node = new simple_html_dom_node( $this );
  1111. ++$this->cursor;
  1112. $node->_[HDOM_INFO_TEXT] = "</".$tag.">";
  1113. $this->link_nodes( $node, false );
  1114. $this->char = ++$this->pos < $this->size ? $this->doc[$this->pos] : null;
  1115. return true;
  1116. }
  1117.  
  1118. protected function skip( $chars )
  1119. {
  1120. $this->pos += strspn( $this->doc, $chars, $this->pos );
  1121. $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
  1122. }
  1123.  
  1124. protected function copy_skip( $chars )
  1125. {
  1126. $pos = $this->pos;
  1127. $len = strspn( $this->doc, $chars, $pos );
  1128. $this->pos += $len;
  1129. $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
  1130. if ( $len === 0 )
  1131. {
  1132. return "";
  1133. }
  1134. return substr( $this->doc, $pos, $len );
  1135. }
  1136.  
  1137. protected function copy_until( $chars )
  1138. {
  1139. $pos = $this->pos;
  1140. $len = strcspn( $this->doc, $chars, $pos );
  1141. $this->pos += $len;
  1142. $this->char = $this->pos < $this->size ? $this->doc[$this->pos] : null;
  1143. return substr( $this->doc, $pos, $len );
  1144. }
  1145.  
  1146. protected function copy_until_char( $char )
  1147. {
  1148. if ( $this->char === null )
  1149. {
  1150. return "";
  1151. }
  1152. if ( ( $pos = strpos( $this->doc, $char, $this->pos ) ) === false )
  1153. {
  1154. $ret = substr( $this->doc, $this->pos, $this->size - $this->pos );
  1155. $this->char = null;
  1156. $this->pos = $this->size;
  1157. return $ret;
  1158. }
  1159. if ( $pos === $this->pos )
  1160. {
  1161. return "";
  1162. }
  1163. $pos_old = $this->pos;
  1164. $this->char = $this->doc[$pos];
  1165. $this->pos = $pos;
  1166. return substr( $this->doc, $pos_old, $pos - $pos_old );
  1167. }
  1168.  
  1169. protected function copy_until_char_escape( $char )
  1170. {
  1171. if ( $this->char === null )
  1172. {
  1173. return "";
  1174. }
  1175. $start = $this->pos;
  1176. if ( !1 )
  1177. {
  1178. break;
  1179. }
  1180. if ( ( $pos = strpos( $this->doc, $char, $start ) ) === false )
  1181. {
  1182. $ret = substr( $this->doc, $this->pos, $this->size - $this->pos );
  1183. $this->char = null;
  1184. $this->pos = $this->size;
  1185. return $ret;
  1186. }
  1187. if ( $pos === $this->pos )
  1188. {
  1189. return "";
  1190. }
  1191. if ( $this->doc[$pos - 1] === "\\" )
  1192. {
  1193. $start = $pos + 1;
  1194. continue;
  1195. }
  1196. $pos_old = $this->pos;
  1197. $this->char = $this->doc[$pos];
  1198. $this->pos = $pos;
  1199. return substr( $this->doc, $pos_old, $pos - $pos_old );
  1200. }
  1201.  
  1202. protected function remove_noise( $pattern, $remove_tag = false )
  1203. {
  1204. $count = preg_match_all( $pattern, $this->doc, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE );
  1205. $i = $count - 1;
  1206. while ( 0 - 1 < $i )
  1207. {
  1208. $key = "___noise___".sprintf( "% 3d", count( $this->noise ) + 100 );
  1209. $idx = $remove_tag ? 0 : 1;
  1210. $this->noise[$key] = $matches[$i][$idx][0];
  1211. $this->doc = substr_replace( $this->doc, $key, $matches[$i][$idx][1], strlen( $matches[$i][$idx][0] ) );
  1212. --$i;
  1213. }
  1214. $this->size = strlen( $this->doc );
  1215. if ( 0 < $this->size )
  1216. {
  1217. $this->char = $this->doc[0];
  1218. }
  1219. }
  1220.  
  1221. public function restore_noise( $text )
  1222. {
  1223. while ( ( $pos = strpos( $text, "___noise___" ) ) !== false )
  1224. {
  1225. $key = "___noise___".$text[$pos + 11].$text[$pos + 12].$text[$pos + 13];
  1226. if ( isset( $this->noise[$key] ) )
  1227. {
  1228. $text = substr( $text, 0, $pos ).$this->noise[$key].substr( $text, $pos + 14 );
  1229. }
  1230. }
  1231. return $text;
  1232. }
  1233.  
  1234. public function __toString( )
  1235. {
  1236. return $this->root->innertext( );
  1237. }
  1238.  
  1239. public function __get( $name )
  1240. {
  1241. switch ( $name )
  1242. {
  1243. case "outertext" :
  1244. return $this->root->innertext( );
  1245. case "innertext" :
  1246. return $this->root->innertext( );
  1247. case "plaintext" :
  1248. return $this->root->text( );
  1249. }
  1250. }
  1251.  
  1252. public function childNodes( $idx = -1 )
  1253. {
  1254. return $this->root->childNodes( $idx );
  1255. }
  1256.  
  1257. public function firstChild( )
  1258. {
  1259. return $this->root->first_child( );
  1260. }
  1261.  
  1262. public function lastChild( )
  1263. {
  1264. return $this->root->last_child( );
  1265. }
  1266.  
  1267. public function getElementById( $id )
  1268. {
  1269. return $this->find( "#{$id}", 0 );
  1270. }
  1271.  
  1272. public function getElementsById( $id, $idx = null )
  1273. {
  1274. return $this->find( "#{$id}", $idx );
  1275. }
  1276.  
  1277. public function getElementByTagName( $name )
  1278. {
  1279. return $this->find( $name, 0 );
  1280. }
  1281.  
  1282. public function getElementsByTagName( $name, $idx = -1 )
  1283. {
  1284. return $this->find( $name, $idx );
  1285. }
  1286.  
  1287. public function loadFile( )
  1288. {
  1289. $args = func_get_args( );
  1290. $this->load( call_user_func_array( "file_get_contents", $args ), true );
  1291. }
  1292.  
  1293. }
  1294.  
  1295. function file_get_html( )
  1296. {
  1297. $dom = new simple_html_dom( );
  1298. $args = func_get_args( );
  1299. $dom->load( call_user_func_array( "file_get_contents", $args ), true );
  1300. return $dom;
  1301. }
  1302.  
  1303. function str_get_html( $str, $lowercase = true )
  1304. {
  1305. $dom = new simple_html_dom( );
  1306. $dom->load( $str, $lowercase );
  1307. return $dom;
  1308. }
  1309.  
  1310. function dump_html_tree( $node, $show_attr = true, $deep = 0 )
  1311. {
  1312. $lead = str_repeat( " ", $deep );
  1313. echo $lead.$node->tag;
  1314. if ( $show_attr && 0 < count( $node->attr ) )
  1315. {
  1316. echo "(";
  1317. foreach ( $node->attr as $k => $v )
  1318. {
  1319. echo "[{$k}]=>\"".$node->$k."\", ";
  1320. }
  1321. echo ")";
  1322. }
  1323. echo "\n";
  1324. foreach ( $node->nodes as $c )
  1325. {
  1326. dump_html_tree( $c, $show_attr, $deep + 1 );
  1327. }
  1328. }
  1329.  
  1330. function file_get_dom( )
  1331. {
  1332. $dom = new simple_html_dom( );
  1333. $args = func_get_args( );
  1334. $dom->load( call_user_func_array( "file_get_contents", $args ), true );
  1335. return $dom;
  1336. }
  1337.  
  1338. function str_get_dom( $str, $lowercase = true )
  1339. {
  1340. $dom = new simple_html_dom( );
  1341. $dom->load( $str, $lowercase );
  1342. return $dom;
  1343. }
  1344.  
  1345. if ( !defined( "_JEXEC" ) )
  1346. {
  1347. exit( "Restricted access" );
  1348. }
  1349. define( "_GTRANSLATE_VERSION", substr( pi( ), 0, 8 ) );
  1350. jimport( "joomla.plugin.plugin" );
  1351. class plgSystemGTranslate extends JPlugin
  1352. {
  1353.  
  1354. public $_main_lang = NULL;
  1355. public $_api_key = NULL;
  1356. public $_bing_api_key = NULL;
  1357. public $_cache_time = NULL;
  1358. public $_ssl_verify_peer = NULL;
  1359. public $_auto_detect = NULL;
  1360. public $_auto_detect_user = NULL;
  1361. public $_debug_mode = NULL;
  1362.  
  1363. public function plgSystemGTranslate( &$subject, $config )
  1364. {
  1365. global $mainframe;
  1366. if ( $mainframe->isAdmin( ) )
  1367. {
  1368. if ( !extension_loaded( "json" ) )
  1369. {
  1370. JError::raisewarning( 410, "PHP JSON library is missing which is required by GTranslate Pro." );
  1371. }
  1372. if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache" ) || !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache/en" ) )
  1373. {
  1374. JError::raisewarning( 412, "/plugins/system/gtranslate/cache and XX language folders should be writable (777 permission) - GTranslate Pro." );
  1375. }
  1376. if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) )
  1377. {
  1378. JError::raisewarning( 413, "/plugins/system/gtranslate/license.dat file should be writable (666 permission) - GTranslate Pro." );
  1379. }
  1380. }
  1381. else
  1382. {
  1383. $FN_-2147483593( $subject, $config );
  1384. $this->_api_key = $this->params->get( "api_key" );
  1385. $this->_bing_api_key = $this->params->get( "bing_api_key" );
  1386. $this->_cache_time = ( double )$this->params->get( "cache_time" );
  1387. $this->_ssl_verify_peer = $this->params->get( "ssl_verify_peer" );
  1388. $this->_auto_detect = $this->params->get( "auto_detect" );
  1389. $this->_auto_detect_user = $this->params->get( "auto_detect_user" );
  1390. $this->_main_lang = $this->params->get( "main_lang" );
  1391. $this->_debug_mode = $this->params->get( "debug_mode" );
  1392. JRequest::setvar( "api_key", $this->_api_key );
  1393. JRequest::setvar( "bing_api_key", $this->_bing_api_key );
  1394. JRequest::setvar( "cache_time", $this->_cache_time );
  1395. JRequest::setvar( "ssl_verify_peer", $this->_ssl_verify_peer );
  1396. JRequest::setvar( "auto_detect", $this->_auto_detect );
  1397. JRequest::setvar( "auto_detect_user", $this->_auto_detect_user );
  1398. JRequest::setvar( "main_lang", $this->_main_lang );
  1399. JRequest::setvar( "debug_mode", $this->_debug_mode );
  1400. }
  1401. }
  1402.  
  1403. public function onAfterInitialise( )
  1404. {
  1405. global $mainframe;
  1406. $user =& JFactory::getuser( );
  1407. $uri =& JURI::getinstance( );
  1408. $router =& $mainframe->getRouter( );
  1409. $router->attachBuildRule( "plgSystemGTranslate::buildGTranslate" );
  1410. $router->attachParseRule( "plgSystemGTranslate::parseGTranslate" );
  1411. switch ( JRequest::getcmd( "task" ) )
  1412. {
  1413. case "saveTranslation" :
  1414. if ( !$user->authorize( "com_content", "edit", "content", "all" ) )
  1415. {
  1416. echo "Not authorized";
  1417. exit( );
  1418. }
  1419. $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".JRequest::getvar( "lang" )."/".JRequest::getvar( "md5" );
  1420. file_put_contents( $file_name.".human", JRequest::getvar( "new" ) );
  1421. unlink( $file_name );
  1422. echo "Success";
  1423. exit( );
  1424. break;
  1425. case "addText" :
  1426. $session =& JFactory::getsession( );
  1427. $new_order = new stdClass( );
  1428. $new_order->md5 = JRequest::getvar( "md5" );
  1429. $new_order->text = JRequest::getvar( "text" );
  1430. $new_order->from = JRequest::getvar( "from" );
  1431. $new_order->to = JRequest::getvar( "to" );
  1432. $orders = $session->get( "orders", array( ), "gtranslate" );
  1433. foreach ( $orders as $order )
  1434. {
  1435. if ( $order->md5 == $new_order->md5 && $order->from == $new_order->from && $order->to == $new_order->to )
  1436. {
  1437. break;
  1438. }
  1439. }
  1440. $orders[] = $new_order;
  1441. $session->set( "orders", $orders, "gtranslate" );
  1442. exit( );
  1443. break;
  1444. case "removeText" :
  1445. $session =& JFactory::getsession( );
  1446. $orders = $session->get( "orders", array( ), "gtranslate" );
  1447. foreach ( $orders as $i => $order )
  1448. {
  1449. if ( $order->md5 == JRequest::getvar( "md5" ) && $order->from == JRequest::getvar( "from" ) && $order->to == JRequest::getvar( "to" ) )
  1450. {
  1451. unset( $orders[$i] );
  1452. break;
  1453. }
  1454. }
  1455. $session->set( "orders", $orders, "gtranslate" );
  1456. exit( );
  1457. break;
  1458. case "getCart" :
  1459. $session =& JFactory::getsession( );
  1460. $orders = $session->get( "orders", array( ), "gtranslate" );
  1461. if ( count( $orders ) )
  1462. {
  1463. 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>";
  1464. $i = $total = 0;
  1465. foreach ( $orders as $order )
  1466. {
  1467. $word_count = str_word_count( $order->text );
  1468. $total += 0.06 * $word_count;
  1469. echo "<tr".( ++$i % 2 == 0 ? " class=\"alt\"" : "" ).">";
  1470. 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>";
  1471. echo "<td>".$i."</td>";
  1472. echo "<td>".$order->from."</td>";
  1473. echo "<td>".$order->to."</td>";
  1474. echo "<td>".substr( $order->text, 0, 50 ).( 50 < strlen( $order->text ) ? "..." : "" )."</td>";
  1475. echo "<td>€ 0.06</td>";
  1476. echo "<td>".$word_count."</td>";
  1477. echo "<td>€ ".( 0.06 * $word_count )."</td>";
  1478. echo "</tr>";
  1479. }
  1480. echo "<tr".( ++$i % 2 == 0 ? " class=\"alt\"" : "" ).">";
  1481. echo "<td colspan=\"7\" align=\"right\">Total</td>";
  1482. echo "<td>€ ".$total."</td>";
  1483. echo "</tr>";
  1484. echo "</table>";
  1485. echo "<p class=\"note\">† All prices are in Euros and exclude PayPal transaction fees.</p>";
  1486. }
  1487. else
  1488. {
  1489. echo "Your cart is empty!";
  1490. }
  1491. exit( );
  1492. break;
  1493. case "completeOrder" :
  1494. $order_id = uniqid( "order_" );
  1495. $session =& JFactory::getsession( );
  1496. $orders = $session->get( "orders", array( ), "gtranslate" );
  1497. if ( file_put_contents( JPATH_BASE."/cache/".$order_id, json_encode( $orders ) ) === false )
  1498. {
  1499. echo "Cannot write order data, cache is not writable or you don't have enough space.";
  1500. exit( );
  1501. }
  1502. $session->clear( "orders", "gtranslate" );
  1503. header( "Location: http://edo.webmaster.am/gtranslate/complete-order?order_id=".$order_id );
  1504. exit( );
  1505. break;
  1506. case "acceptTranslation" :
  1507. }
  1508. $order_id = JRequest::getvar( "order_id" );
  1509. $md5 = JRequest::getvar( "md5" );
  1510. $translation = JRequest::getvar( "translation" );
  1511. if ( file_exists( JPATH_BASE."/cache/".$order_id ) )
  1512. {
  1513. $orders = json_decode( file_get_contents( JPATH_BASE."/cache/".$order_id ) );
  1514. foreach ( $orders as $order )
  1515. {
  1516. if ( $order->md5 == $md5 )
  1517. {
  1518. $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".$order->to."/".$md5;
  1519. file_put_contents( $file_name.".human", $translation );
  1520. unlink( $file_name );
  1521. echo "Success";
  1522. exit( );
  1523. }
  1524. }
  1525. }
  1526. echo "Fail";
  1527. exit( );
  1528. break;
  1529. break;
  1530. }
  1531.  
  1532. public function onAfterRender( )
  1533. {
  1534. $body = JResponse::getbody( );
  1535. $this->doTranslate( $body );
  1536. JResponse::setbody( $body );
  1537. }
  1538.  
  1539. public static function get_translation( $text, $lang )
  1540. {
  1541. if ( JRequest::getvar( "bing_api_key" ) != "" )
  1542. {
  1543. $params = array(
  1544. "appId" => JRequest::getvar( "bing_api_key" ),
  1545. "text" => $text,
  1546. "from" => JRequest::getvar( "main_lang" ),
  1547. "to" => $lang
  1548. );
  1549. try
  1550. {
  1551. $soap = new SoapClient( "http://api.microsofttranslator.com/V2/Soap.svc", array(
  1552. "trace" => true
  1553. ) );
  1554. $result = $soap->Translate( $params );
  1555. }
  1556. catch ( Exception $ex )
  1557. {
  1558. if ( JRequest::getint( "debug_mode" ) == 2 )
  1559. {
  1560. file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Exception: ".print_r( $ex, true )."\n\n", FILE_APPEND );
  1561. }
  1562. }
  1563. if ( JRequest::getint( "debug_mode" ) == 2 )
  1564. {
  1565. file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Request: ".$soap->__getLastRequestHeaders( )."\n", FILE_APPEND );
  1566. file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", $soap->__getLastRequest( )."\n", FILE_APPEND );
  1567. file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "Response: ".$soap->__getLastResponseHeaders( )."\n", FILE_APPEND );
  1568. file_put_contents( JPATH_BASE."/plugins/system/gtranslate/debug.txt", $soap->__getLastResponse( )."\n", FILE_APPEND );
  1569. }
  1570. return $result;
  1571. }
  1572. if ( in_array( $lang, array( "hy", "az", "eu", "ur", "ka", "ta", "te", "bn", "gu", "kn" ) ) || JRequest::getvar( "api_key" ) == "" )
  1573. {
  1574. $params = "client=a";
  1575. $params .= "&text=".urlencode( $text );
  1576. $params .= "&tl=".$lang;
  1577. $params .= "&sl=".( JRequest::getint( "auto_detect", 0 ) ? "auto" : JRequest::getvar( "main_lang" ) );
  1578. $ch = curl_init( );
  1579. curl_setopt( $ch, CURLOPT_URL, "http://translate.google.com/translate_a/t?".$params );
  1580. if ( JRequest::getint( "debug_mode" ) == 2 )
  1581. {
  1582. $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
  1583. curl_setopt( $ch, CURLOPT_VERBOSE, true );
  1584. curl_setopt( $ch, CURLOPT_STDERR, $fh );
  1585. }
  1586. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1587. curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0" );
  1588. $result = curl_exec( $ch );
  1589. curl_close( $ch );
  1590. if ( JRequest::getint( "debug_mode" ) == 2 )
  1591. {
  1592. fwrite( $fh, "Request: {$params}\n\nResponse: {$result}\n" );
  1593. fclose( $fh );
  1594. }
  1595. $result = json_decode( $result );
  1596. $return = new stdClass( );
  1597. $translation = "";
  1598. $i = 0;
  1599. while ( isset( $result->sentences[$i] ) )
  1600. {
  1601. $translation .= $result->sentences[$i]->trans;
  1602. ++$i;
  1603. }
  1604. if ( $translation != "" )
  1605. {
  1606. $return->data->translations[0]->translatedText = $translation;
  1607. }
  1608. return $return;
  1609. }
  1610. $params = "key=".JRequest::getvar( "api_key" );
  1611. $params .= "&q=".urlencode( $text );
  1612. $params .= "&userip=".$_SERVER['REMOTE_ADDR'];
  1613. if ( JRequest::getint( "auto_detect", 0 ) == 0 )
  1614. {
  1615. $params .= "&source=".JRequest::getvar( "main_lang" );
  1616. }
  1617. $params .= "&target=".$lang;
  1618. $ch = curl_init( );
  1619. curl_setopt( $ch, CURLOPT_URL, "https://www.googleapis.com/language/translate/v2" );
  1620. if ( JRequest::getint( "debug_mode" ) == 2 )
  1621. {
  1622. $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
  1623. curl_setopt( $ch, CURLOPT_VERBOSE, true );
  1624. curl_setopt( $ch, CURLOPT_STDERR, $fh );
  1625. }
  1626. curl_setopt( $ch, CURLOPT_HTTPHEADER, array( "X-HTTP-Method-Override: GET" ) );
  1627. curl_setopt( $ch, CURLOPT_POST, true );
  1628. curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
  1629. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1630. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, ( string )JRequest::getint( "ssl_verify_peer" ) );
  1631. $result = curl_exec( $ch );
  1632. curl_close( $ch );
  1633. if ( JRequest::getint( "debug_mode" ) == 2 )
  1634. {
  1635. fwrite( $fh, "Request: {$params}\n\nResponse: {$result}\n" );
  1636. fclose( $fh );
  1637. }
  1638. return json_decode( $result );
  1639. }
  1640.  
  1641. public static function gtranslate( $text )
  1642. {
  1643. if ( trim( str_replace( " ", " ", $text ) ) == "" || strlen( $text ) < 2 )
  1644. {
  1645. return $text;
  1646. }
  1647. if ( JRequest::getint( "debug_mode" ) == 1 )
  1648. {
  1649. return "%".$text."%";
  1650. }
  1651. $session =& JFactory::getsession( );
  1652. $lang = JRequest::getvar( "glang", $session->get( "glang" ) );
  1653. if ( $lang == JRequest::getvar( "main_lang" ) )
  1654. {
  1655. return $text;
  1656. }
  1657. $md5 = md5( $text );
  1658. $limit_exceeded_time = ( integer )file_get_contents( JPATH_ROOT."/plugins/system/gtranslate/cache/limit.txt" );
  1659. $file_name = JPATH_ROOT."/plugins/system/gtranslate/cache/".$lang."/".$md5;
  1660. $is_human = false;
  1661. if ( file_exists( $file_name.".human" ) )
  1662. {
  1663. $file_name .= ".human";
  1664. $is_human = true;
  1665. }
  1666. if ( $is_human || time( ) - filemtime( $file_name ) < JRequest::getint( "cache_time" ) * 24 * 60 * 60 )
  1667. {
  1668. return file_get_contents( $file_name );
  1669. }
  1670. $result = false;
  1671. if ( 5 * 60 < time( ) - $limit_exceeded_time || JRequest::getint( "force_translation", 0 ) )
  1672. {
  1673. $result = self::get_translation( $text, $lang );
  1674. if ( !isset( $result->TranslateResult ) && !isset( $result->data->translations[0]->translatedText ) )
  1675. {
  1676. file_put_contents( "cache/limit.txt", time( ) );
  1677. if ( JRequest::getint( "debug_mode" ) == 2 )
  1678. {
  1679. JError::raisewarning( 503, "Translation API limit exceeded. Will try again in 5 minutes." );
  1680. }
  1681. return $text;
  1682. }
  1683. if ( isset( $result->data->translations[0]->translatedText ) )
  1684. {
  1685. file_put_contents( $file_name, $result->data->translations[0]->translatedText );
  1686. return $result->data->translations[0]->translatedText;
  1687. }
  1688. if ( isset( $result->TranslateResult ) )
  1689. {
  1690. file_put_contents( $file_name, $result->TranslateResult );
  1691. return $result->TranslateResult;
  1692. }
  1693. return $text;
  1694. }
  1695. return $text;
  1696. }
  1697.  
  1698. public static function translate( $e )
  1699. {
  1700. $user =& JFactory::getuser( );
  1701. $session =& JFactory::getsession( );
  1702. $doc =& JFactory::getdocument( );
  1703. $lang = JRequest::getvar( "glang", $session->get( "glang" ) );
  1704. $can_edit = JRequest::getint( "language_edit", 0 ) && $user->authorize( "com_content", "edit", "content", "all" );
  1705. if ( JRequest::getint( "language_edit", 0 ) && !$user->authorize( "com_content", "edit", "content", "all" ) )
  1706. {
  1707. global $mainframe;
  1708. $return = JURI::base( )."index.php?option=com_user&view=login";
  1709. $return .= "&return=".base64_encode( JURI::getinstance( )->toString( array( "scheme", "host", "port", "path", "query" ) ) );
  1710. $mainframe->redirect( $return );
  1711. }
  1712. else
  1713. {
  1714. $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=\"##\">" : "";
  1715. $suffix = $can_edit ? "</span>" : "";
  1716. $meta_desc_orig = $doc->getDescription( );
  1717. $meta_keys_orig = $doc->getMetaData( "keywords" );
  1718. $page_title_orig = $doc->getTitle( );
  1719. switch ( $e->tag )
  1720. {
  1721. case "html" :
  1722. if ( $lang == "ar" || $lang == "iw" || $lang == "fa" || $lang == "ur" )
  1723. {
  1724. $e->setAttribute( "dir", "rtl" );
  1725. }
  1726. break;
  1727. case "meta" :
  1728. do
  1729. {
  1730. if ( !( $e->getAttribute( "name" ) == "description" ) )
  1731. {
  1732. break;
  1733. }
  1734. else
  1735. {
  1736. $meta_desc_orig = $e->getAttribute( "content" );
  1737. $e->setAttribute( "content", self::gtranslate( $meta_desc_orig ) );
  1738. }
  1739. } while ( 0 );
  1740. if ( $e->getAttribute( "name" ) == "keywords" )
  1741. {
  1742. $meta_keys_orig = $e->getAttribute( "content" );
  1743. $e->setAttribute( "content", self::gtranslate( $meta_keys_orig ) );
  1744. }
  1745. break;
  1746. case "text" :
  1747. do
  1748. {
  1749. $p = $e->parent( );
  1750. while ( $p->tag != "body" )
  1751. {
  1752. if ( preg_match( "/notranslate/", $p->getAttribute( "class" ) ) || $p->tag == "script" || $p->tag == "style" )
  1753. {
  1754. break;
  1755. }
  1756. $p = $p->parent( );
  1757. }
  1758. if ( !( $e->parent( )->tag != "title" ) )
  1759. {
  1760. break;
  1761. }
  1762. else
  1763. {
  1764. $e->innertext = str_replace( trim( $e->innertext ), str_replace( array( "%%", "##" ), array(
  1765. htmlspecialchars( trim( $e->innertext ) ),
  1766. md5( trim( $e->innertext ) )
  1767. ), $prefix ).self::gtranslate( trim( $e->innertext ) ).$suffix, $e->innertext );
  1768. }
  1769. } while ( 0 );
  1770. $e->innertext = self::gtranslate( $page_title_orig );
  1771. break;
  1772. case "body" :
  1773. do
  1774. {
  1775. if ( !$can_edit )
  1776. {
  1777. break;
  1778. }
  1779. else
  1780. {
  1781. $p = $e->parent( );
  1782. $metas = $p->getElementsByTagName( "meta" );
  1783. $title = $p->getElementByTagName( "title" );
  1784. $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>";
  1785. foreach ( $metas as $meta )
  1786. {
  1787. if ( $meta->getAttribute( "name" ) == "description" )
  1788. {
  1789. $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>";
  1790. }
  1791. else if ( $meta->getAttribute( "name" ) == "keywords" )
  1792. {
  1793. $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>";
  1794. }
  1795. }
  1796. }
  1797. $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>";
  1798. $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;
  1799. } while ( 0 );
  1800. break;
  1801. case "img" :
  1802. case "td" :
  1803. case "li" :
  1804. if ( preg_match( "/notranslate/", $e->getAttribute( "class" ) ) )
  1805. {
  1806. break;
  1807. }
  1808. if ( $e->hasAttribute( "title" ) )
  1809. {
  1810. $e->setAttribute( "title", self::gtranslate( $e->getAttribute( "title" ) ) );
  1811. }
  1812. break;
  1813. case "a" :
  1814. }
  1815. if ( $e->hasAttribute( "title" ) )
  1816. {
  1817. $e->setAttribute( "title", self::gtranslate( $e->getAttribute( "title" ) ) );
  1818. }
  1819. if ( !preg_match( "/nturl/", $e->getAttribute( "class" ) ) )
  1820. {
  1821. $href = $e->getAttribute( "href" );
  1822. $href = str_replace( "://".$_SERVER['HTTP_HOST'], "://".$_SERVER['HTTP_HOST']."/".$lang, $href );
  1823. $href = isset( $href[0] ) && $href[0] == "/" ? "/".$lang.$href : $href;
  1824. $href = str_replace( "/".$lang."/".$lang, "/".$lang, $href );
  1825. $e->setAttribute( "href", $href );
  1826. }
  1827. if ( $can_edit )
  1828. {
  1829. $e->setAttribute( "onclick", "return false;".$e->getAttribute( "onclick" ) );
  1830. }
  1831. break;
  1832. break;
  1833. }
  1834. }
  1835.  
  1836. private function checkLicense( )
  1837. {
  1838. $domain = preg_replace( "/^www\\./", "", $_SERVER['HTTP_HOST'] );
  1839. $time = time( );
  1840. 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 ) )
  1841. {
  1842. $data = array(
  1843. "domain" => $domain,
  1844. "time" => $time,
  1845. "mcrypt" => ( integer )extension_loaded( "mcrypt" )
  1846. );
  1847. $ch = curl_init( );
  1848. curl_setopt( $ch, CURLOPT_URL, "http://license.gtranslate.net/" );
  1849. curl_setopt( $ch, CURLOPT_POST, true );
  1850. curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
  1851. if ( JRequest::getint( "debug_mode" ) == 2 )
  1852. {
  1853. $fh = fopen( JPATH_BASE."/plugins/system/gtranslate/debug.txt", "a" );
  1854. curl_setopt( $ch, CURLOPT_VERBOSE, true );
  1855. curl_setopt( $ch, CURLOPT_STDERR, $fh );
  1856. }
  1857. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  1858. $result = curl_exec( $ch );
  1859. file_put_contents( JPATH_ROOT."/plugins/system/gtranslate/license.dat", $result );
  1860. }
  1861. $result = file_get_contents( JPATH_ROOT."/plugins/system/gtranslate/license.dat" );
  1862. if ( strlen( $result ) < 200 )
  1863. {
  1864. return false;
  1865. }
  1866. $result = explode( "\r\n", $result );
  1867. unset( $result[0] );
  1868. array_pop( $result );
  1869. $result = implode( "", $result );
  1870. $license = $this->decrypt( $result, extension_loaded( "mcrypt" ) );
  1871. if ( $license['domain'] != $domain )
  1872. {
  1873. return false;
  1874. }
  1875. if ( $time < $license['start_date'] || $license['end_date'] < $time )
  1876. {
  1877. return false;
  1878. }
  1879. return true;
  1880. }
  1881.  
  1882. private function decrypt( $str, $mcrypt = false )
  1883. {
  1884. $str = base64_decode( base64_decode( $str ) );
  1885. $key = "YmUzYWM2sNGU24NNA363cA1IDSDFGDFGB5aVi38BDFGQ3YNO36ycDFGAATq4sYmSFVDFGDFGps7XDYEzGDDw96OfWW3kjCFA8M+UV2kHe1WTTEcM09UMAA8";
  1886. if ( $mcrypt && extension_loaded( "mcrypt" ) )
  1887. {
  1888. $td = mcrypt_module_open( "blowfish", "", "ecb", "" );
  1889. $iv = mcrypt_create_iv( mcrypt_enc_get_iv_size( $td ), MCRYPT_RAND );
  1890. $key = substr( $key, 0, mcrypt_enc_get_key_size( $td ) );
  1891. mcrypt_generic_init( $td, $key, $iv );
  1892. $decrypt = mdecrypt_generic( $td, $str );
  1893. mcrypt_generic_deinit( $td );
  1894. mcrypt_module_close( $td );
  1895. }
  1896. else
  1897. {
  1898. $decrypt = "";
  1899. $i = 1;
  1900. while ( $i <= strlen( $str ) )
  1901. {
  1902. $char = substr( $str, $i - 1, 1 );
  1903. $keychar = substr( $key, $i % strlen( $key ) - 1, 1 );
  1904. $char = chr( ord( $char ) - ord( $keychar ) );
  1905. $decrypt .= $char;
  1906. ++$i;
  1907. }
  1908. }
  1909. return unserialize( $decrypt );
  1910. }
  1911.  
  1912. public function doTranslate( &$content )
  1913. {
  1914. $session =& JFactory::getsession( );
  1915. if ( $this->_main_lang == JRequest::getvar( "glang", $session->get( "glang", $this->_main_lang ) ) )
  1916. {
  1917. return;
  1918. }
  1919. if ( !$this->checkLicense( ) )
  1920. {
  1921. 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>.";
  1922. exit( );
  1923. }
  1924. if ( !extension_loaded( "json" ) )
  1925. {
  1926. JError::raisewarning( 410, "PHP JSON library is missing which is required by GTranslate Pro." );
  1927. }
  1928. else if ( JRequest::getvar( "bing_api_key" ) != "" && !extension_loaded( "soap" ) )
  1929. {
  1930. JError::raisewarning( 411, "PHP Soap client is required to use Bing Translator." );
  1931. }
  1932. else if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache" ) || !is_writable( JPATH_ROOT."/plugins/system/gtranslate/cache/en" ) )
  1933. {
  1934. JError::raisewarning( 412, "/plugins/system/gtranslate/cache and XX language folders should be writable (777 permission) - GTranslate Pro." );
  1935. }
  1936. else if ( !is_writable( JPATH_ROOT."/plugins/system/gtranslate/license.dat" ) )
  1937. {
  1938. JError::raisewarning( 413, "/plugins/system/gtranslate/license.dat file should be writable (666 permission) - GTranslate Pro." );
  1939. }
  1940. else
  1941. {
  1942. $user =& JFactory::getuser( );
  1943. $html = str_get_html( $content );
  1944. $html->set_callback( "plgSystemGTranslate::translate" );
  1945. if ( JRequest::getint( "language_edit", 0 ) && $user->authorize( "com_content", "edit", "content", "all" ) )
  1946. {
  1947. $main_lang = JRequest::getvar( "main_lang" );
  1948. $current_lang = JRequest::getvar( "glang", $session->get( "glang", "" ) );
  1949. $popup = "<div id=\"light\" class=\"white_content\"></div><div id=\"fade\" class=\"black_overlay\"></div>";
  1950. $style = "<style type=\"text/css\">";
  1951. $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);}";
  1952. $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;}";
  1953. $style .= ".outerbox {z-index:24000 !important;}";
  1954. $style .= "img.gicons {cursor:pointer;}";
  1955. $style .= "img.gicons:hover {position:relative;top:-1px;left:-1px;}";
  1956. $style .= "</style>";
  1957. $url_base = JURI::base( true );
  1958. $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