/**
  * Merge fields in a specified template
  * 
  * @since 1.1.0
  * @static
  * 
  * @param string $template_file
  * @param array $merge_fields
  * @return string|\argent_error 
  */
 public static function merge_template($template_file = NULL, $merge_fields = array())
 {
     $error = new argent_error();
     if (!file_exists($template_file)) {
         $error->add('1100', 'File does not exist', $template_file, 'argent_notification');
     }
     if (!is_array($merge_fields)) {
         $error->add('1041', 'Invalid merge fields specified', $merge_fields, 'argent_notification');
     }
     if ($error->has_errors()) {
         return $error;
     }
     $template = file_get_contents($template_file);
     if (!$template) {
         $error->add('1101', 'File could not be read', $template, 'argent_notification');
     }
     if ($error->has_errors()) {
         return $error;
     }
     if (count($merge_fields) > 0) {
         foreach ($merge_fields as $field => $value) {
             $template = str_replace("%%FIELD_" . $field, $value, $template);
         }
     }
     return $template;
 }
 /**
  * Writes raw strings into the appropriate log file
  * 
  * @access private
  * @static
  * @since 1.0.1
  * @param string $log Log file to use; "error" or "event"
  * @param string $string String to enter into the log 
  */
 private static function write_to_log($log, $string)
 {
     switch ($log) {
         case 'error':
             $log_file = "error.log";
             break;
         case 'event':
             $log_file = "events.log";
             break;
         default:
             $error = new argent_error();
             $error->add('0010', 'Invalid log specified', $log);
             return $error;
     }
     $log_file = ABSOLUTE_PATH . 'argent/logs/' . $log_file;
     if (!is_writable($log_file)) {
         die('Argent: Log file not writable: ' . $log_file);
     }
     $log = fopen($log_file, 'a');
     fwrite($log, $string . "\r\n");
 }
 /**
  * Revokes a User Right
  * 
  * @param string $user_id
  * @param string $right
  * @return boolean
  */
 public static function revoke_right($user_id, $right)
 {
     $error = new argent_error();
     $db = new argent_database();
     if (!self::object_exists($user_id)) {
         $error->add('1013', 'Invalid user account', $user_id, 'argent_uauth');
     }
     $right_id = self::right_exists($right);
     if (!$right_id) {
         $error->add('1042', 'Invalid user right', $right, 'argent_uauth');
     }
     if (!self::user_has_right($right, $user_id)) {
         return true;
     }
     return $user_right = argent_meta::unrelate($user_id, $right_id, 'user_right');
 }
 public static function find_related($primary_object_id = null, $secondary_object_id = null, $relationship = null, $include_reverse = false)
 {
     $error = new argent_error();
     $db = new argent_database();
     if ($primary_object_id != null && !argent_meta::object_registered($primary_object_id)) {
         $error->add('1038', 'Object does not exist', $primary_object_id, 'argent_uauth');
     }
     if ($secondary_object_id != null && !argent_meta::object_registered($secondary_object_id)) {
         $error->add('1038', 'Object does not exist', $secondary_object_id, 'argent_uauth');
     }
     if (!is_string($relationship) && $relationship != null) {
         $error->add('1050', 'Invalid data type: expecting STRING', $relationship, 'argent_meta');
     }
     if ($error->has_errors()) {
         return $error;
     }
     $sql = "\r\n                    SELECT\r\n                        *\r\n                    FROM\r\n                        `ua_relationships`\r\n                    WHERE\r\n                        ";
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        (\r\n                        ";
     }
     if ($primary_object_id != null) {
         $sql .= "`primary_object_id` = '{$db->escape_value($primary_object_id)}'";
     }
     if ($primary_object_id != null && $secondary_object_id != null) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($secondary_object_id != null) {
         $sql .= "\r\n                        `secondary_object_id` = '{$db->escape_value($secondary_object_id)}'\r\n                      ";
     }
     if ($primary_object_id != null || $secondary_object_id != null) {
         $sql .= "\r\n                        )\r\n                    ";
     }
     if ($include_reverse) {
         $sql .= "\r\n                        OR\r\n                          ";
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            (\r\n                            ";
         }
         if ($secondary_object_id != null) {
             $sql .= "`primary_object_id` = '{$db->escape_value($secondary_object_id)}'";
         }
         if ($primary_object_id != null && $secondary_object_id != null) {
             $sql .= "\r\n                        AND\r\n                        ";
         }
         if ($primary_object_id != null) {
             $sql .= "\r\n                            `secondary_object_id` = '{$db->escape_value($primary_object_id)}'\r\n                          ";
         }
         if ($primary_object_id != null || $secondary_object_id != null) {
             $sql .= "\r\n                            )\r\n                        ";
         }
     }
     if ($relationship != null && ($primary_object_id != null || $secondary_object_id != null)) {
         $sql .= "\r\n                    AND\r\n                    ";
     }
     if ($relationship != null) {
         $sql .= "\r\n                        `relationship` = '{$db->escape_value($relationship)}'\r\n                    ";
     }
     $relationship_data = $db->returntable($sql);
     return $relationship_data;
 }