mysqlfs.php

Go to the documentation of this file.
00001 <?php
00002 
00023 function mysqlfs_add_file ($dir_id, $file_name, $file_store_as_name, $db){
00024         global $LANGUAGE;
00025         global $CHUNK_SIZE;
00026         //check whenewer some file with such name is already in dir
00027         $query = "select count(*) from (
00028                                 select 
00029                                         dirs.id as dir_id, 
00030                                         dirs.name as dir_name,
00031                                         dirs.parent_dir_id as dir_parrent_dir_id,
00032                                         files.name as file_name,
00033                                         files.id as file_id from
00034                                 dirs join files on files.parent_dir_id = dirs.id ) as file_dir 
00035                                 where file_name = '$file_store_as_name' and dir_id = $dir_id";
00036 
00037         $db->Query($query);
00038         $file_already_exists = $db->GetFirstRelation($query);
00039         if ($file_already_exists) {
00040                 return array('bool'=>false, 'err_text'=>$LANGUAGE['FILE_ALREADY_EXISTS']);
00041         }
00042         
00043         $query = "INSERT INTO files (name, parent_dir_id) values ( '$file_store_as_name','$dir_id')";
00044         
00045         $db->Query($query);
00046         // file name is in db, now insert data.
00047         $file_id = $db->GetLastId();
00048         
00049         //echo $file_name;
00050         
00051         
00052         if (!($fd = fopen ($file_name, "r") )) echo "neotevrel jsem soubor";
00053         while ($chunk = addslashes(fread ($fd, $CHUNK_SIZE))){
00054                         //echo "xxxxx";
00055                         $query = "INSERT INTO data (file_id, data) values ('$file_id', '$chunk')";
00056                         //echo "xxxx<br>".$query;
00057                         $db->Query($query);
00058         }
00059         fclose ($fd);
00060         // now, filedata is in db.
00061         return array('bool'=>true, 'err_text'=>'');
00062 };
00063 
00064 
00065 function mysqlfs_show_dir_tree($dir_id) {
00066         global $db;
00067         global $LANGUAGE;
00068 
00069 
00070         //print name and basic information about a directory
00071         $query = "select * from dirs where id = $dir_id ";
00072         
00073         $db->Query($query);
00074         $dir_info = $db->GetFetchAssoc();
00075         echo "<br><div>".$dir_info['name']." [ directory, id = ".$dir_info['id']."]"."
00076                                 <span style='font-size:14px;'>
00077                                         [<a href='?action=delete_dir&amp;id=".$dir_info['id']."'>".$LANGUAGE['DELETE']."</a>]
00078                                         [".$LANGUAGE['ADD_FILE_TO_THIS_DIR']."<form style='display:inline;' enctype='multipart/form-data' action='?action=add_file&amp;id=".$dir_info['id']."' method='post'>"."<input type='file' name='file'><input type='hidden' name='dir_id' value='".$dir_info['id']."'><input type='submit' value='".$LANGUAGE['ADD_FILE_TO_THIS_DIR']."'></form>]
00079                                         [".$LANGUAGE['ADD_DIR_TO_THIS_DIR']."<form style='display:inline;' action='?action=add_dir&amp;id=".$dir_info['id']."' method='post'>"."<input type='text' name='new_dir_name'><input type='hidden' name='parent_dir_id' value='".$dir_info['id']."'><input type='submit' value='".$LANGUAGE['ADD_DIR_TO_THIS_DIR']."'></form>]
00080                                 </span></div>\n";
00081                 echo "<div style='width:auto;border-left:1px solid gray;border-bottom:1px solid gray;padding-left:20px;'>\n";   
00082         //print files in direcrory
00083         $query = "select * from files where parent_dir_id = $dir_id "; 
00084         $db->Query($query);
00085         while ($resarr = $db->GetFetchAssoc()){
00086                 echo "  <div>
00087                                         ".$resarr['name']." [file, id = ".$resarr['id']."]
00088                                         [<a href='?action=download_file&amp;id=".$resarr['id']."'>".$LANGUAGE["DOWNLOAD"]."</a>]
00089                                         [<a href='?action=delete_file&amp;id=".$resarr['id']."'>".$LANGUAGE['DELETE']."</a>]
00090                                 </div>\n";
00091         }
00092         //print recursively all subdirectories
00093         $query = "select * from dirs where parent_dir_id = $dir_id "; 
00094         $res = $db->Query($query);
00095         while ($resarr = $db->GetFetchAssoc($res)){
00096                 if ($dir_info['id'] != $resarr['id']) {
00097                         mysqlfs_show_dir_tree($resarr['id']);
00098                 }
00099                 echo "&nbsp;";
00100         }
00101         echo "</div>\n";
00102 }
00103 
00104 function mysqlfs_show_fs_tree(){
00105         global $ROOT_DIRECTORY_ID;
00106         mysqlfs_show_dir_tree($ROOT_DIRECTORY_ID);
00107 }
00108 
00109 function mysqlfs_show_form_add_file ($action,$dir_id){
00110         echo ''.
00111         '<br><br><table border="0"><form action="'.$action.'" method="post" enctype="multipart/form-data">
00112                 <td class="reg">File</td>
00113                 <td><div class="cv_file"></div></td>
00114                 <td><input type="file" name="file"></td>
00115                 <td><input type="hidden" name="form_send" value="1">
00116                         <input type="hidden" name="dir_id" value="'.$dir_id.'">
00117                         <input type="submit" name="submit" value="OK">
00118                 </td>
00119         </form></table><br><br>'."\n";
00120 }
00121 
00122 
00123 
00124 function get_arr_index($arr, $index) {
00125         if (array_key_exists($index,$arr)){
00126                 return $arr[$index];
00127         }
00128         else {
00129                 return null;
00130         }
00131 }
00132 
00133 function common_translate_national_chars($str){
00134 global $LANGUAGE;
00135   return strtr($str, $LANGUAGE["LANGUAGE_NATIONALS_NATIONAL"]);
00136 }
00137 
00138 function mysqlfs_get_file($file_id){
00139         global $db;
00140         global $LANGUAGE;
00141         global $CHUNK_SIZE;
00142         
00143         
00144         $file_exists1 = $db->GetFirstRelation("select count(*) from files where id = $file_id");
00145         if (!$file_exists1) {
00146                 return array('bool'=>false, 'err_text'=>$LANGUAGE['NO_SUCH_FILE']);
00147         }
00148         
00149         $file_exists2 = $db->GetFirstRelation("select count(*) from data where file_id = $file_id");
00150         if (!$file_exists2) {
00151                 return array('bool'=>false, 'err_text'=>$LANGUAGE['ZERO_FILE_LENGTH']);
00152         }
00153         
00154         
00155         
00156         //so everthing OK.
00157         $query = "select * from files where id = $file_id ";
00158         $db->Query($query);
00159         $file_info  = $db->GetFetchAssoc();
00160         
00161         if (ereg('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
00162                 $UserBrowser = "Opera";
00163         elseif (ereg('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
00164                 $UserBrowser = "IE";
00165         else
00166                 $UserBrowser = '';
00167         $mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 
00168         'application/octetstream' : 'application/octet-stream';
00169         header('Content-Type: ' . $mime_type);
00170         header('Content-Disposition: attachment; filename="'.$file_info['name'].'"');
00171         header('Accept-Ranges: bytes');
00172         header("Cache-control: private");
00173         header('Pragma: private');                              
00174         header("Content-Length: ".($file_exists2. $CHUNK_SIZE));
00175  
00176         $query = "select * from data where file_id = $file_id";
00177         $db->Query($query);
00178         while ($resarr = $db->GetFetchAssoc()){
00179                 echo $resarr['data'];           
00180         }
00181         header("Connection: close");
00182         return array('bool'=>true, 'err_text'=>'');
00183 }
00184 
00185 function mysqlfs_add_dir($parent_dir_id, $new_dir_name) {
00186         global $db;
00187         global $LANGUAGE;
00188         echo $parent_dir_id;
00189         $parent_dir_exists = $db->GetFirstRelation("select count(*) from dirs where id = $parent_dir_id");
00190         if (!$parent_dir_exists) {
00191                 return array('bool'=>false, 'err_text'=>$LANGUAGE['NO_SUCH_DIR']);
00192         }
00193         
00194         $query = "INSERT INTO dirs (name, parent_dir_id) values ('$new_dir_name', $parent_dir_id)";
00195         $db->Query($query);
00196         return array('bool'=>true, 'err_text'=>'');
00197 }
00198 
00199 function mysqlfs_delete_dir($dir_id) {
00200         global $db;
00201         global $LANGUAGE;
00202         
00203         $query = "DELETE FROM  dirs where id = $dir_id";
00204         $db->Query($query);
00205         return array('bool'=>true, 'err_text'=>'');
00206 }
00207 
00208 function mysqlfs_delete_file($file_id) {
00209         global $db;
00210         global $LANGUAGE;
00211         
00212         $query = "DELETE FROM  files where id = $file_id";
00213         $db->Query($query);
00214         return array('bool'=>true, 'err_text'=>'');
00215 }
00216 
00217 
00218 
00219 ?>

Generated on Mon May 28 12:56:36 2007 for MYSQLFS by  doxygen 1.5.0