Beispiel #1
0
 public function edit($mark_id = 0)
 {
     parent::redirectIfWebView();
     // Figure correct way to handle if no mark id
     if (empty($mark_id) || !is_numeric($mark_id)) {
         header('Location: /');
         exit;
     }
     // Check for CSRF
     if ($this->csrf_valid === true) {
         // Figure what options to send for update
         $options = array();
         // 1.6
         // If title is found, attach it
         if (isset($this->db_clean->title)) {
             $options['mark_title'] = $this->db_clean->title;
         }
         // If label ID is found, attach it
         if (isset($this->clean->label_id) && is_numeric($this->clean->label_id)) {
             $options['label_id'] = $this->clean->label_id;
         }
         // If notes are present set them
         if (isset($this->db_clean->notes)) {
             $options['notes'] = $this->db_clean->notes;
             // Check for hashmarks to tags
             $tags = getTagsFromHash($options['notes']);
         }
         // If tags are present, handle differently
         // Need to add to tags table first
         // Then create association
         // If notes are present set them
         if (isset($tags)) {
             parent::addTags($tags, $mark_id);
         }
         // Update users_to_marks record
         $mark = $this->user_marks->update("users_to_marks.user_id = '" . $this->user_id . "' AND users_to_marks.users_to_mark_id = '" . $mark_id . "'", $options);
         // Check if it was updated
         if ($mark === false) {
             $this->data['errors'] = formatErrors(3);
         } else {
             $this->data['mark'] = $mark;
             // Check if label id was set
             // if so get the parent mark id
             // Then add a smart label for this domain
             if (isset($options['label_id']) && !empty($options['label_id'])) {
                 $this->load->model('labels_model', 'labels');
                 $smart_info = getSmartLabelInfo($mark->url);
                 $total = $this->labels->count("labels.user_id = '" . $this->user_id . "' AND labels.smart_key = '" . $smart_info['key'] . "'");
                 // If not found, create it with label
                 // Else update current
                 if ($total < 1 && $options['label_id'] != '1') {
                     $label = $this->labels->create(array('smart_label_id' => $options['label_id'], 'domain' => $smart_info['domain'], 'smart_key' => $smart_info['key'], 'user_id' => $this->user_id));
                 } else {
                     $active = $options['label_id'] == '1' ? '0' : '1';
                     $label = $this->labels->update("labels.user_id = '" . $this->user_id . "' AND labels.smart_key = '" . $smart_info['key'] . "'", array('smart_label_id' => $options['label_id'], 'active' => $active));
                 }
             }
         }
     } else {
         $this->data['errors'] = formatErrors(600);
     }
     // Figure what to do here (api, redirect or generate view)
     $this->renderJSON();
 }
