Esempio n. 1
0
 /**
  * @param mixed $handler_id The ID of the handler.
  * @param Array $args The argument list.
  * @param Array &$data The local request data.
  */
 public function _handler_create($handler_id, array $args, array &$data)
 {
     midcom::get('auth')->require_user_do('org.openpsa.user:manage', null, 'org_openpsa_user_interface');
     $data['controller'] = $this->get_controller('nullstorage');
     // Check if we get the person
     $this->_person = new midcom_db_person($args[0]);
     $this->_person->require_do('midgard:update');
     $this->_account = new midcom_core_account($this->_person);
     if ($this->_account->get_username()) {
         throw new midcom_error('Given user already has an account');
     }
     switch ($data['controller']->process_form()) {
         case 'save':
             $this->_master->create_account($this->_person, $data["controller"]->formmanager);
         case 'cancel':
             return new midcom_response_relocate('view/' . $this->_person->guid . '/');
     }
     if ($this->_person->email) {
         // Email address (first part) is the default username
         $this->_request_data['default_username'] = preg_replace('/@.*/', '', $this->_person->email);
     } else {
         // Otherwise use cleaned up firstname.lastname
         $this->_request_data['default_username'] = midcom_helper_misc::generate_urlname_from_string($this->_person->firstname) . '.' . midcom_helper_misc::generate_urlname_from_string($this->_person->lastname);
     }
     midcom::get('head')->set_pagetitle("{$this->_person->firstname} {$this->_person->lastname}");
     $this->_prepare_request_data();
     $this->_update_breadcrumb_line('create account');
     // Add toolbar items
     org_openpsa_helpers::dm2_savecancel($this);
 }
Esempio n. 2
0
 /**
  * DM2 creation callback, binds to the current content topic.
  */
 private function _create_product($title, $productgroup)
 {
     $product = new org_openpsa_products_product_dba();
     $product->productGroup = $productgroup;
     $product->title = $title;
     if (!$product->create()) {
         debug_print_r('We operated on this object:', $product);
         return null;
     }
     // Generate URL name
     if ($product->code == '') {
         $product->code = midcom_helper_misc::generate_urlname_from_string($product->title);
         $tries = 0;
         $maxtries = 999;
         while (!$product->update() && $tries < $maxtries) {
             $product->code = midcom_helper_misc::generate_urlname_from_string($product->title);
             if ($tries > 0) {
                 // Append an integer if products with same name exist
                 $product->code .= sprintf("-%03d", $tries);
             }
             $tries++;
         }
     }
     $product->parameter('midcom.helper.datamanager2', 'schema_name', $this->_config->get('api_products_schema'));
     return $product;
 }
Esempio n. 3
0
 /**
  * DM2 creation callback, binds to the current content topic.
  */
 private function _create_article($title)
 {
     $author = midcom::get('auth')->user->get_storage();
     $article = new midcom_db_article();
     $article->topic = $this->_content_topic->id;
     $article->title = $title;
     //Figure out author
     $article->author = $author->id;
     if (!$article->create()) {
         debug_print_r('We operated on this object:', $article);
         return null;
     }
     // Generate URL name
     if ($article->name == '') {
         $article->name = midcom_helper_misc::generate_urlname_from_string($article->title);
         $tries = 0;
         $maxtries = 999;
         while (!$article->update() && $tries < $maxtries) {
             $article->name = midcom_helper_misc::generate_urlname_from_string($article->title);
             if ($tries > 0) {
                 // Append an integer if articles with same name exist
                 $article->name .= sprintf("-%03d", $tries);
             }
             $tries++;
         }
     }
     $article->parameter('midcom.helper.datamanager2', 'schema_name', $this->_config->get('api_metaweblog_schema'));
     return $article;
 }
Esempio n. 4
0
 function read_structure()
 {
     // Generate a safe name for the structure
     $structure_name = midcom_helper_misc::generate_urlname_from_string(midcom::get()->get_page_prefix());
     // Prepare structure
     $structure = array();
     $structure[$structure_name] = array();
     $structure[$structure_name]['name'] = $structure_name;
     $structure[$structure_name]['title'] = $GLOBALS['midcom_config']['midcom_site_title'];
     // Read the topic data
     $root_node = midcom_core_context::get()->get_key(MIDCOM_CONTEXT_ROOTTOPIC);
     $structure[$structure_name]['root'] = $this->read_node($root_node);
     file_put_contents("{$this->root_dir}{$structure_name}.inc", $this->_draw_array($structure));
 }
Esempio n. 5
0
 /**
  * This is what Datamanager calls to actually create a directory
  */
 public function &dm2_create_callback(&$datamanager)
 {
     $topic = new org_openpsa_documents_directory();
     $topic->up = $this->_request_data['directory']->id;
     $topic->component = 'org.openpsa.documents';
     // Set the name by default
     $topic->name = midcom_helper_misc::generate_urlname_from_string($_POST['extra']);
     if (!$topic->create()) {
         debug_print_r('We operated on this object:', $topic);
         throw new midcom_error("Failed to create a new topic, cannot continue. Error: " . midcom_connection::get_error_string());
     }
     $this->_request_data['directory'] = new org_openpsa_documents_directory($topic->id);
     return $topic;
 }
