OSDN Git Service

Initial contents of nce
[nce/stable.git] / nc_update_readcnt_db.php
1 <?php
2 /*
3  NCE (Neetchan Cloud Environment.)
4  name: nc_update_readcnt_db.php
5  author: Akira Ohta (akohta001.gmail.com)
6  date: 2013-apr-14th
7  description:
8  
9  */
10 include_once "account.php";
11 include_once "preference.php";
12 include_once "database.php";
13 include_once "log.php";
14 include_once "http_util.php";
15 include_once "error_code.php";
16
17 $user_id = $_POST['user'];
18 $hashed_passwd = $_POST['hashed_passwd'];
19 $board_name = $_POST['board_name'];
20 $dat_name = $_POST['dat_name'];
21 $res_number = $_POST['res_number'];
22 $record_type = $_POST['record_type'];
23
24 //--- authorization ---
25 $user = chk_passwd( $user_id, $hashed_passwd);
26 if(FALSE === $user){
27         echo $ERROR_MSG;
28         exit;
29 }
30
31 //--- parameter check start ---
32 if(!chk_param($board_name) || !chk_param($dat_name) ||
33         !chk_param($res_number) || !chk_param($record_type)){
34         echo ERROR303;
35         exit;
36 }
37
38 if(!is_numeric($res_number) || !is_numeric($record_type)){
39         echo ERROR303;
40         exit;
41 }
42
43 if(1 != $record_type && 4 != $record_type){
44         echo ERROR304;
45         exit;
46 }
47
48 if(0 >= $res_number || 1010 <= $res_number){
49         echo ERROR304;
50         exit;
51 }
52 //--- parameter check end ---
53
54 $table_name = TABLE_PREFIX."tbl{$user->home_dir}";
55
56 $query1 = <<< QUERY1
57 select id from $table_name 
58  where record_type="{$record_type}" and 
59  board_name='{$board_name}' and 
60   dat_name='{$dat_name}'
61 QUERY1;
62
63 $query2 = <<< QUERY2
64 insert into  $table_name 
65          (record_type, res_number, dat_name, board_name) 
66          values ('{$record_type}', {$res_number}, "{$dat_name}", "{$board_name}")
67 QUERY2;
68
69 $query3 = <<< QUERY3
70 update $table_name 
71         set res_number="{$res_number}" 
72         where record_type="{$record_type}" and 
73                 board_name="{$board_name}" and 
74                 dat_name='{$dat_name}' 
75 QUERY3;
76
77 try{
78         $db->open();
79         $result = $db->execute($query1);
80         if(!$result || 0 == $result->num_rows){
81                 $result = $db->execute($query2);
82                 if(!$result){
83                         echo ERROR501;
84                         exit;
85                 }
86         }else{
87                 $result = $db->execute($query3);
88                 if(!$result){
89                         echo ERROR502;
90                         exit;
91                 }
92         }
93         $db->close();
94 }catch(Exception $e){
95         $error = __FILE__."(".__LINE__.
96                 ") An error occured during update a record.";
97         nc_logging($error, $e);
98         echo ERROR500;
99         exit;
100 }
101
102 echo SUCCESS100;
103
104 ?>