Odyssey
appreport_gp_api.prg
1 <?php
2 /*******************************************************
3 *file: appreport_gp_server.api
4 * This file handles api requests to google storage, it goes
5 through a bucket found on the statistics page of the
6 play store and authennticates it with a google service account.
7 This account has its credentials encrpyted over docker/AWS
8 After confirmation it downloads the prompted data to present
9 in the appreport_gp_client.prg
10 ********************************************************/
11 $monLibrary= dirname(__FILE__) . "/../library";
12 $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
13 $datafolder = dirname(__FILE__) . "/appreportdata";
14 $tmpdir = "/home/homecu/tmp/appreportdata/";
15 $playStoreBucket = "pubsite_prod_rev_03651443995346945786";
16 
17 // Includes necessary files
18 require_once("$monLibrary/cu_top.i");
19 require_once("$monLibrary/ck_hticket.i");
20 require_once("$sharedLibrary/hcuCommon.i");
21 require_once("$sharedLibrary/hcuEnv.i");
22 require_once("$sharedLibrary/cu_flagconst.i");
23 require '/var/www/vendor/autoload.php';
24 use Google\Cloud\Storage\StorageClient;
25 
26 if (!CheckPerm($link, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
27  header("Location: /hcuadm/hcu_noperm.prg");
28  exit;
29 }
30 
31 function returnBucket(){
32  global $playStoreBucket;
33  //Grabs the Credentials Path for the Android/GP Service Account from hcuENV.i
34  $GPStatsCredentialsPath= GetGPStatsCredentialsPath();
35  //finds decrypted address of the credentials file from docker
36  $credentials_json = GetAwsCertFile($GPStatsCredentialsPath,
37  HOMECU_ENC_CERT_DIR,
38  HOMECU_DOCK_CERT_DIR);
39  if ($credentials_json == '') {
40  throw new Exception("[gpstats] Credentials could not be loaded!");
41  }
42  //loads storage client with correct credentials
43  $config = [
44  'keyFilePath' => $credentials_json,
45  'projectId' => 'homecu-gpstats',
46  ];
47  $storage = new StorageClient($config);
48  //populates the homecu android play statistics bucket
49  $bucket = $storage->bucket($playStoreBucket);
50  return $bucket;
51 }
52 
53 function downloadCu($cucode, $date, $report_type, $file_version){
54  global $tmpdir;
55  $bucket = returnBucket();
56  $return_array=array();
57  //make sure this key exists in array
58  $return_array['error_message']='';
59 
60  $object = $bucket->object('stats/' . $report_type .'/' . $report_type . '_com.homecu.' . $cucode . "_" . $date . $file_version);
61 
62  //check if object exists in google storage
63  if($object->exists()==false){
64  $return_array["success"] = FALSE;
65  $return_array["error_message"] = "CU not found in Google database";
66  return $return_array;
67  }
68 
69  //check if download directory exists if not create it
70  $localdir = $tmpdir . $report_type . $date ."/";
71  if (!is_dir($localdir)){
72  mkdir($localdir, 2770, true);
73  }
74  // download csv directly to file
75  $object->downloadToFile($localdir . $cucode . ".csv");
76  $return_array["success"] = TRUE;
77 
78  //paranoia check
79  clearstatcache();
80  if(filesize($localdir . $cucode . ".csv") <= 0 and !isset($return_array["message"])){
81  $return_array["success"] == FALSE;
82  $return_array["error_message"] = "CSV file not downloaded to file";
83  }
84 
85  return $return_array;
86 }