PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/GetNews/modules/getnews/admin/Library.php

http://mhst11-14.googlecode.com/
PHP | 201 lines | 196 code | 5 blank | 0 comment | 13 complexity | 587e4169e6b274648ca6edbf86e82564 MD5 | raw file
  1. <?php
  2. class xmlLibrary
  3. {
  4. function createNode($xml,$nodecha,$element) //tao node trong file xml
  5. {
  6. if($element->nodeType!=4&&$element->nodeType!=8)
  7. {
  8. $nodename=$element->nodeName;
  9. $node=$xml->createElement($nodename,"");
  10. $node->setAttribute("cha",$nodecha->nodeName);
  11. $node->setAttribute("vtcha",$nodecha->getAttribute("vt"));
  12. $count=1;
  13. foreach($nodecha->childNodes as $item)
  14. {if($item->nodeName==$nodename)
  15. $count++;
  16. }
  17. $node->setAttribute("vt",$count);
  18. $nodecha->appendChild($node);
  19. if($element->hasChildNodes())
  20. {
  21. foreach($element->childNodes as $item)
  22. {
  23. if($item->nodeName!="#text")
  24. {
  25. $xmllib=new xmlLibrary();
  26. $xmllib->createNode($xml,$node,$item);
  27. $count++;
  28. }
  29. }
  30. }
  31. $i++;
  32. }
  33. }
  34. function CreateXML($url,$fileName) //tao file XML
  35. {
  36. $file= file_get_contents($url);
  37. $file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8");
  38. $file=preg_replace('/\s+/'," ",$file);
  39. $doc=new DOMDocument();
  40. $doc->encoding="utf-8";
  41. @$doc->loadHTML($file);
  42. $doc->saveHTML();
  43. $xmldoc=new DOMDocument();
  44. $xmldoc->encoding="utf-8";
  45. $xmldoc->formatOutput=true;
  46. $RootNode=$doc->documentElement;
  47. $root=$xmldoc->createElement($RootNode->nodeName,"");
  48. $root->setAttribute("cha",$RootNode->nodeName);
  49. $root->setAttribute("vtcha","1");
  50. $root->setAttribute("vt","1");
  51. $xmldoc->appendChild($root);
  52. $node=$RootNode;
  53. foreach($node->childNodes as $element)
  54. {
  55. if($element->nodeName!="#text")
  56. {
  57. $xml=new xmlLibrary();
  58. $xml->createNode($xmldoc,$root,$element);
  59. }
  60. }
  61. $xmldoc->save($fileName);
  62. }
  63. function CreateXHTML($url,$filename) //tao file xhtml
  64. {
  65. $file=file_get_contents($url);
  66. $file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8");
  67. $file=preg_replace('/\s+/'," ",$file);
  68. $regex='/<html\s*.*?>/';
  69. $file=preg_replace($regex,'<html>',$file);
  70. $regex='/<!DOCTYPE.*?>/';
  71. $file=preg_replace($regex,'',$file);
  72. $dom=new DOMDocument();
  73. $dom->formatOutput=true;
  74. $dom->encoding='utf-8';
  75. @$dom->loadHTML($file);
  76. $dom->save($filename);
  77. }
  78. function GetNodefrom($nodeName,$cha,$vtcha,$vt,$filename) //lay 1 node t? cac thuoc tinh, cha, vtcha, vt
  79. {
  80. $dom=new DOMDocument();
  81. $dom->load($filename);
  82. $elements=$dom->getElementsByTagName('*');
  83. for($i=0;$i<$elements->length;$i++)
  84. {
  85. $attributes=new DOMNamedNodeMap();
  86. $node=$elements->item($i);
  87. $attributes=$node->attributes;
  88. $chaName=$attributes->item(0)->nodeValue;
  89. $vitricha=$attributes->item(1)->nodeValue;
  90. $vitriNode=$attributes->item(2)->nodeValue;
  91. if($node->nodeName==$nodeName&&$chaName==$cha&&$vitricha==$vtcha&&$vitriNode==$vt)
  92. return $node;
  93. }
  94. return $dom->documentElement;
  95. }
  96. function CreateXpathFrom($node,$xpath) //lay xpath tu 1 node
  97. {
  98. if($node->parentNode->nodeName!='html')
  99. {
  100. $nodeCha=$node->parentNode;
  101. $vtCha=$node->attributes->item(1)->nodeValue;
  102. $xpath=$this->CreateXpathFrom($nodeCha,$nodeCha->nodeName.'['.$vtCha.']/'.$xpath);
  103. return $xpath;
  104. }
  105. return $xpath;
  106. }
  107. function CreateXpath($nodeName,$cha,$vtcha,$vt,$filename) //lay xpath tu cac thuoc tinh cua node
  108. {
  109. $node=$this->GetNodefrom($nodeName,$cha,$vtcha,$vt,$filename);
  110. return $this->CreateXpathFrom($node,$nodeName.'['.$vt.']');
  111. }
  112. function GetValueFromXpath($xpath,$filename) //lay gia tri cua xpath
  113. {
  114. $simpleXMLElement=new SimpleXMLElement($filename,null,true);
  115. $values=$simpleXMLElement->xpath($xpath);
  116. $value="";
  117. foreach($values as $item)
  118. {
  119. $value.=$item;
  120. }
  121. return $value;
  122. }
  123. function UpdateNodeValue($dom,$fileName,$tagName,$value,$parentNode)
  124. {
  125. $oldnode=$dom->getElementsByTagName($tagName);
  126. if($oldnode->length>0)
  127. $parentNode->removeChild($oldnode->item(0));
  128. $node=$dom->createElement($tagName,$value);
  129. $parentNode->appendChild($node);
  130. $dom->save($fileName);
  131. }
  132. }
  133. class GetNews //Lop lay tin
  134. {
  135. function GetImage($content) //Lay link anh
  136. {
  137. $regex='/<\s*img.*?src="(?<linkimage>.*?)".*?>/';
  138. preg_match_all($regex,$content,$result);
  139. return $result['linkimage'];
  140. }
  141. function GetFileName($url) //Lay ten file
  142. {
  143. $filename=$url;
  144. $pos=strrpos($filename,"/");
  145. if($pos>0)
  146. $filename=substr($filename,$pos+1);
  147. return $filename;
  148. }
  149. function DownloadFile($inPath,$outPath) //DownloadFile
  150. {
  151. try
  152. {
  153. $filename=$this->GetFileName($inPath);
  154. $in= fopen($inPath, "rb");
  155. $out= fopen($outPath.$filename, "wb");
  156. while ($chunk = fread($in,8192))
  157. {
  158. fwrite($out, $chunk, 8192);
  159. }
  160. fclose($in);
  161. fclose($out);
  162. }catch(Exception $e)
  163. {
  164. }
  165. }
  166. function str_start_with($str,$needle) //Kiem tra 1 chuoi co bat dau bang chuoi $str khong
  167. {
  168. $regex="/\b".$str."/i";
  169. if(preg_match($regex,$needle)==1) return true;
  170. return false;
  171. }
  172. function ReplaceLinkImage($domain,$content,$path) //Thay doi lai toan bo link anh trong noi dung va download anh tu trang nguon
  173. {
  174. $image=$this->GetImage($content);
  175. for($i=0;$i<count($image);$i++)
  176. {
  177. $linkimage=$image[$i];
  178. $filename=$this->GetFileName($linkimage);
  179. if(!$this->str_start_with('http:\/\/',$image[$i]))
  180. $linkimage=$domain.$image[$i];
  181. $this->DownloadFile($linkimage,$path);
  182. $content=str_replace($image[$i],$path.$filename,$content);
  183. }
  184. return $content;
  185. }
  186. function GetNewsFrom($xpathTitle,$xpathHead,$xpathContent,$xpathImage) //lay tin tu cac xpath co san
  187. {
  188. $arr=array();
  189. $arr['title']=$xpathTitle;
  190. $arr['head']=$xpathHead;
  191. $arr['content']=$xpathContent;
  192. $arr['image']=$xpathImage;
  193. return $arr;
  194. }
  195. }
  196. ?>