PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/statement/insertstatement.php

https://code.google.com/p/s3db/
PHP | 221 lines | 147 code | 46 blank | 28 comment | 20 complexity | 40ff3e7d69b80a73856f65a3c0d5ab6f MD5 | raw file
  1. <?php
  2. #insertstatement.php is the interface for inserting a statement (instance, value, notes and file)
  3. #Helena F Deus (helenadeus@gmail.com)
  4. #Helena F Deus (helenadeus@gmail.com)
  5. ini_set('display_errors',0);
  6. if($_REQUEST['su3d'])
  7. ini_set('display_errors',1);
  8. if($_SERVER['HTTP_X_FORWARDED_HOST']!='')
  9. $def = $_SERVER['HTTP_X_FORWARDED_HOST'];
  10. else
  11. $def = $_SERVER['HTTP_HOST'];
  12. if(file_exists('../config.inc.php'))
  13. {
  14. include('../config.inc.php');
  15. }
  16. else
  17. {
  18. Header('Location: http://'.$def.'/s3db/');
  19. exit;
  20. }
  21. $key = $_GET['key'];
  22. #Get the key, send it to check validity
  23. include_once('../core.header.php');
  24. if($key) $user_id = get_entry('access_keys', 'account_id', 'key_id', $key, $db);
  25. else $user_id = $_SESSION['user']['account_id'];
  26. $instance_id = ($_REQUEST['item_id']=='')?$_REQUEST['instance_id']:$_REQUEST['item_id'];
  27. #$instance_info = get_info('instance', $instance_id, $db);
  28. #$instanceAcl = dataAcl(compact('instance_info', 'user_id', 'db', 'project_id'));
  29. $instance_info = URIinfo('I'.$instance_id, $user_id, $key, $db);
  30. $project_id = $_REQUEST['project_id'];
  31. #$acl = find_final_acl($user_id, $project_id, $db);
  32. $rule_id = $_REQUEST['rule_id'];
  33. $rule_info = URIinfo('R'.$rule_id, $user_id, $key, $db);
  34. $ruleOnProject = ereg('(^|_)'.$project_id.'_', $rule_info['permission']);
  35. #$ruleAcl = find_final_acl($user_id, $rule_info['project_id'], $db);#is user allowed on this rule
  36. #relevant extra arguments
  37. #$args = '?key='.$_REQUEST['key'].'&project_id='.$_REQUEST['project_id'].'&instance_id='.$_REQUEST['instance_id'].'&rule_id='.$rule_id;
  38. #include('../webActions.php');
  39. if(!$rule_info['add_data'])
  40. {
  41. echo "User cannot insert statements in this rule";
  42. exit;
  43. }
  44. else
  45. {
  46. #add the header of the instance
  47. include('../resource/instance.header.php');
  48. #echo '<pre>';print_r($_REQUEST);exit;
  49. $insertbutton =$_REQUEST["insert_".str_replace('.','_',$instance_id)."_".str_replace('.','_',$rule_id)];
  50. #echo '<pre>';print_r($_REQUEST);
  51. $rules[0] = $rule_info;
  52. #$rules = include_object_class_id($rules, $project_id, $db);
  53. $rule_info = $rules[0];
  54. #echo '<pre>';print_r($rule_info);exit;
  55. if ($insertbutton!='')
  56. {
  57. $value = $_REQUEST['input_'.$instance_id.'_'.$rule_id];
  58. if($value=='')
  59. $value = $_REQUEST['input_'.str_replace('.','_',$instance_id).'_'.str_replace('.','_',$rule_id)];
  60. $notes = $_REQUEST['text_'.$instance_id.'_'.$rule_id];
  61. $linkref = $_REQUEST['Hyperlink_ref_'.$instance_id.'_'.$rule_id];
  62. $linkname = $_REQUEST['Hyperlink_name_'.$instance_id.'_'.$rule_id];
  63. if ($linkname=='') $linkname = $linkref;
  64. $filename = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['name'];
  65. $mimetype = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['type'];
  66. $filesize = filesize($_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name']);
  67. $uploadedfile = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name'];
  68. #$statement_id = str_replace (array('.', ' '),'', microtime());
  69. $valid_entry = FALSE;
  70. #Minimal code inside inserts to avoid the temptation of displaying what's inside
  71. if ($insertbutton=='Insert')
  72. {
  73. if($_FILES['upload_input_'.$instance_id.'_'.$rule_id]['name'] =='')
  74. {
  75. if ($value!='')
  76. {
  77. $s3ql['db'] = $db;
  78. $s3ql['user_id'] = $user_id;
  79. $s3ql['insert'] = 'statement';
  80. #$s3ql['where']['project_id'] = $project_id;
  81. $s3ql['where']['instance_id'] = $instance_id;
  82. $s3ql['where']['rule_id'] = $rule_id;
  83. $s3ql['where']['value'] = $value;
  84. $s3ql['where']['notes'] = trim($notes);
  85. $s3ql['format']='html';
  86. $done = S3QLaction($s3ql);
  87. $msg=html2cell($done);$msg = $msg[2];
  88. #ereg('<error>([0-9]+)</error>.*<(message|statement_id)>(.*)</(message|statement_id)>', $done, $s3qlout);
  89. #preg_match('/[0-9]+/', $done, $statement_id);
  90. if($msg['error_code']=='0')
  91. {
  92. $statement_id = $msg['statement_id'];
  93. $S = compact('user_id', 'rule_info', 'instance_id', 'statement_id', 'value', 'notes', 'db');
  94. $report_msg = render_inserted($s3ql, $statement_id);
  95. $report_msg .= sprintf("%s\n", ' <br /><input type="button" value="Insert Another" onClick="window.location=\''.$action['insertstatement'].'\'">');
  96. $report_msg .= sprintf("%s\n", ' &nbsp;&nbsp;<input type="button" value="Close Window" onClick="opener.window.location.reload(); self.close();return false;">');
  97. #exit;
  98. }
  99. else
  100. {
  101. $S = compact('user_id', 'rule_info', 'instance_id', 'statement_id', 'value', 'notes', 'db', 's3qlout');
  102. $report_msg = $msg['message'];
  103. $report_msg .= couldnot_insert_statement($S);
  104. #exit;
  105. }
  106. }
  107. else
  108. $report_msg = render_value_cannot_be_null($s3ql);
  109. #$message = "Value cannot be empty";
  110. }
  111. else
  112. {
  113. $value = project_folder_name ($project_id, $db);
  114. $notes = $_REQUEST['text_'.$instance_id.'_'.$rule_id];
  115. $filename = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['name'];
  116. $mimetype = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['type'];
  117. $filesize = filesize($_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name']);
  118. $uploadedfile = $_FILES['upload_input_'.$instance_id.'_'.$rule_id]['tmp_name'];
  119. if ($filesize <= 0)
  120. $report_msg = 'Filesize cannot be null';
  121. elseif($filename == '')
  122. $report_msg = 'Filename cannot be empty';
  123. elseif($value=='' || $uploadedfile=='')
  124. $report_msg = 'Could not move file, please check with you administrator if file uploads are allowed.';
  125. else
  126. {
  127. $tmp = fileNameAndExtension($filename);
  128. extract($tmp);
  129. #write a filekey to send the file by the API
  130. $filekey = generateAFilekey(compact('filename', 'filesize', 'db','user_id'));
  131. #move the file like the API would do
  132. $file = $uploadedfile;
  133. $fileMoved = MoveFile(compact('filekey','db', 'file'));
  134. #generate a statement_id
  135. if($fileMoved)
  136. {
  137. $s3ql = compact('db', 'user_id');
  138. $s3ql['insert'] = 'file';
  139. $s3ql['where']['filekey'] = $filekey;
  140. $s3ql['where']['notes'] = trim($notes);
  141. $s3ql['where']['rule_id'] = $rule_id;
  142. $s3ql['where']['item_id'] = $instance_id;
  143. $s3ql['format']='html';
  144. $done = S3QLaction($s3ql);
  145. $msg=html2cell($done);$msg = $msg[2];
  146. #ereg('<file_id>([0-9]+)</file_id>', $done, $s3qlout);
  147. $statement_id = $msg['file_id'];
  148. if($msg['error_code']==0)
  149. {
  150. $s3ql['file_name'] = $filename;
  151. $insert='file';
  152. $S = compact('rule_id', 'instance_id','db','insert','filename');
  153. $report_msg = render_inserted($S, $statement_id);
  154. $report_msg .= sprintf("%s\n", ' <br /><input type="button" value="Insert Another" onClick="window.location=\''.$action['insertstatement'].'\'">');
  155. $report_msg .= sprintf("%s\n", ' &nbsp;&nbsp;<input type="button" value="Close Window" onClick="opener.window.location.reload(); self.close();return false;">');
  156. }
  157. else
  158. $report_msg = "<font color='red'>".$msg['message']."</font>";
  159. }
  160. else
  161. {
  162. $report_msg = "<font color='red'>Could not move the file</font>";
  163. }
  164. }
  165. }
  166. }
  167. echo $report_msg;
  168. exit;
  169. }
  170. }
  171. $index='1';
  172. echo '<form enctype="multipart/form-data" name="insertstatement" action="'.$action['insertstatement'].'" method="post" autocomplete="on">';
  173. echo render_empty_form(compact('index', 'rule_info', 'project_id', 'instance_id', 'db', 'user_id'));
  174. echo '<td valign="top"><input name="insert_'.$instance_id.'_'.$rule_id.'" value="Insert" type="submit"></td></tr>';
  175. ?>