From f226f71efd8dff89b9fc3c2339ef7b8cce740ac4 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Thu, 14 Feb 2013 17:18:17 -0500
Subject: [PATCH] Add setting to enable cron service (external) at API key
 level Remove piping setting option - it's now part of ticket create service
 (which include XML & JSON)

---
 include/class.api.php                           | 11 ++++++-----
 include/class.config.php                        |  5 -----
 include/staff/apikey.inc.php                    | 16 +++++++++++++---
 include/staff/settings-emails.inc.php           | 10 ++--------
 include/upgrader/sql/c0fd16f4-959a00e.patch.sql | 17 +++++++++++++++++
 main.inc.php                                    |  2 +-
 setup/inc/sql/osTicket-mysql.sql                |  2 +-
 setup/inc/sql/osTicket-mysql.sql.md5            |  2 +-
 8 files changed, 41 insertions(+), 24 deletions(-)
 create mode 100644 include/upgrader/sql/c0fd16f4-959a00e.patch.sql

diff --git a/include/class.api.php b/include/class.api.php
index 48e968df5..f5d2ef5c7 100644
--- a/include/class.api.php
+++ b/include/class.api.php
@@ -72,7 +72,7 @@ class API {
     }
 
     function canExecuteCronjob() {
-        return true;
+        return ($this->ht['can_exec_cron']);
     }
 
     function update($vars, &$errors) {
@@ -129,6 +129,7 @@ class API {
         $sql=' updated=NOW() '
             .',isactive='.db_input($vars['isactive'])
             .',can_create_tickets='.db_input($vars['can_create_tickets'])
+            .',can_exec_cron='.db_input($vars['can_exec_cron'])
             .',notes='.db_input($vars['notes']);
 
         if($id) {
@@ -265,11 +266,11 @@ class ApiController {
         if($error && is_array($error))
             $error = Format::array_implode(": ", "\n", $error);
 
-        //Include api key - if available.
+        //Log the error as a warning - include api key if available.
+        $msg = $error;
         if($_SERVER['HTTP_X_API_KEY'])
-            $error.="\n\n*[".$_SERVER['HTTP_X_API_KEY']."]*\n";
-
-        $ost->logWarning("API Error ($code)", $error, false);
+            $msg.="\n*[".$_SERVER['HTTP_X_API_KEY']."]*\n";
+        $ost->logWarning("API Error ($code)", $msg, false);
 
         $this->response($code, $error); //Responder should exit...
         return false;
diff --git a/include/class.config.php b/include/class.config.php
index c7e966974..50a3174c6 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -336,10 +336,6 @@ class Config {
         return ($this->config['enable_mail_polling']);
     }
         
-    function isEmailPipingEnabled() {
-        return ($this->config['enable_email_piping']);
-    }
-
     function allowPriorityChange() {
         return ($this->config['allow_priority_change']);
     }
@@ -723,7 +719,6 @@ class Config {
             .',admin_email='.db_input($vars['admin_email'])
             .',enable_auto_cron='.db_input(isset($vars['enable_auto_cron'])?1:0)
             .',enable_mail_polling='.db_input(isset($vars['enable_mail_polling'])?1:0)
-            .',enable_email_piping='.db_input(isset($vars['enable_email_piping'])?1:0)
             .',strip_quoted_reply='.db_input(isset($vars['strip_quoted_reply'])?1:0)
             .',reply_separator='.db_input($vars['reply_separator'])
             .' WHERE id='.db_input($this->getId());
diff --git a/include/staff/apikey.inc.php b/include/staff/apikey.inc.php
index 6e2ffe6e6..507753aae 100644
--- a/include/staff/apikey.inc.php
+++ b/include/staff/apikey.inc.php
@@ -71,13 +71,23 @@ $info=Format::htmlchars(($errors && $_POST)?$_POST:$info);
         <?php } ?>
         <tr>
             <th colspan="2">
-                <em><strong>Enabled Services:</strong>: Check applicable API services. All active keys can make cron call.</em>
+                <em><strong>Services:</strong>: Check applicable API services enabled for the key.</em>
             </th>
         </tr>
         <tr>
             <td colspan=2 style="padding-left:5px">
-                <input type="checkbox" name="can_create_tickets" value="1" <?php echo $info['can_create_tickets']?'checked="checked"':''; ?> >
-                Can Create Tickets. <em>(XML/JSON/PIPE)</em>
+                <label>
+                    <input type="checkbox" name="can_create_tickets" value="1" <?php echo $info['can_create_tickets']?'checked="checked"':''; ?> >
+                    Can Create Tickets <em>(XML/JSON/EMAIL)</em>
+                </label>
+            </td>
+        </tr>
+        <tr>
+            <td colspan=2 style="padding-left:5px">
+                <label>
+                    <input type="checkbox" name="can_exec_cron" value="1" <?php echo $info['can_exec_cron']?'checked="checked"':''; ?> >
+                    Can Execute Cron
+                </label>
             </td>
         </tr>
         <tr>
diff --git a/include/staff/settings-emails.inc.php b/include/staff/settings-emails.inc.php
index f8674095d..b7175a06e 100644
--- a/include/staff/settings-emails.inc.php
+++ b/include/staff/settings-emails.inc.php
@@ -61,19 +61,13 @@ if(!defined('OSTADMININC') || !$thisstaff || !$thisstaff->isAdmin() || !$config)
                 &nbsp;&nbsp;<em>(System administrator's email)</em> 
             </td>
         </tr>
-        <tr><th colspan=2><em><strong>Incoming Emails</strong>: For mail fetcher (polling) to work you must set an external cron job or enable auto-cron</em></th>
+        <tr><th colspan=2><em><strong>Incoming Emails</strong>: For mail fetcher (polling) to work you must set an external cron job or enable auto-cron polling</em></th>
         <tr>
             <td width="180">Email Polling:</td>
             <td><input type="checkbox" name="enable_mail_polling" value=1 <?php echo $config['enable_mail_polling']? 'checked="checked"': ''; ?>  > Enable POP/IMAP polling
                  &nbsp;&nbsp;
                  <input type="checkbox" name="enable_auto_cron" <?php echo $config['enable_auto_cron']?'checked="checked"':''; ?>>
-                 Enable Auto-Cron <em>(Poll based on staff activity - NOT recommended)</em>
-            </td>
-        </tr>
-        <tr>
-            <td width="180">Email Piping:</td>
-            <td><input type="checkbox" name="enable_email_piping" value=1 <?php echo $config['enable_email_piping']? 'checked="checked"': ''; ?>> Enable email piping
-                 &nbsp;&nbsp;<em>(You pipe we accept policy)</em>
+                 Poll on auto-cron <em>(Poll based on staff activity - NOT recommended)</em>
             </td>
         </tr>
         <tr>
diff --git a/include/upgrader/sql/c0fd16f4-959a00e.patch.sql b/include/upgrader/sql/c0fd16f4-959a00e.patch.sql
new file mode 100644
index 000000000..186dfc5bb
--- /dev/null
+++ b/include/upgrader/sql/c0fd16f4-959a00e.patch.sql
@@ -0,0 +1,17 @@
+/**
+ * @version v1.7
+ *
+ * @schema d959a00e55c75e0c903b9e37324fd25d
+ */
+
+-- Add cron exec service
+ALTER TABLE  `%TABLE_PREFIX%api_key`
+    ADD  `can_exec_cron` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT  '1' AFTER  `can_create_tickets`;
+
+-- Drop email piping settings from config table.
+ALTER TABLE  `%TABLE_PREFIX%config` 
+    DROP  `enable_email_piping`;
+
+-- update schema signature
+UPDATE `%TABLE_PREFIX%config`
+    SET `schema_signature`='d959a00e55c75e0c903b9e37324fd25d';
diff --git a/main.inc.php b/main.inc.php
index 2dc651c99..2e7691f2b 100644
--- a/main.inc.php
+++ b/main.inc.php
@@ -63,7 +63,7 @@
 
     #Current version && schema signature (Changes from version to version)
     define('THIS_VERSION','1.7-RC4+'); //Shown on admin panel
-    define('SCHEMA_SIGNATURE', 'c0fd16f4eaf99b920be9f7fc6ebead32'); //MD5 signature of the db schema. (used to trigger upgrades)
+    define('SCHEMA_SIGNATURE', 'd959a00e55c75e0c903b9e37324fd25d'); //MD5 signature of the db schema. (used to trigger upgrades)
     #load config info
     $configfile='';
     if(file_exists(ROOT_DIR.'ostconfig.php')) //Old installs prior to v 1.6 RC5
diff --git a/setup/inc/sql/osTicket-mysql.sql b/setup/inc/sql/osTicket-mysql.sql
index ba980b876..3d14f86d5 100644
--- a/setup/inc/sql/osTicket-mysql.sql
+++ b/setup/inc/sql/osTicket-mysql.sql
@@ -6,6 +6,7 @@ CREATE TABLE `%TABLE_PREFIX%api_key` (
   `ipaddr` varchar(64) NOT NULL,
   `apikey` varchar(255) NOT NULL,
   `can_create_tickets` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT  '1',
+  `can_exec_cron` TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT  '1',
   `notes` text,
   `updated` datetime NOT NULL,
   `created` datetime NOT NULL,
@@ -97,7 +98,6 @@ CREATE TABLE `%TABLE_PREFIX%config` (
   `enable_captcha` tinyint(1) unsigned NOT NULL default '0',
   `enable_auto_cron` tinyint(1) unsigned NOT NULL default '0',
   `enable_mail_polling` tinyint(1) unsigned NOT NULL default '0',
-  `enable_email_piping` tinyint(1) unsigned NOT NULL default '0',
   `send_sys_errors` tinyint(1) unsigned NOT NULL default '1',
   `send_sql_errors` tinyint(1) unsigned NOT NULL default '1',
   `send_mailparse_errors` tinyint(1) unsigned NOT NULL default '1',
diff --git a/setup/inc/sql/osTicket-mysql.sql.md5 b/setup/inc/sql/osTicket-mysql.sql.md5
index db25b4569..d14b842c7 100644
--- a/setup/inc/sql/osTicket-mysql.sql.md5
+++ b/setup/inc/sql/osTicket-mysql.sql.md5
@@ -1 +1 @@
-c0fd16f4eaf99b920be9f7fc6ebead32
+d959a00e55c75e0c903b9e37324fd25d
-- 
GitLab