PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/ojs/ojs-2.3.2-1/plugins/generic/counter/CounterHandler.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 361 lines | 220 code | 69 blank | 72 comment | 20 complexity | 3c87674e9856faf2cfa8ff315265c43d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @file CounterHandler.inc.php
  4. *
  5. * Copyright (c) 2003-2009 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class CounterHandler
  9. * @ingroup plugins_generic_counter
  10. *
  11. * @brief Counter statistics request handler.
  12. */
  13. // $Id$
  14. import('handler.Handler');
  15. class CounterHandler extends Handler {
  16. /** Plugin associated with this request **/
  17. var $plugin;
  18. /**
  19. * Constructor
  20. **/
  21. function CounterHandler() {
  22. parent::Handler();
  23. }
  24. /**
  25. * Display the main log analyzer page.
  26. */
  27. function index() {
  28. $this->validate();
  29. $this->setupTemplate();
  30. $plugin =& $this->plugin;
  31. $counterReportDao =& DAORegistry::getDAO('CounterReportDAO');
  32. $years = $counterReportDao->getYears();
  33. $templateManager =& TemplateManager::getManager();
  34. $templateManager->assign('years', $years);
  35. $templateManager->display($plugin->getTemplatePath() . 'index.tpl');
  36. }
  37. /**
  38. * Internal function to collect structures for output
  39. */
  40. function _arrangeEntry($entry, $months, $year) {
  41. $entryArray=null;
  42. $thisYear = date('Y');
  43. $thisMonth = date('n');
  44. for ($i = 0; $i <= 11; $i++) {
  45. if ( ($i+1 > $thisMonth) && ($year == $thisYear) ) break;
  46. $entryArray[$i]['start'] = date("Y-m-d", mktime(0, 0, 0, $i+1, 1, $year));
  47. $entryArray[$i]['end'] = date("Y-m-t", mktime(0, 0, 0, $i+1, 1, $year));
  48. $entryArray[$i]['count_total'] = $entry[$months[$i]];
  49. }
  50. $entryArray[$i]['start'] = date("Y-m-d", mktime(0, 0, 0, 1, 1, $year));
  51. $entryArray[$i]['end'] = date("Y-m-t", mktime(0, 0, 0, 12, 1, $year));
  52. $entryArray[$i]['count_total'] = $entry['count_ytd_total'];
  53. $entryArray[$i]['count_html'] = $entry['count_ytd_html'];
  54. $entryArray[$i]['count_pdf'] = $entry['count_ytd_pdf'];
  55. return $entryArray;
  56. }
  57. /**
  58. * Internal function to assign information for the Counter part of a report
  59. */
  60. function _assignTemplateCounterXML($templateManager) {
  61. $journal =& Request::getJournal();
  62. $year = Request::getUserVar('year');
  63. if ($year < 2000) $year = 2000;
  64. $counterReportDao =& DAORegistry::getDAO('CounterReportDAO');
  65. $months = $counterReportDao->getMonthLabels();
  66. $entry = $counterReportDao->buildMonthlyTotalLog($year);
  67. $totalsArray = $this->_arrangeEntry($entry, $months, $year);
  68. $journalDao =& DAORegistry::getDAO('JournalDAO');
  69. $journalIds = $counterReportDao->getJournalIds();
  70. $i=0;
  71. foreach ($journalIds as $journalId) {
  72. $journal =& $journalDao->getJournal($journalId);
  73. if (!$journal) continue;
  74. $entry = $counterReportDao->buildMonthlyLog($journalId, $year);
  75. $journalsArray[$i]['entries'] = $this->_arrangeEntry($entry, $months, $year);
  76. $journalsArray[$i]['journalTitle'] = $journal->getLocalizedTitle();
  77. $journalsArray[$i]['publisherInstitution'] = $journal->getSetting('publisherInstitution');
  78. $journalsArray[$i]['printIssn'] = $journal->getSetting('printIssn');
  79. $journalsArray[$i]['onlineIssn'] = $journal->getSetting('onlineIssn');
  80. $i++;
  81. }
  82. $siteSettingsDao =& DAORegistry::getDAO('SiteSettingsDAO');
  83. $siteTitle = $siteSettingsDao->getSetting('title',Locale::getLocale());
  84. $base_url =& Config::getVar('general','base_url');
  85. $reqUser =& Request::getUser();
  86. $templateManager->assign_by_ref('reqUser', $reqUser);
  87. $templateManager->assign_by_ref('totalsArray', $totalsArray);
  88. $templateManager->assign_by_ref('journalsArray', $journalsArray);
  89. $templateManager->assign('siteTitle', $siteTitle);
  90. $templateManager->assign('base_url', $base_url);
  91. }
  92. /**
  93. * Counter report in XML
  94. */
  95. function reportXML() {
  96. $this->validate();
  97. $plugin =& $this->plugin;
  98. $this->setupTemplate(true);
  99. $templateManager =& TemplateManager::getManager();
  100. $this->_assignTemplateCounterXML($templateManager);
  101. $templateManager->display($plugin->getTemplatePath() . 'reportxml.tpl', 'text/xml');
  102. }
  103. /**
  104. * SUSHI report
  105. */
  106. function sushiXML() {
  107. $this->validate();
  108. $plugin =& $this->plugin;
  109. $this->setupTemplate(true);
  110. $templateManager =& TemplateManager::getManager();
  111. $this->_assignTemplateCounterXML($templateManager);
  112. /*
  113. $SOAPRequest = '<soapenv:Envelope
  114. xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  115. xmlns:coun="http://www.niso.org/schemas/sushi/counter"
  116. xmlns:sus="http://www.niso.org/schemas/sushi">
  117. <soapenv:Header/>
  118. <soapenv:Body>
  119. <coun:ReportRequest Created="?" ID="?">
  120. <sus:Requestor>
  121. <sus:ID>reqIDdata99</sus:ID>
  122. <sus:Name>reqNamedata99</sus:Name>
  123. <sus:Email>recEmaildata99</sus:Email>
  124. </sus:Requestor>
  125. <sus:CustomerReference>
  126. <sus:ID>test/prime</sus:ID>
  127. </sus:CustomerReference>
  128. <sus:ReportDefinition Name="JR1" Release="3">
  129. <sus:Filters>
  130. <sus:UsageDateRange>
  131. <sus:Begin>2009-01-01</sus:Begin>
  132. <sus:End>2009-06-01</sus:End>
  133. </sus:UsageDateRange>
  134. </sus:Filters>
  135. </sus:ReportDefinition>
  136. </coun:ReportRequest>
  137. </soapenv:Body>
  138. </soapenv:Envelope>';
  139. */
  140. $SOAPRequest = file_get_contents('php://input');
  141. // crude handling of namespaces in the input
  142. // FIXME: only the last prefix in the input will be used for each namespace
  143. $soapEnvPrefix='';
  144. $sushiPrefix='';
  145. $counterPrefix='';
  146. $re = '/xmlns:([^=]+)="([^"]+)"/';
  147. preg_match_all($re, $SOAPRequest, $mat, PREG_SET_ORDER);
  148. foreach ($mat as $xmlns) {
  149. $modURI = $xmlns[2];
  150. if ((strrpos($modURI, '/')+1) == strlen($modURI)) $modURI = substr($modURI, 0, -1);
  151. switch ($modURI) {
  152. case 'http://schemas.xmlsoap.org/soap/envelope':
  153. $soapEnvPrefix = $xmlns[1];
  154. break;
  155. case 'http://www.niso.org/schemas/sushi':
  156. $sushiPrefix = $xmlns[1];
  157. break;
  158. case 'http://www.niso.org/schemas/sushi/counter':
  159. $counterPrefix = $xmlns[1];
  160. break;
  161. }
  162. }
  163. if (strlen($soapEnvPrefix)>0) $soapEnvPrefix .= ':';
  164. if (strlen($sushiPrefix)>0) $sushiPrefix .= ':';
  165. if (strlen($counterPrefix)>0) $counterPrefix .= ':';
  166. $parser = new XMLParser();
  167. $tree = $parser->parseText($SOAPRequest);
  168. $parser->destroy(); // is this necessary?
  169. $reportRequestNode = $tree->getChildByName($soapEnvPrefix.'Body')->getChildByName($counterPrefix.'ReportRequest');
  170. $requestorID = $reportRequestNode->getChildByName($sushiPrefix.'Requestor')->getChildByName($sushiPrefix.'ID')->getValue();
  171. $requestorName = $reportRequestNode->getChildByName($sushiPrefix.'Requestor')->getChildByName($sushiPrefix.'Name')->getValue();
  172. $requestorEmail = $reportRequestNode->getChildByName($sushiPrefix.'Requestor')->getChildByName($sushiPrefix.'Email')->getValue();
  173. $customerReferenceID = $reportRequestNode->getChildByName($sushiPrefix.'CustomerReference')->getChildByName($sushiPrefix.'ID')->getValue();
  174. $reportName = $reportRequestNode->getChildByName($sushiPrefix.'ReportDefinition')->getAttribute('Name');
  175. $reportRelease = $reportRequestNode->getChildByName($sushiPrefix.'ReportDefinition')->getAttribute('Release');
  176. $usageDateRange = $reportRequestNode->getChildByName($sushiPrefix.'ReportDefinition')->getChildByName($sushiPrefix.'Filters')->getChildByName($sushiPrefix.'UsageDateRange');
  177. $usageDateBegin = $usageDateRange->getChildByName($sushiPrefix.'Begin')->getValue();
  178. $usageDateEnd = $usageDateRange->getChildByName($sushiPrefix.'End')->getValue();
  179. $templateManager->assign('requestorID', $requestorID);
  180. $templateManager->assign('requestorName', $requestorName);
  181. $templateManager->assign('requestorEmail', $requestorEmail);
  182. $templateManager->assign('customerReferenceID', $customerReferenceID);
  183. $templateManager->assign('reportName', $reportName);
  184. $templateManager->assign('reportRelease', $reportRelease);
  185. $templateManager->assign('usageDateBegin', $usageDateBegin);
  186. $templateManager->assign('usageDateEnd', $usageDateEnd);
  187. $templateManager->assign('templatePath', $plugin->getTemplatePath());
  188. $templateManager->display($plugin->getTemplatePath() . 'sushixml.tpl', 'text/plain');
  189. }
  190. function report() {
  191. $this->validate();
  192. $plugin =& $this->plugin;
  193. $this->setupTemplate(true);
  194. $journal =& Request::getJournal();
  195. $year = Request::getUserVar('year');
  196. $counterReportDao =& DAORegistry::getDAO('CounterReportDAO');
  197. header('content-type: text/comma-separated-values');
  198. header('content-disposition: attachment; filename=report.csv');
  199. $fp = fopen('php://output', 'wt');
  200. String::fputcsv($fp, array(Locale::translate('plugins.generic.counter.1a.title1')));
  201. String::fputcsv($fp, array(Locale::translate('plugins.generic.counter.1a.title2', array('year' => $year))));
  202. String::fputcsv($fp, array()); // FIXME: Criteria should be here?
  203. String::fputcsv($fp, array(Locale::translate('plugins.generic.counter.1a.dateRun')));
  204. String::fputcsv($fp, array(strftime("%Y-%m-%d")));
  205. $cols = array(
  206. '',
  207. Locale::translate('plugins.generic.counter.1a.publisher'),
  208. Locale::translate('plugins.generic.counter.1a.platform'),
  209. Locale::translate('plugins.generic.counter.1a.printIssn'),
  210. Locale::translate('plugins.generic.counter.1a.onlineIssn')
  211. );
  212. for ($i=1; $i<=12; $i++) {
  213. $time = strtotime($year . '-' . $i . '-01');
  214. strftime('%b', $time);
  215. $cols[] = strftime('%b-%Y', $time);
  216. }
  217. $cols[] = Locale::translate('plugins.generic.counter.1a.ytdTotal');
  218. $cols[] = Locale::translate('plugins.generic.counter.1a.ytdHtml');
  219. $cols[] = Locale::translate('plugins.generic.counter.1a.ytdPdf');
  220. fputcsv($fp, $cols);
  221. // Display the totals first
  222. $entry = $counterReportDao->buildMonthlyTotalLog($year);
  223. $cols = array(
  224. Locale::translate('plugins.generic.counter.1a.totalForAllJournals'),
  225. '-', // Publisher
  226. '', // Platform
  227. '-',
  228. '-'
  229. );
  230. $months = $counterReportDao->getMonthLabels();
  231. for ($i = 0; $i < 12; $i++) {
  232. $cols[] = $entry[$months[$i]];
  233. }
  234. $cols[] = $entry['count_ytd_total'];
  235. $cols[] = $entry['count_ytd_html'];
  236. $cols[] = $entry['count_ytd_pdf'];
  237. fputcsv($fp, $cols);
  238. // Get statistics from the log.
  239. $journalDao =& DAORegistry::getDAO('JournalDAO');
  240. $journalIds = $counterReportDao->getJournalIds();
  241. foreach ($journalIds as $journalId) {
  242. $journal =& $journalDao->getJournal($journalId);
  243. if (!$journal) continue;
  244. $entry = $counterReportDao->buildMonthlyLog($journalId, $year);
  245. $cols = array(
  246. $journal->getLocalizedTitle(),
  247. $journal->getLocalizedSetting('publisherInstitution'),
  248. '', // Platform
  249. $journal->getSetting('printIssn'),
  250. $journal->getSetting('onlineIssn')
  251. );
  252. $months = $counterReportDao->getMonthLabels();
  253. for ($i = 0; $i < 12; $i++) {
  254. $cols[] = $entry[$months[$i]];
  255. }
  256. $cols[] = $entry['count_ytd_total'];
  257. $cols[] = $entry['count_ytd_html'];
  258. $cols[] = $entry['count_ytd_pdf'];
  259. fputcsv($fp, $cols);
  260. unset($journal, $entry);
  261. }
  262. fclose($fp);
  263. }
  264. /**
  265. * Validate that user has site admin privileges or journal manager priveleges.
  266. * Redirects to the user index page if not properly authenticated.
  267. * @param $canRedirect boolean Whether or not to redirect if the user cannot be validated; if not, the script simply terminates.
  268. */
  269. function validate($canRedirect = true) {
  270. parent::validate();
  271. $journal =& Request::getJournal();
  272. if (!Validation::isSiteAdmin()) {
  273. if ($canRedirect) Validation::redirectLogin();
  274. else exit;
  275. }
  276. $plugin =& Registry::get('plugin');
  277. $this->plugin =& $plugin;
  278. return true;
  279. }
  280. /**
  281. * Set up common template variables.
  282. * @param $subclass boolean set to true if caller is below this handler in the heirarchy
  283. */
  284. function setupTemplate($subclass = false) {
  285. parent::setupTemplate();
  286. $templateMgr =& TemplateManager::getManager();
  287. $pageHierarchy = array(array(Request::url(null, 'user'), 'navigation.user'));
  288. if ($subclass) $pageHierarchy[] = array(Request::url(null, 'counter'), 'plugins.generic.counter');
  289. $templateMgr->assign_by_ref('pageHierarchy', $pageHierarchy);
  290. }
  291. }
  292. ?>