Exemple #1
0
 /**
  * Reorders the smileys from a drag/drop event
  * Will move them from post to popup location and visa-versa
  * Will move them to new rows
  */
 public function action_smileyorder()
 {
     global $context, $txt;
     // Start off with an empty response
     $context['xml_data'] = array();
     $errors = array();
     $order = array();
     // Chances are I wear a silly ;D
     loadLanguage('Errors');
     loadLanguage('ManageSmileys');
     require_once SUBSDIR . '/Smileys.subs.php';
     // You have to be allowed to do this
     $validation_token = validateToken('admin-sort', 'post', true, false);
     $validation_session = validateSession();
     if (empty($validation_session) && $validation_token === true) {
         // Valid posting
         if (isset($_POST['order']) && $_POST['order'] == 'reorder') {
             // Get the details on the moved smile
             list(, $smile_moved) = explode('_', $_POST['moved']);
             $smile_moved = (int) $smile_moved;
             $smile_moved_details = getSmiley($smile_moved);
             // Check if we moved rows or locations
             $smile_received_location = null;
             $smile_received_row = null;
             if (!empty($_POST['received'])) {
                 $displayTypes = array('postform' => 0, 'popup' => 2);
                 list($smile_received_location, $smile_received_row) = explode('|', $_POST['received']);
                 $smile_received_location = $displayTypes[substr($smile_received_location, 7)];
             }
             // If these are not set, we are kind of lost :P
             if (isset($smile_received_location, $smile_received_row)) {
                 // Read the new ordering, remember where the moved smiley is in the stack
                 $list_order = 0;
                 $moved_key = 0;
                 $smiley_tree = array();
                 foreach ($_POST['smile'] as $smile_id) {
                     $smiley_tree[] = $smile_id;
                     // Keep track of where the moved smiley is in the sort stack
                     if ($smile_id == $smile_moved) {
                         $moved_key = $list_order;
                     }
                     $list_order++;
                 }
                 // Now get the updated row, location, order
                 $smiley = array();
                 $smiley['row'] = !isset($smile_received_row) ? $smile_moved_details['row'] : $smile_received_row;
                 $smiley['location'] = !isset($smile_received_location) ? $smile_moved_details['location'] : $smile_received_location;
                 $smiley['order'] = -1;
                 // If the node after the drop zone is in the same row/container, we use its position
                 if (isset($smiley_tree[$moved_key + 1])) {
                     $possible_after = getSmiley($smiley_tree[$moved_key - 1]);
                     if ($possible_after['row'] == $smiley['row'] && $possible_after['location'] == $smiley['location']) {
                         $smiley = getSmileyPosition($smiley['location'], $smiley_tree[$moved_key - 1]);
                     }
                 }
                 // Empty means getSmileyPosition failed and so do we
                 if (!empty($smiley)) {
                     moveSmileyPosition($smiley, $smile_moved);
                     // Done with the move, now we clean up across the containers/rows
                     $smileys = getSmileys();
                     foreach (array_keys($smileys) as $location) {
                         foreach ($smileys[$location]['rows'] as $id => $smiley_row) {
                             // Fix empty rows if any.
                             if ($id != $smiley_row[0]['row']) {
                                 updateSmileyRow($id, $smiley_row[0]['row'], $location);
                                 // Only change the first row value of the first smiley.
                                 $smileys[$location]['rows'][$id][0]['row'] = $id;
                             }
                             // Make sure the smiley order is always sequential.
                             foreach ($smiley_row as $order_id => $smiley) {
                                 if ($order_id != $smiley['order']) {
                                     updateSmileyOrder($smiley['id'], $order_id);
                                 }
                             }
                         }
                     }
                     // Clear the cache, its stale now
                     cache_put_data('parsing_smileys', null, 480);
                     cache_put_data('posting_smileys', null, 480);
                     $order[] = array('value' => $txt['smileys_moved_done']);
                 }
             }
         } else {
             $errors[] = array('value' => $txt['smileys_moved_fail']);
         }
     } else {
         if (!empty($validation_session)) {
             $errors[] = array('value' => $txt[$validation_session]);
         }
         if (empty($validation_token)) {
             $errors[] = array('value' => $txt['token_verify_fail']);
         }
     }
     // New generic token for use
     createToken('admin-sort', 'post');
     $tokens = array(array('value' => $context['admin-sort_token'], 'attributes' => array('type' => 'token')), array('value' => $context['admin-sort_token_var'], 'attributes' => array('type' => 'token_var')));
     // Return the response, whatever it is
     $context['sub_template'] = 'generic_xml';
     $context['xml_data'] = array('orders' => array('identifier' => 'order', 'children' => $order), 'tokens' => array('identifier' => 'token', 'children' => $tokens), 'errors' => array('identifier' => 'error', 'children' => $errors));
 }
    /**
     * Add, remove, edit smileys.
     */
    public function action_editsmiley()
    {
        global $modSettings, $context, $txt, $scripturl;
        require_once SUBSDIR . '/Smileys.subs.php';
        // Force the correct tab to be displayed.
        $context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
        $context['sub_template'] = $context['sub_action'];
        // Submitting a form?
        if (isset($_POST['smiley_save']) || isset($_POST['smiley_action'])) {
            checkSession();
            // Changing the selected smileys?
            if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys'])) {
                foreach ($_POST['checked_smileys'] as $id => $smiley_id) {
                    $_POST['checked_smileys'][$id] = (int) $smiley_id;
                }
                if ($_POST['smiley_action'] == 'delete') {
                    deleteSmileys($_POST['checked_smileys']);
                } else {
                    // Check it's a valid type.
                    $displayTypes = array('post' => 0, 'hidden' => 1, 'popup' => 2);
                    if (isset($displayTypes[$_POST['smiley_action']])) {
                        updateSmileyDisplayType($_POST['checked_smileys'], $displayTypes[$_POST['smiley_action']]);
                    }
                }
            } elseif (isset($_POST['smiley'])) {
                $_POST['smiley'] = (int) $_POST['smiley'];
                // Is it a delete?
                if (!empty($_POST['deletesmiley'])) {
                    deleteSmileys(array($_POST['smiley']));
                } else {
                    $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
                    $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
                    $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
                    // Make sure some code was entered.
                    if (empty($_POST['smiley_code'])) {
                        fatal_lang_error('smiley_has_no_code');
                    }
                    // Also make sure a filename was given.
                    if (empty($_POST['smiley_filename'])) {
                        fatal_lang_error('smiley_has_no_filename');
                    }
                    // Check whether the new code has duplicates. It should be unique.
                    if (validateDuplicateSmiley($_POST['smiley_code'], $_POST['smiley'])) {
                        fatal_lang_error('smiley_not_unique');
                    }
                    $param = array('smiley_location' => $_POST['smiley_location'], 'smiley' => $_POST['smiley'], 'smiley_code' => $_POST['smiley_code'], 'smiley_filename' => $_POST['smiley_filename'], 'smiley_description' => $_POST['smiley_description']);
                    updateSmiley($param);
                }
                // Sort all smiley codes for more accurate parsing (longest code first).
                sortSmileyTable();
            }
            cache_put_data('parsing_smileys', null, 480);
            cache_put_data('posting_smileys', null, 480);
        }
        // Load all known smiley sets.
        $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
        $set_names = explode("\n", $modSettings['smiley_sets_names']);
        foreach ($context['smiley_sets'] as $i => $set) {
            $context['smiley_sets'][$i] = array('id' => $i, 'path' => htmlspecialchars($set, ENT_COMPAT, 'UTF-8'), 'name' => htmlspecialchars($set_names[$i], ENT_COMPAT, 'UTF-8'), 'selected' => $set == $modSettings['smiley_sets_default']);
        }
        // Prepare overview of all (custom) smileys.
        if ($context['sub_action'] == 'editsmileys') {
            // Determine the language specific sort order of smiley locations.
            $smiley_locations = array($txt['smileys_location_form'], $txt['smileys_location_hidden'], $txt['smileys_location_popup']);
            asort($smiley_locations);
            // Create a list of options for selecting smiley sets.
            $smileyset_option_list = '
				<select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
            foreach ($context['smiley_sets'] as $smiley_set) {
                $smileyset_option_list .= '
					<option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected="selected"' : '') . '>' . $smiley_set['name'] . '</option>';
            }
            $smileyset_option_list .= '
				</select>';
            $listOptions = array('id' => 'smiley_list', 'title' => $txt['smileys_edit'], 'items_per_page' => 40, 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys', 'default_sort_col' => 'filename', 'get_items' => array('function' => 'list_getSmileys'), 'get_count' => array('function' => 'list_getNumSmileys'), 'no_items_label' => $txt['smileys_no_entries'], 'columns' => array('picture' => array('data' => array('sprintf' => array('format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d"><img src="' . $modSettings['smileys_url'] . '/' . $modSettings['smiley_sets_default'] . '/%2$s" alt="%3$s" id="smiley%1$d" /><input type="hidden" name="smileys[%1$d][filename]" value="%2$s" /></a>', 'params' => array('id_smiley' => false, 'filename' => true, 'description' => true)), 'class' => 'imagecolumn')), 'code' => array('header' => array('value' => $txt['smileys_code']), 'data' => array('db_htmlsafe' => 'code'), 'sort' => array('default' => 'code', 'reverse' => 'code DESC')), 'filename' => array('header' => array('value' => $txt['smileys_filename']), 'data' => array('db_htmlsafe' => 'filename'), 'sort' => array('default' => 'filename', 'reverse' => 'filename DESC')), 'location' => array('header' => array('value' => $txt['smileys_location']), 'data' => array('function' => create_function('$rowData', '
								global $txt;

								if (empty($rowData[\'hidden\']))
									return $txt[\'smileys_location_form\'];
								elseif ($rowData[\'hidden\'] == 1)
									return $txt[\'smileys_location_hidden\'];
								else
									return $txt[\'smileys_location_popup\'];
							')), 'sort' => array('default' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\')', 'reverse' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\') DESC')), 'tooltip' => array('header' => array('value' => $txt['smileys_description']), 'data' => array('function' => create_function('$rowData', empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']) ? '
								return htmlspecialchars($rowData[\'description\'], ENT_COMPAT, \'UTF-8\');
							' : '
								global $context, $txt, $modSettings;

								// Check if there are smileys missing in some sets.
								$missing_sets = array();
								foreach ($context[\'smiley_sets\'] as $smiley_set)
									if (!file_exists(sprintf(\'%1$s/%2$s/%3$s\', $modSettings[\'smileys_dir\'], $smiley_set[\'path\'], $rowData[\'filename\'])))
										$missing_sets[] = $smiley_set[\'path\'];

								$description = htmlspecialchars($rowData[\'description\'], ENT_COMPAT, \'UTF-8\');

								if (!empty($missing_sets))
									$description .= sprintf(\'<br /><span class="smalltext"><strong>%1$s:</strong> %2$s</span>\', $txt[\'smileys_not_found_in_set\'], implode(\', \', $missing_sets));

								return $description;
							')), 'sort' => array('default' => 'description', 'reverse' => 'description DESC')), 'modify' => array('header' => array('value' => $txt['smileys_modify'], 'class' => 'centertext'), 'data' => array('sprintf' => array('format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>', 'params' => array('id_smiley' => false)), 'class' => 'centertext')), 'check' => array('header' => array('value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />', 'class' => 'centertext'), 'data' => array('sprintf' => array('format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d" class="input_check" />', 'params' => array('id_smiley' => false)), 'class' => 'centertext'))), 'form' => array('href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys', 'name' => 'smileyForm'), 'additional_rows' => array(array('position' => 'above_column_headers', 'value' => $smileyset_option_list, 'class' => 'righttext'), array('position' => 'below_table_data', 'value' => '
							<select name="smiley_action" onchange="makeChanges(this.value);">
								<option value="-1">' . $txt['smileys_with_selected'] . ':</option>
								<option value="-1">--------------</option>
								<option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
								<option value="post">' . $txt['smileys_show_on_post'] . '</option>
								<option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
								<option value="delete">' . $txt['smileys_remove'] . '</option>
							</select>
							<noscript>
								<input type="submit" name="perform_action" value="' . $txt['go'] . '" class="right_submit" />
							</noscript>', 'class' => 'righttext')), 'javascript' => '
					function makeChanges(action)
					{
						if (action == \'-1\')
							return false;
						else if (action == \'delete\')
						{
							if (confirm(\'' . $txt['smileys_confirm'] . '\'))
								document.forms.smileyForm.submit();
						}
						else
							document.forms.smileyForm.submit();
						return true;
					}
					function changeSet(newSet)
					{
						var currentImage, i, knownSmileys = [];

						if (knownSmileys.length == 0)
						{
							for (var i = 0, n = document.images.length; i < n; i++)
								if (document.images[i].id.substr(0, 6) == \'smiley\')
									knownSmileys[knownSmileys.length] = document.images[i].id.substr(6);
						}

						for (i = 0; i < knownSmileys.length; i++)
						{
							currentImage = document.getElementById("smiley" + knownSmileys[i]);
							currentImage.src = "' . $modSettings['smileys_url'] . '/" + newSet + "/" + document.forms.smileyForm["smileys[" + knownSmileys[i] + "][filename]"].value;
						}
					}');
            require_once SUBSDIR . '/GenericList.class.php';
            createList($listOptions);
            // The list is the only thing to show, so make it the main template.
            $context['default_list'] = 'smiley_list';
            $context['sub_template'] = 'show_list';
        } elseif ($context['sub_action'] == 'modifysmiley') {
            // Get a list of all known smiley sets.
            $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? BOARDDIR . '/smileys' : $modSettings['smileys_dir'];
            $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
            $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
            $set_names = explode("\n", $modSettings['smiley_sets_names']);
            foreach ($context['smiley_sets'] as $i => $set) {
                $context['smiley_sets'][$i] = array('id' => $i, 'path' => htmlspecialchars($set, ENT_COMPAT, 'UTF-8'), 'name' => htmlspecialchars($set_names[$i], ENT_COMPAT, 'UTF-8'), 'selected' => $set == $modSettings['smiley_sets_default']);
            }
            $context['selected_set'] = $modSettings['smiley_sets_default'];
            // Get all possible filenames for the smileys.
            $context['filenames'] = array();
            if ($context['smileys_dir_found']) {
                foreach ($context['smiley_sets'] as $smiley_set) {
                    if (!file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']))) {
                        continue;
                    }
                    $dir = dir($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']));
                    while ($entry = $dir->read()) {
                        if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png'))) {
                            $context['filenames'][strtolower($entry)] = array('id' => htmlspecialchars($entry, ENT_COMPAT, 'UTF-8'), 'selected' => false);
                        }
                    }
                    $dir->close();
                }
                ksort($context['filenames']);
            }
            $_REQUEST['smiley'] = (int) $_REQUEST['smiley'];
            $context['current_smiley'] = getSmiley($_REQUEST['smiley']);
            $context['current_smiley']['code'] = htmlspecialchars($context['current_smiley']['code'], ENT_COMPAT, 'UTF-8');
            $context['current_smiley']['filename'] = htmlspecialchars($context['current_smiley']['filename'], ENT_COMPAT, 'UTF-8');
            $context['current_smiley']['description'] = htmlspecialchars($context['current_smiley']['description'], ENT_COMPAT, 'UTF-8');
            if (isset($context['filenames'][strtolower($context['current_smiley']['filename'])])) {
                $context['filenames'][strtolower($context['current_smiley']['filename'])]['selected'] = true;
            }
        }
    }