Feed.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. class FreshRSS_Feed extends Minz_Model {
  3. const PRIORITY_MAIN_STREAM = 10;
  4. const PRIORITY_NORMAL = 0;
  5. const PRIORITY_ARCHIVED = -10;
  6. const TTL_DEFAULT = 0;
  7. const KEEP_HISTORY_DEFAULT = -2;
  8. const KEEP_HISTORY_INFINITE = -1;
  9. private $id = 0;
  10. private $url;
  11. private $category = 1;
  12. private $nbEntries = -1;
  13. private $nbNotRead = -1;
  14. private $entries = null;
  15. private $name = '';
  16. private $website = '';
  17. private $description = '';
  18. private $lastUpdate = 0;
  19. private $priority = self::PRIORITY_MAIN_STREAM;
  20. private $pathEntries = '';
  21. private $httpAuth = '';
  22. private $error = false;
  23. private $keep_history = self::KEEP_HISTORY_DEFAULT;
  24. private $ttl = self::TTL_DEFAULT;
  25. private $attributes = array();
  26. private $mute = false;
  27. private $hash = null;
  28. private $lockPath = '';
  29. private $hubUrl = '';
  30. private $selfUrl = '';
  31. public function __construct($url, $validate = true) {
  32. if ($validate) {
  33. $this->_url($url);
  34. } else {
  35. $this->url = $url;
  36. }
  37. }
  38. public static function example() {
  39. $f = new FreshRSS_Feed('http://example.net/', false);
  40. $f->faviconPrepare();
  41. return $f;
  42. }
  43. public function id() {
  44. return $this->id;
  45. }
  46. public function hash() {
  47. if ($this->hash === null) {
  48. $salt = FreshRSS_Context::$system_conf->salt;
  49. $this->hash = hash('crc32b', $salt . $this->url);
  50. }
  51. return $this->hash;
  52. }
  53. public function url($includeCredentials = true) {
  54. return $includeCredentials ? $this->url : SimplePie_Misc::url_remove_credentials($this->url);
  55. }
  56. public function selfUrl() {
  57. return $this->selfUrl;
  58. }
  59. public function hubUrl() {
  60. return $this->hubUrl;
  61. }
  62. public function category() {
  63. return $this->category;
  64. }
  65. public function entries() {
  66. return $this->entries === null ? array() : $this->entries;
  67. }
  68. public function name() {
  69. return $this->name;
  70. }
  71. public function website() {
  72. return $this->website;
  73. }
  74. public function description() {
  75. return $this->description;
  76. }
  77. public function lastUpdate() {
  78. return $this->lastUpdate;
  79. }
  80. public function priority() {
  81. return $this->priority;
  82. }
  83. public function pathEntries() {
  84. return $this->pathEntries;
  85. }
  86. public function httpAuth($raw = true) {
  87. if ($raw) {
  88. return $this->httpAuth;
  89. } else {
  90. $pos_colon = strpos($this->httpAuth, ':');
  91. $user = substr($this->httpAuth, 0, $pos_colon);
  92. $pass = substr($this->httpAuth, $pos_colon + 1);
  93. return array(
  94. 'username' => $user,
  95. 'password' => $pass
  96. );
  97. }
  98. }
  99. public function inError() {
  100. return $this->error;
  101. }
  102. public function keepHistory() {
  103. return $this->keep_history;
  104. }
  105. public function ttl() {
  106. return $this->ttl;
  107. }
  108. public function attributes($key = '') {
  109. if ($key == '') {
  110. return $this->attributes;
  111. } else {
  112. return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
  113. }
  114. }
  115. public function mute() {
  116. return $this->mute;
  117. }
  118. // public function ttlExpire() {
  119. // $ttl = $this->ttl;
  120. // if ($ttl == self::TTL_DEFAULT) { //Default
  121. // $ttl = FreshRSS_Context::$user_conf->ttl_default;
  122. // }
  123. // if ($ttl == -1) { //Never
  124. // $ttl = 64000000; //~2 years. Good enough for PubSubHubbub logic
  125. // }
  126. // return $this->lastUpdate + $ttl;
  127. // }
  128. public function nbEntries() {
  129. if ($this->nbEntries < 0) {
  130. $feedDAO = FreshRSS_Factory::createFeedDao();
  131. $this->nbEntries = $feedDAO->countEntries($this->id());
  132. }
  133. return $this->nbEntries;
  134. }
  135. public function nbNotRead() {
  136. if ($this->nbNotRead < 0) {
  137. $feedDAO = FreshRSS_Factory::createFeedDao();
  138. $this->nbNotRead = $feedDAO->countNotRead($this->id());
  139. }
  140. return $this->nbNotRead;
  141. }
  142. public function faviconPrepare() {
  143. global $favicons_dir;
  144. require_once(LIB_PATH . '/favicons.php');
  145. $url = $this->website;
  146. if ($url == '') {
  147. $url = $this->url;
  148. }
  149. $txt = $favicons_dir . $this->hash() . '.txt';
  150. if (!file_exists($txt)) {
  151. file_put_contents($txt, $url);
  152. }
  153. if (FreshRSS_Context::$isCli) {
  154. $ico = $favicons_dir . $this->hash() . '.ico';
  155. $ico_mtime = @filemtime($ico);
  156. $txt_mtime = @filemtime($txt);
  157. if ($txt_mtime != false &&
  158. ($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (14 * 86400)))) {
  159. // no ico file or we should download a new one.
  160. $url = file_get_contents($txt);
  161. download_favicon($url, $ico) || touch($ico);
  162. }
  163. }
  164. }
  165. public static function faviconDelete($hash) {
  166. $path = DATA_PATH . '/favicons/' . $hash;
  167. @unlink($path . '.ico');
  168. @unlink($path . '.txt');
  169. }
  170. public function favicon() {
  171. return Minz_Url::display('/f.php?' . $this->hash());
  172. }
  173. public function _id($value) {
  174. $this->id = $value;
  175. }
  176. public function _url($value, $validate = true) {
  177. $this->hash = null;
  178. if ($validate) {
  179. $value = checkUrl($value);
  180. }
  181. if (empty($value)) {
  182. throw new FreshRSS_BadUrl_Exception($value);
  183. }
  184. $this->url = $value;
  185. }
  186. public function _category($value) {
  187. $value = intval($value);
  188. $this->category = $value >= 0 ? $value : 0;
  189. }
  190. public function _name($value) {
  191. $this->name = $value === null ? '' : $value;
  192. }
  193. public function _website($value, $validate = true) {
  194. if ($validate) {
  195. $value = checkUrl($value);
  196. }
  197. if (empty($value)) {
  198. $value = '';
  199. }
  200. $this->website = $value;
  201. }
  202. public function _description($value) {
  203. $this->description = $value === null ? '' : $value;
  204. }
  205. public function _lastUpdate($value) {
  206. $this->lastUpdate = $value;
  207. }
  208. public function _priority($value) {
  209. $this->priority = intval($value);
  210. }
  211. public function _pathEntries($value) {
  212. $this->pathEntries = $value;
  213. }
  214. public function _httpAuth($value) {
  215. $this->httpAuth = $value;
  216. }
  217. public function _error($value) {
  218. $this->error = (bool)$value;
  219. }
  220. public function _keepHistory($value) {
  221. $value = intval($value);
  222. $value = min($value, 1000000);
  223. $value = max($value, self::KEEP_HISTORY_DEFAULT);
  224. $this->keep_history = $value;
  225. }
  226. public function _ttl($value) {
  227. $value = intval($value);
  228. $value = min($value, 100000000);
  229. $this->ttl = abs($value);
  230. $this->mute = $value < self::TTL_DEFAULT;
  231. }
  232. public function _attributes($key, $value) {
  233. if ($key == '') {
  234. if (is_string($value)) {
  235. $value = json_decode($value, true);
  236. }
  237. if (is_array($value)) {
  238. $this->attributes = $value;
  239. }
  240. } elseif ($value === null) {
  241. unset($this->attributes[$key]);
  242. } else {
  243. $this->attributes[$key] = $value;
  244. }
  245. }
  246. public function _nbNotRead($value) {
  247. $this->nbNotRead = intval($value);
  248. }
  249. public function _nbEntries($value) {
  250. $this->nbEntries = intval($value);
  251. }
  252. public function load($loadDetails = false, $noCache = false) {
  253. if ($this->url !== null) {
  254. if (CACHE_PATH === false) {
  255. throw new Minz_FileNotExistException(
  256. 'CACHE_PATH',
  257. Minz_Exception::ERROR
  258. );
  259. } else {
  260. $url = htmlspecialchars_decode($this->url, ENT_QUOTES);
  261. if ($this->httpAuth != '') {
  262. $url = preg_replace('#((.+)://)(.+)#', '${1}' . $this->httpAuth . '@${3}', $url);
  263. }
  264. $feed = customSimplePie($this->attributes());
  265. if (substr($url, -11) === '#force_feed') {
  266. $feed->force_feed(true);
  267. $url = substr($url, 0, -11);
  268. }
  269. $feed->set_feed_url($url);
  270. if (!$loadDetails) { //Only activates auto-discovery when adding a new feed
  271. $feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
  272. }
  273. Minz_ExtensionManager::callHook('simplepie_before_init', $feed, $this);
  274. $mtime = $feed->init();
  275. if ((!$mtime) || $feed->error()) {
  276. $errorMessage = $feed->error();
  277. throw new FreshRSS_Feed_Exception(
  278. ($errorMessage == '' ? 'Unknown error for feed' : $errorMessage) . ' [' . $url . ']'
  279. );
  280. }
  281. $links = $feed->get_links('self');
  282. $this->selfUrl = isset($links[0]) ? $links[0] : null;
  283. $links = $feed->get_links('hub');
  284. $this->hubUrl = isset($links[0]) ? $links[0] : null;
  285. if ($loadDetails) {
  286. // si on a utilisé l'auto-discover, notre url va avoir changé
  287. $subscribe_url = $feed->subscribe_url(false);
  288. $title = strtr(html_only_entity_decode($feed->get_title()), array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;')); //HTML to HTML-PRE //ENT_COMPAT except &
  289. $this->_name($title == '' ? $url : $title);
  290. $this->_website(html_only_entity_decode($feed->get_link()));
  291. $this->_description(html_only_entity_decode($feed->get_description()));
  292. } else {
  293. //The case of HTTP 301 Moved Permanently
  294. $subscribe_url = $feed->subscribe_url(true);
  295. }
  296. $clean_url = SimplePie_Misc::url_remove_credentials($subscribe_url);
  297. if ($subscribe_url !== null && $subscribe_url !== $url) {
  298. $this->_url($clean_url);
  299. }
  300. if (($mtime === true) || ($mtime > $this->lastUpdate) || $noCache) {
  301. //Minz_Log::debug('FreshRSS no cache ' . $mtime . ' > ' . $this->lastUpdate . ' for ' . $clean_url);
  302. $this->loadEntries($feed); // et on charge les articles du flux
  303. } else {
  304. //Minz_Log::debug('FreshRSS use cache for ' . $clean_url);
  305. $this->entries = array();
  306. }
  307. $feed->__destruct(); //http://simplepie.org/wiki/faq/i_m_getting_memory_leaks
  308. unset($feed);
  309. }
  310. }
  311. }
  312. public function loadEntries($feed) {
  313. $entries = array();
  314. $guids = array();
  315. $hasUniqueGuids = true;
  316. foreach ($feed->get_items() as $item) {
  317. $title = html_only_entity_decode(strip_tags($item->get_title()));
  318. $authors = $item->get_authors();
  319. $link = $item->get_permalink();
  320. $date = @strtotime($item->get_date());
  321. // gestion des tags (catégorie == tag)
  322. $tags_tmp = $item->get_categories();
  323. $tags = array();
  324. if ($tags_tmp !== null) {
  325. foreach ($tags_tmp as $tag) {
  326. $tags[] = html_only_entity_decode($tag->get_label());
  327. }
  328. }
  329. $content = html_only_entity_decode($item->get_content());
  330. if ($item->get_enclosures() != null) {
  331. $elinks = array();
  332. foreach ($item->get_enclosures() as $enclosure) {
  333. $elink = $enclosure->get_link();
  334. if ($elink != '' && empty($elinks[$elink])) {
  335. $content .= '<div class="enclosure">';
  336. if ($enclosure->get_title() != '') {
  337. $content .= '<p class="enclosure-title">' . $enclosure->get_title() . '</p>';
  338. }
  339. $enclosureContent = '';
  340. $elinks[$elink] = true;
  341. $mime = strtolower($enclosure->get_type());
  342. $medium = strtolower($enclosure->get_medium());
  343. if ($medium === 'image' || strpos($mime, 'image/') === 0) {
  344. $enclosureContent .= '<p class="enclosure-content"><img src="' . $elink . '" alt="" /></p>';
  345. } elseif ($medium === 'audio' || strpos($mime, 'audio/') === 0) {
  346. $enclosureContent .= '<p class="enclosure-content"><audio preload="none" src="' . $elink
  347. . '" controls="controls"></audio> <a download="" href="' . $elink . '">💾</a></p>';
  348. } elseif ($medium === 'video' || strpos($mime, 'video/') === 0) {
  349. $enclosureContent .= '<p class="enclosure-content"><video preload="none" src="' . $elink
  350. . '" controls="controls"></video> <a download="" href="' . $elink . '">💾</a></p>';
  351. } elseif ($medium != '' || strpos($mime, 'application/') === 0 || strpos($mime, 'text/') === 0) {
  352. $enclosureContent .= '<p class="enclosure-content"><a download="" href="' . $elink . '">💾</a></p>';
  353. } else {
  354. unset($elinks[$elink]);
  355. }
  356. $thumbnailContent = '';
  357. if ($enclosure->get_thumbnails() != null) {
  358. foreach ($enclosure->get_thumbnails() as $thumbnail) {
  359. if (empty($elinks[$thumbnail])) {
  360. $elinks[$thumbnail] = true;
  361. $thumbnailContent .= '<p><img class="enclosure-thumbnail" src="' . $thumbnail . '" alt="" /></p>';
  362. }
  363. }
  364. }
  365. $content .= $thumbnailContent;
  366. $content .= $enclosureContent;
  367. if ($enclosure->get_description() != '') {
  368. $content .= '<pre class="enclosure-description">' . $enclosure->get_description() . '</pre>';
  369. }
  370. $content .= "</div>\n";
  371. }
  372. }
  373. }
  374. $guid = $item->get_id(false, false);
  375. $hasUniqueGuids &= empty($guids['_' . $guid]);
  376. $guids['_' . $guid] = true;
  377. $author_names = '';
  378. if (is_array($authors)) {
  379. foreach ($authors as $author) {
  380. $author_names .= html_only_entity_decode(strip_tags($author->name == '' ? $author->email : $author->name)) . ', ';
  381. }
  382. }
  383. $author_names = substr($author_names, 0, -2);
  384. $entry = new FreshRSS_Entry(
  385. $this->id(),
  386. $guid,
  387. $title === null ? '' : $title,
  388. $author_names,
  389. $content === null ? '' : $content,
  390. $link === null ? '' : $link,
  391. $date ? $date : time()
  392. );
  393. $entry->_tags($tags);
  394. $entry->_feed($this);
  395. if ($this->pathEntries != '') {
  396. // Optionally load full content for truncated feeds
  397. $entry->loadCompleteContent();
  398. }
  399. $entries[] = $entry;
  400. unset($item);
  401. }
  402. $hasBadGuids = $this->attributes('hasBadGuids');
  403. if ($hasBadGuids != !$hasUniqueGuids) {
  404. $hasBadGuids = !$hasUniqueGuids;
  405. if ($hasBadGuids) {
  406. Minz_Log::warning('Feed has invalid GUIDs: ' . $this->url);
  407. } else {
  408. Minz_Log::warning('Feed has valid GUIDs again: ' . $this->url);
  409. }
  410. $feedDAO = FreshRSS_Factory::createFeedDao();
  411. $feedDAO->updateFeedAttribute($this, 'hasBadGuids', $hasBadGuids);
  412. }
  413. if (!$hasUniqueGuids) {
  414. foreach ($entries as $entry) {
  415. $entry->_guid('');
  416. }
  417. }
  418. $this->entries = $entries;
  419. }
  420. public function cacheModifiedTime() {
  421. return @filemtime(CACHE_PATH . '/' . md5($this->url) . '.spc');
  422. }
  423. public function lock() {
  424. $this->lockPath = TMP_PATH . '/' . $this->hash() . '.freshrss.lock';
  425. if (file_exists($this->lockPath) && ((time() - @filemtime($this->lockPath)) > 3600)) {
  426. @unlink($this->lockPath);
  427. }
  428. if (($handle = @fopen($this->lockPath, 'x')) === false) {
  429. return false;
  430. }
  431. //register_shutdown_function('unlink', $this->lockPath);
  432. @fclose($handle);
  433. return true;
  434. }
  435. public function unlock() {
  436. @unlink($this->lockPath);
  437. }
  438. //<PubSubHubbub>
  439. public function pubSubHubbubEnabled() {
  440. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  441. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  442. if ($hubFile = @file_get_contents($hubFilename)) {
  443. $hubJson = json_decode($hubFile, true);
  444. if ($hubJson && empty($hubJson['error']) &&
  445. (empty($hubJson['lease_end']) || $hubJson['lease_end'] > time())) {
  446. return true;
  447. }
  448. }
  449. return false;
  450. }
  451. public function pubSubHubbubError($error = true) {
  452. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  453. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  454. $hubFile = @file_get_contents($hubFilename);
  455. $hubJson = $hubFile ? json_decode($hubFile, true) : array();
  456. if (!isset($hubJson['error']) || $hubJson['error'] !== (bool)$error) {
  457. $hubJson['error'] = (bool)$error;
  458. file_put_contents($hubFilename, json_encode($hubJson));
  459. Minz_Log::warning('Set error to ' . ($error ? 1 : 0) . ' for ' . $url, PSHB_LOG);
  460. }
  461. return false;
  462. }
  463. public function pubSubHubbubPrepare() {
  464. $key = '';
  465. if (FreshRSS_Context::$system_conf->base_url && $this->hubUrl && $this->selfUrl && @is_dir(PSHB_PATH)) {
  466. $path = PSHB_PATH . '/feeds/' . base64url_encode($this->selfUrl);
  467. $hubFilename = $path . '/!hub.json';
  468. if ($hubFile = @file_get_contents($hubFilename)) {
  469. $hubJson = json_decode($hubFile, true);
  470. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key'])) {
  471. $text = 'Invalid JSON for PubSubHubbub: ' . $this->url;
  472. Minz_Log::warning($text);
  473. Minz_Log::warning($text, PSHB_LOG);
  474. return false;
  475. }
  476. if ((!empty($hubJson['lease_end'])) && ($hubJson['lease_end'] < (time() + (3600 * 23)))) { //TODO: Make a better policy
  477. $text = 'PubSubHubbub lease ends at '
  478. . date('c', empty($hubJson['lease_end']) ? time() : $hubJson['lease_end'])
  479. . ' and needs renewal: ' . $this->url;
  480. Minz_Log::warning($text);
  481. Minz_Log::warning($text, PSHB_LOG);
  482. $key = $hubJson['key']; //To renew our lease
  483. } elseif (((!empty($hubJson['error'])) || empty($hubJson['lease_end'])) &&
  484. (empty($hubJson['lease_start']) || $hubJson['lease_start'] < time() - (3600 * 23))) { //Do not renew too often
  485. $key = $hubJson['key']; //To renew our lease
  486. }
  487. } else {
  488. @mkdir($path, 0777, true);
  489. $key = sha1($path . FreshRSS_Context::$system_conf->salt);
  490. $hubJson = array(
  491. 'hub' => $this->hubUrl,
  492. 'key' => $key,
  493. );
  494. file_put_contents($hubFilename, json_encode($hubJson));
  495. @mkdir(PSHB_PATH . '/keys/');
  496. file_put_contents(PSHB_PATH . '/keys/' . $key . '.txt', base64url_encode($this->selfUrl));
  497. $text = 'PubSubHubbub prepared for ' . $this->url;
  498. Minz_Log::debug($text);
  499. Minz_Log::debug($text, PSHB_LOG);
  500. }
  501. $currentUser = Minz_Session::param('currentUser');
  502. if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
  503. touch($path . '/' . $currentUser . '.txt');
  504. }
  505. }
  506. return $key;
  507. }
  508. //Parameter true to subscribe, false to unsubscribe.
  509. public function pubSubHubbubSubscribe($state) {
  510. $url = $this->selfUrl ? $this->selfUrl : $this->url;
  511. if (FreshRSS_Context::$system_conf->base_url && $url) {
  512. $hubFilename = PSHB_PATH . '/feeds/' . base64url_encode($url) . '/!hub.json';
  513. $hubFile = @file_get_contents($hubFilename);
  514. if ($hubFile === false) {
  515. Minz_Log::warning('JSON not found for PubSubHubbub: ' . $this->url);
  516. return false;
  517. }
  518. $hubJson = json_decode($hubFile, true);
  519. if (!$hubJson || empty($hubJson['key']) || !ctype_xdigit($hubJson['key']) || empty($hubJson['hub'])) {
  520. Minz_Log::warning('Invalid JSON for PubSubHubbub: ' . $this->url);
  521. return false;
  522. }
  523. $callbackUrl = checkUrl(Minz_Request::getBaseUrl() . '/api/pshb.php?k=' . $hubJson['key']);
  524. if ($callbackUrl == '') {
  525. Minz_Log::warning('Invalid callback for PubSubHubbub: ' . $this->url);
  526. return false;
  527. }
  528. if (!$state) { //unsubscribe
  529. $hubJson['lease_end'] = time() - 60;
  530. file_put_contents($hubFilename, json_encode($hubJson));
  531. }
  532. $ch = curl_init();
  533. curl_setopt_array($ch, array(
  534. CURLOPT_URL => $hubJson['hub'],
  535. CURLOPT_RETURNTRANSFER => true,
  536. CURLOPT_POSTFIELDS => http_build_query(array(
  537. 'hub.verify' => 'sync',
  538. 'hub.mode' => $state ? 'subscribe' : 'unsubscribe',
  539. 'hub.topic' => $url,
  540. 'hub.callback' => $callbackUrl,
  541. )),
  542. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  543. CURLOPT_MAXREDIRS => 10,
  544. ));
  545. if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
  546. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
  547. }
  548. if (defined('CURLOPT_ENCODING')) {
  549. curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
  550. }
  551. $response = curl_exec($ch);
  552. $info = curl_getinfo($ch);
  553. Minz_Log::warning('PubSubHubbub ' . ($state ? 'subscribe' : 'unsubscribe') . ' to ' . $url .
  554. ' with callback ' . $callbackUrl . ': ' . $info['http_code'] . ' ' . $response, PSHB_LOG);
  555. if (substr($info['http_code'], 0, 1) == '2') {
  556. return true;
  557. } else {
  558. $hubJson['lease_start'] = time(); //Prevent trying again too soon
  559. $hubJson['error'] = true;
  560. file_put_contents($hubFilename, json_encode($hubJson));
  561. return false;
  562. }
  563. }
  564. return false;
  565. }
  566. //</PubSubHubbub>
  567. }