<?php require 'libMysqli.php'; require 'libFunction.php'; header("Access-Control-Allow-Origin: *"); //无限制 //读取参数btih,并字符串化,小写化 $btih = getBtih(); $result = NULL; $count = safe_query("SELECT `d_index` FROM `video` WHERE `btih` = UNHEX(?);", &$result, array('s', $btih)); if ($count != 1) { die(json_err('btih_unavailable', -1, 'Error: Video Not Yet Exists, Do You Want to Create It?')); } //无返回值 exit($result[0]['d_index']); //返回字段d_index,该字段是json字符串
//http://stackoverflow.com/a/16308305 //var_dump($linkage_1); //echo '<br>'; //var_dump($linkage_2); //echo '<br>'; //if(count($linkage_1[$key_1])!=count($linkage_1[$key_1]))//但愿不会出现,也许这句话反而会制造麻烦 // die(json_err('link_count_not_match',-1,'Fatal Error: Link Not Match!')) $l_index_1[$key_1] = count($linkage_1[$key_1]); //这个自然是一个值,所以无所谓 $l_index_2[$key_2] = count($linkage_2[$key_2]); //这个自然是一个值,所以无所谓 //保存linkage和l_index $linkage_1 = json_encode($linkage_1); //array->json $l_index_1 = json_encode($l_index_1); //array->json $linkage_2 = json_encode($linkage_2); //array->json $l_index_2 = json_encode($l_index_2); //array->json //我没办法在这里检查update成功,但失败lib_Mysqli必然报错退出 //修改表`video`[vid,uid,btih,time,view,reply,comment,c_index,linkage,l_index,dislike,d_index] $blackhole = NULL; $count = safe_query("UPDATE `video` SET `linkage` = ?, `l_index` = ? WHERE `btih` = UNHEX(?);", &$blackhole, array('sss', $linkage_1, $l_index_1, $btih_1)); $blackhole = NULL; $count = safe_query("UPDATE `video` SET `linkage` = ?, `l_index` = ? WHERE `btih` = UNHEX(?);", &$blackhole, array('sss', $linkage_2, $l_index_2, $btih_2)); //提高积分并暂时硬直 normalFreeze($uid, $const_PointNewLink, $const_DelayNewLink); //返回成功页面 exit(json_err('newLink', 0, "Links Created Successfully!"));
//不论有没有Cookie都要获取最近Cookie数据 $result = NULL; $count = safe_query('SELECT * FROM `user` ORDER BY `uid` DESC LIMIT 1;', &$result); //SELECT * FROM `USER` WHERE `uid` IN (SELECT max(id) FROM `USER`); if ($count != 1) { die(json_err('user_notexist', -1, 'Error: No Users in Database at All')); } //必须先导入startup.sql } //然后获取下一个Cookie $uid = $result[0]['uid'] + 1; //无论如何都要取最近user的原因,因为封装我不能访问last_affected_id,而我需要向cookie写入uid $key = rand(0, 2147483647); $time = time() + 0; //观察期,暂定为新Cookie立刻可以发言 $point = 100; $status = 1; //保存新账号到数据库 $blackhole = NULL; $count = safe_query('INSERT INTO `user` VALUES (?, ?, ?, ?, ?);', &$blackhole, array('iiiii', $uid, $key, $time, $point, $status)); if ($count != 1) { die(json_err('user_notcreated', -1, 'Error: Failed to Create New Cookie')); } //返回空 //设置Cookie setcookie("uid", $uid, 2147483647); //Cookie永不过期 setcookie("key", $key, 2147483647); //Cookie永不过期 exit(json_err('newCookie', 0, 'New Cookie Begotten!')); // 用不着关闭MySQL
$uid = getUid(); //获取btih,查询视频是否已经存在,如btih不存在,退出 $btih = getBtih(); //checkBtih($btih);//用不着,下面语句解决了 //查询视频是否已经存在,如btih不存在,退出 $result = NULL; //d_index出错不会有严重影响,只要更新就好 $count = safe_query("SELECT `c_index`, `dislike`, `d_index` FROM `video` WHERE `btih` = UNHEX(?);", &$result, array('s', $btih)); //http://stackoverflow.com/questions/1747894/ if ($count != 0) { die(json_err('btih_created', -1, 'Error: Video Already Exists')); } //返回空 //添加到`video` $blackhole = NULL; $count = safe_query("INSERT INTO `video` (`uid`, `time`, `view`, `reply`, `btih`, `comment`, `c_index`, `linkage`, `l_index`, `dislike`, `d_index`) \nVALUES (?, ?, 0, 0, UNHEX(?), '', '[]', '{}', '{}', '{}', '{}');", &$blackhole, array('iis', $uid, time(), $btih)); //主键自增,comment赋空字符串,其余元素赋空数组 //startup.sql有一句SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; //NO_AUTO_VALUE_ON_ZERO禁用0,但我事实上传递的是NULL(其实是没传递),可以生成下一个序列号 //为一个NOT NULL的整型赋NULL值,结果是0,它并不会出错,参见http://niutuku.com/tech/Mysql/237698.shtml //MySQL会自动将NULL值转化为该字段的默认值,哪怕是你在表定义时没有明确地为该字段设置默认值 //newCookie.php因为已经获取了最新uid所以无须担心 if ($count != 1) { die(json_err('video_notcreated', -1, 'Error: Failed to Create New Video')); } //返回空 //提高积分并暂时硬直 normalFreeze($uid, $const_PointNewVideo, $const_DelayNewVideo); //返回成功页面 exit(json_err('newVideo', 0, "Video Created Successfully!"));
if ($count < 1) { //无返回值 die(json_err('btih_unavailable', -1, 'Error: No Video Submitted Last 7 Days')); } exit(str_replace('"LOWER(HEX(`btih`))":"', '"btih":"', json_encode($result))); //==>很粗糙是吧,但是没有转义的引号一定不属于字符串值本身,不是吗? break; //其实无用 //以上都不是则视为查询btih,形式上建议用参数action=find //其实无用 //以上都不是则视为查询btih,形式上建议用参数action=find case "find": //当没给出action,action给出空,action值不合法,action值为find时 //当没给出action,action给出空,action值不合法,action值为find时 default: //执行下面的代码 //读取参数btih,并字符串化,小写化 $btih = getBtih(); //按BTIH筛选,取所有信息 $result = NULL; $count = safe_query("SELECT LOWER(HEX(`btih`)), `time`, `view`, `reply` FROM `video` WHERE `btih` = UNHEX(?);", &$result, array('s', $btih)); if ($count != 1) { //无返回值 die(json_err('btih_unavailable', -1, 'Error: Video Not Yet Exists, Do You Want to Create It?')); } exit(str_replace('"LOWER(HEX(`btih`))":"', '"btih":"', json_encode($result[0]))); //既然只返回这一条,我想不应该再套一层数组 //==>很粗糙是吧,但是没有转义的引号一定不属于字符串值本身,不是吗? } die(json_err('process_flow', -1, 'Fatal Error: Unexpected Process Flow!'));
function normalFreeze($uid, $point, $delay) { $blackhole = NULL; $count = safe_query("UPDATE `user` SET `point` = `point` + ?, `time` = `time` + ? WHERE `uid` = ?;", &$blackhole, array('iii', $point, $delay, $uid)); if ($count != 1) { die(json_err('freezing_error', -1, 'Error: Freezing Failed')); } //返回空 return true; }
//要防止病从口入 $new_comment = json_encode($new_comment); //array->json $new_comment .= ','; //结尾添加逗号 //编辑索引[uid,time,size] $c_index = json_decode($result[0]['c_index'], true); //json->array(rather than object) //检验错误 $c_count = count($c_index); if ($result[0]['reply'] != $c_count) { die(json_err('reply_countnotmatch', -1, 'Error: Fatal Error! Counting Does not Match. Please Report to Admin!')); } if ($result[0]['LENGTH(`comment`)'] != $c_index[$c_count - 1][2]) { //$c_index[$c_count-1][2]即最后一条的size元素 die(json_err('reply_lengthnotmatch', -1, 'Error: Fatal Error! Length Does not Match. Please Report to Admin!')); } //编辑索引[uid,time,size] $c_index[] = array($uid, $the_time_now, $result[0]['LENGTH(`comment`)'] + strlen($new_comment)); $c_index = json_encode($c_index); //array->json ++$c_count; //我没办法在这里检查update成功,但失败lib_Mysqli必然报错退出 //修改表`video`[vid,uid,btih,time,view,reply,comment,c_index,linkage,l_index,dislike,d_index] $blackhole = NULL; $count = safe_query("UPDATE `video` SET `reply` = ?, `comment` = CONCAT(`comment`, ?), `c_index` = ? WHERE `btih` = UNHEX(?);", &$blackhole, array('isss', $c_count, $new_comment, $c_index, $btih)); //提高积分并暂时硬直 normalFreeze($uid, $const_PointNewComment, $const_DelayNewComment); //返回成功页面 exit(json_err('newComment', 0, "Comment Created Successfully!"));
function safe_query($query, &$result, $bind_params = NULL) { /* database consts */ global $DB_HOST, $DB_USER, $DB_PSWD, $DB_NAME, $DB_PORT; /* be aware 'const' works ONLY INSIDE of a class definition */ /* connection */ $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PSWD, $DB_NAME, $DB_PORT); //instantiate mysqli /* check connection */ if ($mysqli->connect_errno) { $err_info = json_err("db_connect", $mysqli->connect_errno, $mysqli->connect_error); die($err_info); } /* preparation */ $stmt = $mysqli->prepare($query); //prepare statement, $query must exist /* check preparation */ if (false === $stmt) { //prepare() will return a FALSE on error $err_info = json_err("db_prepare", $mysqli->errno, $mysqli->error); //a BOOLEAN $stmt has no errno or error property $mysqli->close(); die($err_info); } /* you can set up valuse of the parameters bounded, e.g. $emp_id=4; */ //$bind_param is transferred byVal, so don't worry. $bind_param_by_reference = array(array_shift($bind_params)); foreach ($bind_params as &$arg) { $bind_param_by_reference[] =& $arg; } /* binding parameters */ if (false === is_null($bind_params)) { //need binding parameters? yes! bool is-null() $rc = call_user_func_array(array(&$stmt, 'bind_param'), $bind_param_by_reference); //stop using class BindParam //call_user_func_array() binds parameters either byRef or byVal, whilst $stmt->bind_param() needs first parameter byVal and others by Ref. /* check binding */ if (false === $rc) { //bool mysqli_stmt::bind_param $err_info = json_err("db_bind_param", $stmt->errno, $stmt->error); $stmt->close(); $mysqli->close(); die($err_info); } } /* you can still renew valuse of the parameters bounded, e.g. $emp_id=5; */ /* execution */ $rc = $stmt->execute(); //execute statement /* check execution */ if (false === $rc) { //bool mysqli_stmt::execute $err_info = json_err("db_execute", $stmt->errno, $stmt->error); $stmt->close(); $mysqli->close(); die($err_info); } /* field_count */ $rc = $stmt->field_count; //int $mysqli_stmt->field_count; /* check field_count */ if ($rc < 1) { //no columns bounded return $stmt->affected_rows; } /* RETURN 0; if mysqli returns no columns, function finishes here returning no rows it's not an error because $stmt->errno is checked after execution */ /* store_result */ $rc = $stmt->store_result(); //store result /* check store_result */ if (false === $rc) { //bool mysqli_stmt::store_result //it is an erro because execution is correct ,and mysqli did returns something $err_info = json_err("db_store_result", $stmt->errno, $stmt->error); $stmt->close(); $mysqli->close(); die($err_info); } /* get metadata */ $meta = $stmt->result_metadata(); //it would be a mysqli_result object if (false === $meta) { //result_metadata() will return a FALSE on error though $err_info = json_err("db_result_metadata", $stmt->errno, $stmt->error); //a BOOLEAN $meta has no errno or error property, neither has object mysqli_result $stmt->close(); $mysqli->close(); die($err_info); } $bind_results = array(); $row = array(); while ($field = $meta->fetch_field()) { $bind_results[] =& $row[$field->name]; } // pass by reference!!! //http://php.net/manual/zh/class.mysqli-result.php#115009 /* bind_result */ $rc = call_user_func_array(array(&$stmt, 'bind_result'), $bind_results); //call_user_func_array() binds parameters either byRef or byVal, whilst $stmt->bind_result() needs all parameters by Ref. /* check binding */ if (false === $rc) { //bool mysqli_stmt::bind_result $err_info = json_err("db_bind_result", $stmt->errno, $stmt->error); $stmt->close(); $mysqli->close(); die($err_info); } /* fetch */ $i = 0; while ($stmt->fetch()) { $result[$i] = array(); foreach ($row as $k => $v) { $result[$i][$k] = $v; } $i++; } /* exit */ $count_rows = $stmt->affected_rows; $stmt->free_result(); $stmt->close(); $mysqli->close(); return $count_rows; /* RETURN $stmt->affected_rows; */ }
$this_uid = $c_index[$cid][0]; if ($this_uid == $uid) { die(json_err('uid_invalid', -1, 'Error: You Cannot Dislike Yourself!')); } //不许自己差评自己 $this_dislike = $dislike[$cid]; //$cid始终是字符串 if (in_array($uid, $this_dislike)) { die(json_err('dislike_resubmit', -1, 'Error: You Have Already Submitted a Dislike!')); } $this_dislike[] = $uid; //$cid始终是字符串 $dislike[$cid] = $this_dislike; $d_index[$cid] = count($this_dislike); //这个自然是一个值,所以无所谓 $dislike = json_encode($dislike); //array->json,测试了一下$cid被自动转成字符串了 $d_index = json_encode($d_index); //array->json,那么我就不再折腾一遍strval()好了 //我没办法在这里检查update成功,但失败lib_Mysqli必然报错退出 //修改表`video`[vid,uid,btih,time,view,reply,comment,c_index,linkage,l_index,dislike,d_index] $blackhole = NULL; $count = safe_query("UPDATE `video` SET `dislike` = ?, `d_index` = ? WHERE `btih` = UNHEX(?);", &$blackhole, array('sss', $dislike, $d_index, $btih)); $now = time(); //差评对方$this_uid,对方uid必然存在,是由newComment.php保证的 $count = safe_query("UPDATE `user` SET `point` = (CASE WHEN `point` + ? > 0 THEN `point` + ? ELSE 0 END), \n`time` = (CASE WHEN `point` + ? > 0 THEN `time` ELSE (CASE WHEN `time` > ? THEN `time` ELSE ? END) + ? END) \nWHERE `uid` = ?;", &$blackhole, array('iiiiiii', $const_PointNewDislike, $const_PointNewDislike, $const_PointNewDislike, $now, $now, $const_DelayRate, $this_uid)); //减少我方$uid并暂时硬直 $count = safe_query("UPDATE `user` SET `point` = (CASE WHEN `point` + ? > 0 THEN `point` + ? ELSE 0 END), \n`time` = (CASE WHEN `point` + ? > 0 THEN `time` + ? ELSE (CASE WHEN `time` > ? THEN `time` ELSE ? END) + ? END) \nWHERE `uid` = ?;", &$blackhole, array('iiiiiiii', $const_PointNewDislike, $const_PointNewDislike, $const_PointNewDislike, $const_DelayNewDislike, $now, $now, $const_DelayRate, $uid)); //返回成功页面 exit(json_err('newDislike', 0, "Dislike Created Successfully!"));
$err = curl_error($c); curl_close($c); json_error($err); } break; case 'process_file': if (file_exists($r_data) && dirname(realpath($r_data)) === realpath($tmp_add_dir)) { print json_encode(process_torrent_data(file_get_contents($r_data), basename($r_data), false)); } else { json_error('Bad path or filename'); } break; case 'process_magnet': $data = @json_decode($r_data, true); if (!$data || !$data['url'] || !$data['name'] || !$data['hash']) { json_err('Invalid request'); } $data['files'] = array('(Filenames not known for magnet links)' => 0); print json_encode(save_add_data($data['hash'], $data)); break; case 'add': echo <<<HTML <style type="text/css"> body { background: white; color: black; } </style> <script type="text/javascript"> function closeWindow() { if (window.top.hideDialog) {