Пример #1
0
/**
 * Assemble URL from string
 * 
 * This function will convert string in format ?route=name[&param=value...] to 
 * URL based on route and params. Route paremeter is required
 *
 * @param string $string
 * @return null
 */
function assemble_from_string($string)
{
    $params = parse_string(substr($string, 1));
    $route = array_var($params, 'route');
    if (empty($route)) {
        use_error('RouteNotDefinedError');
        return new RouteNotDefinedError($route);
    }
    // if
    unset($params['route']);
    return assemble_url($route, $params);
}
 /**
  * Execute specific controller action
  *
  * @param string $action
  * @return InvalidControllerActionError if action name is not valid or true
  */
 function execute($action)
 {
     $action = trim(strtolower($action));
     $valid = $this->validAction($action);
     if (is_error($valid)) {
         return $valid;
     }
     // if
     if ($valid) {
         $this->setAction($action);
         $this->{$action}();
         return true;
     } else {
         use_error('InvalidControllerActionError');
         return new InvalidControllerActionError($this->getControllerName(), $action);
     }
     // if
 }
Пример #3
0
/**
 * Return application instance
 *
 * @param void
 * @return AngieApplication
 */
function &application()
{
    static $instance = false;
    if (!defined('APPLICATION_NAME')) {
        $nothing = null;
        return $nothing;
    }
    // if
    if ($instance === false) {
        $class = APPLICATION_NAME;
        $file = APPLICATION_PATH . "/{$class}.class.php";
        if (is_file($file)) {
            require $file;
            if (class_exists($class)) {
                $application = new $class();
                if (instance_of($application, 'AngieApplication')) {
                    $instance = $application;
                } else {
                    use_error('ClassNotImplementedError');
                    return new ClassNotImplementedError($class, $file);
                }
                // if
            } else {
                use_error('ClassNotImplementedError');
                return new ClassNotImplementedError($class, $file);
            }
            // if
        } else {
            use_error('ClassNotImplementedError');
            return new ClassNotImplementedError($class, $file);
        }
        // if
    }
    // if
    return $instance;
}
 /**
  * Authenticate with given credential agains authentication source
  *
  * @param array $credentials
  * @return User
  */
 function authenticate($credentials)
 {
     use_error('NotImplementederror');
     return new NotImplementedError('authenticate', 'AuthenticationProvider');
 }
Пример #5
0
 /**
  * trigger Smarty error
  *
  * @param string $error_msg
  * @param integer $error_type
  */
 function trigger_error($error_msg, $error_type = E_USER_WARNING)
 {
     use_error('SmartyError');
     return new SmartyError($error_msg, $error_type);
     //trigger_error("Smarty error: $error_msg", $error_type);
 }
 /**
  * Prepare SQL and execute it...
  *
  * @param string $sql
  * @param arary $arguments
  * @param boolean $only_first
  * @return DBResult
  * @throws DBQueryError
  */
 function prepareAndExecute($sql, $arguments = null, $only_first = false)
 {
     if (is_array($arguments)) {
         $sql = $this->prepareSQL($sql, $arguments);
     }
     // if
     $query_result = mysql_query($sql, $this->link);
     if (DEBUG >= DEBUG_DEVELOPMENT && !str_starts_with(strtolower($sql), 'explain')) {
         log_message($sql, LOG_LEVEL_INFO, 'sql');
     }
     // if
     if ($query_result === false) {
         if (DEBUG >= DEBUG_PRODUCTION) {
             log_message('SQL error. MySQL said: ' . mysql_error($this->link) . "\n({$sql})", LOG_LEVEL_ERROR, 'sql');
         }
         // if
         use_error('DBQueryError');
         $error_message = mysql_error($this->link);
         $error_number = mysql_errno($this->link);
         // Non-transactional tables not rolled back!
         if ($error_number == 1196) {
             log_message('Non-transactional tables not rolled back!', LOG_LEVEL_WARNING, 'sql');
             return true;
             // Server gone away
         } elseif ($error_number == 2006 || $error_number == 2013) {
             if (defined('DB_AUTO_RECONNECT') && DB_AUTO_RECONNECT > 0) {
                 $executed = false;
                 for ($i = 1; $i <= DB_AUTO_RECONNECT; $i++) {
                     if (DEBUG >= DEBUG_PRODUCTION) {
                         log_message("Trying to reconnect, attempt #{$i}", LOG_LEVEL_INFO, 'sql');
                     }
                     // if
                     $connect = $this->connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PERSIST, DB_CHARSET);
                     if ($connect && !is_error($connect)) {
                         $query_result = mysql_query($sql, $this->link);
                         if ($query_result !== false) {
                             $executed = true;
                             break;
                             // end of the loop
                         }
                         // if
                     }
                     // if
                 }
                 // for
                 // Not executed after reconnects?
                 if (!$executed) {
                     return new DBQueryError($sql, $error_number, $error_message);
                 }
                 // if
             } else {
                 return new DBQueryError($sql, $error_number, $error_message);
             }
             // if
             // Other
         } else {
             return new DBQueryError($sql, $error_number, $error_message);
         }
         // if
     }
     // if
     $this->query_counter++;
     // Simple result
     if ($query_result === true) {
         return true;
     }
     // if
     if ($only_first) {
         $return = mysql_fetch_assoc($query_result);
         if (!is_array($return)) {
             $return = null;
         }
         // if
     } else {
         $return = array();
         while ($row = mysql_fetch_assoc($query_result)) {
             $return[] = $row;
         }
         // while
         if (!count($return)) {
             $return = null;
         }
         // if
     }
     // if
     mysql_free_result($query_result);
     return $return;
 }
