Пример #1
0
 /**
  * @param array $cxn
  * @param string $entity
  * @param string $action
  * @param array $params
  * @return mixed
  */
 public static function route($cxn, $entity, $action, $params)
 {
     $SUPER_PERM = array('administer CiviCRM');
     require_once 'api/v3/utils.php';
     // FIXME: Shouldn't the X-Forwarded-Proto check be part of CRM_Utils_System::isSSL()?
     if (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'enableSSL') && !CRM_Utils_System::isSSL() && strtolower(CRM_Utils_Array::value('X_FORWARDED_PROTO', CRM_Utils_System::getRequestHeaders())) != 'https') {
         return civicrm_api3_create_error('System policy requires HTTPS.');
     }
     // Note: $cxn and cxnId are authenticated before router is called.
     $dao = new CRM_Cxn_DAO_Cxn();
     $dao->cxn_id = $cxn['cxnId'];
     if (empty($cxn['cxnId']) || !$dao->find(TRUE) || !$dao->cxn_id) {
         return civicrm_api3_create_error('Failed to lookup connection authorizations.');
     }
     if (!$dao->is_active) {
         return civicrm_api3_create_error('Connection is inactive.');
     }
     if (!is_string($entity) || !is_string($action) || !is_array($params)) {
         return civicrm_api3_create_error('API parameters are malformed.');
     }
     if (empty($cxn['perm']['api']) || !is_array($cxn['perm']['api']) || empty($cxn['perm']['grant']) || !(is_array($cxn['perm']['grant']) || is_string($cxn['perm']['grant']))) {
         return civicrm_api3_create_error('Connection has no permissions.');
     }
     $whitelist = \Civi\API\WhitelistRule::createAll($cxn['perm']['api']);
     Civi\Core\Container::singleton()->get('dispatcher')->addSubscriber(new \Civi\API\Subscriber\WhitelistSubscriber($whitelist));
     CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp();
     if ($cxn['perm']['grant'] === '*') {
         CRM_Core_Config::singleton()->userPermissionTemp->grant($SUPER_PERM);
     } else {
         CRM_Core_Config::singleton()->userPermissionTemp->grant($cxn['perm']['grant']);
     }
     $params['check_permissions'] = 'whitelist';
     return civicrm_api($entity, $action, $params);
 }
Пример #2
0
 public function run()
 {
     $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire('worker.report.CiviReportMail');
     if ($lock->isAcquired()) {
         // try to unset any time limits
         if (!ini_get('safe_mode')) {
             set_time_limit(0);
         }
         // if there are named sets of settings, use them - otherwise use the default (null)
         require_once 'CRM/Report/Utils/Report.php';
         $result = CRM_Report_Utils_Report::processReport();
         echo $result['messages'];
     } else {
         throw new Exception('Could not acquire lock, another CiviReportMail process is running');
     }
     $lock->release();
 }
