diff --git a/lib/Alchemy/Phrasea/Application/Overview.php b/lib/Alchemy/Phrasea/Application/Overview.php index 6e8bb4886d..4d4cee91e7 100644 --- a/lib/Alchemy/Phrasea/Application/Overview.php +++ b/lib/Alchemy/Phrasea/Application/Overview.php @@ -155,76 +155,100 @@ return call_user_func( return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + $deliverPermaview = function ($sbas_id, $record_id, $subdef, $key, $app) { + $databox = \databox::get_instance((int) $sbas_id); + + $record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef); + + if (!($record instanceof \record_adapter)) + throw new \Exception('bad luck'); + + /* @var $twig \Twig_Environment */ + $twig = $app['Core']->getTwig(); + + $params = array( + 'subdef_name' => $subdef + , 'module_name' => 'overview' + , 'module' => 'overview' + , 'view' => 'overview' + , 'record' => $record + ); + + return $twig->render('overview.twig', $params); + }; + + $app->get('/permalink/v1/{sbas_id}/{record_id}/{subdef}/', function (Request $request, $sbas_id, $record_id, $subdef) use ($app, $deliverPermaview) { + $key = $request->query->get('token'); + + return $deliverPermaview($sbas_id, $record_id, $subdef, $key, $app); + })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); + $app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/view/' - , function($label, $sbas_id, $record_id, $key, $subdef) use($app) { - - $databox = \databox::get_instance((int) $sbas_id); - - $record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef); - - if (!($record instanceof \record_adapter)) - throw new \Exception('bad luck'); - - /* @var $twig \Twig_Environment */ - $twig = $app['Core']->getTwig(); - - $params = array( - 'subdef_name' => $subdef - , 'module_name' => 'overview' - , 'module' => 'overview' - , 'view' => 'overview' - , 'record' => $record - ); - - return $twig->render('overview.twig', $params); + , function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $deliverPermaview) { + return $deliverPermaview($sbas_id, $record_id, $subdef, $key, $app); })->assert('sbas_id', '\d+')->assert('record_id', '\d+'); - $app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/' - , function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $session, $deliver_content) { - $databox = \databox::get_instance((int) $sbas_id); - $record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef); + $deliverPermalink = function ($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content) { - if (!($record instanceof \record_adapter)) - throw new \Exception('bad luck'); + $databox = \databox::get_instance((int) $sbas_id); + $record = \media_Permalink_Adapter::challenge_token($databox, $key, $record_id, $subdef); - $watermark = $stamp = false; + if (!($record instanceof \record_adapter)) + throw new \Exception('bad luck'); - if ($session->is_authenticated()) { - $user = \User_Adapter::getInstance($session->get_usr_id(), \appbox::get_instance($app['Core'])); + $watermark = $stamp = false; - $watermark = !$user->ACL()->has_right_on_base($record->get_base_id(), 'nowatermark'); + if ($session->is_authenticated()) { + $user = \User_Adapter::getInstance($session->get_usr_id(), \appbox::get_instance($app['Core'])); - if ($watermark) { + $watermark = !$user->ACL()->has_right_on_base($record->get_base_id(), 'nowatermark'); - $em = $app['Core']->getEntityManager(); + if ($watermark) { - $repository = $em->getRepository('\Entities\BasketElement'); + $em = $app['Core']->getEntityManager(); - if (count($repository->findReceivedValidationElementsByRecord($record, $user)) > 0) { - $watermark = false; - } elseif (count($repository->findReceivedElementsByRecord($record, $user)) > 0) { - $watermark = false; - } - } + $repository = $em->getRepository('\Entities\BasketElement'); - return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); - } else { - $collection = \collection::get_from_base_id($record->get_base_id()); - switch ($collection->get_pub_wm()) { - default: - case 'none': - $watermark = false; - break; - case 'stamp': - $stamp = true; - break; - case 'wm': - $watermark = false; - break; - } + if (count($repository->findReceivedValidationElementsByRecord($record, $user)) > 0) { + $watermark = false; + } elseif (count($repository->findReceivedElementsByRecord($record, $user)) > 0) { + $watermark = false; } + } - return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); + return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); + } else { + $collection = \collection::get_from_base_id($record->get_base_id()); + switch ($collection->get_pub_wm()) { + default: + case 'none': + $watermark = false; + break; + case 'stamp': + $stamp = true; + break; + case 'wm': + $watermark = false; + break; + } + } + + return $deliver_content($app['request'], $session, $record, $subdef, $watermark, $stamp, $app); + }; + + $app->get('/permalink/v1/{sbas_id}/{record_id}/{subdef}/{label}', + function (Request $request, $sbas_id, $record_id, $subdef, $label) use ($app, $deliverPermalink, $session, $deliver_content) { + + $key = $request->query->get('token'); + + return $deliverPermalink($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content); + }) + ->assert('sbas_id', '\d+') + ->assert('record_id', '\d+'); + + $app->get('/permalink/v1/{label}/{sbas_id}/{record_id}/{key}/{subdef}/' + , function($label, $sbas_id, $record_id, $key, $subdef) use ($app, $deliverPermalink, $session, $deliver_content) { + return $deliverPermalink($label, $sbas_id, $record_id, $key, $subdef, $app, $session, $deliver_content); } ) ->assert('sbas_id', '\d+')->assert('record_id', '\d+'); diff --git a/lib/classes/media/Permalink/Adapter.class.php b/lib/classes/media/Permalink/Adapter.class.php index 25484a86a5..37574e2444 100644 --- a/lib/classes/media/Permalink/Adapter.class.php +++ b/lib/classes/media/Permalink/Adapter.class.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Exception\RuntimeException; + /** * * @package subdefs @@ -144,13 +146,14 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea { $registry = registry::get_instance(); - return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/' - , $registry->get('GV_ServerName') - , $this->get_label() - , $this->media_subdef->get_sbas_id() - , $this->media_subdef->get_record_id() - , $this->get_token() - , $this->media_subdef->get_name() + return sprintf('%spermalink/v1/%d/%d/%s/%s.%s?token=%s', + $registry->get('GV_ServerName'), + $this->media_subdef->get_sbas_id(), + $this->media_subdef->get_record_id(), + $this->media_subdef->get_name(), + $this->get_label(), + pathinfo($this->media_subdef->get_record()->get_original_name(), PATHINFO_EXTENSION), + $this->get_token() ); } @@ -161,13 +164,12 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea */ public function get_page(registryInterface $registry) { - return sprintf('%spermalink/v1/%s/%d/%d/%s/%s/view/' - , $registry->get('GV_ServerName') - , $this->get_label() - , $this->media_subdef->get_sbas_id() - , $this->media_subdef->get_record_id() - , $this->get_token() - , $this->media_subdef->get_name() + return sprintf('%spermalink/v1/%d/%d/%s/?token=%s', + $registry->get('GV_ServerName'), + $this->media_subdef->get_sbas_id(), + $this->media_subdef->get_record_id(), + $this->media_subdef->get_name(), + $this->get_token() ); } @@ -226,7 +228,7 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea { $unicode_processor = new unicode(); - $label = trim($label); + $label = trim($label) ? trim($label) : 'untitled'; while (strpos($label, ' ') !== false) $label = str_replace(' ', ' ', $label); @@ -333,13 +335,21 @@ class media_Permalink_Adapter implements media_Permalink_Interface, cache_cachea , ':activated' => '1' ); + $error = null; $stmt = $databox->get_connection()->prepare($sql); - $stmt->execute($params); + try { + $stmt->execute($params); + } catch (\PDOException $e) { + $error = $e; + } $stmt->closeCursor(); - unset($stmt); + + if ($error) { + throw new RuntimeException('Permalink already exists', $e->getCode(), $e); + } $permalink = self::getPermalink($databox, $media_subdef); - $permalink->set_label(strip_tags($media_subdef->get_record()->get_title())); + $permalink->set_label(strip_tags($media_subdef->get_record()->get_title(false, null, true))); return $permalink; } diff --git a/lib/classes/patch/379.class.php b/lib/classes/patch/379.class.php new file mode 100644 index 0000000000..13c40f075a --- /dev/null +++ b/lib/classes/patch/379.class.php @@ -0,0 +1,62 @@ +release; + } + + public function require_all_upgrades() + { + return false; + } + + /** + * + * @return Array + */ + public function concern() + { + return $this->concern; + } + + /** + * @param base $databox + */ + public function apply(base &$appbox) + { + $sql = 'UPDATE permalinks SET label = "untitled" WHERE label = ""'; + $stmt = $appbox->get_connection()->prepare($sql); + $stmt->execute(); + $stmt->closeCursor(); + + return true; + } +} + diff --git a/tests/Alchemy/Phrasea/Application/OverviewTest.php b/tests/Alchemy/Phrasea/Application/OverviewTest.php index fef123922c..655f0d50cf 100644 --- a/tests/Alchemy/Phrasea/Application/OverviewTest.php +++ b/tests/Alchemy/Phrasea/Application/OverviewTest.php @@ -96,6 +96,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract { $appbox = appbox::get_instance(\bootstrap::getCore()); $this->assertTrue($appbox->get_session()->is_authenticated()); + $this->get_a_permalinkBCcompatibility(); $this->get_a_permalink(); } @@ -104,6 +105,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract $appbox = appbox::get_instance(\bootstrap::getCore()); $appbox->get_session()->logout(); $this->assertFalse($appbox->get_session()->is_authenticated()); + $this->get_a_permalinkBCcompatibility(); $this->get_a_permalink(); } @@ -129,8 +131,7 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract $this->assertEquals(200, $response->getStatusCode()); } - - protected function get_a_permalink() + protected function get_a_permalinkBCcompatibility() { $token = static::$records['record_1']->get_preview()->get_permalink()->get_token(); $url = '/permalink/v1/whateverIwannt/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/' . $token . '/preview/'; @@ -145,4 +146,21 @@ class ApplicationOverviewTest extends PhraseanetWebTestCaseAuthenticatedAbstract $response = $this->client->getResponse(); $this->assertEquals(200, $response->getStatusCode()); } + + protected function get_a_permalink() + { + $token = static::$records['record_1']->get_preview()->get_permalink()->get_token(); + $url = '/permalink/v1/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/preview/whateverIwannt.jpg?token=' . $token . ''; + + $crawler = $this->client->request('GET', $url); + $response = $this->client->getResponse(); + + $this->assertEquals(200, $response->getStatusCode()); + + $url = '/permalink/v1/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/preview/?token=' . $token . ''; + $crawler = $this->client->request('GET', $url); + $response = $this->client->getResponse(); + $this->assertEquals(200, $response->getStatusCode()); + $this->assertEquals('text/html; charset=UTF-8', $response->headers->get('Content-Type')); + } } diff --git a/tests/Alchemy/Phrasea/Controller/Root/RSSFeedTest.php b/tests/Alchemy/Phrasea/Controller/Root/RSSFeedTest.php index a06ba020c0..b8f556fb5f 100644 --- a/tests/Alchemy/Phrasea/Controller/Root/RSSFeedTest.php +++ b/tests/Alchemy/Phrasea/Controller/Root/RSSFeedTest.php @@ -174,10 +174,10 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract public function createApplication() { $app = require __DIR__ . '/../../../../../lib/Alchemy/Phrasea/Application/Root.php'; - + $app['debug'] = true; unset($app['exception_handler']); - + return $app; } @@ -347,7 +347,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract { $this->client->request("GET", "/feeds/feed/0/rss/"); } - + /** * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException */ @@ -550,7 +550,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract $is_thumbnail = false; $record = $entry_item->get_record(); - if (substr($current_attributes["url"], 0 - strlen("/preview/")) == "/preview/") { + if (false !== strpos($current_attributes["url"], 'preview')) { $ressource = $record->get_subdef('preview'); } else { $ressource = $record->get_thumbnail(); @@ -679,7 +679,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract foreach ($fields as $key_field => $field) { $role = true; - + if(isset($field["media_field"]['attributes']['role'])){ $role = false; foreach($node->attributes as $attr){ @@ -689,7 +689,7 @@ class ControllerRssFeedTest extends \PhraseanetWebTestCaseAbstract } } } - + if ($field["media_field"]["name"] == $node->nodeName && $role != false) { if ($p4field = $entry_item->get_record()->get_caption()->get_dc_field($field["dc_field"])) { diff --git a/tests/media/Permalink/media_Permalink_AdapterTest.php b/tests/media/Permalink/media_Permalink_AdapterTest.php index c506a3ebfa..4483859323 100644 --- a/tests/media/Permalink/media_Permalink_AdapterTest.php +++ b/tests/media/Permalink/media_Permalink_AdapterTest.php @@ -16,6 +16,12 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract static::$object = media_Permalink_Adapter::getPermalink($databox, static::$records['record_1']->get_subdef('document')); } + public function testGet_label() + { + $this->assertInternalType('string', static::$object->get_label()); + $this->assertEquals('test001', static::$object->get_label()); + } + public function testGetPermalink() { $this->assertTrue((static::$object instanceof media_Permalink_Adapter)); @@ -36,7 +42,7 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract static::$object->set_label('coucou les chicos'); $this->assertEquals('coucou-les-chicos', static::$object->get_label()); static::$object->set_label(''); - $this->assertEquals('', static::$object->get_label()); + $this->assertEquals('untitled', static::$object->get_label()); static::$object->set_label('JE ANp ra&é"\/,;:!§/.?%µ*ù$]@^\[{#~234567890°+\'(-è_çà'); $this->assertEquals('JE-ANp-raeu234567890-e_ca', static::$object->get_label()); } @@ -44,98 +50,59 @@ class media_Permalink_AdapterTest extends PhraseanetPHPUnitAbstract public function testGet_url() { $registry = registry::get_instance(); - $url = $registry->get('GV_ServerName') . 'permalink/v1/' . static::$object->get_label() . '/' . static::$records['record_1']->get_sbas_id() . '/' . static::$records['record_1']->get_record_id() . '/' . - static::$object->get_token() . '/document/'; + $url = $registry->get('GV_ServerName') . 'permalink/v1/' + . static::$records['record_1']->get_sbas_id() . '/' + . static::$records['record_1']->get_record_id() + . '/document/' . static::$object->get_label() + . '.' . pathinfo(static::$records['record_1']->get_original_name(), PATHINFO_EXTENSION) + . '?token=' . static::$object->get_token(); $this->assertEquals($url, static::$object->get_url($registry)); } - /** - * @todo Implement testGet_page(). - */ public function testGet_page() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $registry = registry::get_instance(); + $url = $registry->get('GV_ServerName') . 'permalink/v1/' + . static::$records['record_1']->get_sbas_id() . '/' + . static::$records['record_1']->get_record_id() + . '/document/' + . '?token=' . static::$object->get_token(); + + $this->assertEquals($url, static::$object->get_page($registry)); } - /** - * @todo Implement testGet_id(). - */ public function testGet_id() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInternalType('integer', static::$object->get_id()); } - /** - * @todo Implement testGet_token(). - */ public function testGet_token() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInternalType('string', static::$object->get_token()); } - /** - * @todo Implement testGet_is_activated(). - */ public function testGet_is_activated() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - $this->assertTrue(is_bool(static::$object->get_is_activated)); + $this->assertInternalType('boolean', static::$object->get_is_activated()); } - /** - * @todo Implement testGet_created_on(). - */ public function testGet_created_on() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInstanceOf('DateTime', static::$object->get_created_on()); } - /** - * @todo Implement testGet_last_modified(). - */ public function testGet_last_modified() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $this->assertInstanceOf('DateTime', static::$object->get_last_modified()); } /** - * @todo Implement testGet_label(). + * @expectedException Alchemy\Phrasea\Exception\RuntimeException */ - public function testGet_label() + public function testCreateAPermalinkAlreadyCreated() { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); - } - - /** - * @todo Implement testCreate(). - */ - public function testCreate() - { - // Remove the following lines when you implement this test. - $this->markTestIncomplete( - 'This test has not been implemented yet.' - ); + $databox = static::$records['record_1']->get_databox(); + media_Permalink_Adapter::create($databox, static::$records['record_1']->get_subdef('document')); } }