PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/klutz/backend.php

https://github.com/imr/horde
PHP | 314 lines | 248 code | 34 blank | 32 comment | 97 complexity | 033624687d365818e413548059f5447b MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2002-2014 Horde LLC (http://www.horde.org/)
  4. *
  5. * See the enclosed file COPYING for license information (GPL). If you
  6. * did not receive this file, see http://www.horde.org/licenses/gpl.
  7. */
  8. /**
  9. * Output text only if this is the web interface.
  10. *
  11. * @param string $text The text to print.
  12. */
  13. function webPrint($text, $flush = false)
  14. {
  15. if (!$GLOBALS['cli']->runningFromCLI() && empty($GLOBALS['redirect'])
  16. && $GLOBALS['action'] != 'day') {
  17. echo $text;
  18. if ($flush) {
  19. if (ob_get_level() !== false) {
  20. ob_flush();
  21. }
  22. flush();
  23. }
  24. }
  25. }
  26. $no_compress = true;
  27. require_once __DIR__ . '/lib/Application.php';
  28. Horde_Registry::appInit('klutz');
  29. $cli = $injector->getInstance('Horde_Cli');
  30. // Check for a Klutz administrator.
  31. if (!$cli->runningFromCli() && !$registry->isAdmin(array('permission' => 'klutz:admin'))) {
  32. exit('forbidden');
  33. }
  34. // Don't do anything if the backend is none.
  35. if ($conf['storage']['driver'] == 'none') {
  36. if ($cli->runningFromCLI()) {
  37. fwrite(STDERR, _("You must define a storage backend to use the administrative interface."));
  38. exit(1);
  39. } else {
  40. $notification->push(_("You must define a storage backend to use the administrative interface."), 'horde.error');
  41. header('Location: ' . Horde::url('comics.php'));
  42. exit;
  43. }
  44. }
  45. // Should we redirect when we're done?
  46. $redirect = Horde_Util::getFormData('redirect');
  47. $action = Horde_Util::getFormData('action');
  48. // No more notifications after this point, so close the session so we
  49. // don't lock other pages - if redirecting, we are only fetching one comic
  50. // and would like a notification on success/failure.
  51. if (empty($redirect)) {
  52. session_write_close();
  53. }
  54. // Proceed once we're sure we're authorized.
  55. // $mode is either set from the cli.backend.* scripts as an array of modes
  56. // or passed in as a single mode from the form variable.
  57. $mode = Horde_Util::nonInputVar('mode', Horde_Util::getFormData('mode', array('menu')));
  58. if (!is_array($mode)) {
  59. $mode = array($mode);
  60. }
  61. $time = mktime(0, 0, 0);
  62. $t = getdate($time);
  63. if (!empty($conf['backend']['daystokeep'])) {
  64. $daystokeep = $conf['backend']['daystokeep'];
  65. $oldest = mktime(0, 0, 0, $t['mon'], $t['mday'] - $daystokeep, $t['year']);
  66. } else {
  67. $daystokeep = 90;
  68. $oldest = null;
  69. }
  70. $expired = mktime(0, 0, 0, $t['mon'], $t['mday'] - ($daystokeep + 1), $t['year']);
  71. if (in_array('sums', $mode)) {
  72. $klutz_driver->rebuildSums();
  73. if (!in_array('menu', $mode)) {
  74. webPrint(_("The unique identifiers table has been rebuilt."));
  75. }
  76. }
  77. if (count($mode) == 0 || in_array('menu', $mode)) {
  78. $fetch_date_select = '<select name="date">' .
  79. '<option value="all">' . _("All Dates") . '</option>';
  80. foreach (array_reverse(Klutz_Driver::listDates($time, $oldest)) as $date) {
  81. if ($date == $time) {
  82. $selected = ' selected="selected"';
  83. } else {
  84. $selected = '';
  85. }
  86. $fetch_date_select .= '<option value="' . $date . '"' . $selected . '">' .
  87. strftime('%B %d, %Y', $date) . '</option>';
  88. }
  89. $fetch_date_select .= '</select>';
  90. $delete_date_select = '<select name="date">';
  91. foreach ($klutz_driver->listDates($time, 0, $time) as $date) {
  92. if ($date == $oldest) {
  93. $selected = ' selected="selected"';
  94. } else {
  95. $selected = '';
  96. }
  97. $delete_date_select .= '<option value="' . $date . '"' . $selected . '">' .
  98. strftime("%B %d, %Y", $date) . '</option>';
  99. }
  100. $delete_date_select .= '</select>';
  101. $comic_select = '<select name="index"><option value="all" selected="selected">' .
  102. _("All Comics") . '</option>';
  103. foreach ($klutz->listEnabled() as $index) {
  104. $comic_select .= "<option value=\"$index\">" .
  105. $klutz->getProperty($index, 'name') .
  106. '</option>';
  107. }
  108. $comic_select .= '</select>';
  109. $sums_url = Horde::url('backend.php')->add('mode[]', 'sums')->add('mode[]', 'menu');
  110. $page_output->header(array(
  111. 'title' => _("Comics Update")
  112. ));
  113. require KLUTZ_TEMPLATES . '/backend.html.php';
  114. $page_output->footer();
  115. exit;
  116. }
  117. /* Make it at least look prettier if we are running from web */
  118. if (!$cli->runningFromCLI() && empty($redirect)) {
  119. $page_output->header(array(
  120. 'title' => _("Comics Update")
  121. ));
  122. }
  123. if (in_array('fetch', $mode)) {
  124. if (empty($date)) {
  125. $date = Horde_Util::getFormData('date', mktime(0, 0, 0));
  126. }
  127. if (empty($index)) {
  128. $index = Horde_Util::getFormData('index', 'all');
  129. }
  130. if (empty($overwrite)) {
  131. $overwrite = Horde_Util::getFormData('overwrite', 'false');
  132. }
  133. if (empty($nounique)) {
  134. $nounique = Horde_Util::getFormData('nounique', 'false');
  135. }
  136. if (Horde_String::lower($overwrite) == 'true') {
  137. $overwrite = true;
  138. } else {
  139. $overwrite = false;
  140. }
  141. if (Horde_String::lower($nounique) == 'true') {
  142. $nounique = true;
  143. } else {
  144. $nounique = false;
  145. }
  146. if (Horde_String::lower($date) == 'all') {
  147. $dates = array_reverse(Klutz_Driver::listDates($time, $oldest));
  148. } else {
  149. // make sure the time for $date is midnight
  150. $d = getdate($date);
  151. $date = mktime(0, 0, 0, $d['mon'], $d['mday'], $d['year']);
  152. $dates = array($date);
  153. }
  154. foreach ($dates as $date) {
  155. // is $date today?
  156. $today = false;
  157. if ($date != 'all' && $date == mktime(0, 0, 0)) {
  158. $today = true;
  159. }
  160. if (Horde_String::lower($index) == 'all') {
  161. $comics = $klutz->listEnabled(null, $date);
  162. } else {
  163. $comics = $klutz->listEnabled(array($index), $date);
  164. }
  165. foreach ($comics as $comic) {
  166. // If this comic isn't available all days, nohistory gets
  167. // ugly. SOMEONE PLEASE MAKE IT BETTER ;)
  168. $name = $klutz->getProperty($comic, 'name');
  169. $days = $klutz->getProperty($comic, 'days');
  170. if ($days != 'random' && count($days) < 7) {
  171. $day = mktime(0, 0, 0);
  172. while (!in_array(Horde_String::lower(date('D', $day)), $days)) {
  173. $d = getdate($day);
  174. $day = mktime(0, 0, 0, $d['mon'], $d['mday'] - 1, $d['year']);
  175. }
  176. if ($day == $date) {
  177. $today = true;
  178. }
  179. }
  180. if (!$today && $klutz->getProperty($comic, 'nohistory')) {
  181. webPrint(sprintf(_("Skipping %s for %s - no historical fetching"),
  182. $name, strftime("%B %d, %Y", $date)));
  183. } elseif ($overwrite || !$klutz_driver->imageExists($comic, $date)) {
  184. webPrint(sprintf(_("Fetching %s for date %s..."),
  185. $name, strftime('%B %d, %Y', $date)), true);
  186. $c = $klutz->comicObject($comic);
  187. $image = $c->fetchImage($date);
  188. if (!is_object($image)) {
  189. if (empty($redirect)) {
  190. webPrint(_("The image doesn't exist or can't be retrieved"));
  191. } else {
  192. $GLOBALS['notification']->push(_("The image doesn't exist or can't be retrieved"));
  193. }
  194. } elseif (!$nounique && (method_exists($klutz_driver, 'isUnique') &&
  195. !$klutz_driver->isUnique($image))) {
  196. webPrint(sprintf(_("This image appears to be a repeat (%s)"), md5($image->data)));
  197. } elseif ($klutz_driver->storeImage($comic, $image, $date)) {
  198. if (empty($redirect)) {
  199. webPrint(_("Done"));
  200. } else {
  201. $GLOBALS['notification']->push(sprintf(_("Fetching %s for date %s...Done"),
  202. $name, strftime('%B %d, %Y', $date)));
  203. }
  204. } else {
  205. webPrint(_("Error storing the image"));
  206. }
  207. } else {
  208. webPrint(sprintf(_("Skipping %s for %s"),
  209. $name, strftime('%B %d, %Y', $date)));
  210. }
  211. webPrint("<br />\n");
  212. }
  213. }
  214. }
  215. if (in_array('delete', $mode)) {
  216. // Note: We don't allow overriding of $date from backend.php because it
  217. // would conflict with the $date used for fetching so we'd delete all
  218. // comics if fetch and delete were both requested in one file.
  219. $date = Horde_Util::getFormData('date', $expired);
  220. $index = Horde_Util::getFormData('index', 'all');
  221. $timeframe = Horde_Util::getFormData('timeframe', 'older');
  222. // make sure the time for $date is midnight
  223. $d = getdate($date);
  224. $date = mktime(0, 0, 0, $d['mon'], $d['mday'], $d['year']);
  225. $success = _("Successfully removed %s for %s<br />\n");
  226. $notfound = _("Could not find %s for %s<br />\n");
  227. $error = _("An error occurred removing %s for %s<br />\n");
  228. // Figure out what dates we need to work on
  229. if ($timeframe == 'date') {
  230. $dates = array($date);
  231. } elseif ($timeframe == 'older') {
  232. $dates = $klutz_driver->listDates($date, 0, $date);
  233. } elseif ($timeframe == 'newer') {
  234. $dates = $klutz_driver->listDates($date, $date, mktime(0, 0, 0));
  235. } else {
  236. $dates = array();
  237. }
  238. if ($index == 'all') {
  239. if (count($dates) == 0) {
  240. webPrint(sprintf($notfound, _("any comics"), _("any dates")));
  241. }
  242. foreach ($dates as $date) {
  243. if ($klutz_driver->removeDate($date)) {
  244. webPrint(sprintf($success, _("all comics"), strftime('%B %d, %Y', $date)));
  245. } else {
  246. webPrint(sprintf($error, _("all comics"), strftime('%B %d, %Y', $date)));
  247. }
  248. }
  249. } else {
  250. $name = $klutz->getProperty($index, 'name');
  251. $days = $klutz->getProperty($index, 'days');
  252. foreach ($dates as $date) {
  253. if ($days == 'random' || in_array(Horde_String::lower(date('D', $date)), $days)) {
  254. if (!$klutz_driver->imageExists($index, $date)) {
  255. webPrint(sprintf($notfound, $index, strftime('%B %d, %Y', $date)));
  256. } else {
  257. if ($klutz_driver->removeImage($index, $date)) {
  258. webPrint(sprintf($success, $name, strftime('%B %d, %Y', $date)));
  259. } else {
  260. webPrint(sprintf($error, $name, strftime('%B %d, %Y', $date)));
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. // Save the updated sums if necessary.
  268. if (method_exists($klutz_driver, 'saveSums')) {
  269. $klutz_driver->saveSums();
  270. }
  271. // Redirect?
  272. if (!empty($redirect)) {
  273. header('Location: ' . Horde::url($redirect)->add(array(
  274. 'actionID' => Horde_Util::getFormData('action'),
  275. 'date' => $date,
  276. 'index' => $index))->setRaw(true));
  277. }
  278. if (!$cli->runningFromCLI()) {
  279. $page_output->footer();
  280. }