Пример #3
0
        CRM_Utils_Mail_EmailProcessor::processActivities();
    } else {
        CRM_Utils_Mail_EmailProcessor::processBounces();
    }
    $lock->release();
} else {
    session_start();
    require_once '../civicrm.config.php';
    require_once 'CRM/Core/Config.php';
    $config = CRM_Core_Config::singleton();
    CRM_Utils_System::authenticateScript(TRUE);
    require_once 'CRM/Utils/System.php';
    CRM_Utils_System::loadBootStrap();
    //log the execution of script
    CRM_Core_Error::debug_log_message('EmailProcessor.php');
    $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire('worker.mailing.EmailProcessor');
    if (!$lock->isAcquired()) {
        throw new Exception('Could not acquire lock, another EmailProcessor process is running');
    }
    // try to unset any time limits
    if (!ini_get('safe_mode')) {
        set_time_limit(0);
    }
    require_once 'CRM/Utils/Mail/EmailProcessor.php';
    // cleanup directories with old mail files (if they exist): CRM-4452
    CRM_Utils_Mail_EmailProcessor::cleanupDir($config->customFileUploadDir . DIRECTORY_SEPARATOR . 'CiviMail.ignored');
    CRM_Utils_Mail_EmailProcessor::cleanupDir($config->customFileUploadDir . DIRECTORY_SEPARATOR . 'CiviMail.processed');
    // check if the script is being used for civimail processing or email to
    // activity processing.
    $isCiviMail = !empty($_REQUEST['emailtoactivity']) ? FALSE : TRUE;
    CRM_Utils_Mail_EmailProcessor::process($isCiviMail);
Пример #4
0
 /**
  * Reset all ephemeral system state, e.g. statics,
  * singletons, containers.
  */
 public static function reset()
 {
     self::$statics = array();
     Civi\Core\Container::singleton();
 }
Пример #5
0
 /**
  * Load the smart group cache for a saved search.
  *
  * @param object $group
  *   The smart group that needs to be loaded.
  * @param bool $force
  *   Should we force a search through.
  */
 public static function load(&$group, $force = FALSE)
 {
     $groupID = $group->id;
     $savedSearchID = $group->saved_search_id;
     if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) {
         return;
     }
     // grab a lock so other processes dont compete and do the same query
     $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("data.core.group.{$groupID}");
     if (!$lock->isAcquired()) {
         // this can cause inconsistent results since we dont know if the other process
         // will fill up the cache before our calling routine needs it.
         // however this routine does not return the status either, so basically
         // its a "lets return and hope for the best"
         return;
     }
     self::$_alreadyLoaded[$groupID] = 1;
     // we now have the lock, but some other proces could have actually done the work
     // before we got here, so before we do any work, lets ensure that work needs to be
     // done
     // we allow hidden groups here since we dont know if the caller wants to evaluate an
     // hidden group
     if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) {
         $lock->release();
         return;
     }
     $sql = NULL;
     $idName = 'id';
     $customClass = NULL;
     if ($savedSearchID) {
         $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID);
         // rectify params to what proximity search expects if there is a value for prox_distance
         // CRM-7021
         if (!empty($ssParams)) {
             CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams);
         }
         $returnProperties = array();
         if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) {
             $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
             $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv);
         }
         if (isset($ssParams['customSearchID'])) {
             // if custom search
             // we split it up and store custom class
             // so temp tables are not destroyed if they are used
             // hence customClass is defined above at top of function
             $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID);
             $searchSQL = $customClass->contactIDs();
             $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL);
             if (!strstr($searchSQL, 'WHERE')) {
                 $searchSQL .= " WHERE ( 1 ) ";
             }
             $idName = 'contact_id';
         } else {
             $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID);
             // CRM-17075 using the formValues in this way imposes extra logic and complexity.
             // we have the where_clause and where tables stored in the saved_search table
             // and should use these rather than re-processing the form criteria (which over-works
             // the link between the form layer & the query layer too).
             // It's hard to think of when you would want to use anything other than return
             // properties = array('contact_id' => 1) here as the point would appear to be to
             // generate the list of contact ids in the group.
             // @todo review this to use values in saved_search table (preferably for 4.8).
             $query = new CRM_Contact_BAO_Query($ssParams, $returnProperties, NULL, FALSE, FALSE, 1, TRUE, TRUE, FALSE, CRM_Utils_Array::value('display_relationship_type', $formValues), CRM_Utils_Array::value('operator', $formValues, 'AND'));
             $query->_useDistinct = FALSE;
             $query->_useGroupBy = FALSE;
             $searchSQL = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, TRUE, TRUE, NULL, NULL, NULL, TRUE);
         }
         $groupID = CRM_Utils_Type::escape($groupID, 'Integer');
         $sql = $searchSQL . " AND contact_a.id NOT IN (\n                              SELECT contact_id FROM civicrm_group_contact\n                              WHERE civicrm_group_contact.status = 'Removed'\n                              AND   civicrm_group_contact.group_id = {$groupID} ) ";
     }
     if ($sql) {
         $sql = preg_replace("/^\\s*SELECT/", "SELECT {$groupID} as group_id, ", $sql);
     }
     // lets also store the records that are explicitly added to the group
     // this allows us to skip the group contact LEFT JOIN
     $sqlB = "\nSELECT {$groupID} as group_id, contact_id as {$idName}\nFROM   civicrm_group_contact\nWHERE  civicrm_group_contact.status = 'Added'\n  AND  civicrm_group_contact.group_id = {$groupID} ";
     $groupIDs = array($groupID);
     self::remove($groupIDs);
     $processed = FALSE;
     $tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000);
     foreach (array($sql, $sqlB) as $selectSql) {
         if (!$selectSql) {
             continue;
         }
         $insertSql = "CREATE TEMPORARY TABLE {$tempTable} ({$selectSql});";
         $processed = TRUE;
         $result = CRM_Core_DAO::executeQuery($insertSql);
         CRM_Core_DAO::executeQuery("INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)\n        SELECT DISTINCT {$idName}, group_id FROM {$tempTable}\n      ");
         CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE {$tempTable}");
     }
     self::updateCacheTime($groupIDs, $processed);
     if ($group->children) {
         //Store a list of contacts who are removed from the parent group
         $sql = "\nSELECT contact_id\nFROM civicrm_group_contact\nWHERE  civicrm_group_contact.status = 'Removed'\nAND  civicrm_group_contact.group_id = {$groupID} ";
         $dao = CRM_Core_DAO::executeQuery($sql);
         $removed_contacts = array();
         while ($dao->fetch()) {
             $removed_contacts[] = $dao->contact_id;
         }
         $childrenIDs = explode(',', $group->children);
         foreach ($childrenIDs as $childID) {
             $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE);
             //Unset each contact that is removed from the parent group
             foreach ($removed_contacts as $removed_contact) {
                 unset($contactIDs[$removed_contact]);
             }
             $values = array();
             foreach ($contactIDs as $contactID => $dontCare) {
                 $values[] = "({$groupID},{$contactID})";
             }
             self::store($groupIDs, $values);
         }
     }
     $lock->release();
 }
 /**
  * @param null $mode
  *
  * @return bool
  * @throws Exception
  */
 public static function processQueue($mode = NULL)
 {
     $config = CRM_Core_Config::singleton();
     if ($mode == NULL && CRM_Core_BAO_MailSettings::defaultDomain() == "EXAMPLE.ORG") {
         throw new CRM_Core_Exception(ts('The <a href="%1">default mailbox</a> has not been configured. You will find <a href="%2">more info in the online user and administrator guide</a>', array(1 => CRM_Utils_System::url('civicrm/admin/mailSettings', 'reset=1'), 2 => "http://book.civicrm.org/user/advanced-configuration/email-system-configuration/")));
     }
     // check if we are enforcing number of parallel cron jobs
     // CRM-8460
     $gotCronLock = FALSE;
     if (property_exists($config, 'mailerJobsMax') && $config->mailerJobsMax && $config->mailerJobsMax > 0) {
         $lockArray = range(1, $config->mailerJobsMax);
         shuffle($lockArray);
         // check if we are using global locks
         foreach ($lockArray as $lockID) {
             $cronLock = Civi\Core\Container::singleton()->get('lockManager')->acquire("worker.mailing.send.{$lockID}");
             if ($cronLock->isAcquired()) {
                 $gotCronLock = TRUE;
                 break;
             }
         }
         // exit here since we have enuf cronjobs running
         if (!$gotCronLock) {
             CRM_Core_Error::debug_log_message('Returning early, since max number of cronjobs running');
             return TRUE;
         }
         if (getenv('CIVICRM_CRON_HOLD')) {
             // In testing, we may need to simulate some slow activities.
             sleep(getenv('CIVICRM_CRON_HOLD'));
         }
     }
     // load bootstrap to call hooks
     // Split up the parent jobs into multiple child jobs
     $mailerJobSize = property_exists($config, 'mailerJobSize') ? $config->mailerJobSize : NULL;
     CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, $mode);
     CRM_Mailing_BAO_MailingJob::runJobs(NULL, $mode);
     CRM_Mailing_BAO_MailingJob::runJobs_post($mode);
     // lets release the global cron lock if we do have one
     if ($gotCronLock) {
         $cronLock->release();
     }
     return TRUE;
 }