Esempio n. 6
0
 /**
  * Check whether given wikiword is free in given node
  *
  * Returns true if word is free, false if reserved
  */
 public static function node_wikiword_is_free(&$node, $wikiword)
 {
     if (empty($node)) {
         //Invalid node
         debug_add('given node is not valid', MIDCOM_LOG_ERROR);
         return false;
     }
     $wikiword_name = midcom_helper_misc::generate_urlname_from_string($wikiword);
     $qb = new midgard_query_builder('midgard_article');
     $qb->add_constraint('topic', '=', $node[MIDCOM_NAV_OBJECT]->id);
     $qb->add_constraint('name', '=', $wikiword_name);
     $ret = @$qb->execute();
     if (is_array($ret) && count($ret) > 0) {
         //Match found, word is reserved
         debug_add("QB found matches for name '{$wikiword_name}' in topic #{$node[MIDCOM_NAV_OBJECT]->id}, given word '{$wikiword}' is reserved", MIDCOM_LOG_INFO);
         debug_print_r('QB results:', $ret);
         return false;
     }
     return true;
 }
Esempio n. 7
0
 public function _on_creating()
 {
     if ($this->title == '' || !$this->topic) {
         // We must have wikiword and topic at this stage
         return false;
     }
     // Check for duplicates
     $qb = net_nemein_wiki_wikipage::new_query_builder();
     $qb->add_constraint('topic', '=', $this->topic);
     $qb->add_constraint('title', '=', $this->title);
     $result = $qb->execute();
     if (count($result) > 0) {
         midcom_connection::set_error(MGD_ERR_OBJECT_NAME_EXISTS);
         return false;
     }
     // Generate URL-clean name
     if ($this->name != 'index') {
         $this->name = midcom_helper_misc::generate_urlname_from_string($this->title);
     }
     return true;
 }
Esempio n. 8
0
 /**
  * Copy an object
  *
  * @param mixed &$source     MgdSchema object for reading the parameters
  * @param mixed &$parent      MgdSchema parent object
  * @param array $defaults
  * @return boolean Indicating success
  */
 public function copy_object(&$source, &$parent = null, $defaults = array())
 {
     // Resolve the source object
     self::resolve_object($source);
     // Duplicate the object
     $class_name = get_class($source);
     $target = new $class_name();
     $properties = $this->get_object_properties($source);
     // Copy the object properties
     foreach ($properties as $property) {
         // Skip certain fields
         if (preg_match('/^(_|metadata|guid|id)/', $property)) {
             continue;
         }
         $target->{$property} = $source->{$property};
     }
     // Override requested root object properties
     if (isset($this->target->guid) && $target->guid === $this->target->guid) {
         foreach ($this->root_object_values as $name => $value) {
             $target->{$name} = $value;
         }
     }
     // Override with defaults
     if ($defaults) {
         foreach ($defaults as $name => $value) {
             $target->{$name} = $value;
         }
     }
     $parent_property = $this->get_parent_property($source);
     // Copy the link to parent
     if ($parent) {
         self::resolve_object($parent);
         if (!$parent || !$parent->guid) {
             return false;
         }
         // @TODO: Is there a sure way to determine if the parent is
         // GUID or is it ID? If so, please change it here.
         if (is_string($source->{$parent_property})) {
             $parent_key = 'guid';
         } else {
             $parent_key = 'id';
         }
         $target->{$parent_property} = $parent->{$parent_key};
     } else {
         if (is_string($source->{$parent_property})) {
             $target->{$parent_property} = '';
         } else {
             $target->{$parent_property} = 0;
         }
     }
     $name_property = midcom_helper_reflector::get_name_property($target);
     $resolver = new midcom_helper_reflector_nameresolver($target);
     if (!empty($name_property) && !$resolver->name_is_safe_or_empty($name_property)) {
         debug_add('Source object ' . get_class($source) . " {$source->guid} has unsafe name, rewriting to safe form for the target", MIDCOM_LOG_WARN);
         $name_property = midcom_helper_reflector::get_name_property($target);
         if (empty($name_property)) {
             $this->errors[] = sprintf($this->_l10n->get('cannot fix unsafe name for source object %s, skipping'), get_class($source) . " {$source->guid}");
             return false;
         }
         $name_parts = explode('.', $target->{$name_property}, 2);
         if (isset($name_parts[1])) {
             $target->{$name_property} = midcom_helper_misc::generate_urlname_from_string($name_parts[0]) . ".{$name_parts[1]}";
             // Doublecheck safety and fall back if needed
             if (!$resolver->name_is_safe_or_empty()) {
                 $target->{$name_property} = midcom_helper_misc::generate_urlname_from_string($target->{$name_property});
             }
         } else {
             $target->{$name_property} = midcom_helper_misc::generate_urlname_from_string($target->{$name_property});
         }
         unset($name_parts, $name_property);
     }
     if ($this->allow_name_catenate && $name_property) {
         $name = $resolver->generate_unique_name();
         if ($name !== $target->{$name_property}) {
             $target->{$name_property} = $name;
         }
     }
     // This needs to be here, otherwise it will be overridden
     $target->allow_name_catenate = true;
     if (!$target->create()) {
         $this->errors[] = $this->_l10n->get('failed to create object: ' . mgd_errstr());
         return false;
     }
     // Store for later use - if ever needed
     $this->new_objects[] = $target;
     unset($name_property);
     // Copy parameters
     if (!$this->copy_parameters($source, $target) && $this->halt_on_errors) {
         $this->errors[] = $this->_l10n->get('failed to copy parameters');
         return false;
     }
     // Copy metadata
     if (!$this->copy_metadata($source, $target) && $this->halt_on_errors) {
         $this->errors[] = $this->_l10n->get('failed to copy metadata');
         return false;
     }
     // Copy attachments
     if (!$this->copy_attachments($source, $target) && $this->halt_on_errors) {
         $this->errors[] = $this->_l10n->get('failed to copy attachments');
         return false;
     }
     // Copy privileges
     if (!$this->copy_privileges($source, $target) && $this->halt_on_errors) {
         $this->errors[] = $this->_l10n->get('failed to copy privileges');
         return false;
     }
     return $target;
 }
