case CALL_OUTGOING: $price = round(ceil($options['calltime'] / $call_cost['unitSize']) * $call_cost['costPerUnit'], 5); break; } // insert cdr record to database $DB->Execute("INSERT INTO\n\t\t\t\t\t\t\t\t\t voip_cdr (caller, callee, call_start_time, time_start_to_end, time_answer_to_end, price, status, type, voipaccountid)\n\t\t\t\t\t\t\t\t VALUES\n\t\t\t\t\t\t\t\t\t (?, ?, ?, ?, ?, ?, ?, ?, ?);", array($options['caller'], $options['callee'], $options['startcall'], $options['totaltime'], $options['calltime'], $price, strtolower($options['status']), $call_type, $customer['voipaccountid'])); } else { $fh = isset($options['file']) ? fopen($options['file'], 'r') : fopen('php://stdin', 'r'); $customer_list = getCustomerList(); $error = array(); $i = 0; while ($f_line = fgets($fh)) { // increment file line counter ++$i; // change line to associative array $cdr = parseRow($f_line); // check values of cdr array $cdr_error = validCDR($cdr); if ($cdr_error === TRUE) { $tariff_id = $customer_list[$cdr['caller']]['tariffid']; //include customer tariff if (!isset($tariffs[$tariff_id])) { include_tariff($tariff_id); if (!isset($tariffs[$tariff_id])) { $error['errors'][] = array('line' => $i, 'line_content' => $f_line, 'error' => 'Can\'t find tariff ' . $tariff_id . ' in tariff files.'); continue; } } // set call type $call_type = parseCallType($cdr['call_type']); if ($call_type === NULL) {
// --------- // TRY TO JOIN PREVIOUS LINE if (substr_count($lines[0], ';') == 6 && substr_count($previous_line, ';') == 6) { array_unshift($lines, $previous_line); } else { $lines[0] = $previous_line . $lines[0]; } end($lines); $k = key($lines); $previous_line = $lines[$k]; unset($lines[$k]); // --------- // --------- // INSERT LOADED DATA TO DATABASE foreach ($lines as $k => $l) { $v = parseRow($l); if (!$v) { fwrite($stderr, 'error: can\'t parse row ' . $l . PHP_EOL); continue; } if (!preg_match('/^[0-9a-zA-Z \\/łŁ]*$/', $v['building_num'])) { fwrite($stderr, 'warning: house number contains incorrect characters in row ' . $l . PHP_EOL); continue; } if (strpos($v['symnad'], ' ') !== false) { fwrite($stderr, 'error: symnad contains whitespace characters in row ' . $l . PHP_EOL); continue; } $symnad = ltrim($v['symnad'], '0'); $simc = ltrim($v['simc'], '0'); $city = $location_cache->getCityByIdent($simc);
public function action_store() { $library_id = $this->auth(); $event_tables = array('answers', 'borrows', 'open_scores', 'permissions', 'supports', 'belongs', 'transactions'); $entity_tables = array('roots', 'branches', 'users', 'authors', 'publications', 'objects', 'matches', 'files'); function parseRow($data) { $content = strpos($data, '|'); $values = explode(',', substr($data, 0, $content - 1)); $values[] = substr($data, $content + 1); return $values; } function storeData($library_id, $command, $table, $values) { $event_tables = array('answers', 'borrows', 'open_scores', 'permissions', 'supports', 'belongs', 'transactions'); $entity_tables = array('roots', 'branches', 'users', 'authors', 'publications', 'objects', 'matches', 'files'); if (count($values) > 0 && (in_array($table, $event_tables) || in_array($table, $entity_tables))) { $query = ''; $values = join(',', $values); if ($command == 'insert') { $query = "insert ignore into {$table} values {$values}"; } else { if ($command == 'update') { $query = "replace into {$table} values {$values}"; } else { if ($command == 'delete') { $query = "delete from {$table} where id in ({$values})" . (in_array($table, $event_tables) ? " and library_id = {$library_id}" : ''); } else { returnData('Invalid Db Command'); } } } DB::query($query); } } // extract records $logs = explode('|-|', Input::get('xlogs')); // data validation if (count($logs) != Input::get('count')) { returnError(Input::get('count') . ' rows was sent but ' . count($logs) . ' was received'); } // write logs to file $logFile = fopen(path('storage') . 'files/' . $library_id . '.log', 'a'); // insert data into db in groups $command = ''; $table = ''; $values = array(); foreach ($logs as $row) { // store row before parsing it fwrite($logFile, $row . "\n"); // set new value $row = parseRow($row); if ($row[0] == 'library') { $data = explode(',', str_replace('null', '""', $row[5])); DB::query("update libraries set title = {$data[0]}, description = {$data[1]}, started_at = {$data[2]}, image = {$data[3]}, version = {$data[4]} where id = {$library_id}"); continue; } if ($row[1] == 'delete') { $value = $row[2]; } else { if (in_array($row[0], $event_tables)) { $value = "({$library_id},{$row[2]},{$row[5]})"; } else { $value = "({$row[2]},{$row[5]})"; } } // store values if ($table == $row[0] && $command == $row[1] && count($values) < 50) { $values[] = $value; } else { storeData($library_id, $command, $table, $values); $command = $row[1]; $table = $row[0]; $values = array($value); } } storeData($library_id, $command, $table, $values); // copy files into directory if (count($_FILES) > 0) { foreach ($_FILES as $file) { move_uploaded_file($file['tmp_name'], path('storage') . 'files/' . $file['name']); } } // update reghaabat synced_at $synced_at = Input::get('synced_at'); DB::query('update libraries set synced_at = ? where id = ?', array($synced_at, $library_id)); return returnData(array('synced_at' => $synced_at, 'count' => count($logs))); }
function loadFromFile($list_id) { $prefixList = array(); $error = array(); $lines = file($_FILES['file']['tmp_name']); if (empty($lines)) { return 1; } while (($line = next($lines)) !== false) { if (empty($line)) { continue; } $row = parseRow($line); $name = $row['name']; $prefix = $row['prefix']; $sell = $row['sell']; $result[$name][$sell][] = $prefix; // CHECK FOR DUPLICATE PREFIXES if (isset($prefixList[$prefix])) { $error['duplicate_item'][] = $prefix; } else { $prefixList[$prefix] = $name; } } if ($error) { return $error; } $DB = LMSDB::getInstance(); $groups = buildGroups($result); // ----------------------------------------------------- // GENERATE INSERT QUERY TO `voip_prefix_groups` TABLE // ----------------------------------------------------- $voip_prefix_group = 'INSERT INTO voip_prefix_groups (name, description) VALUES '; foreach ($groups as $groupName => $prefixArray) { $voip_prefix_group .= "('{$groupName}', ''),"; } $voip_prefix_group = rtrim($voip_prefix_group, ',') . ';'; $DB->execute($voip_prefix_group); // ----------------------------------------------------- // GENERATE INSERT QUERY TO `voip_prefix_groups` TABLE // ----------------------------------------------------- $groupHelperArray = $DB->GetAllByKey("SELECT id, name FROM voip_prefix_groups", "name"); $voip_prefix = 'INSERT INTO voip_prefixes (prefix, groupid) VALUES '; foreach ($groups as $groupName => $v) { foreach ($v as $prefixArray) { foreach ($prefixArray as $singlePrefix) { $voip_prefix .= "('{$singlePrefix}', " . $groupHelperArray[$groupName]['id'] . "),"; } } } $voip_prefix = rtrim($voip_prefix, ',') . ';'; $DB->execute($voip_prefix); // ----------------------------------------------------- // CREATE TARIFFS // ----------------------------------------------------- $voip_tariff = 'INSERT INTO voip_price_groups (prefix_group_id, voip_tariff_id, price, unitsize) VALUES '; foreach ($groups as $groupName => $prefixArray) { $price = key($prefixArray); $voip_tariff .= '(' . $groupHelperArray[$groupName]['id'] . ", {$list_id}, {$price}, 60),"; } $voip_tariff = rtrim($voip_tariff, ',') . ';'; $DB->execute($voip_tariff); return 0; }