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

  1. <?php
  2. /* $Id: transformations.lib.php,v 2.3 2003/11/26 22:52:23 rabus Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Set of functions used with the relation and pdf feature
  7.  */
  8.  
  9. function PMA_transformation_getOptions($string) {
  10.     $transform_options = array();
  11.  
  12.     if ($string != '') {
  13.         if ($string{0} == "'" && $string{strlen($string)-1} == "'") {
  14.             $transform_options = explode('\',\'', substr($string, 1, strlen($string)-2));
  15.         } else {
  16.             $transform_options = array(0 => $string);
  17.         }
  18.     }
  19.  
  20.     return $transform_options;
  21. }
  22.  
  23. /**
  24.  * Gets all available MIME-types
  25.  *
  26.  * @return  array    array[mimetype], array[transformation]
  27.  *
  28.  * @access  public
  29.  *
  30.  * @author  Garvin Hicking <me@supergarv.de>
  31.  */
  32. function PMA_getAvailableMIMEtypes() {
  33.     $handle = opendir('./libraries/transformations');
  34.  
  35.     $stack = array();
  36.     $filestack = array();
  37.  
  38.     while (($file = readdir($handle)) != false) {
  39.         $filestack[$file] = $file;
  40.     }
  41.  
  42.     closedir($handle);
  43.  
  44.     if (is_array($filestack)) {
  45.         @ksort($filestack);
  46.         foreach($filestack AS $key => $file) {
  47.  
  48.             if (preg_match('|^.*__.*\.inc\.php(3?)$|', trim($file), $match)) {
  49.                 // File contains transformation functions.
  50.                 $base = explode('__', str_replace('.inc.php' . $match[1], '', $file));
  51.  
  52.                 $mimetype = str_replace('_', '/', $base[0]);
  53.                 $stack['mimetype'][$mimetype] = $mimetype;
  54.  
  55.                 $stack['transformation'][] = $mimetype . ': ' . $base[1];
  56.                 $stack['transformation_file'][] = $file;
  57.  
  58.             } else if (preg_match('|^.*\.inc\.php(3?)$|', trim($file), $match)) {
  59.                 // File is a plain mimetype, no functions.
  60.                 $base = str_replace('.inc.php' . $match[1], '', $file);
  61.  
  62.                 if ($base != 'global') {
  63.                     $mimetype = str_replace('_', '/', $base);
  64.                     $stack['mimetype'][$mimetype] = $mimetype;
  65.                     $stack['empty_mimetype'][$mimetype] = $mimetype;
  66.                 }
  67.             }
  68.  
  69.         }
  70.     }
  71.  
  72.     return $stack;
  73. }
  74.  
  75. /**
  76.  * Gets the mimetypes for all rows of a table
  77.  *
  78.  * @param   string   the name of the db to check for
  79.  * @param   string   the name of the table to check for
  80.  * @param   string   whether to include only results having a mimetype set
  81.  *
  82.  * @return  array    [field_name][field_key] = field_value
  83.  *
  84.  * @global  array    the list of relations settings
  85.  *
  86.  * @access  public
  87.  *
  88.  * @author  Mike Beck <mikebeck@users.sourceforge.net> / Garvin Hicking <me@supergarv.de>
  89.  */
  90. function PMA_getMIME($db, $table, $strict = false) {
  91.     global $cfgRelation;
  92.  
  93.     $com_qry  = 'SELECT column_name, mimetype, transformation, transformation_options FROM ' . PMA_backquote($cfgRelation['column_info'])
  94.               . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  95.               . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  96.               . ' AND (mimetype != \'\'' . (!$strict ? ' OR transformation != \'\' OR transformation_options != \'\'' : '') . ')';
  97.     $com_rs   = PMA_query_as_cu($com_qry);
  98.  
  99.     while ($row = @PMA_mysql_fetch_array($com_rs)) {
  100.         $col                                    = $row['column_name'];
  101.         $mime[$col]['mimetype']                 = $row['mimetype'];
  102.         $mime[$col]['transformation']           = $row['transformation'];
  103.         $mime[$col]['transformation_options']   = $row['transformation_options'];
  104.     } // end while
  105.  
  106.     if (isset($mime) && is_array($mime)) {
  107.         return $mime;
  108.      } else {
  109.         return FALSE;
  110.      }
  111.  } // end of the 'PMA_getMIME()' function
  112.  
  113. /**
  114. * Set a single mimetype to a certain value.
  115. *
  116. * @param   string   the name of the db
  117. * @param   string   the name of the table
  118. * @param   string   the name of the column
  119. * @param   string   the mimetype of the column
  120. * @param   string   the transformation of the column
  121. * @param   string   the transformation options of the column
  122. * @param   string   (optional) force delete, will erase any existing comments for this column
  123. *
  124. * @return  boolean  true, if comment-query was made.
  125. *
  126. * @global  array    the list of relations settings
  127. *
  128. * @access  public
  129. */
  130. function PMA_setMIME($db, $table, $key, $mimetype, $transformation, $transformation_options, $forcedelete = false) {
  131.     global $cfgRelation;
  132.  
  133.     $test_qry  = 'SELECT mimetype, ' . PMA_backquote('comment') . ' FROM ' . PMA_backquote($cfgRelation['column_info'])
  134.                 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
  135.                 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  136.                 . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  137.     $test_rs   = PMA_query_as_cu($test_qry);
  138.  
  139.     if ($test_rs && mysql_num_rows($test_rs) > 0) {
  140.         $row = @PMA_mysql_fetch_array($test_rs);
  141.  
  142.         if (!$forcedelete && (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0 || strlen($row['comment']) > 0)) {
  143.             $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['column_info'])
  144.                    . ' SET mimetype = \'' . PMA_sqlAddslashes($mimetype) . '\','
  145.                    . '     transformation = \'' . PMA_sqlAddslashes($transformation) . '\','
  146.                    . '     transformation_options = \'' . PMA_sqlAddslashes($transformation_options) . '\''
  147.                    . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  148.                    . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  149.                    . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  150.         } else {
  151.             $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['column_info'])
  152.                    . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  153.                    . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  154.                    . ' AND column_name = \'' . PMA_sqlAddslashes($key) . '\'';
  155.         }
  156.     } else if (strlen($mimetype) > 0 || strlen($transformation) > 0 || strlen($transformation_options) > 0) {
  157.         $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['column_info'])
  158.                    . ' (db_name, table_name, column_name, mimetype, transformation, transformation_options) '
  159.                    . ' VALUES('
  160.                    . '\'' . PMA_sqlAddslashes($db) . '\','
  161.                    . '\'' . PMA_sqlAddslashes($table) . '\','
  162.                    . '\'' . PMA_sqlAddslashes($key) . '\','
  163.                    . '\'' . PMA_sqlAddslashes($mimetype) . '\','
  164.                    . '\'' . PMA_sqlAddslashes($transformation) . '\','
  165.                    . '\'' . PMA_sqlAddslashes($transformation_options) . '\')';
  166.     }
  167.  
  168.     if (isset($upd_query)){
  169.         $upd_rs    = PMA_query_as_cu($upd_query);
  170.         unset($upd_query);
  171.         return true;
  172.     } else {
  173.         return false;
  174.     }
  175. } // end of 'PMA_setMIME()' function
  176.  
  177. /**
  178. * Returns the real filename of a configured transformation
  179. *
  180. * @param   string   the current filename
  181. *
  182. * @return  string   the new filename
  183. *
  184. * @access  public
  185. */
  186. function PMA_sanitizeTransformationFile(&$filename) {
  187.     // garvin: for security, never allow to break out from transformations directory
  188.  
  189.     $include_file = preg_replace('@\.\.*@', '.', $filename);
  190.  
  191.     // This value can also contain a 'php3' value, in which case we map this filename to our new 'php' variant
  192.     $testfile = preg_replace('@\.inc\.php3$@', '.inc.php', $include_file);
  193.     if ($include_file{strlen($include_file)-1} == '3' && file_exists('./libraries/transformations/' . $testfile)) {
  194.         $include_file = $testfile;
  195.         $filename     = $testfile; // Corrects the referenced variable for further actions on the filename;
  196.     }
  197.  
  198.     return $include_file;
  199. } // end of 'PMA_sanitizeTransformationFile()' function
  200. ?>
  201.