Esempio n. 9
0
 /**
  * Make sure our name is nice and clean
  *
  * @see http://trac.midgard-project.org/ticket/809
  */
 public function _on_validate()
 {
     $schema = $this->storage->_schema->fields[$this->name];
     $copy = $this->_copy_object($this->storage->object);
     $property = $schema['storage']['location'];
     $resolver = new midcom_helper_reflector_nameresolver($copy);
     if (empty($this->value)) {
         if (isset($this->_datamanager->types[$this->title_field]) && $this->_datamanager->types[$this->title_field]->value) {
             $copy->{$property} = midcom_helper_misc::generate_urlname_from_string($this->_datamanager->types[$this->title_field]->value);
             $this->value = $resolver->generate_unique_name();
         }
     }
     $copy->{$property} = $this->value;
     if (!$resolver->name_is_safe($property)) {
         $this->validation_error = sprintf($this->_l10n->get('type urlname: name is not "URL-safe", try "%s"'), midcom_helper_misc::generate_urlname_from_string($this->value));
         return false;
     }
     if (!$this->allow_unclean && !$resolver->name_is_clean($property)) {
         $this->validation_error = sprintf($this->_l10n->get('type urlname: name is not "clean", try "%s"'), midcom_helper_misc::generate_urlname_from_string($this->value));
         return false;
     }
     if (!$resolver->name_is_unique()) {
         $new_name = $resolver->generate_unique_name();
         if ($this->allow_catenate) {
             // If allowed to, silently use the generated name
             $this->value = $new_name;
             $this->_orig_value = $new_name;
             $copy->{$property} = $this->value;
         } else {
             $this->validation_error = sprintf($this->_l10n->get('type urlname: name is already taken, try "%s"'), $new_name);
             return false;
         }
     }
     return true;
 }
Esempio n. 10
0
 private function _create_topic($prefix)
 {
     if (!empty($this->_new_topic->symlink)) {
         $name = $this->_new_topic->name;
         $topic = $this->_new_topic;
         while (!empty($topic->symlink)) {
             // Only direct symlinks are supported, but indirect symlinks are ok as we change them to direct ones here
             $this->_new_topic->symlink = $topic->symlink;
             try {
                 $topic = new midcom_db_topic($topic->symlink);
             } catch (midcom_error $e) {
                 debug_add("Could not get target for symlinked topic #{$this->_new_topic->id}: " . $e->getMessage(), MIDCOM_LOG_ERROR);
                 $topic = $this->_new_topic;
                 $this->_new_topic->purge();
                 throw new midcom_error("Refusing to create this symlink because its target folder was not found: " . $e->getMessage());
             }
             $name = $topic->name;
         }
         if ($this->_new_topic->up == $topic->up) {
             $this->_new_topic->purge();
             throw new midcom_error("Refusing to create this symlink because it is located in the same " . "folder as its target. You must have made a mistake. Sorry, but this " . "was for your own good.");
         }
         if ($this->_new_topic->up == $topic->id) {
             $this->_new_topic->purge();
             throw new midcom_error("Refusing to create this symlink because its parent folder is the same " . "folder as its target. You must have made a mistake because this would " . "have created an infinite loop situation. The whole site would have " . "been completely and irrevocably broken if this symlink would have been " . "allowed to exist. Infinite loops can not be allowed. Sorry, but this " . "was for your own good.");
         }
         $this->_new_topic->update();
         if (!midcom_admin_folder_management::is_child_listing_finite($topic)) {
             $this->_new_topic->purge();
             throw new midcom_error("Refusing to create this symlink because it would have created an " . "infinite loop situation. The whole site would have been completely " . "and irrevocably broken if this symlink would have been allowed to " . "exist. Please redesign your usage of symlinks. Infinite loops can " . "not be allowed. Sorry, but this was for your own good.");
         }
         $this->_new_topic->name = $name;
         while (!$this->_new_topic->update() && midcom_connection::get_error() == MGD_ERR_DUPLICATE) {
             $this->_new_topic->name .= "-link";
         }
     }
     midcom::get('uimessages')->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder created'));
     // Generate name if it is missing
     if (!$this->_new_topic->name) {
         $this->_new_topic->name = midcom_helper_misc::generate_urlname_from_string($this->_new_topic->extra);
         $this->_new_topic->update();
     }
     // Get the relocation url
     $url = "{$prefix}{$this->_new_topic->name}/";
     return $url;
 }
