/** * get the base url for all requests to Piwik * @param integer $idSite * @param string $pkHost * @param boolean $https * @param string $pkModule * @param string $isoCode * @param string $tokenAuth * @return string */ protected static function getBaseURL($idSite = NULL, $pkHost = NULL, $https = NULL, $pkModule = 'API', $isoCode = NULL, $tokenAuth = NULL) { if ($https === NULL) { $https = (bool) Configuration::get(PKHelper::CPREFIX . 'CRHTTPS'); } if (self::$piwikHost == "" || self::$piwikHost === false) { self::$piwikHost = Configuration::get(PKHelper::CPREFIX . 'HOST'); } if ($pkHost === NULL) { $pkHost = self::$piwikHost; } if ($isoCode === NULL) { $isoCode = strtolower(isset(Context::getContext()->language->iso_code) ? Context::getContext()->language->iso_code : 'en'); } if ($idSite === NULL) { $idSite = Configuration::get(PKHelper::CPREFIX . 'SITEID'); } if ($tokenAuth === NULL) { $tokenAuth = Configuration::get(PKHelper::CPREFIX . 'TOKEN_AUTH'); } return ($https ? 'https' : 'http') . "://{$pkHost}index.php?module={$pkModule}&language={$isoCode}&idSite={$idSite}&token_auth={$tokenAuth}"; }
/** * Method used when making ajax calls to Piwik API, * this method outputs json data. * * NOTE: only methods defiend in "PKHelper::$acp" can be called */ private function __pkapicall() { $apiMethod = Tools::getValue('pkapicall'); if (method_exists('PKHelper', $apiMethod) && isset(PKHelper::$acp[$apiMethod])) { $required = PKHelper::$acp[$apiMethod]['required']; // $optional = PKHelper::$acp[$apiMethod]['optional']; $order = PKHelper::$acp[$apiMethod]['order']; foreach ($required as $requiredOption) { if (!Tools::getIsset($requiredOption)) { PKHelper::DebugLogger("__pkapicall():\n\t- Required parameter \"" . $requiredOption . '" is missing'); die(Tools::jsonEncode(array('error' => true, 'message' => sprintf($this->l('Required parameter "%s" is missing'), $requiredOption)))); } } foreach ($order as &$value) { if (Tools::getIsset($value)) { $value = Tools::getValue($value); } else { $value = NULL; } } if (Tools::getIsset('httpUser')) { PKHelper::$httpAuthUsername = Tools::getValue('httpUser'); } if (Tools::getIsset('httpPasswd')) { PKHelper::$httpAuthPassword = Tools::getValue('httpPasswd'); } if (Tools::getIsset('piwikhost')) { PKHelper::$piwikHost = Tools::getValue('piwikhost'); } PKHelper::DebugLogger("__pkapicall():\n\t- Call PKHelper::" . $apiMethod); $result = call_user_func_array(array('PKHelper', $apiMethod), $order); if ($result === FALSE) { $lastError = ""; if (!empty(PKHelper::$errors)) { $lastError = "\n" . PKHelper::$error; } die(Tools::jsonEncode(array('error' => TRUE, 'message' => sprintf($this->l('Unknown error occurred%s'), $lastError)))); } else { PKHelper::DebugLogger("__pkapicall():\n\t- All good"); if (is_array($result) && isset($result[0])) { $message = $result; } else { if (is_object($result)) { $message = $result; } else { $message = is_string($result) && !is_bool($result) ? $result : (is_array($result) ? implode(', ', $result) : TRUE); } } if (is_bool($message)) { die(Tools::jsonEncode(array('error' => FALSE, 'message' => $this->l('Successfully Updated')))); } else { die(Tools::jsonEncode(array('error' => FALSE, 'message' => $message))); } } } else { die(Tools::jsonEncode(array('error' => true, 'message' => sprintf($this->l('Method "%s" dos not exists in class PKHelper'), $apiMethod)))); } }