Advertisement
Guest User

Untitled

a guest
Sep 6th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. <?php
  2. //require_once('utils2.php');
  3.  
  4. function get_users($filename) {
  5. $i=0;
  6. $lines_us = file($filename);
  7. foreach($lines_us as $idx => $line) {
  8. if (preg_match('/^user:([\A-Za-z0-9А-Яа-я]+)/', $line, $matches)) {
  9. $result[$i]['user'] = $matches[1];
  10. $u = true;
  11. continue;
  12. }
  13. if ($u && preg_match('/^pass:([\А-Яа-яA-Za-z0-9]+)/', $line, $matches)) {
  14. $result[$i]['pass'] = $matches[1];
  15. $u = false;
  16. $i++;
  17. }
  18. }
  19. return $result;
  20. }
  21.  
  22. function check_users($array,$user,$pass){
  23. foreach($array as $idx => $str) {
  24. if ($str['user'] == $user && $str['pass'] == $pass)
  25. return true;
  26. }
  27. return false;
  28. }
  29. //функция для работы с БД
  30. function db(){
  31. $db_host = 'localhost';
  32. $db_name = 'db1';
  33. $db_username = 'root';
  34. $db_password = '';
  35.  
  36. $db_conn_str = "mysql:host=$db_host;dbname=$db_name";
  37.  
  38.  
  39. $dbh = new PDO($db_conn_str, $db_username, $db_password, array(
  40. PDO::ATTR_AUTOCOMMIT => false,
  41. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  42. PDO::ATTR_CASE => PDO::CASE_LOWER,
  43. PDO::MYSQL_ATTR_INIT_COMMAND => 'set names cp1251',
  44. ));
  45. return $dbh;
  46. }//db
  47.  
  48. //функция возвращает всю информацию о всех имеющихся студентах
  49. function getStudents(){
  50. if (is_dir("labs")) {
  51. $dir='labs';
  52. $i=0;
  53. $files_and_dirs = scandir($dir);}
  54. foreach ($files_and_dirs as $idx=>$dir_or_file){
  55. if (is_dir("labs/".$dir_or_file)&&$dir_or_file!='.'&&$dir_or_file!='..'){
  56. $dirs[$i]=$dir_or_file;
  57. $i++;
  58. //echo $dir_or_file;
  59. }
  60. }
  61. return $dirs;
  62. }//getStudents()
  63.  
  64. function getStudentResults($login){
  65. // $students[$id]['name']=$login;
  66. $i=0;
  67. $labs=[];
  68. $get=[];
  69. $sql=false;
  70. $lines = file("labs/$login/".$login.'.txt');
  71. foreach($lines as $idx => $line) {
  72. if (preg_match('/^Лабораторная ([\a-zA-Z0-9#]+):([\sА-Яа-яA-Za-z0-9#]+)/u', $line, $matches)) {
  73. if ($sql) {$sql=false;$i++;}
  74. $get[$i]['lab']=trim($matches[1]);
  75. $get[$i]['name']=trim($matches[2]);
  76.  
  77. }
  78. if (preg_match('/^Задание №([\0-9#]+)/', $line, $matches)) {
  79. $get[$i]['task']=$matches[1];
  80. $get[$i]['date']=$lines[$idx+1];
  81. $get[$i]['status']=$lines[$idx+2];
  82. }
  83. if (preg_match('/^\s*select/', $line)) $sql=true;
  84. //if ($sql&&preg_match('/^Лабораторная №/',$line)) {$sql=false;$i++;};
  85. if ($sql) $get[$i]['sql'].=$line;
  86. }
  87. for ($i=0;$i<sizeof($get);$i++){
  88. if ($get[$i]['name']=='') $labs[$i]=$get[$i]['lab'];
  89. else $labs[$i]=$get[$i]['name'];
  90. }
  91. if ($labs!=NULL) sort($labs);
  92. $labs=array_unique($labs);
  93. $student['labs']=$labs;
  94. $student['lab_with_tasks']=$get;
  95. return $student;
  96. }//getStudentResults
  97. //возвращает настройки
  98. function getSettings(){
  99. require_once('settings.php');
  100. $s=new settings;
  101. $s->get_file("settings.txt");
  102. $color_sovpav=$s->color_sovpav;
  103. $color_your=$s->color_your;
  104. $color_true=$s->color_true;
  105. $max_count_sovpav=$s->max_count_sovpav;
  106. $settings['color_matched']=$color_sovpav;
  107. $settings['color_wrong']=$color_your;
  108. $settings['color_right']=$color_true;
  109. $settings['max_count_matched']=$max_count_sovpav;
  110. $settings['width_query']=$s->width_query;
  111. $settings['height_query']=$s->height_query;
  112. return $settings;
  113. }
  114. //Возвращает данные для compareRows
  115. function checkTask(){
  116. $dbh=db();
  117. $rows[1]=getRows($dbh,$_REQUEST['query']);
  118. $rows[2]=getRows($dbh,get_right_sql($_REQUEST['lab'],$_REQUEST['task']));
  119. //$rows[1]=getRows($dbh,"select * from countries");
  120. //$rows[2]=getRows($dbh,"select * from countries");
  121.  
  122. /*$rows[1]=getRows($dbh,"select P.PRODUCT_ID,
  123. P.PRODUCT_NAME,
  124. P.WARRANTY_PERIOD as \"WARRANTY_MONTHS\",
  125. P.LIST_PRICE,
  126. P.CATALOG_URL
  127. from PRODUCT_INFORMATION P
  128. where P.PRODUCT_NAME rlike '^.*([0-9]+)\s*(mb|gb).*$' and
  129. P.PRODUCT_NAME not rlike '^hd' and
  130. P.PRODUCT_DESCRIPTION not rlike '^.{0,30}(disk|hard|drive)'
  131. order by P.LIST_PRICE
  132. ;
  133. ");
  134. $rows[2]=getRows($dbh,"select P.PRODUCT_ID,
  135. P.PRODUCT_NAME,
  136. P.WARRANTY_PERIOD as \"WARRANTY_MONTHS\",
  137. P.LIST_PRICE,
  138. P.CATALOG_URL
  139. from PRODUCT_INFORMATION P
  140. where P.PRODUCT_NAME rlike '^.*([0-9]+)\s*(mb|gb).*$' and
  141. P.PRODUCT_NAME not rlike '^hd' and
  142. P.PRODUCT_DESCRIPTION not rlike '^.{0,30}(disk|hard|drive)'
  143. order by P.LIST_PRICE
  144. ;
  145. ");*/
  146. //$r=array_pop($rows[1]['rows']);
  147. //$r=array_pop($rows[1]['rows']);
  148. /*$r=array_pop($rows[1]['rows']);
  149. $r=array_pop($rows[1]['rows']);
  150. $r=array_pop($rows[1]['rows']);
  151. $r=array_pop($rows[1]['rows']);*/
  152. /*$r=array_pop($rows[2]['rows']);
  153. $r=array_pop($rows[2]['rows']);
  154. $r=array_pop($rows[2]['rows']);
  155. $r=array_pop($rows[2]['rows']);
  156. $r=array_pop($rows[2]['rows']);
  157. $r=array_pop($rows[2]['rows']);
  158. $r=array_pop($rows[2]['rows']);
  159. $r=array_pop($rows[2]['rows']);
  160. $r=array_pop($rows[2]['rows']);
  161. $r=array_pop($rows[2]['rows']);
  162. $r=array_pop($rows[2]['rows']);
  163. $r=array_pop($rows[2]['rows']);
  164. $r=array_pop($rows[2]['rows']);
  165. $r=array_pop($rows[2]['rows']);
  166. $r=array_pop($rows[2]['rows']);
  167. $r=array_pop($rows[2]['rows']);
  168. $r=array_pop($rows[2]['rows']);*/
  169. // $r=array_pop($rows[2]['rows']);
  170. // $r=array_pop($rows[2]['rows']);
  171. // $r=array_pop($rows[2]['rows']);
  172. //$rows[1]['columns'][0]='code';
  173. // $rows[1]['rows'][3][1]='wrong';
  174. //$rows[1]['rows']=array_slice($rows[1]['rows'],0,-1);
  175. return compareRows($rows[1],$rows[2]);
  176. }//checkTask
  177.  
  178. $act=$_REQUEST['action'];
  179. //$act_post=$_POST['action'];
  180.  
  181. if ($act=='getAuth'){
  182. $arr_us = get_users('users.txt');
  183. session_start();
  184. if (check_users($arr_us, $_REQUEST['login'], $_REQUEST['password'])) {
  185. $_SESSION['login']=$_REQUEST['login'];
  186. echo(json_encode(true));
  187. }
  188. else {
  189. unset($_SESSION['login']);
  190. echo(json_encode(false));
  191. }
  192. }
  193. if ($act=='getLabs'){//загружает меню лабораторных и заданий
  194. require_once('utils2.php');
  195. $arr = readLabs();
  196. //var_dump($labs);
  197. echo(json_encode($arr));
  198. }
  199. if ($act=='getStudents'){//Cтроит список студентов
  200. echo(json_encode(getStudents()));
  201. }
  202. //Cтроит список лабораторных заданного студента по его номеру
  203. //Cтроит таблицу лабораторной по ее номеру
  204. if ($act=='getStudentResults'){
  205. echo(json_encode(getStudentResults($_REQUEST['name'])));
  206. }
  207.  
  208. if ($act=='right_otvet'){
  209. require_once('utils2.php');
  210. $dbh=db();
  211. echo(json_encode(getRows($dbh,get_right_sql($_REQUEST['lab'],$_REQUEST['task']))));
  212. }
  213. if ($act=='your_otvet'){
  214. require_once('utils2.php');
  215. $dbh=db();
  216. echo(json_encode(getRows($dbh,$_REQUEST['query'])));
  217. }
  218. if ($act=='checkTask'){
  219. require_once('utils2.php');
  220. $dbh=db();
  221. $params=checkTask();
  222. echo(json_encode($params));
  223. }
  224. if ($act=='getSettings'){
  225. require_once('settings.php');
  226. echo(json_encode(getSettings()));
  227. }
  228. if ($act=='saveTask'){
  229. require_once('utils2.php');
  230. require_once('settings.php');
  231. $params=checkTask();
  232. $status=$params['message'];
  233. require_once('write.php');
  234. echo(json_encode($params));
  235. }
  236.  
  237. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement