Skip to content
Snippets Groups Projects
Commit e5007c21 authored by Peter Rotich's avatar Peter Rotich
Browse files

Move http url config to http.php

Implement remote cron as api task (more to come)
parent 382a12d1
Branches
Tags
No related merge requests found
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
/********************************************************************* /*********************************************************************
cron.php cron.php
File to handle cron job calls (local and remote). File to handle LOCAL cron job calls.
Peter Rotich <peter@osticket.com> Peter Rotich <peter@osticket.com>
Copyright (c) 2006-2012 osTicket Copyright (c) 2006-2012 osTicket
...@@ -13,9 +13,11 @@ ...@@ -13,9 +13,11 @@
vim: expandtab sw=4 ts=4 sts=4: vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/ **********************************************************************/
if (substr(php_sapi_name(), 0, 3) != 'cli')
die('cron.php only supports local cron jobs - use http -> api/task/cron');
@chdir(realpath(dirname(__FILE__)).'/'); //Change dir. @chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
require('api.inc.php'); require('api.inc.php');
require_once(INCLUDE_DIR.'class.cron.php'); require_once(INCLUDE_DIR.'api.cron.php');
Cron::run(); LocalCronApiController::call();
$ost->logDebug('Cron Job','External cron job executed ['.$_SERVER['REMOTE_ADDR'].']');
?> ?>
...@@ -17,9 +17,14 @@ require 'api.inc.php'; ...@@ -17,9 +17,14 @@ require 'api.inc.php';
# Include the main api urls # Include the main api urls
require_once INCLUDE_DIR."class.dispatcher.php"; require_once INCLUDE_DIR."class.dispatcher.php";
$dispatcher = Dispatcher::include_urls("urls.conf.php");
# Call the respective function $dispatcher = patterns('',
$dispatcher->resolve($_SERVER['PATH_INFO']); url_post("^/tickets\.(?P<format>xml|json|email)$", array('api.tickets.php:TicketApiController','create')),
url('^/task/', patterns('',
url_get("^cron$", array('api.cron.php:CronApiController', 'execute'))
))
);
# Call the respective function
print $dispatcher->resolve($_SERVER['PATH_INFO']);
?> ?>
<?php
# What about patterns("api.tickets.php:TicketApiController", ...) since if the
# class is given as the prefix, it isn't defined in more than one file. This
# would allow for speficying imports only if an item is defined in a
# different class (with the array("class", "method") syntax)
return patterns('api.tickets.php:TicketApiController',
url_post("^/tickets\.(?P<format>xml|json|email)$", 'create')
);
?>
<?php
include_once INCLUDE_DIR.'class.cron.php';
class CronApiController extends ApiController {
function execute() {
if(!($key=$this->requireApiKey()) || !$key->canExecuteCronJob())
return $this->exerr(401, 'API key not authorized');
$this->run();
}
/* private */
function run() {
global $ost;
Cron::run();
$ost->logDebug('Cron Job','Cron job executed ['.$_SERVER['REMOTE_ADDR'].']');
$this->response(200,'Completed');
}
}
class LocalCronApiController extends CronApiController {
function response($code, $resp) {
if($code == 200) //Success - exit silently.
exit(0);
//On error echo the response (error)
echo $resp;
exit(1);
}
function call() {
$cron = new LocalCronApiController();
$cron->run();
}
}
?>
...@@ -71,6 +71,10 @@ class API { ...@@ -71,6 +71,10 @@ class API {
return ($this->ht['can_create_tickets']); return ($this->ht['can_create_tickets']);
} }
function canExecuteCronjob() {
return true;
}
function update($vars, &$errors) { function update($vars, &$errors) {
if(!API::save($this->getId(), $vars, $errors)) if(!API::save($this->getId(), $vars, $errors))
...@@ -166,7 +170,7 @@ class ApiController { ...@@ -166,7 +170,7 @@ class ApiController {
# header # header
if(!($key=$this->getApiKey())) if(!($key=$this->getApiKey()))
return $this->exerr(401, 'API key required'); return $this->exerr(401, 'Valid API key required');
elseif (!$key->isActive() || $key->getIPAddr()!=$_SERVER['REMOTE_ADDR']) elseif (!$key->isActive() || $key->getIPAddr()!=$_SERVER['REMOTE_ADDR'])
return $this->exerr(401, 'API key not found/active or source IP not authorized'); return $this->exerr(401, 'API key not found/active or source IP not authorized');
......
...@@ -39,6 +39,7 @@ images/ticket_status_title.jpg ...@@ -39,6 +39,7 @@ images/ticket_status_title.jpg
include/settings.php include/settings.php
# Removed in 1.7.0 # Removed in 1.7.0
api/urls.conf.php
images/bg.gif images/bg.gif
images/fibres.png images/fibres.png
images/home.gif images/home.gif
......
#!/usr/bin/php -q
<?php
/*********************************************************************
rcron.php
PHP script used for remote cron calls.
Peter Rotich <peter@osticket.com>
Copyright (c) 2006-2013 osTicket
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
# Configuration: Enter the url and key. That is it.
# url => URL to api/task/cron e.g http://yourdomain.com/support/api/task/cron
# key => API's Key (see admin panel on how to generate a key)
#
$config = array(
'url'=>'http://yourdomain.com/support/api/task/cron',
'key'=>'API KEY HERE'
);
#pre-checks
function_exists('curl_version') or die('CURL support required');
#set timeout
set_time_limit(30);
#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url']);
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.7');
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
curl_close($ch);
if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status) && $status[1] == 200)
exit(0);
echo $result;
exit(1);
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment