Odyssey
appreport_gp_server.prg
1 <?php
2 /*******************************************************
3  *file: appreport_gp_server.prg
4  *notes: This file is used to call the google play api
5  depending on the correct CU and Month given by
6  appreport_gp_client.prg.
7  ********************************************************/
8 
9 $monLibrary = dirname(__FILE__) . "/../library";
10 $sharedLibrary = dirname(__FILE__) . "/../../shared/library";
11 $tmpdir = "/home/homecu/tmp/appreportdata/";
12 $datadir = dirname(__FILE__) . "/appreportdata/";
13 
14 // Includes necessary files
15 require_once("$monLibrary/cu_top.i");
16 require_once("$monLibrary/ck_hticket.i");
17 require_once("$sharedLibrary/hcuCommon.i");
18 require_once("$sharedLibrary/cu_flagconst.i");
19 require_once(dirname(__FILE__) . "/appreport_gp_api.prg");
20 
21 if (!CheckPerm($link, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
22  header("Location: /hcuadm/hcu_noperm.prg");
23  exit;
24 }
25 /**
26  * Function that listens for ajax post call and handles which functions to pass the data off to
27  *
28  * @return json response
29  */
30 function ajaxListener()
31 {
32  $aResult = array();
33  if (isset($_POST['functionname'])) {
34  if ($_POST['functionname'] == 'downloadInstalls') {
35  if (isset($_POST['submonth']) and isset($_POST['cucode'])) {
36  $submonth = $_POST['submonth'];
37  $cucode = $_POST['cucode'];
38  $date = calculateMonth($submonth);
39 
40  $aResult = downloadInstalls($date, $cucode);
41  } else {
42  throw new exception("arguments not set in download installs", 1);
43  }
44  }
45  elseif ($_POST['functionname'] == 'downloadCrashes') {
46  if (isset($_POST['submonth']) and isset($_POST['cucode'])) {
47  $submonth = $_POST['submonth'];
48  $cucode = $_POST['cucode'];
49  $date = calculateMonth($submonth);
50 
51  $aResult = downloadCrashes($date, $cucode);
52  } else {
53  throw new exception("arguments not set in download Crashes", 1);
54  }
55  }
56  elseif ($_POST['functionname'] == 'downloadRatings') {
57  if (isset($_POST['submonth']) and isset($_POST['cucode'])) {
58  $submonth = $_POST['submonth'];
59  $cucode = $_POST['cucode'];
60  $date = calculateMonth($submonth);
61 
62  $aResult = downloadRatings($date, $cucode);
63  } else {
64  throw new exception("arguments not set in download Ratings", 1);
65  }
66  }
67  elseif ($_POST['functionname'] == 'view') {
68  if (isset($_POST['report_type']) and isset($_POST['date']) and isset($_POST['cucode'])) {
69  $cucode = $_POST['cucode'];
70  $report_type = $_POST['report_type'];
71  $date = $_POST['date'];
72 
73  $aResult = view($cucode, $report_type, $date);
74  } else {
75  throw new exception("arguments not set in view", 1);
76  }
77  }
78  } else {
79  throw new exception("function name not found", 1);
80  }
81  return $aResult;
82 }
83 /**
84  * Function that listens for ajax post call and handles which functions to pass the data off to
85  *
86  * @param $cucode
87  * @param $date
88  * @return json response
89  */
90 function downloadInstalls($date, $cucode)
91 {
92  global $tmpdir;
93  //initialize variables
94  $report_type = "installs";
95  $file_version = "_overview.csv";
96  // from appreport_gp_api returns array with success bool
97  $data_array = downloadCu($cucode, $date, $report_type, $file_version);
98  if ($data_array["success"]) {
99  //from appreport_gp_utils returns csv content
100  $data_array["content"] = getInfo($tmpdir . $report_type . $date, $cucode);
101  }
102  return $data_array;
103 }
104 
105 /**
106  * Function that listens for ajax post call and handles which functions to pass the data off to
107  *
108  * @param $cucode
109  * @param $date
110  * @return json response
111  */
112 function downloadCrashes($date, $cucode)
113 {
114  global $tmpdir;
115  //initialize variables
116  $report_type = "crashes";
117  $file_version = "_app_version.csv";
118  // from appreport_gp_api returns array with success bool
119  $data_array = downloadCu($cucode, $date, $report_type, $file_version);
120  if ($data_array["success"]) {
121  //from appreport_gp_utils returns csv content
122  $data_array["content"] = getInfo($tmpdir . $report_type . $date, $cucode);
123  }
124  return $data_array;
125 }
126 
127 /**
128  * Function that listens for ajax post call and handles which functions to pass the data off to
129  *
130  * @param $cucode
131  * @param $date
132  * @return json response
133  */
134 function downloadRatings($date, $cucode)
135 {
136  global $tmpdir;
137  //initialize variables
138  $report_type = "ratings";
139  $file_version = "_app_version.csv";
140  // from appreport_gp_api returns array with success bool
141  $data_array = downloadCu($cucode, $date, $report_type, $file_version);
142  if ($data_array["success"]) {
143  //from appreport_gp_utils returns csv content
144  $data_array["content"] = getInfo($tmpdir . $report_type . $date, $cucode);
145  }
146  return $data_array;
147 }
148 
149 /**
150  * grabs local data when details button is press in the crashes and ratings table
151  *
152  * @return json response
153  */
154 function view($cucode, $report_type, $date)
155 {
156  global $tmpdir;
157  $data_array["error_message"] = '';
158 
159  $content = getInfo($tmpdir . $report_type . $date, $cucode);
160  if ($content == "no file") {
161  $data_array["error_message"] = 'file not found';
162  }
163  $data_array["content"] = $content;
164  return $data_array;
165 }
166 
167 function calculateMonth($submonth){
168  $unformatted_date = strtotime($submonth . " Months");
169  return date("Ym", $unformatted_date);
170 }
171 
172 /****************************************************************
173 *Open and parse single csv file from local storage for /view route and see specific details in view textbox.
174 *Params:
175 * $targetdir is the location of downloaded csv file
176 * $cucode is identifier for cu
177 *Returns:
178 * csv content
179 ****************************************************************/
180 function getInfo($targetdir, $cucode){
181  $content = '';
182  $handle = fopen ($targetdir . "/" . $cucode . ".csv","r");
183  if($handle == false){return "false";}
184  while ($data = fgetcsv ($handle, 1000, ";")) {
185  $num = count ($data);
186  for ($c=0; $c < $num; $c++) {
187  // output data
188  $content .= mb_convert_encoding($data[$c], "UTF-8", "UTF-16") . "\n";
189  }
190  }
191  return $content;
192 }
193 
194 echo HCU_JsonEncode(ajaxListener());