Esempio n. 11
0
 public function import()
 {
     // Generate a safe name for the structure
     $structure_name = midcom_helper_misc::generate_urlname_from_string(midcom::get()->get_page_prefix());
     $path = "{$this->root_dir}{$structure_name}.inc";
     if (!file_exists($path)) {
         throw new midcom_error("Structure file {$path} not found");
     }
     $structuredata = file_get_contents($path);
     $structure = midcom_helper_misc::parse_config($structuredata);
     if (!is_array($structure) || !isset($structure[$structure_name]) || !isset($structure[$structure_name]['root'])) {
         throw new midcom_error("Invalid structure file {$path}");
     }
     $this->read_structure($structure[$structure_name]['root']);
 }
Esempio n. 12
0
 /**
  * Imports an item as a news article
  */
 private function import_article($item)
 {
     if ((empty($item['title']) || trim($item['title']) == '...') && empty($item['guid'])) {
         // Something wrong with this entry, skip it
         return false;
     }
     $guid_property = $this->_guid_property;
     $qb = midcom_db_article::new_query_builder();
     $qb->add_constraint('topic', '=', $this->_feed->node);
     // TODO: Move this to a parameter in Midgard 1.8
     $qb->add_constraint($guid_property, '=', substr($item['guid'], 0, 255));
     $articles = $qb->execute();
     if (count($articles) > 0) {
         // This item has been imported already earlier. Update
         $article = $articles[0];
     } else {
         // Check against duplicate hits that may come from different feeds
         if ($item['link']) {
             $qb = midcom_db_article::new_query_builder();
             $qb->add_constraint('topic', '=', $this->_feed->node);
             $qb->add_constraint('url', '=', $item['link']);
             $hits = $qb->count();
             if ($hits > 0) {
                 // Dupe, skip
                 return false;
             }
         }
         // This is a new item
         $article = new midcom_db_article();
     }
     // Sanity check
     if (!is_a($article, 'midcom_db_article')) {
         debug_add('$article is not an instance of midgard_article (or subclass), see debug level logs for object dump', MIDCOM_LOG_ERROR);
         debug_print_r('$article: ', $article);
         return false;
     }
     $article->allow_name_catenate = true;
     $updated = false;
     // Copy properties
     if ($article->title != $item['title']) {
         $article->title = $item['title'];
         $updated = true;
     }
     // FIXME: This breaks with URLs longer than 255 chars
     if ($article->{$guid_property} != $item['guid']) {
         $article->{$guid_property} = $item['guid'];
         $updated = true;
     }
     if ($article->content != $item['description']) {
         $article->content = $item['description'];
         $updated = true;
     }
     $article->topic = $this->_feed->node;
     if ($article->url != $item['link']) {
         $article->url = $item['link'];
         $updated = true;
     }
     $feed_category = 'feed:' . md5($this->_feed->url);
     $orig_extra1 = $article->extra1;
     $article->extra1 = "|{$feed_category}|";
     $article->_activitystream_verb = 'http://community-equity.org/schema/1.0/clone';
     $article->_rcs_message = sprintf(midcom::get('i18n')->get_string('%s was imported from %s', 'net.nemein.rss'), $article->title, $this->_feed->title);
     // Handle categories provided in the feed
     if (isset($item['category'])) {
         // Check if we have multiple categories
         if (is_array($item['category'])) {
             // Some systems provide multiple categories as per in spec
             $categories = $item['category'];
         } elseif (strstr($item['category'], ',')) {
             // Some systems expose multiple categories in single category element
             $categories = explode(',', $item['category']);
         } else {
             $categories = array();
             $categories[] = $item['category'];
         }
         foreach ($categories as $category) {
             // Clean up the categories and save
             $category = str_replace('|', '_', trim($category));
             $article->extra1 .= "{$category}|";
         }
     }
     if ($orig_extra1 != $article->extra1) {
         $updated = true;
     }
     // Try to figure out item author
     if ($this->_feed->forceauthor && $this->_feed->defaultauthor) {
         // Feed has a "default author" set, use it
         $article_author = new midcom_db_person($this->_feed->defaultauthor);
     } else {
         $article_author = $this->match_item_author($item);
         $fallback_person_id = 1;
         if (!$article_author || $article_author->id == $fallback_person_id) {
             if ($this->_feed->defaultauthor) {
                 // Feed has a "default author" set, use it
                 $article_author = new midcom_db_person($this->_feed->defaultauthor);
             } else {
                 // Fall back to "Midgard Admin" just in case
                 $fallback_author = new midcom_db_person($fallback_person_id);
                 $article_author = $fallback_author;
             }
         }
     }
     if (is_object($article_author) && $article_author->guid) {
         if ($article->metadata->authors != "|{$article_author->guid}|") {
             $article->metadata->set('authors', "|{$article_author->guid}|");
             $updated = true;
         }
     }
     // Try to figure out item publication date
     $article_date = null;
     if (isset($item['date_timestamp'])) {
         $article_date = $item['date_timestamp'];
     }
     $article_data_tweaked = false;
     if (!$article_date) {
         $article_date = time();
         $article_data_tweaked = true;
     }
     if ($article_date > $this->_feed->latestupdate) {
         // Cache "latest updated" time to feed
         $this->_feed->latestupdate = $article_date;
         $this->_feed->_use_activitystream = false;
         $this->_feed->_use_rcs = false;
         $this->_feed->update();
     }
     if ($article->id) {
         // store <link rel="replies"> url in parameter
         if (isset($item['link_replies'])) {
             $article->set_parameter('net.nemein.rss', 'replies_url', $item['link_replies']);
         }
         if ($article->metadata->published != $article_date && !$article_data_tweaked) {
             $article->metadata->published = $article_date;
             $updated = true;
         }
         // Safety, make sure we have sane name (the allow_catenate was set earlier, so this will not clash
         if (empty($article->name)) {
             $article->name = midcom_helper_misc::generate_urlname_from_string($article->title);
             $updated = true;
         }
         if (!$updated) {
             // No data changed, avoid unnecessary I/O
             return $article->guid;
         }
         $article->allow_name_catenate = true;
         if ($article->update()) {
             if ($this->_feed->autoapprove) {
                 $metadata = midcom_helper_metadata::retrieve($article);
                 $metadata->approve();
             }
             $this->parse_tags($article, $item);
             $this->parse_parameters($article, $item);
             return $article->guid;
         }
         return false;
     } else {
         // Safety, make sure we have sane name (the allow_catenate was set earlier, so this will not clash
         if (empty($article->name)) {
             $article->name = midcom_helper_misc::generate_urlname_from_string($article->title);
         }
         // This is a new item
         $node = new midcom_db_topic($this->_feed->node);
         $node_lang_code = $node->get_parameter('net.nehmer.blog', 'language');
         if ($node->get_parameter('net.nehmer.blog', 'symlink_topic') != '') {
             try {
                 $symlink_topic = new midcom_db_topic($node->get_parameter('net.nehmer.blog', 'symlink_topic'));
                 $article->topic = $symlink_topic->id;
             } catch (midcom_error $e) {
                 $e->log();
             }
         }
         if ($node_lang_code != '') {
             $lang_id = midcom::get('i18n')->code_to_id($node_lang_code);
             $article->lang = $lang_id;
         }
         $article->allow_name_catenate = true;
         if ($article->create()) {
             // store <link rel="replies"> url in parameter
             if (isset($item['link_replies'])) {
                 $article->set_parameter('net.nemein.rss', 'replies_url', $item['link_replies']);
             }
             // This should be unnecessary but leave it in place just  in case
             if (strlen($article->name) == 0) {
                 // Generate something to avoid empty "/" links in case of failures
                 $article->name = time();
             }
             $article->metadata->published = $article_date;
             $article->allow_name_catenate = true;
             $article->update();
             if ($this->_feed->autoapprove) {
                 $metadata = midcom_helper_metadata::retrieve($article);
                 $metadata->approve();
             }
             $this->parse_tags($article, $item);
             $this->parse_parameters($article, $item);
             return $article->guid;
         }
         return false;
     }
 }
