Beispiel #1
0
    /**
     * Constructor
     *
     * This constructor fetches the local configuration path
     * and constructs the config file for a certain module.
     *
     * After having the xml configuration file loaded, it'll create
     * a Zend_Config_Xml instance and store it in $this->config
     *
     * @param string $name  The name of the configuration to load
     */
    public function __construct($name)
    {
        if (defined('ADMIN_CONF_PATH')) {
            $config = ADMIN_CONF_PATH;
        } else {
            try {
                $config = Zend_Registry::get('localConfigPath');
            } catch (Zend_Exception $e) {}
        }

        $this->configFile = $config . $name . '.xml';
        $this->configName = $name;

        /**
         * @TODO Remove me after the rewrite of the Config Reader. Move this to
         *       Frapi_Internal. I dislike inter-dependencies.
         */
        if ($cachedConfig = Frapi_Internal::getCached('configFile-' . $name))
        {
            $this->config = $cachedConfig;
        } else {
            $helper = new Lupin_Config_Helper_XmlArray();
            $this->config = $helper->parse($this->configFile);
            Frapi_Internal::setCached('configFile-' . $name, $this->config);
        }

        $this->config = $this->config[$name];
    }
Beispiel #2
0
 /**
  * Populate the Output
  *
  * This method populates the $this->response
  * variable with the value returned from the
  * action.
  *
  * @param Mixed  $response Most of the times an array but could be and stdClass
  * @param String $customTemplate The custom template file to use instead of the default one.
  *                        
  * @return Object $This object
  */
 public function populateOutput($data, $customTemplate = false)
 {
     $directory = CUSTOM_OUTPUT . DIRECTORY_SEPARATOR . 'xml';
     $file = $directory . DIRECTORY_SEPARATOR . ucfirst(strtolower($this->action)) . '.xml.tpl';
     if ($customTemplate !== false) {
         $file = $directory . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . $customTemplate . '.xml.tpl';
     }
     $xml = '';
     $print = hash('md5', json_encode($data));
     if ($response = Frapi_Internal::getCached($print)) {
         $this->response = json_decode($response);
     } elseif (file_exists($file)) {
         ob_start();
         echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
         include $file;
         $xml = ob_get_contents();
         ob_end_clean();
         $this->response = $xml;
         Frapi_Internal::setCached($print, json_encode($xml));
     } elseif ($this->action == 'defaultError') {
         $directory = LIBRARY_OUTPUT . DIRECTORY_SEPARATOR . 'xml';
         $file = $directory . DIRECTORY_SEPARATOR . 'Defaulterror.xml.tpl';
         ob_start();
         echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
         include $file;
         $xml = ob_get_contents();
         ob_end_clean();
         $this->response = $xml;
         Frapi_Internal::setCached($print, json_encode($xml));
     } else {
         $this->response = $this->_generateXML($data);
     }
     return $this;
 }
Beispiel #3
0
 /**
  * Find out whether a username is a valid one or not.
  *
  * This method is used from the Digest to figure out whether or not a user
  * is a valid handle and one that should be used.
  *
  * @param string $partnerID  Partner identifier (email).
  *
  * @return mixed Information about a user or a Boolean Whether the 
  *               supplied partnerID are valid details.
  */
 public static function isPartnerHandle($partnerID)
 {
     if (!($partners = Frapi_Internal::getCached("Partners.emails-keys"))) {
         $partners = Frapi_Internal::getCachedPartners();
     }
     if (isset($partners[$partnerID]) && $partners[$partnerID]['email'] == $partnerID) {
         return $partners[$partnerID];
     }
     return false;
 }
Beispiel #4
0
 /**
  * This function is called from Frapi_Security::isPartner
  * so this function must actually implement the validation
  * of the partner.
  *
  * @param String $partnerID  Partner identifier (email).
  * @param String $partnerKey Partner key (sha1 hash).
  *
  * @return Boolean Whether the supplied partnerID and partnerKey are valid details.
  */
 public static function isPartner($partnerID, $partnerKey)
 {
     if (!($partners = Frapi_Internal::getCached("Partners.emails-keys"))) {
         $partners = Frapi_Internal::getCachedPartners();
     }
     if (isset($partners[$partnerID]) && $partners[$partnerID]['email'] == $partnerID && $partners[$partnerID]['api_key'] == $partnerKey) {
         return true;
     }
     return false;
 }
Beispiel #5
0
 /**
  * Load routes from APC, database etc.
  * Prepare routes if necessary!
  *
  * @return void
  */
 public function loadAndPrepareRoutes()
 {
     if ($routes = Frapi_Internal::getCached('Router.routes-prepared')) {
         $this->setPreparedRoutes($routes);
     } else {
         $routes = array();
         $ret = Frapi_Internal::getConfiguration('actions');
         $rows = $ret->getAll('action');
         foreach ($rows as $row) {
             if (isset($row['route']) && !empty($row['route'])) {
                 $routes[$row['name']] = $row['route'];
             }
         }
         $this->setPreparedRoutes($preparedRoutes = self::prepareRoutes($routes));
         Frapi_Internal::setCached('Router.routes-prepared', $preparedRoutes);
     }
 }
