1<?php
2/**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9namespace Piwik\Plugins\Referrers;
10
11use Piwik\Common;
12use Piwik\Piwik;
13use Piwik\UrlHelper;
14
15/**
16 * Returns path component from a URL
17 *
18 * @param string $url
19 * @return string path
20 */
21function getPathFromUrl($url)
22{
23    $path = UrlHelper::getPathAndQueryFromUrl($url);
24    if (empty($path)) {
25        return 'index';
26    }
27    return $path;
28}
29
30/**
31 * Return translated referrer type
32 *
33 * @param string $label
34 * @return string Referrer type
35 */
36function getReferrerTypeLabel($label)
37{
38    switch ($label) {
39        case 'direct':
40        case Common::REFERRER_TYPE_DIRECT_ENTRY:
41            $indexTranslation = 'Referrers_DirectEntry';
42            break;
43        case 'search':
44        case Common::REFERRER_TYPE_SEARCH_ENGINE:
45            $indexTranslation = 'Referrers_SearchEngines';
46            break;
47        case 'social':
48        case Common::REFERRER_TYPE_SOCIAL_NETWORK:
49            $indexTranslation = 'Referrers_Socials';
50            break;
51        case 'website':
52        case Common::REFERRER_TYPE_WEBSITE:
53            $indexTranslation = 'Referrers_Websites';
54            break;
55        case 'campaign':
56        case Common::REFERRER_TYPE_CAMPAIGN:
57            $indexTranslation = 'Referrers_Campaigns';
58            break;
59        default:
60            // case of newsletter, partners, before Piwik 0.2.25
61            $indexTranslation = 'General_Others';
62            break;
63    }
64    return Piwik::translate($indexTranslation);
65}
66
67/**
68 * Works in both directions
69 * @param string $name
70 * @throws \Exception
71 * @return string
72 */
73function getReferrerTypeFromShortName($name)
74{
75    $map = array(
76        Common::REFERRER_TYPE_SEARCH_ENGINE  => 'search',
77        Common::REFERRER_TYPE_SOCIAL_NETWORK => 'social',
78        Common::REFERRER_TYPE_WEBSITE        => 'website',
79        Common::REFERRER_TYPE_DIRECT_ENTRY   => 'direct',
80        Common::REFERRER_TYPE_CAMPAIGN       => 'campaign',
81    );
82    if (isset($map[$name])) {
83        return $map[$name];
84    }
85    if ($found = array_search($name, $map)) {
86        return $found;
87    }
88    throw new \Exception("Referrer type '$name' is not valid.");
89}
90
91/**
92 * Returns a URL w/o the protocol type.
93 *
94 * @param string $url
95 * @return string
96 */
97function removeUrlProtocol($url)
98{
99    if (preg_match('/^[a-zA-Z_-]+:\/\//', $url, $matches)) {
100        return substr($url, strlen($matches[0]));
101    }
102    return $url;
103}
104