|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace okapi\services\fieldnotes\upload; |
| 4 | + |
| 5 | +use okapi\core\Exception\InvalidParam; |
| 6 | +use okapi\core\Exception\ParamMissing; |
| 7 | +use okapi\core\Db; |
| 8 | +use okapi\core\Okapi; |
| 9 | +use okapi\core\OkapiServiceRunner; |
| 10 | +use okapi\core\Request\OkapiInternalRequest; |
| 11 | +use okapi\core\Request\OkapiRequest; |
| 12 | +use okapi\services\logs\LogsCommon; |
| 13 | +use okapi\Settings; |
| 14 | + |
| 15 | +class WebService |
| 16 | +{ |
| 17 | + public static function options() |
| 18 | + { |
| 19 | + return array( |
| 20 | + 'min_auth_level' => 3 |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + public static function call(OkapiRequest $request) |
| 25 | + { |
| 26 | + $result = array( |
| 27 | + 'success' => false // if the installation doesn't support it |
| 28 | + ); |
| 29 | + |
| 30 | + if (Settings::get('OC_BRANCH') == 'oc.de') |
| 31 | + { |
| 32 | + |
| 33 | + $field_notes = $request->get_parameter('field_notes'); |
| 34 | + if (!$field_notes) throw new ParamMissing('field_notes'); |
| 35 | + |
| 36 | + $notes = self::parse_notes($field_notes); |
| 37 | + if ($notes['success'] === false) throw new InvalidParam('field_notes', "Input data not recognized."); |
| 38 | + |
| 39 | + foreach ($notes['records'] as $n) |
| 40 | + { |
| 41 | + $geocache = OkapiServiceRunner::call( |
| 42 | + 'services/caches/geocache', |
| 43 | + new OkapiInternalRequest($request->consumer, $request->token, array( |
| 44 | + 'cache_code' => $n['code'], |
| 45 | + 'fields' => 'internal_id' |
| 46 | + )) |
| 47 | + ); |
| 48 | + $user_id = $request->token->user_id; |
| 49 | + $geocache_id = $geocache['internal_id']; |
| 50 | + $type = Okapi::logtypename2id($n['type']); |
| 51 | + $date = date("Y-m-d H:i:s", strtotime($n['date'])); |
| 52 | + $text = $n['log']; |
| 53 | + |
| 54 | + Db::query(" |
| 55 | + insert into field_note ( |
| 56 | + user_id, geocache_id, type, date, text |
| 57 | + ) values ( |
| 58 | + '".Db::escape_string($user_id)."', |
| 59 | + '".Db::escape_string($geocache_id)."', |
| 60 | + '".Db::escape_string($type)."', |
| 61 | + '".Db::escape_string($date)."', |
| 62 | + '".Db::escape_string($text)."' |
| 63 | + ) |
| 64 | + "); |
| 65 | + |
| 66 | + } |
| 67 | + $result = array( |
| 68 | + 'success' => true, |
| 69 | + 'totalRecords' => $notes['totalRecords'], |
| 70 | + 'processedRecords' => $notes['processedRecords'] |
| 71 | + ); |
| 72 | + //$result = json_encode($notes, JSON_PRETTY_PRINT); // debug |
| 73 | + } |
| 74 | + return Okapi::formatted_response($request, $result); |
| 75 | + } |
| 76 | + |
| 77 | + // ------------------------------------------------------------------ |
| 78 | + |
| 79 | + private static function parse_notes($field_notes) |
| 80 | + { |
| 81 | + $decoded_field_notes = base64_decode($field_notes, true); |
| 82 | + if ($decoded_field_notes === false) return false; |
| 83 | + |
| 84 | + $multiline = self::fieldNotesTxtArea2Array($decoded_field_notes); |
| 85 | + $submittable_logtype_names = Okapi::get_submittable_logtype_names(); |
| 86 | + $records = []; |
| 87 | + $totalRecords = 0; |
| 88 | + $processedRecords = 0; |
| 89 | + |
| 90 | + foreach ($multiline as $line) { |
| 91 | + $totalRecords++; |
| 92 | + $line = trim($line); |
| 93 | + $fields = self::CSVtoArray($line); |
| 94 | + |
| 95 | + $code = $fields[0]; |
| 96 | + $date = $fields[1]; |
| 97 | + $type = $fields[2]; |
| 98 | + |
| 99 | + if (!in_array($type, $submittable_logtype_names)) continue; |
| 100 | + |
| 101 | + $log = nl2br($fields[3]); |
| 102 | + |
| 103 | + $records[] = [ |
| 104 | + 'code' => $code, |
| 105 | + 'date' => $date, |
| 106 | + 'type' => $type, |
| 107 | + 'log' => $log, |
| 108 | + ]; |
| 109 | + $processedRecords++; |
| 110 | + } |
| 111 | + return ['success' => true, 'records' => $records, 'totalRecords' => $totalRecords, 'processedRecords' => $processedRecords]; |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + // ------------------------------------------------------------------ |
| 116 | + |
| 117 | + private static function fieldNotesTxtArea2Array($fieldnotes) |
| 118 | + { |
| 119 | + $output = []; |
| 120 | + $buffer = ''; |
| 121 | + $start = true; |
| 122 | + |
| 123 | + $lines = explode("\n", $fieldnotes); |
| 124 | + $lines = array_filter($lines); // Drop empty lines |
| 125 | + |
| 126 | + foreach ($lines as $line) { |
| 127 | + if ($start) { |
| 128 | + $buffer = $line; |
| 129 | + $start = false; |
| 130 | + } else { |
| 131 | + if (strpos($line, 'OC') !== 0) { |
| 132 | + $buffer .= "\n" . $line; |
| 133 | + } else { |
| 134 | + $output[] = trim($buffer); |
| 135 | + $buffer = $line; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + if (!$start) { |
| 141 | + $output[] = trim($buffer); |
| 142 | + } |
| 143 | + |
| 144 | + return $output; |
| 145 | + } |
| 146 | + |
| 147 | + // ------------------------------------------------------------------ |
| 148 | + |
| 149 | + private static function CSVtoArray($text) |
| 150 | + { |
| 151 | + $ret = ['']; |
| 152 | + $i = 0; |
| 153 | + $p = ''; |
| 154 | + $s = true; |
| 155 | + |
| 156 | + foreach (str_split($text) as $l) { |
| 157 | + if ('"' === $l) { |
| 158 | + $s = !$s; |
| 159 | + if ('"' === $p) { |
| 160 | + $ret[$i] .= '"'; |
| 161 | + $l = '-'; |
| 162 | + } elseif ('' === $p) { |
| 163 | + $l = '-'; |
| 164 | + } |
| 165 | + } elseif ($s && ',' === $l) { |
| 166 | + $l = $ret[++$i] = ''; |
| 167 | + } else { |
| 168 | + $ret[$i] .= $l; |
| 169 | + } |
| 170 | + $p = $l; |
| 171 | + } |
| 172 | + |
| 173 | + return $ret; |
| 174 | + } |
| 175 | +} |
0 commit comments