Beispiel #6
0
 /**
  * Get default format from SQLite database
  *
  * A format (output type) has not been supplied
  * so try to get default from backend.
  *
  * @return String The format.
  */
 public function getDefaultFormatFromConfiguration()
 {
     if ($default_output_format = Frapi_Internal::getCached('Output.default-format')) {
         return $default_output_format;
     }
     $conf = Frapi_Internal::getConfiguration('outputs');
     $row = $conf->getByField('output', 'default', '1');
     if (isset($row) && isset($row['name'])) {
         Frapi_Internal::setCached('Output.default-format', $row['name']);
         return $row['name'];
     }
     return Frapi_Controller_Api::DEFAULT_OUTPUT_FORMAT;
 }
Beispiel #7
0
    /**
     * Private function to get error (statically, APC, or DB).
     *
     * This function tries to locate the error in progressively
     * slower datastores (static class variable, APC, database)
     * and will store the loaded errors in the faster stores.
     *
     * @param string $error_name Name of error.
     * @param string $error_msg  The actual message of the error.
     * @param int    $http_code  This might be hard to grasp however, we are in a web
     *                           industry dealing with the web. The code you are sending
     *                           to your exception should really be represented by the
     *                           HTTP Code returned to your users.
     *
     * @return array An array with the content of the error.
     */
    private static function _get($error_name, $error_msg = false, $http_code = false)
    {
        if (!self::$_statically_loaded) {
            $errors = Frapi_Internal::getCached('Errors.user-defined');

            if ($errors) {
                self::$_errors = $errors;
            } elseif ($errors = self::_getErrorsFromDb()) {
                self::$_errors = $errors;
                Frapi_Internal::setCached('Errors.user-defined', $errors);
            }

            self::$_statically_loaded = true;
        }

        if (isset(self::$_errors[$error_name])) {
            $error = self::$_errors[$error_name];

            if ($error_msg !== false) {
                $error['message'] = $error_msg;
            }

            if ($http_code !== false) {
                $error['http_code'] = $http_code;
            }

            return $error;
        }

        return array(
            'name'      => $error_name,
            'message'   => $error_msg !== false ? $error_msg : $error_name,
            'http_code' => $http_code !== false  ? $http_code : '400',
        );
    }
Beispiel #8
0
 /**
  * Private function to get error (statically, APC, or DB).
  *
  * This function tries to locate the error in progressively
  * slower datastores (static class variable, APC, database)
  * and will store the loaded errors in the faster stores.
  *
  * @param string $error_name Name of error.
  * @param string $error_msg   The actual message of the error.
  * @param int    $http_code   This might be hard to grasp however, we are in a web
  *                            industry dealing with the web. The code you are sending
  *                            to your exception should really be represented by the
  *                            HTTP Code returned to your users.
  * @param string $http_phrase The http phrase associated with the http_code
  *
  * @return array An array with the content of the error.
  */
 private static function _get($error_name, $error_msg = false, $http_code = false, $http_phrase = false)
 {
     if (!self::$_statically_loaded) {
         $errors = Frapi_Internal::getCached('Errors.user-defined');
         if ($errors) {
             self::$_errors = $errors;
         } elseif ($errors = self::_getErrorsFromDb()) {
             self::$_errors = $errors;
             Frapi_Internal::setCached('Errors.user-defined', $errors);
         }
         self::$_statically_loaded = true;
     }
     if (isset(self::$_errors[$error_name])) {
         $error = self::$_errors[$error_name];
         if ($error_msg !== false) {
             $error['message'] = $error_msg;
         }
         if ($http_code !== false) {
             $error['http_code'] = $http_code;
         }
         if ($http_phrase !== false) {
             $error['http_phrase'] = $http_phrase;
         } else {
             if (isset(Frapi_Response::$http_reason[$error['http_code']]) && !$error['http_phrase']) {
                 $error['http_phrase'] = Frapi_Response::$http_reason[$error['http_code']];
             } else {
                 if ($error['message'] !== false && !$error['http_phrase']) {
                     $error['http_phrase'] = $error['message'];
                 }
             }
         }
         return $error;
     }
     if ($http_phrase === false) {
         if (isset(Frapi_Response::$http_reason[$http_code])) {
             $http_phrase = Frapi_Response::$http_reason[$http_code];
         } else {
             if ($error_msg !== false) {
                 $http_phrase = $error_msg;
             }
         }
     }
     return array('name' => $error_name, 'message' => $error_msg !== false ? $error_msg : $error_name, 'http_code' => $http_code !== false ? $http_code : '400', 'http_phrase' => $http_phrase !== false ? $http_phrase : 'Bad Request');
 }
Beispiel #9
0
 public static function getEnabledFormats()
 {
     try {
         if ($formats = Frapi_Internal::getCached('Output.formats-enabled')) {
             return $formats;
         }
         $cache = new Frapi_Internal();
         $outputs = $cache->getConfiguration('outputs')->getAll('output');
         $formats = array();
         foreach ($outputs as $output) {
             if ($output['enabled'] == 1) {
                 $formats[] = strtolower($output['name']);
             }
         }
     } catch (Exception $e) {
         return false;
     }
     Frapi_Internal::setCached('Output.formats-enabled', $formats);
     return $formats;
 }
Beispiel #10
0
 /**
  * Test cache stores and retrieves items.
  *
  * @dataProvider keysValuesProvider
  **/
 public function testCacheStoreAndRetrieve($key, $value)
 {
     Frapi_Internal::setCached($key, $value);
     $this->assertEquals($value, Frapi_Internal::getCached($key));
 }