<?php
/*
 * Copyright (c) 2014-2015 Aimy Extensions, Lingua-Systems Software GmbH
 *
 * http://www.aimy-extensions.com/
 *
 * License: GNU GPLv2, see LICENSE.txt within distribution and/or
 *          http://www.aimy-extensions.com/software-license.html
 */
 defined( '_JEXEC' ) or die(); jimport( 'joomla.session.session' ); require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'rights.php' ); require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'config.php' ); require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'Crawler.php' ); class AimySitemapControllerCrawl extends JControllerAdmin { public function crawl_init_ajax() { JSession::checkToken() or jexit( JText::_( 'INVALID TOKEN' ) ); $rights = AimySitemapRightsHelper::getRights(); if ( ! $rights->get( 'aimysitemap.crawl' ) ) { jexit( JText::_( 'JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN' ) ); } require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'kvstore.php' ); AimySitemapKVStore::delete( 'rebuilding' ); header( 'Content-Type: application/json; charset=UTF-8' ); $crawler = new AimySitemapCrawler(); $res = null; try { $res = $crawler->initialize(); } catch ( Exception $e ) { AimySitemapLogger::error( "Crawl-Init-Ctrl: Exception: $e" ); error_log( "$e" ); $res = array( 'ok' => false, 'msg' => $e->getMessage() ); } echo json_encode( $res ), "\n"; JFactory::getApplication()->close(); } public function crawl_ajax() { JSession::checkToken() or jexit( JText::_( 'INVALID TOKEN' ) ); $rights = AimySitemapRightsHelper::getRights(); if ( ! $rights->get( 'aimysitemap.crawl' ) ) { jexit( JText::_( 'JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN' ) ); } header( 'Content-Type: application/json; charset=UTF-8' ); $crawler = new AimySitemapCrawler(); $res = null; try { $res = $crawler->crawl(); } catch ( Exception $e ) { AimySitemapLogger::error( "Crawl-Ctrl: Exception: $e" ); error_log( "$e" ); echo json_encode(array( 'ok' => false, 'msg' => $e->getMessage() )), "\n"; return JFactory::getApplication()->close(); } if ( isset( $res[ 'url' ] ) ) { AimySitemapLogger::debug( 'Crawl: done' . ( isset( $res[ 'msg' ] ) ? ( ': ' . $res[ 'msg' ] ) : '' ) ); } if ( isset( $res[ 'abort' ] ) && $res[ 'abort' ] ) { AimySitemapLogger::debug( 'Crawl: ' . ( ( isset( $res[ 'ok' ] ) && $res[ 'ok' ] ) ? 'finished' : 'aborted' ) ); if ( isset( $res[ 'ok' ] ) && $res[ 'ok' ] ) { if ( defined( 'JDEBUG' ) && JDEBUG ) { $res = array( 'ok' => 1, 'abort' => 1, 'msg' => JText::_( 'AIMY_SM_MSG_VIEW_LOG' ), 'stats' => array() ); } else { require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'helpers' . DIRECTORY_SEPARATOR . 'kvstore.php' ); if ( AimySitemapKVStore::get( 'rebuilding' ) ) { $res = array( 'ok' => 1, 'msg' => JText::_( 'AIMY_SM_MSG_REBUILDING' ), 'rebuilding' => 1 ); } else { AimySitemapKVStore::set( 'rebuilding', 1 ); require_once( JPATH_COMPONENT . DIRECTORY_SEPARATOR . 'Sitemap.php' ); $sm = new AimySitemapSitemap(); $res[ 'stats' ] = $sm->rebuild(); AimySitemapCrawler::delete_crawl_data(); AimySitemapKVStore::delete( 'rebuilding' ); } } } } echo json_encode( $res ), "\n"; JFactory::getApplication()->close(); } public function get_log() { JSession::checkToken( 'get' ) or jexit( JText::_( 'INVALID TOKEN' ) ); $rights = AimySitemapRightsHelper::getRights(); if ( ! $rights->get( 'aimysitemap.crawl' ) ) { jexit( JText::_( 'JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN' ) ); } if ( ! defined( 'JDEBUG' ) or ! JDEBUG ) { jexit( 'This functionality is available in debugging mode only' ); } $path = AimySitemapLogger::get_path(); if ( ! is_readable( $path ) ) { throw new Exception( 'File not found', 404 ); } $fn = basename( $path ) . '.txt'; header( 'Content-Type: text/plain' ); header( 'Content-Disposition: attachment; filename=' . $fn ); readfile( $path ); JFactory::getApplication()->close(); } } 