Beispiel #2
0
 public function up()
 {
     set_time_limit(0);
     // Check for all tables required for this migration
     parent::checkForTables(array('marks', 'migrations', 'users', 'users_marks', 'users_smartlabels'));
     // Check all columns need per table are found
     // marks, users and users_marks
     parent::checkForColumns(array('recipe', 'id', 'title', 'url', 'oembed', 'dateadded'), 'marks');
     parent::checkForColumns(array('salt', 'status', 'date_joined', 'admin'), 'users');
     parent::checkForColumns(array('addedby', 'groups', 'status', 'id', 'urlid', 'userid', 'tags', 'note', 'dateadded', 'datearchived'), 'users_marks');
     // Make sure all tables are INNODB, UTF-8
     // Original migration they were not
     // If anyone download that version and ran successfully, some keys may not be created correctly
     $this->db->query("ALTER TABLE `marks` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
     $this->db->query("ALTER TABLE `migrations` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
     $this->db->query("ALTER TABLE `users` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
     $this->db->query("ALTER TABLE `users_marks` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
     $this->db->query("ALTER TABLE `users_smartlabels` ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
     // Drop all group releated tables
     $this->db->query("DROP TABLE IF EXISTS `groups`");
     $this->db->query("DROP TABLE IF EXISTS `groups_invites`");
     $this->db->query("DROP TABLE IF EXISTS `users_groups`");
     /*
     - Start updates for labels table
     - Import default system labels
     - Import any user smart labels
     */
     // Create new labels table
     $this->db->query("\n        CREATE TABLE IF NOT EXISTS `labels` (\n          `label_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'The auto-incremented key.',\n          `smart_label_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If a smart label, the label_id to use if a match is found.',\n          `user_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'If a label but owned by a user, place the users.user_id here.',\n          `name` varchar(50) DEFAULT NULL COMMENT 'The name of the label.',\n          `slug` varchar(50) DEFAULT NULL COMMENT 'The slug of the label.',\n          `domain` varchar(255) DEFAULT NULL COMMENT 'The hostname of the domain to match. Keep in all lowercase.',\n          `path` varchar(100) DEFAULT NULL COMMENT 'The path to find to for smartlabels to match. If null, just match host.',\n          `smart_key` varchar(32) DEFAULT NULL COMMENT 'MD5 checksum of domain and path for lookup purposes.',\n          `active` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1 is active, 0 if not. Defaults to 1.',\n          `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',\n          `last_updated` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'The last datetime this record was updated.',\n          PRIMARY KEY (`label_id`),\n          CONSTRAINT `FK_label_smart_label_id` FOREIGN KEY (`smart_label_id`) REFERENCES `labels` (`label_id`)   ON UPDATE CASCADE ON DELETE CASCADE,\n          CONSTRAINT `FK_label_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)   ON UPDATE CASCADE ON DELETE CASCADE,\n          UNIQUE `slug`(slug),\n          INDEX `smart_label_id`(smart_label_id),\n          INDEX `user_id`(user_id),\n          INDEX `smart_key`(smart_key)\n        ) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;\n      ");
     // Add default data to labels
     // Default: Unlabeled, Read, Watch, Listen, Buy, Eat & Drink, Do
     // Then add all the smart labels
     $default_labels = array('unlabeled' => array('label_id' => 1, 'name' => 'Unlabeled'), 'read' => array('label_id' => 2, 'name' => 'Read'), 'watch' => array('label_id' => 3, 'name' => 'Watch'), 'listen' => array('label_id' => 4, 'name' => 'Listen'), 'buy' => array('label_id' => 5, 'name' => 'Buy'), 'eatdrink' => array('label_id' => 6, 'name' => 'Eat & Drink'), 'do' => array('label_id' => 7, 'name' => 'Do'));
     foreach ($default_labels as $label) {
         $slug = generateSlug($label['name']);
         $this->db->query("INSERT INTO `labels` (`label_id`, `name`, `slug`, `created_on`) VALUES ('" . $label['label_id'] . "', '" . $label['name'] . "', '" . $slug . "', '" . date('Y-m-d H:i:s') . "')");
     }
     // Start default system smart labels
     $smart_labels = array('2' => array('php.net', 'api.rubyonrails.org', 'ruby-doc.org', 'docs.jquery.com', 'codeigniter.com', 'css-tricks.com', 'developer.apple.com'), '3' => array('youtube.com', 'viddler.com', 'devour.com', 'ted.com', 'vimeo.com'), '5' => array('svpply.com', 'amazon.com', 'fab.com', 'zappos.com'), '6' => array('simplyrecipes.com', 'allrecipes.com', 'epicurious.com', 'foodnetwork.com', 'food.com'));
     // Loop thru smart labels and add them up
     foreach ($smart_labels as $label_id => $arr) {
         foreach ($arr as $domain) {
             $smart_info = getSmartLabelInfo('http://' . $domain);
             $this->db->query("\n              INSERT INTO `labels`\n              (`smart_label_id`, `domain`, `smart_key`, `created_on`)\n              VALUES ('" . $label_id . "', '" . $smart_info['domain'] . "', '" . $smart_info['key'] . "', '" . date('Y-m-d H:i:s') . "')\n            ");
         }
     }
     // Now open users_smartlabels, import into labels
     $labels = $this->db->query("SELECT userid, domain, path, label FROM users_smartlabels");
     if ($labels->num_rows() >= 1) {
         foreach ($labels->result() as $label) {
             // Proceed only if domain is not empty
             $current_label = strtolower($label->label);
             if (!empty($label->domain) && !empty($label->userid) && is_numeric($label->userid) && !empty($current_label) && array_key_exists($current_label, $default_labels)) {
                 // Standardize label domain, path and smart key
                 $smart_info = getSmartLabelInfo($label->domain . $label->path);
                 $domain = $smart_info['domain'];
                 $path = $smart_info['path'];
                 $md5 = $smart_info['key'];
                 $label_id = $default_labels[$current_label]['label_id'];
                 $path = empty($label->path) ? '' : "'" . $label->path . "', ";
                 $path_c = empty($label->path) ? '' : "`path`, ";
                 // Find an occurences of this record
                 $q = $this->db->query("\n              SELECT COUNT(*) FROM labels\n              WHERE user_id = '" . $label->userid . "'\n              AND domain = '" . $domain . "'\n              AND smart_label_id = '" . $label_id . "'\n            ");
                 $row = $q->row();
                 $total = (int) $row->{'COUNT(*)'};
                 // If not found, add it
                 if ($total < 1) {
                     $res = $this->db->query("\n                INSERT INTO `labels`\n                (`smart_label_id`, `user_id`, `domain`, " . $path_c . "`smart_key`, `created_on`)\n                VALUES ('" . $label_id . "', '" . $label->userid . "', '" . $domain . "', " . $path . "'" . $md5 . "', '" . date('Y-m-d H:i:s') . "')\n              ");
                 }
             }
         }
     }
     /*
     - End labels import
     */
     /*
     - Start updates for marks table
     */
     // Move all recipes to oembed column
     // oembed will be renamed embed for all embeddable content
     $marks = $this->db->query("SELECT id, recipe FROM `marks` WHERE recipe != '' AND LOWER(recipe) != 'none' AND recipe IS NOT NULL");
     if ($marks->num_rows() >= 1) {
         foreach ($marks->result() as $mark) {
             $res = $this->db->query("UPDATE `marks` SET `oembed` = '" . addslashes($mark->recipe) . "' WHERE `id` = '" . $mark->id . "'");
         }
     }
     // Update marks table
     $this->db->query("ALTER TABLE `marks` DROP COLUMN `recipe`");
     $this->db->query("ALTER TABLE `marks` CHANGE COLUMN `id` `mark_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'The auto-incremented id for marks.'");
     $this->db->query("ALTER TABLE `marks` DROP PRIMARY KEY, ADD PRIMARY KEY (`mark_id`)");
     $this->db->query("ALTER TABLE `marks` CHANGE COLUMN `title` `title` varchar(150) NOT NULL COMMENT 'The title from the page being bookmarked.'");
     $this->db->query("ALTER TABLE `marks` CHANGE COLUMN `url` `url` text NOT NULL COMMENT 'The full url from the page being bookmarked.'");
     $this->db->query("ALTER TABLE `marks` ADD COLUMN `url_key` varchar(32) DEFAULT NULL COMMENT 'The MD5 checksum of the url for lookup purposes.' AFTER `url`");
     $this->db->query("ALTER TABLE `marks` CHANGE COLUMN `oembed` `embed` text DEFAULT NULL COMMENT 'The embedded content that could appear on the mark\\'s info page.'");
     $this->db->query("ALTER TABLE `marks` CHANGE COLUMN `dateadded` `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'The datetime this record was created.'");
     $this->db->query("ALTER TABLE `marks` ADD COLUMN `embed_processed` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1 = yes, 0 if not. Defaults to 0.' AFTER `embed`");
     $this->db->query("ALTER TABLE `marks` ADD COLUMN `last_updated` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'The last datetime this record was updated.' AFTER `created_on`");
     // Update embed column to NULL if 'None'
     $res = $this->db->query("UPDATE `marks` SET embed = NULL WHERE LOWER(embed) = 'none'");
     // Set embed to processed if NOT NULL OR isn't in LAST DAY
     $res = $this->db->query("UPDATE `marks` SET embed_processed = '1' WHERE embed IS NOT NULL OR UNIX_TIMESTAMP(created_on) < '" . strtotime('today') . "'");
     // Get all urls from marks table, create MD5 checksum, update record
     $marks = $this->db->query("SELECT mark_id, url FROM `marks`");
     // If any exists, move on
     if ($marks->num_rows() >= 1) {
         // Set up empty arrays to hold marks that may get deleted or changed
         $deleted = array();
         $changed = array();
         // Loop thru results
         foreach ($marks->result() as $mark) {
             // If no url, remove it from DB and add to deleted list
             if (empty($mark->url)) {
                 $res = $this->db->query("DELETE FROM `marks` WHERE `mark_id` = '" . $mark->mark_id . "'");
                 array_push($deleted, $mark->mark_id);
             } else {
                 $checksum = md5($mark->url);
                 $record = $this->db->query("SELECT mark_id FROM `marks` WHERE `url_key` = '" . $checksum . "' LIMIT 1");
                 if ($record->num_rows() == 1) {
                     $record = $record->row();
                     $changed[$mark->mark_id] = $record->mark_id;
                     $res = $this->db->query("DELETE FROM `marks` WHERE `mark_id` = '" . $mark->mark_id . "'");
                 } else {
                     $res = $this->db->query("UPDATE `marks` SET `url_key` = '" . $checksum . "' WHERE `mark_id` = '" . $mark->mark_id . "'");
                 }
             }
         }
         // If the deleted array is not empty
         // Remove any user marks that are attached to it
         if (!empty($deleted)) {
             foreach ($deleted as $mark_id) {
                 $res = $this->db->query("DELETE FROM `users_marks` WHERE `urlid` = '" . $mark_id . "'");
             }
         }
         // If the changed array is not empty
         // Update any user marks from the old mark id to the new one
         if (!empty($changed)) {
             foreach ($changed as $old_id => $new_id) {
                 $res = $this->db->query("UPDATE `users_marks` SET `urlid` = '" . $new_id . "' WHERE `urlid` = '" . $old_id . "'");
             }
         }
     }
     // Finally, add a unique index for url key
     $this->db->query("ALTER TABLE `marks` ADD UNIQUE `url_key`(url_key)");
     /*
     - End updates for marks table
     */
     // Update users table
     // We are moving to active = 1 or 0 column, before altering table, switch active = 1, inactive = 0
     $res = $this->db->query("UPDATE `users` SET status = '0' WHERE status <> 'active'");
     $res = $this->db->query("UPDATE `users` SET status = '1' WHERE status = 'active'");
     $this->db->query("ALTER TABLE `users` DROP COLUMN `salt`");
     $this->db->query("ALTER TABLE `users` CHANGE COLUMN `status` `active` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1 = active, 0 = inactive' AFTER `password`");
     $this->db->query("ALTER TABLE `users` CHANGE COLUMN `date_joined` `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'The datetime the account was created'");
     $this->db->query("ALTER TABLE `users` CHANGE COLUMN `admin` `admin` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1 = admin, 0 = admin' AFTER `active`");
     $this->db->query("ALTER TABLE `users` ADD COLUMN `user_token` varchar(30) NOT NULL DEFAULT '0' COMMENT 'Unique user token.' AFTER `password`");
     // Add user tokens
     // Must check if they exist already
     $users = $this->db->query("SELECT user_id FROM `users`");
     if ($users->num_rows() >= 1) {
         foreach ($users->result() as $obj) {
             do {
                 $user_token = generateToken(30);
                 $res = $this->db->query("SELECT user_id FROM `users` WHERE user_token = '" . $user_token . "'");
             } while (isset($res->user_id));
             // Update user record
             $res = $this->db->query("UPDATE `users` SET user_token = '" . $user_token . "' WHERE user_id = '" . $obj->user_id . "'");
         }
     }
     // Update user token column to be no default
     // Add unique index
     $this->db->query("ALTER TABLE `users` CHANGE COLUMN `user_token` `user_token` varchar(30) NOT NULL COMMENT 'Unique user token.'");
     $this->db->query("ALTER TABLE `users` ADD UNIQUE `user_token`(user_token)");
     // Remove the users_smartlabels table
     $this->db->query("DROP TABLE IF EXISTS `users_smartlabels`");
     /*
     - Start users_to_marks transistion
     */
     // Update `tags` field to a single numeric in order to translate to a FK for labels
     // Move group id to user_marks_to_groups table
     $marks = $this->db->query("SELECT id, urlid, tags, groups, status FROM `users_marks`");
     $archived = array();
     $delete = array();
     if ($marks->num_rows() >= 1) {
         // Loop thru results
         foreach ($marks->result() as $mark) {
             $tags = explode(',', $mark->tags);
             $label_id = 1;
             $group_id = $mark->groups;
             $mark_id = $mark->id;
             // Figure if we should delete this mark
             $res = $this->db->query("SELECT mark_id FROM marks WHERE mark_id = '" . $mark->urlid . "' LIMIT 1");
             if ($res->num_rows() < 1) {
                 array_push($delete, $mark_id);
             }
             // If it was archived, save here to update later
             if (strtolower($mark->status) == 'archive') {
                 array_push($archived, $mark_id);
             }
             foreach ($tags as $tag) {
                 $tag = trim(strtolower($tag));
                 if (array_key_exists($tag, $default_labels)) {
                     $label_id = $default_labels[$tag]['label_id'];
                     break;
                 }
             }
             // Update tags
             $res = $this->db->query("UPDATE `users_marks` SET tags = '" . $label_id . "' WHERE id = '" . $mark_id . "'");
         }
     }
     // Remove any urlids in users_marks that do NOT exist in marks table
     // If we skip this step the FK creation will fail
     // Data is a bit dirty because when marks were deleted, it never deleted their children, leaving little orphan annies
     // Fix it!
     if (!empty($delete)) {
         foreach ($delete as $id) {
             $res = $this->db->query("DELETE FROM `users_marks` WHERE id = '" . $id . "'");
         }
     }
     // Update table name & structure
     $this->db->query("RENAME TABLE `users_marks` TO `users_to_marks`");
     $this->db->query("ALTER TABLE `users_to_marks` DROP COLUMN `addedby`");
     $this->db->query("ALTER TABLE `users_to_marks` DROP COLUMN `groups`");
     $this->db->query("ALTER TABLE `users_to_marks` DROP COLUMN `status`");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `id` `users_to_mark_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'The auto-incremented key.'");
     $this->db->query("ALTER TABLE `users_to_marks` DROP PRIMARY KEY, ADD PRIMARY KEY (`users_to_mark_id`)");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `urlid` `mark_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The mark_id from marks.mark_id.' AFTER `users_to_mark_id`");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `userid` `user_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The user_id from users.user_id' AFTER `mark_id`");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `tags` `label_id` bigint(20) UNSIGNED NOT NULL DEFAULT '1' COMMENT 'The label_id from labels.label_id.'");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `note` `notes` text DEFAULT NULL");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `dateadded` `created_on` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'The datetime this record was created.'");
     $this->db->query("ALTER TABLE `users_to_marks` CHANGE COLUMN `datearchived` `archived_on` datetime DEFAULT NULL COMMENT 'The datetime this mark was archived. NULL = not archived.'");
     $this->db->query("ALTER TABLE `users_to_marks` ADD COLUMN `last_updated` timestamp NOT NULL ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'The last datetime this record was updated.' AFTER `archived_on`");
     $this->db->query("ALTER TABLE `users_to_marks` ADD INDEX `mark_id`(mark_id)");
     $this->db->query("ALTER TABLE `users_to_marks` ADD INDEX `user_id`(user_id)");
     $this->db->query("ALTER TABLE `users_to_marks` ADD INDEX `label_id`(label_id)");
     $this->db->query("ALTER TABLE `users_to_marks` ADD CONSTRAINT `FK_utm_mark_id` FOREIGN KEY (`mark_id`) REFERENCES `marks` (`mark_id`)   ON UPDATE CASCADE ON DELETE CASCADE");
     $this->db->query("ALTER TABLE `users_to_marks` ADD CONSTRAINT `FK_utm_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)   ON UPDATE CASCADE ON DELETE CASCADE");
     $this->db->query("ALTER TABLE `users_to_marks` ADD CONSTRAINT `FK_utm_label_id` FOREIGN KEY (`label_id`) REFERENCES `labels` (`label_id`)   ON UPDATE CASCADE ON DELETE CASCADE");
     // Fix archived marks
     // First set all date_archived fields to NULL properly
     $res = $this->db->query("UPDATE `users_to_marks` SET archived_on = NULL WHERE archived_on IS NOT NULL");
     if (!empty($archived)) {
         foreach ($archived as $users_to_mark_id) {
             $res = $this->db->query("UPDATE `users_to_marks` SET archived_on = '" . date('Y-m-d H:i:s') . "' WHERE users_to_mark_id = '" . $users_to_mark_id . "'");
         }
     }
     /*
     - End users_to_marks transistion
     */
     // Create new tags table
     $this->db->query("\n        CREATE TABLE `tags` (\n          `tag_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'The auto-incremented key fro tags.',\n          `name` varchar(100) NOT NULL COMMENT 'The tag name.',\n          `slug` varchar(100) NOT NULL COMMENT 'The tag slug name.',\n          PRIMARY KEY (`tag_id`),\n          INDEX `name`(name),\n          UNIQUE `slug`(slug)\n        ) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;\n      ");
     // Create new user_marks_to_tags table
     $this->db->query("\n        CREATE TABLE `user_marks_to_tags` (\n          `user_marks_to_tag_id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'The auto-incremented key.',\n          `tag_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The tag id from tags.tag_id',\n          `user_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The user id from users.user_id',\n          `users_to_mark_id` bigint(20) UNSIGNED NOT NULL COMMENT 'The user mark id from users_to_marks.users_to_mark_id',\n          PRIMARY KEY (`user_marks_to_tag_id`),\n          INDEX `users_to_mark_id`(users_to_mark_id),\n          INDEX `tag_id`(tag_id),\n          INDEX `user_id`(user_id),\n          CONSTRAINT `FK_umtt_tag_id` FOREIGN KEY (`tag_id`) REFERENCES `tags` (`tag_id`)   ON UPDATE CASCADE ON DELETE CASCADE,\n          CONSTRAINT `FK_umtt_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`)   ON UPDATE CASCADE ON DELETE CASCADE,\n          CONSTRAINT `FK_umtt_mark_id` FOREIGN KEY (`users_to_mark_id`) REFERENCES `users_to_marks` (`users_to_mark_id`)   ON UPDATE CASCADE ON DELETE CASCADE\n        ) ENGINE=`InnoDB` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;\n      ");
 }
Beispiel #3
0
 protected function addMark($data = array())
 {
     $this->load->helper('http_build_url');
     // Set empty $result
     $result = array();
     // Set default view & redirect
     $view = null;
     $redirect = null;
     // Figure the url and title
     $url = isset($data['url']) ? $data['url'] : null;
     $title = isset($data['title']) ? $data['title'] : null;
     // If url or title are empty
     // error out
     if (empty($url)) {
         return formatErrors(8);
     }
     // 1.6
     // Remove utm_* from URLs before saving
     // Just grab the querystring
     $url_query_parameters = parse_url($url, PHP_URL_QUERY);
     // If utm_ is found, convert to array
     // Notice the _... so that words with utm do not get included
     if (strpos($url_query_parameters, 'utm_') === false) {
         // Whoops! echo '<br>No utm_ found.<br>';
     } else {
         parse_str($url_query_parameters, $url_query_parameters);
         // convert querystring to array
         // Prepare a new query string
         $new_query_string = array();
         // Loop through and reconstruct the querystring, leaving off anything with utm_ in key
         foreach ($url_query_parameters as $key => $value) {
             if (strpos($key, 'utm_') === false) {
                 $new_query_string[] = $key . '=' . $value;
             }
         }
         $new_query_string = implode('&', $new_query_string);
         $url = http_build_url($url, array('query' => $new_query_string));
     }
     // Sometimes browsers will not have a title
     // for things like a PDF or JPG.
     // In these instances, we still want people
     // to be able to save their marks. ;-)
     if (empty($title)) {
         $parsed_url_path = parse_url($url, PHP_URL_PATH);
         // If a path is found at all... use that for title instead
         if (!empty($parsed_url_path)) {
             $title = $parsed_url_path;
         }
     }
     if (empty($title)) {
         return formatErrors(9);
     }
     // Add mark to marks table
     $this->load->model('marks_model', 'mark');
     $mark = $this->mark->create(array('title' => $title, 'url' => $url));
     // Check ish
     if ($mark === false) {
         $user_mark = formatErrors(6);
     } elseif (!isset($mark->mark_id)) {
         $user_mark = $mark;
     } else {
         $this->load->model('users_to_marks_model', 'user_marks');
         $user_mark = $this->user_marks->readComplete("users_to_marks.user_id = '" . $this->user_id . "' AND users_to_marks.mark_id = '" . $mark->mark_id . "' AND users_to_marks.active = '1'");
         // Add
         if (!isset($user_mark->mark_id)) {
             // Set default options
             $options = array('user_id' => $this->user_id, 'mark_id' => $mark->mark_id);
             // Label ID (not required), if set and numeric and greater than 0, use it
             if (isset($data['label_id']) && is_numeric($data['label_id']) && $data['label_id'] > 0) {
                 $options['label_id'] = $data['label_id'];
             }
             // Notes (not required)
             if (isset($data['notes']) && !empty($data['notes'])) {
                 $options['notes'] = $data['notes'];
                 $tags = getTagsFromHash($options['notes']);
             }
             // Figure if any automatic labels should be applied
             $smart_info = getSmartLabelInfo($url);
             if (isset($smart_info['key']) && !empty($smart_info['key']) && !isset($options['label_id'])) {
                 // Load labels model
                 // Sort by user_id DESC (if user has same rule as system, use the user's rule)
                 // Try to extract label
                 $this->load->model('labels_model', 'labels');
                 $this->labels->sort = 'user_id DESC';
                 $label = $this->labels->readComplete("(labels.user_id IS NULL OR labels.user_id = '" . $this->user_id . "') AND labels.smart_key = '" . $smart_info['key'] . "' AND labels.active = '1'", 1);
                 // If a label id is found
                 // Set it to options to save
                 if (isset($label->settings->label->id)) {
                     $options['label_id'] = $label->settings->label->id;
                 }
             }
             // Create the mark
             $user_mark = $this->user_marks->create($options);
         }
         if ($user_mark === false) {
             $user_mark = formatErrors(6);
         } elseif (isset($user_mark->mark_id)) {
             // If tags are present, add them
             // Get updated result
             if (isset($tags)) {
                 $mark_id = $user_mark->mark_id;
                 self::addTags($tags, $mark_id);
                 $user_mark = $this->user_marks->readComplete($mark_id);
             }
         }
     }
     return $user_mark;
 }
Beispiel #4
0
 /**
  * Import mark object into system
  *
  * @param stdObj $markObject
  *            Mark data imported from file
  * @return array Result array
  */
 public function importMark($markObject)
 {
     $result = array();
     $this->CI->load->helper('data_helper');
     // Run in transaction
     $this->CI->db->trans_start();
     if ($this->importData['meta']['export_version'] == 1) {
         $this->CI->load->model('marks_model', 'mark');
         $markArray = array('created_on' => $markObject->created_on, 'title' => empty($markObject->title) ? 'No title' : $markObject->title, 'url' => $markObject->url, 'embed' => $markObject->embed);
         // Import mark object
         $mark = $this->CI->mark->import($markArray);
         // Succesfully created mark
         if ($mark !== false && isset($mark->mark_id)) {
             // Try to create user_mark and other related records
             $this->CI->load->model('users_to_marks_model', 'user_marks');
             $user_mark = $this->CI->user_marks->readComplete("users_to_marks.user_id = '" . $this->importData['user_id'] . "' AND users_to_marks.mark_id = '" . $mark->mark_id . "' AND users_to_marks.active = '1'");
             // User mark does not exist - add one
             if (!isset($user_mark->mark_id)) {
                 // Set default options
                 $options = array('user_id' => $this->importData['user_id'], 'mark_id' => $mark->mark_id, 'active' => $markObject->active, 'archived_on' => $markObject->archived_on, 'created_on' => $markObject->created_on);
                 // Label ID (not required)
                 if (isset($markObject->label_id) && is_numeric($markObject->label_id)) {
                     $this->CI->load->model('labels_model', 'labels');
                     $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id='" . $this->importData['user_id'] . "') AND labels.active='1' AND labels.name = " . $this->CI->db->escape($markObject->label_name), 1);
                     if (!empty($label) && isset($label->label_id)) {
                         $options['label_id'] = $label->label_id;
                     } else {
                         if (!empty($this->unlabeled_label_id)) {
                             $options['label_id'] = $this->unlabeled_label_id;
                             $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Marked as Unlabeled.';
                         } else {
                             if ($this->unlabeled_label_id === false) {
                                 $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Stripped label info.';
                             } else {
                                 // Label not found and no unlabeled cache - looking for unlabeled label id
                                 $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id='" . $this->importData['user_id'] . "') AND labels.active='1' AND labels.name = " . $this->CI->db->escape('Unlabeled'), 1);
                                 if (!empty($label) && isset($label->label_id)) {
                                     $options['label_id'] = $label->label_id;
                                     // Cache the id of unlabeled label id
                                     $this->unlabeled_label_id = $label->label_id;
                                     $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Marked as Unlabeled.';
                                 } else {
                                     // There is no unlabeled label - cache invalid value to mark
                                     $this->unlabeled_label_id = false;
                                     $result['warnings'][] = 'Label ' . $markObject->label_name . ' not found. Stripped label info.';
                                 }
                             }
                         }
                     }
                 }
                 // Notes (not required)
                 if (isset($markObject->notes) && !empty($markObject->notes)) {
                     $options['notes'] = $markObject->notes;
                     $tags = getTagsFromHash($options['notes']);
                 }
                 // Figure if any automatic labels should be applied
                 $smart_info = getSmartLabelInfo($markObject->url);
                 if (isset($smart_info['key']) && !empty($smart_info['key']) && !isset($options['label_id'])) {
                     // Load labels model
                     // Sort by user_id DESC (if user has same rule as system, use the user's rule)
                     // Try to extract label
                     $this->CI->load->model('labels_model', 'labels');
                     $this->CI->labels->sort = 'user_id DESC';
                     $label = $this->CI->labels->readComplete("(labels.user_id IS NULL OR labels.user_id = '" . $this->importData['user_id'] . "') AND labels.smart_key = '" . $smart_info['key'] . "' AND labels.active = '1'", 1);
                     // If a label id is found
                     // Set it to options to save
                     if (isset($label->settings->label->id)) {
                         $options['label_id'] = $label->settings->label->id;
                     }
                 }
                 // Create the mark
                 $user_mark = $this->CI->user_marks->import($options);
                 $result['result'] = 'added';
             } else {
                 $result['result'] = 'skipped';
             }
             // Added user mark
             if (isset($user_mark->mark_id)) {
                 // If tags are present, add them
                 // Get updated result
                 if (isset($tags)) {
                     self::addTags($tags, $user_mark->mark_id);
                 }
             }
         } else {
             if ($mark !== false) {
                 foreach ($mark as $errorCode => $errorMessage) {
                     $result['errors'][] = array('error_code' => $errorCode, 'error_message' => $errorMessage);
                 }
             } else {
                 $result['errors'][] = formatErrors(500);
             }
         }
     } else {
         $result['errors'][] = array('error_message' => 'Invalid data format ' . $this->importData['meta']['export_version']);
     }
     $this->CI->db->trans_complete();
     // Check if DB operations succeeded
     if ($this->CI->db->trans_status() === FALSE) {
         // Internal error
         $result['errors'][] = formatErrors(500);
     }
     if (!empty($result['errors'])) {
         $result['result'] = 'failed';
     }
     return $result;
 }
Beispiel #5
0
 public function edit($label_id = 0)
 {
     // Figure correct way to handle if no mark id
     if (empty($label_id) || !is_numeric($label_id)) {
         $this->data['errors'] = formatErrors(30);
     } else {
         // Set the columns that CAN be updated
         $types = array('label', 'smart');
         // Lookup label_id to get type
         $user_where = parent::isAdmin() === true && isset($this->clean->admin) && !empty($this->clean->admin) ? "labels.user_id IS NULL" : "labels.user_id = '" . $this->user_id . "'";
         $where = "(labels.user_id IS NULL AND labels.label_id= '" . $label_id . "' AND labels.smart_label_id IS NULL) OR (" . $user_where . " AND labels.label_id= '" . $label_id . "' AND labels.smart_label_id IS NOT NULL)";
         $label = $this->labels->readComplete($where, 1);
         // If no type found, there was no label for this id/user combo
         if (!isset($label->type)) {
             $this->data['errors'] = formatErrors(31);
         } elseif ($label->type == 'label' && parent::isAdmin() === false) {
             $this->data['errors'] = formatErrors(37);
         } elseif (!array_key_exists($label->type, $types)) {
             $this->data['errors'] = formatErrors(33);
         } else {
             $options = array();
             $total = 0;
             // Figure options per label type
             if ($label->type == 'smart') {
                 if (isset($this->db_clean->url) && !empty($this->db_clean->url)) {
                     $smart_info = getSmartLabelInfo($this->db_clean->url);
                     $options['domain'] = $smart_info['domain'];
                     $options['path'] = $smart_info['path'];
                     $options['smart_key'] = $smart_info['key'];
                     $total = $this->labels->count($user_where . " AND labels.smart_key = '" . $options['smart_key'] . "'");
                 }
                 // If label is sent, add to options
                 if (isset($this->db_clean->label_id) && !is_numeric($this->db_clean->label_id)) {
                     $options['smart_label_id'] = $this->db_clean->label_id;
                 }
             } else {
                 if (isset($this->db_clean->name) && !empty($this->db_clean->name)) {
                     $options['name'] = $this->db_clean->name;
                     $options['slug'] = generateSlug($options['name']);
                     $total = $this->labels->count("labels.user_id IS NULL AND labels.slug = '" . $options['slug'] . "'");
                 }
             }
             // Figure if there was an active state passed
             if (isset($this->db_clean->active) && ($this->db_clean->active == 0 || $this->db_clean->active == 1)) {
                 $options['active'] = $this->db_clean->active;
             }
             // If no options, return error
             if (empty($options)) {
                 $this->data['errors'] = formatErrors(35);
             } elseif ($total > 0) {
                 $this->data['errors'] = formatErrors(34);
             } else {
                 $label = $this->labels->update($where, $options);
                 // If update failed, tell the user
                 if ($label === false) {
                     $this->data['errors'] = formatErrors(39);
                 } else {
                     $this->data['label'] = $label;
                 }
             }
         }
     }
     // Figure view
     $this->figureView();
 }