ILIAS  trunk Revision v11.0_alpha-1769-g99a433fe2dc
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Crawler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\Data\URI;
24 
28 class Crawler
29 {
33  private const XPATH = '/wopi-discovery/net-zone/app';
34  private array $crawl_actions = [];
35  private ?string $content = null;
36  private ?\SimpleXMLElement $discovery = null;
40  private ?array $xml_app_elements = null;
41 
42  public function __construct()
43  {
44  $this->crawl_actions = [
45  ActionTarget::VIEW->value,
46  ActionTarget::EMBED_VIEW->value,
47  ActionTarget::EDIT->value,
48  ActionTarget::EMBED_EDIT->value,
49  ActionTarget::CONVERT->value,
50  ];
51  }
52 
53  public function validate(URI $discovery_url): bool
54  {
55  try {
56  $this->content = file_get_contents((string) $discovery_url) ?: null;
57  if ($this->content === null) {
58  return false;
59  }
60 
61  $this->discovery = simplexml_load_string($this->content) ?: null;
62  if ($this->discovery === null) {
63  return false;
64  }
65  $this->xml_app_elements = $this->discovery->xpath(self::XPATH);
66 
67  return is_array($this->xml_app_elements);
68  } catch (\Throwable) {
69  return false;
70  }
71  }
72 
73  public function crawl(URI $discovery_url): ?Apps
74  {
75  if (!$this->validate($discovery_url)) {
76  return null;
77  }
78 
79  // read wopi-discovery XML from $discovery_url and parse Apps with it's Actions
80  $apps = [];
81  foreach ($this->xml_app_elements as $app) {
82  $actions = [];
83  foreach ($app->action as $action) {
84  $action_name = $action['name'] ?? null;
85  $action_ext = $action['ext'] ?? null;
86  $action_urlsrc = $action['urlsrc'] ?? null;
87  $target_text = isset($action['targetext']) ? (string) $action['targetext'] : null;
88  if (!$action_name instanceof \SimpleXMLElement) {
89  continue;
90  }
91  if (!$action_ext instanceof \SimpleXMLElement) {
92  continue;
93  }
94  if (!$action_urlsrc instanceof \SimpleXMLElement) {
95  continue;
96  }
97 
98  if (!in_array((string) $action_name, $this->crawl_actions, true)) {
99  continue;
100  }
101 
102  $uri_string = rtrim((string) $action_urlsrc, '?');
103  // remove all after ?
104  $uri = explode('?', $uri_string);
105  $uri_string = $uri[0];
106  $actions[] = new Action(
107  0,
108  (string) $action_name,
109  (string) $action_ext,
110  new URI($uri_string),
111  $uri[1] ?? null,
112  $target_text
113  );
114  }
115  if ($actions === []) {
116  continue;
117  }
118 
119  $app_name = $app['name'] ?? null;
120  if ($app_name === null) {
121  continue;
122  }
123  $app_fav_icon_url = $app['favIconUrl'] ?? null;
124  $apps[] = new App(
125  0,
126  (string) $app_name,
127  $actions,
128  $app_fav_icon_url === null ? null : new URI((string) $app_fav_icon_url)
129  );
130  }
131  return new Apps($apps);
132  }
133 }
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null