Skip to content

Commit 11a7e0c

Browse files
committed
updated php namespaces and compatibility to pimcore 4
1 parent 4390cba commit 11a7e0c

File tree

4 files changed

+159
-163
lines changed

4 files changed

+159
-163
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
.idea

lib/DocumentAuthentication/FrontControllerPlugin.php

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,100 @@
11
<?php
22

3-
class DocumentAuthentication_FrontControllerPlugin extends Zend_Controller_Plugin_Abstract
4-
{
3+
namespace DocumentAuthentication;
54

6-
public function preDispatch(Zend_Controller_Request_Abstract $request)
5+
use Pimcore\Model\Document\Page;
6+
use Pimcore\Model\User;
7+
use Pimcore\Tool\Authentication;
8+
use Pimcore\Tool\Frontend;
9+
10+
class FrontControllerPlugin extends \Zend_Controller_Plugin_Abstract
711
{
8-
parent::preDispatch($request);
912

10-
if ($request->getParam("document") instanceof Document_Page) {
13+
public function preDispatch(\Zend_Controller_Request_Abstract $request)
14+
{
15+
parent::preDispatch($request);
16+
17+
if ($request->getParam("document") instanceof Page) {
18+
19+
$this->handleDocumentAuthentication($request->getParam("document"));
20+
}
1121

12-
$this->handleDocumentAuthentication($request->getParam("document"));
1322
}
1423

15-
}
24+
/**
25+
* @param Page $document
26+
*/
27+
private function handleDocumentAuthentication($document)
28+
{
29+
if (is_object($document)) {
1630

17-
/**
18-
* @param $document Pimcore_Document
19-
* @return void
20-
*/
21-
private function handleDocumentAuthentication($document)
22-
{
23-
if (is_object($document)) {
31+
if (!$document->getProperty(Plugin::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED)) {
32+
return; // all OK, show page
33+
}
34+
}
2435

25-
if (!$document->getProperty(DocumentAuthentication_Plugin::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED)) {
36+
$user = Authentication::authenticateSession();
37+
if ($user instanceof User) {
2638
return; // all OK, show page
2739
}
28-
}
2940

30-
$user = Pimcore_Tool_Authentication::authenticateSession();
31-
if ($user instanceof User) {
32-
return; // all OK, show page
33-
}
41+
if (self::isDocumentAuthenticationValid()) {
42+
return; // all OK, show page
43+
}
3444

35-
if (self::isDocumentAuthenticationValid()) {
36-
return; // all OK, show page
45+
$this->sendHttpBasicAuthResponse();
46+
exit;
3747
}
3848

39-
$this->sendHttpBasicAuthResponse();
40-
exit;
41-
}
49+
/**
50+
* @return bool
51+
*/
52+
private function isDocumentAuthenticationValid()
53+
{
4254

43-
/**
44-
* @return bool
45-
*/
46-
private function isDocumentAuthenticationValid()
47-
{
55+
$config = Frontend::getWebsiteConfig();
4856

49-
$config = Pimcore_Tool_Frontend::getWebsiteConfig();
57+
$username = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME, 'preview');
58+
$password = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, '');
5059

51-
$username = $config->get(DocumentAuthentication_Plugin::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME, 'preview');
52-
$password = $config->get(DocumentAuthentication_Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, '');
60+
if (trim($password) == '') {
61+
// empty password - this is not good; Deny access!
62+
return false;
63+
}
5364

54-
if (trim($password) == '') {
55-
// empty password - this is not good; Deny access!
56-
return false;
57-
}
65+
if (($_SERVER['PHP_AUTH_USER'] === $username) && ($_SERVER['PHP_AUTH_PW'] === $password)) {
66+
return true;
67+
}
5868

59-
if (($_SERVER['PHP_AUTH_USER'] === $username) && ($_SERVER['PHP_AUTH_PW'] === $password)) {
60-
return true;
69+
return false;
6170
}
6271

63-
return false;
64-
}
65-
66-
private function sendHttpBasicAuthResponse()
67-
{
68-
$config = Pimcore_Tool_Frontend::getWebsiteConfig();
69-
$password = $config->get(DocumentAuthentication_Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, null);
72+
private function sendHttpBasicAuthResponse()
73+
{
74+
$config = Frontend::getWebsiteConfig();
75+
$password = $config->get(Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD, null);
7076

71-
if (($password === null) || (trim($password) == '')) {
77+
if (($password === null) || (trim($password) == '')) {
7278

73-
$notice = 'Missing or empty Website Property '
74-
. DocumentAuthentication_Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD;
79+
$notice = 'Missing or empty Website Property '
80+
. Plugin::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD;
7581

76-
} else {
82+
} else {
7783

78-
$notice = 'Authentication required';
79-
}
84+
$notice = 'Authentication required';
85+
}
8086

81-
/** @var $response Zend_Controller_Response_Http */
82-
$response = $this->getResponse();
87+
/** @var $response \Zend_Controller_Response_Http */
88+
$response = $this->getResponse();
8389

84-
$response->setHeader('Cache-Control', 'max-age=0');
85-
$response->setHttpResponseCode(401);
86-
$response->setHeader(
87-
'WWW-Authenticate',
88-
'Basic realm="' . $notice . '"'
89-
);
90+
$response->setHeader('Cache-Control', 'max-age=0');
91+
$response->setHttpResponseCode(401);
92+
$response->setHeader(
93+
'WWW-Authenticate',
94+
'Basic realm="' . $notice . '"'
95+
);
9096

91-
$response->setBody('Unauthorized.');
92-
$response->sendResponse();
97+
$response->setBody('Unauthorized.');
98+
$response->sendResponse();
99+
}
93100
}
94-
}
Lines changed: 69 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,90 @@
11
<?php
22

3-
class DocumentAuthentication_Plugin
4-
extends Pimcore_API_Plugin_Abstract
5-
implements Pimcore_API_Plugin_Interface
6-
{
3+
namespace DocumentAuthentication;
74

8-
const DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED
9-
= 'documentAuthenticationEnabled';
5+
use Pimcore\API\Plugin as PluginApi;
6+
use Pimcore\Db;
7+
use Pimcore\Model\Property\Predefined as PropertyPredefined;
108

11-
const CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD
12-
= 'documentAuthenticationPassword';
9+
class Plugin extends PluginApi\AbstractPlugin implements PluginApi\PluginInterface
10+
{
1311

14-
const CONFIG_DOCUMENT_AUTHENTICATION_USERNAME
15-
= 'documentAuthenticationUser';
12+
const DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED
13+
= 'documentAuthenticationEnabled';
1614

17-
const DB_TABLE_WEBSITE_SETTINGS
18-
= 'website_settings';
15+
const CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD
16+
= 'documentAuthenticationPassword';
1917

20-
const DB_TABLE_PRE_PROPERTIES
21-
= 'properties_predefined';
18+
const CONFIG_DOCUMENT_AUTHENTICATION_USERNAME
19+
= 'documentAuthenticationUser';
2220

23-
public function init()
24-
{
25-
Pimcore::getEventManager()->attach("system.startup", function ($event) {
21+
const DB_TABLE_WEBSITE_SETTINGS
22+
= 'website_settings';
2623

27-
$front = Zend_Controller_Front::getInstance();
24+
public function init()
25+
{
26+
\Pimcore::getEventManager()->attach("system.startup", function ($event) {
2827

29-
$frontControllerPlugin = new DocumentAuthentication_FrontControllerPlugin();
30-
$front->registerPlugin($frontControllerPlugin);
31-
});
32-
}
28+
$front = \Zend_Controller_Front::getInstance();
3329

34-
public static function install()
35-
{
30+
$frontControllerPlugin = new FrontControllerPlugin();
31+
$front->registerPlugin($frontControllerPlugin);
32+
});
33+
}
3634

37-
$database = Pimcore_Resource_Mysql::getConnection();
38-
39-
if (!self::isInstalled()) {
40-
41-
$database->insert(self::DB_TABLE_PRE_PROPERTIES, array(
42-
'name' => self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED,
43-
'key' => self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED,
44-
'type' => 'bool',
45-
'inheritable' => 1,
46-
'ctype' => 'document'
47-
));
48-
49-
$database->insert(self::DB_TABLE_WEBSITE_SETTINGS, array(
50-
'name' => self::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME,
51-
'type' => 'text',
52-
'data' => 'preview'
53-
));
54-
55-
$database->insert(self::DB_TABLE_WEBSITE_SETTINGS, array(
56-
'name' => self::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD,
57-
'type' => 'text',
58-
'data' => md5(uniqid('', true))
59-
));
35+
public static function install()
36+
{
37+
$database = Db::get();
38+
39+
if (!self::isInstalled()) {
40+
41+
$prop = new PropertyPredefined();
42+
$prop->setName(self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED);
43+
$prop->setKey(self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED);
44+
$prop->setType('bool');
45+
$prop->setInheritable(1);
46+
$prop->setCtype('document');
47+
$prop->save();
48+
49+
$database->insert(self::DB_TABLE_WEBSITE_SETTINGS, array(
50+
'name' => self::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME,
51+
'type' => 'text',
52+
'data' => 'preview'
53+
));
54+
55+
$database->insert(self::DB_TABLE_WEBSITE_SETTINGS, array(
56+
'name' => self::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD,
57+
'type' => 'text',
58+
'data' => md5(uniqid('', true))
59+
));
60+
}
61+
62+
return 'Successfully installed plugin DocumentAuthentication.';
6063
}
6164

62-
return 'Successfully installed plugin DocumentAuthentication.';
63-
}
65+
public static function uninstall()
66+
{
67+
$database = Db::get();
6468

65-
public static function uninstall()
66-
{
67-
$database = Pimcore_Resource_Mysql::getConnection();
69+
$prop = PropertyPredefined::getByKey(self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED);
70+
$prop->delete();
6871

69-
$sqlQuery = "DELETE FROM " . self::DB_TABLE_PRE_PROPERTIES . " WHERE name = ?";
70-
$database->query($sqlQuery, array(self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED));
72+
$sqlQuery = "DELETE FROM " . self::DB_TABLE_WEBSITE_SETTINGS . " WHERE name = ?";
73+
$database->query($sqlQuery, array(self::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME));
74+
$database->query($sqlQuery, array(self::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD));
7175

72-
$sqlQuery = "DELETE FROM ".self::DB_TABLE_WEBSITE_SETTINGS." WHERE name = ?";
73-
$database->query($sqlQuery, array(self::CONFIG_DOCUMENT_AUTHENTICATION_USERNAME));
74-
$database->query($sqlQuery, array(self::CONFIG_DOCUMENT_AUTHENTICATION_PASSWORD));
76+
return 'Successfully removed plugin DocumentAuthentication.';
77+
}
7578

76-
return 'Successfully removed plugin DocumentAuthentication.';
77-
}
79+
public static function isInstalled()
80+
{
81+
return (PropertyPredefined::getByKey(self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED)
82+
!= null);
83+
}
7884

79-
public static function isInstalled()
80-
{
81-
$database = Pimcore_Resource_Mysql::getConnection();
82-
83-
$sqlQuery= "SELECT COUNT(id) as num FROM " . self::DB_TABLE_PRE_PROPERTIES . " WHERE name = ?";
84-
$isInstalled = (
85-
(int)$database->fetchOne(
86-
$sqlQuery,
87-
array(
88-
self::DOC_PROPERTY_DOCUMENT_AUTHENTICATION_ENABLED
89-
)
90-
) > 0);
91-
92-
return $isInstalled;
93-
}
85+
public static function needsReloadAfterInstall()
86+
{
87+
return false; // backend only functionality!
88+
}
9489

95-
public static function needsReloadAfterInstall()
96-
{
97-
return false; // backend only functionality!
9890
}
99-
100-
}

plugin.xml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
<?xml version="1.0"?>
22
<zend-config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
3-
<plugin>
3+
<plugin>
44

5-
<!-- the plugin name [A-Za-z_] -->
6-
<pluginName>DocumentAuthentication</pluginName>
5+
<!-- the plugin name [A-Za-z_] -->
6+
<pluginName>DocumentAuthentication</pluginName>
77

8-
<!-- a short description -->
9-
<pluginDescription>Document specific password protection via HTTP Basic Authentication </pluginDescription>
8+
<!-- a short description -->
9+
<pluginDescription>Document specific password protection via HTTP Basic Authentication </pluginDescription>
1010

11-
<!-- meta data -->
12-
<pluginVersion>1.0</pluginVersion>
13-
<pluginRevision>3</pluginRevision>
14-
<pluginBuildTimestamp>0</pluginBuildTimestamp>
11+
<!-- meta data -->
12+
<pluginVersion>1.0</pluginVersion>
13+
<pluginRevision>3</pluginRevision>
14+
<pluginBuildTimestamp>0</pluginBuildTimestamp>
1515

16-
<!-- className of the plugin which extends Pimcore_API_Plugin_Abstract-->
17-
<pluginClassName>DocumentAuthentication_Plugin</pluginClassName>
16+
<!-- className of the plugin which extends Pimcore_API_Plugin_Abstract-->
17+
<pluginClassName>DocumentAuthentication\Plugin</pluginClassName>
1818

19-
<!-- include paths relative to plugin-directory -->
20-
<pluginIncludePaths>
21-
<path>/DocumentAuthentication/lib</path>
22-
</pluginIncludePaths>
19+
<!-- include paths relative to plugin-directory -->
20+
<pluginIncludePaths>
21+
<path>/DocumentAuthentication/lib</path>
22+
</pluginIncludePaths>
2323

24-
<!-- namespaces to register with autoloader-->
25-
<pluginNamespaces>
26-
<namespace>DocumentAuthentication</namespace>
27-
</pluginNamespaces>
24+
<!-- namespaces to register with autoloader-->
25+
<pluginNamespaces>
26+
<namespace>DocumentAuthentication</namespace>
27+
</pluginNamespaces>
2828

29-
</plugin>
29+
</plugin>
3030
</zend-config>

0 commit comments

Comments
 (0)