qa-wysiwyg-ajax.php 2.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-plugin/wysiwyg-editor/qa-wysiwyg-editor.php
	Description: Editor module class for WYSIWYG editor plugin


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/


class qa_wysiwyg_ajax
{
	public function match_request($request)
	{
		return $request == 'wysiwyg-editor-ajax';
	}

31
	// Fix path to WYSIWYG editor smileys
32 33
	public function process_request($request)
	{
34
		require_once QA_INCLUDE_DIR.'qa-app-posts.php';
35

36
		// smiley replacement regexes
37
		$rxSearch = '<(img|a)([^>]+)(src|href)="([^"]+)/wysiwyg-editor/plugins/smiley/images/([^"]+)"';
38 39 40
		$rxReplace = '<$1$2$3="$4/wysiwyg-editor/ckeditor/plugins/smiley/images/$5"';

		qa_suspend_event_reports(true); // avoid infinite loop
41

42 43 44 45 46 47
		// prevent race conditions
		$locks = array('posts', 'categories', 'users', 'users AS lastusers', 'userpoints', 'words', 'titlewords', 'contentwords', 'tagwords', 'words AS x', 'posttags', 'options');
		foreach ($locks as &$tbl)
			$tbl = '^'.$tbl.' WRITE';
		qa_db_query_sub('LOCK TABLES ' . implode(',', $locks));

48 49 50 51 52 53 54
		$sql =
			'SELECT postid, title, content FROM ^posts WHERE format="html" ' .
			'AND content LIKE "%/wysiwyg-editor/plugins/smiley/images/%" ' .
			'AND content RLIKE \'' . $rxSearch . '\' ' .
			'LIMIT 5';
		$result = qa_db_query_sub($sql);

55 56
		$numPosts = 0;
		while (($post=qa_db_read_one_assoc($result, true)) !== null) {
57
			$newcontent = preg_replace("#$rxSearch#", $rxReplace, $post['content']);
58
			qa_post_set_content($post['postid'], $post['title'], $newcontent);
59
			$numPosts++;
60
		}
61 62 63 64 65

		qa_db_query_raw('UNLOCK TABLES');
		qa_suspend_event_reports(false);

		echo $numPosts;
66 67
	}
}