/** * Import routes from a specified Asset ID * * @throws \Exception */ public function routes() { $message = null; try { $id = $this->inputfilter->clean($this->app->get('PARAMS.id'), 'alnum'); $item = (new \Dsc\Mongo\Collections\Assets())->setState('filter.id', $id)->getItem(); if (empty($item->id)) { throw new \Exception('Invalid Item'); } $file = new \SplTempFileObject(); // TODO Push this to the Assets model switch ($item->storage) { case "s3": $contents = @file_get_contents($item->url); break; case "gridfs": default: $length = $item->length; $chunkSize = $item->chunkSize; $chunks = ceil($length / $chunkSize); $collChunkName = $item->collectionNameGridFS() . ".chunks"; $collChunks = $item->getDb()->{$collChunkName}; $contents = null; for ($i = 0; $i < $chunks; $i++) { $chunk = $collChunks->findOne(array("files_id" => $item->_id, "n" => $i)); $contents .= $chunk["data"]->bin; } break; } $file->fwrite($contents); $reader = new \Ddeboer\DataImport\Reader\CsvReader($file, ","); $reader->setHeaderRowNumber(0); $this->app->set('item', $item); $this->app->set('count', count($reader)); $skipped = 0; $inserted = 0; $updated = 0; $failed = 0; $errors = array(); foreach ($reader as $row) { set_time_limit(0); if (empty($row['Original'])) { $skipped++; continue; } if (strpos($row['Original'], '/') === 0) { $row['Original'] = substr($row['Original'], 1); } if (empty($row['Original'])) { $skipped++; continue; } $redirect = \Redirect\Admin\Models\Routes::findOne(array('url.alias' => $row['Original'])); if (empty($redirect->id)) { // insert $redirect = new \Redirect\Admin\Models\Routes(); $redirect->{'url.alias'} = $row['Original']; $redirect->{'url.redirect'} = $row['Target']; try { $redirect->save(); $inserted++; } catch (\Exception $e) { $failed++; $errors[] = $e->getMessage(); } } else { // update $redirect->{'url.alias'} = $row['Original']; $redirect->{'url.redirect'} = $row['Target']; try { $redirect->save(); $updated++; } catch (\Exception $e) { $failed++; $errors[] = $e->getMessage(); } } } $this->app->set('skipped', $skipped); $this->app->set('inserted', $inserted); $this->app->set('updated', $updated); $this->app->set('failed', $failed); $this->app->set('errors', $errors); $this->app->set('message', $message); echo $this->theme->render('Redirect/Admin/Views::importer/routes_results.php'); } catch (\Exception $e) { \Dsc\System::addMessage($e->getMessage(), 'error'); $this->app->reroute('/admin/redirect/import'); return; } }
public static function handleError() { $response = new \stdClass(); $response->action = null; $response->redirect_route = null; $response->callable_handle = null; $response->html = null; $f3 = \Base::instance(); if ($f3->get('ERROR.code') == '404') { $model = new \Redirect\Admin\Models\Routes(); // lets find a proper redirection, if exists $path = substr($model->inputFilter()->clean($f3->hive()['PATH'], 'string'), 1); $redirect = null; $route = $model->setState('filter.url.alias', $path)->getItem(); if (!empty($route->id)) { // count the number of times a redirect is hit and track the date of the last hit $route->hits = (int) $route->hits + 1; $route->last_hit = \Dsc\Mongo\Metastamp::getDate('now'); $route->store(); $redirect = trim($route->{'url.redirect'}); } else { // TODO add a config option to disable this $new_route = new \Redirect\Admin\Models\Routes(); $new_route->set('url.alias', $path); $new_route->hits = 1; $new_route->last_hit = \Dsc\Mongo\Metastamp::getDate('now'); try { $new_route->save(); } catch (\Exception $e) { // couldn't save for some reason // add it to Logger? } } // don't redirect if the path and redirect are the same to prevent recursion if (!empty($redirect) && $path != $redirect) { // if the string doesn't begin with /, make sure it does if (strpos($redirect, '/') !== 0) { $redirect = '/' . $redirect; } $response->action = 'redirect'; $response->redirect_route = $redirect; } else { // use default error route specified by user in Redirect::Settings $settings = \Redirect\Admin\Models\Settings::fetch(); $default_redirect = $settings->{'general.default_error_404'}; if (!empty($default_redirect) && $path != $default_redirect) { // if the string doesn't begin with /, make sure it does if (strpos($default_redirect, '/') !== 0) { $default_redirect = '/' . $default_redirect; } $response->action = 'redirect'; $response->redirect_route = $default_redirect; } else { // let f3 default error handler handle it since the user didn't specify a default 404 redirect $response->action = false; } } } else { // let f3 default error handler handle all other error types $response->action = false; } // clear any system error messages if the response is redirect // so that the user doesn't get displayed "invalid item" notices if ($response->action == 'redirect') { $messages = \Dsc\System::instance()->getMessages(); } return $response; }