Esempio n. 13
0
 /**
  * Rewrite a filename to URL safe form
  *
  * @param string $filename file name to rewrite
  * @param boolean $force_single_extension force file to single extension (defaults to true)
  * @return string rewritten filename
  * @todo add possibility to use the file utility to determine extension if missing.
  */
 public static function safe_filename($filename, $force_single_extension = true)
 {
     $filename = basename(trim($filename));
     if ($force_single_extension) {
         $regex = '/^(.*)(\\..*?)$/';
     } else {
         $regex = '/^(.*?)(\\..*)$/';
     }
     if (preg_match($regex, $filename, $ext_matches)) {
         $name = $ext_matches[1];
         $ext = $ext_matches[2];
     } else {
         $name = $filename;
         $ext = '';
     }
     return midcom_helper_misc::generate_urlname_from_string($name) . $ext;
 }
Esempio n. 14
0
 function import_file($title, $revision_path)
 {
     if (!is_readable($revision_path)) {
         return false;
     }
     if (!is_object($this->root_topic) || empty($this->root_topic->id)) {
         return false;
     }
     if (!$this->_datamanager) {
         return false;
     }
     if (!$this->_l10n) {
         return false;
     }
     $resolver = net_nemein_wiki_resolver($this->root_topic->id);
     // Make sure this is clean
     $this->add_parameters = array();
     $content = trim(file_get_contents($revision_path)) . "\n";
     $content = $this->moinmoin2markdown($content, $title);
     $resolved = $resolver->path_to_wikipage($title, true);
     echo "INFO: Importing '{$revision_path}' into '{$title}'<br/>\n";
     if (!empty($resolved['latest_parent'])) {
         $to_node =& $resolved['latest_parent'];
     } else {
         $to_node =& $resolved['folder'];
     }
     $created_page = false;
     switch (true) {
         case strstr($resolved['remaining_path'], '/'):
             // One or more namespaces left, find first, create it and recurse
             $paths = explode('/', $resolved['remaining_path']);
             $folder_title = array_shift($paths);
             echo "NOTICE: Creating new wiki topic '{$folder_title}' under #{$to_node[MIDCOM_NAV_ID]}<br/>\n";
             $topic = new midcom_db_topic();
             $topic->up = $to_node[MIDCOM_NAV_ID];
             $topic->extra = $folder_title;
             $topic->title = $folder_title;
             $topic->name = midcom_helper_misc::generate_urlname_from_string($folder_title);
             $topic->component = 'net.nemein.wiki';
             if (!$topic->create()) {
                 throw new midcom_error("could not create topic, error: " . midcom_connection::get_error_string());
             }
             $topic = new midcom_db_topic($topic->id);
             // Set the component
             $topic->parameter('midcom', 'component', 'net.nemein.wiki');
             // See if we have article with same title in immediate parent
             $qb = net_nemein_wiki_wikipage::new_query_builder();
             $qb->add_constraint('title', '=', $folder_title);
             $qb->add_constraint('topic', '=', $topic->up);
             $results = $qb->execute();
             if (is_array($results) && count($results) == 1) {
                 echo "INFO: Found page with same title in parent, moving to be index of this new topic<br/>\n";
                 $article =& $results[0];
                 $article->name = 'index';
                 $article->topic = $topic->id;
                 if (!$article->update()) {
                     // Could not move article, do something ?
                     echo "FAILURE: Could not move the page, errstr: " . midcom_connection::get_error_string() . "<br/>\n";
                 }
             } else {
                 $page = new net_nemein_wiki_wikipage();
                 $page->topic = $topic->id;
                 $page->name = 'index';
                 $page->title = $topic->extra;
                 $page->content = $this->_l10n->get('wiki default page content');
                 $page->author = midcom_connection::get_user();
                 if (!$page->create()) {
                     // Could not create index
                     $topic->delete();
                     throw new midcom_error("Could not create index for new topic, errstr: " . midcom_connection::get_error_string());
                 }
             }
             // We have created a new topic, now recurse to create the rest of the path.
             echo "INFO: New topic created with id #{$topic->id}, now recursing the import to process next levels<br/>\n";
             return $this->import_file($title, $revision_path);
             break;
         case is_object($resolved['wikipage']):
             // Page exists, create new revision
             echo "INFO: Updating wikipage #{$resolved['wikipage']->id}<br/>\n";
             $wikipage =& $resolved['wikipage'];
             break;
         default:
             // No more namespaces left, create the page to latest parent
             echo "INFO: Creating new wikipage '{$resolved['remaining_path']}' in topic #{$to_node[MIDCOM_NAV_ID]} <br/>\n";
             $wikipage = new net_nemein_wiki_wikipage();
             $wikipage->title = $resolved['remaining_path'];
             $wikipage->name = midcom_helper_misc::generate_urlname_from_string($resolved['remaining_path']);
             $wikipage->topic = $to_node[MIDCOM_NAV_ID];
             $wikipage->author = midcom_connection::get_user();
             if (!$wikipage->create()) {
                 echo "FAILURE: could not create wikipage object, error: " . midcom_connection::get_error_string() . "<br/>\n";
                 return false;
             }
             $wikipage = new net_nemein_wiki_wikipage($wikipage->id);
             $created_page =& $wikipage;
             $wikipage->set_parameter('midcom.helper.datamanager2', 'schema_name', 'default');
             break;
     }
     if (!$this->_datamanager->autoset_storage($wikipage)) {
         // DM2 initialization failure
         echo "FAILURE: Could not initialize DM2<br/>\n";
         if (is_object($created_page)) {
             // Clean up the just created page
             $created_page->delete();
         }
         return false;
     }
     $this->_datamanager->types['content']->value = $content;
     if (!$this->_datamanager->save()) {
         // DM2 save failure
         echo "FAILURE: DM2->save() failed, errstr: " . midcom_connection::get_error_string() . "<br/>\n";
         if (is_object($created_page)) {
             // Clean up the just created page
             $created_page->delete();
         }
         return false;
     }
     // Handle $this->add_object_parameters
     if (!empty($this->add_object_parameters)) {
         foreach ($this->add_object_parameters as $param) {
             $wikipage->set_parameter($param['domain'], $param['name'], $param['value']);
         }
     }
     echo "INFO: file imported OK<br/>\n";
     return true;
 }
