1 <?php
 2 
 3 /**
 4  * Cronjob Addon.
 5  *
 6  * @author gharlan[at]web[dot]de Gregor Harlan
 7  *
 8  * @package redaxo\cronjob
 9  */
10 
11 class rex_cronjob_urlrequest extends rex_cronjob
12 {
13     public function execute()
14     {
15         try {
16             $socket = rex_socket::factoryUrl($this->getParam('url'));
17             if ($this->getParam('http-auth') == '|1|') {
18                 $socket->addBasicAuthorization($this->getParam('user'), $this->getParam('password'));
19             }
20             if (($post = $this->getParam('post')) != '') {
21                 $response = $socket->doPost($post);
22             } else {
23                 $response = $socket->doGet();
24             }
25             $statusCode = $response->getStatusCode();
26             $success = $response->isSuccessful();
27             $message = $statusCode . ' ' . $response->getStatusMessage();
28             if (in_array($statusCode, [301, 302, 303, 307])
29                 && $this->getParam('redirect', true)
30                 && ($location = $response->getHeader('Location'))
31             ) {
32                 // maximal eine Umleitung zulassen
33                 $this->setParam('redirect', false);
34                 $this->setParam('url', $location);
35                 // rekursiv erneut ausfuehren
36                 $success = $this->execute();
37                 if ($this->hasMessage()) {
38                     $message .= ' -> ' . $this->getMessage();
39                 } else {
40                     $message .= ' -> Unknown error';
41                 }
42             }
43             $this->setMessage($message);
44             return $success;
45         } catch (rex_exception $e) {
46             $this->setMessage($e->getMessage());
47             return false;
48         }
49     }
50 
51     public function getTypeName()
52     {
53         return rex_i18n::msg('cronjob_type_urlrequest');
54     }
55 
56     public function getParamFields()
57     {
58         return [
59             [
60                 'label' => rex_i18n::msg('cronjob_type_urlrequest_url'),
61                 'name' => 'url',
62                 'type' => 'text',
63                 'default' => 'http://',
64             ],
65             [
66                 'label' => rex_i18n::msg('cronjob_type_urlrequest_post'),
67                 'name' => 'post',
68                 'type' => 'text',
69             ],
70             [
71                 'name' => 'http-auth',
72                 'type' => 'checkbox',
73                 'options' => [1 => rex_i18n::msg('cronjob_type_urlrequest_httpauth')],
74             ],
75             [
76                 'label' => rex_i18n::msg('cronjob_type_urlrequest_user'),
77                 'name' => 'user',
78                 'type' => 'text',
79                 'visible_if' => ['http-auth' => 1],
80             ],
81             [
82                 'label' => rex_i18n::msg('cronjob_type_urlrequest_password'),
83                 'name' => 'password',
84                 'type' => 'text',
85                 'visible_if' => ['http-auth' => 1],
86             ],
87         ];
88     }
89 }
90