コード例 #1
0
ファイル: scan.php プロジェクト: cptpike/linuxhardwareguide
 // end of autocreate skipping in debug mode
 $css_id = "col-hw";
 if ($show_public_profile == 1) {
     $css_id = "col-hw-pub";
 }
 echo "\n                        <tr>\n                        <td id=\"" . $css_id . "\">\n\n                        {$logo}\n\n\n                        ";
 $otitle = $title;
 # CPUs
 if ($scantype == "cpu") {
     $title = lhg_get_short_title(lhg_clean_cpu_title($otitle));
     $meta_info = lhg_get_title_metas(lhg_clean_cpu_title($otitle));
 }
 # CPUs
 if ($scantype == "drive") {
     $title = lhg_get_short_title(lhg_clean_drive_title($otitle));
     $meta_info = lhg_get_title_metas(lhg_clean_drive_title($otitle));
 }
 # USB devices
 if ($usbid != "" && $scantype != "mainboard") {
     $title = lhg_get_short_title(lhg_clean_usb_title($otitle));
     $meta_info = lhg_get_title_metas(lhg_clean_usb_title($otitle));
     $scantype = "usb";
 }
 if ($title == "") {
     $title = "(No identifier found)";
 }
 #$title = "Scantype: $scantype".$title;
 print '<div class="subscribe-hwtext">';
 # use full product name if available
 if ($full_title != "") {
     $title = $full_title;
コード例 #2
0
function lhg_create_drive_article($title, $sid, $id)
{
    global $lhg_price_db;
    global $dmesg_content_from_library;
    global $lang;
    # article creation should be limited to .com server
    if ($lang == "de") {
        error_log("Article creation on .de server should not happen. ID: {$id}");
        return;
    }
    # check 1:
    # see if article was already created based on article ID
    $sql = "SELECT created_postid FROM `lhghwscans` WHERE id = \"" . $id . "\" ";
    $created_id = $lhg_price_db->get_var($sql);
    if ($created_id != 0) {
        return $created_id;
    }
    $taglist = array(584);
    if ($dmesg_content_from_library == "") {
        $url = "http://library.linux-hardware-guide.com/showdata.php?sid=" . $sid . "&file=dmesg.txt";
        #extract relevant dmesg output
        $dmesg_content_from_library = file_get_contents($url);
        #$pos = strpos($content, $title);
        #$line = substr($content,$pos,300);
        #print "dmesgline: ".$line."<br>";
    }
    $dmesg = explode("\n", $dmesg_content_from_library);
    #get full dmesg line
    #error_log("Title: $title");
    $title_regexp = trim($title);
    $title_regexp = str_replace("/", ".", $title_regexp);
    $title_regexp = str_replace("-", ".", $title_regexp);
    $dmesg_lines = preg_grep("/" . $title_regexp . "/i", $dmesg);
    if (count($dmesg_lines) > 0) {
        foreach ($dmesg_lines as $dmesg_line) {
            $full_title = $dmesg_line;
        }
    }
    #error_log("Full Title: $full_title");
    # ATA drive
    if (strpos($title, "ATA ") !== false) {
        # 1. get HDD name
        $dmesg_lines = preg_grep("/" . $title . "/i", $dmesg);
        foreach ($dmesg_lines as $dmesg_line) {
            $strpos = strpos($dmesg_line, "ATA ");
            if ($strpos > 0) {
                $searchstring0 = substr($dmesg_line, $strpos + 4);
                $tmp = explode(" ", trim($searchstring0));
                $searchstring = $tmp[0];
                break;
            }
            /*  # In case of USB storage we have no ATA string
                    	$strpos_usbstorage = strpos($dmesg_line,"ANSI: 6");
                    	$strpos_usb_start = strpos($dmesg_line,"Direct-Access");
            	        if ($strpos_usbstorage > 0) {
                    		$searchstring0 = substr($dmesg_line, $strpos_usb_start+14);
            	                $tmp = explode( " ", trim( $searchstring0 ) );
            	                $searchstring = $tmp[0];
                                    break;
            		}
                            */
        }
        # 2. get scsi ID
        $dmesg_lines_array = preg_grep("/scsi(.*)" . $searchstring . "/i", $dmesg);
        foreach ($dmesg_lines_array as $line) {
            $dmesg_line = $line;
        }
        #$dmesg_line = $dmesg_lines_array[0];
        #print "dmla:<br>";
        #var_dump($dmesg_lines_array);
        #print "dml:<br>";
        #var_dump($dmesg_line);
        preg_match("/scsi(.*)[0-9]\\:[0-9]\\:[0-9]\\:[0-9]/i", $dmesg_line, $scsi_match_array);
        preg_match("/[0-9]\\:[0-9]\\:[0-9]\\:[0-9]/i", $dmesg_line, $scsi_nr_array);
        $scsi_nr = $scsi_nr_array[0];
        #error_log("dmesg_line: $dmesg_line");
        #print "scsiNR: $scsi_nr --- $dmesg_line<br>";
        #var_dump($scsi_nr);
        #print "dmesg_line: $dmesg_line<br>";
        #var_dump($dmesg_lines);
        # 3. get ata ID
        $ata_lines_array = preg_grep("/ata[0-9].[0-9][0-9](.*)" . $searchstring . "/i", $dmesg);
        foreach ($ata_lines_array as $line) {
            $ata_line = $line;
        }
        preg_match("/ata[0-9].[0-9][0-9]/i", $ata_line, $ata_nr_array);
        $ata_nr = $ata_nr_array[0];
        #print "ATANR: $ata_nr<br>";
        # 4. get device file name
        $device_array = preg_grep("/sd " . $scsi_nr . "/i", $dmesg);
        foreach ($device_array as $line) {
            #error_log("DL: $line");
            # found line with [s..] definition. Remember and leave loop
            if (preg_match("/\\[s..\\]/i", $line) == 1) {
                $device_line = $line;
                #error_log("Found: $line");
                break;
            }
        }
        preg_match("/\\[s..\\]/i", $device_line, $device_name_array);
        $device_name = $device_name_array[0];
        $device_name_raw = substr($device_name, 1, -1);
        #print "DNAme RAW:".$device_name_raw."<br>";
        #var_dump($device_array);
        #          error_log ("
        #1: $ata_nr<br>
        #2: $device_name<br>
        #3: $device_name_raw<br>
        #4: ".substr($ata_nr,0,4)."<br>
        #5: $scsi_nr<br>")
        #;
        # 5. extract all relevant lines
        $dmesg_outputs = preg_grep("/(" . $ata_nr . "|\\[" . $device_name_raw . "\\]|" . $device_name_raw . "|" . substr($ata_nr, 0, 4) . "|" . $scsi_nr . ")/i", $dmesg);
        $clean_dmesg_outputs = array();
        foreach ($dmesg_outputs as $dmesg_output) {
            #$tmp = explode("] ", $dmesg_output);
            if (strpos($dmesg_output, "Stopping disk") !== false) {
                break;
            }
            array_push($clean_dmesg_outputs, substr($dmesg_output, 15));
        }
        # if more than 100 lines were found something probably went wrong. If not, no use in adding so many lines of log output to post
        if (count($clean_dmesg_outputs) > 100) {
            $clean_dmesg_output = "Error: too many dmesg files found";
        }
        $dmesgOutput = join("\n", $clean_dmesg_outputs);
    }
    # CD-ROM drive
    if (strpos($full_title, "CD-ROM ") !== false) {
        # 1. get scsi ID
        $dmesg_lines_array = preg_grep("/" . $title . "/i", $dmesg);
        foreach ($dmesg_lines_array as $line) {
            $dmesg_line = $line;
        }
        #$dmesg_line = $dmesg_lines_array[0];
        #print "dmla:<br>";
        #var_dump($dmesg_lines_array);
        #print "dml:<br>";
        #var_dump($dmesg_line);
        preg_match("/scsi(.*)[0-9]\\:[0-9]\\:[0-9]\\:[0-9]/i", $dmesg_line, $scsi_match_array);
        preg_match("/[0-9]\\:[0-9]\\:[0-9]\\:[0-9]/i", $dmesg_line, $scsi_nr_array);
        $scsi_nr = $scsi_nr_array[0];
        #error_log("SCSI NR: $scsi_nr");
        # get ata ID based on last identifier of drive (should be drive id)
        #error_log("TRG: $title_regexp");
        $tmp = explode(" ", $title_regexp);
        $tmp2 = array_reverse($tmp);
        $ata_lines_array = preg_grep("/ata[0-9].[0-9][0-9](.*)" . $tmp2[0] . "/i", $dmesg);
        foreach ($ata_lines_array as $line) {
            $ata_line = $line;
        }
        preg_match("/ata[0-9].[0-9][0-9]/i", $ata_line, $ata_nr_array);
        $ata_nr = $ata_nr_array[0];
        if ($ata_nr == "") {
            $ata_nr = "ERROR_NO_ATA_NR_FOUND";
        }
        #error_log("ATANR: $ata_line -> $ata_nr");
        # extract all relevant lines
        $dmesg_outputs = preg_grep("/(" . $ata_nr . "|cdrom\\: |" . $scsi_nr . ")/i", $dmesg);
        $oldline = "";
        $clean_dmesg_outputs = array();
        foreach ($dmesg_outputs as $dmesg_output) {
            #$tmp = explode("] ", $dmesg_output);
            # check if this line is a repetition
            if (substr($dmesg_output, 15) != $oldline) {
                array_push($clean_dmesg_outputs, substr($dmesg_output, 15));
            }
            $oldline = substr($dmesg_output, 15);
        }
        $dmesgOutput = join("\n", $clean_dmesg_outputs);
        #error_log("OUT: $dmesgOutput");
        # Get Identifier String from Dmesg output
        $drive_id_array = preg_grep("/ ANSI: 5/i", $clean_dmesg_outputs);
        foreach ($drive_id_array as $line) {
            $drive_id = $line;
        }
        $pos = strpos($drive_id, "0: ");
        $drive_identifier = substr($drive_id, $pos + 3);
        #error_log("DriveID: $drive_identifier");
        #lhg_create_new_DB_entry_post ( $newPostID, "drive", $drive_identifier );
    }
    $find = array_keys($dmesg, $title);
    $keyword = $title;
    foreach ($dmesg as $key => $arrayItem) {
        if (stristr($arrayItem, $keyword)) {
            #error_log( "Key: $key - ".$dmesg[$key] );
            $foundkey = $key;
            break;
        }
    }
    list($null, $drive_identifier) = explode("0:0: ", $dmesg[$foundkey]);
    #print "ID: $drive_identifier";
    #var_dump($find);
    #print "F0: ".$dmesg[$find[0]]."<br>";
    #print "F1: ".$dmesg[$find[1]]."<br>";
    #print "<br>Dump:".var_dump($dmesg)."<br>";
    #$cpu0 = $cpus[0];
    #print "URL: $url<br>";
    #print "cont: <pre>".$cpu0."</pre>";
    $article = '[lhg_drive_intro]
[code lang="plain" title="dmesg"]
' . $dmesgOutput . '
[/code]
';
    # The scsi line shows a truncated title, if the device name is too long.
    # For the title we will need the full title
    $title = lhg_search_long_title($title, $dmesg);
    #if (preg_match('/Athlon(tm) II/',$title)) array_push ( $taglist , 880);
    $title = wp_strip_all_tags($title);
    $title = lhg_clean_drive_title($title);
    #print "Article creation started";
    $new_taglist = lhg_taglist_by_title($title);
    $taglist = array_merge($taglist, $new_taglist);
    $tagstring = lhg_convert_tag_array_to_string($taglist);
    # set category
    #$category = 478;
    $category_array = lhg_category_by_title($title);
    array_push($category_array, 478);
    #print "<br>Title: $title <br> ScanID: $sid<br>";
    #$title = $title;
    # Do nothing, if article already exists
    $page = get_page_by_title("<!--:us-->" . $title . "<!--:-->");
    if ($page->ID != "" && is_page($page->ID)) {
        return $page->ID;
    }
    $page = get_page_by_title($title);
    if ($page->ID != "" && is_page($page->ID)) {
        return $page->ID;
    }
    $myPost = array('post_status' => 'draft', 'post_content' => "<!--:us-->" . $article . "<!--:-->", 'post_type' => 'post', 'post_author' => 1, 'post_title' => "<!--:us-->" . $title . "<!--:-->", 'post_category' => $category_array, 'tags_input' => $tagstring);
    global $wpdb;
    #$post_if = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '$title'");
    #print "PI: ".$post_if;
    #$post_if2 = $wpdb->get_var("SELECT count(post_title) FROM $wpdb->posts WHERE post_title like '%".$title."%' ");
    $post_if2 = $wpdb->get_var("SELECT count(post_title) FROM {$wpdb->posts} WHERE post_title like '%" . $title . "%' AND post_status = 'draft' ");
    #print "PI2: ".$post_if2;
    $sql = "SELECT created_postid FROM `lhghwscans` WHERE id = \"" . $id . "\" ";
    $created_id = $lhg_price_db->get_var($sql);
    #print "ID: $id<br>
    #found: $post_if2<br>
    #CreatedID: $created_id<br>";
    # if article was created or title already exists
    if ($post_if2 > 0 or $created_id != 0) {
        #print "Title exists".$title."<br>";
        #$newPostID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_title like '%".$title."%' AND post_status = 'draft' ");
        if ($created_id != 0) {
            $newPostID = $created_id;
        }
        if ($created_id == 0) {
            $newPostID = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_title like '%" . $title . "%' ");
        }
        # store created_id for already existing articles
        if ($created_id == 0) {
            $sql = "UPDATE `lhghwscans` SET created_postid = \"" . $newPostID . "\" WHERE id = \"" . $id . "\" ";
            $result = $lhg_price_db->query($sql);
        }
    } else {
        //-- Create the new post
        #print "new article";
        $newPostID = wp_insert_post($myPost);
        #store id
        $sql = "UPDATE `lhghwscans` SET created_postid = \"" . $newPostID . "\" WHERE id = \"" . $id . "\" ";
        $result = $lhg_price_db->query($sql);
    }
    #print "<br>done<br>";
    #error_log("PID: $newPostID -> $drive_identifier");
    if ($drive_identifier != "") {
        lhg_create_new_DB_entry_post($newPostID, "drive", $drive_identifier);
    }
    # get Amazon ID, if available
    $amzid = lhg_get_AMZ_ID_from_scan($sid, "drive", $id);
    # set Amazon ID
    $key = "amazon-product-single-asin";
    $value = $amzid;
    if (get_post_meta($newPostID, $key, FALSE)) {
        //if the custom field already has a value
        update_post_meta($newPostID, $key, $value);
    } else {
        //if the custom field doesn't have a value
        add_post_meta($newPostID, $key, $value);
    }
    return $newPostID;
}