Thursday, April 4, 2013

ckeditor jquery javascript php

https://docs.google.com/file/d/0B5nZNPW48dpFSE5LMk05V2VWSzA/edit?usp=sharing
 
<?php
$ck_id = "ck_editor".time();
if(!isset($ck_className)) {
    $ck_className = time();
}
if(!isset($ck_content)) {
    $ck_content = "";
}
if(!isset($ck_name)) {
    $ck_name = "ck_name";
}

?>
<textarea name="<?php echo $ck_name; ?>
class="<?php echo $ck_className; ?>
id="<?php echo $ck_id; ?>"><?php echo $ck_content; ?></textarea>
 
<script type="text/javascript">
    for (var i in CKEDITOR.instances) {
        if(CKEDITOR.instances[i]) {
            <?php if(isset($replaceAll) && $replaceAll == true) { ?>CKEDITOR.remove(CKEDITOR.instances[i]); <?php } ?>
        }
    }
    CKEDITOR.replace( '<?php echo $ck_id; ?>',
        {
            pasteFromWordRemoveFontStyles : false,
   fullPage : false,
            removePlugins : 'elementspath',
            height : 300,
            toolbar :
                [
                    {
                        name: 'styles',
                        items : [ 'Format','Font','FontSize' ]
                    },
                    {
                        name: 'basicstyles',
                        items : [ 'Bold','Italic','Underline','Strike']
                    },
                    {
                        name: 'paragraph',
                        items : [ 'NumberedList','BulletedList','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock' ]
                    },
                    {
                        name: 'clipboard',
                        items : [ 'Undo','Redo', 'imageUploader' ]
                    },
                    {
                        name: 'tableoperations',
                        items : [ '-', 'Table', 'TableInsertRowBefore', 'TableInsertRowAfter', 'TableRowDelete', '-',
                            'TableInsertColumnBefore', 'TableInsertColumnAfter', 'TableColumnDelete', '-', 'TableCellMerge',
                            'TableCellSplitVertical', 'TableCellSplitHorizontal', 'TableDeleteTable' ]
                    },
                    {
                        name: 'spellcheck',
                        items: [ 'jQuerySpellChecker' ]
                    }
                ]
        });
</script>
 
 
 
 
 
 
 
Extra plugins made by me "imageUploader" includes in config.js file 
CKEDITOR.editorConfig = function( config )
{
 // Define changes to default configuration here. For example:
 //config.language = 'fr';
 config.uiColor = '#FCFFEB';
 config.enterMode = CKEDITOR.ENTER_BR;
 config.extraPlugins = "imageUploader,tableoperations";
 config.pasteFromWordRemoveFontStyles = false;
 config.disableNativeSpellChecker = false; /* default spell checking */ 
};
Make a folder "imageUploader" in plugins folder, 
create a file icon.gif and plugin.js
 
PLUGIN.JS 
(function(){
    //Section 1 : Code to execute when the toolbar button is pressed
    var editorCustom;
    var a = {
        exec:function(editor){
            editorCustom = editor;
            var theSelectedText = "";
            var mySelection = editor.getSelection();

            if (CKEDITOR.env.ie) {
                mySelection.unlock(true);
                theSelectedText = mySelection.getNative().createRange().text;
            } else {
                theSelectedText = mySelection.getNative();
            }

            var backgroundHtmlStyle = " style='background-color: white; ";
            backgroundHtmlStyle += " height: " + $(window).height() + "px; ";
            backgroundHtmlStyle += " width: " + $(document).width() + "px; ";
            backgroundHtmlStyle += " left: 0; opacity: 0.5; position: fixed; top: 0; z-index: 500; ' ";
            var backgroundHtml = "<div class='ck-editor-image-upload-background' "+backgroundHtmlStyle+"></div>";
            $("body").append(backgroundHtml);

            var uploadFormHtml = "<div class='ck-editor-image-upload'><form id='ck-editor-image-upload-form' accept='utf-8' method=\"post\" enctype=\"multipart/form-data\" accept-charset=\"utf-8\" > ";
            uploadFormHtml += "<div><span class='1'>Upload Image File</span><span class='2'></span><input type=\"file\" name=\"file\" class=\"file\"/></div>";
            uploadFormHtml += "</form></div>";

            jQuery.dialogFromHTML("Insert Image", uploadFormHtml, {width: 400});

            $(".ck-editor-image-upload").find(".file").bind("change", function() {
                $("#ck-editor-image-upload-form").attr("action", BASE_URL + "custom_image/upload_ck_editor_image");
                $("#ck-editor-image-upload-form").ajaxForm({
                    beforeSubmit: function() {
                        jQuery.showBackgroundWorkingState();
                    },
                    success: function(data) {
                        if(data != "error" && data != "unsupported") {
                            var imageLocation = CK_EDITOR_IMAGE_DIR + data;
                            var imageHtml = "<img src='"+imageLocation+"' alt='"+data+"'/>";

                            try {
                                editorCustom.insertHtml(imageHtml);
                            } catch (ex) {
                                var content = editorCustom.getData() + "<p>" + imageHtml + "</p>";
                                editorCustom.setData(content);
                            }
                        } else {
                            jQuery.showWarning("File uploading error, please try again");
                        }
                        jQuery.closeAllDialog();
                        jQuery.hideBackgroundWorkingState();
                        jQuery(".ck-editor-image-upload-background").remove();
                    },
                    error: function() {
                        jQuery.showWarning("File uploading error, please try again");
                        jQuery.closeAllDialog();
                        jQuery.hideBackgroundWorkingState();
                        jQuery(".ck-editor-image-upload-background").remove();
                    }
                }).submit();
            });
        }
    },
    //Section 2 : Create the button and add the functionality to it
    b='imageUploader';
    CKEDITOR.plugins.add(b,{
        init:function(editor){
            editor.addCommand(b,a);
            editor.ui.addButton('imageUploader',{
                label:'Insert Image',
                icon: this.path + 'icon.gif',
                command:b
            });
        }
    });
})();

