PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/e107_plugins/import/providers/rss_import_class.php

https://github.com/CasperGemini/e107
PHP | 415 lines | 227 code | 102 blank | 86 comment | 21 complexity | b19e94d230753ff102ea10109a30dfe9 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2009 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. *
  10. *
  11. * $Source: /cvs_backup/e107_0.8/e107_plugins/import/wordpress_import_class.php,v $
  12. * $Revision: 11315 $
  13. * $Date: 2010-02-10 10:18:01 -0800 (Wed, 10 Feb 2010) $
  14. * $Author: secretr $
  15. */
  16. // This must be an incredibly pointless file! But it does allow testing of the basic plugin structure.
  17. // Each import file has an identifier which must be the same for:
  18. // a) This file name - add '_class.php' to get the file name
  19. // b) The array index of certain variables
  20. // Array element key defines the function prefix and the class name; value is displayed in drop-down selection box
  21. require_once('import_classes.php');
  22. class rss_import extends base_import_class
  23. {
  24. public $title = 'RSS';
  25. public $description = 'Import content via RSS v2.0 feeds from virtually any website.';
  26. public $supported = array('news','page','links');
  27. public $mprefix = false;
  28. public $sourceType = 'rss';
  29. var $feedUrl = null;
  30. var $defaultClass = false;
  31. function init()
  32. {
  33. $this->feedUrl = vartrue($_POST['rss_feed'],false);
  34. $this->saveImages = vartrue($_POST['rss_saveimages'],false);
  35. }
  36. function config()
  37. {
  38. $frm = e107::getForm();
  39. $var[0]['caption'] = "Feed URL";
  40. $var[0]['html'] = "<input class='tbox span7' type='text' name='rss_feed' size='180' value='{$_POST['rss_feed']}' maxlength='250' />";
  41. $var[1]['caption'] = "Save Images Locally";
  42. $var[1]['html'] = $frm->checkbox('rss_saveimages',1);
  43. return $var;
  44. }
  45. // Set up a query for the specified task.
  46. // Returns TRUE on success. FALSE on error
  47. function setupQuery($task, $blank_user=FALSE)
  48. {
  49. $mes = e107::getMessage();
  50. $this->arrayData = array();
  51. $xml = e107::getXml();
  52. $file = $this->feedUrl;
  53. $mes->addDebug("rss_import::setupQuery - \$task: ".$task);
  54. $mes->addDebug("rss_import::setupQuery - \$file: ".$file);
  55. switch ($task)
  56. {
  57. case 'news' :
  58. case 'page' :
  59. case 'links' :
  60. // $rawData = $xml->getRemoteFile($file);
  61. // print_a($rawData);
  62. $array = $xml->loadXMLfile($file,'advanced');
  63. // $mes->addDebug("rss - setupQuery - RSS array: ".print_a($array,true));
  64. if ($array === FALSE || $file === FALSE)
  65. {
  66. $mes->addError("No data returned from : ".$file);
  67. return FALSE;
  68. }
  69. foreach($array['channel']['item'] as $val)
  70. {
  71. $this->arrayData[] = $val;
  72. }
  73. $this->arrayData = array_reverse($this->arrayData); // most recent last.
  74. reset($this->arrayData);
  75. break;
  76. default :
  77. return FALSE;
  78. }
  79. $this->copyUserInfo = !$blank_user;
  80. $this->currentTask = $task;
  81. return TRUE;
  82. }
  83. //------------------------------------
  84. // Internal functions below here
  85. //------------------------------------
  86. /**
  87. * Align source data to e107 User Table
  88. * @param $target array - default e107 target values for e107_user table.
  89. * @param $source array - WordPress table data
  90. */
  91. function copyUserData(&$target, &$source)
  92. {
  93. }
  94. /**
  95. * Align source data with e107 News Table
  96. * @param $target array - default e107 target values for e107_news table.
  97. * @param $source array - RSS data
  98. */
  99. function copyNewsData(&$target, &$source)
  100. {
  101. if(!$content = $this->process('content_encoded',$source))
  102. {
  103. $body = $this->process('description',$source);
  104. }
  105. else
  106. {
  107. $body = $content;
  108. }
  109. $body = $this->saveImages($body,'news');
  110. $keywords = $this->process('category',$source);
  111. if(!vartrue($source['title'][0]))
  112. {
  113. list($title,$newbody) = explode("<br />",$body,2);
  114. $title = strip_tags($title);
  115. if(trim($newbody)!='')
  116. {
  117. $body = $newbody;
  118. }
  119. }
  120. else
  121. {
  122. $title = $source['title'][0];
  123. }
  124. $target['news_title'] = $title;
  125. // $target['news_sef'] = $source['post_name'];
  126. $target['news_body'] = "[html]".$body."[/html]";
  127. // $target['news_extended'] = '';
  128. $target['news_meta_keywords'] = implode(",",$keywords);
  129. // $target['news_meta_description'] = '';
  130. $target['news_datestamp'] = strtotime($source['pubDate'][0]);
  131. // $target['news_author'] = $source['post_author'];
  132. // $target['news_category'] = '';
  133. // $target['news_allow_comments'] = ($source['comment_status']=='open') ? 1 : 0;
  134. // $target['news_start'] = '';
  135. // $target['news_end'] = '';
  136. /// $target['news_class'] = '';
  137. // $target['news_render_type'] = '';
  138. // $target['news_comment_total'] = $source['comment_count'];
  139. // $target['news_summary'] = $source['post_excerpt'];
  140. // $target['news_thumbnail'] = '';
  141. // $target['news_sticky'] = '';
  142. return $target; // comment out to debug
  143. $this->renderDebug($source,$target);
  144. // DEBUG INFO BELOW.
  145. }
  146. function process($type='description',$source)
  147. {
  148. switch ($type)
  149. {
  150. case 'category':
  151. $keywords = array();
  152. if(is_array(varset($source['category'][0])))
  153. {
  154. foreach($source['category'] as $val)
  155. {
  156. if(varset($val['@value']))
  157. {
  158. $keywords[] = $val['@value'];
  159. }
  160. }
  161. return $keywords;
  162. }
  163. elseif(is_array(varset($source['category'])))
  164. {
  165. foreach($source['category'] as $val)
  166. {
  167. if(varset($val) && is_string($val))
  168. {
  169. $keywords[] = $val;
  170. }
  171. }
  172. return $keywords;
  173. }
  174. break;
  175. default:
  176. return varset($source[$type][0]);
  177. break;
  178. }
  179. }
  180. /**
  181. * Align source data to e107 Page Table
  182. * @param $target array - default e107 target values for e107_page table.
  183. * @param $source array - WordPress table data
  184. */
  185. function copyPageData(&$target, &$source)
  186. {
  187. $body = $this->saveImages($source['description'][0],'page');
  188. // $target['page_id'] = $source['ID']; // auto increment
  189. $target['page_title'] = $source['title'][0];
  190. // $target['page_sef'] = $source['post_name'];
  191. $target['page_text'] = "[html]".$body."[/html]";
  192. // $target['page_metakeys'] = '';
  193. // $target['page_metadscr'] = '';
  194. $target['page_datestamp'] = strtotime($source['pubDate'][0]);
  195. // $target['page_author'] = $source['post_author'];
  196. // $target['page_category'] = '',
  197. // $target['page_comment_flag'] = ($source['comment_status']=='open') ? 1 : 0;
  198. // $target['page_password'] = $source['post_password'];
  199. return $target; // comment out to debug
  200. // DEBUG INFO BELOW.
  201. $this->renderDebug($source,$target);
  202. }
  203. /**
  204. * Align source data to e107 Links Table
  205. * @param $target array - default e107 target values for e107_links table.
  206. * @param $source array - WordPress table data
  207. */
  208. function copyLinksData(&$target, &$source)
  209. {
  210. $tp = e107::getParser();
  211. // $target['page_id'] = $source['ID']; // auto increment
  212. $target['link_name'] = $source['title'][0];
  213. $target['link_url'] = $source['link'][0];
  214. // $target['link_description'] = "[html]".$source['post_content']."[/html]";
  215. // $target['link_button'] = '';
  216. // $target['link_category'] = '';
  217. // $target['link_order'] = strtotime($source['post_date']);
  218. // $target['link_parent'] = $source['post_author'];
  219. // $target['link_open'] = '';
  220. // $target['link_class'] = '';
  221. // $target['link_sefurl'] = $source['post_password'];
  222. return $target; // comment out to debug
  223. $this->renderDebug($source,$target);
  224. }
  225. /** Download and Import remote images and update body text with local relative-links. eg. {e_MEDIA}
  226. * @param returns text-body with remote links replaced with local ones for the images downloaded.
  227. */
  228. function saveImages($body,$cat='news')
  229. {
  230. $mes = e107::getMessage();
  231. $med = e107::getMedia();
  232. $tp = e107::getParser();
  233. $search = array();
  234. $replace = array();
  235. $fl = e107::getFile();
  236. $result = $tp->getTags($body, 'img');
  237. if($result)
  238. {
  239. $relPath = 'images/'. substr(md5($this->feedUrl),0,10);
  240. if(!is_dir(e_MEDIA.$relPath))
  241. {
  242. mkdir(e_MEDIA.$relPath,'0755');
  243. }
  244. foreach($result['img'] as $att)
  245. {
  246. $filename = basename($att['src']);
  247. if(file_exists(e_MEDIA.$relPath."/".$filename))
  248. {
  249. continue;
  250. }
  251. $fl->getRemoteFile($att['src'], $relPath."/".$filename, 'media');
  252. if(filesize(e_MEDIA.$relPath."/".$filename) > 0)
  253. {
  254. $search[] = $att['src'];
  255. $replace[] = $tp->createConstants(e_MEDIA.$relPath."/".$filename,1);
  256. }
  257. }
  258. }
  259. else
  260. {
  261. $mes->addDebug("No Images Found: ".print_a($result,true));
  262. }
  263. if(count($search))
  264. {
  265. $mes->addDebug("Found: ".print_a($search,true));
  266. $mes->addDebug("Replaced: ".print_a($replace,true));
  267. $med->import($cat,e_MEDIA.$relPath);
  268. }
  269. return str_replace($search,$replace,$body);
  270. // echo htmlentities($body);
  271. preg_match_all("/(((http:\/\/www)|(http:\/\/)|(www))[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)\.(jpg|jpeg|gif|png|svg)/im",$body,$matches);
  272. $fl = e107::getFile();
  273. if(is_array($matches[0]))
  274. {
  275. $relPath = 'images/'. substr(md5($this->feedUrl),0,10);
  276. if(!is_dir(e_MEDIA.$relPath))
  277. {
  278. mkdir(e_MEDIA.$relPath,'0755');
  279. }
  280. foreach($matches[0] as $link)
  281. {
  282. $filename = basename($link);
  283. if(file_exists($relPath."/".$filename))
  284. {
  285. continue;
  286. }
  287. $fl->getRemoteFile($link,$relPath."/".$filename, 'media');
  288. $search[] = $link;
  289. $replace[] = $tp->createConstants(e_MEDIA.$relPath."/".$filename,1);
  290. }
  291. }
  292. if(count($search))
  293. {
  294. $med->import($cat,e_MEDIA.$relPath);
  295. }
  296. return str_replace($search,$replace,$body);
  297. }
  298. function renderDebug($source,$target)
  299. {
  300. // echo print_a($target);
  301. // return;
  302. echo "
  303. <div style='width:1000px'>
  304. <table style='width:100%'>
  305. <tr>
  306. <td style='width:500px;padding:10px'>".print_a($source,TRUE)."</td>
  307. <td style='border-left:1px solid black;padding:10px'>".print_a($target,TRUE)."</td>
  308. </tr>
  309. </table>
  310. </div>";
  311. }
  312. }
  313. ?>