Пример #7
0
 /**
  * before we run jobs, we need to split the jobs
  * @param int $offset
  * @param null $mode
  */
 public static function runJobs_pre($offset = 200, $mode = NULL)
 {
     $job = new CRM_Mailing_BAO_MailingJob();
     $jobTable = CRM_Mailing_DAO_MailingJob::getTableName();
     $mailingTable = CRM_Mailing_DAO_Mailing::getTableName();
     $currentTime = date('YmdHis');
     $mailingACL = CRM_Mailing_BAO_Mailing::mailingACL('m');
     $workflowClause = CRM_Mailing_BAO_MailingJob::workflowClause();
     $domainID = CRM_Core_Config::domainID();
     $modeClause = 'AND m.sms_provider_id IS NULL';
     if ($mode == 'sms') {
         $modeClause = 'AND m.sms_provider_id IS NOT NULL';
     }
     // Select all the mailing jobs that are created from
     // when the mailing is submitted or scheduled.
     $query = "\n    SELECT   j.*\n      FROM   {$jobTable}     j,\n         {$mailingTable} m\n     WHERE   m.id = j.mailing_id AND m.domain_id = {$domainID}\n                 {$workflowClause}\n                 {$modeClause}\n       AND   j.is_test = 0\n       AND   ( ( j.start_date IS null\n       AND       j.scheduled_date <= {$currentTime}\n       AND       j.status = 'Scheduled'\n       AND       j.end_date IS null ) )\n       AND ((j.job_type is NULL) OR (j.job_type <> 'child'))\n    ORDER BY j.scheduled_date,\n         j.start_date";
     $job->query($query);
     // For each of the "Parent Jobs" we find, we split them into
     // X Number of child jobs
     while ($job->fetch()) {
         // still use job level lock for each child job
         $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("data.mailing.job.{$job->id}");
         if (!$lock->isAcquired()) {
             continue;
         }
         // Re-fetch the job status in case things
         // changed between the first query and now
         // to avoid race conditions
         $job->status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $job->id, 'status', 'id', TRUE);
         if ($job->status != 'Scheduled') {
             $lock->release();
             continue;
         }
         $job->split_job($offset);
         // update the status of the parent job
         $transaction = new CRM_Core_Transaction();
         $saveJob = new CRM_Mailing_DAO_MailingJob();
         $saveJob->id = $job->id;
         $saveJob->start_date = date('YmdHis');
         $saveJob->status = 'Running';
         $saveJob->save();
         $transaction->commit();
         // Release the job lock
         $lock->release();
     }
 }
            } else {
                $params['contribution_recur_id'] = $recurring->id;
            }
        }
        $contribution =& CRM_Contribute_BAO_Contribution::create($params, CRM_Core_DAO::$_nullArray);
        if (!$contribution->id) {
            return FALSE;
        }
        return TRUE;
    }
}
// bootstrap the environment and run the processor
session_start();
require_once '../civicrm.config.php';
require_once 'CRM/Core/Config.php';
$config = CRM_Core_Config::singleton();
CRM_Utils_System::authenticateScript(TRUE);
//log the execution of script
CRM_Core_Error::debug_log_message('ContributionProcessor.php');
$lock = Civi\Core\Container::singleton()->get('lockManager')->acquire('worker.contribute.CiviContributeProcessor');
if ($lock->isAcquired()) {
    // try to unset any time limits
    if (!ini_get('safe_mode')) {
        set_time_limit(0);
    }
    CiviContributeProcessor::process();
} else {
    throw new Exception('Could not acquire lock, another CiviContributeProcessor process is running');
}
$lock->release();
echo "Done processing<p>";
Пример #9
0
 /**
  * Store an item in the DB cache.
  *
  * @param object $data
  *   (required) A reference to the data that will be serialized and stored.
  * @param string $group
  *   (required) The group name of the item.
  * @param string $path
  *   (required) The path under which this item is stored.
  * @param int $componentID
  *   The optional component ID (so componenets can share the same name space).
  */
 public static function setItem(&$data, $group, $path, $componentID = NULL)
 {
     if (self::$_cache === NULL) {
         self::$_cache = array();
     }
     $dao = new CRM_Core_DAO_Cache();
     $dao->group_name = $group;
     $dao->path = $path;
     $dao->component_id = $componentID;
     // get a lock so that multiple ajax requests on the same page
     // dont trample on each other
     // CRM-11234
     $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire("cache.{$group}_{$path}._{$componentID}");
     if (!$lock->isAcquired()) {
         CRM_Core_Error::fatal();
     }
     $dao->find(TRUE);
     $dao->data = serialize($data);
     $dao->created_date = date('YmdHis');
     $dao->save();
     $lock->release();
     $dao->free();
     // cache coherency - refresh or remove dependent caches
     $argString = "CRM_CT_{$group}_{$path}_{$componentID}";
     $cache = CRM_Utils_Cache::singleton();
     $data = unserialize($dao->data);
     self::$_cache[$argString] = $data;
     $cache->set($argString, $data);
     $argString = "CRM_CT_CI_{$group}_{$componentID}";
     unset(self::$_cache[$argString]);
     $cache->delete($argString);
 }
