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

/retrieve.php

http://github.com/spotweb/spotweb
PHP | 298 lines | 193 code | 35 blank | 70 comment | 32 complexity | 1b5a4e52a00478bc19ac94ef6af2ad4a MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. error_reporting(2147483647);
  3. try {
  4. require_once __DIR__.'/vendor/autoload.php';
  5. /*
  6. * Initialize the Spotweb base classes
  7. */
  8. $bootstrap = new Bootstrap();
  9. list($settings, $daoFactory, $req) = $bootstrap->boot();
  10. /*
  11. * disable timing, all queries which are ran by retrieve this would make it use
  12. * large amounts of memory
  13. */
  14. SpotTiming::disable();
  15. // Initialize commandline arguments
  16. SpotCommandline::initialize(['force', 'debug', 'retro', 'timing'], ['force' => false, 'timing' => false, 'debug' => false, 'retro' => false]);
  17. // Allow for timing to be displayed after retrieval of spots
  18. $showTiming = SpotCommandline::get('timing');
  19. if ($showTiming) {
  20. SpotTiming::enable();
  21. SpotTiming::enableHtml(false);
  22. SpotTiming::disableExtra(true);
  23. } // if
  24. // Initialize translation to english
  25. SpotTranslation::initialize('en_US');
  26. /*
  27. * When retrieval is run from the webinterface, we want to make
  28. * sure this user is actually allowed to run retrieval.
  29. */
  30. $svcUserRecord = new Services_User_Record($daoFactory, $settings);
  31. $svcUserAuth = new Services_User_Authentication($daoFactory, $settings);
  32. if (!SpotCommandline::isCommandline()) {
  33. /*
  34. * An API key is required, so request it and try to
  35. * create a session with it which we can use to validate
  36. * the user with
  37. */
  38. $apiKey = $req->getDef('apikey', '');
  39. $userSession = $svcUserAuth->verifyApi($apiKey);
  40. /*
  41. * If the session failed or the the user doesn't have access
  42. * to retrieve spots, let the user know
  43. */
  44. if (($userSession === false) || (!$userSession['security']->allowed(SpotSecurity::spotsec_retrieve_spots, ''))) {
  45. throw new PermissionDeniedException(SpotSecurity::spotsec_retrieve_spots, '');
  46. } // if
  47. // Add the user's ip addres, we need it for sending notifications
  48. $userSession['session'] = ['ipaddr' => ''];
  49. } else {
  50. $userSession['user'] = $svcUserRecord->getUser(SPOTWEB_ADMIN_USERID);
  51. $userSession['security'] = new SpotSecurity(
  52. $daoFactory->getUserDao(),
  53. $daoFactory->getAuditDao(),
  54. $settings,
  55. $userSession['user'],
  56. ''
  57. );
  58. $userSession['session'] = ['ipaddr' => ''];
  59. } // if
  60. /*
  61. * We normally check whether we are not running already, because
  62. * this would mean it will mess up all sorts of things like
  63. * comment calculation, but a user can force our hand
  64. */
  65. $forceMode = SpotCommandline::get('force');
  66. /*
  67. * Do we need to debuglog this session? Generates loads of
  68. * output
  69. */
  70. $debugLog = SpotCommandline::get('debug');
  71. if ($debugLog) {
  72. SpotDebug::enable(SpotDebug::TRACE);
  73. } else {
  74. SpotDebug::disable();
  75. } // if
  76. /*
  77. * Retro mode will allow os to start from the beginning and retrieve
  78. * all spots starting from scratch
  79. */
  80. $retroMode = SpotCommandline::get('retro');
  81. /*
  82. * Retention cleanup. Basically when we ask for Spotweb to only
  83. * keep spots for 'xx' days (eg: 30 days), we either have to delete
  84. * everyting older than 'xx' days, or delete all 'full' resources
  85. * older than the specified time period.
  86. *
  87. * The full resources are everything beyond the bare minimum to
  88. * display the spots, so we delete nzb's, images, comments, etc.
  89. */
  90. if (($settings->get('retention') > 0) && (!$retroMode)) {
  91. echo 'Removing Spot information which is beyond retention period,';
  92. $spotDao = $daoFactory->getSpotDao();
  93. $cacheDao = $daoFactory->getCacheDao();
  94. $commentDao = $daoFactory->getCommentDao();
  95. switch ($settings->get('retentiontype')) {
  96. case 'everything':
  97. $spotDao->deleteSpotsRetention($settings->get('retention'));
  98. $cacheDao->expireCache($settings->get('retention'));
  99. // case everything
  100. case 'fullonly':
  101. $cacheDao->expireCache($settings->get('retention'));
  102. $commentDao->expireCommentsFull($settings->get('retention'));
  103. $spotDao->expireSpotsFull($settings->get('retention'));
  104. // case fullonly
  105. } // switch
  106. echo ', done'.PHP_EOL;
  107. } // if
  108. $newSpotCount = 0;
  109. $newCommentCount = 0;
  110. $newReportCount = 0;
  111. $retriever = null;
  112. //# Spots
  113. /*
  114. * Actually retrieve spots from the server
  115. */
  116. $retriever = new Services_Retriever_Spots(
  117. $daoFactory,
  118. $settings,
  119. $forceMode,
  120. $retroMode
  121. );
  122. $newSpotCount = $retriever->perform();
  123. // Show the cumulative timings of the spotsretrieval
  124. if ($showTiming) {
  125. SpotTiming::displayCumul();
  126. SpotTiming::clear();
  127. } // if
  128. //# Creating filter counts
  129. if ($newSpotCount > 0) {
  130. $svcPrv_cacheSpotCount = new Services_Actions_CacheNewSpotCount(
  131. $daoFactory->getUserFilterCountDao(),
  132. $daoFactory->getUserFilterDao(),
  133. $daoFactory->getSpotDao(),
  134. new Services_Search_QueryParser($daoFactory->getConnection())
  135. );
  136. echo 'Calculating how many spots are new';
  137. $notifyNewArray = $svcPrv_cacheSpotCount->cacheNewSpotCount();
  138. echo ', done.'.PHP_EOL;
  139. // Show the cumulative timings of the caching of these spots
  140. if ($showTiming) {
  141. SpotTiming::displayCumul();
  142. SpotTiming::clear();
  143. } // if
  144. } // if
  145. /*
  146. * Should we retrieve comments?
  147. */
  148. if ($settings->get('retrieve_comments')) {
  149. $retriever = new Services_Retriever_Comments(
  150. $daoFactory,
  151. $settings,
  152. $forceMode,
  153. $retroMode
  154. );
  155. $newCommentCount = $retriever->perform();
  156. // Show the cumulative timings of the caching of these comments
  157. if ($showTiming) {
  158. SpotTiming::displayCumul();
  159. SpotTiming::clear();
  160. } // if
  161. } // if
  162. /*
  163. * Retrieval of reports
  164. */
  165. if ($settings->get('retrieve_reports') && !$retroMode) {
  166. $retriever = new Services_Retriever_Reports(
  167. $daoFactory,
  168. $settings,
  169. $forceMode,
  170. $retroMode
  171. );
  172. $newReportCount = $retriever->perform();
  173. // Show the cumulative timings of the caching of these reports
  174. if ($showTiming) {
  175. SpotTiming::displayCumul();
  176. SpotTiming::clear();
  177. } // if
  178. } // if
  179. /*
  180. * SpotStateList cleanup
  181. */
  182. $daoFactory->getSpotStateListDao()->cleanSpotStateList();
  183. try {
  184. //# External blacklist
  185. if ($settings->get('external_blacklist')) {
  186. $svcBwListRetriever = new Services_BWList_Retriever($daoFactory->getBlackWhiteListDao(), $daoFactory->getCacheDao());
  187. $bwResult = $svcBwListRetriever->retrieveBlackList($settings->get('blacklist_url'));
  188. if ($bwResult === false) {
  189. echo 'Blacklist not modified, no need to update'.PHP_EOL;
  190. } else {
  191. echo 'Finished updating blacklist. Added '.$bwResult['added'].', removed '.$bwResult['removed'].', skipped '.$bwResult['skipped'].' of '.$bwResult['total'].' lines.'.PHP_EOL;
  192. } // else
  193. } // if
  194. //# External whitelist
  195. if ($settings->get('external_whitelist')) {
  196. $bwResult = $svcBwListRetriever->retrieveWhiteList($settings->get('whitelist_url'));
  197. if ($bwResult === false) {
  198. echo 'Whitelist not modified, no need to update'.PHP_EOL;
  199. } else {
  200. echo 'Finished updating whitelist. Added '.$bwResult['added'].', removed '.$bwResult['removed'].', skipped '.$bwResult['skipped'].' of '.$bwResult['total'].' lines.'.PHP_EOL;
  201. } // else
  202. } // if
  203. } catch (CorruptBWListException $e) {
  204. echo PHP_EOL.'Non-fatal: Updating black/whitelist failed, most likely unreachable!';
  205. }
  206. //# Statistics
  207. if ($settings->get('prepare_statistics') && $newSpotCount > 0) {
  208. if (extension_loaded('gd') || extension_loaded('gd2')) {
  209. $settings_nntp_hdr = $settings->get('nntp_hdr');
  210. $svcPrv_Stats = new Services_Providers_Statistics(
  211. $daoFactory->getSpotDao(),
  212. $daoFactory->getCachedao(),
  213. $daoFactory->getUsenetStateDao()->getLastUpdate(Dao_UsenetState::State_Spots)
  214. );
  215. echo 'Starting to create statistics '.PHP_EOL;
  216. $svcPrv_Stats->createAllStatistics();
  217. echo 'Finished creating statistics '.PHP_EOL;
  218. echo PHP_EOL;
  219. } else {
  220. echo 'GD extension not loaded, not creating statistics'.PHP_EOL;
  221. } // else
  222. } // if
  223. // Verstuur notificaties
  224. $spotsNotifications = new SpotNotifications($daoFactory, $settings, $userSession);
  225. if (!empty($notifyNewArray)) {
  226. foreach ($notifyNewArray as $userId => $newSpotInfo) {
  227. foreach ($newSpotInfo as $filterInfo) {
  228. if (($filterInfo['newcount'] > 0) && ($filterInfo['enablenotify'])) {
  229. $spotsNotifications->sendNewSpotsForFilter($userId, $filterInfo['title'], $filterInfo['newcount']);
  230. } // if
  231. } // foreach
  232. } // foreach
  233. } // if
  234. $spotsNotifications->sendRetrieverFinished($newSpotCount, $newCommentCount, $newReportCount);
  235. } catch (RetrieverRunningException $x) {
  236. echo PHP_EOL.PHP_EOL;
  237. echo "retriever.php is already running, pass '--force' to ignore this warning.".PHP_EOL;
  238. } catch (NntpException $x) {
  239. echo 'SpotWeb v'.SPOTWEB_VERSION.' on PHP v'.PHP_VERSION.' crashed'.PHP_EOL.PHP_EOL;
  240. echo 'Fatal error occured while connecting to the newsserver:'.PHP_EOL;
  241. echo ' ('.$x->getCode().') '.$x->getMessage().PHP_EOL;
  242. echo PHP_EOL.PHP_EOL;
  243. echo $x->getTraceAsString();
  244. echo PHP_EOL.PHP_EOL;
  245. $retriever->quit();
  246. } catch (DatabaseConnectionException $x) {
  247. echo 'Unable to connect to database: '.$x->getMessage().PHP_EOL;
  248. } // catch
  249. catch (InvalidOwnSettingsSettingException $x) {
  250. echo 'There is an error in your ownsettings.php'.PHP_EOL.PHP_EOL;
  251. echo $x->getMessage().PHP_EOL;
  252. } // InvalidOwnSettingsSetting
  253. catch (Exception $x) {
  254. echo PHP_EOL.PHP_EOL;
  255. echo 'SpotWeb v'.SPOTWEB_VERSION.' on PHP v'.PHP_VERSION.' crashed'.PHP_EOL.PHP_EOL;
  256. echo 'Fatal error occured retrieving reports:'.PHP_EOL;
  257. echo ' '.$x->getMessage().PHP_EOL.PHP_EOL;
  258. echo PHP_EOL.PHP_EOL;
  259. echo $x->getTraceAsString();
  260. echo PHP_EOL.PHP_EOL;
  261. $retriever->quit();
  262. die();
  263. } // catch