Merge branch '2.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / package / PackageAction.class.php
CommitLineData
158bd3ca
TD
1<?php
2namespace wcf\data\package;
3use wcf\data\AbstractDatabaseObjectAction;
eb0f6246
AE
4use wcf\system\exception\SystemException;
5use wcf\system\request\LinkHandler;
317c8af5
AE
6use wcf\system\WCF;
7use wcf\util\HTTPRequest;
8use wcf\util\JSON;
158bd3ca
TD
9
10/**
11 * Executes package-related actions.
12 *
13 * @author Alexander Ebert
ca4ba303 14 * @copyright 2001-2014 WoltLab GmbH
158bd3ca
TD
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package com.woltlab.wcf
17 * @subpackage data.package
9f959ced 18 * @category Community Framework
158bd3ca
TD
19 */
20class PackageAction extends AbstractDatabaseObjectAction {
21 /**
0ad90fc3 22 * @see \wcf\data\AbstractDatabaseObjectAction::$className
158bd3ca
TD
23 */
24 protected $className = 'wcf\data\package\PackageEditor';
25
26 /**
0ad90fc3 27 * @see \wcf\data\AbstractDatabaseObjectAction::$permissionsCreate
158bd3ca
TD
28 */
29 protected $permissionsCreate = array('admin.system.package.canInstallPackage');
30
31 /**
0ad90fc3 32 * @see \wcf\data\AbstractDatabaseObjectAction::$permissionsDelete
158bd3ca
TD
33 */
34 protected $permissionsDelete = array('admin.system.package.canUninstallPackage');
35
36 /**
0ad90fc3 37 * @see \wcf\data\AbstractDatabaseObjectAction::$permissionsUpdate
158bd3ca
TD
38 */
39 protected $permissionsUpdate = array('admin.system.package.canUpdatePackage');
317c8af5
AE
40
41 public function validateSearchForPurchasedItems() {
42 // TODO: validate permissions
43
44 $this->readString('password', true);
45 $this->readString('username', true);
46
317c8af5
AE
47 if (empty($this->parameters['username'])) {
48 // check if user has already provided credentials
49 $sql = "SELECT loginUsername, loginPassword
50 FROM wcf".WCF_N."_package_update_server
51 WHERE serverURL = ?";
52 $statement = WCF::getDB()->prepareStatement($sql, 1);
53 $statement->execute(array('http://store.woltlab.com/typhoon/'));
54 $row = $statement->fetchArray();
55 if (!empty($row['loginUsername']) && !empty($row['loginPassword'])) {
56 $this->parameters['password'] = $row['loginPassword'];
57 $this->parameters['username'] = $row['loginUsername'];
58 }
59 }
60 }
61
62 public function searchForPurchasedItems() {
63 if (empty($this->parameters['username']) || empty($this->parameters['password'])) {
64 return array(
65 'template' => $this->renderAuthorizationDialog(false)
66 );
67 }
68
69 $request = new HTTPRequest('https://www.woltlab.com/api/1.0/customer/purchases/list.json', array(
70 'method' => 'POST'
71 ), array(
72 'username' => $this->parameters['username'],
73 'password' => $this->parameters['password'],
298a7cd8 74 'wcfVersion' => WCF_VERSION
317c8af5
AE
75 ));
76
77 $request->execute();
78 $reply = $request->getReply();
79 $response = JSON::decode($reply['body']);
80
81 $code = (isset($response['status'])) ? $response['status'] : 500;
82 switch ($code) {
83 case 200:
84 if (empty($response['products'])) {
85 return array(
86 'noResults' => WCF::getLanguage()->get('wcf.acp.pluginstore.purchasedItems.noResults')
87 );
88 }
89 else {
90 WCF::getSession()->register('__pluginStoreProducts', $response['products']);
298a7cd8 91 WCF::getSession()->register('__pluginStoreWcfMajorReleases', $response['wcfMajorReleases']);
317c8af5
AE
92
93 return array(
94 'redirectURL' => LinkHandler::getInstance()->getLink('PluginStorePurchasedItems')
95 );
96 }
97 break;
98
99 // authentication error
100 case 401:
101 return array(
102 'template' => $this->renderAuthorizationDialog(true)
103 );
104 break;
105
106 // any other kind of errors
107 default:
108 throw new SystemException(WCF::getLanguage()->getDynamicVariable('wcf.acp.pluginstore.api.error', array('status' => $code)));
109 break;
110 }
111 }
112
113 protected function renderAuthorizationDialog($rejected) {
114 WCF::getTPL()->assign(array(
115 'rejected' => $rejected
116 ));
117
118 return WCF::getTPL()->fetch('pluginStoreAuthorization');
119 }
dcb3a44c 120}