Monday, April 1, 2013

Configure apache virtual host


Make a entry to: C:\Windows\System32\drivers\etc\hosts
-----------------------------------------------------------
127.0.0.1    test.com
127.0.0.1    www.test.com

File Location:
-----------------------------------------------------------
C:\xampp\apache\conf\extra\httpd-vhosts

File Contents:

<VirtualHost 127.0.0.1:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
</VirtualHost>

<VirtualHost 127.0.0.1:80>
    ServerAdmin pritomkucse@gmail.com
    DocumentRoot "D:/src/test"
    ServerName test.com
    ServerAlias www.test.com
    ErrorLog "logs/test.error.log"   
    CustomLog "logs/test.custom.log"
    <Directory "D:/src/test">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

Saturday, March 30, 2013

jquery find element contains text

eq expects a numerical index to only return a single row. If you want to match a td by its contents, you have to use the :contains selector. Saying "it doesn't work" and throwing it away is not the right approach to the problem, as the selector is (most likely) not at fault (Do note its case sensitive, which might be it...)
Anyhow, if you have a table like this:
<table>
  <tr>
    <td>Hello</td>
    <td>World</td>
  </tr>
  <tr>
    <td>World</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
</table>
This jQuery code:
$(function() {
    $("td:contains('Hello')").css('color','red');
});
Will turn all cells with "Hello" to red. Demo.
If you need a case insensitive match, you could do this, using the filter function:
$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase().indexOf(search) != -1;
    }).css('color','red');
});
If you need to match the exact contents of the cell, you could use something similar to the above:
$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase() == search;
    }).css('color','red');
});
The above is case insensitive (by turning both the search and the contents to lower case when comparing) otherwise you can just remove those if you want case sensitivity. Demo.

Friday, March 1, 2013

php authentication basic username and password

index.php


<?php 
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="Need Authentication"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Need Authentication';
    exit;
} else {
    if(empty($_SERVER["PHP_AUTH_USER"]) || $_SERVER["PHP_AUTH_USER"] != "apm"
        || empty($_SERVER["PHP_AUTH_PW"]) || $_SERVER["PHP_AUTH_PW"] != "gh3412") {
        header('WWW-Authenticate: Basic realm="Authentication Error"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Authentication Error';
        exit;
    }
}
?>

 
<?php 
$ch = curl_init();

if(is_array($get_variables))
{
    $get_variables = '?' . str_replace('&amp;','&',urldecode(http_build_query($get_variables)));
}
else
{
    $get_variables = null;
}
curl_setopt($ch, CURLOPT_URL, "http://dom.com/index.php" . $get_variables);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'adm:pwd3012');
$headers[0] = "Authorization: Basic " . base64_encode("apm:gh3412");

if($post_variables == null || !is_array($post_variables)) {
    $post_variables = array();
}
if(is_array($post_variables))
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
}

if(is_array($headers))
{
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
}

$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);
?> 
 

Sending a username and password with PHP file_get_contents()

In the example below, $url is the web address you want to get data from, and $username and $password are the login credentials.
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);
If the login details were incorrect, or you were attempting to access a password protected location and didn't pass any credentials at all, you'll see an error like this:
Warning: file_get_contents(...): failed to open stream: HTTP request failed! 
HTTP/1.1 401 Authorization Required in ... on line 4

http://www.electrictoolbox.com/php-file-get-contents-sending-username-password/ 

How to find if an array contains a specific string in JavaScript/jQuery?

var cat = [];
cat.push('1');
cat.push('2');
cat.push('3');

var found = $.inArray('1', cat) > -1;