Пример #7
0
 /**
  * Assemble URL
  * 
  * Supported options:
  * 
  * - url_base (string): base for URL-s, default is an empty string
  * - query_arg_separator (string): what to use to separate query string 
  *   arguments, default is '&'
  * - anchor (string): name of the URL anchor
  * 
  * @param string $name
  * @param array $data
  * @param array $options
  * @throws Angie_Router_Error_Assemble
  */
 function assemble($name, $data = array(), $options = null)
 {
     $url_base = array_var($options, 'url_base', '');
     if (empty($url_base)) {
         $url_base = URL_BASE;
     }
     // if
     $query_arg_separator = array_var($options, 'query_arg_separator', '&');
     $anchor = array_var($options, 'anchor', '');
     $route = array_var($this->routes, $name);
     if (!instance_of($route, 'Route')) {
         use_error('RouteNotDefinedError');
         return new RouteNotDefinedError($name);
     }
     // if
     log_message("Route {$name} assembled", LOG_LEVEL_INFO, 'routing');
     return $route->assemble($data, $url_base, $query_arg_separator, $anchor);
 }
 /**
  * Authenticate
  *
  * @param void
  * @return null
  */
 function authenticate()
 {
     $provider_class = AUTH_PROVIDER;
     use_auth_provider($provider_class);
     if (!class_exists($provider_class)) {
         use_error('ClassNotImplementedError');
         return new ClassNotImplementedError($provider_class);
     }
     // if
     $provider = new $provider_class();
     if (!instance_of($provider, 'AuthenticationProvider')) {
         return new InvalidInstanceError('provider', $provider, 'AuthenticationProvider');
     }
     // if
     $manager =& Authentication::instance($provider, false);
     $token = false;
     if (FORCE_QUERY_STRING) {
         if (ANGIE_QUERY_STRING) {
             $query_string_aprams = parse_string(ANGIE_QUERY_STRING);
             if (isset($query_string_aprams['token'])) {
                 $token = $query_string_aprams['token'];
             }
             // if
         }
         // if
     } else {
         $token = isset($_GET['token']) ? $_GET['token'] : false;
     }
     // if
     // Handle token based authentication
     if ($token !== false) {
         // Die if disabled or read-only with POST parameters
         if (API_STATUS == API_DISABLED || API_STATUS == API_READ_ONLY && count($_POST) > 0) {
             header('HTTP/1.1 403 Forbidden');
             print "<h1>HTTP/1.1 403 Forbidden</h1>\n";
             if (API_STATUS == API_DISABLED) {
                 print '<p>API is disabled</p>';
             } else {
                 print '<p>API is read-only</p>';
             }
             // if
             die;
         }
         // if
         // Get token and auth_id (old and new API key formats are supported)
         if (strpos($token, '-') !== false) {
             list($auth_id, $token) = explode('-', $token);
         } else {
             $auth_id = array_var($_GET, 'auth_id');
         }
         // if
         $user = null;
         if ($auth_id) {
             $user = Users::findById($auth_id);
         }
         // if
         if (instance_of($user, 'User') && $user->getToken() == $token) {
             $manager->provider->logUserIn($user, array('silent' => true));
             return true;
         } else {
             header('HTTP/1.1 403 Forbidden');
             print '<h1>HTTP/1.1 403 Forbidden</h1>';
             die;
         }
         // if
     }
     // if
     $manager->provider->initialize();
     return true;
 }
