title: Exposing table name and field names in request URL
body: <p>I was tasked to create this Joomla component (yep, joomla; but its unrelated) and a professor told me that I should make my code as dynamic as possible (a code that needs less maintenance) and avoid hard coding. The approach we thought initially is take url parameters, turn them into objects, and pass them to query.</p>
  
  <p>Let's say we want to read hotel with id # 1 in the table "hotels". lets say the table has the fields "hotel_id", "hotel_name" and some other fields.</p>
  
  <p>Now, the approach we took in making the sql query string is to parse the url request that looked like this:</p>
  
  <pre><code>index.php?task=view&amp;table=hotels&amp;hotel_id=1&amp;param1=something&amp;param2=somethingelse
  </code></pre>
  
  <p>and turned it into a PHP object like this (shown in JSON equivalent, easier to understand):</p>
  
  <pre><code>obj = {
    'table':'hotel',
    'conditions':{
          'hotel_id':'1',
          'param1':'something',
          'param2':'somethingelse'
  }
  </code></pre>
  
  <p>and the SQL query will be something like this where conditions are looped and appended into the string where field and value of the WHERE clause are the key and value of the object (still in JSON form for ease):</p>
  
  <pre><code>SELECT * FROM obj.table WHERE hotel_id=1 AND param1=something and so on...
  </code></pre>
  
  <p>The problem that bugged me was the exposing of the table name and field names in the request url. I know it poses a security risk exposing items that should only be seen to the server side. The current solution I'm thinking is giving aliases to each and every table and field for the client side - but that would be hard coding, which is against his policy. and besides, if I did that, and had a thousand tables to alias, it would not be practical.</p>
  
  <p>What is the proper method to do this without: </p>
  
  <ol>
  <li>hard coding stuff </li>
  <li>keep the code as dynamic and adaptable</li>
  </ol>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Regarding the arbitrary queries (I forgot to include this), what currently stops them in the back end is a function, that takes a reference from a hard-coded object (more like a config file shown here), and parses the url by picking out parameters or matching them.</p>
  
  <p>The config looks like:</p>
  
  <pre><code>// 'hotels' here is the table name. instead of parsing the url for a table name
  // php will just find the table from this config. if no match, return error.
  // reduces risk of arbitrary tables.
  
  'hotels' =&gt; array(      
  
    // fields and their types, used to identify what filter to use
  
    'structure' =&gt; array(  
      'hotel_id'=&gt;'int',
      'name'=&gt;'string',
      'description'=&gt;'string',
      'featured'=&gt;'boolean',
      'published'=&gt;'boolean'
    ),
  
     //these are the list of 'tasks' and accepted parameters, based on the ones above
     //these are the actual parameter names which i said were the same as field names
     //the ones in 'values' are usually values for inserting and updating
     //the ones in 'conditions' are the ones used in the WHERE part of the query
  
    'operations' =&gt;array(  
      'add' =&gt; array(
        'values' =&gt; array('name','description','featured'),
        'conditions' =&gt; array()
      ),
      'view' =&gt; array(
      'values' =&gt; array(),
      'conditions' =&gt; array('hotel_id')
      ),
      'edit' =&gt; array(
      'values' =&gt; array('name','description','featured'),
      'conditions' =&gt; array('hotel_id')
      ),
      'remove' =&gt; array(
      'values' =&gt; array(),
      'conditions' =&gt; array('hotel_id')
      )
    )
  )
  </code></pre>
  
  <p>and so, from that config list:</p>
  
  <ul>
  <li>if a parameters sent for a task is not complete, server returns an error.  </li>
  <li>if a parameter from the url is doubled, only the first parameter read is taken. </li>
  <li>any other parameters not in the config are discarded</li>
  <li>if that task is not allowed, it wont be listed for that table </li>
  <li>if a task is not there, server returns an error</li>
  <li>if a table is not there, server returns an error</li>
  </ul>
  
  <p>I actually patterned this after seeing a component in joomla that uses this strategy. It reduces the model and controller to 4 dynamic functions which would be CRUD, leaving only the config file to be the only file editable later on (this was what I meant about dynamic code, I only add tables and tasks if further tables are needed) but I fear it may impose a security risk which I may have not known yet.</p>
  
  <p>Any ideas for an alternative?</p>