<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.sqlite3.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'sqlite3.setauthorizer.php',
    1 => 'SQLite3::setAuthorizer',
    2 => 'Configures a callback to be used as an authorizer to limit what a statement can do',
  ),
  'up' => 
  array (
    0 => 'class.sqlite3.php',
    1 => 'SQLite3',
  ),
  'prev' => 
  array (
    0 => 'sqlite3.querysingle.php',
    1 => 'SQLite3::querySingle',
  ),
  'next' => 
  array (
    0 => 'sqlite3.version.php',
    1 => 'SQLite3::version',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/sqlite3/sqlite3/setauthorizer.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="sqlite3.setauthorizer" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SQLite3::setAuthorizer</h1>
  <p class="verinfo">(PHP 8)</p><p class="refpurpose"><span class="refname">SQLite3::setAuthorizer</span> &mdash; <span class="dc-title">Configures a callback to be used as an authorizer to limit what a statement can do</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-sqlite3.setauthorizer-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SQLite3::setAuthorizer</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span></span> <code class="parameter">$callback</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>

  <p class="para rdfs-comment">
   Sets a callback that will be called by SQLite every time an action is performed
   (reading, deleting, updating, etc.). This is used when preparing a SQL statement from
   an untrusted source to ensure that the SQL statements do not try to access data they
   are not allowed to see, or that they do not try to execute malicious statements that
   damage the database. For example, an application may allow a user to enter arbitrary
   SQL queries for evaluation by a database. But the application does not want the user to
   be able to make arbitrary changes to the database. An authorizer could then be put in
   place while the user-entered SQL is being prepared that disallows everything except
   SELECT statements.
  </p>
  <p class="para">
   The authorizer callback may be called multiple times for each statement prepared by
   SQLite. A <code class="literal">SELECT</code> or <code class="literal">UPDATE</code> query will call the
   authorizer for every column that would be read or updated.
  </p>
  <p class="para">
   The authorizer is called with up to five parameters. The first parameter is always
   given, and is an <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> (action code) matching a constant from
   <code class="literal">SQLite3</code>. The other parameters are only passed for some actions. The
   following table describes the second and third parameters according to the action:
   <table class="doctable table">
    <caption><strong>List of action codes and parameters</strong></caption>
    
     <thead>
      <tr>
       <th>Action</th>
       <th>Second parameter</th>
       <th>Third parameter</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-index">SQLite3::CREATE_INDEX</a></code></strong></td><td>Index Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-table">SQLite3::CREATE_TABLE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-temp-index">SQLite3::CREATE_TEMP_INDEX</a></code></strong></td><td>Index Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-temp-table">SQLite3::CREATE_TEMP_TABLE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-temp-trigger">SQLite3::CREATE_TEMP_TRIGGER</a></code></strong></td><td>Trigger Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-temp-view">SQLite3::CREATE_TEMP_VIEW</a></code></strong></td><td>View Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-trigger">SQLite3::CREATE_TRIGGER</a></code></strong></td><td>Trigger Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-view">SQLite3::CREATE_VIEW</a></code></strong></td><td>View Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.delete">SQLite3::DELETE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-index">SQLite3::DROP_INDEX</a></code></strong></td><td>Index Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-table">SQLite3::DROP_TABLE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-temp-index">SQLite3::DROP_TEMP_INDEX</a></code></strong></td><td>Index Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-temp-table">SQLite3::DROP_TEMP_TABLE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-temp-trigger">SQLite3::DROP_TEMP_TRIGGER</a></code></strong></td><td>Trigger Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-temp-view">SQLite3::DROP_TEMP_VIEW</a></code></strong></td><td>View Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-trigger">SQLite3::DROP_TRIGGER</a></code></strong></td><td>Trigger Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-view">SQLite3::DROP_VIEW</a></code></strong></td><td>View Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.insert">SQLite3::INSERT</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.pragma">SQLite3::PRAGMA</a></code></strong></td><td>Pragma Name</td><td>First argument passed to the pragma, or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.read">SQLite3::READ</a></code></strong></td><td>Table Name</td><td>Column Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.select">SQLite3::SELECT</a></code></strong></td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.transaction">SQLite3::TRANSACTION</a></code></strong></td><td>Operation</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.update">SQLite3::UPDATE</a></code></strong></td><td>Table Name</td><td>Column Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.attach">SQLite3::ATTACH</a></code></strong></td><td>Filename</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.detach">SQLite3::DETACH</a></code></strong></td><td>Database Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.alter-table">SQLite3::ALTER_TABLE</a></code></strong></td><td>Database Name</td><td>Table Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.reindex">SQLite3::REINDEX</a></code></strong></td><td>Index Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.analyze">SQLite3::ANALYZE</a></code></strong></td><td>Table Name</td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.create-vtable">SQLite3::CREATE_VTABLE</a></code></strong></td><td>Table Name</td><td>Module Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.drop-vtable">SQLite3::DROP_VTABLE</a></code></strong></td><td>Table Name</td><td>Module Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.function">SQLite3::FUNCTION</a></code></strong></td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td><td>Function Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.savepoint">SQLite3::SAVEPOINT</a></code></strong></td><td>Operation</td><td>Savepoint Name</td></tr>

      <tr><td><strong><code><a href="class.sqlite3.php#sqlite3.constants.recursive">SQLite3::RECURSIVE</a></code></strong></td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td><td><strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></td></tr>

     </tbody>
    
   </table>

  </p>
  <p class="para">
   The 4th parameter will be the name of the database (<code class="literal">&quot;main&quot;</code>,
   <code class="literal">&quot;temp&quot;</code>, etc.) if applicable.
  </p>
  <p class="para">
   The 5th parameter to the authorizer callback is the name of the inner-most trigger or
   view that is responsible for the access attempt or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> if this access attempt is
   directly from top-level SQL code.
  </p>
  <p class="para">
   When the callback returns <strong><code><a href="class.sqlite3.php#sqlite3.constants.ok">SQLite3::OK</a></code></strong>, that means the operation
   requested is accepted. When the callback returns <strong><code><a href="class.sqlite3.php#sqlite3.constants.deny">SQLite3::DENY</a></code></strong>,
   the call that triggered the authorizer will fail with an error message explaining that
   access is denied.
  </p>
  <p class="para">
   If the action code is <strong><code><a href="class.sqlite3.php#sqlite3.constants.read">SQLite3::READ</a></code></strong> and the callback returns
   <strong><code><a href="class.sqlite3.php#sqlite3.constants.ignore">SQLite3::IGNORE</a></code></strong> then the prepared statement is
   constructed to substitute a <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> value in place of the table column that would have
   been read if <strong><code><a href="class.sqlite3.php#sqlite3.constants.ok">SQLite3::OK</a></code></strong> had been returned. The
   <strong><code><a href="class.sqlite3.php#sqlite3.constants.ignore">SQLite3::IGNORE</a></code></strong> return can be used to deny an untrusted user
   access to individual columns of a table.
  </p>
  <p class="para">
   When a table is referenced by a <code class="literal">SELECT</code> but no column values are
   extracted from that table (for example in a query like <code class="literal">&quot;SELECT count(*) FROM
   table&quot;</code>) then the <strong><code><a href="class.sqlite3.php#sqlite3.constants.read">SQLite3::READ</a></code></strong> authorizer callback is
   invoked once for that table with a column name that is an empty string.
  </p>
  <p class="para">
   If the action code is <strong><code><a href="class.sqlite3.php#sqlite3.constants.delete">SQLite3::DELETE</a></code></strong> and the callback returns
   <strong><code><a href="class.sqlite3.php#sqlite3.constants.ignore">SQLite3::IGNORE</a></code></strong> then the DELETE operation proceeds but the
   truncate optimization is disabled and all rows are deleted individually.
  </p>
  <p class="para">
   Only a single authorizer can be in place on a database connection at a time. Each call
   to <span class="methodname"><strong>SQLite3::setAuthorizer()</strong></span> overrides the previous call. Disable the
   authorizer by installing a <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> callback. The authorizer is disabled by default.
  </p>
  <p class="para">
   The authorizer callback must not do anything that will modify the database connection
   that invoked the authorizer callback.
  </p>
  <p class="para">
   Note that the authorizer is only called when a statement is prepared, not when it&#039;s
   executed.
  </p>
  <p class="para">
   More details can be found in the
   <a href="http://sqlite.org/c3ref/set_authorizer.html" class="link external">&raquo;&nbsp;SQLite3 documentation</a>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-sqlite3.setauthorizer-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
    <dt><code class="parameter">callback</code></dt>
    <dd>
     <p class="para">
      The <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> to be called.
     </p>
     <p class="para">
      If <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> is passed instead, this will disable the current authorizer callback.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-sqlite3.setauthorizer-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   Повертає <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> у разі успіху або <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> в разі помилки.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-sqlite3.setauthorizer-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
   This method doesn&#039;t throw any error, but if an authorizer is enabled and returns an
   invalid value, the statement preparation will throw an error (or exception, depending
   on the use of the <span class="methodname"><a href="sqlite3.enableexceptions.php" class="methodname">SQLite3::enableExceptions()</a></span> method).
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-sqlite3.setauthorizer-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 <span class="methodname"><strong>SQLite3::setAuthorizer()</strong></span> example</strong></p>
   <div class="example-contents"><p>
     This only allows access to reading, and only some columns of the
     <code class="literal">users</code> table will be returned. Other columns will be replaced with
     <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">(</span><span style="color: #DD0000">'data.sqlite'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'CREATE TABLE users (id, name, password);'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'INSERT INTO users VALUES (1, \'Pauline\', \'Snails4eva\');'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$allowed_columns </span><span style="color: #007700">= [</span><span style="color: #DD0000">'id'</span><span style="color: #007700">, </span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAuthorizer</span><span style="color: #007700">(function (</span><span style="color: #0000BB">int $action</span><span style="color: #007700">, ...</span><span style="color: #0000BB">$args</span><span style="color: #007700">) use (</span><span style="color: #0000BB">$allowed_columns</span><span style="color: #007700">) {<br />    if (</span><span style="color: #0000BB">$action </span><span style="color: #007700">=== </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">::</span><span style="color: #0000BB">READ</span><span style="color: #007700">) {<br />        list(</span><span style="color: #0000BB">$table</span><span style="color: #007700">, </span><span style="color: #0000BB">$column</span><span style="color: #007700">) = </span><span style="color: #0000BB">$args</span><span style="color: #007700">;<br /><br />        if (</span><span style="color: #0000BB">$table </span><span style="color: #007700">=== </span><span style="color: #DD0000">'users' </span><span style="color: #007700">&amp;&amp; </span><span style="color: #0000BB">in_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$column</span><span style="color: #007700">, </span><span style="color: #0000BB">$allowed_columns</span><span style="color: #007700">)) {<br />            return </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">::</span><span style="color: #0000BB">OK</span><span style="color: #007700">;<br />        }<br /><br />        return </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">::</span><span style="color: #0000BB">IGNORE</span><span style="color: #007700">;<br />    }<br /><br />    return </span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">::</span><span style="color: #0000BB">DENY</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">querySingle</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT * FROM users WHERE id = 1;'</span><span style="color: #007700">));</span></span></code></div>
   </div>


   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Array
(
    [id] =&gt; 1
    [name] =&gt; Pauline
    [password] =&gt;
)</pre>
</div>
   </div>
  </div>
 </div>


</div><?php manual_footer($setup); ?>