PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/s3dbcore/file2folder.php

https://code.google.com/p/s3db/
PHP | 287 lines | 166 code | 81 blank | 40 comment | 28 complexity | bfbc95293716291d2d50b6ded3a0d015 MD5 | raw file
  1. <?php
  2. #File2folder will accept the file and transfer it to its appropriate location in the
  3. function tmpfile2folder($F)
  4. {
  5. extract($F);
  6. #in order to chose a folder, will need to verify s3db deployment of the project. If the project is not on this deployment, most likely will need to create a new one.
  7. #tmpfile2folder takes inputs in and returns built inputs
  8. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  9. if($inputs['project_id'] =='')
  10. return array(False, 'error_code'=>$GLOBALS['error_codes']['something_went_wrong'], 'message'=>'Need a valid project_id to insert file');
  11. #find which of the user projects can insert instances in this class.
  12. $folder = project_folder_name($inputs['project_id'], $db);
  13. if($folder=='' || !is_dir($maindir.'/'.$folder)) {
  14. $newProj=random_string(15).'.project'.urlencode($inputs['project_id']);
  15. $project_folder = $maindir.'/'.$newProj;
  16. if(mkdir($project_folder))
  17. $folder=$newProj;
  18. }
  19. if($folder=='')
  20. return array(False, 'error_code'=>$GLOBALS['error_codes']['something_went_wrong'], 'message'=>'Failed to find a project_in for this item');
  21. else{
  22. $file_found = fileFoundInfo(array('filekey'=>$inputs['filekey'], 'db'=>$db, 'user_id'=>$user_id, 'rule_id'=>$inputs['rule_id']));
  23. #echo '<pre>';print_r($file_found);
  24. #if(!$file_found[0])
  25. # return array(False, $file_found[1]);
  26. #else {
  27. # $old_file = $file_found['old_file'];
  28. #}
  29. #echo '<pre>';print_r($file_found);
  30. $old_file = $file_found['old_file'];
  31. $inputs['value'] = $folder;
  32. $inputs['file_name'] = $file_found['file_name'];
  33. $inputs['file_size'] = $file_found['file_size'];
  34. #find the extentsion and the filename.
  35. $tmp=fileNameAndExtension($file_found['file_name']);
  36. extract($tmp);
  37. #$tmp=explode(".", $inputs['file_name']);
  38. //echo '<pre>';print_r($tmp);exit;
  39. #$extension= end($tmp);
  40. #$tmp = array_diff($tmp, array($extension));
  41. #$name = implode('.', $tmp);
  42. $inputs['mime_type'] = $extension;
  43. $fileFinName =$maindir.'/'.$folder.'/'.urlencode($name.'_'.$inputs['project_id'].'_'.$inputs['resource_id'].'_'.$inputs['rule_id'].'_'.$inputs['statement_id'].'.'.$extension);
  44. $fileFinName = ereg_replace('/.$','',$fileFinName);
  45. #echo $old_file.'<br />';
  46. #echo $fileFinName;exit;
  47. if(!copy($old_file, $fileFinName))
  48. return array(False,'error_code'=>$GLOBALS['error_codes']['no_permission_message'], 'message'=>'Could not move file');
  49. else {
  50. return array(True,$inputs);
  51. }
  52. }
  53. }
  54. function movefile2folder($F)
  55. {
  56. extract($F);
  57. #Maindir might vary, it will depend on where the users will want their files
  58. #$maindir = S3DB_SERVER_ROOT.'/extras/'.$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  59. #if(is_dir($GLOBALS['s3db_info']['server']['db']['uploads_folder']))
  60. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  61. $folder_code_name = $F['value'];
  62. list($name, $extension) = explode(".", $filename);
  63. #here end the decision as to where the foldr will be and starts the process of renaming and moving the file
  64. $file_in_folder =$folder_code_name.'/'.$name.'_'.$project_id.'_'.$resource_id.'_'.$rule_id.'_'.strval($statement_id).'.'.$extension;
  65. $file_destination = $maindir."/".$file_in_folder;
  66. #echo $file_destination;exit;
  67. #echo '<BR>2'.$uploadedfile;
  68. if(move_uploaded_file($uploadedfile, $file_destination)) ##if not uploaded, try copy
  69. {return TRUE;
  70. exit;
  71. }
  72. elseif(copy($uploadedfile, $file_destination))
  73. {
  74. chmod($file_destination, 0700);
  75. #rename($uploadedfile, $uploadedfile.'_moved');
  76. return TRUE;
  77. exit;
  78. }
  79. else{
  80. return FALSE;
  81. }
  82. }
  83. function move_uploadedfile($resource_info, $project_info)
  84. {
  85. $destDir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'].'/tmps3db/';
  86. $file_to_read = $destDir.'xlsimport_P'.$project_info['project_id'].'_C'.$resource_info['resource_id'].'.txt';
  87. if($_FILES['import']['tmp_name']!='')
  88. $uploaded_file = $_FILES['import']['tmp_name'];
  89. #is there a tab in the first 100 chars? If not, it probably is not a text file
  90. $h=@fread(@fopen($uploaded_file, 'r'), '1000');
  91. if(strpos($h, ' ')=='')
  92. {
  93. echo '<font color="red"><B>Please make sure the file is tab delimited.</B></font>';
  94. }
  95. #list ($filename, $extension) = explode (".", $_FILES['import']['name']);
  96. if(!is_file($uploaded_file))
  97. {echo '<font color="red"><B>Please make sure the file was uploaded.</B></font>';
  98. }
  99. //return file error when move file doesn't work, but stop showing after other forms are submitted
  100. if (!move_uploaded_file($uploaded_file, $file_to_read))
  101. {
  102. return (False);
  103. }
  104. else {
  105. return $file_to_read;
  106. }
  107. } //this ends function movefile
  108. function project_folder_name ($project_id, $db)
  109. {
  110. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  111. $sql = "select project_folder from s3db_project where project_id = '".$project_id."'";
  112. #echo $sql;
  113. $db->query($sql, __LINE__, __FILE__);
  114. if($db->next_record())
  115. {
  116. return $project_folder = $db->f('project_folder');
  117. }
  118. else {
  119. $folders = scandir($maindir);
  120. if(!is_array($folders))
  121. return (False);
  122. else {
  123. foreach ($folders as $key=>$foldername) {
  124. if(ereg('(.*).project'.urlencode($project_id), $foldername))
  125. $matches[]=$foldername;
  126. }
  127. if(count($matches)==1)
  128. return ($matches[0]);
  129. else {
  130. return (False);
  131. }
  132. }
  133. }
  134. }
  135. function MoveFile($F)
  136. {#function MoveFile(compact('filekey','db', 'file'))
  137. #takes any file and moves it to s3db temporeary file directory with a file_id as name.
  138. extract($F);
  139. #if(is_dir($GLOBALS['s3db_info']['server']['db']['uploads_folder']))
  140. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  141. $tmpdir = $maindir.'/tmps3db';
  142. #now find the name that the file will need to have in order to be found by the API
  143. $sql = "select * from s3db_file_transfer where filekey = '".$filekey."'";
  144. $db->query($sql, __LINE__, __FILE__);
  145. if($db->next_record())
  146. {
  147. $file_id = $db->f('file_id');
  148. $filename = $db->f('filename');
  149. }
  150. else {
  151. return (False);
  152. }
  153. list($name, $ext) = explode('.', $filename);
  154. $destination = $tmpdir.'/'.$file_id.'.'.$ext;
  155. if(!copy($file, $destination))
  156. {
  157. if(!move_uploaded_file($file, $destination))
  158. return (False);
  159. else
  160. return (True);
  161. }
  162. else {
  163. return (True);
  164. }
  165. }
  166. function findDestFile($F)
  167. {#function MoveFile(compact('filekey','db', 'file'))
  168. #takes any file and moves it to s3db temporeary file directory with a file_id as name.
  169. extract($F);
  170. #if(is_dir($GLOBALS['s3db_info']['server']['db']['uploads_folder']))
  171. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  172. $tmpdir = $maindir.'/tmps3db';
  173. #now find the name that the file will need to have in order to be found by the API
  174. $sql = "select * from s3db_file_transfer where filekey = '".$filekey."'";
  175. $db->query($sql, __LINE__, __FILE__);
  176. if($db->next_record())
  177. {
  178. $file_id = $db->f('file_id');
  179. $filename = $db->f('filename');
  180. }
  181. else {
  182. return (False);
  183. }
  184. #list($name, $ext) = explode('.', $filename);
  185. $tmp = fileNameAndExtension($filename);
  186. $name = $tmp['name'];
  187. $ext= $tmp['extension'];
  188. $destination = $tmpdir.'/'.$file_id.'.'.$ext;
  189. return ($destination);
  190. }
  191. function fileLocation($statement_info, $db)
  192. {
  193. $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'].$GLOBALS['s3db_info']['server']['db']['uploads_file'];
  194. #retrieve the project information
  195. $folder_code_name = project_folder_name($statement_info['project_id'], $db);
  196. $file_name = $statement_info['file_name'];
  197. #echo '<pre>';print_r($statement_info);
  198. #list ($realname, $extension) = explode('.', $file_name);
  199. $tmp = fileNameAndExtension($file_name);
  200. $realname = $tmp['name'];
  201. $extension = $tmp['extension'];
  202. $file_new_name = $realname.'_'.urlencode($statement_info['project_id'].'_'.$statement_info['resource_id'].'_'.$statement_info['rule_id'].'_'.$statement_info['statement_id']).'.'.$extension;
  203. $file_location = $maindir."/".$folder_code_name."/".$file_new_name;
  204. return ($file_location);
  205. }
  206. ?>