1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153 |
- <?php
- /**
- * @file
- * Main file for Linkit module.
- */
- /**
- * Define that urls should be constructed as it is (raw).
- */
- define('LINKIT_URL_METHOD_RAW', 1);
- /**
- * Define that urls should be constructed as it is (raw) with a prepending
- * slash.
- */
- define('LINKIT_URL_METHOD_RAW_SLASH', 2);
- /**
- * Define that paths should be constructed as an alias.
- */
- define('LINKIT_URL_METHOD_ALIAS', 3);
- /**
- * Define which query string key Better autocomplete will just.
- */
- define('LINKIT_BAC_QUERY_KEY', 's');
- /**
- * Define the minimum number of characters to perform server polling.
- */
- define('LINKIT_CHAR_LIMIT', 3);
- /**
- * Define the time from last key press to poll the server.
- */
- define('LINKIT_WAIT', 350);
- /**
- * Define the client side timeout for a request to the server.
- */
- define('LINKIT_REMOTE_TIMEOUT', 10000);
- /**
- * Define the profile type editor.
- */
- define('LINKIT_PROFILE_TYPE_EDITOR', 1);
- /**
- * Define the profile type field.
- */
- define('LINKIT_PROFILE_TYPE_FIELD', 2);
- // Include the file with the field functions.
- require_once dirname(__FILE__) . '/linkit.field.inc';
- /**
- * Implements hook_permission().
- */
- function linkit_permission() {
- return array(
- 'administer linkit' => array(
- 'title' => t('Administer Linkit'),
- 'description' => t('Perform administration tasks for Linkit.'),
- ),
- );
- }
- /**
- * Implements hook_ctools_plugin_directory().
- */
- function linkit_ctools_plugin_directory($module, $plugin) {
- if ($module == 'ctools' && !empty($plugin)) {
- return "plugins/" . $plugin;
- }
- if ($module == 'linkit' && !empty($plugin)) {
- return "plugins/" . $plugin;
- }
- }
- /**
- * Implements hook_ctools_plugin_TYPE() to inform the plugin system that Linkit
- * owns Linkit plugin types.
- */
- function linkit_ctools_plugin_type() {
- $plugins['linkit_search'] = array(
- 'child plugins' => TRUE,
- 'classes' => array('handler'),
- );
- $plugins['linkit_insert'] = array(
- 'process' => array(
- 'function' => 'linkit_insert_plugin_process',
- ),
- );
- $plugins['linkit_attribute'] = array();
- return $plugins;
- }
- /**
- * Provide defaults for insert plugins.
- */
- function linkit_insert_plugin_process(&$plugin, $info) {
- // The plugin javascript can be just a filename or a full path,
- // in case it's just a filename, add the plugin path.
- if (!file_exists($plugin['javascript'])) {
- $plugin['javascript'] = $plugin['path'] . '/' . $plugin['javascript'];
- }
- }
- /**
- * Load a single Linkit profile.
- *
- * @param $name
- * A string with the name of the profile to load.
- *
- * @return
- * A LinkitProfile object or FALSE if no profile is found.
- */
- function linkit_profile_load($name) {
- ctools_include('export');
- $result = ctools_export_load_object('linkit_profiles', 'names', array($name));
- if (isset($result[$name])) {
- return $result[$name];
- }
- return FALSE;
- }
- /**
- * Load all Linkit profiles.
- *
- * @return
- * An array with LinkitProfile objects.
- */
- function linkit_profile_load_all() {
- ctools_include('export');
- $profiles = ctools_export_load_object('linkit_profiles');
- return $profiles;
- }
- /**
- * Load all Linkit profiles that is for fields.
- *
- * @return
- * An array with LinkitProfile objects.
- */
- function linkit_profile_field_load_all() {
- $profiles = linkit_profile_load_all();
- foreach ($profiles as &$profile) {
- if ($profile->profile_type != LINKIT_PROFILE_TYPE_FIELD) {
- $profile = FALSE;
- }
- }
- return array_filter($profiles);
- }
- /**
- * Load all Linkit profiles that is for editors.
- *
- * @return
- * An array with LinkitProfile objects.
- */
- function linkit_profile_editor_load_all() {
- $profiles = linkit_profile_load_all();
- foreach ($profiles as &$profile) {
- if ($profile->profile_type != LINKIT_PROFILE_TYPE_EDITOR) {
- $profile = FALSE;
- }
- }
- return array_filter($profiles);
- }
- /**
- * Temporary saves the active profile. Active means that the user is working
- * with the profile in the dialog.
- *
- * @param string LinkitProfile $profile
- * A LinkitProfile object.
- */
- function linkit_set_active_profile(LinkitProfile $profile) {
- $active_profile = &drupal_static('linkit_active_profile');
- $active_profile = $profile;
- }
- /**
- * Get the currently active profile.
- *
- * @return
- * A LinkitProfile object if there is one set.
- *
- * @see linkit_set_active_profile()
- */
- function linkit_get_active_profile() {
- return drupal_static('linkit_active_profile');
- }
- /**
- * Fetch metadata for all Linkit search plugins.
- *
- * @return
- * An array of arrays with information about all available Linkit search
- * plugins.
- */
- function linkit_search_plugin_load_all() {
- ctools_include('plugins');
- $plugins = ctools_get_plugins('linkit', 'linkit_search');
- // If you alter the plugin handler, be sure the new handler is registerd or
- // you include it in some other way.
- drupal_alter('linkit_search_plugins', $plugins);
- return $plugins;
- }
- /**
- * Function used by uasort to sort plugins by weight.
- */
- function linkit_sort_plugins_by_weight($a, $b) {
- return $a["weight"] >= $b["weight"];
- }
- /**
- * Fetch metadata for one Linkit search plugin by the given name.
- *
- * @param $plugin_name
- * A string with the name of the plugin to load.
- *
- * @return
- * An array with information about the search plugin.
- */
- function linkit_search_plugin_load($plugin_name) {
- ctools_include('plugins');
- $plugin = ctools_get_plugins('linkit', 'linkit_search', $plugin_name);
- // If you alter the plugin handler, be sure the new handler is registerd or
- // you include it in some other way.
- drupal_alter('linkit_search_plugin', $plugin);
- return $plugin;
- }
- /**
- * Fetch metadata for all Linkit insert plugins.
- *
- * @return
- * An array of arrays with information about all available insert plugins.
- */
- function linkit_insert_plugin_load_all() {
- ctools_include('plugins');
- // Load all insert plugins.
- $plugins = ctools_get_plugins('linkit', 'linkit_insert');
- return $plugins;
- }
- /**
- * Fetch metadata for one Linkit insert plugin by the given name.
- *
- * @param $plugin_name
- * A string with the name of the plugin to load.
- *
- * @return
- * An array with information about the insert plugin.
- */
- function linkit_insert_plugin_load($plugin_name) {
- ctools_include('plugins');
- // Load all insert plugins.
- $plugins = ctools_get_plugins('linkit', 'linkit_insert', $plugin_name);
- return $plugins;
- }
- /**
- * Fetch metadata for all Linkit attribute plugins.
- *
- * @return
- * An array of arrays with information about all available attribute plugins.
- */
- function linkit_attribute_plugin_load_all() {
- ctools_include('plugins');
- // Load all attribute plugins.
- $attributes = ctools_get_plugins('linkit', 'linkit_attribute');
- return $attributes;
- }
- function linkit_attribute_plugin_load($plugin_name) {
- ctools_include('plugins');
- // Load all insert plugins.
- $plugins = ctools_get_plugins('linkit', 'linkit_attribute', $plugin_name);
- return $plugins;
- }
- /**
- * Implements hook_theme().
- */
- function linkit_theme($existing, $type, $theme, $path) {
- return array(
- 'linkit_plugin_form_table' => array(
- 'render element' => 'form',
- 'file' => 'includes/theme.inc',
- ),
- );
- }
- /**
- * Implements hook_menu_alter().
- */
- function linkit_menu_alter(&$items) {
- // Override the default titles that ctools export_ui sets.
- // This way there is less code compared to define this in the plugin array.
- $items['admin/config/content/linkit/add']['title'] = 'Add new profile';
- $items['admin/config/content/linkit/import']['title'] = 'Import profiles';
- // Make tabs instead of action links.
- $items['admin/config/content/linkit/add']['type'] = MENU_LOCAL_TASK;
- $items['admin/config/content/linkit/import']['type'] = MENU_LOCAL_TASK;
- }
- /**
- * Implements hook_module_implements_alter().
- *
- * @TODO: Document why we are doing this.
- * @see linkit_element_info_alter()
- */
- function linkit_module_implements_alter(&$implementations, $hook) {
- if ($hook == 'element_info_alter') {
- $group = $implementations['linkit'];
- unset($implementations['linkit']);
- $implementations['linkit'] = $group;
- }
- }
- /**
- * Implements hook_element_info_alter().
- */
- function linkit_element_info_alter(&$types) {
- // Append a process function for the field integration.
- foreach (linkit_get_allowed_field_elements() as $element) {
- if (isset($types[$element])) {
- $types[$element]['#process'][] = 'linkit_process_field_element';
- }
- }
- // Used when using ckeditor module.
- if (isset($types['text_format']['#pre_render']) && is_array($types['text_format']['#pre_render'])) {
- if (in_array('ckeditor_pre_render_text_format', $types['text_format']['#pre_render'])) {
- $types['text_format']['#pre_render'][] = 'linkit_pre_render_editor_element';
- // Add the linkit library for the editor.
- drupal_add_library('linkit', 'ckeditor');
- }
- }
- // Used when using wysiwyg module.
- if (isset($types['text_format']['#pre_render']) && is_array($types['text_format']['#pre_render'])) {
- if (in_array('wysiwyg_pre_render_text_format', $types['text_format']['#pre_render'])) {
- $types['text_format']['#process'][] = 'linkit_pre_render_editor_element';
- }
- }
- }
- /**
- * Implements hook_library().
- */
- function linkit_library() {
- $path = drupal_get_path('module', 'linkit');
- $common = array(
- 'website' => 'http://drupal.org/project/linkit',
- 'version' => '7.3',
- );
- // Linkit base
- $libraries['base'] = array(
- 'title' => 'Linkit base',
- 'js' => array(
- $path . '/js/linkit.js' => array('group' => JS_DEFAULT, 'weight' => -1),
- // Add global settings for Linkit.
- array(
- 'type' => 'setting',
- // ___profile___ is just a placeholder.
- 'data' => array(
- 'linkit' => array(
- 'autocompletePath' => url('linkit/autocomplete/___profile___', array('query' => array(LINKIT_BAC_QUERY_KEY => ''), 'absolute' => TRUE)),
- 'dashboardPath' => url('linkit/dashboard/'),
- 'currentInstance' => new stdClass(),
- ),
- ),
- ),
- ),
- 'dependencies' => array(
- array('system', 'ui.dialog'),
- array('system', 'drupal.ajax'),
- ),
- );
- // Linkit field ui script.
- $libraries['field'] = array(
- 'title' => 'Linkit Field UI',
- 'js' => array(
- $path . '/js/linkit.field.js' => array('group' => JS_DEFAULT),
- ),
- 'dependencies' => array(
- array('linkit', 'base'),
- ),
- );
- // Linkit ckeditor dialog script.
- $libraries['ckeditor'] = array(
- 'title' => 'Linkit CKeditor',
- 'js' => array(
- $path . '/editors/ckeditor/linkitDialog.js' => array('group' => JS_DEFAULT),
- ),
- 'dependencies' => array(
- array('linkit', 'base'),
- ),
- );
- // Linkit tinymce dialog script.
- $libraries['tinymce'] = array(
- 'title' => 'Linkit TinyMCE',
- 'js' => array(
- $path . '/editors/tinymce/linkitDialog.js' => array('group' => JS_DEFAULT),
- ),
- 'dependencies' => array(
- array('linkit', 'base'),
- ),
- );
- foreach ($libraries as &$library) {
- $library += $common;
- }
- // Linkit BAC
- $libraries['bac'] = array(
- 'website' => 'https://github.com/betamos/Better-Autocomplete',
- 'version' => '1.0',
- 'title' => 'Better autocomplete',
- 'js' => array(
- $path . '/better-autocomplete/jquery.better-autocomplete.js' => array('group' => JS_LIBRARY),
- ),
- 'css' => array(
- $path . '/better-autocomplete/better-autocomplete.css' => array(
- 'group' => CSS_DEFAULT,
- 'preprocess' => FALSE,
- ),
- ),
- );
- return $libraries;
- }
- /**
- * Implements hook_menu().
- */
- function linkit_menu() {
- $items = array();
- // This is the Linkit dashboard menu callback.
- $items['linkit/dashboard/%linkit_profile'] = array(
- 'title' => 'Linkit',
- 'description' => 'Dashboard',
- 'delivery callback' => 'ajax_deliver',
- 'page callback' => 'linkit_dashboard_page',
- 'page arguments' => array(2),
- 'access callback' => TRUE,
- 'theme callback' => 'ajax_base_page_theme',
- 'type' => MENU_CALLBACK,
- 'file path' => 'includes',
- 'file' => 'form.inc',
- );
- // The autocomplete callback, the search_string is found in the $_GET array
- // so dont pass that to the page callback.
- $items['linkit/autocomplete/%linkit_profile'] = array(
- 'title' => 'Linkit autocomplete response function',
- 'page callback' => 'linkit_autocomplete',
- 'page arguments' => array(2),
- 'access callback' => TRUE,
- 'type' => MENU_CALLBACK,
- );
- return $items;
- };
- /**
- * Creates the dialog dashboard.
- *
- * We don't call drupal_get_form() in the menu callback as we don't want to
- * return the rendered form, we just want to print it as it is.
- *
- * @param $profile
- * A LinkitProfile object.
- */
- function linkit_dashboard_page(LinkitProfile $profile) {
- // Set the active Linkit profile.
- linkit_set_active_profile($profile);
- // The dashboard isn't really a form.
- // See comment in linkit_dashboard_form() for more info.
- $form = drupal_get_form('linkit_dashboard_form');
- //$change_profile = drupal_get_form('linkit_dashboad_profile_change_form');
- return array(
- '#type' => 'ajax',
- '#commands' => array(
- ajax_command_html('#linkit-modal', drupal_render($form)),
- ajax_command_prepend('#linkit-modal', theme('status_messages')),
- ),
- );
- }
- /**
- * Ajax Form callback;
- *
- * Returns the Linkit "form" when changing profile.
- */
- function linkit_change_profile($form, $form_state) {
- return $form['linkit_container'];
- }
- /**
- * Create the dashboard page.
- */
- function linkit_dashboard_form($form, &$form_state) {
- // The use of a form here is insignificant as we are not submitting any data.
- // A form here just cause trouble because it can be submitted and will do new
- // request to the action of the form.
- // In normal cases this isn't really a problem as the javascript is preventing
- // the form to be submitted, but in case of a javascript error, this event
- // can't be prevented.
- // We still have to use the FAPI to build this "form", as we want proper
- // classes and ids on the elements and take advantage of all the good parts of
- // the form generation.
- // So, just make sure the form tags dont get rendered.
- //$form['#theme_wrappers'] = array();
- // Get the active Linkit profile.
- if (!empty($form_state['values']['profile'])) {
- linkit_set_active_profile(linkit_profile_load($form_state['values']['profile']));
- }
- $active_profile = linkit_get_active_profile();
- $profiles = linkit_profile_editor_load_all();
- $_profiles = array();
- foreach ($profiles as $profile) {
- $_profiles[$profile->name] = check_plain($profile->admin_title);
- }
- // Attach css to the form.
- $form['#attached']['css'][drupal_get_path('module', 'linkit') . '/css/linkit.css'] = array(
- 'preprocess' => FALSE,
- );
- // Attach js to the form.
- $form['#attached']['js'] = array(
- drupal_get_path('module', 'linkit') . '/js/linkit.dashboard.js',
- );
- if ($active_profile->profile_type == LINKIT_PROFILE_TYPE_EDITOR) {
- $form['profile'] = array(
- '#type' => 'radios',
- '#title' => t('Select profile to use'),
- '#default_value' => $active_profile->name,
- '#weight' => -100,
- '#options' => $_profiles,
- '#ajax' => array(
- 'callback' => 'linkit_change_profile',
- 'wrapper' => 'linkit-dashbaord-ajax-wrapper',
- 'method' => 'replaceWith',
- 'effect' => 'fade',
- 'event' => 'click',
- ),
- '#prefix' => '<div id="linkit-profile-changer">',
- '#suffix' => '</div>',
- );
- foreach ($profiles as $profile) {
- $form['profile'] += array(
- $profile->name => array(
- '#description' => check_markup($profile->admin_description),
- )
- );
- }
- }
- $form['linkit_container'] = array(
- '#type' => 'container',
- '#weight' => 100,
- '#prefix' => '<div id="linkit-dashbaord-ajax-wrapper">',
- '#suffix' => '</div>',
- '#attached' => array(
- 'js' => array(
- array(
- 'data' => array(
- 'linkit' => array(
- 'currentInstance' => array(
- 'profile' => $active_profile->name,
- 'autocomplete' => array_filter($active_profile->data['autocomplete']),
- ),
- ),
- ),
- 'type' => 'setting',
- )
- ),
- ),
- );
- $form['linkit_container']['linkit_search'] = array(
- '#type' => 'textfield',
- '#title' => t('Search for content.'),
- '#description' => t('Start typing to find content or paste a URL.'),
- '#maxlength' => 255,
- '#size' => 60,
- '#default_value' => '',
- '#weight' => -10,
- '#attributes' => array(
- 'class' => array(
- 'linkit-search-element',
- ),
- ),
- '#attached' => array(
- 'library' => array(
- array('linkit', 'bac'),
- ),
- ),
- );
- $form['linkit_container']['linkit_path'] = array(
- '#type' => 'textfield',
- '#title' => t('Link URL'),
- '#description' => t('This will be populated by the search, or you can fill it in yourself.'),
- '#required' => TRUE,
- '#maxlength' => NULL,
- '#size' => 60,
- '#default_value' => '',
- '#weight' => -1,
- '#attributes' => array(
- 'class' => array(
- 'linkit-path-element',
- ),
- ),
- );
- // If we have enabled attributes, lets put them inside a fieldset.
- if (count($active_profile->getEnabledAttributePlugins())) {
- // Create the container fieldset.
- $form['linkit_container']['linkit_attributes'] = array(
- '#type' => 'fieldset',
- '#title' => t('Options'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#weight' => 10,
- '#attributes' => array(
- 'class' => array(
- 'linkit-attributes',
- ),
- ),
- );
- // Append the attributes info the fieldset.
- foreach ($active_profile->getEnabledAttributePlugins() AS $name => $attribute) {
- // Add a class to all items.
- $attribute['#attributes']['class'][] = 'linkit-attribute-' . $name;
- // Add 'linkit_' prefix to ensure that is unique.
- $form['linkit_container']['linkit_attributes']['linkit_' . $name] = $attribute;
- }
- }
- $form['linkit_container']['linkit_insert'] = array(
- '#type' => 'button',
- '#value' => t('Insert link'),
- '#suffix' => '<a id="linkit-cancel" href="#">' . t('Cancel') . '</a>',
- '#weight' => 100,
- '#attributes' => array(
- 'class' => array(
- 'linkit-insert',
- ),
- ),
- );
- return $form;
- }
- /**
- * Autocomplete callback function.
- *
- * @param object $profile
- * A LinkitProfile object.
- */
- function linkit_autocomplete(LinkitProfile $profile) {
- // Set the active Linkit profile.
- linkit_set_active_profile($profile);
- // This is not sanitized until it is used within output.
- $search_string = $_GET[LINKIT_BAC_QUERY_KEY];
- $results = array();
- // Special for link to frontpage.
- if (strpos($search_string, 'front') !== FALSE) {
- $results = array_merge($results, array(
- 'title' => t('Frontpage'),
- 'description' => 'The frontpage for this site.',
- 'path' => url('<front>'),
- 'group' => t('System'),
- ));
- }
- // Let the Linkit search plugins do their job.
- $results = array_merge($results, linkit_autocomplete_search_plugins($search_string));
- // If there is results from the Linkit search plugins, don't care about
- // searching for absolute URL's results.
- if (!count($results)) {
- // Try to parse the string as an URL.
- // We will not use the drupal wrapper function drupal_pasre_url() as that
- // function should only be used for URL's that have been generated by the
- // system, and we can't be sure that this is the case here.
- // Check for an e-mail address then return an e-mail result and create a
- // mail-to link if appropriate.
- if (filter_var($search_string, FILTER_VALIDATE_EMAIL)) {
- $results = array(array(
- 'title' => t('E-mail @email', array('@email' => $search_string)),
- 'path' => 'mailto:'. check_plain($search_string),
- 'description' => t('Open your mail client ready to e-mail @email', array('@email' => $search_string)),
- 'addClass' => 'status-notice',
- ));
- }
- else {
- $parts = parse_url(trim($search_string, '/'));
- // This seems to be an absolute URL.
- if (isset($parts['scheme']) || isset($parts['host'])) {
- $results = array_merge($results, linkit_autocomplete_absolute_url($search_string, $parts));
- }
- }
- }
- // If there is still no results, return a "no results" array.
- if (!count($results)) {
- $results = array(
- array(
- 'title' => t('No results'),
- 'addClass' => 'status-notice',
- 'disabled' => TRUE,
- )
- );
- }
- print drupal_json_output($results);
- drupal_exit();
- }
- /**
- * Perform autocomplete search with the Linkit search plugins.
- *
- * @param $search_string
- * The search string.
- *
- * @return
- * An array with the results objects, or an empty array if no results.
- */
- function linkit_autocomplete_search_plugins($search_string) {
- $matches = array();
- $profile = linkit_get_active_profile();
- // Get matches from all search plugins.
- foreach ($profile->getEnabledsearchPlugins() as $plugin_name => $plugin) {
- $matches = array_merge($matches, $plugin->fetchResults($search_string));
- }
- return $matches;
- }
- /**
- * Retrieve relevant information about a URL. Specifically this function is
- * usable for internal (absolute) URL:s, but it also works for external URL:s.
- *
- * @param $url
- * The search string (URL) that should be scanned.
- *
- * @param $parts
- * An array of URL parts from parse_url().
- *
- * @return
- * An associative array containing:
- * - url: The same as the argument $url, untouched.
- * - target: Either "internal" or "external".
- * - requested_path: If internal, the path requested relative to Drupal root.
- * The only exception is when frontpage is referred directly, then it will
- * be whatever the frontpage is set to.
- * - system_path: If internal and the path is valid, the Drupal system path,
- * e.g. "node/23".
- * - query_fragment: If internal, the query and fragment of the url.
- * Typically it is not needed for searching and is just reappended back
- * when processing of the path is done. It could e.g. look like
- * "?foo=bar#anchor".
- */
- function linkit_parse_url($url, $parts) {
- global $base_url;
- // Make a new array, this will hold the components from parse_url() and our
- // own "Linkit" components.
- $path_info = array();
- // Append the original components from parse_url() to our array.
- $path_info += $parts;
- // Save the whole URL.
- $path_info['url'] = $url;
- if (!isset($path_info['query'])) {
- $path_info['query'] = '';
- }
- // Convert the query string to an array as Drupal can only handle querys as
- // arrays.
- // @see http://api.drupal.org/drupal_http_build_query
- parse_str($path_info['query'], $path_info['query']);
- // The 'q' parameter contains the path of the current page if clean URLs are
- // disabled. It overrides the 'path' of the URL when present, even if clean
- // URLs are enabled, due to how Apache rewriting rules work.
- if (isset($path_info['query']['q'])) {
- $path_info['path'] = $path_info['query']['q'];
- unset($path_info['query']['q']);
- }
- // Load all local stream wrappers. The $path_info['scheme'] will be tested
- // against this later to ensure it is local.
- $local_stream_wrappers = file_get_stream_wrappers(STREAM_WRAPPERS_LOCAL);
- // Internal URL.
- // We can not use the url_is_external() as it treats all absolute links as
- // external and treats all stream wrappers as internal.
- $local_url = trim($path_info['scheme'] . '://' . $path_info['host'] . base_path(), '/') == $base_url;
- $local_stream_wrapper = isset($local_stream_wrappers[$path_info['scheme']]);
- //@TODO: maybe file_valid_uri() ?
- if ($local_url || $local_stream_wrapper) {
- // Set target as internal.
- $path_info['target'] = 'internal';
- // If this is seems to be a valid local stream wrapper string, force the
- // $path_info['path'] to be set to the file_url.
- if ($local_stream_wrapper) {
- $path_info = array_merge($path_info, parse_url(file_create_url($path_info['url'])));
- }
- // Trim the path from slashes.
- $path_info['path'] = trim($path_info['path'], '/');
- // If we have an empty path, and an internal target, we can assume that the
- // URL should go the the frontpage.
- if (empty($path_info['path'])) {
- $path_info['frontpage'] = TRUE;
- $path_info['path'] = variable_get('site_frontpage', 'node');
- }
- // Try converting the path to an internal Drupal path.
- $internal_url = drupal_get_normal_path($path_info['path']);
- // Add the "real" system path (not the alias) if the current user have
- // access to the URL.
- $path_info['system_path'] = drupal_valid_path($internal_url) ? $internal_url : FALSE;
- $menu_item = menu_get_item($path_info['system_path']);
- if ($menu_item) {
- $path_info['menu']['path'] = $path_info['system_path'];
- $path_info['menu']['description'] = check_plain($menu_item['description']);
- $path_info['menu']['title'] = check_plain($menu_item['title']);
- }
- // If we have a valid stream wrapper URL, find out the internal url.
- if ($local_stream_wrapper) {
- $path_info['system_path'] = $path_info['path'];
- $path_info['menu']['path'] = $path_info['path'];
- $path_info['menu']['description'] = 'This funciton is not fully integrated yet.';
- $path_info['menu']['title'] = 'This funciton is not fully integrated yet.';
- }
- }
- else {
- // Set target as external.
- $path_info['target'] = 'external';
- }
- return $path_info;
- }
- /**
- * Retrieve the result object from an absolute URL. Both internal and external
- * paths work.
- *
- * @param $url
- * The search string which seems to be an URL.
- *
- * @param $parts
- * An array of URL parts from parse_url().
- *
- * @return
- * A result object. This is an associative array which consists of:
- * - title: The title of the result.
- * - description: The description of the result (may contain HTML).
- * - path: The target path which will be inserted as the href in the link.
- * - addClass: A CSS class that will be added to the DOM result item.
- */
- function linkit_autocomplete_absolute_url($url, $parts) {
- $result = array();
- // Retrieve relevant information about the search string and if its a URL.
- $path_info = linkit_parse_url($url, $parts);
- // Set default options to pass with the url() function if the URL is internal.
- $url_options = array();
- // Add the URL frament.
- $url_options['fragment'] = isset($path_info['fragment']) ? $path_info['fragment'] : '';
- // Add the URL query.
- $url_options['query'] = isset($path_info['query']) ? $path_info['query'] : '';
- // The URL is registerd by Drupal (Internal).
- if (isset($path_info['menu']) && $path_info['system_path'] !== FALSE) {
- $result = array(
- 'path' => linkit_get_insert_plugin_processed_path(linkit_get_active_profile(), $path_info['menu']['path'], $url_options),
- 'title' => $path_info['menu']['title'] ? check_plain($path_info['menu']['title']) : check_plain($path_info['menu']['path']),
- 'description' => check_plain($path_info['menu']['description']) . ' ' . t('This is an internal path.'),
- 'addClass' => 'status-ok',
- );
- }
- // No internal menu result, but the URL seems to be internal. Either we do not
- // have access to it or it does not exists.
- elseif ($path_info['target'] == 'internal') {
- $result = array(
- 'path' => linkit_get_insert_plugin_processed_path(linkit_get_active_profile(), $path_info['path'], $url_options),
- 'title' => t('Page not found'),
- 'description' => t('This page does not exist or you do not have access to it.'),
- 'addClass' => 'status-warning',
- );
- }
- // The URL seems to be external.
- elseif ($path_info['target'] == 'external') {
- $result = array(
- 'title' => t('No information available'),
- 'description' => t('This is an external URL, but we don\'t know where it leads.'),
- 'path' => $path_info['url'],
- 'addClass' => 'status-notice',
- );
- }
- // Return the results in an array as BAC will need to have it that way.
- return array($result);
- }
- /**
- * Implements hook_image_default_styles().
- *
- * @return
- * An array of image styles, keyed by the style name.
- */
- function linkit_image_default_styles() {
- $styles = array();
- $styles['linkit_thumb'] = array(
- 'effects' => array(
- array(
- 'name' => 'image_scale',
- 'data' => array(
- 'width' => 50,
- 'height' => 50,
- 'upscale' => 0,
- ),
- 'weight' => 0,
- ),
- ),
- );
- return $styles;
- }
- /**
- * Implements hook_wysiwyg_plugin().
- */
- function linkit_wysiwyg_plugin($editor, $version) {
- $plugin = array();
- $plugin['linkit'] = array(
- 'path' => drupal_get_path('module', 'linkit') . '/editors/' . $editor,
- 'buttons' => array('linkit' => t('Linkit')),
- 'url' => 'http://drupal.org/project/linkit',
- 'load' => TRUE,
- );
- // TinyMCE needs to know the filename.
- if ($editor == 'tinymce') {
- $plugin['linkit']['filename'] = 'editor_plugin.js';
- }
- // Add the linkit library for the editor.
- drupal_add_library('linkit', $editor);
- return $plugin;
- }
- /**
- * Implements hook_wysiwyg_editor_settings_alter().
- */
- function linkit_wysiwyg_editor_settings_alter(&$settings, $context) {
- if ($context['profile']->editor == 'ckeditor') {
- $button_exists = isset($context['profile']->settings['buttons']['linkit']);
- if (!empty($context['profile']->settings['default_toolbar_grouping']) && $button_exists) {
- foreach ($settings['toolbar'] as &$group) {
- if ($group['name'] == 'links') {
- array_unshift($group['items'], 'linkit');
- }
- if ($group['name'] == 'other') {
- if (in_array('linkit', $group['items'])) {
- unset($group['items'][array_search('linkit', $group['items'])]);
- // Regenerate the keys.
- $group['items'] = array_values($group['items']);
- }
- }
- }
- }
- }
- }
- /**
- * Implements hook_ckeditor_plugin().
- */
- function linkit_ckeditor_plugin() {
- return array(
- 'linkit' => array(
- // Name of the plugin used to write it
- 'name' => 'linkit',
- // Description of plugin - it would appear in plugins managment of profile settings
- 'desc' => t('Support for Linkit module'),
- // The full path to the CKEditor plugin directory, with trailing slash.
- 'path' => drupal_get_path('module', 'linkit') . '/editors/ckeditor/',
- // Buttons to show up in the toolbar config area.
- 'buttons' => array(
- 'linkit' => array(
- 'label' => 'Linkit',
- 'icon' => 'icons/linkit.png',
- ),
- ),
- ),
- );
- }
- /**
- * Add Linkit settings to pages where we have an editor element.
- */
- function linkit_pre_render_editor_element($element) {
- static $processed = array();
- if (!isset($processed[$element['#id']])) {
- // Load the first profile that is assign to this text format.
- $profile = linkit_profile_load_by_format($element['#format']);
- // If we dont have any profile, lets just return the element.
- if (!$profile) {
- return $element;
- }
- // Set the field ID.
- if (isset($element['value']['#id'])) {
- $field_id = $element['value']['#id'];
- }
- else {
- $field_id = $element['#id'] . '-value';
- }
- $field_js = array(
- 'data' => array(
- 'linkit' => array(
- 'fields' => array(
- $field_id => array(
- 'profile' => $profile->name,
- ),
- ),
- ),
- ),
- 'type' => 'setting',
- );
- // Configure Linkit to have an IMCE selector.
- if (module_invoke('imce', 'access') && isset($profile->data['imce']) && $profile->data['imce'] == 1) {
- $field_js['data']['linkit']['IMCEurl'] = url('imce', array('query' => array(
- 'app' => 'Linkit|sendto@Drupal.linkit.IMCECallback',
- 'absolute' => TRUE,
- )));
- // We will only serv public files with IMCE.
- $field_js['data']['linkit']['publicFilesDirectory'] = variable_get('file_public_path', conf_path() . '/files');
- }
- // Attach the linkit_base librar
- $element['#attached']['library'][] = array('linkit', 'base');
- $element['#attached']['js'][] = $field_js;
- $processed[$element['#id']] = TRUE;
- }
- return $element;
- }
- /**
- * Load the first profile assigned to a text format.
- *
- * @param $format
- * A string with the format machine name.
- *
- * @return
- * A LinkitProfile object or FALSE if no profile is found.
- */
- function linkit_profile_load_by_format($format) {
- $profiles = linkit_profile_editor_load_all();
- foreach ($profiles as $profile) {
- if (is_array($profile->data['text_formats']) && in_array($format, array_filter($profile->data['text_formats']))) {
- return $profile;
- }
- }
- return FALSE;
- }
- /**
- * Get profile type.
- */
- function linkit_get_profile_type($type) {
- switch ($type) {
- case LINKIT_PROFILE_TYPE_EDITOR:
- return t('Editor');
- case LINKIT_PROFILE_TYPE_FIELD:
- return t('Field');
- default:
- return t('Can not find the profile type');
- }
- }
- /**
- * Gets the path processed by the inputfilter choice.
- *
- * @param LinkitProfile $profile
- * The Linkit profile to use.
- *
- * @param $uri
- * The uri to act on.
- *
- * @param $options
- * An array of options to the URL function if its used.
- *
- * @return The processed uri.
- */
- function linkit_get_insert_plugin_processed_path(LinkitProfile $profile, $uri, $options = array()) {
- switch ($profile->data['insert_plugin']['url_method']) {
- case LINKIT_URL_METHOD_RAW:
- $path = $uri;
- break;
- case LINKIT_URL_METHOD_RAW_SLASH:
- $options['alias'] = TRUE;
- $path = url($uri, $options);
- break;
- case LINKIT_URL_METHOD_ALIAS:
- $path = url($uri, $options);
- break;
- }
- return $path;
- }
|