Esempio n. 15
0
 function notify($type = 'update', $event = false, $nl = "\n")
 {
     $l10n = midcom::get('i18n')->get_l10n('org.openpsa.calendar');
     $recipient = $this->get_person_obj();
     if (!$recipient) {
         debug_add('recipient could not be gotten, aborting', MIDCOM_LOG_WARN);
         return false;
     }
     //In general we should have the event passed to us since we might be notifying about changes that have not been committed yet
     if (!$event) {
         $event = $this->get_event_obj();
     }
     if ($recipient->id == midcom_connection::get_user() && !$event->send_notify_me) {
         //Do not send notification to current user
         debug_add('event->send_notify_me is false and recipient is current user, aborting notify');
         return false;
     }
     $message = array();
     $action = 'org.openpsa.calendar:noevent';
     switch ($type) {
         //Event information was updated
         case 'update':
             //PONDER: This in theory should have the old event title
             $action = 'org.openpsa.calendar:event_update';
             $message['title'] = sprintf($l10n->get('event "%s" was updated'), $event->title);
             $message['abstract'] = sprintf($l10n->get('event "%s" (%s) was updated'), $event->title, $event->format_timeframe());
             $message['content'] = sprintf($l10n->get('event "%s" was modified, updated information below.') . "{$nl}{$nl}", $event->title);
             $message['content'] .= $event->details_text(false, $this, $nl);
             break;
             //Participant was added to the event
         //Participant was added to the event
         case 'add':
             $action = 'org.openpsa.calendar:event_add';
             $message['title'] = sprintf($l10n->get('you have been added to event "%s"'), $event->title);
             $message['abstract'] = sprintf($l10n->get('you have been added to event "%s" (%s)'), $event->title, $event->format_timeframe());
             $message['content'] = sprintf($l10n->get('you have been added to event "%s" participants list, event information below.') . "{$nl}{$nl}", $event->title);
             $message['content'] .= $event->details_text(false, $this, $nl);
             break;
             //Participant was removed from event
         //Participant was removed from event
         case 'remove':
             $action = 'org.openpsa.calendar:event_remove';
             $message['title'] = sprintf($l10n->get('you have been removed from event "%s"'), $event->title);
             $message['abstract'] = sprintf($l10n->get('you have been removed from event "%s" (%s)'), $event->title, $event->format_timeframe());
             $message['content'] = sprintf($l10n->get('you have been removed from event "%s" (%s) participants list.'), $event->title, $event->format_timeframe());
             break;
             //Event was cancelled (=deleted)
         //Event was cancelled (=deleted)
         case 'cancel':
             $action = 'org.openpsa.calendar:event_cancel';
             $message['title'] = sprintf($l10n->get('event "%s" was cancelled'), $event->title);
             $message['abstract'] = sprintf($l10n->get('event "%s" (%s) was cancelled'), $event->title, $event->format_timeframe());
             $message['content'] = sprintf($l10n->get('event "%s" (%s) was cancelled.'), $event->title, $event->format_timeframe());
             break;
         default:
             debug_add("action '{$type}' is invalid, aborting notification", MIDCOM_LOG_ERROR);
             return false;
     }
     if ($type == 'cancel' || $type == 'remove') {
         // TODO: Create iCal export with correct delete commands
     } else {
         $encoder = new org_openpsa_calendar_vcal();
         $vcal_data = $encoder->get_headers();
         $vcal_data .= $encoder->export_event($event);
         $vcal_data .= $encoder->get_footers();
         $message['attachments'] = array(array('name' => midcom_helper_misc::generate_urlname_from_string(sprintf('%s on %s', $event->title, date('Ymd_Hi', $event->start))) . '.ics', 'mimetype' => 'text/calendar', 'content' => $vcal_data));
     }
     return org_openpsa_notifications::notify($action, $recipient->guid, $message);
 }