Пример #9
0
/**
 * Contruct controller and execute specific action
 *
 * @param Request $request
 * @return null
 * @throws ControllerDnxError
 * @throws InvalidControllerActionError
 */
function execute_action($request)
{
    $controller_name = $request->getController();
    // we'll use this a lot
    $use_controller = use_controller($controller_name, $request->getModule());
    if (is_error($use_controller)) {
        return $use_controller;
    }
    // if
    $controller_class = get_controller_class($controller_name);
    if (!class_exists($controller_class)) {
        use_error('ControllerDnxError');
        return new ControllerDnxError($controller_name);
    }
    // if
    $controller = new $controller_class($request);
    if (!instance_of($controller, 'Controller')) {
        use_error('ControllerDnxError');
        return new ControllerDnxError($controller_name);
    }
    // if
    return $controller->execute($request->getAction());
}
 /**
  * Render log footer
  * 
  * @param ProjectObject $object
  * @param boolean $in_project
  * @return string
  */
 function renderFooter($object = null, $in_project = false)
 {
     use_error('NotImplementedError');
     return new NotImplementedError('ActivityLog', 'renderFooter');
 }
 /**
  * Initialize authentication
  * 
  * First we get authentication provider and then we create authentication 
  * manager instance...
  *
  * @param void
  * @return null
  */
 function authenticate()
 {
     $provider_class = AUTH_PROVIDER;
     use_auth_provider($provider_class);
     if (!class_exists($provider_class)) {
         use_error('ClassNotImplementedError');
         return new ClassNotImplementedError($provider_class);
     }
     // if
     $provider = new $provider_class();
     if (!instance_of($provider, 'AuthenticationProvider')) {
         return new InvalidInstanceError('provider', $provide, 'AuthenticationProvider');
     }
     // if
     $manager =& Authentication::instance($provider);
 }
 /**
  * Guess content type of file
  * 
  * First we try to use PEAR::MIME_Type, if installed, to detect the content 
  * type, else we check if ext/mime_magic is loaded and properly configured.
  *
  * Returns PEAR_Error if:
  *      o if PEAR::MIME_Type failed to detect a proper content type
  *        (HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE)
  *      o ext/magic.mime is not installed, or not properly configured
  *        (HTTP_DOWNLOAD_E_NO_EXT_MMAGIC)
  *      o mime_content_type() couldn't guess content type or returned
  *        a content type considered to be bogus by setContentType()
  *        (HTTP_DOWNLOAD_E_INVALID_CONTENT_TYPE)
  * 
  * @access  public
  * @return  mixed   Returns true on success or PEAR_Error on failure.
  */
 function guessContentType()
 {
     use_error('NotImplementedError');
     return new NotImplementedError('HTTP_Download', 'guessContentType');
     //        if (class_exists('MIME_Type') || @include_once 'MIME/Type.php') {
     //            if (PEAR::isError($mime_type = MIME_Type::autoDetect($this->file))) {
     //              return new Error($mime_type->getMessage());
     //            }
     //            return $this->setContentType($mime_type);
     //        }
     //        if (!function_exists('mime_content_type')) {
     //            return PEAR::raiseError(
     //                'This feature requires ext/mime_magic!',
     //                HTTP_DOWNLOAD_E_NO_EXT_MMAGIC
     //            );
     //        }
     //        if (!is_file(ini_get('mime_magic.magicfile'))) {
     //            return PEAR::raiseError(
     //                'ext/mime_magic is loaded but not properly configured!',
     //                HTTP_DOWNLOAD_E_NO_EXT_MMAGIC
     //            );
     //        }
     //        if (!$content_type = @mime_content_type($this->file)) {
     //          return new Error('Couldn\'t guess content type with mime_content_type()');
     //        }
     //        return $this->setContentType($content_type);
 }
 /**
  * Clear data from cache - drop everything
  *
  * @param void
  * @return null
  */
 function clear()
 {
     use_error('NotImplementedError');
     return new NotImplementedError('CacheBackend', 'clear');
 }
 /**
  * Returns true if we already have an search index for a given entry
  *
  * @param integer $object_id
  * @param string $type
  * @return boolean
  */
 function hasObject($object_id, $type)
 {
     use_error('NotImplementedError');
     return new NotImplementedError('SearchEngine', 'hasObject');
 }