Пример #10
0
/**
 * This api reloads all the smart groups.
 *
 * If the org has a large number of smart groups it is recommended that they use the limit clause
 * to limit the number of smart groups evaluated on a per job basis.
 *
 * Might also help to increase the smartGroupCacheTimeout and use the cache.
 *
 * @param array $params
 *
 * @return array
 * @throws \API_Exception
 */
function civicrm_api3_job_group_rebuild($params)
{
    $lock = Civi\Core\Container::singleton()->get('lockManager')->acquire('worker.core.GroupRebuild');
    if (!$lock->isAcquired()) {
        throw new API_Exception('Could not acquire lock, another EmailProcessor process is running');
    }
    $limit = CRM_Utils_Array::value('limit', $params, 0);
    CRM_Contact_BAO_GroupContactCache::loadAll(NULL, $limit);
    $lock->release();
    return civicrm_api3_create_success();
}
Пример #11
0
 /**
  *  Common setup functions for all unit tests.
  */
 protected function setUp()
 {
     $session = CRM_Core_Session::singleton();
     $session->set('userID', NULL);
     $this->errorScope = CRM_Core_TemporaryErrorScope::useException();
     // REVERT
     //  Use a temporary file for STDIN
     $GLOBALS['stdin'] = tmpfile();
     if ($GLOBALS['stdin'] === FALSE) {
         echo "Couldn't open temporary file\n";
         exit(1);
     }
     //  Get and save a connection to the database
     $this->_dbconn = $this->getConnection();
     // reload database before each test
     //        $this->_populateDB();
     // "initialize" CiviCRM to avoid problems when running single tests
     // FIXME: look at it closer in second stage
     // initialize the object once db is loaded
     $config = CRM_Core_Config::singleton();
     Civi\Core\Container::singleton(TRUE);
     // when running unit tests, use mockup user framework
     $config->setUserFramework('UnitTests');
     $this->hookClass = CRM_Utils_Hook::singleton(TRUE);
     // also fix the fatal error handler to throw exceptions,
     // rather than exit
     $config->fatalErrorHandler = 'CiviUnitTestCase_fatalErrorHandler';
     // enable backtrace to get meaningful errors
     $config->backtrace = 1;
     // disable any left-over test extensions
     CRM_Core_DAO::executeQuery('DELETE FROM civicrm_extension WHERE full_name LIKE "test.%"');
     // reset all the caches
     CRM_Utils_System::flushCache();
     // Make sure the DB connection is setup properly
     $config->userSystem->setMySQLTimeZone();
     $env = new CRM_Utils_Check_Env();
     CRM_Utils_Check::singleton()->assertValid($env->checkMysqlTime());
     // clear permissions stub to not check permissions
     $config = CRM_Core_Config::singleton();
     $config->userPermissionClass->permissions = NULL;
     //flush component settings
     CRM_Core_Component::getEnabledComponents(TRUE);
     error_reporting(E_ALL);
     $this->_sethtmlGlobals();
 }
Пример #12
0
 /**
  * Retrieve a mailer to send any mail from the application.
  *
  * @return Mail
  * @deprecated
  */
 public static function getMailer()
 {
     return Civi\Core\Container::singleton()->get('pear_mail');
 }