Esempio n. 16
0
 private function _load_page($wikiword)
 {
     $qb = net_nemein_wiki_wikipage::new_query_builder();
     $qb->add_constraint('topic', '=', $this->_topic->id);
     $qb->add_constraint('name', '=', $wikiword);
     $result = $qb->execute();
     if (count($result) > 0) {
         $this->_page = $result[0];
         return true;
     }
     if ($wikiword == 'index') {
         // Autoinitialize
         $this->_topic->require_do('midgard:create');
         $this->_page = net_nemein_wiki_viewer::initialize_index_article($this->_topic);
         if ($this->_page) {
             return true;
         }
     }
     $topic_qb = midcom_db_topic::new_query_builder();
     $topic_qb->add_constraint('up', '=', $this->_topic->id);
     $topic_qb->add_constraint('name', '=', $wikiword);
     $topics = $topic_qb->execute();
     if (count($topics) > 0) {
         // There is a topic by this URL name underneath, go there
         return false;
     }
     // We need to get the node from NAP for safe redirect
     $nap = new midcom_helper_nav();
     $node = $nap->get_node($this->_topic->id);
     $urlized_wikiword = midcom_helper_misc::generate_urlname_from_string($wikiword);
     if ($urlized_wikiword != $wikiword) {
         // Lets see if the page for the wikiword exists
         $qb = net_nemein_wiki_wikipage::new_query_builder();
         $qb->add_constraint('topic', '=', $this->_topic->id);
         $qb->add_constraint('title', '=', $wikiword);
         $result = $qb->execute();
         if (count($result) > 0) {
             // This wiki page actually exists, so go there as "Permanent Redirect"
             midcom::get()->relocate("{$node[MIDCOM_NAV_ABSOLUTEURL]}{$result[0]->name}/", 301);
         }
     }
     midcom::get()->relocate("{$node[MIDCOM_NAV_ABSOLUTEURL]}notfound/" . rawurlencode($wikiword) . '/');
     // This will exit
 }
