You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.9 KiB

  1. <?php
  2. //~ Ajax only
  3. if(!array_key_exists('isAjax', $_POST) || $_POST['isAjax'] !== "true"){
  4. exit('No ajax');
  5. }
  6. //~ Check token
  7. session_start();
  8. if(empty($_SESSION['csrf_token'])){
  9. $_SESSION['csrf_token'] = uniqid(mt_rand(), true);
  10. }
  11. if(!array_key_exists('t', $_POST) || !empty($_POST['t'])){
  12. if ($_POST['t'] !== $_SESSION['csrf_token']) {
  13. //~ Wrong token
  14. $_d['sucess'] = false;
  15. $_d['message'] = "Problème lors de la vérification du token";
  16. header('Content-type: text/json');
  17. echo json_encode($_d);
  18. exit;
  19. }
  20. }else {
  21. //~ No token
  22. $_d['sucess'] = false;
  23. $_d['message'] = "Problème de token";
  24. header('Content-type: text/json');
  25. echo json_encode($_d);
  26. exit;
  27. }
  28. //~ Include RSS Fusion
  29. require_once 'vendor/RSS-Fusion/index.php';
  30. //~ Array for return
  31. $_d = array(
  32. 'sucess' => false,
  33. 'message' => "",
  34. 'data' => array()
  35. );
  36. //var_dump($_POST);
  37. //~ Check data
  38. if(array_key_exists('flux', $_POST) && !empty($_POST['flux'])){
  39. //~ Check if feeds are valid
  40. $oFeeds = new FeedReader($_POST['flux']);
  41. if(!is_null($oFeeds->objParseFeed)){
  42. //~ Valid !
  43. //~ Set <link> RSS
  44. $rss_link = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1)) || $_SERVER['SSL'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
  45. if(array_key_exists('link', $_POST) && !empty($_POST['link'])){
  46. $rss_link = htmlspecialchars($_POST['link'], ENT_QUOTES, Config::get('characterSet'));
  47. }
  48. //~ Set <title> RSS
  49. $rss_title = "";
  50. if(array_key_exists('titre', $_POST) && !empty($_POST['titre'])){
  51. $rss_title = htmlspecialchars($_POST['titre'], ENT_QUOTES, Config::get('characterSet'));
  52. }
  53. //~ Set <description> RSS
  54. $rss_description = "";
  55. if(array_key_exists('desc', $_POST) && !empty($_POST['desc'])){
  56. $rss_description = htmlspecialchars($_POST['desc'], ENT_QUOTES, Config::get('characterSet'));
  57. }
  58. //~ Get param for all feeds
  59. $_links = array();
  60. foreach($_POST['flux'] as $index => $link){
  61. if(!empty($link)){
  62. array_push(
  63. $_links,
  64. array(
  65. 'flux' => $link,
  66. 'filter' => $_POST['filter'][$index],
  67. 'where' => $_POST['where'][$index],
  68. 'words' => $_POST['words'][$index]
  69. )
  70. );
  71. }
  72. }
  73. //~ Config is ok ! Storage
  74. $conf_storage = json_encode(array(
  75. 'flux' => $_links,
  76. 'link' => $rss_link,
  77. 'title' => $rss_title,
  78. 'description' => $rss_description
  79. ));
  80. $conf_name = str_replace('.', '', uniqid(mt_rand(), true));
  81. $path = TL_ROOT . '/../../c/';
  82. if(file_put_contents($path.$conf_name, $conf_storage)){
  83. //~ Generate link to return
  84. $_d['sucess'] = true;
  85. $_d['message'] = "Voici le lien RSS relatif à votre configuration";
  86. $_d['data'] = array(
  87. 'file' => './flux.php?c='.$conf_name,
  88. '_' => $_POST
  89. );
  90. //~ Delete old file conf
  91. if ($handle = opendir($path)){
  92. //~ Loop through the directory
  93. while (false !== ($file = readdir($handle))){
  94. //~ Check the file we're doing is actually a file
  95. if (is_file($path.$file) && $file !== 'index.php' && $file !== '.gitignore'){
  96. //~ Check if the file is older than 30 days old
  97. if (filemtime($path.$file) < (time() - (30 * 24 * 60 * 60))){ //~ 3 days
  98. //~ Do the deletion
  99. unlink($path.$file);
  100. }
  101. }
  102. }
  103. }
  104. }else{
  105. //~ Error
  106. $_d['sucess'] = false;
  107. $_d['message'] = "Une erreur est survenue lors de l'enregistrement de votre configuration";
  108. }
  109. }else{
  110. $_d['sucess'] = false;
  111. $_d['message'] = "Une erreur est survenue lors du chargement de flux RSS";
  112. }
  113. }else{
  114. //~ No data
  115. $_d['sucess'] = false;
  116. $_d['message'] = "Merci de renseigner au moins une URL de flux RSS";
  117. }
  118. header('Content-type: text/json');
  119. echo json_encode($_d);
  120. exit;