3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * File to handle all API requests * Accepts GET and POST * * Each request will be identified by TAG * Response will be JSON data /** * check for POST request */ if (isset($_POST['tag']) && $_POST['tag'] != '') { // get tag $tag = $_POST['tag']; // include db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); // response Array $response = array("tag" => $tag, "error" => FALSE); // check for tag type if ($tag == 'login') { // Request type is check Login $email = $_POST['email']; $password = $_POST['password']; // check for user $user = $db->getUserByEmailAndPassword($email, $password); if ($user != false) { // user found $response["error"] = FALSE; $response["user"]["unique_id"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; $response["user"]["StudentID"] = $user["StudentID"]; echo json_encode($response); } else { // user not found // echo json with error = 1 $response["error"] = TRUE; $response["error_msg"] = "Incorrect email or password!"; echo json_encode($response); } } else if ($tag == 'register') { // Request type is Register new user $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; // check if user is already existed if ($db->isUserExisted($email)) { // user is already existed - error response $response["error"] = TRUE; $response["error_msg"] = "User already existed"; echo json_encode($response); } else { // store user $user = $db->storeUser($name, $email, $password); if ($user) { // user stored successfully $response["error"] = FALSE; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response); } else { // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Error occurred in registration"; echo json_encode($response); } } } else if ($tag == 'getCourses') { $user = FALSE; if(isset ($_POST['StudentID'])) $user = $_POST['StudentID']; $selectionKey = $_POST['searchKey']; $selectionValue = $_POST['searchValue']; $response = $db->getCourses($user, $selectionKey, $selectionValue); if($response){ $response["error"] = FALSE; } else { $response["error"]=TRUE; $response["error_mesg"] = "Error occurred getting the courses"; } echo json_encode($response); } else if ($tag = 'addCourse'){ // Request type is add a course to a specific student $StudentID = $_POST['StudentID']; $CourseID = $_POST['CourseID']; $email = $_POST['email']; // check if user is already existed if ($db->isUserExisted($email)) { // store course $course = $db->storeCourse($StudentID, $CourseID); if ($course !=FALSE) { // course stored successfully $response["error"] = FALSE; $response["course"]["StudentID"] = $course["StudentID"]; $response["course"]["CourseID"] = $course["CourseID"]; echo json_encode($response); } else { // course failed to store $response["error"] = TRUE; $response["error_msg"] = "Error occured adding courses"; echo json_encode($response); } } else{ $response["error"] = TRUE; $response["error_msg"] = "User does not exist"; echo json_encode($response); } } else if($tag == 'getFriends'){ $courseID = $_POST['courseID']; $studentID = $_POST['studentID']; $result = $db->getFriendsByCourseIdAndStudentId($courseID, $studentID); if($result !=FALSE){ $result['error']=FALSE; } else{ $result['error']=TRUE; $result["error_mesg"] = "Error occurred retrieving friends"; } echo json_encode($result); } else { // user failed to store $response["error"] = TRUE; $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'"; echo json_encode($response); } } else { $response["error"] = TRUE; $response["error_msg"] = "Required parameter 'tag' is missing!"; echo json_encode($response); } ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 264
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 73
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 64
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 72
Branch analysis from position: 72
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
1 jumps found. (Code = 42) Position 1 = 272
Branch analysis from position: 272
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 73
2 jumps found. (Code = 43) Position 1 = 75, Position 2 = 139
Branch analysis from position: 75
2 jumps found. (Code = 43) Position 1 = 88, Position 2 = 97
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 138
Branch analysis from position: 138
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 97
2 jumps found. (Code = 43) Position 1 = 104, Position 2 = 130
Branch analysis from position: 104
1 jumps found. (Code = 42) Position 1 = 138
Branch analysis from position: 138
Branch analysis from position: 130
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 139
2 jumps found. (Code = 43) Position 1 = 141, Position 2 = 173
Branch analysis from position: 141
2 jumps found. (Code = 43) Position 1 = 145, Position 2 = 148
Branch analysis from position: 145
2 jumps found. (Code = 43) Position 1 = 161, Position 2 = 164
Branch analysis from position: 161
1 jumps found. (Code = 42) Position 1 = 168
Branch analysis from position: 168
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 164
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 148
Branch analysis from position: 173
2 jumps found. (Code = 43) Position 1 = 175, Position 2 = 228
Branch analysis from position: 175
2 jumps found. (Code = 43) Position 1 = 188, Position 2 = 219
Branch analysis from position: 188
2 jumps found. (Code = 43) Position 1 = 195, Position 2 = 210
Branch analysis from position: 195
1 jumps found. (Code = 42) Position 1 = 218
Branch analysis from position: 218
1 jumps found. (Code = 42) Position 1 = 227
Branch analysis from position: 227
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 210
1 jumps found. (Code = 42) Position 1 = 227
Branch analysis from position: 227
Branch analysis from position: 219
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 228
2 jumps found. (Code = 43) Position 1 = 230, Position 2 = 255
Branch analysis from position: 230
2 jumps found. (Code = 43) Position 1 = 243, Position 2 = 246
Branch analysis from position: 243
1 jumps found. (Code = 42) Position 1 = 250
Branch analysis from position: 250
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 246
1 jumps found. (Code = 42) Position 1 = 263
Branch analysis from position: 263
Branch analysis from position: 255
1 jumps found. (Code = 42) Position 1 = 272
Branch analysis from position: 272
Branch analysis from position: 264
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/0comu
function name:  (null)
number of ops:  273
compiled vars:  !0 = $tag, !1 = $db, !2 = $response, !3 = $email, !4 = $password, !5 = $user, !6 = $name, !7 = $selectionKey, !8 = $selectionValue, !9 = $StudentID, !10 = $CourseID, !11 = $course, !12 = $courseID, !13 = $studentID, !14 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   FETCH_IS                                         ~15     '_POST'
          1        ISSET_ISEMPTY_DIM_OBJ                         0  ~16     ~15, 'tag'
          2      > JMPZ_EX                                          ~16     ~16, ->7
          3    >   FETCH_R                      global              ~17     '_POST'
          4        FETCH_DIM_R                                      ~18     ~17, 'tag'
          5        IS_NOT_EQUAL                                     ~19     ~18, ''
          6        BOOL                                             ~16     ~19
          7    > > JMPZ                                                     ~16, ->264
   15     8    >   FETCH_R                      global              ~20     '_POST'
          9        FETCH_DIM_R                                      ~21     ~20, 'tag'
         10        ASSIGN                                                   !0, ~21
   18    11        INCLUDE_OR_EVAL                                          'include%2FDB_Functions.php', REQUIRE_ONCE
   19    12        NEW                                              $24     'DB_Functions'
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !1, $24
   22    15        INIT_ARRAY                                       ~27     !0, 'tag'
         16        ADD_ARRAY_ELEMENT                                ~27     <false>, 'error'
         17        ASSIGN                                                   !2, ~27
   25    18        IS_EQUAL                                                 !0, 'login'
         19      > JMPZ                                                     ~29, ->73
   27    20    >   FETCH_R                      global              ~30     '_POST'
         21        FETCH_DIM_R                                      ~31     ~30, 'email'
         22        ASSIGN                                                   !3, ~31
   28    23        FETCH_R                      global              ~33     '_POST'
         24        FETCH_DIM_R                                      ~34     ~33, 'password'
         25        ASSIGN                                                   !4, ~34
   31    26        INIT_METHOD_CALL                                         !1, 'getUserByEmailAndPassword'
         27        SEND_VAR_EX                                              !3
         28        SEND_VAR_EX                                              !4
         29        DO_FCALL                                      0  $36     
         30        ASSIGN                                                   !5, $36
   32    31        BOOL                                             ~38     !5
         32      > JMPZ                                                     ~38, ->64
   34    33    >   ASSIGN_DIM                                               !2, 'error'
         34        OP_DATA                                                  <false>
   35    35        FETCH_DIM_R                                      ~42     !5, 'unique_id'
         36        FETCH_DIM_W                                      $40     !2, 'user'
         37        ASSIGN_DIM                                               $40, 'unique_id'
         38        OP_DATA                                                  ~42
   36    39        FETCH_DIM_R                                      ~45     !5, 'name'
         40        FETCH_DIM_W                                      $43     !2, 'user'
         41        ASSIGN_DIM                                               $43, 'name'
         42        OP_DATA                                                  ~45
   37    43        FETCH_DIM_R                                      ~48     !5, 'email'
         44        FETCH_DIM_W                                      $46     !2, 'user'
         45        ASSIGN_DIM                                               $46, 'email'
         46        OP_DATA                                                  ~48
   38    47        FETCH_DIM_R                                      ~51     !5, 'created_at'
         48        FETCH_DIM_W                                      $49     !2, 'user'
         49        ASSIGN_DIM                                               $49, 'created_at'
         50        OP_DATA                                                  ~51
   39    51        FETCH_DIM_R                                      ~54     !5, 'updated_at'
         52        FETCH_DIM_W                                      $52     !2, 'user'
         53        ASSIGN_DIM                                               $52, 'updated_at'
         54        OP_DATA                                                  ~54
   40    55        FETCH_DIM_R                                      ~57     !5, 'StudentID'
         56        FETCH_DIM_W                                      $55     !2, 'user'
         57        ASSIGN_DIM                                               $55, 'StudentID'
         58        OP_DATA                                                  ~57
   41    59        INIT_FCALL                                               'json_encode'
         60        SEND_VAR                                                 !2
         61        DO_ICALL                                         $58     
         62        ECHO                                                     $58
         63      > JMP                                                      ->72
   45    64    >   ASSIGN_DIM                                               !2, 'error'
         65        OP_DATA                                                  <true>
   46    66        ASSIGN_DIM                                               !2, 'error_msg'
         67        OP_DATA                                                  'Incorrect+email+or+password%21'
   47    68        INIT_FCALL                                               'json_encode'
         69        SEND_VAR                                                 !2
         70        DO_ICALL                                         $61     
         71        ECHO                                                     $61
         72    > > JMP                                                      ->263
   50    73    >   IS_EQUAL                                                 !0, 'register'
         74      > JMPZ                                                     ~62, ->139
   52    75    >   FETCH_R                      global              ~63     '_POST'
         76        FETCH_DIM_R                                      ~64     ~63, 'name'
         77        ASSIGN                                                   !6, ~64
   53    78        FETCH_R                      global              ~66     '_POST'
         79        FETCH_DIM_R                                      ~67     ~66, 'email'
         80        ASSIGN                                                   !3, ~67
   54    81        FETCH_R                      global              ~69     '_POST'
         82        FETCH_DIM_R                                      ~70     ~69, 'password'
         83        ASSIGN                                                   !4, ~70
   57    84        INIT_METHOD_CALL                                         !1, 'isUserExisted'
         85        SEND_VAR_EX                                              !3
         86        DO_FCALL                                      0  $72     
         87      > JMPZ                                                     $72, ->97
   59    88    >   ASSIGN_DIM                                               !2, 'error'
         89        OP_DATA                                                  <true>
   60    90        ASSIGN_DIM                                               !2, 'error_msg'
         91        OP_DATA                                                  'User+already+existed'
   61    92        INIT_FCALL                                               'json_encode'
         93        SEND_VAR                                                 !2
         94        DO_ICALL                                         $75     
         95        ECHO                                                     $75
         96      > JMP                                                      ->138
   64    97    >   INIT_METHOD_CALL                                         !1, 'storeUser'
         98        SEND_VAR_EX                                              !6
         99        SEND_VAR_EX                                              !3
        100        SEND_VAR_EX                                              !4
        101        DO_FCALL                                      0  $76     
        102        ASSIGN                                                   !5, $76
   65   103      > JMPZ                                                     !5, ->130
   67   104    >   ASSIGN_DIM                                               !2, 'error'
        105        OP_DATA                                                  <false>
   68   106        FETCH_DIM_R                                      ~80     !5, 'unique_id'
        107        ASSIGN_DIM                                               !2, 'uid'
        108        OP_DATA                                                  ~80
   69   109        FETCH_DIM_R                                      ~83     !5, 'name'
        110        FETCH_DIM_W                                      $81     !2, 'user'
        111        ASSIGN_DIM                                               $81, 'name'
        112        OP_DATA                                                  ~83
   70   113        FETCH_DIM_R                                      ~86     !5, 'email'
        114        FETCH_DIM_W                                      $84     !2, 'user'
        115        ASSIGN_DIM                                               $84, 'email'
        116        OP_DATA                                                  ~86
   71   117        FETCH_DIM_R                                      ~89     !5, 'created_at'
        118        FETCH_DIM_W                                      $87     !2, 'user'
        119        ASSIGN_DIM                                               $87, 'created_at'
        120        OP_DATA                                                  ~89
   72   121        FETCH_DIM_R                                      ~92     !5, 'updated_at'
        122        FETCH_DIM_W                                      $90     !2, 'user'
        123        ASSIGN_DIM                                               $90, 'updated_at'
        124        OP_DATA                                                  ~92
   73   125        INIT_FCALL                                               'json_encode'
        126        SEND_VAR                                                 !2
        127        DO_ICALL                                         $93     
        128        ECHO                                                     $93
        129      > JMP                                                      ->138
   76   130    >   ASSIGN_DIM                                               !2, 'error'
        131        OP_DATA                                                  <true>
   77   132        ASSIGN_DIM                                               !2, 'error_msg'
        133        OP_DATA                                                  'Error+occurred+in+registration'
   78   134        INIT_FCALL                                               'json_encode'
        135        SEND_VAR                                                 !2
        136        DO_ICALL                                         $96     
        137        ECHO                                                     $96
        138    > > JMP                                                      ->263
   82   139    >   IS_EQUAL                                                 !0, 'getCourses'
        140      > JMPZ                                                     ~97, ->173
   83   141    >   ASSIGN                                                   !5, <false>
   84   142        FETCH_IS                                         ~99     '_POST'
        143        ISSET_ISEMPTY_DIM_OBJ                         0          ~99, 'StudentID'
        144      > JMPZ                                                     ~100, ->148
   85   145    >   FETCH_R                      global              ~101    '_POST'
        146        FETCH_DIM_R                                      ~102    ~101, 'StudentID'
        147        ASSIGN                                                   !5, ~102
   87   148    >   FETCH_R                      global              ~104    '_POST'
        149        FETCH_DIM_R                                      ~105    ~104, 'searchKey'
        150        ASSIGN                                                   !7, ~105
   88   151        FETCH_R                      global              ~107    '_POST'
        152        FETCH_DIM_R                                      ~108    ~107, 'searchValue'
        153        ASSIGN                                                   !8, ~108
   90   154        INIT_METHOD_CALL                                         !1, 'getCourses'
        155        SEND_VAR_EX                                              !5
        156        SEND_VAR_EX                                              !7
        157        SEND_VAR_EX                                              !8
        158        DO_FCALL                                      0  $110    
        159        ASSIGN                                                   !2, $110
   91   160      > JMPZ                                                     !2, ->164
   92   161    >   ASSIGN_DIM                                               !2, 'error'
        162        OP_DATA                                                  <false>
        163      > JMP                                                      ->168
   94   164    >   ASSIGN_DIM                                               !2, 'error'
        165        OP_DATA                                                  <true>
   95   166        ASSIGN_DIM                                               !2, 'error_mesg'
        167        OP_DATA                                                  'Error+occurred+getting+the+courses'
   97   168    >   INIT_FCALL                                               'json_encode'
        169        SEND_VAR                                                 !2
        170        DO_ICALL                                         $115    
        171        ECHO                                                     $115
        172      > JMP                                                      ->263
  101   173    >   ASSIGN                                           ~116    !0, 'addCourse'
        174      > JMPZ                                                     ~116, ->228
  104   175    >   FETCH_R                      global              ~117    '_POST'
        176        FETCH_DIM_R                                      ~118    ~117, 'StudentID'
        177        ASSIGN                                                   !9, ~118
  105   178        FETCH_R                      global              ~120    '_POST'
        179        FETCH_DIM_R                                      ~121    ~120, 'CourseID'
        180        ASSIGN                                                   !10, ~121
  106   181        FETCH_R                      global              ~123    '_POST'
        182        FETCH_DIM_R                                      ~124    ~123, 'email'
        183        ASSIGN                                                   !3, ~124
  109   184        INIT_METHOD_CALL                                         !1, 'isUserExisted'
        185        SEND_VAR_EX                                              !3
        186        DO_FCALL                                      0  $126    
        187      > JMPZ                                                     $126, ->219
  112   188    >   INIT_METHOD_CALL                                         !1, 'storeCourse'
        189        SEND_VAR_EX                                              !9
        190        SEND_VAR_EX                                              !10
        191        DO_FCALL                                      0  $127    
        192        ASSIGN                                                   !11, $127
  113   193        BOOL                                             ~129    !11
        194      > JMPZ                                                     ~129, ->210
  115   195    >   ASSIGN_DIM                                               !2, 'error'
        196        OP_DATA                                                  <false>
  116   197        FETCH_DIM_R                                      ~133    !11, 'StudentID'
        198        FETCH_DIM_W                                      $131    !2, 'course'
        199        ASSIGN_DIM                                               $131, 'StudentID'
        200        OP_DATA                                                  ~133
  117   201        FETCH_DIM_R                                      ~136    !11, 'CourseID'
        202        FETCH_DIM_W                                      $134    !2, 'course'
        203        ASSIGN_DIM                                               $134, 'CourseID'
        204        OP_DATA                                                  ~136
  118   205        INIT_FCALL                                               'json_encode'
        206        SEND_VAR                                                 !2
        207        DO_ICALL                                         $137    
        208        ECHO                                                     $137
        209      > JMP                                                      ->218
  121   210    >   ASSIGN_DIM                                               !2, 'error'
        211        OP_DATA                                                  <true>
  122   212        ASSIGN_DIM                                               !2, 'error_msg'
        213        OP_DATA                                                  'Error+occured+adding+courses'
  123   214        INIT_FCALL                                               'json_encode'
        215        SEND_VAR                                                 !2
        216        DO_ICALL                                         $140    
        217        ECHO                                                     $140
        218    > > JMP                                                      ->227
  127   219    >   ASSIGN_DIM                                               !2, 'error'
        220        OP_DATA                                                  <true>
  128   221        ASSIGN_DIM                                               !2, 'error_msg'
        222        OP_DATA                                                  'User+does+not+exist'
  129   223        INIT_FCALL                                               'json_encode'
        224        SEND_VAR                                                 !2
        225        DO_ICALL                                         $143    
        226        ECHO                                                     $143
        227    > > JMP                                                      ->263
  133   228    >   IS_EQUAL                                                 !0, 'getFriends'
        229      > JMPZ                                                     ~144, ->255
  134   230    >   FETCH_R                      global              ~145    '_POST'
        231        FETCH_DIM_R                                      ~146    ~145, 'courseID'
        232        ASSIGN                                                   !12, ~146
  135   233        FETCH_R                      global              ~148    '_POST'
        234        FETCH_DIM_R                                      ~149    ~148, 'studentID'
        235        ASSIGN                                                   !13, ~149
  136   236        INIT_METHOD_CALL                                         !1, 'getFriendsByCourseIdAndStudentId'
        237        SEND_VAR_EX                                              !12
        238        SEND_VAR_EX                                              !13
        239        DO_FCALL                                      0  $151    
        240        ASSIGN                                                   !14, $151
  137   241        BOOL                                             ~153    !14
        242      > JMPZ                                                     ~153, ->246
  138   243    >   ASSIGN_DIM                                               !14, 'error'
        244        OP_DATA                                                  <false>
        245      > JMP                                                      ->250
  140   246    >   ASSIGN_DIM                                               !14, 'error'
        247        OP_DATA                                                  <true>
  141   248        ASSIGN_DIM                                               !14, 'error_mesg'
        249        OP_DATA                                                  'Error+occurred+retrieving+friends'
  143   250    >   INIT_FCALL                                               'json_encode'
        251        SEND_VAR                                                 !14
        252        DO_ICALL                                         $157    
        253        ECHO                                                     $157
        254      > JMP                                                      ->263
  148   255    >   ASSIGN_DIM                                               !2, 'error'
        256        OP_DATA                                                  <true>
  149   257        ASSIGN_DIM                                               !2, 'error_msg'
        258        OP_DATA                                                  'Unknow+%27tag%27+value.+It+should+be+either+%27login%27+or+%27register%27'
  150   259        INIT_FCALL                                               'json_encode'
        260        SEND_VAR                                                 !2
        261        DO_ICALL                                         $160    
        262        ECHO                                                     $160
        263    > > JMP                                                      ->272
  153   264    >   ASSIGN_DIM                                               !2, 'error'
        265        OP_DATA                                                  <true>
  154   266        ASSIGN_DIM                                               !2, 'error_msg'
        267        OP_DATA                                                  'Required+parameter+%27tag%27+is+missing%21'
  155   268        INIT_FCALL                                               'json_encode'
        269        SEND_VAR                                                 !2
        270        DO_ICALL                                         $163    
        271        ECHO                                                     $163
  157   272    > > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.05 ms | 1420 KiB | 15 Q