Esempio n. 17
0
 private function _check_unique_wikiword($wikiword)
 {
     $resolver = new net_nemein_wiki_resolver($this->_topic->id);
     $resolved = $resolver->path_to_wikipage($wikiword, true, true);
     if (!empty($resolved['latest_parent'])) {
         $to_node =& $resolved['latest_parent'];
     } else {
         $to_node =& $resolved['folder'];
     }
     $created_page = false;
     switch (true) {
         case strstr($resolved['remaining_path'], '/'):
             // One or more namespaces left, find first, create it and recurse
             $paths = explode('/', $resolved['remaining_path']);
             $folder_title = array_shift($paths);
             $topic = new midcom_db_topic();
             $topic->up = $to_node[MIDCOM_NAV_ID];
             $topic->extra = $folder_title;
             $topic->title = $folder_title;
             $topic->name = midcom_helper_misc::generate_urlname_from_string($folder_title);
             $topic->component = 'net.nemein.wiki';
             if (!$topic->create()) {
                 throw new midcom_error("Could not create wiki namespace '{$folder_title}', last Midgard error was: " . midcom_connection::get_error_string());
             }
             // refresh
             $topic = new midcom_db_topic($topic->id);
             // See if we have article with same title in immediate parent
             $qb = net_nemein_wiki_wikipage::new_query_builder();
             $qb->add_constraint('title', '=', $folder_title);
             $qb->add_constraint('topic', '=', $topic->up);
             $results = $qb->execute();
             if (is_array($results) && count($results) == 1) {
                 $article =& $results[0];
                 $article->name = 'index';
                 $article->topic = $topic->id;
                 if (!$article->update()) {
                     // Could not move article, do something ?
                 }
             } else {
                 $created_page = net_nemein_wiki_viewer::initialize_index_article($topic);
                 if (!$created_page) {
                     // Could not create index
                     $topic->delete();
                     throw new midcom_error("Could not create index for new topic, errstr: " . midcom_connection::get_error_string());
                 }
             }
             // We have created a new topic, now recurse to create the rest of the path.
             return $this->_check_unique_wikiword($wikiword);
             break;
         case is_object($resolved['wikipage']):
             // Page exists
             throw new midcom_error('Wiki page with that name already exists.');
             break;
         default:
             // No more namespaces left, create the page to latest parent
             if ($to_node[MIDCOM_NAV_ID] != $this->_topic->id) {
                 // Last parent is not this topic, redirect there
                 $wikiword_url = rawurlencode($resolved['remaining_path']);
                 midcom::get()->relocate($to_node[MIDCOM_NAV_FULLURL] . "create/{$this->_schema}?wikiword={$wikiword_url}");
                 // This will exit()
             }
             break;
     }
     return true;
 }
Esempio n. 18
0
 /**
  * Generates an unique name for the given object.
  *
  * 1st IF name is empty, we generate one from title (if title is empty too, we return false)
  * Then we check if it's unique, if not we add an incrementing
  * number to it (before this we make some educated guesses about a
  * good starting value)
  *
  * @param string $title_property, property of the object to use at title, if null will be reflected (see midcom_helper_reflector::get_object_title())
  * @param string $extension The file extension, when working with attachments
  * @return string string usable as name or boolean false on critical failures
  */
 public function generate_unique_name($title_property = null, $extension = '')
 {
     // Get current name and sanity-check
     $original_name = $this->get_object_name();
     if ($original_name === false) {
         // Fatal error with name resolution
         debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} returned critical failure for name resolution, aborting", MIDCOM_LOG_WARN);
         return false;
     }
     // We need the name of the "name" property later
     $name_prop = midcom_helper_reflector::get_name_property($this->_object);
     if (!empty($original_name)) {
         $current_name = (string) $original_name;
     } else {
         // Empty name, try to generate from title
         $title_copy = midcom_helper_reflector::get_object_title($this->_object, $title_property);
         if ($title_copy === false) {
             unset($title_copy);
             // Fatal error with title resolution
             debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} returned critical failure for title resolution when name was empty, aborting", MIDCOM_LOG_WARN);
             return false;
         }
         if (empty($title_copy)) {
             unset($title_copy);
             debug_add("Object " . get_class($this->_object) . " #{$this->_object->id} has empty name and title, aborting", MIDCOM_LOG_WARN);
             return false;
         }
         $current_name = midcom_helper_misc::generate_urlname_from_string($title_copy);
         unset($title_copy);
     }
     // incrementer, the number to add as suffix and the base name. see _generate_unique_name_resolve_i()
     list($i, $base_name) = $this->_generate_unique_name_resolve_i($current_name, $extension);
     $this->_object->name = $base_name;
     // decrementer, do not try more than this many times (the incrementer can raise above this if we start high enough.
     $d = 100;
     // The loop, usually we *should* hit gold in first try
     do {
         if ($i > 1) {
             // Start suffixes from -002
             $this->_object->{$name_prop} = $base_name . sprintf('-%03d', $i) . $extension;
         }
         // Handle the decrementer
         --$d;
         if ($d < 1) {
             // Decrementer undeflowed
             debug_add("Maximum number of tries exceeded, current name was: " . $this->_object->{$name_prop}, MIDCOM_LOG_ERROR);
             $this->_object->{$name_prop} = $original_name;
             unset($i, $d, $name_prop, $original_name, $base_name);
             return false;
         }
         // and the incrementer
         ++$i;
     } while (!$this->name_is_unique());
     unset($i, $d);
     // Get a copy of the current, usable name
     $ret = (string) $this->_object->{$name_prop};
     // Restore the original name
     $this->_object->{$name_prop} = $original_name;
     unset($name_prop, $original_name, $base_name);
     return $ret;
 }