Passed
Branch master (e0a0b4)
by Sam
01:19 queued 16s
created

admin.php (5 issues)

1
<?php
2
3
// Make sure we're already in Piwigo.
4
defined('PHPWG_ROOT_PATH') or exit(1);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
5
6
require_once __DIR__.'/vendor/autoload.php';
7
8
use Mediawiki\Api\ApiUser;
9
use Mediawiki\Api\FluentRequest;
10
use Mediawiki\Api\MediawikiApi;
11
use Mediawiki\Api\UsageException;
12
13
// Prepare the template.
14
$url = isset($_REQUEST['url']) ? $_REQUEST['url'] : '';
15
$p2mConf = isset($conf[PIWIGO2MEDIAWIKI_ID])
16
? $conf[PIWIGO2MEDIAWIKI_ID]
17
: array();
18
$template->assign(array(
19
  'admin_url' => PIWIGO2MEDIAWIKI_ADMIN,
20
  'piwigo2mediawiki_page' => PIWIGO2MEDIAWIKI_PAGE,
21
  'mediawiki_url' => $url,
22
  'p2m_conf' => $p2mConf,
23
));
24
25
// Delete if requested.
26
if (isset($_POST['action']) && $_POST['action']==='delete'
27
  && isset($_POST['id']) && isset($p2mConf[$_POST['id']])
28
) {
29
  unset($p2mConf[$_POST['id']]);
30
  conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf);
0 ignored issues
show
The function conf_update_param was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
  /** @scrutinizer ignore-call */ 
31
  conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf);
Loading history...
31
  redirect(PIWIGO2MEDIAWIKI_ADMIN);
0 ignored issues
show
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
  /** @scrutinizer ignore-call */ 
32
  redirect(PIWIGO2MEDIAWIKI_ADMIN);
Loading history...
32
}
33
34
// Load one wiki's data for editing if an ID is specified.
35
if (isset($_REQUEST['id']) && isset($p2mConf[$_REQUEST['id']])) {
36
  $info = $p2mConf[$_REQUEST['id']];
37
  $info['id'] = $_REQUEST['id'];
38
  $template->assign(array(
39
    'info' => $info,
40
  ));
41
}
42
43
// Save (create or edit) a single wiki's data.
44
if ($url && isset($_POST['action']) && $_POST['action'] === 'add'
45
  && isset($_POST['username']) && $_POST['username']
46
  && isset($_POST['password']) && $_POST['password']
47
) {
48
  // Find the API URL or show an error.
49
  $validUrl = true;
50
  try {
51
    $api = MediawikiApi::newFromPage($url);
52
    $url = $api->getApiUrl();
53
  } catch (Exception $exception) {
54
    $msg = l10n(
0 ignored issues
show
The function l10n was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
    $msg = /** @scrutinizer ignore-call */ l10n(
Loading history...
55
      'MediaWiki API discovery failed for: %s<br />Error: %s',
56
      $url,
57
      $exception->getMessage()
58
    );
59
    $template->assign(array(
60
      'warnings' => $msg,
61
      'info' => $_POST,
62
    ));
63
    $validUrl = false;
64
  }
65
  // If we've got a valid API URL, find the site name and save all data.
66
  if ($validUrl) {
67
68
    $username = $_POST['username'];
69
    $password = $_POST['password'];
70
71
    $loggedIn = false;
72
    try
73
    {
74
      $loggedIn = $api->login(new ApiUser($username, $password));
75
    } catch (UsageException $e) {
76
      $msg = l10n('Authentication failed.<br />Error: %s', $e->getMessage());
77
      $template->assign(array(
78
        'warnings' => $msg,
79
        'info' => $_POST,
80
      ));
81
    }
82
83
    // If logging in worked, get some more information and save the config.
84
    if ($loggedIn)
0 ignored issues
show
The condition $loggedIn is always true.
Loading history...
85
    {
86
      $siteInfo = $api->getRequest(
87
        FluentRequest::factory()
88
          ->setAction('query')
89
          ->setParam('meta', 'siteinfo')
90
      );
91
      $p2mConf[$info['id']] = array(
92
        'sitename' => $siteInfo['query']['general']['sitename'],
93
        'url' => $url,
94
        'username' => $username,
95
        'password' => $password,
96
        'wikitext' => isset($_POST['wikitext']) ? $_POST['wikitext'] : '',
97
      );
98
      conf_update_param(PIWIGO2MEDIAWIKI_ID, $p2mConf);
99
      redirect(PIWIGO2MEDIAWIKI_ADMIN);
100
    }
101
  }
102
103
}
104
105
$template_handle = PIWIGO2MEDIAWIKI_ID.'admin';
106
$template_file = PIWIGO2MEDIAWIKI_DIR.'admin.tpl';
107
$template->set_filename($template_handle, $template_file);
108
$template->assign_var_from_handle('ADMIN_CONTENT', $template_handle);
109