home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / bookmark.lib.php < prev    next >
Encoding:
PHP Script  |  2004-02-11  |  5.5 KB  |  176 lines

  1. <?php
  2. /* $Id: bookmark.lib.php,v 2.2.4.1 2004/02/11 18:13:07 lem9 Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used with the bookmark feature
  7.  */
  8.  
  9.  
  10. /**
  11.  * Defines the bookmark parameters for the current user
  12.  *
  13.  * @return  array    the bookmark parameters for the current user
  14.  *
  15.  * @global  integer  the id of the current server
  16.  *
  17.  * @access  public
  18.  */
  19. function PMA_getBookmarksParam()
  20. {
  21.     global $server;
  22.  
  23.     $cfgBookmark = '';
  24.  
  25.     // No server selected -> no bookmark table
  26.     if ($server == 0) {
  27.         return '';
  28.     }
  29.  
  30.     $cfgBookmark['user']  = $GLOBALS['cfg']['Server']['user'];
  31.     $cfgBookmark['db']    = $GLOBALS['cfg']['Server']['pmadb'];
  32.     $cfgBookmark['table'] = $GLOBALS['cfg']['Server']['bookmarktable'];
  33.  
  34.     return $cfgBookmark;
  35. } // end of the 'PMA_getBookmarksParam()' function
  36.  
  37.  
  38. /**
  39.  * Gets the list of bookmarks defined for the current database
  40.  *
  41.  * @param   string   the current database name
  42.  * @param   array    the bookmark parameters for the current user
  43.  *
  44.  * @return  mixed    the bookmarks list if defined, false else
  45.  *
  46.  * @access  public
  47.  */
  48. function PMA_listBookmarks($db, $cfgBookmark)
  49. {
  50.     $query  = 'SELECT label, id FROM '. PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  51.             . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
  52.             . ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  53.             . '      OR user = \'\')';
  54.     if (isset($GLOBALS['dbh'])) {
  55.         $result = PMA_mysql_query($query, $GLOBALS['dbh']);
  56.     } else {
  57.         $result = PMA_mysql_query($query);
  58.     }
  59.  
  60.     // There is some bookmarks -> store them
  61.     if ($result > 0 && mysql_num_rows($result) > 0) {
  62.         $flag = 1;
  63.         while ($row = PMA_mysql_fetch_row($result)) {
  64.             $bookmark_list[$flag . ' - ' . $row[0]] = $row[1];
  65.             $flag++;
  66.         } // end while
  67.         return $bookmark_list;
  68.     }
  69.     // No bookmarks for the current database
  70.     else {
  71.         return FALSE;
  72.     }
  73. } // end of the 'PMA_listBookmarks()' function
  74.  
  75.  
  76. /**
  77.  * Gets the sql command from a bookmark
  78.  *
  79.  * @param   string   the current database name
  80.  * @param   array    the bookmark parameters for the current user
  81.  * @param   mixed    the id of the bookmark to get
  82.  * @param   string   which field to look up the $id
  83.  * @param   boolean  TRUE: get all bookmarks regardless of the owning user
  84.  *
  85.  * @return  string   the sql query
  86.  *
  87.  * @access  public
  88.  */
  89. function PMA_queryBookmarks($db, $cfgBookmark, $id, $id_field = 'id', $action_bookmark_all = FALSE)
  90. {
  91.     if (empty($cfgBookmark['db']) || empty($cfgBookmark['table'])) {
  92.         return '';
  93.     }
  94.  
  95.     $query          = 'SELECT query FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  96.                     . ' WHERE dbase = \'' . PMA_sqlAddslashes($db) . '\''
  97.                     . ($action_bookmark_all? '' : ' AND (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  98.                     . '      OR user = \'\')' )
  99.                     . ' AND ' . PMA_backquote($id_field) . ' = ' . $id;
  100.  
  101.     if (isset($GLOBALS['dbh'])) {
  102.         $result = PMA_mysql_query($query, $GLOBALS['dbh']);
  103.     } else {
  104.         $result = PMA_mysql_query($query);
  105.     }
  106.     $bookmark_query = @PMA_mysql_result($result, 0, 'query') OR FALSE;
  107.  
  108.     return $bookmark_query;
  109. } // end of the 'PMA_queryBookmarks()' function
  110.  
  111.  
  112. /**
  113.  * Adds a bookmark
  114.  *
  115.  * @param   array    the properties of the bookmark to add
  116.  * @param   array    the bookmark parameters for the current user
  117.  * @param   boolean  whether to make the bookmark available for all users
  118.  *
  119.  * @return  boolean  whether the INSERT succeeds or not
  120.  *
  121.  * @access  public
  122.  */
  123. function PMA_addBookmarks($fields, $cfgBookmark, $all_users = false)
  124. {
  125.     $query = 'INSERT INTO ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  126.            . ' (id, dbase, user, query, label) VALUES (\'\', \'' . PMA_sqlAddslashes($fields['dbase']) . '\', \'' . ($all_users ? '' : PMA_sqlAddslashes($fields['user'])) . '\', \'' . PMA_sqlAddslashes(urldecode($fields['query'])) . '\', \'' . PMA_sqlAddslashes($fields['label']) . '\')';
  127.     if (isset($GLOBALS['dbh'])) {
  128.         $result   = PMA_mysql_query($query, $GLOBALS['dbh']);
  129.         if (PMA_mysql_error($GLOBALS['dbh'])) {
  130.            $error = PMA_mysql_error($GLOBALS['dbh']);
  131.            require_once('./header.inc.php');
  132.            PMA_mysqlDie($error);
  133.         }
  134.     } else {
  135.         $result   = PMA_mysql_query($query);
  136.         if (PMA_mysql_error()) {
  137.            $error = PMA_mysql_error();
  138.            require_once('./header.inc.php');
  139.            PMA_mysqlDie($error);
  140.         }
  141.     }
  142.  
  143.     return TRUE;
  144. } // end of the 'PMA_addBookmarks()' function
  145.  
  146.  
  147. /**
  148.  * Deletes a bookmark
  149.  *
  150.  * @param   string   the current database name
  151.  * @param   array    the bookmark parameters for the current user
  152.  * @param   integer  the id of the bookmark to get
  153.  *
  154.  * @access  public
  155.  */
  156. function PMA_deleteBookmarks($db, $cfgBookmark, $id)
  157. {
  158.     $query  = 'DELETE FROM ' . PMA_backquote($cfgBookmark['db']) . '.' . PMA_backquote($cfgBookmark['table'])
  159.             . ' WHERE (user = \'' . PMA_sqlAddslashes($cfgBookmark['user']) . '\''
  160.             . '        OR user = \'\')'
  161.             . ' AND id = ' . $id;
  162.     if (isset($GLOBALS['dbh'])) {
  163.         $result = PMA_mysql_query($query, $GLOBALS['dbh']);
  164.     } else {
  165.         $result = PMA_mysql_query($query);
  166.     }
  167. } // end of the 'PMA_deleteBookmarks()' function
  168.  
  169.  
  170. /**
  171.  * Bookmark Support
  172.  */
  173. $cfg['Bookmark'] = PMA_getBookmarksParam();
  174.  
  175. ?>
  176.