CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Securely using SQL files in an extension
Pages: [1]

Author Topic: Securely using SQL files in an extension  (Read 645 times)

JohnFF

  • I post frequently
  • ***
  • Posts: 235
  • Karma: 6
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.28
  • MySQL version: 5.5.31-1
  • PHP version: 5.3.27
Securely using SQL files in an extension
September 15, 2013, 10:58:39 am
Hi guys,

I'm trying to design my extension as I mean to go on, and make sure that my beautiful PHP code isn't littered with clunky SQL files.

I attempted to use file_get_contents, but it didn't seem to work.

My questions are:

1) Where in the extension folder should the file be?
2) What php function should I use to call it, bearing in mind security?
If you like empowering charities in a free and open way, then you're going to love Civi.

Email Amender: https://civicrm.org/extensions/email-amender
UK Phone Validator: https://civicrm.org/extensions/uk-phone-number-validator
http://civifirst.com
https://twitter.com/civifirst

Erik Hommel

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1773
  • Karma: 59
    • EE-atWork
  • CiviCRM version: all sorts
  • CMS version: Drupal
  • MySQL version: Ubuntu's latest LTS version
  • PHP version: Ubuntu's latest LTS version
Re: Securely using SQL files in an extension
September 16, 2013, 05:53:59 am
Quote
1) Where in the extension folder should the file be?
Create (if it does not exists yet) a folder 'sql' within your extension folder, and put the SQL files there?

Quote
2) What php function should I use to call it, bearing in mind security?
To get the file? And you mean a file with your SQL statements?

So I am not totally sure what you are trying to achieve? If you have separate files holding your SQL statements, surely you can use a class to hold it and call static functions?
Consultant/project manager at EEatWork and CiviCooP (http://www.civicoop.org/)

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: Securely using SQL files in an extension
September 16, 2013, 02:28:27 pm
Agree with Erik's comments. To add a little more:

2a) If the goal is to execute a long SQL file (e.g. as part of the installation or upgrade process), you should use CRM_Utils_File::sourceSQLFile -- e.g. https://github.com/civicrm/civicrm-core/blob/master/CRM/Utils/File.php#L255

2b) If the goal is to write a single SQL statement without fear of SQL injections, you can use CRM_Core_DAO::executeQuery ans pass in arguments. A common formulation you see in Civi's code is:

Code: [Select]
<?php
$sql 
= "SELECT id, first_name, last_name FROM civicrm_contact WHERE external_id = %1";
$params = array(
  
1 => array("987-65-4321", "String")
);
$query = CRM_Core_DAO::executeQuery($sql, $params);

2c) If the goal is provide a little bit of structure so that SQL queries (like 2b) aren't littering the codebase, you might put the code in a BAO helper function, e.g.

Code: [Select]
<?php
class CRM_Myextension_BAO_Myentity {
  static function 
findByExternalId($externalId) {
    
$sql = "SELECT * FROM civicrm_my_entity WHERE external_id = %1";
    
$params = array(
      
1 => array($externalId, "String")
    );
    
$query = CRM_Core_DAO::executeQuery($sql, $params);
    
// ...
  
}
}

JohnFF

  • I post frequently
  • ***
  • Posts: 235
  • Karma: 6
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.28
  • MySQL version: 5.5.31-1
  • PHP version: 5.3.27
Re: Securely using SQL files in an extension
October 18, 2013, 03:15:04 am
Hi guys,

Thanks for your responses!

The intent was to remove long and complex SQL statements from an otherwise small and tidy codebase. I will look into each of the options given.
If you like empowering charities in a free and open way, then you're going to love Civi.

Email Amender: https://civicrm.org/extensions/email-amender
UK Phone Validator: https://civicrm.org/extensions/uk-phone-number-validator
http://civifirst.com
https://twitter.com/civifirst

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Securely using SQL files in an extension

This forum was archived on 2017-11-26.