Пример #15
0
 /**
  * Assemle URL based on provided input data
  * 
  * This function will use input data and put it into route string. It can 
  * return relative path based on the route string or absolute URL 
  * (PROJECT_URL constant will be used as a base)
  *
  * @param array $data
  * @param string $url_base
  * @param string $query_arg_separator
  * @param string $anchor
  * @return string
  * @throws AssembleURLError
  */
 function assemble($data, $url_base, $query_arg_separator, $anchor = '')
 {
     if (!is_array($data)) {
         if ($data === null) {
             $data = array();
         } else {
             $data = array('id' => $data);
         }
         // if
     }
     // if
     $path_parts = array();
     $part_names = array();
     foreach ($this->parts as $key => $part) {
         if (isset($part['name'])) {
             $part_name = $part['name'];
             $part_names[] = $part_name;
             if (isset($data[$part_name])) {
                 $path_parts[$key] = $data[$part_name];
             } elseif (isset($this->defaults[$part_name])) {
                 $path_parts[$key] = $this->defaults[$part_name];
             } else {
                 use_error('AssembleURLError');
                 return new AssembleURLError($this->getRouteString(), $data, $this->getDefaults());
             }
             // if
         } else {
             $path_parts[$key] = $part['regex'];
         }
         // if
     }
     // foreach
     $query_parts = array();
     foreach ($data as $k => $v) {
         if (!in_array($k, $part_names)) {
             $query_parts[$k] = $v;
         }
         // if
     }
     // foreach
     if (PATH_INFO_THROUGH_QUERY_STRING) {
         $url = $url_base;
         $query_parts = array_merge(array('path_info' => implode('/', $path_parts)), $query_parts);
     } else {
         $url = with_slash($url_base) . implode('/', $path_parts);
         if (!str_ends_with($url, '/') && str_ends_with($this->route_string, '/')) {
             $url .= '/';
         }
         // if
     }
     // if
     if (count($query_parts)) {
         if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
             $url .= '?' . http_build_query($query_parts, '', $query_arg_separator);
         } else {
             $url .= '?' . http_build_query($query_parts, '');
         }
         // if
     }
     // if
     $trimmed_anchor = trim($anchor);
     if ($trimmed_anchor) {
         $url .= '#' . $anchor;
     }
     // if
     return $url;
 }
Пример #16
0
	/**
	 * Deny the given permission code/arg to the given group
	 *
	 * @param int $groupID The ID of the group
	 * @param string $code The permission code
	 * @param string Optional: The permission argument (e.g. a page ID).
	 * @returns Permission Returns the new permission object.
	 */
	public static function deny($groupID, $code, $arg = "any") {
		$perm = new Permission();
		$perm->GroupID = $groupID;
		$perm->Code = $code;
		$perm->Type = self::DENY_PERMISSION;

		// Arg component
		switch($arg) {
			case "any":
				break;
			case "all":
				$perm->Arg = -1;
			default:
				if(is_numeric($arg)) {
					$perm->Arg = $arg;
				} else {
					use_error("Permission::checkMember: bad arg '$arg'",
										E_USER_ERROR);
				}
		}

		$perm->write();
		return $perm;
	}
 /**
  * Attach uploaded file
  * 
  * $file is a single element of $_FILES auto global array
  *
  * @param array $file
  * @param User $user
  * @return boolean
  */
 function attachUploadedFile($file, $user)
 {
     if (is_array($file)) {
         if (isset($file['error']) && $file['error'] > 0) {
             use_error('UploadError');
             return new UploadError($file['error']);
         }
         // if
         $destination_file = get_available_uploads_filename();
         if (move_uploaded_file($file['tmp_name'], $destination_file)) {
             if (FIX_UPLOAD_PERMISSION !== false) {
                 @chmod($destination_file, FIX_UPLOAD_PERMISSION);
             }
             // if
             return $this->addPendingFile($destination_file, array_var($file, 'name'), array_var($file, 'type'), array_var($file, 'size'), $user);
         }
         // if
     }
     // if
     return false;
 }