searchcode logo
Dogecoin is an open source peer-to-peer cryptocurrency

spotweb /retrieve.php

git clone git://github.com/spotweb/spotweb.git

Language

PHP

MD5 Hash

9527d225550d01b9fa5ff276a5d17f6c

Lines in File 280
Blank Lines 40 (14.29%)
Comment Lines 45 (16%)
Code Lines 194 (69%)
Estimated Development Effort (Months)0.43
Estimated Development Effort (Years)0
Estimated Developers0.24
Estimated Cost$4803.00
Generated using David A. Wheeler's 'SLOCCount'.
  1. <?php
  2. error_reporting(E_ALL & ~8192 & ~E_USER_WARNING); # 8192 == E_DEPRECATED maar PHP < 5.3 heeft die niet
  3.  
  4. /*
  5.  * If we are run from another directory, try to change the current
  6.  * working directory to a directory the script is in
  7.  */
  8. if (@!file_exists(getcwd() . '/' . basename($argv[0]))) {
  9. chdir(dirname(__FILE__));
  10. } # if
  11.  
  12. require_once "lib/SpotTranslation.php";
  13. require_once "lib/SpotClassAutoload.php";
  14. try {
  15. require_once "settings.php";
  16. }
  17. catch(InvalidOwnSettingsSettingException $x) {
  18. echo "There is an error in your ownsettings.php" . PHP_EOL . PHP_EOL;
  19. echo $x->getMessage() . PHP_EOL;
  20. die();
  21. } # InvalidOwnSettingsSetting
  22.  
  23. require_once "lib/SpotTiming.php";
  24. require_once "lib/exceptions/ParseSpotXmlException.php";
  25. require_once "lib/exceptions/NntpException.php";
  26.  
  27. /*
  28.  * disable timing, all queries which are ran by retrieve this would make it use
  29.  * large amounts of memory
  30.  */
  31. SpotTiming::disable();
  32.  
  33. # Initialize commandline arguments
  34. SpotCommandline::initialize(array('force', 'debug', 'retro'), array('force' => false, 'debug' => false, 'retro' => false));
  35.  
  36. # Initialize translation to english
  37. SpotTranslation::initialize('en_US');
  38.  
  39. # in safe mode, max execution time cannot be set, warn the user
  40. if (ini_get('safe_mode') ) {
  41. echo "WARNING: PHP safemode is enabled, maximum execution cannot be reset! Turn off safemode if this causes problems" . PHP_EOL . PHP_EOL;
  42. } # if
  43.  
  44. try {
  45. $db = new SpotDb($settings['db']);
  46. $db->connect();
  47. }
  48. catch(DatabaseConnectionException $x) {
  49. die("Unable to connect to database: " . $x->getMessage() . PHP_EOL);
  50. } # catch
  51.  
  52. # Creer het settings object
  53. $settings = SpotSettings::singleton($db, $settings);
  54.  
  55. # Controleer dat we niet een schema upgrade verwachten
  56. if (!$settings->schemaValid()) {
  57. die("Database schema is gewijzigd, draai upgrade-db.php aub" . PHP_EOL);
  58. } # if
  59.  
  60. # Controleer eerst of de settings versie nog wel geldig zijn
  61. if (!$settings->settingsValid()) {
  62. die("Globale settings zijn gewijzigd, draai upgrade-db.php aub" . PHP_EOL);
  63. } # if
  64.  
  65. $req = new SpotReq();
  66. $req->initialize($settings);
  67.  
  68. # We willen alleen uitgevoerd worden door een user die dat mag als
  69. # we via de browser aangeroepen worden. Via console halen we altijd
  70. # het admin-account op
  71. $spotUserSystem = new SpotUserSystem($db, $settings);
  72. if (!SpotCommandline::isCommandline()) {
  73. # Vraag de API key op die de gebruiker opgegeven heeft
  74. $apiKey = $req->getDef('apikey', '');
  75.  
  76. $userSession = $spotUserSystem->verifyApi($apiKey);
  77.  
  78. if (($userSession == false) || (!$userSession['security']->allowed(SpotSecurity::spotsec_retrieve_spots, ''))) {
  79. die("Access denied");
  80. } # if
  81.  
  82. # Add the user's ip addres, we need it for sending notifications
  83. $userSession['session'] = array('ipaddr' => '');
  84. } else {
  85. $userSession['user'] = $db->getUser(SPOTWEB_ADMIN_USERID);
  86. $userSession['security'] = new SpotSecurity($db, $settings, $userSession['user'], '');
  87. $userSession['session'] = array('ipaddr' => '');
  88. } # if
  89.  
  90. if ($req->getDef('output', '') == 'xml') {
  91. echo "<xml>";
  92. } # if
  93.  
  94. # We vragen de nntp_hdr settings alvast op
  95. $settings_nntp_hdr = $settings->get('nntp_hdr');
  96. if (empty($settings_nntp_hdr['host'])) {
  97. die("Unable to continue: You did not setup any newsserver yet." . PHP_EOL);
  98. } # if
  99.  
  100. ## Als we forceren om de "already running" check te bypassen, doe dat dan
  101. if (SpotCommandline::get('force')) {
  102. $db->setRetrieverRunning($settings_nntp_hdr['host'], false);
  103. } # if
  104.  
  105. ## Moeten we debugloggen? Kan alleen als geen --force opgegeven wordt
  106. $debugLog = SpotCommandline::get('debug');
  107.  
  108. ## RETRO MODE! Hiermee kunnen we de fullspots, fullcomments en/of cache achteraf ophalen
  109. $retroMode = SpotCommandline::get('retro');
  110.  
  111. ## Retention cleanup
  112. try {
  113. if ($settings->get('retention') > 0 && !$retroMode) {
  114. $db->deleteSpotsRetention($settings->get('retention'));
  115. } # if
  116. } catch(Exception $x) {
  117. echo PHP_EOL . PHP_EOL;
  118. echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
  119. echo "Fatal error occured while cleaning up messages due to retention:" . PHP_EOL;
  120. echo " " . $x->getMessage() . PHP_EOL;
  121. echo PHP_EOL . PHP_EOL;
  122. echo $x->getTraceAsString();
  123. echo PHP_EOL . PHP_EOL;
  124. die();
  125. } # catch
  126.  
  127. $newSpotCount = 0;
  128. $newCommentCount = 0;
  129. $newReportCount = 0;
  130. $retriever = null;
  131.  
  132. ## Spots
  133. try {
  134. /*
  135. * Actually retrieve spots from the server
  136. */
  137. $retriever = new SpotRetriever_Spots($settings_nntp_hdr,
  138. $db,
  139. $settings,
  140. $req->getDef('output', ''),
  141. $debugLog,
  142. $retroMode);
  143. $newSpotCount = $retriever->perform();
  144.  
  145. ## Creating filter counts
  146. if ($newSpotCount > 0) {
  147. $spotsOverview = new SpotsOverview($db, $settings);
  148. echo 'Calculating how many spots are new';
  149. $spotsOverview->cacheNewSpotCount();
  150. echo ', done.' . PHP_EOL;
  151. } # if
  152.  
  153. /*
  154. * Should we retrieve comments?
  155. */
  156. if ($settings->get('retrieve_comments')) {
  157. $retriever = new SpotRetriever_Comments($settings_nntp_hdr,
  158. $db,
  159. $settings,
  160. $req->getDef('output', ''),
  161. $debugLog,
  162. $retroMode);
  163. $newCommentCount = $retriever->perform();
  164. } # if
  165.  
  166. /*
  167. * Retrieval of reports
  168. */
  169. if ($settings->get('retrieve_reports') && !$retroMode) {
  170. $retriever = new SpotRetriever_Reports($settings_nntp_hdr,
  171. $db,
  172. $settings,
  173. $req->getDef('output', ''),
  174. $debugLog);
  175. $newReportCount = $retriever->perform();
  176. } # if
  177.  
  178. /*
  179. * SpotStateList cleanup
  180. */
  181. $db->cleanSpotStateList();
  182.  
  183. if (!$retroMode) {
  184. $db->cleanCache(30);
  185. } # if
  186. }
  187. catch(RetrieverRunningException $x) {
  188. echo PHP_EOL . PHP_EOL;
  189. die("retriever.php draait al, geef de parameter '--force' mee om te forceren." . PHP_EOL);
  190. }
  191. catch(NntpException $x) {
  192. echo PHP_EOL . PHP_EOL;
  193. echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
  194. echo "Fatal error occured while connecting to the newsserver:" . PHP_EOL;
  195. echo " (" . $x->getCode() . ") " . $x->getMessage() . PHP_EOL;
  196. echo PHP_EOL . PHP_EOL;
  197. echo $x->getTraceAsString();
  198. echo PHP_EOL . PHP_EOL;
  199.  
  200. if (!empty($retriever)){
  201. echo "Updating retrieve status in the database" . PHP_EOL . PHP_EOL;
  202. $retriever->quit();
  203. }
  204. die();
  205. }
  206. catch(Exception $x) {
  207. echo PHP_EOL . PHP_EOL;
  208. echo 'SpotWeb v' . SPOTWEB_VERSION . ' on PHP v' . PHP_VERSION . ' crashed' . PHP_EOL . PHP_EOL;
  209. echo "Fatal error occured retrieving reports:" . PHP_EOL;
  210. echo " " . $x->getMessage() . PHP_EOL . PHP_EOL;
  211. echo PHP_EOL . PHP_EOL;
  212. echo $x->getTraceAsString();
  213. echo PHP_EOL . PHP_EOL;
  214. die();
  215. } # catch
  216.  
  217.  
  218. ## External blacklist
  219. $settings_external_blacklist = $settings->get('external_blacklist');
  220. if ($settings_external_blacklist) {
  221. try {
  222. $spotsOverview = new SpotsOverview($db, $settings);
  223. # haal de blacklist op
  224. list($http_code, $blacklist) = $spotsOverview->getFromWeb($settings->get('blacklist_url'), false, 30*60);
  225.  
  226. if ($http_code == 304) {
  227. echo "Blacklist not modified, no need to update" . PHP_EOL;
  228. } elseif (strpos($blacklist['content'],">")) {
  229. echo "Error, blacklist does not have expected layout!" . PHP_EOL;
  230. } else {
  231. # update de blacklist
  232. $blacklistarray = explode(chr(10),$blacklist['content']);
  233.  
  234. # Perform a very small snaity check on the blacklist
  235. if ((count($blacklistarray) > 5) && (strlen($blacklistarray[0]) < 10)) {
  236. $updateblacklist = $db->updateExternalBlacklist($blacklistarray);
  237. echo "Finished updating blacklist. Added " . $updateblacklist['added'] . ", removed " . $updateblacklist['removed'] . ", skipped " . $updateblacklist['skipped'] . " of " . count($blacklistarray) . " lines." . PHP_EOL;
  238. } else {
  239. echo "Blacklist is probably corrupt, skipping" . PHP_EOL;
  240. } # else
  241. }
  242. } catch(Exception $x) {
  243. echo "Fatal error occured while updating blacklist:" . PHP_EOL;
  244. echo " " . $x->getMessage() . PHP_EOL;
  245. echo PHP_EOL . PHP_EOL;
  246. echo $x->getTraceAsString();
  247. echo PHP_EOL . PHP_EOL;
  248. }
  249. } # if
  250.  
  251. ## Statistics
  252. if ($settings->get('prepare_statistics') && $newSpotCount > 0) {
  253. $spotsOverview = new SpotsOverview($db, $settings);
  254. $spotImage = new SpotImage($db);
  255. $spotsOverview->setActiveRetriever(true);
  256.  
  257. echo "Starting to create statistics " . PHP_EOL;
  258. foreach ($spotImage->getValidStatisticsLimits() as $limitValue => $limitName) {
  259. # Reset timelimit
  260. set_time_limit(60);
  261.  
  262. foreach($settings->get('system_languages') as $language => $name) {
  263. foreach ($spotImage->getValidStatisticsGraphs() as $graphValue => $graphName) {
  264. $spotsOverview->getStatisticsImage($graphValue, $limitValue, $settings_nntp_hdr, $language);
  265. } # foreach graph
  266. } # foreach language
  267. echo "Finished creating statistics " . $limitName . PHP_EOL;
  268. } # foreach limit
  269.  
  270. echo PHP_EOL;
  271. } # if
  272.  
  273. # Verstuur notificaties
  274. $spotsNotifications = new SpotNotifications($db, $settings, $userSession);
  275. $spotsNotifications->sendRetrieverFinished($newSpotCount, $newCommentCount, $newReportCount);
  276.  
  277. if ($req->getDef('output', '') == 'xml') {
  278. echo "</xml>";
  279. } # if
  280.